radeon_sa.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include "drmP.h"
  31. #include "drm.h"
  32. #include "radeon.h"
  33. int radeon_sa_bo_manager_init(struct radeon_device *rdev,
  34. struct radeon_sa_manager *sa_manager,
  35. unsigned size, u32 domain)
  36. {
  37. int r;
  38. spin_lock_init(&sa_manager->lock);
  39. sa_manager->bo = NULL;
  40. sa_manager->size = size;
  41. sa_manager->domain = domain;
  42. INIT_LIST_HEAD(&sa_manager->sa_bo);
  43. r = radeon_bo_create(rdev, size, RADEON_GPU_PAGE_SIZE, true,
  44. RADEON_GEM_DOMAIN_CPU, &sa_manager->bo);
  45. if (r) {
  46. dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r);
  47. return r;
  48. }
  49. return r;
  50. }
  51. void radeon_sa_bo_manager_fini(struct radeon_device *rdev,
  52. struct radeon_sa_manager *sa_manager)
  53. {
  54. struct radeon_sa_bo *sa_bo, *tmp;
  55. if (!list_empty(&sa_manager->sa_bo)) {
  56. dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n");
  57. }
  58. list_for_each_entry_safe(sa_bo, tmp, &sa_manager->sa_bo, list) {
  59. list_del_init(&sa_bo->list);
  60. }
  61. radeon_bo_unref(&sa_manager->bo);
  62. sa_manager->size = 0;
  63. }
  64. int radeon_sa_bo_manager_start(struct radeon_device *rdev,
  65. struct radeon_sa_manager *sa_manager)
  66. {
  67. int r;
  68. if (sa_manager->bo == NULL) {
  69. dev_err(rdev->dev, "no bo for sa manager\n");
  70. return -EINVAL;
  71. }
  72. /* map the buffer */
  73. r = radeon_bo_reserve(sa_manager->bo, false);
  74. if (r) {
  75. dev_err(rdev->dev, "(%d) failed to reserve manager bo\n", r);
  76. return r;
  77. }
  78. r = radeon_bo_pin(sa_manager->bo, sa_manager->domain, &sa_manager->gpu_addr);
  79. if (r) {
  80. radeon_bo_unreserve(sa_manager->bo);
  81. dev_err(rdev->dev, "(%d) failed to pin manager bo\n", r);
  82. return r;
  83. }
  84. r = radeon_bo_kmap(sa_manager->bo, &sa_manager->cpu_ptr);
  85. radeon_bo_unreserve(sa_manager->bo);
  86. return r;
  87. }
  88. int radeon_sa_bo_manager_suspend(struct radeon_device *rdev,
  89. struct radeon_sa_manager *sa_manager)
  90. {
  91. int r;
  92. if (sa_manager->bo == NULL) {
  93. dev_err(rdev->dev, "no bo for sa manager\n");
  94. return -EINVAL;
  95. }
  96. r = radeon_bo_reserve(sa_manager->bo, false);
  97. if (!r) {
  98. radeon_bo_kunmap(sa_manager->bo);
  99. radeon_bo_unpin(sa_manager->bo);
  100. radeon_bo_unreserve(sa_manager->bo);
  101. }
  102. return r;
  103. }
  104. /*
  105. * Principe is simple, we keep a list of sub allocation in offset
  106. * order (first entry has offset == 0, last entry has the highest
  107. * offset).
  108. *
  109. * When allocating new object we first check if there is room at
  110. * the end total_size - (last_object_offset + last_object_size) >=
  111. * alloc_size. If so we allocate new object there.
  112. *
  113. * When there is not enough room at the end, we start waiting for
  114. * each sub object until we reach object_offset+object_size >=
  115. * alloc_size, this object then become the sub object we return.
  116. *
  117. * Alignment can't be bigger than page size
  118. */
  119. int radeon_sa_bo_new(struct radeon_device *rdev,
  120. struct radeon_sa_manager *sa_manager,
  121. struct radeon_sa_bo **sa_bo,
  122. unsigned size, unsigned align)
  123. {
  124. struct radeon_sa_bo *tmp;
  125. struct list_head *head;
  126. unsigned offset = 0, wasted = 0;
  127. BUG_ON(align > RADEON_GPU_PAGE_SIZE);
  128. BUG_ON(size > sa_manager->size);
  129. *sa_bo = kmalloc(sizeof(struct radeon_sa_bo), GFP_KERNEL);
  130. spin_lock(&sa_manager->lock);
  131. /* no one ? */
  132. head = sa_manager->sa_bo.prev;
  133. if (list_empty(&sa_manager->sa_bo)) {
  134. goto out;
  135. }
  136. /* look for a hole big enough */
  137. offset = 0;
  138. list_for_each_entry(tmp, &sa_manager->sa_bo, list) {
  139. /* room before this object ? */
  140. if (offset < tmp->soffset && (tmp->soffset - offset) >= size) {
  141. head = tmp->list.prev;
  142. goto out;
  143. }
  144. offset = tmp->eoffset;
  145. wasted = offset % align;
  146. if (wasted) {
  147. wasted = align - wasted;
  148. }
  149. offset += wasted;
  150. }
  151. /* room at the end ? */
  152. head = sa_manager->sa_bo.prev;
  153. tmp = list_entry(head, struct radeon_sa_bo, list);
  154. offset = tmp->eoffset;
  155. wasted = offset % align;
  156. if (wasted) {
  157. wasted = align - wasted;
  158. }
  159. offset += wasted;
  160. if ((sa_manager->size - offset) < size) {
  161. /* failed to find somethings big enough */
  162. spin_unlock(&sa_manager->lock);
  163. kfree(*sa_bo);
  164. *sa_bo = NULL;
  165. return -ENOMEM;
  166. }
  167. out:
  168. (*sa_bo)->manager = sa_manager;
  169. (*sa_bo)->soffset = offset;
  170. (*sa_bo)->eoffset = offset + size;
  171. list_add(&(*sa_bo)->list, head);
  172. spin_unlock(&sa_manager->lock);
  173. return 0;
  174. }
  175. void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo)
  176. {
  177. if (!sa_bo || !*sa_bo)
  178. return;
  179. spin_lock(&(*sa_bo)->manager->lock);
  180. list_del_init(&(*sa_bo)->list);
  181. spin_unlock(&(*sa_bo)->manager->lock);
  182. kfree(*sa_bo);
  183. *sa_bo = NULL;
  184. }
  185. #if defined(CONFIG_DEBUG_FS)
  186. void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
  187. struct seq_file *m)
  188. {
  189. struct radeon_sa_bo *i;
  190. spin_lock(&sa_manager->lock);
  191. list_for_each_entry(i, &sa_manager->sa_bo, list) {
  192. seq_printf(m, "[%08x %08x] size %4d [%p]\n",
  193. i->soffset, i->eoffset, i->eoffset - i->soffset, i);
  194. }
  195. spin_unlock(&sa_manager->lock);
  196. }
  197. #endif