1/*
2 * Copyright © 2014 NVIDIA Corporation
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#ifdef HAVE_CONFIG_H
24#  include "config.h"
25#endif
26
27#include <errno.h>
28#include <stdio.h>
29
30#include "drm-test-tegra.h"
31#include "tegra.h"
32
33int drm_tegra_gr2d_open(struct drm_tegra *drm, struct drm_tegra_gr2d **gr2dp)
34{
35    struct drm_tegra_gr2d *gr2d;
36    int err;
37
38    gr2d = calloc(1, sizeof(*gr2d));
39    if (!gr2d)
40        return -ENOMEM;
41
42    gr2d->drm = drm;
43
44    err = drm_tegra_channel_open(drm, DRM_TEGRA_GR2D, &gr2d->channel);
45    if (err < 0) {
46        free(gr2d);
47        return err;
48    }
49
50    *gr2dp = gr2d;
51
52    return 0;
53}
54
55int drm_tegra_gr2d_close(struct drm_tegra_gr2d *gr2d)
56{
57    if (!gr2d)
58        return -EINVAL;
59
60    drm_tegra_channel_close(gr2d->channel);
61    free(gr2d);
62
63    return 0;
64}
65
66int drm_tegra_gr2d_fill(struct drm_tegra_gr2d *gr2d, struct drm_framebuffer *fb,
67                        unsigned int x, unsigned int y, unsigned int width,
68                        unsigned int height, uint32_t color)
69{
70    struct drm_tegra_bo *fbo = fb->data;
71    struct drm_tegra_pushbuf *pushbuf;
72    struct drm_tegra_mapping *map;
73    struct drm_tegra_job *job;
74    uint32_t *ptr;
75    int err;
76
77    err = drm_tegra_job_new(gr2d->channel, &job);
78    if (err < 0)
79        return err;
80
81    err = drm_tegra_channel_map(gr2d->channel, fbo, 0, &map);
82    if (err < 0)
83        return err;
84
85    err = drm_tegra_job_get_pushbuf(job, &pushbuf);
86    if (err < 0)
87        return err;
88
89    err = drm_tegra_pushbuf_begin(pushbuf, 32, &ptr);
90    if (err < 0)
91        return err;
92
93    *ptr++ = HOST1X_OPCODE_SETCL(0, HOST1X_CLASS_GR2D, 0);
94
95    *ptr++ = HOST1X_OPCODE_MASK(0x9, 0x9);
96    *ptr++ = 0x0000003a;
97    *ptr++ = 0x00000000;
98
99    *ptr++ = HOST1X_OPCODE_MASK(0x1e, 0x7);
100    *ptr++ = 0x00000000;
101    *ptr++ = (2 << 16) | (1 << 6) | (1 << 2);
102    *ptr++ = 0x000000cc;
103
104    *ptr++ = HOST1X_OPCODE_MASK(0x2b, 0x9);
105
106    /* relocate destination buffer */
107    err = drm_tegra_pushbuf_relocate(pushbuf, &ptr, map, 0, 0, 0);
108    if (err < 0) {
109        fprintf(stderr, "failed to relocate buffer object: %d\n", err);
110        return err;
111    }
112
113    *ptr++ = fb->pitch;
114
115    *ptr++ = HOST1X_OPCODE_NONINCR(0x35, 1);
116    *ptr++ = color;
117
118    *ptr++ = HOST1X_OPCODE_NONINCR(0x46, 1);
119    *ptr++ = 0x00000000;
120
121    *ptr++ = HOST1X_OPCODE_MASK(0x38, 0x5);
122    *ptr++ = height << 16 | width;
123    *ptr++ = y << 16 | x;
124
125    err = drm_tegra_pushbuf_end(pushbuf, ptr);
126    if (err < 0) {
127        fprintf(stderr, "failed to update push buffer: %d\n", -err);
128        return err;
129    }
130
131    err = drm_tegra_job_submit(job, NULL);
132    if (err < 0) {
133        fprintf(stderr, "failed to submit job: %d\n", err);
134        return err;
135    }
136
137    err = drm_tegra_job_wait(job, 0);
138    if (err < 0) {
139        fprintf(stderr, "failed to wait for fence: %d\n", err);
140        return err;
141    }
142
143    drm_tegra_channel_unmap(map);
144    drm_tegra_job_free(job);
145
146    return 0;
147}
148