radeon_mn.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <[email protected]>
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <drm/drm.h>
  34. #include "radeon.h"
  35. /**
  36. * radeon_mn_invalidate - callback to notify about mm change
  37. *
  38. * @mn: our notifier
  39. * @range: the VMA under invalidation
  40. * @cur_seq: Value to pass to mmu_interval_set_seq()
  41. *
  42. * We block for all BOs between start and end to be idle and
  43. * unmap them by move them into system domain again.
  44. */
  45. static bool radeon_mn_invalidate(struct mmu_interval_notifier *mn,
  46. const struct mmu_notifier_range *range,
  47. unsigned long cur_seq)
  48. {
  49. struct radeon_bo *bo = container_of(mn, struct radeon_bo, notifier);
  50. struct ttm_operation_ctx ctx = { false, false };
  51. long r;
  52. if (!bo->tbo.ttm || !radeon_ttm_tt_is_bound(bo->tbo.bdev, bo->tbo.ttm))
  53. return true;
  54. if (!mmu_notifier_range_blockable(range))
  55. return false;
  56. r = radeon_bo_reserve(bo, true);
  57. if (r) {
  58. DRM_ERROR("(%ld) failed to reserve user bo\n", r);
  59. return true;
  60. }
  61. r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
  62. false, MAX_SCHEDULE_TIMEOUT);
  63. if (r <= 0)
  64. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  65. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
  66. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  67. if (r)
  68. DRM_ERROR("(%ld) failed to validate user bo\n", r);
  69. radeon_bo_unreserve(bo);
  70. return true;
  71. }
  72. static const struct mmu_interval_notifier_ops radeon_mn_ops = {
  73. .invalidate = radeon_mn_invalidate,
  74. };
  75. /**
  76. * radeon_mn_register - register a BO for notifier updates
  77. *
  78. * @bo: radeon buffer object
  79. * @addr: userptr addr we should monitor
  80. *
  81. * Registers an MMU notifier for the given BO at the specified address.
  82. * Returns 0 on success, -ERRNO if anything goes wrong.
  83. */
  84. int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
  85. {
  86. int ret;
  87. ret = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
  88. radeon_bo_size(bo), &radeon_mn_ops);
  89. if (ret)
  90. return ret;
  91. /*
  92. * FIXME: radeon appears to allow get_user_pages to run during
  93. * invalidate_range_start/end, which is not a safe way to read the
  94. * PTEs. It should use the mmu_interval_read_begin() scheme around the
  95. * get_user_pages to ensure that the PTEs are read properly
  96. */
  97. mmu_interval_read_begin(&bo->notifier);
  98. return 0;
  99. }
  100. /**
  101. * radeon_mn_unregister - unregister a BO for notifier updates
  102. *
  103. * @bo: radeon buffer object
  104. *
  105. * Remove any registration of MMU notifier updates from the buffer object.
  106. */
  107. void radeon_mn_unregister(struct radeon_bo *bo)
  108. {
  109. if (!bo->notifier.mm)
  110. return;
  111. mmu_interval_notifier_remove(&bo->notifier);
  112. bo->notifier.mm = NULL;
  113. }