radeon_vce.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * Copyright 2013 Advanced Micro Devices, Inc.
  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
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. * Authors: Christian König <christian.koenig@amd.com>
  26. */
  27. #include <linux/firmware.h>
  28. #include <linux/module.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm.h>
  31. #include "radeon.h"
  32. #include "radeon_asic.h"
  33. #include "sid.h"
  34. /* 1 second timeout */
  35. #define VCE_IDLE_TIMEOUT_MS 1000
  36. /* Firmware Names */
  37. #define FIRMWARE_BONAIRE "radeon/BONAIRE_vce.bin"
  38. MODULE_FIRMWARE(FIRMWARE_BONAIRE);
  39. static void radeon_vce_idle_work_handler(struct work_struct *work);
  40. /**
  41. * radeon_vce_init - allocate memory, load vce firmware
  42. *
  43. * @rdev: radeon_device pointer
  44. *
  45. * First step to get VCE online, allocate memory and load the firmware
  46. */
  47. int radeon_vce_init(struct radeon_device *rdev)
  48. {
  49. static const char *fw_version = "[ATI LIB=VCEFW,";
  50. static const char *fb_version = "[ATI LIB=VCEFWSTATS,";
  51. unsigned long size;
  52. const char *fw_name, *c;
  53. uint8_t start, mid, end;
  54. int i, r;
  55. INIT_DELAYED_WORK(&rdev->vce.idle_work, radeon_vce_idle_work_handler);
  56. switch (rdev->family) {
  57. case CHIP_BONAIRE:
  58. case CHIP_KAVERI:
  59. case CHIP_KABINI:
  60. case CHIP_HAWAII:
  61. case CHIP_MULLINS:
  62. fw_name = FIRMWARE_BONAIRE;
  63. break;
  64. default:
  65. return -EINVAL;
  66. }
  67. r = request_firmware(&rdev->vce_fw, fw_name, rdev->dev);
  68. if (r) {
  69. dev_err(rdev->dev, "radeon_vce: Can't load firmware \"%s\"\n",
  70. fw_name);
  71. return r;
  72. }
  73. /* search for firmware version */
  74. size = rdev->vce_fw->size - strlen(fw_version) - 9;
  75. c = rdev->vce_fw->data;
  76. for (;size > 0; --size, ++c)
  77. if (strncmp(c, fw_version, strlen(fw_version)) == 0)
  78. break;
  79. if (size == 0)
  80. return -EINVAL;
  81. c += strlen(fw_version);
  82. if (sscanf(c, "%2hhd.%2hhd.%2hhd]", &start, &mid, &end) != 3)
  83. return -EINVAL;
  84. /* search for feedback version */
  85. size = rdev->vce_fw->size - strlen(fb_version) - 3;
  86. c = rdev->vce_fw->data;
  87. for (;size > 0; --size, ++c)
  88. if (strncmp(c, fb_version, strlen(fb_version)) == 0)
  89. break;
  90. if (size == 0)
  91. return -EINVAL;
  92. c += strlen(fb_version);
  93. if (sscanf(c, "%2u]", &rdev->vce.fb_version) != 1)
  94. return -EINVAL;
  95. DRM_INFO("Found VCE firmware/feedback version %hhd.%hhd.%hhd / %d!\n",
  96. start, mid, end, rdev->vce.fb_version);
  97. rdev->vce.fw_version = (start << 24) | (mid << 16) | (end << 8);
  98. /* we can only work with this fw version for now */
  99. if (rdev->vce.fw_version != ((40 << 24) | (2 << 16) | (2 << 8)))
  100. return -EINVAL;
  101. /* allocate firmware, stack and heap BO */
  102. size = RADEON_GPU_PAGE_ALIGN(rdev->vce_fw->size) +
  103. RADEON_VCE_STACK_SIZE + RADEON_VCE_HEAP_SIZE;
  104. r = radeon_bo_create(rdev, size, PAGE_SIZE, true,
  105. RADEON_GEM_DOMAIN_VRAM, 0, NULL, NULL,
  106. &rdev->vce.vcpu_bo);
  107. if (r) {
  108. dev_err(rdev->dev, "(%d) failed to allocate VCE bo\n", r);
  109. return r;
  110. }
  111. r = radeon_bo_reserve(rdev->vce.vcpu_bo, false);
  112. if (r) {
  113. radeon_bo_unref(&rdev->vce.vcpu_bo);
  114. dev_err(rdev->dev, "(%d) failed to reserve VCE bo\n", r);
  115. return r;
  116. }
  117. r = radeon_bo_pin(rdev->vce.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
  118. &rdev->vce.gpu_addr);
  119. radeon_bo_unreserve(rdev->vce.vcpu_bo);
  120. if (r) {
  121. radeon_bo_unref(&rdev->vce.vcpu_bo);
  122. dev_err(rdev->dev, "(%d) VCE bo pin failed\n", r);
  123. return r;
  124. }
  125. for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
  126. atomic_set(&rdev->vce.handles[i], 0);
  127. rdev->vce.filp[i] = NULL;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * radeon_vce_fini - free memory
  133. *
  134. * @rdev: radeon_device pointer
  135. *
  136. * Last step on VCE teardown, free firmware memory
  137. */
  138. void radeon_vce_fini(struct radeon_device *rdev)
  139. {
  140. if (rdev->vce.vcpu_bo == NULL)
  141. return;
  142. radeon_bo_unref(&rdev->vce.vcpu_bo);
  143. release_firmware(rdev->vce_fw);
  144. }
  145. /**
  146. * radeon_vce_suspend - unpin VCE fw memory
  147. *
  148. * @rdev: radeon_device pointer
  149. *
  150. */
  151. int radeon_vce_suspend(struct radeon_device *rdev)
  152. {
  153. int i;
  154. if (rdev->vce.vcpu_bo == NULL)
  155. return 0;
  156. for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i)
  157. if (atomic_read(&rdev->vce.handles[i]))
  158. break;
  159. if (i == RADEON_MAX_VCE_HANDLES)
  160. return 0;
  161. /* TODO: suspending running encoding sessions isn't supported */
  162. return -EINVAL;
  163. }
  164. /**
  165. * radeon_vce_resume - pin VCE fw memory
  166. *
  167. * @rdev: radeon_device pointer
  168. *
  169. */
  170. int radeon_vce_resume(struct radeon_device *rdev)
  171. {
  172. void *cpu_addr;
  173. int r;
  174. if (rdev->vce.vcpu_bo == NULL)
  175. return -EINVAL;
  176. r = radeon_bo_reserve(rdev->vce.vcpu_bo, false);
  177. if (r) {
  178. dev_err(rdev->dev, "(%d) failed to reserve VCE bo\n", r);
  179. return r;
  180. }
  181. r = radeon_bo_kmap(rdev->vce.vcpu_bo, &cpu_addr);
  182. if (r) {
  183. radeon_bo_unreserve(rdev->vce.vcpu_bo);
  184. dev_err(rdev->dev, "(%d) VCE map failed\n", r);
  185. return r;
  186. }
  187. memcpy(cpu_addr, rdev->vce_fw->data, rdev->vce_fw->size);
  188. radeon_bo_kunmap(rdev->vce.vcpu_bo);
  189. radeon_bo_unreserve(rdev->vce.vcpu_bo);
  190. return 0;
  191. }
  192. /**
  193. * radeon_vce_idle_work_handler - power off VCE
  194. *
  195. * @work: pointer to work structure
  196. *
  197. * power of VCE when it's not used any more
  198. */
  199. static void radeon_vce_idle_work_handler(struct work_struct *work)
  200. {
  201. struct radeon_device *rdev =
  202. container_of(work, struct radeon_device, vce.idle_work.work);
  203. if ((radeon_fence_count_emitted(rdev, TN_RING_TYPE_VCE1_INDEX) == 0) &&
  204. (radeon_fence_count_emitted(rdev, TN_RING_TYPE_VCE2_INDEX) == 0)) {
  205. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  206. radeon_dpm_enable_vce(rdev, false);
  207. } else {
  208. radeon_set_vce_clocks(rdev, 0, 0);
  209. }
  210. } else {
  211. schedule_delayed_work(&rdev->vce.idle_work,
  212. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  213. }
  214. }
  215. /**
  216. * radeon_vce_note_usage - power up VCE
  217. *
  218. * @rdev: radeon_device pointer
  219. *
  220. * Make sure VCE is powerd up when we want to use it
  221. */
  222. void radeon_vce_note_usage(struct radeon_device *rdev)
  223. {
  224. bool streams_changed = false;
  225. bool set_clocks = !cancel_delayed_work_sync(&rdev->vce.idle_work);
  226. set_clocks &= schedule_delayed_work(&rdev->vce.idle_work,
  227. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  228. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  229. /* XXX figure out if the streams changed */
  230. streams_changed = false;
  231. }
  232. if (set_clocks || streams_changed) {
  233. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  234. radeon_dpm_enable_vce(rdev, true);
  235. } else {
  236. radeon_set_vce_clocks(rdev, 53300, 40000);
  237. }
  238. }
  239. }
  240. /**
  241. * radeon_vce_free_handles - free still open VCE handles
  242. *
  243. * @rdev: radeon_device pointer
  244. * @filp: drm file pointer
  245. *
  246. * Close all VCE handles still open by this file pointer
  247. */
  248. void radeon_vce_free_handles(struct radeon_device *rdev, struct drm_file *filp)
  249. {
  250. int i, r;
  251. for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
  252. uint32_t handle = atomic_read(&rdev->vce.handles[i]);
  253. if (!handle || rdev->vce.filp[i] != filp)
  254. continue;
  255. radeon_vce_note_usage(rdev);
  256. r = radeon_vce_get_destroy_msg(rdev, TN_RING_TYPE_VCE1_INDEX,
  257. handle, NULL);
  258. if (r)
  259. DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
  260. rdev->vce.filp[i] = NULL;
  261. atomic_set(&rdev->vce.handles[i], 0);
  262. }
  263. }
  264. /**
  265. * radeon_vce_get_create_msg - generate a VCE create msg
  266. *
  267. * @rdev: radeon_device pointer
  268. * @ring: ring we should submit the msg to
  269. * @handle: VCE session handle to use
  270. * @fence: optional fence to return
  271. *
  272. * Open up a stream for HW test
  273. */
  274. int radeon_vce_get_create_msg(struct radeon_device *rdev, int ring,
  275. uint32_t handle, struct radeon_fence **fence)
  276. {
  277. const unsigned ib_size_dw = 1024;
  278. struct radeon_ib ib;
  279. uint64_t dummy;
  280. int i, r;
  281. r = radeon_ib_get(rdev, ring, &ib, NULL, ib_size_dw * 4);
  282. if (r) {
  283. DRM_ERROR("radeon: failed to get ib (%d).\n", r);
  284. return r;
  285. }
  286. dummy = ib.gpu_addr + 1024;
  287. /* stitch together an VCE create msg */
  288. ib.length_dw = 0;
  289. ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
  290. ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
  291. ib.ptr[ib.length_dw++] = handle;
  292. ib.ptr[ib.length_dw++] = 0x00000030; /* len */
  293. ib.ptr[ib.length_dw++] = 0x01000001; /* create cmd */
  294. ib.ptr[ib.length_dw++] = 0x00000000;
  295. ib.ptr[ib.length_dw++] = 0x00000042;
  296. ib.ptr[ib.length_dw++] = 0x0000000a;
  297. ib.ptr[ib.length_dw++] = 0x00000001;
  298. ib.ptr[ib.length_dw++] = 0x00000080;
  299. ib.ptr[ib.length_dw++] = 0x00000060;
  300. ib.ptr[ib.length_dw++] = 0x00000100;
  301. ib.ptr[ib.length_dw++] = 0x00000100;
  302. ib.ptr[ib.length_dw++] = 0x0000000c;
  303. ib.ptr[ib.length_dw++] = 0x00000000;
  304. ib.ptr[ib.length_dw++] = 0x00000014; /* len */
  305. ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
  306. ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
  307. ib.ptr[ib.length_dw++] = dummy;
  308. ib.ptr[ib.length_dw++] = 0x00000001;
  309. for (i = ib.length_dw; i < ib_size_dw; ++i)
  310. ib.ptr[i] = 0x0;
  311. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  312. if (r) {
  313. DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
  314. }
  315. if (fence)
  316. *fence = radeon_fence_ref(ib.fence);
  317. radeon_ib_free(rdev, &ib);
  318. return r;
  319. }
  320. /**
  321. * radeon_vce_get_destroy_msg - generate a VCE destroy msg
  322. *
  323. * @rdev: radeon_device pointer
  324. * @ring: ring we should submit the msg to
  325. * @handle: VCE session handle to use
  326. * @fence: optional fence to return
  327. *
  328. * Close up a stream for HW test or if userspace failed to do so
  329. */
  330. int radeon_vce_get_destroy_msg(struct radeon_device *rdev, int ring,
  331. uint32_t handle, struct radeon_fence **fence)
  332. {
  333. const unsigned ib_size_dw = 1024;
  334. struct radeon_ib ib;
  335. uint64_t dummy;
  336. int i, r;
  337. r = radeon_ib_get(rdev, ring, &ib, NULL, ib_size_dw * 4);
  338. if (r) {
  339. DRM_ERROR("radeon: failed to get ib (%d).\n", r);
  340. return r;
  341. }
  342. dummy = ib.gpu_addr + 1024;
  343. /* stitch together an VCE destroy msg */
  344. ib.length_dw = 0;
  345. ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
  346. ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
  347. ib.ptr[ib.length_dw++] = handle;
  348. ib.ptr[ib.length_dw++] = 0x00000014; /* len */
  349. ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
  350. ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
  351. ib.ptr[ib.length_dw++] = dummy;
  352. ib.ptr[ib.length_dw++] = 0x00000001;
  353. ib.ptr[ib.length_dw++] = 0x00000008; /* len */
  354. ib.ptr[ib.length_dw++] = 0x02000001; /* destroy cmd */
  355. for (i = ib.length_dw; i < ib_size_dw; ++i)
  356. ib.ptr[i] = 0x0;
  357. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  358. if (r) {
  359. DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
  360. }
  361. if (fence)
  362. *fence = radeon_fence_ref(ib.fence);
  363. radeon_ib_free(rdev, &ib);
  364. return r;
  365. }
  366. /**
  367. * radeon_vce_cs_reloc - command submission relocation
  368. *
  369. * @p: parser context
  370. * @lo: address of lower dword
  371. * @hi: address of higher dword
  372. * @size: size of checker for relocation buffer
  373. *
  374. * Patch relocation inside command stream with real buffer address
  375. */
  376. int radeon_vce_cs_reloc(struct radeon_cs_parser *p, int lo, int hi,
  377. unsigned size)
  378. {
  379. struct radeon_cs_chunk *relocs_chunk;
  380. struct radeon_cs_reloc *reloc;
  381. uint64_t start, end, offset;
  382. unsigned idx;
  383. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  384. offset = radeon_get_ib_value(p, lo);
  385. idx = radeon_get_ib_value(p, hi);
  386. if (idx >= relocs_chunk->length_dw) {
  387. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  388. idx, relocs_chunk->length_dw);
  389. return -EINVAL;
  390. }
  391. reloc = p->relocs_ptr[(idx / 4)];
  392. start = reloc->gpu_offset;
  393. end = start + radeon_bo_size(reloc->robj);
  394. start += offset;
  395. p->ib.ptr[lo] = start & 0xFFFFFFFF;
  396. p->ib.ptr[hi] = start >> 32;
  397. if (end <= start) {
  398. DRM_ERROR("invalid reloc offset %llX!\n", offset);
  399. return -EINVAL;
  400. }
  401. if ((end - start) < size) {
  402. DRM_ERROR("buffer to small (%d / %d)!\n",
  403. (unsigned)(end - start), size);
  404. return -EINVAL;
  405. }
  406. return 0;
  407. }
  408. /**
  409. * radeon_vce_validate_handle - validate stream handle
  410. *
  411. * @p: parser context
  412. * @handle: handle to validate
  413. * @allocated: allocated a new handle?
  414. *
  415. * Validates the handle and return the found session index or -EINVAL
  416. * we we don't have another free session index.
  417. */
  418. static int radeon_vce_validate_handle(struct radeon_cs_parser *p,
  419. uint32_t handle, bool *allocated)
  420. {
  421. unsigned i;
  422. *allocated = false;
  423. /* validate the handle */
  424. for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
  425. if (atomic_read(&p->rdev->vce.handles[i]) == handle) {
  426. if (p->rdev->vce.filp[i] != p->filp) {
  427. DRM_ERROR("VCE handle collision detected!\n");
  428. return -EINVAL;
  429. }
  430. return i;
  431. }
  432. }
  433. /* handle not found try to alloc a new one */
  434. for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i) {
  435. if (!atomic_cmpxchg(&p->rdev->vce.handles[i], 0, handle)) {
  436. p->rdev->vce.filp[i] = p->filp;
  437. p->rdev->vce.img_size[i] = 0;
  438. *allocated = true;
  439. return i;
  440. }
  441. }
  442. DRM_ERROR("No more free VCE handles!\n");
  443. return -EINVAL;
  444. }
  445. /**
  446. * radeon_vce_cs_parse - parse and validate the command stream
  447. *
  448. * @p: parser context
  449. *
  450. */
  451. int radeon_vce_cs_parse(struct radeon_cs_parser *p)
  452. {
  453. int session_idx = -1;
  454. bool destroyed = false, created = false, allocated = false;
  455. uint32_t tmp, handle = 0;
  456. uint32_t *size = &tmp;
  457. int i, r = 0;
  458. while (p->idx < p->chunks[p->chunk_ib_idx].length_dw) {
  459. uint32_t len = radeon_get_ib_value(p, p->idx);
  460. uint32_t cmd = radeon_get_ib_value(p, p->idx + 1);
  461. if ((len < 8) || (len & 3)) {
  462. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  463. r = -EINVAL;
  464. goto out;
  465. }
  466. if (destroyed) {
  467. DRM_ERROR("No other command allowed after destroy!\n");
  468. r = -EINVAL;
  469. goto out;
  470. }
  471. switch (cmd) {
  472. case 0x00000001: // session
  473. handle = radeon_get_ib_value(p, p->idx + 2);
  474. session_idx = radeon_vce_validate_handle(p, handle,
  475. &allocated);
  476. if (session_idx < 0)
  477. return session_idx;
  478. size = &p->rdev->vce.img_size[session_idx];
  479. break;
  480. case 0x00000002: // task info
  481. break;
  482. case 0x01000001: // create
  483. created = true;
  484. if (!allocated) {
  485. DRM_ERROR("Handle already in use!\n");
  486. r = -EINVAL;
  487. goto out;
  488. }
  489. *size = radeon_get_ib_value(p, p->idx + 8) *
  490. radeon_get_ib_value(p, p->idx + 10) *
  491. 8 * 3 / 2;
  492. break;
  493. case 0x04000001: // config extension
  494. case 0x04000002: // pic control
  495. case 0x04000005: // rate control
  496. case 0x04000007: // motion estimation
  497. case 0x04000008: // rdo
  498. break;
  499. case 0x03000001: // encode
  500. r = radeon_vce_cs_reloc(p, p->idx + 10, p->idx + 9,
  501. *size);
  502. if (r)
  503. goto out;
  504. r = radeon_vce_cs_reloc(p, p->idx + 12, p->idx + 11,
  505. *size / 3);
  506. if (r)
  507. goto out;
  508. break;
  509. case 0x02000001: // destroy
  510. destroyed = true;
  511. break;
  512. case 0x05000001: // context buffer
  513. r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2,
  514. *size * 2);
  515. if (r)
  516. goto out;
  517. break;
  518. case 0x05000004: // video bitstream buffer
  519. tmp = radeon_get_ib_value(p, p->idx + 4);
  520. r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2,
  521. tmp);
  522. if (r)
  523. goto out;
  524. break;
  525. case 0x05000005: // feedback buffer
  526. r = radeon_vce_cs_reloc(p, p->idx + 3, p->idx + 2,
  527. 4096);
  528. if (r)
  529. goto out;
  530. break;
  531. default:
  532. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  533. r = -EINVAL;
  534. goto out;
  535. }
  536. if (session_idx == -1) {
  537. DRM_ERROR("no session command at start of IB\n");
  538. r = -EINVAL;
  539. goto out;
  540. }
  541. p->idx += len / 4;
  542. }
  543. if (allocated && !created) {
  544. DRM_ERROR("New session without create command!\n");
  545. r = -ENOENT;
  546. }
  547. out:
  548. if ((!r && destroyed) || (r && allocated)) {
  549. /*
  550. * IB contains a destroy msg or we have allocated an
  551. * handle and got an error, anyway free the handle
  552. */
  553. for (i = 0; i < RADEON_MAX_VCE_HANDLES; ++i)
  554. atomic_cmpxchg(&p->rdev->vce.handles[i], handle, 0);
  555. }
  556. return r;
  557. }
  558. /**
  559. * radeon_vce_semaphore_emit - emit a semaphore command
  560. *
  561. * @rdev: radeon_device pointer
  562. * @ring: engine to use
  563. * @semaphore: address of semaphore
  564. * @emit_wait: true=emit wait, false=emit signal
  565. *
  566. */
  567. bool radeon_vce_semaphore_emit(struct radeon_device *rdev,
  568. struct radeon_ring *ring,
  569. struct radeon_semaphore *semaphore,
  570. bool emit_wait)
  571. {
  572. uint64_t addr = semaphore->gpu_addr;
  573. radeon_ring_write(ring, VCE_CMD_SEMAPHORE);
  574. radeon_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  575. radeon_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  576. radeon_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
  577. if (!emit_wait)
  578. radeon_ring_write(ring, VCE_CMD_END);
  579. return true;
  580. }
  581. /**
  582. * radeon_vce_ib_execute - execute indirect buffer
  583. *
  584. * @rdev: radeon_device pointer
  585. * @ib: the IB to execute
  586. *
  587. */
  588. void radeon_vce_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
  589. {
  590. struct radeon_ring *ring = &rdev->ring[ib->ring];
  591. radeon_ring_write(ring, VCE_CMD_IB);
  592. radeon_ring_write(ring, ib->gpu_addr);
  593. radeon_ring_write(ring, upper_32_bits(ib->gpu_addr));
  594. radeon_ring_write(ring, ib->length_dw);
  595. }
  596. /**
  597. * radeon_vce_fence_emit - add a fence command to the ring
  598. *
  599. * @rdev: radeon_device pointer
  600. * @fence: the fence
  601. *
  602. */
  603. void radeon_vce_fence_emit(struct radeon_device *rdev,
  604. struct radeon_fence *fence)
  605. {
  606. struct radeon_ring *ring = &rdev->ring[fence->ring];
  607. uint64_t addr = rdev->fence_drv[fence->ring].gpu_addr;
  608. radeon_ring_write(ring, VCE_CMD_FENCE);
  609. radeon_ring_write(ring, addr);
  610. radeon_ring_write(ring, upper_32_bits(addr));
  611. radeon_ring_write(ring, fence->seq);
  612. radeon_ring_write(ring, VCE_CMD_TRAP);
  613. radeon_ring_write(ring, VCE_CMD_END);
  614. }
  615. /**
  616. * radeon_vce_ring_test - test if VCE ring is working
  617. *
  618. * @rdev: radeon_device pointer
  619. * @ring: the engine to test on
  620. *
  621. */
  622. int radeon_vce_ring_test(struct radeon_device *rdev, struct radeon_ring *ring)
  623. {
  624. uint32_t rptr = vce_v1_0_get_rptr(rdev, ring);
  625. unsigned i;
  626. int r;
  627. r = radeon_ring_lock(rdev, ring, 16);
  628. if (r) {
  629. DRM_ERROR("radeon: vce failed to lock ring %d (%d).\n",
  630. ring->idx, r);
  631. return r;
  632. }
  633. radeon_ring_write(ring, VCE_CMD_END);
  634. radeon_ring_unlock_commit(rdev, ring, false);
  635. for (i = 0; i < rdev->usec_timeout; i++) {
  636. if (vce_v1_0_get_rptr(rdev, ring) != rptr)
  637. break;
  638. DRM_UDELAY(1);
  639. }
  640. if (i < rdev->usec_timeout) {
  641. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  642. ring->idx, i);
  643. } else {
  644. DRM_ERROR("radeon: ring %d test failed\n",
  645. ring->idx);
  646. r = -ETIMEDOUT;
  647. }
  648. return r;
  649. }
  650. /**
  651. * radeon_vce_ib_test - test if VCE IBs are working
  652. *
  653. * @rdev: radeon_device pointer
  654. * @ring: the engine to test on
  655. *
  656. */
  657. int radeon_vce_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
  658. {
  659. struct radeon_fence *fence = NULL;
  660. int r;
  661. r = radeon_vce_get_create_msg(rdev, ring->idx, 1, NULL);
  662. if (r) {
  663. DRM_ERROR("radeon: failed to get create msg (%d).\n", r);
  664. goto error;
  665. }
  666. r = radeon_vce_get_destroy_msg(rdev, ring->idx, 1, &fence);
  667. if (r) {
  668. DRM_ERROR("radeon: failed to get destroy ib (%d).\n", r);
  669. goto error;
  670. }
  671. r = radeon_fence_wait(fence, false);
  672. if (r) {
  673. DRM_ERROR("radeon: fence wait failed (%d).\n", r);
  674. } else {
  675. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  676. }
  677. error:
  678. radeon_fence_unref(&fence);
  679. return r;
  680. }