radeon_uvd.c 20 KB

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