blob: 512078ebd97b92f143d63f2af16fbe79ea6c7554 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/uaccess.h>
32#include <linux/fs.h>
33#include <linux/file.h>
34#include <linux/module.h>
35#include <linux/mman.h>
36#include <linux/pagemap.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070037#include <linux/shmem_fs.h>
Dave Airlie32488772011-11-25 15:21:02 +000038#include <linux/dma-buf.h>
Tom Lendacky95cf9262017-07-17 16:10:26 -050039#include <linux/mem_encrypt.h>
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020041#include <drm/drm_vma_manager.h>
Daniel Vetterd9fc9412014-09-23 15:46:53 +020042#include <drm/drm_gem.h>
Noralf Trønnes45d58b42017-11-07 20:13:40 +010043#include <drm/drm_print.h>
Daniel Vetter67d0ec42014-09-10 12:43:53 +020044#include "drm_internal.h"
Eric Anholt673a3942008-07-30 12:06:12 -070045
46/** @file drm_gem.c
47 *
48 * This file provides some of the base ioctls and library routines for
49 * the graphics memory manager implemented by each device driver.
50 *
51 * Because various devices have different requirements in terms of
52 * synchronization and migration strategies, implementing that is left up to
53 * the driver, and all that the general API provides should be generic --
54 * allocating objects, reading/writing data with the cpu, freeing objects.
55 * Even there, platform-dependent optimizations for reading/writing data with
56 * the CPU mean we'll likely hook those out to driver-specific calls. However,
57 * the DRI2 implementation wants to have at least allocate/mmap be generic.
58 *
59 * The goal was to have swap-backed object allocation managed through
60 * struct file. However, file descriptors as handles to a struct file have
61 * two major failings:
62 * - Process limits prevent more than 1024 or so being used at a time by
63 * default.
64 * - Inability to allocate high fds will aggravate the X Server's select()
65 * handling, and likely that of many GL client applications as well.
66 *
67 * This led to a plan of using our own integer IDs (called handles, following
68 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
69 * ioctls. The objects themselves will still include the struct file so
70 * that we can transition to fds if the required kernel infrastructure shows
71 * up at a later date, and as our interface with shmfs for memory allocation.
72 */
73
Jesse Barnesa2c0a972008-11-05 10:31:53 -080074/*
75 * We make up offsets for buffer objects so we can recognize them at
76 * mmap time.
77 */
Jordan Crouse05269a32010-05-27 13:40:27 -060078
79/* pgoff in mmap is an unsigned long, so we need to make sure that
80 * the faked up offset will fit
81 */
82
83#if BITS_PER_LONG == 64
Jesse Barnesa2c0a972008-11-05 10:31:53 -080084#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
85#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
Jordan Crouse05269a32010-05-27 13:40:27 -060086#else
87#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
88#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
89#endif
Jesse Barnesa2c0a972008-11-05 10:31:53 -080090
Eric Anholt673a3942008-07-30 12:06:12 -070091/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +010092 * drm_gem_init - Initialize the GEM device fields
93 * @dev: drm_devic structure to initialize
Eric Anholt673a3942008-07-30 12:06:12 -070094 */
Eric Anholt673a3942008-07-30 12:06:12 -070095int
96drm_gem_init(struct drm_device *dev)
97{
Daniel Vetterb04a5902013-12-11 14:24:46 +010098 struct drm_vma_offset_manager *vma_offset_manager;
Jesse Barnesa2c0a972008-11-05 10:31:53 -080099
Daniel Vettercd4f0132013-08-15 00:02:44 +0200100 mutex_init(&dev->object_name_lock);
Chris Wilsone86584c2018-02-12 14:55:33 +0000101 idr_init_base(&dev->object_name_idr, 1);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800102
Daniel Vetterb04a5902013-12-11 14:24:46 +0100103 vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
104 if (!vma_offset_manager) {
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800105 DRM_ERROR("out of memory\n");
106 return -ENOMEM;
107 }
108
Daniel Vetterb04a5902013-12-11 14:24:46 +0100109 dev->vma_offset_manager = vma_offset_manager;
110 drm_vma_offset_manager_init(vma_offset_manager,
David Herrmann0de23972013-07-24 21:07:52 +0200111 DRM_FILE_PAGE_OFFSET_START,
112 DRM_FILE_PAGE_OFFSET_SIZE);
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800113
Eric Anholt673a3942008-07-30 12:06:12 -0700114 return 0;
115}
116
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800117void
118drm_gem_destroy(struct drm_device *dev)
119{
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800120
Daniel Vetterb04a5902013-12-11 14:24:46 +0100121 drm_vma_offset_manager_destroy(dev->vma_offset_manager);
122 kfree(dev->vma_offset_manager);
123 dev->vma_offset_manager = NULL;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800124}
125
Eric Anholt673a3942008-07-30 12:06:12 -0700126/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100127 * drm_gem_object_init - initialize an allocated shmem-backed GEM object
128 * @dev: drm_device the object should be initialized for
129 * @obj: drm_gem_object to initialize
130 * @size: object size
131 *
Alan Cox62cb70112011-06-07 14:17:51 +0100132 * Initialize an already allocated GEM object of the specified size with
Daniel Vetter1d397042010-04-09 19:05:04 +0000133 * shmfs backing store.
134 */
135int drm_gem_object_init(struct drm_device *dev,
136 struct drm_gem_object *obj, size_t size)
137{
David Herrmann89c82332013-07-11 11:56:32 +0200138 struct file *filp;
Daniel Vetter1d397042010-04-09 19:05:04 +0000139
Daniel Vetter6ab11a22014-01-20 08:21:54 +0100140 drm_gem_private_object_init(dev, obj, size);
141
David Herrmann89c82332013-07-11 11:56:32 +0200142 filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
143 if (IS_ERR(filp))
144 return PTR_ERR(filp);
Daniel Vetter1d397042010-04-09 19:05:04 +0000145
David Herrmann89c82332013-07-11 11:56:32 +0200146 obj->filp = filp;
Daniel Vetter1d397042010-04-09 19:05:04 +0000147
Daniel Vetter1d397042010-04-09 19:05:04 +0000148 return 0;
149}
150EXPORT_SYMBOL(drm_gem_object_init);
151
152/**
Laurent Pinchart2a5706a2014-08-28 14:34:36 +0200153 * drm_gem_private_object_init - initialize an allocated private GEM object
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100154 * @dev: drm_device the object should be initialized for
155 * @obj: drm_gem_object to initialize
156 * @size: object size
157 *
Alan Cox62cb70112011-06-07 14:17:51 +0100158 * Initialize an already allocated GEM object of the specified size with
159 * no GEM provided backing store. Instead the caller is responsible for
160 * backing the object and handling it.
161 */
David Herrmann89c82332013-07-11 11:56:32 +0200162void drm_gem_private_object_init(struct drm_device *dev,
163 struct drm_gem_object *obj, size_t size)
Alan Cox62cb70112011-06-07 14:17:51 +0100164{
165 BUG_ON((size & (PAGE_SIZE - 1)) != 0);
166
167 obj->dev = dev;
168 obj->filp = NULL;
169
170 kref_init(&obj->refcount);
Daniel Vettera8e11d12013-08-15 00:02:37 +0200171 obj->handle_count = 0;
Alan Cox62cb70112011-06-07 14:17:51 +0100172 obj->size = size;
David Herrmann88d7ebe2013-08-25 18:28:57 +0200173 drm_vma_node_reset(&obj->vma_node);
Alan Cox62cb70112011-06-07 14:17:51 +0100174}
175EXPORT_SYMBOL(drm_gem_private_object_init);
176
Dave Airlie0ff926c2012-05-20 17:31:16 +0100177static void
178drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
179{
Daniel Vetter319c9332013-08-15 00:02:46 +0200180 /*
181 * Note: obj->dma_buf can't disappear as long as we still hold a
182 * handle reference in obj->handle_count.
183 */
Daniel Vetterd0b2c532013-08-15 00:02:49 +0200184 mutex_lock(&filp->prime.lock);
Daniel Vetter319c9332013-08-15 00:02:46 +0200185 if (obj->dma_buf) {
Daniel Vetterd0b2c532013-08-15 00:02:49 +0200186 drm_prime_remove_buf_handle_locked(&filp->prime,
187 obj->dma_buf);
Dave Airlie0ff926c2012-05-20 17:31:16 +0100188 }
Daniel Vetterd0b2c532013-08-15 00:02:49 +0200189 mutex_unlock(&filp->prime.lock);
Dave Airlie0ff926c2012-05-20 17:31:16 +0100190}
191
Daniel Vetter36da5902013-08-15 00:02:34 +0200192/**
Thierry Redingc6a84322014-10-02 14:45:55 +0200193 * drm_gem_object_handle_free - release resources bound to userspace handles
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100194 * @obj: GEM object to clean up.
195 *
Daniel Vetter36da5902013-08-15 00:02:34 +0200196 * Called after the last handle to the object has been closed
197 *
198 * Removes any name for the object. Note that this must be
199 * called before drm_gem_object_free or we'll be touching
200 * freed memory
201 */
202static void drm_gem_object_handle_free(struct drm_gem_object *obj)
203{
204 struct drm_device *dev = obj->dev;
205
206 /* Remove any name for this object */
Daniel Vetter36da5902013-08-15 00:02:34 +0200207 if (obj->name) {
208 idr_remove(&dev->object_name_idr, obj->name);
209 obj->name = 0;
Daniel Vettera8e11d12013-08-15 00:02:37 +0200210 }
Daniel Vetter36da5902013-08-15 00:02:34 +0200211}
212
Daniel Vetter319c9332013-08-15 00:02:46 +0200213static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
214{
215 /* Unbreak the reference cycle if we have an exported dma_buf. */
216 if (obj->dma_buf) {
217 dma_buf_put(obj->dma_buf);
218 obj->dma_buf = NULL;
219 }
220}
221
Daniel Vetterbecee2a2013-08-15 00:02:39 +0200222static void
Thierry Redinge6b62712017-02-28 15:46:41 +0100223drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
Daniel Vetter36da5902013-08-15 00:02:34 +0200224{
Chris Wilson98a88832016-01-04 10:11:00 +0000225 struct drm_device *dev = obj->dev;
226 bool final = false;
227
Daniel Vettera8e11d12013-08-15 00:02:37 +0200228 if (WARN_ON(obj->handle_count == 0))
Daniel Vetter36da5902013-08-15 00:02:34 +0200229 return;
230
231 /*
232 * Must bump handle count first as this may be the last
233 * ref, in which case the object would disappear before we
234 * checked for a name
235 */
236
Chris Wilson98a88832016-01-04 10:11:00 +0000237 mutex_lock(&dev->object_name_lock);
Daniel Vetter319c9332013-08-15 00:02:46 +0200238 if (--obj->handle_count == 0) {
Daniel Vetter36da5902013-08-15 00:02:34 +0200239 drm_gem_object_handle_free(obj);
Daniel Vetter319c9332013-08-15 00:02:46 +0200240 drm_gem_object_exported_dma_buf_free(obj);
Chris Wilson98a88832016-01-04 10:11:00 +0000241 final = true;
Daniel Vetter319c9332013-08-15 00:02:46 +0200242 }
Chris Wilson98a88832016-01-04 10:11:00 +0000243 mutex_unlock(&dev->object_name_lock);
Daniel Vettera8e11d12013-08-15 00:02:37 +0200244
Chris Wilson98a88832016-01-04 10:11:00 +0000245 if (final)
Thierry Redinge6b62712017-02-28 15:46:41 +0100246 drm_gem_object_put_unlocked(obj);
Daniel Vetter36da5902013-08-15 00:02:34 +0200247}
248
Chris Wilson8815b232016-01-05 09:42:31 +0000249/*
250 * Called at device or object close to release the file's
251 * handle references on objects.
252 */
253static int
254drm_gem_object_release_handle(int id, void *ptr, void *data)
255{
256 struct drm_file *file_priv = data;
257 struct drm_gem_object *obj = ptr;
258 struct drm_device *dev = obj->dev;
259
Chris Wilsond0a133f2017-08-19 13:05:58 +0100260 if (dev->driver->gem_close_object)
261 dev->driver->gem_close_object(obj, file_priv);
262
Chris Wilson8815b232016-01-05 09:42:31 +0000263 if (drm_core_check_feature(dev, DRIVER_PRIME))
264 drm_gem_remove_prime_handles(obj, file_priv);
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200265 drm_vma_node_revoke(&obj->vma_node, file_priv);
Chris Wilson8815b232016-01-05 09:42:31 +0000266
Thierry Redinge6b62712017-02-28 15:46:41 +0100267 drm_gem_object_handle_put_unlocked(obj);
Chris Wilson8815b232016-01-05 09:42:31 +0000268
269 return 0;
270}
271
Eric Anholt673a3942008-07-30 12:06:12 -0700272/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100273 * drm_gem_handle_delete - deletes the given file-private handle
274 * @filp: drm file-private structure to use for the handle look up
275 * @handle: userspace handle to delete
276 *
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200277 * Removes the GEM handle from the @filp lookup table which has been added with
278 * drm_gem_handle_create(). If this is the last handle also cleans up linked
279 * resources like GEM names.
Eric Anholt673a3942008-07-30 12:06:12 -0700280 */
Dave Airlieff72145b2011-02-07 12:16:14 +1000281int
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300282drm_gem_handle_delete(struct drm_file *filp, u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700283{
Eric Anholt673a3942008-07-30 12:06:12 -0700284 struct drm_gem_object *obj;
285
Eric Anholt673a3942008-07-30 12:06:12 -0700286 spin_lock(&filp->table_lock);
287
288 /* Check if we currently have a reference on the object */
Chris Wilsonf6cd7da2016-04-15 12:55:08 +0100289 obj = idr_replace(&filp->object_idr, NULL, handle);
290 spin_unlock(&filp->table_lock);
291 if (IS_ERR_OR_NULL(obj))
Eric Anholt673a3942008-07-30 12:06:12 -0700292 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700293
Chris Wilsonf6cd7da2016-04-15 12:55:08 +0100294 /* Release driver's reference and decrement refcount. */
295 drm_gem_object_release_handle(handle, obj, filp);
296
297 /* And finally make the handle available for future allocations. */
298 spin_lock(&filp->table_lock);
Eric Anholt673a3942008-07-30 12:06:12 -0700299 idr_remove(&filp->object_idr, handle);
300 spin_unlock(&filp->table_lock);
301
Eric Anholt673a3942008-07-30 12:06:12 -0700302 return 0;
303}
Dave Airlieff72145b2011-02-07 12:16:14 +1000304EXPORT_SYMBOL(drm_gem_handle_delete);
Eric Anholt673a3942008-07-30 12:06:12 -0700305
306/**
Noralf Trønnesdb611522017-07-23 21:16:17 +0200307 * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
308 * @file: drm file-private structure containing the gem object
309 * @dev: corresponding drm_device
310 * @handle: gem object handle
311 * @offset: return location for the fake mmap offset
312 *
313 * This implements the &drm_driver.dumb_map_offset kms driver callback for
314 * drivers which use gem to manage their backing storage.
315 *
316 * Returns:
317 * 0 on success or a negative error code on failure.
318 */
319int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
320 u32 handle, u64 *offset)
321{
322 struct drm_gem_object *obj;
323 int ret;
324
325 obj = drm_gem_object_lookup(file, handle);
326 if (!obj)
327 return -ENOENT;
328
Noralf Trønnes90378e52017-08-17 18:21:30 +0200329 /* Don't allow imported objects to be mapped */
330 if (obj->import_attach) {
331 ret = -EINVAL;
332 goto out;
333 }
334
Noralf Trønnesdb611522017-07-23 21:16:17 +0200335 ret = drm_gem_create_mmap_offset(obj);
336 if (ret)
337 goto out;
338
339 *offset = drm_vma_node_offset_addr(&obj->vma_node);
340out:
341 drm_gem_object_put_unlocked(obj);
342
343 return ret;
344}
345EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
346
347/**
Daniel Vetter43387b32013-07-16 09:12:04 +0200348 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100349 * @file: drm file-private structure to remove the dumb handle from
350 * @dev: corresponding drm_device
351 * @handle: the dumb handle to remove
Noralf Trønnes1dd3a0602017-10-26 18:57:26 +0200352 *
Daniel Vetter940eba22017-01-25 07:26:46 +0100353 * This implements the &drm_driver.dumb_destroy kms driver callback for drivers
354 * which use gem to manage their backing storage.
Daniel Vetter43387b32013-07-16 09:12:04 +0200355 */
356int drm_gem_dumb_destroy(struct drm_file *file,
357 struct drm_device *dev,
358 uint32_t handle)
359{
360 return drm_gem_handle_delete(file, handle);
361}
362EXPORT_SYMBOL(drm_gem_dumb_destroy);
363
364/**
Daniel Vetter20228c42013-08-15 00:02:45 +0200365 * drm_gem_handle_create_tail - internal functions to create a handle
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100366 * @file_priv: drm file-private structure to register the handle for
367 * @obj: object to register
Thierry Reding8bf81802014-11-03 13:20:52 +0100368 * @handlep: pointer to return the created handle to the caller
Noralf Trønnes1dd3a0602017-10-26 18:57:26 +0200369 *
Daniel Vetter940eba22017-01-25 07:26:46 +0100370 * This expects the &drm_device.object_name_lock to be held already and will
371 * drop it before returning. Used to avoid races in establishing new handles
372 * when importing an object from either an flink name or a dma-buf.
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200373 *
374 * Handles must be release again through drm_gem_handle_delete(). This is done
375 * when userspace closes @file_priv for all attached handles, or through the
376 * GEM_CLOSE ioctl for individual handles.
Eric Anholt673a3942008-07-30 12:06:12 -0700377 */
378int
Daniel Vetter20228c42013-08-15 00:02:45 +0200379drm_gem_handle_create_tail(struct drm_file *file_priv,
380 struct drm_gem_object *obj,
381 u32 *handlep)
Eric Anholt673a3942008-07-30 12:06:12 -0700382{
Ben Skeggs304eda32011-06-09 00:24:59 +0000383 struct drm_device *dev = obj->dev;
Chris Wilson9649399e2016-01-05 09:42:30 +0000384 u32 handle;
Ben Skeggs304eda32011-06-09 00:24:59 +0000385 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700386
Daniel Vetter20228c42013-08-15 00:02:45 +0200387 WARN_ON(!mutex_is_locked(&dev->object_name_lock));
Chris Wilson98a88832016-01-04 10:11:00 +0000388 if (obj->handle_count++ == 0)
Thierry Redinge6b62712017-02-28 15:46:41 +0100389 drm_gem_object_get(obj);
Daniel Vetter20228c42013-08-15 00:02:45 +0200390
Eric Anholt673a3942008-07-30 12:06:12 -0700391 /*
Tejun Heo2e928812013-02-27 17:04:08 -0800392 * Get the user-visible handle using idr. Preload and perform
393 * allocation under our spinlock.
Eric Anholt673a3942008-07-30 12:06:12 -0700394 */
Tejun Heo2e928812013-02-27 17:04:08 -0800395 idr_preload(GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -0700396 spin_lock(&file_priv->table_lock);
Tejun Heo2e928812013-02-27 17:04:08 -0800397
398 ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
Chris Wilson98a88832016-01-04 10:11:00 +0000399
Eric Anholt673a3942008-07-30 12:06:12 -0700400 spin_unlock(&file_priv->table_lock);
Tejun Heo2e928812013-02-27 17:04:08 -0800401 idr_preload_end();
Chris Wilson98a88832016-01-04 10:11:00 +0000402
Daniel Vettercd4f0132013-08-15 00:02:44 +0200403 mutex_unlock(&dev->object_name_lock);
Chris Wilson69841282016-01-04 10:10:59 +0000404 if (ret < 0)
405 goto err_unref;
406
Chris Wilson9649399e2016-01-05 09:42:30 +0000407 handle = ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700408
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200409 ret = drm_vma_node_allow(&obj->vma_node, file_priv);
Chris Wilson69841282016-01-04 10:10:59 +0000410 if (ret)
411 goto err_remove;
Ben Skeggs304eda32011-06-09 00:24:59 +0000412
413 if (dev->driver->gem_open_object) {
414 ret = dev->driver->gem_open_object(obj, file_priv);
Chris Wilson69841282016-01-04 10:10:59 +0000415 if (ret)
416 goto err_revoke;
Ben Skeggs304eda32011-06-09 00:24:59 +0000417 }
418
Chris Wilson9649399e2016-01-05 09:42:30 +0000419 *handlep = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700420 return 0;
Chris Wilson69841282016-01-04 10:10:59 +0000421
422err_revoke:
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200423 drm_vma_node_revoke(&obj->vma_node, file_priv);
Chris Wilson69841282016-01-04 10:10:59 +0000424err_remove:
425 spin_lock(&file_priv->table_lock);
Chris Wilson9649399e2016-01-05 09:42:30 +0000426 idr_remove(&file_priv->object_idr, handle);
Chris Wilson69841282016-01-04 10:10:59 +0000427 spin_unlock(&file_priv->table_lock);
428err_unref:
Thierry Redinge6b62712017-02-28 15:46:41 +0100429 drm_gem_object_handle_put_unlocked(obj);
Chris Wilson69841282016-01-04 10:10:59 +0000430 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700431}
Daniel Vetter20228c42013-08-15 00:02:45 +0200432
433/**
Thierry Reding8bf81802014-11-03 13:20:52 +0100434 * drm_gem_handle_create - create a gem handle for an object
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100435 * @file_priv: drm file-private structure to register the handle for
436 * @obj: object to register
437 * @handlep: pionter to return the created handle to the caller
438 *
Daniel Vetter390311762018-03-22 09:02:33 +0100439 * Create a handle for this object. This adds a handle reference to the object,
440 * which includes a regular reference count. Callers will likely want to
441 * dereference the object afterwards.
442 *
443 * Since this publishes @obj to userspace it must be fully set up by this point,
444 * drivers must call this last in their buffer object creation callbacks.
Daniel Vetter20228c42013-08-15 00:02:45 +0200445 */
Thierry Reding8bf81802014-11-03 13:20:52 +0100446int drm_gem_handle_create(struct drm_file *file_priv,
447 struct drm_gem_object *obj,
448 u32 *handlep)
Daniel Vetter20228c42013-08-15 00:02:45 +0200449{
450 mutex_lock(&obj->dev->object_name_lock);
451
452 return drm_gem_handle_create_tail(file_priv, obj, handlep);
453}
Eric Anholt673a3942008-07-30 12:06:12 -0700454EXPORT_SYMBOL(drm_gem_handle_create);
455
Rob Clark75ef8b32011-08-10 08:09:07 -0500456
457/**
458 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
459 * @obj: obj in question
460 *
461 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
Daniel Vetterf74418a2016-03-30 11:40:52 +0200462 *
463 * Note that drm_gem_object_release() already calls this function, so drivers
464 * don't have to take care of releasing the mmap offset themselves when freeing
465 * the GEM object.
Rob Clark75ef8b32011-08-10 08:09:07 -0500466 */
467void
468drm_gem_free_mmap_offset(struct drm_gem_object *obj)
469{
470 struct drm_device *dev = obj->dev;
Rob Clark75ef8b32011-08-10 08:09:07 -0500471
Daniel Vetterb04a5902013-12-11 14:24:46 +0100472 drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
Rob Clark75ef8b32011-08-10 08:09:07 -0500473}
474EXPORT_SYMBOL(drm_gem_free_mmap_offset);
475
476/**
Rob Clark367bbd42013-08-07 13:41:23 -0400477 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
478 * @obj: obj in question
479 * @size: the virtual size
480 *
481 * GEM memory mapping works by handing back to userspace a fake mmap offset
482 * it can use in a subsequent mmap(2) call. The DRM core code then looks
483 * up the object based on the offset and sets up the various memory mapping
484 * structures.
485 *
486 * This routine allocates and attaches a fake offset for @obj, in cases where
Daniel Vetter940eba22017-01-25 07:26:46 +0100487 * the virtual size differs from the physical size (ie. &drm_gem_object.size).
488 * Otherwise just use drm_gem_create_mmap_offset().
Daniel Vetterf74418a2016-03-30 11:40:52 +0200489 *
490 * This function is idempotent and handles an already allocated mmap offset
491 * transparently. Drivers do not need to check for this case.
Rob Clark367bbd42013-08-07 13:41:23 -0400492 */
493int
494drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
495{
496 struct drm_device *dev = obj->dev;
Rob Clark367bbd42013-08-07 13:41:23 -0400497
Daniel Vetterb04a5902013-12-11 14:24:46 +0100498 return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
Rob Clark367bbd42013-08-07 13:41:23 -0400499 size / PAGE_SIZE);
500}
501EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
502
503/**
Rob Clark75ef8b32011-08-10 08:09:07 -0500504 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
505 * @obj: obj in question
506 *
507 * GEM memory mapping works by handing back to userspace a fake mmap offset
508 * it can use in a subsequent mmap(2) call. The DRM core code then looks
509 * up the object based on the offset and sets up the various memory mapping
510 * structures.
511 *
512 * This routine allocates and attaches a fake offset for @obj.
Daniel Vetterf74418a2016-03-30 11:40:52 +0200513 *
514 * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
515 * the fake offset again.
Rob Clark75ef8b32011-08-10 08:09:07 -0500516 */
Rob Clark367bbd42013-08-07 13:41:23 -0400517int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
Rob Clark75ef8b32011-08-10 08:09:07 -0500518{
Rob Clark367bbd42013-08-07 13:41:23 -0400519 return drm_gem_create_mmap_offset_size(obj, obj->size);
Rob Clark75ef8b32011-08-10 08:09:07 -0500520}
521EXPORT_SYMBOL(drm_gem_create_mmap_offset);
522
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400523/**
524 * drm_gem_get_pages - helper to allocate backing pages for a GEM object
525 * from shmem
526 * @obj: obj in question
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200527 *
528 * This reads the page-array of the shmem-backing storage of the given gem
529 * object. An array of pages is returned. If a page is not allocated or
530 * swapped-out, this will allocate/swap-in the required pages. Note that the
531 * whole object is covered by the page-array and pinned in memory.
532 *
533 * Use drm_gem_put_pages() to release the array and unpin all pages.
534 *
535 * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
536 * If you require other GFP-masks, you have to do those allocations yourself.
537 *
538 * Note that you are not allowed to change gfp-zones during runtime. That is,
539 * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
540 * set during initialization. If you have special zone constraints, set them
Jordan Crouse5b9fbff2017-10-03 09:38:10 -0600541 * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200542 * to keep pages in the required zone during swap-in.
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400543 */
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200544struct page **drm_gem_get_pages(struct drm_gem_object *obj)
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400545{
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400546 struct address_space *mapping;
547 struct page *p, **pages;
548 int i, npages;
549
550 /* This is the shared memory object that backs the GEM resource */
Al Viro93c76a32015-12-04 23:45:44 -0500551 mapping = obj->filp->f_mapping;
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400552
553 /* We already BUG_ON() for non-page-aligned sizes in
554 * drm_gem_object_init(), so we should never hit this unless
555 * driver author is doing something really wrong:
556 */
557 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
558
559 npages = obj->size >> PAGE_SHIFT;
560
Michal Hocko20981052017-05-17 14:23:12 +0200561 pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400562 if (pages == NULL)
563 return ERR_PTR(-ENOMEM);
564
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400565 for (i = 0; i < npages; i++) {
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200566 p = shmem_read_mapping_page(mapping, i);
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400567 if (IS_ERR(p))
568 goto fail;
569 pages[i] = p;
570
David Herrmann21230002014-05-25 14:34:08 +0200571 /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
572 * correct region during swapin. Note that this requires
573 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
574 * so shmem can relocate pages during swapin if required.
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400575 */
Michal Hockoc62d2552015-11-06 16:28:49 -0800576 BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400577 (page_to_pfn(p) >= 0x00100000UL));
578 }
579
580 return pages;
581
582fail:
583 while (i--)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300584 put_page(pages[i]);
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400585
Michal Hocko20981052017-05-17 14:23:12 +0200586 kvfree(pages);
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400587 return ERR_CAST(p);
588}
589EXPORT_SYMBOL(drm_gem_get_pages);
590
591/**
592 * drm_gem_put_pages - helper to free backing pages for a GEM object
593 * @obj: obj in question
594 * @pages: pages to free
595 * @dirty: if true, pages will be marked as dirty
596 * @accessed: if true, the pages will be marked as accessed
597 */
598void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
599 bool dirty, bool accessed)
600{
601 int i, npages;
602
603 /* We already BUG_ON() for non-page-aligned sizes in
604 * drm_gem_object_init(), so we should never hit this unless
605 * driver author is doing something really wrong:
606 */
607 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
608
609 npages = obj->size >> PAGE_SHIFT;
610
611 for (i = 0; i < npages; i++) {
612 if (dirty)
613 set_page_dirty(pages[i]);
614
615 if (accessed)
616 mark_page_accessed(pages[i]);
617
618 /* Undo the reference we took when populating the table */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 put_page(pages[i]);
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400620 }
621
Michal Hocko20981052017-05-17 14:23:12 +0200622 kvfree(pages);
Rob Clarkbcc5c9d2013-08-07 13:41:24 -0400623}
624EXPORT_SYMBOL(drm_gem_put_pages);
625
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200626/**
627 * drm_gem_object_lookup - look up a GEM object from it's handle
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200628 * @filp: DRM file private date
629 * @handle: userspace handle
630 *
631 * Returns:
632 *
633 * A reference to the object named by the handle if such exists on @filp, NULL
634 * otherwise.
635 */
Eric Anholt673a3942008-07-30 12:06:12 -0700636struct drm_gem_object *
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100637drm_gem_object_lookup(struct drm_file *filp, u32 handle)
Eric Anholt673a3942008-07-30 12:06:12 -0700638{
639 struct drm_gem_object *obj;
640
641 spin_lock(&filp->table_lock);
642
643 /* Check if we currently have a reference on the object */
644 obj = idr_find(&filp->object_idr, handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100645 if (obj)
Thierry Redinge6b62712017-02-28 15:46:41 +0100646 drm_gem_object_get(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700647
648 spin_unlock(&filp->table_lock);
649
650 return obj;
651}
652EXPORT_SYMBOL(drm_gem_object_lookup);
653
654/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100655 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
656 * @dev: drm_device
657 * @data: ioctl data
658 * @file_priv: drm file-private structure
659 *
Eric Anholt673a3942008-07-30 12:06:12 -0700660 * Releases the handle to an mm object.
661 */
662int
663drm_gem_close_ioctl(struct drm_device *dev, void *data,
664 struct drm_file *file_priv)
665{
666 struct drm_gem_close *args = data;
667 int ret;
668
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200669 if (!drm_core_check_feature(dev, DRIVER_GEM))
Chris Wilson69fdf422018-09-13 20:20:50 +0100670 return -EOPNOTSUPP;
Eric Anholt673a3942008-07-30 12:06:12 -0700671
672 ret = drm_gem_handle_delete(file_priv, args->handle);
673
674 return ret;
675}
676
677/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100678 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
679 * @dev: drm_device
680 * @data: ioctl data
681 * @file_priv: drm file-private structure
682 *
Eric Anholt673a3942008-07-30 12:06:12 -0700683 * Create a global name for an object, returning the name.
684 *
685 * Note that the name does not hold a reference; when the object
686 * is freed, the name goes away.
687 */
688int
689drm_gem_flink_ioctl(struct drm_device *dev, void *data,
690 struct drm_file *file_priv)
691{
692 struct drm_gem_flink *args = data;
693 struct drm_gem_object *obj;
694 int ret;
695
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200696 if (!drm_core_check_feature(dev, DRIVER_GEM))
Chris Wilson69fdf422018-09-13 20:20:50 +0100697 return -EOPNOTSUPP;
Eric Anholt673a3942008-07-30 12:06:12 -0700698
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100699 obj = drm_gem_object_lookup(file_priv, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -0700700 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100701 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700702
Daniel Vettercd4f0132013-08-15 00:02:44 +0200703 mutex_lock(&dev->object_name_lock);
Daniel Vettera8e11d12013-08-15 00:02:37 +0200704 /* prevent races with concurrent gem_close. */
705 if (obj->handle_count == 0) {
706 ret = -ENOENT;
707 goto err;
708 }
709
Chris Wilson8d59bae2009-02-11 14:26:28 +0000710 if (!obj->name) {
Chris Wilson0f646422016-01-04 10:11:01 +0000711 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
Tejun Heo2e928812013-02-27 17:04:08 -0800712 if (ret < 0)
Chris Wilson8d59bae2009-02-11 14:26:28 +0000713 goto err;
YoungJun Cho2e07fb22013-06-27 08:58:33 +0900714
715 obj->name = ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700716 }
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000717
YoungJun Cho2e07fb22013-06-27 08:58:33 +0900718 args->name = (uint64_t) obj->name;
719 ret = 0;
720
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000721err:
Daniel Vettercd4f0132013-08-15 00:02:44 +0200722 mutex_unlock(&dev->object_name_lock);
Thierry Redinge6b62712017-02-28 15:46:41 +0100723 drm_gem_object_put_unlocked(obj);
Chris Wilson3e49c4f2009-02-09 11:31:41 +0000724 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700725}
726
727/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100728 * drm_gem_open - implementation of the GEM_OPEN ioctl
729 * @dev: drm_device
730 * @data: ioctl data
731 * @file_priv: drm file-private structure
732 *
Eric Anholt673a3942008-07-30 12:06:12 -0700733 * Open an object using the global name, returning a handle and the size.
734 *
735 * This handle (of course) holds a reference to the object, so the object
736 * will not go away until the handle is deleted.
737 */
738int
739drm_gem_open_ioctl(struct drm_device *dev, void *data,
740 struct drm_file *file_priv)
741{
742 struct drm_gem_open *args = data;
743 struct drm_gem_object *obj;
744 int ret;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300745 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700746
Andrzej Hajda1bcecfa2014-09-30 16:49:56 +0200747 if (!drm_core_check_feature(dev, DRIVER_GEM))
Chris Wilson69fdf422018-09-13 20:20:50 +0100748 return -EOPNOTSUPP;
Eric Anholt673a3942008-07-30 12:06:12 -0700749
Daniel Vettercd4f0132013-08-15 00:02:44 +0200750 mutex_lock(&dev->object_name_lock);
Eric Anholt673a3942008-07-30 12:06:12 -0700751 obj = idr_find(&dev->object_name_idr, (int) args->name);
Daniel Vetter20228c42013-08-15 00:02:45 +0200752 if (obj) {
Thierry Redinge6b62712017-02-28 15:46:41 +0100753 drm_gem_object_get(obj);
Daniel Vetter20228c42013-08-15 00:02:45 +0200754 } else {
755 mutex_unlock(&dev->object_name_lock);
Eric Anholt673a3942008-07-30 12:06:12 -0700756 return -ENOENT;
Daniel Vetter20228c42013-08-15 00:02:45 +0200757 }
Eric Anholt673a3942008-07-30 12:06:12 -0700758
Daniel Vetter20228c42013-08-15 00:02:45 +0200759 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
760 ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
Thierry Redinge6b62712017-02-28 15:46:41 +0100761 drm_gem_object_put_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700762 if (ret)
763 return ret;
764
765 args->handle = handle;
766 args->size = obj->size;
767
768 return 0;
769}
770
771/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100772 * gem_gem_open - initalizes GEM file-private structures at devnode open time
773 * @dev: drm_device which is being opened by userspace
774 * @file_private: drm file-private structure to set up
775 *
Eric Anholt673a3942008-07-30 12:06:12 -0700776 * Called at device open time, sets up the structure for handling refcounting
777 * of mm objects.
778 */
779void
780drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
781{
Chris Wilsone86584c2018-02-12 14:55:33 +0000782 idr_init_base(&file_private->object_idr, 1);
Eric Anholt673a3942008-07-30 12:06:12 -0700783 spin_lock_init(&file_private->table_lock);
784}
785
Eric Anholt673a3942008-07-30 12:06:12 -0700786/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100787 * drm_gem_release - release file-private GEM resources
788 * @dev: drm_device which is being closed by userspace
789 * @file_private: drm file-private structure to clean up
790 *
Eric Anholt673a3942008-07-30 12:06:12 -0700791 * Called at close time when the filp is going away.
792 *
793 * Releases any remaining references on objects by this filp.
794 */
795void
796drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
797{
Eric Anholt673a3942008-07-30 12:06:12 -0700798 idr_for_each(&file_private->object_idr,
Ben Skeggs304eda32011-06-09 00:24:59 +0000799 &drm_gem_object_release_handle, file_private);
Eric Anholt673a3942008-07-30 12:06:12 -0700800 idr_destroy(&file_private->object_idr);
Eric Anholt673a3942008-07-30 12:06:12 -0700801}
802
Daniel Vetterf74418a2016-03-30 11:40:52 +0200803/**
804 * drm_gem_object_release - release GEM buffer object resources
805 * @obj: GEM buffer object
806 *
807 * This releases any structures and resources used by @obj and is the invers of
808 * drm_gem_object_init().
809 */
Daniel Vetterfd632aa2010-04-09 19:05:05 +0000810void
811drm_gem_object_release(struct drm_gem_object *obj)
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000812{
Daniel Vetter319c9332013-08-15 00:02:46 +0200813 WARN_ON(obj->dma_buf);
814
Alan Cox62cb70112011-06-07 14:17:51 +0100815 if (obj->filp)
David Herrmann16d28312014-01-20 20:07:49 +0100816 fput(obj->filp);
David Herrmann77472342014-01-20 20:05:43 +0100817
818 drm_gem_free_mmap_offset(obj);
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000819}
Daniel Vetterfd632aa2010-04-09 19:05:05 +0000820EXPORT_SYMBOL(drm_gem_object_release);
Luca Barbieric3ae90c2010-02-09 05:49:11 +0000821
Eric Anholt673a3942008-07-30 12:06:12 -0700822/**
Daniel Vetter89d61fc2014-01-21 12:39:00 +0100823 * drm_gem_object_free - free a GEM object
824 * @kref: kref of the object to free
825 *
Eric Anholt673a3942008-07-30 12:06:12 -0700826 * Called after the last reference to the object has been lost.
Daniel Vetter940eba22017-01-25 07:26:46 +0100827 * Must be called holding &drm_device.struct_mutex.
Eric Anholt673a3942008-07-30 12:06:12 -0700828 *
829 * Frees the object
830 */
831void
832drm_gem_object_free(struct kref *kref)
833{
Daniel Vetter6ff774b2015-10-15 09:36:26 +0200834 struct drm_gem_object *obj =
835 container_of(kref, struct drm_gem_object, refcount);
Eric Anholt673a3942008-07-30 12:06:12 -0700836 struct drm_device *dev = obj->dev;
837
Daniel Vetter6d3e7fd2016-05-04 14:10:44 +0200838 if (dev->driver->gem_free_object_unlocked) {
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200839 dev->driver->gem_free_object_unlocked(obj);
Daniel Vetter6d3e7fd2016-05-04 14:10:44 +0200840 } else if (dev->driver->gem_free_object) {
841 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
842
Eric Anholt673a3942008-07-30 12:06:12 -0700843 dev->driver->gem_free_object(obj);
Daniel Vetter6d3e7fd2016-05-04 14:10:44 +0200844 }
Eric Anholt673a3942008-07-30 12:06:12 -0700845}
846EXPORT_SYMBOL(drm_gem_object_free);
847
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200848/**
Thierry Redinge6b62712017-02-28 15:46:41 +0100849 * drm_gem_object_put_unlocked - drop a GEM buffer object reference
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200850 * @obj: GEM buffer object
851 *
852 * This releases a reference to @obj. Callers must not hold the
Daniel Vetter940eba22017-01-25 07:26:46 +0100853 * &drm_device.struct_mutex lock when calling this function.
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200854 *
Thierry Redinge6b62712017-02-28 15:46:41 +0100855 * See also __drm_gem_object_put().
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200856 */
857void
Thierry Redinge6b62712017-02-28 15:46:41 +0100858drm_gem_object_put_unlocked(struct drm_gem_object *obj)
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200859{
860 struct drm_device *dev;
861
862 if (!obj)
863 return;
864
865 dev = obj->dev;
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200866
Daniel Vetter3379c042017-07-15 11:53:28 +0200867 if (dev->driver->gem_free_object_unlocked) {
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200868 kref_put(&obj->refcount, drm_gem_object_free);
Daniel Vetter3379c042017-07-15 11:53:28 +0200869 } else {
870 might_lock(&dev->struct_mutex);
871 if (kref_put_mutex(&obj->refcount, drm_gem_object_free,
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200872 &dev->struct_mutex))
Daniel Vetter3379c042017-07-15 11:53:28 +0200873 mutex_unlock(&dev->struct_mutex);
874 }
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200875}
Thierry Redinge6b62712017-02-28 15:46:41 +0100876EXPORT_SYMBOL(drm_gem_object_put_unlocked);
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200877
878/**
Thierry Redinge6b62712017-02-28 15:46:41 +0100879 * drm_gem_object_put - release a GEM buffer object reference
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200880 * @obj: GEM buffer object
881 *
Daniel Vetter940eba22017-01-25 07:26:46 +0100882 * This releases a reference to @obj. Callers must hold the
883 * &drm_device.struct_mutex lock when calling this function, even when the
884 * driver doesn't use &drm_device.struct_mutex for anything.
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200885 *
886 * For drivers not encumbered with legacy locking use
Thierry Redinge6b62712017-02-28 15:46:41 +0100887 * drm_gem_object_put_unlocked() instead.
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200888 */
889void
Thierry Redinge6b62712017-02-28 15:46:41 +0100890drm_gem_object_put(struct drm_gem_object *obj)
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200891{
892 if (obj) {
893 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
894
895 kref_put(&obj->refcount, drm_gem_object_free);
896 }
897}
Thierry Redinge6b62712017-02-28 15:46:41 +0100898EXPORT_SYMBOL(drm_gem_object_put);
Daniel Vetter9f0ba532016-05-02 10:40:51 +0200899
900/**
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200901 * drm_gem_vm_open - vma->ops->open implementation for GEM
902 * @vma: VM area structure
903 *
904 * This function implements the #vm_operations_struct open() callback for GEM
905 * drivers. This must be used together with drm_gem_vm_close().
906 */
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800907void drm_gem_vm_open(struct vm_area_struct *vma)
908{
909 struct drm_gem_object *obj = vma->vm_private_data;
910
Thierry Redinge6b62712017-02-28 15:46:41 +0100911 drm_gem_object_get(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800912}
913EXPORT_SYMBOL(drm_gem_vm_open);
914
Daniel Vetterdf2e0902015-10-22 19:11:29 +0200915/**
916 * drm_gem_vm_close - vma->ops->close implementation for GEM
917 * @vma: VM area structure
918 *
919 * This function implements the #vm_operations_struct close() callback for GEM
920 * drivers. This must be used together with drm_gem_vm_open().
921 */
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800922void drm_gem_vm_close(struct vm_area_struct *vma)
923{
924 struct drm_gem_object *obj = vma->vm_private_data;
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800925
Thierry Redinge6b62712017-02-28 15:46:41 +0100926 drm_gem_object_put_unlocked(obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800927}
928EXPORT_SYMBOL(drm_gem_vm_close);
929
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200930/**
931 * drm_gem_mmap_obj - memory map a GEM object
932 * @obj: the GEM object to map
933 * @obj_size: the object size to be mapped, in bytes
934 * @vma: VMA for the area to be mapped
935 *
936 * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
937 * provided by the driver. Depending on their requirements, drivers can either
938 * provide a fault handler in their gem_vm_ops (in which case any accesses to
939 * the object will be trapped, to perform migration, GTT binding, surface
940 * register allocation, or performance monitoring), or mmap the buffer memory
941 * synchronously after calling drm_gem_mmap_obj.
942 *
943 * This function is mainly intended to implement the DMABUF mmap operation, when
944 * the GEM object is not looked up based on its fake offset. To implement the
945 * DRM mmap operation, drivers should use the drm_gem_mmap() function.
946 *
David Herrmannca481c92013-08-25 18:28:58 +0200947 * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
948 * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
949 * callers must verify access restrictions before calling this helper.
950 *
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200951 * Return 0 or success or -EINVAL if the object size is smaller than the VMA
952 * size, or if no gem_vm_ops are provided.
953 */
954int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
955 struct vm_area_struct *vma)
956{
957 struct drm_device *dev = obj->dev;
958
959 /* Check for valid size. */
960 if (obj_size < vma->vm_end - vma->vm_start)
961 return -EINVAL;
962
963 if (!dev->driver->gem_vm_ops)
964 return -EINVAL;
965
966 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
967 vma->vm_ops = dev->driver->gem_vm_ops;
968 vma->vm_private_data = obj;
David Herrmann16d28312014-01-20 20:07:49 +0100969 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
Tom Lendacky95cf9262017-07-17 16:10:26 -0500970 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200971
972 /* Take a ref for this mapping of the object, so that the fault
973 * handler can dereference the mmap offset's pointer to the object.
974 * This reference is cleaned up by the corresponding vm_close
975 * (which should happen whether the vma was created by this call, or
976 * by a vm_open due to mremap or partial unmap or whatever).
977 */
Thierry Redinge6b62712017-02-28 15:46:41 +0100978 drm_gem_object_get(obj);
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200979
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200980 return 0;
981}
982EXPORT_SYMBOL(drm_gem_mmap_obj);
Jesse Barnesab00b3e2009-02-11 14:01:46 -0800983
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800984/**
985 * drm_gem_mmap - memory map routine for GEM objects
986 * @filp: DRM file pointer
987 * @vma: VMA for the area to be mapped
988 *
989 * If a driver supports GEM object mapping, mmap calls on the DRM file
990 * descriptor will end up here.
991 *
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200992 * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800993 * contain the fake offset we created when the GTT map ioctl was called on
Laurent Pinchart1c5aafa2013-04-16 14:14:52 +0200994 * the object) and map it with a call to drm_gem_mmap_obj().
David Herrmannca481c92013-08-25 18:28:58 +0200995 *
996 * If the caller is not granted access to the buffer object, the mmap will fail
997 * with EACCES. Please see the vma manager for more information.
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800998 */
999int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
1000{
1001 struct drm_file *priv = filp->private_data;
1002 struct drm_device *dev = priv->minor->dev;
Daniel Vetter2225cfe2015-10-15 11:33:43 +02001003 struct drm_gem_object *obj = NULL;
David Herrmann0de23972013-07-24 21:07:52 +02001004 struct drm_vma_offset_node *node;
David Herrmanna8469aa2014-01-20 20:15:38 +01001005 int ret;
Jesse Barnesa2c0a972008-11-05 10:31:53 -08001006
Daniel Vetterc07dcd62017-08-02 13:56:02 +02001007 if (drm_dev_is_unplugged(dev))
Dave Airlie2c07a212012-02-20 14:18:07 +00001008 return -ENODEV;
1009
Daniel Vetter2225cfe2015-10-15 11:33:43 +02001010 drm_vma_offset_lock_lookup(dev->vma_offset_manager);
1011 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
1012 vma->vm_pgoff,
1013 vma_pages(vma));
1014 if (likely(node)) {
1015 obj = container_of(node, struct drm_gem_object, vma_node);
1016 /*
1017 * When the object is being freed, after it hits 0-refcnt it
1018 * proceeds to tear down the object. In the process it will
1019 * attempt to remove the VMA offset and so acquire this
1020 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
1021 * that matches our range, we know it is in the process of being
1022 * destroyed and will be freed as soon as we release the lock -
1023 * so we have to check for the 0-refcnted object and treat it as
1024 * invalid.
1025 */
1026 if (!kref_get_unless_zero(&obj->refcount))
1027 obj = NULL;
1028 }
1029 drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
Jesse Barnesa2c0a972008-11-05 10:31:53 -08001030
Daniel Vetter2225cfe2015-10-15 11:33:43 +02001031 if (!obj)
Daniel Vetter197633b2014-09-23 15:46:48 +02001032 return -EINVAL;
Daniel Vetter2225cfe2015-10-15 11:33:43 +02001033
David Herrmannd9a1f0b2016-09-01 14:48:33 +02001034 if (!drm_vma_node_is_allowed(node, priv)) {
Thierry Redinge6b62712017-02-28 15:46:41 +01001035 drm_gem_object_put_unlocked(obj);
David Herrmannca481c92013-08-25 18:28:58 +02001036 return -EACCES;
Jesse Barnesa2c0a972008-11-05 10:31:53 -08001037 }
1038
Chris Wilson3e977ac2018-07-12 19:53:13 +01001039 if (node->readonly) {
1040 if (vma->vm_flags & VM_WRITE) {
1041 drm_gem_object_put_unlocked(obj);
1042 return -EINVAL;
1043 }
1044
1045 vma->vm_flags &= ~VM_MAYWRITE;
1046 }
1047
Daniel Vetter2225cfe2015-10-15 11:33:43 +02001048 ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
1049 vma);
Jesse Barnesa2c0a972008-11-05 10:31:53 -08001050
Thierry Redinge6b62712017-02-28 15:46:41 +01001051 drm_gem_object_put_unlocked(obj);
Jesse Barnesa2c0a972008-11-05 10:31:53 -08001052
1053 return ret;
1054}
1055EXPORT_SYMBOL(drm_gem_mmap);
Noralf Trønnes45d58b42017-11-07 20:13:40 +01001056
1057void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
1058 const struct drm_gem_object *obj)
1059{
1060 drm_printf_indent(p, indent, "name=%d\n", obj->name);
1061 drm_printf_indent(p, indent, "refcount=%u\n",
1062 kref_read(&obj->refcount));
1063 drm_printf_indent(p, indent, "start=%08lx\n",
1064 drm_vma_node_start(&obj->vma_node));
1065 drm_printf_indent(p, indent, "size=%zu\n", obj->size);
1066 drm_printf_indent(p, indent, "imported=%s\n",
1067 obj->import_attach ? "yes" : "no");
1068
1069 if (obj->dev->driver->gem_print_info)
1070 obj->dev->driver->gem_print_info(p, indent, obj);
1071}