radeon_mn.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. struct radeon_mn_node {
  50. struct interval_tree_node it;
  51. struct list_head bos;
  52. };
  53. /**
  54. * radeon_mn_destroy - destroy the rmn
  55. *
  56. * @work: previously sheduled work item
  57. *
  58. * Lazy destroys the notifier from a work item
  59. */
  60. static void radeon_mn_destroy(struct work_struct *work)
  61. {
  62. struct radeon_mn *rmn = container_of(work, struct radeon_mn, work);
  63. struct radeon_device *rdev = rmn->rdev;
  64. struct radeon_mn_node *node, *next_node;
  65. struct radeon_bo *bo, *next_bo;
  66. mutex_lock(&rdev->mn_lock);
  67. mutex_lock(&rmn->lock);
  68. hash_del(&rmn->node);
  69. rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
  70. it.rb) {
  71. interval_tree_remove(&node->it, &rmn->objects);
  72. list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
  73. bo->mn = NULL;
  74. list_del_init(&bo->mn_list);
  75. }
  76. kfree(node);
  77. }
  78. mutex_unlock(&rmn->lock);
  79. mutex_unlock(&rdev->mn_lock);
  80. mmu_notifier_unregister(&rmn->mn, rmn->mm);
  81. kfree(rmn);
  82. }
  83. /**
  84. * radeon_mn_release - callback to notify about mm destruction
  85. *
  86. * @mn: our notifier
  87. * @mn: the mm this callback is about
  88. *
  89. * Shedule a work item to lazy destroy our notifier.
  90. */
  91. static void radeon_mn_release(struct mmu_notifier *mn,
  92. struct mm_struct *mm)
  93. {
  94. struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
  95. INIT_WORK(&rmn->work, radeon_mn_destroy);
  96. schedule_work(&rmn->work);
  97. }
  98. /**
  99. * radeon_mn_invalidate_range_start - callback to notify about mm change
  100. *
  101. * @mn: our notifier
  102. * @mn: the mm this callback is about
  103. * @start: start of updated range
  104. * @end: end of updated range
  105. *
  106. * We block for all BOs between start and end to be idle and
  107. * unmap them by move them into system domain again.
  108. */
  109. static void radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
  110. struct mm_struct *mm,
  111. unsigned long start,
  112. unsigned long end)
  113. {
  114. struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
  115. struct interval_tree_node *it;
  116. /* notification is exclusive, but interval is inclusive */
  117. end -= 1;
  118. mutex_lock(&rmn->lock);
  119. it = interval_tree_iter_first(&rmn->objects, start, end);
  120. while (it) {
  121. struct radeon_mn_node *node;
  122. struct radeon_bo *bo;
  123. long r;
  124. node = container_of(it, struct radeon_mn_node, it);
  125. it = interval_tree_iter_next(it, start, end);
  126. list_for_each_entry(bo, &node->bos, mn_list) {
  127. if (!bo->tbo.ttm || bo->tbo.ttm->state != tt_bound)
  128. continue;
  129. r = radeon_bo_reserve(bo, true);
  130. if (r) {
  131. DRM_ERROR("(%ld) failed to reserve user bo\n", r);
  132. continue;
  133. }
  134. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  135. true, false, MAX_SCHEDULE_TIMEOUT);
  136. if (r <= 0)
  137. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  138. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
  139. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  140. if (r)
  141. DRM_ERROR("(%ld) failed to validate user bo\n", r);
  142. radeon_bo_unreserve(bo);
  143. }
  144. }
  145. mutex_unlock(&rmn->lock);
  146. }
  147. static const struct mmu_notifier_ops radeon_mn_ops = {
  148. .release = radeon_mn_release,
  149. .invalidate_range_start = radeon_mn_invalidate_range_start,
  150. };
  151. /**
  152. * radeon_mn_get - create notifier context
  153. *
  154. * @rdev: radeon device pointer
  155. *
  156. * Creates a notifier context for current->mm.
  157. */
  158. static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
  159. {
  160. struct mm_struct *mm = current->mm;
  161. struct radeon_mn *rmn;
  162. int r;
  163. down_write(&mm->mmap_sem);
  164. mutex_lock(&rdev->mn_lock);
  165. hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
  166. if (rmn->mm == mm)
  167. goto release_locks;
  168. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  169. if (!rmn) {
  170. rmn = ERR_PTR(-ENOMEM);
  171. goto release_locks;
  172. }
  173. rmn->rdev = rdev;
  174. rmn->mm = mm;
  175. rmn->mn.ops = &radeon_mn_ops;
  176. mutex_init(&rmn->lock);
  177. rmn->objects = RB_ROOT;
  178. r = __mmu_notifier_register(&rmn->mn, mm);
  179. if (r)
  180. goto free_rmn;
  181. hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
  182. release_locks:
  183. mutex_unlock(&rdev->mn_lock);
  184. up_write(&mm->mmap_sem);
  185. return rmn;
  186. free_rmn:
  187. mutex_unlock(&rdev->mn_lock);
  188. up_write(&mm->mmap_sem);
  189. kfree(rmn);
  190. return ERR_PTR(r);
  191. }
  192. /**
  193. * radeon_mn_register - register a BO for notifier updates
  194. *
  195. * @bo: radeon buffer object
  196. * @addr: userptr addr we should monitor
  197. *
  198. * Registers an MMU notifier for the given BO at the specified address.
  199. * Returns 0 on success, -ERRNO if anything goes wrong.
  200. */
  201. int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
  202. {
  203. unsigned long end = addr + radeon_bo_size(bo) - 1;
  204. struct radeon_device *rdev = bo->rdev;
  205. struct radeon_mn *rmn;
  206. struct radeon_mn_node *node = NULL;
  207. struct list_head bos;
  208. struct interval_tree_node *it;
  209. rmn = radeon_mn_get(rdev);
  210. if (IS_ERR(rmn))
  211. return PTR_ERR(rmn);
  212. INIT_LIST_HEAD(&bos);
  213. mutex_lock(&rmn->lock);
  214. while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
  215. kfree(node);
  216. node = container_of(it, struct radeon_mn_node, it);
  217. interval_tree_remove(&node->it, &rmn->objects);
  218. addr = min(it->start, addr);
  219. end = max(it->last, end);
  220. list_splice(&node->bos, &bos);
  221. }
  222. if (!node) {
  223. node = kmalloc(sizeof(struct radeon_mn_node), GFP_KERNEL);
  224. if (!node) {
  225. mutex_unlock(&rmn->lock);
  226. return -ENOMEM;
  227. }
  228. }
  229. bo->mn = rmn;
  230. node->it.start = addr;
  231. node->it.last = end;
  232. INIT_LIST_HEAD(&node->bos);
  233. list_splice(&bos, &node->bos);
  234. list_add(&bo->mn_list, &node->bos);
  235. interval_tree_insert(&node->it, &rmn->objects);
  236. mutex_unlock(&rmn->lock);
  237. return 0;
  238. }
  239. /**
  240. * radeon_mn_unregister - unregister a BO for notifier updates
  241. *
  242. * @bo: radeon buffer object
  243. *
  244. * Remove any registration of MMU notifier updates from the buffer object.
  245. */
  246. void radeon_mn_unregister(struct radeon_bo *bo)
  247. {
  248. struct radeon_device *rdev = bo->rdev;
  249. struct radeon_mn *rmn;
  250. struct list_head *head;
  251. mutex_lock(&rdev->mn_lock);
  252. rmn = bo->mn;
  253. if (rmn == NULL) {
  254. mutex_unlock(&rdev->mn_lock);
  255. return;
  256. }
  257. mutex_lock(&rmn->lock);
  258. /* save the next list entry for later */
  259. head = bo->mn_list.next;
  260. bo->mn = NULL;
  261. list_del(&bo->mn_list);
  262. if (list_empty(head)) {
  263. struct radeon_mn_node *node;
  264. node = container_of(head, struct radeon_mn_node, bos);
  265. interval_tree_remove(&node->it, &rmn->objects);
  266. kfree(node);
  267. }
  268. mutex_unlock(&rmn->lock);
  269. mutex_unlock(&rdev->mn_lock);
  270. }