overlay.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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_drv.h"
  29. #include "nouveau_bo.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_display.h"
  32. #include "nvreg.h"
  33. #include "disp.h"
  34. struct nouveau_plane {
  35. struct drm_plane base;
  36. bool flip;
  37. struct nouveau_bo *cur;
  38. struct {
  39. struct drm_property *colorkey;
  40. struct drm_property *contrast;
  41. struct drm_property *brightness;
  42. struct drm_property *hue;
  43. struct drm_property *saturation;
  44. } props;
  45. int colorkey;
  46. int contrast;
  47. int brightness;
  48. int hue;
  49. int saturation;
  50. enum drm_color_encoding color_encoding;
  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. DRM_FORMAT_NV21,
  58. };
  59. /* Sine can be approximated with
  60. * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
  61. * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
  62. * Note that this only works for the range [0, 180].
  63. * Also note that sin(x) == -sin(x - 180)
  64. */
  65. static inline int
  66. sin_mul(int degrees, int factor)
  67. {
  68. if (degrees > 180) {
  69. degrees -= 180;
  70. factor *= -1;
  71. }
  72. return factor * 4 * degrees * (180 - degrees) /
  73. (40500 - degrees * (180 - degrees));
  74. }
  75. /* cos(x) = sin(x + 90) */
  76. static inline int
  77. cos_mul(int degrees, int factor)
  78. {
  79. return sin_mul((degrees + 90) % 360, factor);
  80. }
  81. static int
  82. verify_scaling(const struct drm_framebuffer *fb, uint8_t shift,
  83. uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h,
  84. uint32_t crtc_w, uint32_t crtc_h)
  85. {
  86. if (crtc_w < (src_w >> shift) || crtc_h < (src_h >> shift)) {
  87. DRM_DEBUG_KMS("Unsuitable framebuffer scaling: %dx%d -> %dx%d\n",
  88. src_w, src_h, crtc_w, crtc_h);
  89. return -ERANGE;
  90. }
  91. if (src_x != 0 || src_y != 0) {
  92. DRM_DEBUG_KMS("Unsuitable framebuffer offset: %d,%d\n",
  93. src_x, src_y);
  94. return -ERANGE;
  95. }
  96. return 0;
  97. }
  98. static int
  99. nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  100. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  101. unsigned int crtc_w, unsigned int crtc_h,
  102. uint32_t src_x, uint32_t src_y,
  103. uint32_t src_w, uint32_t src_h,
  104. struct drm_modeset_acquire_ctx *ctx)
  105. {
  106. struct nouveau_drm *drm = nouveau_drm(plane->dev);
  107. struct nvif_object *dev = &drm->client.device.object;
  108. struct nouveau_plane *nv_plane =
  109. container_of(plane, struct nouveau_plane, base);
  110. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  111. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  112. struct nouveau_bo *cur = nv_plane->cur;
  113. bool flip = nv_plane->flip;
  114. int soff = NV_PCRTC0_SIZE * nv_crtc->index;
  115. int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
  116. unsigned shift = drm->client.device.info.chipset >= 0x30 ? 1 : 3;
  117. unsigned format = 0;
  118. int ret;
  119. /* Source parameters given in 16.16 fixed point, ignore fractional. */
  120. src_x >>= 16;
  121. src_y >>= 16;
  122. src_w >>= 16;
  123. src_h >>= 16;
  124. ret = verify_scaling(fb, shift, 0, 0, src_w, src_h, crtc_w, crtc_h);
  125. if (ret)
  126. return ret;
  127. ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM, false);
  128. if (ret)
  129. return ret;
  130. nv_plane->cur = nv_fb->nvbo;
  131. nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
  132. nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
  133. nvif_wr32(dev, NV_PVIDEO_BASE(flip), 0);
  134. nvif_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nv_fb->nvbo->bo.offset);
  135. nvif_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
  136. nvif_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
  137. nvif_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
  138. nvif_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
  139. nvif_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
  140. nvif_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
  141. if (fb->format->format == DRM_FORMAT_YUYV ||
  142. fb->format->format == DRM_FORMAT_NV12)
  143. format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
  144. if (fb->format->format == DRM_FORMAT_NV12 ||
  145. fb->format->format == DRM_FORMAT_NV21)
  146. format |= NV_PVIDEO_FORMAT_PLANAR;
  147. if (nv_plane->color_encoding == DRM_COLOR_YCBCR_BT709)
  148. format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
  149. if (nv_plane->colorkey & (1 << 24))
  150. format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
  151. if (format & NV_PVIDEO_FORMAT_PLANAR) {
  152. nvif_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
  153. nvif_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
  154. nv_fb->nvbo->bo.offset + fb->offsets[1]);
  155. }
  156. nvif_wr32(dev, NV_PVIDEO_FORMAT(flip), format | fb->pitches[0]);
  157. nvif_wr32(dev, NV_PVIDEO_STOP, 0);
  158. /* TODO: wait for vblank? */
  159. nvif_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
  160. nv_plane->flip = !flip;
  161. if (cur)
  162. nouveau_bo_unpin(cur);
  163. return 0;
  164. }
  165. static int
  166. nv10_disable_plane(struct drm_plane *plane,
  167. struct drm_modeset_acquire_ctx *ctx)
  168. {
  169. struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
  170. struct nouveau_plane *nv_plane =
  171. container_of(plane, struct nouveau_plane, base);
  172. nvif_wr32(dev, NV_PVIDEO_STOP, 1);
  173. if (nv_plane->cur) {
  174. nouveau_bo_unpin(nv_plane->cur);
  175. nv_plane->cur = NULL;
  176. }
  177. return 0;
  178. }
  179. static void
  180. nv_destroy_plane(struct drm_plane *plane)
  181. {
  182. drm_plane_force_disable(plane);
  183. drm_plane_cleanup(plane);
  184. kfree(plane);
  185. }
  186. static void
  187. nv10_set_params(struct nouveau_plane *plane)
  188. {
  189. struct nvif_object *dev = &nouveau_drm(plane->base.dev)->client.device.object;
  190. u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
  191. u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
  192. (cos_mul(plane->hue, plane->saturation) & 0xffff);
  193. u32 format = 0;
  194. nvif_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
  195. nvif_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
  196. nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
  197. nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
  198. nvif_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
  199. if (plane->cur) {
  200. if (plane->color_encoding == DRM_COLOR_YCBCR_BT709)
  201. format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
  202. if (plane->colorkey & (1 << 24))
  203. format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
  204. nvif_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
  205. NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
  206. NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
  207. format);
  208. }
  209. }
  210. static int
  211. nv_set_property(struct drm_plane *plane,
  212. struct drm_property *property,
  213. uint64_t value)
  214. {
  215. struct nouveau_plane *nv_plane =
  216. container_of(plane, struct nouveau_plane, base);
  217. if (property == nv_plane->props.colorkey)
  218. nv_plane->colorkey = value;
  219. else if (property == nv_plane->props.contrast)
  220. nv_plane->contrast = value;
  221. else if (property == nv_plane->props.brightness)
  222. nv_plane->brightness = value;
  223. else if (property == nv_plane->props.hue)
  224. nv_plane->hue = value;
  225. else if (property == nv_plane->props.saturation)
  226. nv_plane->saturation = value;
  227. else if (property == nv_plane->base.color_encoding_property)
  228. nv_plane->color_encoding = value;
  229. else
  230. return -EINVAL;
  231. if (nv_plane->set_params)
  232. nv_plane->set_params(nv_plane);
  233. return 0;
  234. }
  235. static const struct drm_plane_funcs nv10_plane_funcs = {
  236. .update_plane = nv10_update_plane,
  237. .disable_plane = nv10_disable_plane,
  238. .set_property = nv_set_property,
  239. .destroy = nv_destroy_plane,
  240. };
  241. static void
  242. nv10_overlay_init(struct drm_device *device)
  243. {
  244. struct nouveau_drm *drm = nouveau_drm(device);
  245. struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
  246. unsigned int num_formats = ARRAY_SIZE(formats);
  247. int ret;
  248. if (!plane)
  249. return;
  250. switch (drm->client.device.info.chipset) {
  251. case 0x10:
  252. case 0x11:
  253. case 0x15:
  254. case 0x1a:
  255. case 0x20:
  256. num_formats = 2;
  257. break;
  258. }
  259. ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
  260. &nv10_plane_funcs,
  261. formats, num_formats, false);
  262. if (ret)
  263. goto err;
  264. /* Set up the plane properties */
  265. plane->props.colorkey = drm_property_create_range(
  266. device, 0, "colorkey", 0, 0x01ffffff);
  267. plane->props.contrast = drm_property_create_range(
  268. device, 0, "contrast", 0, 8192 - 1);
  269. plane->props.brightness = drm_property_create_range(
  270. device, 0, "brightness", 0, 1024);
  271. plane->props.hue = drm_property_create_range(
  272. device, 0, "hue", 0, 359);
  273. plane->props.saturation = drm_property_create_range(
  274. device, 0, "saturation", 0, 8192 - 1);
  275. if (!plane->props.colorkey ||
  276. !plane->props.contrast ||
  277. !plane->props.brightness ||
  278. !plane->props.hue ||
  279. !plane->props.saturation)
  280. goto cleanup;
  281. plane->colorkey = 0;
  282. drm_object_attach_property(&plane->base.base,
  283. plane->props.colorkey, plane->colorkey);
  284. plane->contrast = 0x1000;
  285. drm_object_attach_property(&plane->base.base,
  286. plane->props.contrast, plane->contrast);
  287. plane->brightness = 512;
  288. drm_object_attach_property(&plane->base.base,
  289. plane->props.brightness, plane->brightness);
  290. plane->hue = 0;
  291. drm_object_attach_property(&plane->base.base,
  292. plane->props.hue, plane->hue);
  293. plane->saturation = 0x1000;
  294. drm_object_attach_property(&plane->base.base,
  295. plane->props.saturation, plane->saturation);
  296. plane->color_encoding = DRM_COLOR_YCBCR_BT601;
  297. drm_plane_create_color_properties(&plane->base,
  298. BIT(DRM_COLOR_YCBCR_BT601) |
  299. BIT(DRM_COLOR_YCBCR_BT709),
  300. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
  301. DRM_COLOR_YCBCR_BT601,
  302. DRM_COLOR_YCBCR_LIMITED_RANGE);
  303. plane->set_params = nv10_set_params;
  304. nv10_set_params(plane);
  305. drm_plane_force_disable(&plane->base);
  306. return;
  307. cleanup:
  308. drm_plane_cleanup(&plane->base);
  309. err:
  310. kfree(plane);
  311. NV_ERROR(drm, "Failed to create plane\n");
  312. }
  313. static int
  314. nv04_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  315. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  316. unsigned int crtc_w, unsigned int crtc_h,
  317. uint32_t src_x, uint32_t src_y,
  318. uint32_t src_w, uint32_t src_h,
  319. struct drm_modeset_acquire_ctx *ctx)
  320. {
  321. struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
  322. struct nouveau_plane *nv_plane =
  323. container_of(plane, struct nouveau_plane, base);
  324. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  325. struct nouveau_bo *cur = nv_plane->cur;
  326. uint32_t overlay = 1;
  327. int brightness = (nv_plane->brightness - 512) * 62 / 512;
  328. int ret, i;
  329. /* Source parameters given in 16.16 fixed point, ignore fractional. */
  330. src_x >>= 16;
  331. src_y >>= 16;
  332. src_w >>= 16;
  333. src_h >>= 16;
  334. ret = verify_scaling(fb, 0, src_x, src_y, src_w, src_h, crtc_w, crtc_h);
  335. if (ret)
  336. return ret;
  337. ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM, false);
  338. if (ret)
  339. return ret;
  340. nv_plane->cur = nv_fb->nvbo;
  341. nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
  342. nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
  343. nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
  344. for (i = 0; i < 2; i++) {
  345. nvif_wr32(dev, NV_PVIDEO_BUFF0_START_ADDRESS + 4 * i,
  346. nv_fb->nvbo->bo.offset);
  347. nvif_wr32(dev, NV_PVIDEO_BUFF0_PITCH_LENGTH + 4 * i,
  348. fb->pitches[0]);
  349. nvif_wr32(dev, NV_PVIDEO_BUFF0_OFFSET + 4 * i, 0);
  350. }
  351. nvif_wr32(dev, NV_PVIDEO_WINDOW_START, crtc_y << 16 | crtc_x);
  352. nvif_wr32(dev, NV_PVIDEO_WINDOW_SIZE, crtc_h << 16 | crtc_w);
  353. nvif_wr32(dev, NV_PVIDEO_STEP_SIZE,
  354. (uint32_t)(((src_h - 1) << 11) / (crtc_h - 1)) << 16 | (uint32_t)(((src_w - 1) << 11) / (crtc_w - 1)));
  355. /* It should be possible to convert hue/contrast to this */
  356. nvif_wr32(dev, NV_PVIDEO_RED_CSC_OFFSET, 0x69 - brightness);
  357. nvif_wr32(dev, NV_PVIDEO_GREEN_CSC_OFFSET, 0x3e + brightness);
  358. nvif_wr32(dev, NV_PVIDEO_BLUE_CSC_OFFSET, 0x89 - brightness);
  359. nvif_wr32(dev, NV_PVIDEO_CSC_ADJUST, 0);
  360. nvif_wr32(dev, NV_PVIDEO_CONTROL_Y, 0x001); /* (BLUR_ON, LINE_HALF) */
  361. nvif_wr32(dev, NV_PVIDEO_CONTROL_X, 0x111); /* (WEIGHT_HEAVY, SHARPENING_ON, SMOOTHING_ON) */
  362. nvif_wr32(dev, NV_PVIDEO_FIFO_BURST_LENGTH, 0x03);
  363. nvif_wr32(dev, NV_PVIDEO_FIFO_THRES_SIZE, 0x38);
  364. nvif_wr32(dev, NV_PVIDEO_KEY, nv_plane->colorkey);
  365. if (nv_plane->colorkey & (1 << 24))
  366. overlay |= 0x10;
  367. if (fb->format->format == DRM_FORMAT_YUYV)
  368. overlay |= 0x100;
  369. nvif_wr32(dev, NV_PVIDEO_OVERLAY, overlay);
  370. nvif_wr32(dev, NV_PVIDEO_SU_STATE, nvif_rd32(dev, NV_PVIDEO_SU_STATE) ^ (1 << 16));
  371. if (cur)
  372. nouveau_bo_unpin(cur);
  373. return 0;
  374. }
  375. static int
  376. nv04_disable_plane(struct drm_plane *plane,
  377. struct drm_modeset_acquire_ctx *ctx)
  378. {
  379. struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
  380. struct nouveau_plane *nv_plane =
  381. container_of(plane, struct nouveau_plane, base);
  382. nvif_mask(dev, NV_PVIDEO_OVERLAY, 1, 0);
  383. nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
  384. nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
  385. nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
  386. if (nv_plane->cur) {
  387. nouveau_bo_unpin(nv_plane->cur);
  388. nv_plane->cur = NULL;
  389. }
  390. return 0;
  391. }
  392. static const struct drm_plane_funcs nv04_plane_funcs = {
  393. .update_plane = nv04_update_plane,
  394. .disable_plane = nv04_disable_plane,
  395. .set_property = nv_set_property,
  396. .destroy = nv_destroy_plane,
  397. };
  398. static void
  399. nv04_overlay_init(struct drm_device *device)
  400. {
  401. struct nouveau_drm *drm = nouveau_drm(device);
  402. struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
  403. int ret;
  404. if (!plane)
  405. return;
  406. ret = drm_plane_init(device, &plane->base, 1 /* single crtc */,
  407. &nv04_plane_funcs,
  408. formats, 2, false);
  409. if (ret)
  410. goto err;
  411. /* Set up the plane properties */
  412. plane->props.colorkey = drm_property_create_range(
  413. device, 0, "colorkey", 0, 0x01ffffff);
  414. plane->props.brightness = drm_property_create_range(
  415. device, 0, "brightness", 0, 1024);
  416. if (!plane->props.colorkey ||
  417. !plane->props.brightness)
  418. goto cleanup;
  419. plane->colorkey = 0;
  420. drm_object_attach_property(&plane->base.base,
  421. plane->props.colorkey, plane->colorkey);
  422. plane->brightness = 512;
  423. drm_object_attach_property(&plane->base.base,
  424. plane->props.brightness, plane->brightness);
  425. drm_plane_force_disable(&plane->base);
  426. return;
  427. cleanup:
  428. drm_plane_cleanup(&plane->base);
  429. err:
  430. kfree(plane);
  431. NV_ERROR(drm, "Failed to create plane\n");
  432. }
  433. void
  434. nouveau_overlay_init(struct drm_device *device)
  435. {
  436. struct nvif_device *dev = &nouveau_drm(device)->client.device;
  437. if (dev->info.chipset < 0x10)
  438. nv04_overlay_init(device);
  439. else if (dev->info.chipset <= 0x40)
  440. nv10_overlay_init(device);
  441. }