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>
14 #include <drm/drm_aperture.h>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_debugfs.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_ioctl.h>
21 #include <drm/drm_prime.h>
22 #include <drm/drm_vblank.h>
27 #define DRIVER_NAME "tegra"
28 #define DRIVER_DESC "NVIDIA Tegra graphics"
29 #define DRIVER_DATE "20120330"
30 #define DRIVER_MAJOR 0
31 #define DRIVER_MINOR 0
32 #define DRIVER_PATCHLEVEL 0
34 #define CARVEOUT_SZ SZ_64M
35 #define CDMA_GATHER_FETCHES_MAX_NB 16383
37 struct tegra_drm_file {
42 static int tegra_atomic_check(struct drm_device *drm,
43 struct drm_atomic_state *state)
47 err = drm_atomic_helper_check(drm, state);
51 return tegra_display_hub_atomic_check(drm, state);
54 static const struct drm_mode_config_funcs tegra_drm_mode_config_funcs = {
55 .fb_create = tegra_fb_create,
56 #ifdef CONFIG_DRM_FBDEV_EMULATION
57 .output_poll_changed = drm_fb_helper_output_poll_changed,
59 .atomic_check = tegra_atomic_check,
60 .atomic_commit = drm_atomic_helper_commit,
63 static void tegra_atomic_commit_tail(struct drm_atomic_state *old_state)
65 struct drm_device *drm = old_state->dev;
66 struct tegra_drm *tegra = drm->dev_private;
69 bool fence_cookie = dma_fence_begin_signalling();
71 drm_atomic_helper_commit_modeset_disables(drm, old_state);
72 tegra_display_hub_atomic_commit(drm, old_state);
73 drm_atomic_helper_commit_planes(drm, old_state, 0);
74 drm_atomic_helper_commit_modeset_enables(drm, old_state);
75 drm_atomic_helper_commit_hw_done(old_state);
76 dma_fence_end_signalling(fence_cookie);
77 drm_atomic_helper_wait_for_vblanks(drm, old_state);
78 drm_atomic_helper_cleanup_planes(drm, old_state);
80 drm_atomic_helper_commit_tail_rpm(old_state);
84 static const struct drm_mode_config_helper_funcs
85 tegra_drm_mode_config_helpers = {
86 .atomic_commit_tail = tegra_atomic_commit_tail,
89 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
91 struct tegra_drm_file *fpriv;
93 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
97 idr_init_base(&fpriv->contexts, 1);
98 mutex_init(&fpriv->lock);
99 filp->driver_priv = fpriv;
104 static void tegra_drm_context_free(struct tegra_drm_context *context)
106 context->client->ops->close_channel(context);
110 static struct host1x_bo *
111 host1x_bo_lookup(struct drm_file *file, u32 handle)
113 struct drm_gem_object *gem;
116 gem = drm_gem_object_lookup(file, handle);
120 bo = to_tegra_bo(gem);
124 static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
125 struct drm_tegra_reloc __user *src,
126 struct drm_device *drm,
127 struct drm_file *file)
132 err = get_user(cmdbuf, &src->cmdbuf.handle);
136 err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
140 err = get_user(target, &src->target.handle);
144 err = get_user(dest->target.offset, &src->target.offset);
148 err = get_user(dest->shift, &src->shift);
152 dest->flags = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
154 dest->cmdbuf.bo = host1x_bo_lookup(file, cmdbuf);
155 if (!dest->cmdbuf.bo)
158 dest->target.bo = host1x_bo_lookup(file, target);
159 if (!dest->target.bo)
165 int tegra_drm_submit(struct tegra_drm_context *context,
166 struct drm_tegra_submit *args, struct drm_device *drm,
167 struct drm_file *file)
169 struct host1x_client *client = &context->client->base;
170 unsigned int num_cmdbufs = args->num_cmdbufs;
171 unsigned int num_relocs = args->num_relocs;
172 struct drm_tegra_cmdbuf __user *user_cmdbufs;
173 struct drm_tegra_reloc __user *user_relocs;
174 struct drm_tegra_syncpt __user *user_syncpt;
175 struct drm_tegra_syncpt syncpt;
176 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
177 struct drm_gem_object **refs;
178 struct host1x_syncpt *sp = NULL;
179 struct host1x_job *job;
180 unsigned int num_refs;
183 user_cmdbufs = u64_to_user_ptr(args->cmdbufs);
184 user_relocs = u64_to_user_ptr(args->relocs);
185 user_syncpt = u64_to_user_ptr(args->syncpts);
187 /* We don't yet support other than one syncpt_incr struct per submit */
188 if (args->num_syncpts != 1)
191 /* We don't yet support waitchks */
192 if (args->num_waitchks != 0)
195 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
200 job->num_relocs = args->num_relocs;
201 job->client = client;
202 job->class = client->class;
203 job->serialize = true;
206 * Track referenced BOs so that they can be unreferenced after the
207 * submission is complete.
209 num_refs = num_cmdbufs + num_relocs * 2;
211 refs = kmalloc_array(num_refs, sizeof(*refs), GFP_KERNEL);
217 /* reuse as an iterator later */
220 while (num_cmdbufs) {
221 struct drm_tegra_cmdbuf cmdbuf;
222 struct host1x_bo *bo;
223 struct tegra_bo *obj;
226 if (copy_from_user(&cmdbuf, user_cmdbufs, sizeof(cmdbuf))) {
232 * The maximum number of CDMA gather fetches is 16383, a higher
233 * value means the words count is malformed.
235 if (cmdbuf.words > CDMA_GATHER_FETCHES_MAX_NB) {
240 bo = host1x_bo_lookup(file, cmdbuf.handle);
246 offset = (u64)cmdbuf.offset + (u64)cmdbuf.words * sizeof(u32);
247 obj = host1x_to_tegra_bo(bo);
248 refs[num_refs++] = &obj->gem;
251 * Gather buffer base address must be 4-bytes aligned,
252 * unaligned offset is malformed and cause commands stream
253 * corruption on the buffer address relocation.
255 if (offset & 3 || offset > obj->gem.size) {
260 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
265 /* copy and resolve relocations from submit */
266 while (num_relocs--) {
267 struct host1x_reloc *reloc;
268 struct tegra_bo *obj;
270 err = host1x_reloc_copy_from_user(&job->relocs[num_relocs],
271 &user_relocs[num_relocs], drm,
276 reloc = &job->relocs[num_relocs];
277 obj = host1x_to_tegra_bo(reloc->cmdbuf.bo);
278 refs[num_refs++] = &obj->gem;
281 * The unaligned cmdbuf offset will cause an unaligned write
282 * during of the relocations patching, corrupting the commands
285 if (reloc->cmdbuf.offset & 3 ||
286 reloc->cmdbuf.offset >= obj->gem.size) {
291 obj = host1x_to_tegra_bo(reloc->target.bo);
292 refs[num_refs++] = &obj->gem;
294 if (reloc->target.offset >= obj->gem.size) {
300 if (copy_from_user(&syncpt, user_syncpt, sizeof(syncpt))) {
305 /* Syncpoint ref will be dropped on job release. */
306 sp = host1x_syncpt_get_by_id(host1x, syncpt.id);
312 job->is_addr_reg = context->client->ops->is_addr_reg;
313 job->is_valid_class = context->client->ops->is_valid_class;
314 job->syncpt_incrs = syncpt.incrs;
316 job->timeout = 10000;
318 if (args->timeout && args->timeout < 10000)
319 job->timeout = args->timeout;
321 err = host1x_job_pin(job, context->client->base.dev);
325 err = host1x_job_submit(job);
327 host1x_job_unpin(job);
331 args->fence = job->syncpt_end;
335 drm_gem_object_put(refs[num_refs]);
345 #ifdef CONFIG_DRM_TEGRA_STAGING
346 static int tegra_gem_create(struct drm_device *drm, void *data,
347 struct drm_file *file)
349 struct drm_tegra_gem_create *args = data;
352 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
360 static int tegra_gem_mmap(struct drm_device *drm, void *data,
361 struct drm_file *file)
363 struct drm_tegra_gem_mmap *args = data;
364 struct drm_gem_object *gem;
367 gem = drm_gem_object_lookup(file, args->handle);
371 bo = to_tegra_bo(gem);
373 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
375 drm_gem_object_put(gem);
380 static int tegra_syncpt_read(struct drm_device *drm, void *data,
381 struct drm_file *file)
383 struct host1x *host = dev_get_drvdata(drm->dev->parent);
384 struct drm_tegra_syncpt_read *args = data;
385 struct host1x_syncpt *sp;
387 sp = host1x_syncpt_get_by_id_noref(host, args->id);
391 args->value = host1x_syncpt_read_min(sp);
395 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
396 struct drm_file *file)
398 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
399 struct drm_tegra_syncpt_incr *args = data;
400 struct host1x_syncpt *sp;
402 sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
406 return host1x_syncpt_incr(sp);
409 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
410 struct drm_file *file)
412 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
413 struct drm_tegra_syncpt_wait *args = data;
414 struct host1x_syncpt *sp;
416 sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
420 return host1x_syncpt_wait(sp, args->thresh,
421 msecs_to_jiffies(args->timeout),
425 static int tegra_client_open(struct tegra_drm_file *fpriv,
426 struct tegra_drm_client *client,
427 struct tegra_drm_context *context)
431 err = client->ops->open_channel(client, context);
435 err = idr_alloc(&fpriv->contexts, context, 1, 0, GFP_KERNEL);
437 client->ops->close_channel(context);
441 context->client = client;
447 static int tegra_open_channel(struct drm_device *drm, void *data,
448 struct drm_file *file)
450 struct tegra_drm_file *fpriv = file->driver_priv;
451 struct tegra_drm *tegra = drm->dev_private;
452 struct drm_tegra_open_channel *args = data;
453 struct tegra_drm_context *context;
454 struct tegra_drm_client *client;
457 context = kzalloc(sizeof(*context), GFP_KERNEL);
461 mutex_lock(&fpriv->lock);
463 list_for_each_entry(client, &tegra->clients, list)
464 if (client->base.class == args->client) {
465 err = tegra_client_open(fpriv, client, context);
469 args->context = context->id;
476 mutex_unlock(&fpriv->lock);
480 static int tegra_close_channel(struct drm_device *drm, void *data,
481 struct drm_file *file)
483 struct tegra_drm_file *fpriv = file->driver_priv;
484 struct drm_tegra_close_channel *args = data;
485 struct tegra_drm_context *context;
488 mutex_lock(&fpriv->lock);
490 context = idr_find(&fpriv->contexts, args->context);
496 idr_remove(&fpriv->contexts, context->id);
497 tegra_drm_context_free(context);
500 mutex_unlock(&fpriv->lock);
504 static int tegra_get_syncpt(struct drm_device *drm, void *data,
505 struct drm_file *file)
507 struct tegra_drm_file *fpriv = file->driver_priv;
508 struct drm_tegra_get_syncpt *args = data;
509 struct tegra_drm_context *context;
510 struct host1x_syncpt *syncpt;
513 mutex_lock(&fpriv->lock);
515 context = idr_find(&fpriv->contexts, args->context);
521 if (args->index >= context->client->base.num_syncpts) {
526 syncpt = context->client->base.syncpts[args->index];
527 args->id = host1x_syncpt_id(syncpt);
530 mutex_unlock(&fpriv->lock);
534 static int tegra_submit(struct drm_device *drm, void *data,
535 struct drm_file *file)
537 struct tegra_drm_file *fpriv = file->driver_priv;
538 struct drm_tegra_submit *args = data;
539 struct tegra_drm_context *context;
542 mutex_lock(&fpriv->lock);
544 context = idr_find(&fpriv->contexts, args->context);
550 err = context->client->ops->submit(context, args, drm, file);
553 mutex_unlock(&fpriv->lock);
557 static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
558 struct drm_file *file)
560 struct tegra_drm_file *fpriv = file->driver_priv;
561 struct drm_tegra_get_syncpt_base *args = data;
562 struct tegra_drm_context *context;
563 struct host1x_syncpt_base *base;
564 struct host1x_syncpt *syncpt;
567 mutex_lock(&fpriv->lock);
569 context = idr_find(&fpriv->contexts, args->context);
575 if (args->syncpt >= context->client->base.num_syncpts) {
580 syncpt = context->client->base.syncpts[args->syncpt];
582 base = host1x_syncpt_get_base(syncpt);
588 args->id = host1x_syncpt_base_id(base);
591 mutex_unlock(&fpriv->lock);
595 static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
596 struct drm_file *file)
598 struct drm_tegra_gem_set_tiling *args = data;
599 enum tegra_bo_tiling_mode mode;
600 struct drm_gem_object *gem;
601 unsigned long value = 0;
604 switch (args->mode) {
605 case DRM_TEGRA_GEM_TILING_MODE_PITCH:
606 mode = TEGRA_BO_TILING_MODE_PITCH;
608 if (args->value != 0)
613 case DRM_TEGRA_GEM_TILING_MODE_TILED:
614 mode = TEGRA_BO_TILING_MODE_TILED;
616 if (args->value != 0)
621 case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
622 mode = TEGRA_BO_TILING_MODE_BLOCK;
634 gem = drm_gem_object_lookup(file, args->handle);
638 bo = to_tegra_bo(gem);
640 bo->tiling.mode = mode;
641 bo->tiling.value = value;
643 drm_gem_object_put(gem);
648 static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
649 struct drm_file *file)
651 struct drm_tegra_gem_get_tiling *args = data;
652 struct drm_gem_object *gem;
656 gem = drm_gem_object_lookup(file, args->handle);
660 bo = to_tegra_bo(gem);
662 switch (bo->tiling.mode) {
663 case TEGRA_BO_TILING_MODE_PITCH:
664 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
668 case TEGRA_BO_TILING_MODE_TILED:
669 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
673 case TEGRA_BO_TILING_MODE_BLOCK:
674 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
675 args->value = bo->tiling.value;
683 drm_gem_object_put(gem);
688 static int tegra_gem_set_flags(struct drm_device *drm, void *data,
689 struct drm_file *file)
691 struct drm_tegra_gem_set_flags *args = data;
692 struct drm_gem_object *gem;
695 if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
698 gem = drm_gem_object_lookup(file, args->handle);
702 bo = to_tegra_bo(gem);
705 if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
706 bo->flags |= TEGRA_BO_BOTTOM_UP;
708 drm_gem_object_put(gem);
713 static int tegra_gem_get_flags(struct drm_device *drm, void *data,
714 struct drm_file *file)
716 struct drm_tegra_gem_get_flags *args = data;
717 struct drm_gem_object *gem;
720 gem = drm_gem_object_lookup(file, args->handle);
724 bo = to_tegra_bo(gem);
727 if (bo->flags & TEGRA_BO_BOTTOM_UP)
728 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
730 drm_gem_object_put(gem);
736 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
737 #ifdef CONFIG_DRM_TEGRA_STAGING
738 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create,
740 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap,
742 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read,
744 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr,
746 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait,
748 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel,
750 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel,
752 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt,
754 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit,
756 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base,
758 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling,
760 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling,
762 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags,
764 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags,
769 static const struct file_operations tegra_drm_fops = {
770 .owner = THIS_MODULE,
772 .release = drm_release,
773 .unlocked_ioctl = drm_ioctl,
774 .mmap = tegra_drm_mmap,
777 .compat_ioctl = drm_compat_ioctl,
778 .llseek = noop_llseek,
781 static int tegra_drm_context_cleanup(int id, void *p, void *data)
783 struct tegra_drm_context *context = p;
785 tegra_drm_context_free(context);
790 static void tegra_drm_postclose(struct drm_device *drm, struct drm_file *file)
792 struct tegra_drm_file *fpriv = file->driver_priv;
794 mutex_lock(&fpriv->lock);
795 idr_for_each(&fpriv->contexts, tegra_drm_context_cleanup, NULL);
796 mutex_unlock(&fpriv->lock);
798 idr_destroy(&fpriv->contexts);
799 mutex_destroy(&fpriv->lock);
803 #ifdef CONFIG_DEBUG_FS
804 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
806 struct drm_info_node *node = (struct drm_info_node *)s->private;
807 struct drm_device *drm = node->minor->dev;
808 struct drm_framebuffer *fb;
810 mutex_lock(&drm->mode_config.fb_lock);
812 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
813 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
814 fb->base.id, fb->width, fb->height,
816 fb->format->cpp[0] * 8,
817 drm_framebuffer_read_refcount(fb));
820 mutex_unlock(&drm->mode_config.fb_lock);
825 static int tegra_debugfs_iova(struct seq_file *s, void *data)
827 struct drm_info_node *node = (struct drm_info_node *)s->private;
828 struct drm_device *drm = node->minor->dev;
829 struct tegra_drm *tegra = drm->dev_private;
830 struct drm_printer p = drm_seq_file_printer(s);
833 mutex_lock(&tegra->mm_lock);
834 drm_mm_print(&tegra->mm, &p);
835 mutex_unlock(&tegra->mm_lock);
841 static struct drm_info_list tegra_debugfs_list[] = {
842 { "framebuffers", tegra_debugfs_framebuffers, 0 },
843 { "iova", tegra_debugfs_iova, 0 },
846 static void tegra_debugfs_init(struct drm_minor *minor)
848 drm_debugfs_create_files(tegra_debugfs_list,
849 ARRAY_SIZE(tegra_debugfs_list),
850 minor->debugfs_root, minor);
854 static const struct drm_driver tegra_drm_driver = {
855 .driver_features = DRIVER_MODESET | DRIVER_GEM |
856 DRIVER_ATOMIC | DRIVER_RENDER,
857 .open = tegra_drm_open,
858 .postclose = tegra_drm_postclose,
859 .lastclose = drm_fb_helper_lastclose,
861 #if defined(CONFIG_DEBUG_FS)
862 .debugfs_init = tegra_debugfs_init,
865 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
866 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
867 .gem_prime_import = tegra_gem_prime_import,
869 .dumb_create = tegra_bo_dumb_create,
871 .ioctls = tegra_drm_ioctls,
872 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
873 .fops = &tegra_drm_fops,
878 .major = DRIVER_MAJOR,
879 .minor = DRIVER_MINOR,
880 .patchlevel = DRIVER_PATCHLEVEL,
883 int tegra_drm_register_client(struct tegra_drm *tegra,
884 struct tegra_drm_client *client)
886 mutex_lock(&tegra->clients_lock);
887 list_add_tail(&client->list, &tegra->clients);
889 mutex_unlock(&tegra->clients_lock);
894 int tegra_drm_unregister_client(struct tegra_drm *tegra,
895 struct tegra_drm_client *client)
897 mutex_lock(&tegra->clients_lock);
898 list_del_init(&client->list);
900 mutex_unlock(&tegra->clients_lock);
905 int host1x_client_iommu_attach(struct host1x_client *client)
907 struct iommu_domain *domain = iommu_get_domain_for_dev(client->dev);
908 struct drm_device *drm = dev_get_drvdata(client->host);
909 struct tegra_drm *tegra = drm->dev_private;
910 struct iommu_group *group = NULL;
914 * If the host1x client is already attached to an IOMMU domain that is
915 * not the shared IOMMU domain, don't try to attach it to a different
916 * domain. This allows using the IOMMU-backed DMA API.
918 if (domain && domain != tegra->domain)
922 group = iommu_group_get(client->dev);
926 if (domain != tegra->domain) {
927 err = iommu_attach_group(tegra->domain, group);
929 iommu_group_put(group);
934 tegra->use_explicit_iommu = true;
937 client->group = group;
942 void host1x_client_iommu_detach(struct host1x_client *client)
944 struct drm_device *drm = dev_get_drvdata(client->host);
945 struct tegra_drm *tegra = drm->dev_private;
946 struct iommu_domain *domain;
950 * Devices that are part of the same group may no longer be
951 * attached to a domain at this point because their group may
952 * have been detached by an earlier client.
954 domain = iommu_get_domain_for_dev(client->dev);
956 iommu_detach_group(tegra->domain, client->group);
958 iommu_group_put(client->group);
959 client->group = NULL;
963 void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *dma)
971 size = iova_align(&tegra->carveout.domain, size);
973 size = PAGE_ALIGN(size);
975 gfp = GFP_KERNEL | __GFP_ZERO;
976 if (!tegra->domain) {
978 * Many units only support 32-bit addresses, even on 64-bit
979 * SoCs. If there is no IOMMU to translate into a 32-bit IO
980 * virtual address space, force allocations to be in the
981 * lower 32-bit range.
986 virt = (void *)__get_free_pages(gfp, get_order(size));
988 return ERR_PTR(-ENOMEM);
990 if (!tegra->domain) {
992 * If IOMMU is disabled, devices address physical memory
995 *dma = virt_to_phys(virt);
999 alloc = alloc_iova(&tegra->carveout.domain,
1000 size >> tegra->carveout.shift,
1001 tegra->carveout.limit, true);
1007 *dma = iova_dma_addr(&tegra->carveout.domain, alloc);
1008 err = iommu_map(tegra->domain, *dma, virt_to_phys(virt),
1009 size, IOMMU_READ | IOMMU_WRITE);
1016 __free_iova(&tegra->carveout.domain, alloc);
1018 free_pages((unsigned long)virt, get_order(size));
1020 return ERR_PTR(err);
1023 void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
1027 size = iova_align(&tegra->carveout.domain, size);
1029 size = PAGE_ALIGN(size);
1031 if (tegra->domain) {
1032 iommu_unmap(tegra->domain, dma, size);
1033 free_iova(&tegra->carveout.domain,
1034 iova_pfn(&tegra->carveout.domain, dma));
1037 free_pages((unsigned long)virt, get_order(size));
1040 static bool host1x_drm_wants_iommu(struct host1x_device *dev)
1042 struct host1x *host1x = dev_get_drvdata(dev->dev.parent);
1043 struct iommu_domain *domain;
1046 * If the Tegra DRM clients are backed by an IOMMU, push buffers are
1047 * likely to be allocated beyond the 32-bit boundary if sufficient
1048 * system memory is available. This is problematic on earlier Tegra
1049 * generations where host1x supports a maximum of 32 address bits in
1050 * the GATHER opcode. In this case, unless host1x is behind an IOMMU
1051 * as well it won't be able to process buffers allocated beyond the
1054 * The DMA API will use bounce buffers in this case, so that could
1055 * perhaps still be made to work, even if less efficient, but there
1056 * is another catch: in order to perform cache maintenance on pages
1057 * allocated for discontiguous buffers we need to map and unmap the
1058 * SG table representing these buffers. This is fine for something
1059 * small like a push buffer, but it exhausts the bounce buffer pool
1060 * (typically on the order of a few MiB) for framebuffers (many MiB
1061 * for any modern resolution).
1063 * Work around this by making sure that Tegra DRM clients only use
1064 * an IOMMU if the parent host1x also uses an IOMMU.
1066 * Note that there's still a small gap here that we don't cover: if
1067 * the DMA API is backed by an IOMMU there's no way to control which
1068 * device is attached to an IOMMU and which isn't, except via wiring
1069 * up the device tree appropriately. This is considered an problem
1070 * of integration, so care must be taken for the DT to be consistent.
1072 domain = iommu_get_domain_for_dev(dev->dev.parent);
1075 * Tegra20 and Tegra30 don't support addressing memory beyond the
1076 * 32-bit boundary, so the regular GATHER opcodes will always be
1077 * sufficient and whether or not the host1x is attached to an IOMMU
1080 if (!domain && host1x_get_dma_mask(host1x) <= DMA_BIT_MASK(32))
1083 return domain != NULL;
1086 static int host1x_drm_probe(struct host1x_device *dev)
1088 struct tegra_drm *tegra;
1089 struct drm_device *drm;
1092 drm = drm_dev_alloc(&tegra_drm_driver, &dev->dev);
1094 return PTR_ERR(drm);
1096 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
1102 if (host1x_drm_wants_iommu(dev) && iommu_present(&platform_bus_type)) {
1103 tegra->domain = iommu_domain_alloc(&platform_bus_type);
1104 if (!tegra->domain) {
1109 err = iova_cache_get();
1114 mutex_init(&tegra->clients_lock);
1115 INIT_LIST_HEAD(&tegra->clients);
1117 dev_set_drvdata(&dev->dev, drm);
1118 drm->dev_private = tegra;
1121 drm_mode_config_init(drm);
1123 drm->mode_config.min_width = 0;
1124 drm->mode_config.min_height = 0;
1125 drm->mode_config.max_width = 0;
1126 drm->mode_config.max_height = 0;
1128 drm->mode_config.normalize_zpos = true;
1130 drm->mode_config.funcs = &tegra_drm_mode_config_funcs;
1131 drm->mode_config.helper_private = &tegra_drm_mode_config_helpers;
1133 err = tegra_drm_fb_prepare(drm);
1137 drm_kms_helper_poll_init(drm);
1139 err = host1x_device_init(dev);
1144 * Now that all display controller have been initialized, the maximum
1145 * supported resolution is known and the bitmask for horizontal and
1146 * vertical bitfields can be computed.
1148 tegra->hmask = drm->mode_config.max_width - 1;
1149 tegra->vmask = drm->mode_config.max_height - 1;
1151 if (tegra->use_explicit_iommu) {
1152 u64 carveout_start, carveout_end, gem_start, gem_end;
1153 u64 dma_mask = dma_get_mask(&dev->dev);
1154 dma_addr_t start, end;
1155 unsigned long order;
1157 start = tegra->domain->geometry.aperture_start & dma_mask;
1158 end = tegra->domain->geometry.aperture_end & dma_mask;
1161 gem_end = end - CARVEOUT_SZ;
1162 carveout_start = gem_end + 1;
1165 order = __ffs(tegra->domain->pgsize_bitmap);
1166 init_iova_domain(&tegra->carveout.domain, 1UL << order,
1167 carveout_start >> order);
1169 tegra->carveout.shift = iova_shift(&tegra->carveout.domain);
1170 tegra->carveout.limit = carveout_end >> tegra->carveout.shift;
1172 drm_mm_init(&tegra->mm, gem_start, gem_end - gem_start + 1);
1173 mutex_init(&tegra->mm_lock);
1175 DRM_DEBUG_DRIVER("IOMMU apertures:\n");
1176 DRM_DEBUG_DRIVER(" GEM: %#llx-%#llx\n", gem_start, gem_end);
1177 DRM_DEBUG_DRIVER(" Carveout: %#llx-%#llx\n", carveout_start,
1179 } else if (tegra->domain) {
1180 iommu_domain_free(tegra->domain);
1181 tegra->domain = NULL;
1186 err = tegra_display_hub_prepare(tegra->hub);
1192 * We don't use the drm_irq_install() helpers provided by the DRM
1193 * core, so we need to set this manually in order to allow the
1194 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
1196 drm->irq_enabled = true;
1198 /* syncpoints are used for full 32-bit hardware VBLANK counters */
1199 drm->max_vblank_count = 0xffffffff;
1201 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
1205 drm_mode_config_reset(drm);
1207 err = drm_aperture_remove_framebuffers(false, "tegradrmfb");
1211 err = tegra_drm_fb_init(drm);
1215 err = drm_dev_register(drm, 0);
1222 tegra_drm_fb_exit(drm);
1225 tegra_display_hub_cleanup(tegra->hub);
1227 if (tegra->domain) {
1228 mutex_destroy(&tegra->mm_lock);
1229 drm_mm_takedown(&tegra->mm);
1230 put_iova_domain(&tegra->carveout.domain);
1234 host1x_device_exit(dev);
1236 drm_kms_helper_poll_fini(drm);
1237 tegra_drm_fb_free(drm);
1239 drm_mode_config_cleanup(drm);
1242 iommu_domain_free(tegra->domain);
1250 static int host1x_drm_remove(struct host1x_device *dev)
1252 struct drm_device *drm = dev_get_drvdata(&dev->dev);
1253 struct tegra_drm *tegra = drm->dev_private;
1256 drm_dev_unregister(drm);
1258 drm_kms_helper_poll_fini(drm);
1259 tegra_drm_fb_exit(drm);
1260 drm_atomic_helper_shutdown(drm);
1261 drm_mode_config_cleanup(drm);
1264 tegra_display_hub_cleanup(tegra->hub);
1266 err = host1x_device_exit(dev);
1268 dev_err(&dev->dev, "host1x device cleanup failed: %d\n", err);
1270 if (tegra->domain) {
1271 mutex_destroy(&tegra->mm_lock);
1272 drm_mm_takedown(&tegra->mm);
1273 put_iova_domain(&tegra->carveout.domain);
1275 iommu_domain_free(tegra->domain);
1284 #ifdef CONFIG_PM_SLEEP
1285 static int host1x_drm_suspend(struct device *dev)
1287 struct drm_device *drm = dev_get_drvdata(dev);
1289 return drm_mode_config_helper_suspend(drm);
1292 static int host1x_drm_resume(struct device *dev)
1294 struct drm_device *drm = dev_get_drvdata(dev);
1296 return drm_mode_config_helper_resume(drm);
1300 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
1303 static const struct of_device_id host1x_drm_subdevs[] = {
1304 { .compatible = "nvidia,tegra20-dc", },
1305 { .compatible = "nvidia,tegra20-hdmi", },
1306 { .compatible = "nvidia,tegra20-gr2d", },
1307 { .compatible = "nvidia,tegra20-gr3d", },
1308 { .compatible = "nvidia,tegra30-dc", },
1309 { .compatible = "nvidia,tegra30-hdmi", },
1310 { .compatible = "nvidia,tegra30-gr2d", },
1311 { .compatible = "nvidia,tegra30-gr3d", },
1312 { .compatible = "nvidia,tegra114-dc", },
1313 { .compatible = "nvidia,tegra114-dsi", },
1314 { .compatible = "nvidia,tegra114-hdmi", },
1315 { .compatible = "nvidia,tegra114-gr2d", },
1316 { .compatible = "nvidia,tegra114-gr3d", },
1317 { .compatible = "nvidia,tegra124-dc", },
1318 { .compatible = "nvidia,tegra124-sor", },
1319 { .compatible = "nvidia,tegra124-hdmi", },
1320 { .compatible = "nvidia,tegra124-dsi", },
1321 { .compatible = "nvidia,tegra124-vic", },
1322 { .compatible = "nvidia,tegra132-dsi", },
1323 { .compatible = "nvidia,tegra210-dc", },
1324 { .compatible = "nvidia,tegra210-dsi", },
1325 { .compatible = "nvidia,tegra210-sor", },
1326 { .compatible = "nvidia,tegra210-sor1", },
1327 { .compatible = "nvidia,tegra210-vic", },
1328 { .compatible = "nvidia,tegra186-display", },
1329 { .compatible = "nvidia,tegra186-dc", },
1330 { .compatible = "nvidia,tegra186-sor", },
1331 { .compatible = "nvidia,tegra186-sor1", },
1332 { .compatible = "nvidia,tegra186-vic", },
1333 { .compatible = "nvidia,tegra194-display", },
1334 { .compatible = "nvidia,tegra194-dc", },
1335 { .compatible = "nvidia,tegra194-sor", },
1336 { .compatible = "nvidia,tegra194-vic", },
1340 static struct host1x_driver host1x_drm_driver = {
1343 .pm = &host1x_drm_pm_ops,
1345 .probe = host1x_drm_probe,
1346 .remove = host1x_drm_remove,
1347 .subdevs = host1x_drm_subdevs,
1350 static struct platform_driver * const drivers[] = {
1351 &tegra_display_hub_driver,
1355 &tegra_dpaux_driver,
1362 static int __init host1x_drm_init(void)
1366 err = host1x_driver_register(&host1x_drm_driver);
1370 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1372 goto unregister_host1x;
1377 host1x_driver_unregister(&host1x_drm_driver);
1380 module_init(host1x_drm_init);
1382 static void __exit host1x_drm_exit(void)
1384 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1385 host1x_driver_unregister(&host1x_drm_driver);
1387 module_exit(host1x_drm_exit);
1390 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1391 MODULE_LICENSE("GPL v2");