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