overlay.c 14 KB

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