Subversion Repositories Kolibri OS

Rev

Rev 6937 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6937 Rev 7144
1
/*
1
/*
2
 * Copyright © 2008 Intel Corporation
2
 * Copyright © 2008 Intel Corporation
3
 *
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
10
 *
11
 * The above copyright notice and this permission notice (including the next
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
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
13
 * Software.
14
 *
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
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
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
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
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * IN THE SOFTWARE.
21
 * IN THE SOFTWARE.
22
 *
22
 *
23
 * Authors:
23
 * Authors:
24
 *    Eric Anholt 
24
 *    Eric Anholt 
25
 *
25
 *
26
 */
26
 */
27
 
27
 
28
#include 
28
#include 
29
#include 
29
#include 
30
#include 
30
#include 
31
#include 
31
#include 
32
#include 
32
#include 
33
#include 
33
#include 
34
#include 
34
#include 
35
#include 
35
#include 
36
#include 
36
#include 
37
#include 
37
#include 
38
#include 
38
#include 
39
#include 
39
#include 
40
#include "drm_internal.h"
40
#include "drm_internal.h"
41
 
41
 
42
/** @file drm_gem.c
42
/** @file drm_gem.c
43
 *
43
 *
44
 * This file provides some of the base ioctls and library routines for
44
 * This file provides some of the base ioctls and library routines for
45
 * the graphics memory manager implemented by each device driver.
45
 * the graphics memory manager implemented by each device driver.
46
 *
46
 *
47
 * Because various devices have different requirements in terms of
47
 * Because various devices have different requirements in terms of
48
 * synchronization and migration strategies, implementing that is left up to
48
 * synchronization and migration strategies, implementing that is left up to
49
 * the driver, and all that the general API provides should be generic --
49
 * the driver, and all that the general API provides should be generic --
50
 * allocating objects, reading/writing data with the cpu, freeing objects.
50
 * allocating objects, reading/writing data with the cpu, freeing objects.
51
 * Even there, platform-dependent optimizations for reading/writing data with
51
 * Even there, platform-dependent optimizations for reading/writing data with
52
 * the CPU mean we'll likely hook those out to driver-specific calls.  However,
52
 * the CPU mean we'll likely hook those out to driver-specific calls.  However,
53
 * the DRI2 implementation wants to have at least allocate/mmap be generic.
53
 * the DRI2 implementation wants to have at least allocate/mmap be generic.
54
 *
54
 *
55
 * The goal was to have swap-backed object allocation managed through
55
 * The goal was to have swap-backed object allocation managed through
56
 * struct file.  However, file descriptors as handles to a struct file have
56
 * struct file.  However, file descriptors as handles to a struct file have
57
 * two major failings:
57
 * two major failings:
58
 * - Process limits prevent more than 1024 or so being used at a time by
58
 * - Process limits prevent more than 1024 or so being used at a time by
59
 *   default.
59
 *   default.
60
 * - Inability to allocate high fds will aggravate the X Server's select()
60
 * - Inability to allocate high fds will aggravate the X Server's select()
61
 *   handling, and likely that of many GL client applications as well.
61
 *   handling, and likely that of many GL client applications as well.
62
 *
62
 *
63
 * This led to a plan of using our own integer IDs (called handles, following
63
 * This led to a plan of using our own integer IDs (called handles, following
64
 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
64
 * DRM terminology) to mimic fds, and implement the fd syscalls we need as
65
 * ioctls.  The objects themselves will still include the struct file so
65
 * ioctls.  The objects themselves will still include the struct file so
66
 * that we can transition to fds if the required kernel infrastructure shows
66
 * that we can transition to fds if the required kernel infrastructure shows
67
 * up at a later date, and as our interface with shmfs for memory allocation.
67
 * up at a later date, and as our interface with shmfs for memory allocation.
68
 */
68
 */
69
 
69
 
70
/*
70
/*
71
 * We make up offsets for buffer objects so we can recognize them at
71
 * We make up offsets for buffer objects so we can recognize them at
72
 * mmap time.
72
 * mmap time.
73
 */
73
 */
74
 
74
 
75
/* pgoff in mmap is an unsigned long, so we need to make sure that
75
/* pgoff in mmap is an unsigned long, so we need to make sure that
76
 * the faked up offset will fit
76
 * the faked up offset will fit
77
 */
77
 */
78
 
78
 
79
#if BITS_PER_LONG == 64
79
#if BITS_PER_LONG == 64
80
#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
80
#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
81
#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
81
#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
82
#else
82
#else
83
#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
83
#define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
84
#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
84
#define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
85
#endif
85
#endif
86
 
86
 
87
/**
87
/**
88
 * drm_gem_init - Initialize the GEM device fields
88
 * drm_gem_init - Initialize the GEM device fields
89
 * @dev: drm_devic structure to initialize
89
 * @dev: drm_devic structure to initialize
90
 */
90
 */
91
int
91
int
92
drm_gem_init(struct drm_device *dev)
92
drm_gem_init(struct drm_device *dev)
93
{
93
{
94
	struct drm_vma_offset_manager *vma_offset_manager;
94
	struct drm_vma_offset_manager *vma_offset_manager;
95
 
95
 
96
	mutex_init(&dev->object_name_lock);
96
	mutex_init(&dev->object_name_lock);
97
	idr_init(&dev->object_name_idr);
97
	idr_init(&dev->object_name_idr);
98
 
98
 
99
	vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
99
	vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
100
	if (!vma_offset_manager) {
100
	if (!vma_offset_manager) {
101
		DRM_ERROR("out of memory\n");
101
		DRM_ERROR("out of memory\n");
102
		return -ENOMEM;
102
		return -ENOMEM;
103
	}
103
	}
104
 
104
 
105
	dev->vma_offset_manager = vma_offset_manager;
105
	dev->vma_offset_manager = vma_offset_manager;
106
	drm_vma_offset_manager_init(vma_offset_manager,
106
	drm_vma_offset_manager_init(vma_offset_manager,
107
				    DRM_FILE_PAGE_OFFSET_START,
107
				    DRM_FILE_PAGE_OFFSET_START,
108
				    DRM_FILE_PAGE_OFFSET_SIZE);
108
				    DRM_FILE_PAGE_OFFSET_SIZE);
109
 
109
 
110
	return 0;
110
	return 0;
111
}
111
}
112
 
112
 
113
void
113
void
114
drm_gem_destroy(struct drm_device *dev)
114
drm_gem_destroy(struct drm_device *dev)
115
{
115
{
116
 
116
 
117
	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
117
	drm_vma_offset_manager_destroy(dev->vma_offset_manager);
118
	kfree(dev->vma_offset_manager);
118
	kfree(dev->vma_offset_manager);
119
	dev->vma_offset_manager = NULL;
119
	dev->vma_offset_manager = NULL;
120
}
120
}
121
 
121
 
122
/**
122
/**
123
 * drm_gem_object_init - initialize an allocated shmem-backed GEM object
123
 * drm_gem_object_init - initialize an allocated shmem-backed GEM object
124
 * @dev: drm_device the object should be initialized for
124
 * @dev: drm_device the object should be initialized for
125
 * @obj: drm_gem_object to initialize
125
 * @obj: drm_gem_object to initialize
126
 * @size: object size
126
 * @size: object size
127
 *
127
 *
128
 * Initialize an already allocated GEM object of the specified size with
128
 * Initialize an already allocated GEM object of the specified size with
129
 * shmfs backing store.
129
 * shmfs backing store.
130
 */
