radeon_uvd.c 23 KB

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