radeon_mn.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. struct fence *fence;
  113. int r;
  114. bo = container_of(it, struct radeon_bo, mn_it);
  115. it = interval_tree_iter_next(it, start, end);
  116. r = radeon_bo_reserve(bo, true);
  117. if (r) {
  118. DRM_ERROR("(%d) failed to reserve user bo\n", r);
  119. continue;
  120. }
  121. fence = reservation_object_get_excl(bo->tbo.resv);
  122. if (fence) {
  123. r = radeon_fence_wait((struct radeon_fence *)fence, false);
  124. if (r)
  125. DRM_ERROR("(%d) failed to wait for user bo\n", r);
  126. }
  127. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
  128. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  129. if (r)
  130. DRM_ERROR("(%d) failed to validate user bo\n", r);
  131. radeon_bo_unreserve(bo);
  132. }
  133. mutex_unlock(&rmn->lock);
  134. }
  135. static const struct mmu_notifier_ops radeon_mn_ops = {
  136. .release = radeon_mn_release,
  137. .invalidate_range_start = radeon_mn_invalidate_range_start,
  138. };
  139. /**
  140. * radeon_mn_get - create notifier context
  141. *
  142. * @rdev: radeon device pointer
  143. *
  144. * Creates a notifier context for current->mm.
  145. */
  146. static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
  147. {
  148. struct mm_struct *mm = current->mm;
  149. struct radeon_mn *rmn;
  150. int r;
  151. down_write(&mm->mmap_sem);
  152. mutex_lock(&rdev->mn_lock);
  153. hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
  154. if (rmn->mm == mm)
  155. goto release_locks;
  156. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  157. if (!rmn) {
  158. rmn = ERR_PTR(-ENOMEM);
  159. goto release_locks;
  160. }
  161. rmn->rdev = rdev;
  162. rmn->mm = mm;
  163. rmn->mn.ops = &radeon_mn_ops;
  164. mutex_init(&rmn->lock);
  165. rmn->objects = RB_ROOT;
  166. r = __mmu_notifier_register(&rmn->mn, mm);
  167. if (r)
  168. goto free_rmn;
  169. hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
  170. release_locks:
  171. mutex_unlock(&rdev->mn_lock);
  172. up_write(&mm->mmap_sem);
  173. return rmn;
  174. free_rmn:
  175. mutex_unlock(&rdev->mn_lock);
  176. up_write(&mm->mmap_sem);
  177. kfree(rmn);
  178. return ERR_PTR(r);
  179. }
  180. /**
  181. * radeon_mn_register - register a BO for notifier updates
  182. *
  183. * @bo: radeon buffer object
  184. * @addr: userptr addr we should monitor
  185. *
  186. * Registers an MMU notifier for the given BO at the specified address.
  187. * Returns 0 on success, -ERRNO if anything goes wrong.
  188. */
  189. int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
  190. {
  191. unsigned long end = addr + radeon_bo_size(bo) - 1;
  192. struct radeon_device *rdev = bo->rdev;
  193. struct radeon_mn *rmn;
  194. struct interval_tree_node *it;
  195. rmn = radeon_mn_get(rdev);
  196. if (IS_ERR(rmn))
  197. return PTR_ERR(rmn);
  198. mutex_lock(&rmn->lock);
  199. it = interval_tree_iter_first(&rmn->objects, addr, end);
  200. if (it) {
  201. mutex_unlock(&rmn->lock);
  202. return -EEXIST;
  203. }
  204. bo->mn = rmn;
  205. bo->mn_it.start = addr;
  206. bo->mn_it.last = end;
  207. interval_tree_insert(&bo->mn_it, &rmn->objects);
  208. mutex_unlock(&rmn->lock);
  209. return 0;
  210. }
  211. /**
  212. * radeon_mn_unregister - unregister a BO for notifier updates
  213. *
  214. * @bo: radeon buffer object
  215. *
  216. * Remove any registration of MMU notifier updates from the buffer object.
  217. */
  218. void radeon_mn_unregister(struct radeon_bo *bo)
  219. {
  220. struct radeon_device *rdev = bo->rdev;
  221. struct radeon_mn *rmn;
  222. mutex_lock(&rdev->mn_lock);
  223. rmn = bo->mn;
  224. if (rmn == NULL) {
  225. mutex_unlock(&rdev->mn_lock);
  226. return;
  227. }
  228. mutex_lock(&rmn->lock);
  229. interval_tree_remove(&bo->mn_it, &rmn->objects);
  230. bo->mn = NULL;
  231. mutex_unlock(&rmn->lock);
  232. mutex_unlock(&rdev->mn_lock);
  233. }