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