radeon_cs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 < i; 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 -ENOENT;
  72. }
  73. p->relocs_ptr[i] = &p->relocs[i];
  74. p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
  75. p->relocs[i].lobj.bo = p->relocs[i].robj;
  76. p->relocs[i].lobj.wdomain = r->write_domain;
  77. p->relocs[i].lobj.rdomain = r->read_domains;
  78. p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
  79. p->relocs[i].handle = r->handle;
  80. p->relocs[i].flags = r->flags;
  81. radeon_bo_list_add_object(&p->relocs[i].lobj,
  82. &p->validated);
  83. } else
  84. p->relocs[i].handle = 0;
  85. }
  86. return radeon_bo_list_validate(&p->validated);
  87. }
  88. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  89. {
  90. struct drm_radeon_cs *cs = data;
  91. uint64_t *chunk_array_ptr;
  92. unsigned size, i, flags = 0;
  93. if (!cs->num_chunks) {
  94. return 0;
  95. }
  96. /* get chunks */
  97. INIT_LIST_HEAD(&p->validated);
  98. p->idx = 0;
  99. p->chunk_ib_idx = -1;
  100. p->chunk_relocs_idx = -1;
  101. p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
  102. if (p->chunks_array == NULL) {
  103. return -ENOMEM;
  104. }
  105. chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
  106. if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
  107. sizeof(uint64_t)*cs->num_chunks)) {
  108. return -EFAULT;
  109. }
  110. p->nchunks = cs->num_chunks;
  111. p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
  112. if (p->chunks == NULL) {
  113. return -ENOMEM;
  114. }
  115. for (i = 0; i < p->nchunks; i++) {
  116. struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
  117. struct drm_radeon_cs_chunk user_chunk;
  118. uint32_t __user *cdata;
  119. chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
  120. if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
  121. sizeof(struct drm_radeon_cs_chunk))) {
  122. return -EFAULT;
  123. }
  124. p->chunks[i].length_dw = user_chunk.length_dw;
  125. p->chunks[i].kdata = NULL;
  126. p->chunks[i].chunk_id = user_chunk.chunk_id;
  127. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
  128. p->chunk_relocs_idx = i;
  129. }
  130. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
  131. p->chunk_ib_idx = i;
  132. /* zero length IB isn't useful */
  133. if (p->chunks[i].length_dw == 0)
  134. return -EINVAL;
  135. }
  136. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS &&
  137. !p->chunks[i].length_dw) {
  138. return -EINVAL;
  139. }
  140. p->chunks[i].length_dw = user_chunk.length_dw;
  141. p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
  142. cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
  143. if (p->chunks[i].chunk_id != RADEON_CHUNK_ID_IB) {
  144. size = p->chunks[i].length_dw * sizeof(uint32_t);
  145. p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
  146. if (p->chunks[i].kdata == NULL) {
  147. return -ENOMEM;
  148. }
  149. if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
  150. p->chunks[i].user_ptr, size)) {
  151. return -EFAULT;
  152. }
  153. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  154. flags = p->chunks[i].kdata[0];
  155. }
  156. } else {
  157. p->chunks[i].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  158. p->chunks[i].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  159. if (p->chunks[i].kpage[0] == NULL || p->chunks[i].kpage[1] == NULL) {
  160. kfree(p->chunks[i].kpage[0]);
  161. kfree(p->chunks[i].kpage[1]);
  162. return -ENOMEM;
  163. }
  164. p->chunks[i].kpage_idx[0] = -1;
  165. p->chunks[i].kpage_idx[1] = -1;
  166. p->chunks[i].last_copied_page = -1;
  167. p->chunks[i].last_page_index = ((p->chunks[i].length_dw * 4) - 1) / PAGE_SIZE;
  168. }
  169. }
  170. if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
  171. DRM_ERROR("cs IB too big: %d\n",
  172. p->chunks[p->chunk_ib_idx].length_dw);
  173. return -EINVAL;
  174. }
  175. p->keep_tiling_flags = (flags & RADEON_CS_KEEP_TILING_FLAGS) != 0;
  176. return 0;
  177. }
  178. /**
  179. * cs_parser_fini() - clean parser states
  180. * @parser: parser structure holding parsing context.
  181. * @error: error number
  182. *
  183. * If error is set than unvalidate buffer, otherwise just free memory
  184. * used by parsing context.
  185. **/
  186. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  187. {
  188. unsigned i;
  189. if (!error && parser->ib)
  190. ttm_eu_fence_buffer_objects(&parser->validated,
  191. parser->ib->fence);
  192. else
  193. ttm_eu_backoff_reservation(&parser->validated);
  194. if (parser->relocs != NULL) {
  195. for (i = 0; i < parser->nrelocs; i++) {
  196. if (parser->relocs[i].gobj)
  197. drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
  198. }
  199. }
  200. kfree(parser->track);
  201. kfree(parser->relocs);
  202. kfree(parser->relocs_ptr);
  203. for (i = 0; i < parser->nchunks; i++) {
  204. kfree(parser->chunks[i].kdata);
  205. kfree(parser->chunks[i].kpage[0]);
  206. kfree(parser->chunks[i].kpage[1]);
  207. }
  208. kfree(parser->chunks);
  209. kfree(parser->chunks_array);
  210. radeon_ib_free(parser->rdev, &parser->ib);
  211. }
  212. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  213. {
  214. struct radeon_device *rdev = dev->dev_private;
  215. struct radeon_cs_parser parser;
  216. struct radeon_cs_chunk *ib_chunk;
  217. int r;
  218. radeon_mutex_lock(&rdev->cs_mutex);
  219. /* initialize parser */
  220. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  221. parser.filp = filp;
  222. parser.rdev = rdev;
  223. parser.dev = rdev->dev;
  224. parser.family = rdev->family;
  225. r = radeon_cs_parser_init(&parser, data);
  226. if (r) {
  227. DRM_ERROR("Failed to initialize parser !\n");
  228. radeon_cs_parser_fini(&parser, r);
  229. radeon_mutex_unlock(&rdev->cs_mutex);
  230. return r;
  231. }
  232. r = radeon_ib_get(rdev, RADEON_RING_TYPE_GFX_INDEX, &parser.ib);
  233. if (r) {
  234. DRM_ERROR("Failed to get ib !\n");
  235. radeon_cs_parser_fini(&parser, r);
  236. radeon_mutex_unlock(&rdev->cs_mutex);
  237. return r;
  238. }
  239. r = radeon_cs_parser_relocs(&parser);
  240. if (r) {
  241. if (r != -ERESTARTSYS)
  242. DRM_ERROR("Failed to parse relocation %d!\n", r);
  243. radeon_cs_parser_fini(&parser, r);
  244. radeon_mutex_unlock(&rdev->cs_mutex);
  245. return r;
  246. }
  247. /* Copy the packet into the IB, the parser will read from the
  248. * input memory (cached) and write to the IB (which can be
  249. * uncached). */
  250. ib_chunk = &parser.chunks[parser.chunk_ib_idx];
  251. parser.ib->length_dw = ib_chunk->length_dw;
  252. r = radeon_cs_parse(&parser);
  253. if (r || parser.parser_error) {
  254. DRM_ERROR("Invalid command stream !\n");
  255. radeon_cs_parser_fini(&parser, r);
  256. radeon_mutex_unlock(&rdev->cs_mutex);
  257. return r;
  258. }
  259. r = radeon_cs_finish_pages(&parser);
  260. if (r) {
  261. DRM_ERROR("Invalid command stream !\n");
  262. radeon_cs_parser_fini(&parser, r);
  263. radeon_mutex_unlock(&rdev->cs_mutex);
  264. return r;
  265. }
  266. r = radeon_ib_schedule(rdev, parser.ib);
  267. if (r) {
  268. DRM_ERROR("Failed to schedule IB !\n");
  269. }
  270. radeon_cs_parser_fini(&parser, r);
  271. radeon_mutex_unlock(&rdev->cs_mutex);
  272. return r;
  273. }
  274. int radeon_cs_finish_pages(struct radeon_cs_parser *p)
  275. {
  276. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  277. int i;
  278. int size = PAGE_SIZE;
  279. for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
  280. if (i == ibc->last_page_index) {
  281. size = (ibc->length_dw * 4) % PAGE_SIZE;
  282. if (size == 0)
  283. size = PAGE_SIZE;
  284. }
  285. if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
  286. ibc->user_ptr + (i * PAGE_SIZE),
  287. size))
  288. return -EFAULT;
  289. }
  290. return 0;
  291. }
  292. int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
  293. {
  294. int new_page;
  295. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  296. int i;
  297. int size = PAGE_SIZE;
  298. for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
  299. if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
  300. ibc->user_ptr + (i * PAGE_SIZE),
  301. PAGE_SIZE)) {
  302. p->parser_error = -EFAULT;
  303. return 0;
  304. }
  305. }
  306. new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
  307. if (pg_idx == ibc->last_page_index) {
  308. size = (ibc->length_dw * 4) % PAGE_SIZE;
  309. if (size == 0)
  310. size = PAGE_SIZE;
  311. }
  312. if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
  313. ibc->user_ptr + (pg_idx * PAGE_SIZE),
  314. size)) {
  315. p->parser_error = -EFAULT;
  316. return 0;
  317. }
  318. /* copy to IB here */
  319. memcpy((void *)(p->ib->ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
  320. ibc->last_copied_page = pg_idx;
  321. ibc->kpage_idx[new_page] = pg_idx;
  322. return new_page;
  323. }