blob: fd736efd14bd963cd38571ae7de02fb54c785ce8 [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 Reding386a2a72013-09-24 13:22:17 +020036 mutex_init(&tegra->clients_lock);
37 INIT_LIST_HEAD(&tegra->clients);
Thierry Reding386a2a72013-09-24 13:22:17 +020038 drm->dev_private = tegra;
39 tegra->drm = drm;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000040
41 drm_mode_config_init(drm);
42
Thierry Redinge2215322014-06-27 17:19:25 +020043 err = tegra_drm_fb_prepare(drm);
44 if (err < 0)
45 return err;
46
47 drm_kms_helper_poll_init(drm);
48
Thierry Reding776dc382013-10-14 14:43:22 +020049 err = host1x_device_init(device);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000050 if (err < 0)
51 return err;
52
Thierry Reding603f0cc2013-04-22 21:22:14 +020053 /*
54 * We don't use the drm_irq_install() helpers provided by the DRM
55 * core, so we need to set this manually in order to allow the
56 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
57 */
Ville Syrjälä44238432013-10-04 14:53:37 +030058 drm->irq_enabled = true;
Thierry Reding603f0cc2013-04-22 21:22:14 +020059
Thierry Reding6e5ff992012-11-28 11:45:47 +010060 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
61 if (err < 0)
62 return err;
63
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000064 err = tegra_drm_fb_init(drm);
65 if (err < 0)
66 return err;
67
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000068 return 0;
69}
70
71static int tegra_drm_unload(struct drm_device *drm)
72{
Thierry Reding776dc382013-10-14 14:43:22 +020073 struct host1x_device *device = to_host1x_device(drm->dev);
74 int err;
75
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000076 drm_kms_helper_poll_fini(drm);
77 tegra_drm_fb_exit(drm);
Thierry Redingf002abc2013-10-14 14:06:02 +020078 drm_vblank_cleanup(drm);
79 drm_mode_config_cleanup(drm);
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000080
Thierry Reding776dc382013-10-14 14:43:22 +020081 err = host1x_device_exit(device);
82 if (err < 0)
83 return err;
84
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000085 return 0;
86}
87
88static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
89{
Thierry Reding08943e62013-09-26 16:08:18 +020090 struct tegra_drm_file *fpriv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +020091
92 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
93 if (!fpriv)
94 return -ENOMEM;
95
96 INIT_LIST_HEAD(&fpriv->contexts);
97 filp->driver_priv = fpriv;
98
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000099 return 0;
100}
101
Thierry Redingc88c3632013-09-26 16:08:22 +0200102static void tegra_drm_context_free(struct tegra_drm_context *context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200103{
104 context->client->ops->close_channel(context);
105 kfree(context);
106}
107
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000108static void tegra_drm_lastclose(struct drm_device *drm)
109{
Paul Bolle6e601632014-02-09 14:01:33 +0100110#ifdef CONFIG_DRM_TEGRA_FBDEV
Thierry Reding386a2a72013-09-24 13:22:17 +0200111 struct tegra_drm *tegra = drm->dev_private;
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000112
Thierry Reding386a2a72013-09-24 13:22:17 +0200113 tegra_fbdev_restore_mode(tegra->fbdev);
Thierry Reding60c2f702013-10-31 13:28:50 +0100114#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000115}
116
Thierry Redingc40f0f12013-10-10 11:00:33 +0200117static struct host1x_bo *
118host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle)
119{
120 struct drm_gem_object *gem;
121 struct tegra_bo *bo;
122
123 gem = drm_gem_object_lookup(drm, file, handle);
124 if (!gem)
125 return NULL;
126
127 mutex_lock(&drm->struct_mutex);
128 drm_gem_object_unreference(gem);
129 mutex_unlock(&drm->struct_mutex);
130
131 bo = to_tegra_bo(gem);
132 return &bo->base;
133}
134
135int tegra_drm_submit(struct tegra_drm_context *context,
136 struct drm_tegra_submit *args, struct drm_device *drm,
137 struct drm_file *file)
138{
139 unsigned int num_cmdbufs = args->num_cmdbufs;
140 unsigned int num_relocs = args->num_relocs;
141 unsigned int num_waitchks = args->num_waitchks;
142 struct drm_tegra_cmdbuf __user *cmdbufs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100143 (void __user *)(uintptr_t)args->cmdbufs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200144 struct drm_tegra_reloc __user *relocs =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100145 (void __user *)(uintptr_t)args->relocs;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200146 struct drm_tegra_waitchk __user *waitchks =
Thierry Redinga7ed68f2013-11-08 13:15:43 +0100147 (void __user *)(uintptr_t)args->waitchks;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200148 struct drm_tegra_syncpt syncpt;
149 struct host1x_job *job;
150 int err;
151
152 /* We don't yet support other than one syncpt_incr struct per submit */
153 if (args->num_syncpts != 1)
154 return -EINVAL;
155
156 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
157 args->num_relocs, args->num_waitchks);
158 if (!job)
159 return -ENOMEM;
160
161 job->num_relocs = args->num_relocs;
162 job->num_waitchk = args->num_waitchks;
163 job->client = (u32)args->context;
164 job->class = context->client->base.class;
165 job->serialize = true;
166
167 while (num_cmdbufs) {
168 struct drm_tegra_cmdbuf cmdbuf;
169 struct host1x_bo *bo;
170
Dan Carpenter9a991602013-11-08 13:07:37 +0300171 if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
172 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200173 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300174 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200175
176 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
177 if (!bo) {
178 err = -ENOENT;
179 goto fail;
180 }
181
182 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
183 num_cmdbufs--;
184 cmdbufs++;
185 }
186
Dan Carpenter9a991602013-11-08 13:07:37 +0300187 if (copy_from_user(job->relocarray, relocs,
188 sizeof(*relocs) * num_relocs)) {
189 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200190 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300191 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200192
193 while (num_relocs--) {
194 struct host1x_reloc *reloc = &job->relocarray[num_relocs];
195 struct host1x_bo *cmdbuf, *target;
196
197 cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
198 target = host1x_bo_lookup(drm, file, (u32)reloc->target);
199
200 reloc->cmdbuf = cmdbuf;
201 reloc->target = target;
202
203 if (!reloc->target || !reloc->cmdbuf) {
204 err = -ENOENT;
205 goto fail;
206 }
207 }
208
Dan Carpenter9a991602013-11-08 13:07:37 +0300209 if (copy_from_user(job->waitchk, waitchks,
210 sizeof(*waitchks) * num_waitchks)) {
211 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200212 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300213 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200214
Dan Carpenter9a991602013-11-08 13:07:37 +0300215 if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
216 sizeof(syncpt))) {
217 err = -EFAULT;
Thierry Redingc40f0f12013-10-10 11:00:33 +0200218 goto fail;
Dan Carpenter9a991602013-11-08 13:07:37 +0300219 }
Thierry Redingc40f0f12013-10-10 11:00:33 +0200220
221 job->is_addr_reg = context->client->ops->is_addr_reg;
222 job->syncpt_incrs = syncpt.incrs;
223 job->syncpt_id = syncpt.id;
224 job->timeout = 10000;
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
242fail_submit:
243 host1x_job_unpin(job);
244fail:
245 host1x_job_put(job);
246 return err;
247}
248
249
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200250#ifdef CONFIG_DRM_TEGRA_STAGING
Thierry Redingc88c3632013-09-26 16:08:22 +0200251static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200252{
Thierry Redingc88c3632013-09-26 16:08:22 +0200253 return (struct tegra_drm_context *)(uintptr_t)context;
254}
255
256static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
257 struct tegra_drm_context *context)
258{
259 struct tegra_drm_context *ctx;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200260
261 list_for_each_entry(ctx, &file->contexts, list)
262 if (ctx == context)
263 return true;
264
265 return false;
266}
267
268static int tegra_gem_create(struct drm_device *drm, void *data,
269 struct drm_file *file)
270{
271 struct drm_tegra_gem_create *args = data;
272 struct tegra_bo *bo;
273
Thierry Reding773af772013-10-04 22:34:01 +0200274 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200275 &args->handle);
276 if (IS_ERR(bo))
277 return PTR_ERR(bo);
278
279 return 0;
280}
281
282static int tegra_gem_mmap(struct drm_device *drm, void *data,
283 struct drm_file *file)
284{
285 struct drm_tegra_gem_mmap *args = data;
286 struct drm_gem_object *gem;
287 struct tegra_bo *bo;
288
289 gem = drm_gem_object_lookup(drm, file, args->handle);
290 if (!gem)
291 return -EINVAL;
292
293 bo = to_tegra_bo(gem);
294
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200295 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200296
297 drm_gem_object_unreference(gem);
298
299 return 0;
300}
301
302static int tegra_syncpt_read(struct drm_device *drm, void *data,
303 struct drm_file *file)
304{
Thierry Reding776dc382013-10-14 14:43:22 +0200305 struct host1x *host = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200306 struct drm_tegra_syncpt_read *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200307 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200308
Thierry Reding776dc382013-10-14 14:43:22 +0200309 sp = host1x_syncpt_get(host, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200310 if (!sp)
311 return -EINVAL;
312
313 args->value = host1x_syncpt_read_min(sp);
314 return 0;
315}
316
317static int tegra_syncpt_incr(struct drm_device *drm, void *data,
318 struct drm_file *file)
319{
Thierry Reding776dc382013-10-14 14:43:22 +0200320 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200321 struct drm_tegra_syncpt_incr *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200322 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200323
Thierry Reding776dc382013-10-14 14:43:22 +0200324 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200325 if (!sp)
326 return -EINVAL;
327
Arto Merilainenebae30b2013-05-29 13:26:08 +0300328 return host1x_syncpt_incr(sp);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200329}
330
331static int tegra_syncpt_wait(struct drm_device *drm, void *data,
332 struct drm_file *file)
333{
Thierry Reding776dc382013-10-14 14:43:22 +0200334 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200335 struct drm_tegra_syncpt_wait *args = data;
Thierry Reding776dc382013-10-14 14:43:22 +0200336 struct host1x_syncpt *sp;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200337
Thierry Reding776dc382013-10-14 14:43:22 +0200338 sp = host1x_syncpt_get(host1x, args->id);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200339 if (!sp)
340 return -EINVAL;
341
342 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
343 &args->value);
344}
345
346static int tegra_open_channel(struct drm_device *drm, void *data,
347 struct drm_file *file)
348{
Thierry Reding08943e62013-09-26 16:08:18 +0200349 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding386a2a72013-09-24 13:22:17 +0200350 struct tegra_drm *tegra = drm->dev_private;
351 struct drm_tegra_open_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200352 struct tegra_drm_context *context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200353 struct tegra_drm_client *client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200354 int err = -ENODEV;
355
356 context = kzalloc(sizeof(*context), GFP_KERNEL);
357 if (!context)
358 return -ENOMEM;
359
Thierry Reding776dc382013-10-14 14:43:22 +0200360 list_for_each_entry(client, &tegra->clients, list)
Thierry Reding53fa7f72013-09-24 15:35:40 +0200361 if (client->base.class == args->client) {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200362 err = client->ops->open_channel(client, context);
363 if (err)
364 break;
365
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200366 list_add(&context->list, &fpriv->contexts);
367 args->context = (uintptr_t)context;
Thierry Reding53fa7f72013-09-24 15:35:40 +0200368 context->client = client;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200369 return 0;
370 }
371
372 kfree(context);
373 return err;
374}
375
376static int tegra_close_channel(struct drm_device *drm, void *data,
377 struct drm_file *file)
378{
Thierry Reding08943e62013-09-26 16:08:18 +0200379 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Reding776dc382013-10-14 14:43:22 +0200380 struct drm_tegra_close_channel *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200381 struct tegra_drm_context *context;
382
383 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200384
Thierry Reding08943e62013-09-26 16:08:18 +0200385 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200386 return -EINVAL;
387
388 list_del(&context->list);
Thierry Redingc88c3632013-09-26 16:08:22 +0200389 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200390
391 return 0;
392}
393
394static int tegra_get_syncpt(struct drm_device *drm, void *data,
395 struct drm_file *file)
396{
Thierry Reding08943e62013-09-26 16:08:18 +0200397 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200398 struct drm_tegra_get_syncpt *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200399 struct tegra_drm_context *context;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200400 struct host1x_syncpt *syncpt;
401
Thierry Redingc88c3632013-09-26 16:08:22 +0200402 context = tegra_drm_get_context(args->context);
403
Thierry Reding08943e62013-09-26 16:08:18 +0200404 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200405 return -ENODEV;
406
Thierry Reding53fa7f72013-09-24 15:35:40 +0200407 if (args->index >= context->client->base.num_syncpts)
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200408 return -EINVAL;
409
Thierry Reding53fa7f72013-09-24 15:35:40 +0200410 syncpt = context->client->base.syncpts[args->index];
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200411 args->id = host1x_syncpt_id(syncpt);
412
413 return 0;
414}
415
416static int tegra_submit(struct drm_device *drm, void *data,
417 struct drm_file *file)
418{
Thierry Reding08943e62013-09-26 16:08:18 +0200419 struct tegra_drm_file *fpriv = file->driver_priv;
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200420 struct drm_tegra_submit *args = data;
Thierry Redingc88c3632013-09-26 16:08:22 +0200421 struct tegra_drm_context *context;
422
423 context = tegra_drm_get_context(args->context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200424
Thierry Reding08943e62013-09-26 16:08:18 +0200425 if (!tegra_drm_file_owns_context(fpriv, context))
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200426 return -ENODEV;
427
428 return context->client->ops->submit(context, args, drm, file);
429}
Arto Merilainenc54a1692013-10-14 15:21:54 +0300430
431static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
432 struct drm_file *file)
433{
434 struct tegra_drm_file *fpriv = file->driver_priv;
435 struct drm_tegra_get_syncpt_base *args = data;
436 struct tegra_drm_context *context;
437 struct host1x_syncpt_base *base;
438 struct host1x_syncpt *syncpt;
439
440 context = tegra_drm_get_context(args->context);
441
442 if (!tegra_drm_file_owns_context(fpriv, context))
443 return -ENODEV;
444
445 if (args->syncpt >= context->client->base.num_syncpts)
446 return -EINVAL;
447
448 syncpt = context->client->base.syncpts[args->syncpt];
449
450 base = host1x_syncpt_get_base(syncpt);
451 if (!base)
452 return -ENXIO;
453
454 args->id = host1x_syncpt_base_id(base);
455
456 return 0;
457}
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200458#endif
459
Rob Clarkbaa70942013-08-02 13:27:49 -0400460static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200461#ifdef CONFIG_DRM_TEGRA_STAGING
462 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_UNLOCKED | DRM_AUTH),
463 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_UNLOCKED),
464 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, DRM_UNLOCKED),
465 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, DRM_UNLOCKED),
466 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, DRM_UNLOCKED),
467 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, DRM_UNLOCKED),
468 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, DRM_UNLOCKED),
469 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, DRM_UNLOCKED),
470 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, DRM_UNLOCKED),
Arto Merilainenc54a1692013-10-14 15:21:54 +0300471 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, DRM_UNLOCKED),
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200472#endif
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000473};
474
475static const struct file_operations tegra_drm_fops = {
476 .owner = THIS_MODULE,
477 .open = drm_open,
478 .release = drm_release,
479 .unlocked_ioctl = drm_ioctl,
Arto Merilainende2ba662013-03-22 16:34:08 +0200480 .mmap = tegra_drm_mmap,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000481 .poll = drm_poll,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000482 .read = drm_read,
483#ifdef CONFIG_COMPAT
484 .compat_ioctl = drm_compat_ioctl,
485#endif
486 .llseek = noop_llseek,
487};
488
Thierry Reding6e5ff992012-11-28 11:45:47 +0100489static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
490{
491 struct drm_crtc *crtc;
492
493 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
494 struct tegra_dc *dc = to_tegra_dc(crtc);
495
496 if (dc->pipe == pipe)
497 return crtc;
498 }
499
500 return NULL;
501}
502
503static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
504{
505 /* TODO: implement real hardware counter using syncpoints */
506 return drm_vblank_count(dev, crtc);
507}
508
509static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
510{
511 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
512 struct tegra_dc *dc = to_tegra_dc(crtc);
513
514 if (!crtc)
515 return -ENODEV;
516
517 tegra_dc_enable_vblank(dc);
518
519 return 0;
520}
521
522static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
523{
524 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
525 struct tegra_dc *dc = to_tegra_dc(crtc);
526
527 if (crtc)
528 tegra_dc_disable_vblank(dc);
529}
530
Thierry Reding3c03c462012-11-28 12:00:18 +0100531static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
532{
Thierry Reding08943e62013-09-26 16:08:18 +0200533 struct tegra_drm_file *fpriv = file->driver_priv;
Thierry Redingc88c3632013-09-26 16:08:22 +0200534 struct tegra_drm_context *context, *tmp;
Thierry Reding3c03c462012-11-28 12:00:18 +0100535 struct drm_crtc *crtc;
536
537 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
538 tegra_dc_cancel_page_flip(crtc, file);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200539
540 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
Thierry Redingc88c3632013-09-26 16:08:22 +0200541 tegra_drm_context_free(context);
Terje Bergstromd43f81c2013-03-22 16:34:09 +0200542
543 kfree(fpriv);
Thierry Reding3c03c462012-11-28 12:00:18 +0100544}
545
Thierry Redinge450fcc2013-02-13 16:13:16 +0100546#ifdef CONFIG_DEBUG_FS
547static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
548{
549 struct drm_info_node *node = (struct drm_info_node *)s->private;
550 struct drm_device *drm = node->minor->dev;
551 struct drm_framebuffer *fb;
552
553 mutex_lock(&drm->mode_config.fb_lock);
554
555 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
556 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
557 fb->base.id, fb->width, fb->height, fb->depth,
558 fb->bits_per_pixel,
559 atomic_read(&fb->refcount.refcount));
560 }
561
562 mutex_unlock(&drm->mode_config.fb_lock);
563
564 return 0;
565}
566
567static struct drm_info_list tegra_debugfs_list[] = {
568 { "framebuffers", tegra_debugfs_framebuffers, 0 },
569};
570
571static int tegra_debugfs_init(struct drm_minor *minor)
572{
573 return drm_debugfs_create_files(tegra_debugfs_list,
574 ARRAY_SIZE(tegra_debugfs_list),
575 minor->debugfs_root, minor);
576}
577
578static void tegra_debugfs_cleanup(struct drm_minor *minor)
579{
580 drm_debugfs_remove_files(tegra_debugfs_list,
581 ARRAY_SIZE(tegra_debugfs_list), minor);
582}
583#endif
584
Thierry Reding9b57f5f2013-11-08 13:17:14 +0100585static struct drm_driver tegra_drm_driver = {
Thierry Reding38003912013-12-12 10:00:43 +0100586 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000587 .load = tegra_drm_load,
588 .unload = tegra_drm_unload,
589 .open = tegra_drm_open,
Thierry Reding3c03c462012-11-28 12:00:18 +0100590 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000591 .lastclose = tegra_drm_lastclose,
592
Thierry Reding6e5ff992012-11-28 11:45:47 +0100593 .get_vblank_counter = tegra_drm_get_vblank_counter,
594 .enable_vblank = tegra_drm_enable_vblank,
595 .disable_vblank = tegra_drm_disable_vblank,
596
Thierry Redinge450fcc2013-02-13 16:13:16 +0100597#if defined(CONFIG_DEBUG_FS)
598 .debugfs_init = tegra_debugfs_init,
599 .debugfs_cleanup = tegra_debugfs_cleanup,
600#endif
601
Arto Merilainende2ba662013-03-22 16:34:08 +0200602 .gem_free_object = tegra_bo_free_object,
603 .gem_vm_ops = &tegra_bo_vm_ops,
Thierry Reding38003912013-12-12 10:00:43 +0100604
605 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
606 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
607 .gem_prime_export = tegra_gem_prime_export,
608 .gem_prime_import = tegra_gem_prime_import,
609
Arto Merilainende2ba662013-03-22 16:34:08 +0200610 .dumb_create = tegra_bo_dumb_create,
611 .dumb_map_offset = tegra_bo_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200612 .dumb_destroy = drm_gem_dumb_destroy,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000613
614 .ioctls = tegra_drm_ioctls,
615 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
616 .fops = &tegra_drm_fops,
617
618 .name = DRIVER_NAME,
619 .desc = DRIVER_DESC,
620 .date = DRIVER_DATE,
621 .major = DRIVER_MAJOR,
622 .minor = DRIVER_MINOR,
623 .patchlevel = DRIVER_PATCHLEVEL,
624};
Thierry Reding776dc382013-10-14 14:43:22 +0200625
626int tegra_drm_register_client(struct tegra_drm *tegra,
627 struct tegra_drm_client *client)
628{
629 mutex_lock(&tegra->clients_lock);
630 list_add_tail(&client->list, &tegra->clients);
631 mutex_unlock(&tegra->clients_lock);
632
633 return 0;
634}
635
636int tegra_drm_unregister_client(struct tegra_drm *tegra,
637 struct tegra_drm_client *client)
638{
639 mutex_lock(&tegra->clients_lock);
640 list_del_init(&client->list);
641 mutex_unlock(&tegra->clients_lock);
642
643 return 0;
644}
645
Thierry Reding9910f5c2014-05-22 09:57:15 +0200646static int host1x_drm_probe(struct host1x_device *dev)
Thierry Reding776dc382013-10-14 14:43:22 +0200647{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200648 struct drm_driver *driver = &tegra_drm_driver;
649 struct drm_device *drm;
650 int err;
651
652 drm = drm_dev_alloc(driver, &dev->dev);
653 if (!drm)
654 return -ENOMEM;
655
656 drm_dev_set_unique(drm, dev_name(&dev->dev));
657 dev_set_drvdata(&dev->dev, drm);
658
659 err = drm_dev_register(drm, 0);
660 if (err < 0)
661 goto unref;
662
663 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
664 driver->major, driver->minor, driver->patchlevel,
665 driver->date, drm->primary->index);
666
667 return 0;
668
669unref:
670 drm_dev_unref(drm);
671 return err;
Thierry Reding776dc382013-10-14 14:43:22 +0200672}
673
Thierry Reding9910f5c2014-05-22 09:57:15 +0200674static int host1x_drm_remove(struct host1x_device *dev)
Thierry Reding776dc382013-10-14 14:43:22 +0200675{
Thierry Reding9910f5c2014-05-22 09:57:15 +0200676 struct drm_device *drm = dev_get_drvdata(&dev->dev);
677
678 drm_dev_unregister(drm);
679 drm_dev_unref(drm);
Thierry Reding776dc382013-10-14 14:43:22 +0200680
681 return 0;
682}
683
684static const struct of_device_id host1x_drm_subdevs[] = {
685 { .compatible = "nvidia,tegra20-dc", },
686 { .compatible = "nvidia,tegra20-hdmi", },
687 { .compatible = "nvidia,tegra20-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100688 { .compatible = "nvidia,tegra20-gr3d", },
Thierry Reding776dc382013-10-14 14:43:22 +0200689 { .compatible = "nvidia,tegra30-dc", },
690 { .compatible = "nvidia,tegra30-hdmi", },
691 { .compatible = "nvidia,tegra30-gr2d", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100692 { .compatible = "nvidia,tegra30-gr3d", },
Thierry Redingdec72732013-09-03 08:45:46 +0200693 { .compatible = "nvidia,tegra114-dsi", },
Mikko Perttunen7d1d28a2013-09-30 16:54:47 +0200694 { .compatible = "nvidia,tegra114-hdmi", },
Thierry Reding5f60ed02013-02-28 08:08:01 +0100695 { .compatible = "nvidia,tegra114-gr3d", },
Thierry Reding8620fc62013-12-12 11:03:59 +0100696 { .compatible = "nvidia,tegra124-dc", },
Thierry Reding6b6b6042013-11-15 16:06:05 +0100697 { .compatible = "nvidia,tegra124-sor", },
Thierry Redingfb7be702013-11-15 16:07:32 +0100698 { .compatible = "nvidia,tegra124-hdmi", },
Thierry Reding776dc382013-10-14 14:43:22 +0200699 { /* sentinel */ }
700};
701
702static struct host1x_driver host1x_drm_driver = {
703 .name = "drm",
704 .probe = host1x_drm_probe,
705 .remove = host1x_drm_remove,
706 .subdevs = host1x_drm_subdevs,
707};
708
709static int __init host1x_drm_init(void)
710{
711 int err;
712
713 err = host1x_driver_register(&host1x_drm_driver);
714 if (err < 0)
715 return err;
716
717 err = platform_driver_register(&tegra_dc_driver);
718 if (err < 0)
719 goto unregister_host1x;
720
Thierry Redingdec72732013-09-03 08:45:46 +0200721 err = platform_driver_register(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200722 if (err < 0)
723 goto unregister_dc;
724
Thierry Reding6b6b6042013-11-15 16:06:05 +0100725 err = platform_driver_register(&tegra_sor_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200726 if (err < 0)
727 goto unregister_dsi;
728
Thierry Reding6b6b6042013-11-15 16:06:05 +0100729 err = platform_driver_register(&tegra_hdmi_driver);
730 if (err < 0)
731 goto unregister_sor;
732
733 err = platform_driver_register(&tegra_dpaux_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200734 if (err < 0)
735 goto unregister_hdmi;
736
Thierry Reding6b6b6042013-11-15 16:06:05 +0100737 err = platform_driver_register(&tegra_gr2d_driver);
738 if (err < 0)
739 goto unregister_dpaux;
740
Thierry Reding5f60ed02013-02-28 08:08:01 +0100741 err = platform_driver_register(&tegra_gr3d_driver);
742 if (err < 0)
743 goto unregister_gr2d;
744
Thierry Reding776dc382013-10-14 14:43:22 +0200745 return 0;
746
Thierry Reding5f60ed02013-02-28 08:08:01 +0100747unregister_gr2d:
748 platform_driver_unregister(&tegra_gr2d_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100749unregister_dpaux:
750 platform_driver_unregister(&tegra_dpaux_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200751unregister_hdmi:
752 platform_driver_unregister(&tegra_hdmi_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100753unregister_sor:
754 platform_driver_unregister(&tegra_sor_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200755unregister_dsi:
756 platform_driver_unregister(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200757unregister_dc:
758 platform_driver_unregister(&tegra_dc_driver);
759unregister_host1x:
760 host1x_driver_unregister(&host1x_drm_driver);
761 return err;
762}
763module_init(host1x_drm_init);
764
765static void __exit host1x_drm_exit(void)
766{
Thierry Reding5f60ed02013-02-28 08:08:01 +0100767 platform_driver_unregister(&tegra_gr3d_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200768 platform_driver_unregister(&tegra_gr2d_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100769 platform_driver_unregister(&tegra_dpaux_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200770 platform_driver_unregister(&tegra_hdmi_driver);
Thierry Reding6b6b6042013-11-15 16:06:05 +0100771 platform_driver_unregister(&tegra_sor_driver);
Thierry Redingdec72732013-09-03 08:45:46 +0200772 platform_driver_unregister(&tegra_dsi_driver);
Thierry Reding776dc382013-10-14 14:43:22 +0200773 platform_driver_unregister(&tegra_dc_driver);
774 host1x_driver_unregister(&host1x_drm_driver);
775}
776module_exit(host1x_drm_exit);
777
778MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
779MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
780MODULE_LICENSE("GPL v2");