130
 */
131
int drm_gem_object_init(struct drm_device *dev,
131
int drm_gem_object_init(struct drm_device *dev,
132
			struct drm_gem_object *obj, size_t size)
132
			struct drm_gem_object *obj, size_t size)
133
{
133
{
134
	struct file *filp;
134
	struct file *filp;
135
 
135
 
136
	drm_gem_private_object_init(dev, obj, size);
136
	drm_gem_private_object_init(dev, obj, size);
137
 
137
 
138
	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
138
	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
139
	if (IS_ERR(filp))
139
	if (IS_ERR(filp))
140
		return PTR_ERR(filp);
140
		return PTR_ERR(filp);
141
 
141
 
142
	obj->filp = filp;
142
	obj->filp = filp;
143
 
143
 
144
	return 0;
144
	return 0;
145
}
145
}
146
EXPORT_SYMBOL(drm_gem_object_init);
146
EXPORT_SYMBOL(drm_gem_object_init);
147
 
147
 
148
/**
148
/**
149
 * drm_gem_private_object_init - initialize an allocated private GEM object
149
 * drm_gem_private_object_init - initialize an allocated private GEM object
150
 * @dev: drm_device the object should be initialized for
150
 * @dev: drm_device the object should be initialized for
151
 * @obj: drm_gem_object to initialize
151
 * @obj: drm_gem_object to initialize
152
 * @size: object size
152
 * @size: object size
153
 *
153
 *
154
 * Initialize an already allocated GEM object of the specified size with
154
 * Initialize an already allocated GEM object of the specified size with
155
 * no GEM provided backing store. Instead the caller is responsible for
155
 * no GEM provided backing store. Instead the caller is responsible for
156
 * backing the object and handling it.
156
 * backing the object and handling it.
157
 */
157
 */
158
void drm_gem_private_object_init(struct drm_device *dev,
158
void drm_gem_private_object_init(struct drm_device *dev,
159
				 struct drm_gem_object *obj, size_t size)
159
				 struct drm_gem_object *obj, size_t size)
160
{
160
{
161
	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
161
	BUG_ON((size & (PAGE_SIZE - 1)) != 0);
162
 
162
 
163
	obj->dev = dev;
163
	obj->dev = dev;
164
	obj->filp = NULL;
164
	obj->filp = NULL;
165
 
165
 
166
	kref_init(&obj->refcount);
166
	kref_init(&obj->refcount);
167
	obj->handle_count = 0;
167
	obj->handle_count = 0;
168
	obj->size = size;
168
	obj->size = size;
169
	drm_vma_node_reset(&obj->vma_node);
169
	drm_vma_node_reset(&obj->vma_node);
170
}
170
}
171
EXPORT_SYMBOL(drm_gem_private_object_init);
171
EXPORT_SYMBOL(drm_gem_private_object_init);
172
 
172
 
173
static void
173
static void
174
drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
174
drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
175
{
175
{
176
	/*
176
	/*
177
	 * Note: obj->dma_buf can't disappear as long as we still hold a
177
	 * Note: obj->dma_buf can't disappear as long as we still hold a
178
	 * handle reference in obj->handle_count.
178
	 * handle reference in obj->handle_count.
179
	 */
179
	 */
180
}
180
}
181
 
181
 
182
/**
182
/**
183
 * drm_gem_object_handle_free - release resources bound to userspace handles
183
 * drm_gem_object_handle_free - release resources bound to userspace handles
184
 * @obj: GEM object to clean up.
184
 * @obj: GEM object to clean up.
185
 *
185
 *
186
 * Called after the last handle to the object has been closed
186
 * Called after the last handle to the object has been closed
187
 *
187
 *
188
 * Removes any name for the object. Note that this must be
188
 * Removes any name for the object. Note that this must be
189
 * called before drm_gem_object_free or we'll be touching
189
 * called before drm_gem_object_free or we'll be touching
190
 * freed memory
190
 * freed memory
191
 */
191
 */
192
static void drm_gem_object_handle_free(struct drm_gem_object *obj)
192
static void drm_gem_object_handle_free(struct drm_gem_object *obj)
193
{
193
{
194
	struct drm_device *dev = obj->dev;
194
	struct drm_device *dev = obj->dev;
195
 
195
 
196
	/* Remove any name for this object */
196
	/* Remove any name for this object */
197
	if (obj->name) {
197
	if (obj->name) {
198
		idr_remove(&dev->object_name_idr, obj->name);
198
		idr_remove(&dev->object_name_idr, obj->name);
199
		obj->name = 0;
199
		obj->name = 0;
200
	}
200
	}
201
}
201
}
202
 
202
 
203
 
203
 
204
static void
204
static void
205
drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
205
drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
206
{
206
{
207
	struct drm_device *dev = obj->dev;
207
	struct drm_device *dev = obj->dev;
208
	bool final = false;
208
	bool final = false;
209
 
209
 
210
	if (WARN_ON(obj->handle_count == 0))
210
	if (WARN_ON(obj->handle_count == 0))
211
		return;
211
		return;
212
 
212
 
213
	/*
213
	/*
214
	* Must bump handle count first as this may be the last
214
	* Must bump handle count first as this may be the last
215
	* ref, in which case the object would disappear before we
215
	* ref, in which case the object would disappear before we
216
	* checked for a name
216
	* checked for a name
217
	*/
217
	*/
218
 
218
 
219
	mutex_lock(&dev->object_name_lock);
219
	mutex_lock(&dev->object_name_lock);
220
	if (--obj->handle_count == 0) {
220
	if (--obj->handle_count == 0) {
221
		drm_gem_object_handle_free(obj);
221
		drm_gem_object_handle_free(obj);
222
		final = true;
222
		final = true;
223
	}
223
	}
224
	mutex_unlock(&dev->object_name_lock);
224
	mutex_unlock(&dev->object_name_lock);
225
 
225
 
226
	if (final)
226
	if (final)
227
	drm_gem_object_unreference_unlocked(obj);
227
		drm_gem_object_unreference_unlocked(obj);
228
}
228
}
229
 
229
 
230
/*
230
/*
231
 * Called at device or object close to release the file's
231
 * Called at device or object close to release the file's
232
 * handle references on objects.
232
 * handle references on objects.
233
 */
233
 */
234
static int
234
static int
235
drm_gem_object_release_handle(int id, void *ptr, void *data)
235
drm_gem_object_release_handle(int id, void *ptr, void *data)
236
{
236
{
237
	struct drm_file *file_priv = data;
237
	struct drm_file *file_priv = data;
238
	struct drm_gem_object *obj = ptr;
238
	struct drm_gem_object *obj = ptr;
239
	struct drm_device *dev = obj->dev;
239
	struct drm_device *dev = obj->dev;
240
 
240
 
241
	if (drm_core_check_feature(dev, DRIVER_PRIME))
241
	if (drm_core_check_feature(dev, DRIVER_PRIME))
242
		drm_gem_remove_prime_handles(obj, file_priv);
242
		drm_gem_remove_prime_handles(obj, file_priv);
243
//	drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
243
//	drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
244
 
244
 
245
	if (dev->driver->gem_close_object)
245
	if (dev->driver->gem_close_object)
246
		dev->driver->gem_close_object(obj, file_priv);
246
		dev->driver->gem_close_object(obj, file_priv);
247
 
247
 
248
	drm_gem_object_handle_unreference_unlocked(obj);
248
	drm_gem_object_handle_unreference_unlocked(obj);
249
 
249
 
250
	return 0;
250
	return 0;
251
}
251
}
252
 
