radeon_cs.c 22 KB

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