amdgpu_vce.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. cancel_delayed_work_sync(&adev->vce.idle_work);
  194. /* TODO: suspending running encoding sessions isn't supported */
  195. return -EINVAL;
  196. }
  197. /**
  198. * amdgpu_vce_resume - pin VCE fw memory
  199. *
  200. * @adev: amdgpu_device pointer
  201. *
  202. */
  203. int amdgpu_vce_resume(struct amdgpu_device *adev)
  204. {
  205. void *cpu_addr;
  206. const struct common_firmware_header *hdr;
  207. unsigned offset;
  208. int r;
  209. if (adev->vce.vcpu_bo == NULL)
  210. return -EINVAL;
  211. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  212. if (r) {
  213. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  214. return r;
  215. }
  216. r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
  217. if (r) {
  218. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  219. dev_err(adev->dev, "(%d) VCE map failed\n", r);
  220. return r;
  221. }
  222. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  223. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  224. memcpy(cpu_addr, (adev->vce.fw->data) + offset,
  225. (adev->vce.fw->size) - offset);
  226. amdgpu_bo_kunmap(adev->vce.vcpu_bo);
  227. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  228. return 0;
  229. }
  230. /**
  231. * amdgpu_vce_idle_work_handler - power off VCE
  232. *
  233. * @work: pointer to work structure
  234. *
  235. * power of VCE when it's not used any more
  236. */
  237. static void amdgpu_vce_idle_work_handler(struct work_struct *work)
  238. {
  239. struct amdgpu_device *adev =
  240. container_of(work, struct amdgpu_device, vce.idle_work.work);
  241. if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) &&
  242. (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) {
  243. if (adev->pm.dpm_enabled) {
  244. amdgpu_dpm_enable_vce(adev, false);
  245. } else {
  246. amdgpu_asic_set_vce_clocks(adev, 0, 0);
  247. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  248. AMD_PG_STATE_GATE);
  249. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  250. AMD_CG_STATE_GATE);
  251. }
  252. } else {
  253. schedule_delayed_work(&adev->vce.idle_work,
  254. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  255. }
  256. }
  257. /**
  258. * amdgpu_vce_note_usage - power up VCE
  259. *
  260. * @adev: amdgpu_device pointer
  261. *
  262. * Make sure VCE is powerd up when we want to use it
  263. */
  264. static void amdgpu_vce_note_usage(struct amdgpu_device *adev)
  265. {
  266. bool streams_changed = false;
  267. bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
  268. set_clocks &= schedule_delayed_work(&adev->vce.idle_work,
  269. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  270. if (adev->pm.dpm_enabled) {
  271. /* XXX figure out if the streams changed */
  272. streams_changed = false;
  273. }
  274. if (set_clocks || streams_changed) {
  275. if (adev->pm.dpm_enabled) {
  276. amdgpu_dpm_enable_vce(adev, true);
  277. } else {
  278. amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
  279. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  280. AMD_CG_STATE_UNGATE);
  281. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  282. AMD_PG_STATE_UNGATE);
  283. }
  284. }
  285. }
  286. /**
  287. * amdgpu_vce_free_handles - free still open VCE handles
  288. *
  289. * @adev: amdgpu_device pointer
  290. * @filp: drm file pointer
  291. *
  292. * Close all VCE handles still open by this file pointer
  293. */
  294. void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
  295. {
  296. struct amdgpu_ring *ring = &adev->vce.ring[0];
  297. int i, r;
  298. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  299. uint32_t handle = atomic_read(&adev->vce.handles[i]);
  300. if (!handle || adev->vce.filp[i] != filp)
  301. continue;
  302. amdgpu_vce_note_usage(adev);
  303. r = amdgpu_vce_get_destroy_msg(ring, handle, NULL);
  304. if (r)
  305. DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
  306. adev->vce.filp[i] = NULL;
  307. atomic_set(&adev->vce.handles[i], 0);
  308. }
  309. }
  310. static int amdgpu_vce_free_job(
  311. struct amdgpu_job *job)
  312. {
  313. amdgpu_ib_free(job->adev, job->ibs);
  314. kfree(job->ibs);
  315. return 0;
  316. }
  317. /**
  318. * amdgpu_vce_get_create_msg - generate a VCE create msg
  319. *
  320. * @adev: amdgpu_device pointer
  321. * @ring: ring we should submit the msg to
  322. * @handle: VCE session handle to use
  323. * @fence: optional fence to return
  324. *
  325. * Open up a stream for HW test
  326. */
  327. int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  328. struct fence **fence)
  329. {
  330. const unsigned ib_size_dw = 1024;
  331. struct amdgpu_ib *ib = NULL;
  332. struct fence *f = NULL;
  333. struct amdgpu_device *adev = ring->adev;
  334. uint64_t dummy;
  335. int i, r;
  336. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  337. if (!ib)
  338. return -ENOMEM;
  339. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib);
  340. if (r) {
  341. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  342. kfree(ib);
  343. return r;
  344. }
  345. dummy = ib->gpu_addr + 1024;
  346. /* stitch together an VCE create msg */
  347. ib->length_dw = 0;
  348. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  349. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  350. ib->ptr[ib->length_dw++] = handle;
  351. if ((ring->adev->vce.fw_version >> 24) >= 52)
  352. ib->ptr[ib->length_dw++] = 0x00000040; /* len */
  353. else
  354. ib->ptr[ib->length_dw++] = 0x00000030; /* len */
  355. ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
  356. ib->ptr[ib->length_dw++] = 0x00000000;
  357. ib->ptr[ib->length_dw++] = 0x00000042;
  358. ib->ptr[ib->length_dw++] = 0x0000000a;
  359. ib->ptr[ib->length_dw++] = 0x00000001;
  360. ib->ptr[ib->length_dw++] = 0x00000080;
  361. ib->ptr[ib->length_dw++] = 0x00000060;
  362. ib->ptr[ib->length_dw++] = 0x00000100;
  363. ib->ptr[ib->length_dw++] = 0x00000100;
  364. ib->ptr[ib->length_dw++] = 0x0000000c;
  365. ib->ptr[ib->length_dw++] = 0x00000000;
  366. if ((ring->adev->vce.fw_version >> 24) >= 52) {
  367. ib->ptr[ib->length_dw++] = 0x00000000;
  368. ib->ptr[ib->length_dw++] = 0x00000000;
  369. ib->ptr[ib->length_dw++] = 0x00000000;
  370. ib->ptr[ib->length_dw++] = 0x00000000;
  371. }
  372. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  373. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  374. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  375. ib->ptr[ib->length_dw++] = dummy;
  376. ib->ptr[ib->length_dw++] = 0x00000001;
  377. for (i = ib->length_dw; i < ib_size_dw; ++i)
  378. ib->ptr[i] = 0x0;
  379. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  380. &amdgpu_vce_free_job,
  381. AMDGPU_FENCE_OWNER_UNDEFINED,
  382. &f);
  383. if (r)
  384. goto err;
  385. if (fence)
  386. *fence = fence_get(f);
  387. fence_put(f);
  388. if (amdgpu_enable_scheduler)
  389. return 0;
  390. err:
  391. amdgpu_ib_free(adev, ib);
  392. kfree(ib);
  393. return r;
  394. }
  395. /**
  396. * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
  397. *
  398. * @adev: amdgpu_device pointer
  399. * @ring: ring we should submit the msg to
  400. * @handle: VCE session handle to use
  401. * @fence: optional fence to return
  402. *
  403. * Close up a stream for HW test or if userspace failed to do so
  404. */
  405. int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  406. struct fence **fence)
  407. {
  408. const unsigned ib_size_dw = 1024;
  409. struct amdgpu_ib *ib = NULL;
  410. struct fence *f = NULL;
  411. struct amdgpu_device *adev = ring->adev;
  412. uint64_t dummy;
  413. int i, r;
  414. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  415. if (!ib)
  416. return -ENOMEM;
  417. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib);
  418. if (r) {
  419. kfree(ib);
  420. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  421. return r;
  422. }
  423. dummy = ib->gpu_addr + 1024;
  424. /* stitch together an VCE destroy msg */
  425. ib->length_dw = 0;
  426. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  427. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  428. ib->ptr[ib->length_dw++] = handle;
  429. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  430. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  431. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  432. ib->ptr[ib->length_dw++] = dummy;
  433. ib->ptr[ib->length_dw++] = 0x00000001;
  434. ib->ptr[ib->length_dw++] = 0x00000008; /* len */
  435. ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
  436. for (i = ib->length_dw; i < ib_size_dw; ++i)
  437. ib->ptr[i] = 0x0;
  438. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  439. &amdgpu_vce_free_job,
  440. AMDGPU_FENCE_OWNER_UNDEFINED,
  441. &f);
  442. if (r)
  443. goto err;
  444. if (fence)
  445. *fence = fence_get(f);
  446. fence_put(f);
  447. if (amdgpu_enable_scheduler)
  448. return 0;
  449. err:
  450. amdgpu_ib_free(adev, ib);
  451. kfree(ib);
  452. return r;
  453. }
  454. /**
  455. * amdgpu_vce_cs_reloc - command submission relocation
  456. *
  457. * @p: parser context
  458. * @lo: address of lower dword
  459. * @hi: address of higher dword
  460. * @size: minimum size
  461. *
  462. * Patch relocation inside command stream with real buffer address
  463. */
  464. static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
  465. int lo, int hi, unsigned size, uint32_t index)
  466. {
  467. struct amdgpu_bo_va_mapping *mapping;
  468. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  469. struct amdgpu_bo *bo;
  470. uint64_t addr;
  471. if (index == 0xffffffff)
  472. index = 0;
  473. addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
  474. ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
  475. addr += ((uint64_t)size) * ((uint64_t)index);
  476. mapping = amdgpu_cs_find_mapping(p, addr, &bo);
  477. if (mapping == NULL) {
  478. DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
  479. addr, lo, hi, size, index);
  480. return -EINVAL;
  481. }
  482. if ((addr + (uint64_t)size) >
  483. ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  484. DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
  485. addr, lo, hi);
  486. return -EINVAL;
  487. }
  488. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  489. addr += amdgpu_bo_gpu_offset(bo);
  490. addr -= ((uint64_t)size) * ((uint64_t)index);
  491. ib->ptr[lo] = addr & 0xFFFFFFFF;
  492. ib->ptr[hi] = addr >> 32;
  493. return 0;
  494. }
  495. /**
  496. * amdgpu_vce_validate_handle - validate stream handle
  497. *
  498. * @p: parser context
  499. * @handle: handle to validate
  500. * @allocated: allocated a new handle?
  501. *
  502. * Validates the handle and return the found session index or -EINVAL
  503. * we we don't have another free session index.
  504. */
  505. static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
  506. uint32_t handle, bool *allocated)
  507. {
  508. unsigned i;
  509. *allocated = false;
  510. /* validate the handle */
  511. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  512. if (atomic_read(&p->adev->vce.handles[i]) == handle) {
  513. if (p->adev->vce.filp[i] != p->filp) {
  514. DRM_ERROR("VCE handle collision detected!\n");
  515. return -EINVAL;
  516. }
  517. return i;
  518. }
  519. }
  520. /* handle not found try to alloc a new one */
  521. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  522. if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
  523. p->adev->vce.filp[i] = p->filp;
  524. p->adev->vce.img_size[i] = 0;
  525. *allocated = true;
  526. return i;
  527. }
  528. }
  529. DRM_ERROR("No more free VCE handles!\n");
  530. return -EINVAL;
  531. }
  532. /**
  533. * amdgpu_vce_cs_parse - parse and validate the command stream
  534. *
  535. * @p: parser context
  536. *
  537. */
  538. int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
  539. {
  540. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  541. unsigned fb_idx = 0, bs_idx = 0;
  542. int session_idx = -1;
  543. bool destroyed = false;
  544. bool created = false;
  545. bool allocated = false;
  546. uint32_t tmp, handle = 0;
  547. uint32_t *size = &tmp;
  548. int i, r = 0, idx = 0;
  549. amdgpu_vce_note_usage(p->adev);
  550. while (idx < ib->length_dw) {
  551. uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
  552. uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
  553. if ((len < 8) || (len & 3)) {
  554. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  555. r = -EINVAL;
  556. goto out;
  557. }
  558. if (destroyed) {
  559. DRM_ERROR("No other command allowed after destroy!\n");
  560. r = -EINVAL;
  561. goto out;
  562. }
  563. switch (cmd) {
  564. case 0x00000001: // session
  565. handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
  566. session_idx = amdgpu_vce_validate_handle(p, handle,
  567. &allocated);
  568. if (session_idx < 0)
  569. return session_idx;
  570. size = &p->adev->vce.img_size[session_idx];
  571. break;
  572. case 0x00000002: // task info
  573. fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
  574. bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
  575. break;
  576. case 0x01000001: // create
  577. created = true;
  578. if (!allocated) {
  579. DRM_ERROR("Handle already in use!\n");
  580. r = -EINVAL;
  581. goto out;
  582. }
  583. *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
  584. amdgpu_get_ib_value(p, ib_idx, idx + 10) *
  585. 8 * 3 / 2;
  586. break;
  587. case 0x04000001: // config extension
  588. case 0x04000002: // pic control
  589. case 0x04000005: // rate control
  590. case 0x04000007: // motion estimation
  591. case 0x04000008: // rdo
  592. case 0x04000009: // vui
  593. case 0x05000002: // auxiliary buffer
  594. break;
  595. case 0x03000001: // encode
  596. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
  597. *size, 0);
  598. if (r)
  599. goto out;
  600. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
  601. *size / 3, 0);
  602. if (r)
  603. goto out;
  604. break;
  605. case 0x02000001: // destroy
  606. destroyed = true;
  607. break;
  608. case 0x05000001: // context buffer
  609. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  610. *size * 2, 0);
  611. if (r)
  612. goto out;
  613. break;
  614. case 0x05000004: // video bitstream buffer
  615. tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
  616. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  617. tmp, bs_idx);
  618. if (r)
  619. goto out;
  620. break;
  621. case 0x05000005: // feedback buffer
  622. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  623. 4096, fb_idx);
  624. if (r)
  625. goto out;
  626. break;
  627. default:
  628. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  629. r = -EINVAL;
  630. goto out;
  631. }
  632. if (session_idx == -1) {
  633. DRM_ERROR("no session command at start of IB\n");
  634. r = -EINVAL;
  635. goto out;
  636. }
  637. idx += len / 4;
  638. }
  639. if (allocated && !created) {
  640. DRM_ERROR("New session without create command!\n");
  641. r = -ENOENT;
  642. }
  643. out:
  644. if ((!r && destroyed) || (r && allocated)) {
  645. /*
  646. * IB contains a destroy msg or we have allocated an
  647. * handle and got an error, anyway free the handle
  648. */
  649. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  650. atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
  651. }
  652. return r;
  653. }
  654. /**
  655. * amdgpu_vce_ring_emit_semaphore - emit a semaphore command
  656. *
  657. * @ring: engine to use
  658. * @semaphore: address of semaphore
  659. * @emit_wait: true=emit wait, false=emit signal
  660. *
  661. */
  662. bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring,
  663. struct amdgpu_semaphore *semaphore,
  664. bool emit_wait)
  665. {
  666. uint64_t addr = semaphore->gpu_addr;
  667. amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE);
  668. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  669. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  670. amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
  671. if (!emit_wait)
  672. amdgpu_ring_write(ring, VCE_CMD_END);
  673. return true;
  674. }
  675. /**
  676. * amdgpu_vce_ring_emit_ib - execute indirect buffer
  677. *
  678. * @ring: engine to use
  679. * @ib: the IB to execute
  680. *
  681. */
  682. void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  683. {
  684. amdgpu_ring_write(ring, VCE_CMD_IB);
  685. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  686. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  687. amdgpu_ring_write(ring, ib->length_dw);
  688. }
  689. /**
  690. * amdgpu_vce_ring_emit_fence - add a fence command to the ring
  691. *
  692. * @ring: engine to use
  693. * @fence: the fence
  694. *
  695. */
  696. void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  697. unsigned flags)
  698. {
  699. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  700. amdgpu_ring_write(ring, VCE_CMD_FENCE);
  701. amdgpu_ring_write(ring, addr);
  702. amdgpu_ring_write(ring, upper_32_bits(addr));
  703. amdgpu_ring_write(ring, seq);
  704. amdgpu_ring_write(ring, VCE_CMD_TRAP);
  705. amdgpu_ring_write(ring, VCE_CMD_END);
  706. }
  707. /**
  708. * amdgpu_vce_ring_test_ring - test if VCE ring is working
  709. *
  710. * @ring: the engine to test on
  711. *
  712. */
  713. int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
  714. {
  715. struct amdgpu_device *adev = ring->adev;
  716. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  717. unsigned i;
  718. int r;
  719. r = amdgpu_ring_lock(ring, 16);
  720. if (r) {
  721. DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
  722. ring->idx, r);
  723. return r;
  724. }
  725. amdgpu_ring_write(ring, VCE_CMD_END);
  726. amdgpu_ring_unlock_commit(ring);
  727. for (i = 0; i < adev->usec_timeout; i++) {
  728. if (amdgpu_ring_get_rptr(ring) != rptr)
  729. break;
  730. DRM_UDELAY(1);
  731. }
  732. if (i < adev->usec_timeout) {
  733. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  734. ring->idx, i);
  735. } else {
  736. DRM_ERROR("amdgpu: ring %d test failed\n",
  737. ring->idx);
  738. r = -ETIMEDOUT;
  739. }
  740. return r;
  741. }
  742. /**
  743. * amdgpu_vce_ring_test_ib - test if VCE IBs are working
  744. *
  745. * @ring: the engine to test on
  746. *
  747. */
  748. int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
  749. {
  750. struct fence *fence = NULL;
  751. int r;
  752. /* skip vce ring1 ib test for now, since it's not reliable */
  753. if (ring == &ring->adev->vce.ring[1])
  754. return 0;
  755. r = amdgpu_vce_get_create_msg(ring, 1, NULL);
  756. if (r) {
  757. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  758. goto error;
  759. }
  760. r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
  761. if (r) {
  762. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  763. goto error;
  764. }
  765. r = fence_wait(fence, false);
  766. if (r) {
  767. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  768. } else {
  769. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  770. }
  771. error:
  772. fence_put(fence);
  773. return r;
  774. }