252
 
253
/**
253
/**
254
 * drm_gem_handle_delete - deletes the given file-private handle
254
 * drm_gem_handle_delete - deletes the given file-private handle
255
 * @filp: drm file-private structure to use for the handle look up
255
 * @filp: drm file-private structure to use for the handle look up
256
 * @handle: userspace handle to delete
256
 * @handle: userspace handle to delete
257
 *
257
 *
258
 * Removes the GEM handle from the @filp lookup table which has been added with
258
 * Removes the GEM handle from the @filp lookup table which has been added with
259
 * drm_gem_handle_create(). If this is the last handle also cleans up linked
259
 * drm_gem_handle_create(). If this is the last handle also cleans up linked
260
 * resources like GEM names.
260
 * resources like GEM names.
261
 */
261
 */
262
int
262
int
263
drm_gem_handle_delete(struct drm_file *filp, u32 handle)
263
drm_gem_handle_delete(struct drm_file *filp, u32 handle)
264
{
264
{
265
	struct drm_device *dev;
265
	struct drm_device *dev;
266
	struct drm_gem_object *obj;
266
	struct drm_gem_object *obj;
267
 
267
 
268
	/* This is gross. The idr system doesn't let us try a delete and
268
	/* This is gross. The idr system doesn't let us try a delete and
269
	 * return an error code.  It just spews if you fail at deleting.
269
	 * return an error code.  It just spews if you fail at deleting.
270
	 * So, we have to grab a lock around finding the object and then
270
	 * So, we have to grab a lock around finding the object and then
271
	 * doing the delete on it and dropping the refcount, or the user
271
	 * doing the delete on it and dropping the refcount, or the user
272
	 * could race us to double-decrement the refcount and cause a
272
	 * could race us to double-decrement the refcount and cause a
273
	 * use-after-free later.  Given the frequency of our handle lookups,
273
	 * use-after-free later.  Given the frequency of our handle lookups,
274
	 * we may want to use ida for number allocation and a hash table
274
	 * we may want to use ida for number allocation and a hash table
275
	 * for the pointers, anyway.
275
	 * for the pointers, anyway.
276
	 */
276
	 */
277
	spin_lock(&filp->table_lock);
277
	spin_lock(&filp->table_lock);
278
 
278
 
279
	/* Check if we currently have a reference on the object */
279
	/* Check if we currently have a reference on the object */
280
	obj = idr_find(&filp->object_idr, handle);
280
	obj = idr_find(&filp->object_idr, handle);
281
	if (obj == NULL) {
281
	if (obj == NULL) {
282
		spin_unlock(&filp->table_lock);
282
		spin_unlock(&filp->table_lock);
283
		return -EINVAL;
283
		return -EINVAL;
284
	}
284
	}
285
	dev = obj->dev;
285
	dev = obj->dev;
286
 
286
 
287
	/* Release reference and decrement refcount. */
287
	/* Release reference and decrement refcount. */
288
	idr_remove(&filp->object_idr, handle);
288
	idr_remove(&filp->object_idr, handle);
289
	spin_unlock(&filp->table_lock);
289
	spin_unlock(&filp->table_lock);
290
 
290
 
291
	drm_gem_object_release_handle(handle, obj, filp);
291
	drm_gem_object_release_handle(handle, obj, filp);
292
	return 0;
292
	return 0;
293
}
293
}
294
EXPORT_SYMBOL(drm_gem_handle_delete);
294
EXPORT_SYMBOL(drm_gem_handle_delete);
295
 
295
 
296
/**
296
/**
297
 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
297
 * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
298
 * @file: drm file-private structure to remove the dumb handle from
298
 * @file: drm file-private structure to remove the dumb handle from
299
 * @dev: corresponding drm_device
299
 * @dev: corresponding drm_device
300
 * @handle: the dumb handle to remove
300
 * @handle: the dumb handle to remove
301
 *
301
 * 
302
 * This implements the ->dumb_destroy kms driver callback for drivers which use
302
 * This implements the ->dumb_destroy kms driver callback for drivers which use
303
 * gem to manage their backing storage.
303
 * gem to manage their backing storage.
304
 */
304
 */
305
int drm_gem_dumb_destroy(struct drm_file *file,
305
int drm_gem_dumb_destroy(struct drm_file *file,
306
			 struct drm_device *dev,
306
			 struct drm_device *dev,
307
			 uint32_t handle)
307
			 uint32_t handle)
308
{
308
{
309
	return drm_gem_handle_delete(file, handle);
309
	return drm_gem_handle_delete(file, handle);
310
}
310
}
311
EXPORT_SYMBOL(drm_gem_dumb_destroy);
311
EXPORT_SYMBOL(drm_gem_dumb_destroy);
312
 
312
 
313
/**
313
/**
314
 * drm_gem_handle_create_tail - internal functions to create a handle
314
 * drm_gem_handle_create_tail - internal functions to create a handle
315
 * @file_priv: drm file-private structure to register the handle for
315
 * @file_priv: drm file-private structure to register the handle for
316
 * @obj: object to register
316
 * @obj: object to register
317
 * @handlep: pointer to return the created handle to the caller
317
 * @handlep: pointer to return the created handle to the caller
318
 *
318
 * 
319
 * This expects the dev->object_name_lock to be held already and will drop it
319
 * This expects the dev->object_name_lock to be held already and will drop it
320
 * before returning. Used to avoid races in establishing new handles when
320
 * before returning. Used to avoid races in establishing new handles when
321
 * importing an object from either an flink name or a dma-buf.
321
 * importing an object from either an flink name or a dma-buf.
322
 *
322
 *
323
 * Handles must be release again through drm_gem_handle_delete(). This is done
323
 * Handles must be release again through drm_gem_handle_delete(). This is done
324
 * when userspace closes @file_priv for all attached handles, or through the
324
 * when userspace closes @file_priv for all attached handles, or through the
325
 * GEM_CLOSE ioctl for individual handles.
325
 * GEM_CLOSE ioctl for individual handles.
326
 */
326
 */
327
int
327
int
328
drm_gem_handle_create_tail(struct drm_file *file_priv,
328
drm_gem_handle_create_tail(struct drm_file *file_priv,
329
			   struct drm_gem_object *obj,
329
			   struct drm_gem_object *obj,
330
			   u32 *handlep)
330
			   u32 *handlep)
