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 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 32 | /** |
| 33 | * DOC: cma helpers |
| 34 | * |
| 35 | * The Contiguous Memory Allocator reserves a pool of memory at early boot |
| 36 | * that is used to service requests for large blocks of contiguous memory. |
| 37 | * |
| 38 | * The DRM GEM/CMA helpers use this allocator as a means to provide buffer |
| 39 | * objects that are physically contiguous in memory. This is useful for |
| 40 | * display drivers that are unable to map scattered buffers via an IOMMU. |
| 41 | */ |
| 42 | |
| 43 | /** |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 44 | * __drm_gem_cma_create - Create a GEM CMA object without allocating memory |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 45 | * @drm: DRM device |
| 46 | * @size: size of the object to allocate |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 47 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 48 | * This function creates and initializes a GEM CMA object of the given size, |
| 49 | * but doesn't allocate any memory to back the object. |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 50 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 51 | * Returns: |
| 52 | * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative |
| 53 | * error code on failure. |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 54 | */ |
| 55 | static struct drm_gem_cma_object * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 56 | __drm_gem_cma_create(struct drm_device *drm, size_t size) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 57 | { |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 58 | struct drm_gem_cma_object *cma_obj; |
| 59 | struct drm_gem_object *gem_obj; |
| 60 | int ret; |
| 61 | |
Eric Anholt | 10028c5 | 2015-11-30 10:55:13 -0800 | [diff] [blame] | 62 | if (drm->driver->gem_create_object) |
| 63 | gem_obj = drm->driver->gem_create_object(drm, size); |
| 64 | else |
| 65 | gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL); |
| 66 | if (!gem_obj) |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 67 | return ERR_PTR(-ENOMEM); |
Eric Anholt | 10028c5 | 2015-11-30 10:55:13 -0800 | [diff] [blame] | 68 | cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base); |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 69 | |
| 70 | ret = drm_gem_object_init(drm, gem_obj, size); |
| 71 | if (ret) |
| 72 | goto error; |
| 73 | |
| 74 | ret = drm_gem_create_mmap_offset(gem_obj); |
| 75 | if (ret) { |
| 76 | drm_gem_object_release(gem_obj); |
| 77 | goto error; |
| 78 | } |
| 79 | |
| 80 | return cma_obj; |
| 81 | |
| 82 | error: |
| 83 | kfree(cma_obj); |
| 84 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 87 | /** |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 88 | * drm_gem_cma_create - allocate an object with the given size |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 89 | * @drm: DRM device |
| 90 | * @size: size of the object to allocate |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 91 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 92 | * This function creates a CMA GEM object and allocates a contiguous chunk of |
| 93 | * memory as backing store. The backing memory has the writecombine attribute |
| 94 | * set. |
| 95 | * |
| 96 | * Returns: |
| 97 | * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative |
| 98 | * error code on failure. |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 99 | */ |
| 100 | struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 101 | size_t size) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 102 | { |
| 103 | struct drm_gem_cma_object *cma_obj; |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 104 | int ret; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 105 | |
| 106 | size = round_up(size, PAGE_SIZE); |
| 107 | |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 108 | cma_obj = __drm_gem_cma_create(drm, size); |
| 109 | if (IS_ERR(cma_obj)) |
| 110 | return cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 111 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 112 | cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr, |
| 113 | GFP_KERNEL | __GFP_NOWARN); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 114 | if (!cma_obj->vaddr) { |
Magnus Damm | 30c4cf3 | 2015-05-04 16:16:19 +0200 | [diff] [blame] | 115 | dev_err(drm->dev, "failed to allocate buffer with size %zu\n", |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 116 | size); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 117 | ret = -ENOMEM; |
| 118 | goto error; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 121 | return cma_obj; |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 122 | |
| 123 | error: |
Thierry Reding | e6b6271 | 2017-02-28 15:46:41 +0100 | [diff] [blame] | 124 | drm_gem_object_put_unlocked(&cma_obj->base); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 125 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL_GPL(drm_gem_cma_create); |
| 128 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 129 | /** |
| 130 | * drm_gem_cma_create_with_handle - allocate an object with the given size and |
| 131 | * return a GEM handle to it |
| 132 | * @file_priv: DRM file-private structure to register the handle for |
| 133 | * @drm: DRM device |
| 134 | * @size: size of the object to allocate |
| 135 | * @handle: return location for the GEM handle |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 136 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 137 | * This function creates a CMA GEM object, allocating a physically contiguous |
| 138 | * chunk of memory as backing store. The GEM object is then added to the list |
| 139 | * of object associated with the given file and a handle to it is returned. |
| 140 | * |
| 141 | * Returns: |
| 142 | * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative |
| 143 | * error code on failure. |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 144 | */ |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 145 | static struct drm_gem_cma_object * |
| 146 | drm_gem_cma_create_with_handle(struct drm_file *file_priv, |
| 147 | struct drm_device *drm, size_t size, |
| 148 | uint32_t *handle) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 149 | { |
| 150 | struct drm_gem_cma_object *cma_obj; |
| 151 | struct drm_gem_object *gem_obj; |
| 152 | int ret; |
| 153 | |
| 154 | cma_obj = drm_gem_cma_create(drm, size); |
| 155 | if (IS_ERR(cma_obj)) |
| 156 | return cma_obj; |
| 157 | |
| 158 | gem_obj = &cma_obj->base; |
| 159 | |
| 160 | /* |
| 161 | * allocate a id of idr table where the obj is registered |
| 162 | * and handle has the id what user can see. |
| 163 | */ |
| 164 | ret = drm_gem_handle_create(file_priv, gem_obj, handle); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 165 | /* drop reference from allocate - handle holds it now. */ |
Thierry Reding | e6b6271 | 2017-02-28 15:46:41 +0100 | [diff] [blame] | 166 | drm_gem_object_put_unlocked(gem_obj); |
Chris Wilson | afe705b | 2016-05-31 22:25:52 +0100 | [diff] [blame] | 167 | if (ret) |
| 168 | return ERR_PTR(ret); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 169 | |
| 170 | return cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 173 | /** |
| 174 | * drm_gem_cma_free_object - free resources associated with a CMA GEM object |
| 175 | * @gem_obj: GEM object to free |
| 176 | * |
| 177 | * This function frees the backing memory of the CMA GEM object, cleans up the |
| 178 | * GEM object state and frees the memory used to store the object itself. |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 179 | * Drivers using the CMA helpers should set this as their |
| 180 | * &drm_driver.gem_free_object callback. |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 181 | */ |
| 182 | void drm_gem_cma_free_object(struct drm_gem_object *gem_obj) |
| 183 | { |
| 184 | struct drm_gem_cma_object *cma_obj; |
| 185 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 186 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 187 | |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 188 | if (cma_obj->vaddr) { |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 189 | dma_free_wc(gem_obj->dev->dev, cma_obj->base.size, |
| 190 | cma_obj->vaddr, cma_obj->paddr); |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 191 | } else if (gem_obj->import_attach) { |
| 192 | drm_prime_gem_destroy(gem_obj, cma_obj->sgt); |
| 193 | } |
Laurent Pinchart | a5ed894 | 2013-02-17 01:54:26 +0100 | [diff] [blame] | 194 | |
| 195 | drm_gem_object_release(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 196 | |
| 197 | kfree(cma_obj); |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(drm_gem_cma_free_object); |
| 200 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 201 | /** |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 202 | * drm_gem_cma_dumb_create_internal - create a dumb buffer object |
| 203 | * @file_priv: DRM file-private structure to create the dumb buffer for |
| 204 | * @drm: DRM device |
| 205 | * @args: IOCTL data |
| 206 | * |
| 207 | * This aligns the pitch and size arguments to the minimum required. This is |
| 208 | * an internal helper that can be wrapped by a driver to account for hardware |
| 209 | * with more specific alignment requirements. It should not be used directly |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 210 | * as their &drm_driver.dumb_create callback. |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 211 | * |
| 212 | * Returns: |
| 213 | * 0 on success or a negative error code on failure. |
| 214 | */ |
| 215 | int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv, |
| 216 | struct drm_device *drm, |
| 217 | struct drm_mode_create_dumb *args) |
| 218 | { |
| 219 | unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 220 | struct drm_gem_cma_object *cma_obj; |
| 221 | |
| 222 | if (args->pitch < min_pitch) |
| 223 | args->pitch = min_pitch; |
| 224 | |
| 225 | if (args->size < args->pitch * args->height) |
| 226 | args->size = args->pitch * args->height; |
| 227 | |
| 228 | cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size, |
| 229 | &args->handle); |
| 230 | return PTR_ERR_OR_ZERO(cma_obj); |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal); |
| 233 | |
| 234 | /** |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 235 | * drm_gem_cma_dumb_create - create a dumb buffer object |
| 236 | * @file_priv: DRM file-private structure to create the dumb buffer for |
| 237 | * @drm: DRM device |
| 238 | * @args: IOCTL data |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 239 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 240 | * This function computes the pitch of the dumb buffer and rounds it up to an |
| 241 | * integer number of bytes per pixel. Drivers for hardware that doesn't have |
| 242 | * any additional restrictions on the pitch can directly use this function as |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 243 | * their &drm_driver.dumb_create callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 244 | * |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 245 | * For hardware with additional restrictions, drivers can adjust the fields |
| 246 | * set up by userspace and pass the IOCTL data along to the |
| 247 | * drm_gem_cma_dumb_create_internal() function. |
| 248 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 249 | * Returns: |
| 250 | * 0 on success or a negative error code on failure. |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 251 | */ |
| 252 | int drm_gem_cma_dumb_create(struct drm_file *file_priv, |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 253 | struct drm_device *drm, |
| 254 | struct drm_mode_create_dumb *args) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 255 | { |
| 256 | struct drm_gem_cma_object *cma_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 257 | |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 258 | args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
| 259 | args->size = args->pitch * args->height; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 260 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 261 | cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size, |
| 262 | &args->handle); |
Sachin Kamat | b03cda5 | 2013-07-15 16:04:46 +0530 | [diff] [blame] | 263 | return PTR_ERR_OR_ZERO(cma_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 264 | } |
| 265 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create); |
| 266 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 267 | /** |
| 268 | * drm_gem_cma_dumb_map_offset - return the fake mmap offset for a CMA GEM |
| 269 | * object |
| 270 | * @file_priv: DRM file-private structure containing the GEM object |
| 271 | * @drm: DRM device |
| 272 | * @handle: GEM object handle |
| 273 | * @offset: return location for the fake mmap offset |
| 274 | * |
| 275 | * This function look up an object by its handle and returns the fake mmap |
| 276 | * offset associated with it. Drivers using the CMA helpers should set this |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 277 | * as their &drm_driver.dumb_map_offset callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 278 | * |
| 279 | * Returns: |
| 280 | * 0 on success or a negative error code on failure. |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 281 | */ |
| 282 | int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv, |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 283 | struct drm_device *drm, u32 handle, |
| 284 | u64 *offset) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 285 | { |
| 286 | struct drm_gem_object *gem_obj; |
| 287 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 288 | gem_obj = drm_gem_object_lookup(file_priv, handle); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 289 | if (!gem_obj) { |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 290 | dev_err(drm->dev, "failed to lookup GEM object\n"); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | } |
| 293 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 294 | *offset = drm_vma_node_offset_addr(&gem_obj->vma_node); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 295 | |
Thierry Reding | e6b6271 | 2017-02-28 15:46:41 +0100 | [diff] [blame] | 296 | drm_gem_object_put_unlocked(gem_obj); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset); |
| 301 | |
| 302 | const struct vm_operations_struct drm_gem_cma_vm_ops = { |
| 303 | .open = drm_gem_vm_open, |
| 304 | .close = drm_gem_vm_close, |
| 305 | }; |
| 306 | EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops); |
| 307 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 308 | static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj, |
| 309 | struct vm_area_struct *vma) |
| 310 | { |
| 311 | int ret; |
| 312 | |
Laurent Pinchart | b65e64f | 2014-03-02 20:09:48 +0100 | [diff] [blame] | 313 | /* |
| 314 | * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the |
| 315 | * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map |
| 316 | * the whole buffer. |
| 317 | */ |
| 318 | vma->vm_flags &= ~VM_PFNMAP; |
| 319 | vma->vm_pgoff = 0; |
| 320 | |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 321 | ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr, |
| 322 | cma_obj->paddr, vma->vm_end - vma->vm_start); |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 323 | if (ret) |
| 324 | drm_gem_vm_close(vma); |
| 325 | |
| 326 | return ret; |
| 327 | } |
| 328 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 329 | /** |
| 330 | * drm_gem_cma_mmap - memory-map a CMA GEM object |
| 331 | * @filp: file object |
| 332 | * @vma: VMA for the area to be mapped |
| 333 | * |
| 334 | * This function implements an augmented version of the GEM DRM file mmap |
| 335 | * operation for CMA objects: In addition to the usual GEM VMA setup it |
| 336 | * immediately faults in the entire object instead of using on-demaind |
| 337 | * faulting. Drivers which employ the CMA helpers should use this function |
| 338 | * as their ->mmap() handler in the DRM device file's file_operations |
| 339 | * structure. |
| 340 | * |
Daniel Vetter | d55f7e5 | 2017-03-08 15:12:56 +0100 | [diff] [blame] | 341 | * Instead of directly referencing this function, drivers should use the |
| 342 | * DEFINE_DRM_GEM_CMA_FOPS().macro. |
| 343 | * |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 344 | * Returns: |
| 345 | * 0 on success or a negative error code on failure. |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 346 | */ |
| 347 | int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma) |
| 348 | { |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 349 | struct drm_gem_cma_object *cma_obj; |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 350 | struct drm_gem_object *gem_obj; |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 351 | int ret; |
| 352 | |
| 353 | ret = drm_gem_mmap(filp, vma); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | |
| 357 | gem_obj = vma->vm_private_data; |
| 358 | cma_obj = to_drm_gem_cma_obj(gem_obj); |
| 359 | |
Laurent Pinchart | ebaf9e0 | 2013-04-16 14:32:34 +0200 | [diff] [blame] | 360 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 361 | } |
| 362 | EXPORT_SYMBOL_GPL(drm_gem_cma_mmap); |
| 363 | |
Benjamin Gaignard | 62a0d98 | 2017-01-04 10:12:57 +0100 | [diff] [blame] | 364 | #ifndef CONFIG_MMU |
| 365 | /** |
| 366 | * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases |
| 367 | * @filp: file object |
| 368 | * @addr: memory address |
| 369 | * @len: buffer size |
| 370 | * @pgoff: page offset |
| 371 | * @flags: memory flags |
| 372 | * |
| 373 | * This function is used in noMMU platforms to propose address mapping |
| 374 | * for a given buffer. |
| 375 | * It's intended to be used as a direct handler for the struct |
| 376 | * &file_operations.get_unmapped_area operation. |
| 377 | * |
| 378 | * Returns: |
| 379 | * mapping address on success or a negative error code on failure. |
| 380 | */ |
| 381 | unsigned long drm_gem_cma_get_unmapped_area(struct file *filp, |
| 382 | unsigned long addr, |
| 383 | unsigned long len, |
| 384 | unsigned long pgoff, |
| 385 | unsigned long flags) |
| 386 | { |
| 387 | struct drm_gem_cma_object *cma_obj; |
| 388 | struct drm_gem_object *obj = NULL; |
| 389 | struct drm_file *priv = filp->private_data; |
| 390 | struct drm_device *dev = priv->minor->dev; |
| 391 | struct drm_vma_offset_node *node; |
| 392 | |
| 393 | if (drm_device_is_unplugged(dev)) |
| 394 | return -ENODEV; |
| 395 | |
| 396 | drm_vma_offset_lock_lookup(dev->vma_offset_manager); |
| 397 | node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, |
| 398 | pgoff, |
| 399 | len >> PAGE_SHIFT); |
| 400 | if (likely(node)) { |
| 401 | obj = container_of(node, struct drm_gem_object, vma_node); |
| 402 | /* |
| 403 | * When the object is being freed, after it hits 0-refcnt it |
| 404 | * proceeds to tear down the object. In the process it will |
| 405 | * attempt to remove the VMA offset and so acquire this |
| 406 | * mgr->vm_lock. Therefore if we find an object with a 0-refcnt |
| 407 | * that matches our range, we know it is in the process of being |
| 408 | * destroyed and will be freed as soon as we release the lock - |
| 409 | * so we have to check for the 0-refcnted object and treat it as |
| 410 | * invalid. |
| 411 | */ |
| 412 | if (!kref_get_unless_zero(&obj->refcount)) |
| 413 | obj = NULL; |
| 414 | } |
| 415 | |
| 416 | drm_vma_offset_unlock_lookup(dev->vma_offset_manager); |
| 417 | |
| 418 | if (!obj) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | if (!drm_vma_node_is_allowed(node, priv)) { |
Thierry Reding | e6b6271 | 2017-02-28 15:46:41 +0100 | [diff] [blame] | 422 | drm_gem_object_put_unlocked(obj); |
Benjamin Gaignard | 62a0d98 | 2017-01-04 10:12:57 +0100 | [diff] [blame] | 423 | return -EACCES; |
| 424 | } |
| 425 | |
| 426 | cma_obj = to_drm_gem_cma_obj(obj); |
| 427 | |
Thierry Reding | e6b6271 | 2017-02-28 15:46:41 +0100 | [diff] [blame] | 428 | drm_gem_object_put_unlocked(obj); |
Benjamin Gaignard | 62a0d98 | 2017-01-04 10:12:57 +0100 | [diff] [blame] | 429 | |
| 430 | return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL; |
| 431 | } |
| 432 | EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area); |
| 433 | #endif |
| 434 | |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 435 | #ifdef CONFIG_DEBUG_FS |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 436 | /** |
| 437 | * drm_gem_cma_describe - describe a CMA GEM object for debugfs |
| 438 | * @cma_obj: CMA GEM object |
| 439 | * @m: debugfs file handle |
| 440 | * |
| 441 | * This function can be used to dump a human-readable representation of the |
| 442 | * CMA GEM object into a synthetic file. |
| 443 | */ |
| 444 | void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, |
| 445 | struct seq_file *m) |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 446 | { |
| 447 | struct drm_gem_object *obj = &cma_obj->base; |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 448 | uint64_t off; |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 449 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 450 | off = drm_vma_node_start(&obj->vma_node); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 451 | |
Magnus Damm | 30c4cf3 | 2015-05-04 16:16:19 +0200 | [diff] [blame] | 452 | seq_printf(m, "%2d (%2d) %08llx %pad %p %zu", |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 453 | obj->name, kref_read(&obj->refcount), |
Laurent Pinchart | f4d1b02 | 2014-03-04 19:10:17 +0100 | [diff] [blame] | 454 | off, &cma_obj->paddr, cma_obj->vaddr, obj->size); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 455 | |
| 456 | seq_printf(m, "\n"); |
| 457 | } |
| 458 | EXPORT_SYMBOL_GPL(drm_gem_cma_describe); |
| 459 | #endif |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 460 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 461 | /** |
| 462 | * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned |
| 463 | * pages for a CMA GEM object |
| 464 | * @obj: GEM object |
| 465 | * |
| 466 | * This function exports a scatter/gather table suitable for PRIME usage by |
| 467 | * calling the standard DMA mapping API. Drivers using the CMA helpers should |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 468 | * set this as their &drm_driver.gem_prime_get_sg_table callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 469 | * |
| 470 | * Returns: |
| 471 | * A pointer to the scatter/gather table of pinned pages or NULL on failure. |
| 472 | */ |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 473 | struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj) |
| 474 | { |
| 475 | struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); |
| 476 | struct sg_table *sgt; |
| 477 | int ret; |
| 478 | |
| 479 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 480 | if (!sgt) |
| 481 | return NULL; |
| 482 | |
| 483 | ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr, |
| 484 | cma_obj->paddr, obj->size); |
| 485 | if (ret < 0) |
| 486 | goto out; |
| 487 | |
| 488 | return sgt; |
| 489 | |
| 490 | out: |
| 491 | kfree(sgt); |
| 492 | return NULL; |
| 493 | } |
| 494 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table); |
| 495 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 496 | /** |
| 497 | * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another |
| 498 | * driver's scatter/gather table of pinned pages |
| 499 | * @dev: device to import into |
| 500 | * @attach: DMA-BUF attachment |
| 501 | * @sgt: scatter/gather table of pinned pages |
| 502 | * |
| 503 | * This function imports a scatter/gather table exported via DMA-BUF by |
| 504 | * another driver. Imported buffers must be physically contiguous in memory |
| 505 | * (i.e. the scatter/gather table must contain a single entry). Drivers that |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 506 | * use the CMA helpers should set this as their |
| 507 | * &drm_driver.gem_prime_import_sg_table callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 508 | * |
| 509 | * Returns: |
| 510 | * A pointer to a newly created GEM object or an ERR_PTR-encoded negative |
| 511 | * error code on failure. |
| 512 | */ |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 513 | struct drm_gem_object * |
Maarten Lankhorst | b5e9c1a | 2014-01-09 11:03:14 +0100 | [diff] [blame] | 514 | drm_gem_cma_prime_import_sg_table(struct drm_device *dev, |
| 515 | struct dma_buf_attachment *attach, |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 516 | struct sg_table *sgt) |
| 517 | { |
| 518 | struct drm_gem_cma_object *cma_obj; |
| 519 | |
| 520 | if (sgt->nents != 1) |
| 521 | return ERR_PTR(-EINVAL); |
| 522 | |
| 523 | /* Create a CMA GEM buffer. */ |
Maarten Lankhorst | b5e9c1a | 2014-01-09 11:03:14 +0100 | [diff] [blame] | 524 | cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 525 | if (IS_ERR(cma_obj)) |
Sachin Kamat | e7c3634 | 2014-07-03 17:33:33 +0530 | [diff] [blame] | 526 | return ERR_CAST(cma_obj); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 527 | |
| 528 | cma_obj->paddr = sg_dma_address(sgt->sgl); |
| 529 | cma_obj->sgt = sgt; |
| 530 | |
Maarten Lankhorst | b5e9c1a | 2014-01-09 11:03:14 +0100 | [diff] [blame] | 531 | DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 532 | |
| 533 | return &cma_obj->base; |
| 534 | } |
| 535 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table); |
| 536 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 537 | /** |
| 538 | * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object |
| 539 | * @obj: GEM object |
| 540 | * @vma: VMA for the area to be mapped |
| 541 | * |
| 542 | * This function maps a buffer imported via DRM PRIME into a userspace |
| 543 | * process's address space. Drivers that use the CMA helpers should set this |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 544 | * as their &drm_driver.gem_prime_mmap callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 545 | * |
| 546 | * Returns: |
| 547 | * 0 on success or a negative error code on failure. |
| 548 | */ |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 549 | int drm_gem_cma_prime_mmap(struct drm_gem_object *obj, |
| 550 | struct vm_area_struct *vma) |
| 551 | { |
| 552 | struct drm_gem_cma_object *cma_obj; |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 553 | int ret; |
| 554 | |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 555 | ret = drm_gem_mmap_obj(obj, obj->size, vma); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 556 | if (ret < 0) |
| 557 | return ret; |
| 558 | |
| 559 | cma_obj = to_drm_gem_cma_obj(obj); |
| 560 | return drm_gem_cma_mmap_obj(cma_obj, vma); |
| 561 | } |
| 562 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap); |
| 563 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 564 | /** |
| 565 | * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual |
| 566 | * address space |
| 567 | * @obj: GEM object |
| 568 | * |
| 569 | * This function maps a buffer exported via DRM PRIME into the kernel's |
| 570 | * virtual address space. Since the CMA buffers are already mapped into the |
| 571 | * kernel virtual address space this simply returns the cached virtual |
| 572 | * address. Drivers using the CMA helpers should set this as their DRM |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 573 | * driver's &drm_driver.gem_prime_vmap callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 574 | * |
| 575 | * Returns: |
| 576 | * The kernel virtual address of the CMA GEM object's backing store. |
| 577 | */ |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 578 | void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj) |
| 579 | { |
| 580 | struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); |
| 581 | |
| 582 | return cma_obj->vaddr; |
| 583 | } |
| 584 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap); |
| 585 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 586 | /** |
| 587 | * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual |
| 588 | * address space |
| 589 | * @obj: GEM object |
| 590 | * @vaddr: kernel virtual address where the CMA GEM object was mapped |
| 591 | * |
| 592 | * This function removes a buffer exported via DRM PRIME from the kernel's |
| 593 | * virtual address space. This is a no-op because CMA buffers cannot be |
| 594 | * unmapped from kernel space. Drivers using the CMA helpers should set this |
Daniel Vetter | 421242a | 2016-12-29 21:48:34 +0100 | [diff] [blame] | 595 | * as their &drm_driver.gem_prime_vunmap callback. |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 596 | */ |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 597 | void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 598 | { |
| 599 | /* Nothing to do */ |
| 600 | } |
| 601 | EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap); |