10ed5401bSmrg/*
20ed5401bSmrg * Copyright © 2018 NVIDIA Corporation
30ed5401bSmrg *
40ed5401bSmrg * Permission is hereby granted, free of charge, to any person obtaining a
50ed5401bSmrg * copy of this software and associated documentation files (the "Software"),
60ed5401bSmrg * to deal in the Software without restriction, including without limitation
70ed5401bSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
80ed5401bSmrg * and/or sell copies of the Software, and to permit persons to whom the
90ed5401bSmrg * Software is furnished to do so, subject to the following conditions:
100ed5401bSmrg *
110ed5401bSmrg * The above copyright notice and this permission notice shall be included in
120ed5401bSmrg * all copies or substantial portions of the Software.
130ed5401bSmrg *
140ed5401bSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
150ed5401bSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160ed5401bSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
170ed5401bSmrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
180ed5401bSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
190ed5401bSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
200ed5401bSmrg * OTHER DEALINGS IN THE SOFTWARE.
210ed5401bSmrg */
220ed5401bSmrg
230ed5401bSmrg#include <errno.h>
240ed5401bSmrg#include <string.h>
250ed5401bSmrg
260ed5401bSmrg#include "private.h"
270ed5401bSmrg#include "tegra.h"
280ed5401bSmrg#include "vic.h"
290ed5401bSmrg#include "vic40.h"
300ed5401bSmrg
310ed5401bSmrgstruct vic40 {
320ed5401bSmrg    struct vic base;
330ed5401bSmrg
340ed5401bSmrg    struct {
350ed5401bSmrg        struct drm_tegra_mapping *map;
360ed5401bSmrg        struct drm_tegra_bo *bo;
370ed5401bSmrg    } config;
380ed5401bSmrg
390ed5401bSmrg    struct {
400ed5401bSmrg        struct drm_tegra_mapping *map;
410ed5401bSmrg        struct drm_tegra_bo *bo;
420ed5401bSmrg    } filter;
430ed5401bSmrg};
440ed5401bSmrg
450ed5401bSmrgstatic int vic40_fill(struct vic *v, struct vic_image *output,
460ed5401bSmrg                      unsigned int left, unsigned int top,
470ed5401bSmrg                      unsigned int right, unsigned int bottom,
480ed5401bSmrg                      unsigned int alpha, unsigned int red,
490ed5401bSmrg                      unsigned int green, unsigned int blue)
500ed5401bSmrg{
510ed5401bSmrg    struct vic40 *vic = container_of(v, struct vic40, base);
520ed5401bSmrg    ConfigStruct *c;
530ed5401bSmrg    int err;
540ed5401bSmrg
550ed5401bSmrg    err = drm_tegra_bo_map(vic->config.bo, (void **)&c);
560ed5401bSmrg    if (err < 0) {
570ed5401bSmrg        fprintf(stderr, "failed to map configuration structure: %s\n",
580ed5401bSmrg                strerror(-err));
590ed5401bSmrg        return err;
600ed5401bSmrg    }
610ed5401bSmrg
620ed5401bSmrg    memset(c, 0, sizeof(*c));
630ed5401bSmrg
640ed5401bSmrg    c->outputConfig.TargetRectTop = top;
650ed5401bSmrg    c->outputConfig.TargetRectLeft = left;
660ed5401bSmrg    c->outputConfig.TargetRectRight = right;
670ed5401bSmrg    c->outputConfig.TargetRectBottom = bottom;
680ed5401bSmrg    c->outputConfig.BackgroundAlpha = alpha;
690ed5401bSmrg    c->outputConfig.BackgroundR = red;
700ed5401bSmrg    c->outputConfig.BackgroundG = green;
710ed5401bSmrg    c->outputConfig.BackgroundB = blue;
720ed5401bSmrg
730ed5401bSmrg    c->outputSurfaceConfig.OutPixelFormat = output->format;
740ed5401bSmrg    c->outputSurfaceConfig.OutBlkKind = output->kind;
750ed5401bSmrg    c->outputSurfaceConfig.OutBlkHeight = 0;
760ed5401bSmrg    c->outputSurfaceConfig.OutSurfaceWidth = output->width - 1;
770ed5401bSmrg    c->outputSurfaceConfig.OutSurfaceHeight = output->height - 1;
780ed5401bSmrg    c->outputSurfaceConfig.OutLumaWidth = output->stride - 1;
790ed5401bSmrg    c->outputSurfaceConfig.OutLumaHeight = output->height - 1;
800ed5401bSmrg    c->outputSurfaceConfig.OutChromaWidth = 16383;
810ed5401bSmrg    c->outputSurfaceConfig.OutChromaHeight = 16383;
820ed5401bSmrg
830ed5401bSmrg    drm_tegra_bo_unmap(vic->config.bo);
840ed5401bSmrg
850ed5401bSmrg    return 0;
860ed5401bSmrg}
870ed5401bSmrg
880ed5401bSmrgstatic int vic40_blit(struct vic *v, struct vic_image *output,
890ed5401bSmrg                      struct vic_image *input)
900ed5401bSmrg{
910ed5401bSmrg    struct vic40 *vic = container_of(v, struct vic40, base);
920ed5401bSmrg    SlotSurfaceConfig *surface;
930ed5401bSmrg    SlotConfig *slot;
940ed5401bSmrg    ConfigStruct *c;
950ed5401bSmrg    int err;
960ed5401bSmrg
970ed5401bSmrg    err = drm_tegra_bo_map(vic->config.bo, (void **)&c);
980ed5401bSmrg    if (err < 0) {
990ed5401bSmrg        fprintf(stderr, "failed to map configuration structure: %s\n",
1000ed5401bSmrg                strerror(-err));
1010ed5401bSmrg        return err;
1020ed5401bSmrg    }
1030ed5401bSmrg
1040ed5401bSmrg    memset(c, 0, sizeof(*c));
1050ed5401bSmrg
1060ed5401bSmrg    c->outputConfig.TargetRectTop = 0;
1070ed5401bSmrg    c->outputConfig.TargetRectLeft = 0;
1080ed5401bSmrg    c->outputConfig.TargetRectRight = output->width - 1;
1090ed5401bSmrg    c->outputConfig.TargetRectBottom = output->height - 1;
1100ed5401bSmrg    c->outputConfig.BackgroundAlpha = 1023;
1110ed5401bSmrg    c->outputConfig.BackgroundR = 1023;
1120ed5401bSmrg    c->outputConfig.BackgroundG = 1023;
1130ed5401bSmrg    c->outputConfig.BackgroundB = 1023;
1140ed5401bSmrg
1150ed5401bSmrg    c->outputSurfaceConfig.OutPixelFormat = output->format;
1160ed5401bSmrg    c->outputSurfaceConfig.OutBlkKind = output->kind;
1170ed5401bSmrg    c->outputSurfaceConfig.OutBlkHeight = 0;
1180ed5401bSmrg    c->outputSurfaceConfig.OutSurfaceWidth = output->width - 1;
1190ed5401bSmrg    c->outputSurfaceConfig.OutSurfaceHeight = output->height - 1;
1200ed5401bSmrg    c->outputSurfaceConfig.OutLumaWidth = output->stride - 1;
1210ed5401bSmrg    c->outputSurfaceConfig.OutLumaHeight = output->height - 1;
1220ed5401bSmrg    c->outputSurfaceConfig.OutChromaWidth = 16383;
1230ed5401bSmrg    c->outputSurfaceConfig.OutChromaHeight = 16383;
1240ed5401bSmrg
1250ed5401bSmrg    slot = &c->slotStruct[0].slotConfig;
1260ed5401bSmrg    slot->SlotEnable = 1;
1270ed5401bSmrg    slot->CurrentFieldEnable = 1;
1280ed5401bSmrg    slot->PlanarAlpha = 1023;
1290ed5401bSmrg    slot->ConstantAlpha = 1;
1300ed5401bSmrg    slot->SourceRectLeft = 0 << 16;
1310ed5401bSmrg    slot->SourceRectRight = (input->width - 1) << 16;
1320ed5401bSmrg    slot->SourceRectTop = 0 << 16;
1330ed5401bSmrg    slot->SourceRectBottom = (input->height - 1) << 16;
1340ed5401bSmrg    slot->DestRectLeft = 0;
1350ed5401bSmrg    slot->DestRectRight = output->width - 1;
1360ed5401bSmrg    slot->DestRectTop = 0;
1370ed5401bSmrg    slot->DestRectBottom = output->height - 1;
1380ed5401bSmrg    slot->SoftClampHigh = 1023;
1390ed5401bSmrg
1400ed5401bSmrg    surface = &c->slotStruct[0].slotSurfaceConfig;
1410ed5401bSmrg    surface->SlotPixelFormat = input->format;
1420ed5401bSmrg    surface->SlotBlkKind = input->kind;
1430ed5401bSmrg    surface->SlotBlkHeight = 0; /* XXX */
1440ed5401bSmrg    surface->SlotCacheWidth = VIC_CACHE_WIDTH_64Bx4; /* XXX */
1450ed5401bSmrg    surface->SlotSurfaceWidth = input->width - 1;
1460ed5401bSmrg    surface->SlotSurfaceHeight = input->height - 1;
1470ed5401bSmrg    surface->SlotLumaWidth = input->stride - 1;
1480ed5401bSmrg    surface->SlotLumaHeight = input->height - 1;
1490ed5401bSmrg    surface->SlotChromaWidth = 16383;
1500ed5401bSmrg    surface->SlotChromaHeight = 16383;
1510ed5401bSmrg
1520ed5401bSmrg    drm_tegra_bo_unmap(vic->config.bo);
1530ed5401bSmrg
1540ed5401bSmrg    return 0;
1550ed5401bSmrg}
1560ed5401bSmrg
1570ed5401bSmrgstatic int vic40_flip(struct vic *v, struct vic_image *output,
1580ed5401bSmrg                      struct vic_image *input)
1590ed5401bSmrg{
1600ed5401bSmrg    struct vic40 *vic = container_of(v, struct vic40, base);
1610ed5401bSmrg    SlotSurfaceConfig *surface;
1620ed5401bSmrg    SlotConfig *slot;
1630ed5401bSmrg    ConfigStruct *c;
1640ed5401bSmrg    int err;
1650ed5401bSmrg
1660ed5401bSmrg    err = drm_tegra_bo_map(vic->config.bo, (void **)&c);
1670ed5401bSmrg    if (err < 0) {
1680ed5401bSmrg        fprintf(stderr, "failed to map configuration structure: %s\n",
1690ed5401bSmrg                strerror(-err));
1700ed5401bSmrg        return err;
1710ed5401bSmrg    }
1720ed5401bSmrg
1730ed5401bSmrg    memset(c, 0, sizeof(*c));
1740ed5401bSmrg
1750ed5401bSmrg    c->outputConfig.TargetRectTop = 0;
1760ed5401bSmrg    c->outputConfig.TargetRectLeft = 0;
1770ed5401bSmrg    c->outputConfig.TargetRectRight = output->width - 1;
1780ed5401bSmrg    c->outputConfig.TargetRectBottom = output->height - 1;
1790ed5401bSmrg    c->outputConfig.BackgroundAlpha = 1023;
1800ed5401bSmrg    c->outputConfig.BackgroundR = 1023;
1810ed5401bSmrg    c->outputConfig.BackgroundG = 1023;
1820ed5401bSmrg    c->outputConfig.BackgroundB = 1023;
1830ed5401bSmrg    c->outputConfig.OutputFlipY = 1;
1840ed5401bSmrg
1850ed5401bSmrg    c->outputSurfaceConfig.OutPixelFormat = output->format;
1860ed5401bSmrg    c->outputSurfaceConfig.OutBlkKind = output->kind;
1870ed5401bSmrg    c->outputSurfaceConfig.OutBlkHeight = 0;
1880ed5401bSmrg    c->outputSurfaceConfig.OutSurfaceWidth = output->width - 1;
1890ed5401bSmrg    c->outputSurfaceConfig.OutSurfaceHeight = output->height - 1;
1900ed5401bSmrg    c->outputSurfaceConfig.OutLumaWidth = output->stride - 1;
1910ed5401bSmrg    c->outputSurfaceConfig.OutLumaHeight = output->height - 1;
1920ed5401bSmrg    c->outputSurfaceConfig.OutChromaWidth = 16383;
1930ed5401bSmrg    c->outputSurfaceConfig.OutChromaHeight = 16383;
1940ed5401bSmrg
1950ed5401bSmrg    slot = &c->slotStruct[0].slotConfig;
1960ed5401bSmrg    slot->SlotEnable = 1;
1970ed5401bSmrg    slot->CurrentFieldEnable = 1;
1980ed5401bSmrg    slot->PlanarAlpha = 1023;
1990ed5401bSmrg    slot->ConstantAlpha = 1;
2000ed5401bSmrg    slot->SourceRectLeft = 0 << 16;
2010ed5401bSmrg    slot->SourceRectRight = (input->width - 1) << 16;
2020ed5401bSmrg    slot->SourceRectTop = 0 << 16;
2030ed5401bSmrg    slot->SourceRectBottom = (input->height - 1) << 16;
2040ed5401bSmrg    slot->DestRectLeft = 0;
2050ed5401bSmrg    slot->DestRectRight = output->width - 1;
2060ed5401bSmrg    slot->DestRectTop = 0;
2070ed5401bSmrg    slot->DestRectBottom = output->height - 1;
2080ed5401bSmrg    slot->SoftClampHigh = 1023;
2090ed5401bSmrg
2100ed5401bSmrg    surface = &c->slotStruct[0].slotSurfaceConfig;
2110ed5401bSmrg    surface->SlotPixelFormat = input->format;
2120ed5401bSmrg    surface->SlotBlkKind = input->kind;
2130ed5401bSmrg    surface->SlotBlkHeight = 0; /* XXX */
2140ed5401bSmrg    surface->SlotCacheWidth = VIC_CACHE_WIDTH_64Bx4; /* XXX */
2150ed5401bSmrg    surface->SlotSurfaceWidth = input->width - 1;
2160ed5401bSmrg    surface->SlotSurfaceHeight = input->height - 1;
2170ed5401bSmrg    surface->SlotLumaWidth = input->stride - 1;
2180ed5401bSmrg    surface->SlotLumaHeight = input->height - 1;
2190ed5401bSmrg    surface->SlotChromaWidth = 16383;
2200ed5401bSmrg    surface->SlotChromaHeight = 16383;
2210ed5401bSmrg
2220ed5401bSmrg    drm_tegra_bo_unmap(vic->config.bo);
2230ed5401bSmrg
2240ed5401bSmrg    return 0;
2250ed5401bSmrg}
2260ed5401bSmrg
2270ed5401bSmrgstatic int vic40_execute(struct vic *v, struct drm_tegra_pushbuf *pushbuf,
2280ed5401bSmrg                         uint32_t **ptrp, struct vic_image *output,
2290ed5401bSmrg                         struct vic_image **inputs, unsigned int num_inputs)
2300ed5401bSmrg{
2310ed5401bSmrg    struct vic40 *vic = container_of(v, struct vic40, base);
2320ed5401bSmrg    unsigned int i;
2330ed5401bSmrg
2340ed5401bSmrg    if (num_inputs > 1)
2350ed5401bSmrg        return -EINVAL;
2360ed5401bSmrg
2370ed5401bSmrg    VIC_PUSH_METHOD(pushbuf, ptrp, NVB0B6_VIDEO_COMPOSITOR_SET_APPLICATION_ID, 1);
2380ed5401bSmrg    VIC_PUSH_METHOD(pushbuf, ptrp, NVB0B6_VIDEO_COMPOSITOR_SET_CONTROL_PARAMS, (sizeof(ConfigStruct) / 16) << 16);
2390ed5401bSmrg    VIC_PUSH_BUFFER(pushbuf, ptrp, NVB0B6_VIDEO_COMPOSITOR_SET_CONFIG_STRUCT_OFFSET, vic->config.map, 0, 0);
2400ed5401bSmrg    VIC_PUSH_BUFFER(pushbuf, ptrp, NVB0B6_VIDEO_COMPOSITOR_SET_OUTPUT_SURFACE_LUMA_OFFSET, output->map, 0, 0);
2410ed5401bSmrg
2420ed5401bSmrg    for (i = 0; i < num_inputs; i++)
2430ed5401bSmrg        VIC_PUSH_BUFFER(pushbuf, ptrp, NVB0B6_VIDEO_COMPOSITOR_SET_SURFACE0_SLOT0_LUMA_OFFSET, inputs[i]->map, 0, 0);
2440ed5401bSmrg
2450ed5401bSmrg    VIC_PUSH_METHOD(pushbuf, ptrp, NVB0B6_VIDEO_COMPOSITOR_EXECUTE, 1 << 8);
2460ed5401bSmrg
2470ed5401bSmrg    return 0;
2480ed5401bSmrg}
2490ed5401bSmrg
2500ed5401bSmrgstatic void vic40_free(struct vic *v)
2510ed5401bSmrg{
2520ed5401bSmrg    struct vic40 *vic = container_of(v, struct vic40, base);
2530ed5401bSmrg
2540ed5401bSmrg    drm_tegra_channel_unmap(vic->filter.map);
2550ed5401bSmrg    drm_tegra_bo_unref(vic->filter.bo);
2560ed5401bSmrg
2570ed5401bSmrg    drm_tegra_channel_unmap(vic->config.map);
2580ed5401bSmrg    drm_tegra_bo_unref(vic->config.bo);
2590ed5401bSmrg
2600ed5401bSmrg    drm_tegra_syncpoint_free(v->syncpt);
2610ed5401bSmrg
2620ed5401bSmrg    free(vic);
2630ed5401bSmrg}
2640ed5401bSmrg
2650ed5401bSmrgstatic const struct vic_ops vic40_ops = {
2660ed5401bSmrg    .fill = vic40_fill,
2670ed5401bSmrg    .blit = vic40_blit,
2680ed5401bSmrg    .flip = vic40_flip,
2690ed5401bSmrg    .execute = vic40_execute,
2700ed5401bSmrg    .free = vic40_free,
2710ed5401bSmrg};
2720ed5401bSmrg
2730ed5401bSmrgint vic40_new(struct drm_tegra *drm, struct drm_tegra_channel *channel,
2740ed5401bSmrg              struct vic **vicp)
2750ed5401bSmrg{
2760ed5401bSmrg    struct vic40 *vic;
2770ed5401bSmrg    void *ptr;
2780ed5401bSmrg    int err;
2790ed5401bSmrg
2800ed5401bSmrg    vic = calloc(1, sizeof(*vic));
2810ed5401bSmrg    if (!vic)
2820ed5401bSmrg        return -ENOMEM;
2830ed5401bSmrg
2840ed5401bSmrg    vic->base.drm = drm;
2850ed5401bSmrg    vic->base.channel = channel;
2860ed5401bSmrg    vic->base.ops = &vic40_ops;
2870ed5401bSmrg    vic->base.version = 0x21;
2880ed5401bSmrg
2890ed5401bSmrg    err = drm_tegra_syncpoint_new(drm, &vic->base.syncpt);
2900ed5401bSmrg    if (err < 0) {
2910ed5401bSmrg        fprintf(stderr, "failed to allocate syncpoint: %s\n", strerror(-err));
2920ed5401bSmrg        return err;
2930ed5401bSmrg    }
2940ed5401bSmrg
2950ed5401bSmrg    err = drm_tegra_bo_new(drm, 0, 16384, &vic->config.bo);
2960ed5401bSmrg    if (err < 0) {
2970ed5401bSmrg        fprintf(stderr, "failed to allocate configuration structurer: %s\n",
2980ed5401bSmrg                strerror(-err));
2990ed5401bSmrg        return err;
3000ed5401bSmrg    }
3010ed5401bSmrg
3020ed5401bSmrg    err = drm_tegra_channel_map(channel, vic->config.bo, DRM_TEGRA_CHANNEL_MAP_READ,
3030ed5401bSmrg                                &vic->config.map);
3040ed5401bSmrg    if (err < 0) {
3050ed5401bSmrg        fprintf(stderr, "failed to map configuration structure: %s\n",
3060ed5401bSmrg                strerror(-err));
3070ed5401bSmrg        return err;
3080ed5401bSmrg    }
3090ed5401bSmrg
3100ed5401bSmrg    err = drm_tegra_bo_new(drm, 0, 16384, &vic->filter.bo);
3110ed5401bSmrg    if (err < 0) {
3120ed5401bSmrg        fprintf(stderr, "failed to allocate filter buffer: %s\n",
3130ed5401bSmrg                strerror(-err));
3140ed5401bSmrg        return err;
3150ed5401bSmrg    }
3160ed5401bSmrg
3170ed5401bSmrg    err = drm_tegra_bo_map(vic->filter.bo, &ptr);
3180ed5401bSmrg    if (err < 0) {
3190ed5401bSmrg        fprintf(stderr, "failed to map filter buffer: %s\n", strerror(-err));
3200ed5401bSmrg        return err;
3210ed5401bSmrg    }
3220ed5401bSmrg
3230ed5401bSmrg    memset(ptr, 0, 16384);
3240ed5401bSmrg    drm_tegra_bo_unmap(vic->filter.bo);
3250ed5401bSmrg
3260ed5401bSmrg    err = drm_tegra_channel_map(channel, vic->filter.bo, DRM_TEGRA_CHANNEL_MAP_READ,
3270ed5401bSmrg                                &vic->filter.map);
3280ed5401bSmrg    if (err < 0) {
3290ed5401bSmrg        fprintf(stderr, "failed to map filter buffer: %s\n",
3300ed5401bSmrg                strerror(-err));
3310ed5401bSmrg        return err;
3320ed5401bSmrg    }
3330ed5401bSmrg
3340ed5401bSmrg    if (vicp)
3350ed5401bSmrg        *vicp = &vic->base;
3360ed5401bSmrg
3370ed5401bSmrg    return 0;
3380ed5401bSmrg}
339