5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
11 * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
39 unsigned int drm_debug
= 0; /* 1 to enable debug output */
40 EXPORT_SYMBOL(drm_debug
);
42 MODULE_AUTHOR(CORE_AUTHOR
);
43 MODULE_DESCRIPTION(CORE_DESC
);
44 MODULE_LICENSE("GPL and additional rights");
45 MODULE_PARM_DESC(debug
, "Enable debug output");
47 module_param_named(debug
, drm_debug
, int, 0600);
49 struct idr drm_minors_idr
;
51 struct class *drm_class
;
52 struct proc_dir_entry
*drm_proc_root
;
53 struct dentry
*drm_debugfs_root
;
54 void drm_ut_debug_printk(unsigned int request_level
,
56 const char *function_name
,
57 const char *format
, ...)
61 if (drm_debug
& request_level
) {
63 printk(KERN_DEBUG
"[%s:%s], ", prefix
, function_name
);
64 va_start(args
, format
);
65 vprintk(format
, args
);
69 EXPORT_SYMBOL(drm_ut_debug_printk
);
70 static int drm_minor_get_id(struct drm_device
*dev
, int type
)
74 int base
= 0, limit
= 63;
76 if (type
== DRM_MINOR_CONTROL
) {
79 } else if (type
== DRM_MINOR_RENDER
) {
85 if (idr_pre_get(&drm_minors_idr
, GFP_KERNEL
) == 0) {
86 DRM_ERROR("Out of memory expanding drawable idr\n");
89 mutex_lock(&dev
->struct_mutex
);
90 ret
= idr_get_new_above(&drm_minors_idr
, NULL
,
92 mutex_unlock(&dev
->struct_mutex
);
99 if (new_id
>= limit
) {
100 idr_remove(&drm_minors_idr
, new_id
);
106 struct drm_master
*drm_master_create(struct drm_minor
*minor
)
108 struct drm_master
*master
;
110 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
114 kref_init(&master
->refcount
);
115 spin_lock_init(&master
->lock
.spinlock
);
116 init_waitqueue_head(&master
->lock
.lock_queue
);
117 drm_ht_create(&master
->magiclist
, DRM_MAGIC_HASH_ORDER
);
118 INIT_LIST_HEAD(&master
->magicfree
);
119 master
->minor
= minor
;
121 list_add_tail(&master
->head
, &minor
->master_list
);
126 struct drm_master
*drm_master_get(struct drm_master
*master
)
128 kref_get(&master
->refcount
);
131 EXPORT_SYMBOL(drm_master_get
);
133 static void drm_master_destroy(struct kref
*kref
)
135 struct drm_master
*master
= container_of(kref
, struct drm_master
, refcount
);
136 struct drm_magic_entry
*pt
, *next
;
137 struct drm_device
*dev
= master
->minor
->dev
;
138 struct drm_map_list
*r_list
, *list_temp
;
140 list_del(&master
->head
);
142 if (dev
->driver
->master_destroy
)
143 dev
->driver
->master_destroy(dev
, master
);
145 list_for_each_entry_safe(r_list
, list_temp
, &dev
->maplist
, head
) {
146 if (r_list
->master
== master
) {
147 drm_rmmap_locked(dev
, r_list
->map
);
152 if (master
->unique
) {
153 kfree(master
->unique
);
154 master
->unique
= NULL
;
155 master
->unique_len
= 0;
158 list_for_each_entry_safe(pt
, next
, &master
->magicfree
, head
) {
160 drm_ht_remove_item(&master
->magiclist
, &pt
->hash_item
);
164 drm_ht_remove(&master
->magiclist
);
169 void drm_master_put(struct drm_master
**master
)
171 kref_put(&(*master
)->refcount
, drm_master_destroy
);
174 EXPORT_SYMBOL(drm_master_put
);
176 int drm_setmaster_ioctl(struct drm_device
*dev
, void *data
,
177 struct drm_file
*file_priv
)
181 if (file_priv
->is_master
)
184 if (file_priv
->minor
->master
&& file_priv
->minor
->master
!= file_priv
->master
)
187 if (!file_priv
->master
)
190 if (!file_priv
->minor
->master
&&
191 file_priv
->minor
->master
!= file_priv
->master
) {
192 mutex_lock(&dev
->struct_mutex
);
193 file_priv
->minor
->master
= drm_master_get(file_priv
->master
);
194 file_priv
->is_master
= 1;
195 if (dev
->driver
->master_set
) {
196 ret
= dev
->driver
->master_set(dev
, file_priv
, false);
197 if (unlikely(ret
!= 0)) {
198 file_priv
->is_master
= 0;
199 drm_master_put(&file_priv
->minor
->master
);
202 mutex_unlock(&dev
->struct_mutex
);
208 int drm_dropmaster_ioctl(struct drm_device
*dev
, void *data
,
209 struct drm_file
*file_priv
)
211 if (!file_priv
->is_master
)
214 if (!file_priv
->minor
->master
)
217 mutex_lock(&dev
->struct_mutex
);
218 if (dev
->driver
->master_drop
)
219 dev
->driver
->master_drop(dev
, file_priv
, false);
220 drm_master_put(&file_priv
->minor
->master
);
221 file_priv
->is_master
= 0;
222 mutex_unlock(&dev
->struct_mutex
);
226 static int drm_fill_in_dev(struct drm_device
* dev
, struct pci_dev
*pdev
,
227 const struct pci_device_id
*ent
,
228 struct drm_driver
*driver
)
232 INIT_LIST_HEAD(&dev
->filelist
);
233 INIT_LIST_HEAD(&dev
->ctxlist
);
234 INIT_LIST_HEAD(&dev
->vmalist
);
235 INIT_LIST_HEAD(&dev
->maplist
);
236 INIT_LIST_HEAD(&dev
->vblank_event_list
);
238 spin_lock_init(&dev
->count_lock
);
239 spin_lock_init(&dev
->drw_lock
);
240 spin_lock_init(&dev
->event_lock
);
241 init_timer(&dev
->timer
);
242 mutex_init(&dev
->struct_mutex
);
243 mutex_init(&dev
->ctxlist_mutex
);
245 idr_init(&dev
->drw_idr
);
248 dev
->pci_device
= pdev
->device
;
249 dev
->pci_vendor
= pdev
->vendor
;
252 dev
->hose
= pdev
->sysdata
;
255 if (drm_ht_create(&dev
->map_hash
, 12)) {
259 /* the DRM has 6 basic counters */
261 dev
->types
[0] = _DRM_STAT_LOCK
;
262 dev
->types
[1] = _DRM_STAT_OPENS
;
263 dev
->types
[2] = _DRM_STAT_CLOSES
;
264 dev
->types
[3] = _DRM_STAT_IOCTLS
;
265 dev
->types
[4] = _DRM_STAT_LOCKS
;
266 dev
->types
[5] = _DRM_STAT_UNLOCKS
;
268 dev
->driver
= driver
;
270 if (drm_core_has_AGP(dev
)) {
271 if (drm_device_is_agp(dev
))
272 dev
->agp
= drm_agp_init(dev
);
273 if (drm_core_check_feature(dev
, DRIVER_REQUIRE_AGP
)
274 && (dev
->agp
== NULL
)) {
275 DRM_ERROR("Cannot initialize the agpgart module.\n");
277 goto error_out_unreg
;
279 if (drm_core_has_MTRR(dev
)) {
282 mtrr_add(dev
->agp
->agp_info
.aper_base
,
283 dev
->agp
->agp_info
.aper_size
*
284 1024 * 1024, MTRR_TYPE_WRCOMB
, 1);
289 retcode
= drm_ctxbitmap_init(dev
);
291 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
292 goto error_out_unreg
;
295 if (driver
->driver_features
& DRIVER_GEM
) {
296 retcode
= drm_gem_init(dev
);
298 DRM_ERROR("Cannot initialize graphics execution "
300 goto error_out_unreg
;
313 * Get a secondary minor number.
315 * \param dev device data structure
316 * \param sec-minor structure to hold the assigned minor
317 * \return negative number on failure.
319 * Search an empty entry and initialize it to the given parameters, and
320 * create the proc init entry via proc_init(). This routines assigns
321 * minor numbers to secondary heads of multi-headed cards
323 static int drm_get_minor(struct drm_device
*dev
, struct drm_minor
**minor
, int type
)
325 struct drm_minor
*new_minor
;
331 minor_id
= drm_minor_get_id(dev
, type
);
335 new_minor
= kzalloc(sizeof(struct drm_minor
), GFP_KERNEL
);
341 new_minor
->type
= type
;
342 new_minor
->device
= MKDEV(DRM_MAJOR
, minor_id
);
343 new_minor
->dev
= dev
;
344 new_minor
->index
= minor_id
;
345 INIT_LIST_HEAD(&new_minor
->master_list
);
347 idr_replace(&drm_minors_idr
, new_minor
, minor_id
);
349 if (type
== DRM_MINOR_LEGACY
) {
350 ret
= drm_proc_init(new_minor
, minor_id
, drm_proc_root
);
352 DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
356 new_minor
->proc_root
= NULL
;
358 #if defined(CONFIG_DEBUG_FS)
359 ret
= drm_debugfs_init(new_minor
, minor_id
, drm_debugfs_root
);
361 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
366 ret
= drm_sysfs_device_add(new_minor
);
369 "DRM: Error sysfs_device_add.\n");
374 DRM_DEBUG("new minor assigned %d\n", minor_id
);
379 if (new_minor
->type
== DRM_MINOR_LEGACY
)
380 drm_proc_cleanup(new_minor
, drm_proc_root
);
384 idr_remove(&drm_minors_idr
, minor_id
);
392 * \param pdev - PCI device structure
393 * \param ent entry from the PCI ID table with device type flags
394 * \return zero on success or a negative number on failure.
396 * Attempt to gets inter module "drm" information. If we are first
397 * then register the character device and inter module information.
398 * Try and register, if we fail to register, backout previous work.
400 int drm_get_dev(struct pci_dev
*pdev
, const struct pci_device_id
*ent
,
401 struct drm_driver
*driver
)
403 struct drm_device
*dev
;
408 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
412 ret
= pci_enable_device(pdev
);
416 pci_set_master(pdev
);
417 if ((ret
= drm_fill_in_dev(dev
, pdev
, ent
, driver
))) {
418 printk(KERN_ERR
"DRM: Fill_in_dev failed.\n");
422 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
423 pci_set_drvdata(pdev
, dev
);
424 ret
= drm_get_minor(dev
, &dev
->control
, DRM_MINOR_CONTROL
);
429 if ((ret
= drm_get_minor(dev
, &dev
->primary
, DRM_MINOR_LEGACY
)))
432 if (dev
->driver
->load
) {
433 ret
= dev
->driver
->load(dev
, ent
->driver_data
);
438 /* setup the grouping for the legacy output */
439 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
440 ret
= drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
445 list_add_tail(&dev
->driver_item
, &driver
->device_list
);
447 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
448 driver
->name
, driver
->major
, driver
->minor
, driver
->patchlevel
,
449 driver
->date
, pci_name(pdev
), dev
->primary
->index
);
454 drm_put_minor(&dev
->primary
);
456 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
457 drm_put_minor(&dev
->control
);
459 pci_disable_device(pdev
);
464 EXPORT_SYMBOL(drm_get_dev
);
467 * Put a secondary minor number.
469 * \param sec_minor - structure to be released
470 * \return always zero
472 * Cleans up the proc resources. Not legal for this to be the
473 * last minor released.
476 int drm_put_minor(struct drm_minor
**minor_p
)
478 struct drm_minor
*minor
= *minor_p
;
480 DRM_DEBUG("release secondary minor %d\n", minor
->index
);
482 if (minor
->type
== DRM_MINOR_LEGACY
)
483 drm_proc_cleanup(minor
, drm_proc_root
);
484 #if defined(CONFIG_DEBUG_FS)
485 drm_debugfs_cleanup(minor
);
488 drm_sysfs_device_remove(minor
);
490 idr_remove(&drm_minors_idr
, minor
->index
);
498 * Called via drm_exit() at module unload time or when pci device is
501 * Cleans up all DRM device, calling drm_lastclose().
505 void drm_put_dev(struct drm_device
*dev
)
507 struct drm_driver
*driver
;
508 struct drm_map_list
*r_list
, *list_temp
;
513 DRM_ERROR("cleanup called no dev\n");
516 driver
= dev
->driver
;
518 drm_vblank_cleanup(dev
);
522 if (drm_core_has_MTRR(dev
) && drm_core_has_AGP(dev
) &&
523 dev
->agp
&& dev
->agp
->agp_mtrr
>= 0) {
525 retval
= mtrr_del(dev
->agp
->agp_mtrr
,
526 dev
->agp
->agp_info
.aper_base
,
527 dev
->agp
->agp_info
.aper_size
* 1024 * 1024);
528 DRM_DEBUG("mtrr_del=%d\n", retval
);
531 if (dev
->driver
->unload
)
532 dev
->driver
->unload(dev
);
534 if (drm_core_has_AGP(dev
) && dev
->agp
) {
539 list_for_each_entry_safe(r_list
, list_temp
, &dev
->maplist
, head
)
540 drm_rmmap(dev
, r_list
->map
);
541 drm_ht_remove(&dev
->map_hash
);
543 drm_ctxbitmap_cleanup(dev
);
545 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
546 drm_put_minor(&dev
->control
);
548 if (driver
->driver_features
& DRIVER_GEM
)
549 drm_gem_destroy(dev
);
551 drm_put_minor(&dev
->primary
);
559 EXPORT_SYMBOL(drm_put_dev
);