331
{
331
{
332
	struct drm_device *dev = obj->dev;
332
	struct drm_device *dev = obj->dev;
333
	u32 handle;
333
	u32 handle;
334
	int ret;
334
	int ret;
335
 
335
 
336
	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
336
	WARN_ON(!mutex_is_locked(&dev->object_name_lock));
337
	if (obj->handle_count++ == 0)
337
	if (obj->handle_count++ == 0)
338
		drm_gem_object_reference(obj);
338
		drm_gem_object_reference(obj);
339
 
339
 
340
	/*
340
	/*
341
	 * Get the user-visible handle using idr.  Preload and perform
341
	 * Get the user-visible handle using idr.  Preload and perform
342
	 * allocation under our spinlock.
342
	 * allocation under our spinlock.
343
	 */
343
	 */
344
	idr_preload(GFP_KERNEL);
344
	idr_preload(GFP_KERNEL);
345
	spin_lock(&file_priv->table_lock);
345
	spin_lock(&file_priv->table_lock);
346
 
346
 
347
	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
347
	ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
348
 
348
 
349
	spin_unlock(&file_priv->table_lock);
349
	spin_unlock(&file_priv->table_lock);
350
	idr_preload_end();
350
	idr_preload_end();
351
 
351
 
352
	mutex_unlock(&dev->object_name_lock);
352
	mutex_unlock(&dev->object_name_lock);
353
	if (ret < 0)
353
	if (ret < 0)
354
		goto err_unref;
354
		goto err_unref;
355
 
355
 
356
	handle = ret;
356
	handle = ret;
357
 
357
 
358
//	ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
358
//	ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
359
//	if (ret) {
359
//	if (ret) {
360
//		drm_gem_handle_delete(file_priv, *handlep);
360
//		drm_gem_handle_delete(file_priv, *handlep);
361
//		return ret;
361
//		return ret;
362
//	}
362
//	}
363
 
363
 
364
	if (dev->driver->gem_open_object) {
364
	if (dev->driver->gem_open_object) {
365
		ret = dev->driver->gem_open_object(obj, file_priv);
365
		ret = dev->driver->gem_open_object(obj, file_priv);
366
		if (ret)
366
		if (ret)
367
			goto err_revoke;
367
			goto err_revoke;
368
	}
368
	}
369
 
369
 
370
	*handlep = handle;
370
	*handlep = handle;
371
	return 0;
371
	return 0;
372
 
372
 
373
err_revoke:
373
err_revoke:
374
//	drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
374
//	drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
375
err_remove:
375
err_remove:
376
	spin_lock(&file_priv->table_lock);
376
	spin_lock(&file_priv->table_lock);
377
	idr_remove(&file_priv->object_idr, handle);
377
	idr_remove(&file_priv->object_idr, handle);
378
	spin_unlock(&file_priv->table_lock);
378
	spin_unlock(&file_priv->table_lock);
379
err_unref:
379
err_unref:
380
	drm_gem_object_handle_unreference_unlocked(obj);
380
	drm_gem_object_handle_unreference_unlocked(obj);
381
	return ret;
381
	return ret;
382
}
382
}
383
 
383
 
384
/**
384
/**
385
 * drm_gem_handle_create - create a gem handle for an object
385
 * drm_gem_handle_create - create a gem handle for an object
386
 * @file_priv: drm file-private structure to register the handle for
386
 * @file_priv: drm file-private structure to register the handle for
387
 * @obj: object to register
387
 * @obj: object to register
388
 * @handlep: pionter to return the created handle to the caller
388
 * @handlep: pionter to return the created handle to the caller
389
 *
389
 *
390
 * Create a handle for this object. This adds a handle reference
390
 * Create a handle for this object. This adds a handle reference
391
 * to the object, which includes a regular reference count. Callers
391
 * to the object, which includes a regular reference count. Callers
392
 * will likely want to dereference the object afterwards.
392
 * will likely want to dereference the object afterwards.
393
 */
393
 */
394
int drm_gem_handle_create(struct drm_file *file_priv,
394
int drm_gem_handle_create(struct drm_file *file_priv,
395
			  struct drm_gem_object *obj,
395
			  struct drm_gem_object *obj,
396
			  u32 *handlep)
396
			  u32 *handlep)
397
{
397
{
398
	mutex_lock(&obj->dev->object_name_lock);
398
	mutex_lock(&obj->dev->object_name_lock);
399
 
399
 
400
	return drm_gem_handle_create_tail(file_priv, obj, handlep);
400
	return drm_gem_handle_create_tail(file_priv, obj, handlep);
401
}
401
}
402
EXPORT_SYMBOL(drm_gem_handle_create);
402
EXPORT_SYMBOL(drm_gem_handle_create);
403
 
403
 
404
#if 0
404
#if 0
405
/**
405
/**
406
 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
406
 * drm_gem_free_mmap_offset - release a fake mmap offset for an object
407
 * @obj: obj in question
407
 * @obj: obj in question
408
 *
408
 *
409
 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
409
 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
410
 */
410
 */
411
void
411
void
412
drm_gem_free_mmap_offset(struct drm_gem_object *obj)
412
drm_gem_free_mmap_offset(struct drm_gem_object *obj)
413
{
413
{
414
	struct drm_device *dev = obj->dev;
414
	struct drm_device *dev = obj->dev;
415
 
415
 
416
	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
416
	drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
417
}
417
}
418
EXPORT_SYMBOL(drm_gem_free_mmap_offset);
418
EXPORT_SYMBOL(drm_gem_free_mmap_offset);
419
 
419
 
420
/**
420
/**
421
 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
421
 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
422
 * @obj: obj in question
422
 * @obj: obj in question
423
 * @size: the virtual size
423
 * @size: the virtual size
424
 *
424
 *
425
 * GEM memory mapping works by handing back to userspace a fake mmap offset
425
 * GEM memory mapping works by handing back to userspace a fake mmap offset
426
 * it can use in a subsequent mmap(2) call.  The DRM core code then looks
426
 * it can use in a subsequent mmap(2) call.  The DRM core code then looks
427
 * up the object based on the offset and sets up the various memory mapping
427
 * up the object based on the offset and sets up the various memory mapping
428
 * structures.
428
 * structures.
429
 *
429
 *
430
 * This routine allocates and attaches a fake offset for @obj, in cases where
430
 * This routine allocates and attaches a fake offset for @obj, in cases where
431
 * the virtual size differs from the physical size (ie. obj->size).  Otherwise
431
 * the virtual size differs from the physical size (ie. obj->size).  Otherwise
432
 * just use drm_gem_create_mmap_offset().
432
 * just use drm_gem_create_mmap_offset().
433
 */
433
 */
434
int
434
int
435
drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
435
drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
436
{
436
{
437
	struct drm_device *dev = obj->dev;
437
	struct drm_device *dev = obj->dev;
438
 
438
 
439
	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
439
	return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
440
				  size / PAGE_SIZE);
440
				  size / PAGE_SIZE);
441
}
441
}
442
EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
442
EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
443
 
443
 
444
/**
444
/**
445
 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
445
 * drm_gem_create_mmap_offset - create a fake mmap offset for an object
446
 * @obj: obj in question
446
 * @obj: obj in question
447
 *
447
 *
448
 * GEM memory mapping works by handing back to userspace a fake mmap offset
448
 * GEM memory mapping works by handing back to userspace a fake mmap offset
449
 * it can use in a subsequent mmap(2) call.  The DRM core code then looks
449
 * it can use in a subsequent mmap(2) call.  The DRM core code then looks
450
 * up the object based on the offset and sets up the various memory mapping
450
 * up the object based on the offset and sets up the various memory mapping
451
 * structures.
451
 * structures.
452
 *
452
 *
453
 * This routine allocates and attaches a fake offset for @obj.
453
 * This routine allocates and attaches a fake offset for @obj.
454
 */
