amdgpu_vce.c 28 KB

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