blob: 88a529008ce0e59210e917422b2cfe07570a0d7d [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
2 * Copyright (C) 2012 Avionic Design GmbH
Terje Bergstromd43f81c2013-03-22 16:34:09 +02003 * Copyright (C) 2012-2013 NVIDIA CORPORATION. All rights reserved.
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
Thierry Reding776dc382013-10-14 14:43:22 +020010#include <linux/host1x.h>
11
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000012#include "drm.h"
Arto Merilainende2ba662013-03-22 16:34:08 +020013#include "gem.h"
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000014
15#define DRIVER_NAME "tegra"
16#define DRIVER_DESC "NVIDIA Tegra graphics"
17#define DRIVER_DATE "20120330"
18#define DRIVER_MAJOR 0
19#define DRIVER_MINOR 0
20#define DRIVER_PATCHLEVEL 0
21
Thierry Reding08943e62013-09-26 16:08:18 +020022struct tegra_drm_file {
23 struct list_head contexts;
24};
25
Thierry Reding776dc382013-10-14 14:43:22 +020026static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000027{
Thierry Reding776dc382013-10-14 14:43:22 +020028 struct host1x_device *device = to_host1x_device(drm->dev);
Thierry Reding386a2a72013-09-24 13:22:17 +020029 struct tegra_drm *tegra;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000030 int err;
31
Thierry Reding776dc382013-10-14 14:43:22 +020032 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
Thierry Reding386a2a72013-09-24 13:22:17 +020033 if (!tegra)
Terje Bergstrom692e6d72013-03-22 16:34:07 +020034 return -ENOMEM;
35
Thierry Reding776dc382013-10-14 14:43:22 +020036 dev_set_drvdata(drm->dev, tegra);
Thierry Reding386a2a72013-09-24 13:22:17 +020037 mutex_init(&tegra->clients_lock);
38 INIT_LIST_HEAD(&tegra->clients);
Thierry Reding386a2a72013-09-24 13:22:17 +020039 drm->dev_private = tegra;
40 tegra->drm = drm;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000041
42 drm_mode_config_init(drm);
43
Thierry Reding776dc382013-10-14 14:43:22 +020044 err = host1x_device_init(device);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000045 if (err < 0)
46 return err;
47
Thierry Reding603f0cc2013-04-22 21:22:14 +020048 /*
49 * We don't use the drm_irq_install() helpers provided by the DRM
50 * core, so we need to set this manually in order to allow the
51 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
52 */
Ville Syrjälä44238432013-10-04 14:53:37 +030053 drm->irq_enabled = true;
Thierry Reding603f0cc2013-04-22 21:22:14 +020054
Thierry Reding6e5ff992012-11-28 11:45:47 +010055 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
56 if (err < 0)
57 return err;
58
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000059 err = tegra_drm_fb_init(drm);
60 if (err < 0)
61 return err;
62
63 drm_kms_helper_poll_init(drm);
64
65 return 0;
66}
67
68static int tegra_drm_unload(struct drm_device *drm)
69{
Thierry Reding776dc382013-10-14 14:43:22 +020070 struct host1x_device *device = to_host1x_device(drm->dev);
71 int err;
72
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000073 drm_kms_helper_poll_fini(drm);
74 tegra_drm_fb_exit(drm);
Thierry Redingf002abc2013-10-14 14:06:02 +020075 drm_vblank_cleanup(drm);
76 drm_mode_config_cleanup(drm);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000077
Thierry Reding776dc382013-10-14 14:43:22 +020078 err = host1x_device_exit(device);
79 if (err < 0)
80 return err;
81
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000082 return 0;
83}
84
85static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
86{
Thierry Reding08943e62013-09-26 16:08:18 +020087 struct tegra_drm_file *fpriv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +020088
89 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
90 if (!fpriv)
91 return -ENOMEM;
92
93 INIT_LIST_HEAD(&fpriv->contexts);
94 filp->driver_priv = fpriv;
95
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000096 return 0;
97}
98
Thierry Redingc88c3632013-09-26 16:08:22 +020099static void tegra_drm_context_free(struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200100{
101 context->client->ops->close_channel(context);
102 kfree(context);
103}
104
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000105static void tegra_drm_lastclose(struct drm_device *drm)
106{
Thierry Reding60c2f702013-10-31 13:28:50 +0100107#ifdef CONFIG_TEGRA_DRM_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200108 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000109
Thierry Reding386a2a72013-09-24 13:22:17 +0200110 tegra_fbdev_restore_mode(tegra->fbdev);
Thierry Reding60c2f702013-10-31 13:28:50 +0100111#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000112}
113
Thierry Redingc40f0f12013-10-10 11:00:33 +0200114static struct host1x_bo *
115host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle)
116{
117 struct drm_gem_object *gem;
118 struct tegra_bo *bo;
119
120 gem = drm_gem_object_lookup(drm, file, handle);
121 if (!gem)
122 return NULL;
123
124 mutex_lock(&drm->struct_mutex);
125 drm_gem_object_unreference(gem);
126 mutex_unlock(&drm->struct_mutex);
127
128 bo = to_tegra_bo(gem);
129 return &bo->base;
130}
131
132int tegra_drm_submit(struct tegra_drm_context *context,
133 struct drm_tegra_submit *args, struct drm_device *drm,
134 struct drm_file *file)
135{
136 unsigned int num_cmdbufs = args->num_cmdbufs;
137 unsigned int num_relocs = args->num_relocs;
138 unsigned int num_waitchks = args->num_waitchks;
139 struct drm_tegra_cmdbuf __user *cmdbufs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100140 (void __user *)(uintptr_t)args->cmdbufs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200141 struct drm_tegra_reloc __user *relocs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100142 (void __user *)(uintptr_t)args->relocs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200143 struct drm_tegra_waitchk __user *waitchks =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100144 (void __user *)(uintptr_t)args->waitchks;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200145 struct drm_tegra_syncpt syncpt;
146 struct host1x_job *job;
147 int err;
148
149 /* We don't yet support other than one syncpt_incr struct per submit */
150 if (args->num_syncpts != 1)
151 return -EINVAL;
152
153 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
154 args->num_relocs, args->num_waitchks);
155 if (!job)
156 return -ENOMEM;
157
158 job->num_relocs = args->num_relocs;
159 job->num_waitchk = args->num_waitchks;
160 job->client = (u32)args->context;
161 job->class = context->client->base.class;
162 job->serialize = true;
163
164 while (num_cmdbufs) {
165 struct drm_tegra_cmdbuf cmdbuf;
166 struct host1x_bo *bo;
167
Dan Carpenter9a991602013-11-08 13:07:37 +0300168 if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
169 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200170 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300171 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200172
173 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
174 if (!bo) {
175 err = -ENOENT;
176 goto fail;
177 }
178
179 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
180 num_cmdbufs--;
181 cmdbufs++;
182 }
183
Dan Carpenter9a991602013-11-08 13:07:37 +0300184 if (copy_from_user(job->relocarray, relocs,
185 sizeof(*relocs) * num_relocs)) {
186 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200187 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300188 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200189
190 while (num_relocs--) {
191 struct host1x_reloc *reloc = &job->relocarray[num_relocs];
192 struct host1x_bo *cmdbuf, *target;
193
194 cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
195 target = host1x_bo_lookup(drm, file, (u32)reloc->target);
196
197 reloc->cmdbuf = cmdbuf;
198 reloc->target = target;
199
200 if (!reloc->target || !reloc->cmdbuf) {
201 err = -ENOENT;
202 goto fail;
203 }
204 }
205
Dan Carpenter9a991602013-11-08 13:07:37 +0300206 if (copy_from_user(job->waitchk, waitchks,
207 sizeof(*waitchks) * num_waitchks)) {
208 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200209 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300210 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200211
Dan Carpenter9a991602013-11-08 13:07:37 +0300212 if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
213 sizeof(syncpt))) {
214 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200215 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300216 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200217
218 job->is_addr_reg = context->client->ops->is_addr_reg;
219 job->syncpt_incrs = syncpt.incrs;
220 job->syncpt_id = syncpt.id;
221 job->timeout = 10000;
222
223 if (args->timeout && args->timeout < 10000)
224 job->timeout = args->timeout;
225
226 err = host1x_job_pin(job, context->client->base.dev);
227 if (err)
228 goto fail;
229
230 err = host1x_job_submit(job);
231 if (err)
232 goto fail_submit;
233
234 args->fence = job->syncpt_end;
235
236 host1x_job_put(job);
237 return 0;
238
239fail_submit:
240 host1x_job_unpin(job);
241fail:
242 host1x_job_put(job);
243 return err;
244}
245
246
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200247#ifdef CONFIG_DRM_TEGRA_STAGING
Thierry Redingc88c3632013-09-26 16:08:22 +0200248static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200249{
Thierry Redingc88c3632013-09-26 16:08:22 +0200250 return (struct tegra_drm_context *)(uintptr_t)context;
251}
252
253static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
254 struct tegra_drm_context *context)
255{
256 struct tegra_drm_context *ctx;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200257
258 list_for_each_entry(ctx, &file->contexts, list)
259 if (ctx == context)
260 return true;
261
262 return false;
263}
264
265static int tegra_gem_create(struct drm_device *drm, void *data,
266 struct drm_file *file)
267{
268 struct drm_tegra_gem_create *args = data;
269 struct tegra_bo *bo;
270
Thierry Reding773af772013-10-04 22:34:01 +0200271 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200272 &args->handle);
273 if (IS_ERR(bo))
274 return PTR_ERR(bo);
275
276 return 0;
277}
278
279static int tegra_gem_mmap(struct drm_device *drm, void *data,
280 struct drm_file *file)
281{
282 struct drm_tegra_gem_mmap *args = data;
283 struct drm_gem_object *gem;
284 struct tegra_bo *bo;
285
286 gem = drm_gem_object_lookup(drm, file, args->handle);
287 if (!gem)
288 return -EINVAL;
289
290 bo = to_tegra_bo(gem);
291
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200292 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200293
294 drm_gem_object_unreference(gem);
295
296 return 0;
297}
298
299static int tegra_syncpt_read(struct drm_device *drm, void *data,
300 struct drm_file *file)
301{
Thierry Reding776dc382013-10-14 14:43:22 +0200302 struct host1x *host = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200303 struct drm_tegra_syncpt_read *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200304 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200305
Thierry Reding776dc382013-10-14 14:43:22 +0200306 sp = host1x_syncpt_get(host, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200307 if (!sp)
308 return -EINVAL;
309
310 args->value = host1x_syncpt_read_min(sp);
311 return 0;
312}
313
314static int tegra_syncpt_incr(struct drm_device *drm, void *data,
315 struct drm_file *file)
316{
Thierry Reding776dc382013-10-14 14:43:22 +0200317 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200318 struct drm_tegra_syncpt_incr *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200319 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200320
Thierry Reding776dc382013-10-14 14:43:22 +0200321 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200322 if (!sp)
323 return -EINVAL;
324
Arto Merilainenebae30b2013-05-29 13:26:08 +0300325 return host1x_syncpt_incr(sp);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200326}
327
328static int tegra_syncpt_wait(struct drm_device *drm, void *data,
329 struct drm_file *file)
330{
Thierry Reding776dc382013-10-14 14:43:22 +0200331 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200332 struct drm_tegra_syncpt_wait *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200333 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200334
Thierry Reding776dc382013-10-14 14:43:22 +0200335 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200336 if (!sp)
337 return -EINVAL;
338
339 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
340 &args->value);
341}
342
343static int tegra_open_channel(struct drm_device *drm, void *data,
344 struct drm_file *file)
345{
Thierry Reding08943e62013-09-26 16:08:18 +0200346 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding386a2a72013-09-24 13:22:17 +0200347 struct tegra_drm *tegra = drm->dev_private;
348 struct drm_tegra_open_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200349 struct tegra_drm_context *context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200350 struct tegra_drm_client *client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200351 int err = -ENODEV;
352
353 context = kzalloc(sizeof(*context), GFP_KERNEL);
354 if (!context)
355 return -ENOMEM;
356
Thierry Reding776dc382013-10-14 14:43:22 +0200357 list_for_each_entry(client, &tegra->clients, list)
Thierry Reding53fa7f72013-09-24 15:35:40 +0200358 if (client->base.class == args->client) {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200359 err = client->ops->open_channel(client, context);
360 if (err)
361 break;
362
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200363 list_add(&context->list, &fpriv->contexts);
364 args->context = (uintptr_t)context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200365 context->client = client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200366 return 0;
367 }
368
369 kfree(context);
370 return err;
371}
372
373static int tegra_close_channel(struct drm_device *drm, void *data,
374 struct drm_file *file)
375{
Thierry Reding08943e62013-09-26 16:08:18 +0200376 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding776dc382013-10-14 14:43:22 +0200377 struct drm_tegra_close_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200378 struct tegra_drm_context *context;
379
380 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200381
Thierry Reding08943e62013-09-26 16:08:18 +0200382 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200383 return -EINVAL;
384
385 list_del(&context->list);
Thierry Redingc88c3632013-09-26 16:08:22 +0200386 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200387
388 return 0;
389}
390
391static int tegra_get_syncpt(struct drm_device *drm, void *data,
392 struct drm_file *file)
393{
Thierry Reding08943e62013-09-26 16:08:18 +0200394 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200395 struct drm_tegra_get_syncpt *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200396 struct tegra_drm_context *context;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200397 struct host1x_syncpt *syncpt;
398
Thierry Redingc88c3632013-09-26 16:08:22 +0200399 context = tegra_drm_get_context(args->context);
400
Thierry Reding08943e62013-09-26 16:08:18 +0200401 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200402 return -ENODEV;
403
Thierry Reding53fa7f72013-09-24 15:35:40 +0200404 if (args->index >= context->client->base.num_syncpts)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200405 return -EINVAL;
406
Thierry Reding53fa7f72013-09-24 15:35:40 +0200407 syncpt = context->client->base.syncpts[args->index];
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200408 args->id = host1x_syncpt_id(syncpt);
409
410 return 0;
411}
412
413static int tegra_submit(struct drm_device *drm, void *data,
414 struct drm_file *file)
415{
Thierry Reding08943e62013-09-26 16:08:18 +0200416 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200417 struct drm_tegra_submit *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200418 struct tegra_drm_context *context;
419
420 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200421
Thierry Reding08943e62013-09-26 16:08:18 +0200422 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200423 return -ENODEV;
424
425 return context->client->ops->submit(context, args, drm, file);
426}
Arto Merilainenc54a1692013-10-14 15:21:54 +0300427
428static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
429 struct drm_file *file)
430{
431 struct tegra_drm_file *fpriv = file->driver_priv;
432 struct drm_tegra_get_syncpt_base *args = data;
433 struct tegra_drm_context *context;
434 struct host1x_syncpt_base *base;
435 struct host1x_syncpt *syncpt;
436
437 context = tegra_drm_get_context(args->context);
438
439 if (!tegra_drm_file_owns_context(fpriv, context))
440 return -ENODEV;
441
442 if (args->syncpt >= context->client->base.num_syncpts)
443 return -EINVAL;
444
445 syncpt = context->client->base.syncpts[args->syncpt];
446
447 base = host1x_syncpt_get_base(syncpt);
448 if (!base)
449 return -ENXIO;
450
451 args->id = host1x_syncpt_base_id(base);
452
453 return 0;
454}
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200455#endif
456
Rob Clarkbaa70942013-08-02 13:27:49 -0400457static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200458#ifdef CONFIG_DRM_TEGRA_STAGING
459 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
460 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
461 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
462 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
463 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
464 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
465 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
466 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
467 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
Arto Merilainenc54a1692013-10-14 15:21:54 +0300468 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, DRM_UNLOCKED),
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200469#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000470};
471
472static const struct file_operations tegra_drm_fops = {
473 .owner = THIS_MODULE,
474 .open = drm_open,
475 .release = drm_release,
476 .unlocked_ioctl = drm_ioctl,
Arto Merilainende2ba662013-03-22 16:34:08 +0200477 .mmap = tegra_drm_mmap,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000478 .poll = drm_poll,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000479 .read = drm_read,
480#ifdef CONFIG_COMPAT
481 .compat_ioctl = drm_compat_ioctl,
482#endif
483 .llseek = noop_llseek,
484};
485
Thierry Reding6e5ff992012-11-28 11:45:47 +0100486static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
487{
488 struct drm_crtc *crtc;
489
490 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
491 struct tegra_dc *dc = to_tegra_dc(crtc);
492
493 if (dc->pipe == pipe)
494 return crtc;
495 }
496
497 return NULL;
498}
499
500static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
501{
502 /* TODO: implement real hardware counter using syncpoints */
503 return drm_vblank_count(dev, crtc);
504}
505
506static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
507{
508 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
509 struct tegra_dc *dc = to_tegra_dc(crtc);
510
511 if (!crtc)
512 return -ENODEV;
513
514 tegra_dc_enable_vblank(dc);
515
516 return 0;
517}
518
519static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
520{
521 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
522 struct tegra_dc *dc = to_tegra_dc(crtc);
523
524 if (crtc)
525 tegra_dc_disable_vblank(dc);
526}
527
Thierry Reding3c03c462012-11-28 12:00:18 +0100528static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
529{
Thierry Reding08943e62013-09-26 16:08:18 +0200530 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Redingc88c3632013-09-26 16:08:22 +0200531 struct tegra_drm_context *context, *tmp;
Thierry Reding3c03c462012-11-28 12:00:18 +0100532 struct drm_crtc *crtc;
533
534 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
535 tegra_dc_cancel_page_flip(crtc, file);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200536
537 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
Thierry Redingc88c3632013-09-26 16:08:22 +0200538 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200539
540 kfree(fpriv);
Thierry Reding3c03c462012-11-28 12:00:18 +0100541}
542
Thierry Redinge450fcc2013-02-13 16:13:16 +0100543#ifdef CONFIG_DEBUG_FS
544static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
545{
546 struct drm_info_node *node = (struct drm_info_node *)s->private;
547 struct drm_device *drm = node->minor->dev;
548 struct drm_framebuffer *fb;
549
550 mutex_lock(&drm->mode_config.fb_lock);
551
552 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
553 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
554 fb->base.id, fb->width, fb->height, fb->depth,
555 fb->bits_per_pixel,
556 atomic_read(&fb->refcount.refcount));
557 }
558
559 mutex_unlock(&drm->mode_config.fb_lock);
560
561 return 0;
562}
563
564static struct drm_info_list tegra_debugfs_list[] = {
565 { "framebuffers", tegra_debugfs_framebuffers, 0 },
566};
567
568static int tegra_debugfs_init(struct drm_minor *minor)
569{
570 return drm_debugfs_create_files(tegra_debugfs_list,
571 ARRAY_SIZE(tegra_debugfs_list),
572 minor->debugfs_root, minor);
573}
574
575static void tegra_debugfs_cleanup(struct drm_minor *minor)
576{
577 drm_debugfs_remove_files(tegra_debugfs_list,
578 ARRAY_SIZE(tegra_debugfs_list), minor);
579}
580#endif
581
Thierry Reding9b57f5f2013-11-08 13:17:14 +0100582static struct drm_driver tegra_drm_driver = {
Thierry Reding38003912013-12-12 10:00:43 +0100583 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000584 .load = tegra_drm_load,
585 .unload = tegra_drm_unload,
586 .open = tegra_drm_open,
Thierry Reding3c03c462012-11-28 12:00:18 +0100587 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000588 .lastclose = tegra_drm_lastclose,
589
Thierry Reding6e5ff992012-11-28 11:45:47 +0100590 .get_vblank_counter = tegra_drm_get_vblank_counter,
591 .enable_vblank = tegra_drm_enable_vblank,
592 .disable_vblank = tegra_drm_disable_vblank,
593
Thierry Redinge450fcc2013-02-13 16:13:16 +0100594#if defined(CONFIG_DEBUG_FS)
595 .debugfs_init = tegra_debugfs_init,
596 .debugfs_cleanup = tegra_debugfs_cleanup,
597#endif
598
Arto Merilainende2ba662013-03-22 16:34:08 +0200599 .gem_free_object = tegra_bo_free_object,
600 .gem_vm_ops = &tegra_bo_vm_ops,
Thierry Reding38003912013-12-12 10:00:43 +0100601
602 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
603 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
604 .gem_prime_export = tegra_gem_prime_export,
605 .gem_prime_import = tegra_gem_prime_import,
606
Arto Merilainende2ba662013-03-22 16:34:08 +0200607 .dumb_create = tegra_bo_dumb_create,
608 .dumb_map_offset = tegra_bo_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200609 .dumb_destroy = drm_gem_dumb_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000610
611 .ioctls = tegra_drm_ioctls,
612 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
613 .fops = &tegra_drm_fops,
614
615 .name = DRIVER_NAME,
616 .desc = DRIVER_DESC,
617 .date = DRIVER_DATE,
618 .major = DRIVER_MAJOR,
619 .minor = DRIVER_MINOR,
620 .patchlevel = DRIVER_PATCHLEVEL,
621};
Thierry Reding776dc382013-10-14 14:43:22 +0200622
623int tegra_drm_register_client(struct tegra_drm *tegra,
624 struct tegra_drm_client *client)
625{
626 mutex_lock(&tegra->clients_lock);
627 list_add_tail(&client->list, &tegra->clients);
628 mutex_unlock(&tegra->clients_lock);
629
630 return 0;
631}
632
633int tegra_drm_unregister_client(struct tegra_drm *tegra,
634 struct tegra_drm_client *client)
635{
636 mutex_lock(&tegra->clients_lock);
637 list_del_init(&client->list);
638 mutex_unlock(&tegra->clients_lock);
639
640 return 0;
641}
642
643static int host1x_drm_probe(struct host1x_device *device)
644{
645 return drm_host1x_init(&tegra_drm_driver, device);
646}
647
648static int host1x_drm_remove(struct host1x_device *device)
649{
650 drm_host1x_exit(&tegra_drm_driver, device);
651
652 return 0;
653}
654
655static const struct of_device_id host1x_drm_subdevs[] = {
656 { .compatible = "nvidia,tegra20-dc", },
657 { .compatible = "nvidia,tegra20-hdmi", },
658 { .compatible = "nvidia,tegra20-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100659 { .compatible = "nvidia,tegra20-gr3d", },
Thierry Reding776dc382013-10-14 14:43:22 +0200660 { .compatible = "nvidia,tegra30-dc", },
661 { .compatible = "nvidia,tegra30-hdmi", },
662 { .compatible = "nvidia,tegra30-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100663 { .compatible = "nvidia,tegra30-gr3d", },
Thierry Redingdec72732013-09-03 08:45:46 +0200664 { .compatible = "nvidia,tegra114-dsi", },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200665 { .compatible = "nvidia,tegra114-hdmi", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100666 { .compatible = "nvidia,tegra114-gr3d", },
Thierry Reding8620fc62013-12-12 11:03:59 +0100667 { .compatible = "nvidia,tegra124-dc", },
Thierry Reding776dc382013-10-14 14:43:22 +0200668 { /* sentinel */ }
669};
670
671static struct host1x_driver host1x_drm_driver = {
672 .name = "drm",
673 .probe = host1x_drm_probe,
674 .remove = host1x_drm_remove,
675 .subdevs = host1x_drm_subdevs,
676};
677
678static int __init host1x_drm_init(void)
679{
680 int err;
681
682 err = host1x_driver_register(&host1x_drm_driver);
683 if (err < 0)
684 return err;
685
686 err = platform_driver_register(&tegra_dc_driver);
687 if (err < 0)
688 goto unregister_host1x;
689
Thierry Redingdec72732013-09-03 08:45:46 +0200690 err = platform_driver_register(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200691 if (err < 0)
692 goto unregister_dc;
693
Thierry Redingdec72732013-09-03 08:45:46 +0200694 err = platform_driver_register(&tegra_hdmi_driver);
695 if (err < 0)
696 goto unregister_dsi;
697
Thierry Reding776dc382013-10-14 14:43:22 +0200698 err = platform_driver_register(&tegra_gr2d_driver);
699 if (err < 0)
700 goto unregister_hdmi;
701
Thierry Reding5f60ed02013-02-28 08:08:01 +0100702 err = platform_driver_register(&tegra_gr3d_driver);
703 if (err < 0)
704 goto unregister_gr2d;
705
Thierry Reding776dc382013-10-14 14:43:22 +0200706 return 0;
707
Thierry Reding5f60ed02013-02-28 08:08:01 +0100708unregister_gr2d:
709 platform_driver_unregister(&tegra_gr2d_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200710unregister_hdmi:
711 platform_driver_unregister(&tegra_hdmi_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200712unregister_dsi:
713 platform_driver_unregister(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200714unregister_dc:
715 platform_driver_unregister(&tegra_dc_driver);
716unregister_host1x:
717 host1x_driver_unregister(&host1x_drm_driver);
718 return err;
719}
720module_init(host1x_drm_init);
721
722static void __exit host1x_drm_exit(void)
723{
Thierry Reding5f60ed02013-02-28 08:08:01 +0100724 platform_driver_unregister(&tegra_gr3d_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200725 platform_driver_unregister(&tegra_gr2d_driver);
726 platform_driver_unregister(&tegra_hdmi_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200727 platform_driver_unregister(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200728 platform_driver_unregister(&tegra_dc_driver);
729 host1x_driver_unregister(&host1x_drm_driver);
730}
731module_exit(host1x_drm_exit);
732
733MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
734MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
735MODULE_LICENSE("GPL v2");