Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drm gem CMA (contiguous memory allocator) helper functions |
| 3 | * |
| 4 | * Copyright (C) 2012 Sascha Hauer, Pengutronix |
| 5 | * |
| 6 | * Based on Samsung Exynos code |
| 7 | * |
| 8 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 9 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/export.h> |
| 24 | #include <linux/dma-mapping.h> |
| 25 | |
| 26 | #include <drm/drmP.h> |
| 27 | #include <drm/drm.h> |
| 28 | #include <drm/drm_gem_cma_helper.h> |
| 29 | |
| 30 | static unsigned int get_gem_mmap_offset(struct drm_gem_object *obj) |
| 31 | { |
| 32 | return (unsigned int)obj->map_list.hash.key << PAGE_SHIFT; |
| 33 | } |
| 34 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 35 | /* |
| 36 | * __drm_gem_cma_create - Create a GEM CMA object without allocating memory |
| 37 | * @drm: The drm device |
| 38 | * @size: The GEM object size |
| 39 | * |
| 40 | * This function creates and initializes a GEM CMA object of the given size, but |
| 41 | * doesn't allocate any memory to back the object. |
| 42 | * |
| 43 | * Return a struct drm_gem_cma_object* on success or ERR_PTR values on failure. |
| 44 | */ |
| 45 | static struct drm_gem_cma_object * |
| 46 | __drm_gem_cma_create(struct drm_device *drm, unsigned int size) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 47 | { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 48 | struct drm_gem_cma_object *cma_obj; |
| 49 | struct drm_gem_object *gem_obj; |
| 50 | int ret; |
| 51 | |
| 52 | cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL); |
| 53 | if (!cma_obj) |
| 54 | return ERR_PTR(-ENOMEM); |
| 55 | |
| 56 | gem_obj = &cma_obj->base; |
| 57 | |
| 58 | ret = drm_gem_object_init(drm, gem_obj, size); |
| 59 | if (ret) |
| 60 | goto error; |
| 61 | |
| 62 | ret = drm_gem_create_mmap_offset(gem_obj); |
| 63 | if (ret) { |
| 64 | drm_gem_object_release(gem_obj); |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | return cma_obj; |
| 69 | |
| 70 | error: |
| 71 | kfree(cma_obj); |
| 72 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * drm_gem_cma_create - allocate an object with the given size |
| 77 | * |
| 78 | * returns a struct drm_gem_cma_object* on success or ERR_PTR values |
| 79 | * on failure. |
| 80 | */ |
| 81 | struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, |
| 82 | unsigned int size) |
| 83 | { |
| 84 | struct drm_gem_cma_object *cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 85 | |
| 86 | size = round_up(size, PAGE_SIZE); |
| 87 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 88 | cma_obj = __drm_gem_cma_create(drm, size); |
| 89 | if (IS_ERR(cma_obj)) |
| 90 | return cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 91 | |
| 92 | cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size, |
| 93 | &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN); |
| 94 | if (!cma_obj->vaddr) { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 95 | dev_err(drm->dev, "failed to allocate buffer with size %d\n", |
| 96 | size); |
| 97 | drm_gem_cma_free_object(&cma_obj->base); |
| 98 | return ERR_PTR(-ENOMEM); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 101 | return cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 102 | } |
| 103 | EXPORT_SYMBOL_GPL(drm_gem_cma_create); |
| 104 | |
| 105 | /* |
| 106 | * drm_gem_cma_create_with_handle - allocate an object with the given |
| 107 | * size and create a gem handle on it |
| 108 | * |
| 109 | * returns a struct drm_gem_cma_object* on success or ERR_PTR values |
| 110 | * on failure. |
| 111 | */ |
| 112 | static struct drm_gem_cma_object *drm_gem_cma_create_with_handle( |
| 113 | struct drm_file *file_priv, |
| 114 | struct drm_device *drm, unsigned int size, |
| 115 | unsigned int *handle) |
| 116 | { |
| 117 | struct drm_gem_cma_object *cma_obj; |
| 118 | struct drm_gem_object *gem_obj; |
| 119 | int ret; |
| 120 | |
| 121 | cma_obj = drm_gem_cma_create(drm, size); |
| 122 | if (IS_ERR(cma_obj)) |
| 123 | return cma_obj; |
| 124 | |
| 125 | gem_obj = &cma_obj->base; |
| 126 | |
| 127 | /* |
| 128 | * allocate a id of idr table where the obj is registered |
| 129 | * and handle has the id what user can see. |
| 130 | */ |
| 131 | ret = drm_gem_handle_create(file_priv, gem_obj, handle); |
| 132 | if (ret) |
| 133 | goto err_handle_create; |
| 134 | |
| 135 | /* drop reference from allocate - handle holds it now. */ |
| 136 | drm_gem_object_unreference_unlocked(gem_obj); |
| 137 | |
| 138 | return cma_obj; |
| 139 | |
| 140 | err_handle_create: |
| 141 | drm_gem_cma_free_object(gem_obj); |
| 142 | |
| 143 | return ERR_PTR(ret); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback |
| 148 | * function |
| 149 | */ |
| 150 | void drm_gem_cma_free_object(struct drm_gem_object *gem_obj) |
| 151 | { |
| 152 | struct drm_gem_cma_object *cma_obj; |
| 153 | |
| 154 | if (gem_obj->map_list.map) |
| 155 | drm_gem_free_mmap_offset(gem_obj); |
| 156 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 157 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 158 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 159 | if (cma_obj->vaddr) |
| 160 | dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size, |
| 161 | cma_obj->vaddr, cma_obj->paddr); |
| 162 | |
| 163 | drm_gem_object_release(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 164 | |
| 165 | kfree(cma_obj); |
| 166 | } |
| 167 | EXPORT_SYMBOL_GPL(drm_gem_cma_free_object); |
| 168 | |
| 169 | /* |
| 170 | * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback |
| 171 | * function |
| 172 | * |
| 173 | * This aligns the pitch and size arguments to the minimum required. wrap |
| 174 | * this into your own function if you need bigger alignment. |
| 175 | */ |
| 176 | int drm_gem_cma_dumb_create(struct drm_file *file_priv, |
| 177 | struct drm_device *dev, struct drm_mode_create_dumb *args) |
| 178 | { |
| 179 | struct drm_gem_cma_object *cma_obj; |
| 180 | int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 181 | |
| 182 | if (args->pitch < min_pitch) |
| 183 | args->pitch = min_pitch; |
| 184 | |
| 185 | if (args->size < args->pitch * args->height) |
| 186 | args->size = args->pitch * args->height; |
| 187 | |
| 188 | cma_obj = drm_gem_cma_create_with_handle(file_priv, dev, |
| 189 | args->size, &args->handle); |
| 190 | if (IS_ERR(cma_obj)) |
| 191 | return PTR_ERR(cma_obj); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create); |
| 196 | |
| 197 | /* |
| 198 | * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback |
| 199 | * function |
| 200 | */ |
| 201 | int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv, |
| 202 | struct drm_device *drm, uint32_t handle, uint64_t *offset) |
| 203 | { |
| 204 | struct drm_gem_object *gem_obj; |
| 205 | |
| 206 | mutex_lock(&drm->struct_mutex); |
| 207 | |
| 208 | gem_obj = drm_gem_object_lookup(drm, file_priv, handle); |
| 209 | if (!gem_obj) { |
| 210 | dev_err(drm->dev, "failed to lookup gem object\n"); |
| 211 | mutex_unlock(&drm->struct_mutex); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | *offset = get_gem_mmap_offset(gem_obj); |
| 216 | |
| 217 | drm_gem_object_unreference(gem_obj); |
| 218 | |
| 219 | mutex_unlock(&drm->struct_mutex); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset); |
| 224 | |
| 225 | const struct vm_operations_struct drm_gem_cma_vm_ops = { |
| 226 | .open = drm_gem_vm_open, |
| 227 | .close = drm_gem_vm_close, |
| 228 | }; |
| 229 | EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops); |
| 230 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame^] | 231 | static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj, |
| 232 | struct vm_area_struct *vma) |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | ret = remap_pfn_range(vma, vma->vm_start, cma_obj->paddr >> PAGE_SHIFT, |
| 237 | vma->vm_end - vma->vm_start, vma->vm_page_prot); |
| 238 | if (ret) |
| 239 | drm_gem_vm_close(vma); |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 244 | /* |
| 245 | * drm_gem_cma_mmap - (struct file_operation)->mmap callback function |
| 246 | */ |
| 247 | int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma) |
| 248 | { |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 249 | struct drm_gem_cma_object *cma_obj; |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame^] | 250 | struct drm_gem_object *gem_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 251 | int ret; |
| 252 | |
| 253 | ret = drm_gem_mmap(filp, vma); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | |
| 257 | gem_obj = vma->vm_private_data; |
| 258 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 259 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame^] | 260 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 261 | } |
| 262 | EXPORT_SYMBOL_GPL(drm_gem_cma_mmap); |
| 263 | |
| 264 | /* |
| 265 | * drm_gem_cma_dumb_destroy - (struct drm_driver)->dumb_destroy callback function |
| 266 | */ |
| 267 | int drm_gem_cma_dumb_destroy(struct drm_file *file_priv, |
| 268 | struct drm_device *drm, unsigned int handle) |
| 269 | { |
| 270 | return drm_gem_handle_delete(file_priv, handle); |
| 271 | } |
| 272 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_destroy); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 273 | |
| 274 | #ifdef CONFIG_DEBUG_FS |
| 275 | void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m) |
| 276 | { |
| 277 | struct drm_gem_object *obj = &cma_obj->base; |
| 278 | struct drm_device *dev = obj->dev; |
| 279 | uint64_t off = 0; |
| 280 | |
| 281 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 282 | |
| 283 | if (obj->map_list.map) |
| 284 | off = (uint64_t)obj->map_list.hash.key; |
| 285 | |
| 286 | seq_printf(m, "%2d (%2d) %08llx %08Zx %p %d", |
| 287 | obj->name, obj->refcount.refcount.counter, |
| 288 | off, cma_obj->paddr, cma_obj->vaddr, obj->size); |
| 289 | |
| 290 | seq_printf(m, "\n"); |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(drm_gem_cma_describe); |
| 293 | #endif |