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