2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012-2016 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.
10 #include <linux/bitops.h>
11 #include <linux/host1x.h>
12 #include <linux/idr.h>
13 #include <linux/iommu.h>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
21 #define DRIVER_NAME "tegra"
22 #define DRIVER_DESC "NVIDIA Tegra graphics"
23 #define DRIVER_DATE "20120330"
24 #define DRIVER_MAJOR 0
25 #define DRIVER_MINOR 0
26 #define DRIVER_PATCHLEVEL 0
28 #define CARVEOUT_SZ SZ_64M
29 #define CDMA_GATHER_FETCHES_MAX_NB 16383
31 struct tegra_drm_file
{
36 static int tegra_atomic_check(struct drm_device
*drm
,
37 struct drm_atomic_state
*state
)
41 err
= drm_atomic_helper_check_modeset(drm
, state
);
45 err
= drm_atomic_normalize_zpos(drm
, state
);
49 err
= drm_atomic_helper_check_planes(drm
, state
);
53 if (state
->legacy_cursor_update
)
54 state
->async_update
= !drm_atomic_helper_async_check(drm
, state
);
59 static struct drm_atomic_state
*
60 tegra_atomic_state_alloc(struct drm_device
*drm
)
62 struct tegra_atomic_state
*state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
64 if (!state
|| drm_atomic_state_init(drm
, &state
->base
) < 0) {
72 static void tegra_atomic_state_clear(struct drm_atomic_state
*state
)
74 struct tegra_atomic_state
*tegra
= to_tegra_atomic_state(state
);
76 drm_atomic_state_default_clear(state
);
77 tegra
->clk_disp
= NULL
;
82 static void tegra_atomic_state_free(struct drm_atomic_state
*state
)
84 drm_atomic_state_default_release(state
);
88 static const struct drm_mode_config_funcs tegra_drm_mode_config_funcs
= {
89 .fb_create
= tegra_fb_create
,
90 #ifdef CONFIG_DRM_FBDEV_EMULATION
91 .output_poll_changed
= drm_fb_helper_output_poll_changed
,
93 .atomic_check
= tegra_atomic_check
,
94 .atomic_commit
= drm_atomic_helper_commit
,
95 .atomic_state_alloc
= tegra_atomic_state_alloc
,
96 .atomic_state_clear
= tegra_atomic_state_clear
,
97 .atomic_state_free
= tegra_atomic_state_free
,
100 static void tegra_atomic_commit_tail(struct drm_atomic_state
*old_state
)
102 struct drm_device
*drm
= old_state
->dev
;
103 struct tegra_drm
*tegra
= drm
->dev_private
;
106 drm_atomic_helper_commit_modeset_disables(drm
, old_state
);
107 tegra_display_hub_atomic_commit(drm
, old_state
);
108 drm_atomic_helper_commit_planes(drm
, old_state
, 0);
109 drm_atomic_helper_commit_modeset_enables(drm
, old_state
);
110 drm_atomic_helper_commit_hw_done(old_state
);
111 drm_atomic_helper_wait_for_vblanks(drm
, old_state
);
112 drm_atomic_helper_cleanup_planes(drm
, old_state
);
114 drm_atomic_helper_commit_tail_rpm(old_state
);
118 static const struct drm_mode_config_helper_funcs
119 tegra_drm_mode_config_helpers
= {
120 .atomic_commit_tail
= tegra_atomic_commit_tail
,
123 static int tegra_drm_load(struct drm_device
*drm
, unsigned long flags
)
125 struct host1x_device
*device
= to_host1x_device(drm
->dev
);
126 struct tegra_drm
*tegra
;
129 tegra
= kzalloc(sizeof(*tegra
), GFP_KERNEL
);
133 if (iommu_present(&platform_bus_type
)) {
134 u64 carveout_start
, carveout_end
, gem_start
, gem_end
;
135 struct iommu_domain_geometry
*geometry
;
138 tegra
->domain
= iommu_domain_alloc(&platform_bus_type
);
139 if (!tegra
->domain
) {
144 geometry
= &tegra
->domain
->geometry
;
145 gem_start
= geometry
->aperture_start
;
146 gem_end
= geometry
->aperture_end
- CARVEOUT_SZ
;
147 carveout_start
= gem_end
+ 1;
148 carveout_end
= geometry
->aperture_end
;
150 order
= __ffs(tegra
->domain
->pgsize_bitmap
);
151 init_iova_domain(&tegra
->carveout
.domain
, 1UL << order
,
152 carveout_start
>> order
);
154 tegra
->carveout
.shift
= iova_shift(&tegra
->carveout
.domain
);
155 tegra
->carveout
.limit
= carveout_end
>> tegra
->carveout
.shift
;
157 drm_mm_init(&tegra
->mm
, gem_start
, gem_end
- gem_start
+ 1);
158 mutex_init(&tegra
->mm_lock
);
160 DRM_DEBUG("IOMMU apertures:\n");
161 DRM_DEBUG(" GEM: %#llx-%#llx\n", gem_start
, gem_end
);
162 DRM_DEBUG(" Carveout: %#llx-%#llx\n", carveout_start
,
166 mutex_init(&tegra
->clients_lock
);
167 INIT_LIST_HEAD(&tegra
->clients
);
169 drm
->dev_private
= tegra
;
172 drm_mode_config_init(drm
);
174 drm
->mode_config
.min_width
= 0;
175 drm
->mode_config
.min_height
= 0;
177 drm
->mode_config
.max_width
= 4096;
178 drm
->mode_config
.max_height
= 4096;
180 drm
->mode_config
.allow_fb_modifiers
= true;
182 drm
->mode_config
.funcs
= &tegra_drm_mode_config_funcs
;
183 drm
->mode_config
.helper_private
= &tegra_drm_mode_config_helpers
;
185 err
= tegra_drm_fb_prepare(drm
);
189 drm_kms_helper_poll_init(drm
);
191 err
= host1x_device_init(device
);
196 err
= tegra_display_hub_prepare(tegra
->hub
);
202 * We don't use the drm_irq_install() helpers provided by the DRM
203 * core, so we need to set this manually in order to allow the
204 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
206 drm
->irq_enabled
= true;
208 /* syncpoints are used for full 32-bit hardware VBLANK counters */
209 drm
->max_vblank_count
= 0xffffffff;
211 err
= drm_vblank_init(drm
, drm
->mode_config
.num_crtc
);
215 drm_mode_config_reset(drm
);
217 err
= tegra_drm_fb_init(drm
);
225 tegra_display_hub_cleanup(tegra
->hub
);
227 host1x_device_exit(device
);
229 drm_kms_helper_poll_fini(drm
);
230 tegra_drm_fb_free(drm
);
232 drm_mode_config_cleanup(drm
);
235 iommu_domain_free(tegra
->domain
);
236 drm_mm_takedown(&tegra
->mm
);
237 mutex_destroy(&tegra
->mm_lock
);
238 put_iova_domain(&tegra
->carveout
.domain
);
245 static void tegra_drm_unload(struct drm_device
*drm
)
247 struct host1x_device
*device
= to_host1x_device(drm
->dev
);
248 struct tegra_drm
*tegra
= drm
->dev_private
;
251 drm_kms_helper_poll_fini(drm
);
252 tegra_drm_fb_exit(drm
);
253 drm_atomic_helper_shutdown(drm
);
254 drm_mode_config_cleanup(drm
);
256 err
= host1x_device_exit(device
);
261 iommu_domain_free(tegra
->domain
);
262 drm_mm_takedown(&tegra
->mm
);
263 mutex_destroy(&tegra
->mm_lock
);
264 put_iova_domain(&tegra
->carveout
.domain
);
270 static int tegra_drm_open(struct drm_device
*drm
, struct drm_file
*filp
)
272 struct tegra_drm_file
*fpriv
;
274 fpriv
= kzalloc(sizeof(*fpriv
), GFP_KERNEL
);
278 idr_init(&fpriv
->contexts
);
279 mutex_init(&fpriv
->lock
);
280 filp
->driver_priv
= fpriv
;
285 static void tegra_drm_context_free(struct tegra_drm_context
*context
)
287 context
->client
->ops
->close_channel(context
);
291 static struct host1x_bo
*
292 host1x_bo_lookup(struct drm_file
*file
, u32 handle
)
294 struct drm_gem_object
*gem
;
297 gem
= drm_gem_object_lookup(file
, handle
);
301 bo
= to_tegra_bo(gem
);
305 static int host1x_reloc_copy_from_user(struct host1x_reloc
*dest
,
306 struct drm_tegra_reloc __user
*src
,
307 struct drm_device
*drm
,
308 struct drm_file
*file
)
313 err
= get_user(cmdbuf
, &src
->cmdbuf
.handle
);
317 err
= get_user(dest
->cmdbuf
.offset
, &src
->cmdbuf
.offset
);
321 err
= get_user(target
, &src
->target
.handle
);
325 err
= get_user(dest
->target
.offset
, &src
->target
.offset
);
329 err
= get_user(dest
->shift
, &src
->shift
);
333 dest
->cmdbuf
.bo
= host1x_bo_lookup(file
, cmdbuf
);
334 if (!dest
->cmdbuf
.bo
)
337 dest
->target
.bo
= host1x_bo_lookup(file
, target
);
338 if (!dest
->target
.bo
)
344 static int host1x_waitchk_copy_from_user(struct host1x_waitchk
*dest
,
345 struct drm_tegra_waitchk __user
*src
,
346 struct drm_file
*file
)
351 err
= get_user(cmdbuf
, &src
->handle
);
355 err
= get_user(dest
->offset
, &src
->offset
);
359 err
= get_user(dest
->syncpt_id
, &src
->syncpt
);
363 err
= get_user(dest
->thresh
, &src
->thresh
);
367 dest
->bo
= host1x_bo_lookup(file
, cmdbuf
);
374 int tegra_drm_submit(struct tegra_drm_context
*context
,
375 struct drm_tegra_submit
*args
, struct drm_device
*drm
,
376 struct drm_file
*file
)
378 unsigned int num_cmdbufs
= args
->num_cmdbufs
;
379 unsigned int num_relocs
= args
->num_relocs
;
380 unsigned int num_waitchks
= args
->num_waitchks
;
381 struct drm_tegra_cmdbuf __user
*user_cmdbufs
;
382 struct drm_tegra_reloc __user
*user_relocs
;
383 struct drm_tegra_waitchk __user
*user_waitchks
;
384 struct drm_tegra_syncpt __user
*user_syncpt
;
385 struct drm_tegra_syncpt syncpt
;
386 struct host1x
*host1x
= dev_get_drvdata(drm
->dev
->parent
);
387 struct drm_gem_object
**refs
;
388 struct host1x_syncpt
*sp
;
389 struct host1x_job
*job
;
390 unsigned int num_refs
;
393 user_cmdbufs
= u64_to_user_ptr(args
->cmdbufs
);
394 user_relocs
= u64_to_user_ptr(args
->relocs
);
395 user_waitchks
= u64_to_user_ptr(args
->waitchks
);
396 user_syncpt
= u64_to_user_ptr(args
->syncpts
);
398 /* We don't yet support other than one syncpt_incr struct per submit */
399 if (args
->num_syncpts
!= 1)
402 /* We don't yet support waitchks */
403 if (args
->num_waitchks
!= 0)
406 job
= host1x_job_alloc(context
->channel
, args
->num_cmdbufs
,
407 args
->num_relocs
, args
->num_waitchks
);
411 job
->num_relocs
= args
->num_relocs
;
412 job
->num_waitchk
= args
->num_waitchks
;
413 job
->client
= (u32
)args
->context
;
414 job
->class = context
->client
->base
.class;
415 job
->serialize
= true;
418 * Track referenced BOs so that they can be unreferenced after the
419 * submission is complete.
421 num_refs
= num_cmdbufs
+ num_relocs
* 2 + num_waitchks
;
423 refs
= kmalloc_array(num_refs
, sizeof(*refs
), GFP_KERNEL
);
429 /* reuse as an iterator later */
432 while (num_cmdbufs
) {
433 struct drm_tegra_cmdbuf cmdbuf
;
434 struct host1x_bo
*bo
;
435 struct tegra_bo
*obj
;
438 if (copy_from_user(&cmdbuf
, user_cmdbufs
, sizeof(cmdbuf
))) {
444 * The maximum number of CDMA gather fetches is 16383, a higher
445 * value means the words count is malformed.
447 if (cmdbuf
.words
> CDMA_GATHER_FETCHES_MAX_NB
) {
452 bo
= host1x_bo_lookup(file
, cmdbuf
.handle
);
458 offset
= (u64
)cmdbuf
.offset
+ (u64
)cmdbuf
.words
* sizeof(u32
);
459 obj
= host1x_to_tegra_bo(bo
);
460 refs
[num_refs
++] = &obj
->gem
;
463 * Gather buffer base address must be 4-bytes aligned,
464 * unaligned offset is malformed and cause commands stream
465 * corruption on the buffer address relocation.
467 if (offset
& 3 || offset
>= obj
->gem
.size
) {
472 host1x_job_add_gather(job
, bo
, cmdbuf
.words
, cmdbuf
.offset
);
477 /* copy and resolve relocations from submit */
478 while (num_relocs
--) {
479 struct host1x_reloc
*reloc
;
480 struct tegra_bo
*obj
;
482 err
= host1x_reloc_copy_from_user(&job
->relocarray
[num_relocs
],
483 &user_relocs
[num_relocs
], drm
,
488 reloc
= &job
->relocarray
[num_relocs
];
489 obj
= host1x_to_tegra_bo(reloc
->cmdbuf
.bo
);
490 refs
[num_refs
++] = &obj
->gem
;
493 * The unaligned cmdbuf offset will cause an unaligned write
494 * during of the relocations patching, corrupting the commands
497 if (reloc
->cmdbuf
.offset
& 3 ||
498 reloc
->cmdbuf
.offset
>= obj
->gem
.size
) {
503 obj
= host1x_to_tegra_bo(reloc
->target
.bo
);
504 refs
[num_refs
++] = &obj
->gem
;
506 if (reloc
->target
.offset
>= obj
->gem
.size
) {
512 /* copy and resolve waitchks from submit */
513 while (num_waitchks
--) {
514 struct host1x_waitchk
*wait
= &job
->waitchk
[num_waitchks
];
515 struct tegra_bo
*obj
;
517 err
= host1x_waitchk_copy_from_user(
518 wait
, &user_waitchks
[num_waitchks
], file
);
522 obj
= host1x_to_tegra_bo(wait
->bo
);
523 refs
[num_refs
++] = &obj
->gem
;
526 * The unaligned offset will cause an unaligned write during
527 * of the waitchks patching, corrupting the commands stream.
529 if (wait
->offset
& 3 ||
530 wait
->offset
>= obj
->gem
.size
) {
536 if (copy_from_user(&syncpt
, user_syncpt
, sizeof(syncpt
))) {
541 /* check whether syncpoint ID is valid */
542 sp
= host1x_syncpt_get(host1x
, syncpt
.id
);
548 job
->is_addr_reg
= context
->client
->ops
->is_addr_reg
;
549 job
->is_valid_class
= context
->client
->ops
->is_valid_class
;
550 job
->syncpt_incrs
= syncpt
.incrs
;
551 job
->syncpt_id
= syncpt
.id
;
552 job
->timeout
= 10000;
554 if (args
->timeout
&& args
->timeout
< 10000)
555 job
->timeout
= args
->timeout
;
557 err
= host1x_job_pin(job
, context
->client
->base
.dev
);
561 err
= host1x_job_submit(job
);
563 host1x_job_unpin(job
);
567 args
->fence
= job
->syncpt_end
;
571 drm_gem_object_put_unlocked(refs
[num_refs
]);
581 #ifdef CONFIG_DRM_TEGRA_STAGING
582 static int tegra_gem_create(struct drm_device
*drm
, void *data
,
583 struct drm_file
*file
)
585 struct drm_tegra_gem_create
*args
= data
;
588 bo
= tegra_bo_create_with_handle(file
, drm
, args
->size
, args
->flags
,
596 static int tegra_gem_mmap(struct drm_device
*drm
, void *data
,
597 struct drm_file
*file
)
599 struct drm_tegra_gem_mmap
*args
= data
;
600 struct drm_gem_object
*gem
;
603 gem
= drm_gem_object_lookup(file
, args
->handle
);
607 bo
= to_tegra_bo(gem
);
609 args
->offset
= drm_vma_node_offset_addr(&bo
->gem
.vma_node
);
611 drm_gem_object_put_unlocked(gem
);
616 static int tegra_syncpt_read(struct drm_device
*drm
, void *data
,
617 struct drm_file
*file
)
619 struct host1x
*host
= dev_get_drvdata(drm
->dev
->parent
);
620 struct drm_tegra_syncpt_read
*args
= data
;
621 struct host1x_syncpt
*sp
;
623 sp
= host1x_syncpt_get(host
, args
->id
);
627 args
->value
= host1x_syncpt_read_min(sp
);
631 static int tegra_syncpt_incr(struct drm_device
*drm
, void *data
,
632 struct drm_file
*file
)
634 struct host1x
*host1x
= dev_get_drvdata(drm
->dev
->parent
);
635 struct drm_tegra_syncpt_incr
*args
= data
;
636 struct host1x_syncpt
*sp
;
638 sp
= host1x_syncpt_get(host1x
, args
->id
);
642 return host1x_syncpt_incr(sp
);
645 static int tegra_syncpt_wait(struct drm_device
*drm
, void *data
,
646 struct drm_file
*file
)
648 struct host1x
*host1x
= dev_get_drvdata(drm
->dev
->parent
);
649 struct drm_tegra_syncpt_wait
*args
= data
;
650 struct host1x_syncpt
*sp
;
652 sp
= host1x_syncpt_get(host1x
, args
->id
);
656 return host1x_syncpt_wait(sp
, args
->thresh
,
657 msecs_to_jiffies(args
->timeout
),
661 static int tegra_client_open(struct tegra_drm_file
*fpriv
,
662 struct tegra_drm_client
*client
,
663 struct tegra_drm_context
*context
)
667 err
= client
->ops
->open_channel(client
, context
);
671 err
= idr_alloc(&fpriv
->contexts
, context
, 1, 0, GFP_KERNEL
);
673 client
->ops
->close_channel(context
);
677 context
->client
= client
;
683 static int tegra_open_channel(struct drm_device
*drm
, void *data
,
684 struct drm_file
*file
)
686 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
687 struct tegra_drm
*tegra
= drm
->dev_private
;
688 struct drm_tegra_open_channel
*args
= data
;
689 struct tegra_drm_context
*context
;
690 struct tegra_drm_client
*client
;
693 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
697 mutex_lock(&fpriv
->lock
);
699 list_for_each_entry(client
, &tegra
->clients
, list
)
700 if (client
->base
.class == args
->client
) {
701 err
= tegra_client_open(fpriv
, client
, context
);
705 args
->context
= context
->id
;
712 mutex_unlock(&fpriv
->lock
);
716 static int tegra_close_channel(struct drm_device
*drm
, void *data
,
717 struct drm_file
*file
)
719 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
720 struct drm_tegra_close_channel
*args
= data
;
721 struct tegra_drm_context
*context
;
724 mutex_lock(&fpriv
->lock
);
726 context
= idr_find(&fpriv
->contexts
, args
->context
);
732 idr_remove(&fpriv
->contexts
, context
->id
);
733 tegra_drm_context_free(context
);
736 mutex_unlock(&fpriv
->lock
);
740 static int tegra_get_syncpt(struct drm_device
*drm
, void *data
,
741 struct drm_file
*file
)
743 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
744 struct drm_tegra_get_syncpt
*args
= data
;
745 struct tegra_drm_context
*context
;
746 struct host1x_syncpt
*syncpt
;
749 mutex_lock(&fpriv
->lock
);
751 context
= idr_find(&fpriv
->contexts
, args
->context
);
757 if (args
->index
>= context
->client
->base
.num_syncpts
) {
762 syncpt
= context
->client
->base
.syncpts
[args
->index
];
763 args
->id
= host1x_syncpt_id(syncpt
);
766 mutex_unlock(&fpriv
->lock
);
770 static int tegra_submit(struct drm_device
*drm
, void *data
,
771 struct drm_file
*file
)
773 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
774 struct drm_tegra_submit
*args
= data
;
775 struct tegra_drm_context
*context
;
778 mutex_lock(&fpriv
->lock
);
780 context
= idr_find(&fpriv
->contexts
, args
->context
);
786 err
= context
->client
->ops
->submit(context
, args
, drm
, file
);
789 mutex_unlock(&fpriv
->lock
);
793 static int tegra_get_syncpt_base(struct drm_device
*drm
, void *data
,
794 struct drm_file
*file
)
796 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
797 struct drm_tegra_get_syncpt_base
*args
= data
;
798 struct tegra_drm_context
*context
;
799 struct host1x_syncpt_base
*base
;
800 struct host1x_syncpt
*syncpt
;
803 mutex_lock(&fpriv
->lock
);
805 context
= idr_find(&fpriv
->contexts
, args
->context
);
811 if (args
->syncpt
>= context
->client
->base
.num_syncpts
) {
816 syncpt
= context
->client
->base
.syncpts
[args
->syncpt
];
818 base
= host1x_syncpt_get_base(syncpt
);
824 args
->id
= host1x_syncpt_base_id(base
);
827 mutex_unlock(&fpriv
->lock
);
831 static int tegra_gem_set_tiling(struct drm_device
*drm
, void *data
,
832 struct drm_file
*file
)
834 struct drm_tegra_gem_set_tiling
*args
= data
;
835 enum tegra_bo_tiling_mode mode
;
836 struct drm_gem_object
*gem
;
837 unsigned long value
= 0;
840 switch (args
->mode
) {
841 case DRM_TEGRA_GEM_TILING_MODE_PITCH
:
842 mode
= TEGRA_BO_TILING_MODE_PITCH
;
844 if (args
->value
!= 0)
849 case DRM_TEGRA_GEM_TILING_MODE_TILED
:
850 mode
= TEGRA_BO_TILING_MODE_TILED
;
852 if (args
->value
!= 0)
857 case DRM_TEGRA_GEM_TILING_MODE_BLOCK
:
858 mode
= TEGRA_BO_TILING_MODE_BLOCK
;
870 gem
= drm_gem_object_lookup(file
, args
->handle
);
874 bo
= to_tegra_bo(gem
);
876 bo
->tiling
.mode
= mode
;
877 bo
->tiling
.value
= value
;
879 drm_gem_object_put_unlocked(gem
);
884 static int tegra_gem_get_tiling(struct drm_device
*drm
, void *data
,
885 struct drm_file
*file
)
887 struct drm_tegra_gem_get_tiling
*args
= data
;
888 struct drm_gem_object
*gem
;
892 gem
= drm_gem_object_lookup(file
, args
->handle
);
896 bo
= to_tegra_bo(gem
);
898 switch (bo
->tiling
.mode
) {
899 case TEGRA_BO_TILING_MODE_PITCH
:
900 args
->mode
= DRM_TEGRA_GEM_TILING_MODE_PITCH
;
904 case TEGRA_BO_TILING_MODE_TILED
:
905 args
->mode
= DRM_TEGRA_GEM_TILING_MODE_TILED
;
909 case TEGRA_BO_TILING_MODE_BLOCK
:
910 args
->mode
= DRM_TEGRA_GEM_TILING_MODE_BLOCK
;
911 args
->value
= bo
->tiling
.value
;
919 drm_gem_object_put_unlocked(gem
);
924 static int tegra_gem_set_flags(struct drm_device
*drm
, void *data
,
925 struct drm_file
*file
)
927 struct drm_tegra_gem_set_flags
*args
= data
;
928 struct drm_gem_object
*gem
;
931 if (args
->flags
& ~DRM_TEGRA_GEM_FLAGS
)
934 gem
= drm_gem_object_lookup(file
, args
->handle
);
938 bo
= to_tegra_bo(gem
);
941 if (args
->flags
& DRM_TEGRA_GEM_BOTTOM_UP
)
942 bo
->flags
|= TEGRA_BO_BOTTOM_UP
;
944 drm_gem_object_put_unlocked(gem
);
949 static int tegra_gem_get_flags(struct drm_device
*drm
, void *data
,
950 struct drm_file
*file
)
952 struct drm_tegra_gem_get_flags
*args
= data
;
953 struct drm_gem_object
*gem
;
956 gem
= drm_gem_object_lookup(file
, args
->handle
);
960 bo
= to_tegra_bo(gem
);
963 if (bo
->flags
& TEGRA_BO_BOTTOM_UP
)
964 args
->flags
|= DRM_TEGRA_GEM_BOTTOM_UP
;
966 drm_gem_object_put_unlocked(gem
);
972 static const struct drm_ioctl_desc tegra_drm_ioctls
[] = {
973 #ifdef CONFIG_DRM_TEGRA_STAGING
974 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE
, tegra_gem_create
,
975 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
976 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP
, tegra_gem_mmap
,
977 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
978 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ
, tegra_syncpt_read
,
979 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
980 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR
, tegra_syncpt_incr
,
981 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
982 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT
, tegra_syncpt_wait
,
983 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
984 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL
, tegra_open_channel
,
985 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
986 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL
, tegra_close_channel
,
987 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
988 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT
, tegra_get_syncpt
,
989 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
990 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT
, tegra_submit
,
991 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
992 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE
, tegra_get_syncpt_base
,
993 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
994 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING
, tegra_gem_set_tiling
,
995 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
996 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING
, tegra_gem_get_tiling
,
997 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
998 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS
, tegra_gem_set_flags
,
999 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
1000 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS
, tegra_gem_get_flags
,
1001 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
1005 static const struct file_operations tegra_drm_fops
= {
1006 .owner
= THIS_MODULE
,
1008 .release
= drm_release
,
1009 .unlocked_ioctl
= drm_ioctl
,
1010 .mmap
= tegra_drm_mmap
,
1013 .compat_ioctl
= drm_compat_ioctl
,
1014 .llseek
= noop_llseek
,
1017 static int tegra_drm_context_cleanup(int id
, void *p
, void *data
)
1019 struct tegra_drm_context
*context
= p
;
1021 tegra_drm_context_free(context
);
1026 static void tegra_drm_postclose(struct drm_device
*drm
, struct drm_file
*file
)
1028 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
1030 mutex_lock(&fpriv
->lock
);
1031 idr_for_each(&fpriv
->contexts
, tegra_drm_context_cleanup
, NULL
);
1032 mutex_unlock(&fpriv
->lock
);
1034 idr_destroy(&fpriv
->contexts
);
1035 mutex_destroy(&fpriv
->lock
);
1039 #ifdef CONFIG_DEBUG_FS
1040 static int tegra_debugfs_framebuffers(struct seq_file
*s
, void *data
)
1042 struct drm_info_node
*node
= (struct drm_info_node
*)s
->private;
1043 struct drm_device
*drm
= node
->minor
->dev
;
1044 struct drm_framebuffer
*fb
;
1046 mutex_lock(&drm
->mode_config
.fb_lock
);
1048 list_for_each_entry(fb
, &drm
->mode_config
.fb_list
, head
) {
1049 seq_printf(s
, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
1050 fb
->base
.id
, fb
->width
, fb
->height
,
1052 fb
->format
->cpp
[0] * 8,
1053 drm_framebuffer_read_refcount(fb
));
1056 mutex_unlock(&drm
->mode_config
.fb_lock
);
1061 static int tegra_debugfs_iova(struct seq_file
*s
, void *data
)
1063 struct drm_info_node
*node
= (struct drm_info_node
*)s
->private;
1064 struct drm_device
*drm
= node
->minor
->dev
;
1065 struct tegra_drm
*tegra
= drm
->dev_private
;
1066 struct drm_printer p
= drm_seq_file_printer(s
);
1068 if (tegra
->domain
) {
1069 mutex_lock(&tegra
->mm_lock
);
1070 drm_mm_print(&tegra
->mm
, &p
);
1071 mutex_unlock(&tegra
->mm_lock
);
1077 static struct drm_info_list tegra_debugfs_list
[] = {
1078 { "framebuffers", tegra_debugfs_framebuffers
, 0 },
1079 { "iova", tegra_debugfs_iova
, 0 },
1082 static int tegra_debugfs_init(struct drm_minor
*minor
)
1084 return drm_debugfs_create_files(tegra_debugfs_list
,
1085 ARRAY_SIZE(tegra_debugfs_list
),
1086 minor
->debugfs_root
, minor
);
1090 static struct drm_driver tegra_drm_driver
= {
1091 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
1092 DRIVER_ATOMIC
| DRIVER_RENDER
,
1093 .load
= tegra_drm_load
,
1094 .unload
= tegra_drm_unload
,
1095 .open
= tegra_drm_open
,
1096 .postclose
= tegra_drm_postclose
,
1097 .lastclose
= drm_fb_helper_lastclose
,
1099 #if defined(CONFIG_DEBUG_FS)
1100 .debugfs_init
= tegra_debugfs_init
,
1103 .gem_free_object_unlocked
= tegra_bo_free_object
,
1104 .gem_vm_ops
= &tegra_bo_vm_ops
,
1106 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
1107 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
1108 .gem_prime_export
= tegra_gem_prime_export
,
1109 .gem_prime_import
= tegra_gem_prime_import
,
1111 .dumb_create
= tegra_bo_dumb_create
,
1113 .ioctls
= tegra_drm_ioctls
,
1114 .num_ioctls
= ARRAY_SIZE(tegra_drm_ioctls
),
1115 .fops
= &tegra_drm_fops
,
1117 .name
= DRIVER_NAME
,
1118 .desc
= DRIVER_DESC
,
1119 .date
= DRIVER_DATE
,
1120 .major
= DRIVER_MAJOR
,
1121 .minor
= DRIVER_MINOR
,
1122 .patchlevel
= DRIVER_PATCHLEVEL
,
1125 int tegra_drm_register_client(struct tegra_drm
*tegra
,
1126 struct tegra_drm_client
*client
)
1128 mutex_lock(&tegra
->clients_lock
);
1129 list_add_tail(&client
->list
, &tegra
->clients
);
1130 mutex_unlock(&tegra
->clients_lock
);
1135 int tegra_drm_unregister_client(struct tegra_drm
*tegra
,
1136 struct tegra_drm_client
*client
)
1138 mutex_lock(&tegra
->clients_lock
);
1139 list_del_init(&client
->list
);
1140 mutex_unlock(&tegra
->clients_lock
);
1145 void *tegra_drm_alloc(struct tegra_drm
*tegra
, size_t size
, dma_addr_t
*dma
)
1153 size
= iova_align(&tegra
->carveout
.domain
, size
);
1155 size
= PAGE_ALIGN(size
);
1157 gfp
= GFP_KERNEL
| __GFP_ZERO
;
1158 if (!tegra
->domain
) {
1160 * Many units only support 32-bit addresses, even on 64-bit
1161 * SoCs. If there is no IOMMU to translate into a 32-bit IO
1162 * virtual address space, force allocations to be in the
1163 * lower 32-bit range.
1168 virt
= (void *)__get_free_pages(gfp
, get_order(size
));
1170 return ERR_PTR(-ENOMEM
);
1172 if (!tegra
->domain
) {
1174 * If IOMMU is disabled, devices address physical memory
1177 *dma
= virt_to_phys(virt
);
1181 alloc
= alloc_iova(&tegra
->carveout
.domain
,
1182 size
>> tegra
->carveout
.shift
,
1183 tegra
->carveout
.limit
, true);
1189 *dma
= iova_dma_addr(&tegra
->carveout
.domain
, alloc
);
1190 err
= iommu_map(tegra
->domain
, *dma
, virt_to_phys(virt
),
1191 size
, IOMMU_READ
| IOMMU_WRITE
);
1198 __free_iova(&tegra
->carveout
.domain
, alloc
);
1200 free_pages((unsigned long)virt
, get_order(size
));
1202 return ERR_PTR(err
);
1205 void tegra_drm_free(struct tegra_drm
*tegra
, size_t size
, void *virt
,
1209 size
= iova_align(&tegra
->carveout
.domain
, size
);
1211 size
= PAGE_ALIGN(size
);
1213 if (tegra
->domain
) {
1214 iommu_unmap(tegra
->domain
, dma
, size
);
1215 free_iova(&tegra
->carveout
.domain
,
1216 iova_pfn(&tegra
->carveout
.domain
, dma
));
1219 free_pages((unsigned long)virt
, get_order(size
));
1222 static int host1x_drm_probe(struct host1x_device
*dev
)
1224 struct drm_driver
*driver
= &tegra_drm_driver
;
1225 struct drm_device
*drm
;
1228 drm
= drm_dev_alloc(driver
, &dev
->dev
);
1230 return PTR_ERR(drm
);
1232 dev_set_drvdata(&dev
->dev
, drm
);
1234 err
= drm_dev_register(drm
, 0);
1245 static int host1x_drm_remove(struct host1x_device
*dev
)
1247 struct drm_device
*drm
= dev_get_drvdata(&dev
->dev
);
1249 drm_dev_unregister(drm
);
1255 #ifdef CONFIG_PM_SLEEP
1256 static int host1x_drm_suspend(struct device
*dev
)
1258 struct drm_device
*drm
= dev_get_drvdata(dev
);
1259 struct tegra_drm
*tegra
= drm
->dev_private
;
1261 drm_kms_helper_poll_disable(drm
);
1262 tegra_drm_fb_suspend(drm
);
1264 tegra
->state
= drm_atomic_helper_suspend(drm
);
1265 if (IS_ERR(tegra
->state
)) {
1266 tegra_drm_fb_resume(drm
);
1267 drm_kms_helper_poll_enable(drm
);
1268 return PTR_ERR(tegra
->state
);
1274 static int host1x_drm_resume(struct device
*dev
)
1276 struct drm_device
*drm
= dev_get_drvdata(dev
);
1277 struct tegra_drm
*tegra
= drm
->dev_private
;
1279 drm_atomic_helper_resume(drm
, tegra
->state
);
1280 tegra_drm_fb_resume(drm
);
1281 drm_kms_helper_poll_enable(drm
);
1287 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops
, host1x_drm_suspend
,
1290 static const struct of_device_id host1x_drm_subdevs
[] = {
1291 { .compatible
= "nvidia,tegra20-dc", },
1292 { .compatible
= "nvidia,tegra20-hdmi", },
1293 { .compatible
= "nvidia,tegra20-gr2d", },
1294 { .compatible
= "nvidia,tegra20-gr3d", },
1295 { .compatible
= "nvidia,tegra30-dc", },
1296 { .compatible
= "nvidia,tegra30-hdmi", },
1297 { .compatible
= "nvidia,tegra30-gr2d", },
1298 { .compatible
= "nvidia,tegra30-gr3d", },
1299 { .compatible
= "nvidia,tegra114-dsi", },
1300 { .compatible
= "nvidia,tegra114-hdmi", },
1301 { .compatible
= "nvidia,tegra114-gr3d", },
1302 { .compatible
= "nvidia,tegra124-dc", },
1303 { .compatible
= "nvidia,tegra124-sor", },
1304 { .compatible
= "nvidia,tegra124-hdmi", },
1305 { .compatible
= "nvidia,tegra124-dsi", },
1306 { .compatible
= "nvidia,tegra124-vic", },
1307 { .compatible
= "nvidia,tegra132-dsi", },
1308 { .compatible
= "nvidia,tegra210-dc", },
1309 { .compatible
= "nvidia,tegra210-dsi", },
1310 { .compatible
= "nvidia,tegra210-sor", },
1311 { .compatible
= "nvidia,tegra210-sor1", },
1312 { .compatible
= "nvidia,tegra210-vic", },
1313 { .compatible
= "nvidia,tegra186-display", },
1314 { .compatible
= "nvidia,tegra186-dc", },
1315 { .compatible
= "nvidia,tegra186-sor", },
1316 { .compatible
= "nvidia,tegra186-sor1", },
1317 { .compatible
= "nvidia,tegra186-vic", },
1321 static struct host1x_driver host1x_drm_driver
= {
1324 .pm
= &host1x_drm_pm_ops
,
1326 .probe
= host1x_drm_probe
,
1327 .remove
= host1x_drm_remove
,
1328 .subdevs
= host1x_drm_subdevs
,
1331 static struct platform_driver
* const drivers
[] = {
1332 &tegra_display_hub_driver
,
1336 &tegra_dpaux_driver
,
1343 static int __init
host1x_drm_init(void)
1347 err
= host1x_driver_register(&host1x_drm_driver
);
1351 err
= platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
1353 goto unregister_host1x
;
1358 host1x_driver_unregister(&host1x_drm_driver
);
1361 module_init(host1x_drm_init
);
1363 static void __exit
host1x_drm_exit(void)
1365 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
1366 host1x_driver_unregister(&host1x_drm_driver
);
1368 module_exit(host1x_drm_exit
);
1370 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1371 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1372 MODULE_LICENSE("GPL v2");