amdgpu_vce.c 24 KB

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