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