radeon_cs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * Copyright 2008 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Jerome Glisse <glisse@freedesktop.org>
  26. */
  27. #include "drmP.h"
  28. #include "radeon_drm.h"
  29. #include "radeon_reg.h"
  30. #include "radeon.h"
  31. void r100_cs_dump_packet(struct radeon_cs_parser *p,
  32. struct radeon_cs_packet *pkt);
  33. int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
  34. {
  35. struct drm_device *ddev = p->rdev->ddev;
  36. struct radeon_cs_chunk *chunk;
  37. unsigned i, j;
  38. bool duplicate;
  39. if (p->chunk_relocs_idx == -1) {
  40. return 0;
  41. }
  42. chunk = &p->chunks[p->chunk_relocs_idx];
  43. /* FIXME: we assume that each relocs use 4 dwords */
  44. p->nrelocs = chunk->length_dw / 4;
  45. p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
  46. if (p->relocs_ptr == NULL) {
  47. return -ENOMEM;
  48. }
  49. p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  50. if (p->relocs == NULL) {
  51. return -ENOMEM;
  52. }
  53. for (i = 0; i < p->nrelocs; i++) {
  54. struct drm_radeon_cs_reloc *r;
  55. duplicate = false;
  56. r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
  57. for (j = 0; j < i; j++) {
  58. if (r->handle == p->relocs[j].handle) {
  59. p->relocs_ptr[i] = &p->relocs[j];
  60. duplicate = true;
  61. break;
  62. }
  63. }
  64. if (!duplicate) {
  65. p->relocs[i].gobj = drm_gem_object_lookup(ddev,
  66. p->filp,
  67. r->handle);
  68. if (p->relocs[i].gobj == NULL) {
  69. DRM_ERROR("gem object lookup failed 0x%x\n",
  70. r->handle);
  71. return -ENOENT;
  72. }
  73. p->relocs_ptr[i] = &p->relocs[i];
  74. p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
  75. p->relocs[i].lobj.bo = p->relocs[i].robj;
  76. p->relocs[i].lobj.wdomain = r->write_domain;
  77. p->relocs[i].lobj.rdomain = r->read_domains;
  78. p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
  79. p->relocs[i].handle = r->handle;
  80. p->relocs[i].flags = r->flags;
  81. radeon_bo_list_add_object(&p->relocs[i].lobj,
  82. &p->validated);
  83. } else
  84. p->relocs[i].handle = 0;
  85. }
  86. return radeon_bo_list_validate(&p->validated);
  87. }
  88. static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
  89. {
  90. p->priority = priority;
  91. switch (ring) {
  92. default:
  93. DRM_ERROR("unknown ring id: %d\n", ring);
  94. return -EINVAL;
  95. case RADEON_CS_RING_GFX:
  96. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  97. break;
  98. case RADEON_CS_RING_COMPUTE:
  99. if (p->rdev->family >= CHIP_TAHITI) {
  100. if (p->priority > 0)
  101. p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
  102. else
  103. p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
  104. } else
  105. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  106. break;
  107. }
  108. return 0;
  109. }
  110. static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
  111. {
  112. int i;
  113. for (i = 0; i < p->nrelocs; i++) {
  114. struct radeon_fence *a, *b;
  115. if (!p->relocs[i].robj || !p->relocs[i].robj->tbo.sync_obj)
  116. continue;
  117. a = p->relocs[i].robj->tbo.sync_obj;
  118. b = p->ib.sync_to[a->ring];
  119. p->ib.sync_to[a->ring] = radeon_fence_later(a, b);
  120. }
  121. }
  122. /* XXX: note that this is called from the legacy UMS CS ioctl as well */
  123. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  124. {
  125. struct drm_radeon_cs *cs = data;
  126. uint64_t *chunk_array_ptr;
  127. unsigned size, i;
  128. u32 ring = RADEON_CS_RING_GFX;
  129. s32 priority = 0;
  130. if (!cs->num_chunks) {
  131. return 0;
  132. }
  133. /* get chunks */
  134. INIT_LIST_HEAD(&p->validated);
  135. p->idx = 0;
  136. p->ib.sa_bo = NULL;
  137. p->ib.semaphore = NULL;
  138. p->const_ib.sa_bo = NULL;
  139. p->const_ib.semaphore = NULL;
  140. p->chunk_ib_idx = -1;
  141. p->chunk_relocs_idx = -1;
  142. p->chunk_flags_idx = -1;
  143. p->chunk_const_ib_idx = -1;
  144. p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
  145. if (p->chunks_array == NULL) {
  146. return -ENOMEM;
  147. }
  148. chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
  149. if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
  150. sizeof(uint64_t)*cs->num_chunks)) {
  151. return -EFAULT;
  152. }
  153. p->cs_flags = 0;
  154. p->nchunks = cs->num_chunks;
  155. p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
  156. if (p->chunks == NULL) {
  157. return -ENOMEM;
  158. }
  159. for (i = 0; i < p->nchunks; i++) {
  160. struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
  161. struct drm_radeon_cs_chunk user_chunk;
  162. uint32_t __user *cdata;
  163. chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
  164. if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
  165. sizeof(struct drm_radeon_cs_chunk))) {
  166. return -EFAULT;
  167. }
  168. p->chunks[i].length_dw = user_chunk.length_dw;
  169. p->chunks[i].kdata = NULL;
  170. p->chunks[i].chunk_id = user_chunk.chunk_id;
  171. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
  172. p->chunk_relocs_idx = i;
  173. }
  174. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
  175. p->chunk_ib_idx = i;
  176. /* zero length IB isn't useful */
  177. if (p->chunks[i].length_dw == 0)
  178. return -EINVAL;
  179. }
  180. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
  181. p->chunk_const_ib_idx = i;
  182. /* zero length CONST IB isn't useful */
  183. if (p->chunks[i].length_dw == 0)
  184. return -EINVAL;
  185. }
  186. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  187. p->chunk_flags_idx = i;
  188. /* zero length flags aren't useful */
  189. if (p->chunks[i].length_dw == 0)
  190. return -EINVAL;
  191. }
  192. p->chunks[i].length_dw = user_chunk.length_dw;
  193. p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
  194. cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
  195. if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
  196. (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
  197. size = p->chunks[i].length_dw * sizeof(uint32_t);
  198. p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
  199. if (p->chunks[i].kdata == NULL) {
  200. return -ENOMEM;
  201. }
  202. if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
  203. p->chunks[i].user_ptr, size)) {
  204. return -EFAULT;
  205. }
  206. if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
  207. p->cs_flags = p->chunks[i].kdata[0];
  208. if (p->chunks[i].length_dw > 1)
  209. ring = p->chunks[i].kdata[1];
  210. if (p->chunks[i].length_dw > 2)
  211. priority = (s32)p->chunks[i].kdata[2];
  212. }
  213. }
  214. }
  215. /* these are KMS only */
  216. if (p->rdev) {
  217. if ((p->cs_flags & RADEON_CS_USE_VM) &&
  218. !p->rdev->vm_manager.enabled) {
  219. DRM_ERROR("VM not active on asic!\n");
  220. return -EINVAL;
  221. }
  222. /* we only support VM on SI+ */
  223. if ((p->rdev->family >= CHIP_TAHITI) &&
  224. ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
  225. DRM_ERROR("VM required on SI+!\n");
  226. return -EINVAL;
  227. }
  228. if (radeon_cs_get_ring(p, ring, priority))
  229. return -EINVAL;
  230. }
  231. /* deal with non-vm */
  232. if ((p->chunk_ib_idx != -1) &&
  233. ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
  234. (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
  235. if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
  236. DRM_ERROR("cs IB too big: %d\n",
  237. p->chunks[p->chunk_ib_idx].length_dw);
  238. return -EINVAL;
  239. }
  240. if ((p->rdev->flags & RADEON_IS_AGP)) {
  241. p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  242. p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  243. if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
  244. p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
  245. kfree(p->chunks[i].kpage[0]);
  246. kfree(p->chunks[i].kpage[1]);
  247. return -ENOMEM;
  248. }
  249. }
  250. p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
  251. p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
  252. p->chunks[p->chunk_ib_idx].last_copied_page = -1;
  253. p->chunks[p->chunk_ib_idx].last_page_index =
  254. ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * cs_parser_fini() - clean parser states
  260. * @parser: parser structure holding parsing context.
  261. * @error: error number
  262. *
  263. * If error is set than unvalidate buffer, otherwise just free memory
  264. * used by parsing context.
  265. **/
  266. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  267. {
  268. unsigned i;
  269. if (!error)
  270. ttm_eu_fence_buffer_objects(&parser->validated,
  271. parser->ib.fence);
  272. else
  273. ttm_eu_backoff_reservation(&parser->validated);
  274. if (parser->relocs != NULL) {
  275. for (i = 0; i < parser->nrelocs; i++) {
  276. if (parser->relocs[i].gobj)
  277. drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
  278. }
  279. }
  280. kfree(parser->track);
  281. kfree(parser->relocs);
  282. kfree(parser->relocs_ptr);
  283. for (i = 0; i < parser->nchunks; i++) {
  284. kfree(parser->chunks[i].kdata);
  285. if ((parser->rdev->flags & RADEON_IS_AGP)) {
  286. kfree(parser->chunks[i].kpage[0]);
  287. kfree(parser->chunks[i].kpage[1]);
  288. }
  289. }
  290. kfree(parser->chunks);
  291. kfree(parser->chunks_array);
  292. radeon_ib_free(parser->rdev, &parser->ib);
  293. radeon_ib_free(parser->rdev, &parser->const_ib);
  294. }
  295. static int radeon_cs_ib_chunk(struct radeon_device *rdev,
  296. struct radeon_cs_parser *parser)
  297. {
  298. struct radeon_cs_chunk *ib_chunk;
  299. int r;
  300. if (parser->chunk_ib_idx == -1)
  301. return 0;
  302. if (parser->cs_flags & RADEON_CS_USE_VM)
  303. return 0;
  304. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  305. /* Copy the packet into the IB, the parser will read from the
  306. * input memory (cached) and write to the IB (which can be
  307. * uncached).
  308. */
  309. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  310. ib_chunk->length_dw * 4);
  311. if (r) {
  312. DRM_ERROR("Failed to get ib !\n");
  313. return r;
  314. }
  315. parser->ib.length_dw = ib_chunk->length_dw;
  316. r = radeon_cs_parse(rdev, parser->ring, parser);
  317. if (r || parser->parser_error) {
  318. DRM_ERROR("Invalid command stream !\n");
  319. return r;
  320. }
  321. r = radeon_cs_finish_pages(parser);
  322. if (r) {
  323. DRM_ERROR("Invalid command stream !\n");
  324. return r;
  325. }
  326. radeon_cs_sync_rings(parser);
  327. parser->ib.vm_id = 0;
  328. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  329. if (r) {
  330. DRM_ERROR("Failed to schedule IB !\n");
  331. }
  332. return r;
  333. }
  334. static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
  335. struct radeon_vm *vm)
  336. {
  337. struct radeon_bo_list *lobj;
  338. struct radeon_bo *bo;
  339. int r;
  340. list_for_each_entry(lobj, &parser->validated, tv.head) {
  341. bo = lobj->bo;
  342. r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
  343. if (r) {
  344. return r;
  345. }
  346. }
  347. return 0;
  348. }
  349. static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
  350. struct radeon_cs_parser *parser)
  351. {
  352. struct radeon_cs_chunk *ib_chunk;
  353. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  354. struct radeon_vm *vm = &fpriv->vm;
  355. int r;
  356. if (parser->chunk_ib_idx == -1)
  357. return 0;
  358. if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
  359. return 0;
  360. if ((rdev->family >= CHIP_TAHITI) &&
  361. (parser->chunk_const_ib_idx != -1)) {
  362. ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
  363. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  364. DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
  365. return -EINVAL;
  366. }
  367. r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
  368. ib_chunk->length_dw * 4);
  369. if (r) {
  370. DRM_ERROR("Failed to get const ib !\n");
  371. return r;
  372. }
  373. parser->const_ib.is_const_ib = true;
  374. parser->const_ib.length_dw = ib_chunk->length_dw;
  375. /* Copy the packet into the IB */
  376. if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr,
  377. ib_chunk->length_dw * 4)) {
  378. return -EFAULT;
  379. }
  380. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
  381. if (r) {
  382. return r;
  383. }
  384. }
  385. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  386. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  387. DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
  388. return -EINVAL;
  389. }
  390. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  391. ib_chunk->length_dw * 4);
  392. if (r) {
  393. DRM_ERROR("Failed to get ib !\n");
  394. return r;
  395. }
  396. parser->ib.length_dw = ib_chunk->length_dw;
  397. /* Copy the packet into the IB */
  398. if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
  399. ib_chunk->length_dw * 4)) {
  400. return -EFAULT;
  401. }
  402. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
  403. if (r) {
  404. return r;
  405. }
  406. mutex_lock(&rdev->vm_manager.lock);
  407. mutex_lock(&vm->mutex);
  408. r = radeon_vm_bind(rdev, vm);
  409. if (r) {
  410. goto out;
  411. }
  412. r = radeon_bo_vm_update_pte(parser, vm);
  413. if (r) {
  414. goto out;
  415. }
  416. radeon_cs_sync_rings(parser);
  417. parser->ib.vm_id = vm->id;
  418. /* ib pool is bind at 0 in virtual address space,
  419. * so gpu_addr is the offset inside the pool bo
  420. */
  421. parser->ib.gpu_addr = parser->ib.sa_bo->soffset;
  422. if ((rdev->family >= CHIP_TAHITI) &&
  423. (parser->chunk_const_ib_idx != -1)) {
  424. parser->const_ib.vm_id = vm->id;
  425. /* ib pool is bind at 0 in virtual address space,
  426. * so gpu_addr is the offset inside the pool bo
  427. */
  428. parser->const_ib.gpu_addr = parser->const_ib.sa_bo->soffset;
  429. r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
  430. } else {
  431. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  432. }
  433. out:
  434. if (!r) {
  435. if (vm->fence) {
  436. radeon_fence_unref(&vm->fence);
  437. }
  438. vm->fence = radeon_fence_ref(parser->ib.fence);
  439. }
  440. mutex_unlock(&vm->mutex);
  441. mutex_unlock(&rdev->vm_manager.lock);
  442. return r;
  443. }
  444. static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
  445. {
  446. if (r == -EDEADLK) {
  447. r = radeon_gpu_reset(rdev);
  448. if (!r)
  449. r = -EAGAIN;
  450. }
  451. return r;
  452. }
  453. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  454. {
  455. struct radeon_device *rdev = dev->dev_private;
  456. struct radeon_cs_parser parser;
  457. int r;
  458. down_read(&rdev->exclusive_lock);
  459. if (!rdev->accel_working) {
  460. up_read(&rdev->exclusive_lock);
  461. return -EBUSY;
  462. }
  463. /* initialize parser */
  464. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  465. parser.filp = filp;
  466. parser.rdev = rdev;
  467. parser.dev = rdev->dev;
  468. parser.family = rdev->family;
  469. r = radeon_cs_parser_init(&parser, data);
  470. if (r) {
  471. DRM_ERROR("Failed to initialize parser !\n");
  472. radeon_cs_parser_fini(&parser, r);
  473. up_read(&rdev->exclusive_lock);
  474. r = radeon_cs_handle_lockup(rdev, r);
  475. return r;
  476. }
  477. r = radeon_cs_parser_relocs(&parser);
  478. if (r) {
  479. if (r != -ERESTARTSYS)
  480. DRM_ERROR("Failed to parse relocation %d!\n", r);
  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_ib_chunk(rdev, &parser);
  487. if (r) {
  488. goto out;
  489. }
  490. r = radeon_cs_ib_vm_chunk(rdev, &parser);
  491. if (r) {
  492. goto out;
  493. }
  494. out:
  495. radeon_cs_parser_fini(&parser, r);
  496. up_read(&rdev->exclusive_lock);
  497. r = radeon_cs_handle_lockup(rdev, r);
  498. return r;
  499. }
  500. int radeon_cs_finish_pages(struct radeon_cs_parser *p)
  501. {
  502. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  503. int i;
  504. int size = PAGE_SIZE;
  505. for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
  506. if (i == ibc->last_page_index) {
  507. size = (ibc->length_dw * 4) % PAGE_SIZE;
  508. if (size == 0)
  509. size = PAGE_SIZE;
  510. }
  511. if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
  512. ibc->user_ptr + (i * PAGE_SIZE),
  513. size))
  514. return -EFAULT;
  515. }
  516. return 0;
  517. }
  518. static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
  519. {
  520. int new_page;
  521. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  522. int i;
  523. int size = PAGE_SIZE;
  524. bool copy1 = (p->rdev->flags & RADEON_IS_AGP) ? false : true;
  525. for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
  526. if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
  527. ibc->user_ptr + (i * PAGE_SIZE),
  528. PAGE_SIZE)) {
  529. p->parser_error = -EFAULT;
  530. return 0;
  531. }
  532. }
  533. if (pg_idx == ibc->last_page_index) {
  534. size = (ibc->length_dw * 4) % PAGE_SIZE;
  535. if (size == 0)
  536. size = PAGE_SIZE;
  537. }
  538. new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
  539. if (copy1)
  540. ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
  541. if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
  542. ibc->user_ptr + (pg_idx * PAGE_SIZE),
  543. size)) {
  544. p->parser_error = -EFAULT;
  545. return 0;
  546. }
  547. /* copy to IB for non single case */
  548. if (!copy1)
  549. memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
  550. ibc->last_copied_page = pg_idx;
  551. ibc->kpage_idx[new_page] = pg_idx;
  552. return new_page;
  553. }
  554. u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
  555. {
  556. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  557. u32 pg_idx, pg_offset;
  558. u32 idx_value = 0;
  559. int new_page;
  560. pg_idx = (idx * 4) / PAGE_SIZE;
  561. pg_offset = (idx * 4) % PAGE_SIZE;
  562. if (ibc->kpage_idx[0] == pg_idx)
  563. return ibc->kpage[0][pg_offset/4];
  564. if (ibc->kpage_idx[1] == pg_idx)
  565. return ibc->kpage[1][pg_offset/4];
  566. new_page = radeon_cs_update_pages(p, pg_idx);
  567. if (new_page < 0) {
  568. p->parser_error = new_page;
  569. return 0;
  570. }
  571. idx_value = ibc->kpage[new_page][pg_offset/4];
  572. return idx_value;
  573. }