amdgpu_vce.c 28 KB

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