radeon_cs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2008 Jerome Glisse.
  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 "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Jerome Glisse <glisse@freedesktop.org>
  26. */
  27. #include "drmP.h"
  28. #include "radeon_drm.h"
  29. #include "radeon_reg.h"
  30. #include "radeon.h"
  31. void r100_cs_dump_packet(struct radeon_cs_parser *p,
  32. struct radeon_cs_packet *pkt);
  33. int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
  34. {
  35. struct drm_device *ddev = p->rdev->ddev;
  36. struct radeon_cs_chunk *chunk;
  37. unsigned i, j;
  38. bool duplicate;
  39. if (p->chunk_relocs_idx == -1) {
  40. return 0;
  41. }
  42. chunk = &p->chunks[p->chunk_relocs_idx];
  43. /* FIXME: we assume that each relocs use 4 dwords */
  44. p->nrelocs = chunk->length_dw / 4;
  45. p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
  46. if (p->relocs_ptr == NULL) {
  47. return -ENOMEM;
  48. }
  49. p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  50. if (p->relocs == NULL) {
  51. return -ENOMEM;
  52. }
  53. for (i = 0; i < p->nrelocs; i++) {
  54. struct drm_radeon_cs_reloc *r;
  55. duplicate = false;
  56. r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
  57. for (j = 0; j < p->nrelocs; j++) {
  58. if (r->handle == p->relocs[j].handle) {
  59. p->relocs_ptr[i] = &p->relocs[j];
  60. duplicate = true;
  61. break;
  62. }
  63. }
  64. if (!duplicate) {
  65. p->relocs[i].gobj = drm_gem_object_lookup(ddev,
  66. p->filp,
  67. r->handle);
  68. if (p->relocs[i].gobj == NULL) {
  69. DRM_ERROR("gem object lookup failed 0x%x\n",
  70. r->handle);
  71. return -EINVAL;
  72. }
  73. p->relocs_ptr[i] = &p->relocs[i];
  74. p->relocs[i].robj = p->relocs[i].gobj->driver_private;
  75. p->relocs[i].lobj.robj = p->relocs[i].robj;
  76. p->relocs[i].lobj.rdomain = r->read_domains;
  77. p->relocs[i].lobj.wdomain = r->write_domain;
  78. p->relocs[i].handle = r->handle;
  79. p->relocs[i].flags = r->flags;
  80. INIT_LIST_HEAD(&p->relocs[i].lobj.list);
  81. radeon_object_list_add_object(&p->relocs[i].lobj,
  82. &p->validated);
  83. }
  84. }
  85. return radeon_object_list_validate(&p->validated, p->ib->fence);
  86. }
  87. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  88. {
  89. struct drm_radeon_cs *cs = data;
  90. uint64_t *chunk_array_ptr;
  91. unsigned size, i;
  92. if (!cs->num_chunks) {
  93. return 0;
  94. }
  95. /* get chunks */
  96. INIT_LIST_HEAD(&p->validated);
  97. p->idx = 0;
  98. p->chunk_ib_idx = -1;
  99. p->chunk_relocs_idx = -1;
  100. p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
  101. if (p->chunks_array == NULL) {
  102. return -ENOMEM;
  103. }
  104. chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
  105. if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
  106. sizeof(uint64_t)*cs->num_chunks)) {
  107. return -EFAULT;
  108. }
  109. p->nchunks = cs->num_chunks;
  110. p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
  111. if (p->chunks == NULL) {
  112. return -ENOMEM;
  113. }
  114. for (i = 0; i < p->nchunks; i++) {
  115. struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
  116. struct drm_radeon_cs_chunk user_chunk;
  117. uint32_t __user *cdata;
  118. chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
  119. if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
  120. sizeof(struct drm_radeon_cs_chunk))) {
  121. return -EFAULT;
  122. }
  123. p->chunks[i].chunk_id = user_chunk.chunk_id;
  124. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
  125. p->chunk_relocs_idx = i;
  126. }
  127. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
  128. p->chunk_ib_idx = i;
  129. }
  130. p->chunks[i].length_dw = user_chunk.length_dw;
  131. cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
  132. p->chunks[i].kdata = NULL;
  133. size = p->chunks[i].length_dw * sizeof(uint32_t);
  134. p->chunks[i].kdata = kzalloc(size, GFP_KERNEL);
  135. if (p->chunks[i].kdata == NULL) {
  136. return -ENOMEM;
  137. }
  138. if (DRM_COPY_FROM_USER(p->chunks[i].kdata, cdata, size)) {
  139. return -EFAULT;
  140. }
  141. }
  142. if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
  143. DRM_ERROR("cs IB too big: %d\n",
  144. p->chunks[p->chunk_ib_idx].length_dw);
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * cs_parser_fini() - clean parser states
  151. * @parser: parser structure holding parsing context.
  152. * @error: error number
  153. *
  154. * If error is set than unvalidate buffer, otherwise just free memory
  155. * used by parsing context.
  156. **/
  157. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  158. {
  159. unsigned i;
  160. if (error) {
  161. radeon_object_list_unvalidate(&parser->validated);
  162. } else {
  163. radeon_object_list_clean(&parser->validated);
  164. }
  165. for (i = 0; i < parser->nrelocs; i++) {
  166. if (parser->relocs[i].gobj) {
  167. mutex_lock(&parser->rdev->ddev->struct_mutex);
  168. drm_gem_object_unreference(parser->relocs[i].gobj);
  169. mutex_unlock(&parser->rdev->ddev->struct_mutex);
  170. }
  171. }
  172. kfree(parser->relocs);
  173. kfree(parser->relocs_ptr);
  174. for (i = 0; i < parser->nchunks; i++) {
  175. kfree(parser->chunks[i].kdata);
  176. }
  177. kfree(parser->chunks);
  178. kfree(parser->chunks_array);
  179. radeon_ib_free(parser->rdev, &parser->ib);
  180. }
  181. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  182. {
  183. struct radeon_device *rdev = dev->dev_private;
  184. struct radeon_cs_parser parser;
  185. struct radeon_cs_chunk *ib_chunk;
  186. int r;
  187. mutex_lock(&rdev->cs_mutex);
  188. if (rdev->gpu_lockup) {
  189. mutex_unlock(&rdev->cs_mutex);
  190. return -EINVAL;
  191. }
  192. /* initialize parser */
  193. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  194. parser.filp = filp;
  195. parser.rdev = rdev;
  196. r = radeon_cs_parser_init(&parser, data);
  197. if (r) {
  198. DRM_ERROR("Failed to initialize parser !\n");
  199. radeon_cs_parser_fini(&parser, r);
  200. mutex_unlock(&rdev->cs_mutex);
  201. return r;
  202. }
  203. r = radeon_ib_get(rdev, &parser.ib);
  204. if (r) {
  205. DRM_ERROR("Failed to get ib !\n");
  206. radeon_cs_parser_fini(&parser, r);
  207. mutex_unlock(&rdev->cs_mutex);
  208. return r;
  209. }
  210. r = radeon_cs_parser_relocs(&parser);
  211. if (r) {
  212. DRM_ERROR("Failed to parse relocation !\n");
  213. radeon_cs_parser_fini(&parser, r);
  214. mutex_unlock(&rdev->cs_mutex);
  215. return r;
  216. }
  217. /* Copy the packet into the IB, the parser will read from the
  218. * input memory (cached) and write to the IB (which can be
  219. * uncached). */
  220. ib_chunk = &parser.chunks[parser.chunk_ib_idx];
  221. parser.ib->length_dw = ib_chunk->length_dw;
  222. memcpy((void *)parser.ib->ptr, ib_chunk->kdata, ib_chunk->length_dw*4);
  223. r = radeon_cs_parse(&parser);
  224. if (r) {
  225. DRM_ERROR("Invalid command stream !\n");
  226. radeon_cs_parser_fini(&parser, r);
  227. mutex_unlock(&rdev->cs_mutex);
  228. return r;
  229. }
  230. r = radeon_ib_schedule(rdev, parser.ib);
  231. if (r) {
  232. DRM_ERROR("Faild to schedule IB !\n");
  233. }
  234. radeon_cs_parser_fini(&parser, r);
  235. mutex_unlock(&rdev->cs_mutex);
  236. return r;
  237. }