amdgpu_mn.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright 2014 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 <christian.koenig@amd.com>
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <linux/interval_tree.h>
  34. #include <drm/drmP.h>
  35. #include <drm/drm.h>
  36. #include "amdgpu.h"
  37. struct amdgpu_mn {
  38. /* constant after initialisation */
  39. struct amdgpu_device *adev;
  40. struct mm_struct *mm;
  41. struct mmu_notifier mn;
  42. /* only used on destruction */
  43. struct work_struct work;
  44. /* protected by adev->mn_lock */
  45. struct hlist_node node;
  46. /* objects protected by lock */
  47. struct rw_semaphore lock;
  48. struct rb_root_cached objects;
  49. struct mutex read_lock;
  50. atomic_t recursion;
  51. };
  52. struct amdgpu_mn_node {
  53. struct interval_tree_node it;
  54. struct list_head bos;
  55. };
  56. /**
  57. * amdgpu_mn_destroy - destroy the rmn
  58. *
  59. * @work: previously sheduled work item
  60. *
  61. * Lazy destroys the notifier from a work item
  62. */
  63. static void amdgpu_mn_destroy(struct work_struct *work)
  64. {
  65. struct amdgpu_mn *rmn = container_of(work, struct amdgpu_mn, work);
  66. struct amdgpu_device *adev = rmn->adev;
  67. struct amdgpu_mn_node *node, *next_node;
  68. struct amdgpu_bo *bo, *next_bo;
  69. mutex_lock(&adev->mn_lock);
  70. down_write(&rmn->lock);
  71. hash_del(&rmn->node);
  72. rbtree_postorder_for_each_entry_safe(node, next_node,
  73. &rmn->objects.rb_root, it.rb) {
  74. list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
  75. bo->mn = NULL;
  76. list_del_init(&bo->mn_list);
  77. }
  78. kfree(node);
  79. }
  80. up_write(&rmn->lock);
  81. mutex_unlock(&adev->mn_lock);
  82. mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
  83. kfree(rmn);
  84. }
  85. /**
  86. * amdgpu_mn_release - callback to notify about mm destruction
  87. *
  88. * @mn: our notifier
  89. * @mn: the mm this callback is about
  90. *
  91. * Shedule a work item to lazy destroy our notifier.
  92. */
  93. static void amdgpu_mn_release(struct mmu_notifier *mn,
  94. struct mm_struct *mm)
  95. {
  96. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  97. INIT_WORK(&rmn->work, amdgpu_mn_destroy);
  98. schedule_work(&rmn->work);
  99. }
  100. /**
  101. * amdgpu_mn_lock - take the write side lock for this mn
  102. */
  103. void amdgpu_mn_lock(struct amdgpu_mn *mn)
  104. {
  105. if (mn)
  106. down_write(&mn->lock);
  107. }
  108. /**
  109. * amdgpu_mn_unlock - drop the write side lock for this mn
  110. */
  111. void amdgpu_mn_unlock(struct amdgpu_mn *mn)
  112. {
  113. if (mn)
  114. up_write(&mn->lock);
  115. }
  116. /**
  117. * amdgpu_mn_read_lock - take the rmn read lock
  118. *
  119. * @rmn: our notifier
  120. *
  121. * Take the rmn read side lock.
  122. */
  123. static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
  124. {
  125. mutex_lock(&rmn->read_lock);
  126. if (atomic_inc_return(&rmn->recursion) == 1)
  127. down_read_non_owner(&rmn->lock);
  128. mutex_unlock(&rmn->read_lock);
  129. }
  130. /**
  131. * amdgpu_mn_read_unlock - drop the rmn read lock
  132. *
  133. * @rmn: our notifier
  134. *
  135. * Drop the rmn read side lock.
  136. */
  137. static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
  138. {
  139. if (atomic_dec_return(&rmn->recursion) == 0)
  140. up_read_non_owner(&rmn->lock);
  141. }
  142. /**
  143. * amdgpu_mn_invalidate_node - unmap all BOs of a node
  144. *
  145. * @node: the node with the BOs to unmap
  146. *
  147. * We block for all BOs and unmap them by move them
  148. * into system domain again.
  149. */
  150. static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
  151. unsigned long start,
  152. unsigned long end)
  153. {
  154. struct amdgpu_bo *bo;
  155. long r;
  156. list_for_each_entry(bo, &node->bos, mn_list) {
  157. if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
  158. continue;
  159. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  160. true, false, MAX_SCHEDULE_TIMEOUT);
  161. if (r <= 0)
  162. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  163. amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
  164. }
  165. }
  166. /**
  167. * amdgpu_mn_invalidate_range_start - callback to notify about mm change
  168. *
  169. * @mn: our notifier
  170. * @mn: the mm this callback is about
  171. * @start: start of updated range
  172. * @end: end of updated range
  173. *
  174. * We block for all BOs between start and end to be idle and
  175. * unmap them by move them into system domain again.
  176. */
  177. static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
  178. struct mm_struct *mm,
  179. unsigned long start,
  180. unsigned long end)
  181. {
  182. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  183. struct interval_tree_node *it;
  184. /* notification is exclusive, but interval is inclusive */
  185. end -= 1;
  186. amdgpu_mn_read_lock(rmn);
  187. it = interval_tree_iter_first(&rmn->objects, start, end);
  188. while (it) {
  189. struct amdgpu_mn_node *node;
  190. node = container_of(it, struct amdgpu_mn_node, it);
  191. it = interval_tree_iter_next(it, start, end);
  192. amdgpu_mn_invalidate_node(node, start, end);
  193. }
  194. }
  195. /**
  196. * amdgpu_mn_invalidate_range_end - callback to notify about mm change
  197. *
  198. * @mn: our notifier
  199. * @mn: the mm this callback is about
  200. * @start: start of updated range
  201. * @end: end of updated range
  202. *
  203. * Release the lock again to allow new command submissions.
  204. */
  205. static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
  206. struct mm_struct *mm,
  207. unsigned long start,
  208. unsigned long end)
  209. {
  210. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  211. amdgpu_mn_read_unlock(rmn);
  212. }
  213. static const struct mmu_notifier_ops amdgpu_mn_ops = {
  214. .release = amdgpu_mn_release,
  215. .invalidate_range_start = amdgpu_mn_invalidate_range_start,
  216. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  217. };
  218. /**
  219. * amdgpu_mn_get - create notifier context
  220. *
  221. * @adev: amdgpu device pointer
  222. *
  223. * Creates a notifier context for current->mm.
  224. */
  225. struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
  226. {
  227. struct mm_struct *mm = current->mm;
  228. struct amdgpu_mn *rmn;
  229. int r;
  230. mutex_lock(&adev->mn_lock);
  231. if (down_write_killable(&mm->mmap_sem)) {
  232. mutex_unlock(&adev->mn_lock);
  233. return ERR_PTR(-EINTR);
  234. }
  235. hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm)
  236. if (rmn->mm == mm)
  237. goto release_locks;
  238. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  239. if (!rmn) {
  240. rmn = ERR_PTR(-ENOMEM);
  241. goto release_locks;
  242. }
  243. rmn->adev = adev;
  244. rmn->mm = mm;
  245. rmn->mn.ops = &amdgpu_mn_ops;
  246. init_rwsem(&rmn->lock);
  247. rmn->objects = RB_ROOT_CACHED;
  248. mutex_init(&rmn->read_lock);
  249. atomic_set(&rmn->recursion, 0);
  250. r = __mmu_notifier_register(&rmn->mn, mm);
  251. if (r)
  252. goto free_rmn;
  253. hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm);
  254. release_locks:
  255. up_write(&mm->mmap_sem);
  256. mutex_unlock(&adev->mn_lock);
  257. return rmn;
  258. free_rmn:
  259. up_write(&mm->mmap_sem);
  260. mutex_unlock(&adev->mn_lock);
  261. kfree(rmn);
  262. return ERR_PTR(r);
  263. }
  264. /**
  265. * amdgpu_mn_register - register a BO for notifier updates
  266. *
  267. * @bo: amdgpu buffer object
  268. * @addr: userptr addr we should monitor
  269. *
  270. * Registers an MMU notifier for the given BO at the specified address.
  271. * Returns 0 on success, -ERRNO if anything goes wrong.
  272. */
  273. int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  274. {
  275. unsigned long end = addr + amdgpu_bo_size(bo) - 1;
  276. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  277. struct amdgpu_mn *rmn;
  278. struct amdgpu_mn_node *node = NULL;
  279. struct list_head bos;
  280. struct interval_tree_node *it;
  281. rmn = amdgpu_mn_get(adev);
  282. if (IS_ERR(rmn))
  283. return PTR_ERR(rmn);
  284. INIT_LIST_HEAD(&bos);
  285. down_write(&rmn->lock);
  286. while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
  287. kfree(node);
  288. node = container_of(it, struct amdgpu_mn_node, it);
  289. interval_tree_remove(&node->it, &rmn->objects);
  290. addr = min(it->start, addr);
  291. end = max(it->last, end);
  292. list_splice(&node->bos, &bos);
  293. }
  294. if (!node) {
  295. node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
  296. if (!node) {
  297. up_write(&rmn->lock);
  298. return -ENOMEM;
  299. }
  300. }
  301. bo->mn = rmn;
  302. node->it.start = addr;
  303. node->it.last = end;
  304. INIT_LIST_HEAD(&node->bos);
  305. list_splice(&bos, &node->bos);
  306. list_add(&bo->mn_list, &node->bos);
  307. interval_tree_insert(&node->it, &rmn->objects);
  308. up_write(&rmn->lock);
  309. return 0;
  310. }
  311. /**
  312. * amdgpu_mn_unregister - unregister a BO for notifier updates
  313. *
  314. * @bo: amdgpu buffer object
  315. *
  316. * Remove any registration of MMU notifier updates from the buffer object.
  317. */
  318. void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  319. {
  320. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  321. struct amdgpu_mn *rmn;
  322. struct list_head *head;
  323. mutex_lock(&adev->mn_lock);
  324. rmn = bo->mn;
  325. if (rmn == NULL) {
  326. mutex_unlock(&adev->mn_lock);
  327. return;
  328. }
  329. down_write(&rmn->lock);
  330. /* save the next list entry for later */
  331. head = bo->mn_list.next;
  332. bo->mn = NULL;
  333. list_del_init(&bo->mn_list);
  334. if (list_empty(head)) {
  335. struct amdgpu_mn_node *node;
  336. node = container_of(head, struct amdgpu_mn_node, bos);
  337. interval_tree_remove(&node->it, &rmn->objects);
  338. kfree(node);
  339. }
  340. up_write(&rmn->lock);
  341. mutex_unlock(&adev->mn_lock);
  342. }