drm_cdevsw.c revision 1.21 1 1.21 riastrad /* $NetBSD: drm_cdevsw.c,v 1.21 2021/12/19 01:59:19 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.21 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_cdevsw.c,v 1.21 2021/12/19 01:59:19 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/param.h>
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/conf.h>
38 1.1 riastrad #include <sys/device.h>
39 1.1 riastrad #include <sys/file.h>
40 1.1 riastrad #include <sys/filedesc.h>
41 1.1 riastrad #include <sys/ioccom.h>
42 1.1 riastrad #include <sys/kauth.h>
43 1.1 riastrad #ifndef _MODULE
44 1.1 riastrad /* XXX Mega-kludge because modules are broken. */
45 1.1 riastrad #include <sys/once.h>
46 1.1 riastrad #endif
47 1.1 riastrad #include <sys/pmf.h>
48 1.1 riastrad #include <sys/poll.h>
49 1.1 riastrad #ifndef _MODULE
50 1.1 riastrad #include <sys/reboot.h> /* XXX drm_init kludge */
51 1.1 riastrad #endif
52 1.1 riastrad #include <sys/select.h>
53 1.1 riastrad
54 1.1 riastrad #include <uvm/uvm_extern.h>
55 1.1 riastrad
56 1.1 riastrad #include <linux/err.h>
57 1.1 riastrad
58 1.1 riastrad #include <linux/pm.h>
59 1.1 riastrad
60 1.1 riastrad #include <drm/drmP.h>
61 1.21 riastrad #include <drm/drm_drv.h>
62 1.21 riastrad #include <drm/drm_file.h>
63 1.21 riastrad #include <drm/drm_legacy.h>
64 1.17 riastrad
65 1.17 riastrad #include "../dist/drm/drm_internal.h"
66 1.1 riastrad #include "../dist/drm/drm_legacy.h"
67 1.1 riastrad
68 1.1 riastrad static dev_type_open(drm_open);
69 1.1 riastrad
70 1.1 riastrad static int drm_firstopen(struct drm_device *);
71 1.1 riastrad
72 1.1 riastrad static int drm_close(struct file *);
73 1.1 riastrad static int drm_read(struct file *, off_t *, struct uio *, kauth_cred_t,
74 1.1 riastrad int);
75 1.1 riastrad static int drm_dequeue_event(struct drm_file *, size_t,
76 1.1 riastrad struct drm_pending_event **, int);
77 1.13 maya static int drm_ioctl_shim(struct file *, unsigned long, void *);
78 1.1 riastrad static int drm_poll(struct file *, int);
79 1.1 riastrad static int drm_kqfilter(struct file *, struct knote *);
80 1.1 riastrad static int drm_stat(struct file *, struct stat *);
81 1.1 riastrad static int drm_fop_mmap(struct file *, off_t *, size_t, int, int *, int *,
82 1.1 riastrad struct uvm_object **, int *);
83 1.8 riastrad static paddr_t drm_legacy_mmap(dev_t, off_t, int);
84 1.1 riastrad
85 1.1 riastrad const struct cdevsw drm_cdevsw = {
86 1.1 riastrad .d_open = drm_open,
87 1.1 riastrad .d_close = noclose,
88 1.1 riastrad .d_read = noread,
89 1.1 riastrad .d_write = nowrite,
90 1.1 riastrad .d_ioctl = noioctl,
91 1.1 riastrad .d_stop = nostop,
92 1.1 riastrad .d_tty = notty,
93 1.1 riastrad .d_poll = nopoll,
94 1.8 riastrad .d_mmap = drm_legacy_mmap,
95 1.1 riastrad .d_kqfilter = nokqfilter,
96 1.1 riastrad .d_discard = nodiscard,
97 1.1 riastrad /* XXX was D_TTY | D_NEGOFFSAFE */
98 1.1 riastrad /* XXX Add D_MPSAFE some day... */
99 1.1 riastrad .d_flag = D_NEGOFFSAFE,
100 1.1 riastrad };
101 1.1 riastrad
102 1.1 riastrad static const struct fileops drm_fileops = {
103 1.1 riastrad .fo_name = "drm",
104 1.1 riastrad .fo_read = drm_read,
105 1.1 riastrad .fo_write = fbadop_write,
106 1.13 maya .fo_ioctl = drm_ioctl_shim,
107 1.1 riastrad .fo_fcntl = fnullop_fcntl,
108 1.1 riastrad .fo_poll = drm_poll,
109 1.1 riastrad .fo_stat = drm_stat,
110 1.1 riastrad .fo_close = drm_close,
111 1.1 riastrad .fo_kqfilter = drm_kqfilter,
112 1.1 riastrad .fo_restart = fnullop_restart,
113 1.1 riastrad .fo_mmap = drm_fop_mmap,
114 1.1 riastrad };
115 1.1 riastrad
116 1.1 riastrad static int
117 1.1 riastrad drm_open(dev_t d, int flags, int fmt, struct lwp *l)
118 1.1 riastrad {
119 1.1 riastrad struct drm_minor *dminor;
120 1.1 riastrad struct drm_device *dev;
121 1.1 riastrad bool firstopen, lastclose;
122 1.1 riastrad int fd;
123 1.1 riastrad struct file *fp;
124 1.1 riastrad int error;
125 1.1 riastrad
126 1.1 riastrad error = drm_guarantee_initialized();
127 1.1 riastrad if (error)
128 1.1 riastrad goto fail0;
129 1.1 riastrad
130 1.1 riastrad if (flags & O_EXCL) {
131 1.1 riastrad error = EBUSY;
132 1.1 riastrad goto fail0;
133 1.1 riastrad }
134 1.1 riastrad
135 1.1 riastrad dminor = drm_minor_acquire(minor(d));
136 1.1 riastrad if (IS_ERR(dminor)) {
137 1.1 riastrad /* XXX errno Linux->NetBSD */
138 1.1 riastrad error = -PTR_ERR(dminor);
139 1.1 riastrad goto fail0;
140 1.1 riastrad }
141 1.1 riastrad dev = dminor->dev;
142 1.1 riastrad if (dev->switch_power_state != DRM_SWITCH_POWER_ON) {
143 1.1 riastrad error = EINVAL;
144 1.1 riastrad goto fail1;
145 1.1 riastrad }
146 1.1 riastrad
147 1.6 riastrad mutex_lock(&drm_global_mutex);
148 1.1 riastrad if (dev->open_count == INT_MAX) {
149 1.6 riastrad mutex_unlock(&drm_global_mutex);
150 1.1 riastrad error = EBUSY;
151 1.1 riastrad goto fail1;
152 1.1 riastrad }
153 1.1 riastrad firstopen = (dev->open_count == 0);
154 1.1 riastrad dev->open_count++;
155 1.10 riastrad mutex_unlock(&drm_global_mutex);
156 1.1 riastrad
157 1.1 riastrad if (firstopen) {
158 1.1 riastrad /* XXX errno Linux->NetBSD */
159 1.5 riastrad error = -drm_firstopen(dev);
160 1.1 riastrad if (error)
161 1.1 riastrad goto fail2;
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad error = fd_allocfile(&fp, &fd);
165 1.1 riastrad if (error)
166 1.1 riastrad goto fail2;
167 1.1 riastrad
168 1.1 riastrad struct drm_file *const file = kmem_zalloc(sizeof(*file), KM_SLEEP);
169 1.1 riastrad /* XXX errno Linux->NetBSD */
170 1.1 riastrad error = -drm_open_file(file, fp, dminor);
171 1.1 riastrad if (error)
172 1.1 riastrad goto fail3;
173 1.1 riastrad
174 1.1 riastrad error = fd_clone(fp, fd, flags, &drm_fileops, file);
175 1.1 riastrad KASSERT(error == EMOVEFD); /* XXX */
176 1.1 riastrad
177 1.1 riastrad /* Success! (But error has to be EMOVEFD, not 0.) */
178 1.1 riastrad return error;
179 1.1 riastrad
180 1.1 riastrad fail3: kmem_free(file, sizeof(*file));
181 1.1 riastrad fd_abort(curproc, fp, fd);
182 1.6 riastrad fail2: mutex_lock(&drm_global_mutex);
183 1.1 riastrad KASSERT(0 < dev->open_count);
184 1.1 riastrad --dev->open_count;
185 1.1 riastrad lastclose = (dev->open_count == 0);
186 1.6 riastrad mutex_unlock(&drm_global_mutex);
187 1.1 riastrad if (lastclose)
188 1.18 riastrad drm_lastclose(dev);
189 1.1 riastrad fail1: drm_minor_release(dminor);
190 1.1 riastrad fail0: KASSERT(error);
191 1.14 mrg if (error == ERESTARTSYS)
192 1.14 mrg error = ERESTART;
193 1.1 riastrad return error;
194 1.1 riastrad }
195 1.1 riastrad
196 1.1 riastrad static int
197 1.1 riastrad drm_close(struct file *fp)
198 1.1 riastrad {
199 1.1 riastrad struct drm_file *const file = fp->f_data;
200 1.1 riastrad struct drm_minor *const dminor = file->minor;
201 1.1 riastrad struct drm_device *const dev = dminor->dev;
202 1.1 riastrad bool lastclose;
203 1.1 riastrad
204 1.1 riastrad drm_close_file(file);
205 1.1 riastrad kmem_free(file, sizeof(*file));
206 1.1 riastrad
207 1.6 riastrad mutex_lock(&drm_global_mutex);
208 1.1 riastrad KASSERT(0 < dev->open_count);
209 1.1 riastrad --dev->open_count;
210 1.1 riastrad lastclose = (dev->open_count == 0);
211 1.6 riastrad mutex_unlock(&drm_global_mutex);
212 1.1 riastrad
213 1.1 riastrad if (lastclose)
214 1.18 riastrad drm_lastclose(dev);
215 1.1 riastrad
216 1.1 riastrad drm_minor_release(dminor);
217 1.1 riastrad
218 1.1 riastrad return 0;
219 1.1 riastrad }
220 1.1 riastrad
221 1.1 riastrad static int
222 1.1 riastrad drm_firstopen(struct drm_device *dev)
223 1.1 riastrad {
224 1.1 riastrad int ret;
225 1.1 riastrad
226 1.1 riastrad if (drm_core_check_feature(dev, DRIVER_MODESET))
227 1.1 riastrad return 0;
228 1.1 riastrad
229 1.1 riastrad if (dev->driver->firstopen) {
230 1.1 riastrad ret = (*dev->driver->firstopen)(dev);
231 1.1 riastrad if (ret)
232 1.1 riastrad goto fail0;
233 1.1 riastrad }
234 1.1 riastrad
235 1.1 riastrad ret = drm_legacy_dma_setup(dev);
236 1.1 riastrad if (ret)
237 1.1 riastrad goto fail1;
238 1.1 riastrad
239 1.1 riastrad return 0;
240 1.1 riastrad
241 1.1 riastrad fail2: __unused
242 1.1 riastrad drm_legacy_dma_takedown(dev);
243 1.1 riastrad fail1: if (dev->driver->lastclose)
244 1.1 riastrad (*dev->driver->lastclose)(dev);
245 1.1 riastrad fail0: KASSERT(ret);
246 1.1 riastrad return ret;
247 1.1 riastrad }
248 1.1 riastrad
249 1.18 riastrad void
250 1.1 riastrad drm_lastclose(struct drm_device *dev)
251 1.1 riastrad {
252 1.1 riastrad
253 1.1 riastrad /* XXX Order is sketchy here... */
254 1.1 riastrad if (dev->driver->lastclose)
255 1.1 riastrad (*dev->driver->lastclose)(dev);
256 1.1 riastrad if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
257 1.1 riastrad drm_irq_uninstall(dev);
258 1.1 riastrad
259 1.1 riastrad mutex_lock(&dev->struct_mutex);
260 1.1 riastrad if (dev->agp)
261 1.19 riastrad drm_legacy_agp_clear(dev);
262 1.1 riastrad drm_legacy_sg_cleanup(dev);
263 1.1 riastrad drm_legacy_dma_takedown(dev);
264 1.1 riastrad mutex_unlock(&dev->struct_mutex);
265 1.1 riastrad
266 1.7 riastrad /* XXX Synchronize with drm_legacy_dev_reinit. */
267 1.7 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
268 1.7 riastrad dev->sigdata.lock = NULL;
269 1.7 riastrad dev->context_flag = 0;
270 1.7 riastrad dev->last_context = 0;
271 1.7 riastrad dev->if_version = 0;
272 1.7 riastrad }
273 1.1 riastrad }
274 1.1 riastrad
275 1.1 riastrad static int
276 1.1 riastrad drm_read(struct file *fp, off_t *off, struct uio *uio, kauth_cred_t cred,
277 1.1 riastrad int flags)
278 1.1 riastrad {
279 1.1 riastrad struct drm_file *const file = fp->f_data;
280 1.20 riastrad struct drm_device *const dev = file->minor->dev;
281 1.1 riastrad struct drm_pending_event *event;
282 1.1 riastrad bool first;
283 1.20 riastrad int ret = 0;
284 1.20 riastrad
285 1.20 riastrad /*
286 1.20 riastrad * Only one event reader at a time, so that if copyout faults
287 1.20 riastrad * after dequeueing one event and we have to put the event
288 1.20 riastrad * back, another reader won't see out-of-order events.
289 1.20 riastrad */
290 1.20 riastrad spin_lock(&dev->event_lock);
291 1.20 riastrad DRM_SPIN_WAIT_NOINTR_UNTIL(ret, &file->event_read_wq, &dev->event_lock,
292 1.20 riastrad file->event_read_lock == NULL);
293 1.20 riastrad if (ret) {
294 1.20 riastrad spin_unlock(&dev->event_lock);
295 1.20 riastrad /* XXX errno Linux->NetBSD */
296 1.20 riastrad return -ret;
297 1.20 riastrad }
298 1.20 riastrad file->event_read_lock = curlwp;
299 1.20 riastrad spin_unlock(&dev->event_lock);
300 1.1 riastrad
301 1.1 riastrad for (first = true; ; first = false) {
302 1.1 riastrad int f = 0;
303 1.20 riastrad off_t offset;
304 1.20 riastrad size_t resid;
305 1.1 riastrad
306 1.1 riastrad if (!first || ISSET(fp->f_flag, FNONBLOCK))
307 1.1 riastrad f |= FNONBLOCK;
308 1.1 riastrad
309 1.20 riastrad ret = drm_dequeue_event(file, uio->uio_resid, &event, f);
310 1.20 riastrad if (ret) {
311 1.20 riastrad if ((ret == -EWOULDBLOCK) && !first)
312 1.20 riastrad ret = 0;
313 1.1 riastrad break;
314 1.1 riastrad }
315 1.1 riastrad if (event == NULL)
316 1.1 riastrad break;
317 1.20 riastrad
318 1.20 riastrad offset = uio->uio_offset;
319 1.20 riastrad resid = uio->uio_resid;
320 1.20 riastrad /* XXX errno NetBSD->Linux */
321 1.20 riastrad ret = -uiomove(event->event, event->event->length, uio);
322 1.20 riastrad if (ret) {
323 1.20 riastrad /*
324 1.20 riastrad * Faulted on copyout. Put the event back and
325 1.20 riastrad * stop here.
326 1.20 riastrad */
327 1.20 riastrad if (!first) {
328 1.20 riastrad /*
329 1.20 riastrad * Already transferred some events.
330 1.20 riastrad * Rather than back them all out, just
331 1.20 riastrad * say we succeeded at returning those.
332 1.20 riastrad */
333 1.20 riastrad ret = 0;
334 1.20 riastrad }
335 1.20 riastrad uio->uio_offset = offset;
336 1.20 riastrad uio->uio_resid = resid;
337 1.20 riastrad drm_requeue_event(file, event);
338 1.1 riastrad break;
339 1.20 riastrad }
340 1.20 riastrad kfree(event);
341 1.1 riastrad }
342 1.1 riastrad
343 1.20 riastrad /* Release the event read lock. */
344 1.20 riastrad spin_lock(&dev->event_lock);
345 1.20 riastrad KASSERT(file->event_read_lock == curlwp);
346 1.20 riastrad file->event_read_lock = NULL;
347 1.20 riastrad DRM_SPIN_WAKEUP_ONE(&file->event_read_wq, &dev->event_lock);
348 1.20 riastrad spin_unlock(&dev->event_lock);
349 1.20 riastrad
350 1.20 riastrad /* XXX errno Linux->NetBSD */
351 1.20 riastrad
352 1.1 riastrad /* Success! */
353 1.20 riastrad if (ret == ERESTARTSYS)
354 1.20 riastrad ret = ERESTART;
355 1.20 riastrad return -ret;
356 1.1 riastrad }
357 1.1 riastrad
358 1.1 riastrad static int
359 1.1 riastrad drm_dequeue_event(struct drm_file *file, size_t max_length,
360 1.1 riastrad struct drm_pending_event **eventp, int flags)
361 1.1 riastrad {
362 1.1 riastrad struct drm_device *const dev = file->minor->dev;
363 1.1 riastrad struct drm_pending_event *event = NULL;
364 1.1 riastrad unsigned long irqflags;
365 1.1 riastrad int ret = 0;
366 1.1 riastrad
367 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
368 1.1 riastrad
369 1.1 riastrad if (ISSET(flags, FNONBLOCK)) {
370 1.1 riastrad if (list_empty(&file->event_list))
371 1.1 riastrad ret = -EWOULDBLOCK;
372 1.1 riastrad } else {
373 1.1 riastrad DRM_SPIN_WAIT_UNTIL(ret, &file->event_wait, &dev->event_lock,
374 1.1 riastrad !list_empty(&file->event_list));
375 1.1 riastrad }
376 1.1 riastrad if (ret)
377 1.1 riastrad goto out;
378 1.1 riastrad
379 1.1 riastrad event = list_first_entry(&file->event_list, struct drm_pending_event,
380 1.1 riastrad link);
381 1.1 riastrad if (event->event->length > max_length) {
382 1.1 riastrad /* Event is too large, can't return it. */
383 1.1 riastrad event = NULL;
384 1.1 riastrad ret = 0;
385 1.1 riastrad goto out;
386 1.1 riastrad }
387 1.1 riastrad
388 1.1 riastrad file->event_space += event->event->length;
389 1.1 riastrad list_del(&event->link);
390 1.1 riastrad
391 1.1 riastrad out: spin_unlock_irqrestore(&dev->event_lock, irqflags);
392 1.1 riastrad *eventp = event;
393 1.1 riastrad return ret;
394 1.1 riastrad }
395 1.1 riastrad
396 1.20 riastrad static void
397 1.20 riastrad drm_requeue_event(struct drm_file *file, struct drm_pending_event *event)
398 1.20 riastrad {
399 1.20 riastrad struct drm_device *const dev = file->minor->dev;
400 1.20 riastrad unsigned long irqflags;
401 1.20 riastrad
402 1.20 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
403 1.20 riastrad list_add(&event->link, &file->event_list);
404 1.20 riastrad KASSERT(file->event_space >= event->event->length);
405 1.20 riastrad file->event_space -= event->event->length;
406 1.20 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
407 1.20 riastrad }
408 1.20 riastrad
409 1.1 riastrad static int
410 1.13 maya drm_ioctl_shim(struct file *fp, unsigned long cmd, void *data)
411 1.13 maya {
412 1.13 maya struct drm_file *file = fp->f_data;
413 1.13 maya struct drm_driver *driver = file->minor->dev->driver;
414 1.14 mrg int error;
415 1.13 maya
416 1.13 maya if (driver->ioctl_override)
417 1.14 mrg error = driver->ioctl_override(fp, cmd, data);
418 1.13 maya else
419 1.14 mrg error = drm_ioctl(fp, cmd, data);
420 1.14 mrg if (error == ERESTARTSYS)
421 1.14 mrg error = ERESTART;
422 1.14 mrg
423 1.14 mrg return error;
424 1.13 maya }
425 1.13 maya
426 1.13 maya static int
427 1.1 riastrad drm_poll(struct file *fp __unused, int events __unused)
428 1.1 riastrad {
429 1.1 riastrad struct drm_file *const file = fp->f_data;
430 1.1 riastrad struct drm_device *const dev = file->minor->dev;
431 1.1 riastrad int revents = 0;
432 1.1 riastrad unsigned long irqflags;
433 1.1 riastrad
434 1.1 riastrad if (!ISSET(events, (POLLIN | POLLRDNORM)))
435 1.1 riastrad return 0;
436 1.1 riastrad
437 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
438 1.1 riastrad if (list_empty(&file->event_list))
439 1.1 riastrad selrecord(curlwp, &file->event_selq);
440 1.1 riastrad else
441 1.1 riastrad revents |= (events & (POLLIN | POLLRDNORM));
442 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
443 1.1 riastrad
444 1.1 riastrad return revents;
445 1.1 riastrad }
446 1.1 riastrad
447 1.1 riastrad static void filt_drm_detach(struct knote *);
448 1.1 riastrad static int filt_drm_event(struct knote *, long);
449 1.1 riastrad
450 1.1 riastrad static const struct filterops drm_filtops = {
451 1.16 thorpej .f_flags = FILTEROP_ISFD,
452 1.1 riastrad .f_attach = NULL,
453 1.1 riastrad .f_detach = filt_drm_detach,
454 1.1 riastrad .f_event = filt_drm_event,
455 1.1 riastrad };
456 1.1 riastrad
457 1.1 riastrad static int
458 1.1 riastrad drm_kqfilter(struct file *fp, struct knote *kn)
459 1.1 riastrad {
460 1.1 riastrad struct drm_file *const file = fp->f_data;
461 1.1 riastrad struct drm_device *const dev = file->minor->dev;
462 1.1 riastrad unsigned long irqflags;
463 1.1 riastrad
464 1.1 riastrad switch (kn->kn_filter) {
465 1.1 riastrad case EVFILT_READ:
466 1.1 riastrad kn->kn_fop = &drm_filtops;
467 1.1 riastrad kn->kn_hook = file;
468 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
469 1.15 thorpej selrecord_knote(&file->event_selq, kn);
470 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
471 1.1 riastrad return 0;
472 1.1 riastrad case EVFILT_WRITE:
473 1.1 riastrad default:
474 1.1 riastrad return EINVAL;
475 1.1 riastrad }
476 1.1 riastrad }
477 1.1 riastrad
478 1.1 riastrad static void
479 1.1 riastrad filt_drm_detach(struct knote *kn)
480 1.1 riastrad {
481 1.1 riastrad struct drm_file *const file = kn->kn_hook;
482 1.1 riastrad struct drm_device *const dev = file->minor->dev;
483 1.1 riastrad unsigned long irqflags;
484 1.1 riastrad
485 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
486 1.15 thorpej selremove_knote(&file->event_selq, kn);
487 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
488 1.1 riastrad }
489 1.1 riastrad
490 1.1 riastrad static int
491 1.1 riastrad filt_drm_event(struct knote *kn, long hint)
492 1.1 riastrad {
493 1.1 riastrad struct drm_file *const file = kn->kn_hook;
494 1.1 riastrad struct drm_device *const dev = file->minor->dev;
495 1.1 riastrad unsigned long irqflags;
496 1.1 riastrad int ret;
497 1.1 riastrad
498 1.1 riastrad if (hint == NOTE_SUBMIT)
499 1.1 riastrad KASSERT(spin_is_locked(&dev->event_lock));
500 1.1 riastrad else
501 1.1 riastrad spin_lock_irqsave(&dev->event_lock, irqflags);
502 1.1 riastrad if (list_empty(&file->event_list)) {
503 1.1 riastrad ret = 0;
504 1.1 riastrad } else {
505 1.1 riastrad struct drm_pending_event *const event =
506 1.1 riastrad list_first_entry(&file->event_list,
507 1.1 riastrad struct drm_pending_event, link);
508 1.1 riastrad kn->kn_data = event->event->length;
509 1.1 riastrad ret = 1;
510 1.1 riastrad }
511 1.1 riastrad if (hint == NOTE_SUBMIT)
512 1.1 riastrad KASSERT(spin_is_locked(&dev->event_lock));
513 1.1 riastrad else
514 1.1 riastrad spin_unlock_irqrestore(&dev->event_lock, irqflags);
515 1.1 riastrad
516 1.1 riastrad return ret;
517 1.1 riastrad }
518 1.1 riastrad
519 1.1 riastrad static int
520 1.1 riastrad drm_stat(struct file *fp, struct stat *st)
521 1.1 riastrad {
522 1.1 riastrad struct drm_file *const file = fp->f_data;
523 1.1 riastrad struct drm_minor *const dminor = file->minor;
524 1.1 riastrad const dev_t devno = makedev(cdevsw_lookup_major(&drm_cdevsw),
525 1.1 riastrad 64*dminor->type + dminor->index);
526 1.1 riastrad
527 1.1 riastrad (void)memset(st, 0, sizeof(*st));
528 1.1 riastrad
529 1.1 riastrad st->st_dev = devno;
530 1.1 riastrad st->st_ino = 0; /* XXX (dev,ino) uniqueness bleh */
531 1.1 riastrad st->st_uid = kauth_cred_geteuid(fp->f_cred);
532 1.1 riastrad st->st_gid = kauth_cred_getegid(fp->f_cred);
533 1.1 riastrad st->st_mode = S_IFCHR; /* XXX what? */
534 1.1 riastrad st->st_rdev = devno;
535 1.1 riastrad /* XXX what else? */
536 1.1 riastrad
537 1.1 riastrad return 0;
538 1.1 riastrad }
539 1.1 riastrad
540 1.1 riastrad static int
541 1.1 riastrad drm_fop_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
542 1.1 riastrad int *advicep, struct uvm_object **uobjp, int *maxprotp)
543 1.1 riastrad {
544 1.1 riastrad struct drm_file *const file = fp->f_data;
545 1.1 riastrad struct drm_device *const dev = file->minor->dev;
546 1.1 riastrad int error;
547 1.1 riastrad
548 1.1 riastrad KASSERT(fp == file->filp);
549 1.14 mrg /* XXX errno Linux->NetBSD */
550 1.14 mrg error = -(*dev->driver->mmap_object)(dev, *offp, len, prot, uobjp,
551 1.1 riastrad offp, file->filp);
552 1.1 riastrad *maxprotp = prot;
553 1.1 riastrad *advicep = UVM_ADV_RANDOM;
554 1.14 mrg if (error == ERESTARTSYS)
555 1.14 mrg error = ERESTART;
556 1.14 mrg return error;
557 1.1 riastrad }
558 1.1 riastrad
559 1.1 riastrad static paddr_t
560 1.8 riastrad drm_legacy_mmap(dev_t d, off_t offset, int prot)
561 1.1 riastrad {
562 1.1 riastrad struct drm_minor *dminor;
563 1.1 riastrad paddr_t paddr;
564 1.1 riastrad
565 1.1 riastrad dminor = drm_minor_acquire(minor(d));
566 1.1 riastrad if (IS_ERR(dminor))
567 1.1 riastrad return (paddr_t)-1;
568 1.1 riastrad
569 1.8 riastrad paddr = drm_legacy_mmap_paddr(dminor->dev, offset, prot);
570 1.1 riastrad
571 1.1 riastrad drm_minor_release(dminor);
572 1.1 riastrad return paddr;
573 1.1 riastrad }
574