radeon_uvd.c 23 KB

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