radeon_cs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 <drm/drmP.h>
  28. #include <drm/radeon_drm.h>
  29. #include "radeon_reg.h"
  30. #include "radeon.h"
  31. #include "radeon_trace.h"
  32. static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
  33. {
  34. struct drm_device *ddev = p->rdev->ddev;
  35. struct radeon_cs_chunk *chunk;
  36. unsigned i, j;
  37. bool duplicate;
  38. if (p->chunk_relocs_idx == -1) {
  39. return 0;
  40. }
  41. chunk = &p->chunks[p->chunk_relocs_idx];
  42. p->dma_reloc_idx = 0;
  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].handle = 0;
  66. continue;
  67. }
  68. p->relocs[i].gobj = drm_gem_object_lookup(ddev, p->filp,
  69. r->handle);
  70. if (p->relocs[i].gobj == NULL) {
  71. DRM_ERROR("gem object lookup failed 0x%x\n",
  72. r->handle);
  73. return -ENOENT;
  74. }
  75. p->relocs_ptr[i] = &p->relocs[i];
  76. p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
  77. p->relocs[i].lobj.bo = p->relocs[i].robj;
  78. p->relocs[i].lobj.written = !!r->write_domain;
  79. /* the first reloc of an UVD job is the msg and that must be in
  80. VRAM, also but everything into VRAM on AGP cards to avoid
  81. image corruptions */
  82. if (p->ring == R600_RING_TYPE_UVD_INDEX &&
  83. (i == 0 || drm_pci_device_is_agp(p->rdev->ddev))) {
  84. /* TODO: is this still needed for NI+ ? */
  85. p->relocs[i].lobj.domain =
  86. RADEON_GEM_DOMAIN_VRAM;
  87. p->relocs[i].lobj.alt_domain =
  88. RADEON_GEM_DOMAIN_VRAM;
  89. } else {
  90. uint32_t domain = r->write_domain ?
  91. r->write_domain : r->read_domains;
  92. p->relocs[i].lobj.domain = domain;
  93. if (domain == RADEON_GEM_DOMAIN_VRAM)
  94. domain |= RADEON_GEM_DOMAIN_GTT;
  95. p->relocs[i].lobj.alt_domain = domain;
  96. }
  97. p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
  98. p->relocs[i].handle = r->handle;
  99. radeon_bo_list_add_object(&p->relocs[i].lobj,
  100. &p->validated);
  101. }
  102. return radeon_bo_list_validate(&p->ticket, &p->validated, p->ring);
  103. }
  104. static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
  105. {
  106. p->priority = priority;
  107. switch (ring) {
  108. default:
  109. DRM_ERROR("unknown ring id: %d\n", ring);
  110. return -EINVAL;
  111. case RADEON_CS_RING_GFX:
  112. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  113. break;
  114. case RADEON_CS_RING_COMPUTE:
  115. if (p->rdev->family >= CHIP_TAHITI) {
  116. if (p->priority > 0)
  117. p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
  118. else
  119. p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
  120. } else
  121. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  122. break;
  123. case RADEON_CS_RING_DMA:
  124. if (p->rdev->family >= CHIP_CAYMAN) {
  125. if (p->priority > 0)
  126. p->ring = R600_RING_TYPE_DMA_INDEX;
  127. else
  128. p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
  129. } else if (p->rdev->family >= CHIP_R600) {
  130. p->ring = R600_RING_TYPE_DMA_INDEX;
  131. } else {
  132. return -EINVAL;
  133. }
  134. break;
  135. case RADEON_CS_RING_UVD:
  136. p->ring = R600_RING_TYPE_UVD_INDEX;
  137. break;
  138. }
  139. return 0;
  140. }
  141. static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
  142. {
  143. int i;
  144. for (i = 0; i < p->nrelocs; i++) {
  145. if (!p->relocs[i].robj)
  146. continue;
  147. radeon_semaphore_sync_to(p->ib.semaphore,
  148. p->relocs[i].robj->tbo.sync_obj);
  149. }
  150. }
  151. /* XXX: note that this is called from the legacy UMS CS ioctl as well */
  152. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  153. {
  154. struct drm_radeon_cs *cs = data;
  155. uint64_t *chunk_array_ptr;
  156. unsigned size, i;
  157. u32 ring = RADEON_CS_RING_GFX;
  158. s32 priority = 0;
  159. if (!cs->num_chunks) {
  160. return 0;
  161. }
  162. /* get chunks */
  163. INIT_LIST_HEAD(&p->validated);
  164. p->idx = 0;
  165. p->ib.sa_bo = NULL;
  166. p->ib.semaphore = NULL;
  167. p->const_ib.sa_bo = NULL;
  168. p->const_ib.semaphore = NULL;
  169. p->chunk_ib_idx = -1;
  170. p->chunk_relocs_idx = -1;
  171. p->chunk_flags_idx = -1;
  172. p->chunk_const_ib_idx = -1;
  173. p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
  174. if (p->chunks_array == NULL) {
  175. return -ENOMEM;
  176. }
  177. chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
  178. if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
  179. sizeof(uint64_t)*cs->num_chunks)) {
  180. return -EFAULT;
  181. }
  182. p->cs_flags = 0;
  183. p->nchunks = cs->num_chunks;
  184. p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
  185. if (p->chunks == NULL) {
  186. return -ENOMEM;
  187. }
  188. for (i = 0; i < p->nchunks; i++) {
  189. struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
  190. struct drm_radeon_cs_chunk user_chunk;
  191. uint32_t __user *cdata;
  192. chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
  193. if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
  194. sizeof(struct drm_radeon_cs_chunk))) {
  195. return -EFAULT;
  196. }
  197. p->chunks[i].length_dw = user_chunk.length_dw;
  198. p->chunks[i].chunk_id = user_chunk.chunk_id;
  199. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
  200. p->chunk_relocs_idx = i;
  201. }
  202. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
  203. p->chunk_ib_idx = i;
  204. /* zero length IB isn't useful */
  205. if (p->chunks[i].length_dw == 0)
  206. return -EINVAL;
  207. }
  208. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
  209. p->chunk_const_ib_idx = i;
  210. /* zero length CONST IB isn't useful */
  211. if (p->chunks[i].length_dw == 0)
  212. return -EINVAL;
  213. }
  214. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  215. p->chunk_flags_idx = i;
  216. /* zero length flags aren't useful */
  217. if (p->chunks[i].length_dw == 0)
  218. return -EINVAL;
  219. }
  220. size = p->chunks[i].length_dw;
  221. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  222. p->chunks[i].user_ptr = cdata;
  223. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB)
  224. continue;
  225. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
  226. if (!p->rdev || !(p->rdev->flags & RADEON_IS_AGP))
  227. continue;
  228. }
  229. p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
  230. size *= sizeof(uint32_t);
  231. if (p->chunks[i].kdata == NULL) {
  232. return -ENOMEM;
  233. }
  234. if (DRM_COPY_FROM_USER(p->chunks[i].kdata, cdata, size)) {
  235. return -EFAULT;
  236. }
  237. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  238. p->cs_flags = p->chunks[i].kdata[0];
  239. if (p->chunks[i].length_dw > 1)
  240. ring = p->chunks[i].kdata[1];
  241. if (p->chunks[i].length_dw > 2)
  242. priority = (s32)p->chunks[i].kdata[2];
  243. }
  244. }
  245. /* these are KMS only */
  246. if (p->rdev) {
  247. if ((p->cs_flags & RADEON_CS_USE_VM) &&
  248. !p->rdev->vm_manager.enabled) {
  249. DRM_ERROR("VM not active on asic!\n");
  250. return -EINVAL;
  251. }
  252. if (radeon_cs_get_ring(p, ring, priority))
  253. return -EINVAL;
  254. /* we only support VM on some SI+ rings */
  255. if ((p->rdev->asic->ring[p->ring]->cs_parse == NULL) &&
  256. ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
  257. DRM_ERROR("Ring %d requires VM!\n", p->ring);
  258. return -EINVAL;
  259. }
  260. }
  261. return 0;
  262. }
  263. /**
  264. * cs_parser_fini() - clean parser states
  265. * @parser: parser structure holding parsing context.
  266. * @error: error number
  267. *
  268. * If error is set than unvalidate buffer, otherwise just free memory
  269. * used by parsing context.
  270. **/
  271. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error, bool backoff)
  272. {
  273. unsigned i;
  274. if (!error) {
  275. ttm_eu_fence_buffer_objects(&parser->ticket,
  276. &parser->validated,
  277. parser->ib.fence);
  278. } else if (backoff) {
  279. ttm_eu_backoff_reservation(&parser->ticket,
  280. &parser->validated);
  281. }
  282. if (parser->relocs != NULL) {
  283. for (i = 0; i < parser->nrelocs; i++) {
  284. if (parser->relocs[i].gobj)
  285. drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
  286. }
  287. }
  288. kfree(parser->track);
  289. kfree(parser->relocs);
  290. kfree(parser->relocs_ptr);
  291. for (i = 0; i < parser->nchunks; i++)
  292. drm_free_large(parser->chunks[i].kdata);
  293. kfree(parser->chunks);
  294. kfree(parser->chunks_array);
  295. radeon_ib_free(parser->rdev, &parser->ib);
  296. radeon_ib_free(parser->rdev, &parser->const_ib);
  297. }
  298. static int radeon_cs_ib_chunk(struct radeon_device *rdev,
  299. struct radeon_cs_parser *parser)
  300. {
  301. int r;
  302. if (parser->chunk_ib_idx == -1)
  303. return 0;
  304. if (parser->cs_flags & RADEON_CS_USE_VM)
  305. return 0;
  306. r = radeon_cs_parse(rdev, parser->ring, parser);
  307. if (r || parser->parser_error) {
  308. DRM_ERROR("Invalid command stream !\n");
  309. return r;
  310. }
  311. if (parser->ring == R600_RING_TYPE_UVD_INDEX)
  312. radeon_uvd_note_usage(rdev);
  313. radeon_cs_sync_rings(parser);
  314. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  315. if (r) {
  316. DRM_ERROR("Failed to schedule IB !\n");
  317. }
  318. return r;
  319. }
  320. static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
  321. struct radeon_vm *vm)
  322. {
  323. struct radeon_device *rdev = parser->rdev;
  324. struct radeon_bo_list *lobj;
  325. struct radeon_bo *bo;
  326. int r;
  327. r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
  328. if (r) {
  329. return r;
  330. }
  331. list_for_each_entry(lobj, &parser->validated, tv.head) {
  332. bo = lobj->bo;
  333. r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
  334. if (r) {
  335. return r;
  336. }
  337. }
  338. return 0;
  339. }
  340. static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
  341. struct radeon_cs_parser *parser)
  342. {
  343. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  344. struct radeon_vm *vm = &fpriv->vm;
  345. int r;
  346. if (parser->chunk_ib_idx == -1)
  347. return 0;
  348. if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
  349. return 0;
  350. if (parser->const_ib.length_dw) {
  351. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
  352. if (r) {
  353. return r;
  354. }
  355. }
  356. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
  357. if (r) {
  358. return r;
  359. }
  360. if (parser->ring == R600_RING_TYPE_UVD_INDEX)
  361. radeon_uvd_note_usage(rdev);
  362. mutex_lock(&rdev->vm_manager.lock);
  363. mutex_lock(&vm->mutex);
  364. r = radeon_vm_alloc_pt(rdev, vm);
  365. if (r) {
  366. goto out;
  367. }
  368. r = radeon_bo_vm_update_pte(parser, vm);
  369. if (r) {
  370. goto out;
  371. }
  372. radeon_cs_sync_rings(parser);
  373. radeon_semaphore_sync_to(parser->ib.semaphore, vm->fence);
  374. radeon_semaphore_sync_to(parser->ib.semaphore,
  375. radeon_vm_grab_id(rdev, vm, parser->ring));
  376. if ((rdev->family >= CHIP_TAHITI) &&
  377. (parser->chunk_const_ib_idx != -1)) {
  378. r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
  379. } else {
  380. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  381. }
  382. if (!r) {
  383. radeon_vm_fence(rdev, vm, parser->ib.fence);
  384. }
  385. out:
  386. radeon_vm_add_to_lru(rdev, vm);
  387. mutex_unlock(&vm->mutex);
  388. mutex_unlock(&rdev->vm_manager.lock);
  389. return r;
  390. }
  391. static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
  392. {
  393. if (r == -EDEADLK) {
  394. r = radeon_gpu_reset(rdev);
  395. if (!r)
  396. r = -EAGAIN;
  397. }
  398. return r;
  399. }
  400. static int radeon_cs_ib_fill(struct radeon_device *rdev, struct radeon_cs_parser *parser)
  401. {
  402. struct radeon_cs_chunk *ib_chunk;
  403. struct radeon_vm *vm = NULL;
  404. int r;
  405. if (parser->chunk_ib_idx == -1)
  406. return 0;
  407. if (parser->cs_flags & RADEON_CS_USE_VM) {
  408. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  409. vm = &fpriv->vm;
  410. if ((rdev->family >= CHIP_TAHITI) &&
  411. (parser->chunk_const_ib_idx != -1)) {
  412. ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
  413. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  414. DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
  415. return -EINVAL;
  416. }
  417. r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
  418. vm, ib_chunk->length_dw * 4);
  419. if (r) {
  420. DRM_ERROR("Failed to get const ib !\n");
  421. return r;
  422. }
  423. parser->const_ib.is_const_ib = true;
  424. parser->const_ib.length_dw = ib_chunk->length_dw;
  425. if (DRM_COPY_FROM_USER(parser->const_ib.ptr,
  426. ib_chunk->user_ptr,
  427. ib_chunk->length_dw * 4))
  428. return -EFAULT;
  429. }
  430. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  431. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  432. DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
  433. return -EINVAL;
  434. }
  435. }
  436. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  437. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  438. vm, ib_chunk->length_dw * 4);
  439. if (r) {
  440. DRM_ERROR("Failed to get ib !\n");
  441. return r;
  442. }
  443. parser->ib.length_dw = ib_chunk->length_dw;
  444. if (ib_chunk->kdata)
  445. memcpy(parser->ib.ptr, ib_chunk->kdata, ib_chunk->length_dw * 4);
  446. else if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr, ib_chunk->length_dw * 4))
  447. return -EFAULT;
  448. return 0;
  449. }
  450. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  451. {
  452. struct radeon_device *rdev = dev->dev_private;
  453. struct radeon_cs_parser parser;
  454. int r;
  455. down_read(&rdev->exclusive_lock);
  456. if (!rdev->accel_working) {
  457. up_read(&rdev->exclusive_lock);
  458. return -EBUSY;
  459. }
  460. /* initialize parser */
  461. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  462. parser.filp = filp;
  463. parser.rdev = rdev;
  464. parser.dev = rdev->dev;
  465. parser.family = rdev->family;
  466. r = radeon_cs_parser_init(&parser, data);
  467. if (r) {
  468. DRM_ERROR("Failed to initialize parser !\n");
  469. radeon_cs_parser_fini(&parser, r, false);
  470. up_read(&rdev->exclusive_lock);
  471. r = radeon_cs_handle_lockup(rdev, r);
  472. return r;
  473. }
  474. r = radeon_cs_ib_fill(rdev, &parser);
  475. if (!r) {
  476. r = radeon_cs_parser_relocs(&parser);
  477. if (r && r != -ERESTARTSYS)
  478. DRM_ERROR("Failed to parse relocation %d!\n", r);
  479. }
  480. if (r) {
  481. radeon_cs_parser_fini(&parser, r, false);
  482. up_read(&rdev->exclusive_lock);
  483. r = radeon_cs_handle_lockup(rdev, r);
  484. return r;
  485. }
  486. trace_radeon_cs(&parser);
  487. r = radeon_cs_ib_chunk(rdev, &parser);
  488. if (r) {
  489. goto out;
  490. }
  491. r = radeon_cs_ib_vm_chunk(rdev, &parser);
  492. if (r) {
  493. goto out;
  494. }
  495. out:
  496. radeon_cs_parser_fini(&parser, r, true);
  497. up_read(&rdev->exclusive_lock);
  498. r = radeon_cs_handle_lockup(rdev, r);
  499. return r;
  500. }
  501. /**
  502. * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
  503. * @parser: parser structure holding parsing context.
  504. * @pkt: where to store packet information
  505. *
  506. * Assume that chunk_ib_index is properly set. Will return -EINVAL
  507. * if packet is bigger than remaining ib size. or if packets is unknown.
  508. **/
  509. int radeon_cs_packet_parse(struct radeon_cs_parser *p,
  510. struct radeon_cs_packet *pkt,
  511. unsigned idx)
  512. {
  513. struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
  514. struct radeon_device *rdev = p->rdev;
  515. uint32_t header;
  516. if (idx >= ib_chunk->length_dw) {
  517. DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
  518. idx, ib_chunk->length_dw);
  519. return -EINVAL;
  520. }
  521. header = radeon_get_ib_value(p, idx);
  522. pkt->idx = idx;
  523. pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
  524. pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
  525. pkt->one_reg_wr = 0;
  526. switch (pkt->type) {
  527. case RADEON_PACKET_TYPE0:
  528. if (rdev->family < CHIP_R600) {
  529. pkt->reg = R100_CP_PACKET0_GET_REG(header);
  530. pkt->one_reg_wr =
  531. RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
  532. } else
  533. pkt->reg = R600_CP_PACKET0_GET_REG(header);
  534. break;
  535. case RADEON_PACKET_TYPE3:
  536. pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
  537. break;
  538. case RADEON_PACKET_TYPE2:
  539. pkt->count = -1;
  540. break;
  541. default:
  542. DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
  543. return -EINVAL;
  544. }
  545. if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
  546. DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
  547. pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
  548. return -EINVAL;
  549. }
  550. return 0;
  551. }
  552. /**
  553. * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
  554. * @p: structure holding the parser context.
  555. *
  556. * Check if the next packet is NOP relocation packet3.
  557. **/
  558. bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
  559. {
  560. struct radeon_cs_packet p3reloc;
  561. int r;
  562. r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
  563. if (r)
  564. return false;
  565. if (p3reloc.type != RADEON_PACKET_TYPE3)
  566. return false;
  567. if (p3reloc.opcode != RADEON_PACKET3_NOP)
  568. return false;
  569. return true;
  570. }
  571. /**
  572. * radeon_cs_dump_packet() - dump raw packet context
  573. * @p: structure holding the parser context.
  574. * @pkt: structure holding the packet.
  575. *
  576. * Used mostly for debugging and error reporting.
  577. **/
  578. void radeon_cs_dump_packet(struct radeon_cs_parser *p,
  579. struct radeon_cs_packet *pkt)
  580. {
  581. volatile uint32_t *ib;
  582. unsigned i;
  583. unsigned idx;
  584. ib = p->ib.ptr;
  585. idx = pkt->idx;
  586. for (i = 0; i <= (pkt->count + 1); i++, idx++)
  587. DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
  588. }
  589. /**
  590. * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
  591. * @parser: parser structure holding parsing context.
  592. * @data: pointer to relocation data
  593. * @offset_start: starting offset
  594. * @offset_mask: offset mask (to align start offset on)
  595. * @reloc: reloc informations
  596. *
  597. * Check if next packet is relocation packet3, do bo validation and compute
  598. * GPU offset using the provided start.
  599. **/
  600. int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
  601. struct radeon_cs_reloc **cs_reloc,
  602. int nomm)
  603. {
  604. struct radeon_cs_chunk *relocs_chunk;
  605. struct radeon_cs_packet p3reloc;
  606. unsigned idx;
  607. int r;
  608. if (p->chunk_relocs_idx == -1) {
  609. DRM_ERROR("No relocation chunk !\n");
  610. return -EINVAL;
  611. }
  612. *cs_reloc = NULL;
  613. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  614. r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
  615. if (r)
  616. return r;
  617. p->idx += p3reloc.count + 2;
  618. if (p3reloc.type != RADEON_PACKET_TYPE3 ||
  619. p3reloc.opcode != RADEON_PACKET3_NOP) {
  620. DRM_ERROR("No packet3 for relocation for packet at %d.\n",
  621. p3reloc.idx);
  622. radeon_cs_dump_packet(p, &p3reloc);
  623. return -EINVAL;
  624. }
  625. idx = radeon_get_ib_value(p, p3reloc.idx + 1);
  626. if (idx >= relocs_chunk->length_dw) {
  627. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  628. idx, relocs_chunk->length_dw);
  629. radeon_cs_dump_packet(p, &p3reloc);
  630. return -EINVAL;
  631. }
  632. /* FIXME: we assume reloc size is 4 dwords */
  633. if (nomm) {
  634. *cs_reloc = p->relocs;
  635. (*cs_reloc)->lobj.gpu_offset =
  636. (u64)relocs_chunk->kdata[idx + 3] << 32;
  637. (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
  638. } else
  639. *cs_reloc = p->relocs_ptr[(idx / 4)];
  640. return 0;
  641. }