amdgpu_vce.c 25 KB

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