radeon_uvd.c 20 KB

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