overlay.c 14 KB

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