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