amdgpu_vce.c 28 KB

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