radeon_cs.c 22 KB

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