radeon_uvd.c 24 KB

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