amdgpu_vce.c 25 KB

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