radeon_uvd.c 22 KB

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