radeon_mn.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <drm/drmP.h>
  34. #include <drm/drm.h>
  35. #include "radeon.h"
  36. struct radeon_mn {
  37. /* constant after initialisation */
  38. struct radeon_device *rdev;
  39. struct mm_struct *mm;
  40. struct mmu_notifier mn;
  41. /* only used on destruction */
  42. struct work_struct work;
  43. /* protected by rdev->mn_lock */
  44. struct hlist_node node;
  45. /* objects protected by lock */
  46. struct mutex lock;
  47. struct rb_root objects;
  48. };
  49. /**
  50. * radeon_mn_destroy - destroy the rmn
  51. *
  52. * @work: previously sheduled work item
  53. *
  54. * Lazy destroys the notifier from a work item
  55. */
  56. static void radeon_mn_destroy(struct work_struct *work)
  57. {
  58. struct radeon_mn *rmn = container_of(work, struct radeon_mn, work);
  59. struct radeon_device *rdev = rmn->rdev;
  60. struct radeon_bo *bo, *next;
  61. mutex_lock(&rdev->mn_lock);
  62. mutex_lock(&rmn->lock);
  63. hash_del(&rmn->node);
  64. rbtree_postorder_for_each_entry_safe(bo, next, &rmn->objects, mn_it.rb) {
  65. interval_tree_remove(&bo->mn_it, &rmn->objects);
  66. bo->mn = NULL;
  67. }
  68. mutex_unlock(&rmn->lock);
  69. mutex_unlock(&rdev->mn_lock);
  70. mmu_notifier_unregister(&rmn->mn, rmn->mm);
  71. kfree(rmn);
  72. }
  73. /**
  74. * radeon_mn_release - callback to notify about mm destruction
  75. *
  76. * @mn: our notifier
  77. * @mn: the mm this callback is about
  78. *
  79. * Shedule a work item to lazy destroy our notifier.
  80. */
  81. static void radeon_mn_release(struct mmu_notifier *mn,
  82. struct mm_struct *mm)
  83. {
  84. struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
  85. INIT_WORK(&rmn->work, radeon_mn_destroy);
  86. schedule_work(&rmn->work);
  87. }
  88. /**
  89. * radeon_mn_invalidate_range_start - callback to notify about mm change
  90. *
  91. * @mn: our notifier
  92. * @mn: the mm this callback is about
  93. * @start: start of updated range
  94. * @end: end of updated range
  95. *
  96. * We block for all BOs between start and end to be idle and
  97. * unmap them by move them into system domain again.
  98. */
  99. static void radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
  100. struct mm_struct *mm,
  101. unsigned long start,
  102. unsigned long end)
  103. {
  104. struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
  105. struct interval_tree_node *it;
  106. /* notification is exclusive, but interval is inclusive */
  107. end -= 1;
  108. mutex_lock(&rmn->lock);
  109. it = interval_tree_iter_first(&rmn->objects, start, end);
  110. while (it) {
  111. struct radeon_bo *bo;
  112. int r;
  113. bo = container_of(it, struct radeon_bo, mn_it);
  114. it = interval_tree_iter_next(it, start, end);
  115. r = radeon_bo_reserve(bo, true);
  116. if (r) {
  117. DRM_ERROR("(%d) failed to reserve user bo\n", r);
  118. continue;
  119. }
  120. r = reservation_object_wait_timeout_rcu(bo->tbo.resv, true,
  121. false, MAX_SCHEDULE_TIMEOUT);
  122. if (r)
  123. DRM_ERROR("(%d) failed to wait for user bo\n", r);
  124. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
  125. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  126. if (r)
  127. DRM_ERROR("(%d) failed to validate user bo\n", r);
  128. radeon_bo_unreserve(bo);
  129. }
  130. mutex_unlock(&rmn->lock);
  131. }
  132. static const struct mmu_notifier_ops radeon_mn_ops = {
  133. .release = radeon_mn_release,
  134. .invalidate_range_start = radeon_mn_invalidate_range_start,
  135. };
  136. /**
  137. * radeon_mn_get - create notifier context
  138. *
  139. * @rdev: radeon device pointer
  140. *
  141. * Creates a notifier context for current->mm.
  142. */
  143. static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
  144. {
  145. struct mm_struct *mm = current->mm;
  146. struct radeon_mn *rmn;
  147. int r;
  148. down_write(&mm->mmap_sem);
  149. mutex_lock(&rdev->mn_lock);
  150. hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
  151. if (rmn->mm == mm)
  152. goto release_locks;
  153. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  154. if (!rmn) {
  155. rmn = ERR_PTR(-ENOMEM);
  156. goto release_locks;
  157. }
  158. rmn->rdev = rdev;
  159. rmn->mm = mm;
  160. rmn->mn.ops = &radeon_mn_ops;
  161. mutex_init(&rmn->lock);
  162. rmn->objects = RB_ROOT;
  163. r = __mmu_notifier_register(&rmn->mn, mm);
  164. if (r)
  165. goto free_rmn;
  166. hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
  167. release_locks:
  168. mutex_unlock(&rdev->mn_lock);
  169. up_write(&mm->mmap_sem);
  170. return rmn;
  171. free_rmn:
  172. mutex_unlock(&rdev->mn_lock);
  173. up_write(&mm->mmap_sem);
  174. kfree(rmn);
  175. return ERR_PTR(r);
  176. }
  177. /**
  178. * radeon_mn_register - register a BO for notifier updates
  179. *
  180. * @bo: radeon buffer object
  181. * @addr: userptr addr we should monitor
  182. *
  183. * Registers an MMU notifier for the given BO at the specified address.
  184. * Returns 0 on success, -ERRNO if anything goes wrong.
  185. */
  186. int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
  187. {
  188. unsigned long end = addr + radeon_bo_size(bo) - 1;
  189. struct radeon_device *rdev = bo->rdev;
  190. struct radeon_mn *rmn;
  191. struct interval_tree_node *it;
  192. rmn = radeon_mn_get(rdev);
  193. if (IS_ERR(rmn))
  194. return PTR_ERR(rmn);
  195. mutex_lock(&rmn->lock);
  196. it = interval_tree_iter_first(&rmn->objects, addr, end);
  197. if (it) {
  198. mutex_unlock(&rmn->lock);
  199. return -EEXIST;
  200. }
  201. bo->mn = rmn;
  202. bo->mn_it.start = addr;
  203. bo->mn_it.last = end;
  204. interval_tree_insert(&bo->mn_it, &rmn->objects);
  205. mutex_unlock(&rmn->lock);
  206. return 0;
  207. }
  208. /**
  209. * radeon_mn_unregister - unregister a BO for notifier updates
  210. *
  211. * @bo: radeon buffer object
  212. *
  213. * Remove any registration of MMU notifier updates from the buffer object.
  214. */
  215. void radeon_mn_unregister(struct radeon_bo *bo)
  216. {
  217. struct radeon_device *rdev = bo->rdev;
  218. struct radeon_mn *rmn;
  219. mutex_lock(&rdev->mn_lock);
  220. rmn = bo->mn;
  221. if (rmn == NULL) {
  222. mutex_unlock(&rdev->mn_lock);
  223. return;
  224. }
  225. mutex_lock(&rmn->lock);
  226. interval_tree_remove(&bo->mn_it, &rmn->objects);
  227. bo->mn = NULL;
  228. mutex_unlock(&rmn->lock);
  229. mutex_unlock(&rdev->mn_lock);
  230. }