radeon_uvd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Copyright 2011 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. */
  26. /*
  27. * Authors:
  28. * Christian König <deathsimple@vodafone.de>
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm.h>
  34. #include "radeon.h"
  35. #include "r600d.h"
  36. /* 1 second timeout */
  37. #define UVD_IDLE_TIMEOUT_MS 1000
  38. /* Firmware Names */
  39. #define FIRMWARE_RV710 "radeon/RV710_uvd.bin"
  40. #define FIRMWARE_CYPRESS "radeon/CYPRESS_uvd.bin"
  41. #define FIRMWARE_SUMO "radeon/SUMO_uvd.bin"
  42. #define FIRMWARE_TAHITI "radeon/TAHITI_uvd.bin"
  43. MODULE_FIRMWARE(FIRMWARE_RV710);
  44. MODULE_FIRMWARE(FIRMWARE_CYPRESS);
  45. MODULE_FIRMWARE(FIRMWARE_SUMO);
  46. MODULE_FIRMWARE(FIRMWARE_TAHITI);
  47. static void radeon_uvd_idle_work_handler(struct work_struct *work);
  48. int radeon_uvd_init(struct radeon_device *rdev)
  49. {
  50. struct platform_device *pdev;
  51. unsigned long bo_size;
  52. const char *fw_name;
  53. int i, r;
  54. INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
  55. pdev = platform_device_register_simple("radeon_uvd", 0, NULL, 0);
  56. r = IS_ERR(pdev);
  57. if (r) {
  58. dev_err(rdev->dev, "radeon_uvd: Failed to register firmware\n");
  59. return -EINVAL;
  60. }
  61. switch (rdev->family) {
  62. case CHIP_RV710:
  63. case CHIP_RV730:
  64. case CHIP_RV740:
  65. fw_name = FIRMWARE_RV710;
  66. break;
  67. case CHIP_CYPRESS:
  68. case CHIP_HEMLOCK:
  69. case CHIP_JUNIPER:
  70. case CHIP_REDWOOD:
  71. case CHIP_CEDAR:
  72. fw_name = FIRMWARE_CYPRESS;
  73. break;
  74. case CHIP_SUMO:
  75. case CHIP_SUMO2:
  76. case CHIP_PALM:
  77. case CHIP_CAYMAN:
  78. case CHIP_BARTS:
  79. case CHIP_TURKS:
  80. case CHIP_CAICOS:
  81. fw_name = FIRMWARE_SUMO;
  82. break;
  83. case CHIP_TAHITI:
  84. case CHIP_VERDE:
  85. case CHIP_PITCAIRN:
  86. case CHIP_ARUBA:
  87. fw_name = FIRMWARE_TAHITI;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. r = request_firmware(&rdev->uvd_fw, fw_name, &pdev->dev);
  93. if (r) {
  94. dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
  95. fw_name);
  96. platform_device_unregister(pdev);
  97. return r;
  98. }
  99. platform_device_unregister(pdev);
  100. bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
  101. RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE;
  102. r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
  103. RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->uvd.vcpu_bo);
  104. if (r) {
  105. dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
  106. return r;
  107. }
  108. r = radeon_uvd_resume(rdev);
  109. if (r)
  110. return r;
  111. memset(rdev->uvd.cpu_addr, 0, bo_size);
  112. memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
  113. r = radeon_uvd_suspend(rdev);
  114. if (r)
  115. return r;
  116. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  117. atomic_set(&rdev->uvd.handles[i], 0);
  118. rdev->uvd.filp[i] = NULL;
  119. }
  120. return 0;
  121. }
  122. void radeon_uvd_fini(struct radeon_device *rdev)
  123. {
  124. radeon_uvd_suspend(rdev);
  125. radeon_bo_unref(&rdev->uvd.vcpu_bo);
  126. }
  127. int radeon_uvd_suspend(struct radeon_device *rdev)
  128. {
  129. int r;
  130. if (rdev->uvd.vcpu_bo == NULL)
  131. return 0;
  132. r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
  133. if (!r) {
  134. radeon_bo_kunmap(rdev->uvd.vcpu_bo);
  135. radeon_bo_unpin(rdev->uvd.vcpu_bo);
  136. rdev->uvd.cpu_addr = NULL;
  137. if (!radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_CPU, NULL)) {
  138. radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
  139. }
  140. radeon_bo_unreserve(rdev->uvd.vcpu_bo);
  141. if (rdev->uvd.cpu_addr) {
  142. radeon_fence_driver_start_ring(rdev, R600_RING_TYPE_UVD_INDEX);
  143. } else {
  144. rdev->fence_drv[R600_RING_TYPE_UVD_INDEX].cpu_addr = NULL;
  145. }
  146. }
  147. return r;
  148. }
  149. int radeon_uvd_resume(struct radeon_device *rdev)
  150. {
  151. int r;
  152. if (rdev->uvd.vcpu_bo == NULL)
  153. return -EINVAL;
  154. r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
  155. if (r) {
  156. radeon_bo_unref(&rdev->uvd.vcpu_bo);
  157. dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
  158. return r;
  159. }
  160. /* Have been pin in cpu unmap unpin */
  161. radeon_bo_kunmap(rdev->uvd.vcpu_bo);
  162. radeon_bo_unpin(rdev->uvd.vcpu_bo);
  163. r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
  164. &rdev->uvd.gpu_addr);
  165. if (r) {
  166. radeon_bo_unreserve(rdev->uvd.vcpu_bo);
  167. radeon_bo_unref(&rdev->uvd.vcpu_bo);
  168. dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
  169. return r;
  170. }
  171. r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
  172. if (r) {
  173. dev_err(rdev->dev, "(%d) UVD map failed\n", r);
  174. return r;
  175. }
  176. radeon_bo_unreserve(rdev->uvd.vcpu_bo);
  177. return 0;
  178. }
  179. void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
  180. {
  181. rbo->placement.fpfn = 0 >> PAGE_SHIFT;
  182. rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
  183. }
  184. void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
  185. {
  186. int i, r;
  187. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  188. if (rdev->uvd.filp[i] == filp) {
  189. uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
  190. struct radeon_fence *fence;
  191. r = radeon_uvd_get_destroy_msg(rdev,
  192. R600_RING_TYPE_UVD_INDEX, handle, &fence);
  193. if (r) {
  194. DRM_ERROR("Error destroying UVD (%d)!\n", r);
  195. continue;
  196. }
  197. radeon_fence_wait(fence, false);
  198. radeon_fence_unref(&fence);
  199. rdev->uvd.filp[i] = NULL;
  200. atomic_set(&rdev->uvd.handles[i], 0);
  201. }
  202. }
  203. }
  204. static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
  205. {
  206. unsigned stream_type = msg[4];
  207. unsigned width = msg[6];
  208. unsigned height = msg[7];
  209. unsigned dpb_size = msg[9];
  210. unsigned pitch = msg[28];
  211. unsigned width_in_mb = width / 16;
  212. unsigned height_in_mb = ALIGN(height / 16, 2);
  213. unsigned image_size, tmp, min_dpb_size;
  214. image_size = width * height;
  215. image_size += image_size / 2;
  216. image_size = ALIGN(image_size, 1024);
  217. switch (stream_type) {
  218. case 0: /* H264 */
  219. /* reference picture buffer */
  220. min_dpb_size = image_size * 17;
  221. /* macroblock context buffer */
  222. min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
  223. /* IT surface buffer */
  224. min_dpb_size += width_in_mb * height_in_mb * 32;
  225. break;
  226. case 1: /* VC1 */
  227. /* reference picture buffer */
  228. min_dpb_size = image_size * 3;
  229. /* CONTEXT_BUFFER */
  230. min_dpb_size += width_in_mb * height_in_mb * 128;
  231. /* IT surface buffer */
  232. min_dpb_size += width_in_mb * 64;
  233. /* DB surface buffer */
  234. min_dpb_size += width_in_mb * 128;
  235. /* BP */
  236. tmp = max(width_in_mb, height_in_mb);
  237. min_dpb_size += ALIGN(tmp * 7 * 16, 64);
  238. break;
  239. case 3: /* MPEG2 */
  240. /* reference picture buffer */
  241. min_dpb_size = image_size * 3;
  242. break;
  243. case 4: /* MPEG4 */
  244. /* reference picture buffer */
  245. min_dpb_size = image_size * 3;
  246. /* CM */
  247. min_dpb_size += width_in_mb * height_in_mb * 64;
  248. /* IT surface buffer */
  249. min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
  250. break;
  251. default:
  252. DRM_ERROR("UVD codec not handled %d!\n", stream_type);
  253. return -EINVAL;
  254. }
  255. if (width > pitch) {
  256. DRM_ERROR("Invalid UVD decoding target pitch!\n");
  257. return -EINVAL;
  258. }
  259. if (dpb_size < min_dpb_size) {
  260. DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
  261. dpb_size, min_dpb_size);
  262. return -EINVAL;
  263. }
  264. buf_sizes[0x1] = dpb_size;
  265. buf_sizes[0x2] = image_size;
  266. return 0;
  267. }
  268. static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
  269. unsigned offset, unsigned buf_sizes[])
  270. {
  271. int32_t *msg, msg_type, handle;
  272. void *ptr;
  273. int i, r;
  274. if (offset & 0x3F) {
  275. DRM_ERROR("UVD messages must be 64 byte aligned!\n");
  276. return -EINVAL;
  277. }
  278. r = radeon_bo_kmap(bo, &ptr);
  279. if (r)
  280. return r;
  281. msg = ptr + offset;
  282. msg_type = msg[1];
  283. handle = msg[2];
  284. if (handle == 0) {
  285. DRM_ERROR("Invalid UVD handle!\n");
  286. return -EINVAL;
  287. }
  288. if (msg_type == 1) {
  289. /* it's a decode msg, calc buffer sizes */
  290. r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
  291. radeon_bo_kunmap(bo);
  292. if (r)
  293. return r;
  294. } else if (msg_type == 2) {
  295. /* it's a destroy msg, free the handle */
  296. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
  297. atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
  298. radeon_bo_kunmap(bo);
  299. return 0;
  300. } else {
  301. /* it's a create msg, no special handling needed */
  302. radeon_bo_kunmap(bo);
  303. }
  304. /* create or decode, validate the handle */
  305. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  306. if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
  307. return 0;
  308. }
  309. /* handle not found try to alloc a new one */
  310. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  311. if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
  312. p->rdev->uvd.filp[i] = p->filp;
  313. return 0;
  314. }
  315. }
  316. DRM_ERROR("No more free UVD handles!\n");
  317. return -EINVAL;
  318. }
  319. static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
  320. int data0, int data1,
  321. unsigned buf_sizes[])
  322. {
  323. struct radeon_cs_chunk *relocs_chunk;
  324. struct radeon_cs_reloc *reloc;
  325. unsigned idx, cmd, offset;
  326. uint64_t start, end;
  327. int r;
  328. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  329. offset = radeon_get_ib_value(p, data0);
  330. idx = radeon_get_ib_value(p, data1);
  331. if (idx >= relocs_chunk->length_dw) {
  332. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  333. idx, relocs_chunk->length_dw);
  334. return -EINVAL;
  335. }
  336. reloc = p->relocs_ptr[(idx / 4)];
  337. start = reloc->lobj.gpu_offset;
  338. end = start + radeon_bo_size(reloc->robj);
  339. start += offset;
  340. p->ib.ptr[data0] = start & 0xFFFFFFFF;
  341. p->ib.ptr[data1] = start >> 32;
  342. cmd = radeon_get_ib_value(p, p->idx) >> 1;
  343. if (cmd < 0x4) {
  344. if ((end - start) < buf_sizes[cmd]) {
  345. DRM_ERROR("buffer to small (%d / %d)!\n",
  346. (unsigned)(end - start), buf_sizes[cmd]);
  347. return -EINVAL;
  348. }
  349. } else if (cmd != 0x100) {
  350. DRM_ERROR("invalid UVD command %X!\n", cmd);
  351. return -EINVAL;
  352. }
  353. if ((start >> 28) != (end >> 28)) {
  354. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  355. start, end);
  356. return -EINVAL;
  357. }
  358. /* TODO: is this still necessary on NI+ ? */
  359. if ((cmd == 0 || cmd == 0x3) &&
  360. (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
  361. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  362. start, end);
  363. return -EINVAL;
  364. }
  365. if (cmd == 0) {
  366. r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
  367. if (r)
  368. return r;
  369. }
  370. return 0;
  371. }
  372. static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
  373. struct radeon_cs_packet *pkt,
  374. int *data0, int *data1,
  375. unsigned buf_sizes[])
  376. {
  377. int i, r;
  378. p->idx++;
  379. for (i = 0; i <= pkt->count; ++i) {
  380. switch (pkt->reg + i*4) {
  381. case UVD_GPCOM_VCPU_DATA0:
  382. *data0 = p->idx;
  383. break;
  384. case UVD_GPCOM_VCPU_DATA1:
  385. *data1 = p->idx;
  386. break;
  387. case UVD_GPCOM_VCPU_CMD:
  388. r = radeon_uvd_cs_reloc(p, *data0, *data1, buf_sizes);
  389. if (r)
  390. return r;
  391. break;
  392. case UVD_ENGINE_CNTL:
  393. break;
  394. default:
  395. DRM_ERROR("Invalid reg 0x%X!\n",
  396. pkt->reg + i*4);
  397. return -EINVAL;
  398. }
  399. p->idx++;
  400. }
  401. return 0;
  402. }
  403. int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
  404. {
  405. struct radeon_cs_packet pkt;
  406. int r, data0 = 0, data1 = 0;
  407. /* minimum buffer sizes */
  408. unsigned buf_sizes[] = {
  409. [0x00000000] = 2048,
  410. [0x00000001] = 32 * 1024 * 1024,
  411. [0x00000002] = 2048 * 1152 * 3,
  412. [0x00000003] = 2048,
  413. };
  414. if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
  415. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  416. p->chunks[p->chunk_ib_idx].length_dw);
  417. return -EINVAL;
  418. }
  419. if (p->chunk_relocs_idx == -1) {
  420. DRM_ERROR("No relocation chunk !\n");
  421. return -EINVAL;
  422. }
  423. do {
  424. r = radeon_cs_packet_parse(p, &pkt, p->idx);
  425. if (r)
  426. return r;
  427. switch (pkt.type) {
  428. case RADEON_PACKET_TYPE0:
  429. r = radeon_uvd_cs_reg(p, &pkt, &data0,
  430. &data1, buf_sizes);
  431. if (r)
  432. return r;
  433. break;
  434. case RADEON_PACKET_TYPE2:
  435. p->idx += pkt.count + 2;
  436. break;
  437. default:
  438. DRM_ERROR("Unknown packet type %d !\n", pkt.type);
  439. return -EINVAL;
  440. }
  441. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  442. return 0;
  443. }
  444. static int radeon_uvd_send_msg(struct radeon_device *rdev,
  445. int ring, struct radeon_bo *bo,
  446. struct radeon_fence **fence)
  447. {
  448. struct ttm_validate_buffer tv;
  449. struct list_head head;
  450. struct radeon_ib ib;
  451. uint64_t addr;
  452. int i, r;
  453. memset(&tv, 0, sizeof(tv));
  454. tv.bo = &bo->tbo;
  455. INIT_LIST_HEAD(&head);
  456. list_add(&tv.head, &head);
  457. r = ttm_eu_reserve_buffers(&head);
  458. if (r)
  459. return r;
  460. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
  461. radeon_uvd_force_into_uvd_segment(bo);
  462. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  463. if (r) {
  464. ttm_eu_backoff_reservation(&head);
  465. return r;
  466. }
  467. r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
  468. if (r) {
  469. ttm_eu_backoff_reservation(&head);
  470. return r;
  471. }
  472. addr = radeon_bo_gpu_offset(bo);
  473. ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
  474. ib.ptr[1] = addr;
  475. ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
  476. ib.ptr[3] = addr >> 32;
  477. ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
  478. ib.ptr[5] = 0;
  479. for (i = 6; i < 16; ++i)
  480. ib.ptr[i] = PACKET2(0);
  481. ib.length_dw = 16;
  482. r = radeon_ib_schedule(rdev, &ib, NULL);
  483. if (r) {
  484. ttm_eu_backoff_reservation(&head);
  485. return r;
  486. }
  487. ttm_eu_fence_buffer_objects(&head, ib.fence);
  488. if (fence)
  489. *fence = radeon_fence_ref(ib.fence);
  490. radeon_ib_free(rdev, &ib);
  491. radeon_bo_unref(&bo);
  492. return 0;
  493. }
  494. /* multiple fence commands without any stream commands in between can
  495. crash the vcpu so just try to emmit a dummy create/destroy msg to
  496. avoid this */
  497. int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
  498. uint32_t handle, struct radeon_fence **fence)
  499. {
  500. struct radeon_bo *bo;
  501. uint32_t *msg;
  502. int r, i;
  503. r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
  504. RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
  505. if (r)
  506. return r;
  507. r = radeon_bo_reserve(bo, false);
  508. if (r) {
  509. radeon_bo_unref(&bo);
  510. return r;
  511. }
  512. r = radeon_bo_kmap(bo, (void **)&msg);
  513. if (r) {
  514. radeon_bo_unreserve(bo);
  515. radeon_bo_unref(&bo);
  516. return r;
  517. }
  518. /* stitch together an UVD create msg */
  519. msg[0] = cpu_to_le32(0x00000de4);
  520. msg[1] = cpu_to_le32(0x00000000);
  521. msg[2] = cpu_to_le32(handle);
  522. msg[3] = cpu_to_le32(0x00000000);
  523. msg[4] = cpu_to_le32(0x00000000);
  524. msg[5] = cpu_to_le32(0x00000000);
  525. msg[6] = cpu_to_le32(0x00000000);
  526. msg[7] = cpu_to_le32(0x00000780);
  527. msg[8] = cpu_to_le32(0x00000440);
  528. msg[9] = cpu_to_le32(0x00000000);
  529. msg[10] = cpu_to_le32(0x01b37000);
  530. for (i = 11; i < 1024; ++i)
  531. msg[i] = cpu_to_le32(0x0);
  532. radeon_bo_kunmap(bo);
  533. radeon_bo_unreserve(bo);
  534. return radeon_uvd_send_msg(rdev, ring, bo, fence);
  535. }
  536. int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
  537. uint32_t handle, struct radeon_fence **fence)
  538. {
  539. struct radeon_bo *bo;
  540. uint32_t *msg;
  541. int r, i;
  542. r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
  543. RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
  544. if (r)
  545. return r;
  546. r = radeon_bo_reserve(bo, false);
  547. if (r) {
  548. radeon_bo_unref(&bo);
  549. return r;
  550. }
  551. r = radeon_bo_kmap(bo, (void **)&msg);
  552. if (r) {
  553. radeon_bo_unreserve(bo);
  554. radeon_bo_unref(&bo);
  555. return r;
  556. }
  557. /* stitch together an UVD destroy msg */
  558. msg[0] = cpu_to_le32(0x00000de4);
  559. msg[1] = cpu_to_le32(0x00000002);
  560. msg[2] = cpu_to_le32(handle);
  561. msg[3] = cpu_to_le32(0x00000000);
  562. for (i = 4; i < 1024; ++i)
  563. msg[i] = cpu_to_le32(0x0);
  564. radeon_bo_kunmap(bo);
  565. radeon_bo_unreserve(bo);
  566. return radeon_uvd_send_msg(rdev, ring, bo, fence);
  567. }
  568. static void radeon_uvd_idle_work_handler(struct work_struct *work)
  569. {
  570. struct radeon_device *rdev =
  571. container_of(work, struct radeon_device, uvd.idle_work.work);
  572. if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0)
  573. radeon_set_uvd_clocks(rdev, 0, 0);
  574. else
  575. schedule_delayed_work(&rdev->uvd.idle_work,
  576. msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
  577. }
  578. void radeon_uvd_note_usage(struct radeon_device *rdev)
  579. {
  580. bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
  581. set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
  582. msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
  583. if (set_clocks)
  584. radeon_set_uvd_clocks(rdev, 53300, 40000);
  585. }
  586. static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
  587. unsigned target_freq,
  588. unsigned pd_min,
  589. unsigned pd_even)
  590. {
  591. unsigned post_div = vco_freq / target_freq;
  592. /* adjust to post divider minimum value */
  593. if (post_div < pd_min)
  594. post_div = pd_min;
  595. /* we alway need a frequency less than or equal the target */
  596. if ((vco_freq / post_div) > target_freq)
  597. post_div += 1;
  598. /* post dividers above a certain value must be even */
  599. if (post_div > pd_even && post_div % 2)
  600. post_div += 1;
  601. return post_div;
  602. }
  603. /**
  604. * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
  605. *
  606. * @rdev: radeon_device pointer
  607. * @vclk: wanted VCLK
  608. * @dclk: wanted DCLK
  609. * @vco_min: minimum VCO frequency
  610. * @vco_max: maximum VCO frequency
  611. * @fb_factor: factor to multiply vco freq with
  612. * @fb_mask: limit and bitmask for feedback divider
  613. * @pd_min: post divider minimum
  614. * @pd_max: post divider maximum
  615. * @pd_even: post divider must be even above this value
  616. * @optimal_fb_div: resulting feedback divider
  617. * @optimal_vclk_div: resulting vclk post divider
  618. * @optimal_dclk_div: resulting dclk post divider
  619. *
  620. * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
  621. * Returns zero on success -EINVAL on error.
  622. */
  623. int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
  624. unsigned vclk, unsigned dclk,
  625. unsigned vco_min, unsigned vco_max,
  626. unsigned fb_factor, unsigned fb_mask,
  627. unsigned pd_min, unsigned pd_max,
  628. unsigned pd_even,
  629. unsigned *optimal_fb_div,
  630. unsigned *optimal_vclk_div,
  631. unsigned *optimal_dclk_div)
  632. {
  633. unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
  634. /* start off with something large */
  635. unsigned optimal_score = ~0;
  636. /* loop through vco from low to high */
  637. vco_min = max(max(vco_min, vclk), dclk);
  638. for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
  639. uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
  640. unsigned vclk_div, dclk_div, score;
  641. do_div(fb_div, ref_freq);
  642. /* fb div out of range ? */
  643. if (fb_div > fb_mask)
  644. break; /* it can oly get worse */
  645. fb_div &= fb_mask;
  646. /* calc vclk divider with current vco freq */
  647. vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
  648. pd_min, pd_even);
  649. if (vclk_div > pd_max)
  650. break; /* vco is too big, it has to stop */
  651. /* calc dclk divider with current vco freq */
  652. dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
  653. pd_min, pd_even);
  654. if (vclk_div > pd_max)
  655. break; /* vco is too big, it has to stop */
  656. /* calc score with current vco freq */
  657. score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
  658. /* determine if this vco setting is better than current optimal settings */
  659. if (score < optimal_score) {
  660. *optimal_fb_div = fb_div;
  661. *optimal_vclk_div = vclk_div;
  662. *optimal_dclk_div = dclk_div;
  663. optimal_score = score;
  664. if (optimal_score == 0)
  665. break; /* it can't get better than this */
  666. }
  667. }
  668. /* did we found a valid setup ? */
  669. if (optimal_score == ~0)
  670. return -EINVAL;
  671. return 0;
  672. }
  673. int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
  674. unsigned cg_upll_func_cntl)
  675. {
  676. unsigned i;
  677. /* make sure UPLL_CTLREQ is deasserted */
  678. WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
  679. mdelay(10);
  680. /* assert UPLL_CTLREQ */
  681. WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
  682. /* wait for CTLACK and CTLACK2 to get asserted */
  683. for (i = 0; i < 100; ++i) {
  684. uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
  685. if ((RREG32(cg_upll_func_cntl) & mask) == mask)
  686. break;
  687. mdelay(10);
  688. }
  689. /* deassert UPLL_CTLREQ */
  690. WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
  691. if (i == 100) {
  692. DRM_ERROR("Timeout setting UVD clocks!\n");
  693. return -ETIMEDOUT;
  694. }
  695. return 0;
  696. }