radeon_uvd.c 20 KB

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