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>
29 #define DRIVER_NAME "tegra"
30 #define DRIVER_DESC "NVIDIA Tegra graphics"
31 #define DRIVER_DATE "20120330"
32 #define DRIVER_MAJOR 1
33 #define DRIVER_MINOR 0
34 #define DRIVER_PATCHLEVEL 0
36 #define CARVEOUT_SZ SZ_64M
37 #define CDMA_GATHER_FETCHES_MAX_NB 16383
39 static int tegra_atomic_check(struct drm_device *drm,
40 struct drm_atomic_state *state)
44 err = drm_atomic_helper_check(drm, state);
48 return tegra_display_hub_atomic_check(drm, state);
51 static const struct drm_mode_config_funcs tegra_drm_mode_config_funcs = {
52 .fb_create = tegra_fb_create,
53 #ifdef CONFIG_DRM_FBDEV_EMULATION
54 .output_poll_changed = drm_fb_helper_output_poll_changed,
56 .atomic_check = tegra_atomic_check,
57 .atomic_commit = drm_atomic_helper_commit,
60 static void tegra_atomic_post_commit(struct drm_device *drm,
61 struct drm_atomic_state *old_state)
63 struct drm_crtc_state *old_crtc_state __maybe_unused;
64 struct drm_crtc *crtc;
67 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i)
68 tegra_crtc_atomic_post_commit(crtc, old_state);
71 static void tegra_atomic_commit_tail(struct drm_atomic_state *old_state)
73 struct drm_device *drm = old_state->dev;
74 struct tegra_drm *tegra = drm->dev_private;
77 bool fence_cookie = dma_fence_begin_signalling();
79 drm_atomic_helper_commit_modeset_disables(drm, old_state);
80 tegra_display_hub_atomic_commit(drm, old_state);
81 drm_atomic_helper_commit_planes(drm, old_state, 0);
82 drm_atomic_helper_commit_modeset_enables(drm, old_state);
83 drm_atomic_helper_commit_hw_done(old_state);
84 dma_fence_end_signalling(fence_cookie);
85 drm_atomic_helper_wait_for_vblanks(drm, old_state);
86 drm_atomic_helper_cleanup_planes(drm, old_state);
88 drm_atomic_helper_commit_tail_rpm(old_state);
91 tegra_atomic_post_commit(drm, old_state);
94 static const struct drm_mode_config_helper_funcs
95 tegra_drm_mode_config_helpers = {
96 .atomic_commit_tail = tegra_atomic_commit_tail,
99 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
101 struct tegra_drm_file *fpriv;
103 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
107 idr_init_base(&fpriv->legacy_contexts, 1);
108 xa_init_flags(&fpriv->contexts, XA_FLAGS_ALLOC1);
109 xa_init(&fpriv->syncpoints);
110 mutex_init(&fpriv->lock);
111 filp->driver_priv = fpriv;
116 static void tegra_drm_context_free(struct tegra_drm_context *context)
118 context->client->ops->close_channel(context);
122 static int host1x_reloc_copy_from_user(struct host1x_reloc *dest,
123 struct drm_tegra_reloc __user *src,
124 struct drm_device *drm,
125 struct drm_file *file)
130 err = get_user(cmdbuf, &src->cmdbuf.handle);
134 err = get_user(dest->cmdbuf.offset, &src->cmdbuf.offset);
138 err = get_user(target, &src->target.handle);
142 err = get_user(dest->target.offset, &src->target.offset);
146 err = get_user(dest->shift, &src->shift);
150 dest->flags = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
152 dest->cmdbuf.bo = tegra_gem_lookup(file, cmdbuf);
153 if (!dest->cmdbuf.bo)
156 dest->target.bo = tegra_gem_lookup(file, target);
157 if (!dest->target.bo)
163 int tegra_drm_submit(struct tegra_drm_context *context,
164 struct drm_tegra_submit *args, struct drm_device *drm,
165 struct drm_file *file)
167 struct host1x_client *client = &context->client->base;
168 unsigned int num_cmdbufs = args->num_cmdbufs;
169 unsigned int num_relocs = args->num_relocs;
170 struct drm_tegra_cmdbuf __user *user_cmdbufs;
171 struct drm_tegra_reloc __user *user_relocs;
172 struct drm_tegra_syncpt __user *user_syncpt;
173 struct drm_tegra_syncpt syncpt;
174 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
175 struct drm_gem_object **refs;
176 struct host1x_syncpt *sp = NULL;
177 struct host1x_job *job;
178 unsigned int num_refs;
181 user_cmdbufs = u64_to_user_ptr(args->cmdbufs);
182 user_relocs = u64_to_user_ptr(args->relocs);
183 user_syncpt = u64_to_user_ptr(args->syncpts);
185 /* We don't yet support other than one syncpt_incr struct per submit */
186 if (args->num_syncpts != 1)
189 /* We don't yet support waitchks */
190 if (args->num_waitchks != 0)
193 job = host1x_job_alloc(context->channel, args->num_cmdbufs,
194 args->num_relocs, false);
198 job->num_relocs = args->num_relocs;
199 job->client = client;
200 job->class = client->class;
201 job->serialize = true;
202 job->syncpt_recovery = true;
205 * Track referenced BOs so that they can be unreferenced after the
206 * submission is complete.
208 num_refs = num_cmdbufs + num_relocs * 2;
210 refs = kmalloc_array(num_refs, sizeof(*refs), GFP_KERNEL);
216 /* reuse as an iterator later */
219 while (num_cmdbufs) {
220 struct drm_tegra_cmdbuf cmdbuf;
221 struct host1x_bo *bo;
222 struct tegra_bo *obj;
225 if (copy_from_user(&cmdbuf, user_cmdbufs, sizeof(cmdbuf))) {
231 * The maximum number of CDMA gather fetches is 16383, a higher
232 * value means the words count is malformed.
234 if (cmdbuf.words > CDMA_GATHER_FETCHES_MAX_NB) {
239 bo = tegra_gem_lookup(file, cmdbuf.handle);
245 offset = (u64)cmdbuf.offset + (u64)cmdbuf.words * sizeof(u32);
246 obj = host1x_to_tegra_bo(bo);
247 refs[num_refs++] = &obj->gem;
250 * Gather buffer base address must be 4-bytes aligned,
251 * unaligned offset is malformed and cause commands stream
252 * corruption on the buffer address relocation.
254 if (offset & 3 || offset > obj->gem.size) {
259 host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
264 /* copy and resolve relocations from submit */
265 while (num_relocs--) {
266 struct host1x_reloc *reloc;
267 struct tegra_bo *obj;
269 err = host1x_reloc_copy_from_user(&job->relocs[num_relocs],
270 &user_relocs[num_relocs], drm,
275 reloc = &job->relocs[num_relocs];
276 obj = host1x_to_tegra_bo(reloc->cmdbuf.bo);
277 refs[num_refs++] = &obj->gem;
280 * The unaligned cmdbuf offset will cause an unaligned write
281 * during of the relocations patching, corrupting the commands
284 if (reloc->cmdbuf.offset & 3 ||
285 reloc->cmdbuf.offset >= obj->gem.size) {
290 obj = host1x_to_tegra_bo(reloc->target.bo);
291 refs[num_refs++] = &obj->gem;
293 if (reloc->target.offset >= obj->gem.size) {
299 if (copy_from_user(&syncpt, user_syncpt, sizeof(syncpt))) {
304 /* Syncpoint ref will be dropped on job release. */
305 sp = host1x_syncpt_get_by_id(host1x, syncpt.id);
311 job->is_addr_reg = context->client->ops->is_addr_reg;
312 job->is_valid_class = context->client->ops->is_valid_class;
313 job->syncpt_incrs = syncpt.incrs;
315 job->timeout = 10000;
317 if (args->timeout && args->timeout < 10000)
318 job->timeout = args->timeout;
320 err = host1x_job_pin(job, context->client->base.dev);
324 err = host1x_job_submit(job);
326 host1x_job_unpin(job);
330 args->fence = job->syncpt_end;
334 drm_gem_object_put(refs[num_refs]);
344 #ifdef CONFIG_DRM_TEGRA_STAGING
345 static int tegra_gem_create(struct drm_device *drm, void *data,
346 struct drm_file *file)
348 struct drm_tegra_gem_create *args = data;
351 bo = tegra_bo_create_with_handle(file, drm, args->size, args->flags,
359 static int tegra_gem_mmap(struct drm_device *drm, void *data,
360 struct drm_file *file)
362 struct drm_tegra_gem_mmap *args = data;
363 struct drm_gem_object *gem;
366 gem = drm_gem_object_lookup(file, args->handle);
370 bo = to_tegra_bo(gem);
372 args->offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
374 drm_gem_object_put(gem);
379 static int tegra_syncpt_read(struct drm_device *drm, void *data,
380 struct drm_file *file)
382 struct host1x *host = dev_get_drvdata(drm->dev->parent);
383 struct drm_tegra_syncpt_read *args = data;
384 struct host1x_syncpt *sp;
386 sp = host1x_syncpt_get_by_id_noref(host, args->id);
390 args->value = host1x_syncpt_read_min(sp);
394 static int tegra_syncpt_incr(struct drm_device *drm, void *data,
395 struct drm_file *file)
397 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
398 struct drm_tegra_syncpt_incr *args = data;
399 struct host1x_syncpt *sp;
401 sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
405 return host1x_syncpt_incr(sp);
408 static int tegra_syncpt_wait(struct drm_device *drm, void *data,
409 struct drm_file *file)
411 struct host1x *host1x = dev_get_drvdata(drm->dev->parent);
412 struct drm_tegra_syncpt_wait *args = data;
413 struct host1x_syncpt *sp;
415 sp = host1x_syncpt_get_by_id_noref(host1x, args->id);
419 return host1x_syncpt_wait(sp, args->thresh,
420 msecs_to_jiffies(args->timeout),
424 static int tegra_client_open(struct tegra_drm_file *fpriv,
425 struct tegra_drm_client *client,
426 struct tegra_drm_context *context)
430 err = client->ops->open_channel(client, context);
434 err = idr_alloc(&fpriv->legacy_contexts, context, 1, 0, GFP_KERNEL);
436 client->ops->close_channel(context);
440 context->client = client;
446 static int tegra_open_channel(struct drm_device *drm, void *data,
447 struct drm_file *file)
449 struct tegra_drm_file *fpriv = file->driver_priv;
450 struct tegra_drm *tegra = drm->dev_private;
451 struct drm_tegra_open_channel *args = data;
452 struct tegra_drm_context *context;
453 struct tegra_drm_client *client;
456 context = kzalloc(sizeof(*context), GFP_KERNEL);
460 mutex_lock(&fpriv->lock);
462 list_for_each_entry(client, &tegra->clients, list)
463 if (client->base.class == args->client) {
464 err = tegra_client_open(fpriv, client, context);
468 args->context = context->id;
475 mutex_unlock(&fpriv->lock);
479 static int tegra_close_channel(struct drm_device *drm, void *data,
480 struct drm_file *file)
482 struct tegra_drm_file *fpriv = file->driver_priv;
483 struct drm_tegra_close_channel *args = data;
484 struct tegra_drm_context *context;
487 mutex_lock(&fpriv->lock);
489 context = idr_find(&fpriv->legacy_contexts, args->context);
495 idr_remove(&fpriv->legacy_contexts, context->id);
496 tegra_drm_context_free(context);
499 mutex_unlock(&fpriv->lock);
503 static int tegra_get_syncpt(struct drm_device *drm, void *data,
504 struct drm_file *file)
506 struct tegra_drm_file *fpriv = file->driver_priv;
507 struct drm_tegra_get_syncpt *args = data;
508 struct tegra_drm_context *context;
509 struct host1x_syncpt *syncpt;
512 mutex_lock(&fpriv->lock);
514 context = idr_find(&fpriv->legacy_contexts, args->context);
520 if (args->index >= context->client->base.num_syncpts) {
525 syncpt = context->client->base.syncpts[args->index];
526 args->id = host1x_syncpt_id(syncpt);
529 mutex_unlock(&fpriv->lock);
533 static int tegra_submit(struct drm_device *drm, void *data,
534 struct drm_file *file)
536 struct tegra_drm_file *fpriv = file->driver_priv;
537 struct drm_tegra_submit *args = data;
538 struct tegra_drm_context *context;
541 mutex_lock(&fpriv->lock);
543 context = idr_find(&fpriv->legacy_contexts, args->context);
549 err = context->client->ops->submit(context, args, drm, file);
552 mutex_unlock(&fpriv->lock);
556 static int tegra_get_syncpt_base(struct drm_device *drm, void *data,
557 struct drm_file *file)
559 struct tegra_drm_file *fpriv = file->driver_priv;
560 struct drm_tegra_get_syncpt_base *args = data;
561 struct tegra_drm_context *context;
562 struct host1x_syncpt_base *base;
563 struct host1x_syncpt *syncpt;
566 mutex_lock(&fpriv->lock);
568 context = idr_find(&fpriv->legacy_contexts, args->context);
574 if (args->syncpt >= context->client->base.num_syncpts) {
579 syncpt = context->client->base.syncpts[args->syncpt];
581 base = host1x_syncpt_get_base(syncpt);
587 args->id = host1x_syncpt_base_id(base);
590 mutex_unlock(&fpriv->lock);
594 static int tegra_gem_set_tiling(struct drm_device *drm, void *data,
595 struct drm_file *file)
597 struct drm_tegra_gem_set_tiling *args = data;
598 enum tegra_bo_tiling_mode mode;
599 struct drm_gem_object *gem;
600 unsigned long value = 0;
603 switch (args->mode) {
604 case DRM_TEGRA_GEM_TILING_MODE_PITCH:
605 mode = TEGRA_BO_TILING_MODE_PITCH;
607 if (args->value != 0)
612 case DRM_TEGRA_GEM_TILING_MODE_TILED:
613 mode = TEGRA_BO_TILING_MODE_TILED;
615 if (args->value != 0)
620 case DRM_TEGRA_GEM_TILING_MODE_BLOCK:
621 mode = TEGRA_BO_TILING_MODE_BLOCK;
633 gem = drm_gem_object_lookup(file, args->handle);
637 bo = to_tegra_bo(gem);
639 bo->tiling.mode = mode;
640 bo->tiling.value = value;
642 drm_gem_object_put(gem);
647 static int tegra_gem_get_tiling(struct drm_device *drm, void *data,
648 struct drm_file *file)
650 struct drm_tegra_gem_get_tiling *args = data;
651 struct drm_gem_object *gem;
655 gem = drm_gem_object_lookup(file, args->handle);
659 bo = to_tegra_bo(gem);
661 switch (bo->tiling.mode) {
662 case TEGRA_BO_TILING_MODE_PITCH:
663 args->mode = DRM_TEGRA_GEM_TILING_MODE_PITCH;
667 case TEGRA_BO_TILING_MODE_TILED:
668 args->mode = DRM_TEGRA_GEM_TILING_MODE_TILED;
672 case TEGRA_BO_TILING_MODE_BLOCK:
673 args->mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK;
674 args->value = bo->tiling.value;
682 drm_gem_object_put(gem);
687 static int tegra_gem_set_flags(struct drm_device *drm, void *data,
688 struct drm_file *file)
690 struct drm_tegra_gem_set_flags *args = data;
691 struct drm_gem_object *gem;
694 if (args->flags & ~DRM_TEGRA_GEM_FLAGS)
697 gem = drm_gem_object_lookup(file, args->handle);
701 bo = to_tegra_bo(gem);
704 if (args->flags & DRM_TEGRA_GEM_BOTTOM_UP)
705 bo->flags |= TEGRA_BO_BOTTOM_UP;
707 drm_gem_object_put(gem);
712 static int tegra_gem_get_flags(struct drm_device *drm, void *data,
713 struct drm_file *file)
715 struct drm_tegra_gem_get_flags *args = data;
716 struct drm_gem_object *gem;
719 gem = drm_gem_object_lookup(file, args->handle);
723 bo = to_tegra_bo(gem);
726 if (bo->flags & TEGRA_BO_BOTTOM_UP)
727 args->flags |= DRM_TEGRA_GEM_BOTTOM_UP;
729 drm_gem_object_put(gem);
735 static const struct drm_ioctl_desc tegra_drm_ioctls[] = {
736 #ifdef CONFIG_DRM_TEGRA_STAGING
737 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_OPEN, tegra_drm_ioctl_channel_open,
739 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_CLOSE, tegra_drm_ioctl_channel_close,
741 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_MAP, tegra_drm_ioctl_channel_map,
743 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_UNMAP, tegra_drm_ioctl_channel_unmap,
745 DRM_IOCTL_DEF_DRV(TEGRA_CHANNEL_SUBMIT, tegra_drm_ioctl_channel_submit,
747 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_ALLOCATE, tegra_drm_ioctl_syncpoint_allocate,
749 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_FREE, tegra_drm_ioctl_syncpoint_free,
751 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPOINT_WAIT, tegra_drm_ioctl_syncpoint_wait,
754 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE, tegra_gem_create, DRM_RENDER_ALLOW),
755 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP, tegra_gem_mmap, DRM_RENDER_ALLOW),
756 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ, tegra_syncpt_read,
758 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR, tegra_syncpt_incr,
760 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT, tegra_syncpt_wait,
762 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL, tegra_open_channel,
764 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL, tegra_close_channel,
766 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT, tegra_get_syncpt,
768 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT, tegra_submit,
770 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE, tegra_get_syncpt_base,
772 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING, tegra_gem_set_tiling,
774 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING, tegra_gem_get_tiling,
776 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS, tegra_gem_set_flags,
778 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS, tegra_gem_get_flags,
783 static const struct file_operations tegra_drm_fops = {
784 .owner = THIS_MODULE,
786 .release = drm_release,
787 .unlocked_ioctl = drm_ioctl,
788 .mmap = tegra_drm_mmap,
791 .compat_ioctl = drm_compat_ioctl,
792 .llseek = noop_llseek,
795 static int tegra_drm_context_cleanup(int id, void *p, void *data)
797 struct tegra_drm_context *context = p;
799 tegra_drm_context_free(context);
804 static void tegra_drm_postclose(struct drm_device *drm, struct drm_file *file)
806 struct tegra_drm_file *fpriv = file->driver_priv;
808 mutex_lock(&fpriv->lock);
809 idr_for_each(&fpriv->legacy_contexts, tegra_drm_context_cleanup, NULL);
810 tegra_drm_uapi_close_file(fpriv);
811 mutex_unlock(&fpriv->lock);
813 idr_destroy(&fpriv->legacy_contexts);
814 mutex_destroy(&fpriv->lock);
818 #ifdef CONFIG_DEBUG_FS
819 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
821 struct drm_info_node *node = (struct drm_info_node *)s->private;
822 struct drm_device *drm = node->minor->dev;
823 struct drm_framebuffer *fb;
825 mutex_lock(&drm->mode_config.fb_lock);
827 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
828 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
829 fb->base.id, fb->width, fb->height,
831 fb->format->cpp[0] * 8,
832 drm_framebuffer_read_refcount(fb));
835 mutex_unlock(&drm->mode_config.fb_lock);
840 static int tegra_debugfs_iova(struct seq_file *s, void *data)
842 struct drm_info_node *node = (struct drm_info_node *)s->private;
843 struct drm_device *drm = node->minor->dev;
844 struct tegra_drm *tegra = drm->dev_private;
845 struct drm_printer p = drm_seq_file_printer(s);
848 mutex_lock(&tegra->mm_lock);
849 drm_mm_print(&tegra->mm, &p);
850 mutex_unlock(&tegra->mm_lock);
856 static struct drm_info_list tegra_debugfs_list[] = {
857 { "framebuffers", tegra_debugfs_framebuffers, 0 },
858 { "iova", tegra_debugfs_iova, 0 },
861 static void tegra_debugfs_init(struct drm_minor *minor)
863 drm_debugfs_create_files(tegra_debugfs_list,
864 ARRAY_SIZE(tegra_debugfs_list),
865 minor->debugfs_root, minor);
869 static const struct drm_driver tegra_drm_driver = {
870 .driver_features = DRIVER_MODESET | DRIVER_GEM |
871 DRIVER_ATOMIC | DRIVER_RENDER | DRIVER_SYNCOBJ,
872 .open = tegra_drm_open,
873 .postclose = tegra_drm_postclose,
874 .lastclose = drm_fb_helper_lastclose,
876 #if defined(CONFIG_DEBUG_FS)
877 .debugfs_init = tegra_debugfs_init,
880 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
881 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
882 .gem_prime_import = tegra_gem_prime_import,
884 .dumb_create = tegra_bo_dumb_create,
886 .ioctls = tegra_drm_ioctls,
887 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
888 .fops = &tegra_drm_fops,
893 .major = DRIVER_MAJOR,
894 .minor = DRIVER_MINOR,
895 .patchlevel = DRIVER_PATCHLEVEL,
898 int tegra_drm_register_client(struct tegra_drm *tegra,
899 struct tegra_drm_client *client)
902 * When MLOCKs are implemented, change to allocate a shared channel
903 * only when MLOCKs are disabled.
905 client->shared_channel = host1x_channel_request(&client->base);
906 if (!client->shared_channel)
909 mutex_lock(&tegra->clients_lock);
910 list_add_tail(&client->list, &tegra->clients);
912 mutex_unlock(&tegra->clients_lock);
917 int tegra_drm_unregister_client(struct tegra_drm *tegra,
918 struct tegra_drm_client *client)
920 mutex_lock(&tegra->clients_lock);
921 list_del_init(&client->list);
923 mutex_unlock(&tegra->clients_lock);
925 if (client->shared_channel)
926 host1x_channel_put(client->shared_channel);
931 int host1x_client_iommu_attach(struct host1x_client *client)
933 struct iommu_domain *domain = iommu_get_domain_for_dev(client->dev);
934 struct drm_device *drm = dev_get_drvdata(client->host);
935 struct tegra_drm *tegra = drm->dev_private;
936 struct iommu_group *group = NULL;
940 * If the host1x client is already attached to an IOMMU domain that is
941 * not the shared IOMMU domain, don't try to attach it to a different
942 * domain. This allows using the IOMMU-backed DMA API.
944 if (domain && domain != tegra->domain)
948 group = iommu_group_get(client->dev);
952 if (domain != tegra->domain) {
953 err = iommu_attach_group(tegra->domain, group);
955 iommu_group_put(group);
960 tegra->use_explicit_iommu = true;
963 client->group = group;
968 void host1x_client_iommu_detach(struct host1x_client *client)
970 struct drm_device *drm = dev_get_drvdata(client->host);
971 struct tegra_drm *tegra = drm->dev_private;
972 struct iommu_domain *domain;
976 * Devices that are part of the same group may no longer be
977 * attached to a domain at this point because their group may
978 * have been detached by an earlier client.
980 domain = iommu_get_domain_for_dev(client->dev);
982 iommu_detach_group(tegra->domain, client->group);
984 iommu_group_put(client->group);
985 client->group = NULL;
989 void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *dma)
997 size = iova_align(&tegra->carveout.domain, size);
999 size = PAGE_ALIGN(size);
1001 gfp = GFP_KERNEL | __GFP_ZERO;
1002 if (!tegra->domain) {
1004 * Many units only support 32-bit addresses, even on 64-bit
1005 * SoCs. If there is no IOMMU to translate into a 32-bit IO
1006 * virtual address space, force allocations to be in the
1007 * lower 32-bit range.
1012 virt = (void *)__get_free_pages(gfp, get_order(size));
1014 return ERR_PTR(-ENOMEM);
1016 if (!tegra->domain) {
1018 * If IOMMU is disabled, devices address physical memory
1021 *dma = virt_to_phys(virt);
1025 alloc = alloc_iova(&tegra->carveout.domain,
1026 size >> tegra->carveout.shift,
1027 tegra->carveout.limit, true);
1033 *dma = iova_dma_addr(&tegra->carveout.domain, alloc);
1034 err = iommu_map(tegra->domain, *dma, virt_to_phys(virt),
1035 size, IOMMU_READ | IOMMU_WRITE);
1042 __free_iova(&tegra->carveout.domain, alloc);
1044 free_pages((unsigned long)virt, get_order(size));
1046 return ERR_PTR(err);
1049 void tegra_drm_free(struct tegra_drm *tegra, size_t size, void *virt,
1053 size = iova_align(&tegra->carveout.domain, size);
1055 size = PAGE_ALIGN(size);
1057 if (tegra->domain) {
1058 iommu_unmap(tegra->domain, dma, size);
1059 free_iova(&tegra->carveout.domain,
1060 iova_pfn(&tegra->carveout.domain, dma));
1063 free_pages((unsigned long)virt, get_order(size));
1066 static bool host1x_drm_wants_iommu(struct host1x_device *dev)
1068 struct host1x *host1x = dev_get_drvdata(dev->dev.parent);
1069 struct iommu_domain *domain;
1072 * If the Tegra DRM clients are backed by an IOMMU, push buffers are
1073 * likely to be allocated beyond the 32-bit boundary if sufficient
1074 * system memory is available. This is problematic on earlier Tegra
1075 * generations where host1x supports a maximum of 32 address bits in
1076 * the GATHER opcode. In this case, unless host1x is behind an IOMMU
1077 * as well it won't be able to process buffers allocated beyond the
1080 * The DMA API will use bounce buffers in this case, so that could
1081 * perhaps still be made to work, even if less efficient, but there
1082 * is another catch: in order to perform cache maintenance on pages
1083 * allocated for discontiguous buffers we need to map and unmap the
1084 * SG table representing these buffers. This is fine for something
1085 * small like a push buffer, but it exhausts the bounce buffer pool
1086 * (typically on the order of a few MiB) for framebuffers (many MiB
1087 * for any modern resolution).
1089 * Work around this by making sure that Tegra DRM clients only use
1090 * an IOMMU if the parent host1x also uses an IOMMU.
1092 * Note that there's still a small gap here that we don't cover: if
1093 * the DMA API is backed by an IOMMU there's no way to control which
1094 * device is attached to an IOMMU and which isn't, except via wiring
1095 * up the device tree appropriately. This is considered an problem
1096 * of integration, so care must be taken for the DT to be consistent.
1098 domain = iommu_get_domain_for_dev(dev->dev.parent);
1101 * Tegra20 and Tegra30 don't support addressing memory beyond the
1102 * 32-bit boundary, so the regular GATHER opcodes will always be
1103 * sufficient and whether or not the host1x is attached to an IOMMU
1106 if (!domain && host1x_get_dma_mask(host1x) <= DMA_BIT_MASK(32))
1109 return domain != NULL;
1112 static int host1x_drm_probe(struct host1x_device *dev)
1114 struct tegra_drm *tegra;
1115 struct drm_device *drm;
1118 drm = drm_dev_alloc(&tegra_drm_driver, &dev->dev);
1120 return PTR_ERR(drm);
1122 tegra = kzalloc(sizeof(*tegra), GFP_KERNEL);
1128 if (host1x_drm_wants_iommu(dev) && iommu_present(&platform_bus_type)) {
1129 tegra->domain = iommu_domain_alloc(&platform_bus_type);
1130 if (!tegra->domain) {
1135 err = iova_cache_get();
1140 mutex_init(&tegra->clients_lock);
1141 INIT_LIST_HEAD(&tegra->clients);
1143 dev_set_drvdata(&dev->dev, drm);
1144 drm->dev_private = tegra;
1147 drm_mode_config_init(drm);
1149 drm->mode_config.min_width = 0;
1150 drm->mode_config.min_height = 0;
1151 drm->mode_config.max_width = 0;
1152 drm->mode_config.max_height = 0;
1154 drm->mode_config.normalize_zpos = true;
1156 drm->mode_config.funcs = &tegra_drm_mode_config_funcs;
1157 drm->mode_config.helper_private = &tegra_drm_mode_config_helpers;
1159 err = tegra_drm_fb_prepare(drm);
1163 drm_kms_helper_poll_init(drm);
1165 err = host1x_device_init(dev);
1170 * Now that all display controller have been initialized, the maximum
1171 * supported resolution is known and the bitmask for horizontal and
1172 * vertical bitfields can be computed.
1174 tegra->hmask = drm->mode_config.max_width - 1;
1175 tegra->vmask = drm->mode_config.max_height - 1;
1177 if (tegra->use_explicit_iommu) {
1178 u64 carveout_start, carveout_end, gem_start, gem_end;
1179 u64 dma_mask = dma_get_mask(&dev->dev);
1180 dma_addr_t start, end;
1181 unsigned long order;
1183 start = tegra->domain->geometry.aperture_start & dma_mask;
1184 end = tegra->domain->geometry.aperture_end & dma_mask;
1187 gem_end = end - CARVEOUT_SZ;
1188 carveout_start = gem_end + 1;
1191 order = __ffs(tegra->domain->pgsize_bitmap);
1192 init_iova_domain(&tegra->carveout.domain, 1UL << order,
1193 carveout_start >> order);
1195 tegra->carveout.shift = iova_shift(&tegra->carveout.domain);
1196 tegra->carveout.limit = carveout_end >> tegra->carveout.shift;
1198 drm_mm_init(&tegra->mm, gem_start, gem_end - gem_start + 1);
1199 mutex_init(&tegra->mm_lock);
1201 DRM_DEBUG_DRIVER("IOMMU apertures:\n");
1202 DRM_DEBUG_DRIVER(" GEM: %#llx-%#llx\n", gem_start, gem_end);
1203 DRM_DEBUG_DRIVER(" Carveout: %#llx-%#llx\n", carveout_start,
1205 } else if (tegra->domain) {
1206 iommu_domain_free(tegra->domain);
1207 tegra->domain = NULL;
1212 err = tegra_display_hub_prepare(tegra->hub);
1217 /* syncpoints are used for full 32-bit hardware VBLANK counters */
1218 drm->max_vblank_count = 0xffffffff;
1220 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
1224 drm_mode_config_reset(drm);
1226 err = drm_aperture_remove_framebuffers(false, &tegra_drm_driver);
1230 err = tegra_drm_fb_init(drm);
1234 err = drm_dev_register(drm, 0);
1241 tegra_drm_fb_exit(drm);
1244 tegra_display_hub_cleanup(tegra->hub);
1246 if (tegra->domain) {
1247 mutex_destroy(&tegra->mm_lock);
1248 drm_mm_takedown(&tegra->mm);
1249 put_iova_domain(&tegra->carveout.domain);
1253 host1x_device_exit(dev);
1255 drm_kms_helper_poll_fini(drm);
1256 tegra_drm_fb_free(drm);
1258 drm_mode_config_cleanup(drm);
1261 iommu_domain_free(tegra->domain);
1269 static int host1x_drm_remove(struct host1x_device *dev)
1271 struct drm_device *drm = dev_get_drvdata(&dev->dev);
1272 struct tegra_drm *tegra = drm->dev_private;
1275 drm_dev_unregister(drm);
1277 drm_kms_helper_poll_fini(drm);
1278 tegra_drm_fb_exit(drm);
1279 drm_atomic_helper_shutdown(drm);
1280 drm_mode_config_cleanup(drm);
1283 tegra_display_hub_cleanup(tegra->hub);
1285 err = host1x_device_exit(dev);
1287 dev_err(&dev->dev, "host1x device cleanup failed: %d\n", err);
1289 if (tegra->domain) {
1290 mutex_destroy(&tegra->mm_lock);
1291 drm_mm_takedown(&tegra->mm);
1292 put_iova_domain(&tegra->carveout.domain);
1294 iommu_domain_free(tegra->domain);
1303 #ifdef CONFIG_PM_SLEEP
1304 static int host1x_drm_suspend(struct device *dev)
1306 struct drm_device *drm = dev_get_drvdata(dev);
1308 return drm_mode_config_helper_suspend(drm);
1311 static int host1x_drm_resume(struct device *dev)
1313 struct drm_device *drm = dev_get_drvdata(dev);
1315 return drm_mode_config_helper_resume(drm);
1319 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops, host1x_drm_suspend,
1322 static const struct of_device_id host1x_drm_subdevs[] = {
1323 { .compatible = "nvidia,tegra20-dc", },
1324 { .compatible = "nvidia,tegra20-hdmi", },
1325 { .compatible = "nvidia,tegra20-gr2d", },
1326 { .compatible = "nvidia,tegra20-gr3d", },
1327 { .compatible = "nvidia,tegra30-dc", },
1328 { .compatible = "nvidia,tegra30-hdmi", },
1329 { .compatible = "nvidia,tegra30-gr2d", },
1330 { .compatible = "nvidia,tegra30-gr3d", },
1331 { .compatible = "nvidia,tegra114-dc", },
1332 { .compatible = "nvidia,tegra114-dsi", },
1333 { .compatible = "nvidia,tegra114-hdmi", },
1334 { .compatible = "nvidia,tegra114-gr2d", },
1335 { .compatible = "nvidia,tegra114-gr3d", },
1336 { .compatible = "nvidia,tegra124-dc", },
1337 { .compatible = "nvidia,tegra124-sor", },
1338 { .compatible = "nvidia,tegra124-hdmi", },
1339 { .compatible = "nvidia,tegra124-dsi", },
1340 { .compatible = "nvidia,tegra124-vic", },
1341 { .compatible = "nvidia,tegra132-dsi", },
1342 { .compatible = "nvidia,tegra210-dc", },
1343 { .compatible = "nvidia,tegra210-dsi", },
1344 { .compatible = "nvidia,tegra210-sor", },
1345 { .compatible = "nvidia,tegra210-sor1", },
1346 { .compatible = "nvidia,tegra210-vic", },
1347 { .compatible = "nvidia,tegra186-display", },
1348 { .compatible = "nvidia,tegra186-dc", },
1349 { .compatible = "nvidia,tegra186-sor", },
1350 { .compatible = "nvidia,tegra186-sor1", },
1351 { .compatible = "nvidia,tegra186-vic", },
1352 { .compatible = "nvidia,tegra194-display", },
1353 { .compatible = "nvidia,tegra194-dc", },
1354 { .compatible = "nvidia,tegra194-sor", },
1355 { .compatible = "nvidia,tegra194-vic", },
1359 static struct host1x_driver host1x_drm_driver = {
1362 .pm = &host1x_drm_pm_ops,
1364 .probe = host1x_drm_probe,
1365 .remove = host1x_drm_remove,
1366 .subdevs = host1x_drm_subdevs,
1369 static struct platform_driver * const drivers[] = {
1370 &tegra_display_hub_driver,
1374 &tegra_dpaux_driver,
1381 static int __init host1x_drm_init(void)
1385 err = host1x_driver_register(&host1x_drm_driver);
1389 err = platform_register_drivers(drivers, ARRAY_SIZE(drivers));
1391 goto unregister_host1x;
1396 host1x_driver_unregister(&host1x_drm_driver);
1399 module_init(host1x_drm_init);
1401 static void __exit host1x_drm_exit(void)
1403 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
1404 host1x_driver_unregister(&host1x_drm_driver);
1406 module_exit(host1x_drm_exit);
1409 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1410 MODULE_LICENSE("GPL v2");