radeon_cs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. radeon_cs_sync_rings(parser);
  356. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  357. if (r) {
  358. DRM_ERROR("Failed to schedule IB !\n");
  359. }
  360. return r;
  361. }
  362. static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
  363. struct radeon_vm *vm)
  364. {
  365. struct radeon_device *rdev = parser->rdev;
  366. struct radeon_bo_list *lobj;
  367. struct radeon_bo *bo;
  368. int r;
  369. r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
  370. if (r) {
  371. return r;
  372. }
  373. list_for_each_entry(lobj, &parser->validated, tv.head) {
  374. bo = lobj->bo;
  375. r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
  376. if (r) {
  377. return r;
  378. }
  379. }
  380. return 0;
  381. }
  382. static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
  383. struct radeon_cs_parser *parser)
  384. {
  385. struct radeon_cs_chunk *ib_chunk;
  386. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  387. struct radeon_vm *vm = &fpriv->vm;
  388. int r;
  389. if (parser->chunk_ib_idx == -1)
  390. return 0;
  391. if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
  392. return 0;
  393. if ((rdev->family >= CHIP_TAHITI) &&
  394. (parser->chunk_const_ib_idx != -1)) {
  395. ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
  396. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  397. DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
  398. return -EINVAL;
  399. }
  400. r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
  401. vm, ib_chunk->length_dw * 4);
  402. if (r) {
  403. DRM_ERROR("Failed to get const ib !\n");
  404. return r;
  405. }
  406. parser->const_ib.is_const_ib = true;
  407. parser->const_ib.length_dw = ib_chunk->length_dw;
  408. /* Copy the packet into the IB */
  409. if (DRM_COPY_FROM_USER(parser->const_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->const_ib);
  414. if (r) {
  415. return r;
  416. }
  417. }
  418. ib_chunk = &parser->chunks[parser->chunk_ib_idx];
  419. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  420. DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
  421. return -EINVAL;
  422. }
  423. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  424. vm, ib_chunk->length_dw * 4);
  425. if (r) {
  426. DRM_ERROR("Failed to get ib !\n");
  427. return r;
  428. }
  429. parser->ib.length_dw = ib_chunk->length_dw;
  430. /* Copy the packet into the IB */
  431. if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
  432. ib_chunk->length_dw * 4)) {
  433. return -EFAULT;
  434. }
  435. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
  436. if (r) {
  437. return r;
  438. }
  439. mutex_lock(&rdev->vm_manager.lock);
  440. mutex_lock(&vm->mutex);
  441. r = radeon_vm_alloc_pt(rdev, vm);
  442. if (r) {
  443. goto out;
  444. }
  445. r = radeon_bo_vm_update_pte(parser, vm);
  446. if (r) {
  447. goto out;
  448. }
  449. radeon_cs_sync_rings(parser);
  450. radeon_ib_sync_to(&parser->ib, vm->fence);
  451. radeon_ib_sync_to(&parser->ib, radeon_vm_grab_id(
  452. rdev, vm, parser->ring));
  453. if ((rdev->family >= CHIP_TAHITI) &&
  454. (parser->chunk_const_ib_idx != -1)) {
  455. r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
  456. } else {
  457. r = radeon_ib_schedule(rdev, &parser->ib, NULL);
  458. }
  459. if (!r) {
  460. radeon_vm_fence(rdev, vm, parser->ib.fence);
  461. }
  462. out:
  463. radeon_vm_add_to_lru(rdev, vm);
  464. mutex_unlock(&vm->mutex);
  465. mutex_unlock(&rdev->vm_manager.lock);
  466. return r;
  467. }
  468. static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
  469. {
  470. if (r == -EDEADLK) {
  471. r = radeon_gpu_reset(rdev);
  472. if (!r)
  473. r = -EAGAIN;
  474. }
  475. return r;
  476. }
  477. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  478. {
  479. struct radeon_device *rdev = dev->dev_private;
  480. struct radeon_cs_parser parser;
  481. int r;
  482. down_read(&rdev->exclusive_lock);
  483. if (!rdev->accel_working) {
  484. up_read(&rdev->exclusive_lock);
  485. return -EBUSY;
  486. }
  487. /* initialize parser */
  488. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  489. parser.filp = filp;
  490. parser.rdev = rdev;
  491. parser.dev = rdev->dev;
  492. parser.family = rdev->family;
  493. r = radeon_cs_parser_init(&parser, data);
  494. if (r) {
  495. DRM_ERROR("Failed to initialize parser !\n");
  496. radeon_cs_parser_fini(&parser, r, false);
  497. up_read(&rdev->exclusive_lock);
  498. r = radeon_cs_handle_lockup(rdev, r);
  499. return r;
  500. }
  501. r = radeon_cs_parser_relocs(&parser);
  502. if (r) {
  503. if (r != -ERESTARTSYS)
  504. DRM_ERROR("Failed to parse relocation %d!\n", r);
  505. radeon_cs_parser_fini(&parser, r, false);
  506. up_read(&rdev->exclusive_lock);
  507. r = radeon_cs_handle_lockup(rdev, r);
  508. return r;
  509. }
  510. /* XXX pick SD/HD/MVC */
  511. if (parser.ring == R600_RING_TYPE_UVD_INDEX)
  512. radeon_uvd_note_usage(rdev);
  513. r = radeon_cs_ib_chunk(rdev, &parser);
  514. if (r) {
  515. goto out;
  516. }
  517. r = radeon_cs_ib_vm_chunk(rdev, &parser);
  518. if (r) {
  519. goto out;
  520. }
  521. out:
  522. radeon_cs_parser_fini(&parser, r, true);
  523. up_read(&rdev->exclusive_lock);
  524. r = radeon_cs_handle_lockup(rdev, r);
  525. return r;
  526. }
  527. int radeon_cs_finish_pages(struct radeon_cs_parser *p)
  528. {
  529. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  530. int i;
  531. int size = PAGE_SIZE;
  532. for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
  533. if (i == ibc->last_page_index) {
  534. size = (ibc->length_dw * 4) % PAGE_SIZE;
  535. if (size == 0)
  536. size = PAGE_SIZE;
  537. }
  538. if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
  539. ibc->user_ptr + (i * PAGE_SIZE),
  540. size))
  541. return -EFAULT;
  542. }
  543. return 0;
  544. }
  545. static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
  546. {
  547. int new_page;
  548. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  549. int i;
  550. int size = PAGE_SIZE;
  551. bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ?
  552. false : true;
  553. for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
  554. if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
  555. ibc->user_ptr + (i * PAGE_SIZE),
  556. PAGE_SIZE)) {
  557. p->parser_error = -EFAULT;
  558. return 0;
  559. }
  560. }
  561. if (pg_idx == ibc->last_page_index) {
  562. size = (ibc->length_dw * 4) % PAGE_SIZE;
  563. if (size == 0)
  564. size = PAGE_SIZE;
  565. }
  566. new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
  567. if (copy1)
  568. ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
  569. if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
  570. ibc->user_ptr + (pg_idx * PAGE_SIZE),
  571. size)) {
  572. p->parser_error = -EFAULT;
  573. return 0;
  574. }
  575. /* copy to IB for non single case */
  576. if (!copy1)
  577. memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
  578. ibc->last_copied_page = pg_idx;
  579. ibc->kpage_idx[new_page] = pg_idx;
  580. return new_page;
  581. }
  582. u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
  583. {
  584. struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
  585. u32 pg_idx, pg_offset;
  586. u32 idx_value = 0;
  587. int new_page;
  588. pg_idx = (idx * 4) / PAGE_SIZE;
  589. pg_offset = (idx * 4) % PAGE_SIZE;
  590. if (ibc->kpage_idx[0] == pg_idx)
  591. return ibc->kpage[0][pg_offset/4];
  592. if (ibc->kpage_idx[1] == pg_idx)
  593. return ibc->kpage[1][pg_offset/4];
  594. new_page = radeon_cs_update_pages(p, pg_idx);
  595. if (new_page < 0) {
  596. p->parser_error = new_page;
  597. return 0;
  598. }
  599. idx_value = ibc->kpage[new_page][pg_offset/4];
  600. return idx_value;
  601. }
  602. /**
  603. * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
  604. * @parser: parser structure holding parsing context.
  605. * @pkt: where to store packet information
  606. *
  607. * Assume that chunk_ib_index is properly set. Will return -EINVAL
  608. * if packet is bigger than remaining ib size. or if packets is unknown.
  609. **/
  610. int radeon_cs_packet_parse(struct radeon_cs_parser *p,
  611. struct radeon_cs_packet *pkt,
  612. unsigned idx)
  613. {
  614. struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
  615. struct radeon_device *rdev = p->rdev;
  616. uint32_t header;
  617. if (idx >= ib_chunk->length_dw) {
  618. DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
  619. idx, ib_chunk->length_dw);
  620. return -EINVAL;
  621. }
  622. header = radeon_get_ib_value(p, idx);
  623. pkt->idx = idx;
  624. pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
  625. pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
  626. pkt->one_reg_wr = 0;
  627. switch (pkt->type) {
  628. case RADEON_PACKET_TYPE0:
  629. if (rdev->family < CHIP_R600) {
  630. pkt->reg = R100_CP_PACKET0_GET_REG(header);
  631. pkt->one_reg_wr =
  632. RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
  633. } else
  634. pkt->reg = R600_CP_PACKET0_GET_REG(header);
  635. break;
  636. case RADEON_PACKET_TYPE3:
  637. pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
  638. break;
  639. case RADEON_PACKET_TYPE2:
  640. pkt->count = -1;
  641. break;
  642. default:
  643. DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
  644. return -EINVAL;
  645. }
  646. if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
  647. DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
  648. pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
  649. return -EINVAL;
  650. }
  651. return 0;
  652. }
  653. /**
  654. * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
  655. * @p: structure holding the parser context.
  656. *
  657. * Check if the next packet is NOP relocation packet3.
  658. **/
  659. bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
  660. {
  661. struct radeon_cs_packet p3reloc;
  662. int r;
  663. r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
  664. if (r)
  665. return false;
  666. if (p3reloc.type != RADEON_PACKET_TYPE3)
  667. return false;
  668. if (p3reloc.opcode != RADEON_PACKET3_NOP)
  669. return false;
  670. return true;
  671. }
  672. /**
  673. * radeon_cs_dump_packet() - dump raw packet context
  674. * @p: structure holding the parser context.
  675. * @pkt: structure holding the packet.
  676. *
  677. * Used mostly for debugging and error reporting.
  678. **/
  679. void radeon_cs_dump_packet(struct radeon_cs_parser *p,
  680. struct radeon_cs_packet *pkt)
  681. {
  682. volatile uint32_t *ib;
  683. unsigned i;
  684. unsigned idx;
  685. ib = p->ib.ptr;
  686. idx = pkt->idx;
  687. for (i = 0; i <= (pkt->count + 1); i++, idx++)
  688. DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
  689. }
  690. /**
  691. * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
  692. * @parser: parser structure holding parsing context.
  693. * @data: pointer to relocation data
  694. * @offset_start: starting offset
  695. * @offset_mask: offset mask (to align start offset on)
  696. * @reloc: reloc informations
  697. *
  698. * Check if next packet is relocation packet3, do bo validation and compute
  699. * GPU offset using the provided start.
  700. **/
  701. int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
  702. struct radeon_cs_reloc **cs_reloc,
  703. int nomm)
  704. {
  705. struct radeon_cs_chunk *relocs_chunk;
  706. struct radeon_cs_packet p3reloc;
  707. unsigned idx;
  708. int r;
  709. if (p->chunk_relocs_idx == -1) {
  710. DRM_ERROR("No relocation chunk !\n");
  711. return -EINVAL;
  712. }
  713. *cs_reloc = NULL;
  714. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  715. r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
  716. if (r)
  717. return r;
  718. p->idx += p3reloc.count + 2;
  719. if (p3reloc.type != RADEON_PACKET_TYPE3 ||
  720. p3reloc.opcode != RADEON_PACKET3_NOP) {
  721. DRM_ERROR("No packet3 for relocation for packet at %d.\n",
  722. p3reloc.idx);
  723. radeon_cs_dump_packet(p, &p3reloc);
  724. return -EINVAL;
  725. }
  726. idx = radeon_get_ib_value(p, p3reloc.idx + 1);
  727. if (idx >= relocs_chunk->length_dw) {
  728. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  729. idx, relocs_chunk->length_dw);
  730. radeon_cs_dump_packet(p, &p3reloc);
  731. return -EINVAL;
  732. }
  733. /* FIXME: we assume reloc size is 4 dwords */
  734. if (nomm) {
  735. *cs_reloc = p->relocs;
  736. (*cs_reloc)->lobj.gpu_offset =
  737. (u64)relocs_chunk->kdata[idx + 3] << 32;
  738. (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
  739. } else
  740. *cs_reloc = p->relocs_ptr[(idx / 4)];
  741. return 0;
  742. }