radeon_cs.c 22 KB

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