10ed5401bSmrg/*
20ed5401bSmrg * Copyright © 2014 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#ifdef HAVE_CONFIG_H
240ed5401bSmrg#  include "config.h"
250ed5401bSmrg#endif
260ed5401bSmrg
270ed5401bSmrg#include <errno.h>
280ed5401bSmrg#include <stdio.h>
290ed5401bSmrg
300ed5401bSmrg#include "drm-test-tegra.h"
310ed5401bSmrg#include "tegra.h"
320ed5401bSmrg
330ed5401bSmrgint drm_tegra_gr2d_open(struct drm_tegra *drm, struct drm_tegra_gr2d **gr2dp)
340ed5401bSmrg{
350ed5401bSmrg    struct drm_tegra_gr2d *gr2d;
360ed5401bSmrg    int err;
370ed5401bSmrg
380ed5401bSmrg    gr2d = calloc(1, sizeof(*gr2d));
390ed5401bSmrg    if (!gr2d)
400ed5401bSmrg        return -ENOMEM;
410ed5401bSmrg
420ed5401bSmrg    gr2d->drm = drm;
430ed5401bSmrg
440ed5401bSmrg    err = drm_tegra_channel_open(drm, DRM_TEGRA_GR2D, &gr2d->channel);
450ed5401bSmrg    if (err < 0) {
460ed5401bSmrg        free(gr2d);
470ed5401bSmrg        return err;
480ed5401bSmrg    }
490ed5401bSmrg
500ed5401bSmrg    *gr2dp = gr2d;
510ed5401bSmrg
520ed5401bSmrg    return 0;
530ed5401bSmrg}
540ed5401bSmrg
550ed5401bSmrgint drm_tegra_gr2d_close(struct drm_tegra_gr2d *gr2d)
560ed5401bSmrg{
570ed5401bSmrg    if (!gr2d)
580ed5401bSmrg        return -EINVAL;
590ed5401bSmrg
600ed5401bSmrg    drm_tegra_channel_close(gr2d->channel);
610ed5401bSmrg    free(gr2d);
620ed5401bSmrg
630ed5401bSmrg    return 0;
640ed5401bSmrg}
650ed5401bSmrg
660ed5401bSmrgint drm_tegra_gr2d_fill(struct drm_tegra_gr2d *gr2d, struct drm_framebuffer *fb,
670ed5401bSmrg                        unsigned int x, unsigned int y, unsigned int width,
680ed5401bSmrg                        unsigned int height, uint32_t color)
690ed5401bSmrg{
700ed5401bSmrg    struct drm_tegra_bo *fbo = fb->data;
710ed5401bSmrg    struct drm_tegra_pushbuf *pushbuf;
720ed5401bSmrg    struct drm_tegra_mapping *map;
730ed5401bSmrg    struct drm_tegra_job *job;
740ed5401bSmrg    uint32_t *ptr;
750ed5401bSmrg    int err;
760ed5401bSmrg
770ed5401bSmrg    err = drm_tegra_job_new(gr2d->channel, &job);
780ed5401bSmrg    if (err < 0)
790ed5401bSmrg        return err;
800ed5401bSmrg
810ed5401bSmrg    err = drm_tegra_channel_map(gr2d->channel, fbo, 0, &map);
820ed5401bSmrg    if (err < 0)
830ed5401bSmrg        return err;
840ed5401bSmrg
850ed5401bSmrg    err = drm_tegra_job_get_pushbuf(job, &pushbuf);
860ed5401bSmrg    if (err < 0)
870ed5401bSmrg        return err;
880ed5401bSmrg
890ed5401bSmrg    err = drm_tegra_pushbuf_begin(pushbuf, 32, &ptr);
900ed5401bSmrg    if (err < 0)
910ed5401bSmrg        return err;
920ed5401bSmrg
930ed5401bSmrg    *ptr++ = HOST1X_OPCODE_SETCL(0, HOST1X_CLASS_GR2D, 0);
940ed5401bSmrg
950ed5401bSmrg    *ptr++ = HOST1X_OPCODE_MASK(0x9, 0x9);
960ed5401bSmrg    *ptr++ = 0x0000003a;
970ed5401bSmrg    *ptr++ = 0x00000000;
980ed5401bSmrg
990ed5401bSmrg    *ptr++ = HOST1X_OPCODE_MASK(0x1e, 0x7);
1000ed5401bSmrg    *ptr++ = 0x00000000;
1010ed5401bSmrg    *ptr++ = (2 << 16) | (1 << 6) | (1 << 2);
1020ed5401bSmrg    *ptr++ = 0x000000cc;
1030ed5401bSmrg
1040ed5401bSmrg    *ptr++ = HOST1X_OPCODE_MASK(0x2b, 0x9);
1050ed5401bSmrg
1060ed5401bSmrg    /* relocate destination buffer */
1070ed5401bSmrg    err = drm_tegra_pushbuf_relocate(pushbuf, &ptr, map, 0, 0, 0);
1080ed5401bSmrg    if (err < 0) {
1090ed5401bSmrg        fprintf(stderr, "failed to relocate buffer object: %d\n", err);
1100ed5401bSmrg        return err;
1110ed5401bSmrg    }
1120ed5401bSmrg
1130ed5401bSmrg    *ptr++ = fb->pitch;
1140ed5401bSmrg
1150ed5401bSmrg    *ptr++ = HOST1X_OPCODE_NONINCR(0x35, 1);
1160ed5401bSmrg    *ptr++ = color;
1170ed5401bSmrg
1180ed5401bSmrg    *ptr++ = HOST1X_OPCODE_NONINCR(0x46, 1);
1190ed5401bSmrg    *ptr++ = 0x00000000;
1200ed5401bSmrg
1210ed5401bSmrg    *ptr++ = HOST1X_OPCODE_MASK(0x38, 0x5);
1220ed5401bSmrg    *ptr++ = height << 16 | width;
1230ed5401bSmrg    *ptr++ = y << 16 | x;
1240ed5401bSmrg
1250ed5401bSmrg    err = drm_tegra_pushbuf_end(pushbuf, ptr);
1260ed5401bSmrg    if (err < 0) {
1270ed5401bSmrg        fprintf(stderr, "failed to update push buffer: %d\n", -err);
1280ed5401bSmrg        return err;
1290ed5401bSmrg    }
1300ed5401bSmrg
1310ed5401bSmrg    err = drm_tegra_job_submit(job, NULL);
1320ed5401bSmrg    if (err < 0) {
1330ed5401bSmrg        fprintf(stderr, "failed to submit job: %d\n", err);
1340ed5401bSmrg        return err;
1350ed5401bSmrg    }
1360ed5401bSmrg
1370ed5401bSmrg    err = drm_tegra_job_wait(job, 0);
1380ed5401bSmrg    if (err < 0) {
1390ed5401bSmrg        fprintf(stderr, "failed to wait for fence: %d\n", err);
1400ed5401bSmrg        return err;
1410ed5401bSmrg    }
1420ed5401bSmrg
1430ed5401bSmrg    drm_tegra_channel_unmap(map);
1440ed5401bSmrg    drm_tegra_job_free(job);
1450ed5401bSmrg
1460ed5401bSmrg    return 0;
1470ed5401bSmrg}
148