blob: a7edc794c5ceba77f8903f71b4b31cc17ca87996 [file] [log] [blame]
Terje Bergstromd43f81c2013-03-22 16:34:09 +02001/*
Terje Bergstromd43f81c2013-03-22 16:34:09 +02002 * 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
Terje Bergstromd43f81c2013-03-22 16:34:09 +020017#include <linux/clk.h>
18
19#include "channel.h"
20#include "drm.h"
21#include "gem.h"
22#include "job.h"
Terje Bergstromd43f81c2013-03-22 16:34:09 +020023#include "host1x_bo.h"
24#include "host1x_client.h"
25#include "syncpt.h"
26
Thierry Redingc1bef812013-09-26 16:09:43 +020027#define GR2D_NUM_REGS 0x4d
28
Terje Bergstromd43f81c2013-03-22 16:34:09 +020029struct gr2d {
Thierry Reding53fa7f72013-09-24 15:35:40 +020030 struct tegra_drm_client client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +020031 struct host1x_channel *channel;
Thierry Redingc1bef812013-09-26 16:09:43 +020032 struct clk *clk;
33
34 DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
Terje Bergstromd43f81c2013-03-22 16:34:09 +020035};
36
Thierry Reding53fa7f72013-09-24 15:35:40 +020037static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
Terje Bergstromd43f81c2013-03-22 16:34:09 +020038{
39 return container_of(client, struct gr2d, client);
40}
41
Thierry Reding53fa7f72013-09-24 15:35:40 +020042static int gr2d_client_init(struct host1x_client *client)
Terje Bergstromd43f81c2013-03-22 16:34:09 +020043{
44 return 0;
45}
46
47static int gr2d_client_exit(struct host1x_client *client)
48{
49 return 0;
50}
51
Thierry Reding53fa7f72013-09-24 15:35:40 +020052static const struct host1x_client_ops gr2d_client_ops = {
53 .init = gr2d_client_init,
54 .exit = gr2d_client_exit,
55};
56
57static int gr2d_open_channel(struct tegra_drm_client *client,
Thierry Redingc88c3632013-09-26 16:08:22 +020058 struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +020059{
60 struct gr2d *gr2d = to_gr2d(client);
61
62 context->channel = host1x_channel_get(gr2d->channel);
Terje Bergstromd43f81c2013-03-22 16:34:09 +020063 if (!context->channel)
64 return -ENOMEM;
65
66 return 0;
67}
68
Thierry Redingc88c3632013-09-26 16:08:22 +020069static void gr2d_close_channel(struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +020070{
71 host1x_channel_put(context->channel);
72}
73
74static 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)
Thierry Redingdc618db2013-05-19 14:21:22 +020083 return NULL;
Terje Bergstromd43f81c2013-03-22 16:34:09 +020084
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
Thierry Redingc1bef812013-09-26 16:09:43 +020093static 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
Thierry Redingc88c3632013-09-26 16:08:22 +0200118static int gr2d_submit(struct tegra_drm_context *context,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200119 struct drm_tegra_submit *args, struct drm_device *drm,
120 struct drm_file *file)
121{
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200122 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;
Thierry Redingc1bef812013-09-26 16:09:43 +0200132 struct host1x_job *job;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200133 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;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200147 job->class = context->client->base.class;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200148 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);
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800159 if (!bo) {
160 err = -ENOENT;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200161 goto fail;
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800162 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200163
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
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800184 if (!reloc->target || !reloc->cmdbuf) {
185 err = -ENOENT;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200186 goto fail;
Wei Yongjun2639f2b2013-04-24 14:50:51 +0800187 }
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200188 }
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
Thierry Reding53fa7f72013-09-24 15:35:40 +0200208 err = host1x_job_pin(job, context->client->base.dev);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200209 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
221fail_submit:
222 host1x_job_unpin(job);
223fail:
224 host1x_job_put(job);
225 return err;
226}
227
Thierry Reding53fa7f72013-09-24 15:35:40 +0200228static const struct tegra_drm_client_ops gr2d_ops = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200229 .open_channel = gr2d_open_channel,
230 .close_channel = gr2d_close_channel,
231 .submit = gr2d_submit,
232};
233
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200234static const struct of_device_id gr2d_match[] = {
235 { .compatible = "nvidia,tegra30-gr2d" },
236 { .compatible = "nvidia,tegra20-gr2d" },
237 { },
238};
239
Thierry Redingc1bef812013-09-26 16:09:43 +0200240static const u32 gr2d_addr_regs[] = {
241 0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31, 0x32,
242 0x48, 0x49, 0x4a, 0x4b, 0x4c
243};
244
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200245static int gr2d_probe(struct platform_device *pdev)
246{
Thierry Redingc1bef812013-09-26 16:09:43 +0200247 struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200248 struct device *dev = &pdev->dev;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200249 struct host1x_syncpt **syncpts;
Thierry Redingc1bef812013-09-26 16:09:43 +0200250 struct gr2d *gr2d;
251 unsigned int i;
252 int err;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200253
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
Arto Merilainenece66892013-05-29 13:26:07 +0300278 *syncpts = host1x_syncpt_request(dev, false);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200279 if (!(*syncpts)) {
280 host1x_channel_free(gr2d->channel);
281 return -ENOMEM;
282 }
283
Thierry Reding53fa7f72013-09-24 15:35:40 +0200284 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;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200291
Thierry Reding53fa7f72013-09-24 15:35:40 +0200292 err = host1x_register_client(tegra, &gr2d->client.base);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200293 if (err < 0) {
294 dev_err(dev, "failed to register host1x client: %d\n", err);
295 return err;
296 }
297
Thierry Redingc1bef812013-09-26 16:09:43 +0200298 /* 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);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200301
302 platform_set_drvdata(pdev, gr2d);
303
304 return 0;
305}
306
Thierry Redingc1bef812013-09-26 16:09:43 +0200307static int gr2d_remove(struct platform_device *pdev)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200308{
Thierry Reding386a2a72013-09-24 13:22:17 +0200309 struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200310 struct gr2d *gr2d = platform_get_drvdata(pdev);
311 unsigned int i;
312 int err;
313
Thierry Reding53fa7f72013-09-24 15:35:40 +0200314 err = host1x_unregister_client(tegra, &gr2d->client.base);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200315 if (err < 0) {
Thierry Redingc1bef812013-09-26 16:09:43 +0200316 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
317 err);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200318 return err;
319 }
320
Thierry Reding53fa7f72013-09-24 15:35:40 +0200321 for (i = 0; i < gr2d->client.base.num_syncpts; i++)
322 host1x_syncpt_free(gr2d->client.base.syncpts[i]);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200323
324 host1x_channel_free(gr2d->channel);
325 clk_disable_unprepare(gr2d->clk);
326
327 return 0;
328}
329
330struct platform_driver tegra_gr2d_driver = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200331 .driver = {
Thierry Redinga137ce32013-10-14 14:44:54 +0200332 .name = "tegra-gr2d",
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200333 .of_match_table = gr2d_match,
Thierry Redingc1bef812013-09-26 16:09:43 +0200334 },
335 .probe = gr2d_probe,
336 .remove = gr2d_remove,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200337};