amdgpu_mn.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 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, &rmn->objects,
  73. 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_page - callback to notify about mm change
  168. *
  169. * @mn: our notifier
  170. * @mn: the mm this callback is about
  171. * @address: address of invalidate page
  172. *
  173. * Invalidation of a single page. Blocks for all BOs mapping it
  174. * and unmap them by move them into system domain again.
  175. */
  176. static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
  177. struct mm_struct *mm,
  178. unsigned long address)
  179. {
  180. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  181. struct interval_tree_node *it;
  182. amdgpu_mn_read_lock(rmn);
  183. it = interval_tree_iter_first(&rmn->objects, address, address);
  184. if (it) {
  185. struct amdgpu_mn_node *node;
  186. node = container_of(it, struct amdgpu_mn_node, it);
  187. amdgpu_mn_invalidate_node(node, address, address);
  188. }
  189. amdgpu_mn_read_unlock(rmn);
  190. }
  191. /**
  192. * amdgpu_mn_invalidate_range_start - callback to notify about mm change
  193. *
  194. * @mn: our notifier
  195. * @mn: the mm this callback is about
  196. * @start: start of updated range
  197. * @end: end of updated range
  198. *
  199. * We block for all BOs between start and end to be idle and
  200. * unmap them by move them into system domain again.
  201. */
  202. static void amdgpu_mn_invalidate_range_start(struct mmu_notifier *mn,
  203. struct mm_struct *mm,
  204. unsigned long start,
  205. unsigned long end)
  206. {
  207. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  208. struct interval_tree_node *it;
  209. /* notification is exclusive, but interval is inclusive */
  210. end -= 1;
  211. amdgpu_mn_read_lock(rmn);
  212. it = interval_tree_iter_first(&rmn->objects, start, end);
  213. while (it) {
  214. struct amdgpu_mn_node *node;
  215. node = container_of(it, struct amdgpu_mn_node, it);
  216. it = interval_tree_iter_next(it, start, end);
  217. amdgpu_mn_invalidate_node(node, start, end);
  218. }
  219. }
  220. /**
  221. * amdgpu_mn_invalidate_range_end - callback to notify about mm change
  222. *
  223. * @mn: our notifier
  224. * @mn: the mm this callback is about
  225. * @start: start of updated range
  226. * @end: end of updated range
  227. *
  228. * Release the lock again to allow new command submissions.
  229. */
  230. static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
  231. struct mm_struct *mm,
  232. unsigned long start,
  233. unsigned long end)
  234. {
  235. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  236. amdgpu_mn_read_unlock(rmn);
  237. }
  238. static const struct mmu_notifier_ops amdgpu_mn_ops = {
  239. .release = amdgpu_mn_release,
  240. .invalidate_page = amdgpu_mn_invalidate_page,
  241. .invalidate_range_start = amdgpu_mn_invalidate_range_start,
  242. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  243. };
  244. /**
  245. * amdgpu_mn_get - create notifier context
  246. *
  247. * @adev: amdgpu device pointer
  248. *
  249. * Creates a notifier context for current->mm.
  250. */
  251. struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
  252. {
  253. struct mm_struct *mm = current->mm;
  254. struct amdgpu_mn *rmn;
  255. int r;
  256. mutex_lock(&adev->mn_lock);
  257. if (down_write_killable(&mm->mmap_sem)) {
  258. mutex_unlock(&adev->mn_lock);
  259. return ERR_PTR(-EINTR);
  260. }
  261. hash_for_each_possible(adev->mn_hash, rmn, node, (unsigned long)mm)
  262. if (rmn->mm == mm)
  263. goto release_locks;
  264. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  265. if (!rmn) {
  266. rmn = ERR_PTR(-ENOMEM);
  267. goto release_locks;
  268. }
  269. rmn->adev = adev;
  270. rmn->mm = mm;
  271. rmn->mn.ops = &amdgpu_mn_ops;
  272. init_rwsem(&rmn->lock);
  273. rmn->objects = RB_ROOT;
  274. mutex_init(&rmn->read_lock);
  275. atomic_set(&rmn->recursion, 0);
  276. r = __mmu_notifier_register(&rmn->mn, mm);
  277. if (r)
  278. goto free_rmn;
  279. hash_add(adev->mn_hash, &rmn->node, (unsigned long)mm);
  280. release_locks:
  281. up_write(&mm->mmap_sem);
  282. mutex_unlock(&adev->mn_lock);
  283. return rmn;
  284. free_rmn:
  285. up_write(&mm->mmap_sem);
  286. mutex_unlock(&adev->mn_lock);
  287. kfree(rmn);
  288. return ERR_PTR(r);
  289. }
  290. /**
  291. * amdgpu_mn_register - register a BO for notifier updates
  292. *
  293. * @bo: amdgpu buffer object
  294. * @addr: userptr addr we should monitor
  295. *
  296. * Registers an MMU notifier for the given BO at the specified address.
  297. * Returns 0 on success, -ERRNO if anything goes wrong.
  298. */
  299. int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  300. {
  301. unsigned long end = addr + amdgpu_bo_size(bo) - 1;
  302. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  303. struct amdgpu_mn *rmn;
  304. struct amdgpu_mn_node *node = NULL;
  305. struct list_head bos;
  306. struct interval_tree_node *it;
  307. rmn = amdgpu_mn_get(adev);
  308. if (IS_ERR(rmn))
  309. return PTR_ERR(rmn);
  310. INIT_LIST_HEAD(&bos);
  311. down_write(&rmn->lock);
  312. while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
  313. kfree(node);
  314. node = container_of(it, struct amdgpu_mn_node, it);
  315. interval_tree_remove(&node->it, &rmn->objects);
  316. addr = min(it->start, addr);
  317. end = max(it->last, end);
  318. list_splice(&node->bos, &bos);
  319. }
  320. if (!node) {
  321. node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
  322. if (!node) {
  323. up_write(&rmn->lock);
  324. return -ENOMEM;
  325. }
  326. }
  327. bo->mn = rmn;
  328. node->it.start = addr;
  329. node->it.last = end;
  330. INIT_LIST_HEAD(&node->bos);
  331. list_splice(&bos, &node->bos);
  332. list_add(&bo->mn_list, &node->bos);
  333. interval_tree_insert(&node->it, &rmn->objects);
  334. up_write(&rmn->lock);
  335. return 0;
  336. }
  337. /**
  338. * amdgpu_mn_unregister - unregister a BO for notifier updates
  339. *
  340. * @bo: amdgpu buffer object
  341. *
  342. * Remove any registration of MMU notifier updates from the buffer object.
  343. */
  344. void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  345. {
  346. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  347. struct amdgpu_mn *rmn;
  348. struct list_head *head;
  349. mutex_lock(&adev->mn_lock);
  350. rmn = bo->mn;
  351. if (rmn == NULL) {
  352. mutex_unlock(&adev->mn_lock);
  353. return;
  354. }
  355. down_write(&rmn->lock);
  356. /* save the next list entry for later */
  357. head = bo->mn_list.next;
  358. bo->mn = NULL;
  359. list_del_init(&bo->mn_list);
  360. if (list_empty(head)) {
  361. struct amdgpu_mn_node *node;
  362. node = container_of(head, struct amdgpu_mn_node, bos);
  363. interval_tree_remove(&node->it, &rmn->objects);
  364. kfree(node);
  365. }
  366. up_write(&rmn->lock);
  367. mutex_unlock(&adev->mn_lock);
  368. }