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 | struct sg_table *sgt = NULL; |
| 83 | int ret; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 84 | |
| 85 | size = round_up(size, PAGE_SIZE); |
| 86 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 87 | cma_obj = __drm_gem_cma_create(drm, size); |
| 88 | if (IS_ERR(cma_obj)) |
| 89 | return cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 90 | |
| 91 | cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size, |
| 92 | &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN); |
| 93 | if (!cma_obj->vaddr) { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 94 | dev_err(drm->dev, "failed to allocate buffer with size %d\n", |
| 95 | size); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 96 | ret = -ENOMEM; |
| 97 | goto error; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 100 | sgt = kzalloc(sizeof(*cma_obj->sgt), GFP_KERNEL); |
| 101 | if (sgt == NULL) { |
| 102 | ret = -ENOMEM; |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | ret = dma_get_sgtable(drm->dev, sgt, cma_obj->vaddr, |
| 107 | cma_obj->paddr, size); |
| 108 | if (ret < 0) |
| 109 | goto error; |
| 110 | |
| 111 | cma_obj->sgt = sgt; |
| 112 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 113 | return cma_obj; |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 114 | |
| 115 | error: |
| 116 | kfree(sgt); |
| 117 | drm_gem_cma_free_object(&cma_obj->base); |
| 118 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL_GPL(drm_gem_cma_create); |
| 121 | |
| 122 | /* |
| 123 | * drm_gem_cma_create_with_handle - allocate an object with the given |
| 124 | * size and create a gem handle on it |
| 125 | * |
| 126 | * returns a struct drm_gem_cma_object* on success or ERR_PTR values |
| 127 | * on failure. |
| 128 | */ |
| 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) |
| 133 | { |
| 134 | struct drm_gem_cma_object *cma_obj; |
| 135 | struct drm_gem_object *gem_obj; |
| 136 | int ret; |
| 137 | |
| 138 | cma_obj = drm_gem_cma_create(drm, size); |
| 139 | if (IS_ERR(cma_obj)) |
| 140 | return cma_obj; |
| 141 | |
| 142 | gem_obj = &cma_obj->base; |
| 143 | |
| 144 | /* |
| 145 | * allocate a id of idr table where the obj is registered |
| 146 | * and handle has the id what user can see. |
| 147 | */ |
| 148 | ret = drm_gem_handle_create(file_priv, gem_obj, handle); |
| 149 | if (ret) |
| 150 | goto err_handle_create; |
| 151 | |
| 152 | /* drop reference from allocate - handle holds it now. */ |
| 153 | drm_gem_object_unreference_unlocked(gem_obj); |
| 154 | |
| 155 | return cma_obj; |
| 156 | |
| 157 | err_handle_create: |
| 158 | drm_gem_cma_free_object(gem_obj); |
| 159 | |
| 160 | return ERR_PTR(ret); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback |
| 165 | * function |
| 166 | */ |
| 167 | void drm_gem_cma_free_object(struct drm_gem_object *gem_obj) |
| 168 | { |
| 169 | struct drm_gem_cma_object *cma_obj; |
| 170 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 171 | drm_gem_free_mmap_offset(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 172 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 173 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 174 | |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 175 | if (cma_obj->vaddr) { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 176 | dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size, |
| 177 | cma_obj->vaddr, cma_obj->paddr); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 178 | if (cma_obj->sgt) { |
| 179 | sg_free_table(cma_obj->sgt); |
| 180 | kfree(cma_obj->sgt); |
| 181 | } |
| 182 | } else if (gem_obj->import_attach) { |
| 183 | drm_prime_gem_destroy(gem_obj, cma_obj->sgt); |
| 184 | } |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 185 | |
| 186 | drm_gem_object_release(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 187 | |
| 188 | kfree(cma_obj); |
| 189 | } |
| 190 | EXPORT_SYMBOL_GPL(drm_gem_cma_free_object); |
| 191 | |
| 192 | /* |
| 193 | * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback |
| 194 | * function |
| 195 | * |
| 196 | * This aligns the pitch and size arguments to the minimum required. wrap |
| 197 | * this into your own function if you need bigger alignment. |
| 198 | */ |
| 199 | int drm_gem_cma_dumb_create(struct drm_file *file_priv, |
| 200 | struct drm_device *dev, struct drm_mode_create_dumb *args) |
| 201 | { |
| 202 | struct drm_gem_cma_object *cma_obj; |
| 203 | int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 204 | |
| 205 | if (args->pitch < min_pitch) |
| 206 | args->pitch = min_pitch; |
| 207 | |
| 208 | if (args->size < args->pitch * args->height) |
| 209 | args->size = args->pitch * args->height; |
| 210 | |
| 211 | cma_obj = drm_gem_cma_create_with_handle(file_priv, dev, |
| 212 | args->size, &args->handle); |
Sachin Kamat | b03cda5 | 2013-07-15 16:04:46 +0530 | [diff] [blame] | 213 | return PTR_ERR_OR_ZERO(cma_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 214 | } |
| 215 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create); |
| 216 | |
| 217 | /* |
| 218 | * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback |
| 219 | * function |
| 220 | */ |
| 221 | int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv, |
| 222 | struct drm_device *drm, uint32_t handle, uint64_t *offset) |
| 223 | { |
| 224 | struct drm_gem_object *gem_obj; |
| 225 | |
| 226 | mutex_lock(&drm->struct_mutex); |
| 227 | |
| 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; |
| 233 | } |
| 234 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 235 | *offset = drm_vma_node_offset_addr(&gem_obj->vma_node); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 236 | |
| 237 | drm_gem_object_unreference(gem_obj); |
| 238 | |
| 239 | mutex_unlock(&drm->struct_mutex); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset); |
| 244 | |
| 245 | const struct vm_operations_struct drm_gem_cma_vm_ops = { |
| 246 | .open = drm_gem_vm_open, |
| 247 | .close = drm_gem_vm_close, |
| 248 | }; |
| 249 | EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops); |
| 250 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 251 | static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj, |
| 252 | struct vm_area_struct *vma) |
| 253 | { |
| 254 | int ret; |
| 255 | |
| 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); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 264 | /* |
| 265 | * drm_gem_cma_mmap - (struct file_operation)->mmap callback function |
| 266 | */ |
| 267 | int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma) |
| 268 | { |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 269 | struct drm_gem_cma_object *cma_obj; |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 270 | struct drm_gem_object *gem_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 271 | int ret; |
| 272 | |
| 273 | ret = drm_gem_mmap(filp, vma); |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
| 277 | gem_obj = vma->vm_private_data; |
| 278 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 279 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 280 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 281 | } |
| 282 | EXPORT_SYMBOL_GPL(drm_gem_cma_mmap); |
| 283 | |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 284 | #ifdef CONFIG_DEBUG_FS |
| 285 | void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m) |
| 286 | { |
| 287 | struct drm_gem_object *obj = &cma_obj->base; |
| 288 | struct drm_device *dev = obj->dev; |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 289 | uint64_t off; |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 290 | |
| 291 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 292 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 293 | off = drm_vma_node_start(&obj->vma_node); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 294 | |
| 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); |
| 298 | |
| 299 | seq_printf(m, "\n"); |
| 300 | } |
| 301 | EXPORT_SYMBOL_GPL(drm_gem_cma_describe); |
| 302 | #endif |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 303 | |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 304 | /* low-level interface prime helpers */ |
| 305 | struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj) |
| 306 | { |
| 307 | struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); |
| 308 | struct sg_table *sgt; |
| 309 | int ret; |
| 310 | |
| 311 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 312 | if (!sgt) |
| 313 | return NULL; |
| 314 | |
| 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; |
| 319 | |
| 320 | return sgt; |
| 321 | |
| 322 | out: |
| 323 | kfree(sgt); |
| 324 | return NULL; |
| 325 | } |
| 326 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table); |
| 327 | |
| 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) |
| 331 | { |
| 332 | struct drm_gem_cma_object *cma_obj; |
| 333 | |
| 334 | if (sgt->nents != 1) |
| 335 | return ERR_PTR(-EINVAL); |
| 336 | |
| 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)); |
| 341 | |
| 342 | cma_obj->paddr = sg_dma_address(sgt->sgl); |
| 343 | cma_obj->sgt = sgt; |
| 344 | |
| 345 | DRM_DEBUG_PRIME("dma_addr = 0x%x, size = %zu\n", cma_obj->paddr, size); |
| 346 | |
| 347 | return &cma_obj->base; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table); |
| 350 | |
| 351 | int drm_gem_cma_prime_mmap(struct drm_gem_object *obj, |
| 352 | struct vm_area_struct *vma) |
| 353 | { |
| 354 | struct drm_gem_cma_object *cma_obj; |
| 355 | struct drm_device *dev = obj->dev; |
| 356 | int ret; |
| 357 | |
| 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; |
| 363 | |
| 364 | cma_obj = to_drm_gem_cma_obj(obj); |
| 365 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
| 366 | } |
| 367 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap); |
| 368 | |
| 369 | void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj) |
| 370 | { |
| 371 | struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); |
| 372 | |
| 373 | return cma_obj->vaddr; |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap); |
| 376 | |
| 377 | void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 378 | { |
| 379 | /* Nothing to do */ |
| 380 | } |
| 381 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap); |