amdgpu_vce.c 27 KB

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