Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2013 Ilia Mirkin
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0018  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
0019  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020  * SOFTWARE.
0021  *
0022  * Implementation based on the pre-KMS implementation in xf86-video-nouveau,
0023  * written by Arthur Huillet.
0024  */
0025 
0026 #include <drm/drm_crtc.h>
0027 #include <drm/drm_fourcc.h>
0028 
0029 #include "nouveau_drv.h"
0030 
0031 #include "nouveau_bo.h"
0032 #include "nouveau_connector.h"
0033 #include "nouveau_display.h"
0034 #include "nouveau_gem.h"
0035 #include "nvreg.h"
0036 #include "disp.h"
0037 
0038 struct nouveau_plane {
0039     struct drm_plane base;
0040     bool flip;
0041     struct nouveau_bo *cur;
0042 
0043     struct {
0044         struct drm_property *colorkey;
0045         struct drm_property *contrast;
0046         struct drm_property *brightness;
0047         struct drm_property *hue;
0048         struct drm_property *saturation;
0049     } props;
0050 
0051     int colorkey;
0052     int contrast;
0053     int brightness;
0054     int hue;
0055     int saturation;
0056     enum drm_color_encoding color_encoding;
0057 
0058     void (*set_params)(struct nouveau_plane *);
0059 };
0060 
0061 static uint32_t formats[] = {
0062     DRM_FORMAT_YUYV,
0063     DRM_FORMAT_UYVY,
0064     DRM_FORMAT_NV12,
0065     DRM_FORMAT_NV21,
0066 };
0067 
0068 /* Sine can be approximated with
0069  * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
0070  * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
0071  * Note that this only works for the range [0, 180].
0072  * Also note that sin(x) == -sin(x - 180)
0073  */
0074 static inline int
0075 sin_mul(int degrees, int factor)
0076 {
0077     if (degrees > 180) {
0078         degrees -= 180;
0079         factor *= -1;
0080     }
0081     return factor * 4 * degrees * (180 - degrees) /
0082         (40500 - degrees * (180 - degrees));
0083 }
0084 
0085 /* cos(x) = sin(x + 90) */
0086 static inline int
0087 cos_mul(int degrees, int factor)
0088 {
0089     return sin_mul((degrees + 90) % 360, factor);
0090 }
0091 
0092 static int
0093 verify_scaling(const struct drm_framebuffer *fb, uint8_t shift,
0094                uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h,
0095                uint32_t crtc_w, uint32_t crtc_h)
0096 {
0097     if (crtc_w < (src_w >> shift) || crtc_h < (src_h >> shift)) {
0098         DRM_DEBUG_KMS("Unsuitable framebuffer scaling: %dx%d -> %dx%d\n",
0099                   src_w, src_h, crtc_w, crtc_h);
0100         return -ERANGE;
0101     }
0102 
0103     if (src_x != 0 || src_y != 0) {
0104         DRM_DEBUG_KMS("Unsuitable framebuffer offset: %d,%d\n",
0105                               src_x, src_y);
0106         return -ERANGE;
0107     }
0108 
0109     return 0;
0110 }
0111 
0112 static int
0113 nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
0114           struct drm_framebuffer *fb, int crtc_x, int crtc_y,
0115           unsigned int crtc_w, unsigned int crtc_h,
0116           uint32_t src_x, uint32_t src_y,
0117           uint32_t src_w, uint32_t src_h,
0118           struct drm_modeset_acquire_ctx *ctx)
0119 {
0120     struct nouveau_drm *drm = nouveau_drm(plane->dev);
0121     struct nvif_object *dev = &drm->client.device.object;
0122     struct nouveau_plane *nv_plane =
0123         container_of(plane, struct nouveau_plane, base);
0124     struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
0125     struct nouveau_bo *cur = nv_plane->cur;
0126     struct nouveau_bo *nvbo;
0127     bool flip = nv_plane->flip;
0128     int soff = NV_PCRTC0_SIZE * nv_crtc->index;
0129     int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
0130     unsigned shift = drm->client.device.info.chipset >= 0x30 ? 1 : 3;
0131     unsigned format = 0;
0132     int ret;
0133 
0134     /* Source parameters given in 16.16 fixed point, ignore fractional. */
0135     src_x >>= 16;
0136     src_y >>= 16;
0137     src_w >>= 16;
0138     src_h >>= 16;
0139 
0140     ret = verify_scaling(fb, shift, 0, 0, src_w, src_h, crtc_w, crtc_h);
0141     if (ret)
0142         return ret;
0143 
0144     nvbo = nouveau_gem_object(fb->obj[0]);
0145     ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, false);
0146     if (ret)
0147         return ret;
0148 
0149     nv_plane->cur = nvbo;
0150 
0151     nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
0152     nvif_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
0153 
0154     nvif_wr32(dev, NV_PVIDEO_BASE(flip), 0);
0155     nvif_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nvbo->offset);
0156     nvif_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
0157     nvif_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
0158     nvif_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
0159     nvif_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
0160     nvif_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
0161     nvif_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
0162 
0163     if (fb->format->format == DRM_FORMAT_YUYV ||
0164         fb->format->format == DRM_FORMAT_NV12)
0165         format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
0166     if (fb->format->format == DRM_FORMAT_NV12 ||
0167         fb->format->format == DRM_FORMAT_NV21)
0168         format |= NV_PVIDEO_FORMAT_PLANAR;
0169     if (nv_plane->color_encoding == DRM_COLOR_YCBCR_BT709)
0170         format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
0171     if (nv_plane->colorkey & (1 << 24))
0172         format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
0173 
0174     if (format & NV_PVIDEO_FORMAT_PLANAR) {
0175         nvif_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
0176         nvif_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
0177             nvbo->offset + fb->offsets[1]);
0178     }
0179     nvif_wr32(dev, NV_PVIDEO_FORMAT(flip), format | fb->pitches[0]);
0180     nvif_wr32(dev, NV_PVIDEO_STOP, 0);
0181     /* TODO: wait for vblank? */
0182     nvif_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
0183     nv_plane->flip = !flip;
0184 
0185     if (cur)
0186         nouveau_bo_unpin(cur);
0187 
0188     return 0;
0189 }
0190 
0191 static int
0192 nv10_disable_plane(struct drm_plane *plane,
0193            struct drm_modeset_acquire_ctx *ctx)
0194 {
0195     struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
0196     struct nouveau_plane *nv_plane =
0197         container_of(plane, struct nouveau_plane, base);
0198 
0199     nvif_wr32(dev, NV_PVIDEO_STOP, 1);
0200     if (nv_plane->cur) {
0201         nouveau_bo_unpin(nv_plane->cur);
0202         nv_plane->cur = NULL;
0203     }
0204 
0205     return 0;
0206 }
0207 
0208 static void
0209 nv_destroy_plane(struct drm_plane *plane)
0210 {
0211     drm_plane_force_disable(plane);
0212     drm_plane_cleanup(plane);
0213     kfree(plane);
0214 }
0215 
0216 static void
0217 nv10_set_params(struct nouveau_plane *plane)
0218 {
0219     struct nvif_object *dev = &nouveau_drm(plane->base.dev)->client.device.object;
0220     u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
0221     u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
0222         (cos_mul(plane->hue, plane->saturation) & 0xffff);
0223     u32 format = 0;
0224 
0225     nvif_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
0226     nvif_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
0227     nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
0228     nvif_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
0229     nvif_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
0230 
0231     if (plane->cur) {
0232         if (plane->color_encoding == DRM_COLOR_YCBCR_BT709)
0233             format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
0234         if (plane->colorkey & (1 << 24))
0235             format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
0236         nvif_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
0237             NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
0238             NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
0239             format);
0240     }
0241 }
0242 
0243 static int
0244 nv_set_property(struct drm_plane *plane,
0245         struct drm_property *property,
0246         uint64_t value)
0247 {
0248     struct nouveau_plane *nv_plane =
0249         container_of(plane, struct nouveau_plane, base);
0250 
0251     if (property == nv_plane->props.colorkey)
0252         nv_plane->colorkey = value;
0253     else if (property == nv_plane->props.contrast)
0254         nv_plane->contrast = value;
0255     else if (property == nv_plane->props.brightness)
0256         nv_plane->brightness = value;
0257     else if (property == nv_plane->props.hue)
0258         nv_plane->hue = value;
0259     else if (property == nv_plane->props.saturation)
0260         nv_plane->saturation = value;
0261     else if (property == nv_plane->base.color_encoding_property)
0262         nv_plane->color_encoding = value;
0263     else
0264         return -EINVAL;
0265 
0266     if (nv_plane->set_params)
0267         nv_plane->set_params(nv_plane);
0268     return 0;
0269 }
0270 
0271 static const struct drm_plane_funcs nv10_plane_funcs = {
0272     .update_plane = nv10_update_plane,
0273     .disable_plane = nv10_disable_plane,
0274     .set_property = nv_set_property,
0275     .destroy = nv_destroy_plane,
0276 };
0277 
0278 static void
0279 nv10_overlay_init(struct drm_device *device)
0280 {
0281     struct nouveau_drm *drm = nouveau_drm(device);
0282     struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
0283     unsigned int num_formats = ARRAY_SIZE(formats);
0284     int ret;
0285 
0286     if (!plane)
0287         return;
0288 
0289     switch (drm->client.device.info.chipset) {
0290     case 0x10:
0291     case 0x11:
0292     case 0x15:
0293     case 0x1a:
0294     case 0x20:
0295         num_formats = 2;
0296         break;
0297     }
0298 
0299     ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
0300                  &nv10_plane_funcs,
0301                  formats, num_formats, false);
0302     if (ret)
0303         goto err;
0304 
0305     /* Set up the plane properties */
0306     plane->props.colorkey = drm_property_create_range(
0307             device, 0, "colorkey", 0, 0x01ffffff);
0308     plane->props.contrast = drm_property_create_range(
0309             device, 0, "contrast", 0, 8192 - 1);
0310     plane->props.brightness = drm_property_create_range(
0311             device, 0, "brightness", 0, 1024);
0312     plane->props.hue = drm_property_create_range(
0313             device, 0, "hue", 0, 359);
0314     plane->props.saturation = drm_property_create_range(
0315             device, 0, "saturation", 0, 8192 - 1);
0316     if (!plane->props.colorkey ||
0317         !plane->props.contrast ||
0318         !plane->props.brightness ||
0319         !plane->props.hue ||
0320         !plane->props.saturation)
0321         goto cleanup;
0322 
0323     plane->colorkey = 0;
0324     drm_object_attach_property(&plane->base.base,
0325                    plane->props.colorkey, plane->colorkey);
0326 
0327     plane->contrast = 0x1000;
0328     drm_object_attach_property(&plane->base.base,
0329                    plane->props.contrast, plane->contrast);
0330 
0331     plane->brightness = 512;
0332     drm_object_attach_property(&plane->base.base,
0333                    plane->props.brightness, plane->brightness);
0334 
0335     plane->hue = 0;
0336     drm_object_attach_property(&plane->base.base,
0337                    plane->props.hue, plane->hue);
0338 
0339     plane->saturation = 0x1000;
0340     drm_object_attach_property(&plane->base.base,
0341                    plane->props.saturation, plane->saturation);
0342 
0343     plane->color_encoding = DRM_COLOR_YCBCR_BT601;
0344     drm_plane_create_color_properties(&plane->base,
0345                       BIT(DRM_COLOR_YCBCR_BT601) |
0346                       BIT(DRM_COLOR_YCBCR_BT709),
0347                       BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
0348                       DRM_COLOR_YCBCR_BT601,
0349                       DRM_COLOR_YCBCR_LIMITED_RANGE);
0350 
0351     plane->set_params = nv10_set_params;
0352     nv10_set_params(plane);
0353     drm_plane_force_disable(&plane->base);
0354     return;
0355 cleanup:
0356     drm_plane_cleanup(&plane->base);
0357 err:
0358     kfree(plane);
0359     NV_ERROR(drm, "Failed to create plane\n");
0360 }
0361 
0362 static int
0363 nv04_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
0364           struct drm_framebuffer *fb, int crtc_x, int crtc_y,
0365           unsigned int crtc_w, unsigned int crtc_h,
0366           uint32_t src_x, uint32_t src_y,
0367           uint32_t src_w, uint32_t src_h,
0368           struct drm_modeset_acquire_ctx *ctx)
0369 {
0370     struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
0371     struct nouveau_plane *nv_plane =
0372         container_of(plane, struct nouveau_plane, base);
0373     struct nouveau_bo *cur = nv_plane->cur;
0374     struct nouveau_bo *nvbo;
0375     uint32_t overlay = 1;
0376     int brightness = (nv_plane->brightness - 512) * 62 / 512;
0377     int ret, i;
0378 
0379     /* Source parameters given in 16.16 fixed point, ignore fractional. */
0380     src_x >>= 16;
0381     src_y >>= 16;
0382     src_w >>= 16;
0383     src_h >>= 16;
0384 
0385     ret = verify_scaling(fb, 0, src_x, src_y, src_w, src_h, crtc_w, crtc_h);
0386     if (ret)
0387         return ret;
0388 
0389     nvbo = nouveau_gem_object(fb->obj[0]);
0390     ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, false);
0391     if (ret)
0392         return ret;
0393 
0394     nv_plane->cur = nvbo;
0395 
0396     nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
0397     nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
0398     nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
0399 
0400     for (i = 0; i < 2; i++) {
0401         nvif_wr32(dev, NV_PVIDEO_BUFF0_START_ADDRESS + 4 * i,
0402               nvbo->offset);
0403         nvif_wr32(dev, NV_PVIDEO_BUFF0_PITCH_LENGTH + 4 * i,
0404               fb->pitches[0]);
0405         nvif_wr32(dev, NV_PVIDEO_BUFF0_OFFSET + 4 * i, 0);
0406     }
0407     nvif_wr32(dev, NV_PVIDEO_WINDOW_START, crtc_y << 16 | crtc_x);
0408     nvif_wr32(dev, NV_PVIDEO_WINDOW_SIZE, crtc_h << 16 | crtc_w);
0409     nvif_wr32(dev, NV_PVIDEO_STEP_SIZE,
0410         (uint32_t)(((src_h - 1) << 11) / (crtc_h - 1)) << 16 | (uint32_t)(((src_w - 1) << 11) / (crtc_w - 1)));
0411 
0412     /* It should be possible to convert hue/contrast to this */
0413     nvif_wr32(dev, NV_PVIDEO_RED_CSC_OFFSET, 0x69 - brightness);
0414     nvif_wr32(dev, NV_PVIDEO_GREEN_CSC_OFFSET, 0x3e + brightness);
0415     nvif_wr32(dev, NV_PVIDEO_BLUE_CSC_OFFSET, 0x89 - brightness);
0416     nvif_wr32(dev, NV_PVIDEO_CSC_ADJUST, 0);
0417 
0418     nvif_wr32(dev, NV_PVIDEO_CONTROL_Y, 0x001); /* (BLUR_ON, LINE_HALF) */
0419     nvif_wr32(dev, NV_PVIDEO_CONTROL_X, 0x111); /* (WEIGHT_HEAVY, SHARPENING_ON, SMOOTHING_ON) */
0420 
0421     nvif_wr32(dev, NV_PVIDEO_FIFO_BURST_LENGTH, 0x03);
0422     nvif_wr32(dev, NV_PVIDEO_FIFO_THRES_SIZE, 0x38);
0423 
0424     nvif_wr32(dev, NV_PVIDEO_KEY, nv_plane->colorkey);
0425 
0426     if (nv_plane->colorkey & (1 << 24))
0427         overlay |= 0x10;
0428     if (fb->format->format == DRM_FORMAT_YUYV)
0429         overlay |= 0x100;
0430 
0431     nvif_wr32(dev, NV_PVIDEO_OVERLAY, overlay);
0432 
0433     nvif_wr32(dev, NV_PVIDEO_SU_STATE, nvif_rd32(dev, NV_PVIDEO_SU_STATE) ^ (1 << 16));
0434 
0435     if (cur)
0436         nouveau_bo_unpin(cur);
0437 
0438     return 0;
0439 }
0440 
0441 static int
0442 nv04_disable_plane(struct drm_plane *plane,
0443            struct drm_modeset_acquire_ctx *ctx)
0444 {
0445     struct nvif_object *dev = &nouveau_drm(plane->dev)->client.device.object;
0446     struct nouveau_plane *nv_plane =
0447         container_of(plane, struct nouveau_plane, base);
0448 
0449     nvif_mask(dev, NV_PVIDEO_OVERLAY, 1, 0);
0450     nvif_wr32(dev, NV_PVIDEO_OE_STATE, 0);
0451     nvif_wr32(dev, NV_PVIDEO_SU_STATE, 0);
0452     nvif_wr32(dev, NV_PVIDEO_RM_STATE, 0);
0453     if (nv_plane->cur) {
0454         nouveau_bo_unpin(nv_plane->cur);
0455         nv_plane->cur = NULL;
0456     }
0457 
0458     return 0;
0459 }
0460 
0461 static const struct drm_plane_funcs nv04_plane_funcs = {
0462     .update_plane = nv04_update_plane,
0463     .disable_plane = nv04_disable_plane,
0464     .set_property = nv_set_property,
0465     .destroy = nv_destroy_plane,
0466 };
0467 
0468 static void
0469 nv04_overlay_init(struct drm_device *device)
0470 {
0471     struct nouveau_drm *drm = nouveau_drm(device);
0472     struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
0473     int ret;
0474 
0475     if (!plane)
0476         return;
0477 
0478     ret = drm_plane_init(device, &plane->base, 1 /* single crtc */,
0479                  &nv04_plane_funcs,
0480                  formats, 2, false);
0481     if (ret)
0482         goto err;
0483 
0484     /* Set up the plane properties */
0485     plane->props.colorkey = drm_property_create_range(
0486             device, 0, "colorkey", 0, 0x01ffffff);
0487     plane->props.brightness = drm_property_create_range(
0488             device, 0, "brightness", 0, 1024);
0489     if (!plane->props.colorkey ||
0490         !plane->props.brightness)
0491         goto cleanup;
0492 
0493     plane->colorkey = 0;
0494     drm_object_attach_property(&plane->base.base,
0495                    plane->props.colorkey, plane->colorkey);
0496 
0497     plane->brightness = 512;
0498     drm_object_attach_property(&plane->base.base,
0499                    plane->props.brightness, plane->brightness);
0500 
0501     drm_plane_force_disable(&plane->base);
0502     return;
0503 cleanup:
0504     drm_plane_cleanup(&plane->base);
0505 err:
0506     kfree(plane);
0507     NV_ERROR(drm, "Failed to create plane\n");
0508 }
0509 
0510 void
0511 nouveau_overlay_init(struct drm_device *device)
0512 {
0513     struct nvif_device *dev = &nouveau_drm(device)->client.device;
0514     if (dev->info.chipset < 0x10)
0515         nv04_overlay_init(device);
0516     else if (dev->info.chipset <= 0x40)
0517         nv10_overlay_init(device);
0518 }