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) {
32 udl_gem_create(struct drm_file
*file
,
33 struct drm_device
*dev
,
37 struct udl_gem_object
*obj
;
41 size
= roundup(size
, PAGE_SIZE
);
43 obj
= udl_gem_alloc_object(dev
, size
);
47 ret
= drm_gem_handle_create(file
, &obj
->base
, &handle
);
49 drm_gem_object_release(&obj
->base
);
54 drm_gem_object_unreference(&obj
->base
);
59 int udl_dumb_create(struct drm_file
*file
,
60 struct drm_device
*dev
,
61 struct drm_mode_create_dumb
*args
)
63 args
->pitch
= args
->width
* DIV_ROUND_UP(args
->bpp
, 8);
64 args
->size
= args
->pitch
* args
->height
;
65 return udl_gem_create(file
, dev
,
66 args
->size
, &args
->handle
);
69 int udl_drm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
73 ret
= drm_gem_mmap(filp
, vma
);
77 vma
->vm_flags
&= ~VM_PFNMAP
;
78 vma
->vm_flags
|= VM_MIXEDMAP
;
83 int udl_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
85 struct udl_gem_object
*obj
= to_udl_bo(vma
->vm_private_data
);
87 unsigned int page_offset
;
90 page_offset
= ((unsigned long)vmf
->virtual_address
- vma
->vm_start
) >>
94 return VM_FAULT_SIGBUS
;
96 page
= obj
->pages
[page_offset
];
97 ret
= vm_insert_page(vma
, (unsigned long)vmf
->virtual_address
, page
);
102 return VM_FAULT_NOPAGE
;
106 return VM_FAULT_SIGBUS
;
110 static int udl_gem_get_pages(struct udl_gem_object
*obj
, gfp_t gfpmask
)
117 pages
= drm_gem_get_pages(&obj
->base
, gfpmask
);
119 return PTR_ERR(pages
);
126 static void udl_gem_put_pages(struct udl_gem_object
*obj
)
128 if (obj
->base
.import_attach
) {
129 drm_free_large(obj
->pages
);
134 drm_gem_put_pages(&obj
->base
, obj
->pages
, false, false);
138 int udl_gem_vmap(struct udl_gem_object
*obj
)
140 int page_count
= obj
->base
.size
/ PAGE_SIZE
;
143 if (obj
->base
.import_attach
) {
144 obj
->vmapping
= dma_buf_vmap(obj
->base
.import_attach
->dmabuf
);
150 ret
= udl_gem_get_pages(obj
, GFP_KERNEL
);
154 obj
->vmapping
= vmap(obj
->pages
, page_count
, 0, PAGE_KERNEL
);
160 void udl_gem_vunmap(struct udl_gem_object
*obj
)
162 if (obj
->base
.import_attach
) {
163 dma_buf_vunmap(obj
->base
.import_attach
->dmabuf
, obj
->vmapping
);
168 vunmap(obj
->vmapping
);
170 udl_gem_put_pages(obj
);
173 void udl_gem_free_object(struct drm_gem_object
*gem_obj
)
175 struct udl_gem_object
*obj
= to_udl_bo(gem_obj
);
180 if (gem_obj
->import_attach
) {
181 drm_prime_gem_destroy(gem_obj
, obj
->sg
);
182 put_device(gem_obj
->dev
->dev
);
186 udl_gem_put_pages(obj
);
188 drm_gem_free_mmap_offset(gem_obj
);
191 /* the dumb interface doesn't work with the GEM straight MMAP
192 interface, it expects to do MMAP on the drm fd, like normal */
193 int udl_gem_mmap(struct drm_file
*file
, struct drm_device
*dev
,
194 uint32_t handle
, uint64_t *offset
)
196 struct udl_gem_object
*gobj
;
197 struct drm_gem_object
*obj
;
200 mutex_lock(&dev
->struct_mutex
);
201 obj
= drm_gem_object_lookup(dev
, file
, handle
);
206 gobj
= to_udl_bo(obj
);
208 ret
= udl_gem_get_pages(gobj
, GFP_KERNEL
);
211 ret
= drm_gem_create_mmap_offset(obj
);
215 *offset
= drm_vma_node_offset_addr(&gobj
->base
.vma_node
);
218 drm_gem_object_unreference(&gobj
->base
);
220 mutex_unlock(&dev
->struct_mutex
);
224 static int udl_prime_create(struct drm_device
*dev
,
227 struct udl_gem_object
**obj_p
)
229 struct udl_gem_object
*obj
;
232 npages
= size
/ PAGE_SIZE
;
235 obj
= udl_gem_alloc_object(dev
, npages
* PAGE_SIZE
);
240 obj
->pages
= drm_malloc_ab(npages
, sizeof(struct page
*));
241 if (obj
->pages
== NULL
) {
242 DRM_ERROR("obj pages is NULL %d\n", npages
);
246 drm_prime_sg_to_page_addr_arrays(sg
, obj
->pages
, NULL
, npages
);
252 struct drm_gem_object
*udl_gem_prime_import(struct drm_device
*dev
,
253 struct dma_buf
*dma_buf
)
255 struct dma_buf_attachment
*attach
;
257 struct udl_gem_object
*uobj
;
261 get_device(dev
->dev
);
262 attach
= dma_buf_attach(dma_buf
, dev
->dev
);
263 if (IS_ERR(attach
)) {
264 put_device(dev
->dev
);
265 return ERR_CAST(attach
);
268 get_dma_buf(dma_buf
);
270 sg
= dma_buf_map_attachment(attach
, DMA_BIDIRECTIONAL
);
276 ret
= udl_prime_create(dev
, dma_buf
->size
, sg
, &uobj
);
281 uobj
->base
.import_attach
= attach
;
286 dma_buf_unmap_attachment(attach
, sg
, DMA_BIDIRECTIONAL
);
288 dma_buf_detach(dma_buf
, attach
);
289 dma_buf_put(dma_buf
);
290 put_device(dev
->dev
);