1 /**
2 * \file drm_fops.h
3 * File operations for DRM
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
8 */
9
10 /*
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
12 *
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice (including the next
25 * paragraph) shall be included in all copies or substantial portions of the
26 * Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
35 */
36
37 #include "drmP.h"
38 #include <linux/poll.h>
39
40 static int drm_setup( drm_device_t *dev )
41 {
42 int i;
43 int ret;
44
45 if (dev->driver->presetup)
46 {
47 ret=dev->driver->presetup(dev);
48 if (ret!=0)
49 return ret;
50 }
51
52 atomic_set( &dev->ioctl_count, 0 );
53 atomic_set( &dev->vma_count, 0 );
54 dev->buf_use = 0;
55 atomic_set( &dev->buf_alloc, 0 );
56
57 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
58 {
59 i = drm_dma_setup( dev );
60 if ( i < 0 )
61 return i;
62 }
63
64 for ( i = 0 ; i < DRM_ARRAY_SIZE(dev->counts) ; i++ )
65 atomic_set( &dev->counts[i], 0 );
66
67 for ( i = 0 ; i < DRM_HASH_SIZE ; i++ ) {
68 dev->magiclist[i].head = NULL;
69 dev->magiclist[i].tail = NULL;
70 }
71
72 dev->maplist = drm_alloc(sizeof(*dev->maplist),
73 DRM_MEM_MAPS);
74 if(dev->maplist == NULL) return -ENOMEM;
75 memset(dev->maplist, 0, sizeof(*dev->maplist));
76 INIT_LIST_HEAD(&dev->maplist->head);
77
78 dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist),
79 DRM_MEM_CTXLIST);
80 if(dev->ctxlist == NULL) return -ENOMEM;
81 memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
82 INIT_LIST_HEAD(&dev->ctxlist->head);
83
84 dev->vmalist = NULL;
85 dev->sigdata.lock = dev->lock.hw_lock = NULL;
86 init_waitqueue_head( &dev->lock.lock_queue );
87 dev->queue_count = 0;
88 dev->queue_reserved = 0;
89 dev->queue_slots = 0;
90 dev->queuelist = NULL;
91 dev->irq_enabled = 0;
92 dev->context_flag = 0;
93 dev->interrupt_flag = 0;
94 dev->dma_flag = 0;
95 dev->last_context = 0;
96 dev->last_switch = 0;
97 dev->last_checked = 0;
98 init_waitqueue_head( &dev->context_wait );
99 dev->if_version = 0;
100
101 dev->ctx_start = 0;
102 dev->lck_start = 0;
103
104 dev->buf_rp = dev->buf;
105 dev->buf_wp = dev->buf;
106 dev->buf_end = dev->buf + DRM_BSZ;
107 dev->buf_async = NULL;
108 init_waitqueue_head( &dev->buf_readers );
109 init_waitqueue_head( &dev->buf_writers );
110
111 DRM_DEBUG( "\n" );
112
113 /*
114 * The kernel's context could be created here, but is now created
115 * in drm_dma_enqueue. This is more resource-efficient for
116 * hardware that does not do DMA, but may mean that
117 * drm_select_queue fails between the time the interrupt is
118 * initialized and the time the queues are initialized.
119 */
120 if (dev->driver->postsetup)
121 dev->driver->postsetup(dev);
122
123 return 0;
124 }
125
126 /**
127 * Open file.
128 *
129 * \param inode device inode
130 * \param filp file pointer.
131 * \return zero on success or a negative number on failure.
132 *
133 * Searches the DRM device with the same minor number, calls open_helper(), and
134 * increments the device open count. If the open count was previous at zero,
135 * i.e., it's the first that the device is open, then calls setup().
136 */
137 int drm_open( struct inode *inode, struct file *filp )
138 {
139 drm_device_t *dev = NULL;
140 int minor = iminor(inode);
141 int retcode = 0;
142
143 if (!((minor >= 0) && (minor < drm_cards_limit)))
144 return -ENODEV;
145
146 dev = drm_minors[minor].dev;
147 if (!dev)
148 return -ENODEV;
149
150 retcode = drm_open_helper( inode, filp, dev );
151 if ( !retcode ) {
152 atomic_inc( &dev->counts[_DRM_STAT_OPENS] );
153 spin_lock( &dev->count_lock );
154 if ( !dev->open_count++ ) {
155 spin_unlock( &dev->count_lock );
156 return drm_setup( dev );
157 }
158 spin_unlock( &dev->count_lock );
159 }
160
161 return retcode;
162 }
163 EXPORT_SYMBOL(drm_open);
164
165 /**
166 * Release file.
167 *
168 * \param inode device inode
169 * \param filp file pointer.
170 * \return zero on success or a negative number on failure.
171 *
172 * If the hardware lock is held then free it, and take it again for the kernel
173 * context since it's necessary to reclaim buffers. Unlink the file private
174 * data from its list and free it. Decreases the open count and if it reaches
175 * zero calls takedown().
176 */
177 int drm_release( struct inode *inode, struct file *filp )
178 {
179 drm_file_t *priv = filp->private_data;
180 drm_device_t *dev;
181 int retcode = 0;
182
183 lock_kernel();
184 dev = priv->dev;
185
186 DRM_DEBUG( "open_count = %d\n", dev->open_count );
187
188 if (dev->driver->prerelease)
189 dev->driver->prerelease(dev, filp);
190
191 /* ========================================================
192 * Begin inline drm_release
193 */
194
195 DRM_DEBUG( "pid = %d, device = 0x%lx, open_count = %d\n",
196 current->pid, (long)old_encode_dev(dev->device), dev->open_count );
197
198 if ( priv->lock_count && dev->lock.hw_lock &&
199 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
200 dev->lock.filp == filp ) {
201 DRM_DEBUG( "File %p released, freeing lock for context %d\n",
202 filp,
203 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
204
205 if (dev->driver->release)
206 dev->driver->release(dev, filp);
207
208 drm_lock_free( dev, &dev->lock.hw_lock->lock,
209 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) );
210
211 /* FIXME: may require heavy-handed reset of
212 hardware at this point, possibly
213 processed via a callback to the X
214 server. */
215 }
216 else if ( dev->driver->release && priv->lock_count && dev->lock.hw_lock ) {
217 /* The lock is required to reclaim buffers */
218 DECLARE_WAITQUEUE( entry, current );
219
220 add_wait_queue( &dev->lock.lock_queue, &entry );
221 for (;;) {
222 __set_current_state(TASK_INTERRUPTIBLE);
223 if ( !dev->lock.hw_lock ) {
224 /* Device has been unregistered */
225 retcode = -EINTR;
226 break;
227 }
228 if ( drm_lock_take( &dev->lock.hw_lock->lock,
229 DRM_KERNEL_CONTEXT ) ) {
230 dev->lock.filp = filp;
231 dev->lock.lock_time = jiffies;
232 atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
233 break; /* Got lock */
234 }
235 /* Contention */
236 schedule();
237 if ( signal_pending( current ) ) {
238 retcode = -ERESTARTSYS;
239 break;
240 }
241 }
242 __set_current_state(TASK_RUNNING);
243 remove_wait_queue( &dev->lock.lock_queue, &entry );
244 if( !retcode ) {
245 if (dev->driver->release)
246 dev->driver->release(dev, filp);
247 drm_lock_free( dev, &dev->lock.hw_lock->lock,
248 DRM_KERNEL_CONTEXT );
249 }
250 }
251
252 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
253 {
254 dev->driver->reclaim_buffers(dev, filp);
255 }
256
257 drm_fasync( -1, filp, 0 );
258
259 down( &dev->ctxlist_sem );
260 if ( !list_empty( &dev->ctxlist->head ) ) {
261 drm_ctx_list_t *pos, *n;
262
263 list_for_each_entry_safe( pos, n, &dev->ctxlist->head, head ) {
264 if ( pos->tag == priv &&
265 pos->handle != DRM_KERNEL_CONTEXT ) {
266 if (dev->driver->context_dtor)
267 dev->driver->context_dtor(dev, pos->handle);
268
269 drm_ctxbitmap_free( dev, pos->handle );
270
271 list_del( &pos->head );
272 drm_free( pos, sizeof(*pos), DRM_MEM_CTXLIST );
273 --dev->ctx_count;
274 }
275 }
276 }
277 up( &dev->ctxlist_sem );
278
279 down( &dev->struct_sem );
280 if ( priv->remove_auth_on_close == 1 ) {
281 drm_file_t *temp = dev->file_first;
282 while ( temp ) {
283 temp->authenticated = 0;
284 temp = temp->next;
285 }
286 }
287 if ( priv->prev ) {
288 priv->prev->next = priv->next;
289 } else {
290 dev->file_first = priv->next;
291 }
292 if ( priv->next ) {
293 priv->next->prev = priv->prev;
294 } else {
295 dev->file_last = priv->prev;
296 }
297 up( &dev->struct_sem );
298
299 if (dev->driver->free_filp_priv)
300 dev->driver->free_filp_priv(dev, priv);
301
302 drm_free( priv, sizeof(*priv), DRM_MEM_FILES );
303
304 /* ========================================================
305 * End inline drm_release
306 */
307
308 atomic_inc( &dev->counts[_DRM_STAT_CLOSES] );
309 spin_lock( &dev->count_lock );
310 if ( !--dev->open_count ) {
311 if ( atomic_read( &dev->ioctl_count ) || dev->blocked ) {
312 DRM_ERROR( "Device busy: %d %d\n",
313 atomic_read( &dev->ioctl_count ),
314 dev->blocked );
315 spin_unlock( &dev->count_lock );
316 unlock_kernel();
317 return -EBUSY;
318 }
319 spin_unlock( &dev->count_lock );
320 unlock_kernel();
321 return drm_takedown( dev );
322 }
323 spin_unlock( &dev->count_lock );
324
325 unlock_kernel();
326
327 return retcode;
328 }
329 EXPORT_SYMBOL(drm_release);
330
331 /**
332 * Called whenever a process opens /dev/drm.
333 *
334 * \param inode device inode.
335 * \param filp file pointer.
336 * \param dev device.
337 * \return zero on success or a negative number on failure.
338 *
339 * Creates and initializes a drm_file structure for the file private data in \p
340 * filp and add it into the double linked list in \p dev.
341 */
342 int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t *dev)
343 {
344 int minor = iminor(inode);
345 drm_file_t *priv;
346 int ret;
347
348 if (filp->f_flags & O_EXCL) return -EBUSY; /* No exclusive opens */
349 if (!drm_cpu_valid()) return -EINVAL;
350
351 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
352
353 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
354 if(!priv) return -ENOMEM;
355
356 memset(priv, 0, sizeof(*priv));
357 filp->private_data = priv;
358 priv->uid = current->euid;
359 priv->pid = current->pid;
360 priv->minor = minor;
361 priv->dev = dev;
362 priv->ioctl_count = 0;
363 priv->authenticated = capable(CAP_SYS_ADMIN);
364 priv->lock_count = 0;
365
366 if (dev->driver->open_helper) {
367 ret=dev->driver->open_helper(dev, priv);
368 if (ret < 0)
369 goto out_free;
370 }
371
372 down(&dev->struct_sem);
373 if (!dev->file_last) {
374 priv->next = NULL;
375 priv->prev = NULL;
376 dev->file_first = priv;
377 dev->file_last = priv;
378 } else {
379 priv->next = NULL;
380 priv->prev = dev->file_last;
381 dev->file_last->next = priv;
382 dev->file_last = priv;
383 }
384 up(&dev->struct_sem);
385
386 #ifdef __alpha__
387 /*
388 * Default the hose
389 */
390 if (!dev->hose) {
391 struct pci_dev *pci_dev;
392 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
393 if (pci_dev) {
394 dev->hose = pci_dev->sysdata;
395 pci_dev_put(pci_dev);
396 }
397 if (!dev->hose) {
398 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
399 if (b) dev->hose = b->sysdata;
400 }
401 }
402 #endif
403
404 return 0;
405 out_free:
406 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
407 filp->private_data=NULL;
408 return ret;
409 }
410
411 /** No-op. */
412 int drm_flush(struct file *filp)
413 {
414 drm_file_t *priv = filp->private_data;
415 drm_device_t *dev = priv->dev;
416
417 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
418 current->pid, (long)old_encode_dev(dev->device), dev->open_count);
419 return 0;
420 }
421 EXPORT_SYMBOL(drm_flush);
422
423 /** No-op. */
424 int drm_fasync(int fd, struct file *filp, int on)
425 {
426 drm_file_t *priv = filp->private_data;
427 drm_device_t *dev = priv->dev;
428 int retcode;
429
430 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd, (long)old_encode_dev(dev->device));
431 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
432 if (retcode < 0) return retcode;
433 return 0;
434 }
435 EXPORT_SYMBOL(drm_fasync);
436
437 /** No-op. */
438 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
439 {
440 return 0;
441 }
442 EXPORT_SYMBOL(drm_poll);
443
444
445 /** No-op. */
446 ssize_t drm_read(struct file *filp, char __user *buf, size_t count, loff_t *off)
447 {
448 return 0;
449 }
450
|
This page was automatically generated by the
LXR engine.
|