3 * File operations for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
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:
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
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.
38 #include "drm_sarea.h"
39 #include <linux/poll.h>
41 static int drm_open_helper(struct inode
*inode
, struct file
*filp
,
44 static int drm_setup(drm_device_t
* dev
)
51 if (dev
->driver
->firstopen
) {
52 ret
= dev
->driver
->firstopen(dev
);
57 dev
->magicfree
.next
= NULL
;
59 /* prebuild the SAREA */
60 sareapage
= max_t(unsigned, SAREA_MAX
, PAGE_SIZE
);
61 i
= drm_addmap(dev
, 0, sareapage
, _DRM_SHM
, _DRM_CONTAINS_LOCK
, &map
);
65 atomic_set(&dev
->ioctl_count
, 0);
66 atomic_set(&dev
->vma_count
, 0);
68 atomic_set(&dev
->buf_alloc
, 0);
70 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
)) {
71 i
= drm_dma_setup(dev
);
76 for (i
= 0; i
< ARRAY_SIZE(dev
->counts
); i
++)
77 atomic_set(&dev
->counts
[i
], 0);
79 drm_ht_create(&dev
->magiclist
, DRM_MAGIC_HASH_ORDER
);
80 INIT_LIST_HEAD(&dev
->magicfree
);
82 dev
->ctxlist
= drm_alloc(sizeof(*dev
->ctxlist
), DRM_MEM_CTXLIST
);
83 if (dev
->ctxlist
== NULL
)
85 memset(dev
->ctxlist
, 0, sizeof(*dev
->ctxlist
));
86 INIT_LIST_HEAD(&dev
->ctxlist
->head
);
89 dev
->sigdata
.lock
= NULL
;
90 init_waitqueue_head(&dev
->lock
.lock_queue
);
92 dev
->queue_reserved
= 0;
94 dev
->queuelist
= NULL
;
96 dev
->context_flag
= 0;
97 dev
->interrupt_flag
= 0;
99 dev
->last_context
= 0;
100 dev
->last_switch
= 0;
101 dev
->last_checked
= 0;
102 init_waitqueue_head(&dev
->context_wait
);
108 dev
->buf_async
= NULL
;
109 init_waitqueue_head(&dev
->buf_readers
);
110 init_waitqueue_head(&dev
->buf_writers
);
115 * The kernel's context could be created here, but is now created
116 * in drm_dma_enqueue. This is more resource-efficient for
117 * hardware that does not do DMA, but may mean that
118 * drm_select_queue fails between the time the interrupt is
119 * initialized and the time the queues are initialized.
128 * \param inode device inode
129 * \param filp file pointer.
130 * \return zero on success or a negative number on failure.
132 * Searches the DRM device with the same minor number, calls open_helper(), and
133 * increments the device open count. If the open count was previous at zero,
134 * i.e., it's the first that the device is open, then calls setup().
136 int drm_open(struct inode
*inode
, struct file
*filp
)
138 drm_device_t
*dev
= NULL
;
139 int minor
= iminor(inode
);
142 if (!((minor
>= 0) && (minor
< drm_cards_limit
)))
145 if (!drm_heads
[minor
])
148 if (!(dev
= drm_heads
[minor
]->dev
))
151 retcode
= drm_open_helper(inode
, filp
, dev
);
153 atomic_inc(&dev
->counts
[_DRM_STAT_OPENS
]);
154 spin_lock(&dev
->count_lock
);
155 if (!dev
->open_count
++) {
156 spin_unlock(&dev
->count_lock
);
157 return drm_setup(dev
);
159 spin_unlock(&dev
->count_lock
);
164 EXPORT_SYMBOL(drm_open
);
167 * File \c open operation.
169 * \param inode device inode.
170 * \param filp file pointer.
172 * Puts the dev->fops corresponding to the device minor number into
173 * \p filp, call the \c open method, and restore the file operations.
175 int drm_stub_open(struct inode
*inode
, struct file
*filp
)
177 drm_device_t
*dev
= NULL
;
178 int minor
= iminor(inode
);
180 const struct file_operations
*old_fops
;
184 if (!((minor
>= 0) && (minor
< drm_cards_limit
)))
187 if (!drm_heads
[minor
])
190 if (!(dev
= drm_heads
[minor
]->dev
))
193 old_fops
= filp
->f_op
;
194 filp
->f_op
= fops_get(&dev
->driver
->fops
);
195 if (filp
->f_op
->open
&& (err
= filp
->f_op
->open(inode
, filp
))) {
196 fops_put(filp
->f_op
);
197 filp
->f_op
= fops_get(old_fops
);
205 * Check whether DRI will run on this CPU.
207 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
209 static int drm_cpu_valid(void)
211 #if defined(__i386__)
212 if (boot_cpu_data
.x86
== 3)
213 return 0; /* No cmpxchg on a 386 */
215 #if defined(__sparc__) && !defined(__sparc_v9__)
216 return 0; /* No cmpxchg before v9 sparc. */
222 * Called whenever a process opens /dev/drm.
224 * \param inode device inode.
225 * \param filp file pointer.
227 * \return zero on success or a negative number on failure.
229 * Creates and initializes a drm_file structure for the file private data in \p
230 * filp and add it into the double linked list in \p dev.
232 static int drm_open_helper(struct inode
*inode
, struct file
*filp
,
235 int minor
= iminor(inode
);
239 if (filp
->f_flags
& O_EXCL
)
240 return -EBUSY
; /* No exclusive opens */
241 if (!drm_cpu_valid())
244 DRM_DEBUG("pid = %d, minor = %d\n", current
->pid
, minor
);
246 priv
= drm_alloc(sizeof(*priv
), DRM_MEM_FILES
);
250 memset(priv
, 0, sizeof(*priv
));
251 filp
->private_data
= priv
;
252 priv
->uid
= current
->euid
;
253 priv
->pid
= current
->pid
;
255 priv
->head
= drm_heads
[minor
];
256 priv
->ioctl_count
= 0;
257 /* for compatibility root is always authenticated */
258 priv
->authenticated
= capable(CAP_SYS_ADMIN
);
259 priv
->lock_count
= 0;
261 if (dev
->driver
->open
) {
262 ret
= dev
->driver
->open(dev
, priv
);
267 mutex_lock(&dev
->struct_mutex
);
268 if (!dev
->file_last
) {
271 dev
->file_first
= priv
;
272 dev
->file_last
= priv
;
273 /* first opener automatically becomes master */
277 priv
->prev
= dev
->file_last
;
278 dev
->file_last
->next
= priv
;
279 dev
->file_last
= priv
;
281 mutex_unlock(&dev
->struct_mutex
);
288 struct pci_dev
*pci_dev
;
289 pci_dev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, NULL
);
291 dev
->hose
= pci_dev
->sysdata
;
292 pci_dev_put(pci_dev
);
295 struct pci_bus
*b
= pci_bus_b(pci_root_buses
.next
);
297 dev
->hose
= b
->sysdata
;
304 drm_free(priv
, sizeof(*priv
), DRM_MEM_FILES
);
305 filp
->private_data
= NULL
;
310 int drm_fasync(int fd
, struct file
*filp
, int on
)
312 drm_file_t
*priv
= filp
->private_data
;
313 drm_device_t
*dev
= priv
->head
->dev
;
316 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd
,
317 (long)old_encode_dev(priv
->head
->device
));
318 retcode
= fasync_helper(fd
, filp
, on
, &dev
->buf_async
);
323 EXPORT_SYMBOL(drm_fasync
);
328 * \param inode device inode
329 * \param filp file pointer.
330 * \return zero on success or a negative number on failure.
332 * If the hardware lock is held then free it, and take it again for the kernel
333 * context since it's necessary to reclaim buffers. Unlink the file private
334 * data from its list and free it. Decreases the open count and if it reaches
335 * zero calls drm_lastclose().
337 int drm_release(struct inode
*inode
, struct file
*filp
)
339 drm_file_t
*priv
= filp
->private_data
;
344 dev
= priv
->head
->dev
;
346 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
348 if (dev
->driver
->preclose
)
349 dev
->driver
->preclose(dev
, filp
);
351 /* ========================================================
352 * Begin inline drm_release
355 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
356 current
->pid
, (long)old_encode_dev(priv
->head
->device
),
359 if (dev
->driver
->reclaim_buffers_locked
&& dev
->lock
.hw_lock
) {
360 if (drm_i_have_hw_lock(filp
)) {
361 dev
->driver
->reclaim_buffers_locked(dev
, filp
);
363 unsigned long _end
=jiffies
+ 3*DRM_HZ
;
366 drm_idlelock_take(&dev
->lock
);
373 spin_lock(&dev
->lock
.spinlock
);
374 locked
= dev
->lock
.idle_has_lock
;
375 spin_unlock(&dev
->lock
.spinlock
);
379 } while (!time_after_eq(jiffies
, _end
));
382 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
383 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
384 "\tI will go on reclaiming the buffers anyway.\n");
387 dev
->driver
->reclaim_buffers_locked(dev
, filp
);
388 drm_idlelock_release(&dev
->lock
);
392 if (dev
->driver
->reclaim_buffers_idlelocked
&& dev
->lock
.hw_lock
) {
394 drm_idlelock_take(&dev
->lock
);
395 dev
->driver
->reclaim_buffers_idlelocked(dev
, filp
);
396 drm_idlelock_release(&dev
->lock
);
400 if (drm_i_have_hw_lock(filp
)) {
401 DRM_DEBUG("File %p released, freeing lock for context %d\n",
402 filp
, _DRM_LOCKING_CONTEXT(dev
->lock
.hw_lock
->lock
));
404 drm_lock_free(&dev
->lock
,
405 _DRM_LOCKING_CONTEXT(dev
->lock
.hw_lock
->lock
));
409 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
) &&
410 !dev
->driver
->reclaim_buffers_locked
) {
411 dev
->driver
->reclaim_buffers(dev
, filp
);
414 drm_fasync(-1, filp
, 0);
416 mutex_lock(&dev
->ctxlist_mutex
);
417 if (dev
->ctxlist
&& (!list_empty(&dev
->ctxlist
->head
))) {
418 drm_ctx_list_t
*pos
, *n
;
420 list_for_each_entry_safe(pos
, n
, &dev
->ctxlist
->head
, head
) {
421 if (pos
->tag
== priv
&&
422 pos
->handle
!= DRM_KERNEL_CONTEXT
) {
423 if (dev
->driver
->context_dtor
)
424 dev
->driver
->context_dtor(dev
,
427 drm_ctxbitmap_free(dev
, pos
->handle
);
429 list_del(&pos
->head
);
430 drm_free(pos
, sizeof(*pos
), DRM_MEM_CTXLIST
);
435 mutex_unlock(&dev
->ctxlist_mutex
);
437 mutex_lock(&dev
->struct_mutex
);
438 if (priv
->remove_auth_on_close
== 1) {
439 drm_file_t
*temp
= dev
->file_first
;
441 temp
->authenticated
= 0;
446 priv
->prev
->next
= priv
->next
;
448 dev
->file_first
= priv
->next
;
451 priv
->next
->prev
= priv
->prev
;
453 dev
->file_last
= priv
->prev
;
455 mutex_unlock(&dev
->struct_mutex
);
457 if (dev
->driver
->postclose
)
458 dev
->driver
->postclose(dev
, priv
);
459 drm_free(priv
, sizeof(*priv
), DRM_MEM_FILES
);
461 /* ========================================================
462 * End inline drm_release
465 atomic_inc(&dev
->counts
[_DRM_STAT_CLOSES
]);
466 spin_lock(&dev
->count_lock
);
467 if (!--dev
->open_count
) {
468 if (atomic_read(&dev
->ioctl_count
) || dev
->blocked
) {
469 DRM_ERROR("Device busy: %d %d\n",
470 atomic_read(&dev
->ioctl_count
), dev
->blocked
);
471 spin_unlock(&dev
->count_lock
);
475 spin_unlock(&dev
->count_lock
);
477 return drm_lastclose(dev
);
479 spin_unlock(&dev
->count_lock
);
485 EXPORT_SYMBOL(drm_release
);
488 unsigned int drm_poll(struct file
*filp
, struct poll_table_struct
*wait
)
492 EXPORT_SYMBOL(drm_poll
);