xref: /freebsd/sys/dev/drm2/drm_stub.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1*592ffb21SWarner Losh /**
2*592ffb21SWarner Losh  * \file drm_stub.h
3*592ffb21SWarner Losh  * Stub support
4*592ffb21SWarner Losh  *
5*592ffb21SWarner Losh  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6*592ffb21SWarner Losh  */
7*592ffb21SWarner Losh 
8*592ffb21SWarner Losh /*
9*592ffb21SWarner Losh  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
10*592ffb21SWarner Losh  *
11*592ffb21SWarner Losh  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12*592ffb21SWarner Losh  * All Rights Reserved.
13*592ffb21SWarner Losh  *
14*592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
15*592ffb21SWarner Losh  * copy of this software and associated documentation files (the "Software"),
16*592ffb21SWarner Losh  * to deal in the Software without restriction, including without limitation
17*592ffb21SWarner Losh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18*592ffb21SWarner Losh  * and/or sell copies of the Software, and to permit persons to whom the
19*592ffb21SWarner Losh  * Software is furnished to do so, subject to the following conditions:
20*592ffb21SWarner Losh  *
21*592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the next
22*592ffb21SWarner Losh  * paragraph) shall be included in all copies or substantial portions of the
23*592ffb21SWarner Losh  * Software.
24*592ffb21SWarner Losh  *
25*592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26*592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27*592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28*592ffb21SWarner Losh  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29*592ffb21SWarner Losh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30*592ffb21SWarner Losh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31*592ffb21SWarner Losh  * DEALINGS IN THE SOFTWARE.
32*592ffb21SWarner Losh  */
33*592ffb21SWarner Losh 
34*592ffb21SWarner Losh #include <sys/cdefs.h>
35*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
36*592ffb21SWarner Losh #include <dev/drm2/drm_core.h>
37*592ffb21SWarner Losh 
38*592ffb21SWarner Losh #ifdef DRM_DEBUG_DEFAULT_ON
39*592ffb21SWarner Losh unsigned int drm_debug = (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS |
40*592ffb21SWarner Losh     DRM_DEBUGBITS_FAILED_IOCTL);
41*592ffb21SWarner Losh #else
42*592ffb21SWarner Losh unsigned int drm_debug = 0;	/* 1 to enable debug output */
43*592ffb21SWarner Losh #endif
44*592ffb21SWarner Losh EXPORT_SYMBOL(drm_debug);
45*592ffb21SWarner Losh 
46*592ffb21SWarner Losh unsigned int drm_notyet = 0;
47*592ffb21SWarner Losh 
48*592ffb21SWarner Losh unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
49*592ffb21SWarner Losh EXPORT_SYMBOL(drm_vblank_offdelay);
50*592ffb21SWarner Losh 
51*592ffb21SWarner Losh unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
52*592ffb21SWarner Losh EXPORT_SYMBOL(drm_timestamp_precision);
53*592ffb21SWarner Losh 
54*592ffb21SWarner Losh /*
55*592ffb21SWarner Losh  * Default to use monotonic timestamps for wait-for-vblank and page-flip
56*592ffb21SWarner Losh  * complete events.
57*592ffb21SWarner Losh  */
58*592ffb21SWarner Losh unsigned int drm_timestamp_monotonic = 1;
59*592ffb21SWarner Losh 
60*592ffb21SWarner Losh MODULE_AUTHOR(CORE_AUTHOR);
61*592ffb21SWarner Losh MODULE_DESCRIPTION(CORE_DESC);
62*592ffb21SWarner Losh MODULE_LICENSE("GPL and additional rights");
63*592ffb21SWarner Losh MODULE_PARM_DESC(debug, "Enable debug output");
64*592ffb21SWarner Losh MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
65*592ffb21SWarner Losh MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
66*592ffb21SWarner Losh MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
67*592ffb21SWarner Losh 
68*592ffb21SWarner Losh module_param_named(debug, drm_debug, int, 0600);
69*592ffb21SWarner Losh module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
70*592ffb21SWarner Losh module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
71*592ffb21SWarner Losh module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
72*592ffb21SWarner Losh 
73*592ffb21SWarner Losh static struct cdevsw drm_cdevsw = {
74*592ffb21SWarner Losh 	.d_version =	D_VERSION,
75*592ffb21SWarner Losh 	.d_open =	drm_open,
76*592ffb21SWarner Losh 	.d_read =	drm_read,
77*592ffb21SWarner Losh 	.d_ioctl =	drm_ioctl,
78*592ffb21SWarner Losh 	.d_poll =	drm_poll,
79*592ffb21SWarner Losh 	.d_mmap_single = drm_mmap_single,
80*592ffb21SWarner Losh 	.d_name =	"drm",
81*592ffb21SWarner Losh 	.d_flags =	D_TRACKCLOSE
82*592ffb21SWarner Losh };
83*592ffb21SWarner Losh 
drm_minor_get_id(struct drm_device * dev,int type)84*592ffb21SWarner Losh static int drm_minor_get_id(struct drm_device *dev, int type)
85*592ffb21SWarner Losh {
86*592ffb21SWarner Losh 	int new_id;
87*592ffb21SWarner Losh 
88*592ffb21SWarner Losh 	new_id = device_get_unit(dev->dev);
89*592ffb21SWarner Losh 
90*592ffb21SWarner Losh 	if (new_id >= 64)
91*592ffb21SWarner Losh 		return -EINVAL;
92*592ffb21SWarner Losh 
93*592ffb21SWarner Losh 	if (type == DRM_MINOR_CONTROL) {
94*592ffb21SWarner Losh 		new_id += 64;
95*592ffb21SWarner Losh 	} else if (type == DRM_MINOR_RENDER) {
96*592ffb21SWarner Losh 		new_id += 128;
97*592ffb21SWarner Losh 	}
98*592ffb21SWarner Losh 
99*592ffb21SWarner Losh 	return new_id;
100*592ffb21SWarner Losh }
101*592ffb21SWarner Losh 
drm_master_create(struct drm_minor * minor)102*592ffb21SWarner Losh struct drm_master *drm_master_create(struct drm_minor *minor)
103*592ffb21SWarner Losh {
104*592ffb21SWarner Losh 	struct drm_master *master;
105*592ffb21SWarner Losh 
106*592ffb21SWarner Losh 	master = malloc(sizeof(*master), DRM_MEM_KMS, M_NOWAIT | M_ZERO);
107*592ffb21SWarner Losh 	if (!master)
108*592ffb21SWarner Losh 		return NULL;
109*592ffb21SWarner Losh 
110*592ffb21SWarner Losh 	refcount_init(&master->refcount, 1);
111*592ffb21SWarner Losh 	mtx_init(&master->lock.spinlock, "drm_master__lock__spinlock",
112*592ffb21SWarner Losh 	    NULL, MTX_DEF);
113*592ffb21SWarner Losh 	DRM_INIT_WAITQUEUE(&master->lock.lock_queue);
114*592ffb21SWarner Losh 	drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
115*592ffb21SWarner Losh 	INIT_LIST_HEAD(&master->magicfree);
116*592ffb21SWarner Losh 	master->minor = minor;
117*592ffb21SWarner Losh 
118*592ffb21SWarner Losh 	list_add_tail(&master->head, &minor->master_list);
119*592ffb21SWarner Losh 
120*592ffb21SWarner Losh 	return master;
121*592ffb21SWarner Losh }
122*592ffb21SWarner Losh 
drm_master_get(struct drm_master * master)123*592ffb21SWarner Losh struct drm_master *drm_master_get(struct drm_master *master)
124*592ffb21SWarner Losh {
125*592ffb21SWarner Losh 	refcount_acquire(&master->refcount);
126*592ffb21SWarner Losh 	return master;
127*592ffb21SWarner Losh }
128*592ffb21SWarner Losh EXPORT_SYMBOL(drm_master_get);
129*592ffb21SWarner Losh 
drm_master_destroy(struct drm_master * master)130*592ffb21SWarner Losh static void drm_master_destroy(struct drm_master *master)
131*592ffb21SWarner Losh {
132*592ffb21SWarner Losh 	struct drm_magic_entry *pt, *next;
133*592ffb21SWarner Losh 	struct drm_device *dev = master->minor->dev;
134*592ffb21SWarner Losh 	struct drm_map_list *r_list, *list_temp;
135*592ffb21SWarner Losh 
136*592ffb21SWarner Losh 	list_del(&master->head);
137*592ffb21SWarner Losh 
138*592ffb21SWarner Losh 	if (dev->driver->master_destroy)
139*592ffb21SWarner Losh 		dev->driver->master_destroy(dev, master);
140*592ffb21SWarner Losh 
141*592ffb21SWarner Losh 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
142*592ffb21SWarner Losh 		if (r_list->master == master) {
143*592ffb21SWarner Losh 			drm_rmmap_locked(dev, r_list->map);
144*592ffb21SWarner Losh 			r_list = NULL;
145*592ffb21SWarner Losh 		}
146*592ffb21SWarner Losh 	}
147*592ffb21SWarner Losh 
148*592ffb21SWarner Losh 	if (master->unique) {
149*592ffb21SWarner Losh 		free(master->unique, DRM_MEM_DRIVER);
150*592ffb21SWarner Losh 		master->unique = NULL;
151*592ffb21SWarner Losh 		master->unique_len = 0;
152*592ffb21SWarner Losh 	}
153*592ffb21SWarner Losh 
154*592ffb21SWarner Losh 	list_for_each_entry_safe(pt, next, &master->magicfree, head) {
155*592ffb21SWarner Losh 		list_del(&pt->head);
156*592ffb21SWarner Losh 		drm_ht_remove_item(&master->magiclist, &pt->hash_item);
157*592ffb21SWarner Losh 		free(pt, DRM_MEM_MAGIC);
158*592ffb21SWarner Losh 	}
159*592ffb21SWarner Losh 
160*592ffb21SWarner Losh 	drm_ht_remove(&master->magiclist);
161*592ffb21SWarner Losh 
162*592ffb21SWarner Losh 	free(master, DRM_MEM_KMS);
163*592ffb21SWarner Losh }
164*592ffb21SWarner Losh 
drm_master_put(struct drm_master ** master)165*592ffb21SWarner Losh void drm_master_put(struct drm_master **master)
166*592ffb21SWarner Losh {
167*592ffb21SWarner Losh 	if (refcount_release(&(*master)->refcount))
168*592ffb21SWarner Losh 		drm_master_destroy(*master);
169*592ffb21SWarner Losh 	*master = NULL;
170*592ffb21SWarner Losh }
171*592ffb21SWarner Losh EXPORT_SYMBOL(drm_master_put);
172*592ffb21SWarner Losh 
drm_setmaster_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)173*592ffb21SWarner Losh int drm_setmaster_ioctl(struct drm_device *dev, void *data,
174*592ffb21SWarner Losh 			struct drm_file *file_priv)
175*592ffb21SWarner Losh {
176*592ffb21SWarner Losh 	int ret;
177*592ffb21SWarner Losh 
178*592ffb21SWarner Losh 	if (file_priv->is_master)
179*592ffb21SWarner Losh 		return 0;
180*592ffb21SWarner Losh 
181*592ffb21SWarner Losh 	if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
182*592ffb21SWarner Losh 		return -EINVAL;
183*592ffb21SWarner Losh 
184*592ffb21SWarner Losh 	if (!file_priv->master)
185*592ffb21SWarner Losh 		return -EINVAL;
186*592ffb21SWarner Losh 
187*592ffb21SWarner Losh 	if (file_priv->minor->master)
188*592ffb21SWarner Losh 		return -EINVAL;
189*592ffb21SWarner Losh 
190*592ffb21SWarner Losh 	DRM_LOCK(dev);
191*592ffb21SWarner Losh 	file_priv->minor->master = drm_master_get(file_priv->master);
192*592ffb21SWarner Losh 	file_priv->is_master = 1;
193*592ffb21SWarner Losh 	if (dev->driver->master_set) {
194*592ffb21SWarner Losh 		ret = dev->driver->master_set(dev, file_priv, false);
195*592ffb21SWarner Losh 		if (unlikely(ret != 0)) {
196*592ffb21SWarner Losh 			file_priv->is_master = 0;
197*592ffb21SWarner Losh 			drm_master_put(&file_priv->minor->master);
198*592ffb21SWarner Losh 		}
199*592ffb21SWarner Losh 	}
200*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
201*592ffb21SWarner Losh 
202*592ffb21SWarner Losh 	return 0;
203*592ffb21SWarner Losh }
204*592ffb21SWarner Losh 
drm_dropmaster_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)205*592ffb21SWarner Losh int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
206*592ffb21SWarner Losh 			 struct drm_file *file_priv)
207*592ffb21SWarner Losh {
208*592ffb21SWarner Losh 	if (!file_priv->is_master)
209*592ffb21SWarner Losh 		return -EINVAL;
210*592ffb21SWarner Losh 
211*592ffb21SWarner Losh 	if (!file_priv->minor->master)
212*592ffb21SWarner Losh 		return -EINVAL;
213*592ffb21SWarner Losh 
214*592ffb21SWarner Losh 	DRM_LOCK(dev);
215*592ffb21SWarner Losh 	if (dev->driver->master_drop)
216*592ffb21SWarner Losh 		dev->driver->master_drop(dev, file_priv, false);
217*592ffb21SWarner Losh 	drm_master_put(&file_priv->minor->master);
218*592ffb21SWarner Losh 	file_priv->is_master = 0;
219*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
220*592ffb21SWarner Losh 	return 0;
221*592ffb21SWarner Losh }
222*592ffb21SWarner Losh 
drm_fill_in_dev(struct drm_device * dev,struct drm_driver * driver)223*592ffb21SWarner Losh int drm_fill_in_dev(struct drm_device *dev,
224*592ffb21SWarner Losh 			   struct drm_driver *driver)
225*592ffb21SWarner Losh {
226*592ffb21SWarner Losh 	int retcode, i;
227*592ffb21SWarner Losh 
228*592ffb21SWarner Losh 	INIT_LIST_HEAD(&dev->filelist);
229*592ffb21SWarner Losh 	INIT_LIST_HEAD(&dev->ctxlist);
230*592ffb21SWarner Losh 	INIT_LIST_HEAD(&dev->maplist);
231*592ffb21SWarner Losh 	INIT_LIST_HEAD(&dev->vblank_event_list);
232*592ffb21SWarner Losh 
233*592ffb21SWarner Losh 	mtx_init(&dev->irq_lock, "drmirq", NULL, MTX_DEF);
234*592ffb21SWarner Losh 	mtx_init(&dev->count_lock, "drmcount", NULL, MTX_DEF);
235*592ffb21SWarner Losh 	mtx_init(&dev->event_lock, "drmev", NULL, MTX_DEF);
236*592ffb21SWarner Losh 	sx_init(&dev->dev_struct_lock, "drmslk");
237*592ffb21SWarner Losh 	mtx_init(&dev->ctxlist_mutex, "drmctxlist", NULL, MTX_DEF);
238*592ffb21SWarner Losh 	mtx_init(&dev->pcir_lock, "drmpcir", NULL, MTX_DEF);
239*592ffb21SWarner Losh 
240*592ffb21SWarner Losh 	if (drm_ht_create(&dev->map_hash, 12)) {
241*592ffb21SWarner Losh 		return -ENOMEM;
242*592ffb21SWarner Losh 	}
243*592ffb21SWarner Losh 
244*592ffb21SWarner Losh 	/* the DRM has 6 basic counters */
245*592ffb21SWarner Losh 	dev->counters = 6;
246*592ffb21SWarner Losh 	dev->types[0] = _DRM_STAT_LOCK;
247*592ffb21SWarner Losh 	dev->types[1] = _DRM_STAT_OPENS;
248*592ffb21SWarner Losh 	dev->types[2] = _DRM_STAT_CLOSES;
249*592ffb21SWarner Losh 	dev->types[3] = _DRM_STAT_IOCTLS;
250*592ffb21SWarner Losh 	dev->types[4] = _DRM_STAT_LOCKS;
251*592ffb21SWarner Losh 	dev->types[5] = _DRM_STAT_UNLOCKS;
252*592ffb21SWarner Losh 
253*592ffb21SWarner Losh 	/*
254*592ffb21SWarner Losh 	 * FIXME Linux<->FreeBSD: this is done in drm_setup() on Linux.
255*592ffb21SWarner Losh 	 */
256*592ffb21SWarner Losh 	for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
257*592ffb21SWarner Losh 		atomic_set(&dev->counts[i], 0);
258*592ffb21SWarner Losh 
259*592ffb21SWarner Losh 	dev->driver = driver;
260*592ffb21SWarner Losh 
261*592ffb21SWarner Losh 	retcode = drm_pci_agp_init(dev);
262*592ffb21SWarner Losh 	if (retcode)
263*592ffb21SWarner Losh 		goto error_out_unreg;
264*592ffb21SWarner Losh 
265*592ffb21SWarner Losh 
266*592ffb21SWarner Losh 
267*592ffb21SWarner Losh 	retcode = drm_ctxbitmap_init(dev);
268*592ffb21SWarner Losh 	if (retcode) {
269*592ffb21SWarner Losh 		DRM_ERROR("Cannot allocate memory for context bitmap.\n");
270*592ffb21SWarner Losh 		goto error_out_unreg;
271*592ffb21SWarner Losh 	}
272*592ffb21SWarner Losh 
273*592ffb21SWarner Losh 	if (driver->driver_features & DRIVER_GEM) {
274*592ffb21SWarner Losh 		retcode = drm_gem_init(dev);
275*592ffb21SWarner Losh 		if (retcode) {
276*592ffb21SWarner Losh 			DRM_ERROR("Cannot initialize graphics execution "
277*592ffb21SWarner Losh 				  "manager (GEM)\n");
278*592ffb21SWarner Losh 			goto error_out_unreg;
279*592ffb21SWarner Losh 		}
280*592ffb21SWarner Losh 	}
281*592ffb21SWarner Losh 
282*592ffb21SWarner Losh 	retcode = drm_sysctl_init(dev);
283*592ffb21SWarner Losh 	if (retcode != 0) {
284*592ffb21SWarner Losh 		DRM_ERROR("Failed to create hw.dri sysctl entry: %d\n",
285*592ffb21SWarner Losh 		    retcode);
286*592ffb21SWarner Losh 	}
287*592ffb21SWarner Losh 
288*592ffb21SWarner Losh 	return 0;
289*592ffb21SWarner Losh 
290*592ffb21SWarner Losh       error_out_unreg:
291*592ffb21SWarner Losh 	drm_cancel_fill_in_dev(dev);
292*592ffb21SWarner Losh 	return retcode;
293*592ffb21SWarner Losh }
294*592ffb21SWarner Losh EXPORT_SYMBOL(drm_fill_in_dev);
295*592ffb21SWarner Losh 
drm_cancel_fill_in_dev(struct drm_device * dev)296*592ffb21SWarner Losh void drm_cancel_fill_in_dev(struct drm_device *dev)
297*592ffb21SWarner Losh {
298*592ffb21SWarner Losh 	struct drm_driver *driver;
299*592ffb21SWarner Losh 
300*592ffb21SWarner Losh 	driver = dev->driver;
301*592ffb21SWarner Losh 
302*592ffb21SWarner Losh 	drm_sysctl_cleanup(dev);
303*592ffb21SWarner Losh 	if (driver->driver_features & DRIVER_GEM)
304*592ffb21SWarner Losh 		drm_gem_destroy(dev);
305*592ffb21SWarner Losh 	drm_ctxbitmap_cleanup(dev);
306*592ffb21SWarner Losh 
307*592ffb21SWarner Losh 	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
308*592ffb21SWarner Losh 	    dev->agp && dev->agp->agp_mtrr >= 0) {
309*592ffb21SWarner Losh 		int retval;
310*592ffb21SWarner Losh 		retval = drm_mtrr_del(dev->agp->agp_mtrr,
311*592ffb21SWarner Losh 				  dev->agp->agp_info.ai_aperture_base,
312*592ffb21SWarner Losh 				  dev->agp->agp_info.ai_aperture_size,
313*592ffb21SWarner Losh 				  DRM_MTRR_WC);
314*592ffb21SWarner Losh 		DRM_DEBUG("mtrr_del=%d\n", retval);
315*592ffb21SWarner Losh 	}
316*592ffb21SWarner Losh 	free(dev->agp, DRM_MEM_AGPLISTS);
317*592ffb21SWarner Losh 	dev->agp = NULL;
318*592ffb21SWarner Losh 
319*592ffb21SWarner Losh 	drm_ht_remove(&dev->map_hash);
320*592ffb21SWarner Losh 
321*592ffb21SWarner Losh 	mtx_destroy(&dev->irq_lock);
322*592ffb21SWarner Losh 	mtx_destroy(&dev->count_lock);
323*592ffb21SWarner Losh 	mtx_destroy(&dev->event_lock);
324*592ffb21SWarner Losh 	sx_destroy(&dev->dev_struct_lock);
325*592ffb21SWarner Losh 	mtx_destroy(&dev->ctxlist_mutex);
326*592ffb21SWarner Losh 	mtx_destroy(&dev->pcir_lock);
327*592ffb21SWarner Losh }
328*592ffb21SWarner Losh 
329*592ffb21SWarner Losh /**
330*592ffb21SWarner Losh  * Get a secondary minor number.
331*592ffb21SWarner Losh  *
332*592ffb21SWarner Losh  * \param dev device data structure
333*592ffb21SWarner Losh  * \param sec-minor structure to hold the assigned minor
334*592ffb21SWarner Losh  * \return negative number on failure.
335*592ffb21SWarner Losh  *
336*592ffb21SWarner Losh  * Search an empty entry and initialize it to the given parameters, and
337*592ffb21SWarner Losh  * create the proc init entry via proc_init(). This routines assigns
338*592ffb21SWarner Losh  * minor numbers to secondary heads of multi-headed cards
339*592ffb21SWarner Losh  */
drm_get_minor(struct drm_device * dev,struct drm_minor ** minor,int type)340*592ffb21SWarner Losh int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
341*592ffb21SWarner Losh {
342*592ffb21SWarner Losh 	struct drm_minor *new_minor;
343*592ffb21SWarner Losh 	int ret;
344*592ffb21SWarner Losh 	int minor_id;
345*592ffb21SWarner Losh 	const char *minor_devname;
346*592ffb21SWarner Losh 
347*592ffb21SWarner Losh 	DRM_DEBUG("\n");
348*592ffb21SWarner Losh 
349*592ffb21SWarner Losh 	minor_id = drm_minor_get_id(dev, type);
350*592ffb21SWarner Losh 	if (minor_id < 0)
351*592ffb21SWarner Losh 		return minor_id;
352*592ffb21SWarner Losh 
353*592ffb21SWarner Losh 	new_minor = malloc(sizeof(struct drm_minor), DRM_MEM_MINOR,
354*592ffb21SWarner Losh 	    M_NOWAIT | M_ZERO);
355*592ffb21SWarner Losh 	if (!new_minor) {
356*592ffb21SWarner Losh 		ret = -ENOMEM;
357*592ffb21SWarner Losh 		goto err_idr;
358*592ffb21SWarner Losh 	}
359*592ffb21SWarner Losh 
360*592ffb21SWarner Losh 	new_minor->type = type;
361*592ffb21SWarner Losh 	new_minor->dev = dev;
362*592ffb21SWarner Losh 	new_minor->index = minor_id;
363*592ffb21SWarner Losh 	INIT_LIST_HEAD(&new_minor->master_list);
364*592ffb21SWarner Losh 
365*592ffb21SWarner Losh 	new_minor->buf_sigio = NULL;
366*592ffb21SWarner Losh 
367*592ffb21SWarner Losh 	switch (type) {
368*592ffb21SWarner Losh 	case DRM_MINOR_CONTROL:
369*592ffb21SWarner Losh 		minor_devname = "dri/controlD%d";
370*592ffb21SWarner Losh 		break;
371*592ffb21SWarner Losh 	case DRM_MINOR_RENDER:
372*592ffb21SWarner Losh 		minor_devname = "dri/renderD%d";
373*592ffb21SWarner Losh 		break;
374*592ffb21SWarner Losh 	default:
375*592ffb21SWarner Losh 		minor_devname = "dri/card%d";
376*592ffb21SWarner Losh 		break;
377*592ffb21SWarner Losh 	}
378*592ffb21SWarner Losh 
379*592ffb21SWarner Losh 	ret = make_dev_p(MAKEDEV_WAITOK | MAKEDEV_CHECKNAME, &new_minor->device,
380*592ffb21SWarner Losh 	    &drm_cdevsw, 0, DRM_DEV_UID, DRM_DEV_GID,
381*592ffb21SWarner Losh 	    DRM_DEV_MODE, minor_devname, minor_id);
382*592ffb21SWarner Losh 	if (ret) {
383*592ffb21SWarner Losh 		DRM_ERROR("Failed to create cdev: %d\n", ret);
384*592ffb21SWarner Losh 		goto err_mem;
385*592ffb21SWarner Losh 	}
386*592ffb21SWarner Losh 	new_minor->device->si_drv1 = new_minor;
387*592ffb21SWarner Losh 	*minor = new_minor;
388*592ffb21SWarner Losh 
389*592ffb21SWarner Losh 	DRM_DEBUG("new minor assigned %d\n", minor_id);
390*592ffb21SWarner Losh 	return 0;
391*592ffb21SWarner Losh 
392*592ffb21SWarner Losh 
393*592ffb21SWarner Losh err_mem:
394*592ffb21SWarner Losh 	free(new_minor, DRM_MEM_MINOR);
395*592ffb21SWarner Losh err_idr:
396*592ffb21SWarner Losh 	*minor = NULL;
397*592ffb21SWarner Losh 	return ret;
398*592ffb21SWarner Losh }
399*592ffb21SWarner Losh EXPORT_SYMBOL(drm_get_minor);
400*592ffb21SWarner Losh 
401*592ffb21SWarner Losh /**
402*592ffb21SWarner Losh  * Put a secondary minor number.
403*592ffb21SWarner Losh  *
404*592ffb21SWarner Losh  * \param sec_minor - structure to be released
405*592ffb21SWarner Losh  * \return always zero
406*592ffb21SWarner Losh  *
407*592ffb21SWarner Losh  * Cleans up the proc resources. Not legal for this to be the
408*592ffb21SWarner Losh  * last minor released.
409*592ffb21SWarner Losh  *
410*592ffb21SWarner Losh  */
drm_put_minor(struct drm_minor ** minor_p)411*592ffb21SWarner Losh int drm_put_minor(struct drm_minor **minor_p)
412*592ffb21SWarner Losh {
413*592ffb21SWarner Losh 	struct drm_minor *minor = *minor_p;
414*592ffb21SWarner Losh 
415*592ffb21SWarner Losh 	DRM_DEBUG("release secondary minor %d\n", minor->index);
416*592ffb21SWarner Losh 
417*592ffb21SWarner Losh 	funsetown(&minor->buf_sigio);
418*592ffb21SWarner Losh 
419*592ffb21SWarner Losh 	destroy_dev(minor->device);
420*592ffb21SWarner Losh 
421*592ffb21SWarner Losh 	free(minor, DRM_MEM_MINOR);
422*592ffb21SWarner Losh 	*minor_p = NULL;
423*592ffb21SWarner Losh 	return 0;
424*592ffb21SWarner Losh }
425*592ffb21SWarner Losh EXPORT_SYMBOL(drm_put_minor);
426*592ffb21SWarner Losh 
427*592ffb21SWarner Losh /**
428*592ffb21SWarner Losh  * Called via drm_exit() at module unload time or when pci device is
429*592ffb21SWarner Losh  * unplugged.
430*592ffb21SWarner Losh  *
431*592ffb21SWarner Losh  * Cleans up all DRM device, calling drm_lastclose().
432*592ffb21SWarner Losh  *
433*592ffb21SWarner Losh  */
drm_put_dev(struct drm_device * dev)434*592ffb21SWarner Losh void drm_put_dev(struct drm_device *dev)
435*592ffb21SWarner Losh {
436*592ffb21SWarner Losh 	struct drm_driver *driver;
437*592ffb21SWarner Losh 	struct drm_map_list *r_list, *list_temp;
438*592ffb21SWarner Losh 
439*592ffb21SWarner Losh 	DRM_DEBUG("\n");
440*592ffb21SWarner Losh 
441*592ffb21SWarner Losh 	if (!dev) {
442*592ffb21SWarner Losh 		DRM_ERROR("cleanup called no dev\n");
443*592ffb21SWarner Losh 		return;
444*592ffb21SWarner Losh 	}
445*592ffb21SWarner Losh 	driver = dev->driver;
446*592ffb21SWarner Losh 
447*592ffb21SWarner Losh 	drm_lastclose(dev);
448*592ffb21SWarner Losh 
449*592ffb21SWarner Losh 	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
450*592ffb21SWarner Losh 	    dev->agp && dev->agp->agp_mtrr >= 0) {
451*592ffb21SWarner Losh 		int retval;
452*592ffb21SWarner Losh 		retval = drm_mtrr_del(dev->agp->agp_mtrr,
453*592ffb21SWarner Losh 				  dev->agp->agp_info.ai_aperture_base,
454*592ffb21SWarner Losh 				  dev->agp->agp_info.ai_aperture_size,
455*592ffb21SWarner Losh 				  DRM_MTRR_WC);
456*592ffb21SWarner Losh 		DRM_DEBUG("mtrr_del=%d\n", retval);
457*592ffb21SWarner Losh 	}
458*592ffb21SWarner Losh 
459*592ffb21SWarner Losh 	if (drm_core_check_feature(dev, DRIVER_MODESET))
460*592ffb21SWarner Losh 		drm_mode_group_free(&dev->primary->mode_group);
461*592ffb21SWarner Losh 
462*592ffb21SWarner Losh 	if (dev->driver->unload)
463*592ffb21SWarner Losh 		dev->driver->unload(dev);
464*592ffb21SWarner Losh 
465*592ffb21SWarner Losh 	drm_sysctl_cleanup(dev);
466*592ffb21SWarner Losh 
467*592ffb21SWarner Losh 	if (drm_core_has_AGP(dev) && dev->agp) {
468*592ffb21SWarner Losh 		free(dev->agp, DRM_MEM_AGPLISTS);
469*592ffb21SWarner Losh 		dev->agp = NULL;
470*592ffb21SWarner Losh 	}
471*592ffb21SWarner Losh 
472*592ffb21SWarner Losh 	drm_vblank_cleanup(dev);
473*592ffb21SWarner Losh 
474*592ffb21SWarner Losh 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
475*592ffb21SWarner Losh 		drm_rmmap(dev, r_list->map);
476*592ffb21SWarner Losh 	drm_ht_remove(&dev->map_hash);
477*592ffb21SWarner Losh 
478*592ffb21SWarner Losh 	drm_ctxbitmap_cleanup(dev);
479*592ffb21SWarner Losh 
480*592ffb21SWarner Losh 	if (drm_core_check_feature(dev, DRIVER_MODESET))
481*592ffb21SWarner Losh 		drm_put_minor(&dev->control);
482*592ffb21SWarner Losh 
483*592ffb21SWarner Losh 	if (driver->driver_features & DRIVER_GEM)
484*592ffb21SWarner Losh 		drm_gem_destroy(dev);
485*592ffb21SWarner Losh 
486*592ffb21SWarner Losh 	drm_put_minor(&dev->primary);
487*592ffb21SWarner Losh 
488*592ffb21SWarner Losh 	mtx_destroy(&dev->irq_lock);
489*592ffb21SWarner Losh 	mtx_destroy(&dev->count_lock);
490*592ffb21SWarner Losh 	mtx_destroy(&dev->event_lock);
491*592ffb21SWarner Losh 	sx_destroy(&dev->dev_struct_lock);
492*592ffb21SWarner Losh 	mtx_destroy(&dev->ctxlist_mutex);
493*592ffb21SWarner Losh 	mtx_destroy(&dev->pcir_lock);
494*592ffb21SWarner Losh 
495*592ffb21SWarner Losh #ifdef FREEBSD_NOTYET
496*592ffb21SWarner Losh 	list_del(&dev->driver_item);
497*592ffb21SWarner Losh #endif /* FREEBSD_NOTYET */
498*592ffb21SWarner Losh }
499*592ffb21SWarner Losh EXPORT_SYMBOL(drm_put_dev);
500