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