radeon_mn.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_cached 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,
  70. &rmn->objects.rb_root, 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 int radeon_mn_invalidate_range_start(struct mmu_notifier *mn,
  110. struct mm_struct *mm,
  111. unsigned long start,
  112. unsigned long end,
  113. bool blockable)
  114. {
  115. struct radeon_mn *rmn = container_of(mn, struct radeon_mn, mn);
  116. struct ttm_operation_ctx ctx = { false, false };
  117. struct interval_tree_node *it;
  118. int ret = 0;
  119. /* notification is exclusive, but interval is inclusive */
  120. end -= 1;
  121. /* TODO we should be able to split locking for interval tree and
  122. * the tear down.
  123. */
  124. if (blockable)
  125. mutex_lock(&rmn->lock);
  126. else if (!mutex_trylock(&rmn->lock))
  127. return -EAGAIN;
  128. it = interval_tree_iter_first(&rmn->objects, start, end);
  129. while (it) {
  130. struct radeon_mn_node *node;
  131. struct radeon_bo *bo;
  132. long r;
  133. if (!blockable) {
  134. ret = -EAGAIN;
  135. goto out_unlock;
  136. }
  137. node = container_of(it, struct radeon_mn_node, it);
  138. it = interval_tree_iter_next(it, start, end);
  139. list_for_each_entry(bo, &node->bos, mn_list) {
  140. if (!bo->tbo.ttm || bo->tbo.ttm->state != tt_bound)
  141. continue;
  142. r = radeon_bo_reserve(bo, true);
  143. if (r) {
  144. DRM_ERROR("(%ld) failed to reserve user bo\n", r);
  145. continue;
  146. }
  147. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  148. true, false, MAX_SCHEDULE_TIMEOUT);
  149. if (r <= 0)
  150. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  151. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
  152. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  153. if (r)
  154. DRM_ERROR("(%ld) failed to validate user bo\n", r);
  155. radeon_bo_unreserve(bo);
  156. }
  157. }
  158. out_unlock:
  159. mutex_unlock(&rmn->lock);
  160. return ret;
  161. }
  162. static const struct mmu_notifier_ops radeon_mn_ops = {
  163. .release = radeon_mn_release,
  164. .invalidate_range_start = radeon_mn_invalidate_range_start,
  165. };
  166. /**
  167. * radeon_mn_get - create notifier context
  168. *
  169. * @rdev: radeon device pointer
  170. *
  171. * Creates a notifier context for current->mm.
  172. */
  173. static struct radeon_mn *radeon_mn_get(struct radeon_device *rdev)
  174. {
  175. struct mm_struct *mm = current->mm;
  176. struct radeon_mn *rmn;
  177. int r;
  178. if (down_write_killable(&mm->mmap_sem))
  179. return ERR_PTR(-EINTR);
  180. mutex_lock(&rdev->mn_lock);
  181. hash_for_each_possible(rdev->mn_hash, rmn, node, (unsigned long)mm)
  182. if (rmn->mm == mm)
  183. goto release_locks;
  184. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  185. if (!rmn) {
  186. rmn = ERR_PTR(-ENOMEM);
  187. goto release_locks;
  188. }
  189. rmn->rdev = rdev;
  190. rmn->mm = mm;
  191. rmn->mn.ops = &radeon_mn_ops;
  192. mutex_init(&rmn->lock);
  193. rmn->objects = RB_ROOT_CACHED;
  194. r = __mmu_notifier_register(&rmn->mn, mm);
  195. if (r)
  196. goto free_rmn;
  197. hash_add(rdev->mn_hash, &rmn->node, (unsigned long)mm);
  198. release_locks:
  199. mutex_unlock(&rdev->mn_lock);
  200. up_write(&mm->mmap_sem);
  201. return rmn;
  202. free_rmn:
  203. mutex_unlock(&rdev->mn_lock);
  204. up_write(&mm->mmap_sem);
  205. kfree(rmn);
  206. return ERR_PTR(r);
  207. }
  208. /**
  209. * radeon_mn_register - register a BO for notifier updates
  210. *
  211. * @bo: radeon buffer object
  212. * @addr: userptr addr we should monitor
  213. *
  214. * Registers an MMU notifier for the given BO at the specified address.
  215. * Returns 0 on success, -ERRNO if anything goes wrong.
  216. */
  217. int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
  218. {
  219. unsigned long end = addr + radeon_bo_size(bo) - 1;
  220. struct radeon_device *rdev = bo->rdev;
  221. struct radeon_mn *rmn;
  222. struct radeon_mn_node *node = NULL;
  223. struct list_head bos;
  224. struct interval_tree_node *it;
  225. rmn = radeon_mn_get(rdev);
  226. if (IS_ERR(rmn))
  227. return PTR_ERR(rmn);
  228. INIT_LIST_HEAD(&bos);
  229. mutex_lock(&rmn->lock);
  230. while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
  231. kfree(node);
  232. node = container_of(it, struct radeon_mn_node, it);
  233. interval_tree_remove(&node->it, &rmn->objects);
  234. addr = min(it->start, addr);
  235. end = max(it->last, end);
  236. list_splice(&node->bos, &bos);
  237. }
  238. if (!node) {
  239. node = kmalloc(sizeof(struct radeon_mn_node), GFP_KERNEL);
  240. if (!node) {
  241. mutex_unlock(&rmn->lock);
  242. return -ENOMEM;
  243. }
  244. }
  245. bo->mn = rmn;
  246. node->it.start = addr;
  247. node->it.last = end;
  248. INIT_LIST_HEAD(&node->bos);
  249. list_splice(&bos, &node->bos);
  250. list_add(&bo->mn_list, &node->bos);
  251. interval_tree_insert(&node->it, &rmn->objects);
  252. mutex_unlock(&rmn->lock);
  253. return 0;
  254. }
  255. /**
  256. * radeon_mn_unregister - unregister a BO for notifier updates
  257. *
  258. * @bo: radeon buffer object
  259. *
  260. * Remove any registration of MMU notifier updates from the buffer object.
  261. */
  262. void radeon_mn_unregister(struct radeon_bo *bo)
  263. {
  264. struct radeon_device *rdev = bo->rdev;
  265. struct radeon_mn *rmn;
  266. struct list_head *head;
  267. mutex_lock(&rdev->mn_lock);
  268. rmn = bo->mn;
  269. if (rmn == NULL) {
  270. mutex_unlock(&rdev->mn_lock);
  271. return;
  272. }
  273. mutex_lock(&rmn->lock);
  274. /* save the next list entry for later */
  275. head = bo->mn_list.next;
  276. bo->mn = NULL;
  277. list_del(&bo->mn_list);
  278. if (list_empty(head)) {
  279. struct radeon_mn_node *node;
  280. node = container_of(head, struct radeon_mn_node, bos);
  281. interval_tree_remove(&node->it, &rmn->objects);
  282. kfree(node);
  283. }
  284. mutex_unlock(&rmn->lock);
  285. mutex_unlock(&rdev->mn_lock);
  286. }