amdgpu_vce.c 22 KB

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