2 * Copyright (c) 2012-2013, NVIDIA Corporation.
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.
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
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/>.
17 #include <linux/clk.h>
23 #include "host1x_bo.h"
24 #include "host1x_client.h"
27 #define GR2D_NUM_REGS 0x4d
30 struct tegra_drm_client client;
31 struct host1x_channel *channel;
34 DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
37 static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
39 return container_of(client, struct gr2d, client);
42 static int gr2d_client_init(struct host1x_client *client)
47 static int gr2d_client_exit(struct host1x_client *client)
52 static const struct host1x_client_ops gr2d_client_ops = {
53 .init = gr2d_client_init,
54 .exit = gr2d_client_exit,
57 static int gr2d_open_channel(struct tegra_drm_client *client,
58 struct tegra_drm_context *context)
60 struct gr2d *gr2d = to_gr2d(client);
62 context->channel = host1x_channel_get(gr2d->channel);
63 if (!context->channel)
69 static void gr2d_close_channel(struct tegra_drm_context *context)
71 host1x_channel_put(context->channel);
74 static struct host1x_bo *host1x_bo_lookup(struct drm_device *drm,
75 struct drm_file *file,
78 struct drm_gem_object *gem;
81 gem = drm_gem_object_lookup(drm, file, handle);
85 mutex_lock(&drm->struct_mutex);
86 drm_gem_object_unreference(gem);
87 mutex_unlock(&drm->struct_mutex);
89 bo = to_tegra_bo(gem);
93 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
95 struct gr2d *gr2d = dev_get_drvdata(dev);
98 case HOST1X_CLASS_HOST1X:
104 case HOST1X_CLASS_GR2D:
105 case HOST1X_CLASS_GR2D_SB:
106 if (offset >= GR2D_NUM_REGS)
109 if (test_bit(offset, gr2d->addr_regs))
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)
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;
135 /* We don't yet support other than one syncpt_incr struct per submit */
136 if (args->num_syncpts != 1)
139 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
140 args->num_relocs, args->num_waitchks);
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;
150 while (num_cmdbufs) {
151 struct drm_tegra_cmdbuf cmdbuf;
152 struct host1x_bo *bo;
154 err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf));
158 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
164 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
169 err = copy_from_user(job->relocarray, relocs,
170 sizeof(*relocs) * num_relocs);
174 while (num_relocs--) {
175 struct host1x_reloc *reloc = &job->relocarray[num_relocs];
176 struct host1x_bo *cmdbuf, *target;
178 cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
179 target = host1x_bo_lookup(drm, file, (u32)reloc->target);
181 reloc->cmdbuf = cmdbuf;
182 reloc->target = target;
184 if (!reloc->target || !reloc->cmdbuf) {
190 err = copy_from_user(job->waitchk, waitchks,
191 sizeof(*waitchks) * num_waitchks);
195 err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts,
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;
205 if (args->timeout && args->timeout < 10000)
206 job->timeout = args->timeout;
208 err = host1x_job_pin(job, context->client->base.dev);
212 err = host1x_job_submit(job);
216 args->fence = job->syncpt_end;
222 host1x_job_unpin(job);
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,
234 static const struct of_device_id gr2d_match[] = {
235 { .compatible = "nvidia,tegra30-gr2d" },
236 { .compatible = "nvidia,tegra20-gr2d" },
240 static const u32 gr2d_addr_regs[] = {
241 0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31, 0x32,
242 0x48, 0x49, 0x4a, 0x4b, 0x4c
245 static int gr2d_probe(struct platform_device *pdev)
247 struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent);
248 struct device *dev = &pdev->dev;
249 struct host1x_syncpt **syncpts;
254 gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
258 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
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);
268 err = clk_prepare_enable(gr2d->clk);
270 dev_err(dev, "cannot turn on clock\n");
274 gr2d->channel = host1x_channel_request(dev);
278 *syncpts = host1x_syncpt_request(dev, false);
280 host1x_channel_free(gr2d->channel);
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;
292 err = host1x_register_client(tegra, &gr2d->client.base);
294 dev_err(dev, "failed to register host1x client: %d\n", err);
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);
302 platform_set_drvdata(pdev, gr2d);
307 static int gr2d_remove(struct platform_device *pdev)
309 struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent);
310 struct gr2d *gr2d = platform_get_drvdata(pdev);
314 err = host1x_unregister_client(tegra, &gr2d->client.base);
316 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
321 for (i = 0; i < gr2d->client.base.num_syncpts; i++)
322 host1x_syncpt_free(gr2d->client.base.syncpts[i]);
324 host1x_channel_free(gr2d->channel);
325 clk_disable_unprepare(gr2d->clk);
330 struct platform_driver tegra_gr2d_driver = {
332 .name = "tegra-gr2d",
333 .of_match_table = gr2d_match,
336 .remove = gr2d_remove,