radeon_cs_space.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright © 2009 Red Hat Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a 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,
  14. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
  17. * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. */
  26. /*
  27. */
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <stdlib.h>
  31. #include "libdrm_macros.h"
  32. #include "radeon_cs.h"
  33. #include "radeon_bo_int.h"
  34. #include "radeon_cs_int.h"
  35. struct rad_sizes {
  36. int32_t op_read;
  37. int32_t op_gart_write;
  38. int32_t op_vram_write;
  39. };
  40. static inline int radeon_cs_setup_bo(struct radeon_cs_space_check *sc, struct rad_sizes *sizes)
  41. {
  42. uint32_t read_domains, write_domain;
  43. struct radeon_bo_int *bo;
  44. bo = sc->bo;
  45. sc->new_accounted = 0;
  46. read_domains = sc->read_domains;
  47. write_domain = sc->write_domain;
  48. /* legacy needs a static check */
  49. if (radeon_bo_is_static((struct radeon_bo *)sc->bo)) {
  50. bo->space_accounted = sc->new_accounted = (read_domains << 16) | write_domain;
  51. return 0;
  52. }
  53. /* already accounted this bo */
  54. if (write_domain && (write_domain == bo->space_accounted)) {
  55. sc->new_accounted = bo->space_accounted;
  56. return 0;
  57. }
  58. if (read_domains && ((read_domains << 16) == bo->space_accounted)) {
  59. sc->new_accounted = bo->space_accounted;
  60. return 0;
  61. }
  62. if (bo->space_accounted == 0) {
  63. if (write_domain) {
  64. if (write_domain == RADEON_GEM_DOMAIN_VRAM)
  65. sizes->op_vram_write += bo->size;
  66. else if (write_domain == RADEON_GEM_DOMAIN_GTT)
  67. sizes->op_gart_write += bo->size;
  68. sc->new_accounted = write_domain;
  69. } else {
  70. sizes->op_read += bo->size;
  71. sc->new_accounted = read_domains << 16;
  72. }
  73. } else {
  74. uint16_t old_read, old_write;
  75. old_read = bo->space_accounted >> 16;
  76. old_write = bo->space_accounted & 0xffff;
  77. if (write_domain && (old_read & write_domain)) {
  78. sc->new_accounted = write_domain;
  79. /* moving from read to a write domain */
  80. if (write_domain == RADEON_GEM_DOMAIN_VRAM) {
  81. sizes->op_read -= bo->size;
  82. sizes->op_vram_write += bo->size;
  83. } else if (write_domain == RADEON_GEM_DOMAIN_GTT) {
  84. sizes->op_read -= bo->size;
  85. sizes->op_gart_write += bo->size;
  86. }
  87. } else if (read_domains & old_write) {
  88. sc->new_accounted = bo->space_accounted & 0xffff;
  89. } else {
  90. /* rewrite the domains */
  91. if (write_domain != old_write)
  92. fprintf(stderr,"WRITE DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, write_domain, old_write);
  93. if (read_domains != old_read)
  94. fprintf(stderr,"READ DOMAIN RELOC FAILURE 0x%x %d %d\n", bo->handle, read_domains, old_read);
  95. return RADEON_CS_SPACE_FLUSH;
  96. }
  97. }
  98. return 0;
  99. }
  100. static int radeon_cs_do_space_check(struct radeon_cs_int *cs, struct radeon_cs_space_check *new_tmp)
  101. {
  102. struct radeon_cs_manager *csm = cs->csm;
  103. int i;
  104. struct radeon_bo_int *bo;
  105. struct rad_sizes sizes;
  106. int ret;
  107. /* check the totals for this operation */
  108. if (cs->bo_count == 0 && !new_tmp)
  109. return 0;
  110. memset(&sizes, 0, sizeof(struct rad_sizes));
  111. /* prepare */
  112. for (i = 0; i < cs->bo_count; i++) {
  113. ret = radeon_cs_setup_bo(&cs->bos[i], &sizes);
  114. if (ret)
  115. return ret;
  116. }
  117. if (new_tmp) {
  118. ret = radeon_cs_setup_bo(new_tmp, &sizes);
  119. if (ret)
  120. return ret;
  121. }
  122. if (sizes.op_read < 0)
  123. sizes.op_read = 0;
  124. /* check sizes - operation first */
  125. if ((sizes.op_read + sizes.op_gart_write > csm->gart_limit) ||
  126. (sizes.op_vram_write > csm->vram_limit)) {
  127. return RADEON_CS_SPACE_OP_TO_BIG;
  128. }
  129. if (((csm->vram_write_used + sizes.op_vram_write) > csm->vram_limit) ||
  130. ((csm->read_used + csm->gart_write_used + sizes.op_gart_write + sizes.op_read) > csm->gart_limit)) {
  131. return RADEON_CS_SPACE_FLUSH;
  132. }
  133. csm->gart_write_used += sizes.op_gart_write;
  134. csm->vram_write_used += sizes.op_vram_write;
  135. csm->read_used += sizes.op_read;
  136. /* commit */
  137. for (i = 0; i < cs->bo_count; i++) {
  138. bo = cs->bos[i].bo;
  139. bo->space_accounted = cs->bos[i].new_accounted;
  140. }
  141. if (new_tmp)
  142. new_tmp->bo->space_accounted = new_tmp->new_accounted;
  143. return RADEON_CS_SPACE_OK;
  144. }
  145. drm_public void
  146. radeon_cs_space_add_persistent_bo(struct radeon_cs *cs, struct radeon_bo *bo,
  147. uint32_t read_domains, uint32_t write_domain)
  148. {
  149. struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
  150. struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
  151. int i;
  152. for (i = 0; i < csi->bo_count; i++) {
  153. if (csi->bos[i].bo == boi &&
  154. csi->bos[i].read_domains == read_domains &&
  155. csi->bos[i].write_domain == write_domain)
  156. return;
  157. }
  158. radeon_bo_ref(bo);
  159. i = csi->bo_count;
  160. csi->bos[i].bo = boi;
  161. csi->bos[i].read_domains = read_domains;
  162. csi->bos[i].write_domain = write_domain;
  163. csi->bos[i].new_accounted = 0;
  164. csi->bo_count++;
  165. assert(csi->bo_count < MAX_SPACE_BOS);
  166. }
  167. static int radeon_cs_check_space_internal(struct radeon_cs_int *cs,
  168. struct radeon_cs_space_check *tmp_bo)
  169. {
  170. int ret;
  171. int flushed = 0;
  172. again:
  173. ret = radeon_cs_do_space_check(cs, tmp_bo);
  174. if (ret == RADEON_CS_SPACE_OP_TO_BIG)
  175. return -1;
  176. if (ret == RADEON_CS_SPACE_FLUSH) {
  177. (*cs->space_flush_fn)(cs->space_flush_data);
  178. if (flushed)
  179. return -1;
  180. flushed = 1;
  181. goto again;
  182. }
  183. return 0;
  184. }
  185. drm_public int
  186. radeon_cs_space_check_with_bo(struct radeon_cs *cs, struct radeon_bo *bo,
  187. uint32_t read_domains, uint32_t write_domain)
  188. {
  189. struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
  190. struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
  191. struct radeon_cs_space_check temp_bo;
  192. int ret = 0;
  193. if (bo) {
  194. temp_bo.bo = boi;
  195. temp_bo.read_domains = read_domains;
  196. temp_bo.write_domain = write_domain;
  197. temp_bo.new_accounted = 0;
  198. }
  199. ret = radeon_cs_check_space_internal(csi, bo ? &temp_bo : NULL);
  200. return ret;
  201. }
  202. drm_public int radeon_cs_space_check(struct radeon_cs *cs)
  203. {
  204. struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
  205. return radeon_cs_check_space_internal(csi, NULL);
  206. }
  207. drm_public void radeon_cs_space_reset_bos(struct radeon_cs *cs)
  208. {
  209. struct radeon_cs_int *csi = (struct radeon_cs_int *)cs;
  210. int i;
  211. for (i = 0; i < csi->bo_count; i++) {
  212. radeon_bo_unref((struct radeon_bo *)csi->bos[i].bo);
  213. csi->bos[i].bo = NULL;
  214. csi->bos[i].read_domains = 0;
  215. csi->bos[i].write_domain = 0;
  216. csi->bos[i].new_accounted = 0;
  217. }
  218. csi->bo_count = 0;
  219. }