amdgpu_vce.c 25 KB

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