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> |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 24 | #include <linux/dma-buf.h> |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 25 | #include <linux/dma-mapping.h> |
| 26 | |
| 27 | #include <drm/drmP.h> |
| 28 | #include <drm/drm.h> |
| 29 | #include <drm/drm_gem_cma_helper.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 30 | #include <drm/drm_vma_manager.h> |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 31 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 32 | /* |
| 33 | * __drm_gem_cma_create - Create a GEM CMA object without allocating memory |
| 34 | * @drm: The drm device |
| 35 | * @size: The GEM object size |
| 36 | * |
| 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. |
| 39 | * |
| 40 | * Return a struct drm_gem_cma_object* on success or ERR_PTR values on failure. |
| 41 | */ |
| 42 | static struct drm_gem_cma_object * |
| 43 | __drm_gem_cma_create(struct drm_device *drm, unsigned int size) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 44 | { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 45 | struct drm_gem_cma_object *cma_obj; |
| 46 | struct drm_gem_object *gem_obj; |
| 47 | int ret; |
| 48 | |
| 49 | cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL); |
| 50 | if (!cma_obj) |
| 51 | return ERR_PTR(-ENOMEM); |
| 52 | |
| 53 | gem_obj = &cma_obj->base; |
| 54 | |
| 55 | ret = drm_gem_object_init(drm, gem_obj, size); |
| 56 | if (ret) |
| 57 | goto error; |
| 58 | |
| 59 | ret = drm_gem_create_mmap_offset(gem_obj); |
| 60 | if (ret) { |
| 61 | drm_gem_object_release(gem_obj); |
| 62 | goto error; |
| 63 | } |
| 64 | |
| 65 | return cma_obj; |
| 66 | |
| 67 | error: |
| 68 | kfree(cma_obj); |
| 69 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | * drm_gem_cma_create - allocate an object with the given size |
| 74 | * |
| 75 | * returns a struct drm_gem_cma_object* on success or ERR_PTR values |
| 76 | * on failure. |
| 77 | */ |
| 78 | struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, |
| 79 | unsigned int size) |
| 80 | { |
| 81 | struct drm_gem_cma_object *cma_obj; |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 82 | int ret; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 83 | |
| 84 | size = round_up(size, PAGE_SIZE); |
| 85 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 86 | cma_obj = __drm_gem_cma_create(drm, size); |
| 87 | if (IS_ERR(cma_obj)) |
| 88 | return cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 89 | |
| 90 | cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size, |
| 91 | &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN); |
| 92 | if (!cma_obj->vaddr) { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 93 | dev_err(drm->dev, "failed to allocate buffer with size %d\n", |
| 94 | size); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 95 | ret = -ENOMEM; |
| 96 | goto error; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 99 | return cma_obj; |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 100 | |
| 101 | error: |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 102 | drm_gem_cma_free_object(&cma_obj->base); |
| 103 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL_GPL(drm_gem_cma_create); |
| 106 | |
| 107 | /* |
| 108 | * drm_gem_cma_create_with_handle - allocate an object with the given |
| 109 | * size and create a gem handle on it |
| 110 | * |
| 111 | * returns a struct drm_gem_cma_object* on success or ERR_PTR values |
| 112 | * on failure. |
| 113 | */ |
| 114 | static struct drm_gem_cma_object *drm_gem_cma_create_with_handle( |
| 115 | struct drm_file *file_priv, |
| 116 | struct drm_device *drm, unsigned int size, |
| 117 | unsigned int *handle) |
| 118 | { |
| 119 | struct drm_gem_cma_object *cma_obj; |
| 120 | struct drm_gem_object *gem_obj; |
| 121 | int ret; |
| 122 | |
| 123 | cma_obj = drm_gem_cma_create(drm, size); |
| 124 | if (IS_ERR(cma_obj)) |
| 125 | return cma_obj; |
| 126 | |
| 127 | gem_obj = &cma_obj->base; |
| 128 | |
| 129 | /* |
| 130 | * allocate a id of idr table where the obj is registered |
| 131 | * and handle has the id what user can see. |
| 132 | */ |
| 133 | ret = drm_gem_handle_create(file_priv, gem_obj, handle); |
| 134 | if (ret) |
| 135 | goto err_handle_create; |
| 136 | |
| 137 | /* drop reference from allocate - handle holds it now. */ |
| 138 | drm_gem_object_unreference_unlocked(gem_obj); |
| 139 | |
| 140 | return cma_obj; |
| 141 | |
| 142 | err_handle_create: |
| 143 | drm_gem_cma_free_object(gem_obj); |
| 144 | |
| 145 | return ERR_PTR(ret); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback |
| 150 | * function |
| 151 | */ |
| 152 | void drm_gem_cma_free_object(struct drm_gem_object *gem_obj) |
| 153 | { |
| 154 | struct drm_gem_cma_object *cma_obj; |
| 155 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 156 | drm_gem_free_mmap_offset(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 157 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 158 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 159 | |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 160 | if (cma_obj->vaddr) { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 161 | dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size, |
| 162 | cma_obj->vaddr, cma_obj->paddr); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 163 | } else if (gem_obj->import_attach) { |
| 164 | drm_prime_gem_destroy(gem_obj, cma_obj->sgt); |
| 165 | } |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 166 | |
| 167 | drm_gem_object_release(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 168 | |
| 169 | kfree(cma_obj); |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(drm_gem_cma_free_object); |
| 172 | |
| 173 | /* |
| 174 | * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback |
| 175 | * function |
| 176 | * |
| 177 | * This aligns the pitch and size arguments to the minimum required. wrap |
| 178 | * this into your own function if you need bigger alignment. |
| 179 | */ |
| 180 | int drm_gem_cma_dumb_create(struct drm_file *file_priv, |
| 181 | struct drm_device *dev, struct drm_mode_create_dumb *args) |
| 182 | { |
| 183 | struct drm_gem_cma_object *cma_obj; |
| 184 | int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 185 | |
| 186 | if (args->pitch < min_pitch) |
| 187 | args->pitch = min_pitch; |
| 188 | |
| 189 | if (args->size < args->pitch * args->height) |
| 190 | args->size = args->pitch * args->height; |
| 191 | |
| 192 | cma_obj = drm_gem_cma_create_with_handle(file_priv, dev, |
| 193 | args->size, &args->handle); |
Sachin Kamat | b03cda5 | 2013-07-15 16:04:46 +0530 | [diff] [blame] | 194 | return PTR_ERR_OR_ZERO(cma_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 195 | } |
| 196 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create); |
| 197 | |
| 198 | /* |
| 199 | * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback |
| 200 | * function |
| 201 | */ |
| 202 | int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv, |
| 203 | struct drm_device *drm, uint32_t handle, uint64_t *offset) |
| 204 | { |
| 205 | struct drm_gem_object *gem_obj; |
| 206 | |
| 207 | mutex_lock(&drm->struct_mutex); |
| 208 | |
| 209 | gem_obj = drm_gem_object_lookup(drm, file_priv, handle); |
| 210 | if (!gem_obj) { |
| 211 | dev_err(drm->dev, "failed to lookup gem object\n"); |
| 212 | mutex_unlock(&drm->struct_mutex); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 216 | *offset = drm_vma_node_offset_addr(&gem_obj->vma_node); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 217 | |
| 218 | drm_gem_object_unreference(gem_obj); |
| 219 | |
| 220 | mutex_unlock(&drm->struct_mutex); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset); |
| 225 | |
| 226 | const struct vm_operations_struct drm_gem_cma_vm_ops = { |
| 227 | .open = drm_gem_vm_open, |
| 228 | .close = drm_gem_vm_close, |
| 229 | }; |
| 230 | EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops); |
| 231 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 232 | static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj, |
| 233 | struct vm_area_struct *vma) |
| 234 | { |
| 235 | int ret; |
| 236 | |
Laurent Pinchart | b65e64f | 2014-03-02 20:09:48 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the |
| 239 | * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map |
| 240 | * the whole buffer. |
| 241 | */ |
| 242 | vma->vm_flags &= ~VM_PFNMAP; |
| 243 | vma->vm_pgoff = 0; |
| 244 | |
| 245 | ret = dma_mmap_writecombine(cma_obj->base.dev->dev, vma, |
| 246 | cma_obj->vaddr, cma_obj->paddr, |
| 247 | vma->vm_end - vma->vm_start); |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 248 | if (ret) |
| 249 | drm_gem_vm_close(vma); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 254 | /* |
| 255 | * drm_gem_cma_mmap - (struct file_operation)->mmap callback function |
| 256 | */ |
| 257 | int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma) |
| 258 | { |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 259 | struct drm_gem_cma_object *cma_obj; |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 260 | struct drm_gem_object *gem_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 261 | int ret; |
| 262 | |
| 263 | ret = drm_gem_mmap(filp, vma); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | gem_obj = vma->vm_private_data; |
| 268 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 269 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 270 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 271 | } |
| 272 | EXPORT_SYMBOL_GPL(drm_gem_cma_mmap); |
| 273 | |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 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; |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 279 | uint64_t off; |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 280 | |
| 281 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 282 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 283 | off = drm_vma_node_start(&obj->vma_node); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 284 | |
Laurent Pinchart | f4d1b02 | 2014-03-04 19:10:17 +0100 | [diff] [blame^] | 285 | seq_printf(m, "%2d (%2d) %08llx %pad %p %d", |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 286 | obj->name, obj->refcount.refcount.counter, |
Laurent Pinchart | f4d1b02 | 2014-03-04 19:10:17 +0100 | [diff] [blame^] | 287 | off, &cma_obj->paddr, cma_obj->vaddr, obj->size); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 288 | |
| 289 | seq_printf(m, "\n"); |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(drm_gem_cma_describe); |
| 292 | #endif |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 293 | |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 294 | /* low-level interface prime helpers */ |
| 295 | struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj) |
| 296 | { |
| 297 | struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); |
| 298 | struct sg_table *sgt; |
| 299 | int ret; |
| 300 | |
| 301 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 302 | if (!sgt) |
| 303 | return NULL; |
| 304 | |
| 305 | ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr, |
| 306 | cma_obj->paddr, obj->size); |
| 307 | if (ret < 0) |
| 308 | goto out; |
| 309 | |
| 310 | return sgt; |
| 311 | |
| 312 | out: |
| 313 | kfree(sgt); |
| 314 | return NULL; |
| 315 | } |
| 316 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table); |
| 317 | |
| 318 | struct drm_gem_object * |
| 319 | drm_gem_cma_prime_import_sg_table(struct drm_device *dev, size_t size, |
| 320 | struct sg_table *sgt) |
| 321 | { |
| 322 | struct drm_gem_cma_object *cma_obj; |
| 323 | |
| 324 | if (sgt->nents != 1) |
| 325 | return ERR_PTR(-EINVAL); |
| 326 | |
| 327 | /* Create a CMA GEM buffer. */ |
| 328 | cma_obj = __drm_gem_cma_create(dev, size); |
| 329 | if (IS_ERR(cma_obj)) |
| 330 | return ERR_PTR(PTR_ERR(cma_obj)); |
| 331 | |
| 332 | cma_obj->paddr = sg_dma_address(sgt->sgl); |
| 333 | cma_obj->sgt = sgt; |
| 334 | |
Laurent Pinchart | f4d1b02 | 2014-03-04 19:10:17 +0100 | [diff] [blame^] | 335 | DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, size); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 336 | |
| 337 | return &cma_obj->base; |
| 338 | } |
| 339 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table); |
| 340 | |
| 341 | int drm_gem_cma_prime_mmap(struct drm_gem_object *obj, |
| 342 | struct vm_area_struct *vma) |
| 343 | { |
| 344 | struct drm_gem_cma_object *cma_obj; |
| 345 | struct drm_device *dev = obj->dev; |
| 346 | int ret; |
| 347 | |
| 348 | mutex_lock(&dev->struct_mutex); |
| 349 | ret = drm_gem_mmap_obj(obj, obj->size, vma); |
| 350 | mutex_unlock(&dev->struct_mutex); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | |
| 354 | cma_obj = to_drm_gem_cma_obj(obj); |
| 355 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap); |
| 358 | |
| 359 | void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj) |
| 360 | { |
| 361 | struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); |
| 362 | |
| 363 | return cma_obj->vaddr; |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap); |
| 366 | |
| 367 | void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 368 | { |
| 369 | /* Nothing to do */ |
| 370 | } |
| 371 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap); |