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