2 * drivers/video/tegra/host/gr2d/gr2d.c
6 * Copyright (c) 2012-2013, NVIDIA Corporation.
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.
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
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/>.
21 #include <linux/export.h>
23 #include <linux/of_device.h>
24 #include <linux/clk.h>
31 #include "host1x_bo.h"
32 #include "host1x_client.h"
36 struct host1x_client client;
38 struct host1x_channel *channel;
39 unsigned long *addr_regs;
42 static inline struct gr2d *to_gr2d(struct host1x_client *client)
44 return container_of(client, struct gr2d, client);
47 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 reg);
49 static int gr2d_client_init(struct host1x_client *client,
50 struct drm_device *drm)
55 static int gr2d_client_exit(struct host1x_client *client)
60 static int gr2d_open_channel(struct host1x_client *client,
61 struct host1x_drm_context *context)
63 struct gr2d *gr2d = to_gr2d(client);
65 context->channel = host1x_channel_get(gr2d->channel);
67 if (!context->channel)
73 static void gr2d_close_channel(struct host1x_drm_context *context)
75 host1x_channel_put(context->channel);
78 static struct host1x_bo *host1x_bo_lookup(struct drm_device *drm,
79 struct drm_file *file,
82 struct drm_gem_object *gem;
85 gem = drm_gem_object_lookup(drm, file, handle);
89 mutex_lock(&drm->struct_mutex);
90 drm_gem_object_unreference(gem);
91 mutex_unlock(&drm->struct_mutex);
93 bo = to_tegra_bo(gem);
97 static int gr2d_submit(struct host1x_drm_context *context,
98 struct drm_tegra_submit *args, struct drm_device *drm,
99 struct drm_file *file)
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;
114 /* We don't yet support other than one syncpt_incr struct per submit */
115 if (args->num_syncpts != 1)
118 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
119 args->num_relocs, args->num_waitchks);
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;
129 while (num_cmdbufs) {
130 struct drm_tegra_cmdbuf cmdbuf;
131 struct host1x_bo *bo;
133 err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf));
137 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
141 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
146 err = copy_from_user(job->relocarray, relocs,
147 sizeof(*relocs) * num_relocs);
151 while (num_relocs--) {
152 struct host1x_reloc *reloc = &job->relocarray[num_relocs];
153 struct host1x_bo *cmdbuf, *target;
155 cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
156 target = host1x_bo_lookup(drm, file, (u32)reloc->target);
158 reloc->cmdbuf = cmdbuf;
159 reloc->target = target;
161 if (!reloc->target || !reloc->cmdbuf)
165 err = copy_from_user(job->waitchk, waitchks,
166 sizeof(*waitchks) * num_waitchks);
170 err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts,
175 job->syncpt_id = syncpt.id;
176 job->syncpt_incrs = syncpt.incrs;
177 job->timeout = 10000;
178 job->is_addr_reg = gr2d_is_addr_reg;
180 if (args->timeout && args->timeout < 10000)
181 job->timeout = args->timeout;
183 err = host1x_job_pin(job, context->client->dev);
187 err = host1x_job_submit(job);
191 args->fence = job->syncpt_end;
197 host1x_job_unpin(job);
203 static struct host1x_client_ops gr2d_client_ops = {
204 .drm_init = gr2d_client_init,
205 .drm_exit = gr2d_client_exit,
206 .open_channel = gr2d_open_channel,
207 .close_channel = gr2d_close_channel,
208 .submit = gr2d_submit,
211 static void gr2d_init_addr_reg_map(struct device *dev, struct gr2d *gr2d)
213 const u32 gr2d_addr_regs[] = {0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31,
214 0x32, 0x48, 0x49, 0x4a, 0x4b, 0x4c};
215 unsigned long *bitmap;
218 bitmap = devm_kzalloc(dev, DIV_ROUND_UP(256, BITS_PER_BYTE),
221 for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); ++i) {
222 u32 reg = gr2d_addr_regs[i];
223 bitmap[BIT_WORD(reg)] |= BIT_MASK(reg);
226 gr2d->addr_regs = bitmap;
229 static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 reg)
231 struct gr2d *gr2d = dev_get_drvdata(dev);
234 case HOST1X_CLASS_HOST1X:
236 case HOST1X_CLASS_GR2D:
237 case HOST1X_CLASS_GR2D_SB:
239 if (gr2d->addr_regs[BIT_WORD(reg)] & BIT_MASK(reg))
246 static const struct of_device_id gr2d_match[] = {
247 { .compatible = "nvidia,tegra30-gr2d" },
248 { .compatible = "nvidia,tegra20-gr2d" },
252 static int gr2d_probe(struct platform_device *pdev)
254 struct device *dev = &pdev->dev;
255 struct host1x_drm *host1x = host1x_get_drm_data(dev->parent);
257 struct gr2d *gr2d = NULL;
258 struct host1x_syncpt **syncpts;
260 gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
264 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
268 gr2d->clk = devm_clk_get(dev, NULL);
269 if (IS_ERR(gr2d->clk)) {
270 dev_err(dev, "cannot get clock\n");
271 return PTR_ERR(gr2d->clk);
274 err = clk_prepare_enable(gr2d->clk);
276 dev_err(dev, "cannot turn on clock\n");
280 gr2d->channel = host1x_channel_request(dev);
284 *syncpts = host1x_syncpt_request(dev, 0);
286 host1x_channel_free(gr2d->channel);
290 gr2d->client.ops = &gr2d_client_ops;
291 gr2d->client.dev = dev;
292 gr2d->client.class = HOST1X_CLASS_GR2D;
293 gr2d->client.syncpts = syncpts;
294 gr2d->client.num_syncpts = 1;
296 err = host1x_register_client(host1x, &gr2d->client);
298 dev_err(dev, "failed to register host1x client: %d\n", err);
302 gr2d_init_addr_reg_map(dev, gr2d);
304 platform_set_drvdata(pdev, gr2d);
309 static int __exit gr2d_remove(struct platform_device *pdev)
311 struct host1x_drm *host1x = host1x_get_drm_data(pdev->dev.parent);
312 struct gr2d *gr2d = platform_get_drvdata(pdev);
316 err = host1x_unregister_client(host1x, &gr2d->client);
318 dev_err(&pdev->dev, "failed to unregister client: %d\n", err);
322 for (i = 0; i < gr2d->client.num_syncpts; i++)
323 host1x_syncpt_free(gr2d->client.syncpts[i]);
325 host1x_channel_free(gr2d->channel);
326 clk_disable_unprepare(gr2d->clk);
331 struct platform_driver tegra_gr2d_driver = {
333 .remove = __exit_p(gr2d_remove),
335 .owner = THIS_MODULE,
337 .of_match_table = gr2d_match,