blob: 07eba596d458d22b63bc9f0a731d6ca3befae036 [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 Reding386a2a72013-09-24 13:22:17 +0200107 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000108
Thierry Reding386a2a72013-09-24 13:22:17 +0200109 tegra_fbdev_restore_mode(tegra->fbdev);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000110}
111
Thierry Redingc40f0f12013-10-10 11:00:33 +0200112static struct host1x_bo *
113host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle)
114{
115 struct drm_gem_object *gem;
116 struct tegra_bo *bo;
117
118 gem = drm_gem_object_lookup(drm, file, handle);
119 if (!gem)
120 return NULL;
121
122 mutex_lock(&drm->struct_mutex);
123 drm_gem_object_unreference(gem);
124 mutex_unlock(&drm->struct_mutex);
125
126 bo = to_tegra_bo(gem);
127 return &bo->base;
128}
129
130int tegra_drm_submit(struct tegra_drm_context *context,
131 struct drm_tegra_submit *args, struct drm_device *drm,
132 struct drm_file *file)
133{
134 unsigned int num_cmdbufs = args->num_cmdbufs;
135 unsigned int num_relocs = args->num_relocs;
136 unsigned int num_waitchks = args->num_waitchks;
137 struct drm_tegra_cmdbuf __user *cmdbufs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100138 (void __user *)(uintptr_t)args->cmdbufs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200139 struct drm_tegra_reloc __user *relocs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100140 (void __user *)(uintptr_t)args->relocs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200141 struct drm_tegra_waitchk __user *waitchks =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100142 (void __user *)(uintptr_t)args->waitchks;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200143 struct drm_tegra_syncpt syncpt;
144 struct host1x_job *job;
145 int err;
146
147 /* We don't yet support other than one syncpt_incr struct per submit */
148 if (args->num_syncpts != 1)
149 return -EINVAL;
150
151 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
152 args->num_relocs, args->num_waitchks);
153 if (!job)
154 return -ENOMEM;
155
156 job->num_relocs = args->num_relocs;
157 job->num_waitchk = args->num_waitchks;
158 job->client = (u32)args->context;
159 job->class = context->client->base.class;
160 job->serialize = true;
161
162 while (num_cmdbufs) {
163 struct drm_tegra_cmdbuf cmdbuf;
164 struct host1x_bo *bo;
165
Dan Carpenter9a991602013-11-08 13:07:37 +0300166 if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
167 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200168 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300169 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200170
171 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
172 if (!bo) {
173 err = -ENOENT;
174 goto fail;
175 }
176
177 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
178 num_cmdbufs--;
179 cmdbufs++;
180 }
181
Dan Carpenter9a991602013-11-08 13:07:37 +0300182 if (copy_from_user(job->relocarray, relocs,
183 sizeof(*relocs) * num_relocs)) {
184 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200185 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300186 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200187
188 while (num_relocs--) {
189 struct host1x_reloc *reloc = &job->relocarray[num_relocs];
190 struct host1x_bo *cmdbuf, *target;
191
192 cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
193 target = host1x_bo_lookup(drm, file, (u32)reloc->target);
194
195 reloc->cmdbuf = cmdbuf;
196 reloc->target = target;
197
198 if (!reloc->target || !reloc->cmdbuf) {
199 err = -ENOENT;
200 goto fail;
201 }
202 }
203
Dan Carpenter9a991602013-11-08 13:07:37 +0300204 if (copy_from_user(job->waitchk, waitchks,
205 sizeof(*waitchks) * num_waitchks)) {
206 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200207 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300208 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200209
Dan Carpenter9a991602013-11-08 13:07:37 +0300210 if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
211 sizeof(syncpt))) {
212 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200213 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300214 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200215
216 job->is_addr_reg = context->client->ops->is_addr_reg;
217 job->syncpt_incrs = syncpt.incrs;
218 job->syncpt_id = syncpt.id;
219 job->timeout = 10000;
220
221 if (args->timeout && args->timeout < 10000)
222 job->timeout = args->timeout;
223
224 err = host1x_job_pin(job, context->client->base.dev);
225 if (err)
226 goto fail;
227
228 err = host1x_job_submit(job);
229 if (err)
230 goto fail_submit;
231
232 args->fence = job->syncpt_end;
233
234 host1x_job_put(job);
235 return 0;
236
237fail_submit:
238 host1x_job_unpin(job);
239fail:
240 host1x_job_put(job);
241 return err;
242}
243
244
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200245#ifdef CONFIG_DRM_TEGRA_STAGING
Thierry Redingc88c3632013-09-26 16:08:22 +0200246static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200247{
Thierry Redingc88c3632013-09-26 16:08:22 +0200248 return (struct tegra_drm_context *)(uintptr_t)context;
249}
250
251static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
252 struct tegra_drm_context *context)
253{
254 struct tegra_drm_context *ctx;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200255
256 list_for_each_entry(ctx, &file->contexts, list)
257 if (ctx == context)
258 return true;
259
260 return false;
261}
262
263static int tegra_gem_create(struct drm_device *drm, void *data,
264 struct drm_file *file)
265{
266 struct drm_tegra_gem_create *args = data;
267 struct tegra_bo *bo;
268
Thierry Reding773af772013-10-04 22:34:01 +0200269 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200270 &args->handle);
271 if (IS_ERR(bo))
272 return PTR_ERR(bo);
273
274 return 0;
275}
276
277static int tegra_gem_mmap(struct drm_device *drm, void *data,
278 struct drm_file *file)
279{
280 struct drm_tegra_gem_mmap *args = data;
281 struct drm_gem_object *gem;
282 struct tegra_bo *bo;
283
284 gem = drm_gem_object_lookup(drm, file, args->handle);
285 if (!gem)
286 return -EINVAL;
287
288 bo = to_tegra_bo(gem);
289
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200290 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200291
292 drm_gem_object_unreference(gem);
293
294 return 0;
295}
296
297static int tegra_syncpt_read(struct drm_device *drm, void *data,
298 struct drm_file *file)
299{
Thierry Reding776dc382013-10-14 14:43:22 +0200300 struct host1x *host = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200301 struct drm_tegra_syncpt_read *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200302 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200303
Thierry Reding776dc382013-10-14 14:43:22 +0200304 sp = host1x_syncpt_get(host, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200305 if (!sp)
306 return -EINVAL;
307
308 args->value = host1x_syncpt_read_min(sp);
309 return 0;
310}
311
312static int tegra_syncpt_incr(struct drm_device *drm, void *data,
313 struct drm_file *file)
314{
Thierry Reding776dc382013-10-14 14:43:22 +0200315 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200316 struct drm_tegra_syncpt_incr *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200317 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200318
Thierry Reding776dc382013-10-14 14:43:22 +0200319 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200320 if (!sp)
321 return -EINVAL;
322
Arto Merilainenebae30b2013-05-29 13:26:08 +0300323 return host1x_syncpt_incr(sp);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200324}
325
326static int tegra_syncpt_wait(struct drm_device *drm, void *data,
327 struct drm_file *file)
328{
Thierry Reding776dc382013-10-14 14:43:22 +0200329 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200330 struct drm_tegra_syncpt_wait *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200331 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200332
Thierry Reding776dc382013-10-14 14:43:22 +0200333 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200334 if (!sp)
335 return -EINVAL;
336
337 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
338 &args->value);
339}
340
341static int tegra_open_channel(struct drm_device *drm, void *data,
342 struct drm_file *file)
343{
Thierry Reding08943e62013-09-26 16:08:18 +0200344 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding386a2a72013-09-24 13:22:17 +0200345 struct tegra_drm *tegra = drm->dev_private;
346 struct drm_tegra_open_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200347 struct tegra_drm_context *context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200348 struct tegra_drm_client *client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200349 int err = -ENODEV;
350
351 context = kzalloc(sizeof(*context), GFP_KERNEL);
352 if (!context)
353 return -ENOMEM;
354
Thierry Reding776dc382013-10-14 14:43:22 +0200355 list_for_each_entry(client, &tegra->clients, list)
Thierry Reding53fa7f72013-09-24 15:35:40 +0200356 if (client->base.class == args->client) {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200357 err = client->ops->open_channel(client, context);
358 if (err)
359 break;
360
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200361 list_add(&context->list, &fpriv->contexts);
362 args->context = (uintptr_t)context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200363 context->client = client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200364 return 0;
365 }
366
367 kfree(context);
368 return err;
369}
370
371static int tegra_close_channel(struct drm_device *drm, void *data,
372 struct drm_file *file)
373{
Thierry Reding08943e62013-09-26 16:08:18 +0200374 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding776dc382013-10-14 14:43:22 +0200375 struct drm_tegra_close_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200376 struct tegra_drm_context *context;
377
378 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200379
Thierry Reding08943e62013-09-26 16:08:18 +0200380 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200381 return -EINVAL;
382
383 list_del(&context->list);
Thierry Redingc88c3632013-09-26 16:08:22 +0200384 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200385
386 return 0;
387}
388
389static int tegra_get_syncpt(struct drm_device *drm, void *data,
390 struct drm_file *file)
391{
Thierry Reding08943e62013-09-26 16:08:18 +0200392 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200393 struct drm_tegra_get_syncpt *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200394 struct tegra_drm_context *context;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200395 struct host1x_syncpt *syncpt;
396
Thierry Redingc88c3632013-09-26 16:08:22 +0200397 context = tegra_drm_get_context(args->context);
398
Thierry Reding08943e62013-09-26 16:08:18 +0200399 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200400 return -ENODEV;
401
Thierry Reding53fa7f72013-09-24 15:35:40 +0200402 if (args->index >= context->client->base.num_syncpts)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200403 return -EINVAL;
404
Thierry Reding53fa7f72013-09-24 15:35:40 +0200405 syncpt = context->client->base.syncpts[args->index];
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200406 args->id = host1x_syncpt_id(syncpt);
407
408 return 0;
409}
410
411static int tegra_submit(struct drm_device *drm, void *data,
412 struct drm_file *file)
413{
Thierry Reding08943e62013-09-26 16:08:18 +0200414 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200415 struct drm_tegra_submit *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200416 struct tegra_drm_context *context;
417
418 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200419
Thierry Reding08943e62013-09-26 16:08:18 +0200420 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200421 return -ENODEV;
422
423 return context->client->ops->submit(context, args, drm, file);
424}
Arto Merilainenc54a1692013-10-14 15:21:54 +0300425
426static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
427 struct drm_file *file)
428{
429 struct tegra_drm_file *fpriv = file->driver_priv;
430 struct drm_tegra_get_syncpt_base *args = data;
431 struct tegra_drm_context *context;
432 struct host1x_syncpt_base *base;
433 struct host1x_syncpt *syncpt;
434
435 context = tegra_drm_get_context(args->context);
436
437 if (!tegra_drm_file_owns_context(fpriv, context))
438 return -ENODEV;
439
440 if (args->syncpt >= context->client->base.num_syncpts)
441 return -EINVAL;
442
443 syncpt = context->client->base.syncpts[args->syncpt];
444
445 base = host1x_syncpt_get_base(syncpt);
446 if (!base)
447 return -ENXIO;
448
449 args->id = host1x_syncpt_base_id(base);
450
451 return 0;
452}
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200453#endif
454
Rob Clarkbaa70942013-08-02 13:27:49 -0400455static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200456#ifdef CONFIG_DRM_TEGRA_STAGING
457 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
458 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
459 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
460 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
461 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
462 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
463 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
464 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
465 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
Arto Merilainenc54a1692013-10-14 15:21:54 +0300466 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, DRM_UNLOCKED),
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200467#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000468};
469
470static const struct file_operations tegra_drm_fops = {
471 .owner = THIS_MODULE,
472 .open = drm_open,
473 .release = drm_release,
474 .unlocked_ioctl = drm_ioctl,
Arto Merilainende2ba662013-03-22 16:34:08 +0200475 .mmap = tegra_drm_mmap,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000476 .poll = drm_poll,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000477 .read = drm_read,
478#ifdef CONFIG_COMPAT
479 .compat_ioctl = drm_compat_ioctl,
480#endif
481 .llseek = noop_llseek,
482};
483
Thierry Reding6e5ff992012-11-28 11:45:47 +0100484static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
485{
486 struct drm_crtc *crtc;
487
488 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
489 struct tegra_dc *dc = to_tegra_dc(crtc);
490
491 if (dc->pipe == pipe)
492 return crtc;
493 }
494
495 return NULL;
496}
497
498static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
499{
500 /* TODO: implement real hardware counter using syncpoints */
501 return drm_vblank_count(dev, crtc);
502}
503
504static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
505{
506 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
507 struct tegra_dc *dc = to_tegra_dc(crtc);
508
509 if (!crtc)
510 return -ENODEV;
511
512 tegra_dc_enable_vblank(dc);
513
514 return 0;
515}
516
517static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
518{
519 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
520 struct tegra_dc *dc = to_tegra_dc(crtc);
521
522 if (crtc)
523 tegra_dc_disable_vblank(dc);
524}
525
Thierry Reding3c03c462012-11-28 12:00:18 +0100526static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
527{
Thierry Reding08943e62013-09-26 16:08:18 +0200528 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Redingc88c3632013-09-26 16:08:22 +0200529 struct tegra_drm_context *context, *tmp;
Thierry Reding3c03c462012-11-28 12:00:18 +0100530 struct drm_crtc *crtc;
531
532 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
533 tegra_dc_cancel_page_flip(crtc, file);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200534
535 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
Thierry Redingc88c3632013-09-26 16:08:22 +0200536 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200537
538 kfree(fpriv);
Thierry Reding3c03c462012-11-28 12:00:18 +0100539}
540
Thierry Redinge450fcc2013-02-13 16:13:16 +0100541#ifdef CONFIG_DEBUG_FS
542static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
543{
544 struct drm_info_node *node = (struct drm_info_node *)s->private;
545 struct drm_device *drm = node->minor->dev;
546 struct drm_framebuffer *fb;
547
548 mutex_lock(&drm->mode_config.fb_lock);
549
550 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
551 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
552 fb->base.id, fb->width, fb->height, fb->depth,
553 fb->bits_per_pixel,
554 atomic_read(&fb->refcount.refcount));
555 }
556
557 mutex_unlock(&drm->mode_config.fb_lock);
558
559 return 0;
560}
561
562static struct drm_info_list tegra_debugfs_list[] = {
563 { "framebuffers", tegra_debugfs_framebuffers, 0 },
564};
565
566static int tegra_debugfs_init(struct drm_minor *minor)
567{
568 return drm_debugfs_create_files(tegra_debugfs_list,
569 ARRAY_SIZE(tegra_debugfs_list),
570 minor->debugfs_root, minor);
571}
572
573static void tegra_debugfs_cleanup(struct drm_minor *minor)
574{
575 drm_debugfs_remove_files(tegra_debugfs_list,
576 ARRAY_SIZE(tegra_debugfs_list), minor);
577}
578#endif
579
Thierry Reding9b57f5f2013-11-08 13:17:14 +0100580static struct drm_driver tegra_drm_driver = {
Laurent Pinchart604faa72013-05-29 07:44:34 +0200581 .driver_features = DRIVER_MODESET | DRIVER_GEM,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000582 .load = tegra_drm_load,
583 .unload = tegra_drm_unload,
584 .open = tegra_drm_open,
Thierry Reding3c03c462012-11-28 12:00:18 +0100585 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000586 .lastclose = tegra_drm_lastclose,
587
Thierry Reding6e5ff992012-11-28 11:45:47 +0100588 .get_vblank_counter = tegra_drm_get_vblank_counter,
589 .enable_vblank = tegra_drm_enable_vblank,
590 .disable_vblank = tegra_drm_disable_vblank,
591
Thierry Redinge450fcc2013-02-13 16:13:16 +0100592#if defined(CONFIG_DEBUG_FS)
593 .debugfs_init = tegra_debugfs_init,
594 .debugfs_cleanup = tegra_debugfs_cleanup,
595#endif
596
Arto Merilainende2ba662013-03-22 16:34:08 +0200597 .gem_free_object = tegra_bo_free_object,
598 .gem_vm_ops = &tegra_bo_vm_ops,
599 .dumb_create = tegra_bo_dumb_create,
600 .dumb_map_offset = tegra_bo_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200601 .dumb_destroy = drm_gem_dumb_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000602
603 .ioctls = tegra_drm_ioctls,
604 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
605 .fops = &tegra_drm_fops,
606
607 .name = DRIVER_NAME,
608 .desc = DRIVER_DESC,
609 .date = DRIVER_DATE,
610 .major = DRIVER_MAJOR,
611 .minor = DRIVER_MINOR,
612 .patchlevel = DRIVER_PATCHLEVEL,
613};
Thierry Reding776dc382013-10-14 14:43:22 +0200614
615int tegra_drm_register_client(struct tegra_drm *tegra,
616 struct tegra_drm_client *client)
617{
618 mutex_lock(&tegra->clients_lock);
619 list_add_tail(&client->list, &tegra->clients);
620 mutex_unlock(&tegra->clients_lock);
621
622 return 0;
623}
624
625int tegra_drm_unregister_client(struct tegra_drm *tegra,
626 struct tegra_drm_client *client)
627{
628 mutex_lock(&tegra->clients_lock);
629 list_del_init(&client->list);
630 mutex_unlock(&tegra->clients_lock);
631
632 return 0;
633}
634
635static int host1x_drm_probe(struct host1x_device *device)
636{
637 return drm_host1x_init(&tegra_drm_driver, device);
638}
639
640static int host1x_drm_remove(struct host1x_device *device)
641{
642 drm_host1x_exit(&tegra_drm_driver, device);
643
644 return 0;
645}
646
647static const struct of_device_id host1x_drm_subdevs[] = {
648 { .compatible = "nvidia,tegra20-dc", },
649 { .compatible = "nvidia,tegra20-hdmi", },
650 { .compatible = "nvidia,tegra20-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100651 { .compatible = "nvidia,tegra20-gr3d", },
Thierry Reding776dc382013-10-14 14:43:22 +0200652 { .compatible = "nvidia,tegra30-dc", },
653 { .compatible = "nvidia,tegra30-hdmi", },
654 { .compatible = "nvidia,tegra30-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100655 { .compatible = "nvidia,tegra30-gr3d", },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200656 { .compatible = "nvidia,tegra114-hdmi", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100657 { .compatible = "nvidia,tegra114-gr3d", },
Thierry Reding776dc382013-10-14 14:43:22 +0200658 { /* sentinel */ }
659};
660
661static struct host1x_driver host1x_drm_driver = {
662 .name = "drm",
663 .probe = host1x_drm_probe,
664 .remove = host1x_drm_remove,
665 .subdevs = host1x_drm_subdevs,
666};
667
668static int __init host1x_drm_init(void)
669{
670 int err;
671
672 err = host1x_driver_register(&host1x_drm_driver);
673 if (err < 0)
674 return err;
675
676 err = platform_driver_register(&tegra_dc_driver);
677 if (err < 0)
678 goto unregister_host1x;
679
680 err = platform_driver_register(&tegra_hdmi_driver);
681 if (err < 0)
682 goto unregister_dc;
683
684 err = platform_driver_register(&tegra_gr2d_driver);
685 if (err < 0)
686 goto unregister_hdmi;
687
Thierry Reding5f60ed02013-02-28 08:08:01 +0100688 err = platform_driver_register(&tegra_gr3d_driver);
689 if (err < 0)
690 goto unregister_gr2d;
691
Thierry Reding776dc382013-10-14 14:43:22 +0200692 return 0;
693
Thierry Reding5f60ed02013-02-28 08:08:01 +0100694unregister_gr2d:
695 platform_driver_unregister(&tegra_gr2d_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200696unregister_hdmi:
697 platform_driver_unregister(&tegra_hdmi_driver);
698unregister_dc:
699 platform_driver_unregister(&tegra_dc_driver);
700unregister_host1x:
701 host1x_driver_unregister(&host1x_drm_driver);
702 return err;
703}
704module_init(host1x_drm_init);
705
706static void __exit host1x_drm_exit(void)
707{
Thierry Reding5f60ed02013-02-28 08:08:01 +0100708 platform_driver_unregister(&tegra_gr3d_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200709 platform_driver_unregister(&tegra_gr2d_driver);
710 platform_driver_unregister(&tegra_hdmi_driver);
711 platform_driver_unregister(&tegra_dc_driver);
712 host1x_driver_unregister(&host1x_drm_driver);
713}
714module_exit(host1x_drm_exit);
715
716MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
717MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
718MODULE_LICENSE("GPL v2");