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