radeon_cs.c 21 KB

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