radeon_cs.c 21 KB

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