radeon_sa.c 5.1 KB

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