radeon_vce.c 21 KB

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