radeon_sa.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2011 Red Hat 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. * Jerome Glisse <glisse@freedesktop.org>
  29. */
  30. /* Algorithm:
  31. *
  32. * We store the last allocated bo in "hole", we always try to allocate
  33. * after the last allocated bo. Principle is that in a linear GPU ring
  34. * progression was is after last is the oldest bo we allocated and thus
  35. * the first one that should no longer be in use by the GPU.
  36. *
  37. * If it's not the case we skip over the bo after last to the closest
  38. * done bo if such one exist. If none exist and we are not asked to
  39. * block we report failure to allocate.
  40. *
  41. * If we are asked to block we wait on all the oldest fence of all
  42. * rings. We just wait for any of those fence to complete.
  43. */
  44. #include "radeon.h"
  45. int radeon_sa_bo_manager_init(struct radeon_device *rdev,
  46. struct radeon_sa_manager *sa_manager,
  47. unsigned int size, u32 sa_align, u32 domain,
  48. u32 flags)
  49. {
  50. int r;
  51. r = radeon_bo_create(rdev, size, RADEON_GPU_PAGE_SIZE, true,
  52. domain, flags, NULL, NULL, &sa_manager->bo);
  53. if (r) {
  54. dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
  55. return r;
  56. }
  57. sa_manager->domain = domain;
  58. drm_suballoc_manager_init(&sa_manager->base, size, sa_align);
  59. return r;
  60. }
  61. void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
  62. struct radeon_sa_manager *sa_manager)
  63. {
  64. drm_suballoc_manager_fini(&sa_manager->base);
  65. radeon_bo_unref(&sa_manager->bo);
  66. }
  67. int radeon_sa_bo_manager_start(struct radeon_device *rdev,
  68. struct radeon_sa_manager *sa_manager)
  69. {
  70. int r;
  71. if (sa_manager->bo == NULL) {
  72. dev_err(rdev->dev, "no bo for sa manager\n");
  73. return -EINVAL;
  74. }
  75. /* map the buffer */
  76. r = radeon_bo_reserve(sa_manager->bo, false);
  77. if (r) {
  78. dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
  79. return r;
  80. }
  81. r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
  82. if (r) {
  83. radeon_bo_unreserve(sa_manager->bo);
  84. dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
  85. return r;
  86. }
  87. r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
  88. radeon_bo_unreserve(sa_manager->bo);
  89. return r;
  90. }
  91. int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
  92. struct radeon_sa_manager *sa_manager)
  93. {
  94. int r;
  95. if (sa_manager->bo == NULL) {
  96. dev_err(rdev->dev, "no bo for sa manager\n");
  97. return -EINVAL;
  98. }
  99. r = radeon_bo_reserve(sa_manager->bo, false);
  100. if (!r) {
  101. radeon_bo_kunmap(sa_manager->bo);
  102. radeon_bo_unpin(sa_manager->bo);
  103. radeon_bo_unreserve(sa_manager->bo);
  104. }
  105. return r;
  106. }
  107. int radeon_sa_bo_new(struct radeon_sa_manager *sa_manager,
  108. struct drm_suballoc **sa_bo,
  109. unsigned int size, unsigned int align)
  110. {
  111. struct drm_suballoc *sa = drm_suballoc_new(&sa_manager->base, size,
  112. GFP_KERNEL, false, align);
  113. if (IS_ERR(sa)) {
  114. *sa_bo = NULL;
  115. return PTR_ERR(sa);
  116. }
  117. *sa_bo = sa;
  118. return 0;
  119. }
  120. void radeon_sa_bo_free(struct drm_suballoc **sa_bo,
  121. struct radeon_fence *fence)
  122. {
  123. if (sa_bo == NULL || *sa_bo == NULL) {
  124. return;
  125. }
  126. if (fence)
  127. drm_suballoc_free(*sa_bo, &fence->base);
  128. else
  129. drm_suballoc_free(*sa_bo, NULL);
  130. *sa_bo = NULL;
  131. }
  132. #if defined(CONFIG_DEBUG_FS)
  133. void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
  134. struct seq_file *m)
  135. {
  136. struct drm_printer p = drm_seq_file_printer(m);
  137. drm_suballoc_dump_debug_info(&sa_manager->base, &p, sa_manager->gpu_addr);
  138. }
  139. #endif