drm/i915: Upgrade to Linux 4.0
[dragonfly.git] / sys / dev / drm / drm_drv.c
1 /*
2  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3  *
4  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Author Rickard E. (Rik) Faith <faith@valinux.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <drm/drmP.h>
32 #include <drm/drm_core.h>
33 #include "drm_legacy.h"
34 #include "drm_internal.h"
35
36 unsigned int drm_debug = 0;     /* 1 to enable debug output */
37 EXPORT_SYMBOL(drm_debug);
38
39 int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
40
41 unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
42
43 /*
44  * Default to use monotonic timestamps for wait-for-vblank and page-flip
45  * complete events.
46  */
47 unsigned int drm_timestamp_monotonic = 1;
48
49 MODULE_AUTHOR(CORE_AUTHOR);
50 MODULE_DESCRIPTION(CORE_DESC);
51 MODULE_PARM_DESC(debug, "Enable debug output");
52 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
53 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
54 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
55
56 module_param_named(debug, drm_debug, int, 0600);
57 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
58 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
59 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
60
61 #if 0
62 static DEFINE_SPINLOCK(drm_minor_lock);
63 static struct idr drm_minors_idr;
64 #endif
65
66 struct class *drm_class;
67 #if 0
68 static struct dentry *drm_debugfs_root;
69 #endif
70
71 void drm_err(const char *format, ...)
72 {
73 #if 0
74         struct va_format vaf;
75         va_list args;
76         int r;
77
78         va_start(args, format);
79
80         vaf.fmt = format;
81         vaf.va = &args;
82
83         printk(KERN_ERR "[" DRM_NAME ":%pf] *ERROR* %pV",
84                __builtin_return_address(0), &vaf);
85
86         va_end(args);
87
88         return r;
89 #endif
90 }
91 EXPORT_SYMBOL(drm_err);
92
93 void drm_ut_debug_printk(const char *function_name, const char *format, ...)
94 {
95 #if 0
96         struct va_format vaf;
97         va_list args;
98
99         va_start(args, format);
100         vaf.fmt = format;
101         vaf.va = &args;
102
103         printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
104
105         va_end(args);
106 #endif
107 }
108 EXPORT_SYMBOL(drm_ut_debug_printk);
109
110 #if 0
111 struct drm_master *drm_master_create(struct drm_minor *minor)
112 {
113         struct drm_master *master;
114
115         master = kzalloc(sizeof(*master), GFP_KERNEL);
116         if (!master)
117                 return NULL;
118
119         kref_init(&master->refcount);
120         spin_lock_init(&master->lock.spinlock);
121         init_waitqueue_head(&master->lock.lock_queue);
122         if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
123                 kfree(master);
124                 return NULL;
125         }
126         INIT_LIST_HEAD(&master->magicfree);
127         master->minor = minor;
128
129         return master;
130 }
131
132 struct drm_master *drm_master_get(struct drm_master *master)
133 {
134         kref_get(&master->refcount);
135         return master;
136 }
137 EXPORT_SYMBOL(drm_master_get);
138
139 static void drm_master_destroy(struct kref *kref)
140 {
141         struct drm_master *master = container_of(kref, struct drm_master, refcount);
142         struct drm_device *dev = master->minor->dev;
143         struct drm_map_list *r_list, *list_temp;
144
145         mutex_lock(&dev->struct_mutex);
146         if (dev->driver->master_destroy)
147                 dev->driver->master_destroy(dev, master);
148
149         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
150                 if (r_list->master == master) {
151                         drm_legacy_rmmap_locked(dev, r_list->map);
152                         r_list = NULL;
153                 }
154         }
155
156         if (master->unique) {
157                 kfree(master->unique);
158                 master->unique = NULL;
159                 master->unique_len = 0;
160         }
161
162         drm_ht_remove(&master->magiclist);
163
164         mutex_unlock(&dev->struct_mutex);
165         kfree(master);
166 }
167
168 void drm_master_put(struct drm_master **master)
169 {
170         kref_put(&(*master)->refcount, drm_master_destroy);
171         *master = NULL;
172 }
173 EXPORT_SYMBOL(drm_master_put);
174 #endif
175
176 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
177                         struct drm_file *file_priv)
178 {
179         DRM_DEBUG("setmaster\n");
180
181         if (file_priv->master != 0)
182                 return (0);
183
184         return (-EPERM);
185 }
186
187 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
188                          struct drm_file *file_priv)
189 {
190         DRM_DEBUG("dropmaster\n");
191         if (file_priv->master != 0)
192                 return -EINVAL;
193         return 0;
194 }
195
196 #if 0
197 /*
198  * DRM Minors
199  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
200  * of them is represented by a drm_minor object. Depending on the capabilities
201  * of the device-driver, different interfaces are registered.
202  *
203  * Minors can be accessed via dev->$minor_name. This pointer is either
204  * NULL or a valid drm_minor pointer and stays valid as long as the device is
205  * valid. This means, DRM minors have the same life-time as the underlying
206  * device. However, this doesn't mean that the minor is active. Minors are
207  * registered and unregistered dynamically according to device-state.
208  */
209
210 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
211                                              unsigned int type)
212 {
213         switch (type) {
214         case DRM_MINOR_LEGACY:
215                 return &dev->primary;
216         case DRM_MINOR_RENDER:
217                 return &dev->render;
218         case DRM_MINOR_CONTROL:
219                 return &dev->control;
220         default:
221                 return NULL;
222         }
223 }
224
225 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
226 {
227         struct drm_minor *minor;
228         unsigned long flags;
229         int r;
230
231         minor = kzalloc(sizeof(*minor), GFP_KERNEL);
232         if (!minor)
233                 return -ENOMEM;
234
235         minor->type = type;
236         minor->dev = dev;
237
238         idr_preload(GFP_KERNEL);
239         spin_lock_irqsave(&drm_minor_lock, flags);
240         r = idr_alloc(&drm_minors_idr,
241                       NULL,
242                       64 * type,
243                       64 * (type + 1),
244                       GFP_NOWAIT);
245         spin_unlock_irqrestore(&drm_minor_lock, flags);
246         idr_preload_end();
247
248         if (r < 0)
249                 goto err_free;
250
251         minor->index = r;
252
253         minor->kdev = drm_sysfs_minor_alloc(minor);
254         if (IS_ERR(minor->kdev)) {
255                 r = PTR_ERR(minor->kdev);
256                 goto err_index;
257         }
258
259         *drm_minor_get_slot(dev, type) = minor;
260         return 0;
261
262 err_index:
263         spin_lock_irqsave(&drm_minor_lock, flags);
264         idr_remove(&drm_minors_idr, minor->index);
265         spin_unlock_irqrestore(&drm_minor_lock, flags);
266 err_free:
267         kfree(minor);
268         return r;
269 }
270
271 static void drm_minor_free(struct drm_device *dev, unsigned int type)
272 {
273         struct drm_minor **slot, *minor;
274         unsigned long flags;
275
276         slot = drm_minor_get_slot(dev, type);
277         minor = *slot;
278         if (!minor)
279                 return;
280
281         drm_mode_group_destroy(&minor->mode_group);
282         put_device(minor->kdev);
283
284         spin_lock_irqsave(&drm_minor_lock, flags);
285         idr_remove(&drm_minors_idr, minor->index);
286         spin_unlock_irqrestore(&drm_minor_lock, flags);
287
288         kfree(minor);
289         *slot = NULL;
290 }
291
292 static int drm_minor_register(struct drm_device *dev, unsigned int type)
293 {
294         struct drm_minor *minor;
295         unsigned long flags;
296         int ret;
297
298         DRM_DEBUG("\n");
299
300         minor = *drm_minor_get_slot(dev, type);
301         if (!minor)
302                 return 0;
303
304         ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
305         if (ret) {
306                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
307                 return ret;
308         }
309
310         ret = device_add(minor->kdev);
311         if (ret)
312                 goto err_debugfs;
313
314         /* replace NULL with @minor so lookups will succeed from now on */
315         spin_lock_irqsave(&drm_minor_lock, flags);
316         idr_replace(&drm_minors_idr, minor, minor->index);
317         spin_unlock_irqrestore(&drm_minor_lock, flags);
318
319         DRM_DEBUG("new minor registered %d\n", minor->index);
320         return 0;
321
322 err_debugfs:
323         drm_debugfs_cleanup(minor);
324         return ret;
325 }
326
327 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
328 {
329         struct drm_minor *minor;
330         unsigned long flags;
331
332         minor = *drm_minor_get_slot(dev, type);
333         if (!minor || !device_is_registered(minor->kdev))
334                 return;
335
336         /* replace @minor with NULL so lookups will fail from now on */
337         spin_lock_irqsave(&drm_minor_lock, flags);
338         idr_replace(&drm_minors_idr, NULL, minor->index);
339         spin_unlock_irqrestore(&drm_minor_lock, flags);
340
341         device_del(minor->kdev);
342         dev_set_drvdata(minor->kdev, NULL); /* safety belt */
343         drm_debugfs_cleanup(minor);
344 }
345
346 /**
347  * drm_minor_acquire - Acquire a DRM minor
348  * @minor_id: Minor ID of the DRM-minor
349  *
350  * Looks up the given minor-ID and returns the respective DRM-minor object. The
351  * refence-count of the underlying device is increased so you must release this
352  * object with drm_minor_release().
353  *
354  * As long as you hold this minor, it is guaranteed that the object and the
355  * minor->dev pointer will stay valid! However, the device may get unplugged and
356  * unregistered while you hold the minor.
357  *
358  * Returns:
359  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
360  * failure.
361  */
362 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
363 {
364         struct drm_minor *minor;
365         unsigned long flags;
366
367         spin_lock_irqsave(&drm_minor_lock, flags);
368         minor = idr_find(&drm_minors_idr, minor_id);
369         if (minor)
370                 drm_dev_ref(minor->dev);
371         spin_unlock_irqrestore(&drm_minor_lock, flags);
372
373         if (!minor) {
374                 return ERR_PTR(-ENODEV);
375         } else if (drm_device_is_unplugged(minor->dev)) {
376                 drm_dev_unref(minor->dev);
377                 return ERR_PTR(-ENODEV);
378         }
379
380         return minor;
381 }
382
383 /**
384  * drm_minor_release - Release DRM minor
385  * @minor: Pointer to DRM minor object
386  *
387  * Release a minor that was previously acquired via drm_minor_acquire().
388  */
389 void drm_minor_release(struct drm_minor *minor)
390 {
391         drm_dev_unref(minor->dev);
392 }
393
394 /**
395  * drm_put_dev - Unregister and release a DRM device
396  * @dev: DRM device
397  *
398  * Called at module unload time or when a PCI device is unplugged.
399  *
400  * Use of this function is discouraged. It will eventually go away completely.
401  * Please use drm_dev_unregister() and drm_dev_unref() explicitly instead.
402  *
403  * Cleans up all DRM device, calling drm_lastclose().
404  */
405 void drm_put_dev(struct drm_device *dev)
406 {
407         DRM_DEBUG("\n");
408
409         if (!dev) {
410                 DRM_ERROR("cleanup called no dev\n");
411                 return;
412         }
413
414         drm_dev_unregister(dev);
415         drm_dev_unref(dev);
416 }
417 EXPORT_SYMBOL(drm_put_dev);
418
419 void drm_unplug_dev(struct drm_device *dev)
420 {
421         /* for a USB device */
422         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
423         drm_minor_unregister(dev, DRM_MINOR_RENDER);
424         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
425
426         mutex_lock(&drm_global_mutex);
427
428         drm_device_set_unplugged(dev);
429
430         if (dev->open_count == 0) {
431                 drm_put_dev(dev);
432         }
433         mutex_unlock(&drm_global_mutex);
434 }
435 EXPORT_SYMBOL(drm_unplug_dev);
436
437 /*
438  * DRM internal mount
439  * We want to be able to allocate our own "struct address_space" to control
440  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
441  * stand-alone address_space objects, so we need an underlying inode. As there
442  * is no way to allocate an independent inode easily, we need a fake internal
443  * VFS mount-point.
444  *
445  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
446  * frees it again. You are allowed to use iget() and iput() to get references to
447  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
448  * drm_fs_inode_free() call (which does not have to be the last iput()).
449  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
450  * between multiple inode-users. You could, technically, call
451  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
452  * iput(), but this way you'd end up with a new vfsmount for each inode.
453  */
454
455 static int drm_fs_cnt;
456 static struct vfsmount *drm_fs_mnt;
457
458 static const struct dentry_operations drm_fs_dops = {
459         .d_dname        = simple_dname,
460 };
461
462 static const struct super_operations drm_fs_sops = {
463         .statfs         = simple_statfs,
464 };
465
466 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
467                                    const char *dev_name, void *data)
468 {
469         return mount_pseudo(fs_type,
470                             "drm:",
471                             &drm_fs_sops,
472                             &drm_fs_dops,
473                             0x010203ff);
474 }
475
476 static struct file_system_type drm_fs_type = {
477         .name           = "drm",
478         .owner          = THIS_MODULE,
479         .mount          = drm_fs_mount,
480         .kill_sb        = kill_anon_super,
481 };
482
483 static struct inode *drm_fs_inode_new(void)
484 {
485         struct inode *inode;
486         int r;
487
488         r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
489         if (r < 0) {
490                 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
491                 return ERR_PTR(r);
492         }
493
494         inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
495         if (IS_ERR(inode))
496                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
497
498         return inode;
499 }
500
501 static void drm_fs_inode_free(struct inode *inode)
502 {
503         if (inode) {
504                 iput(inode);
505                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
506         }
507 }
508
509 /**
510  * drm_dev_alloc - Allocate new DRM device
511  * @driver: DRM driver to allocate device for
512  * @parent: Parent device object
513  *
514  * Allocate and initialize a new DRM device. No device registration is done.
515  * Call drm_dev_register() to advertice the device to user space and register it
516  * with other core subsystems.
517  *
518  * The initial ref-count of the object is 1. Use drm_dev_ref() and
519  * drm_dev_unref() to take and drop further ref-counts.
520  *
521  * Note that for purely virtual devices @parent can be NULL.
522  *
523  * RETURNS:
524  * Pointer to new DRM device, or NULL if out of memory.
525  */
526 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
527                                  struct device *parent)
528 {
529         struct drm_device *dev;
530         int ret;
531
532         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
533         if (!dev)
534                 return NULL;
535
536         kref_init(&dev->ref);
537         dev->dev = parent;
538         dev->driver = driver;
539
540         INIT_LIST_HEAD(&dev->filelist);
541         INIT_LIST_HEAD(&dev->ctxlist);
542         INIT_LIST_HEAD(&dev->vmalist);
543         INIT_LIST_HEAD(&dev->maplist);
544         INIT_LIST_HEAD(&dev->vblank_event_list);
545
546         spin_lock_init(&dev->buf_lock);
547         spin_lock_init(&dev->event_lock);
548         mutex_init(&dev->struct_mutex);
549         mutex_init(&dev->ctxlist_mutex);
550         mutex_init(&dev->master_mutex);
551
552         dev->anon_inode = drm_fs_inode_new();
553         if (IS_ERR(dev->anon_inode)) {
554                 ret = PTR_ERR(dev->anon_inode);
555                 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
556                 goto err_free;
557         }
558
559         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
560                 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
561                 if (ret)
562                         goto err_minors;
563         }
564
565         if (drm_core_check_feature(dev, DRIVER_RENDER)) {
566                 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
567                 if (ret)
568                         goto err_minors;
569         }
570
571         ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
572         if (ret)
573                 goto err_minors;
574
575         if (drm_ht_create(&dev->map_hash, 12))
576                 goto err_minors;
577
578         ret = drm_legacy_ctxbitmap_init(dev);
579         if (ret) {
580                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
581                 goto err_ht;
582         }
583
584         if (drm_core_check_feature(dev, DRIVER_GEM)) {
585                 ret = drm_gem_init(dev);
586                 if (ret) {
587                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
588                         goto err_ctxbitmap;
589                 }
590         }
591
592         return dev;
593
594 err_ctxbitmap:
595         drm_legacy_ctxbitmap_cleanup(dev);
596 err_ht:
597         drm_ht_remove(&dev->map_hash);
598 err_minors:
599         drm_minor_free(dev, DRM_MINOR_LEGACY);
600         drm_minor_free(dev, DRM_MINOR_RENDER);
601         drm_minor_free(dev, DRM_MINOR_CONTROL);
602         drm_fs_inode_free(dev->anon_inode);
603 err_free:
604         mutex_destroy(&dev->master_mutex);
605         kfree(dev);
606         return NULL;
607 }
608 EXPORT_SYMBOL(drm_dev_alloc);
609
610 static void drm_dev_release(struct kref *ref)
611 {
612         struct drm_device *dev = container_of(ref, struct drm_device, ref);
613
614         if (drm_core_check_feature(dev, DRIVER_GEM))
615                 drm_gem_destroy(dev);
616
617         drm_legacy_ctxbitmap_cleanup(dev);
618         drm_ht_remove(&dev->map_hash);
619         drm_fs_inode_free(dev->anon_inode);
620
621         drm_minor_free(dev, DRM_MINOR_LEGACY);
622         drm_minor_free(dev, DRM_MINOR_RENDER);
623         drm_minor_free(dev, DRM_MINOR_CONTROL);
624
625         mutex_destroy(&dev->master_mutex);
626         kfree(dev->unique);
627         kfree(dev);
628 }
629
630 /**
631  * drm_dev_ref - Take reference of a DRM device
632  * @dev: device to take reference of or NULL
633  *
634  * This increases the ref-count of @dev by one. You *must* already own a
635  * reference when calling this. Use drm_dev_unref() to drop this reference
636  * again.
637  *
638  * This function never fails. However, this function does not provide *any*
639  * guarantee whether the device is alive or running. It only provides a
640  * reference to the object and the memory associated with it.
641  */
642 void drm_dev_ref(struct drm_device *dev)
643 {
644         if (dev)
645                 kref_get(&dev->ref);
646 }
647 EXPORT_SYMBOL(drm_dev_ref);
648
649 /**
650  * drm_dev_unref - Drop reference of a DRM device
651  * @dev: device to drop reference of or NULL
652  *
653  * This decreases the ref-count of @dev by one. The device is destroyed if the
654  * ref-count drops to zero.
655  */
656 void drm_dev_unref(struct drm_device *dev)
657 {
658         if (dev)
659                 kref_put(&dev->ref, drm_dev_release);
660 }
661 EXPORT_SYMBOL(drm_dev_unref);
662
663 /**
664  * drm_dev_register - Register DRM device
665  * @dev: Device to register
666  * @flags: Flags passed to the driver's .load() function
667  *
668  * Register the DRM device @dev with the system, advertise device to user-space
669  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
670  * previously.
671  *
672  * Never call this twice on any device!
673  *
674  * RETURNS:
675  * 0 on success, negative error code on failure.
676  */
677 int drm_dev_register(struct drm_device *dev, unsigned long flags)
678 {
679         int ret;
680
681         mutex_lock(&drm_global_mutex);
682
683         ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
684         if (ret)
685                 goto err_minors;
686
687         ret = drm_minor_register(dev, DRM_MINOR_RENDER);
688         if (ret)
689                 goto err_minors;
690
691         ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
692         if (ret)
693                 goto err_minors;
694
695         if (dev->driver->load) {
696                 ret = dev->driver->load(dev, flags);
697                 if (ret)
698                         goto err_minors;
699         }
700
701         /* setup grouping for legacy outputs */
702         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
703                 ret = drm_mode_group_init_legacy_group(dev,
704                                 &dev->primary->mode_group);
705                 if (ret)
706                         goto err_unload;
707         }
708
709         ret = 0;
710         goto out_unlock;
711
712 err_unload:
713         if (dev->driver->unload)
714                 dev->driver->unload(dev);
715 err_minors:
716         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
717         drm_minor_unregister(dev, DRM_MINOR_RENDER);
718         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
719 out_unlock:
720         mutex_unlock(&drm_global_mutex);
721         return ret;
722 }
723 EXPORT_SYMBOL(drm_dev_register);
724
725 /**
726  * drm_dev_unregister - Unregister DRM device
727  * @dev: Device to unregister
728  *
729  * Unregister the DRM device from the system. This does the reverse of
730  * drm_dev_register() but does not deallocate the device. The caller must call
731  * drm_dev_unref() to drop their final reference.
732  */
733 void drm_dev_unregister(struct drm_device *dev)
734 {
735         struct drm_map_list *r_list, *list_temp;
736
737         drm_lastclose(dev);
738
739         if (dev->driver->unload)
740                 dev->driver->unload(dev);
741
742         if (dev->agp)
743                 drm_pci_agp_destroy(dev);
744
745         drm_vblank_cleanup(dev);
746
747         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
748                 drm_legacy_rmmap(dev, r_list->map);
749
750         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
751         drm_minor_unregister(dev, DRM_MINOR_RENDER);
752         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
753 }
754 EXPORT_SYMBOL(drm_dev_unregister);
755
756 /**
757  * drm_dev_set_unique - Set the unique name of a DRM device
758  * @dev: device of which to set the unique name
759  * @fmt: format string for unique name
760  *
761  * Sets the unique name of a DRM device using the specified format string and
762  * a variable list of arguments. Drivers can use this at driver probe time if
763  * the unique name of the devices they drive is static.
764  *
765  * Return: 0 on success or a negative error code on failure.
766  */
767 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
768 {
769         va_list ap;
770
771         kfree(dev->unique);
772
773         va_start(ap, fmt);
774         dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
775         va_end(ap);
776
777         return dev->unique ? 0 : -ENOMEM;
778 }
779 EXPORT_SYMBOL(drm_dev_set_unique);
780 #endif
781
782 /*
783  * DRM Core
784  * The DRM core module initializes all global DRM objects and makes them
785  * available to drivers. Once setup, drivers can probe their respective
786  * devices.
787  * Currently, core management includes:
788  *  - The "DRM-Global" key/value database
789  *  - Global ID management for connectors
790  *  - DRM major number allocation
791  *  - DRM minor management
792  *  - DRM sysfs class
793  *  - DRM debugfs root
794  *
795  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
796  * interface registered on a DRM device, you can request minor numbers from DRM
797  * core. DRM core takes care of major-number management and char-dev
798  * registration. A stub ->open() callback forwards any open() requests to the
799  * registered minor.
800  */
801
802 #if 0
803 static int drm_stub_open(struct inode *inode, struct file *filp)
804 {
805         const struct file_operations *new_fops;
806         struct drm_minor *minor;
807         int err;
808
809         DRM_DEBUG("\n");
810
811         mutex_lock(&drm_global_mutex);
812         minor = drm_minor_acquire(iminor(inode));
813         if (IS_ERR(minor)) {
814                 err = PTR_ERR(minor);
815                 goto out_unlock;
816         }
817
818         new_fops = fops_get(minor->dev->driver->fops);
819         if (!new_fops) {
820                 err = -ENODEV;
821                 goto out_release;
822         }
823
824         replace_fops(filp, new_fops);
825         if (filp->f_op->open)
826                 err = filp->f_op->open(inode, filp);
827         else
828                 err = 0;
829
830 out_release:
831         drm_minor_release(minor);
832 out_unlock:
833         mutex_unlock(&drm_global_mutex);
834         return err;
835 }
836
837 static const struct file_operations drm_stub_fops = {
838         .owner = THIS_MODULE,
839         .open = drm_stub_open,
840         .llseek = noop_llseek,
841 };
842
843 static int __init drm_core_init(void)
844 {
845         int ret = -ENOMEM;
846
847         drm_global_init();
848         drm_connector_ida_init();
849         idr_init(&drm_minors_idr);
850
851         if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
852                 goto err_p1;
853
854         drm_class = drm_sysfs_create(THIS_MODULE, "drm");
855         if (IS_ERR(drm_class)) {
856                 printk(KERN_ERR "DRM: Error creating drm class.\n");
857                 ret = PTR_ERR(drm_class);
858                 goto err_p2;
859         }
860
861         drm_debugfs_root = debugfs_create_dir("dri", NULL);
862         if (!drm_debugfs_root) {
863                 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
864                 ret = -1;
865                 goto err_p3;
866         }
867
868         DRM_INFO("Initialized %s %d.%d.%d %s\n",
869                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
870         return 0;
871 err_p3:
872         drm_sysfs_destroy();
873 err_p2:
874         unregister_chrdev(DRM_MAJOR, "drm");
875
876         idr_destroy(&drm_minors_idr);
877 err_p1:
878         return ret;
879 }
880
881 static void __exit drm_core_exit(void)
882 {
883         debugfs_remove(drm_debugfs_root);
884         drm_sysfs_destroy();
885
886         unregister_chrdev(DRM_MAJOR, "drm");
887
888         drm_connector_ida_destroy();
889         idr_destroy(&drm_minors_idr);
890 }
891
892 module_init(drm_core_init);
893 module_exit(drm_core_exit);
894 #endif
895
896 #include <sys/devfs.h>
897
898 #include <linux/export.h>
899 #include <linux/dmi.h>
900 #include <drm/drmP.h>
901 #include <drm/drm_core.h>
902
903 #if DRM_DEBUG_DEFAULT_ON == 1
904 #define DRM_DEBUGBITS_ON (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS | \
905     DRM_DEBUGBITS_FAILED_IOCTL)
906 #elif DRM_DEBUG_DEFAULT_ON == 2
907 #define DRM_DEBUGBITS_ON (DRM_DEBUGBITS_DEBUG | DRM_DEBUGBITS_KMS | \
908     DRM_DEBUGBITS_FAILED_IOCTL | DRM_DEBUGBITS_VERBOSE)
909 #else
910 #define DRM_DEBUGBITS_ON (0x0)
911 #endif
912
913 int drm_notyet_flag = 0;
914
915 static int drm_load(struct drm_device *dev);
916 drm_pci_id_list_t *drm_find_description(int vendor, int device,
917     drm_pci_id_list_t *idlist);
918
919 #define DRIVER_SOFTC(unit) \
920         ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
921
922 static int
923 drm_modevent(module_t mod, int type, void *data)
924 {
925
926         switch (type) {
927         case MOD_LOAD:
928                 TUNABLE_INT_FETCH("drm.debug", &drm_debug);
929                 TUNABLE_INT_FETCH("drm.notyet", &drm_notyet_flag);
930                 break;
931         }
932         return (0);
933 }
934
935 static moduledata_t drm_mod = {
936         "drm",
937         drm_modevent,
938         0
939 };
940 DECLARE_MODULE(drm, drm_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
941 MODULE_VERSION(drm, 1);
942 MODULE_DEPEND(drm, agp, 1, 1, 1);
943 MODULE_DEPEND(drm, pci, 1, 1, 1);
944 MODULE_DEPEND(drm, iicbus, 1, 1, 1);
945
946 static struct dev_ops drm_cdevsw = {
947         { "drm", 0, D_TRACKCLOSE | D_MPSAFE },
948         .d_open =       drm_open,
949         .d_close =      drm_close,
950         .d_read =       drm_read,
951         .d_ioctl =      drm_ioctl,
952         .d_kqfilter =   drm_kqfilter,
953         .d_mmap =       drm_mmap,
954         .d_mmap_single = drm_mmap_single,
955 };
956
957 static int drm_msi = 0; /* Disable by default. This is because there are issues with
958                            freezes using MSI and i915 
959                          */
960 TUNABLE_INT("hw.drm.msi.enable", &drm_msi);
961 SYSCTL_NODE(_hw, OID_AUTO, drm, CTLFLAG_RW, NULL, "DRM device");
962 SYSCTL_NODE(_hw_drm, OID_AUTO, msi, CTLFLAG_RW, NULL, "DRM device msi");
963 SYSCTL_INT(_hw_drm_msi, OID_AUTO, enable, CTLFLAG_RD, &drm_msi, 0,
964     "Enable MSI interrupts for drm devices");
965 SYSCTL_INT(_hw_drm, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0,
966     "DRM debugging");
967
968 static struct drm_msi_blacklist_entry drm_msi_blacklist[] = {
969         {0x8086, 0x2772}, /* Intel i945G        */ \
970         {0x8086, 0x27A2}, /* Intel i945GM       */ \
971         {0x8086, 0x27AE}, /* Intel i945GME      */ \
972         {0, 0}
973 };
974
975 static int drm_msi_is_blacklisted(struct drm_device *dev, unsigned long flags)
976 {
977         int i = 0;
978
979         if (dev->driver->use_msi != NULL) {
980                 int use_msi;
981
982                 use_msi = dev->driver->use_msi(dev, flags);
983
984                 return (!use_msi);
985         }
986
987         /* TODO: Maybe move this to a callback in i915? */
988         for (i = 0; drm_msi_blacklist[i].vendor != 0; i++) {
989                 if ((drm_msi_blacklist[i].vendor == dev->pci_vendor) &&
990                     (drm_msi_blacklist[i].device == dev->pci_device)) {
991                         return 1;
992                 }
993         }
994
995         return 0;
996 }
997
998 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist)
999 {
1000         drm_pci_id_list_t *id_entry;
1001         int vendor, device;
1002
1003         vendor = pci_get_vendor(kdev);
1004         device = pci_get_device(kdev);
1005
1006         if (pci_get_class(kdev) != PCIC_DISPLAY)
1007                 return ENXIO;
1008
1009         id_entry = drm_find_description(vendor, device, idlist);
1010         if (id_entry != NULL) {
1011                 if (!device_get_desc(kdev)) {
1012                         DRM_DEBUG("desc : %s\n", device_get_desc(kdev));
1013                         device_set_desc(kdev, id_entry->name);
1014                 }
1015                 return 0;
1016         }
1017
1018         return ENXIO;
1019 }
1020
1021 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist)
1022 {
1023         struct drm_device *dev;
1024         drm_pci_id_list_t *id_entry;
1025         int unit, error;
1026         u_int irq_flags;
1027         int msi_enable;
1028
1029         unit = device_get_unit(kdev);
1030         dev = device_get_softc(kdev);
1031
1032         if (!strcmp(device_get_name(kdev), "drmsub"))
1033                 dev->dev = device_get_parent(kdev);
1034         else
1035                 dev->dev = kdev;
1036
1037         dev->pci_domain = pci_get_domain(dev->dev);
1038         dev->pci_bus = pci_get_bus(dev->dev);
1039         dev->pci_slot = pci_get_slot(dev->dev);
1040         dev->pci_func = pci_get_function(dev->dev);
1041
1042         dev->pci_vendor = pci_get_vendor(dev->dev);
1043         dev->pci_device = pci_get_device(dev->dev);
1044         dev->pci_subvendor = pci_get_subvendor(dev->dev);
1045         dev->pci_subdevice = pci_get_subdevice(dev->dev);
1046
1047         id_entry = drm_find_description(dev->pci_vendor,
1048             dev->pci_device, idlist);
1049         dev->id_entry = id_entry;
1050
1051         if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) {
1052                 msi_enable = drm_msi;
1053
1054                 if (drm_msi_is_blacklisted(dev, dev->id_entry->driver_private)) {
1055                         msi_enable = 0;
1056                 }
1057
1058                 dev->irq_type = pci_alloc_1intr(dev->dev, msi_enable,
1059                     &dev->irqrid, &irq_flags);
1060
1061                 dev->irqr = bus_alloc_resource_any(dev->dev, SYS_RES_IRQ,
1062                     &dev->irqrid, irq_flags);
1063
1064                 if (!dev->irqr) {
1065                         return (ENOENT);
1066                 }
1067
1068                 dev->irq = (int) rman_get_start(dev->irqr);
1069         }
1070
1071         lockinit(&dev->dev_lock, "drmdev", 0, LK_CANRECURSE);
1072         lwkt_serialize_init(&dev->irq_lock);
1073         lockinit(&dev->event_lock, "drmev", 0, LK_CANRECURSE);
1074         lockinit(&dev->struct_mutex, "drmslk", 0, LK_CANRECURSE);
1075
1076         error = drm_load(dev);
1077         if (error)
1078                 goto error;
1079
1080         error = drm_create_cdevs(kdev);
1081         if (error)
1082                 goto error;
1083
1084         return (error);
1085 error:
1086         if (dev->irqr) {
1087                 bus_release_resource(dev->dev, SYS_RES_IRQ,
1088                     dev->irqrid, dev->irqr);
1089         }
1090         if (dev->irq_type == PCI_INTR_TYPE_MSI) {
1091                 pci_release_msi(dev->dev);
1092         }
1093         return (error);
1094 }
1095
1096 int
1097 drm_create_cdevs(device_t kdev)
1098 {
1099         struct drm_device *dev;
1100         int error, unit;
1101
1102         unit = device_get_unit(kdev);
1103         dev = device_get_softc(kdev);
1104
1105         dev->devnode = make_dev(&drm_cdevsw, unit, DRM_DEV_UID, DRM_DEV_GID,
1106                                 DRM_DEV_MODE, "dri/card%d", unit);
1107         error = 0;
1108         if (error == 0)
1109                 dev->devnode->si_drv1 = dev;
1110         return (error);
1111 }
1112
1113 #ifndef DRM_DEV_NAME
1114 #define DRM_DEV_NAME "drm"
1115 #endif
1116
1117 devclass_t drm_devclass;
1118
1119 drm_pci_id_list_t *drm_find_description(int vendor, int device,
1120     drm_pci_id_list_t *idlist)
1121 {
1122         int i = 0;
1123
1124         for (i = 0; idlist[i].vendor != 0; i++) {
1125                 if ((idlist[i].vendor == vendor) &&
1126                     ((idlist[i].device == device) ||
1127                     (idlist[i].device == 0))) {
1128                         return &idlist[i];
1129                 }
1130         }
1131         return NULL;
1132 }
1133
1134 static int drm_load(struct drm_device *dev)
1135 {
1136         int i, retcode;
1137
1138         DRM_DEBUG("\n");
1139
1140         INIT_LIST_HEAD(&dev->maplist);
1141
1142         drm_mem_init();
1143         drm_sysctl_init(dev);
1144         INIT_LIST_HEAD(&dev->filelist);
1145
1146         dev->counters  = 6;
1147         dev->types[0]  = _DRM_STAT_LOCK;
1148         dev->types[1]  = _DRM_STAT_OPENS;
1149         dev->types[2]  = _DRM_STAT_CLOSES;
1150         dev->types[3]  = _DRM_STAT_IOCTLS;
1151         dev->types[4]  = _DRM_STAT_LOCKS;
1152         dev->types[5]  = _DRM_STAT_UNLOCKS;
1153
1154         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
1155                 atomic_set(&dev->counts[i], 0);
1156
1157         INIT_LIST_HEAD(&dev->vblank_event_list);
1158
1159         if (drm_core_has_AGP(dev)) {
1160                 if (drm_device_is_agp(dev))
1161                         dev->agp = drm_agp_init();
1162                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP) &&
1163                     dev->agp == NULL) {
1164                         DRM_ERROR("Card isn't AGP, or couldn't initialize "
1165                             "AGP.\n");
1166                         retcode = ENOMEM;
1167                         goto error;
1168                 }
1169                 if (dev->agp != NULL && dev->agp->agp_info.ai_aperture_base != 0) {
1170                         if (drm_mtrr_add(dev->agp->agp_info.ai_aperture_base,
1171                             dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC) == 0)
1172                                 dev->agp->agp_mtrr = 1;
1173                 }
1174         }
1175
1176         if (dev->driver->driver_features & DRIVER_GEM) {
1177                 retcode = drm_gem_init(dev);
1178                 if (retcode != 0) {
1179                         DRM_ERROR("Cannot initialize graphics execution "
1180                                   "manager (GEM)\n");
1181                         goto error1;
1182                 }
1183         }
1184
1185         if (dev->driver->load != NULL) {
1186                 DRM_LOCK(dev);
1187                 /* Shared code returns -errno. */
1188                 retcode = -dev->driver->load(dev,
1189                     dev->id_entry->driver_private);
1190                 if (pci_enable_busmaster(dev->dev))
1191                         DRM_ERROR("Request to enable bus-master failed.\n");
1192                 DRM_UNLOCK(dev);
1193                 if (retcode != 0)
1194                         goto error1;
1195         }
1196
1197         DRM_INFO("Initialized %s %d.%d.%d %s\n",
1198             dev->driver->name,
1199             dev->driver->major,
1200             dev->driver->minor,
1201             dev->driver->patchlevel,
1202             dev->driver->date);
1203
1204         return 0;
1205
1206 error1:
1207         drm_gem_destroy(dev);
1208 error:
1209         drm_sysctl_cleanup(dev);
1210         DRM_LOCK(dev);
1211         drm_lastclose(dev);
1212         DRM_UNLOCK(dev);
1213         if (dev->devnode != NULL)
1214                 destroy_dev(dev->devnode);
1215
1216         lockuninit(&dev->vbl_lock);
1217         lockuninit(&dev->dev_lock);
1218         lockuninit(&dev->event_lock);
1219         lockuninit(&dev->struct_mutex);
1220
1221         return retcode;
1222 }
1223
1224 /*
1225  * Stub is needed for devfs
1226  */
1227 int drm_close(struct dev_close_args *ap)
1228 {
1229         return 0;
1230 }
1231
1232 void drm_cdevpriv_dtor(void *cd)
1233 {
1234         struct drm_file *file_priv = cd;
1235         struct drm_device *dev = file_priv->dev;
1236         int retcode = 0;
1237
1238         DRM_DEBUG("open_count = %d\n", dev->open_count);
1239
1240         DRM_LOCK(dev);
1241
1242         if (dev->driver->preclose != NULL)
1243                 dev->driver->preclose(dev, file_priv);
1244
1245         /* ========================================================
1246          * Begin inline drm_release
1247          */
1248
1249         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
1250             DRM_CURRENTPID, (long)dev->dev, dev->open_count);
1251
1252         if (dev->driver->driver_features & DRIVER_GEM)
1253                 drm_gem_release(dev, file_priv);
1254
1255         if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
1256             && dev->lock.file_priv == file_priv) {
1257                 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
1258                           DRM_CURRENTPID,
1259                           _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
1260                 if (dev->driver->reclaim_buffers_locked != NULL)
1261                         dev->driver->reclaim_buffers_locked(dev, file_priv);
1262
1263                 drm_lock_free(&dev->lock,
1264                     _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
1265
1266                                 /* FIXME: may require heavy-handed reset of
1267                                    hardware at this point, possibly
1268                                    processed via a callback to the X
1269                                    server. */
1270         } else if (dev->driver->reclaim_buffers_locked != NULL &&
1271             dev->lock.hw_lock != NULL) {
1272                 /* The lock is required to reclaim buffers */
1273                 for (;;) {
1274                         if (!dev->lock.hw_lock) {
1275                                 /* Device has been unregistered */
1276                                 retcode = EINTR;
1277                                 break;
1278                         }
1279                         /* Contention */
1280                         retcode = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue,
1281                             PCATCH, "drmlk2", 0);
1282                         if (retcode)
1283                                 break;
1284                 }
1285                 if (retcode == 0) {
1286                         dev->driver->reclaim_buffers_locked(dev, file_priv);
1287                 }
1288         }
1289
1290         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
1291             !dev->driver->reclaim_buffers_locked)
1292                 drm_legacy_reclaim_buffers(dev, file_priv);
1293
1294         funsetown(&dev->buf_sigio);
1295
1296         if (dev->driver->postclose != NULL)
1297                 dev->driver->postclose(dev, file_priv);
1298         list_del(&file_priv->lhead);
1299
1300
1301         /* ========================================================
1302          * End inline drm_release
1303          */
1304
1305         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
1306         device_unbusy(dev->dev);
1307         if (--dev->open_count == 0) {
1308                 retcode = drm_lastclose(dev);
1309         }
1310
1311         DRM_UNLOCK(dev);
1312 }
1313
1314 int
1315 drm_add_busid_modesetting(struct drm_device *dev, struct sysctl_ctx_list *ctx,
1316     struct sysctl_oid *top)
1317 {
1318         struct sysctl_oid *oid;
1319
1320         ksnprintf(dev->busid_str, sizeof(dev->busid_str),
1321              "pci:%04x:%02x:%02x.%d", dev->pci_domain, dev->pci_bus,
1322              dev->pci_slot, dev->pci_func);
1323         oid = SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(top), OID_AUTO, "busid",
1324             CTLFLAG_RD, dev->busid_str, 0, NULL);
1325         if (oid == NULL)
1326                 return (ENOMEM);
1327         dev->modesetting = (dev->driver->driver_features & DRIVER_MODESET) != 0;
1328         oid = SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(top), OID_AUTO,
1329             "modesetting", CTLFLAG_RD, &dev->modesetting, 0, NULL);
1330         if (oid == NULL)
1331                 return (ENOMEM);
1332
1333         return (0);
1334 }
1335
1336 int
1337 drm_mmap_single(struct dev_mmap_single_args *ap)
1338 {
1339         struct drm_device *dev;
1340         struct cdev *kdev = ap->a_head.a_dev;
1341         vm_ooffset_t *offset = ap->a_offset;
1342         vm_size_t size = ap->a_size;
1343         struct vm_object **obj_res = ap->a_object;
1344         int nprot = ap->a_nprot;
1345
1346         dev = drm_get_device_from_kdev(kdev);
1347         if (dev->drm_ttm_bdev != NULL) {
1348                 return (ttm_bo_mmap_single(dev->drm_ttm_bdev, offset, size,
1349                     obj_res, nprot));
1350         } else if ((dev->driver->driver_features & DRIVER_GEM) != 0) {
1351                 return (drm_gem_mmap_single(dev, offset, size, obj_res, nprot));
1352         } else {
1353                 return (ENODEV);
1354         }
1355 }
1356
1357 #if DRM_LINUX
1358
1359 #include <sys/sysproto.h>
1360
1361 MODULE_DEPEND(DRIVER_NAME, linux, 1, 1, 1);
1362
1363 #define LINUX_IOCTL_DRM_MIN             0x6400
1364 #define LINUX_IOCTL_DRM_MAX             0x64ff
1365
1366 static linux_ioctl_function_t drm_linux_ioctl;
1367 static struct linux_ioctl_handler drm_handler = {drm_linux_ioctl,
1368     LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX};
1369
1370 /* The bits for in/out are switched on Linux */
1371 #define LINUX_IOC_IN    IOC_OUT
1372 #define LINUX_IOC_OUT   IOC_IN
1373
1374 static int
1375 drm_linux_ioctl(DRM_STRUCTPROC *p, struct linux_ioctl_args* args)
1376 {
1377         int error;
1378         int cmd = args->cmd;
1379
1380         args->cmd &= ~(LINUX_IOC_IN | LINUX_IOC_OUT);
1381         if (cmd & LINUX_IOC_IN)
1382                 args->cmd |= IOC_IN;
1383         if (cmd & LINUX_IOC_OUT)
1384                 args->cmd |= IOC_OUT;
1385         
1386         error = ioctl(p, (struct ioctl_args *)args);
1387
1388         return error;
1389 }
1390 #endif /* DRM_LINUX */
1391
1392 static int
1393 drm_core_init(void *arg)
1394 {
1395
1396         drm_global_init();
1397
1398 #if DRM_LINUX
1399         linux_ioctl_register_handler(&drm_handler);
1400 #endif /* DRM_LINUX */
1401
1402         DRM_INFO("Initialized %s %d.%d.%d %s\n",
1403                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
1404         return 0;
1405 }
1406
1407 static void
1408 drm_core_exit(void *arg)
1409 {
1410
1411 #if DRM_LINUX
1412         linux_ioctl_unregister_handler(&drm_handler);
1413 #endif /* DRM_LINUX */
1414
1415         drm_global_release();
1416 }
1417
1418 SYSINIT(drm_register, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1419     drm_core_init, NULL);
1420 SYSUNINIT(drm_unregister, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1421     drm_core_exit, NULL);
1422
1423
1424 #include <linux/dmi.h>
1425
1426 /*
1427  * Check if dmi_system_id structure matches system DMI data
1428  */
1429 static bool
1430 dmi_found(const struct dmi_system_id *dsi)
1431 {
1432         int i, slot;
1433         bool found = false;
1434         char *sys_vendor, *board_vendor, *product_name, *board_name;
1435
1436         sys_vendor = kgetenv("smbios.system.maker");
1437         board_vendor = kgetenv("smbios.planar.maker");
1438         product_name = kgetenv("smbios.system.product");
1439         board_name = kgetenv("smbios.planar.product");
1440
1441         for (i = 0; i < NELEM(dsi->matches); i++) {
1442                 slot = dsi->matches[i].slot;
1443                 switch (slot) {
1444                 case DMI_NONE:
1445                         break;
1446                 case DMI_SYS_VENDOR:
1447                         if (sys_vendor != NULL &&
1448                             !strcmp(sys_vendor, dsi->matches[i].substr))
1449                                 break;
1450                         else
1451                                 goto done;
1452                 case DMI_BOARD_VENDOR:
1453                         if (board_vendor != NULL &&
1454                             !strcmp(board_vendor, dsi->matches[i].substr))
1455                                 break;
1456                         else
1457                                 goto done;
1458                 case DMI_PRODUCT_NAME:
1459                         if (product_name != NULL &&
1460                             !strcmp(product_name, dsi->matches[i].substr))
1461                                 break;
1462                         else
1463                                 goto done;
1464                 case DMI_BOARD_NAME:
1465                         if (board_name != NULL &&
1466                             !strcmp(board_name, dsi->matches[i].substr))
1467                                 break;
1468                         else
1469                                 goto done;
1470                 default:
1471                         goto done;
1472                 }
1473         }
1474         found = true;
1475
1476 done:
1477         if (sys_vendor != NULL)
1478                 kfreeenv(sys_vendor);
1479         if (board_vendor != NULL)
1480                 kfreeenv(board_vendor);
1481         if (product_name != NULL)
1482                 kfreeenv(product_name);
1483         if (board_name != NULL)
1484                 kfreeenv(board_name);
1485
1486         return found;
1487 }
1488
1489 int dmi_check_system(const struct dmi_system_id *sysid)
1490 {
1491         const struct dmi_system_id *dsi;
1492         int num = 0;
1493
1494         for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
1495                 if (dmi_found(dsi)) {
1496                         num++;
1497                         if (dsi->callback && dsi->callback(dsi))
1498                                 break;
1499                 }
1500         }
1501         return (num);
1502 }