2 * Copyright (C) 2012 Red Hat
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License v2. See the file COPYING in the main directory of this archive for
11 #include <linux/shmem_fs.h>
12 #include <linux/dma-buf.h>
14 struct udl_gem_object
*udl_gem_alloc_object(struct drm_device
*dev
,
17 struct udl_gem_object
*obj
;
19 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
23 if (drm_gem_object_init(dev
, &obj
->base
, size
) != 0) {
28 obj
->flags
= UDL_BO_CACHEABLE
;
33 udl_gem_create(struct drm_file
*file
,
34 struct drm_device
*dev
,
38 struct udl_gem_object
*obj
;
42 size
= roundup(size
, PAGE_SIZE
);
44 obj
= udl_gem_alloc_object(dev
, size
);
48 ret
= drm_gem_handle_create(file
, &obj
->base
, &handle
);
50 drm_gem_object_release(&obj
->base
);
55 drm_gem_object_unreference(&obj
->base
);
60 static void update_vm_cache_attr(struct udl_gem_object
*obj
,
61 struct vm_area_struct
*vma
)
63 DRM_DEBUG_KMS("flags = 0x%x\n", obj
->flags
);
65 /* non-cacheable as default. */
66 if (obj
->flags
& UDL_BO_CACHEABLE
) {
67 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
68 } else if (obj
->flags
& UDL_BO_WC
) {
70 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
73 pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
77 int udl_dumb_create(struct drm_file
*file
,
78 struct drm_device
*dev
,
79 struct drm_mode_create_dumb
*args
)
81 args
->pitch
= args
->width
* DIV_ROUND_UP(args
->bpp
, 8);
82 args
->size
= args
->pitch
* args
->height
;
83 return udl_gem_create(file
, dev
,
84 args
->size
, &args
->handle
);
87 int udl_drm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
91 ret
= drm_gem_mmap(filp
, vma
);
95 vma
->vm_flags
&= ~VM_PFNMAP
;
96 vma
->vm_flags
|= VM_MIXEDMAP
;
98 update_vm_cache_attr(to_udl_bo(vma
->vm_private_data
), vma
);
103 int udl_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
105 struct udl_gem_object
*obj
= to_udl_bo(vma
->vm_private_data
);
107 unsigned int page_offset
;
110 page_offset
= ((unsigned long)vmf
->virtual_address
- vma
->vm_start
) >>
114 return VM_FAULT_SIGBUS
;
116 page
= obj
->pages
[page_offset
];
117 ret
= vm_insert_page(vma
, (unsigned long)vmf
->virtual_address
, page
);
122 return VM_FAULT_NOPAGE
;
126 return VM_FAULT_SIGBUS
;
130 int udl_gem_get_pages(struct udl_gem_object
*obj
)
137 pages
= drm_gem_get_pages(&obj
->base
);
139 return PTR_ERR(pages
);
146 void udl_gem_put_pages(struct udl_gem_object
*obj
)
148 if (obj
->base
.import_attach
) {
149 drm_free_large(obj
->pages
);
154 drm_gem_put_pages(&obj
->base
, obj
->pages
, false, false);
158 int udl_gem_vmap(struct udl_gem_object
*obj
)
160 int page_count
= obj
->base
.size
/ PAGE_SIZE
;
163 if (obj
->base
.import_attach
) {
164 obj
->vmapping
= dma_buf_vmap(obj
->base
.import_attach
->dmabuf
);
170 ret
= udl_gem_get_pages(obj
);
174 obj
->vmapping
= vmap(obj
->pages
, page_count
, 0, PAGE_KERNEL
);
180 void udl_gem_vunmap(struct udl_gem_object
*obj
)
182 if (obj
->base
.import_attach
) {
183 dma_buf_vunmap(obj
->base
.import_attach
->dmabuf
, obj
->vmapping
);
187 vunmap(obj
->vmapping
);
189 udl_gem_put_pages(obj
);
192 void udl_gem_free_object(struct drm_gem_object
*gem_obj
)
194 struct udl_gem_object
*obj
= to_udl_bo(gem_obj
);
199 if (gem_obj
->import_attach
) {
200 drm_prime_gem_destroy(gem_obj
, obj
->sg
);
201 put_device(gem_obj
->dev
->dev
);
205 udl_gem_put_pages(obj
);
207 drm_gem_free_mmap_offset(gem_obj
);
210 /* the dumb interface doesn't work with the GEM straight MMAP
211 interface, it expects to do MMAP on the drm fd, like normal */
212 int udl_gem_mmap(struct drm_file
*file
, struct drm_device
*dev
,
213 uint32_t handle
, uint64_t *offset
)
215 struct udl_gem_object
*gobj
;
216 struct drm_gem_object
*obj
;
219 mutex_lock(&dev
->struct_mutex
);
220 obj
= drm_gem_object_lookup(dev
, file
, handle
);
225 gobj
= to_udl_bo(obj
);
227 ret
= udl_gem_get_pages(gobj
);
230 ret
= drm_gem_create_mmap_offset(obj
);
234 *offset
= drm_vma_node_offset_addr(&gobj
->base
.vma_node
);
237 drm_gem_object_unreference(&gobj
->base
);
239 mutex_unlock(&dev
->struct_mutex
);