Rev 4560 | Rev 5271 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4560 | Rev 5060 | ||
---|---|---|---|
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 | 38 | ||
39 | /** @file drm_gem.c |
39 | /** @file drm_gem.c |
40 | * |
40 | * |
41 | * This file provides some of the base ioctls and library routines for |
41 | * This file provides some of the base ioctls and library routines for |
42 | * the graphics memory manager implemented by each device driver. |
42 | * the graphics memory manager implemented by each device driver. |
43 | * |
43 | * |
44 | * Because various devices have different requirements in terms of |
44 | * Because various devices have different requirements in terms of |
45 | * synchronization and migration strategies, implementing that is left up to |
45 | * synchronization and migration strategies, implementing that is left up to |
46 | * the driver, and all that the general API provides should be generic -- |
46 | * the driver, and all that the general API provides should be generic -- |
47 | * allocating objects, reading/writing data with the cpu, freeing objects. |
47 | * allocating objects, reading/writing data with the cpu, freeing objects. |
48 | * Even there, platform-dependent optimizations for reading/writing data with |
48 | * Even there, platform-dependent optimizations for reading/writing data with |
49 | * the CPU mean we'll likely hook those out to driver-specific calls. However, |
49 | * the CPU mean we'll likely hook those out to driver-specific calls. However, |
50 | * the DRI2 implementation wants to have at least allocate/mmap be generic. |
50 | * the DRI2 implementation wants to have at least allocate/mmap be generic. |
51 | * |
51 | * |
52 | * The goal was to have swap-backed object allocation managed through |
52 | * The goal was to have swap-backed object allocation managed through |
53 | * struct file. However, file descriptors as handles to a struct file have |
53 | * struct file. However, file descriptors as handles to a struct file have |
54 | * two major failings: |
54 | * two major failings: |
55 | * - Process limits prevent more than 1024 or so being used at a time by |
55 | * - Process limits prevent more than 1024 or so being used at a time by |
56 | * default. |
56 | * default. |
57 | * - Inability to allocate high fds will aggravate the X Server's select() |
57 | * - Inability to allocate high fds will aggravate the X Server's select() |
58 | * handling, and likely that of many GL client applications as well. |
58 | * handling, and likely that of many GL client applications as well. |
59 | * |
59 | * |
60 | * This led to a plan of using our own integer IDs (called handles, following |
60 | * This led to a plan of using our own integer IDs (called handles, following |
61 | * DRM terminology) to mimic fds, and implement the fd syscalls we need as |
61 | * DRM terminology) to mimic fds, and implement the fd syscalls we need as |
62 | * ioctls. The objects themselves will still include the struct file so |
62 | * ioctls. The objects themselves will still include the struct file so |
63 | * that we can transition to fds if the required kernel infrastructure shows |
63 | * that we can transition to fds if the required kernel infrastructure shows |
64 | * up at a later date, and as our interface with shmfs for memory allocation. |
64 | * up at a later date, and as our interface with shmfs for memory allocation. |
65 | */ |
65 | */ |
66 | 66 | ||
67 | /* |
67 | /* |
68 | * We make up offsets for buffer objects so we can recognize them at |
68 | * We make up offsets for buffer objects so we can recognize them at |
69 | * mmap time. |
69 | * mmap time. |
70 | */ |
70 | */ |
71 | 71 | ||
72 | /* pgoff in mmap is an unsigned long, so we need to make sure that |
72 | /* pgoff in mmap is an unsigned long, so we need to make sure that |
73 | * the faked up offset will fit |
73 | * the faked up offset will fit |
74 | */ |
74 | */ |
75 | 75 | ||
76 | #if BITS_PER_LONG == 64 |
76 | #if BITS_PER_LONG == 64 |
77 | #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) |
77 | #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1) |
78 | #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) |
78 | #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16) |
79 | #else |
79 | #else |
80 | #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) |
80 | #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1) |
81 | #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) |
81 | #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16) |
82 | #endif |
82 | #endif |
83 | 83 | ||
84 | /** |
84 | /** |
85 | * Initialize the GEM device fields |
85 | * drm_gem_init - Initialize the GEM device fields |
- | 86 | * @dev: drm_devic structure to initialize |
|
86 | */ |
87 | */ |
87 | - | ||
88 | int |
88 | int |
89 | drm_gem_init(struct drm_device *dev) |
89 | drm_gem_init(struct drm_device *dev) |
90 | { |
90 | { |
91 | struct drm_vma_offset_manager *vma_offset_manager; |
91 | struct drm_vma_offset_manager *vma_offset_manager; |
92 | 92 | ||
93 | mutex_init(&dev->object_name_lock); |
93 | mutex_init(&dev->object_name_lock); |
94 | idr_init(&dev->object_name_idr); |
94 | idr_init(&dev->object_name_idr); |
95 | 95 | ||
96 | vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL); |
96 | vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL); |
97 | if (!vma_offset_manager) { |
97 | if (!vma_offset_manager) { |
98 | DRM_ERROR("out of memory\n"); |
98 | DRM_ERROR("out of memory\n"); |
99 | return -ENOMEM; |
99 | return -ENOMEM; |
100 | } |
100 | } |
101 | 101 | ||
102 | dev->vma_offset_manager = vma_offset_manager; |
102 | dev->vma_offset_manager = vma_offset_manager; |
103 | drm_vma_offset_manager_init(vma_offset_manager, |
103 | drm_vma_offset_manager_init(vma_offset_manager, |
104 | DRM_FILE_PAGE_OFFSET_START, |
104 | DRM_FILE_PAGE_OFFSET_START, |
105 | DRM_FILE_PAGE_OFFSET_SIZE); |
105 | DRM_FILE_PAGE_OFFSET_SIZE); |
106 | 106 | ||
107 | return 0; |
107 | return 0; |
108 | } |
108 | } |
109 | 109 | ||
110 | void |
110 | void |
111 | drm_gem_destroy(struct drm_device *dev) |
111 | drm_gem_destroy(struct drm_device *dev) |
112 | { |
112 | { |
113 | 113 | ||
114 | drm_vma_offset_manager_destroy(dev->vma_offset_manager); |
114 | drm_vma_offset_manager_destroy(dev->vma_offset_manager); |
115 | kfree(dev->vma_offset_manager); |
115 | kfree(dev->vma_offset_manager); |
116 | dev->vma_offset_manager = NULL; |
116 | dev->vma_offset_manager = NULL; |
117 | } |
117 | } |
118 | 118 | ||
119 | /** |
119 | /** |
- | 120 | * drm_gem_object_init - initialize an allocated shmem-backed GEM object |
|
- | 121 | * @dev: drm_device the object should be initialized for |
|
- | 122 | * @obj: drm_gem_object to initialize |
|
- | 123 | * @size: object size |
|
- | 124 | * |
|
120 | * Initialize an already allocated GEM object of the specified size with |
125 | * Initialize an already allocated GEM object of the specified size with |
121 | * shmfs backing store. |
126 | * shmfs backing store. |
122 | */ |
127 | */ |
123 | int drm_gem_object_init(struct drm_device *dev, |
128 | int drm_gem_object_init(struct drm_device *dev, |
124 | struct drm_gem_object *obj, size_t size) |
129 | struct drm_gem_object *obj, size_t size) |
125 | { |
130 | { |
126 | struct file *filp; |
131 | struct file *filp; |
127 | 132 | ||
128 | drm_gem_private_object_init(dev, obj, size); |
133 | drm_gem_private_object_init(dev, obj, size); |
129 | 134 | ||
130 | filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); |
135 | filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); |
131 | if (IS_ERR(filp)) |
136 | if (IS_ERR(filp)) |
132 | return PTR_ERR(filp); |
137 | return PTR_ERR(filp); |
133 | 138 | ||
134 | obj->filp = filp; |
139 | obj->filp = filp; |
135 | 140 | ||
136 | return 0; |
141 | return 0; |
137 | } |
142 | } |
138 | EXPORT_SYMBOL(drm_gem_object_init); |
143 | EXPORT_SYMBOL(drm_gem_object_init); |
139 | 144 | ||
140 | /** |
145 | /** |
- | 146 | * drm_gem_object_init - initialize an allocated private GEM object |
|
- | 147 | * @dev: drm_device the object should be initialized for |
|
- | 148 | * @obj: drm_gem_object to initialize |
|
- | 149 | * @size: object size |
|
- | 150 | * |
|
141 | * Initialize an already allocated GEM object of the specified size with |
151 | * Initialize an already allocated GEM object of the specified size with |
142 | * no GEM provided backing store. Instead the caller is responsible for |
152 | * no GEM provided backing store. Instead the caller is responsible for |
143 | * backing the object and handling it. |
153 | * backing the object and handling it. |
144 | */ |
154 | */ |
145 | void drm_gem_private_object_init(struct drm_device *dev, |
155 | void drm_gem_private_object_init(struct drm_device *dev, |
146 | struct drm_gem_object *obj, size_t size) |
156 | struct drm_gem_object *obj, size_t size) |
147 | { |
157 | { |
148 | BUG_ON((size & (PAGE_SIZE - 1)) != 0); |
158 | BUG_ON((size & (PAGE_SIZE - 1)) != 0); |
149 | 159 | ||
150 | obj->dev = dev; |
160 | obj->dev = dev; |
151 | obj->filp = NULL; |
161 | obj->filp = NULL; |
152 | 162 | ||
153 | kref_init(&obj->refcount); |
163 | kref_init(&obj->refcount); |
154 | obj->handle_count = 0; |
164 | obj->handle_count = 0; |
155 | obj->size = size; |
165 | obj->size = size; |
156 | drm_vma_node_reset(&obj->vma_node); |
166 | drm_vma_node_reset(&obj->vma_node); |
157 | } |
167 | } |
158 | EXPORT_SYMBOL(drm_gem_private_object_init); |
168 | EXPORT_SYMBOL(drm_gem_private_object_init); |
159 | 169 | ||
160 | /** |
170 | /** |
- | 171 | * drm_gem_object_free - release resources bound to userspace handles |
|
- | 172 | * @obj: GEM object to clean up. |
|
- | 173 | * |
|
161 | * Called after the last handle to the object has been closed |
174 | * Called after the last handle to the object has been closed |
162 | * |
175 | * |
163 | * Removes any name for the object. Note that this must be |
176 | * Removes any name for the object. Note that this must be |
164 | * called before drm_gem_object_free or we'll be touching |
177 | * called before drm_gem_object_free or we'll be touching |
165 | * freed memory |
178 | * freed memory |
166 | */ |
179 | */ |
167 | static void drm_gem_object_handle_free(struct drm_gem_object *obj) |
180 | static void drm_gem_object_handle_free(struct drm_gem_object *obj) |
168 | { |
181 | { |
169 | struct drm_device *dev = obj->dev; |
182 | struct drm_device *dev = obj->dev; |
170 | 183 | ||
171 | /* Remove any name for this object */ |
184 | /* Remove any name for this object */ |
172 | if (obj->name) { |
185 | if (obj->name) { |
173 | idr_remove(&dev->object_name_idr, obj->name); |
186 | idr_remove(&dev->object_name_idr, obj->name); |
174 | obj->name = 0; |
187 | obj->name = 0; |
175 | } |
188 | } |
176 | } |
189 | } |
177 | 190 | ||
178 | 191 | ||
179 | static void |
192 | static void |
180 | drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj) |
193 | drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj) |
181 | { |
194 | { |
182 | if (WARN_ON(obj->handle_count == 0)) |
195 | if (WARN_ON(obj->handle_count == 0)) |
183 | return; |
196 | return; |
184 | 197 | ||
185 | /* |
198 | /* |
186 | * Must bump handle count first as this may be the last |
199 | * Must bump handle count first as this may be the last |
187 | * ref, in which case the object would disappear before we |
200 | * ref, in which case the object would disappear before we |
188 | * checked for a name |
201 | * checked for a name |
189 | */ |
202 | */ |
190 | 203 | ||
191 | mutex_lock(&obj->dev->object_name_lock); |
204 | mutex_lock(&obj->dev->object_name_lock); |
192 | if (--obj->handle_count == 0) { |
205 | if (--obj->handle_count == 0) { |
193 | drm_gem_object_handle_free(obj); |
206 | drm_gem_object_handle_free(obj); |
194 | } |
207 | } |
195 | mutex_unlock(&obj->dev->object_name_lock); |
208 | mutex_unlock(&obj->dev->object_name_lock); |
196 | 209 | ||
197 | drm_gem_object_unreference_unlocked(obj); |
210 | drm_gem_object_unreference_unlocked(obj); |
198 | } |
211 | } |
199 | 212 | ||
200 | /** |
213 | /** |
- | 214 | * drm_gem_handle_delete - deletes the given file-private handle |
|
- | 215 | * @filp: drm file-private structure to use for the handle look up |
|
- | 216 | * @handle: userspace handle to delete |
|
- | 217 | * |
|
201 | * Removes the mapping from handle to filp for this object. |
218 | * Removes the GEM handle from the @filp lookup table and if this is the last |
- | 219 | * handle also cleans up linked resources like GEM names. |
|
202 | */ |
220 | */ |
203 | int |
221 | int |
204 | drm_gem_handle_delete(struct drm_file *filp, u32 handle) |
222 | drm_gem_handle_delete(struct drm_file *filp, u32 handle) |
205 | { |
223 | { |
206 | struct drm_device *dev; |
224 | struct drm_device *dev; |
207 | struct drm_gem_object *obj; |
225 | struct drm_gem_object *obj; |
208 | 226 | ||
209 | /* This is gross. The idr system doesn't let us try a delete and |
227 | /* This is gross. The idr system doesn't let us try a delete and |
210 | * return an error code. It just spews if you fail at deleting. |
228 | * return an error code. It just spews if you fail at deleting. |
211 | * So, we have to grab a lock around finding the object and then |
229 | * So, we have to grab a lock around finding the object and then |
212 | * doing the delete on it and dropping the refcount, or the user |
230 | * doing the delete on it and dropping the refcount, or the user |
213 | * could race us to double-decrement the refcount and cause a |
231 | * could race us to double-decrement the refcount and cause a |
214 | * use-after-free later. Given the frequency of our handle lookups, |
232 | * use-after-free later. Given the frequency of our handle lookups, |
215 | * we may want to use ida for number allocation and a hash table |
233 | * we may want to use ida for number allocation and a hash table |
216 | * for the pointers, anyway. |
234 | * for the pointers, anyway. |
217 | */ |
235 | */ |
218 | spin_lock(&filp->table_lock); |
236 | spin_lock(&filp->table_lock); |
219 | 237 | ||
220 | /* Check if we currently have a reference on the object */ |
238 | /* Check if we currently have a reference on the object */ |
221 | obj = idr_find(&filp->object_idr, handle); |
239 | obj = idr_find(&filp->object_idr, handle); |
222 | if (obj == NULL) { |
240 | if (obj == NULL) { |
223 | spin_unlock(&filp->table_lock); |
241 | spin_unlock(&filp->table_lock); |
224 | return -EINVAL; |
242 | return -EINVAL; |
225 | } |
243 | } |
226 | dev = obj->dev; |
244 | dev = obj->dev; |
227 | 245 | ||
228 | /* Release reference and decrement refcount. */ |
246 | /* Release reference and decrement refcount. */ |
229 | idr_remove(&filp->object_idr, handle); |
247 | idr_remove(&filp->object_idr, handle); |
230 | spin_unlock(&filp->table_lock); |
248 | spin_unlock(&filp->table_lock); |
231 | 249 | ||
232 | // drm_vma_node_revoke(&obj->vma_node, filp->filp); |
250 | // drm_vma_node_revoke(&obj->vma_node, filp->filp); |
233 | 251 | ||
234 | if (dev->driver->gem_close_object) |
252 | if (dev->driver->gem_close_object) |
235 | dev->driver->gem_close_object(obj, filp); |
253 | dev->driver->gem_close_object(obj, filp); |
236 | drm_gem_object_handle_unreference_unlocked(obj); |
254 | drm_gem_object_handle_unreference_unlocked(obj); |
237 | 255 | ||
238 | return 0; |
256 | return 0; |
239 | } |
257 | } |
240 | EXPORT_SYMBOL(drm_gem_handle_delete); |
258 | EXPORT_SYMBOL(drm_gem_handle_delete); |
241 | 259 | ||
242 | /** |
260 | /** |
243 | * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers |
261 | * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers |
- | 262 | * @file: drm file-private structure to remove the dumb handle from |
|
- | 263 | * @dev: corresponding drm_device |
|
- | 264 | * @handle: the dumb handle to remove |
|
244 | * |
265 | * |
245 | * This implements the ->dumb_destroy kms driver callback for drivers which use |
266 | * This implements the ->dumb_destroy kms driver callback for drivers which use |
246 | * gem to manage their backing storage. |
267 | * gem to manage their backing storage. |
247 | */ |
268 | */ |
248 | int drm_gem_dumb_destroy(struct drm_file *file, |
269 | int drm_gem_dumb_destroy(struct drm_file *file, |
249 | struct drm_device *dev, |
270 | struct drm_device *dev, |
250 | uint32_t handle) |
271 | uint32_t handle) |
251 | { |
272 | { |
252 | return drm_gem_handle_delete(file, handle); |
273 | return drm_gem_handle_delete(file, handle); |
253 | } |
274 | } |
254 | EXPORT_SYMBOL(drm_gem_dumb_destroy); |
275 | EXPORT_SYMBOL(drm_gem_dumb_destroy); |
255 | 276 | ||
256 | /** |
277 | /** |
257 | * drm_gem_handle_create_tail - internal functions to create a handle |
278 | * drm_gem_handle_create_tail - internal functions to create a handle |
- | 279 | * @file_priv: drm file-private structure to register the handle for |
|
- | 280 | * @obj: object to register |
|
- | 281 | * @handlep: pionter to return the created handle to the caller |
|
258 | * |
282 | * |
259 | * This expects the dev->object_name_lock to be held already and will drop it |
283 | * This expects the dev->object_name_lock to be held already and will drop it |
260 | * before returning. Used to avoid races in establishing new handles when |
284 | * before returning. Used to avoid races in establishing new handles when |
261 | * importing an object from either an flink name or a dma-buf. |
285 | * importing an object from either an flink name or a dma-buf. |
262 | */ |
286 | */ |
263 | int |
287 | int |
264 | drm_gem_handle_create_tail(struct drm_file *file_priv, |
288 | drm_gem_handle_create_tail(struct drm_file *file_priv, |
265 | struct drm_gem_object *obj, |
289 | struct drm_gem_object *obj, |
266 | u32 *handlep) |
290 | u32 *handlep) |
267 | { |
291 | { |
268 | struct drm_device *dev = obj->dev; |
292 | struct drm_device *dev = obj->dev; |
269 | int ret; |
293 | int ret; |
270 | 294 | ||
271 | WARN_ON(!mutex_is_locked(&dev->object_name_lock)); |
295 | WARN_ON(!mutex_is_locked(&dev->object_name_lock)); |
272 | 296 | ||
273 | /* |
297 | /* |
274 | * Get the user-visible handle using idr. Preload and perform |
298 | * Get the user-visible handle using idr. Preload and perform |
275 | * allocation under our spinlock. |
299 | * allocation under our spinlock. |
276 | */ |
300 | */ |
277 | idr_preload(GFP_KERNEL); |
301 | idr_preload(GFP_KERNEL); |
278 | spin_lock(&file_priv->table_lock); |
302 | spin_lock(&file_priv->table_lock); |
279 | 303 | ||
280 | ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); |
304 | ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT); |
281 | drm_gem_object_reference(obj); |
305 | drm_gem_object_reference(obj); |
282 | obj->handle_count++; |
306 | obj->handle_count++; |
283 | spin_unlock(&file_priv->table_lock); |
307 | spin_unlock(&file_priv->table_lock); |
284 | idr_preload_end(); |
308 | idr_preload_end(); |
285 | mutex_unlock(&dev->object_name_lock); |
309 | mutex_unlock(&dev->object_name_lock); |
286 | if (ret < 0) { |
310 | if (ret < 0) { |
287 | drm_gem_object_handle_unreference_unlocked(obj); |
311 | drm_gem_object_handle_unreference_unlocked(obj); |
288 | return ret; |
312 | return ret; |
289 | } |
313 | } |
290 | *handlep = ret; |
314 | *handlep = ret; |
291 | 315 | ||
292 | // ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp); |
316 | // ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp); |
293 | // if (ret) { |
317 | // if (ret) { |
294 | // drm_gem_handle_delete(file_priv, *handlep); |
318 | // drm_gem_handle_delete(file_priv, *handlep); |
295 | // return ret; |
319 | // return ret; |
296 | // } |
320 | // } |
297 | 321 | ||
298 | if (dev->driver->gem_open_object) { |
322 | if (dev->driver->gem_open_object) { |
299 | ret = dev->driver->gem_open_object(obj, file_priv); |
323 | ret = dev->driver->gem_open_object(obj, file_priv); |
300 | if (ret) { |
324 | if (ret) { |
301 | drm_gem_handle_delete(file_priv, *handlep); |
325 | drm_gem_handle_delete(file_priv, *handlep); |
302 | return ret; |
326 | return ret; |
303 | } |
327 | } |
304 | } |
328 | } |
305 | 329 | ||
306 | return 0; |
330 | return 0; |
307 | } |
331 | } |
308 | 332 | ||
309 | /** |
333 | /** |
- | 334 | * gem_handle_create - create a gem handle for an object |
|
- | 335 | * @file_priv: drm file-private structure to register the handle for |
|
- | 336 | * @obj: object to register |
|
- | 337 | * @handlep: pionter to return the created handle to the caller |
|
- | 338 | * |
|
310 | * Create a handle for this object. This adds a handle reference |
339 | * Create a handle for this object. This adds a handle reference |
311 | * to the object, which includes a regular reference count. Callers |
340 | * to the object, which includes a regular reference count. Callers |
312 | * will likely want to dereference the object afterwards. |
341 | * will likely want to dereference the object afterwards. |
313 | */ |
342 | */ |
314 | int |
343 | int |
315 | drm_gem_handle_create(struct drm_file *file_priv, |
344 | drm_gem_handle_create(struct drm_file *file_priv, |
316 | struct drm_gem_object *obj, |
345 | struct drm_gem_object *obj, |
317 | u32 *handlep) |
346 | u32 *handlep) |
318 | { |
347 | { |
319 | mutex_lock(&obj->dev->object_name_lock); |
348 | mutex_lock(&obj->dev->object_name_lock); |
320 | 349 | ||
321 | return drm_gem_handle_create_tail(file_priv, obj, handlep); |
350 | return drm_gem_handle_create_tail(file_priv, obj, handlep); |
322 | } |
351 | } |
323 | EXPORT_SYMBOL(drm_gem_handle_create); |
352 | EXPORT_SYMBOL(drm_gem_handle_create); |
324 | 353 | ||
325 | #if 0 |
354 | #if 0 |
326 | /** |
355 | /** |
327 | * drm_gem_free_mmap_offset - release a fake mmap offset for an object |
356 | * drm_gem_free_mmap_offset - release a fake mmap offset for an object |
328 | * @obj: obj in question |
357 | * @obj: obj in question |
329 | * |
358 | * |
330 | * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). |
359 | * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). |
331 | */ |
360 | */ |
332 | void |
361 | void |
333 | drm_gem_free_mmap_offset(struct drm_gem_object *obj) |
362 | drm_gem_free_mmap_offset(struct drm_gem_object *obj) |
334 | { |
363 | { |
335 | struct drm_device *dev = obj->dev; |
364 | struct drm_device *dev = obj->dev; |
336 | 365 | ||
337 | drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); |
366 | drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); |
338 | } |
367 | } |
339 | EXPORT_SYMBOL(drm_gem_free_mmap_offset); |
368 | EXPORT_SYMBOL(drm_gem_free_mmap_offset); |
340 | 369 | ||
341 | /** |
370 | /** |
342 | * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object |
371 | * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object |
343 | * @obj: obj in question |
372 | * @obj: obj in question |
344 | * @size: the virtual size |
373 | * @size: the virtual size |
345 | * |
374 | * |
346 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
375 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
347 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
376 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
348 | * up the object based on the offset and sets up the various memory mapping |
377 | * up the object based on the offset and sets up the various memory mapping |
349 | * structures. |
378 | * structures. |
350 | * |
379 | * |
351 | * This routine allocates and attaches a fake offset for @obj, in cases where |
380 | * This routine allocates and attaches a fake offset for @obj, in cases where |
352 | * the virtual size differs from the physical size (ie. obj->size). Otherwise |
381 | * the virtual size differs from the physical size (ie. obj->size). Otherwise |
353 | * just use drm_gem_create_mmap_offset(). |
382 | * just use drm_gem_create_mmap_offset(). |
354 | */ |
383 | */ |
355 | int |
384 | int |
356 | drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) |
385 | drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) |
357 | { |
386 | { |
358 | struct drm_device *dev = obj->dev; |
387 | struct drm_device *dev = obj->dev; |
359 | 388 | ||
360 | return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, |
389 | return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, |
361 | size / PAGE_SIZE); |
390 | size / PAGE_SIZE); |
362 | } |
391 | } |
363 | EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); |
392 | EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); |
364 | 393 | ||
365 | /** |
394 | /** |
366 | * drm_gem_create_mmap_offset - create a fake mmap offset for an object |
395 | * drm_gem_create_mmap_offset - create a fake mmap offset for an object |
367 | * @obj: obj in question |
396 | * @obj: obj in question |
368 | * |
397 | * |
369 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
398 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
370 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
399 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
371 | * up the object based on the offset and sets up the various memory mapping |
400 | * up the object based on the offset and sets up the various memory mapping |
372 | * structures. |
401 | * structures. |
373 | * |
402 | * |
374 | * This routine allocates and attaches a fake offset for @obj. |
403 | * This routine allocates and attaches a fake offset for @obj. |
375 | */ |
404 | */ |
376 | int drm_gem_create_mmap_offset(struct drm_gem_object *obj) |
405 | int drm_gem_create_mmap_offset(struct drm_gem_object *obj) |
377 | { |
406 | { |
378 | return drm_gem_create_mmap_offset_size(obj, obj->size); |
407 | return drm_gem_create_mmap_offset_size(obj, obj->size); |
379 | } |
408 | } |
380 | EXPORT_SYMBOL(drm_gem_create_mmap_offset); |
409 | EXPORT_SYMBOL(drm_gem_create_mmap_offset); |
381 | 410 | ||
382 | /** |
411 | /** |
383 | * drm_gem_get_pages - helper to allocate backing pages for a GEM object |
412 | * drm_gem_get_pages - helper to allocate backing pages for a GEM object |
384 | * from shmem |
413 | * from shmem |
385 | * @obj: obj in question |
414 | * @obj: obj in question |
- | 415 | * |
|
- | 416 | * This reads the page-array of the shmem-backing storage of the given gem |
|
- | 417 | * object. An array of pages is returned. If a page is not allocated or |
|
- | 418 | * swapped-out, this will allocate/swap-in the required pages. Note that the |
|
- | 419 | * whole object is covered by the page-array and pinned in memory. |
|
- | 420 | * |
|
- | 421 | * Use drm_gem_put_pages() to release the array and unpin all pages. |
|
- | 422 | * |
|
- | 423 | * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()). |
|
- | 424 | * If you require other GFP-masks, you have to do those allocations yourself. |
|
- | 425 | * |
|
- | 426 | * Note that you are not allowed to change gfp-zones during runtime. That is, |
|
- | 427 | * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as |
|
- | 428 | * set during initialization. If you have special zone constraints, set them |
|
- | 429 | * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care |
|
386 | * @gfpmask: gfp mask of requested pages |
430 | * to keep pages in the required zone during swap-in. |
387 | */ |
431 | */ |
388 | struct page **drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask) |
432 | struct page **drm_gem_get_pages(struct drm_gem_object *obj) |
389 | { |
433 | { |
390 | struct inode *inode; |
- | |
391 | struct address_space *mapping; |
434 | struct address_space *mapping; |
392 | struct page *p, **pages; |
435 | struct page *p, **pages; |
393 | int i, npages; |
436 | int i, npages; |
394 | 437 | ||
395 | /* This is the shared memory object that backs the GEM resource */ |
438 | /* This is the shared memory object that backs the GEM resource */ |
396 | inode = file_inode(obj->filp); |
- | |
397 | mapping = inode->i_mapping; |
439 | mapping = file_inode(obj->filp)->i_mapping; |
398 | 440 | ||
399 | /* We already BUG_ON() for non-page-aligned sizes in |
441 | /* We already BUG_ON() for non-page-aligned sizes in |
400 | * drm_gem_object_init(), so we should never hit this unless |
442 | * drm_gem_object_init(), so we should never hit this unless |
401 | * driver author is doing something really wrong: |
443 | * driver author is doing something really wrong: |
402 | */ |
444 | */ |
403 | WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); |
445 | WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); |
404 | 446 | ||
405 | npages = obj->size >> PAGE_SHIFT; |
447 | npages = obj->size >> PAGE_SHIFT; |
406 | 448 | ||
407 | pages = drm_malloc_ab(npages, sizeof(struct page *)); |
449 | pages = drm_malloc_ab(npages, sizeof(struct page *)); |
408 | if (pages == NULL) |
450 | if (pages == NULL) |
409 | return ERR_PTR(-ENOMEM); |
451 | return ERR_PTR(-ENOMEM); |
410 | - | ||
411 | gfpmask |= mapping_gfp_mask(mapping); |
- | |
412 | 452 | ||
413 | for (i = 0; i < npages; i++) { |
453 | for (i = 0; i < npages; i++) { |
414 | p = shmem_read_mapping_page_gfp(mapping, i, gfpmask); |
454 | p = shmem_read_mapping_page(mapping, i); |
415 | if (IS_ERR(p)) |
455 | if (IS_ERR(p)) |
416 | goto fail; |
456 | goto fail; |
417 | pages[i] = p; |
457 | pages[i] = p; |
418 | - | ||
419 | /* There is a hypothetical issue w/ drivers that require |
458 | |
420 | * buffer memory in the low 4GB.. if the pages are un- |
- | |
421 | * pinned, and swapped out, they can end up swapped back |
- | |
422 | * in above 4GB. If pages are already in memory, then |
- | |
423 | * shmem_read_mapping_page_gfp will ignore the gfpmask, |
- | |
424 | * even if the already in-memory page disobeys the mask. |
- | |
425 | * |
- | |
426 | * It is only a theoretical issue today, because none of |
- | |
427 | * the devices with this limitation can be populated with |
459 | /* Make sure shmem keeps __GFP_DMA32 allocated pages in the |
428 | * enough memory to trigger the issue. But this BUG_ON() |
460 | * correct region during swapin. Note that this requires |
429 | * is here as a reminder in case the problem with |
461 | * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping) |
430 | * shmem_read_mapping_page_gfp() isn't solved by the time |
- | |
431 | * it does become a real issue. |
- | |
432 | * |
- | |
433 | * See this thread: http://lkml.org/lkml/2011/7/11/238 |
462 | * so shmem can relocate pages during swapin if required. |
434 | */ |
463 | */ |
435 | BUG_ON((gfpmask & __GFP_DMA32) && |
464 | BUG_ON((mapping_gfp_mask(mapping) & __GFP_DMA32) && |
436 | (page_to_pfn(p) >= 0x00100000UL)); |
465 | (page_to_pfn(p) >= 0x00100000UL)); |
437 | } |
466 | } |
438 | 467 | ||
439 | return pages; |
468 | return pages; |
440 | 469 | ||
441 | fail: |
470 | fail: |
442 | while (i--) |
471 | while (i--) |
443 | page_cache_release(pages[i]); |
472 | page_cache_release(pages[i]); |
444 | 473 | ||
445 | drm_free_large(pages); |
474 | drm_free_large(pages); |
446 | return ERR_CAST(p); |
475 | return ERR_CAST(p); |
447 | } |
476 | } |
448 | EXPORT_SYMBOL(drm_gem_get_pages); |
477 | EXPORT_SYMBOL(drm_gem_get_pages); |
449 | 478 | ||
450 | /** |
479 | /** |
451 | * drm_gem_put_pages - helper to free backing pages for a GEM object |
480 | * drm_gem_put_pages - helper to free backing pages for a GEM object |
452 | * @obj: obj in question |
481 | * @obj: obj in question |
453 | * @pages: pages to free |
482 | * @pages: pages to free |
454 | * @dirty: if true, pages will be marked as dirty |
483 | * @dirty: if true, pages will be marked as dirty |
455 | * @accessed: if true, the pages will be marked as accessed |
484 | * @accessed: if true, the pages will be marked as accessed |
456 | */ |
485 | */ |
457 | void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, |
486 | void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, |
458 | bool dirty, bool accessed) |
487 | bool dirty, bool accessed) |
459 | { |
488 | { |
460 | int i, npages; |
489 | int i, npages; |
461 | 490 | ||
462 | /* We already BUG_ON() for non-page-aligned sizes in |
491 | /* We already BUG_ON() for non-page-aligned sizes in |
463 | * drm_gem_object_init(), so we should never hit this unless |
492 | * drm_gem_object_init(), so we should never hit this unless |
464 | * driver author is doing something really wrong: |
493 | * driver author is doing something really wrong: |
465 | */ |
494 | */ |
466 | WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); |
495 | WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); |
467 | 496 | ||
468 | npages = obj->size >> PAGE_SHIFT; |
497 | npages = obj->size >> PAGE_SHIFT; |
469 | 498 | ||
470 | for (i = 0; i < npages; i++) { |
499 | for (i = 0; i < npages; i++) { |
471 | if (dirty) |
500 | if (dirty) |
472 | set_page_dirty(pages[i]); |
501 | set_page_dirty(pages[i]); |
473 | 502 | ||
474 | if (accessed) |
503 | if (accessed) |
475 | mark_page_accessed(pages[i]); |
504 | mark_page_accessed(pages[i]); |
476 | 505 | ||
477 | /* Undo the reference we took when populating the table */ |
506 | /* Undo the reference we took when populating the table */ |
478 | page_cache_release(pages[i]); |
507 | page_cache_release(pages[i]); |
479 | } |
508 | } |
480 | 509 | ||
481 | drm_free_large(pages); |
510 | drm_free_large(pages); |
482 | } |
511 | } |
483 | EXPORT_SYMBOL(drm_gem_put_pages); |
512 | EXPORT_SYMBOL(drm_gem_put_pages); |
484 | #endif |
513 | #endif |
485 | 514 | ||
486 | /** Returns a reference to the object named by the handle. */ |
515 | /** Returns a reference to the object named by the handle. */ |
487 | struct drm_gem_object * |
516 | struct drm_gem_object * |
488 | drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, |
517 | drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp, |
489 | u32 handle) |
518 | u32 handle) |
490 | { |
519 | { |
491 | struct drm_gem_object *obj; |
520 | struct drm_gem_object *obj; |
492 | 521 | ||
493 | spin_lock(&filp->table_lock); |
522 | spin_lock(&filp->table_lock); |
494 | 523 | ||
495 | /* Check if we currently have a reference on the object */ |
524 | /* Check if we currently have a reference on the object */ |
496 | obj = idr_find(&filp->object_idr, handle); |
525 | obj = idr_find(&filp->object_idr, handle); |
497 | if (obj == NULL) { |
526 | if (obj == NULL) { |
498 | spin_unlock(&filp->table_lock); |
527 | spin_unlock(&filp->table_lock); |
499 | return NULL; |
528 | return NULL; |
500 | } |
529 | } |
501 | 530 | ||
502 | drm_gem_object_reference(obj); |
531 | drm_gem_object_reference(obj); |
503 | 532 | ||
504 | spin_unlock(&filp->table_lock); |
533 | spin_unlock(&filp->table_lock); |
505 | 534 | ||
506 | return obj; |
535 | return obj; |
507 | } |
536 | } |
508 | EXPORT_SYMBOL(drm_gem_object_lookup); |
537 | EXPORT_SYMBOL(drm_gem_object_lookup); |
509 | 538 | ||
510 | /** |
539 | /** |
- | 540 | * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl |
|
- | 541 | * @dev: drm_device |
|
- | 542 | * @data: ioctl data |
|
- | 543 | * @file_priv: drm file-private structure |
|
- | 544 | * |
|
511 | * Releases the handle to an mm object. |
545 | * Releases the handle to an mm object. |
512 | */ |
546 | */ |
513 | int |
547 | int |
514 | drm_gem_close_ioctl(struct drm_device *dev, void *data, |
548 | drm_gem_close_ioctl(struct drm_device *dev, void *data, |
515 | struct drm_file *file_priv) |
549 | struct drm_file *file_priv) |
516 | { |
550 | { |
517 | struct drm_gem_close *args = data; |
551 | struct drm_gem_close *args = data; |
518 | int ret; |
552 | int ret; |
- | 553 | ||
- | 554 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
|
- | 555 | return -ENODEV; |
|
519 | 556 | ||
520 | ret = drm_gem_handle_delete(file_priv, args->handle); |
557 | ret = drm_gem_handle_delete(file_priv, args->handle); |
521 | 558 | ||
522 | return ret; |
559 | return ret; |
523 | } |
560 | } |
524 | 561 | ||
525 | /** |
562 | /** |
- | 563 | * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl |
|
- | 564 | * @dev: drm_device |
|
- | 565 | * @data: ioctl data |
|
- | 566 | * @file_priv: drm file-private structure |
|
- | 567 | * |
|
526 | * Create a global name for an object, returning the name. |
568 | * Create a global name for an object, returning the name. |
527 | * |
569 | * |
528 | * Note that the name does not hold a reference; when the object |
570 | * Note that the name does not hold a reference; when the object |
529 | * is freed, the name goes away. |
571 | * is freed, the name goes away. |
530 | */ |
572 | */ |
531 | int |
573 | int |
532 | drm_gem_flink_ioctl(struct drm_device *dev, void *data, |
574 | drm_gem_flink_ioctl(struct drm_device *dev, void *data, |
533 | struct drm_file *file_priv) |
575 | struct drm_file *file_priv) |
534 | { |
576 | { |
535 | struct drm_gem_flink *args = data; |
577 | struct drm_gem_flink *args = data; |
536 | struct drm_gem_object *obj; |
578 | struct drm_gem_object *obj; |
537 | int ret; |
579 | int ret; |
538 | 580 | ||
539 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
581 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
540 | return -ENODEV; |
582 | return -ENODEV; |
541 | 583 | ||
542 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
584 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
543 | if (obj == NULL) |
585 | if (obj == NULL) |
544 | return -ENOENT; |
586 | return -ENOENT; |
545 | 587 | ||
546 | mutex_lock(&dev->object_name_lock); |
588 | mutex_lock(&dev->object_name_lock); |
547 | idr_preload(GFP_KERNEL); |
589 | idr_preload(GFP_KERNEL); |
548 | /* prevent races with concurrent gem_close. */ |
590 | /* prevent races with concurrent gem_close. */ |
549 | if (obj->handle_count == 0) { |
591 | if (obj->handle_count == 0) { |
550 | ret = -ENOENT; |
592 | ret = -ENOENT; |
551 | goto err; |
593 | goto err; |
552 | } |
594 | } |
553 | 595 | ||
554 | if (!obj->name) { |
596 | if (!obj->name) { |
555 | ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT); |
597 | ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_NOWAIT); |
556 | if (ret < 0) |
598 | if (ret < 0) |
557 | goto err; |
599 | goto err; |
558 | 600 | ||
559 | obj->name = ret; |
601 | obj->name = ret; |
560 | } |
602 | } |
561 | 603 | ||
562 | args->name = (uint64_t) obj->name; |
604 | args->name = (uint64_t) obj->name; |
563 | ret = 0; |
605 | ret = 0; |
564 | 606 | ||
565 | err: |
607 | err: |
566 | idr_preload_end(); |
608 | idr_preload_end(); |
567 | mutex_unlock(&dev->object_name_lock); |
609 | mutex_unlock(&dev->object_name_lock); |
568 | drm_gem_object_unreference_unlocked(obj); |
610 | drm_gem_object_unreference_unlocked(obj); |
569 | return ret; |
611 | return ret; |
570 | } |
612 | } |
571 | 613 | ||
572 | /** |
614 | /** |
- | 615 | * drm_gem_open - implementation of the GEM_OPEN ioctl |
|
- | 616 | * @dev: drm_device |
|
- | 617 | * @data: ioctl data |
|
- | 618 | * @file_priv: drm file-private structure |
|
- | 619 | * |
|
573 | * Open an object using the global name, returning a handle and the size. |
620 | * Open an object using the global name, returning a handle and the size. |
574 | * |
621 | * |
575 | * This handle (of course) holds a reference to the object, so the object |
622 | * This handle (of course) holds a reference to the object, so the object |
576 | * will not go away until the handle is deleted. |
623 | * will not go away until the handle is deleted. |
577 | */ |
624 | */ |
578 | int |
625 | int |
579 | drm_gem_open_ioctl(struct drm_device *dev, void *data, |
626 | drm_gem_open_ioctl(struct drm_device *dev, void *data, |
580 | struct drm_file *file_priv) |
627 | struct drm_file *file_priv) |
581 | { |
628 | { |
582 | struct drm_gem_open *args = data; |
629 | struct drm_gem_open *args = data; |
583 | struct drm_gem_object *obj; |
630 | struct drm_gem_object *obj; |
584 | int ret; |
631 | int ret; |
585 | u32 handle; |
632 | u32 handle; |
586 | 633 | ||
587 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
634 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
588 | return -ENODEV; |
635 | return -ENODEV; |
589 | 636 | ||
590 | mutex_lock(&dev->object_name_lock); |
637 | mutex_lock(&dev->object_name_lock); |
591 | obj = idr_find(&dev->object_name_idr, (int) args->name); |
638 | obj = idr_find(&dev->object_name_idr, (int) args->name); |
592 | if (obj) { |
639 | if (obj) { |
593 | drm_gem_object_reference(obj); |
640 | drm_gem_object_reference(obj); |
594 | } else { |
641 | } else { |
595 | mutex_unlock(&dev->object_name_lock); |
642 | mutex_unlock(&dev->object_name_lock); |
596 | return -ENOENT; |
643 | return -ENOENT; |
597 | } |
644 | } |
598 | 645 | ||
599 | /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ |
646 | /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ |
600 | ret = drm_gem_handle_create_tail(file_priv, obj, &handle); |
647 | ret = drm_gem_handle_create_tail(file_priv, obj, &handle); |
601 | drm_gem_object_unreference_unlocked(obj); |
648 | drm_gem_object_unreference_unlocked(obj); |
602 | if (ret) |
649 | if (ret) |
603 | return ret; |
650 | return ret; |
604 | 651 | ||
605 | args->handle = handle; |
652 | args->handle = handle; |
606 | args->size = obj->size; |
653 | args->size = obj->size; |
607 | 654 | ||
608 | return 0; |
655 | return 0; |
609 | } |
656 | } |
610 | 657 | ||
611 | #if 0 |
658 | #if 0 |
612 | /** |
659 | /** |
- | 660 | * gem_gem_open - initalizes GEM file-private structures at devnode open time |
|
- | 661 | * @dev: drm_device which is being opened by userspace |
|
- | 662 | * @file_private: drm file-private structure to set up |
|
- | 663 | * |
|
613 | * Called at device open time, sets up the structure for handling refcounting |
664 | * Called at device open time, sets up the structure for handling refcounting |
614 | * of mm objects. |
665 | * of mm objects. |
615 | */ |
666 | */ |
616 | void |
667 | void |
617 | drm_gem_open(struct drm_device *dev, struct drm_file *file_private) |
668 | drm_gem_open(struct drm_device *dev, struct drm_file *file_private) |
618 | { |
669 | { |
619 | idr_init(&file_private->object_idr); |
670 | idr_init(&file_private->object_idr); |
620 | spin_lock_init(&file_private->table_lock); |
671 | spin_lock_init(&file_private->table_lock); |
621 | } |
672 | } |
622 | 673 | ||
623 | /** |
674 | /* |
624 | * Called at device close to release the file's |
675 | * Called at device close to release the file's |
625 | * handle references on objects. |
676 | * handle references on objects. |
626 | */ |
677 | */ |
627 | static int |
678 | static int |
628 | drm_gem_object_release_handle(int id, void *ptr, void *data) |
679 | drm_gem_object_release_handle(int id, void *ptr, void *data) |
629 | { |
680 | { |
630 | struct drm_file *file_priv = data; |
681 | struct drm_file *file_priv = data; |
631 | struct drm_gem_object *obj = ptr; |
682 | struct drm_gem_object *obj = ptr; |
632 | struct drm_device *dev = obj->dev; |
683 | struct drm_device *dev = obj->dev; |
633 | 684 | ||
634 | drm_vma_node_revoke(&obj->vma_node, file_priv->filp); |
685 | drm_vma_node_revoke(&obj->vma_node, file_priv->filp); |
635 | 686 | ||
636 | if (dev->driver->gem_close_object) |
687 | if (dev->driver->gem_close_object) |
637 | dev->driver->gem_close_object(obj, file_priv); |
688 | dev->driver->gem_close_object(obj, file_priv); |
638 | 689 | ||
639 | drm_gem_object_handle_unreference_unlocked(obj); |
690 | drm_gem_object_handle_unreference_unlocked(obj); |
640 | 691 | ||
641 | return 0; |
692 | return 0; |
642 | } |
693 | } |
643 | 694 | ||
644 | /** |
695 | /** |
- | 696 | * drm_gem_release - release file-private GEM resources |
|
- | 697 | * @dev: drm_device which is being closed by userspace |
|
- | 698 | * @file_private: drm file-private structure to clean up |
|
- | 699 | * |
|
645 | * Called at close time when the filp is going away. |
700 | * Called at close time when the filp is going away. |
646 | * |
701 | * |
647 | * Releases any remaining references on objects by this filp. |
702 | * Releases any remaining references on objects by this filp. |
648 | */ |
703 | */ |
649 | void |
704 | void |
650 | drm_gem_release(struct drm_device *dev, struct drm_file *file_private) |
705 | drm_gem_release(struct drm_device *dev, struct drm_file *file_private) |
651 | { |
706 | { |
652 | idr_for_each(&file_private->object_idr, |
707 | idr_for_each(&file_private->object_idr, |
653 | &drm_gem_object_release_handle, file_private); |
708 | &drm_gem_object_release_handle, file_private); |
654 | idr_destroy(&file_private->object_idr); |
709 | idr_destroy(&file_private->object_idr); |
655 | } |
710 | } |
656 | #endif |
711 | #endif |
657 | 712 | ||
658 | void |
713 | void |
659 | drm_gem_object_release(struct drm_gem_object *obj) |
714 | drm_gem_object_release(struct drm_gem_object *obj) |
660 | { |
715 | { |
- | 716 | WARN_ON(obj->dma_buf); |
|
- | 717 | ||
661 | if (obj->filp) |
718 | if (obj->filp) |
662 | free(obj->filp); |
719 | free(obj->filp); |
663 | } |
720 | } |
664 | EXPORT_SYMBOL(drm_gem_object_release); |
721 | EXPORT_SYMBOL(drm_gem_object_release); |
665 | 722 | ||
666 | /** |
723 | /** |
- | 724 | * drm_gem_object_free - free a GEM object |
|
- | 725 | * @kref: kref of the object to free |
|
- | 726 | * |
|
667 | * Called after the last reference to the object has been lost. |
727 | * Called after the last reference to the object has been lost. |
668 | * Must be called holding struct_ mutex |
728 | * Must be called holding struct_ mutex |
669 | * |
729 | * |
670 | * Frees the object |
730 | * Frees the object |
671 | */ |
731 | */ |
672 | void |
732 | void |
673 | drm_gem_object_free(struct kref *kref) |
733 | drm_gem_object_free(struct kref *kref) |
674 | { |
734 | { |
675 | struct drm_gem_object *obj = (struct drm_gem_object *) kref; |
735 | struct drm_gem_object *obj = (struct drm_gem_object *) kref; |
676 | struct drm_device *dev = obj->dev; |
736 | struct drm_device *dev = obj->dev; |
677 | 737 | ||
678 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
738 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
679 | 739 | ||
680 | if (dev->driver->gem_free_object != NULL) |
740 | if (dev->driver->gem_free_object != NULL) |
681 | dev->driver->gem_free_object(obj); |
741 | dev->driver->gem_free_object(obj); |
682 | } |
742 | } |
683 | EXPORT_SYMBOL(drm_gem_object_free); |
743 | EXPORT_SYMBOL(drm_gem_object_free); |
684 | 744 | ||
685 | 745 | ||
686 | #if 0 |
746 | #if 0 |
687 | void drm_gem_vm_open(struct vm_area_struct *vma) |
747 | void drm_gem_vm_open(struct vm_area_struct *vma) |
688 | { |
748 | { |
689 | struct drm_gem_object *obj = vma->vm_private_data; |
749 | struct drm_gem_object *obj = vma->vm_private_data; |
690 | 750 | ||
691 | drm_gem_object_reference(obj); |
751 | drm_gem_object_reference(obj); |
692 | 752 | ||
693 | mutex_lock(&obj->dev->struct_mutex); |
753 | mutex_lock(&obj->dev->struct_mutex); |
694 | drm_vm_open_locked(obj->dev, vma); |
754 | drm_vm_open_locked(obj->dev, vma); |
695 | mutex_unlock(&obj->dev->struct_mutex); |
755 | mutex_unlock(&obj->dev->struct_mutex); |
696 | } |
756 | } |
697 | EXPORT_SYMBOL(drm_gem_vm_open); |
757 | EXPORT_SYMBOL(drm_gem_vm_open); |
698 | 758 | ||
699 | void drm_gem_vm_close(struct vm_area_struct *vma) |
759 | void drm_gem_vm_close(struct vm_area_struct *vma) |
700 | { |
760 | { |
701 | struct drm_gem_object *obj = vma->vm_private_data; |
761 | struct drm_gem_object *obj = vma->vm_private_data; |
702 | struct drm_device *dev = obj->dev; |
762 | struct drm_device *dev = obj->dev; |
703 | 763 | ||
704 | mutex_lock(&dev->struct_mutex); |
764 | mutex_lock(&dev->struct_mutex); |
705 | drm_vm_close_locked(obj->dev, vma); |
765 | drm_vm_close_locked(obj->dev, vma); |
706 | drm_gem_object_unreference(obj); |
766 | drm_gem_object_unreference(obj); |
707 | mutex_unlock(&dev->struct_mutex); |
767 | mutex_unlock(&dev->struct_mutex); |
708 | } |
768 | } |
709 | EXPORT_SYMBOL(drm_gem_vm_close); |
769 | EXPORT_SYMBOL(drm_gem_vm_close); |
710 | 770 | ||
711 | #endif>>>> |
771 | #endif>>>> |