xref: /gfx-drm/usr/src/uts/common/io/drm/drm_fops.c (revision 47dc10d7)
1 /*
2  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
3  */
4 
5 /**
6  * \file drm_fops.c
7  * File operations for DRM
8  *
9  * \author Rickard E. (Rik) Faith <faith@valinux.com>
10  * \author Daryll Strauss <daryll@valinux.com>
11  * \author Gareth Hughes <gareth@valinux.com>
12  */
13 
14 /*
15  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
16  *
17  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
18  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
19  * Copyright (c) 2009, 2013, Intel Corporation.
20  * All Rights Reserved.
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a
23  * copy of this software and associated documentation files (the "Software"),
24  * to deal in the Software without restriction, including without limitation
25  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
26  * and/or sell copies of the Software, and to permit persons to whom the
27  * Software is furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice (including the next
30  * paragraph) shall be included in all copies or substantial portions of the
31  * Software.
32  *
33  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
36  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
37  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
38  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
39  * OTHER DEALINGS IN THE SOFTWARE.
40  */
41 
42 #include "drmP.h"
43 
44 static int drm_open_helper(struct drm_minor *minor,
45 			int clone_id, int flags, cred_t *credp);
46 
drm_device_is_agp(struct drm_device * dev)47 static __inline__ int drm_device_is_agp(struct drm_device *dev)
48 {
49 	if (drm_core_check_feature(dev, DRIVER_USE_PLATFORM_DEVICE))
50 		return 0;
51 	if (dev->driver->device_is_agp != NULL) {
52 		int err = (*dev->driver->device_is_agp) (dev);
53 
54 		if (err != 2) {
55 			return err;
56 		}
57 	}
58 
59 	return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
60 }
61 
drm_setup(struct drm_device * dev)62 static int drm_setup(struct drm_device * dev)
63 {
64 	int i;
65 	int ret;
66 	static bool first_call = true;
67 
68 	if (first_call) {
69 		/* OSOL_drm: moved from drm_fill_in_dev */
70 		if (drm_core_has_AGP(dev)) {
71 			if (drm_device_is_agp(dev))
72 				dev->agp = drm_agp_init(dev);
73 			if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
74 			    && (dev->agp == NULL)) {
75 				DRM_ERROR("Cannot initialize the agpgart module.\n");
76 				return -EINVAL;
77 			}
78 		}
79 	}
80 
81 	if (dev->driver->firstopen) {
82 		ret = dev->driver->firstopen(dev);
83 		if (ret != 0)
84 			return ret;
85 	}
86 
87 	if (first_call) {
88 		/* OSOL_drm: moved from drm_get_dev */
89 		/* setup the grouping for the legacy output */
90 		if (drm_core_check_feature(dev, DRIVER_MODESET)) {
91 			ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
92 			if (ret)
93 				return ret;
94 		}
95 	}
96 
97 	atomic_set(&dev->ioctl_count, 0);
98 
99 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
100 	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
101 		dev->buf_use = 0;
102 		atomic_set(&dev->buf_alloc, 0);
103 
104 		i = drm_dma_setup(dev);
105 		if (i < 0)
106 			return i;
107 	}
108 
109 	for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
110 		atomic_set(&dev->counts[i], 0);
111 
112 	dev->context_flag = 0;
113 	dev->last_context = 0;
114 	dev->if_version = 0;
115 
116 
117 	DRM_DEBUG("\n");
118 
119 	/*
120 	 * The kernel's context could be created here, but is now created
121 	 * in drm_dma_enqueue.  This is more resource-efficient for
122 	 * hardware that does not do DMA, but may mean that
123 	 * drm_select_queue fails between the time the interrupt is
124 	 * initialized and the time the queues are initialized.
125 	 */
126 
127 	first_call = false;
128 	return 0;
129 }
130 
131 /**
132  * Open file.
133  *
134  * \return zero on success or a negative number on failure.
135  *
136  * Searches the DRM device with the same minor number, calls open_helper(), and
137  * increments the device open count. If the open count was previous at zero,
138  * i.e., it's the first that the device is open, then calls setup().
139  */
drm_open(struct drm_minor * minor,int clone_id,int flags,cred_t * credp)140 int drm_open(struct drm_minor *minor, int clone_id, int flags, cred_t *credp)
141 {
142 	struct drm_device *dev = minor->dev;
143 	int retcode = 0;
144 
145 	DRM_DEBUG("minor->index=%d, clone_id=%d", minor->index, clone_id);
146 
147 	retcode = drm_open_helper(minor, clone_id, flags, credp);
148 	if (!retcode) {
149 		atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
150 		spin_lock(&dev->count_lock);
151 		if (!dev->open_count++) {
152 			spin_unlock(&dev->count_lock);
153 			retcode = drm_setup(dev);
154 			goto out;
155 		}
156 		spin_unlock(&dev->count_lock);
157 	}
158 out:
159 	return retcode;
160 }
161 
162 /**
163  * Called whenever a process opens /dev/drm.
164  *
165  * Creates and initializes a drm_file structure for the file private data in \p
166  * filp and add it into the double linked list in \p dev.
167  */
drm_open_helper(struct drm_minor * minor,int clone_id,int flags,cred_t * credp)168 static int drm_open_helper(struct drm_minor *minor,
169 			int clone_id, int flags, cred_t *credp)
170 {
171 	struct drm_device *dev = minor->dev;
172 	struct drm_file *priv;
173 	int minor_id = minor->index;
174 	int ret;
175 
176 	if (flags & FEXCL)
177 		return -EBUSY;	/* No exclusive opens */
178 
179 	if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
180 		return -EINVAL;
181 
182 	DRM_DEBUG("pid = %d, minor = %d\n", ddi_get_pid(), minor_id);
183 
184 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
185 	if (!priv)
186 		return -ENOMEM;
187 
188 	(void) memset(priv, 0, sizeof(*priv));
189 	(void) idr_replace(&minor->clone_idr, priv, clone_id);  /* OSOL_drm */
190 	priv->uid = crgetsuid(credp);
191 	priv->pid = ddi_get_pid();
192 	priv->minor = minor;
193 	priv->ioctl_count = 0;
194 	/* for compatibility root is always authenticated */
195 	priv->authenticated = DRM_SUSER(credp);
196 	priv->lock_count = 0;
197 
198 	INIT_LIST_HEAD(&priv->lhead);
199 	INIT_LIST_HEAD(&priv->fbs);
200 	mutex_init(&priv->fbs_lock, NULL, MUTEX_DRIVER, NULL);
201 	INIT_LIST_HEAD(&priv->event_list);
202 	DRM_INIT_WAITQUEUE(&priv->event_wait, DRM_INTR_PRI(dev));
203 	priv->event_space = 4096; /* set aside 4k for event buffer */
204 
205 	if (dev->driver->driver_features & DRIVER_GEM)
206 		drm_gem_open(dev, priv);
207 
208 	if (dev->driver->open) {
209 		ret = dev->driver->open(dev, priv);
210 		if (ret < 0)
211 			goto out_free;
212 	}
213 
214 
215 	/* if there is no current master make this fd it */
216 	mutex_lock(&dev->struct_mutex);
217 	if (!priv->minor->master) {
218 		/* create a new master */
219 		priv->minor->master = drm_master_create(priv->minor);
220 		if (!priv->minor->master) {
221 			mutex_unlock(&dev->struct_mutex);
222 			ret = -ENOMEM;
223 			goto out_free;
224 		}
225 
226 		priv->is_master = 1;
227 		/* take another reference for the copy in the local file priv */
228 		priv->master = drm_master_get(priv->minor->master);
229 
230 		priv->authenticated = 1;
231 
232 		mutex_unlock(&dev->struct_mutex);
233 		if (dev->driver->master_create) {
234 			ret = dev->driver->master_create(dev, priv->master);
235 			if (ret) {
236 				mutex_lock(&dev->struct_mutex);
237 				/* drop both references if this fails */
238 				drm_master_put(&priv->minor->master);
239 				drm_master_put(&priv->master);
240 				mutex_unlock(&dev->struct_mutex);
241 				goto out_free;
242 			}
243 		}
244 		mutex_lock(&dev->struct_mutex);
245 		if (dev->driver->master_set) {
246 			ret = dev->driver->master_set(dev, priv, true);
247 			if (ret) {
248 				/* drop both references if this fails */
249 				drm_master_put(&priv->minor->master);
250 				drm_master_put(&priv->master);
251 				mutex_unlock(&dev->struct_mutex);
252 				goto out_free;
253 			}
254 		}
255 		mutex_unlock(&dev->struct_mutex);
256 	} else {
257 		/* get a reference to the master */
258 		priv->master = drm_master_get(priv->minor->master);
259 		mutex_unlock(&dev->struct_mutex);
260 	}
261 
262 	mutex_lock(&dev->struct_mutex);
263 	list_add(&priv->lhead, &dev->filelist, (caddr_t)priv);
264 	mutex_unlock(&dev->struct_mutex);
265 
266 	return 0;
267 out_free:
268 	kfree(priv, sizeof (*priv));
269 	return ret;
270 }
271 
drm_master_release(struct drm_device * dev,struct drm_file * fpriv)272 void drm_master_release(struct drm_device *dev, struct drm_file *fpriv)
273 {
274 	struct drm_master *master = fpriv->master;
275 
276 	if (drm_i_have_hw_lock(dev, fpriv)) {
277 		DRM_DEBUG("Process %d dead, freeing lock for context %d",
278 		    DRM_CURRENTPID,  _DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock));
279 		(void) drm_lock_free(&master->lock,
280 		    _DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock));
281 	}
282 
283 }
284 
drm_events_release(struct drm_file * file_priv)285 static void drm_events_release(struct drm_file *file_priv)
286 {
287 	struct drm_device *dev = file_priv->minor->dev;
288 	struct drm_pending_event *e, *et;
289 	struct drm_pending_vblank_event *v, *vt;
290 	unsigned long flags;
291 
292 	spin_lock_irqsave(&dev->event_lock, flags);
293 
294 	/* Remove pending flips */
295 	list_for_each_entry_safe(v, vt, struct drm_pending_vblank_event, &dev->vblank_event_list, base.link)
296 	if (v->base.file_priv == file_priv) {
297 		list_del(&v->base.link);
298 		drm_vblank_put(dev, v->pipe);
299 		v->base.destroy(&v->base, sizeof(struct drm_pending_vblank_event));
300 	}
301 
302 	/* Remove unconsumed events */
303 	list_for_each_entry_safe(e, et, struct drm_pending_event, &file_priv->event_list, link)
304 		e->destroy(e,  sizeof(struct drm_pending_vblank_event));
305 
306 	spin_unlock_irqrestore(&dev->event_lock, flags);
307 }
308 
309 /**
310  * Release file.
311  *
312  * \return zero on success or a negative number on failure.
313  *
314  * If the hardware lock is held then free it, and take it again for the kernel
315  * context since it's necessary to reclaim buffers. Unlink the file private
316  * data from its list and free it. Decreases the open count and if it reaches
317  * zero calls drm_lastclose().
318  */
319 int
drm_release(struct drm_file * file_priv)320 drm_release(struct drm_file *file_priv)
321 {
322 	struct drm_device *dev = file_priv->minor->dev;
323 	int retcode = 0;
324 
325 
326 
327 	DRM_DEBUG("open_count = %d\n", dev->open_count);
328 
329 	if (dev->driver->preclose)
330 		dev->driver->preclose(dev, file_priv);
331 
332 	/* ========================================================
333 	 * Begin inline drm_release
334 	 */
335 
336 	/* Release any auth tokens that might point to this file_priv,
337 	   (do that under the drm_global_mutex) */
338 	if (file_priv->magic)
339 		(void) drm_remove_magic(file_priv->master, file_priv->magic);
340 
341 	/* if the master has gone away we can't do anything with the lock */
342 	if (file_priv->minor->master)
343 		drm_master_release(dev, file_priv);
344 
345 	if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
346 		drm_core_reclaim_buffers(dev, file_priv);
347 
348 	drm_events_release(file_priv);
349 
350 	if (dev->driver->driver_features & DRIVER_MODESET)
351 		drm_fb_release(file_priv);
352 
353 	if (dev->driver->driver_features & DRIVER_GEM)
354 		drm_gem_release(dev, file_priv);
355 
356 	mutex_lock(&dev->ctxlist_mutex);
357 	if (!list_empty(&dev->ctxlist)) {
358 		struct drm_ctx_list *pos, *n;
359 
360 		list_for_each_entry_safe(pos, n, struct drm_ctx_list, &dev->ctxlist, head) {
361 			if (pos->tag == file_priv &&
362 			    pos->handle != DRM_KERNEL_CONTEXT) {
363 				if (dev->driver->context_dtor)
364 					dev->driver->context_dtor(dev,
365 								  pos->handle);
366 
367 				drm_ctxbitmap_free(dev, pos->handle);
368 
369 				list_del(&pos->head);
370 				kfree(pos, sizeof (*pos));
371 				--dev->ctx_count;
372 			}
373 		}
374 	}
375 	mutex_unlock(&dev->ctxlist_mutex);
376 
377 	mutex_lock(&dev->struct_mutex);
378 
379 	if (file_priv->is_master) {
380 		struct drm_master *master = file_priv->master;
381 		struct drm_file *temp;
382 		list_for_each_entry(temp, struct drm_file, &dev->filelist, lhead) {
383 			if ((temp->master == file_priv->master) &&
384 			    (temp != file_priv))
385 				temp->authenticated = 0;
386 		}
387 
388 		/**
389 		 * Since the master is disappearing, so is the
390 		 * possibility to lock.
391 		 */
392 
393 		if (master->lock.hw_lock) {
394 			master->lock.hw_lock = NULL;
395 			master->lock.file_priv = NULL;
396 		}
397 
398 		if (file_priv->minor->master == file_priv->master) {
399 			/* drop the reference held my the minor */
400 			if (dev->driver->master_drop)
401 				dev->driver->master_drop(dev, file_priv, true);
402 			drm_master_put(&file_priv->minor->master);
403 		}
404 	}
405 
406 	/* drop the reference held my the file priv */
407 	drm_master_put(&file_priv->master);
408 	file_priv->is_master = 0;
409 	list_del(&file_priv->lhead);
410 	mutex_unlock(&dev->struct_mutex);
411 
412 	if (dev->driver->postclose)
413 		dev->driver->postclose(dev, file_priv);
414 	kfree(file_priv, sizeof (*file_priv));
415 
416 	/* ========================================================
417 	 * End inline drm_release
418 	 */
419 
420 	atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
421 	spin_lock(&dev->count_lock);
422 	if (!--dev->open_count) {
423 		if (atomic_read(&dev->ioctl_count)) {
424 			DRM_ERROR("Device busy: %d\n",
425 				  atomic_read(&dev->ioctl_count));
426 			spin_unlock(&dev->count_lock);
427 			return -EBUSY;
428 		}
429 		spin_unlock(&dev->count_lock);
430 		return drm_lastclose(dev);
431 	}
432 	spin_unlock(&dev->count_lock);
433 
434 	return retcode;
435 }
436 
437 static bool
drm_dequeue_event(struct drm_file * file_priv,size_t total,size_t max,struct drm_pending_event ** out)438 drm_dequeue_event(struct drm_file *file_priv,
439 	size_t total, size_t max, struct drm_pending_event **out)
440 {
441 	struct drm_device *dev = file_priv->minor->dev;
442 	struct drm_pending_event *e;
443 	unsigned long flags;
444 	bool ret = false;
445 
446 	spin_lock_irqsave(&dev->event_lock, flags);
447 
448 	*out = NULL;
449 	if (list_empty(&file_priv->event_list))
450 		goto out;
451 	e = list_first_entry(&file_priv->event_list,
452 	struct drm_pending_event, link);
453 	if (e->event->length + total > max)
454 		goto out;
455 
456 	file_priv->event_space += e->event->length;
457 	list_del(&e->link);
458 	*out = e;
459 	ret = true;
460 
461 out:
462 	spin_unlock_irqrestore(&dev->event_lock, flags);
463 	return ret;
464 }
465 
drm_read(struct drm_file * file_priv,struct uio * uiop)466 ssize_t drm_read(struct drm_file *file_priv, struct uio *uiop)
467 {
468 	struct drm_pending_event *e;
469 	size_t total;
470 	int ret = 0;
471 
472 	DRM_WAIT(ret, &file_priv->event_wait,
473 		!list_empty(&file_priv->event_list));
474 	if (ret < 0) {
475 		DRM_ERROR("returns %d event_list is %d", ret, list_empty(&file_priv->event_list));
476 		return ret;
477 	}
478 
479 	total = 0;
480 	while (drm_dequeue_event(file_priv, total, uiop->uio_iov->iov_len, &e)) {
481 		ret = uiomove((caddr_t)e->event, e->event->length, UIO_READ, uiop);
482 		if (ret) {
483 			DRM_ERROR("Failed to copy to user: %d", ret);
484 			return (0);
485 		}
486 		total += e->event->length;
487 		e->destroy(e, sizeof(struct drm_pending_vblank_event));
488 	}
489 
490 	return total;
491 }
492 
drm_poll(struct drm_file * file_priv,short events)493 short drm_poll(struct drm_file *file_priv, short events)
494 {
495 	short revent = 0;
496 
497 	if (!list_empty(&file_priv->event_list)) {
498 		if (events & POLLIN)
499 			revent |= POLLIN;
500 		if (events & POLLRDNORM)
501 			revent |= POLLRDNORM;
502 	}
503 
504 	return revent;
505 }
506 
507 
508