1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat
6 #include <linux/dma-buf.h>
7 #include <linux/vmalloc.h>
9 #include <drm/drm_drv.h>
10 #include <drm/drm_gem_shmem_helper.h>
11 #include <drm/drm_mode.h>
12 #include <drm/drm_prime.h>
20 static int udl_gem_object_mmap(struct drm_gem_object
*obj
,
21 struct vm_area_struct
*vma
)
25 ret
= drm_gem_shmem_mmap(obj
, vma
);
29 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
30 if (obj
->import_attach
)
31 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
32 vma
->vm_page_prot
= pgprot_decrypted(vma
->vm_page_prot
);
37 static void *udl_gem_object_vmap(struct drm_gem_object
*obj
)
39 struct drm_gem_shmem_object
*shmem
= to_drm_gem_shmem_obj(obj
);
42 ret
= mutex_lock_interruptible(&shmem
->vmap_lock
);
46 if (shmem
->vmap_use_count
++ > 0)
49 ret
= drm_gem_shmem_get_pages(shmem
);
53 if (obj
->import_attach
)
54 shmem
->vaddr
= dma_buf_vmap(obj
->import_attach
->dmabuf
);
56 shmem
->vaddr
= vmap(shmem
->pages
, obj
->size
>> PAGE_SHIFT
,
60 DRM_DEBUG_KMS("Failed to vmap pages\n");
66 mutex_unlock(&shmem
->vmap_lock
);
70 drm_gem_shmem_put_pages(shmem
);
72 shmem
->vmap_use_count
= 0;
73 mutex_unlock(&shmem
->vmap_lock
);
77 static const struct drm_gem_object_funcs udl_gem_object_funcs
= {
78 .free
= drm_gem_shmem_free_object
,
79 .print_info
= drm_gem_shmem_print_info
,
80 .pin
= drm_gem_shmem_pin
,
81 .unpin
= drm_gem_shmem_unpin
,
82 .get_sg_table
= drm_gem_shmem_get_sg_table
,
83 .vmap
= udl_gem_object_vmap
,
84 .vunmap
= drm_gem_shmem_vunmap
,
85 .mmap
= udl_gem_object_mmap
,
89 * Helpers for struct drm_driver
92 struct drm_gem_object
*udl_driver_gem_create_object(struct drm_device
*dev
,
95 struct drm_gem_shmem_object
*shmem
;
96 struct drm_gem_object
*obj
;
98 shmem
= kzalloc(sizeof(*shmem
), GFP_KERNEL
);
103 obj
->funcs
= &udl_gem_object_funcs
;