overlay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright 2013 Ilia Mirkin
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  19. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Implementation based on the pre-KMS implementation in xf86-video-nouveau,
  23. * written by Arthur Huillet.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/drm_crtc.h>
  27. #include <drm/drm_fourcc.h>
  28. #include "nouveau_drm.h"
  29. #include "nouveau_bo.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_display.h"
  32. #include "nvreg.h"
  33. struct nouveau_plane {
  34. struct drm_plane base;
  35. bool flip;
  36. struct nouveau_bo *cur;
  37. struct {
  38. struct drm_property *colorkey;
  39. struct drm_property *contrast;
  40. struct drm_property *brightness;
  41. struct drm_property *hue;
  42. struct drm_property *saturation;
  43. struct drm_property *iturbt_709;
  44. } props;
  45. int colorkey;
  46. int contrast;
  47. int brightness;
  48. int hue;
  49. int saturation;
  50. int iturbt_709;
  51. void (*set_params)(struct nouveau_plane *);
  52. };
  53. static uint32_t formats[] = {
  54. DRM_FORMAT_YUYV,
  55. DRM_FORMAT_UYVY,
  56. DRM_FORMAT_NV12,
  57. };
  58. /* Sine can be approximated with
  59. * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
  60. * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
  61. * Note that this only works for the range [0, 180].
  62. * Also note that sin(x) == -sin(x - 180)
  63. */
  64. static inline int
  65. sin_mul(int degrees, int factor)
  66. {
  67. if (degrees > 180) {
  68. degrees -= 180;
  69. factor *= -1;
  70. }
  71. return factor * 4 * degrees * (180 - degrees) /
  72. (40500 - degrees * (180 - degrees));
  73. }
  74. /* cos(x) = sin(x + 90) */
  75. static inline int
  76. cos_mul(int degrees, int factor)
  77. {
  78. return sin_mul((degrees + 90) % 360, factor);
  79. }
  80. static int
  81. nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  82. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  83. unsigned int crtc_w, unsigned int crtc_h,
  84. uint32_t src_x, uint32_t src_y,
  85. uint32_t src_w, uint32_t src_h)
  86. {
  87. struct nvif_device *dev = &nouveau_drm(plane->dev)->device;
  88. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  89. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  90. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  91. struct nouveau_bo *cur = nv_plane->cur;
  92. bool flip = nv_plane->flip;
  93. int soff = NV_PCRTC0_SIZE * nv_crtc->index;
  94. int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
  95. int format, ret;
  96. /* Source parameters given in 16.16 fixed point, ignore fractional. */
  97. src_x >>= 16;
  98. src_y >>= 16;
  99. src_w >>= 16;
  100. src_h >>= 16;
  101. format = ALIGN(src_w * 4, 0x100);
  102. if (format > 0xffff)
  103. return -ERANGE;
  104. if (dev->info.chipset >= 0x30) {
  105. if (crtc_w < (src_w >> 1) || crtc_h < (src_h >> 1))
  106. return -ERANGE;
  107. } else {
  108. if (crtc_w < (src_w >> 3) || crtc_h < (src_h >> 3))
  109. return -ERANGE;
  110. }
  111. ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM);
  112. if (ret)
  113. return ret;
  114. nv_plane->cur = nv_fb->nvbo;
  115. nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
  116. nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
  117. nvif_wr32(dev, NV_PVIDEO_BASE(flip), 0);
  118. nvif_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nv_fb->nvbo->bo.offset);
  119. nvif_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
  120. nvif_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
  121. nvif_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
  122. nvif_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
  123. nvif_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
  124. nvif_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
  125. if (fb->pixel_format != DRM_FORMAT_UYVY)
  126. format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
  127. if (fb->pixel_format == DRM_FORMAT_NV12)
  128. format |= NV_PVIDEO_FORMAT_PLANAR;
  129. if (nv_plane->iturbt_709)
  130. format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
  131. if (nv_plane->colorkey & (1 << 24))
  132. format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
  133. if (fb->pixel_format == DRM_FORMAT_NV12) {
  134. nvif_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
  135. nvif_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
  136. nv_fb->nvbo->bo.offset + fb->offsets[1]);
  137. }
  138. nvif_wr32(dev, NV_PVIDEO_FORMAT(flip), format);
  139. nvif_wr32(dev, NV_PVIDEO_STOP, 0);
  140. /* TODO: wait for vblank? */
  141. nvif_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
  142. nv_plane->flip = !flip;
  143. if (cur)
  144. nouveau_bo_unpin(cur);
  145. return 0;
  146. }
  147. static int
  148. nv10_disable_plane(struct drm_plane *plane)
  149. {
  150. struct nvif_device *dev = &nouveau_drm(plane->dev)->device;
  151. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  152. nvif_wr32(dev, NV_PVIDEO_STOP, 1);
  153. if (nv_plane->cur) {
  154. nouveau_bo_unpin(nv_plane->cur);
  155. nv_plane->cur = NULL;
  156. }
  157. return 0;
  158. }
  159. static void
  160. nv_destroy_plane(struct drm_plane *plane)
  161. {
  162. plane->funcs->disable_plane(plane);
  163. drm_plane_cleanup(plane);
  164. kfree(plane);
  165. }
  166. static void
  167. nv10_set_params(struct nouveau_plane *plane)
  168. {
  169. struct nvif_device *dev = &nouveau_drm(plane->base.dev)->device;
  170. u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
  171. u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
  172. (cos_mul(plane->hue, plane->saturation) & 0xffff);
  173. u32 format = 0;
  174. nvif_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
  175. nvif_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
  176. nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
  177. nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
  178. nvif_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
  179. if (plane->cur) {
  180. if (plane->iturbt_709)
  181. format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
  182. if (plane->colorkey & (1 << 24))
  183. format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
  184. nvif_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
  185. NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
  186. NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
  187. format);
  188. }
  189. }
  190. static int
  191. nv_set_property(struct drm_plane *plane,
  192. struct drm_property *property,
  193. uint64_t value)
  194. {
  195. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  196. if (property == nv_plane->props.colorkey)
  197. nv_plane->colorkey = value;
  198. else if (property == nv_plane->props.contrast)
  199. nv_plane->contrast = value;
  200. else if (property == nv_plane->props.brightness)
  201. nv_plane->brightness = value;
  202. else if (property == nv_plane->props.hue)
  203. nv_plane->hue = value;
  204. else if (property == nv_plane->props.saturation)
  205. nv_plane->saturation = value;
  206. else if (property == nv_plane->props.iturbt_709)
  207. nv_plane->iturbt_709 = value;
  208. else
  209. return -EINVAL;
  210. if (nv_plane->set_params)
  211. nv_plane->set_params(nv_plane);
  212. return 0;
  213. }
  214. static const struct drm_plane_funcs nv10_plane_funcs = {
  215. .update_plane = nv10_update_plane,
  216. .disable_plane = nv10_disable_plane,
  217. .set_property = nv_set_property,
  218. .destroy = nv_destroy_plane,
  219. };
  220. static void
  221. nv10_overlay_init(struct drm_device *device)
  222. {
  223. struct nouveau_drm *drm = nouveau_drm(device);
  224. struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
  225. int num_formats = ARRAY_SIZE(formats);
  226. int ret;
  227. if (!plane)
  228. return;
  229. switch (drm->device.info.chipset) {
  230. case 0x10:
  231. case 0x11:
  232. case 0x15:
  233. case 0x1a:
  234. case 0x20:
  235. num_formats = 2;
  236. break;
  237. }
  238. ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
  239. &nv10_plane_funcs,
  240. formats, num_formats, false);
  241. if (ret)
  242. goto err;
  243. /* Set up the plane properties */
  244. plane->props.colorkey = drm_property_create_range(
  245. device, 0, "colorkey", 0, 0x01ffffff);
  246. plane->props.contrast = drm_property_create_range(
  247. device, 0, "contrast", 0, 8192 - 1);
  248. plane->props.brightness = drm_property_create_range(
  249. device, 0, "brightness", 0, 1024);
  250. plane->props.hue = drm_property_create_range(
  251. device, 0, "hue", 0, 359);
  252. plane->props.saturation = drm_property_create_range(
  253. device, 0, "saturation", 0, 8192 - 1);
  254. plane->props.iturbt_709 = drm_property_create_range(
  255. device, 0, "iturbt_709", 0, 1);
  256. if (!plane->props.colorkey ||
  257. !plane->props.contrast ||
  258. !plane->props.brightness ||
  259. !plane->props.hue ||
  260. !plane->props.saturation ||
  261. !plane->props.iturbt_709)
  262. goto cleanup;
  263. plane->colorkey = 0;
  264. drm_object_attach_property(&plane->base.base,
  265. plane->props.colorkey, plane->colorkey);
  266. plane->contrast = 0x1000;
  267. drm_object_attach_property(&plane->base.base,
  268. plane->props.contrast, plane->contrast);
  269. plane->brightness = 512;
  270. drm_object_attach_property(&plane->base.base,
  271. plane->props.brightness, plane->brightness);
  272. plane->hue = 0;
  273. drm_object_attach_property(&plane->base.base,
  274. plane->props.hue, plane->hue);
  275. plane->saturation = 0x1000;
  276. drm_object_attach_property(&plane->base.base,
  277. plane->props.saturation, plane->saturation);
  278. plane->iturbt_709 = 0;
  279. drm_object_attach_property(&plane->base.base,
  280. plane->props.iturbt_709, plane->iturbt_709);
  281. plane->set_params = nv10_set_params;
  282. nv10_set_params(plane);
  283. nv10_disable_plane(&plane->base);
  284. return;
  285. cleanup:
  286. drm_plane_cleanup(&plane->base);
  287. err:
  288. kfree(plane);
  289. NV_ERROR(drm, "Failed to create plane\n");
  290. }
  291. static int
  292. nv04_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  293. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  294. unsigned int crtc_w, unsigned int crtc_h,
  295. uint32_t src_x, uint32_t src_y,
  296. uint32_t src_w, uint32_t src_h)
  297. {
  298. struct nvif_device *dev = &nouveau_drm(plane->dev)->device;
  299. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  300. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  301. struct nouveau_bo *cur = nv_plane->cur;
  302. uint32_t overlay = 1;
  303. int brightness = (nv_plane->brightness - 512) * 62 / 512;
  304. int pitch, ret, i;
  305. /* Source parameters given in 16.16 fixed point, ignore fractional. */
  306. src_x >>= 16;
  307. src_y >>= 16;
  308. src_w >>= 16;
  309. src_h >>= 16;
  310. pitch = ALIGN(src_w * 4, 0x100);
  311. if (pitch > 0xffff)
  312. return -ERANGE;
  313. /* TODO: Compute an offset? Not sure how to do this for YUYV. */
  314. if (src_x != 0 || src_y != 0)
  315. return -ERANGE;
  316. if (crtc_w < src_w || crtc_h < src_h)
  317. return -ERANGE;
  318. ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM);
  319. if (ret)
  320. return ret;
  321. nv_plane->cur = nv_fb->nvbo;
  322. nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
  323. nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
  324. nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
  325. for (i = 0; i < 2; i++) {
  326. nvif_wr32(dev, NV_PVIDEO_BUFF0_START_ADDRESS + 4 * i,
  327. nv_fb->nvbo->bo.offset);
  328. nvif_wr32(dev, NV_PVIDEO_BUFF0_PITCH_LENGTH + 4 * i, pitch);
  329. nvif_wr32(dev, NV_PVIDEO_BUFF0_OFFSET + 4 * i, 0);
  330. }
  331. nvif_wr32(dev, NV_PVIDEO_WINDOW_START, crtc_y << 16 | crtc_x);
  332. nvif_wr32(dev, NV_PVIDEO_WINDOW_SIZE, crtc_h << 16 | crtc_w);
  333. nvif_wr32(dev, NV_PVIDEO_STEP_SIZE,
  334. (uint32_t)(((src_h - 1) << 11) / (crtc_h - 1)) << 16 | (uint32_t)(((src_w - 1) << 11) / (crtc_w - 1)));
  335. /* It should be possible to convert hue/contrast to this */
  336. nvif_wr32(dev, NV_PVIDEO_RED_CSC_OFFSET, 0x69 - brightness);
  337. nvif_wr32(dev, NV_PVIDEO_GREEN_CSC_OFFSET, 0x3e + brightness);
  338. nvif_wr32(dev, NV_PVIDEO_BLUE_CSC_OFFSET, 0x89 - brightness);
  339. nvif_wr32(dev, NV_PVIDEO_CSC_ADJUST, 0);
  340. nvif_wr32(dev, NV_PVIDEO_CONTROL_Y, 0x001); /* (BLUR_ON, LINE_HALF) */
  341. nvif_wr32(dev, NV_PVIDEO_CONTROL_X, 0x111); /* (WEIGHT_HEAVY, SHARPENING_ON, SMOOTHING_ON) */
  342. nvif_wr32(dev, NV_PVIDEO_FIFO_BURST_LENGTH, 0x03);
  343. nvif_wr32(dev, NV_PVIDEO_FIFO_THRES_SIZE, 0x38);
  344. nvif_wr32(dev, NV_PVIDEO_KEY, nv_plane->colorkey);
  345. if (nv_plane->colorkey & (1 << 24))
  346. overlay |= 0x10;
  347. if (fb->pixel_format == DRM_FORMAT_YUYV)
  348. overlay |= 0x100;
  349. nvif_wr32(dev, NV_PVIDEO_OVERLAY, overlay);
  350. nvif_wr32(dev, NV_PVIDEO_SU_STATE, nvif_rd32(dev, NV_PVIDEO_SU_STATE) ^ (1 << 16));
  351. if (cur)
  352. nouveau_bo_unpin(cur);
  353. return 0;
  354. }
  355. static int
  356. nv04_disable_plane(struct drm_plane *plane)
  357. {
  358. struct nvif_device *dev = &nouveau_drm(plane->dev)->device;
  359. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  360. nvif_mask(dev, NV_PVIDEO_OVERLAY, 1, 0);
  361. nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
  362. nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
  363. nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
  364. if (nv_plane->cur) {
  365. nouveau_bo_unpin(nv_plane->cur);
  366. nv_plane->cur = NULL;
  367. }
  368. return 0;
  369. }
  370. static const struct drm_plane_funcs nv04_plane_funcs = {
  371. .update_plane = nv04_update_plane,
  372. .disable_plane = nv04_disable_plane,
  373. .set_property = nv_set_property,
  374. .destroy = nv_destroy_plane,
  375. };
  376. static void
  377. nv04_overlay_init(struct drm_device *device)
  378. {
  379. struct nouveau_drm *drm = nouveau_drm(device);
  380. struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
  381. int ret;
  382. if (!plane)
  383. return;
  384. ret = drm_plane_init(device, &plane->base, 1 /* single crtc */,
  385. &nv04_plane_funcs,
  386. formats, 2, false);
  387. if (ret)
  388. goto err;
  389. /* Set up the plane properties */
  390. plane->props.colorkey = drm_property_create_range(
  391. device, 0, "colorkey", 0, 0x01ffffff);
  392. plane->props.brightness = drm_property_create_range(
  393. device, 0, "brightness", 0, 1024);
  394. if (!plane->props.colorkey ||
  395. !plane->props.brightness)
  396. goto cleanup;
  397. plane->colorkey = 0;
  398. drm_object_attach_property(&plane->base.base,
  399. plane->props.colorkey, plane->colorkey);
  400. plane->brightness = 512;
  401. drm_object_attach_property(&plane->base.base,
  402. plane->props.brightness, plane->brightness);
  403. nv04_disable_plane(&plane->base);
  404. return;
  405. cleanup:
  406. drm_plane_cleanup(&plane->base);
  407. err:
  408. kfree(plane);
  409. NV_ERROR(drm, "Failed to create plane\n");
  410. }
  411. void
  412. nouveau_overlay_init(struct drm_device *device)
  413. {
  414. struct nvif_device *dev = &nouveau_drm(device)->device;
  415. if (dev->info.chipset < 0x10)
  416. nv04_overlay_init(device);
  417. else if (dev->info.chipset <= 0x40)
  418. nv10_overlay_init(device);
  419. }