radeon_uvd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. if (bo->tbo.sync_obj) {
  285. r = radeon_fence_wait(bo->tbo.sync_obj, false);
  286. if (r) {
  287. DRM_ERROR("Failed waiting for UVD message (%d)!\n", r);
  288. return r;
  289. }
  290. }
  291. r = radeon_bo_kmap(bo, &ptr);
  292. if (r) {
  293. DRM_ERROR("Failed mapping the UVD message (%d)!\n", r);
  294. return r;
  295. }
  296. msg = ptr + offset;
  297. msg_type = msg[1];
  298. handle = msg[2];
  299. if (handle == 0) {
  300. DRM_ERROR("Invalid UVD handle!\n");
  301. return -EINVAL;
  302. }
  303. if (msg_type == 1) {
  304. /* it's a decode msg, calc buffer sizes */
  305. r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
  306. radeon_bo_kunmap(bo);
  307. if (r)
  308. return r;
  309. } else if (msg_type == 2) {
  310. /* it's a destroy msg, free the handle */
  311. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
  312. atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
  313. radeon_bo_kunmap(bo);
  314. return 0;
  315. } else {
  316. radeon_bo_kunmap(bo);
  317. if (msg_type != 0) {
  318. DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
  319. return -EINVAL;
  320. }
  321. /* it's a create msg, no special handling needed */
  322. }
  323. /* create or decode, validate the handle */
  324. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  325. if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
  326. return 0;
  327. }
  328. /* handle not found try to alloc a new one */
  329. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  330. if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
  331. p->rdev->uvd.filp[i] = p->filp;
  332. return 0;
  333. }
  334. }
  335. DRM_ERROR("No more free UVD handles!\n");
  336. return -EINVAL;
  337. }
  338. static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
  339. int data0, int data1,
  340. unsigned buf_sizes[], bool *has_msg_cmd)
  341. {
  342. struct radeon_cs_chunk *relocs_chunk;
  343. struct radeon_cs_reloc *reloc;
  344. unsigned idx, cmd, offset;
  345. uint64_t start, end;
  346. int r;
  347. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  348. offset = radeon_get_ib_value(p, data0);
  349. idx = radeon_get_ib_value(p, data1);
  350. if (idx >= relocs_chunk->length_dw) {
  351. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  352. idx, relocs_chunk->length_dw);
  353. return -EINVAL;
  354. }
  355. reloc = p->relocs_ptr[(idx / 4)];
  356. start = reloc->lobj.gpu_offset;
  357. end = start + radeon_bo_size(reloc->robj);
  358. start += offset;
  359. p->ib.ptr[data0] = start & 0xFFFFFFFF;
  360. p->ib.ptr[data1] = start >> 32;
  361. cmd = radeon_get_ib_value(p, p->idx) >> 1;
  362. if (cmd < 0x4) {
  363. if ((end - start) < buf_sizes[cmd]) {
  364. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  365. (unsigned)(end - start), buf_sizes[cmd]);
  366. return -EINVAL;
  367. }
  368. } else if (cmd != 0x100) {
  369. DRM_ERROR("invalid UVD command %X!\n", cmd);
  370. return -EINVAL;
  371. }
  372. if ((start >> 28) != (end >> 28)) {
  373. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  374. start, end);
  375. return -EINVAL;
  376. }
  377. /* TODO: is this still necessary on NI+ ? */
  378. if ((cmd == 0 || cmd == 0x3) &&
  379. (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
  380. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  381. start, end);
  382. return -EINVAL;
  383. }
  384. if (cmd == 0) {
  385. if (*has_msg_cmd) {
  386. DRM_ERROR("More than one message in a UVD-IB!\n");
  387. return -EINVAL;
  388. }
  389. *has_msg_cmd = true;
  390. r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
  391. if (r)
  392. return r;
  393. } else if (!*has_msg_cmd) {
  394. DRM_ERROR("Message needed before other commands are send!\n");
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
  400. struct radeon_cs_packet *pkt,
  401. int *data0, int *data1,
  402. unsigned buf_sizes[],
  403. bool *has_msg_cmd)
  404. {
  405. int i, r;
  406. p->idx++;
  407. for (i = 0; i <= pkt->count; ++i) {
  408. switch (pkt->reg + i*4) {
  409. case UVD_GPCOM_VCPU_DATA0:
  410. *data0 = p->idx;
  411. break;
  412. case UVD_GPCOM_VCPU_DATA1:
  413. *data1 = p->idx;
  414. break;
  415. case UVD_GPCOM_VCPU_CMD:
  416. r = radeon_uvd_cs_reloc(p, *data0, *data1,
  417. buf_sizes, has_msg_cmd);
  418. if (r)
  419. return r;
  420. break;
  421. case UVD_ENGINE_CNTL:
  422. break;
  423. default:
  424. DRM_ERROR("Invalid reg 0x%X!\n",
  425. pkt->reg + i*4);
  426. return -EINVAL;
  427. }
  428. p->idx++;
  429. }
  430. return 0;
  431. }
  432. int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
  433. {
  434. struct radeon_cs_packet pkt;
  435. int r, data0 = 0, data1 = 0;
  436. /* does the IB has a msg command */
  437. bool has_msg_cmd = false;
  438. /* minimum buffer sizes */
  439. unsigned buf_sizes[] = {
  440. [0x00000000] = 2048,
  441. [0x00000001] = 32 * 1024 * 1024,
  442. [0x00000002] = 2048 * 1152 * 3,
  443. [0x00000003] = 2048,
  444. };
  445. if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
  446. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  447. p->chunks[p->chunk_ib_idx].length_dw);
  448. return -EINVAL;
  449. }
  450. if (p->chunk_relocs_idx == -1) {
  451. DRM_ERROR("No relocation chunk !\n");
  452. return -EINVAL;
  453. }
  454. do {
  455. r = radeon_cs_packet_parse(p, &pkt, p->idx);
  456. if (r)
  457. return r;
  458. switch (pkt.type) {
  459. case RADEON_PACKET_TYPE0:
  460. r = radeon_uvd_cs_reg(p, &pkt, &data0, &data1,
  461. buf_sizes, &has_msg_cmd);
  462. if (r)
  463. return r;
  464. break;
  465. case RADEON_PACKET_TYPE2:
  466. p->idx += pkt.count + 2;
  467. break;
  468. default:
  469. DRM_ERROR("Unknown packet type %d !\n", pkt.type);
  470. return -EINVAL;
  471. }
  472. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  473. if (!has_msg_cmd) {
  474. DRM_ERROR("UVD-IBs need a msg command!\n");
  475. return -EINVAL;
  476. }
  477. return 0;
  478. }
  479. static int radeon_uvd_send_msg(struct radeon_device *rdev,
  480. int ring, struct radeon_bo *bo,
  481. struct radeon_fence **fence)
  482. {
  483. struct ttm_validate_buffer tv;
  484. struct ww_acquire_ctx ticket;
  485. struct list_head head;
  486. struct radeon_ib ib;
  487. uint64_t addr;
  488. int i, r;
  489. memset(&tv, 0, sizeof(tv));
  490. tv.bo = &bo->tbo;
  491. INIT_LIST_HEAD(&head);
  492. list_add(&tv.head, &head);
  493. r = ttm_eu_reserve_buffers(&ticket, &head);
  494. if (r)
  495. return r;
  496. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
  497. radeon_uvd_force_into_uvd_segment(bo);
  498. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  499. if (r)
  500. goto err;
  501. r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
  502. if (r)
  503. goto err;
  504. addr = radeon_bo_gpu_offset(bo);
  505. ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
  506. ib.ptr[1] = addr;
  507. ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
  508. ib.ptr[3] = addr >> 32;
  509. ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
  510. ib.ptr[5] = 0;
  511. for (i = 6; i < 16; ++i)
  512. ib.ptr[i] = PACKET2(0);
  513. ib.length_dw = 16;
  514. r = radeon_ib_schedule(rdev, &ib, NULL);
  515. if (r)
  516. goto err;
  517. ttm_eu_fence_buffer_objects(&ticket, &head, ib.fence);
  518. if (fence)
  519. *fence = radeon_fence_ref(ib.fence);
  520. radeon_ib_free(rdev, &ib);
  521. radeon_bo_unref(&bo);
  522. return 0;
  523. err:
  524. ttm_eu_backoff_reservation(&ticket, &head);
  525. return r;
  526. }
  527. /* multiple fence commands without any stream commands in between can
  528. crash the vcpu so just try to emmit a dummy create/destroy msg to
  529. avoid this */
  530. int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
  531. uint32_t handle, struct radeon_fence **fence)
  532. {
  533. struct radeon_bo *bo;
  534. uint32_t *msg;
  535. int r, i;
  536. r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
  537. RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
  538. if (r)
  539. return r;
  540. r = radeon_bo_reserve(bo, false);
  541. if (r) {
  542. radeon_bo_unref(&bo);
  543. return r;
  544. }
  545. r = radeon_bo_kmap(bo, (void **)&msg);
  546. if (r) {
  547. radeon_bo_unreserve(bo);
  548. radeon_bo_unref(&bo);
  549. return r;
  550. }
  551. /* stitch together an UVD create msg */
  552. msg[0] = cpu_to_le32(0x00000de4);
  553. msg[1] = cpu_to_le32(0x00000000);
  554. msg[2] = cpu_to_le32(handle);
  555. msg[3] = cpu_to_le32(0x00000000);
  556. msg[4] = cpu_to_le32(0x00000000);
  557. msg[5] = cpu_to_le32(0x00000000);
  558. msg[6] = cpu_to_le32(0x00000000);
  559. msg[7] = cpu_to_le32(0x00000780);
  560. msg[8] = cpu_to_le32(0x00000440);
  561. msg[9] = cpu_to_le32(0x00000000);
  562. msg[10] = cpu_to_le32(0x01b37000);
  563. for (i = 11; i < 1024; ++i)
  564. msg[i] = cpu_to_le32(0x0);
  565. radeon_bo_kunmap(bo);
  566. radeon_bo_unreserve(bo);
  567. return radeon_uvd_send_msg(rdev, ring, bo, fence);
  568. }
  569. int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
  570. uint32_t handle, struct radeon_fence **fence)
  571. {
  572. struct radeon_bo *bo;
  573. uint32_t *msg;
  574. int r, i;
  575. r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
  576. RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
  577. if (r)
  578. return r;
  579. r = radeon_bo_reserve(bo, false);
  580. if (r) {
  581. radeon_bo_unref(&bo);
  582. return r;
  583. }
  584. r = radeon_bo_kmap(bo, (void **)&msg);
  585. if (r) {
  586. radeon_bo_unreserve(bo);
  587. radeon_bo_unref(&bo);
  588. return r;
  589. }
  590. /* stitch together an UVD destroy msg */
  591. msg[0] = cpu_to_le32(0x00000de4);
  592. msg[1] = cpu_to_le32(0x00000002);
  593. msg[2] = cpu_to_le32(handle);
  594. msg[3] = cpu_to_le32(0x00000000);
  595. for (i = 4; i < 1024; ++i)
  596. msg[i] = cpu_to_le32(0x0);
  597. radeon_bo_kunmap(bo);
  598. radeon_bo_unreserve(bo);
  599. return radeon_uvd_send_msg(rdev, ring, bo, fence);
  600. }
  601. static void radeon_uvd_idle_work_handler(struct work_struct *work)
  602. {
  603. struct radeon_device *rdev =
  604. container_of(work, struct radeon_device, uvd.idle_work.work);
  605. if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0) {
  606. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  607. mutex_lock(&rdev->pm.mutex);
  608. rdev->pm.dpm.uvd_active = false;
  609. mutex_unlock(&rdev->pm.mutex);
  610. radeon_pm_compute_clocks(rdev);
  611. } else {
  612. radeon_set_uvd_clocks(rdev, 0, 0);
  613. }
  614. } else {
  615. schedule_delayed_work(&rdev->uvd.idle_work,
  616. msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
  617. }
  618. }
  619. void radeon_uvd_note_usage(struct radeon_device *rdev)
  620. {
  621. bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
  622. set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
  623. msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
  624. if (set_clocks) {
  625. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  626. /* XXX pick SD/HD/MVC */
  627. radeon_dpm_enable_power_state(rdev, POWER_STATE_TYPE_INTERNAL_UVD);
  628. } else {
  629. radeon_set_uvd_clocks(rdev, 53300, 40000);
  630. }
  631. }
  632. }
  633. static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
  634. unsigned target_freq,
  635. unsigned pd_min,
  636. unsigned pd_even)
  637. {
  638. unsigned post_div = vco_freq / target_freq;
  639. /* adjust to post divider minimum value */
  640. if (post_div < pd_min)
  641. post_div = pd_min;
  642. /* we alway need a frequency less than or equal the target */
  643. if ((vco_freq / post_div) > target_freq)
  644. post_div += 1;
  645. /* post dividers above a certain value must be even */
  646. if (post_div > pd_even && post_div % 2)
  647. post_div += 1;
  648. return post_div;
  649. }
  650. /**
  651. * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
  652. *
  653. * @rdev: radeon_device pointer
  654. * @vclk: wanted VCLK
  655. * @dclk: wanted DCLK
  656. * @vco_min: minimum VCO frequency
  657. * @vco_max: maximum VCO frequency
  658. * @fb_factor: factor to multiply vco freq with
  659. * @fb_mask: limit and bitmask for feedback divider
  660. * @pd_min: post divider minimum
  661. * @pd_max: post divider maximum
  662. * @pd_even: post divider must be even above this value
  663. * @optimal_fb_div: resulting feedback divider
  664. * @optimal_vclk_div: resulting vclk post divider
  665. * @optimal_dclk_div: resulting dclk post divider
  666. *
  667. * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
  668. * Returns zero on success -EINVAL on error.
  669. */
  670. int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
  671. unsigned vclk, unsigned dclk,
  672. unsigned vco_min, unsigned vco_max,
  673. unsigned fb_factor, unsigned fb_mask,
  674. unsigned pd_min, unsigned pd_max,
  675. unsigned pd_even,
  676. unsigned *optimal_fb_div,
  677. unsigned *optimal_vclk_div,
  678. unsigned *optimal_dclk_div)
  679. {
  680. unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
  681. /* start off with something large */
  682. unsigned optimal_score = ~0;
  683. /* loop through vco from low to high */
  684. vco_min = max(max(vco_min, vclk), dclk);
  685. for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
  686. uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
  687. unsigned vclk_div, dclk_div, score;
  688. do_div(fb_div, ref_freq);
  689. /* fb div out of range ? */
  690. if (fb_div > fb_mask)
  691. break; /* it can oly get worse */
  692. fb_div &= fb_mask;
  693. /* calc vclk divider with current vco freq */
  694. vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
  695. pd_min, pd_even);
  696. if (vclk_div > pd_max)
  697. break; /* vco is too big, it has to stop */
  698. /* calc dclk divider with current vco freq */
  699. dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
  700. pd_min, pd_even);
  701. if (vclk_div > pd_max)
  702. break; /* vco is too big, it has to stop */
  703. /* calc score with current vco freq */
  704. score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
  705. /* determine if this vco setting is better than current optimal settings */
  706. if (score < optimal_score) {
  707. *optimal_fb_div = fb_div;
  708. *optimal_vclk_div = vclk_div;
  709. *optimal_dclk_div = dclk_div;
  710. optimal_score = score;
  711. if (optimal_score == 0)
  712. break; /* it can't get better than this */
  713. }
  714. }
  715. /* did we found a valid setup ? */
  716. if (optimal_score == ~0)
  717. return -EINVAL;
  718. return 0;
  719. }
  720. int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
  721. unsigned cg_upll_func_cntl)
  722. {
  723. unsigned i;
  724. /* make sure UPLL_CTLREQ is deasserted */
  725. WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
  726. mdelay(10);
  727. /* assert UPLL_CTLREQ */
  728. WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
  729. /* wait for CTLACK and CTLACK2 to get asserted */
  730. for (i = 0; i < 100; ++i) {
  731. uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
  732. if ((RREG32(cg_upll_func_cntl) & mask) == mask)
  733. break;
  734. mdelay(10);
  735. }
  736. /* deassert UPLL_CTLREQ */
  737. WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
  738. if (i == 100) {
  739. DRM_ERROR("Timeout setting UVD clocks!\n");
  740. return -ETIMEDOUT;
  741. }
  742. return 0;
  743. }