radeon_uvd.c 24 KB

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