2 * drm gem CMA (contiguous memory allocator) helper functions
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
6 * Based on Samsung Exynos code
8 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/export.h>
24 #include <linux/dma-buf.h>
25 #include <linux/dma-mapping.h>
29 #include <drm/drm_gem_cma_helper.h>
30 #include <drm/drm_vma_manager.h>
33 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
34 * @drm: The drm device
35 * @size: The GEM object size
37 * This function creates and initializes a GEM CMA object of the given size, but
38 * doesn't allocate any memory to back the object.
40 * Return a struct drm_gem_cma_object* on success or ERR_PTR values on failure.
42 static struct drm_gem_cma_object
*
43 __drm_gem_cma_create(struct drm_device
*drm
, unsigned int size
)
45 struct drm_gem_cma_object
*cma_obj
;
46 struct drm_gem_object
*gem_obj
;
49 cma_obj
= kzalloc(sizeof(*cma_obj
), GFP_KERNEL
);
51 return ERR_PTR(-ENOMEM
);
53 gem_obj
= &cma_obj
->base
;
55 ret
= drm_gem_object_init(drm
, gem_obj
, size
);
59 ret
= drm_gem_create_mmap_offset(gem_obj
);
61 drm_gem_object_release(gem_obj
);
73 * drm_gem_cma_create - allocate an object with the given size
75 * returns a struct drm_gem_cma_object* on success or ERR_PTR values
78 struct drm_gem_cma_object
*drm_gem_cma_create(struct drm_device
*drm
,
81 struct drm_gem_cma_object
*cma_obj
;
82 struct sg_table
*sgt
= NULL
;
85 size
= round_up(size
, PAGE_SIZE
);
87 cma_obj
= __drm_gem_cma_create(drm
, size
);
91 cma_obj
->vaddr
= dma_alloc_writecombine(drm
->dev
, size
,
92 &cma_obj
->paddr
, GFP_KERNEL
| __GFP_NOWARN
);
93 if (!cma_obj
->vaddr
) {
94 dev_err(drm
->dev
, "failed to allocate buffer with size %d\n",
100 sgt
= kzalloc(sizeof(*cma_obj
->sgt
), GFP_KERNEL
);
106 ret
= dma_get_sgtable(drm
->dev
, sgt
, cma_obj
->vaddr
,
107 cma_obj
->paddr
, size
);
117 drm_gem_cma_free_object(&cma_obj
->base
);
120 EXPORT_SYMBOL_GPL(drm_gem_cma_create
);
123 * drm_gem_cma_create_with_handle - allocate an object with the given
124 * size and create a gem handle on it
126 * returns a struct drm_gem_cma_object* on success or ERR_PTR values
129 static struct drm_gem_cma_object
*drm_gem_cma_create_with_handle(
130 struct drm_file
*file_priv
,
131 struct drm_device
*drm
, unsigned int size
,
132 unsigned int *handle
)
134 struct drm_gem_cma_object
*cma_obj
;
135 struct drm_gem_object
*gem_obj
;
138 cma_obj
= drm_gem_cma_create(drm
, size
);
142 gem_obj
= &cma_obj
->base
;
145 * allocate a id of idr table where the obj is registered
146 * and handle has the id what user can see.
148 ret
= drm_gem_handle_create(file_priv
, gem_obj
, handle
);
150 goto err_handle_create
;
152 /* drop reference from allocate - handle holds it now. */
153 drm_gem_object_unreference_unlocked(gem_obj
);
158 drm_gem_cma_free_object(gem_obj
);
164 * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback
167 void drm_gem_cma_free_object(struct drm_gem_object
*gem_obj
)
169 struct drm_gem_cma_object
*cma_obj
;
171 drm_gem_free_mmap_offset(gem_obj
);
173 cma_obj
= to_drm_gem_cma_obj(gem_obj
);
175 if (cma_obj
->vaddr
) {
176 dma_free_writecombine(gem_obj
->dev
->dev
, cma_obj
->base
.size
,
177 cma_obj
->vaddr
, cma_obj
->paddr
);
179 sg_free_table(cma_obj
->sgt
);
182 } else if (gem_obj
->import_attach
) {
183 drm_prime_gem_destroy(gem_obj
, cma_obj
->sgt
);
186 drm_gem_object_release(gem_obj
);
190 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object
);
193 * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback
196 * This aligns the pitch and size arguments to the minimum required. wrap
197 * this into your own function if you need bigger alignment.
199 int drm_gem_cma_dumb_create(struct drm_file
*file_priv
,
200 struct drm_device
*dev
, struct drm_mode_create_dumb
*args
)
202 struct drm_gem_cma_object
*cma_obj
;
203 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
205 if (args
->pitch
< min_pitch
)
206 args
->pitch
= min_pitch
;
208 if (args
->size
< args
->pitch
* args
->height
)
209 args
->size
= args
->pitch
* args
->height
;
211 cma_obj
= drm_gem_cma_create_with_handle(file_priv
, dev
,
212 args
->size
, &args
->handle
);
213 return PTR_ERR_OR_ZERO(cma_obj
);
215 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create
);
218 * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback
221 int drm_gem_cma_dumb_map_offset(struct drm_file
*file_priv
,
222 struct drm_device
*drm
, uint32_t handle
, uint64_t *offset
)
224 struct drm_gem_object
*gem_obj
;
226 mutex_lock(&drm
->struct_mutex
);
228 gem_obj
= drm_gem_object_lookup(drm
, file_priv
, handle
);
230 dev_err(drm
->dev
, "failed to lookup gem object\n");
231 mutex_unlock(&drm
->struct_mutex
);
235 *offset
= drm_vma_node_offset_addr(&gem_obj
->vma_node
);
237 drm_gem_object_unreference(gem_obj
);
239 mutex_unlock(&drm
->struct_mutex
);
243 EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset
);
245 const struct vm_operations_struct drm_gem_cma_vm_ops
= {
246 .open
= drm_gem_vm_open
,
247 .close
= drm_gem_vm_close
,
249 EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops
);
251 static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object
*cma_obj
,
252 struct vm_area_struct
*vma
)
256 ret
= remap_pfn_range(vma
, vma
->vm_start
, cma_obj
->paddr
>> PAGE_SHIFT
,
257 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
259 drm_gem_vm_close(vma
);
265 * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
267 int drm_gem_cma_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
269 struct drm_gem_cma_object
*cma_obj
;
270 struct drm_gem_object
*gem_obj
;
273 ret
= drm_gem_mmap(filp
, vma
);
277 gem_obj
= vma
->vm_private_data
;
278 cma_obj
= to_drm_gem_cma_obj(gem_obj
);
280 return drm_gem_cma_mmap_obj(cma_obj
, vma
);
282 EXPORT_SYMBOL_GPL(drm_gem_cma_mmap
);
284 #ifdef CONFIG_DEBUG_FS
285 void drm_gem_cma_describe(struct drm_gem_cma_object
*cma_obj
, struct seq_file
*m
)
287 struct drm_gem_object
*obj
= &cma_obj
->base
;
288 struct drm_device
*dev
= obj
->dev
;
291 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
293 off
= drm_vma_node_start(&obj
->vma_node
);
295 seq_printf(m
, "%2d (%2d) %08llx %08Zx %p %d",
296 obj
->name
, obj
->refcount
.refcount
.counter
,
297 off
, cma_obj
->paddr
, cma_obj
->vaddr
, obj
->size
);
301 EXPORT_SYMBOL_GPL(drm_gem_cma_describe
);
304 /* low-level interface prime helpers */
305 struct sg_table
*drm_gem_cma_prime_get_sg_table(struct drm_gem_object
*obj
)
307 struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
308 struct sg_table
*sgt
;
311 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
315 ret
= dma_get_sgtable(obj
->dev
->dev
, sgt
, cma_obj
->vaddr
,
316 cma_obj
->paddr
, obj
->size
);
326 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table
);
328 struct drm_gem_object
*
329 drm_gem_cma_prime_import_sg_table(struct drm_device
*dev
, size_t size
,
330 struct sg_table
*sgt
)
332 struct drm_gem_cma_object
*cma_obj
;
335 return ERR_PTR(-EINVAL
);
337 /* Create a CMA GEM buffer. */
338 cma_obj
= __drm_gem_cma_create(dev
, size
);
340 return ERR_PTR(PTR_ERR(cma_obj
));
342 cma_obj
->paddr
= sg_dma_address(sgt
->sgl
);
345 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = %zu\n", cma_obj
->paddr
, size
);
347 return &cma_obj
->base
;
349 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table
);
351 int drm_gem_cma_prime_mmap(struct drm_gem_object
*obj
,
352 struct vm_area_struct
*vma
)
354 struct drm_gem_cma_object
*cma_obj
;
355 struct drm_device
*dev
= obj
->dev
;
358 mutex_lock(&dev
->struct_mutex
);
359 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
360 mutex_unlock(&dev
->struct_mutex
);
364 cma_obj
= to_drm_gem_cma_obj(obj
);
365 return drm_gem_cma_mmap_obj(cma_obj
, vma
);
367 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap
);
369 void *drm_gem_cma_prime_vmap(struct drm_gem_object
*obj
)
371 struct drm_gem_cma_object
*cma_obj
= to_drm_gem_cma_obj(obj
);
373 return cma_obj
->vaddr
;
375 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap
);
377 void drm_gem_cma_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
381 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap
);