radeon_uvd.c 19 KB

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