xref: /freebsd-13-stable/sys/dev/drm2/drm_fops.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /**
2  * \file drm_fops.c
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 <sys/cdefs.h>
38 #include <dev/drm2/drmP.h>
39 
40 static int drm_open_helper(struct cdev *kdev, int flags, int fmt,
41 			   DRM_STRUCTPROC *p, struct drm_device *dev);
42 
drm_setup(struct drm_device * dev)43 static int drm_setup(struct drm_device * dev)
44 {
45 	int i;
46 	int ret;
47 
48 	if (dev->driver->firstopen) {
49 		ret = dev->driver->firstopen(dev);
50 		if (ret != 0)
51 			return ret;
52 	}
53 
54 	atomic_set(&dev->ioctl_count, 0);
55 	atomic_set(&dev->vma_count, 0);
56 
57 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
58 	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
59 		dev->buf_use = 0;
60 		atomic_set(&dev->buf_alloc, 0);
61 
62 		i = drm_dma_setup(dev);
63 		if (i < 0)
64 			return i;
65 	}
66 
67 	/*
68 	 * FIXME Linux<->FreeBSD: counter incremented in drm_open() and
69 	 * reset to 0 here.
70 	 */
71 #if 0
72 	for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
73 		atomic_set(&dev->counts[i], 0);
74 #endif
75 
76 	dev->sigdata.lock = NULL;
77 
78 	dev->context_flag = 0;
79 	dev->interrupt_flag = 0;
80 	dev->dma_flag = 0;
81 	dev->last_context = 0;
82 	dev->last_switch = 0;
83 	dev->last_checked = 0;
84 	DRM_INIT_WAITQUEUE(&dev->context_wait);
85 	dev->if_version = 0;
86 
87 #ifdef FREEBSD_NOTYET
88 	dev->ctx_start = 0;
89 	dev->lck_start = 0;
90 
91 	dev->buf_async = NULL;
92 	DRM_INIT_WAITQUEUE(&dev->buf_readers);
93 	DRM_INIT_WAITQUEUE(&dev->buf_writers);
94 #endif /* FREEBSD_NOTYET */
95 
96 	DRM_DEBUG("\n");
97 
98 	/*
99 	 * The kernel's context could be created here, but is now created
100 	 * in drm_dma_enqueue.  This is more resource-efficient for
101 	 * hardware that does not do DMA, but may mean that
102 	 * drm_select_queue fails between the time the interrupt is
103 	 * initialized and the time the queues are initialized.
104 	 */
105 
106 	return 0;
107 }
108 
109 /**
110  * Open file.
111  *
112  * \param inode device inode
113  * \param filp file pointer.
114  * \return zero on success or a negative number on failure.
115  *
116  * Searches the DRM device with the same minor number, calls open_helper(), and
117  * increments the device open count. If the open count was previous at zero,
118  * i.e., it's the first that the device is open, then calls setup().
119  */
drm_open(struct cdev * kdev,int flags,int fmt,DRM_STRUCTPROC * p)120 int drm_open(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p)
121 {
122 	struct drm_device *dev = NULL;
123 	struct drm_minor *minor;
124 	int retcode = 0;
125 	int need_setup = 0;
126 
127 	minor = kdev->si_drv1;
128 	if (!minor)
129 		return ENODEV;
130 
131 	if (!(dev = minor->dev))
132 		return ENODEV;
133 
134 	sx_xlock(&drm_global_mutex);
135 
136 	/*
137 	 * FIXME Linux<->FreeBSD: On Linux, counter updated outside
138 	 * global mutex.
139 	 */
140 	if (!dev->open_count++)
141 		need_setup = 1;
142 
143 	retcode = drm_open_helper(kdev, flags, fmt, p, dev);
144 	if (retcode) {
145 		sx_xunlock(&drm_global_mutex);
146 		return (-retcode);
147 	}
148 	atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
149 	if (need_setup) {
150 		retcode = drm_setup(dev);
151 		if (retcode)
152 			goto err_undo;
153 	}
154 	sx_xunlock(&drm_global_mutex);
155 	return 0;
156 
157 err_undo:
158 	mtx_lock(&Giant); /* FIXME: Giant required? */
159 	device_unbusy(dev->dev);
160 	mtx_unlock(&Giant);
161 	dev->open_count--;
162 	sx_xunlock(&drm_global_mutex);
163 	return -retcode;
164 }
165 EXPORT_SYMBOL(drm_open);
166 
167 /**
168  * Called whenever a process opens /dev/drm.
169  *
170  * \param inode device inode.
171  * \param filp file pointer.
172  * \param dev device.
173  * \return zero on success or a negative number on failure.
174  *
175  * Creates and initializes a drm_file structure for the file private data in \p
176  * filp and add it into the double linked list in \p dev.
177  */
drm_open_helper(struct cdev * kdev,int flags,int fmt,DRM_STRUCTPROC * p,struct drm_device * dev)178 static int drm_open_helper(struct cdev *kdev, int flags, int fmt,
179 			   DRM_STRUCTPROC *p, struct drm_device *dev)
180 {
181 	struct drm_file *priv;
182 	int ret;
183 
184 	if (flags & O_EXCL)
185 		return -EBUSY;	/* No exclusive opens */
186 	if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
187 		return -EINVAL;
188 
189 	DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
190 
191 	priv = malloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
192 	if (!priv)
193 		return -ENOMEM;
194 
195 	priv->uid = p->td_ucred->cr_svuid;
196 	priv->pid = p->td_proc->p_pid;
197 	priv->minor = kdev->si_drv1;
198 	priv->ioctl_count = 0;
199 	/* for compatibility root is always authenticated */
200 	priv->authenticated = DRM_SUSER(p);
201 	priv->lock_count = 0;
202 
203 	INIT_LIST_HEAD(&priv->lhead);
204 	INIT_LIST_HEAD(&priv->fbs);
205 	INIT_LIST_HEAD(&priv->event_list);
206 	priv->event_space = 4096; /* set aside 4k for event buffer */
207 
208 	if (dev->driver->driver_features & DRIVER_GEM)
209 		drm_gem_open(dev, priv);
210 
211 #ifdef FREEBSD_NOTYET
212 	if (drm_core_check_feature(dev, DRIVER_PRIME))
213 		drm_prime_init_file_private(&priv->prime);
214 #endif /* FREEBSD_NOTYET */
215 
216 	if (dev->driver->open) {
217 		ret = dev->driver->open(dev, priv);
218 		if (ret < 0)
219 			goto out_free;
220 	}
221 
222 
223 	/* if there is no current master make this fd it */
224 	DRM_LOCK(dev);
225 	if (!priv->minor->master) {
226 		/* create a new master */
227 		priv->minor->master = drm_master_create(priv->minor);
228 		if (!priv->minor->master) {
229 			DRM_UNLOCK(dev);
230 			ret = -ENOMEM;
231 			goto out_free;
232 		}
233 
234 		priv->is_master = 1;
235 		/* take another reference for the copy in the local file priv */
236 		priv->master = drm_master_get(priv->minor->master);
237 
238 		priv->authenticated = 1;
239 
240 		DRM_UNLOCK(dev);
241 		if (dev->driver->master_create) {
242 			ret = dev->driver->master_create(dev, priv->master);
243 			if (ret) {
244 				DRM_LOCK(dev);
245 				/* drop both references if this fails */
246 				drm_master_put(&priv->minor->master);
247 				drm_master_put(&priv->master);
248 				DRM_UNLOCK(dev);
249 				goto out_free;
250 			}
251 		}
252 		DRM_LOCK(dev);
253 		if (dev->driver->master_set) {
254 			ret = dev->driver->master_set(dev, priv, true);
255 			if (ret) {
256 				/* drop both references if this fails */
257 				drm_master_put(&priv->minor->master);
258 				drm_master_put(&priv->master);
259 				DRM_UNLOCK(dev);
260 				goto out_free;
261 			}
262 		}
263 		DRM_UNLOCK(dev);
264 	} else {
265 		/* get a reference to the master */
266 		priv->master = drm_master_get(priv->minor->master);
267 		DRM_UNLOCK(dev);
268 	}
269 
270 	DRM_LOCK(dev);
271 	list_add(&priv->lhead, &dev->filelist);
272 	DRM_UNLOCK(dev);
273 
274 	mtx_lock(&Giant); /* FIXME: Giant required? */
275 	device_busy(dev->dev);
276 	mtx_unlock(&Giant);
277 
278 	ret = devfs_set_cdevpriv(priv, drm_release);
279 	if (ret != 0)
280 		drm_release(priv);
281 
282 	return ret;
283       out_free:
284 	free(priv, DRM_MEM_FILES);
285 	return ret;
286 }
287 
drm_master_release(struct drm_device * dev,struct drm_file * file_priv)288 static void drm_master_release(struct drm_device *dev, struct drm_file *file_priv)
289 {
290 
291 	if (drm_i_have_hw_lock(dev, file_priv)) {
292 		DRM_DEBUG("File %p released, freeing lock for context %d\n",
293 			  file_priv, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
294 		drm_lock_free(&file_priv->master->lock,
295 			      _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
296 	}
297 }
298 
drm_events_release(struct drm_file * file_priv)299 static void drm_events_release(struct drm_file *file_priv)
300 {
301 	struct drm_device *dev = file_priv->minor->dev;
302 	struct drm_pending_event *e, *et;
303 	struct drm_pending_vblank_event *v, *vt;
304 	unsigned long flags;
305 
306 	DRM_SPINLOCK_IRQSAVE(&dev->event_lock, flags);
307 
308 	/* Remove pending flips */
309 	list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
310 		if (v->base.file_priv == file_priv) {
311 			list_del(&v->base.link);
312 			drm_vblank_put(dev, v->pipe);
313 			v->base.destroy(&v->base);
314 		}
315 
316 	/* Remove unconsumed events */
317 	list_for_each_entry_safe(e, et, &file_priv->event_list, link)
318 		e->destroy(e);
319 
320 	DRM_SPINUNLOCK_IRQRESTORE(&dev->event_lock, flags);
321 }
322 
323 /**
324  * Release file.
325  *
326  * \param inode device inode
327  * \param file_priv DRM file private.
328  * \return zero on success or a negative number on failure.
329  *
330  * If the hardware lock is held then free it, and take it again for the kernel
331  * context since it's necessary to reclaim buffers. Unlink the file private
332  * data from its list and free it. Decreases the open count and if it reaches
333  * zero calls drm_lastclose().
334  */
drm_release(void * data)335 void drm_release(void *data)
336 {
337 	struct drm_file *file_priv = data;
338 	struct drm_device *dev = file_priv->minor->dev;
339 
340 	sx_xlock(&drm_global_mutex);
341 
342 	DRM_DEBUG("open_count = %d\n", dev->open_count);
343 
344 	if (dev->driver->preclose)
345 		dev->driver->preclose(dev, file_priv);
346 
347 	/* ========================================================
348 	 * Begin inline drm_release
349 	 */
350 
351 	DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
352 		  DRM_CURRENTPID,
353 		  (long)file_priv->minor->device,
354 		  dev->open_count);
355 
356 	/* Release any auth tokens that might point to this file_priv,
357 	   (do that under the drm_global_mutex) */
358 	if (file_priv->magic)
359 		(void) drm_remove_magic(file_priv->master, file_priv->magic);
360 
361 	/* if the master has gone away we can't do anything with the lock */
362 	if (file_priv->minor->master)
363 		drm_master_release(dev, file_priv);
364 
365 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
366 		drm_core_reclaim_buffers(dev, file_priv);
367 
368 	drm_events_release(file_priv);
369 
370 	seldrain(&file_priv->event_poll);
371 
372 	if (dev->driver->driver_features & DRIVER_MODESET)
373 		drm_fb_release(file_priv);
374 
375 	if (dev->driver->driver_features & DRIVER_GEM)
376 		drm_gem_release(dev, file_priv);
377 
378 #ifdef FREEBSD_NOTYET
379 	mutex_lock(&dev->ctxlist_mutex);
380 	if (!list_empty(&dev->ctxlist)) {
381 		struct drm_ctx_list *pos, *n;
382 
383 		list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
384 			if (pos->tag == file_priv &&
385 			    pos->handle != DRM_KERNEL_CONTEXT) {
386 				if (dev->driver->context_dtor)
387 					dev->driver->context_dtor(dev,
388 								  pos->handle);
389 
390 				drm_ctxbitmap_free(dev, pos->handle);
391 
392 				list_del(&pos->head);
393 				kfree(pos);
394 				--dev->ctx_count;
395 			}
396 		}
397 	}
398 	mutex_unlock(&dev->ctxlist_mutex);
399 #endif /* FREEBSD_NOTYET */
400 
401 	DRM_LOCK(dev);
402 
403 	if (file_priv->is_master) {
404 		struct drm_master *master = file_priv->master;
405 		struct drm_file *temp;
406 		list_for_each_entry(temp, &dev->filelist, lhead) {
407 			if ((temp->master == file_priv->master) &&
408 			    (temp != file_priv))
409 				temp->authenticated = 0;
410 		}
411 
412 		/**
413 		 * Since the master is disappearing, so is the
414 		 * possibility to lock.
415 		 */
416 
417 		if (master->lock.hw_lock) {
418 			if (dev->sigdata.lock == master->lock.hw_lock)
419 				dev->sigdata.lock = NULL;
420 			master->lock.hw_lock = NULL;
421 			master->lock.file_priv = NULL;
422 			DRM_WAKEUP_INT(&master->lock.lock_queue);
423 		}
424 
425 		if (file_priv->minor->master == file_priv->master) {
426 			/* drop the reference held my the minor */
427 			if (dev->driver->master_drop)
428 				dev->driver->master_drop(dev, file_priv, true);
429 			drm_master_put(&file_priv->minor->master);
430 		}
431 	}
432 
433 	/* drop the reference held my the file priv */
434 	drm_master_put(&file_priv->master);
435 	file_priv->is_master = 0;
436 	list_del(&file_priv->lhead);
437 	DRM_UNLOCK(dev);
438 
439 	if (dev->driver->postclose)
440 		dev->driver->postclose(dev, file_priv);
441 
442 #ifdef FREEBSD_NOTYET
443 	if (drm_core_check_feature(dev, DRIVER_PRIME))
444 		drm_prime_destroy_file_private(&file_priv->prime);
445 #endif /* FREEBSD_NOTYET */
446 
447 	free(file_priv, DRM_MEM_FILES);
448 
449 	/* ========================================================
450 	 * End inline drm_release
451 	 */
452 
453 	atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
454 	mtx_lock(&Giant);
455 	device_unbusy(dev->dev);
456 	mtx_unlock(&Giant);
457 	if (!--dev->open_count) {
458 		if (atomic_read(&dev->ioctl_count)) {
459 			DRM_ERROR("Device busy: %d\n",
460 				  atomic_read(&dev->ioctl_count));
461 		} else
462 			drm_lastclose(dev);
463 	}
464 	sx_xunlock(&drm_global_mutex);
465 }
466 EXPORT_SYMBOL(drm_release);
467 
468 static bool
drm_dequeue_event(struct drm_file * file_priv,struct uio * uio,struct drm_pending_event ** out)469 drm_dequeue_event(struct drm_file *file_priv, struct uio *uio,
470     struct drm_pending_event **out)
471 {
472 	struct drm_pending_event *e;
473 	bool ret = false;
474 
475 	/* Already locked in drm_read(). */
476 	/* DRM_SPINLOCK_IRQSAVE(&dev->event_lock, flags); */
477 
478 	*out = NULL;
479 	if (list_empty(&file_priv->event_list))
480 		goto out;
481 	e = list_first_entry(&file_priv->event_list,
482 			     struct drm_pending_event, link);
483 	if (e->event->length > uio->uio_resid)
484 		goto out;
485 
486 	file_priv->event_space += e->event->length;
487 	list_del(&e->link);
488 	*out = e;
489 	ret = true;
490 
491 out:
492 	/* DRM_SPINUNLOCK_IRQRESTORE(&dev->event_lock, flags); */
493 	return ret;
494 }
495 
496 int
drm_read(struct cdev * kdev,struct uio * uio,int ioflag)497 drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
498 {
499 	struct drm_file *file_priv;
500 	struct drm_device *dev;
501 	struct drm_pending_event *e;
502 	ssize_t error;
503 
504 	error = devfs_get_cdevpriv((void **)&file_priv);
505 	if (error != 0) {
506 		DRM_ERROR("can't find authenticator\n");
507 		return (EINVAL);
508 	}
509 
510 	dev = drm_get_device_from_kdev(kdev);
511 	mtx_lock(&dev->event_lock);
512 	while (list_empty(&file_priv->event_list)) {
513 		if ((ioflag & O_NONBLOCK) != 0) {
514 			error = EAGAIN;
515 			goto out;
516 		}
517 		error = msleep(&file_priv->event_space, &dev->event_lock,
518 	           PCATCH, "drmrea", 0);
519 	       if (error != 0)
520 		       goto out;
521 	}
522 
523 	while (drm_dequeue_event(file_priv, uio, &e)) {
524 		mtx_unlock(&dev->event_lock);
525 		error = uiomove(e->event, e->event->length, uio);
526 		CTR3(KTR_DRM, "drm_event_dequeued %d %d %d", curproc->p_pid,
527 		    e->event->type, e->event->length);
528 
529 		e->destroy(e);
530 		if (error != 0)
531 			return (error);
532 		mtx_lock(&dev->event_lock);
533 	}
534 
535 out:
536 	mtx_unlock(&dev->event_lock);
537 	return (error);
538 }
539 EXPORT_SYMBOL(drm_read);
540 
541 void
drm_event_wakeup(struct drm_pending_event * e)542 drm_event_wakeup(struct drm_pending_event *e)
543 {
544 	struct drm_file *file_priv;
545 	struct drm_device *dev;
546 
547 	file_priv = e->file_priv;
548 	dev = file_priv->minor->dev;
549 	mtx_assert(&dev->event_lock, MA_OWNED);
550 
551 	wakeup(&file_priv->event_space);
552 	selwakeup(&file_priv->event_poll);
553 }
554 
555 int
drm_poll(struct cdev * kdev,int events,struct thread * td)556 drm_poll(struct cdev *kdev, int events, struct thread *td)
557 {
558 	struct drm_file *file_priv;
559 	struct drm_device *dev;
560 	int error, revents;
561 
562 	error = devfs_get_cdevpriv((void **)&file_priv);
563 	if (error != 0) {
564 		DRM_ERROR("can't find authenticator\n");
565 		return (EINVAL);
566 	}
567 
568 	dev = drm_get_device_from_kdev(kdev);
569 
570 	revents = 0;
571 	mtx_lock(&dev->event_lock);
572 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
573 		if (list_empty(&file_priv->event_list)) {
574 			CTR0(KTR_DRM, "drm_poll empty list");
575 			selrecord(td, &file_priv->event_poll);
576 		} else {
577 			revents |= events & (POLLIN | POLLRDNORM);
578 			CTR1(KTR_DRM, "drm_poll revents %x", revents);
579 		}
580 	}
581 	mtx_unlock(&dev->event_lock);
582 	return (revents);
583 }
584 EXPORT_SYMBOL(drm_poll);
585 
586 int
drm_mmap_single(struct cdev * kdev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** obj_res,int nprot)587 drm_mmap_single(struct cdev *kdev, vm_ooffset_t *offset, vm_size_t size,
588     struct vm_object **obj_res, int nprot)
589 {
590 	struct drm_device *dev;
591 
592 	dev = drm_get_device_from_kdev(kdev);
593 	if (dev->drm_ttm_bdev != NULL) {
594 		return (-ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
595 		    obj_res, nprot));
596 	} else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
597 		return (-drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
598 	} else {
599 		return (ENODEV);
600 	}
601 }
602