radeon_uvd.c 20 KB

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