radeon_cs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 int radeon_cs_sync_rings(struct radeon_cs_parser *p)
  111. {
  112. bool sync_to_ring[RADEON_NUM_RINGS] = { };
  113. int i, r;
  114. for (i = 0; i < p->nrelocs; i++) {
  115. if (!p->relocs[i].robj || !p->relocs[i].robj->tbo.sync_obj)
  116. continue;
  117. if (!(p->relocs[i].flags & RADEON_RELOC_DONT_SYNC)) {
  118. struct radeon_fence *fence = p->relocs[i].robj->tbo.sync_obj;
  119. if (!radeon_fence_signaled(fence)) {
  120. sync_to_ring[fence->ring] = true;
  121. }
  122. }
  123. }
  124. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  125. /* no need to sync to our own or unused rings */
  126. if (i == p->ring || !sync_to_ring[i] || !p->rdev->ring[i].ready)
  127. continue;
  128. if (!p->ib->fence->semaphore) {
  129. r = radeon_semaphore_create(p->rdev, &p->ib->fence->semaphore);
  130. if (r)
  131. return r;
  132. }
  133. r = radeon_ring_lock(p->rdev, &p->rdev->ring[i], 3);
  134. if (r)
  135. return r;
  136. radeon_semaphore_emit_signal(p->rdev, i, p->ib->fence->semaphore);
  137. radeon_ring_unlock_commit(p->rdev, &p->rdev->ring[i]);
  138. r = radeon_ring_lock(p->rdev, &p->rdev->ring[p->ring], 3);
  139. if (r)
  140. return r;
  141. radeon_semaphore_emit_wait(p->rdev, p->ring, p->ib->fence->semaphore);
  142. radeon_ring_unlock_commit(p->rdev, &p->rdev->ring[p->ring]);
  143. }
  144. return 0;
  145. }
  146. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  147. {
  148. struct drm_radeon_cs *cs = data;
  149. uint64_t *chunk_array_ptr;
  150. unsigned size, i;
  151. u32 ring = RADEON_CS_RING_GFX;
  152. s32 priority = 0;
  153. if (!cs->num_chunks) {
  154. return 0;
  155. }
  156. /* get chunks */
  157. INIT_LIST_HEAD(&p->validated);
  158. p->idx = 0;
  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. if ((p->cs_flags & RADEON_CS_USE_VM) &&
  235. !p->rdev->vm_manager.enabled) {
  236. DRM_ERROR("VM not active on asic!\n");
  237. return -EINVAL;
  238. }
  239. /* we only support VM on SI+ */
  240. if ((p->rdev->family >= CHIP_TAHITI) &&
  241. ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
  242. DRM_ERROR("VM required on SI+!\n");
  243. return -EINVAL;
  244. }
  245. if (radeon_cs_get_ring(p, ring, priority))
  246. return -EINVAL;
  247. /* deal with non-vm */
  248. if ((p->chunk_ib_idx != -1) &&
  249. ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
  250. (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
  251. if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
  252. DRM_ERROR("cs IB too big: %d\n",
  253. p->chunks[p->chunk_ib_idx].length_dw);
  254. return -EINVAL;
  255. }
  256. p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  257. p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  258. if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
  259. p->chunks[p->chunk_ib_idx].kpage[1] == NULL)
  260. return -ENOMEM;
  261. p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
  262. p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
  263. p->chunks[p->chunk_ib_idx].last_copied_page = -1;
  264. p->chunks[p->chunk_ib_idx].last_page_index =
  265. ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
  266. }
  267. return 0;
  268. }
  269. /**
  270. * cs_parser_fini() - clean parser states
  271. * @parser: parser structure holding parsing context.
  272. * @error: error number
  273. *
  274. * If error is set than unvalidate buffer, otherwise just free memory
  275. * used by parsing context.
  276. **/
  277. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  278. {
  279. unsigned i;
  280. if (!error && parser->ib)
  281. ttm_eu_fence_buffer_objects(&parser->validated,
  282. parser->ib->fence);
  283. else
  284. ttm_eu_backoff_reservation(&parser->validated);
  285. if (parser->relocs != NULL) {
  286. for (i = 0; i < parser->nrelocs; i++) {
  287. if (parser->relocs[i].gobj)
  288. drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
  289. }
  290. }
  291. kfree(parser->track);
  292. kfree(parser->relocs);
  293. kfree(parser->relocs_ptr);
  294. for (i = 0; i < parser->nchunks; i++) {
  295. kfree(parser->chunks[i].kdata);
  296. kfree(parser->chunks[i].kpage[0]);
  297. kfree(parser->chunks[i].kpage[1]);
  298. }
  299. kfree(parser->chunks);
  300. kfree(parser->chunks_array);
  301. radeon_ib_free(parser->rdev, &parser->ib);
  302. }
  303. static int radeon_cs_ib_chunk(struct radeon_device *rdev,
  304. struct radeon_cs_parser *parser)
  305. {
  306. struct radeon_cs_chunk *ib_chunk;
  307. int r;
  308. if (parser->chunk_ib_idx == -1)
  309. return 0;
  310. if (parser->cs_flags & RADEON_CS_USE_VM)
  311. return 0;
  312. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  313. /* Copy the packet into the IB, the parser will read from the
  314. * input memory (cached) and write to the IB (which can be
  315. * uncached).
  316. */
  317. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  318. ib_chunk->length_dw * 4);
  319. if (r) {
  320. DRM_ERROR("Failed to get ib !\n");
  321. return r;
  322. }
  323. parser->ib->length_dw = ib_chunk->length_dw;
  324. r = radeon_cs_parse(rdev, parser->ring, parser);
  325. if (r || parser->parser_error) {
  326. DRM_ERROR("Invalid command stream !\n");
  327. return r;
  328. }
  329. r = radeon_cs_finish_pages(parser);
  330. if (r) {
  331. DRM_ERROR("Invalid command stream !\n");
  332. return r;
  333. }
  334. r = radeon_cs_sync_rings(parser);
  335. if (r) {
  336. DRM_ERROR("Failed to synchronize rings !\n");
  337. }
  338. parser->ib->vm_id = 0;
  339. r = radeon_ib_schedule(rdev, parser->ib);
  340. if (r) {
  341. DRM_ERROR("Failed to schedule IB !\n");
  342. }
  343. return 0;
  344. }
  345. static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
  346. struct radeon_vm *vm)
  347. {
  348. struct radeon_bo_list *lobj;
  349. struct radeon_bo *bo;
  350. int r;
  351. list_for_each_entry(lobj, &parser->validated, tv.head) {
  352. bo = lobj->bo;
  353. r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
  354. if (r) {
  355. return r;
  356. }
  357. }
  358. return 0;
  359. }
  360. static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
  361. struct radeon_cs_parser *parser)
  362. {
  363. struct radeon_cs_chunk *ib_chunk;
  364. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  365. struct radeon_vm *vm = &fpriv->vm;
  366. int r;
  367. if (parser->chunk_ib_idx == -1)
  368. return 0;
  369. if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
  370. return 0;
  371. if ((rdev->family >= CHIP_TAHITI) &&
  372. (parser->chunk_const_ib_idx != -1)) {
  373. ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
  374. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  375. DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
  376. return -EINVAL;
  377. }
  378. r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
  379. ib_chunk->length_dw * 4);
  380. if (r) {
  381. DRM_ERROR("Failed to get const ib !\n");
  382. return r;
  383. }
  384. parser->const_ib->is_const_ib = true;
  385. parser->const_ib->length_dw = ib_chunk->length_dw;
  386. /* Copy the packet into the IB */
  387. if (DRM_COPY_FROM_USER(parser->const_ib->ptr, ib_chunk->user_ptr,
  388. ib_chunk->length_dw * 4)) {
  389. return -EFAULT;
  390. }
  391. r = radeon_ring_ib_parse(rdev, parser->ring, parser->const_ib);
  392. if (r) {
  393. return r;
  394. }
  395. }
  396. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  397. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  398. DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
  399. return -EINVAL;
  400. }
  401. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  402. ib_chunk->length_dw * 4);
  403. if (r) {
  404. DRM_ERROR("Failed to get ib !\n");
  405. return r;
  406. }
  407. parser->ib->length_dw = ib_chunk->length_dw;
  408. /* Copy the packet into the IB */
  409. if (DRM_COPY_FROM_USER(parser->ib->ptr, ib_chunk->user_ptr,
  410. ib_chunk->length_dw * 4)) {
  411. return -EFAULT;
  412. }
  413. r = radeon_ring_ib_parse(rdev, parser->ring, parser->ib);
  414. if (r) {
  415. return r;
  416. }
  417. mutex_lock(&vm->mutex);
  418. r = radeon_vm_bind(rdev, vm);
  419. if (r) {
  420. goto out;
  421. }
  422. r = radeon_bo_vm_update_pte(parser, vm);
  423. if (r) {
  424. goto out;
  425. }
  426. r = radeon_cs_sync_rings(parser);
  427. if (r) {
  428. DRM_ERROR("Failed to synchronize rings !\n");
  429. }
  430. if ((rdev->family >= CHIP_TAHITI) &&
  431. (parser->chunk_const_ib_idx != -1)) {
  432. parser->const_ib->vm_id = vm->id;
  433. /* ib pool is bind at 0 in virtual address space to gpu_addr is the
  434. * offset inside the pool bo
  435. */
  436. parser->const_ib->gpu_addr = parser->const_ib->sa_bo.offset;
  437. r = radeon_ib_schedule(rdev, parser->const_ib);
  438. if (r)
  439. goto out;
  440. }
  441. parser->ib->vm_id = vm->id;
  442. /* ib pool is bind at 0 in virtual address space to gpu_addr is the
  443. * offset inside the pool bo
  444. */
  445. parser->ib->gpu_addr = parser->ib->sa_bo.offset;
  446. parser->ib->is_const_ib = false;
  447. r = radeon_ib_schedule(rdev, parser->ib);
  448. out:
  449. if (!r) {
  450. if (vm->fence) {
  451. radeon_fence_unref(&vm->fence);
  452. }
  453. vm->fence = radeon_fence_ref(parser->ib->fence);
  454. }
  455. mutex_unlock(&fpriv->vm.mutex);
  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. radeon_mutex_lock(&rdev->cs_mutex);
  464. if (!rdev->accel_working) {
  465. radeon_mutex_unlock(&rdev->cs_mutex);
  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. radeon_mutex_unlock(&rdev->cs_mutex);
  479. return r;
  480. }
  481. r = radeon_cs_parser_relocs(&parser);
  482. if (r) {
  483. if (r != -ERESTARTSYS)
  484. DRM_ERROR("Failed to parse relocation %d!\n", r);
  485. radeon_cs_parser_fini(&parser, r);
  486. radeon_mutex_unlock(&rdev->cs_mutex);
  487. return r;
  488. }
  489. r = radeon_cs_ib_chunk(rdev, &parser);
  490. if (r) {
  491. goto out;
  492. }
  493. r = radeon_cs_ib_vm_chunk(rdev, &parser);
  494. if (r) {
  495. goto out;
  496. }
  497. out:
  498. radeon_cs_parser_fini(&parser, r);
  499. radeon_mutex_unlock(&rdev->cs_mutex);
  500. return r;
  501. }
  502. int radeon_cs_finish_pages(struct radeon_cs_parser *p)
  503. {
  504. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  505. int i;
  506. int size = PAGE_SIZE;
  507. for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
  508. if (i == ibc->last_page_index) {
  509. size = (ibc->length_dw * 4) % PAGE_SIZE;
  510. if (size == 0)
  511. size = PAGE_SIZE;
  512. }
  513. if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
  514. ibc->user_ptr + (i * PAGE_SIZE),
  515. size))
  516. return -EFAULT;
  517. }
  518. return 0;
  519. }
  520. int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
  521. {
  522. int new_page;
  523. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  524. int i;
  525. int size = PAGE_SIZE;
  526. for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
  527. if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
  528. ibc->user_ptr + (i * PAGE_SIZE),
  529. PAGE_SIZE)) {
  530. p->parser_error = -EFAULT;
  531. return 0;
  532. }
  533. }
  534. new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
  535. if (pg_idx == ibc->last_page_index) {
  536. size = (ibc->length_dw * 4) % PAGE_SIZE;
  537. if (size == 0)
  538. size = PAGE_SIZE;
  539. }
  540. if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
  541. ibc->user_ptr + (pg_idx * PAGE_SIZE),
  542. size)) {
  543. p->parser_error = -EFAULT;
  544. return 0;
  545. }
  546. /* copy to IB here */
  547. memcpy((void *)(p->ib->ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
  548. ibc->last_copied_page = pg_idx;
  549. ibc->kpage_idx[new_page] = pg_idx;
  550. return new_page;
  551. }