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_mode_config_cleanup(drm
);
255 err
= host1x_device_exit(device
);
260 iommu_domain_free(tegra
->domain
);
261 drm_mm_takedown(&tegra
->mm
);
262 mutex_destroy(&tegra
->mm_lock
);
263 put_iova_domain(&tegra
->carveout
.domain
);
269 static int tegra_drm_open(struct drm_device
*drm
, struct drm_file
*filp
)
271 struct tegra_drm_file
*fpriv
;
273 fpriv
= kzalloc(sizeof(*fpriv
), GFP_KERNEL
);
277 idr_init(&fpriv
->contexts
);
278 mutex_init(&fpriv
->lock
);
279 filp
->driver_priv
= fpriv
;
284 static void tegra_drm_context_free(struct tegra_drm_context
*context
)
286 context
->client
->ops
->close_channel(context
);
290 static struct host1x_bo
*
291 host1x_bo_lookup(struct drm_file
*file
, u32 handle
)
293 struct drm_gem_object
*gem
;
296 gem
= drm_gem_object_lookup(file
, handle
);
300 bo
= to_tegra_bo(gem
);
304 static int host1x_reloc_copy_from_user(struct host1x_reloc
*dest
,
305 struct drm_tegra_reloc __user
*src
,
306 struct drm_device
*drm
,
307 struct drm_file
*file
)
312 err
= get_user(cmdbuf
, &src
->cmdbuf
.handle
);
316 err
= get_user(dest
->cmdbuf
.offset
, &src
->cmdbuf
.offset
);
320 err
= get_user(target
, &src
->target
.handle
);
324 err
= get_user(dest
->target
.offset
, &src
->target
.offset
);
328 err
= get_user(dest
->shift
, &src
->shift
);
332 dest
->cmdbuf
.bo
= host1x_bo_lookup(file
, cmdbuf
);
333 if (!dest
->cmdbuf
.bo
)
336 dest
->target
.bo
= host1x_bo_lookup(file
, target
);
337 if (!dest
->target
.bo
)
343 static int host1x_waitchk_copy_from_user(struct host1x_waitchk
*dest
,
344 struct drm_tegra_waitchk __user
*src
,
345 struct drm_file
*file
)
350 err
= get_user(cmdbuf
, &src
->handle
);
354 err
= get_user(dest
->offset
, &src
->offset
);
358 err
= get_user(dest
->syncpt_id
, &src
->syncpt
);
362 err
= get_user(dest
->thresh
, &src
->thresh
);
366 dest
->bo
= host1x_bo_lookup(file
, cmdbuf
);
373 int tegra_drm_submit(struct tegra_drm_context
*context
,
374 struct drm_tegra_submit
*args
, struct drm_device
*drm
,
375 struct drm_file
*file
)
377 unsigned int num_cmdbufs
= args
->num_cmdbufs
;
378 unsigned int num_relocs
= args
->num_relocs
;
379 unsigned int num_waitchks
= args
->num_waitchks
;
380 struct drm_tegra_cmdbuf __user
*user_cmdbufs
;
381 struct drm_tegra_reloc __user
*user_relocs
;
382 struct drm_tegra_waitchk __user
*user_waitchks
;
383 struct drm_tegra_syncpt __user
*user_syncpt
;
384 struct drm_tegra_syncpt syncpt
;
385 struct host1x
*host1x
= dev_get_drvdata(drm
->dev
->parent
);
386 struct drm_gem_object
**refs
;
387 struct host1x_syncpt
*sp
;
388 struct host1x_job
*job
;
389 unsigned int num_refs
;
392 user_cmdbufs
= u64_to_user_ptr(args
->cmdbufs
);
393 user_relocs
= u64_to_user_ptr(args
->relocs
);
394 user_waitchks
= u64_to_user_ptr(args
->waitchks
);
395 user_syncpt
= u64_to_user_ptr(args
->syncpts
);
397 /* We don't yet support other than one syncpt_incr struct per submit */
398 if (args
->num_syncpts
!= 1)
401 /* We don't yet support waitchks */
402 if (args
->num_waitchks
!= 0)
405 job
= host1x_job_alloc(context
->channel
, args
->num_cmdbufs
,
406 args
->num_relocs
, args
->num_waitchks
);
410 job
->num_relocs
= args
->num_relocs
;
411 job
->num_waitchk
= args
->num_waitchks
;
412 job
->client
= (u32
)args
->context
;
413 job
->class = context
->client
->base
.class;
414 job
->serialize
= true;
417 * Track referenced BOs so that they can be unreferenced after the
418 * submission is complete.
420 num_refs
= num_cmdbufs
+ num_relocs
* 2 + num_waitchks
;
422 refs
= kmalloc_array(num_refs
, sizeof(*refs
), GFP_KERNEL
);
428 /* reuse as an iterator later */
431 while (num_cmdbufs
) {
432 struct drm_tegra_cmdbuf cmdbuf
;
433 struct host1x_bo
*bo
;
434 struct tegra_bo
*obj
;
437 if (copy_from_user(&cmdbuf
, user_cmdbufs
, sizeof(cmdbuf
))) {
443 * The maximum number of CDMA gather fetches is 16383, a higher
444 * value means the words count is malformed.
446 if (cmdbuf
.words
> CDMA_GATHER_FETCHES_MAX_NB
) {
451 bo
= host1x_bo_lookup(file
, cmdbuf
.handle
);
457 offset
= (u64
)cmdbuf
.offset
+ (u64
)cmdbuf
.words
* sizeof(u32
);
458 obj
= host1x_to_tegra_bo(bo
);
459 refs
[num_refs
++] = &obj
->gem
;
462 * Gather buffer base address must be 4-bytes aligned,
463 * unaligned offset is malformed and cause commands stream
464 * corruption on the buffer address relocation.
466 if (offset
& 3 || offset
>= obj
->gem
.size
) {
471 host1x_job_add_gather(job
, bo
, cmdbuf
.words
, cmdbuf
.offset
);
476 /* copy and resolve relocations from submit */
477 while (num_relocs
--) {
478 struct host1x_reloc
*reloc
;
479 struct tegra_bo
*obj
;
481 err
= host1x_reloc_copy_from_user(&job
->relocarray
[num_relocs
],
482 &user_relocs
[num_relocs
], drm
,
487 reloc
= &job
->relocarray
[num_relocs
];
488 obj
= host1x_to_tegra_bo(reloc
->cmdbuf
.bo
);
489 refs
[num_refs
++] = &obj
->gem
;
492 * The unaligned cmdbuf offset will cause an unaligned write
493 * during of the relocations patching, corrupting the commands
496 if (reloc
->cmdbuf
.offset
& 3 ||
497 reloc
->cmdbuf
.offset
>= obj
->gem
.size
) {
502 obj
= host1x_to_tegra_bo(reloc
->target
.bo
);
503 refs
[num_refs
++] = &obj
->gem
;
505 if (reloc
->target
.offset
>= obj
->gem
.size
) {
511 /* copy and resolve waitchks from submit */
512 while (num_waitchks
--) {
513 struct host1x_waitchk
*wait
= &job
->waitchk
[num_waitchks
];
514 struct tegra_bo
*obj
;
516 err
= host1x_waitchk_copy_from_user(
517 wait
, &user_waitchks
[num_waitchks
], file
);
521 obj
= host1x_to_tegra_bo(wait
->bo
);
522 refs
[num_refs
++] = &obj
->gem
;
525 * The unaligned offset will cause an unaligned write during
526 * of the waitchks patching, corrupting the commands stream.
528 if (wait
->offset
& 3 ||
529 wait
->offset
>= obj
->gem
.size
) {
535 if (copy_from_user(&syncpt
, user_syncpt
, sizeof(syncpt
))) {
540 /* check whether syncpoint ID is valid */
541 sp
= host1x_syncpt_get(host1x
, syncpt
.id
);
547 job
->is_addr_reg
= context
->client
->ops
->is_addr_reg
;
548 job
->is_valid_class
= context
->client
->ops
->is_valid_class
;
549 job
->syncpt_incrs
= syncpt
.incrs
;
550 job
->syncpt_id
= syncpt
.id
;
551 job
->timeout
= 10000;
553 if (args
->timeout
&& args
->timeout
< 10000)
554 job
->timeout
= args
->timeout
;
556 err
= host1x_job_pin(job
, context
->client
->base
.dev
);
560 err
= host1x_job_submit(job
);
562 host1x_job_unpin(job
);
566 args
->fence
= job
->syncpt_end
;
570 drm_gem_object_put_unlocked(refs
[num_refs
]);
580 #ifdef CONFIG_DRM_TEGRA_STAGING
581 static int tegra_gem_create(struct drm_device
*drm
, void *data
,
582 struct drm_file
*file
)
584 struct drm_tegra_gem_create
*args
= data
;
587 bo
= tegra_bo_create_with_handle(file
, drm
, args
->size
, args
->flags
,
595 static int tegra_gem_mmap(struct drm_device
*drm
, void *data
,
596 struct drm_file
*file
)
598 struct drm_tegra_gem_mmap
*args
= data
;
599 struct drm_gem_object
*gem
;
602 gem
= drm_gem_object_lookup(file
, args
->handle
);
606 bo
= to_tegra_bo(gem
);
608 args
->offset
= drm_vma_node_offset_addr(&bo
->gem
.vma_node
);
610 drm_gem_object_put_unlocked(gem
);
615 static int tegra_syncpt_read(struct drm_device
*drm
, void *data
,
616 struct drm_file
*file
)
618 struct host1x
*host
= dev_get_drvdata(drm
->dev
->parent
);
619 struct drm_tegra_syncpt_read
*args
= data
;
620 struct host1x_syncpt
*sp
;
622 sp
= host1x_syncpt_get(host
, args
->id
);
626 args
->value
= host1x_syncpt_read_min(sp
);
630 static int tegra_syncpt_incr(struct drm_device
*drm
, void *data
,
631 struct drm_file
*file
)
633 struct host1x
*host1x
= dev_get_drvdata(drm
->dev
->parent
);
634 struct drm_tegra_syncpt_incr
*args
= data
;
635 struct host1x_syncpt
*sp
;
637 sp
= host1x_syncpt_get(host1x
, args
->id
);
641 return host1x_syncpt_incr(sp
);
644 static int tegra_syncpt_wait(struct drm_device
*drm
, void *data
,
645 struct drm_file
*file
)
647 struct host1x
*host1x
= dev_get_drvdata(drm
->dev
->parent
);
648 struct drm_tegra_syncpt_wait
*args
= data
;
649 struct host1x_syncpt
*sp
;
651 sp
= host1x_syncpt_get(host1x
, args
->id
);
655 return host1x_syncpt_wait(sp
, args
->thresh
,
656 msecs_to_jiffies(args
->timeout
),
660 static int tegra_client_open(struct tegra_drm_file
*fpriv
,
661 struct tegra_drm_client
*client
,
662 struct tegra_drm_context
*context
)
666 err
= client
->ops
->open_channel(client
, context
);
670 err
= idr_alloc(&fpriv
->contexts
, context
, 1, 0, GFP_KERNEL
);
672 client
->ops
->close_channel(context
);
676 context
->client
= client
;
682 static int tegra_open_channel(struct drm_device
*drm
, void *data
,
683 struct drm_file
*file
)
685 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
686 struct tegra_drm
*tegra
= drm
->dev_private
;
687 struct drm_tegra_open_channel
*args
= data
;
688 struct tegra_drm_context
*context
;
689 struct tegra_drm_client
*client
;
692 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
696 mutex_lock(&fpriv
->lock
);
698 list_for_each_entry(client
, &tegra
->clients
, list
)
699 if (client
->base
.class == args
->client
) {
700 err
= tegra_client_open(fpriv
, client
, context
);
704 args
->context
= context
->id
;
711 mutex_unlock(&fpriv
->lock
);
715 static int tegra_close_channel(struct drm_device
*drm
, void *data
,
716 struct drm_file
*file
)
718 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
719 struct drm_tegra_close_channel
*args
= data
;
720 struct tegra_drm_context
*context
;
723 mutex_lock(&fpriv
->lock
);
725 context
= idr_find(&fpriv
->contexts
, args
->context
);
731 idr_remove(&fpriv
->contexts
, context
->id
);
732 tegra_drm_context_free(context
);
735 mutex_unlock(&fpriv
->lock
);
739 static int tegra_get_syncpt(struct drm_device
*drm
, void *data
,
740 struct drm_file
*file
)
742 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
743 struct drm_tegra_get_syncpt
*args
= data
;
744 struct tegra_drm_context
*context
;
745 struct host1x_syncpt
*syncpt
;
748 mutex_lock(&fpriv
->lock
);
750 context
= idr_find(&fpriv
->contexts
, args
->context
);
756 if (args
->index
>= context
->client
->base
.num_syncpts
) {
761 syncpt
= context
->client
->base
.syncpts
[args
->index
];
762 args
->id
= host1x_syncpt_id(syncpt
);
765 mutex_unlock(&fpriv
->lock
);
769 static int tegra_submit(struct drm_device
*drm
, void *data
,
770 struct drm_file
*file
)
772 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
773 struct drm_tegra_submit
*args
= data
;
774 struct tegra_drm_context
*context
;
777 mutex_lock(&fpriv
->lock
);
779 context
= idr_find(&fpriv
->contexts
, args
->context
);
785 err
= context
->client
->ops
->submit(context
, args
, drm
, file
);
788 mutex_unlock(&fpriv
->lock
);
792 static int tegra_get_syncpt_base(struct drm_device
*drm
, void *data
,
793 struct drm_file
*file
)
795 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
796 struct drm_tegra_get_syncpt_base
*args
= data
;
797 struct tegra_drm_context
*context
;
798 struct host1x_syncpt_base
*base
;
799 struct host1x_syncpt
*syncpt
;
802 mutex_lock(&fpriv
->lock
);
804 context
= idr_find(&fpriv
->contexts
, args
->context
);
810 if (args
->syncpt
>= context
->client
->base
.num_syncpts
) {
815 syncpt
= context
->client
->base
.syncpts
[args
->syncpt
];
817 base
= host1x_syncpt_get_base(syncpt
);
823 args
->id
= host1x_syncpt_base_id(base
);
826 mutex_unlock(&fpriv
->lock
);
830 static int tegra_gem_set_tiling(struct drm_device
*drm
, void *data
,
831 struct drm_file
*file
)
833 struct drm_tegra_gem_set_tiling
*args
= data
;
834 enum tegra_bo_tiling_mode mode
;
835 struct drm_gem_object
*gem
;
836 unsigned long value
= 0;
839 switch (args
->mode
) {
840 case DRM_TEGRA_GEM_TILING_MODE_PITCH
:
841 mode
= TEGRA_BO_TILING_MODE_PITCH
;
843 if (args
->value
!= 0)
848 case DRM_TEGRA_GEM_TILING_MODE_TILED
:
849 mode
= TEGRA_BO_TILING_MODE_TILED
;
851 if (args
->value
!= 0)
856 case DRM_TEGRA_GEM_TILING_MODE_BLOCK
:
857 mode
= TEGRA_BO_TILING_MODE_BLOCK
;
869 gem
= drm_gem_object_lookup(file
, args
->handle
);
873 bo
= to_tegra_bo(gem
);
875 bo
->tiling
.mode
= mode
;
876 bo
->tiling
.value
= value
;
878 drm_gem_object_put_unlocked(gem
);
883 static int tegra_gem_get_tiling(struct drm_device
*drm
, void *data
,
884 struct drm_file
*file
)
886 struct drm_tegra_gem_get_tiling
*args
= data
;
887 struct drm_gem_object
*gem
;
891 gem
= drm_gem_object_lookup(file
, args
->handle
);
895 bo
= to_tegra_bo(gem
);
897 switch (bo
->tiling
.mode
) {
898 case TEGRA_BO_TILING_MODE_PITCH
:
899 args
->mode
= DRM_TEGRA_GEM_TILING_MODE_PITCH
;
903 case TEGRA_BO_TILING_MODE_TILED
:
904 args
->mode
= DRM_TEGRA_GEM_TILING_MODE_TILED
;
908 case TEGRA_BO_TILING_MODE_BLOCK
:
909 args
->mode
= DRM_TEGRA_GEM_TILING_MODE_BLOCK
;
910 args
->value
= bo
->tiling
.value
;
918 drm_gem_object_put_unlocked(gem
);
923 static int tegra_gem_set_flags(struct drm_device
*drm
, void *data
,
924 struct drm_file
*file
)
926 struct drm_tegra_gem_set_flags
*args
= data
;
927 struct drm_gem_object
*gem
;
930 if (args
->flags
& ~DRM_TEGRA_GEM_FLAGS
)
933 gem
= drm_gem_object_lookup(file
, args
->handle
);
937 bo
= to_tegra_bo(gem
);
940 if (args
->flags
& DRM_TEGRA_GEM_BOTTOM_UP
)
941 bo
->flags
|= TEGRA_BO_BOTTOM_UP
;
943 drm_gem_object_put_unlocked(gem
);
948 static int tegra_gem_get_flags(struct drm_device
*drm
, void *data
,
949 struct drm_file
*file
)
951 struct drm_tegra_gem_get_flags
*args
= data
;
952 struct drm_gem_object
*gem
;
955 gem
= drm_gem_object_lookup(file
, args
->handle
);
959 bo
= to_tegra_bo(gem
);
962 if (bo
->flags
& TEGRA_BO_BOTTOM_UP
)
963 args
->flags
|= DRM_TEGRA_GEM_BOTTOM_UP
;
965 drm_gem_object_put_unlocked(gem
);
971 static const struct drm_ioctl_desc tegra_drm_ioctls
[] = {
972 #ifdef CONFIG_DRM_TEGRA_STAGING
973 DRM_IOCTL_DEF_DRV(TEGRA_GEM_CREATE
, tegra_gem_create
,
974 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
975 DRM_IOCTL_DEF_DRV(TEGRA_GEM_MMAP
, tegra_gem_mmap
,
976 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
977 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_READ
, tegra_syncpt_read
,
978 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
979 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_INCR
, tegra_syncpt_incr
,
980 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
981 DRM_IOCTL_DEF_DRV(TEGRA_SYNCPT_WAIT
, tegra_syncpt_wait
,
982 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
983 DRM_IOCTL_DEF_DRV(TEGRA_OPEN_CHANNEL
, tegra_open_channel
,
984 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
985 DRM_IOCTL_DEF_DRV(TEGRA_CLOSE_CHANNEL
, tegra_close_channel
,
986 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
987 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT
, tegra_get_syncpt
,
988 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
989 DRM_IOCTL_DEF_DRV(TEGRA_SUBMIT
, tegra_submit
,
990 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
991 DRM_IOCTL_DEF_DRV(TEGRA_GET_SYNCPT_BASE
, tegra_get_syncpt_base
,
992 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
993 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_TILING
, tegra_gem_set_tiling
,
994 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
995 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_TILING
, tegra_gem_get_tiling
,
996 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
997 DRM_IOCTL_DEF_DRV(TEGRA_GEM_SET_FLAGS
, tegra_gem_set_flags
,
998 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
999 DRM_IOCTL_DEF_DRV(TEGRA_GEM_GET_FLAGS
, tegra_gem_get_flags
,
1000 DRM_UNLOCKED
| DRM_RENDER_ALLOW
),
1004 static const struct file_operations tegra_drm_fops
= {
1005 .owner
= THIS_MODULE
,
1007 .release
= drm_release
,
1008 .unlocked_ioctl
= drm_ioctl
,
1009 .mmap
= tegra_drm_mmap
,
1012 .compat_ioctl
= drm_compat_ioctl
,
1013 .llseek
= noop_llseek
,
1016 static int tegra_drm_context_cleanup(int id
, void *p
, void *data
)
1018 struct tegra_drm_context
*context
= p
;
1020 tegra_drm_context_free(context
);
1025 static void tegra_drm_postclose(struct drm_device
*drm
, struct drm_file
*file
)
1027 struct tegra_drm_file
*fpriv
= file
->driver_priv
;
1029 mutex_lock(&fpriv
->lock
);
1030 idr_for_each(&fpriv
->contexts
, tegra_drm_context_cleanup
, NULL
);
1031 mutex_unlock(&fpriv
->lock
);
1033 idr_destroy(&fpriv
->contexts
);
1034 mutex_destroy(&fpriv
->lock
);
1038 #ifdef CONFIG_DEBUG_FS
1039 static int tegra_debugfs_framebuffers(struct seq_file
*s
, void *data
)
1041 struct drm_info_node
*node
= (struct drm_info_node
*)s
->private;
1042 struct drm_device
*drm
= node
->minor
->dev
;
1043 struct drm_framebuffer
*fb
;
1045 mutex_lock(&drm
->mode_config
.fb_lock
);
1047 list_for_each_entry(fb
, &drm
->mode_config
.fb_list
, head
) {
1048 seq_printf(s
, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
1049 fb
->base
.id
, fb
->width
, fb
->height
,
1051 fb
->format
->cpp
[0] * 8,
1052 drm_framebuffer_read_refcount(fb
));
1055 mutex_unlock(&drm
->mode_config
.fb_lock
);
1060 static int tegra_debugfs_iova(struct seq_file
*s
, void *data
)
1062 struct drm_info_node
*node
= (struct drm_info_node
*)s
->private;
1063 struct drm_device
*drm
= node
->minor
->dev
;
1064 struct tegra_drm
*tegra
= drm
->dev_private
;
1065 struct drm_printer p
= drm_seq_file_printer(s
);
1067 if (tegra
->domain
) {
1068 mutex_lock(&tegra
->mm_lock
);
1069 drm_mm_print(&tegra
->mm
, &p
);
1070 mutex_unlock(&tegra
->mm_lock
);
1076 static struct drm_info_list tegra_debugfs_list
[] = {
1077 { "framebuffers", tegra_debugfs_framebuffers
, 0 },
1078 { "iova", tegra_debugfs_iova
, 0 },
1081 static int tegra_debugfs_init(struct drm_minor
*minor
)
1083 return drm_debugfs_create_files(tegra_debugfs_list
,
1084 ARRAY_SIZE(tegra_debugfs_list
),
1085 minor
->debugfs_root
, minor
);
1089 static struct drm_driver tegra_drm_driver
= {
1090 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
1091 DRIVER_ATOMIC
| DRIVER_RENDER
,
1092 .load
= tegra_drm_load
,
1093 .unload
= tegra_drm_unload
,
1094 .open
= tegra_drm_open
,
1095 .postclose
= tegra_drm_postclose
,
1096 .lastclose
= drm_fb_helper_lastclose
,
1098 #if defined(CONFIG_DEBUG_FS)
1099 .debugfs_init
= tegra_debugfs_init
,
1102 .gem_free_object_unlocked
= tegra_bo_free_object
,
1103 .gem_vm_ops
= &tegra_bo_vm_ops
,
1105 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
1106 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
1107 .gem_prime_export
= tegra_gem_prime_export
,
1108 .gem_prime_import
= tegra_gem_prime_import
,
1110 .dumb_create
= tegra_bo_dumb_create
,
1112 .ioctls
= tegra_drm_ioctls
,
1113 .num_ioctls
= ARRAY_SIZE(tegra_drm_ioctls
),
1114 .fops
= &tegra_drm_fops
,
1116 .name
= DRIVER_NAME
,
1117 .desc
= DRIVER_DESC
,
1118 .date
= DRIVER_DATE
,
1119 .major
= DRIVER_MAJOR
,
1120 .minor
= DRIVER_MINOR
,
1121 .patchlevel
= DRIVER_PATCHLEVEL
,
1124 int tegra_drm_register_client(struct tegra_drm
*tegra
,
1125 struct tegra_drm_client
*client
)
1127 mutex_lock(&tegra
->clients_lock
);
1128 list_add_tail(&client
->list
, &tegra
->clients
);
1129 mutex_unlock(&tegra
->clients_lock
);
1134 int tegra_drm_unregister_client(struct tegra_drm
*tegra
,
1135 struct tegra_drm_client
*client
)
1137 mutex_lock(&tegra
->clients_lock
);
1138 list_del_init(&client
->list
);
1139 mutex_unlock(&tegra
->clients_lock
);
1144 void *tegra_drm_alloc(struct tegra_drm
*tegra
, size_t size
, dma_addr_t
*dma
)
1152 size
= iova_align(&tegra
->carveout
.domain
, size
);
1154 size
= PAGE_ALIGN(size
);
1156 gfp
= GFP_KERNEL
| __GFP_ZERO
;
1157 if (!tegra
->domain
) {
1159 * Many units only support 32-bit addresses, even on 64-bit
1160 * SoCs. If there is no IOMMU to translate into a 32-bit IO
1161 * virtual address space, force allocations to be in the
1162 * lower 32-bit range.
1167 virt
= (void *)__get_free_pages(gfp
, get_order(size
));
1169 return ERR_PTR(-ENOMEM
);
1171 if (!tegra
->domain
) {
1173 * If IOMMU is disabled, devices address physical memory
1176 *dma
= virt_to_phys(virt
);
1180 alloc
= alloc_iova(&tegra
->carveout
.domain
,
1181 size
>> tegra
->carveout
.shift
,
1182 tegra
->carveout
.limit
, true);
1188 *dma
= iova_dma_addr(&tegra
->carveout
.domain
, alloc
);
1189 err
= iommu_map(tegra
->domain
, *dma
, virt_to_phys(virt
),
1190 size
, IOMMU_READ
| IOMMU_WRITE
);
1197 __free_iova(&tegra
->carveout
.domain
, alloc
);
1199 free_pages((unsigned long)virt
, get_order(size
));
1201 return ERR_PTR(err
);
1204 void tegra_drm_free(struct tegra_drm
*tegra
, size_t size
, void *virt
,
1208 size
= iova_align(&tegra
->carveout
.domain
, size
);
1210 size
= PAGE_ALIGN(size
);
1212 if (tegra
->domain
) {
1213 iommu_unmap(tegra
->domain
, dma
, size
);
1214 free_iova(&tegra
->carveout
.domain
,
1215 iova_pfn(&tegra
->carveout
.domain
, dma
));
1218 free_pages((unsigned long)virt
, get_order(size
));
1221 static int host1x_drm_probe(struct host1x_device
*dev
)
1223 struct drm_driver
*driver
= &tegra_drm_driver
;
1224 struct drm_device
*drm
;
1227 drm
= drm_dev_alloc(driver
, &dev
->dev
);
1229 return PTR_ERR(drm
);
1231 dev_set_drvdata(&dev
->dev
, drm
);
1233 err
= drm_dev_register(drm
, 0);
1244 static int host1x_drm_remove(struct host1x_device
*dev
)
1246 struct drm_device
*drm
= dev_get_drvdata(&dev
->dev
);
1248 drm_dev_unregister(drm
);
1254 #ifdef CONFIG_PM_SLEEP
1255 static int host1x_drm_suspend(struct device
*dev
)
1257 struct drm_device
*drm
= dev_get_drvdata(dev
);
1258 struct tegra_drm
*tegra
= drm
->dev_private
;
1260 drm_kms_helper_poll_disable(drm
);
1261 tegra_drm_fb_suspend(drm
);
1263 tegra
->state
= drm_atomic_helper_suspend(drm
);
1264 if (IS_ERR(tegra
->state
)) {
1265 tegra_drm_fb_resume(drm
);
1266 drm_kms_helper_poll_enable(drm
);
1267 return PTR_ERR(tegra
->state
);
1273 static int host1x_drm_resume(struct device
*dev
)
1275 struct drm_device
*drm
= dev_get_drvdata(dev
);
1276 struct tegra_drm
*tegra
= drm
->dev_private
;
1278 drm_atomic_helper_resume(drm
, tegra
->state
);
1279 tegra_drm_fb_resume(drm
);
1280 drm_kms_helper_poll_enable(drm
);
1286 static SIMPLE_DEV_PM_OPS(host1x_drm_pm_ops
, host1x_drm_suspend
,
1289 static const struct of_device_id host1x_drm_subdevs
[] = {
1290 { .compatible
= "nvidia,tegra20-dc", },
1291 { .compatible
= "nvidia,tegra20-hdmi", },
1292 { .compatible
= "nvidia,tegra20-gr2d", },
1293 { .compatible
= "nvidia,tegra20-gr3d", },
1294 { .compatible
= "nvidia,tegra30-dc", },
1295 { .compatible
= "nvidia,tegra30-hdmi", },
1296 { .compatible
= "nvidia,tegra30-gr2d", },
1297 { .compatible
= "nvidia,tegra30-gr3d", },
1298 { .compatible
= "nvidia,tegra114-dsi", },
1299 { .compatible
= "nvidia,tegra114-hdmi", },
1300 { .compatible
= "nvidia,tegra114-gr3d", },
1301 { .compatible
= "nvidia,tegra124-dc", },
1302 { .compatible
= "nvidia,tegra124-sor", },
1303 { .compatible
= "nvidia,tegra124-hdmi", },
1304 { .compatible
= "nvidia,tegra124-dsi", },
1305 { .compatible
= "nvidia,tegra124-vic", },
1306 { .compatible
= "nvidia,tegra132-dsi", },
1307 { .compatible
= "nvidia,tegra210-dc", },
1308 { .compatible
= "nvidia,tegra210-dsi", },
1309 { .compatible
= "nvidia,tegra210-sor", },
1310 { .compatible
= "nvidia,tegra210-sor1", },
1311 { .compatible
= "nvidia,tegra210-vic", },
1312 { .compatible
= "nvidia,tegra186-display", },
1313 { .compatible
= "nvidia,tegra186-dc", },
1314 { .compatible
= "nvidia,tegra186-sor", },
1315 { .compatible
= "nvidia,tegra186-sor1", },
1316 { .compatible
= "nvidia,tegra186-vic", },
1320 static struct host1x_driver host1x_drm_driver
= {
1323 .pm
= &host1x_drm_pm_ops
,
1325 .probe
= host1x_drm_probe
,
1326 .remove
= host1x_drm_remove
,
1327 .subdevs
= host1x_drm_subdevs
,
1330 static struct platform_driver
* const drivers
[] = {
1331 &tegra_display_hub_driver
,
1335 &tegra_dpaux_driver
,
1342 static int __init
host1x_drm_init(void)
1346 err
= host1x_driver_register(&host1x_drm_driver
);
1350 err
= platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
1352 goto unregister_host1x
;
1357 host1x_driver_unregister(&host1x_drm_driver
);
1360 module_init(host1x_drm_init
);
1362 static void __exit
host1x_drm_exit(void)
1364 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
1365 host1x_driver_unregister(&host1x_drm_driver
);
1367 module_exit(host1x_drm_exit
);
1369 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1370 MODULE_DESCRIPTION("NVIDIA Tegra DRM driver");
1371 MODULE_LICENSE("GPL v2");