amdgpu_vce.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 "amdgpu.h"
  32. #include "amdgpu_pm.h"
  33. #include "amdgpu_vce.h"
  34. #include "cikd.h"
  35. /* 1 second timeout */
  36. #define VCE_IDLE_TIMEOUT_MS 1000
  37. /* Firmware Names */
  38. #ifdef CONFIG_DRM_AMDGPU_CIK
  39. #define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin"
  40. #define FIRMWARE_KABINI "radeon/kabini_vce.bin"
  41. #define FIRMWARE_KAVERI "radeon/kaveri_vce.bin"
  42. #define FIRMWARE_HAWAII "radeon/hawaii_vce.bin"
  43. #define FIRMWARE_MULLINS "radeon/mullins_vce.bin"
  44. #endif
  45. #define FIRMWARE_TONGA "amdgpu/tonga_vce.bin"
  46. #define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin"
  47. #define FIRMWARE_FIJI "amdgpu/fiji_vce.bin"
  48. #define FIRMWARE_STONEY "amdgpu/stoney_vce.bin"
  49. #ifdef CONFIG_DRM_AMDGPU_CIK
  50. MODULE_FIRMWARE(FIRMWARE_BONAIRE);
  51. MODULE_FIRMWARE(FIRMWARE_KABINI);
  52. MODULE_FIRMWARE(FIRMWARE_KAVERI);
  53. MODULE_FIRMWARE(FIRMWARE_HAWAII);
  54. MODULE_FIRMWARE(FIRMWARE_MULLINS);
  55. #endif
  56. MODULE_FIRMWARE(FIRMWARE_TONGA);
  57. MODULE_FIRMWARE(FIRMWARE_CARRIZO);
  58. MODULE_FIRMWARE(FIRMWARE_FIJI);
  59. MODULE_FIRMWARE(FIRMWARE_STONEY);
  60. static void amdgpu_vce_idle_work_handler(struct work_struct *work);
  61. /**
  62. * amdgpu_vce_init - allocate memory, load vce firmware
  63. *
  64. * @adev: amdgpu_device pointer
  65. *
  66. * First step to get VCE online, allocate memory and load the firmware
  67. */
  68. int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
  69. {
  70. const char *fw_name;
  71. const struct common_firmware_header *hdr;
  72. unsigned ucode_version, version_major, version_minor, binary_id;
  73. int i, r;
  74. INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
  75. switch (adev->asic_type) {
  76. #ifdef CONFIG_DRM_AMDGPU_CIK
  77. case CHIP_BONAIRE:
  78. fw_name = FIRMWARE_BONAIRE;
  79. break;
  80. case CHIP_KAVERI:
  81. fw_name = FIRMWARE_KAVERI;
  82. break;
  83. case CHIP_KABINI:
  84. fw_name = FIRMWARE_KABINI;
  85. break;
  86. case CHIP_HAWAII:
  87. fw_name = FIRMWARE_HAWAII;
  88. break;
  89. case CHIP_MULLINS:
  90. fw_name = FIRMWARE_MULLINS;
  91. break;
  92. #endif
  93. case CHIP_TONGA:
  94. fw_name = FIRMWARE_TONGA;
  95. break;
  96. case CHIP_CARRIZO:
  97. fw_name = FIRMWARE_CARRIZO;
  98. break;
  99. case CHIP_FIJI:
  100. fw_name = FIRMWARE_FIJI;
  101. break;
  102. case CHIP_STONEY:
  103. fw_name = FIRMWARE_STONEY;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
  109. if (r) {
  110. dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
  111. fw_name);
  112. return r;
  113. }
  114. r = amdgpu_ucode_validate(adev->vce.fw);
  115. if (r) {
  116. dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
  117. fw_name);
  118. release_firmware(adev->vce.fw);
  119. adev->vce.fw = NULL;
  120. return r;
  121. }
  122. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  123. ucode_version = le32_to_cpu(hdr->ucode_version);
  124. version_major = (ucode_version >> 20) & 0xfff;
  125. version_minor = (ucode_version >> 8) & 0xfff;
  126. binary_id = ucode_version & 0xff;
  127. DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
  128. version_major, version_minor, binary_id);
  129. adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
  130. (binary_id << 8));
  131. /* allocate firmware, stack and heap BO */
  132. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
  133. AMDGPU_GEM_DOMAIN_VRAM,
  134. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  135. NULL, NULL, &adev->vce.vcpu_bo);
  136. if (r) {
  137. dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
  138. return r;
  139. }
  140. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  141. if (r) {
  142. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  143. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  144. return r;
  145. }
  146. r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
  147. &adev->vce.gpu_addr);
  148. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  149. if (r) {
  150. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  151. dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
  152. return r;
  153. }
  154. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  155. atomic_set(&adev->vce.handles[i], 0);
  156. adev->vce.filp[i] = NULL;
  157. }
  158. return 0;
  159. }
  160. /**
  161. * amdgpu_vce_fini - free memory
  162. *
  163. * @adev: amdgpu_device pointer
  164. *
  165. * Last step on VCE teardown, free firmware memory
  166. */
  167. int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
  168. {
  169. if (adev->vce.vcpu_bo == NULL)
  170. return 0;
  171. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  172. amdgpu_ring_fini(&adev->vce.ring[0]);
  173. amdgpu_ring_fini(&adev->vce.ring[1]);
  174. release_firmware(adev->vce.fw);
  175. return 0;
  176. }
  177. /**
  178. * amdgpu_vce_suspend - unpin VCE fw memory
  179. *
  180. * @adev: amdgpu_device pointer
  181. *
  182. */
  183. int amdgpu_vce_suspend(struct amdgpu_device *adev)
  184. {
  185. int i;
  186. if (adev->vce.vcpu_bo == NULL)
  187. return 0;
  188. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  189. if (atomic_read(&adev->vce.handles[i]))
  190. break;
  191. if (i == AMDGPU_MAX_VCE_HANDLES)
  192. return 0;
  193. /* TODO: suspending running encoding sessions isn't supported */
  194. return -EINVAL;
  195. }
  196. /**
  197. * amdgpu_vce_resume - pin VCE fw memory
  198. *
  199. * @adev: amdgpu_device pointer
  200. *
  201. */
  202. int amdgpu_vce_resume(struct amdgpu_device *adev)
  203. {
  204. void *cpu_addr;
  205. const struct common_firmware_header *hdr;
  206. unsigned offset;
  207. int r;
  208. if (adev->vce.vcpu_bo == NULL)
  209. return -EINVAL;
  210. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  211. if (r) {
  212. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  213. return r;
  214. }
  215. r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
  216. if (r) {
  217. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  218. dev_err(adev->dev, "(%d) VCE map failed\n", r);
  219. return r;
  220. }
  221. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  222. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  223. memcpy(cpu_addr, (adev->vce.fw->data) + offset,
  224. (adev->vce.fw->size) - offset);
  225. amdgpu_bo_kunmap(adev->vce.vcpu_bo);
  226. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  227. return 0;
  228. }
  229. /**
  230. * amdgpu_vce_idle_work_handler - power off VCE
  231. *
  232. * @work: pointer to work structure
  233. *
  234. * power of VCE when it's not used any more
  235. */
  236. static void amdgpu_vce_idle_work_handler(struct work_struct *work)
  237. {
  238. struct amdgpu_device *adev =
  239. container_of(work, struct amdgpu_device, vce.idle_work.work);
  240. if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) &&
  241. (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) {
  242. if (adev->pm.dpm_enabled) {
  243. amdgpu_dpm_enable_vce(adev, false);
  244. } else {
  245. amdgpu_asic_set_vce_clocks(adev, 0, 0);
  246. }
  247. } else {
  248. schedule_delayed_work(&adev->vce.idle_work,
  249. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  250. }
  251. }
  252. /**
  253. * amdgpu_vce_note_usage - power up VCE
  254. *
  255. * @adev: amdgpu_device pointer
  256. *
  257. * Make sure VCE is powerd up when we want to use it
  258. */
  259. static void amdgpu_vce_note_usage(struct amdgpu_device *adev)
  260. {
  261. bool streams_changed = false;
  262. bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
  263. set_clocks &= schedule_delayed_work(&adev->vce.idle_work,
  264. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  265. if (adev->pm.dpm_enabled) {
  266. /* XXX figure out if the streams changed */
  267. streams_changed = false;
  268. }
  269. if (set_clocks || streams_changed) {
  270. if (adev->pm.dpm_enabled) {
  271. amdgpu_dpm_enable_vce(adev, true);
  272. } else {
  273. amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
  274. }
  275. }
  276. }
  277. /**
  278. * amdgpu_vce_free_handles - free still open VCE handles
  279. *
  280. * @adev: amdgpu_device pointer
  281. * @filp: drm file pointer
  282. *
  283. * Close all VCE handles still open by this file pointer
  284. */
  285. void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
  286. {
  287. struct amdgpu_ring *ring = &adev->vce.ring[0];
  288. int i, r;
  289. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  290. uint32_t handle = atomic_read(&adev->vce.handles[i]);
  291. if (!handle || adev->vce.filp[i] != filp)
  292. continue;
  293. amdgpu_vce_note_usage(adev);
  294. r = amdgpu_vce_get_destroy_msg(ring, handle, NULL);
  295. if (r)
  296. DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
  297. adev->vce.filp[i] = NULL;
  298. atomic_set(&adev->vce.handles[i], 0);
  299. }
  300. }
  301. static int amdgpu_vce_free_job(
  302. struct amdgpu_job *job)
  303. {
  304. amdgpu_ib_free(job->adev, job->ibs);
  305. kfree(job->ibs);
  306. return 0;
  307. }
  308. /**
  309. * amdgpu_vce_get_create_msg - generate a VCE create msg
  310. *
  311. * @adev: amdgpu_device pointer
  312. * @ring: ring we should submit the msg to
  313. * @handle: VCE session handle to use
  314. * @fence: optional fence to return
  315. *
  316. * Open up a stream for HW test
  317. */
  318. int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  319. struct fence **fence)
  320. {
  321. const unsigned ib_size_dw = 1024;
  322. struct amdgpu_ib *ib = NULL;
  323. struct fence *f = NULL;
  324. struct amdgpu_device *adev = ring->adev;
  325. uint64_t dummy;
  326. int i, r;
  327. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  328. if (!ib)
  329. return -ENOMEM;
  330. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib);
  331. if (r) {
  332. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  333. kfree(ib);
  334. return r;
  335. }
  336. dummy = ib->gpu_addr + 1024;
  337. /* stitch together an VCE create msg */
  338. ib->length_dw = 0;
  339. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  340. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  341. ib->ptr[ib->length_dw++] = handle;
  342. if ((ring->adev->vce.fw_version >> 24) >= 52)
  343. ib->ptr[ib->length_dw++] = 0x00000040; /* len */
  344. else
  345. ib->ptr[ib->length_dw++] = 0x00000030; /* len */
  346. ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
  347. ib->ptr[ib->length_dw++] = 0x00000000;
  348. ib->ptr[ib->length_dw++] = 0x00000042;
  349. ib->ptr[ib->length_dw++] = 0x0000000a;
  350. ib->ptr[ib->length_dw++] = 0x00000001;
  351. ib->ptr[ib->length_dw++] = 0x00000080;
  352. ib->ptr[ib->length_dw++] = 0x00000060;
  353. ib->ptr[ib->length_dw++] = 0x00000100;
  354. ib->ptr[ib->length_dw++] = 0x00000100;
  355. ib->ptr[ib->length_dw++] = 0x0000000c;
  356. ib->ptr[ib->length_dw++] = 0x00000000;
  357. if ((ring->adev->vce.fw_version >> 24) >= 52) {
  358. ib->ptr[ib->length_dw++] = 0x00000000;
  359. ib->ptr[ib->length_dw++] = 0x00000000;
  360. ib->ptr[ib->length_dw++] = 0x00000000;
  361. ib->ptr[ib->length_dw++] = 0x00000000;
  362. }
  363. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  364. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  365. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  366. ib->ptr[ib->length_dw++] = dummy;
  367. ib->ptr[ib->length_dw++] = 0x00000001;
  368. for (i = ib->length_dw; i < ib_size_dw; ++i)
  369. ib->ptr[i] = 0x0;
  370. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  371. &amdgpu_vce_free_job,
  372. AMDGPU_FENCE_OWNER_UNDEFINED,
  373. &f);
  374. if (r)
  375. goto err;
  376. if (fence)
  377. *fence = fence_get(f);
  378. fence_put(f);
  379. if (amdgpu_enable_scheduler)
  380. return 0;
  381. err:
  382. amdgpu_ib_free(adev, ib);
  383. kfree(ib);
  384. return r;
  385. }
  386. /**
  387. * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
  388. *
  389. * @adev: amdgpu_device pointer
  390. * @ring: ring we should submit the msg to
  391. * @handle: VCE session handle to use
  392. * @fence: optional fence to return
  393. *
  394. * Close up a stream for HW test or if userspace failed to do so
  395. */
  396. int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  397. struct fence **fence)
  398. {
  399. const unsigned ib_size_dw = 1024;
  400. struct amdgpu_ib *ib = NULL;
  401. struct fence *f = NULL;
  402. struct amdgpu_device *adev = ring->adev;
  403. uint64_t dummy;
  404. int i, r;
  405. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  406. if (!ib)
  407. return -ENOMEM;
  408. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib);
  409. if (r) {
  410. kfree(ib);
  411. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  412. return r;
  413. }
  414. dummy = ib->gpu_addr + 1024;
  415. /* stitch together an VCE destroy msg */
  416. ib->length_dw = 0;
  417. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  418. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  419. ib->ptr[ib->length_dw++] = handle;
  420. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  421. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  422. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  423. ib->ptr[ib->length_dw++] = dummy;
  424. ib->ptr[ib->length_dw++] = 0x00000001;
  425. ib->ptr[ib->length_dw++] = 0x00000008; /* len */
  426. ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
  427. for (i = ib->length_dw; i < ib_size_dw; ++i)
  428. ib->ptr[i] = 0x0;
  429. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  430. &amdgpu_vce_free_job,
  431. AMDGPU_FENCE_OWNER_UNDEFINED,
  432. &f);
  433. if (r)
  434. goto err;
  435. if (fence)
  436. *fence = fence_get(f);
  437. fence_put(f);
  438. if (amdgpu_enable_scheduler)
  439. return 0;
  440. err:
  441. amdgpu_ib_free(adev, ib);
  442. kfree(ib);
  443. return r;
  444. }
  445. /**
  446. * amdgpu_vce_cs_reloc - command submission relocation
  447. *
  448. * @p: parser context
  449. * @lo: address of lower dword
  450. * @hi: address of higher dword
  451. * @size: minimum size
  452. *
  453. * Patch relocation inside command stream with real buffer address
  454. */
  455. static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
  456. int lo, int hi, unsigned size, uint32_t index)
  457. {
  458. struct amdgpu_bo_va_mapping *mapping;
  459. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  460. struct amdgpu_bo *bo;
  461. uint64_t addr;
  462. if (index == 0xffffffff)
  463. index = 0;
  464. addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
  465. ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
  466. addr += ((uint64_t)size) * ((uint64_t)index);
  467. mapping = amdgpu_cs_find_mapping(p, addr, &bo);
  468. if (mapping == NULL) {
  469. DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
  470. addr, lo, hi, size, index);
  471. return -EINVAL;
  472. }
  473. if ((addr + (uint64_t)size) >
  474. ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  475. DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
  476. addr, lo, hi);
  477. return -EINVAL;
  478. }
  479. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  480. addr += amdgpu_bo_gpu_offset(bo);
  481. addr -= ((uint64_t)size) * ((uint64_t)index);
  482. ib->ptr[lo] = addr & 0xFFFFFFFF;
  483. ib->ptr[hi] = addr >> 32;
  484. return 0;
  485. }
  486. /**
  487. * amdgpu_vce_validate_handle - validate stream handle
  488. *
  489. * @p: parser context
  490. * @handle: handle to validate
  491. * @allocated: allocated a new handle?
  492. *
  493. * Validates the handle and return the found session index or -EINVAL
  494. * we we don't have another free session index.
  495. */
  496. static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
  497. uint32_t handle, bool *allocated)
  498. {
  499. unsigned i;
  500. *allocated = false;
  501. /* validate the handle */
  502. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  503. if (atomic_read(&p->adev->vce.handles[i]) == handle) {
  504. if (p->adev->vce.filp[i] != p->filp) {
  505. DRM_ERROR("VCE handle collision detected!\n");
  506. return -EINVAL;
  507. }
  508. return i;
  509. }
  510. }
  511. /* handle not found try to alloc a new one */
  512. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  513. if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
  514. p->adev->vce.filp[i] = p->filp;
  515. p->adev->vce.img_size[i] = 0;
  516. *allocated = true;
  517. return i;
  518. }
  519. }
  520. DRM_ERROR("No more free VCE handles!\n");
  521. return -EINVAL;
  522. }
  523. /**
  524. * amdgpu_vce_cs_parse - parse and validate the command stream
  525. *
  526. * @p: parser context
  527. *
  528. */
  529. int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
  530. {
  531. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  532. unsigned fb_idx = 0, bs_idx = 0;
  533. int session_idx = -1;
  534. bool destroyed = false;
  535. bool created = false;
  536. bool allocated = false;
  537. uint32_t tmp, handle = 0;
  538. uint32_t *size = &tmp;
  539. int i, r = 0, idx = 0;
  540. amdgpu_vce_note_usage(p->adev);
  541. while (idx < ib->length_dw) {
  542. uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
  543. uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
  544. if ((len < 8) || (len & 3)) {
  545. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  546. r = -EINVAL;
  547. goto out;
  548. }
  549. if (destroyed) {
  550. DRM_ERROR("No other command allowed after destroy!\n");
  551. r = -EINVAL;
  552. goto out;
  553. }
  554. switch (cmd) {
  555. case 0x00000001: // session
  556. handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
  557. session_idx = amdgpu_vce_validate_handle(p, handle,
  558. &allocated);
  559. if (session_idx < 0)
  560. return session_idx;
  561. size = &p->adev->vce.img_size[session_idx];
  562. break;
  563. case 0x00000002: // task info
  564. fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
  565. bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
  566. break;
  567. case 0x01000001: // create
  568. created = true;
  569. if (!allocated) {
  570. DRM_ERROR("Handle already in use!\n");
  571. r = -EINVAL;
  572. goto out;
  573. }
  574. *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
  575. amdgpu_get_ib_value(p, ib_idx, idx + 10) *
  576. 8 * 3 / 2;
  577. break;
  578. case 0x04000001: // config extension
  579. case 0x04000002: // pic control
  580. case 0x04000005: // rate control
  581. case 0x04000007: // motion estimation
  582. case 0x04000008: // rdo
  583. case 0x04000009: // vui
  584. case 0x05000002: // auxiliary buffer
  585. break;
  586. case 0x03000001: // encode
  587. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
  588. *size, 0);
  589. if (r)
  590. goto out;
  591. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
  592. *size / 3, 0);
  593. if (r)
  594. goto out;
  595. break;
  596. case 0x02000001: // destroy
  597. destroyed = true;
  598. break;
  599. case 0x05000001: // context buffer
  600. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  601. *size * 2, 0);
  602. if (r)
  603. goto out;
  604. break;
  605. case 0x05000004: // video bitstream buffer
  606. tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
  607. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  608. tmp, bs_idx);
  609. if (r)
  610. goto out;
  611. break;
  612. case 0x05000005: // feedback buffer
  613. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  614. 4096, fb_idx);
  615. if (r)
  616. goto out;
  617. break;
  618. default:
  619. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  620. r = -EINVAL;
  621. goto out;
  622. }
  623. if (session_idx == -1) {
  624. DRM_ERROR("no session command at start of IB\n");
  625. r = -EINVAL;
  626. goto out;
  627. }
  628. idx += len / 4;
  629. }
  630. if (allocated && !created) {
  631. DRM_ERROR("New session without create command!\n");
  632. r = -ENOENT;
  633. }
  634. out:
  635. if ((!r && destroyed) || (r && allocated)) {
  636. /*
  637. * IB contains a destroy msg or we have allocated an
  638. * handle and got an error, anyway free the handle
  639. */
  640. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  641. atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
  642. }
  643. return r;
  644. }
  645. /**
  646. * amdgpu_vce_ring_emit_semaphore - emit a semaphore command
  647. *
  648. * @ring: engine to use
  649. * @semaphore: address of semaphore
  650. * @emit_wait: true=emit wait, false=emit signal
  651. *
  652. */
  653. bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring,
  654. struct amdgpu_semaphore *semaphore,
  655. bool emit_wait)
  656. {
  657. uint64_t addr = semaphore->gpu_addr;
  658. amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE);
  659. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  660. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  661. amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
  662. if (!emit_wait)
  663. amdgpu_ring_write(ring, VCE_CMD_END);
  664. return true;
  665. }
  666. /**
  667. * amdgpu_vce_ring_emit_ib - execute indirect buffer
  668. *
  669. * @ring: engine to use
  670. * @ib: the IB to execute
  671. *
  672. */
  673. void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  674. {
  675. amdgpu_ring_write(ring, VCE_CMD_IB);
  676. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  677. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  678. amdgpu_ring_write(ring, ib->length_dw);
  679. }
  680. /**
  681. * amdgpu_vce_ring_emit_fence - add a fence command to the ring
  682. *
  683. * @ring: engine to use
  684. * @fence: the fence
  685. *
  686. */
  687. void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  688. unsigned flags)
  689. {
  690. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  691. amdgpu_ring_write(ring, VCE_CMD_FENCE);
  692. amdgpu_ring_write(ring, addr);
  693. amdgpu_ring_write(ring, upper_32_bits(addr));
  694. amdgpu_ring_write(ring, seq);
  695. amdgpu_ring_write(ring, VCE_CMD_TRAP);
  696. amdgpu_ring_write(ring, VCE_CMD_END);
  697. }
  698. /**
  699. * amdgpu_vce_ring_test_ring - test if VCE ring is working
  700. *
  701. * @ring: the engine to test on
  702. *
  703. */
  704. int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
  705. {
  706. struct amdgpu_device *adev = ring->adev;
  707. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  708. unsigned i;
  709. int r;
  710. r = amdgpu_ring_lock(ring, 16);
  711. if (r) {
  712. DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
  713. ring->idx, r);
  714. return r;
  715. }
  716. amdgpu_ring_write(ring, VCE_CMD_END);
  717. amdgpu_ring_unlock_commit(ring);
  718. for (i = 0; i < adev->usec_timeout; i++) {
  719. if (amdgpu_ring_get_rptr(ring) != rptr)
  720. break;
  721. DRM_UDELAY(1);
  722. }
  723. if (i < adev->usec_timeout) {
  724. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  725. ring->idx, i);
  726. } else {
  727. DRM_ERROR("amdgpu: ring %d test failed\n",
  728. ring->idx);
  729. r = -ETIMEDOUT;
  730. }
  731. return r;
  732. }
  733. /**
  734. * amdgpu_vce_ring_test_ib - test if VCE IBs are working
  735. *
  736. * @ring: the engine to test on
  737. *
  738. */
  739. int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
  740. {
  741. struct fence *fence = NULL;
  742. int r;
  743. /* skip vce ring1 ib test for now, since it's not reliable */
  744. if (ring == &ring->adev->vce.ring[1])
  745. return 0;
  746. r = amdgpu_vce_get_create_msg(ring, 1, NULL);
  747. if (r) {
  748. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  749. goto error;
  750. }
  751. r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
  752. if (r) {
  753. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  754. goto error;
  755. }
  756. r = fence_wait(fence, false);
  757. if (r) {
  758. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  759. } else {
  760. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  761. }
  762. error:
  763. fence_put(fence);
  764. return r;
  765. }