EDAC: i7core, sb_edac: Don't return NOTIFY_BAD from mce_decoder callback
[linux/fpc-iii.git] / drivers / gpu / drm / tegra / drm.c
blob8e6b18caa706d207b0a1ef66e3a5299e54bc3e54
1 /*
2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2013 NVIDIA CORPORATION. All rights reserved.
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 */
10 #include <linux/host1x.h>
11 #include <linux/iommu.h>
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_atomic_helper.h>
16 #include "drm.h"
17 #include "gem.h"
19 #define DRIVER_NAME "tegra"
20 #define DRIVER_DESC "NVIDIA Tegra graphics"
21 #define DRIVER_DATE "20120330"
22 #define DRIVER_MAJOR 0
23 #define DRIVER_MINOR 0
24 #define DRIVER_PATCHLEVEL 0
26 struct tegra_drm_file {
27 struct list_head contexts;
30 static void tegra_atomic_schedule(struct tegra_drm *tegra,
31 struct drm_atomic_state *state)
33 tegra->commit.state = state;
34 schedule_work(&tegra->commit.work);
37 static void tegra_atomic_complete(struct tegra_drm *tegra,
38 struct drm_atomic_state *state)
40 struct drm_device *drm = tegra->drm;
43 * Everything below can be run asynchronously without the need to grab
44 * any modeset locks at all under one condition: It must be guaranteed
45 * that the asynchronous work has either been cancelled (if the driver
46 * supports it, which at least requires that the framebuffers get
47 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
48 * before the new state gets committed on the software side with
49 * drm_atomic_helper_swap_state().
51 * This scheme allows new atomic state updates to be prepared and
52 * checked in parallel to the asynchronous completion of the previous
53 * update. Which is important since compositors need to figure out the
54 * composition of the next frame right after having submitted the
55 * current layout.
58 drm_atomic_helper_commit_modeset_disables(drm, state);
59 drm_atomic_helper_commit_planes(drm, state, false);
60 drm_atomic_helper_commit_modeset_enables(drm, state);
62 drm_atomic_helper_wait_for_vblanks(drm, state);
64 drm_atomic_helper_cleanup_planes(drm, state);
65 drm_atomic_state_free(state);
68 static void tegra_atomic_work(struct work_struct *work)
70 struct tegra_drm *tegra = container_of(work, struct tegra_drm,
71 commit.work);
73 tegra_atomic_complete(tegra, tegra->commit.state);
76 static int tegra_atomic_commit(struct drm_device *drm,
77 struct drm_atomic_state *state, bool async)
79 struct tegra_drm *tegra = drm->dev_private;
80 int err;
82 err = drm_atomic_helper_prepare_planes(drm, state);
83 if (err)
84 return err;
86 /* serialize outstanding asynchronous commits */
87 mutex_lock(&tegra->commit.lock);
88 flush_work(&tegra->commit.work);
91 * This is the point of no return - everything below never fails except
92 * when the hw goes bonghits. Which means we can commit the new state on
93 * the software side now.
96 drm_atomic_helper_swap_state(drm, state);
98 if (async)
99 tegra_atomic_schedule(tegra, state);
100 else
101 tegra_atomic_complete(tegra, state);
103 mutex_unlock(&tegra->commit.lock);
104 return 0;
107 static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
108 .fb_create = tegra_fb_create,
109 #ifdef CONFIG_DRM_FBDEV_EMULATION
110 .output_poll_changed = tegra_fb_output_poll_changed,
111 #endif
112 .atomic_check = drm_atomic_helper_check,
113 .atomic_commit = tegra_atomic_commit,
116 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
118 struct host1x_device *device = to_host1x_device(drm->dev);
119 struct tegra_drm *tegra;
120 int err;
122 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
123 if (!tegra)
124 return -ENOMEM;
126 if (iommu_present(&platform_bus_type)) {
127 struct iommu_domain_geometry *geometry;
128 u64 start, end;
130 tegra->domain = iommu_domain_alloc(&platform_bus_type);
131 if (!tegra->domain) {
132 err = -ENOMEM;
133 goto free;
136 geometry = &tegra->domain->geometry;
137 start = geometry->aperture_start;
138 end = geometry->aperture_end;
140 DRM_DEBUG_DRIVER("IOMMU aperture initialized (%#llx-%#llx)\n",
141 start, end);
142 drm_mm_init(&tegra->mm, start, end - start + 1);
145 mutex_init(&tegra->clients_lock);
146 INIT_LIST_HEAD(&tegra->clients);
148 mutex_init(&tegra->commit.lock);
149 INIT_WORK(&tegra->commit.work, tegra_atomic_work);
151 drm->dev_private = tegra;
152 tegra->drm = drm;
154 drm_mode_config_init(drm);
156 drm->mode_config.min_width = 0;
157 drm->mode_config.min_height = 0;
159 drm->mode_config.max_width = 4096;
160 drm->mode_config.max_height = 4096;
162 drm->mode_config.funcs = &tegra_drm_mode_funcs;
164 err = tegra_drm_fb_prepare(drm);
165 if (err < 0)
166 goto config;
168 drm_kms_helper_poll_init(drm);
170 err = host1x_device_init(device);
171 if (err < 0)
172 goto fbdev;
175 * We don't use the drm_irq_install() helpers provided by the DRM
176 * core, so we need to set this manually in order to allow the
177 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
179 drm->irq_enabled = true;
181 /* syncpoints are used for full 32-bit hardware VBLANK counters */
182 drm->max_vblank_count = 0xffffffff;
183 drm->vblank_disable_allowed = true;
185 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
186 if (err < 0)
187 goto device;
189 drm_mode_config_reset(drm);
191 err = tegra_drm_fb_init(drm);
192 if (err < 0)
193 goto vblank;
195 return 0;
197 vblank:
198 drm_vblank_cleanup(drm);
199 device:
200 host1x_device_exit(device);
201 fbdev:
202 drm_kms_helper_poll_fini(drm);
203 tegra_drm_fb_free(drm);
204 config:
205 drm_mode_config_cleanup(drm);
207 if (tegra->domain) {
208 iommu_domain_free(tegra->domain);
209 drm_mm_takedown(&tegra->mm);
211 free:
212 kfree(tegra);
213 return err;
216 static int tegra_drm_unload(struct drm_device *drm)
218 struct host1x_device *device = to_host1x_device(drm->dev);
219 struct tegra_drm *tegra = drm->dev_private;
220 int err;
222 drm_kms_helper_poll_fini(drm);
223 tegra_drm_fb_exit(drm);
224 drm_mode_config_cleanup(drm);
225 drm_vblank_cleanup(drm);
227 err = host1x_device_exit(device);
228 if (err < 0)
229 return err;
231 if (tegra->domain) {
232 iommu_domain_free(tegra->domain);
233 drm_mm_takedown(&tegra->mm);
236 kfree(tegra);
238 return 0;
241 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
243 struct tegra_drm_file *fpriv;
245 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
246 if (!fpriv)
247 return -ENOMEM;
249 INIT_LIST_HEAD(&fpriv->contexts);
250 filp->driver_priv = fpriv;
252 return 0;
255 static void tegra_drm_context_free(struct tegra_drm_context *context)
257 context->client->ops->close_channel(context);
258 kfree(context);
261 static void tegra_drm_lastclose(struct drm_device *drm)
263 #ifdef CONFIG_DRM_FBDEV_EMULATION
264 struct tegra_drm *tegra = drm->dev_private;
266 tegra_fbdev_restore_mode(tegra->fbdev);
267 #endif
270 static struct host1x_bo *
271 host1x_bo_lookup(struct drm_device *drm, struct drm_file *file, u32 handle)
273 struct drm_gem_object *gem;
274 struct tegra_bo *bo;
276 gem = drm_gem_object_lookup(drm, file, handle);
277 if (!gem)
278 return NULL;
280 drm_gem_object_unreference_unlocked(gem);
282 bo = to_tegra_bo(gem);
283 return &bo->base;
286 static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
287 struct drm_tegra_reloc __user *src,
288 struct drm_device *drm,
289 struct drm_file *file)
291 u32 cmdbuf, target;
292 int err;
294 err = get_user(cmdbuf, &src->cmdbuf.handle);
295 if (err < 0)
296 return err;
298 err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
299 if (err < 0)
300 return err;
302 err = get_user(target, &src->target.handle);
303 if (err < 0)
304 return err;
306 err = get_user(dest->target.offset, &src->target.offset);
307 if (err < 0)
308 return err;
310 err = get_user(dest->shift, &src->shift);
311 if (err < 0)
312 return err;
314 dest->cmdbuf.bo = host1x_bo_lookup(drm, file, cmdbuf);
315 if (!dest->cmdbuf.bo)
316 return -ENOENT;
318 dest->target.bo = host1x_bo_lookup(drm, file, target);
319 if (!dest->target.bo)
320 return -ENOENT;
322 return 0;
325 int tegra_drm_submit(struct tegra_drm_context *context,
326 struct drm_tegra_submit *args, struct drm_device *drm,
327 struct drm_file *file)
329 unsigned int num_cmdbufs = args->num_cmdbufs;
330 unsigned int num_relocs = args->num_relocs;
331 unsigned int num_waitchks = args->num_waitchks;
332 struct drm_tegra_cmdbuf __user *cmdbufs =
333 (void __user *)(uintptr_t)args->cmdbufs;
334 struct drm_tegra_reloc __user *relocs =
335 (void __user *)(uintptr_t)args->relocs;
336 struct drm_tegra_waitchk __user *waitchks =
337 (void __user *)(uintptr_t)args->waitchks;
338 struct drm_tegra_syncpt syncpt;
339 struct host1x_job *job;
340 int err;
342 /* We don't yet support other than one syncpt_incr struct per submit */
343 if (args->num_syncpts != 1)
344 return -EINVAL;
346 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
347 args->num_relocs, args->num_waitchks);
348 if (!job)
349 return -ENOMEM;
351 job->num_relocs = args->num_relocs;
352 job->num_waitchk = args->num_waitchks;
353 job->client = (u32)args->context;
354 job->class = context->client->base.class;
355 job->serialize = true;
357 while (num_cmdbufs) {
358 struct drm_tegra_cmdbuf cmdbuf;
359 struct host1x_bo *bo;
361 if (copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf))) {
362 err = -EFAULT;
363 goto fail;
366 bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
367 if (!bo) {
368 err = -ENOENT;
369 goto fail;
372 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
373 num_cmdbufs--;
374 cmdbufs++;
377 /* copy and resolve relocations from submit */
378 while (num_relocs--) {
379 err = host1x_reloc_copy_from_user(&job->relocarray[num_relocs],
380 &relocs[num_relocs], drm,
381 file);
382 if (err < 0)
383 goto fail;
386 if (copy_from_user(job->waitchk, waitchks,
387 sizeof(*waitchks) * num_waitchks)) {
388 err = -EFAULT;
389 goto fail;
392 if (copy_from_user(&syncpt, (void __user *)(uintptr_t)args->syncpts,
393 sizeof(syncpt))) {
394 err = -EFAULT;
395 goto fail;
398 job->is_addr_reg = context->client->ops->is_addr_reg;
399 job->syncpt_incrs = syncpt.incrs;
400 job->syncpt_id = syncpt.id;
401 job->timeout = 10000;
403 if (args->timeout && args->timeout < 10000)
404 job->timeout = args->timeout;
406 err = host1x_job_pin(job, context->client->base.dev);
407 if (err)
408 goto fail;
410 err = host1x_job_submit(job);
411 if (err)
412 goto fail_submit;
414 args->fence = job->syncpt_end;
416 host1x_job_put(job);
417 return 0;
419 fail_submit:
420 host1x_job_unpin(job);
421 fail:
422 host1x_job_put(job);
423 return err;
427 #ifdef CONFIG_DRM_TEGRA_STAGING
428 static struct tegra_drm_context *tegra_drm_get_context(__u64 context)
430 return (struct tegra_drm_context *)(uintptr_t)context;
433 static bool tegra_drm_file_owns_context(struct tegra_drm_file *file,
434 struct tegra_drm_context *context)
436 struct tegra_drm_context *ctx;
438 list_for_each_entry(ctx, &file->contexts, list)
439 if (ctx == context)
440 return true;
442 return false;
445 static int tegra_gem_create(struct drm_device *drm, void *data,
446 struct drm_file *file)
448 struct drm_tegra_gem_create *args = data;
449 struct tegra_bo *bo;
451 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
452 &args->handle);
453 if (IS_ERR(bo))
454 return PTR_ERR(bo);
456 return 0;
459 static int tegra_gem_mmap(struct drm_device *drm, void *data,
460 struct drm_file *file)
462 struct drm_tegra_gem_mmap *args = data;
463 struct drm_gem_object *gem;
464 struct tegra_bo *bo;
466 gem = drm_gem_object_lookup(drm, file, args->handle);
467 if (!gem)
468 return -EINVAL;
470 bo = to_tegra_bo(gem);
472 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
474 drm_gem_object_unreference_unlocked(gem);
476 return 0;
479 static int tegra_syncpt_read(struct drm_device *drm, void *data,
480 struct drm_file *file)
482 struct host1x *host = dev_get_drvdata(drm->dev->parent);
483 struct drm_tegra_syncpt_read *args = data;
484 struct host1x_syncpt *sp;
486 sp = host1x_syncpt_get(host, args->id);
487 if (!sp)
488 return -EINVAL;
490 args->value = host1x_syncpt_read_min(sp);
491 return 0;
494 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
495 struct drm_file *file)
497 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
498 struct drm_tegra_syncpt_incr *args = data;
499 struct host1x_syncpt *sp;
501 sp = host1x_syncpt_get(host1x, args->id);
502 if (!sp)
503 return -EINVAL;
505 return host1x_syncpt_incr(sp);
508 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
509 struct drm_file *file)
511 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
512 struct drm_tegra_syncpt_wait *args = data;
513 struct host1x_syncpt *sp;
515 sp = host1x_syncpt_get(host1x, args->id);
516 if (!sp)
517 return -EINVAL;
519 return host1x_syncpt_wait(sp, args->thresh, args->timeout,
520 &args->value);
523 static int tegra_open_channel(struct drm_device *drm, void *data,
524 struct drm_file *file)
526 struct tegra_drm_file *fpriv = file->driver_priv;
527 struct tegra_drm *tegra = drm->dev_private;
528 struct drm_tegra_open_channel *args = data;
529 struct tegra_drm_context *context;
530 struct tegra_drm_client *client;
531 int err = -ENODEV;
533 context = kzalloc(sizeof(*context), GFP_KERNEL);
534 if (!context)
535 return -ENOMEM;
537 list_for_each_entry(client, &tegra->clients, list)
538 if (client->base.class == args->client) {
539 err = client->ops->open_channel(client, context);
540 if (err)
541 break;
543 list_add(&context->list, &fpriv->contexts);
544 args->context = (uintptr_t)context;
545 context->client = client;
546 return 0;
549 kfree(context);
550 return err;
553 static int tegra_close_channel(struct drm_device *drm, void *data,
554 struct drm_file *file)
556 struct tegra_drm_file *fpriv = file->driver_priv;
557 struct drm_tegra_close_channel *args = data;
558 struct tegra_drm_context *context;
560 context = tegra_drm_get_context(args->context);
562 if (!tegra_drm_file_owns_context(fpriv, context))
563 return -EINVAL;
565 list_del(&context->list);
566 tegra_drm_context_free(context);
568 return 0;
571 static int tegra_get_syncpt(struct drm_device *drm, void *data,
572 struct drm_file *file)
574 struct tegra_drm_file *fpriv = file->driver_priv;
575 struct drm_tegra_get_syncpt *args = data;
576 struct tegra_drm_context *context;
577 struct host1x_syncpt *syncpt;
579 context = tegra_drm_get_context(args->context);
581 if (!tegra_drm_file_owns_context(fpriv, context))
582 return -ENODEV;
584 if (args->index >= context->client->base.num_syncpts)
585 return -EINVAL;
587 syncpt = context->client->base.syncpts[args->index];
588 args->id = host1x_syncpt_id(syncpt);
590 return 0;
593 static int tegra_submit(struct drm_device *drm, void *data,
594 struct drm_file *file)
596 struct tegra_drm_file *fpriv = file->driver_priv;
597 struct drm_tegra_submit *args = data;
598 struct tegra_drm_context *context;
600 context = tegra_drm_get_context(args->context);
602 if (!tegra_drm_file_owns_context(fpriv, context))
603 return -ENODEV;
605 return context->client->ops->submit(context, args, drm, file);
608 static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
609 struct drm_file *file)
611 struct tegra_drm_file *fpriv = file->driver_priv;
612 struct drm_tegra_get_syncpt_base *args = data;
613 struct tegra_drm_context *context;
614 struct host1x_syncpt_base *base;
615 struct host1x_syncpt *syncpt;
617 context = tegra_drm_get_context(args->context);
619 if (!tegra_drm_file_owns_context(fpriv, context))
620 return -ENODEV;
622 if (args->syncpt >= context->client->base.num_syncpts)
623 return -EINVAL;
625 syncpt = context->client->base.syncpts[args->syncpt];
627 base = host1x_syncpt_get_base(syncpt);
628 if (!base)
629 return -ENXIO;
631 args->id = host1x_syncpt_base_id(base);
633 return 0;
636 static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
637 struct drm_file *file)
639 struct drm_tegra_gem_set_tiling *args = data;
640 enum tegra_bo_tiling_mode mode;
641 struct drm_gem_object *gem;
642 unsigned long value = 0;
643 struct tegra_bo *bo;
645 switch (args->mode) {
646 case DRM_TEGRA_GEM_TILING_MODE_PITCH:
647 mode = TEGRA_BO_TILING_MODE_PITCH;
649 if (args->value != 0)
650 return -EINVAL;
652 break;
654 case DRM_TEGRA_GEM_TILING_MODE_TILED:
655 mode = TEGRA_BO_TILING_MODE_TILED;
657 if (args->value != 0)
658 return -EINVAL;
660 break;
662 case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
663 mode = TEGRA_BO_TILING_MODE_BLOCK;
665 if (args->value > 5)
666 return -EINVAL;
668 value = args->value;
669 break;
671 default:
672 return -EINVAL;
675 gem = drm_gem_object_lookup(drm, file, args->handle);
676 if (!gem)
677 return -ENOENT;
679 bo = to_tegra_bo(gem);
681 bo->tiling.mode = mode;
682 bo->tiling.value = value;
684 drm_gem_object_unreference_unlocked(gem);
686 return 0;
689 static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
690 struct drm_file *file)
692 struct drm_tegra_gem_get_tiling *args = data;
693 struct drm_gem_object *gem;
694 struct tegra_bo *bo;
695 int err = 0;
697 gem = drm_gem_object_lookup(drm, file, args->handle);
698 if (!gem)
699 return -ENOENT;
701 bo = to_tegra_bo(gem);
703 switch (bo->tiling.mode) {
704 case TEGRA_BO_TILING_MODE_PITCH:
705 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
706 args->value = 0;
707 break;
709 case TEGRA_BO_TILING_MODE_TILED:
710 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
711 args->value = 0;
712 break;
714 case TEGRA_BO_TILING_MODE_BLOCK:
715 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
716 args->value = bo->tiling.value;
717 break;
719 default:
720 err = -EINVAL;
721 break;
724 drm_gem_object_unreference_unlocked(gem);
726 return err;
729 static int tegra_gem_set_flags(struct drm_device *drm, void *data,
730 struct drm_file *file)
732 struct drm_tegra_gem_set_flags *args = data;
733 struct drm_gem_object *gem;
734 struct tegra_bo *bo;
736 if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
737 return -EINVAL;
739 gem = drm_gem_object_lookup(drm, file, args->handle);
740 if (!gem)
741 return -ENOENT;
743 bo = to_tegra_bo(gem);
744 bo->flags = 0;
746 if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
747 bo->flags |= TEGRA_BO_BOTTOM_UP;
749 drm_gem_object_unreference_unlocked(gem);
751 return 0;
754 static int tegra_gem_get_flags(struct drm_device *drm, void *data,
755 struct drm_file *file)
757 struct drm_tegra_gem_get_flags *args = data;
758 struct drm_gem_object *gem;
759 struct tegra_bo *bo;
761 gem = drm_gem_object_lookup(drm, file, args->handle);
762 if (!gem)
763 return -ENOENT;
765 bo = to_tegra_bo(gem);
766 args->flags = 0;
768 if (bo->flags & TEGRA_BO_BOTTOM_UP)
769 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
771 drm_gem_object_unreference_unlocked(gem);
773 return 0;
775 #endif
777 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
778 #ifdef CONFIG_DRM_TEGRA_STAGING
779 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, 0),
780 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, 0),
781 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read, 0),
782 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr, 0),
783 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait, 0),
784 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel, 0),
785 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel, 0),
786 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt, 0),
787 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit, 0),
788 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base, 0),
789 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling, 0),
790 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling, 0),
791 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags, 0),
792 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags, 0),
793 #endif
796 static const struct file_operations tegra_drm_fops = {
797 .owner = THIS_MODULE,
798 .open = drm_open,
799 .release = drm_release,
800 .unlocked_ioctl = drm_ioctl,
801 .mmap = tegra_drm_mmap,
802 .poll = drm_poll,
803 .read = drm_read,
804 #ifdef CONFIG_COMPAT
805 .compat_ioctl = drm_compat_ioctl,
806 #endif
807 .llseek = noop_llseek,
810 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm,
811 unsigned int pipe)
813 struct drm_crtc *crtc;
815 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
816 if (pipe == drm_crtc_index(crtc))
817 return crtc;
820 return NULL;
823 static u32 tegra_drm_get_vblank_counter(struct drm_device *drm,
824 unsigned int pipe)
826 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
827 struct tegra_dc *dc = to_tegra_dc(crtc);
829 if (!crtc)
830 return 0;
832 return tegra_dc_get_vblank_counter(dc);
835 static int tegra_drm_enable_vblank(struct drm_device *drm, unsigned int pipe)
837 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
838 struct tegra_dc *dc = to_tegra_dc(crtc);
840 if (!crtc)
841 return -ENODEV;
843 tegra_dc_enable_vblank(dc);
845 return 0;
848 static void tegra_drm_disable_vblank(struct drm_device *drm, unsigned int pipe)
850 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
851 struct tegra_dc *dc = to_tegra_dc(crtc);
853 if (crtc)
854 tegra_dc_disable_vblank(dc);
857 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
859 struct tegra_drm_file *fpriv = file->driver_priv;
860 struct tegra_drm_context *context, *tmp;
862 list_for_each_entry_safe(context, tmp, &fpriv->contexts, list)
863 tegra_drm_context_free(context);
865 kfree(fpriv);
868 #ifdef CONFIG_DEBUG_FS
869 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
871 struct drm_info_node *node = (struct drm_info_node *)s->private;
872 struct drm_device *drm = node->minor->dev;
873 struct drm_framebuffer *fb;
875 mutex_lock(&drm->mode_config.fb_lock);
877 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
878 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
879 fb->base.id, fb->width, fb->height, fb->depth,
880 fb->bits_per_pixel,
881 atomic_read(&fb->refcount.refcount));
884 mutex_unlock(&drm->mode_config.fb_lock);
886 return 0;
889 static int tegra_debugfs_iova(struct seq_file *s, void *data)
891 struct drm_info_node *node = (struct drm_info_node *)s->private;
892 struct drm_device *drm = node->minor->dev;
893 struct tegra_drm *tegra = drm->dev_private;
895 return drm_mm_dump_table(s, &tegra->mm);
898 static struct drm_info_list tegra_debugfs_list[] = {
899 { "framebuffers", tegra_debugfs_framebuffers, 0 },
900 { "iova", tegra_debugfs_iova, 0 },
903 static int tegra_debugfs_init(struct drm_minor *minor)
905 return drm_debugfs_create_files(tegra_debugfs_list,
906 ARRAY_SIZE(tegra_debugfs_list),
907 minor->debugfs_root, minor);
910 static void tegra_debugfs_cleanup(struct drm_minor *minor)
912 drm_debugfs_remove_files(tegra_debugfs_list,
913 ARRAY_SIZE(tegra_debugfs_list), minor);
915 #endif
917 static struct drm_driver tegra_drm_driver = {
918 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
919 DRIVER_ATOMIC,
920 .load = tegra_drm_load,
921 .unload = tegra_drm_unload,
922 .open = tegra_drm_open,
923 .preclose = tegra_drm_preclose,
924 .lastclose = tegra_drm_lastclose,
926 .get_vblank_counter = tegra_drm_get_vblank_counter,
927 .enable_vblank = tegra_drm_enable_vblank,
928 .disable_vblank = tegra_drm_disable_vblank,
930 #if defined(CONFIG_DEBUG_FS)
931 .debugfs_init = tegra_debugfs_init,
932 .debugfs_cleanup = tegra_debugfs_cleanup,
933 #endif
935 .gem_free_object = tegra_bo_free_object,
936 .gem_vm_ops = &tegra_bo_vm_ops,
938 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
939 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
940 .gem_prime_export = tegra_gem_prime_export,
941 .gem_prime_import = tegra_gem_prime_import,
943 .dumb_create = tegra_bo_dumb_create,
944 .dumb_map_offset = tegra_bo_dumb_map_offset,
945 .dumb_destroy = drm_gem_dumb_destroy,
947 .ioctls = tegra_drm_ioctls,
948 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
949 .fops = &tegra_drm_fops,
951 .name = DRIVER_NAME,
952 .desc = DRIVER_DESC,
953 .date = DRIVER_DATE,
954 .major = DRIVER_MAJOR,
955 .minor = DRIVER_MINOR,
956 .patchlevel = DRIVER_PATCHLEVEL,
959 int tegra_drm_register_client(struct tegra_drm *tegra,
960 struct tegra_drm_client *client)
962 mutex_lock(&tegra->clients_lock);
963 list_add_tail(&client->list, &tegra->clients);
964 mutex_unlock(&tegra->clients_lock);
966 return 0;
969 int tegra_drm_unregister_client(struct tegra_drm *tegra,
970 struct tegra_drm_client *client)
972 mutex_lock(&tegra->clients_lock);
973 list_del_init(&client->list);
974 mutex_unlock(&tegra->clients_lock);
976 return 0;
979 static int host1x_drm_probe(struct host1x_device *dev)
981 struct drm_driver *driver = &tegra_drm_driver;
982 struct drm_device *drm;
983 int err;
985 drm = drm_dev_alloc(driver, &dev->dev);
986 if (!drm)
987 return -ENOMEM;
989 dev_set_drvdata(&dev->dev, drm);
991 err = drm_dev_register(drm, 0);
992 if (err < 0)
993 goto unref;
995 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", driver->name,
996 driver->major, driver->minor, driver->patchlevel,
997 driver->date, drm->primary->index);
999 return 0;
1001 unref:
1002 drm_dev_unref(drm);
1003 return err;
1006 static int host1x_drm_remove(struct host1x_device *dev)
1008 struct drm_device *drm = dev_get_drvdata(&dev->dev);
1010 drm_dev_unregister(drm);
1011 drm_dev_unref(drm);
1013 return 0;
1016 #ifdef CONFIG_PM_SLEEP
1017 static int host1x_drm_suspend(struct device *dev)
1019 struct drm_device *drm = dev_get_drvdata(dev);
1020 struct tegra_drm *tegra = drm->dev_private;
1022 drm_kms_helper_poll_disable(drm);
1023 tegra_drm_fb_suspend(drm);
1025 tegra->state = drm_atomic_helper_suspend(drm);
1026 if (IS_ERR(tegra->state)) {
1027 tegra_drm_fb_resume(drm);
1028 drm_kms_helper_poll_enable(drm);
1029 return PTR_ERR(tegra->state);
1032 return 0;
1035 static int host1x_drm_resume(struct device *dev)
1037 struct drm_device *drm = dev_get_drvdata(dev);
1038 struct tegra_drm *tegra = drm->dev_private;
1040 drm_atomic_helper_resume(drm, tegra->state);
1041 tegra_drm_fb_resume(drm);
1042 drm_kms_helper_poll_enable(drm);
1044 return 0;
1046 #endif
1048 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
1049 host1x_drm_resume);
1051 static const struct of_device_id host1x_drm_subdevs[] = {
1052 { .compatible = "nvidia,tegra20-dc", },
1053 { .compatible = "nvidia,tegra20-hdmi", },
1054 { .compatible = "nvidia,tegra20-gr2d", },
1055 { .compatible = "nvidia,tegra20-gr3d", },
1056 { .compatible = "nvidia,tegra30-dc", },
1057 { .compatible = "nvidia,tegra30-hdmi", },
1058 { .compatible = "nvidia,tegra30-gr2d", },
1059 { .compatible = "nvidia,tegra30-gr3d", },
1060 { .compatible = "nvidia,tegra114-dsi", },
1061 { .compatible = "nvidia,tegra114-hdmi", },
1062 { .compatible = "nvidia,tegra114-gr3d", },
1063 { .compatible = "nvidia,tegra124-dc", },
1064 { .compatible = "nvidia,tegra124-sor", },
1065 { .compatible = "nvidia,tegra124-hdmi", },
1066 { .compatible = "nvidia,tegra124-dsi", },
1067 { .compatible = "nvidia,tegra132-dsi", },
1068 { .compatible = "nvidia,tegra210-dc", },
1069 { .compatible = "nvidia,tegra210-dsi", },
1070 { .compatible = "nvidia,tegra210-sor", },
1071 { .compatible = "nvidia,tegra210-sor1", },
1072 { /* sentinel */ }
1075 static struct host1x_driver host1x_drm_driver = {
1076 .driver = {
1077 .name = "drm",
1078 .pm = &host1x_drm_pm_ops,
1080 .probe = host1x_drm_probe,
1081 .remove = host1x_drm_remove,
1082 .subdevs = host1x_drm_subdevs,
1085 static struct platform_driver * const drivers[] = {
1086 &tegra_dc_driver,
1087 &tegra_hdmi_driver,
1088 &tegra_dsi_driver,
1089 &tegra_dpaux_driver,
1090 &tegra_sor_driver,
1091 &tegra_gr2d_driver,
1092 &tegra_gr3d_driver,
1095 static int __init host1x_drm_init(void)
1097 int err;
1099 err = host1x_driver_register(&host1x_drm_driver);
1100 if (err < 0)
1101 return err;
1103 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1104 if (err < 0)
1105 goto unregister_host1x;
1107 return 0;
1109 unregister_host1x:
1110 host1x_driver_unregister(&host1x_drm_driver);
1111 return err;
1113 module_init(host1x_drm_init);
1115 static void __exit host1x_drm_exit(void)
1117 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1118 host1x_driver_unregister(&host1x_drm_driver);
1120 module_exit(host1x_drm_exit);
1122 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1123 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1124 MODULE_LICENSE("GPL v2");