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>
24 struct tegra_drm_client client
;
25 struct host1x_channel
*channel
;
28 DECLARE_BITMAP(addr_regs
, GR2D_NUM_REGS
);
31 static inline struct gr2d
*to_gr2d(struct tegra_drm_client
*client
)
33 return container_of(client
, struct gr2d
, client
);
36 static int gr2d_init(struct host1x_client
*client
)
38 struct tegra_drm_client
*drm
= host1x_to_drm_client(client
);
39 struct tegra_drm
*tegra
= dev_get_drvdata(client
->parent
);
40 struct gr2d
*gr2d
= to_gr2d(drm
);
42 gr2d
->channel
= host1x_channel_request(client
->dev
);
46 client
->syncpts
[0] = host1x_syncpt_request(client
->dev
, false);
47 if (!client
->syncpts
[0]) {
48 host1x_channel_free(gr2d
->channel
);
52 return tegra_drm_register_client(tegra
, drm
);
55 static int gr2d_exit(struct host1x_client
*client
)
57 struct tegra_drm_client
*drm
= host1x_to_drm_client(client
);
58 struct tegra_drm
*tegra
= dev_get_drvdata(client
->parent
);
59 struct gr2d
*gr2d
= to_gr2d(drm
);
62 err
= tegra_drm_unregister_client(tegra
, drm
);
66 host1x_syncpt_free(client
->syncpts
[0]);
67 host1x_channel_free(gr2d
->channel
);
72 static const struct host1x_client_ops gr2d_client_ops
= {
77 static int gr2d_open_channel(struct tegra_drm_client
*client
,
78 struct tegra_drm_context
*context
)
80 struct gr2d
*gr2d
= to_gr2d(client
);
82 context
->channel
= host1x_channel_get(gr2d
->channel
);
83 if (!context
->channel
)
89 static void gr2d_close_channel(struct tegra_drm_context
*context
)
91 host1x_channel_put(context
->channel
);
94 static struct host1x_bo
*host1x_bo_lookup(struct drm_device
*drm
,
95 struct drm_file
*file
,
98 struct drm_gem_object
*gem
;
101 gem
= drm_gem_object_lookup(drm
, file
, handle
);
105 mutex_lock(&drm
->struct_mutex
);
106 drm_gem_object_unreference(gem
);
107 mutex_unlock(&drm
->struct_mutex
);
109 bo
= to_tegra_bo(gem
);
113 static int gr2d_is_addr_reg(struct device
*dev
, u32
class, u32 offset
)
115 struct gr2d
*gr2d
= dev_get_drvdata(dev
);
118 case HOST1X_CLASS_HOST1X
:
124 case HOST1X_CLASS_GR2D
:
125 case HOST1X_CLASS_GR2D_SB
:
126 if (offset
>= GR2D_NUM_REGS
)
129 if (test_bit(offset
, gr2d
->addr_regs
))
138 static int gr2d_submit(struct tegra_drm_context
*context
,
139 struct drm_tegra_submit
*args
, struct drm_device
*drm
,
140 struct drm_file
*file
)
142 unsigned int num_cmdbufs
= args
->num_cmdbufs
;
143 unsigned int num_relocs
= args
->num_relocs
;
144 unsigned int num_waitchks
= args
->num_waitchks
;
145 struct drm_tegra_cmdbuf __user
*cmdbufs
=
146 (void * __user
)(uintptr_t)args
->cmdbufs
;
147 struct drm_tegra_reloc __user
*relocs
=
148 (void * __user
)(uintptr_t)args
->relocs
;
149 struct drm_tegra_waitchk __user
*waitchks
=
150 (void * __user
)(uintptr_t)args
->waitchks
;
151 struct drm_tegra_syncpt syncpt
;
152 struct host1x_job
*job
;
155 /* We don't yet support other than one syncpt_incr struct per submit */
156 if (args
->num_syncpts
!= 1)
159 job
= host1x_job_alloc(context
->channel
, args
->num_cmdbufs
,
160 args
->num_relocs
, args
->num_waitchks
);
164 job
->num_relocs
= args
->num_relocs
;
165 job
->num_waitchk
= args
->num_waitchks
;
166 job
->client
= (u32
)args
->context
;
167 job
->class = context
->client
->base
.class;
168 job
->serialize
= true;
170 while (num_cmdbufs
) {
171 struct drm_tegra_cmdbuf cmdbuf
;
172 struct host1x_bo
*bo
;
174 err
= copy_from_user(&cmdbuf
, cmdbufs
, sizeof(cmdbuf
));
178 bo
= host1x_bo_lookup(drm
, file
, cmdbuf
.handle
);
184 host1x_job_add_gather(job
, bo
, cmdbuf
.words
, cmdbuf
.offset
);
189 err
= copy_from_user(job
->relocarray
, relocs
,
190 sizeof(*relocs
) * num_relocs
);
194 while (num_relocs
--) {
195 struct host1x_reloc
*reloc
= &job
->relocarray
[num_relocs
];
196 struct host1x_bo
*cmdbuf
, *target
;
198 cmdbuf
= host1x_bo_lookup(drm
, file
, (u32
)reloc
->cmdbuf
);
199 target
= host1x_bo_lookup(drm
, file
, (u32
)reloc
->target
);
201 reloc
->cmdbuf
= cmdbuf
;
202 reloc
->target
= target
;
204 if (!reloc
->target
|| !reloc
->cmdbuf
) {
210 err
= copy_from_user(job
->waitchk
, waitchks
,
211 sizeof(*waitchks
) * num_waitchks
);
215 err
= copy_from_user(&syncpt
, (void * __user
)(uintptr_t)args
->syncpts
,
220 job
->syncpt_id
= syncpt
.id
;
221 job
->syncpt_incrs
= syncpt
.incrs
;
222 job
->timeout
= 10000;
223 job
->is_addr_reg
= gr2d_is_addr_reg
;
225 if (args
->timeout
&& args
->timeout
< 10000)
226 job
->timeout
= args
->timeout
;
228 err
= host1x_job_pin(job
, context
->client
->base
.dev
);
232 err
= host1x_job_submit(job
);
236 args
->fence
= job
->syncpt_end
;
242 host1x_job_unpin(job
);
248 static const struct tegra_drm_client_ops gr2d_ops
= {
249 .open_channel
= gr2d_open_channel
,
250 .close_channel
= gr2d_close_channel
,
251 .submit
= gr2d_submit
,
254 static const struct of_device_id gr2d_match
[] = {
255 { .compatible
= "nvidia,tegra30-gr2d" },
256 { .compatible
= "nvidia,tegra20-gr2d" },
260 static const u32 gr2d_addr_regs
[] = {
269 GR2D_SRC_BASE_ADDR_SB
,
270 GR2D_DSTA_BASE_ADDR_SB
,
271 GR2D_DSTB_BASE_ADDR_SB
,
272 GR2D_UA_BASE_ADDR_SB
,
273 GR2D_VA_BASE_ADDR_SB
,
276 static int gr2d_probe(struct platform_device
*pdev
)
278 struct device
*dev
= &pdev
->dev
;
279 struct host1x_syncpt
**syncpts
;
284 gr2d
= devm_kzalloc(dev
, sizeof(*gr2d
), GFP_KERNEL
);
288 syncpts
= devm_kzalloc(dev
, sizeof(*syncpts
), GFP_KERNEL
);
292 gr2d
->clk
= devm_clk_get(dev
, NULL
);
293 if (IS_ERR(gr2d
->clk
)) {
294 dev_err(dev
, "cannot get clock\n");
295 return PTR_ERR(gr2d
->clk
);
298 err
= clk_prepare_enable(gr2d
->clk
);
300 dev_err(dev
, "cannot turn on clock\n");
304 INIT_LIST_HEAD(&gr2d
->client
.base
.list
);
305 gr2d
->client
.base
.ops
= &gr2d_client_ops
;
306 gr2d
->client
.base
.dev
= dev
;
307 gr2d
->client
.base
.class = HOST1X_CLASS_GR2D
;
308 gr2d
->client
.base
.syncpts
= syncpts
;
309 gr2d
->client
.base
.num_syncpts
= 1;
311 INIT_LIST_HEAD(&gr2d
->client
.list
);
312 gr2d
->client
.ops
= &gr2d_ops
;
314 err
= host1x_client_register(&gr2d
->client
.base
);
316 dev_err(dev
, "failed to register host1x client: %d\n", err
);
320 /* initialize address register map */
321 for (i
= 0; i
< ARRAY_SIZE(gr2d_addr_regs
); i
++)
322 set_bit(gr2d_addr_regs
[i
], gr2d
->addr_regs
);
324 platform_set_drvdata(pdev
, gr2d
);
329 static int gr2d_remove(struct platform_device
*pdev
)
331 struct gr2d
*gr2d
= platform_get_drvdata(pdev
);
334 err
= host1x_client_unregister(&gr2d
->client
.base
);
336 dev_err(&pdev
->dev
, "failed to unregister host1x client: %d\n",
341 clk_disable_unprepare(gr2d
->clk
);
346 struct platform_driver tegra_gr2d_driver
= {
348 .name
= "tegra-gr2d",
349 .of_match_table
= gr2d_match
,
352 .remove
= gr2d_remove
,