454
 */
455
int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
455
int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
456
{
456
{
457
	return drm_gem_create_mmap_offset_size(obj, obj->size);
457
	return drm_gem_create_mmap_offset_size(obj, obj->size);
458
}
458
}
459
EXPORT_SYMBOL(drm_gem_create_mmap_offset);
459
EXPORT_SYMBOL(drm_gem_create_mmap_offset);
460
 
460
 
461
/**
461
/**
462
 * drm_gem_get_pages - helper to allocate backing pages for a GEM object
462
 * drm_gem_get_pages - helper to allocate backing pages for a GEM object
463
 * from shmem
463
 * from shmem
464
 * @obj: obj in question
464
 * @obj: obj in question
465
 *
465
 *
466
 * This reads the page-array of the shmem-backing storage of the given gem
466
 * This reads the page-array of the shmem-backing storage of the given gem
467
 * object. An array of pages is returned. If a page is not allocated or
467
 * object. An array of pages is returned. If a page is not allocated or
468
 * swapped-out, this will allocate/swap-in the required pages. Note that the
468
 * swapped-out, this will allocate/swap-in the required pages. Note that the
469
 * whole object is covered by the page-array and pinned in memory.
469
 * whole object is covered by the page-array and pinned in memory.
470
 *
470
 *
471
 * Use drm_gem_put_pages() to release the array and unpin all pages.
471
 * Use drm_gem_put_pages() to release the array and unpin all pages.
472
 *
472
 *
473
 * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
473
 * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
474
 * If you require other GFP-masks, you have to do those allocations yourself.
474
 * If you require other GFP-masks, you have to do those allocations yourself.
475
 *
475
 *
476
 * Note that you are not allowed to change gfp-zones during runtime. That is,
476
 * Note that you are not allowed to change gfp-zones during runtime. That is,
477
 * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
477
 * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
478
 * set during initialization. If you have special zone constraints, set them
478
 * set during initialization. If you have special zone constraints, set them
479
 * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
479
 * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
480
 * to keep pages in the required zone during swap-in.
480
 * to keep pages in the required zone during swap-in.
481
 */
481
 */
482
struct page **drm_gem_get_pages(struct drm_gem_object *obj)
482
struct page **drm_gem_get_pages(struct drm_gem_object *obj)
483
{
483
{
484
	struct address_space *mapping;
484
	struct address_space *mapping;
485
	struct page *p, **pages;
485
	struct page *p, **pages;
486
	int i, npages;
486
	int i, npages;
487
 
487
 
488
	/* This is the shared memory object that backs the GEM resource */
488
	/* This is the shared memory object that backs the GEM resource */
489
	mapping = file_inode(obj->filp)->i_mapping;
489
	mapping = file_inode(obj->filp)->i_mapping;
490
 
490
 
491
	/* We already BUG_ON() for non-page-aligned sizes in
491
	/* We already BUG_ON() for non-page-aligned sizes in
492
	 * drm_gem_object_init(), so we should never hit this unless
492
	 * drm_gem_object_init(), so we should never hit this unless
493
	 * driver author is doing something really wrong:
493
	 * driver author is doing something really wrong:
494
	 */
494
	 */
495
	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
495
	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
496
 
496
 
497
	npages = obj->size >> PAGE_SHIFT;
497
	npages = obj->size >> PAGE_SHIFT;
498
 
498
 
499
	pages = drm_malloc_ab(npages, sizeof(struct page *));
499
	pages = drm_malloc_ab(npages, sizeof(struct page *));
500
	if (pages == NULL)
500
	if (pages == NULL)
501
		return ERR_PTR(-ENOMEM);
501
		return ERR_PTR(-ENOMEM);
502
 
502
 
503
	for (i = 0; i < npages; i++) {
503
	for (i = 0; i < npages; i++) {
504
		p = shmem_read_mapping_page(mapping, i);
504
		p = shmem_read_mapping_page(mapping, i);
505
		if (IS_ERR(p))
505
		if (IS_ERR(p))
506
			goto fail;
506
			goto fail;
507
		pages[i] = p;
507
		pages[i] = p;
508
 
508
 
509
		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
509
		/* Make sure shmem keeps __GFP_DMA32 allocated pages in the
510
		 * correct region during swapin. Note that this requires
510
		 * correct region during swapin. Note that this requires
511
		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
511
		 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
512
		 * so shmem can relocate pages during swapin if required.
512
		 * so shmem can relocate pages during swapin if required.
513
		 */
513
		 */
514
		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
514
		BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
515
				(page_to_pfn(p) >= 0x00100000UL));
515
				(page_to_pfn(p) >= 0x00100000UL));
516
	}
516
	}
517
 
517
 
518
	return pages;
518
	return pages;
519
 
519
 
520
fail:
520
fail:
521
	while (i--)
521
	while (i--)
522
		page_cache_release(pages[i]);
522
		page_cache_release(pages[i]);
523
 
523
 
524
	drm_free_large(pages);
524
	drm_free_large(pages);
525
	return ERR_CAST(p);
525
	return ERR_CAST(p);
526
}
526
}
527
EXPORT_SYMBOL(drm_gem_get_pages);
527
EXPORT_SYMBOL(drm_gem_get_pages);
528
 
528
 
529
/**
529
/**
530
 * drm_gem_put_pages - helper to free backing pages for a GEM object
530
 * drm_gem_put_pages - helper to free backing pages for a GEM object
531
 * @obj: obj in question
531
 * @obj: obj in question
532
 * @pages: pages to free
532
 * @pages: pages to free
533
 * @dirty: if true, pages will be marked as dirty
533
 * @dirty: if true, pages will be marked as dirty
534
 * @accessed: if true, the pages will be marked as accessed
534
 * @accessed: if true, the pages will be marked as accessed
535
 */
535
 */
536
void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
536
void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
537
		bool dirty, bool accessed)
537
		bool dirty, bool accessed)
538
{
538
{
539
	int i, npages;
539
	int i, npages;
540
 
540
 
541
	/* We already BUG_ON() for non-page-aligned sizes in
541
	/* We already BUG_ON() for non-page-aligned sizes in
542
	 * drm_gem_object_init(), so we should never hit this unless
542
	 * drm_gem_object_init(), so we should never hit this unless
543
	 * driver author is doing something really wrong:
543
	 * driver author is doing something really wrong:
544
	 */
544
	 */
545
	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
545
	WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
546
 
546
 
547
	npages = obj->size >> PAGE_SHIFT;
547
	npages = obj->size >> PAGE_SHIFT;
548
 
548
 
549
	for (i = 0; i < npages; i++) {
549
	for (i = 0; i < npages; i++) {
550
		if (dirty)
550
		if (dirty)
551
			set_page_dirty(pages[i]);
551
			set_page_dirty(pages[i]);
552
 
552
 
553
		if (accessed)
553
		if (accessed)
554
			mark_page_accessed(pages[i]);
554
			mark_page_accessed(pages[i]);
555
 
555
 
556
		/* Undo the reference we took when populating the table */
556
		/* Undo the reference we took when populating the table */
557
		page_cache_release(pages[i]);
557
		page_cache_release(pages[i]);
558
	}
558
	}
559
 
559
 
560
	drm_free_large(pages);
560
	drm_free_large(pages);
561
}
561
}
562
EXPORT_SYMBOL(drm_gem_put_pages);
562
EXPORT_SYMBOL(drm_gem_put_pages);
563
#endif
563
#endif
564
 
