radeon_uvd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright 2011 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Christian König <deathsimple@vodafone.de>
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm.h>
  34. #include "radeon.h"
  35. #include "r600d.h"
  36. /* 1 second timeout */
  37. #define UVD_IDLE_TIMEOUT_MS 1000
  38. /* Firmware Names */
  39. #define FIRMWARE_RV710 "radeon/RV710_uvd.bin"
  40. #define FIRMWARE_CYPRESS "radeon/CYPRESS_uvd.bin"
  41. #define FIRMWARE_SUMO "radeon/SUMO_uvd.bin"
  42. #define FIRMWARE_TAHITI "radeon/TAHITI_uvd.bin"
  43. #define FIRMWARE_BONAIRE "radeon/BONAIRE_uvd.bin"
  44. MODULE_FIRMWARE(FIRMWARE_RV710);
  45. MODULE_FIRMWARE(FIRMWARE_CYPRESS);
  46. MODULE_FIRMWARE(FIRMWARE_SUMO);
  47. MODULE_FIRMWARE(FIRMWARE_TAHITI);
  48. MODULE_FIRMWARE(FIRMWARE_BONAIRE);
  49. static void radeon_uvd_idle_work_handler(struct work_struct *work);
  50. int radeon_uvd_init(struct radeon_device *rdev)
  51. {
  52. unsigned long bo_size;
  53. const char *fw_name;
  54. int i, r;
  55. INIT_DELAYED_WORK(&rdev->uvd.idle_work, radeon_uvd_idle_work_handler);
  56. switch (rdev->family) {
  57. case CHIP_RV710:
  58. case CHIP_RV730:
  59. case CHIP_RV740:
  60. fw_name = FIRMWARE_RV710;
  61. break;
  62. case CHIP_CYPRESS:
  63. case CHIP_HEMLOCK:
  64. case CHIP_JUNIPER:
  65. case CHIP_REDWOOD:
  66. case CHIP_CEDAR:
  67. fw_name = FIRMWARE_CYPRESS;
  68. break;
  69. case CHIP_SUMO:
  70. case CHIP_SUMO2:
  71. case CHIP_PALM:
  72. case CHIP_CAYMAN:
  73. case CHIP_BARTS:
  74. case CHIP_TURKS:
  75. case CHIP_CAICOS:
  76. fw_name = FIRMWARE_SUMO;
  77. break;
  78. case CHIP_TAHITI:
  79. case CHIP_VERDE:
  80. case CHIP_PITCAIRN:
  81. case CHIP_ARUBA:
  82. fw_name = FIRMWARE_TAHITI;
  83. break;
  84. case CHIP_BONAIRE:
  85. case CHIP_KABINI:
  86. case CHIP_KAVERI:
  87. fw_name = FIRMWARE_BONAIRE;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. r = request_firmware(&rdev->uvd_fw, fw_name, rdev->dev);
  93. if (r) {
  94. dev_err(rdev->dev, "radeon_uvd: Can't load firmware \"%s\"\n",
  95. fw_name);
  96. return r;
  97. }
  98. bo_size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size + 8) +
  99. RADEON_UVD_STACK_SIZE + RADEON_UVD_HEAP_SIZE;
  100. r = radeon_bo_create(rdev, bo_size, PAGE_SIZE, true,
  101. RADEON_GEM_DOMAIN_VRAM, NULL, &rdev->uvd.vcpu_bo);
  102. if (r) {
  103. dev_err(rdev->dev, "(%d) failed to allocate UVD bo\n", r);
  104. return r;
  105. }
  106. r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
  107. if (r) {
  108. radeon_bo_unref(&rdev->uvd.vcpu_bo);
  109. dev_err(rdev->dev, "(%d) failed to reserve UVD bo\n", r);
  110. return r;
  111. }
  112. r = radeon_bo_pin(rdev->uvd.vcpu_bo, RADEON_GEM_DOMAIN_VRAM,
  113. &rdev->uvd.gpu_addr);
  114. if (r) {
  115. radeon_bo_unreserve(rdev->uvd.vcpu_bo);
  116. radeon_bo_unref(&rdev->uvd.vcpu_bo);
  117. dev_err(rdev->dev, "(%d) UVD bo pin failed\n", r);
  118. return r;
  119. }
  120. r = radeon_bo_kmap(rdev->uvd.vcpu_bo, &rdev->uvd.cpu_addr);
  121. if (r) {
  122. dev_err(rdev->dev, "(%d) UVD map failed\n", r);
  123. return r;
  124. }
  125. radeon_bo_unreserve(rdev->uvd.vcpu_bo);
  126. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  127. atomic_set(&rdev->uvd.handles[i], 0);
  128. rdev->uvd.filp[i] = NULL;
  129. }
  130. return 0;
  131. }
  132. void radeon_uvd_fini(struct radeon_device *rdev)
  133. {
  134. int r;
  135. if (rdev->uvd.vcpu_bo == NULL)
  136. return;
  137. r = radeon_bo_reserve(rdev->uvd.vcpu_bo, false);
  138. if (!r) {
  139. radeon_bo_kunmap(rdev->uvd.vcpu_bo);
  140. radeon_bo_unpin(rdev->uvd.vcpu_bo);
  141. radeon_bo_unreserve(rdev->uvd.vcpu_bo);
  142. }
  143. radeon_bo_unref(&rdev->uvd.vcpu_bo);
  144. release_firmware(rdev->uvd_fw);
  145. }
  146. int radeon_uvd_suspend(struct radeon_device *rdev)
  147. {
  148. unsigned size;
  149. void *ptr;
  150. int i;
  151. if (rdev->uvd.vcpu_bo == NULL)
  152. return 0;
  153. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
  154. if (atomic_read(&rdev->uvd.handles[i]))
  155. break;
  156. if (i == RADEON_MAX_UVD_HANDLES)
  157. return 0;
  158. size = radeon_bo_size(rdev->uvd.vcpu_bo);
  159. size -= rdev->uvd_fw->size;
  160. ptr = rdev->uvd.cpu_addr;
  161. ptr += rdev->uvd_fw->size;
  162. rdev->uvd.saved_bo = kmalloc(size, GFP_KERNEL);
  163. memcpy(rdev->uvd.saved_bo, ptr, size);
  164. return 0;
  165. }
  166. int radeon_uvd_resume(struct radeon_device *rdev)
  167. {
  168. unsigned size;
  169. void *ptr;
  170. if (rdev->uvd.vcpu_bo == NULL)
  171. return -EINVAL;
  172. memcpy(rdev->uvd.cpu_addr, rdev->uvd_fw->data, rdev->uvd_fw->size);
  173. size = radeon_bo_size(rdev->uvd.vcpu_bo);
  174. size -= rdev->uvd_fw->size;
  175. ptr = rdev->uvd.cpu_addr;
  176. ptr += rdev->uvd_fw->size;
  177. if (rdev->uvd.saved_bo != NULL) {
  178. memcpy(ptr, rdev->uvd.saved_bo, size);
  179. kfree(rdev->uvd.saved_bo);
  180. rdev->uvd.saved_bo = NULL;
  181. } else
  182. memset(ptr, 0, size);
  183. return 0;
  184. }
  185. void radeon_uvd_force_into_uvd_segment(struct radeon_bo *rbo)
  186. {
  187. rbo->placement.fpfn = 0 >> PAGE_SHIFT;
  188. rbo->placement.lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
  189. }
  190. void radeon_uvd_free_handles(struct radeon_device *rdev, struct drm_file *filp)
  191. {
  192. int i, r;
  193. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  194. uint32_t handle = atomic_read(&rdev->uvd.handles[i]);
  195. if (handle != 0 && rdev->uvd.filp[i] == filp) {
  196. struct radeon_fence *fence;
  197. r = radeon_uvd_get_destroy_msg(rdev,
  198. R600_RING_TYPE_UVD_INDEX, handle, &fence);
  199. if (r) {
  200. DRM_ERROR("Error destroying UVD (%d)!\n", r);
  201. continue;
  202. }
  203. radeon_fence_wait(fence, false);
  204. radeon_fence_unref(&fence);
  205. rdev->uvd.filp[i] = NULL;
  206. atomic_set(&rdev->uvd.handles[i], 0);
  207. }
  208. }
  209. }
  210. static int radeon_uvd_cs_msg_decode(uint32_t *msg, unsigned buf_sizes[])
  211. {
  212. unsigned stream_type = msg[4];
  213. unsigned width = msg[6];
  214. unsigned height = msg[7];
  215. unsigned dpb_size = msg[9];
  216. unsigned pitch = msg[28];
  217. unsigned width_in_mb = width / 16;
  218. unsigned height_in_mb = ALIGN(height / 16, 2);
  219. unsigned image_size, tmp, min_dpb_size;
  220. image_size = width * height;
  221. image_size += image_size / 2;
  222. image_size = ALIGN(image_size, 1024);
  223. switch (stream_type) {
  224. case 0: /* H264 */
  225. /* reference picture buffer */
  226. min_dpb_size = image_size * 17;
  227. /* macroblock context buffer */
  228. min_dpb_size += width_in_mb * height_in_mb * 17 * 192;
  229. /* IT surface buffer */
  230. min_dpb_size += width_in_mb * height_in_mb * 32;
  231. break;
  232. case 1: /* VC1 */
  233. /* reference picture buffer */
  234. min_dpb_size = image_size * 3;
  235. /* CONTEXT_BUFFER */
  236. min_dpb_size += width_in_mb * height_in_mb * 128;
  237. /* IT surface buffer */
  238. min_dpb_size += width_in_mb * 64;
  239. /* DB surface buffer */
  240. min_dpb_size += width_in_mb * 128;
  241. /* BP */
  242. tmp = max(width_in_mb, height_in_mb);
  243. min_dpb_size += ALIGN(tmp * 7 * 16, 64);
  244. break;
  245. case 3: /* MPEG2 */
  246. /* reference picture buffer */
  247. min_dpb_size = image_size * 3;
  248. break;
  249. case 4: /* MPEG4 */
  250. /* reference picture buffer */
  251. min_dpb_size = image_size * 3;
  252. /* CM */
  253. min_dpb_size += width_in_mb * height_in_mb * 64;
  254. /* IT surface buffer */
  255. min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
  256. break;
  257. default:
  258. DRM_ERROR("UVD codec not handled %d!\n", stream_type);
  259. return -EINVAL;
  260. }
  261. if (width > pitch) {
  262. DRM_ERROR("Invalid UVD decoding target pitch!\n");
  263. return -EINVAL;
  264. }
  265. if (dpb_size < min_dpb_size) {
  266. DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
  267. dpb_size, min_dpb_size);
  268. return -EINVAL;
  269. }
  270. buf_sizes[0x1] = dpb_size;
  271. buf_sizes[0x2] = image_size;
  272. return 0;
  273. }
  274. static int radeon_uvd_cs_msg(struct radeon_cs_parser *p, struct radeon_bo *bo,
  275. unsigned offset, unsigned buf_sizes[])
  276. {
  277. int32_t *msg, msg_type, handle;
  278. void *ptr;
  279. int i, r;
  280. if (offset & 0x3F) {
  281. DRM_ERROR("UVD messages must be 64 byte aligned!\n");
  282. return -EINVAL;
  283. }
  284. r = radeon_bo_kmap(bo, &ptr);
  285. if (r) {
  286. DRM_ERROR("Failed mapping the UVD message (%d)!\n", r);
  287. return r;
  288. }
  289. msg = ptr + offset;
  290. msg_type = msg[1];
  291. handle = msg[2];
  292. if (handle == 0) {
  293. DRM_ERROR("Invalid UVD handle!\n");
  294. return -EINVAL;
  295. }
  296. if (msg_type == 1) {
  297. /* it's a decode msg, calc buffer sizes */
  298. r = radeon_uvd_cs_msg_decode(msg, buf_sizes);
  299. radeon_bo_kunmap(bo);
  300. if (r)
  301. return r;
  302. } else if (msg_type == 2) {
  303. /* it's a destroy msg, free the handle */
  304. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i)
  305. atomic_cmpxchg(&p->rdev->uvd.handles[i], handle, 0);
  306. radeon_bo_kunmap(bo);
  307. return 0;
  308. } else {
  309. radeon_bo_kunmap(bo);
  310. if (msg_type != 0) {
  311. DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
  312. return -EINVAL;
  313. }
  314. /* it's a create msg, no special handling needed */
  315. }
  316. /* create or decode, validate the handle */
  317. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  318. if (atomic_read(&p->rdev->uvd.handles[i]) == handle)
  319. return 0;
  320. }
  321. /* handle not found try to alloc a new one */
  322. for (i = 0; i < RADEON_MAX_UVD_HANDLES; ++i) {
  323. if (!atomic_cmpxchg(&p->rdev->uvd.handles[i], 0, handle)) {
  324. p->rdev->uvd.filp[i] = p->filp;
  325. return 0;
  326. }
  327. }
  328. DRM_ERROR("No more free UVD handles!\n");
  329. return -EINVAL;
  330. }
  331. static int radeon_uvd_cs_reloc(struct radeon_cs_parser *p,
  332. int data0, int data1,
  333. unsigned buf_sizes[], bool *has_msg_cmd)
  334. {
  335. struct radeon_cs_chunk *relocs_chunk;
  336. struct radeon_cs_reloc *reloc;
  337. unsigned idx, cmd, offset;
  338. uint64_t start, end;
  339. int r;
  340. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  341. offset = radeon_get_ib_value(p, data0);
  342. idx = radeon_get_ib_value(p, data1);
  343. if (idx >= relocs_chunk->length_dw) {
  344. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  345. idx, relocs_chunk->length_dw);
  346. return -EINVAL;
  347. }
  348. reloc = p->relocs_ptr[(idx / 4)];
  349. start = reloc->lobj.gpu_offset;
  350. end = start + radeon_bo_size(reloc->robj);
  351. start += offset;
  352. p->ib.ptr[data0] = start & 0xFFFFFFFF;
  353. p->ib.ptr[data1] = start >> 32;
  354. cmd = radeon_get_ib_value(p, p->idx) >> 1;
  355. if (cmd < 0x4) {
  356. if ((end - start) < buf_sizes[cmd]) {
  357. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  358. (unsigned)(end - start), buf_sizes[cmd]);
  359. return -EINVAL;
  360. }
  361. } else if (cmd != 0x100) {
  362. DRM_ERROR("invalid UVD command %X!\n", cmd);
  363. return -EINVAL;
  364. }
  365. if ((start >> 28) != (end >> 28)) {
  366. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  367. start, end);
  368. return -EINVAL;
  369. }
  370. /* TODO: is this still necessary on NI+ ? */
  371. if ((cmd == 0 || cmd == 0x3) &&
  372. (start >> 28) != (p->rdev->uvd.gpu_addr >> 28)) {
  373. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  374. start, end);
  375. return -EINVAL;
  376. }
  377. if (cmd == 0) {
  378. if (*has_msg_cmd) {
  379. DRM_ERROR("More than one message in a UVD-IB!\n");
  380. return -EINVAL;
  381. }
  382. *has_msg_cmd = true;
  383. r = radeon_uvd_cs_msg(p, reloc->robj, offset, buf_sizes);
  384. if (r)
  385. return r;
  386. } else if (!*has_msg_cmd) {
  387. DRM_ERROR("Message needed before other commands are send!\n");
  388. return -EINVAL;
  389. }
  390. return 0;
  391. }
  392. static int radeon_uvd_cs_reg(struct radeon_cs_parser *p,
  393. struct radeon_cs_packet *pkt,
  394. int *data0, int *data1,
  395. unsigned buf_sizes[],
  396. bool *has_msg_cmd)
  397. {
  398. int i, r;
  399. p->idx++;
  400. for (i = 0; i <= pkt->count; ++i) {
  401. switch (pkt->reg + i*4) {
  402. case UVD_GPCOM_VCPU_DATA0:
  403. *data0 = p->idx;
  404. break;
  405. case UVD_GPCOM_VCPU_DATA1:
  406. *data1 = p->idx;
  407. break;
  408. case UVD_GPCOM_VCPU_CMD:
  409. r = radeon_uvd_cs_reloc(p, *data0, *data1,
  410. buf_sizes, has_msg_cmd);
  411. if (r)
  412. return r;
  413. break;
  414. case UVD_ENGINE_CNTL:
  415. break;
  416. default:
  417. DRM_ERROR("Invalid reg 0x%X!\n",
  418. pkt->reg + i*4);
  419. return -EINVAL;
  420. }
  421. p->idx++;
  422. }
  423. return 0;
  424. }
  425. int radeon_uvd_cs_parse(struct radeon_cs_parser *p)
  426. {
  427. struct radeon_cs_packet pkt;
  428. int r, data0 = 0, data1 = 0;
  429. /* does the IB has a msg command */
  430. bool has_msg_cmd = false;
  431. /* minimum buffer sizes */
  432. unsigned buf_sizes[] = {
  433. [0x00000000] = 2048,
  434. [0x00000001] = 32 * 1024 * 1024,
  435. [0x00000002] = 2048 * 1152 * 3,
  436. [0x00000003] = 2048,
  437. };
  438. if (p->chunks[p->chunk_ib_idx].length_dw % 16) {
  439. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  440. p->chunks[p->chunk_ib_idx].length_dw);
  441. return -EINVAL;
  442. }
  443. if (p->chunk_relocs_idx == -1) {
  444. DRM_ERROR("No relocation chunk !\n");
  445. return -EINVAL;
  446. }
  447. do {
  448. r = radeon_cs_packet_parse(p, &pkt, p->idx);
  449. if (r)
  450. return r;
  451. switch (pkt.type) {
  452. case RADEON_PACKET_TYPE0:
  453. r = radeon_uvd_cs_reg(p, &pkt, &data0, &data1,
  454. buf_sizes, &has_msg_cmd);
  455. if (r)
  456. return r;
  457. break;
  458. case RADEON_PACKET_TYPE2:
  459. p->idx += pkt.count + 2;
  460. break;
  461. default:
  462. DRM_ERROR("Unknown packet type %d !\n", pkt.type);
  463. return -EINVAL;
  464. }
  465. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  466. if (!has_msg_cmd) {
  467. DRM_ERROR("UVD-IBs need a msg command!\n");
  468. return -EINVAL;
  469. }
  470. return 0;
  471. }
  472. static int radeon_uvd_send_msg(struct radeon_device *rdev,
  473. int ring, struct radeon_bo *bo,
  474. struct radeon_fence **fence)
  475. {
  476. struct ttm_validate_buffer tv;
  477. struct ww_acquire_ctx ticket;
  478. struct list_head head;
  479. struct radeon_ib ib;
  480. uint64_t addr;
  481. int i, r;
  482. memset(&tv, 0, sizeof(tv));
  483. tv.bo = &bo->tbo;
  484. INIT_LIST_HEAD(&head);
  485. list_add(&tv.head, &head);
  486. r = ttm_eu_reserve_buffers(&ticket, &head);
  487. if (r)
  488. return r;
  489. radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_VRAM);
  490. radeon_uvd_force_into_uvd_segment(bo);
  491. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  492. if (r)
  493. goto err;
  494. r = radeon_ib_get(rdev, ring, &ib, NULL, 16);
  495. if (r)
  496. goto err;
  497. addr = radeon_bo_gpu_offset(bo);
  498. ib.ptr[0] = PACKET0(UVD_GPCOM_VCPU_DATA0, 0);
  499. ib.ptr[1] = addr;
  500. ib.ptr[2] = PACKET0(UVD_GPCOM_VCPU_DATA1, 0);
  501. ib.ptr[3] = addr >> 32;
  502. ib.ptr[4] = PACKET0(UVD_GPCOM_VCPU_CMD, 0);
  503. ib.ptr[5] = 0;
  504. for (i = 6; i < 16; ++i)
  505. ib.ptr[i] = PACKET2(0);
  506. ib.length_dw = 16;
  507. r = radeon_ib_schedule(rdev, &ib, NULL);
  508. if (r)
  509. goto err;
  510. ttm_eu_fence_buffer_objects(&ticket, &head, ib.fence);
  511. if (fence)
  512. *fence = radeon_fence_ref(ib.fence);
  513. radeon_ib_free(rdev, &ib);
  514. radeon_bo_unref(&bo);
  515. return 0;
  516. err:
  517. ttm_eu_backoff_reservation(&ticket, &head);
  518. return r;
  519. }
  520. /* multiple fence commands without any stream commands in between can
  521. crash the vcpu so just try to emmit a dummy create/destroy msg to
  522. avoid this */
  523. int radeon_uvd_get_create_msg(struct radeon_device *rdev, int ring,
  524. uint32_t handle, struct radeon_fence **fence)
  525. {
  526. struct radeon_bo *bo;
  527. uint32_t *msg;
  528. int r, i;
  529. r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
  530. RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
  531. if (r)
  532. return r;
  533. r = radeon_bo_reserve(bo, false);
  534. if (r) {
  535. radeon_bo_unref(&bo);
  536. return r;
  537. }
  538. r = radeon_bo_kmap(bo, (void **)&msg);
  539. if (r) {
  540. radeon_bo_unreserve(bo);
  541. radeon_bo_unref(&bo);
  542. return r;
  543. }
  544. /* stitch together an UVD create msg */
  545. msg[0] = cpu_to_le32(0x00000de4);
  546. msg[1] = cpu_to_le32(0x00000000);
  547. msg[2] = cpu_to_le32(handle);
  548. msg[3] = cpu_to_le32(0x00000000);
  549. msg[4] = cpu_to_le32(0x00000000);
  550. msg[5] = cpu_to_le32(0x00000000);
  551. msg[6] = cpu_to_le32(0x00000000);
  552. msg[7] = cpu_to_le32(0x00000780);
  553. msg[8] = cpu_to_le32(0x00000440);
  554. msg[9] = cpu_to_le32(0x00000000);
  555. msg[10] = cpu_to_le32(0x01b37000);
  556. for (i = 11; i < 1024; ++i)
  557. msg[i] = cpu_to_le32(0x0);
  558. radeon_bo_kunmap(bo);
  559. radeon_bo_unreserve(bo);
  560. return radeon_uvd_send_msg(rdev, ring, bo, fence);
  561. }
  562. int radeon_uvd_get_destroy_msg(struct radeon_device *rdev, int ring,
  563. uint32_t handle, struct radeon_fence **fence)
  564. {
  565. struct radeon_bo *bo;
  566. uint32_t *msg;
  567. int r, i;
  568. r = radeon_bo_create(rdev, 1024, PAGE_SIZE, true,
  569. RADEON_GEM_DOMAIN_VRAM, NULL, &bo);
  570. if (r)
  571. return r;
  572. r = radeon_bo_reserve(bo, false);
  573. if (r) {
  574. radeon_bo_unref(&bo);
  575. return r;
  576. }
  577. r = radeon_bo_kmap(bo, (void **)&msg);
  578. if (r) {
  579. radeon_bo_unreserve(bo);
  580. radeon_bo_unref(&bo);
  581. return r;
  582. }
  583. /* stitch together an UVD destroy msg */
  584. msg[0] = cpu_to_le32(0x00000de4);
  585. msg[1] = cpu_to_le32(0x00000002);
  586. msg[2] = cpu_to_le32(handle);
  587. msg[3] = cpu_to_le32(0x00000000);
  588. for (i = 4; i < 1024; ++i)
  589. msg[i] = cpu_to_le32(0x0);
  590. radeon_bo_kunmap(bo);
  591. radeon_bo_unreserve(bo);
  592. return radeon_uvd_send_msg(rdev, ring, bo, fence);
  593. }
  594. static void radeon_uvd_idle_work_handler(struct work_struct *work)
  595. {
  596. struct radeon_device *rdev =
  597. container_of(work, struct radeon_device, uvd.idle_work.work);
  598. if (radeon_fence_count_emitted(rdev, R600_RING_TYPE_UVD_INDEX) == 0) {
  599. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  600. mutex_lock(&rdev->pm.mutex);
  601. rdev->pm.dpm.uvd_active = false;
  602. mutex_unlock(&rdev->pm.mutex);
  603. radeon_pm_compute_clocks(rdev);
  604. } else {
  605. radeon_set_uvd_clocks(rdev, 0, 0);
  606. }
  607. } else {
  608. schedule_delayed_work(&rdev->uvd.idle_work,
  609. msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
  610. }
  611. }
  612. void radeon_uvd_note_usage(struct radeon_device *rdev)
  613. {
  614. bool set_clocks = !cancel_delayed_work_sync(&rdev->uvd.idle_work);
  615. set_clocks &= schedule_delayed_work(&rdev->uvd.idle_work,
  616. msecs_to_jiffies(UVD_IDLE_TIMEOUT_MS));
  617. if (set_clocks) {
  618. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  619. /* XXX pick SD/HD/MVC */
  620. radeon_dpm_enable_power_state(rdev, POWER_STATE_TYPE_INTERNAL_UVD);
  621. } else {
  622. radeon_set_uvd_clocks(rdev, 53300, 40000);
  623. }
  624. }
  625. }
  626. static unsigned radeon_uvd_calc_upll_post_div(unsigned vco_freq,
  627. unsigned target_freq,
  628. unsigned pd_min,
  629. unsigned pd_even)
  630. {
  631. unsigned post_div = vco_freq / target_freq;
  632. /* adjust to post divider minimum value */
  633. if (post_div < pd_min)
  634. post_div = pd_min;
  635. /* we alway need a frequency less than or equal the target */
  636. if ((vco_freq / post_div) > target_freq)
  637. post_div += 1;
  638. /* post dividers above a certain value must be even */
  639. if (post_div > pd_even && post_div % 2)
  640. post_div += 1;
  641. return post_div;
  642. }
  643. /**
  644. * radeon_uvd_calc_upll_dividers - calc UPLL clock dividers
  645. *
  646. * @rdev: radeon_device pointer
  647. * @vclk: wanted VCLK
  648. * @dclk: wanted DCLK
  649. * @vco_min: minimum VCO frequency
  650. * @vco_max: maximum VCO frequency
  651. * @fb_factor: factor to multiply vco freq with
  652. * @fb_mask: limit and bitmask for feedback divider
  653. * @pd_min: post divider minimum
  654. * @pd_max: post divider maximum
  655. * @pd_even: post divider must be even above this value
  656. * @optimal_fb_div: resulting feedback divider
  657. * @optimal_vclk_div: resulting vclk post divider
  658. * @optimal_dclk_div: resulting dclk post divider
  659. *
  660. * Calculate dividers for UVDs UPLL (R6xx-SI, except APUs).
  661. * Returns zero on success -EINVAL on error.
  662. */
  663. int radeon_uvd_calc_upll_dividers(struct radeon_device *rdev,
  664. unsigned vclk, unsigned dclk,
  665. unsigned vco_min, unsigned vco_max,
  666. unsigned fb_factor, unsigned fb_mask,
  667. unsigned pd_min, unsigned pd_max,
  668. unsigned pd_even,
  669. unsigned *optimal_fb_div,
  670. unsigned *optimal_vclk_div,
  671. unsigned *optimal_dclk_div)
  672. {
  673. unsigned vco_freq, ref_freq = rdev->clock.spll.reference_freq;
  674. /* start off with something large */
  675. unsigned optimal_score = ~0;
  676. /* loop through vco from low to high */
  677. vco_min = max(max(vco_min, vclk), dclk);
  678. for (vco_freq = vco_min; vco_freq <= vco_max; vco_freq += 100) {
  679. uint64_t fb_div = (uint64_t)vco_freq * fb_factor;
  680. unsigned vclk_div, dclk_div, score;
  681. do_div(fb_div, ref_freq);
  682. /* fb div out of range ? */
  683. if (fb_div > fb_mask)
  684. break; /* it can oly get worse */
  685. fb_div &= fb_mask;
  686. /* calc vclk divider with current vco freq */
  687. vclk_div = radeon_uvd_calc_upll_post_div(vco_freq, vclk,
  688. pd_min, pd_even);
  689. if (vclk_div > pd_max)
  690. break; /* vco is too big, it has to stop */
  691. /* calc dclk divider with current vco freq */
  692. dclk_div = radeon_uvd_calc_upll_post_div(vco_freq, dclk,
  693. pd_min, pd_even);
  694. if (vclk_div > pd_max)
  695. break; /* vco is too big, it has to stop */
  696. /* calc score with current vco freq */
  697. score = vclk - (vco_freq / vclk_div) + dclk - (vco_freq / dclk_div);
  698. /* determine if this vco setting is better than current optimal settings */
  699. if (score < optimal_score) {
  700. *optimal_fb_div = fb_div;
  701. *optimal_vclk_div = vclk_div;
  702. *optimal_dclk_div = dclk_div;
  703. optimal_score = score;
  704. if (optimal_score == 0)
  705. break; /* it can't get better than this */
  706. }
  707. }
  708. /* did we found a valid setup ? */
  709. if (optimal_score == ~0)
  710. return -EINVAL;
  711. return 0;
  712. }
  713. int radeon_uvd_send_upll_ctlreq(struct radeon_device *rdev,
  714. unsigned cg_upll_func_cntl)
  715. {
  716. unsigned i;
  717. /* make sure UPLL_CTLREQ is deasserted */
  718. WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
  719. mdelay(10);
  720. /* assert UPLL_CTLREQ */
  721. WREG32_P(cg_upll_func_cntl, UPLL_CTLREQ_MASK, ~UPLL_CTLREQ_MASK);
  722. /* wait for CTLACK and CTLACK2 to get asserted */
  723. for (i = 0; i < 100; ++i) {
  724. uint32_t mask = UPLL_CTLACK_MASK | UPLL_CTLACK2_MASK;
  725. if ((RREG32(cg_upll_func_cntl) & mask) == mask)
  726. break;
  727. mdelay(10);
  728. }
  729. /* deassert UPLL_CTLREQ */
  730. WREG32_P(cg_upll_func_cntl, 0, ~UPLL_CTLREQ_MASK);
  731. if (i == 100) {
  732. DRM_ERROR("Timeout setting UVD clocks!\n");
  733. return -ETIMEDOUT;
  734. }
  735. return 0;
  736. }