1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Avionic Design GmbH
4 * Copyright (C) 2012-2016 NVIDIA CORPORATION. All rights reserved.
7 #include <linux/bitops.h>
8 #include <linux/host1x.h>
10 #include <linux/iommu.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
15 #include <drm/drm_aperture.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_debugfs.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_fourcc.h>
21 #include <drm/drm_framebuffer.h>
22 #include <drm/drm_ioctl.h>
23 #include <drm/drm_prime.h>
24 #include <drm/drm_vblank.h>
26 #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
27 #include <asm/dma-iommu.h>
35 #define DRIVER_NAME "tegra"
36 #define DRIVER_DESC "NVIDIA Tegra graphics"
37 #define DRIVER_DATE "20120330"
38 #define DRIVER_MAJOR 1
39 #define DRIVER_MINOR 0
40 #define DRIVER_PATCHLEVEL 0
42 #define CARVEOUT_SZ SZ_64M
43 #define CDMA_GATHER_FETCHES_MAX_NB 16383
45 static int tegra_atomic_check(struct drm_device *drm,
46 struct drm_atomic_state *state)
50 err = drm_atomic_helper_check(drm, state);
54 return tegra_display_hub_atomic_check(drm, state);
57 static const struct drm_mode_config_funcs tegra_drm_mode_config_funcs = {
58 .fb_create = tegra_fb_create,
59 .atomic_check = tegra_atomic_check,
60 .atomic_commit = drm_atomic_helper_commit,
63 static void tegra_atomic_post_commit(struct drm_device *drm,
64 struct drm_atomic_state *old_state)
66 struct drm_crtc_state *old_crtc_state __maybe_unused;
67 struct drm_crtc *crtc;
70 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i)
71 tegra_crtc_atomic_post_commit(crtc, old_state);
74 static void tegra_atomic_commit_tail(struct drm_atomic_state *old_state)
76 struct drm_device *drm = old_state->dev;
77 struct tegra_drm *tegra = drm->dev_private;
80 bool fence_cookie = dma_fence_begin_signalling();
82 drm_atomic_helper_commit_modeset_disables(drm, old_state);
83 tegra_display_hub_atomic_commit(drm, old_state);
84 drm_atomic_helper_commit_planes(drm, old_state, 0);
85 drm_atomic_helper_commit_modeset_enables(drm, old_state);
86 drm_atomic_helper_commit_hw_done(old_state);
87 dma_fence_end_signalling(fence_cookie);
88 drm_atomic_helper_wait_for_vblanks(drm, old_state);
89 drm_atomic_helper_cleanup_planes(drm, old_state);
91 drm_atomic_helper_commit_tail_rpm(old_state);
94 tegra_atomic_post_commit(drm, old_state);
97 static const struct drm_mode_config_helper_funcs
98 tegra_drm_mode_config_helpers = {
99 .atomic_commit_tail = tegra_atomic_commit_tail,
102 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
104 struct tegra_drm_file *fpriv;
106 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
110 idr_init_base(&fpriv->legacy_contexts, 1);
111 xa_init_flags(&fpriv->contexts, XA_FLAGS_ALLOC1);
112 xa_init(&fpriv->syncpoints);
113 mutex_init(&fpriv->lock);
114 filp->driver_priv = fpriv;
119 static void tegra_drm_context_free(struct tegra_drm_context *context)
121 context->client->ops->close_channel(context);
122 pm_runtime_put(context->client->base.dev);
126 static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
127 struct drm_tegra_reloc __user *src,
128 struct drm_device *drm,
129 struct drm_file *file)
134 err = get_user(cmdbuf, &src->cmdbuf.handle);
138 err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
142 err = get_user(target, &src->target.handle);
146 err = get_user(dest->target.offset, &src->target.offset);
150 err = get_user(dest->shift, &src->shift);
154 dest->flags = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
156 dest->cmdbuf.bo = tegra_gem_lookup(file, cmdbuf);
157 if (!dest->cmdbuf.bo)
160 dest->target.bo = tegra_gem_lookup(file, target);
161 if (!dest->target.bo)
167 int tegra_drm_submit(struct tegra_drm_context *context,
168 struct drm_tegra_submit *args, struct drm_device *drm,
169 struct drm_file *file)
171 struct host1x_client *client = &context->client->base;
172 unsigned int num_cmdbufs = args->num_cmdbufs;
173 unsigned int num_relocs = args->num_relocs;
174 struct drm_tegra_cmdbuf __user *user_cmdbufs;
175 struct drm_tegra_reloc __user *user_relocs;
176 struct drm_tegra_syncpt __user *user_syncpt;
177 struct drm_tegra_syncpt syncpt;
178 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
179 struct drm_gem_object **refs;
180 struct host1x_syncpt *sp = NULL;
181 struct host1x_job *job;
182 unsigned int num_refs;
185 user_cmdbufs = u64_to_user_ptr(args->cmdbufs);
186 user_relocs = u64_to_user_ptr(args->relocs);
187 user_syncpt = u64_to_user_ptr(args->syncpts);
189 /* We don't yet support other than one syncpt_incr struct per submit */
190 if (args->num_syncpts != 1)
193 /* We don't yet support waitchks */
194 if (args->num_waitchks != 0)
197 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
198 args->num_relocs, false);
202 job->num_relocs = args->num_relocs;
203 job->client = client;
204 job->class = client->class;
205 job->serialize = true;
206 job->syncpt_recovery = true;
209 * Track referenced BOs so that they can be unreferenced after the
210 * submission is complete.
212 num_refs = num_cmdbufs + num_relocs * 2;
214 refs = kmalloc_array(num_refs, sizeof(*refs), GFP_KERNEL);
220 /* reuse as an iterator later */
223 while (num_cmdbufs) {
224 struct drm_tegra_cmdbuf cmdbuf;
225 struct host1x_bo *bo;
226 struct tegra_bo *obj;
229 if (copy_from_user(&cmdbuf, user_cmdbufs, sizeof(cmdbuf))) {
235 * The maximum number of CDMA gather fetches is 16383, a higher
236 * value means the words count is malformed.
238 if (cmdbuf.words > CDMA_GATHER_FETCHES_MAX_NB) {
243 bo = tegra_gem_lookup(file, cmdbuf.handle);
249 offset = (u64)cmdbuf.offset + (u64)cmdbuf.words * sizeof(u32);
250 obj = host1x_to_tegra_bo(bo);
251 refs[num_refs++] = &obj->gem;
254 * Gather buffer base address must be 4-bytes aligned,
255 * unaligned offset is malformed and cause commands stream
256 * corruption on the buffer address relocation.
258 if (offset & 3 || offset > obj->gem.size) {
263 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
268 /* copy and resolve relocations from submit */
269 while (num_relocs--) {
270 struct host1x_reloc *reloc;
271 struct tegra_bo *obj;
273 err = host1x_reloc_copy_from_user(&job->relocs[num_relocs],
274 &user_relocs[num_relocs], drm,
279 reloc = &job->relocs[num_relocs];
280 obj = host1x_to_tegra_bo(reloc->cmdbuf.bo);
281 refs[num_refs++] = &obj->gem;
284 * The unaligned cmdbuf offset will cause an unaligned write
285 * during of the relocations patching, corrupting the commands
288 if (reloc->cmdbuf.offset & 3 ||
289 reloc->cmdbuf.offset >= obj->gem.size) {
294 obj = host1x_to_tegra_bo(reloc->target.bo);
295 refs[num_refs++] = &obj->gem;
297 if (reloc->target.offset >= obj->gem.size) {
303 if (copy_from_user(&syncpt, user_syncpt, sizeof(syncpt))) {
308 /* Syncpoint ref will be dropped on job release. */
309 sp = host1x_syncpt_get_by_id(host1x, syncpt.id);
315 job->is_addr_reg = context->client->ops->is_addr_reg;
316 job->is_valid_class = context->client->ops->is_valid_class;
317 job->syncpt_incrs = syncpt.incrs;
319 job->timeout = 10000;
321 if (args->timeout && args->timeout < 10000)
322 job->timeout = args->timeout;
324 err = host1x_job_pin(job, context->client->base.dev);
328 err = host1x_job_submit(job);
330 host1x_job_unpin(job);
334 args->fence = job->syncpt_end;
338 drm_gem_object_put(refs[num_refs]);
348 #ifdef CONFIG_DRM_TEGRA_STAGING
349 static int tegra_gem_create(struct drm_device *drm, void *data,
350 struct drm_file *file)
352 struct drm_tegra_gem_create *args = data;
355 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
363 static int tegra_gem_mmap(struct drm_device *drm, void *data,
364 struct drm_file *file)
366 struct drm_tegra_gem_mmap *args = data;
367 struct drm_gem_object *gem;
370 gem = drm_gem_object_lookup(file, args->handle);
374 bo = to_tegra_bo(gem);
376 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
378 drm_gem_object_put(gem);
383 static int tegra_syncpt_read(struct drm_device *drm, void *data,
384 struct drm_file *file)
386 struct host1x *host = dev_get_drvdata(drm->dev->parent);
387 struct drm_tegra_syncpt_read *args = data;
388 struct host1x_syncpt *sp;
390 sp = host1x_syncpt_get_by_id_noref(host, args->id);
394 args->value = host1x_syncpt_read_min(sp);
398 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
399 struct drm_file *file)
401 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
402 struct drm_tegra_syncpt_incr *args = data;
403 struct host1x_syncpt *sp;
405 sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
409 return host1x_syncpt_incr(sp);
412 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
413 struct drm_file *file)
415 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
416 struct drm_tegra_syncpt_wait *args = data;
417 struct host1x_syncpt *sp;
419 sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
423 return host1x_syncpt_wait(sp, args->thresh,
424 msecs_to_jiffies(args->timeout),
428 static int tegra_client_open(struct tegra_drm_file *fpriv,
429 struct tegra_drm_client *client,
430 struct tegra_drm_context *context)
434 err = pm_runtime_resume_and_get(client->base.dev);
438 err = client->ops->open_channel(client, context);
440 pm_runtime_put(client->base.dev);
444 err = idr_alloc(&fpriv->legacy_contexts, context, 1, 0, GFP_KERNEL);
446 client->ops->close_channel(context);
447 pm_runtime_put(client->base.dev);
451 context->client = client;
457 static int tegra_open_channel(struct drm_device *drm, void *data,
458 struct drm_file *file)
460 struct tegra_drm_file *fpriv = file->driver_priv;
461 struct tegra_drm *tegra = drm->dev_private;
462 struct drm_tegra_open_channel *args = data;
463 struct tegra_drm_context *context;
464 struct tegra_drm_client *client;
467 context = kzalloc(sizeof(*context), GFP_KERNEL);
471 mutex_lock(&fpriv->lock);
473 list_for_each_entry(client, &tegra->clients, list)
474 if (client->base.class == args->client) {
475 err = tegra_client_open(fpriv, client, context);
479 args->context = context->id;
486 mutex_unlock(&fpriv->lock);
490 static int tegra_close_channel(struct drm_device *drm, void *data,
491 struct drm_file *file)
493 struct tegra_drm_file *fpriv = file->driver_priv;
494 struct drm_tegra_close_channel *args = data;
495 struct tegra_drm_context *context;
498 mutex_lock(&fpriv->lock);
500 context = idr_find(&fpriv->legacy_contexts, args->context);
506 idr_remove(&fpriv->legacy_contexts, context->id);
507 tegra_drm_context_free(context);
510 mutex_unlock(&fpriv->lock);
514 static int tegra_get_syncpt(struct drm_device *drm, void *data,
515 struct drm_file *file)
517 struct tegra_drm_file *fpriv = file->driver_priv;
518 struct drm_tegra_get_syncpt *args = data;
519 struct tegra_drm_context *context;
520 struct host1x_syncpt *syncpt;
523 mutex_lock(&fpriv->lock);
525 context = idr_find(&fpriv->legacy_contexts, args->context);
531 if (args->index >= context->client->base.num_syncpts) {
536 syncpt = context->client->base.syncpts[args->index];
537 args->id = host1x_syncpt_id(syncpt);
540 mutex_unlock(&fpriv->lock);
544 static int tegra_submit(struct drm_device *drm, void *data,
545 struct drm_file *file)
547 struct tegra_drm_file *fpriv = file->driver_priv;
548 struct drm_tegra_submit *args = data;
549 struct tegra_drm_context *context;
552 mutex_lock(&fpriv->lock);
554 context = idr_find(&fpriv->legacy_contexts, args->context);
560 err = context->client->ops->submit(context, args, drm, file);
563 mutex_unlock(&fpriv->lock);
567 static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
568 struct drm_file *file)
570 struct tegra_drm_file *fpriv = file->driver_priv;
571 struct drm_tegra_get_syncpt_base *args = data;
572 struct tegra_drm_context *context;
573 struct host1x_syncpt_base *base;
574 struct host1x_syncpt *syncpt;
577 mutex_lock(&fpriv->lock);
579 context = idr_find(&fpriv->legacy_contexts, args->context);
585 if (args->syncpt >= context->client->base.num_syncpts) {
590 syncpt = context->client->base.syncpts[args->syncpt];
592 base = host1x_syncpt_get_base(syncpt);
598 args->id = host1x_syncpt_base_id(base);
601 mutex_unlock(&fpriv->lock);
605 static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
606 struct drm_file *file)
608 struct drm_tegra_gem_set_tiling *args = data;
609 enum tegra_bo_tiling_mode mode;
610 struct drm_gem_object *gem;
611 unsigned long value = 0;
614 switch (args->mode) {
615 case DRM_TEGRA_GEM_TILING_MODE_PITCH:
616 mode = TEGRA_BO_TILING_MODE_PITCH;
618 if (args->value != 0)
623 case DRM_TEGRA_GEM_TILING_MODE_TILED:
624 mode = TEGRA_BO_TILING_MODE_TILED;
626 if (args->value != 0)
631 case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
632 mode = TEGRA_BO_TILING_MODE_BLOCK;
644 gem = drm_gem_object_lookup(file, args->handle);
648 bo = to_tegra_bo(gem);
650 bo->tiling.mode = mode;
651 bo->tiling.value = value;
653 drm_gem_object_put(gem);
658 static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
659 struct drm_file *file)
661 struct drm_tegra_gem_get_tiling *args = data;
662 struct drm_gem_object *gem;
666 gem = drm_gem_object_lookup(file, args->handle);
670 bo = to_tegra_bo(gem);
672 switch (bo->tiling.mode) {
673 case TEGRA_BO_TILING_MODE_PITCH:
674 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
678 case TEGRA_BO_TILING_MODE_TILED:
679 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
683 case TEGRA_BO_TILING_MODE_BLOCK:
684 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
685 args->value = bo->tiling.value;
693 drm_gem_object_put(gem);
698 static int tegra_gem_set_flags(struct drm_device *drm, void *data,
699 struct drm_file *file)
701 struct drm_tegra_gem_set_flags *args = data;
702 struct drm_gem_object *gem;
705 if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
708 gem = drm_gem_object_lookup(file, args->handle);
712 bo = to_tegra_bo(gem);
715 if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
716 bo->flags |= TEGRA_BO_BOTTOM_UP;
718 drm_gem_object_put(gem);
723 static int tegra_gem_get_flags(struct drm_device *drm, void *data,
724 struct drm_file *file)
726 struct drm_tegra_gem_get_flags *args = data;
727 struct drm_gem_object *gem;
730 gem = drm_gem_object_lookup(file, args->handle);
734 bo = to_tegra_bo(gem);
737 if (bo->flags & TEGRA_BO_BOTTOM_UP)
738 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
740 drm_gem_object_put(gem);
746 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
747 #ifdef CONFIG_DRM_TEGRA_STAGING
748 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_OPEN, tegra_drm_ioctl_channel_open,
750 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_CLOSE, tegra_drm_ioctl_channel_close,
752 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_MAP, tegra_drm_ioctl_channel_map,
754 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_UNMAP, tegra_drm_ioctl_channel_unmap,
756 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_SUBMIT, tegra_drm_ioctl_channel_submit,
758 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_ALLOCATE, tegra_drm_ioctl_syncpoint_allocate,
760 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_FREE, tegra_drm_ioctl_syncpoint_free,
762 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_WAIT, tegra_drm_ioctl_syncpoint_wait,
765 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_RENDER_ALLOW),
766 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_RENDER_ALLOW),
767 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read,
769 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr,
771 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait,
773 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel,
775 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel,
777 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt,
779 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit,
781 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base,
783 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling,
785 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling,
787 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags,
789 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags,
794 static const struct file_operations tegra_drm_fops = {
795 .owner = THIS_MODULE,
797 .release = drm_release,
798 .unlocked_ioctl = drm_ioctl,
799 .mmap = tegra_drm_mmap,
802 .compat_ioctl = drm_compat_ioctl,
803 .llseek = noop_llseek,
806 static int tegra_drm_context_cleanup(int id, void *p, void *data)
808 struct tegra_drm_context *context = p;
810 tegra_drm_context_free(context);
815 static void tegra_drm_postclose(struct drm_device *drm, struct drm_file *file)
817 struct tegra_drm_file *fpriv = file->driver_priv;
819 mutex_lock(&fpriv->lock);
820 idr_for_each(&fpriv->legacy_contexts, tegra_drm_context_cleanup, NULL);
821 tegra_drm_uapi_close_file(fpriv);
822 mutex_unlock(&fpriv->lock);
824 idr_destroy(&fpriv->legacy_contexts);
825 mutex_destroy(&fpriv->lock);
829 #ifdef CONFIG_DEBUG_FS
830 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
832 struct drm_info_node *node = (struct drm_info_node *)s->private;
833 struct drm_device *drm = node->minor->dev;
834 struct drm_framebuffer *fb;
836 mutex_lock(&drm->mode_config.fb_lock);
838 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
839 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
840 fb->base.id, fb->width, fb->height,
842 fb->format->cpp[0] * 8,
843 drm_framebuffer_read_refcount(fb));
846 mutex_unlock(&drm->mode_config.fb_lock);
851 static int tegra_debugfs_iova(struct seq_file *s, void *data)
853 struct drm_info_node *node = (struct drm_info_node *)s->private;
854 struct drm_device *drm = node->minor->dev;
855 struct tegra_drm *tegra = drm->dev_private;
856 struct drm_printer p = drm_seq_file_printer(s);
859 mutex_lock(&tegra->mm_lock);
860 drm_mm_print(&tegra->mm, &p);
861 mutex_unlock(&tegra->mm_lock);
867 static struct drm_info_list tegra_debugfs_list[] = {
868 { "framebuffers", tegra_debugfs_framebuffers, 0 },
869 { "iova", tegra_debugfs_iova, 0 },
872 static void tegra_debugfs_init(struct drm_minor *minor)
874 drm_debugfs_create_files(tegra_debugfs_list,
875 ARRAY_SIZE(tegra_debugfs_list),
876 minor->debugfs_root, minor);
880 static const struct drm_driver tegra_drm_driver = {
881 .driver_features = DRIVER_MODESET | DRIVER_GEM |
882 DRIVER_ATOMIC | DRIVER_RENDER | DRIVER_SYNCOBJ,
883 .open = tegra_drm_open,
884 .postclose = tegra_drm_postclose,
886 #if defined(CONFIG_DEBUG_FS)
887 .debugfs_init = tegra_debugfs_init,
890 .gem_prime_import = tegra_gem_prime_import,
892 .dumb_create = tegra_bo_dumb_create,
894 .ioctls = tegra_drm_ioctls,
895 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
896 .fops = &tegra_drm_fops,
901 .major = DRIVER_MAJOR,
902 .minor = DRIVER_MINOR,
903 .patchlevel = DRIVER_PATCHLEVEL,
906 int tegra_drm_register_client(struct tegra_drm *tegra,
907 struct tegra_drm_client *client)
910 * When MLOCKs are implemented, change to allocate a shared channel
911 * only when MLOCKs are disabled.
913 client->shared_channel = host1x_channel_request(&client->base);
914 if (!client->shared_channel)
917 mutex_lock(&tegra->clients_lock);
918 list_add_tail(&client->list, &tegra->clients);
920 mutex_unlock(&tegra->clients_lock);
925 int tegra_drm_unregister_client(struct tegra_drm *tegra,
926 struct tegra_drm_client *client)
928 mutex_lock(&tegra->clients_lock);
929 list_del_init(&client->list);
931 mutex_unlock(&tegra->clients_lock);
933 if (client->shared_channel)
934 host1x_channel_put(client->shared_channel);
939 int host1x_client_iommu_attach(struct host1x_client *client)
941 struct iommu_domain *domain = iommu_get_domain_for_dev(client->dev);
942 struct drm_device *drm = dev_get_drvdata(client->host);
943 struct tegra_drm *tegra = drm->dev_private;
944 struct iommu_group *group = NULL;
947 #if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
948 if (client->dev->archdata.mapping) {
949 struct dma_iommu_mapping *mapping =
950 to_dma_iommu_mapping(client->dev);
951 arm_iommu_detach_device(client->dev);
952 arm_iommu_release_mapping(mapping);
954 domain = iommu_get_domain_for_dev(client->dev);
959 * If the host1x client is already attached to an IOMMU domain that is
960 * not the shared IOMMU domain, don't try to attach it to a different
961 * domain. This allows using the IOMMU-backed DMA API.
963 if (domain && domain != tegra->domain)
967 group = iommu_group_get(client->dev);
971 if (domain != tegra->domain) {
972 err = iommu_attach_group(tegra->domain, group);
974 iommu_group_put(group);
979 tegra->use_explicit_iommu = true;
982 client->group = group;
987 void host1x_client_iommu_detach(struct host1x_client *client)
989 struct drm_device *drm = dev_get_drvdata(client->host);
990 struct tegra_drm *tegra = drm->dev_private;
991 struct iommu_domain *domain;
995 * Devices that are part of the same group may no longer be
996 * attached to a domain at this point because their group may
997 * have been detached by an earlier client.
999 domain = iommu_get_domain_for_dev(client->dev);
1001 iommu_detach_group(tegra->domain, client->group);
1003 iommu_group_put(client->group);
1004 client->group = NULL;
1008 void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *dma)
1016 size = iova_align(&tegra->carveout.domain, size);
1018 size = PAGE_ALIGN(size);
1020 gfp = GFP_KERNEL | __GFP_ZERO;
1021 if (!tegra->domain) {
1023 * Many units only support 32-bit addresses, even on 64-bit
1024 * SoCs. If there is no IOMMU to translate into a 32-bit IO
1025 * virtual address space, force allocations to be in the
1026 * lower 32-bit range.
1031 virt = (void *)__get_free_pages(gfp, get_order(size));
1033 return ERR_PTR(-ENOMEM);
1035 if (!tegra->domain) {
1037 * If IOMMU is disabled, devices address physical memory
1040 *dma = virt_to_phys(virt);
1044 alloc = alloc_iova(&tegra->carveout.domain,
1045 size >> tegra->carveout.shift,
1046 tegra->carveout.limit, true);
1052 *dma = iova_dma_addr(&tegra->carveout.domain, alloc);
1053 err = iommu_map(tegra->domain, *dma, virt_to_phys(virt),
1054 size, IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);
1061 __free_iova(&tegra->carveout.domain, alloc);
1063 free_pages((unsigned long)virt, get_order(size));
1065 return ERR_PTR(err);
1068 void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
1072 size = iova_align(&tegra->carveout.domain, size);
1074 size = PAGE_ALIGN(size);
1076 if (tegra->domain) {
1077 iommu_unmap(tegra->domain, dma, size);
1078 free_iova(&tegra->carveout.domain,
1079 iova_pfn(&tegra->carveout.domain, dma));
1082 free_pages((unsigned long)virt, get_order(size));
1085 static bool host1x_drm_wants_iommu(struct host1x_device *dev)
1087 struct host1x *host1x = dev_get_drvdata(dev->dev.parent);
1088 struct iommu_domain *domain;
1090 /* Our IOMMU usage policy doesn't currently play well with GART */
1091 if (of_machine_is_compatible("nvidia,tegra20"))
1095 * If the Tegra DRM clients are backed by an IOMMU, push buffers are
1096 * likely to be allocated beyond the 32-bit boundary if sufficient
1097 * system memory is available. This is problematic on earlier Tegra
1098 * generations where host1x supports a maximum of 32 address bits in
1099 * the GATHER opcode. In this case, unless host1x is behind an IOMMU
1100 * as well it won't be able to process buffers allocated beyond the
1103 * The DMA API will use bounce buffers in this case, so that could
1104 * perhaps still be made to work, even if less efficient, but there
1105 * is another catch: in order to perform cache maintenance on pages
1106 * allocated for discontiguous buffers we need to map and unmap the
1107 * SG table representing these buffers. This is fine for something
1108 * small like a push buffer, but it exhausts the bounce buffer pool
1109 * (typically on the order of a few MiB) for framebuffers (many MiB
1110 * for any modern resolution).
1112 * Work around this by making sure that Tegra DRM clients only use
1113 * an IOMMU if the parent host1x also uses an IOMMU.
1115 * Note that there's still a small gap here that we don't cover: if
1116 * the DMA API is backed by an IOMMU there's no way to control which
1117 * device is attached to an IOMMU and which isn't, except via wiring
1118 * up the device tree appropriately. This is considered an problem
1119 * of integration, so care must be taken for the DT to be consistent.
1121 domain = iommu_get_domain_for_dev(dev->dev.parent);
1124 * Tegra20 and Tegra30 don't support addressing memory beyond the
1125 * 32-bit boundary, so the regular GATHER opcodes will always be
1126 * sufficient and whether or not the host1x is attached to an IOMMU
1129 if (!domain && host1x_get_dma_mask(host1x) <= DMA_BIT_MASK(32))
1132 return domain != NULL;
1135 static int host1x_drm_probe(struct host1x_device *dev)
1137 struct tegra_drm *tegra;
1138 struct drm_device *drm;
1141 drm = drm_dev_alloc(&tegra_drm_driver, &dev->dev);
1143 return PTR_ERR(drm);
1145 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
1151 if (host1x_drm_wants_iommu(dev) && iommu_present(&platform_bus_type)) {
1152 tegra->domain = iommu_domain_alloc(&platform_bus_type);
1153 if (!tegra->domain) {
1158 err = iova_cache_get();
1163 mutex_init(&tegra->clients_lock);
1164 INIT_LIST_HEAD(&tegra->clients);
1166 dev_set_drvdata(&dev->dev, drm);
1167 drm->dev_private = tegra;
1170 drm_mode_config_init(drm);
1172 drm->mode_config.min_width = 0;
1173 drm->mode_config.min_height = 0;
1174 drm->mode_config.max_width = 0;
1175 drm->mode_config.max_height = 0;
1177 drm->mode_config.normalize_zpos = true;
1179 drm->mode_config.funcs = &tegra_drm_mode_config_funcs;
1180 drm->mode_config.helper_private = &tegra_drm_mode_config_helpers;
1182 drm_kms_helper_poll_init(drm);
1184 err = host1x_device_init(dev);
1189 * Now that all display controller have been initialized, the maximum
1190 * supported resolution is known and the bitmask for horizontal and
1191 * vertical bitfields can be computed.
1193 tegra->hmask = drm->mode_config.max_width - 1;
1194 tegra->vmask = drm->mode_config.max_height - 1;
1196 if (tegra->use_explicit_iommu) {
1197 u64 carveout_start, carveout_end, gem_start, gem_end;
1198 u64 dma_mask = dma_get_mask(&dev->dev);
1199 dma_addr_t start, end;
1200 unsigned long order;
1202 start = tegra->domain->geometry.aperture_start & dma_mask;
1203 end = tegra->domain->geometry.aperture_end & dma_mask;
1206 gem_end = end - CARVEOUT_SZ;
1207 carveout_start = gem_end + 1;
1210 order = __ffs(tegra->domain->pgsize_bitmap);
1211 init_iova_domain(&tegra->carveout.domain, 1UL << order,
1212 carveout_start >> order);
1214 tegra->carveout.shift = iova_shift(&tegra->carveout.domain);
1215 tegra->carveout.limit = carveout_end >> tegra->carveout.shift;
1217 drm_mm_init(&tegra->mm, gem_start, gem_end - gem_start + 1);
1218 mutex_init(&tegra->mm_lock);
1220 DRM_DEBUG_DRIVER("IOMMU apertures:\n");
1221 DRM_DEBUG_DRIVER(" GEM: %#llx-%#llx\n", gem_start, gem_end);
1222 DRM_DEBUG_DRIVER(" Carveout: %#llx-%#llx\n", carveout_start,
1224 } else if (tegra->domain) {
1225 iommu_domain_free(tegra->domain);
1226 tegra->domain = NULL;
1231 err = tegra_display_hub_prepare(tegra->hub);
1236 /* syncpoints are used for full 32-bit hardware VBLANK counters */
1237 drm->max_vblank_count = 0xffffffff;
1239 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
1243 drm_mode_config_reset(drm);
1245 err = drm_aperture_remove_framebuffers(&tegra_drm_driver);
1249 err = drm_dev_register(drm, 0);
1253 tegra_fbdev_setup(drm);
1259 tegra_display_hub_cleanup(tegra->hub);
1261 if (tegra->domain) {
1262 mutex_destroy(&tegra->mm_lock);
1263 drm_mm_takedown(&tegra->mm);
1264 put_iova_domain(&tegra->carveout.domain);
1268 host1x_device_exit(dev);
1270 drm_kms_helper_poll_fini(drm);
1271 drm_mode_config_cleanup(drm);
1274 iommu_domain_free(tegra->domain);
1282 static int host1x_drm_remove(struct host1x_device *dev)
1284 struct drm_device *drm = dev_get_drvdata(&dev->dev);
1285 struct tegra_drm *tegra = drm->dev_private;
1288 drm_dev_unregister(drm);
1290 drm_kms_helper_poll_fini(drm);
1291 drm_atomic_helper_shutdown(drm);
1292 drm_mode_config_cleanup(drm);
1295 tegra_display_hub_cleanup(tegra->hub);
1297 err = host1x_device_exit(dev);
1299 dev_err(&dev->dev, "host1x device cleanup failed: %d\n", err);
1301 if (tegra->domain) {
1302 mutex_destroy(&tegra->mm_lock);
1303 drm_mm_takedown(&tegra->mm);
1304 put_iova_domain(&tegra->carveout.domain);
1306 iommu_domain_free(tegra->domain);
1315 #ifdef CONFIG_PM_SLEEP
1316 static int host1x_drm_suspend(struct device *dev)
1318 struct drm_device *drm = dev_get_drvdata(dev);
1320 return drm_mode_config_helper_suspend(drm);
1323 static int host1x_drm_resume(struct device *dev)
1325 struct drm_device *drm = dev_get_drvdata(dev);
1327 return drm_mode_config_helper_resume(drm);
1331 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
1334 static const struct of_device_id host1x_drm_subdevs[] = {
1335 { .compatible = "nvidia,tegra20-dc", },
1336 { .compatible = "nvidia,tegra20-hdmi", },
1337 { .compatible = "nvidia,tegra20-gr2d", },
1338 { .compatible = "nvidia,tegra20-gr3d", },
1339 { .compatible = "nvidia,tegra30-dc", },
1340 { .compatible = "nvidia,tegra30-hdmi", },
1341 { .compatible = "nvidia,tegra30-gr2d", },
1342 { .compatible = "nvidia,tegra30-gr3d", },
1343 { .compatible = "nvidia,tegra114-dc", },
1344 { .compatible = "nvidia,tegra114-dsi", },
1345 { .compatible = "nvidia,tegra114-hdmi", },
1346 { .compatible = "nvidia,tegra114-gr2d", },
1347 { .compatible = "nvidia,tegra114-gr3d", },
1348 { .compatible = "nvidia,tegra124-dc", },
1349 { .compatible = "nvidia,tegra124-sor", },
1350 { .compatible = "nvidia,tegra124-hdmi", },
1351 { .compatible = "nvidia,tegra124-dsi", },
1352 { .compatible = "nvidia,tegra124-vic", },
1353 { .compatible = "nvidia,tegra132-dsi", },
1354 { .compatible = "nvidia,tegra210-dc", },
1355 { .compatible = "nvidia,tegra210-dsi", },
1356 { .compatible = "nvidia,tegra210-sor", },
1357 { .compatible = "nvidia,tegra210-sor1", },
1358 { .compatible = "nvidia,tegra210-vic", },
1359 { .compatible = "nvidia,tegra210-nvdec", },
1360 { .compatible = "nvidia,tegra186-display", },
1361 { .compatible = "nvidia,tegra186-dc", },
1362 { .compatible = "nvidia,tegra186-sor", },
1363 { .compatible = "nvidia,tegra186-sor1", },
1364 { .compatible = "nvidia,tegra186-vic", },
1365 { .compatible = "nvidia,tegra186-nvdec", },
1366 { .compatible = "nvidia,tegra194-display", },
1367 { .compatible = "nvidia,tegra194-dc", },
1368 { .compatible = "nvidia,tegra194-sor", },
1369 { .compatible = "nvidia,tegra194-vic", },
1370 { .compatible = "nvidia,tegra194-nvdec", },
1371 { .compatible = "nvidia,tegra234-vic", },
1372 { .compatible = "nvidia,tegra234-nvdec", },
1376 static struct host1x_driver host1x_drm_driver = {
1379 .pm = &host1x_drm_pm_ops,
1381 .probe = host1x_drm_probe,
1382 .remove = host1x_drm_remove,
1383 .subdevs = host1x_drm_subdevs,
1386 static struct platform_driver * const drivers[] = {
1387 &tegra_display_hub_driver,
1391 &tegra_dpaux_driver,
1396 &tegra_nvdec_driver,
1399 static int __init host1x_drm_init(void)
1403 if (drm_firmware_drivers_only())
1406 err = host1x_driver_register(&host1x_drm_driver);
1410 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1412 goto unregister_host1x;
1417 host1x_driver_unregister(&host1x_drm_driver);
1420 module_init(host1x_drm_init);
1422 static void __exit host1x_drm_exit(void)
1424 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1425 host1x_driver_unregister(&host1x_drm_driver);
1427 module_exit(host1x_drm_exit);
1429 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1430 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1431 MODULE_LICENSE("GPL v2");