564
 
565
/**
565
/**
566
 * drm_gem_object_lookup - look up a GEM object from it's handle
566
 * drm_gem_object_lookup - look up a GEM object from it's handle
567
 * @dev: DRM device
567
 * @dev: DRM device
568
 * @filp: DRM file private date
568
 * @filp: DRM file private date
569
 * @handle: userspace handle
569
 * @handle: userspace handle
570
 *
570
 *
571
 * Returns:
571
 * Returns:
572
 *
572
 *
573
 * A reference to the object named by the handle if such exists on @filp, NULL
573
 * A reference to the object named by the handle if such exists on @filp, NULL
574
 * otherwise.
574
 * otherwise.
575
 */
575
 */
576
struct drm_gem_object *
576
struct drm_gem_object *
577
drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
577
drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
578
		      u32 handle)
578
		      u32 handle)
579
{
579
{
580
	struct drm_gem_object *obj;
580
	struct drm_gem_object *obj;
581
 
581
 
582
	spin_lock(&filp->table_lock);
582
	spin_lock(&filp->table_lock);
583
 
583
 
584
	/* Check if we currently have a reference on the object */
584
	/* Check if we currently have a reference on the object */
585
	obj = idr_find(&filp->object_idr, handle);
585
	obj = idr_find(&filp->object_idr, handle);
586
	if (obj == NULL) {
586
	if (obj == NULL) {
587
		spin_unlock(&filp->table_lock);
587
		spin_unlock(&filp->table_lock);
588
		return NULL;
588
		return NULL;
589
	}
589
	}
590
 
590
 
591
	drm_gem_object_reference(obj);
591
	drm_gem_object_reference(obj);
592
 
592
 
593
	spin_unlock(&filp->table_lock);
593
	spin_unlock(&filp->table_lock);
594
 
594
 
595
	return obj;
595
	return obj;
596
}
596
}
597
EXPORT_SYMBOL(drm_gem_object_lookup);
597
EXPORT_SYMBOL(drm_gem_object_lookup);
598
 
598
 
599
/**
599
/**
600
 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
600
 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
601
 * @dev: drm_device
601
 * @dev: drm_device
602
 * @data: ioctl data
602
 * @data: ioctl data
603
 * @file_priv: drm file-private structure
603
 * @file_priv: drm file-private structure
604
 *
604
 *
605
 * Releases the handle to an mm object.
605
 * Releases the handle to an mm object.
606
 */
606
 */
607
int
607
int
608
drm_gem_close_ioctl(struct drm_device *dev, void *data,
608
drm_gem_close_ioctl(struct drm_device *dev, void *data,
609
		    struct drm_file *file_priv)
609
		    struct drm_file *file_priv)
610
{
610
{
611
	struct drm_gem_close *args = data;
611
	struct drm_gem_close *args = data;
612
	int ret;
612
	int ret;
613
 
613
 
614
	if (!drm_core_check_feature(dev, DRIVER_GEM))
614
	if (!drm_core_check_feature(dev, DRIVER_GEM))
615
		return -ENODEV;
615
		return -ENODEV;
616
 
616
 
617
	ret = drm_gem_handle_delete(file_priv, args->handle);
617
	ret = drm_gem_handle_delete(file_priv, args->handle);
618
 
618
 
619
	return ret;
619
	return ret;
620
}
620
}
621
 
621
 
622
/**
622
/**
623
 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
623
 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
624
 * @dev: drm_device
624
 * @dev: drm_device
625
 * @data: ioctl data
625
 * @data: ioctl data
626
 * @file_priv: drm file-private structure
626
 * @file_priv: drm file-private structure
627
 *
627
 *
628
 * Create a global name for an object, returning the name.
628
 * Create a global name for an object, returning the name.
629
 *
629
 *
630
 * Note that the name does not hold a reference; when the object
630
 * Note that the name does not hold a reference; when the object
631
 * is freed, the name goes away.
631
 * is freed, the name goes away.
632
 */
632
 */
633
int
633
int
634
drm_gem_flink_ioctl(struct drm_device *dev, void *data,
634
drm_gem_flink_ioctl(struct drm_device *dev, void *data,
635
		    struct drm_file *file_priv)
635
		    struct drm_file *file_priv)
636
{
636
{
637
	struct drm_gem_flink *args = data;
637
	struct drm_gem_flink *args = data;
638
	struct drm_gem_object *obj;
638
	struct drm_gem_object *obj;
639
	int ret;
639
	int ret;
640
 
640
 
641
	if (!drm_core_check_feature(dev, DRIVER_GEM))
641
	if (!drm_core_check_feature(dev, DRIVER_GEM))
642
		return -ENODEV;
642
		return -ENODEV;
643
 
643
 
644
	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
644
	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
645
	if (obj == NULL)
645
	if (obj == NULL)
646
		return -ENOENT;
646
		return -ENOENT;
647
 
647
 
648
	mutex_lock(&dev->object_name_lock);
648
	mutex_lock(&dev->object_name_lock);
649
	/* prevent races with concurrent gem_close. */
649
	/* prevent races with concurrent gem_close. */
650
	if (obj->handle_count == 0) {
650
	if (obj->handle_count == 0) {
651
		ret = -ENOENT;
651
		ret = -ENOENT;
652
		goto err;
652
		goto err;
653
	}
653
	}
654
 
654
 
655
	if (!obj->name) {
655
	if (!obj->name) {
656
		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
656
		ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
657
		if (ret < 0)
657
		if (ret < 0)
658
			goto err;
658
			goto err;
659
 
659
 
660
		obj->name = ret;
660
		obj->name = ret;
661
	}
661
	}
662
 
662
 
663
	args->name = (uint64_t) obj->name;
663
	args->name = (uint64_t) obj->name;
664
	ret = 0;
664
	ret = 0;
665
 
665
 
666
err:
666
err:
667
	mutex_unlock(&dev->object_name_lock);
667
	mutex_unlock(&dev->object_name_lock);
668
	drm_gem_object_unreference_unlocked(obj);
668
	drm_gem_object_unreference_unlocked(obj);
669
 
669
 
670
//    printf("%s object %p name %d refcount %d\n",
670
//    printf("%s object %p name %d refcount %d\n",
671
//           __FUNCTION__, obj, obj->name, obj->refcount.refcount);
671
//           __FUNCTION__, obj, obj->name, obj->refcount.refcount);
672
 
672
 
673
	return ret;
673
	return ret;
674
}
674
}
675
 
675
 
