x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / gpu / drm / drm_gem_cma_helper.c
blob6b51bf90df0ef0c1849fdcba6e476c4dc53d3bc4
1 /*
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.
20 #include <linux/mm.h>
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>
27 #include <drm/drmP.h>
28 #include <drm/drm.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;
47 int ret;
49 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
50 if (!cma_obj)
51 return ERR_PTR(-ENOMEM);
53 gem_obj = &cma_obj->base;
55 ret = drm_gem_object_init(drm, gem_obj, size);
56 if (ret)
57 goto error;
59 ret = drm_gem_create_mmap_offset(gem_obj);
60 if (ret) {
61 drm_gem_object_release(gem_obj);
62 goto error;
65 return cma_obj;
67 error:
68 kfree(cma_obj);
69 return ERR_PTR(ret);
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
76 * on failure.
78 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
79 unsigned int size)
81 struct drm_gem_cma_object *cma_obj;
82 struct sg_table *sgt = NULL;
83 int ret;
85 size = round_up(size, PAGE_SIZE);
87 cma_obj = __drm_gem_cma_create(drm, size);
88 if (IS_ERR(cma_obj))
89 return cma_obj;
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",
95 size);
96 ret = -ENOMEM;
97 goto error;
100 sgt = kzalloc(sizeof(*cma_obj->sgt), GFP_KERNEL);
101 if (sgt == NULL) {
102 ret = -ENOMEM;
103 goto error;
106 ret = dma_get_sgtable(drm->dev, sgt, cma_obj->vaddr,
107 cma_obj->paddr, size);
108 if (ret < 0)
109 goto error;
111 cma_obj->sgt = sgt;
113 return cma_obj;
115 error:
116 kfree(sgt);
117 drm_gem_cma_free_object(&cma_obj->base);
118 return ERR_PTR(ret);
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
127 * on failure.
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;
136 int ret;
138 cma_obj = drm_gem_cma_create(drm, size);
139 if (IS_ERR(cma_obj))
140 return cma_obj;
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);
149 if (ret)
150 goto err_handle_create;
152 /* drop reference from allocate - handle holds it now. */
153 drm_gem_object_unreference_unlocked(gem_obj);
155 return cma_obj;
157 err_handle_create:
158 drm_gem_cma_free_object(gem_obj);
160 return ERR_PTR(ret);
164 * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback
165 * function
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);
178 if (cma_obj->sgt) {
179 sg_free_table(cma_obj->sgt);
180 kfree(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);
188 kfree(cma_obj);
190 EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
193 * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback
194 * function
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
219 * function
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);
229 if (!gem_obj) {
230 dev_err(drm->dev, "failed to lookup gem object\n");
231 mutex_unlock(&drm->struct_mutex);
232 return -EINVAL;
235 *offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
237 drm_gem_object_unreference(gem_obj);
239 mutex_unlock(&drm->struct_mutex);
241 return 0;
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)
254 int ret;
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);
258 if (ret)
259 drm_gem_vm_close(vma);
261 return ret;
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;
271 int ret;
273 ret = drm_gem_mmap(filp, vma);
274 if (ret)
275 return ret;
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;
289 uint64_t off;
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);
299 seq_printf(m, "\n");
301 EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
302 #endif
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;
309 int ret;
311 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
312 if (!sgt)
313 return NULL;
315 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
316 cma_obj->paddr, obj->size);
317 if (ret < 0)
318 goto out;
320 return sgt;
322 out:
323 kfree(sgt);
324 return NULL;
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;
334 if (sgt->nents != 1)
335 return ERR_PTR(-EINVAL);
337 /* Create a CMA GEM buffer. */
338 cma_obj = __drm_gem_cma_create(dev, size);
339 if (IS_ERR(cma_obj))
340 return ERR_PTR(PTR_ERR(cma_obj));
342 cma_obj->paddr = sg_dma_address(sgt->sgl);
343 cma_obj->sgt = sgt;
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;
356 int ret;
358 mutex_lock(&dev->struct_mutex);
359 ret = drm_gem_mmap_obj(obj, obj->size, vma);
360 mutex_unlock(&dev->struct_mutex);
361 if (ret < 0)
362 return ret;
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)
379 /* Nothing to do */
381 EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);