radeon_uvd.c 22 KB

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