676
/**
676
/**
677
 * drm_gem_open - implementation of the GEM_OPEN ioctl
677
 * drm_gem_open - implementation of the GEM_OPEN ioctl
678
 * @dev: drm_device
678
 * @dev: drm_device
679
 * @data: ioctl data
679
 * @data: ioctl data
680
 * @file_priv: drm file-private structure
680
 * @file_priv: drm file-private structure
681
 *
681
 *
682
 * Open an object using the global name, returning a handle and the size.
682
 * Open an object using the global name, returning a handle and the size.
683
 *
683
 *
684
 * This handle (of course) holds a reference to the object, so the object
684
 * This handle (of course) holds a reference to the object, so the object
685
 * will not go away until the handle is deleted.
685
 * will not go away until the handle is deleted.
686
 */
686
 */
687
int
687
int
688
drm_gem_open_ioctl(struct drm_device *dev, void *data,
688
drm_gem_open_ioctl(struct drm_device *dev, void *data,
689
		   struct drm_file *file_priv)
689
		   struct drm_file *file_priv)
690
{
690
{
691
	struct drm_gem_open *args = data;
691
	struct drm_gem_open *args = data;
692
	struct drm_gem_object *obj;
692
	struct drm_gem_object *obj;
693
	int ret;
693
	int ret;
694
	u32 handle;
694
	u32 handle;
695
 
695
 
696
	if (!drm_core_check_feature(dev, DRIVER_GEM))
696
	if (!drm_core_check_feature(dev, DRIVER_GEM))
697
		return -ENODEV;
697
		return -ENODEV;
698
 
698
 
699
	mutex_lock(&dev->object_name_lock);
699
	mutex_lock(&dev->object_name_lock);
700
	obj = idr_find(&dev->object_name_idr, (int) args->name);
700
	obj = idr_find(&dev->object_name_idr, (int) args->name);
701
	if (obj) {
701
	if (obj) {
702
		drm_gem_object_reference(obj);
702
		drm_gem_object_reference(obj);
703
	} else {
703
	} else {
704
		mutex_unlock(&dev->object_name_lock);
704
		mutex_unlock(&dev->object_name_lock);
705
		return -ENOENT;
705
		return -ENOENT;
706
	}
706
	}
707
 
707
 
708
	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
708
	/* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
709
	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
709
	ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
710
	drm_gem_object_unreference_unlocked(obj);
710
	drm_gem_object_unreference_unlocked(obj);
711
	if (ret)
711
	if (ret)
712
		return ret;
712
		return ret;
713
 
713
 
714
	args->handle = handle;
714
	args->handle = handle;
715
	args->size = obj->size;
715
	args->size = obj->size;
716
 
716
 
717
//    printf("%s object %p handle %d refcount %d\n",
717
//    printf("%s object %p handle %d refcount %d\n",
718
//           __FUNCTION__, obj, handle, obj->refcount.refcount);
718
//           __FUNCTION__, obj, handle, obj->refcount.refcount);
719
 
719
 
720
	return 0;
720
	return 0;
721
}
721
}
722
 
722
 
723
#if 0
723
#if 0
724
/**
724
/**
725
 * gem_gem_open - initalizes GEM file-private structures at devnode open time
725
 * gem_gem_open - initalizes GEM file-private structures at devnode open time
726
 * @dev: drm_device which is being opened by userspace
726
 * @dev: drm_device which is being opened by userspace
727
 * @file_private: drm file-private structure to set up
727
 * @file_private: drm file-private structure to set up
728
 *
728
 *
729
 * Called at device open time, sets up the structure for handling refcounting
729
 * Called at device open time, sets up the structure for handling refcounting
730
 * of mm objects.
730
 * of mm objects.
731
 */
731
 */
732
void
732
void
733
drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
733
drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
734
{
734
{
735
	idr_init(&file_private->object_idr);
735
	idr_init(&file_private->object_idr);
736
	spin_lock_init(&file_private->table_lock);
736
	spin_lock_init(&file_private->table_lock);
737
}
737
}
738
 
738
 
739
/**
739
/**
740
 * drm_gem_release - release file-private GEM resources
740
 * drm_gem_release - release file-private GEM resources
741
 * @dev: drm_device which is being closed by userspace
741
 * @dev: drm_device which is being closed by userspace
742
 * @file_private: drm file-private structure to clean up
742
 * @file_private: drm file-private structure to clean up
743
 *
743
 *
744
 * Called at close time when the filp is going away.
744
 * Called at close time when the filp is going away.
745
 *
745
 *
746
 * Releases any remaining references on objects by this filp.
746
 * Releases any remaining references on objects by this filp.
747
 */
747
 */
748
void
748
void
749
drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
749
drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
750
{
750
{
751
	idr_for_each(&file_private->object_idr,
751
	idr_for_each(&file_private->object_idr,
752
		     &drm_gem_object_release_handle, file_private);
752
		     &drm_gem_object_release_handle, file_private);
753
	idr_destroy(&file_private->object_idr);
753
	idr_destroy(&file_private->object_idr);
754
}
754
}
755
#endif
755
#endif
756
 
756
 
757
void
757
void
758
drm_gem_object_release(struct drm_gem_object *obj)
758
drm_gem_object_release(struct drm_gem_object *obj)
759
{
759
{
760
	WARN_ON(obj->dma_buf);
760
	WARN_ON(obj->dma_buf);
761
 
761
 
762
	if (obj->filp)
762
	if (obj->filp)
763
	    free(obj->filp);
763
	    free(obj->filp);
764
}
764
}
765
EXPORT_SYMBOL(drm_gem_object_release);
765
EXPORT_SYMBOL(drm_gem_object_release);
766
 
766
 
767
/**
767
/**
768
 * drm_gem_object_free - free a GEM object
768
 * drm_gem_object_free - free a GEM object
769
 * @kref: kref of the object to free
769
 * @kref: kref of the object to free
770
 *
770
 *
771
 * Called after the last reference to the object has been lost.
771
 * Called after the last reference to the object has been lost.
772
 * Must be called holding struct_ mutex
772
 * Must be called holding struct_ mutex
773
 *
773
 *
774
 * Frees the object
774
 * Frees the object
775
 */
775
 */
776
void
776
void
777
drm_gem_object_free(struct kref *kref)
777
drm_gem_object_free(struct kref *kref)
778
{
778
{
779
	struct drm_gem_object *obj =
779
	struct drm_gem_object *obj =
780
		container_of(kref, struct drm_gem_object, refcount);
780
		container_of(kref, struct drm_gem_object, refcount);
781
	struct drm_device *dev = obj->dev;
781
	struct drm_device *dev = obj->dev;
782
 
782
 
783
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
783
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
784
 
784
 
785
	if (dev->driver->gem_free_object != NULL)
785
	if (dev->driver->gem_free_object != NULL)
786
		dev->driver->gem_free_object(obj);
786
		dev->driver->gem_free_object(obj);
787
}
787
}
788
EXPORT_SYMBOL(drm_gem_object_free);
788
EXPORT_SYMBOL(drm_gem_object_free);
-
 
789
 
-
 
790
/**
-
 
791
 * drm_gem_vm_open - vma->ops->open implementation for GEM
789
 
792
 * @vma: VM area structure
-
 
793
 *
-
 
794
 * This function implements the #vm_operations_struct open() callback for GEM
-
 
795
 * drivers. This must be used together with drm_gem_vm_close().
790
 
796
 */
791
#if 0
797
#if 0
792
void drm_gem_vm_open(struct vm_area_struct *vma)
798
void drm_gem_vm_open(struct vm_area_struct *vma)
793
{
799
{
794
	struct drm_gem_object *obj = vma->vm_private_data;
800
	struct drm_gem_object *obj = vma->vm_private_data;
795
 
801
 
796
	drm_gem_object_reference(obj);
802
	drm_gem_object_reference(obj);
797
}
803
}
798
EXPORT_SYMBOL(drm_gem_vm_open);
804
EXPORT_SYMBOL(drm_gem_vm_open);
-
 
805
 
-
 
806
/**
-
 
807
 * drm_gem_vm_close - vma->ops->close implementation for GEM
-
 
808
 * @vma: VM area structure
-
 
809
 *
-
 
810
 * This function implements the #vm_operations_struct close() callback for GEM
-
 
811
 * drivers. This must be used together with drm_gem_vm_open().
799
 
812
 */
800
void drm_gem_vm_close(struct vm_area_struct *vma)
813
void drm_gem_vm_close(struct vm_area_struct *vma)
801
{
814
{
802
	struct drm_gem_object *obj = vma->vm_private_data;
815
	struct drm_gem_object *obj = vma->vm_private_data;
803
	struct drm_device *dev = obj->dev;
-
 
804
 
-
 
805
	mutex_lock(&dev->struct_mutex);
-
 
806
	drm_vm_close_locked(obj->dev, vma);
816
 
807
	drm_gem_object_unreference(obj);
-
 
808
	mutex_unlock(&dev->struct_mutex);
817
	drm_gem_object_unreference_unlocked(obj);
809
}
818
}
810
EXPORT_SYMBOL(drm_gem_vm_close);
819
EXPORT_SYMBOL(drm_gem_vm_close);
-
 
820
 
-
 
821
/**
-
 
822
 * drm_gem_mmap_obj - memory map a GEM object
-
 
823
 * @obj: the GEM object to map
-
 
824
 * @obj_size: the object size to be mapped, in bytes
-
 
825
 * @vma: VMA for the area to be mapped
-
 
826
 *
-
 
827
 * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
-
 
828
 * provided by the driver. Depending on their requirements, drivers can either
-
 
829
 * provide a fault handler in their gem_vm_ops (in which case any accesses to
-
 
830
 * the object will be trapped, to perform migration, GTT binding, surface
-
 
831
 * register allocation, or performance monitoring), or mmap the buffer memory
-
 
832
 * synchronously after calling drm_gem_mmap_obj.
-
 
833
 *
-
 
834
 * This function is mainly intended to implement the DMABUF mmap operation, when
-
 
835
 * the GEM object is not looked up based on its fake offset. To implement the
-
 
836
 * DRM mmap operation, drivers should use the drm_gem_mmap() function.
-
 
837
 *
-
 
838
 * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
-
 
839
 * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
-
 
840
 * callers must verify access restrictions before calling this helper.
-
 
841
 *
-
 
842
 * Return 0 or success or -EINVAL if the object size is smaller than the VMA
-
 
843
 * size, or if no gem_vm_ops are provided.
-
 
844
 */
-
 
845
int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
-
 
846
		     struct vm_area_struct *vma)
-
 
847
{
-
 
848
	struct drm_device *dev = obj->dev;
-
 
849
 
-
 
850
	/* Check for valid size. */
-
 
851
	if (obj_size < vma->vm_end - vma->vm_start)
-
 
852
		return -EINVAL;
-
 
853
 
-
 
854
	if (!dev->driver->gem_vm_ops)
-
 
855
		return -EINVAL;
-
 
856
 
-
 
857
	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
-
 
858
	vma->vm_ops = dev->driver->gem_vm_ops;
-
 
859
	vma->vm_private_data = obj;
-
 
860
	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
-
 
861
 
-
 
862
	/* Take a ref for this mapping of the object, so that the fault
-
 
863
	 * handler can dereference the mmap offset's pointer to the object.
-
 
864
	 * This reference is cleaned up by the corresponding vm_close
-
 
865
	 * (which should happen whether the vma was created by this call, or
-
 
866
	 * by a vm_open due to mremap or partial unmap or whatever).
-
 
867
	 */
-
 
868
	drm_gem_object_reference(obj);
-
 
869
 
-
 
870
	return 0;
-
 
871
}
-
 
872
EXPORT_SYMBOL(drm_gem_mmap_obj);
-
 
873
 
-
 
874
/**
-
 
875
 * drm_gem_mmap - memory map routine for GEM objects
-
 
876
 * @filp: DRM file pointer
-
 
877
 * @vma: VMA for the area to be mapped
-
 
878
 *
-
 
879
 * If a driver supports GEM object mapping, mmap calls on the DRM file
-
 
880
 * descriptor will end up here.
-
 
881
 *
-
 
882
 * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
-
 
883
 * contain the fake offset we created when the GTT map ioctl was called on
-
 
884
 * the object) and map it with a call to drm_gem_mmap_obj().
-
 
885
 *
-
 
886
 * If the caller is not granted access to the buffer object, the mmap will fail
-
 
887
 * with EACCES. Please see the vma manager for more information.
-
 
888
 */
-
 
889
int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
-
 
890
{
-
 
891
	struct drm_file *priv = filp->private_data;
-
 
892
	struct drm_device *dev = priv->minor->dev;
-
 
893
	struct drm_gem_object *obj = NULL;
-
 
894
	struct drm_vma_offset_node *node;
-
 
895
	int ret;
-
 
896
 
-
 
897
	if (drm_device_is_unplugged(dev))
-
 
898
		return -ENODEV;
-
 
899
 
-
 
900
	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
-
 
901
	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
-
 
902
						  vma->vm_pgoff,
-
 
903
						  vma_pages(vma));
-
 
904
	if (likely(node)) {
-
 
905
		obj = container_of(node, struct drm_gem_object, vma_node);
-
 
906
		/*
-
 
907
		 * When the object is being freed, after it hits 0-refcnt it
-
 
908
		 * proceeds to tear down the object. In the process it will
-
 
909
		 * attempt to remove the VMA offset and so acquire this
-
 
910
		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
-
 
911
		 * that matches our range, we know it is in the process of being
-
 
912
		 * destroyed and will be freed as soon as we release the lock -
-
 
913
		 * so we have to check for the 0-refcnted object and treat it as
811
 
914
		 * invalid.
-
 
915
		 */
-
 
916
		if (!kref_get_unless_zero(&obj->refcount))
-
 
917
			obj = NULL;
-
 
918
	}
-
 
919
	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
-
 
920
 
-
 
921
	if (!obj)
-
 
922
		return -EINVAL;
-
 
923
 
-
 
924
	if (!drm_vma_node_is_allowed(node, filp)) {
-
 
925
		drm_gem_object_unreference_unlocked(obj);
-
 
926
		return -EACCES;
-
 
927
	}
-
 
928
 
-
 
929
	ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
-
 
930
			       vma);
-
 
931
 
-
 
932
	drm_gem_object_unreference_unlocked(obj);
-
 
933
 
812
#endif
934
	return ret;
-
 
935
}
-
 
936
EXPORT_SYMBOL(drm_gem_mmap);
-
 
937
#endif