blob: 27ffcf15a4b4dc825efb29aed0d13818aee33f75 [file] [log] [blame]
Terje Bergstromd43f81c2013-03-22 16:34:09 +02001/*
2 * drivers/video/tegra/host/gr2d/gr2d.c
3 *
4 * Tegra Graphics 2D
5 *
6 * Copyright (c) 2012-2013, NVIDIA Corporation.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/export.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/clk.h>
25
26#include "channel.h"
27#include "drm.h"
28#include "gem.h"
29#include "job.h"
30#include "host1x.h"
31#include "host1x_bo.h"
32#include "host1x_client.h"
33#include "syncpt.h"
34
35struct gr2d {
36 struct host1x_client client;
37 struct clk *clk;
38 struct host1x_channel *channel;
39 unsigned long *addr_regs;
40};
41
42static inline struct gr2d *to_gr2d(struct host1x_client *client)
43{
44 return container_of(client, struct gr2d, client);
45}
46
47static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 reg);
48
49static int gr2d_client_init(struct host1x_client *client,
50 struct drm_device *drm)
51{
52 return 0;
53}
54
55static int gr2d_client_exit(struct host1x_client *client)
56{
57 return 0;
58}
59
60static int gr2d_open_channel(struct host1x_client *client,
61 struct host1x_drm_context *context)
62{
63 struct gr2d *gr2d = to_gr2d(client);
64
65 context->channel = host1x_channel_get(gr2d->channel);
66
67 if (!context->channel)
68 return -ENOMEM;
69
70 return 0;
71}
72
73static void gr2d_close_channel(struct host1x_drm_context *context)
74{
75 host1x_channel_put(context->channel);
76}
77
78static struct host1x_bo *host1x_bo_lookup(struct drm_device *drm,
79 struct drm_file *file,
80 u32 handle)
81{
82 struct drm_gem_object *gem;
83 struct tegra_bo *bo;
84
85 gem = drm_gem_object_lookup(drm, file, handle);
86 if (!gem)
Thierry Redingdc618db2013-05-19 14:21:22 +020087 return NULL;
Terje Bergstromd43f81c2013-03-22 16:34:09 +020088
89 mutex_lock(&drm->struct_mutex);
90 drm_gem_object_unreference(gem);
91 mutex_unlock(&drm->struct_mutex);
92
93 bo = to_tegra_bo(gem);
94 return &bo->base;
95}
96
97static int gr2d_submit(struct host1x_drm_context *context,
98 struct drm_tegra_submit *args, struct drm_device *drm,
99 struct drm_file *file)
100{
101 struct host1x_job *job;
102 unsigned int num_cmdbufs = args->num_cmdbufs;
103 unsigned int num_relocs = args->num_relocs;
104 unsigned int num_waitchks = args->num_waitchks;
105 struct drm_tegra_cmdbuf __user *cmdbufs =
106 (void * __user)(uintptr_t)args->cmdbufs;
107 struct drm_tegra_reloc __user *relocs =
108 (void * __user)(uintptr_t)args->relocs;
109 struct drm_tegra_waitchk __user *waitchks =
110 (void * __user)(uintptr_t)args->waitchks;
111 struct drm_tegra_syncpt syncpt;
112 int err;
113
114 /* We don't yet support other than one syncpt_incr struct per submit */
115 if (args->num_syncpts != 1)
116 return -EINVAL;
117
118 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
119 args->num_relocs, args->num_waitchks);
120 if (!job)
121 return -ENOMEM;
122
123 job->num_relocs = args->num_relocs;
124 job->num_waitchk = args->num_waitchks;
125 job->client = (u32)args->context;
126 job->class = context->client->class;
127 job->serialize = true;
128
129 while (num_cmdbufs) {
130 struct drm_tegra_cmdbuf cmdbuf;
131 struct host1x_bo *bo;
132
133 err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf));
134 if (err)
135 goto fail;
136
137 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800138 if (!bo) {
139 err = -ENOENT;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200140 goto fail;
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800141 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200142
143 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
144 num_cmdbufs--;
145 cmdbufs++;
146 }
147
148 err = copy_from_user(job->relocarray, relocs,
149 sizeof(*relocs) * num_relocs);
150 if (err)
151 goto fail;
152
153 while (num_relocs--) {
154 struct host1x_reloc *reloc = &job->relocarray[num_relocs];
155 struct host1x_bo *cmdbuf, *target;
156
157 cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
158 target = host1x_bo_lookup(drm, file, (u32)reloc->target);
159
160 reloc->cmdbuf = cmdbuf;
161 reloc->target = target;
162
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800163 if (!reloc->target || !reloc->cmdbuf) {
164 err = -ENOENT;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200165 goto fail;
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800166 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200167 }
168
169 err = copy_from_user(job->waitchk, waitchks,
170 sizeof(*waitchks) * num_waitchks);
171 if (err)
172 goto fail;
173
174 err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts,
175 sizeof(syncpt));
176 if (err)
177 goto fail;
178
179 job->syncpt_id = syncpt.id;
180 job->syncpt_incrs = syncpt.incrs;
181 job->timeout = 10000;
182 job->is_addr_reg = gr2d_is_addr_reg;
183
184 if (args->timeout && args->timeout < 10000)
185 job->timeout = args->timeout;
186
187 err = host1x_job_pin(job, context->client->dev);
188 if (err)
189 goto fail;
190
191 err = host1x_job_submit(job);
192 if (err)
193 goto fail_submit;
194
195 args->fence = job->syncpt_end;
196
197 host1x_job_put(job);
198 return 0;
199
200fail_submit:
201 host1x_job_unpin(job);
202fail:
203 host1x_job_put(job);
204 return err;
205}
206
207static struct host1x_client_ops gr2d_client_ops = {
208 .drm_init = gr2d_client_init,
209 .drm_exit = gr2d_client_exit,
210 .open_channel = gr2d_open_channel,
211 .close_channel = gr2d_close_channel,
212 .submit = gr2d_submit,
213};
214
215static void gr2d_init_addr_reg_map(struct device *dev, struct gr2d *gr2d)
216{
217 const u32 gr2d_addr_regs[] = {0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31,
218 0x32, 0x48, 0x49, 0x4a, 0x4b, 0x4c};
219 unsigned long *bitmap;
220 int i;
221
222 bitmap = devm_kzalloc(dev, DIV_ROUND_UP(256, BITS_PER_BYTE),
223 GFP_KERNEL);
224
225 for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); ++i) {
226 u32 reg = gr2d_addr_regs[i];
227 bitmap[BIT_WORD(reg)] |= BIT_MASK(reg);
228 }
229
230 gr2d->addr_regs = bitmap;
231}
232
233static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 reg)
234{
235 struct gr2d *gr2d = dev_get_drvdata(dev);
236
237 switch (class) {
238 case HOST1X_CLASS_HOST1X:
239 return reg == 0x2b;
240 case HOST1X_CLASS_GR2D:
241 case HOST1X_CLASS_GR2D_SB:
242 reg &= 0xff;
243 if (gr2d->addr_regs[BIT_WORD(reg)] & BIT_MASK(reg))
244 return 1;
245 default:
246 return 0;
247 }
248}
249
250static const struct of_device_id gr2d_match[] = {
251 { .compatible = "nvidia,tegra30-gr2d" },
252 { .compatible = "nvidia,tegra20-gr2d" },
253 { },
254};
255
256static int gr2d_probe(struct platform_device *pdev)
257{
258 struct device *dev = &pdev->dev;
259 struct host1x_drm *host1x = host1x_get_drm_data(dev->parent);
260 int err;
261 struct gr2d *gr2d = NULL;
262 struct host1x_syncpt **syncpts;
263
264 gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
265 if (!gr2d)
266 return -ENOMEM;
267
268 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
269 if (!syncpts)
270 return -ENOMEM;
271
272 gr2d->clk = devm_clk_get(dev, NULL);
273 if (IS_ERR(gr2d->clk)) {
274 dev_err(dev, "cannot get clock\n");
275 return PTR_ERR(gr2d->clk);
276 }
277
278 err = clk_prepare_enable(gr2d->clk);
279 if (err) {
280 dev_err(dev, "cannot turn on clock\n");
281 return err;
282 }
283
284 gr2d->channel = host1x_channel_request(dev);
285 if (!gr2d->channel)
286 return -ENOMEM;
287
Arto Merilainenece66892013-05-29 13:26:07 +0300288 *syncpts = host1x_syncpt_request(dev, false);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200289 if (!(*syncpts)) {
290 host1x_channel_free(gr2d->channel);
291 return -ENOMEM;
292 }
293
294 gr2d->client.ops = &gr2d_client_ops;
295 gr2d->client.dev = dev;
296 gr2d->client.class = HOST1X_CLASS_GR2D;
297 gr2d->client.syncpts = syncpts;
298 gr2d->client.num_syncpts = 1;
299
300 err = host1x_register_client(host1x, &gr2d->client);
301 if (err < 0) {
302 dev_err(dev, "failed to register host1x client: %d\n", err);
303 return err;
304 }
305
306 gr2d_init_addr_reg_map(dev, gr2d);
307
308 platform_set_drvdata(pdev, gr2d);
309
310 return 0;
311}
312
313static int __exit gr2d_remove(struct platform_device *pdev)
314{
315 struct host1x_drm *host1x = host1x_get_drm_data(pdev->dev.parent);
316 struct gr2d *gr2d = platform_get_drvdata(pdev);
317 unsigned int i;
318 int err;
319
320 err = host1x_unregister_client(host1x, &gr2d->client);
321 if (err < 0) {
322 dev_err(&pdev->dev, "failed to unregister client: %d\n", err);
323 return err;
324 }
325
326 for (i = 0; i < gr2d->client.num_syncpts; i++)
327 host1x_syncpt_free(gr2d->client.syncpts[i]);
328
329 host1x_channel_free(gr2d->channel);
330 clk_disable_unprepare(gr2d->clk);
331
332 return 0;
333}
334
335struct platform_driver tegra_gr2d_driver = {
336 .probe = gr2d_probe,
337 .remove = __exit_p(gr2d_remove),
338 .driver = {
339 .owner = THIS_MODULE,
340 .name = "gr2d",
341 .of_match_table = gr2d_match,
342 }
343};