radeon_uvd.c 25 KB

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