overlay.c 14 KB

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