amdgpu_bo_list.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Christian König <deathsimple@vodafone.de>
  29. */
  30. #include <drm/drmP.h>
  31. #include "amdgpu.h"
  32. #include "amdgpu_trace.h"
  33. #define AMDGPU_BO_LIST_MAX_PRIORITY 32u
  34. #define AMDGPU_BO_LIST_NUM_BUCKETS (AMDGPU_BO_LIST_MAX_PRIORITY + 1)
  35. static void amdgpu_bo_list_free_rcu(struct rcu_head *rcu)
  36. {
  37. struct amdgpu_bo_list *list = container_of(rcu, struct amdgpu_bo_list,
  38. rhead);
  39. kvfree(list);
  40. }
  41. static void amdgpu_bo_list_free(struct kref *ref)
  42. {
  43. struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,
  44. refcount);
  45. struct amdgpu_bo_list_entry *e;
  46. amdgpu_bo_list_for_each_entry(e, list)
  47. amdgpu_bo_unref(&e->robj);
  48. call_rcu(&list->rhead, amdgpu_bo_list_free_rcu);
  49. }
  50. int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
  51. struct drm_amdgpu_bo_list_entry *info,
  52. unsigned num_entries, struct amdgpu_bo_list **result)
  53. {
  54. unsigned last_entry = 0, first_userptr = num_entries;
  55. struct amdgpu_bo_list_entry *array;
  56. struct amdgpu_bo_list *list;
  57. uint64_t total_size = 0;
  58. size_t size;
  59. unsigned i;
  60. int r;
  61. if (num_entries > (SIZE_MAX - sizeof(struct amdgpu_bo_list))
  62. / sizeof(struct amdgpu_bo_list_entry))
  63. return -EINVAL;
  64. size = sizeof(struct amdgpu_bo_list);
  65. size += num_entries * sizeof(struct amdgpu_bo_list_entry);
  66. list = kvmalloc(size, GFP_KERNEL);
  67. if (!list)
  68. return -ENOMEM;
  69. kref_init(&list->refcount);
  70. list->gds_obj = adev->gds.gds_gfx_bo;
  71. list->gws_obj = adev->gds.gws_gfx_bo;
  72. list->oa_obj = adev->gds.oa_gfx_bo;
  73. array = amdgpu_bo_list_array_entry(list, 0);
  74. memset(array, 0, num_entries * sizeof(struct amdgpu_bo_list_entry));
  75. for (i = 0; i < num_entries; ++i) {
  76. struct amdgpu_bo_list_entry *entry;
  77. struct drm_gem_object *gobj;
  78. struct amdgpu_bo *bo;
  79. struct mm_struct *usermm;
  80. gobj = drm_gem_object_lookup(filp, info[i].bo_handle);
  81. if (!gobj) {
  82. r = -ENOENT;
  83. goto error_free;
  84. }
  85. bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
  86. drm_gem_object_put_unlocked(gobj);
  87. usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
  88. if (usermm) {
  89. if (usermm != current->mm) {
  90. amdgpu_bo_unref(&bo);
  91. r = -EPERM;
  92. goto error_free;
  93. }
  94. entry = &array[--first_userptr];
  95. } else {
  96. entry = &array[last_entry++];
  97. }
  98. entry->robj = bo;
  99. entry->priority = min(info[i].bo_priority,
  100. AMDGPU_BO_LIST_MAX_PRIORITY);
  101. entry->tv.bo = &entry->robj->tbo;
  102. entry->tv.shared = !entry->robj->prime_shared_count;
  103. if (entry->robj->preferred_domains == AMDGPU_GEM_DOMAIN_GDS)
  104. list->gds_obj = entry->robj;
  105. if (entry->robj->preferred_domains == AMDGPU_GEM_DOMAIN_GWS)
  106. list->gws_obj = entry->robj;
  107. if (entry->robj->preferred_domains == AMDGPU_GEM_DOMAIN_OA)
  108. list->oa_obj = entry->robj;
  109. total_size += amdgpu_bo_size(entry->robj);
  110. trace_amdgpu_bo_list_set(list, entry->robj);
  111. }
  112. list->first_userptr = first_userptr;
  113. list->num_entries = num_entries;
  114. trace_amdgpu_cs_bo_status(list->num_entries, total_size);
  115. *result = list;
  116. return 0;
  117. error_free:
  118. while (i--)
  119. amdgpu_bo_unref(&array[i].robj);
  120. kvfree(list);
  121. return r;
  122. }
  123. static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
  124. {
  125. struct amdgpu_bo_list *list;
  126. mutex_lock(&fpriv->bo_list_lock);
  127. list = idr_remove(&fpriv->bo_list_handles, id);
  128. mutex_unlock(&fpriv->bo_list_lock);
  129. if (list)
  130. kref_put(&list->refcount, amdgpu_bo_list_free);
  131. }
  132. int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
  133. struct amdgpu_bo_list **result)
  134. {
  135. rcu_read_lock();
  136. *result = idr_find(&fpriv->bo_list_handles, id);
  137. if (*result && kref_get_unless_zero(&(*result)->refcount)) {
  138. rcu_read_unlock();
  139. return 0;
  140. }
  141. rcu_read_unlock();
  142. return -ENOENT;
  143. }
  144. void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
  145. struct list_head *validated)
  146. {
  147. /* This is based on the bucket sort with O(n) time complexity.
  148. * An item with priority "i" is added to bucket[i]. The lists are then
  149. * concatenated in descending order.
  150. */
  151. struct list_head bucket[AMDGPU_BO_LIST_NUM_BUCKETS];
  152. struct amdgpu_bo_list_entry *e;
  153. unsigned i;
  154. for (i = 0; i < AMDGPU_BO_LIST_NUM_BUCKETS; i++)
  155. INIT_LIST_HEAD(&bucket[i]);
  156. /* Since buffers which appear sooner in the relocation list are
  157. * likely to be used more often than buffers which appear later
  158. * in the list, the sort mustn't change the ordering of buffers
  159. * with the same priority, i.e. it must be stable.
  160. */
  161. amdgpu_bo_list_for_each_entry(e, list) {
  162. unsigned priority = e->priority;
  163. if (!e->robj->parent)
  164. list_add_tail(&e->tv.head, &bucket[priority]);
  165. e->user_pages = NULL;
  166. }
  167. /* Connect the sorted buckets in the output list. */
  168. for (i = 0; i < AMDGPU_BO_LIST_NUM_BUCKETS; i++)
  169. list_splice(&bucket[i], validated);
  170. }
  171. void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
  172. {
  173. kref_put(&list->refcount, amdgpu_bo_list_free);
  174. }
  175. int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
  176. struct drm_amdgpu_bo_list_entry **info_param)
  177. {
  178. const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
  179. const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
  180. struct drm_amdgpu_bo_list_entry *info;
  181. int r;
  182. info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
  183. if (!info)
  184. return -ENOMEM;
  185. /* copy the handle array from userspace to a kernel buffer */
  186. r = -EFAULT;
  187. if (likely(info_size == in->bo_info_size)) {
  188. unsigned long bytes = in->bo_number *
  189. in->bo_info_size;
  190. if (copy_from_user(info, uptr, bytes))
  191. goto error_free;
  192. } else {
  193. unsigned long bytes = min(in->bo_info_size, info_size);
  194. unsigned i;
  195. memset(info, 0, in->bo_number * info_size);
  196. for (i = 0; i < in->bo_number; ++i) {
  197. if (copy_from_user(&info[i], uptr, bytes))
  198. goto error_free;
  199. uptr += in->bo_info_size;
  200. }
  201. }
  202. *info_param = info;
  203. return 0;
  204. error_free:
  205. kvfree(info);
  206. return r;
  207. }
  208. int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  209. struct drm_file *filp)
  210. {
  211. struct amdgpu_device *adev = dev->dev_private;
  212. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  213. union drm_amdgpu_bo_list *args = data;
  214. uint32_t handle = args->in.list_handle;
  215. struct drm_amdgpu_bo_list_entry *info = NULL;
  216. struct amdgpu_bo_list *list, *old;
  217. int r;
  218. r = amdgpu_bo_create_list_entry_array(&args->in, &info);
  219. if (r)
  220. return r;
  221. switch (args->in.operation) {
  222. case AMDGPU_BO_LIST_OP_CREATE:
  223. r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
  224. &list);
  225. if (r)
  226. goto error_free;
  227. mutex_lock(&fpriv->bo_list_lock);
  228. r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
  229. mutex_unlock(&fpriv->bo_list_lock);
  230. if (r < 0) {
  231. goto error_put_list;
  232. }
  233. handle = r;
  234. break;
  235. case AMDGPU_BO_LIST_OP_DESTROY:
  236. amdgpu_bo_list_destroy(fpriv, handle);
  237. handle = 0;
  238. break;
  239. case AMDGPU_BO_LIST_OP_UPDATE:
  240. r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
  241. &list);
  242. if (r)
  243. goto error_free;
  244. mutex_lock(&fpriv->bo_list_lock);
  245. old = idr_replace(&fpriv->bo_list_handles, list, handle);
  246. mutex_unlock(&fpriv->bo_list_lock);
  247. if (IS_ERR(old)) {
  248. r = PTR_ERR(old);
  249. goto error_put_list;
  250. }
  251. amdgpu_bo_list_put(old);
  252. break;
  253. default:
  254. r = -EINVAL;
  255. goto error_free;
  256. }
  257. memset(args, 0, sizeof(*args));
  258. args->out.list_handle = handle;
  259. kvfree(info);
  260. return 0;
  261. error_put_list:
  262. amdgpu_bo_list_put(list);
  263. error_free:
  264. kvfree(info);
  265. return r;
  266. }