amdgpu_vce.c 21 KB

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