radeon_cs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. void r100_cs_dump_packet(struct radeon_cs_parser *p,
  32. struct radeon_cs_packet *pkt);
  33. static 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. p->dma_reloc_idx = 0;
  44. /* FIXME: we assume that each relocs use 4 dwords */
  45. p->nrelocs = chunk->length_dw / 4;
  46. p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
  47. if (p->relocs_ptr == NULL) {
  48. return -ENOMEM;
  49. }
  50. p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  51. if (p->relocs == NULL) {
  52. return -ENOMEM;
  53. }
  54. for (i = 0; i < p->nrelocs; i++) {
  55. struct drm_radeon_cs_reloc *r;
  56. duplicate = false;
  57. r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
  58. for (j = 0; j < i; j++) {
  59. if (r->handle == p->relocs[j].handle) {
  60. p->relocs_ptr[i] = &p->relocs[j];
  61. duplicate = true;
  62. break;
  63. }
  64. }
  65. if (!duplicate) {
  66. p->relocs[i].gobj = drm_gem_object_lookup(ddev,
  67. p->filp,
  68. r->handle);
  69. if (p->relocs[i].gobj == NULL) {
  70. DRM_ERROR("gem object lookup failed 0x%x\n",
  71. r->handle);
  72. return -ENOENT;
  73. }
  74. p->relocs_ptr[i] = &p->relocs[i];
  75. p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
  76. p->relocs[i].lobj.bo = p->relocs[i].robj;
  77. p->relocs[i].lobj.wdomain = r->write_domain;
  78. p->relocs[i].lobj.rdomain = r->read_domains;
  79. p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
  80. p->relocs[i].handle = r->handle;
  81. p->relocs[i].flags = r->flags;
  82. radeon_bo_list_add_object(&p->relocs[i].lobj,
  83. &p->validated);
  84. } else
  85. p->relocs[i].handle = 0;
  86. }
  87. return radeon_bo_list_validate(&p->validated);
  88. }
  89. static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
  90. {
  91. p->priority = priority;
  92. switch (ring) {
  93. default:
  94. DRM_ERROR("unknown ring id: %d\n", ring);
  95. return -EINVAL;
  96. case RADEON_CS_RING_GFX:
  97. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  98. break;
  99. case RADEON_CS_RING_COMPUTE:
  100. if (p->rdev->family >= CHIP_TAHITI) {
  101. if (p->priority > 0)
  102. p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
  103. else
  104. p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
  105. } else
  106. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  107. break;
  108. case RADEON_CS_RING_DMA:
  109. if (p->rdev->family >= CHIP_CAYMAN) {
  110. if (p->priority > 0)
  111. p->ring = R600_RING_TYPE_DMA_INDEX;
  112. else
  113. p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
  114. } else if (p->rdev->family >= CHIP_R600) {
  115. p->ring = R600_RING_TYPE_DMA_INDEX;
  116. } else {
  117. return -EINVAL;
  118. }
  119. break;
  120. }
  121. return 0;
  122. }
  123. static void radeon_cs_sync_to(struct radeon_cs_parser *p,
  124. struct radeon_fence *fence)
  125. {
  126. struct radeon_fence *other;
  127. if (!fence)
  128. return;
  129. other = p->ib.sync_to[fence->ring];
  130. p->ib.sync_to[fence->ring] = radeon_fence_later(fence, other);
  131. }
  132. static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
  133. {
  134. int i;
  135. for (i = 0; i < p->nrelocs; i++) {
  136. if (!p->relocs[i].robj)
  137. continue;
  138. radeon_cs_sync_to(p, p->relocs[i].robj->tbo.sync_obj);
  139. }
  140. }
  141. /* XXX: note that this is called from the legacy UMS CS ioctl as well */
  142. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  143. {
  144. struct drm_radeon_cs *cs = data;
  145. uint64_t *chunk_array_ptr;
  146. unsigned size, i;
  147. u32 ring = RADEON_CS_RING_GFX;
  148. s32 priority = 0;
  149. if (!cs->num_chunks) {
  150. return 0;
  151. }
  152. /* get chunks */
  153. INIT_LIST_HEAD(&p->validated);
  154. p->idx = 0;
  155. p->ib.sa_bo = NULL;
  156. p->ib.semaphore = NULL;
  157. p->const_ib.sa_bo = NULL;
  158. p->const_ib.semaphore = NULL;
  159. p->chunk_ib_idx = -1;
  160. p->chunk_relocs_idx = -1;
  161. p->chunk_flags_idx = -1;
  162. p->chunk_const_ib_idx = -1;
  163. p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
  164. if (p->chunks_array == NULL) {
  165. return -ENOMEM;
  166. }
  167. chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
  168. if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
  169. sizeof(uint64_t)*cs->num_chunks)) {
  170. return -EFAULT;
  171. }
  172. p->cs_flags = 0;
  173. p->nchunks = cs->num_chunks;
  174. p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
  175. if (p->chunks == NULL) {
  176. return -ENOMEM;
  177. }
  178. for (i = 0; i < p->nchunks; i++) {
  179. struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
  180. struct drm_radeon_cs_chunk user_chunk;
  181. uint32_t __user *cdata;
  182. chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
  183. if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
  184. sizeof(struct drm_radeon_cs_chunk))) {
  185. return -EFAULT;
  186. }
  187. p->chunks[i].length_dw = user_chunk.length_dw;
  188. p->chunks[i].kdata = NULL;
  189. p->chunks[i].chunk_id = user_chunk.chunk_id;
  190. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
  191. p->chunk_relocs_idx = i;
  192. }
  193. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
  194. p->chunk_ib_idx = i;
  195. /* zero length 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_CONST_IB) {
  200. p->chunk_const_ib_idx = i;
  201. /* zero length CONST IB isn't useful */
  202. if (p->chunks[i].length_dw == 0)
  203. return -EINVAL;
  204. }
  205. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  206. p->chunk_flags_idx = i;
  207. /* zero length flags aren't useful */
  208. if (p->chunks[i].length_dw == 0)
  209. return -EINVAL;
  210. }
  211. p->chunks[i].length_dw = user_chunk.length_dw;
  212. p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
  213. cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
  214. if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
  215. (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
  216. size = p->chunks[i].length_dw * sizeof(uint32_t);
  217. p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
  218. if (p->chunks[i].kdata == NULL) {
  219. return -ENOMEM;
  220. }
  221. if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
  222. p->chunks[i].user_ptr, size)) {
  223. return -EFAULT;
  224. }
  225. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  226. p->cs_flags = p->chunks[i].kdata[0];
  227. if (p->chunks[i].length_dw > 1)
  228. ring = p->chunks[i].kdata[1];
  229. if (p->chunks[i].length_dw > 2)
  230. priority = (s32)p->chunks[i].kdata[2];
  231. }
  232. }
  233. }
  234. /* these are KMS only */
  235. if (p->rdev) {
  236. if ((p->cs_flags & RADEON_CS_USE_VM) &&
  237. !p->rdev->vm_manager.enabled) {
  238. DRM_ERROR("VM not active on asic!\n");
  239. return -EINVAL;
  240. }
  241. /* we only support VM on SI+ */
  242. if ((p->rdev->family >= CHIP_TAHITI) &&
  243. ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
  244. DRM_ERROR("VM required on SI+!\n");
  245. return -EINVAL;
  246. }
  247. if (radeon_cs_get_ring(p, ring, priority))
  248. return -EINVAL;
  249. }
  250. /* deal with non-vm */
  251. if ((p->chunk_ib_idx != -1) &&
  252. ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
  253. (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
  254. if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
  255. DRM_ERROR("cs IB too big: %d\n",
  256. p->chunks[p->chunk_ib_idx].length_dw);
  257. return -EINVAL;
  258. }
  259. if (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) {
  260. p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  261. p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  262. if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
  263. p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
  264. kfree(p->chunks[p->chunk_ib_idx].kpage[0]);
  265. kfree(p->chunks[p->chunk_ib_idx].kpage[1]);
  266. p->chunks[p->chunk_ib_idx].kpage[0] = NULL;
  267. p->chunks[p->chunk_ib_idx].kpage[1] = NULL;
  268. return -ENOMEM;
  269. }
  270. }
  271. p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
  272. p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
  273. p->chunks[p->chunk_ib_idx].last_copied_page = -1;
  274. p->chunks[p->chunk_ib_idx].last_page_index =
  275. ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
  276. }
  277. return 0;
  278. }
  279. /**
  280. * cs_parser_fini() - clean parser states
  281. * @parser: parser structure holding parsing context.
  282. * @error: error number
  283. *
  284. * If error is set than unvalidate buffer, otherwise just free memory
  285. * used by parsing context.
  286. **/
  287. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  288. {
  289. unsigned i;
  290. if (!error) {
  291. ttm_eu_fence_buffer_objects(&parser->validated,
  292. parser->ib.fence);
  293. } else {
  294. ttm_eu_backoff_reservation(&parser->validated);
  295. }
  296. if (parser->relocs != NULL) {
  297. for (i = 0; i < parser->nrelocs; i++) {
  298. if (parser->relocs[i].gobj)
  299. drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
  300. }
  301. }
  302. kfree(parser->track);
  303. kfree(parser->relocs);
  304. kfree(parser->relocs_ptr);
  305. for (i = 0; i < parser->nchunks; i++) {
  306. kfree(parser->chunks[i].kdata);
  307. if ((parser->rdev->flags & RADEON_IS_AGP)) {
  308. kfree(parser->chunks[i].kpage[0]);
  309. kfree(parser->chunks[i].kpage[1]);
  310. }
  311. }
  312. kfree(parser->chunks);
  313. kfree(parser->chunks_array);
  314. radeon_ib_free(parser->rdev, &parser->ib);
  315. radeon_ib_free(parser->rdev, &parser->const_ib);
  316. }
  317. static int radeon_cs_ib_chunk(struct radeon_device *rdev,
  318. struct radeon_cs_parser *parser)
  319. {
  320. struct radeon_cs_chunk *ib_chunk;
  321. int r;
  322. if (parser->chunk_ib_idx == -1)
  323. return 0;
  324. if (parser->cs_flags & RADEON_CS_USE_VM)
  325. return 0;
  326. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  327. /* Copy the packet into the IB, the parser will read from the
  328. * input memory (cached) and write to the IB (which can be
  329. * uncached).
  330. */
  331. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  332. NULL, ib_chunk->length_dw * 4);
  333. if (r) {
  334. DRM_ERROR("Failed to get ib !\n");
  335. return r;
  336. }
  337. parser->ib.length_dw = ib_chunk->length_dw;
  338. r = radeon_cs_parse(rdev, parser->ring, parser);
  339. if (r || parser->parser_error) {
  340. DRM_ERROR("Invalid command stream !\n");
  341. return r;
  342. }
  343. r = radeon_cs_finish_pages(parser);
  344. if (r) {
  345. DRM_ERROR("Invalid command stream !\n");
  346. return r;
  347. }
  348. radeon_cs_sync_rings(parser);
  349. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  350. if (r) {
  351. DRM_ERROR("Failed to schedule IB !\n");
  352. }
  353. return r;
  354. }
  355. static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
  356. struct radeon_vm *vm)
  357. {
  358. struct radeon_device *rdev = parser->rdev;
  359. struct radeon_bo_list *lobj;
  360. struct radeon_bo *bo;
  361. int r;
  362. r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
  363. if (r) {
  364. return r;
  365. }
  366. list_for_each_entry(lobj, &parser->validated, tv.head) {
  367. bo = lobj->bo;
  368. r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
  369. if (r) {
  370. return r;
  371. }
  372. }
  373. return 0;
  374. }
  375. static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
  376. struct radeon_cs_parser *parser)
  377. {
  378. struct radeon_cs_chunk *ib_chunk;
  379. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  380. struct radeon_vm *vm = &fpriv->vm;
  381. int r;
  382. if (parser->chunk_ib_idx == -1)
  383. return 0;
  384. if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
  385. return 0;
  386. if ((rdev->family >= CHIP_TAHITI) &&
  387. (parser->chunk_const_ib_idx != -1)) {
  388. ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
  389. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  390. DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
  391. return -EINVAL;
  392. }
  393. r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
  394. vm, ib_chunk->length_dw * 4);
  395. if (r) {
  396. DRM_ERROR("Failed to get const ib !\n");
  397. return r;
  398. }
  399. parser->const_ib.is_const_ib = true;
  400. parser->const_ib.length_dw = ib_chunk->length_dw;
  401. /* Copy the packet into the IB */
  402. if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr,
  403. ib_chunk->length_dw * 4)) {
  404. return -EFAULT;
  405. }
  406. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
  407. if (r) {
  408. return r;
  409. }
  410. }
  411. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  412. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  413. DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
  414. return -EINVAL;
  415. }
  416. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  417. vm, ib_chunk->length_dw * 4);
  418. if (r) {
  419. DRM_ERROR("Failed to get ib !\n");
  420. return r;
  421. }
  422. parser->ib.length_dw = ib_chunk->length_dw;
  423. /* Copy the packet into the IB */
  424. if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
  425. ib_chunk->length_dw * 4)) {
  426. return -EFAULT;
  427. }
  428. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
  429. if (r) {
  430. return r;
  431. }
  432. mutex_lock(&rdev->vm_manager.lock);
  433. mutex_lock(&vm->mutex);
  434. r = radeon_vm_alloc_pt(rdev, vm);
  435. if (r) {
  436. goto out;
  437. }
  438. r = radeon_bo_vm_update_pte(parser, vm);
  439. if (r) {
  440. goto out;
  441. }
  442. radeon_cs_sync_rings(parser);
  443. radeon_cs_sync_to(parser, vm->fence);
  444. radeon_cs_sync_to(parser, radeon_vm_grab_id(rdev, vm, parser->ring));
  445. if ((rdev->family >= CHIP_TAHITI) &&
  446. (parser->chunk_const_ib_idx != -1)) {
  447. r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
  448. } else {
  449. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  450. }
  451. if (!r) {
  452. radeon_vm_fence(rdev, vm, parser->ib.fence);
  453. }
  454. out:
  455. radeon_vm_add_to_lru(rdev, vm);
  456. mutex_unlock(&vm->mutex);
  457. mutex_unlock(&rdev->vm_manager.lock);
  458. return r;
  459. }
  460. static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
  461. {
  462. if (r == -EDEADLK) {
  463. r = radeon_gpu_reset(rdev);
  464. if (!r)
  465. r = -EAGAIN;
  466. }
  467. return r;
  468. }
  469. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  470. {
  471. struct radeon_device *rdev = dev->dev_private;
  472. struct radeon_cs_parser parser;
  473. int r;
  474. down_read(&rdev->exclusive_lock);
  475. if (!rdev->accel_working) {
  476. up_read(&rdev->exclusive_lock);
  477. return -EBUSY;
  478. }
  479. /* initialize parser */
  480. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  481. parser.filp = filp;
  482. parser.rdev = rdev;
  483. parser.dev = rdev->dev;
  484. parser.family = rdev->family;
  485. r = radeon_cs_parser_init(&parser, data);
  486. if (r) {
  487. DRM_ERROR("Failed to initialize parser !\n");
  488. radeon_cs_parser_fini(&parser, r);
  489. up_read(&rdev->exclusive_lock);
  490. r = radeon_cs_handle_lockup(rdev, r);
  491. return r;
  492. }
  493. r = radeon_cs_parser_relocs(&parser);
  494. if (r) {
  495. if (r != -ERESTARTSYS)
  496. DRM_ERROR("Failed to parse relocation %d!\n", r);
  497. radeon_cs_parser_fini(&parser, r);
  498. up_read(&rdev->exclusive_lock);
  499. r = radeon_cs_handle_lockup(rdev, r);
  500. return r;
  501. }
  502. r = radeon_cs_ib_chunk(rdev, &parser);
  503. if (r) {
  504. goto out;
  505. }
  506. r = radeon_cs_ib_vm_chunk(rdev, &parser);
  507. if (r) {
  508. goto out;
  509. }
  510. out:
  511. radeon_cs_parser_fini(&parser, r);
  512. up_read(&rdev->exclusive_lock);
  513. r = radeon_cs_handle_lockup(rdev, r);
  514. return r;
  515. }
  516. int radeon_cs_finish_pages(struct radeon_cs_parser *p)
  517. {
  518. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  519. int i;
  520. int size = PAGE_SIZE;
  521. for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
  522. if (i == ibc->last_page_index) {
  523. size = (ibc->length_dw * 4) % PAGE_SIZE;
  524. if (size == 0)
  525. size = PAGE_SIZE;
  526. }
  527. if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
  528. ibc->user_ptr + (i * PAGE_SIZE),
  529. size))
  530. return -EFAULT;
  531. }
  532. return 0;
  533. }
  534. static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
  535. {
  536. int new_page;
  537. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  538. int i;
  539. int size = PAGE_SIZE;
  540. bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ?
  541. false : true;
  542. for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
  543. if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
  544. ibc->user_ptr + (i * PAGE_SIZE),
  545. PAGE_SIZE)) {
  546. p->parser_error = -EFAULT;
  547. return 0;
  548. }
  549. }
  550. if (pg_idx == ibc->last_page_index) {
  551. size = (ibc->length_dw * 4) % PAGE_SIZE;
  552. if (size == 0)
  553. size = PAGE_SIZE;
  554. }
  555. new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
  556. if (copy1)
  557. ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
  558. if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
  559. ibc->user_ptr + (pg_idx * PAGE_SIZE),
  560. size)) {
  561. p->parser_error = -EFAULT;
  562. return 0;
  563. }
  564. /* copy to IB for non single case */
  565. if (!copy1)
  566. memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
  567. ibc->last_copied_page = pg_idx;
  568. ibc->kpage_idx[new_page] = pg_idx;
  569. return new_page;
  570. }
  571. u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
  572. {
  573. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  574. u32 pg_idx, pg_offset;
  575. u32 idx_value = 0;
  576. int new_page;
  577. pg_idx = (idx * 4) / PAGE_SIZE;
  578. pg_offset = (idx * 4) % PAGE_SIZE;
  579. if (ibc->kpage_idx[0] == pg_idx)
  580. return ibc->kpage[0][pg_offset/4];
  581. if (ibc->kpage_idx[1] == pg_idx)
  582. return ibc->kpage[1][pg_offset/4];
  583. new_page = radeon_cs_update_pages(p, pg_idx);
  584. if (new_page < 0) {
  585. p->parser_error = new_page;
  586. return 0;
  587. }
  588. idx_value = ibc->kpage[new_page][pg_offset/4];
  589. return idx_value;
  590. }