radeon_cs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 <linux/list_sort.h>
  28. #include <drm/drmP.h>
  29. #include <drm/radeon_drm.h>
  30. #include "radeon_reg.h"
  31. #include "radeon.h"
  32. #include "radeon_trace.h"
  33. #define RADEON_CS_MAX_PRIORITY 32u
  34. #define RADEON_CS_NUM_BUCKETS (RADEON_CS_MAX_PRIORITY + 1)
  35. /* This is based on the bucket sort with O(n) time complexity.
  36. * An item with priority "i" is added to bucket[i]. The lists are then
  37. * concatenated in descending order.
  38. */
  39. struct radeon_cs_buckets {
  40. struct list_head bucket[RADEON_CS_NUM_BUCKETS];
  41. };
  42. static void radeon_cs_buckets_init(struct radeon_cs_buckets *b)
  43. {
  44. unsigned i;
  45. for (i = 0; i < RADEON_CS_NUM_BUCKETS; i++)
  46. INIT_LIST_HEAD(&b->bucket[i]);
  47. }
  48. static void radeon_cs_buckets_add(struct radeon_cs_buckets *b,
  49. struct list_head *item, unsigned priority)
  50. {
  51. /* Since buffers which appear sooner in the relocation list are
  52. * likely to be used more often than buffers which appear later
  53. * in the list, the sort mustn't change the ordering of buffers
  54. * with the same priority, i.e. it must be stable.
  55. */
  56. list_add_tail(item, &b->bucket[min(priority, RADEON_CS_MAX_PRIORITY)]);
  57. }
  58. static void radeon_cs_buckets_get_list(struct radeon_cs_buckets *b,
  59. struct list_head *out_list)
  60. {
  61. unsigned i;
  62. /* Connect the sorted buckets in the output list. */
  63. for (i = 0; i < RADEON_CS_NUM_BUCKETS; i++) {
  64. list_splice(&b->bucket[i], out_list);
  65. }
  66. }
  67. static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
  68. {
  69. struct radeon_cs_chunk *chunk;
  70. struct radeon_cs_buckets buckets;
  71. unsigned i;
  72. bool need_mmap_lock = false;
  73. int r;
  74. if (p->chunk_relocs == NULL) {
  75. return 0;
  76. }
  77. chunk = p->chunk_relocs;
  78. p->dma_reloc_idx = 0;
  79. /* FIXME: we assume that each relocs use 4 dwords */
  80. p->nrelocs = chunk->length_dw / 4;
  81. p->relocs = kvmalloc_array(p->nrelocs, sizeof(struct radeon_bo_list),
  82. GFP_KERNEL | __GFP_ZERO);
  83. if (p->relocs == NULL) {
  84. return -ENOMEM;
  85. }
  86. radeon_cs_buckets_init(&buckets);
  87. for (i = 0; i < p->nrelocs; i++) {
  88. struct drm_radeon_cs_reloc *r;
  89. struct drm_gem_object *gobj;
  90. unsigned priority;
  91. r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
  92. gobj = drm_gem_object_lookup(p->filp, r->handle);
  93. if (gobj == NULL) {
  94. DRM_ERROR("gem object lookup failed 0x%x\n",
  95. r->handle);
  96. return -ENOENT;
  97. }
  98. p->relocs[i].robj = gem_to_radeon_bo(gobj);
  99. /* The userspace buffer priorities are from 0 to 15. A higher
  100. * number means the buffer is more important.
  101. * Also, the buffers used for write have a higher priority than
  102. * the buffers used for read only, which doubles the range
  103. * to 0 to 31. 32 is reserved for the kernel driver.
  104. */
  105. priority = (r->flags & RADEON_RELOC_PRIO_MASK) * 2
  106. + !!r->write_domain;
  107. /* The first reloc of an UVD job is the msg and that must be in
  108. * VRAM, the second reloc is the DPB and for WMV that must be in
  109. * VRAM as well. Also put everything into VRAM on AGP cards and older
  110. * IGP chips to avoid image corruptions
  111. */
  112. if (p->ring == R600_RING_TYPE_UVD_INDEX &&
  113. (i <= 0 || pci_find_capability(p->rdev->ddev->pdev,
  114. PCI_CAP_ID_AGP) ||
  115. p->rdev->family == CHIP_RS780 ||
  116. p->rdev->family == CHIP_RS880)) {
  117. /* TODO: is this still needed for NI+ ? */
  118. p->relocs[i].preferred_domains =
  119. RADEON_GEM_DOMAIN_VRAM;
  120. p->relocs[i].allowed_domains =
  121. RADEON_GEM_DOMAIN_VRAM;
  122. /* prioritize this over any other relocation */
  123. priority = RADEON_CS_MAX_PRIORITY;
  124. } else {
  125. uint32_t domain = r->write_domain ?
  126. r->write_domain : r->read_domains;
  127. if (domain & RADEON_GEM_DOMAIN_CPU) {
  128. DRM_ERROR("RADEON_GEM_DOMAIN_CPU is not valid "
  129. "for command submission\n");
  130. return -EINVAL;
  131. }
  132. p->relocs[i].preferred_domains = domain;
  133. if (domain == RADEON_GEM_DOMAIN_VRAM)
  134. domain |= RADEON_GEM_DOMAIN_GTT;
  135. p->relocs[i].allowed_domains = domain;
  136. }
  137. if (radeon_ttm_tt_has_userptr(p->relocs[i].robj->tbo.ttm)) {
  138. uint32_t domain = p->relocs[i].preferred_domains;
  139. if (!(domain & RADEON_GEM_DOMAIN_GTT)) {
  140. DRM_ERROR("Only RADEON_GEM_DOMAIN_GTT is "
  141. "allowed for userptr BOs\n");
  142. return -EINVAL;
  143. }
  144. need_mmap_lock = true;
  145. domain = RADEON_GEM_DOMAIN_GTT;
  146. p->relocs[i].preferred_domains = domain;
  147. p->relocs[i].allowed_domains = domain;
  148. }
  149. /* Objects shared as dma-bufs cannot be moved to VRAM */
  150. if (p->relocs[i].robj->prime_shared_count) {
  151. p->relocs[i].allowed_domains &= ~RADEON_GEM_DOMAIN_VRAM;
  152. if (!p->relocs[i].allowed_domains) {
  153. DRM_ERROR("BO associated with dma-buf cannot "
  154. "be moved to VRAM\n");
  155. return -EINVAL;
  156. }
  157. }
  158. p->relocs[i].tv.bo = &p->relocs[i].robj->tbo;
  159. p->relocs[i].tv.shared = !r->write_domain;
  160. radeon_cs_buckets_add(&buckets, &p->relocs[i].tv.head,
  161. priority);
  162. }
  163. radeon_cs_buckets_get_list(&buckets, &p->validated);
  164. if (p->cs_flags & RADEON_CS_USE_VM)
  165. p->vm_bos = radeon_vm_get_bos(p->rdev, p->ib.vm,
  166. &p->validated);
  167. if (need_mmap_lock)
  168. down_read(&current->mm->mmap_sem);
  169. r = radeon_bo_list_validate(p->rdev, &p->ticket, &p->validated, p->ring);
  170. if (need_mmap_lock)
  171. up_read(&current->mm->mmap_sem);
  172. return r;
  173. }
  174. static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
  175. {
  176. p->priority = priority;
  177. switch (ring) {
  178. default:
  179. DRM_ERROR("unknown ring id: %d\n", ring);
  180. return -EINVAL;
  181. case RADEON_CS_RING_GFX:
  182. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  183. break;
  184. case RADEON_CS_RING_COMPUTE:
  185. if (p->rdev->family >= CHIP_TAHITI) {
  186. if (p->priority > 0)
  187. p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
  188. else
  189. p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
  190. } else
  191. p->ring = RADEON_RING_TYPE_GFX_INDEX;
  192. break;
  193. case RADEON_CS_RING_DMA:
  194. if (p->rdev->family >= CHIP_CAYMAN) {
  195. if (p->priority > 0)
  196. p->ring = R600_RING_TYPE_DMA_INDEX;
  197. else
  198. p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
  199. } else if (p->rdev->family >= CHIP_RV770) {
  200. p->ring = R600_RING_TYPE_DMA_INDEX;
  201. } else {
  202. return -EINVAL;
  203. }
  204. break;
  205. case RADEON_CS_RING_UVD:
  206. p->ring = R600_RING_TYPE_UVD_INDEX;
  207. break;
  208. case RADEON_CS_RING_VCE:
  209. /* TODO: only use the low priority ring for now */
  210. p->ring = TN_RING_TYPE_VCE1_INDEX;
  211. break;
  212. }
  213. return 0;
  214. }
  215. static int radeon_cs_sync_rings(struct radeon_cs_parser *p)
  216. {
  217. struct radeon_bo_list *reloc;
  218. int r;
  219. list_for_each_entry(reloc, &p->validated, tv.head) {
  220. struct reservation_object *resv;
  221. resv = reloc->robj->tbo.resv;
  222. r = radeon_sync_resv(p->rdev, &p->ib.sync, resv,
  223. reloc->tv.shared);
  224. if (r)
  225. return r;
  226. }
  227. return 0;
  228. }
  229. /* XXX: note that this is called from the legacy UMS CS ioctl as well */
  230. int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
  231. {
  232. struct drm_radeon_cs *cs = data;
  233. uint64_t *chunk_array_ptr;
  234. unsigned size, i;
  235. u32 ring = RADEON_CS_RING_GFX;
  236. s32 priority = 0;
  237. INIT_LIST_HEAD(&p->validated);
  238. if (!cs->num_chunks) {
  239. return 0;
  240. }
  241. /* get chunks */
  242. p->idx = 0;
  243. p->ib.sa_bo = NULL;
  244. p->const_ib.sa_bo = NULL;
  245. p->chunk_ib = NULL;
  246. p->chunk_relocs = NULL;
  247. p->chunk_flags = NULL;
  248. p->chunk_const_ib = NULL;
  249. p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
  250. if (p->chunks_array == NULL) {
  251. return -ENOMEM;
  252. }
  253. chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
  254. if (copy_from_user(p->chunks_array, chunk_array_ptr,
  255. sizeof(uint64_t)*cs->num_chunks)) {
  256. return -EFAULT;
  257. }
  258. p->cs_flags = 0;
  259. p->nchunks = cs->num_chunks;
  260. p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
  261. if (p->chunks == NULL) {
  262. return -ENOMEM;
  263. }
  264. for (i = 0; i < p->nchunks; i++) {
  265. struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
  266. struct drm_radeon_cs_chunk user_chunk;
  267. uint32_t __user *cdata;
  268. chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
  269. if (copy_from_user(&user_chunk, chunk_ptr,
  270. sizeof(struct drm_radeon_cs_chunk))) {
  271. return -EFAULT;
  272. }
  273. p->chunks[i].length_dw = user_chunk.length_dw;
  274. if (user_chunk.chunk_id == RADEON_CHUNK_ID_RELOCS) {
  275. p->chunk_relocs = &p->chunks[i];
  276. }
  277. if (user_chunk.chunk_id == RADEON_CHUNK_ID_IB) {
  278. p->chunk_ib = &p->chunks[i];
  279. /* zero length IB isn't useful */
  280. if (p->chunks[i].length_dw == 0)
  281. return -EINVAL;
  282. }
  283. if (user_chunk.chunk_id == RADEON_CHUNK_ID_CONST_IB) {
  284. p->chunk_const_ib = &p->chunks[i];
  285. /* zero length CONST IB isn't useful */
  286. if (p->chunks[i].length_dw == 0)
  287. return -EINVAL;
  288. }
  289. if (user_chunk.chunk_id == RADEON_CHUNK_ID_FLAGS) {
  290. p->chunk_flags = &p->chunks[i];
  291. /* zero length flags aren't useful */
  292. if (p->chunks[i].length_dw == 0)
  293. return -EINVAL;
  294. }
  295. size = p->chunks[i].length_dw;
  296. cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
  297. p->chunks[i].user_ptr = cdata;
  298. if (user_chunk.chunk_id == RADEON_CHUNK_ID_CONST_IB)
  299. continue;
  300. if (user_chunk.chunk_id == RADEON_CHUNK_ID_IB) {
  301. if (!p->rdev || !(p->rdev->flags & RADEON_IS_AGP))
  302. continue;
  303. }
  304. p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  305. size *= sizeof(uint32_t);
  306. if (p->chunks[i].kdata == NULL) {
  307. return -ENOMEM;
  308. }
  309. if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
  310. return -EFAULT;
  311. }
  312. if (user_chunk.chunk_id == RADEON_CHUNK_ID_FLAGS) {
  313. p->cs_flags = p->chunks[i].kdata[0];
  314. if (p->chunks[i].length_dw > 1)
  315. ring = p->chunks[i].kdata[1];
  316. if (p->chunks[i].length_dw > 2)
  317. priority = (s32)p->chunks[i].kdata[2];
  318. }
  319. }
  320. /* these are KMS only */
  321. if (p->rdev) {
  322. if ((p->cs_flags & RADEON_CS_USE_VM) &&
  323. !p->rdev->vm_manager.enabled) {
  324. DRM_ERROR("VM not active on asic!\n");
  325. return -EINVAL;
  326. }
  327. if (radeon_cs_get_ring(p, ring, priority))
  328. return -EINVAL;
  329. /* we only support VM on some SI+ rings */
  330. if ((p->cs_flags & RADEON_CS_USE_VM) == 0) {
  331. if (p->rdev->asic->ring[p->ring]->cs_parse == NULL) {
  332. DRM_ERROR("Ring %d requires VM!\n", p->ring);
  333. return -EINVAL;
  334. }
  335. } else {
  336. if (p->rdev->asic->ring[p->ring]->ib_parse == NULL) {
  337. DRM_ERROR("VM not supported on ring %d!\n",
  338. p->ring);
  339. return -EINVAL;
  340. }
  341. }
  342. }
  343. return 0;
  344. }
  345. static int cmp_size_smaller_first(void *priv, struct list_head *a,
  346. struct list_head *b)
  347. {
  348. struct radeon_bo_list *la = list_entry(a, struct radeon_bo_list, tv.head);
  349. struct radeon_bo_list *lb = list_entry(b, struct radeon_bo_list, tv.head);
  350. /* Sort A before B if A is smaller. */
  351. return (int)la->robj->tbo.num_pages - (int)lb->robj->tbo.num_pages;
  352. }
  353. /**
  354. * cs_parser_fini() - clean parser states
  355. * @parser: parser structure holding parsing context.
  356. * @error: error number
  357. *
  358. * If error is set than unvalidate buffer, otherwise just free memory
  359. * used by parsing context.
  360. **/
  361. static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error, bool backoff)
  362. {
  363. unsigned i;
  364. if (!error) {
  365. /* Sort the buffer list from the smallest to largest buffer,
  366. * which affects the order of buffers in the LRU list.
  367. * This assures that the smallest buffers are added first
  368. * to the LRU list, so they are likely to be later evicted
  369. * first, instead of large buffers whose eviction is more
  370. * expensive.
  371. *
  372. * This slightly lowers the number of bytes moved by TTM
  373. * per frame under memory pressure.
  374. */
  375. list_sort(NULL, &parser->validated, cmp_size_smaller_first);
  376. ttm_eu_fence_buffer_objects(&parser->ticket,
  377. &parser->validated,
  378. &parser->ib.fence->base);
  379. } else if (backoff) {
  380. ttm_eu_backoff_reservation(&parser->ticket,
  381. &parser->validated);
  382. }
  383. if (parser->relocs != NULL) {
  384. for (i = 0; i < parser->nrelocs; i++) {
  385. struct radeon_bo *bo = parser->relocs[i].robj;
  386. if (bo == NULL)
  387. continue;
  388. drm_gem_object_put_unlocked(&bo->gem_base);
  389. }
  390. }
  391. kfree(parser->track);
  392. kvfree(parser->relocs);
  393. kvfree(parser->vm_bos);
  394. for (i = 0; i < parser->nchunks; i++)
  395. kvfree(parser->chunks[i].kdata);
  396. kfree(parser->chunks);
  397. kfree(parser->chunks_array);
  398. radeon_ib_free(parser->rdev, &parser->ib);
  399. radeon_ib_free(parser->rdev, &parser->const_ib);
  400. }
  401. static int radeon_cs_ib_chunk(struct radeon_device *rdev,
  402. struct radeon_cs_parser *parser)
  403. {
  404. int r;
  405. if (parser->chunk_ib == NULL)
  406. return 0;
  407. if (parser->cs_flags & RADEON_CS_USE_VM)
  408. return 0;
  409. r = radeon_cs_parse(rdev, parser->ring, parser);
  410. if (r || parser->parser_error) {
  411. DRM_ERROR("Invalid command stream !\n");
  412. return r;
  413. }
  414. r = radeon_cs_sync_rings(parser);
  415. if (r) {
  416. if (r != -ERESTARTSYS)
  417. DRM_ERROR("Failed to sync rings: %i\n", r);
  418. return r;
  419. }
  420. if (parser->ring == R600_RING_TYPE_UVD_INDEX)
  421. radeon_uvd_note_usage(rdev);
  422. else if ((parser->ring == TN_RING_TYPE_VCE1_INDEX) ||
  423. (parser->ring == TN_RING_TYPE_VCE2_INDEX))
  424. radeon_vce_note_usage(rdev);
  425. r = radeon_ib_schedule(rdev, &parser->ib, NULL, true);
  426. if (r) {
  427. DRM_ERROR("Failed to schedule IB !\n");
  428. }
  429. return r;
  430. }
  431. static int radeon_bo_vm_update_pte(struct radeon_cs_parser *p,
  432. struct radeon_vm *vm)
  433. {
  434. struct radeon_device *rdev = p->rdev;
  435. struct radeon_bo_va *bo_va;
  436. int i, r;
  437. r = radeon_vm_update_page_directory(rdev, vm);
  438. if (r)
  439. return r;
  440. r = radeon_vm_clear_freed(rdev, vm);
  441. if (r)
  442. return r;
  443. if (vm->ib_bo_va == NULL) {
  444. DRM_ERROR("Tmp BO not in VM!\n");
  445. return -EINVAL;
  446. }
  447. r = radeon_vm_bo_update(rdev, vm->ib_bo_va,
  448. &rdev->ring_tmp_bo.bo->tbo.mem);
  449. if (r)
  450. return r;
  451. for (i = 0; i < p->nrelocs; i++) {
  452. struct radeon_bo *bo;
  453. bo = p->relocs[i].robj;
  454. bo_va = radeon_vm_bo_find(vm, bo);
  455. if (bo_va == NULL) {
  456. dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
  457. return -EINVAL;
  458. }
  459. r = radeon_vm_bo_update(rdev, bo_va, &bo->tbo.mem);
  460. if (r)
  461. return r;
  462. radeon_sync_fence(&p->ib.sync, bo_va->last_pt_update);
  463. }
  464. return radeon_vm_clear_invalids(rdev, vm);
  465. }
  466. static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
  467. struct radeon_cs_parser *parser)
  468. {
  469. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  470. struct radeon_vm *vm = &fpriv->vm;
  471. int r;
  472. if (parser->chunk_ib == NULL)
  473. return 0;
  474. if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
  475. return 0;
  476. if (parser->const_ib.length_dw) {
  477. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
  478. if (r) {
  479. return r;
  480. }
  481. }
  482. r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
  483. if (r) {
  484. return r;
  485. }
  486. if (parser->ring == R600_RING_TYPE_UVD_INDEX)
  487. radeon_uvd_note_usage(rdev);
  488. mutex_lock(&vm->mutex);
  489. r = radeon_bo_vm_update_pte(parser, vm);
  490. if (r) {
  491. goto out;
  492. }
  493. r = radeon_cs_sync_rings(parser);
  494. if (r) {
  495. if (r != -ERESTARTSYS)
  496. DRM_ERROR("Failed to sync rings: %i\n", r);
  497. goto out;
  498. }
  499. if ((rdev->family >= CHIP_TAHITI) &&
  500. (parser->chunk_const_ib != NULL)) {
  501. r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib, true);
  502. } else {
  503. r = radeon_ib_schedule(rdev, &parser->ib, NULL, true);
  504. }
  505. out:
  506. mutex_unlock(&vm->mutex);
  507. return r;
  508. }
  509. static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
  510. {
  511. if (r == -EDEADLK) {
  512. r = radeon_gpu_reset(rdev);
  513. if (!r)
  514. r = -EAGAIN;
  515. }
  516. return r;
  517. }
  518. static int radeon_cs_ib_fill(struct radeon_device *rdev, struct radeon_cs_parser *parser)
  519. {
  520. struct radeon_cs_chunk *ib_chunk;
  521. struct radeon_vm *vm = NULL;
  522. int r;
  523. if (parser->chunk_ib == NULL)
  524. return 0;
  525. if (parser->cs_flags & RADEON_CS_USE_VM) {
  526. struct radeon_fpriv *fpriv = parser->filp->driver_priv;
  527. vm = &fpriv->vm;
  528. if ((rdev->family >= CHIP_TAHITI) &&
  529. (parser->chunk_const_ib != NULL)) {
  530. ib_chunk = parser->chunk_const_ib;
  531. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  532. DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
  533. return -EINVAL;
  534. }
  535. r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
  536. vm, ib_chunk->length_dw * 4);
  537. if (r) {
  538. DRM_ERROR("Failed to get const ib !\n");
  539. return r;
  540. }
  541. parser->const_ib.is_const_ib = true;
  542. parser->const_ib.length_dw = ib_chunk->length_dw;
  543. if (copy_from_user(parser->const_ib.ptr,
  544. ib_chunk->user_ptr,
  545. ib_chunk->length_dw * 4))
  546. return -EFAULT;
  547. }
  548. ib_chunk = parser->chunk_ib;
  549. if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
  550. DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
  551. return -EINVAL;
  552. }
  553. }
  554. ib_chunk = parser->chunk_ib;
  555. r = radeon_ib_get(rdev, parser->ring, &parser->ib,
  556. vm, ib_chunk->length_dw * 4);
  557. if (r) {
  558. DRM_ERROR("Failed to get ib !\n");
  559. return r;
  560. }
  561. parser->ib.length_dw = ib_chunk->length_dw;
  562. if (ib_chunk->kdata)
  563. memcpy(parser->ib.ptr, ib_chunk->kdata, ib_chunk->length_dw * 4);
  564. else if (copy_from_user(parser->ib.ptr, ib_chunk->user_ptr, ib_chunk->length_dw * 4))
  565. return -EFAULT;
  566. return 0;
  567. }
  568. int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  569. {
  570. struct radeon_device *rdev = dev->dev_private;
  571. struct radeon_cs_parser parser;
  572. int r;
  573. down_read(&rdev->exclusive_lock);
  574. if (!rdev->accel_working) {
  575. up_read(&rdev->exclusive_lock);
  576. return -EBUSY;
  577. }
  578. if (rdev->in_reset) {
  579. up_read(&rdev->exclusive_lock);
  580. r = radeon_gpu_reset(rdev);
  581. if (!r)
  582. r = -EAGAIN;
  583. return r;
  584. }
  585. /* initialize parser */
  586. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  587. parser.filp = filp;
  588. parser.rdev = rdev;
  589. parser.dev = rdev->dev;
  590. parser.family = rdev->family;
  591. r = radeon_cs_parser_init(&parser, data);
  592. if (r) {
  593. DRM_ERROR("Failed to initialize parser !\n");
  594. radeon_cs_parser_fini(&parser, r, false);
  595. up_read(&rdev->exclusive_lock);
  596. r = radeon_cs_handle_lockup(rdev, r);
  597. return r;
  598. }
  599. r = radeon_cs_ib_fill(rdev, &parser);
  600. if (!r) {
  601. r = radeon_cs_parser_relocs(&parser);
  602. if (r && r != -ERESTARTSYS)
  603. DRM_ERROR("Failed to parse relocation %d!\n", r);
  604. }
  605. if (r) {
  606. radeon_cs_parser_fini(&parser, r, false);
  607. up_read(&rdev->exclusive_lock);
  608. r = radeon_cs_handle_lockup(rdev, r);
  609. return r;
  610. }
  611. trace_radeon_cs(&parser);
  612. r = radeon_cs_ib_chunk(rdev, &parser);
  613. if (r) {
  614. goto out;
  615. }
  616. r = radeon_cs_ib_vm_chunk(rdev, &parser);
  617. if (r) {
  618. goto out;
  619. }
  620. out:
  621. radeon_cs_parser_fini(&parser, r, true);
  622. up_read(&rdev->exclusive_lock);
  623. r = radeon_cs_handle_lockup(rdev, r);
  624. return r;
  625. }
  626. /**
  627. * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
  628. * @parser: parser structure holding parsing context.
  629. * @pkt: where to store packet information
  630. *
  631. * Assume that chunk_ib_index is properly set. Will return -EINVAL
  632. * if packet is bigger than remaining ib size. or if packets is unknown.
  633. **/
  634. int radeon_cs_packet_parse(struct radeon_cs_parser *p,
  635. struct radeon_cs_packet *pkt,
  636. unsigned idx)
  637. {
  638. struct radeon_cs_chunk *ib_chunk = p->chunk_ib;
  639. struct radeon_device *rdev = p->rdev;
  640. uint32_t header;
  641. int ret = 0, i;
  642. if (idx >= ib_chunk->length_dw) {
  643. DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
  644. idx, ib_chunk->length_dw);
  645. return -EINVAL;
  646. }
  647. header = radeon_get_ib_value(p, idx);
  648. pkt->idx = idx;
  649. pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
  650. pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
  651. pkt->one_reg_wr = 0;
  652. switch (pkt->type) {
  653. case RADEON_PACKET_TYPE0:
  654. if (rdev->family < CHIP_R600) {
  655. pkt->reg = R100_CP_PACKET0_GET_REG(header);
  656. pkt->one_reg_wr =
  657. RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
  658. } else
  659. pkt->reg = R600_CP_PACKET0_GET_REG(header);
  660. break;
  661. case RADEON_PACKET_TYPE3:
  662. pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
  663. break;
  664. case RADEON_PACKET_TYPE2:
  665. pkt->count = -1;
  666. break;
  667. default:
  668. DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
  669. ret = -EINVAL;
  670. goto dump_ib;
  671. }
  672. if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
  673. DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
  674. pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
  675. ret = -EINVAL;
  676. goto dump_ib;
  677. }
  678. return 0;
  679. dump_ib:
  680. for (i = 0; i < ib_chunk->length_dw; i++) {
  681. if (i == idx)
  682. printk("\t0x%08x <---\n", radeon_get_ib_value(p, i));
  683. else
  684. printk("\t0x%08x\n", radeon_get_ib_value(p, i));
  685. }
  686. return ret;
  687. }
  688. /**
  689. * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
  690. * @p: structure holding the parser context.
  691. *
  692. * Check if the next packet is NOP relocation packet3.
  693. **/
  694. bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
  695. {
  696. struct radeon_cs_packet p3reloc;
  697. int r;
  698. r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
  699. if (r)
  700. return false;
  701. if (p3reloc.type != RADEON_PACKET_TYPE3)
  702. return false;
  703. if (p3reloc.opcode != RADEON_PACKET3_NOP)
  704. return false;
  705. return true;
  706. }
  707. /**
  708. * radeon_cs_dump_packet() - dump raw packet context
  709. * @p: structure holding the parser context.
  710. * @pkt: structure holding the packet.
  711. *
  712. * Used mostly for debugging and error reporting.
  713. **/
  714. void radeon_cs_dump_packet(struct radeon_cs_parser *p,
  715. struct radeon_cs_packet *pkt)
  716. {
  717. volatile uint32_t *ib;
  718. unsigned i;
  719. unsigned idx;
  720. ib = p->ib.ptr;
  721. idx = pkt->idx;
  722. for (i = 0; i <= (pkt->count + 1); i++, idx++)
  723. DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
  724. }
  725. /**
  726. * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
  727. * @parser: parser structure holding parsing context.
  728. * @data: pointer to relocation data
  729. * @offset_start: starting offset
  730. * @offset_mask: offset mask (to align start offset on)
  731. * @reloc: reloc informations
  732. *
  733. * Check if next packet is relocation packet3, do bo validation and compute
  734. * GPU offset using the provided start.
  735. **/
  736. int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
  737. struct radeon_bo_list **cs_reloc,
  738. int nomm)
  739. {
  740. struct radeon_cs_chunk *relocs_chunk;
  741. struct radeon_cs_packet p3reloc;
  742. unsigned idx;
  743. int r;
  744. if (p->chunk_relocs == NULL) {
  745. DRM_ERROR("No relocation chunk !\n");
  746. return -EINVAL;
  747. }
  748. *cs_reloc = NULL;
  749. relocs_chunk = p->chunk_relocs;
  750. r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
  751. if (r)
  752. return r;
  753. p->idx += p3reloc.count + 2;
  754. if (p3reloc.type != RADEON_PACKET_TYPE3 ||
  755. p3reloc.opcode != RADEON_PACKET3_NOP) {
  756. DRM_ERROR("No packet3 for relocation for packet at %d.\n",
  757. p3reloc.idx);
  758. radeon_cs_dump_packet(p, &p3reloc);
  759. return -EINVAL;
  760. }
  761. idx = radeon_get_ib_value(p, p3reloc.idx + 1);
  762. if (idx >= relocs_chunk->length_dw) {
  763. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  764. idx, relocs_chunk->length_dw);
  765. radeon_cs_dump_packet(p, &p3reloc);
  766. return -EINVAL;
  767. }
  768. /* FIXME: we assume reloc size is 4 dwords */
  769. if (nomm) {
  770. *cs_reloc = p->relocs;
  771. (*cs_reloc)->gpu_offset =
  772. (u64)relocs_chunk->kdata[idx + 3] << 32;
  773. (*cs_reloc)->gpu_offset |= relocs_chunk->kdata[idx + 0];
  774. } else
  775. *cs_reloc = &p->relocs[(idx / 4)];
  776. return 0;
  777. }