drm: set/clear is_master when master changed
[safe/jmp/linux-2.6] / drivers / gpu / drm / drm_stub.c
1 /**
2  * \file drm_stub.h
3  * Stub support
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  */
7
8 /*
9  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
10  *
11  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12  * All Rights Reserved.
13  *
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:
20  *
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
23  * Software.
24  *
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.
32  */
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include "drmP.h"
37 #include "drm_core.h"
38
39 unsigned int drm_debug = 0;     /* 1 to enable debug output */
40 EXPORT_SYMBOL(drm_debug);
41
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");
46
47 module_param_named(debug, drm_debug, int, 0600);
48
49 struct idr drm_minors_idr;
50
51 struct class *drm_class;
52 struct proc_dir_entry *drm_proc_root;
53 struct dentry *drm_debugfs_root;
54
55 static int drm_minor_get_id(struct drm_device *dev, int type)
56 {
57         int new_id;
58         int ret;
59         int base = 0, limit = 63;
60
61         if (type == DRM_MINOR_CONTROL) {
62                 base += 64;
63                 limit = base + 127;
64         } else if (type == DRM_MINOR_RENDER) {
65                 base += 128;
66                 limit = base + 255;
67         }
68
69 again:
70         if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
71                 DRM_ERROR("Out of memory expanding drawable idr\n");
72                 return -ENOMEM;
73         }
74         mutex_lock(&dev->struct_mutex);
75         ret = idr_get_new_above(&drm_minors_idr, NULL,
76                                 base, &new_id);
77         mutex_unlock(&dev->struct_mutex);
78         if (ret == -EAGAIN) {
79                 goto again;
80         } else if (ret) {
81                 return ret;
82         }
83
84         if (new_id >= limit) {
85                 idr_remove(&drm_minors_idr, new_id);
86                 return -EINVAL;
87         }
88         return new_id;
89 }
90
91 struct drm_master *drm_master_create(struct drm_minor *minor)
92 {
93         struct drm_master *master;
94
95         master = drm_calloc(1, sizeof(*master), DRM_MEM_DRIVER);
96         if (!master)
97                 return NULL;
98
99         kref_init(&master->refcount);
100         spin_lock_init(&master->lock.spinlock);
101         init_waitqueue_head(&master->lock.lock_queue);
102         drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
103         INIT_LIST_HEAD(&master->magicfree);
104         master->minor = minor;
105
106         list_add_tail(&master->head, &minor->master_list);
107
108         return master;
109 }
110
111 struct drm_master *drm_master_get(struct drm_master *master)
112 {
113         kref_get(&master->refcount);
114         return master;
115 }
116
117 static void drm_master_destroy(struct kref *kref)
118 {
119         struct drm_master *master = container_of(kref, struct drm_master, refcount);
120         struct drm_magic_entry *pt, *next;
121         struct drm_device *dev = master->minor->dev;
122         struct drm_map_list *r_list, *list_temp;
123
124         list_del(&master->head);
125
126         if (dev->driver->master_destroy)
127                 dev->driver->master_destroy(dev, master);
128
129         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
130                 if (r_list->master == master) {
131                         drm_rmmap_locked(dev, r_list->map);
132                         r_list = NULL;
133                 }
134         }
135
136         if (master->unique) {
137                 drm_free(master->unique, master->unique_size, DRM_MEM_DRIVER);
138                 master->unique = NULL;
139                 master->unique_len = 0;
140         }
141
142         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
143                 list_del(&pt->head);
144                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
145                 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
146         }
147
148         drm_ht_remove(&master->magiclist);
149
150         drm_free(master, sizeof(*master), DRM_MEM_DRIVER);
151 }
152
153 void drm_master_put(struct drm_master **master)
154 {
155         kref_put(&(*master)->refcount, drm_master_destroy);
156         *master = NULL;
157 }
158
159 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
160                         struct drm_file *file_priv)
161 {
162         if (file_priv->is_master)
163                 return 0;
164
165         if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
166                 return -EINVAL;
167
168         if (!file_priv->master)
169                 return -EINVAL;
170
171         if (!file_priv->minor->master &&
172             file_priv->minor->master != file_priv->master) {
173                 mutex_lock(&dev->struct_mutex);
174                 file_priv->minor->master = drm_master_get(file_priv->master);
175                 file_priv->is_master = 1;
176                 mutex_unlock(&dev->struct_mutex);
177         }
178
179         return 0;
180 }
181
182 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
183                          struct drm_file *file_priv)
184 {
185         if (!file_priv->is_master)
186                 return -EINVAL;
187
188         mutex_lock(&dev->struct_mutex);
189         drm_master_put(&file_priv->minor->master);
190         file_priv->is_master = 0;
191         mutex_unlock(&dev->struct_mutex);
192         return 0;
193 }
194
195 static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
196                            const struct pci_device_id *ent,
197                            struct drm_driver *driver)
198 {
199         int retcode;
200
201         INIT_LIST_HEAD(&dev->filelist);
202         INIT_LIST_HEAD(&dev->ctxlist);
203         INIT_LIST_HEAD(&dev->vmalist);
204         INIT_LIST_HEAD(&dev->maplist);
205
206         spin_lock_init(&dev->count_lock);
207         spin_lock_init(&dev->drw_lock);
208         init_timer(&dev->timer);
209         mutex_init(&dev->struct_mutex);
210         mutex_init(&dev->ctxlist_mutex);
211
212         idr_init(&dev->drw_idr);
213
214         dev->pdev = pdev;
215         dev->pci_device = pdev->device;
216         dev->pci_vendor = pdev->vendor;
217
218 #ifdef __alpha__
219         dev->hose = pdev->sysdata;
220 #endif
221
222         if (drm_ht_create(&dev->map_hash, 12)) {
223                 return -ENOMEM;
224         }
225
226         /* the DRM has 6 basic counters */
227         dev->counters = 6;
228         dev->types[0] = _DRM_STAT_LOCK;
229         dev->types[1] = _DRM_STAT_OPENS;
230         dev->types[2] = _DRM_STAT_CLOSES;
231         dev->types[3] = _DRM_STAT_IOCTLS;
232         dev->types[4] = _DRM_STAT_LOCKS;
233         dev->types[5] = _DRM_STAT_UNLOCKS;
234
235         dev->driver = driver;
236
237         if (drm_core_has_AGP(dev)) {
238                 if (drm_device_is_agp(dev))
239                         dev->agp = drm_agp_init(dev);
240                 if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
241                     && (dev->agp == NULL)) {
242                         DRM_ERROR("Cannot initialize the agpgart module.\n");
243                         retcode = -EINVAL;
244                         goto error_out_unreg;
245                 }
246                 if (drm_core_has_MTRR(dev)) {
247                         if (dev->agp)
248                                 dev->agp->agp_mtrr =
249                                     mtrr_add(dev->agp->agp_info.aper_base,
250                                              dev->agp->agp_info.aper_size *
251                                              1024 * 1024, MTRR_TYPE_WRCOMB, 1);
252                 }
253         }
254
255
256         retcode = drm_ctxbitmap_init(dev);
257         if (retcode) {
258                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
259                 goto error_out_unreg;
260         }
261
262         if (driver->driver_features & DRIVER_GEM) {
263                 retcode = drm_gem_init(dev);
264                 if (retcode) {
265                         DRM_ERROR("Cannot initialize graphics execution "
266                                   "manager (GEM)\n");
267                         goto error_out_unreg;
268                 }
269         }
270
271         return 0;
272
273       error_out_unreg:
274         drm_lastclose(dev);
275         return retcode;
276 }
277
278
279 /**
280  * Get a secondary minor number.
281  *
282  * \param dev device data structure
283  * \param sec-minor structure to hold the assigned minor
284  * \return negative number on failure.
285  *
286  * Search an empty entry and initialize it to the given parameters, and
287  * create the proc init entry via proc_init(). This routines assigns
288  * minor numbers to secondary heads of multi-headed cards
289  */
290 static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
291 {
292         struct drm_minor *new_minor;
293         int ret;
294         int minor_id;
295
296         DRM_DEBUG("\n");
297
298         minor_id = drm_minor_get_id(dev, type);
299         if (minor_id < 0)
300                 return minor_id;
301
302         new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
303         if (!new_minor) {
304                 ret = -ENOMEM;
305                 goto err_idr;
306         }
307
308         new_minor->type = type;
309         new_minor->device = MKDEV(DRM_MAJOR, minor_id);
310         new_minor->dev = dev;
311         new_minor->index = minor_id;
312         INIT_LIST_HEAD(&new_minor->master_list);
313
314         idr_replace(&drm_minors_idr, new_minor, minor_id);
315
316         if (type == DRM_MINOR_LEGACY) {
317                 ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
318                 if (ret) {
319                         DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
320                         goto err_mem;
321                 }
322         } else
323                 new_minor->proc_root = NULL;
324
325 #if defined(CONFIG_DEBUG_FS)
326         ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
327         if (ret) {
328                 DRM_ERROR("DRM: Failed to initialize /debugfs/dri.\n");
329                 goto err_g2;
330         }
331 #endif
332
333         ret = drm_sysfs_device_add(new_minor);
334         if (ret) {
335                 printk(KERN_ERR
336                        "DRM: Error sysfs_device_add.\n");
337                 goto err_g2;
338         }
339         *minor = new_minor;
340
341         DRM_DEBUG("new minor assigned %d\n", minor_id);
342         return 0;
343
344
345 err_g2:
346         if (new_minor->type == DRM_MINOR_LEGACY)
347                 drm_proc_cleanup(new_minor, drm_proc_root);
348 err_mem:
349         kfree(new_minor);
350 err_idr:
351         idr_remove(&drm_minors_idr, minor_id);
352         *minor = NULL;
353         return ret;
354 }
355
356 /**
357  * Register.
358  *
359  * \param pdev - PCI device structure
360  * \param ent entry from the PCI ID table with device type flags
361  * \return zero on success or a negative number on failure.
362  *
363  * Attempt to gets inter module "drm" information. If we are first
364  * then register the character device and inter module information.
365  * Try and register, if we fail to register, backout previous work.
366  */
367 int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
368                 struct drm_driver *driver)
369 {
370         struct drm_device *dev;
371         int ret;
372
373         DRM_DEBUG("\n");
374
375         dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
376         if (!dev)
377                 return -ENOMEM;
378
379         ret = pci_enable_device(pdev);
380         if (ret)
381                 goto err_g1;
382
383         pci_set_master(pdev);
384         if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
385                 printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
386                 goto err_g2;
387         }
388
389         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
390                 pci_set_drvdata(pdev, dev);
391                 ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
392                 if (ret)
393                         goto err_g2;
394         }
395
396         if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
397                 goto err_g3;
398
399         if (dev->driver->load) {
400                 ret = dev->driver->load(dev, ent->driver_data);
401                 if (ret)
402                         goto err_g3;
403         }
404
405         /* setup the grouping for the legacy output */
406         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
407                 ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
408                 if (ret)
409                         goto err_g3;
410         }
411
412         list_add_tail(&dev->driver_item, &driver->device_list);
413
414         DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
415                  driver->name, driver->major, driver->minor, driver->patchlevel,
416                  driver->date, pci_name(pdev), dev->primary->index);
417
418         return 0;
419
420 err_g3:
421         drm_put_minor(&dev->primary);
422 err_g2:
423         pci_disable_device(pdev);
424 err_g1:
425         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
426         return ret;
427 }
428 EXPORT_SYMBOL(drm_get_dev);
429
430 /**
431  * Put a secondary minor number.
432  *
433  * \param sec_minor - structure to be released
434  * \return always zero
435  *
436  * Cleans up the proc resources. Not legal for this to be the
437  * last minor released.
438  *
439  */
440 int drm_put_minor(struct drm_minor **minor_p)
441 {
442         struct drm_minor *minor = *minor_p;
443
444         DRM_DEBUG("release secondary minor %d\n", minor->index);
445
446         if (minor->type == DRM_MINOR_LEGACY)
447                 drm_proc_cleanup(minor, drm_proc_root);
448 #if defined(CONFIG_DEBUG_FS)
449         drm_debugfs_cleanup(minor);
450 #endif
451
452         drm_sysfs_device_remove(minor);
453
454         idr_remove(&drm_minors_idr, minor->index);
455
456         kfree(minor);
457         *minor_p = NULL;
458         return 0;
459 }
460
461 /**
462  * Called via drm_exit() at module unload time or when pci device is
463  * unplugged.
464  *
465  * Cleans up all DRM device, calling drm_lastclose().
466  *
467  * \sa drm_init
468  */
469 void drm_put_dev(struct drm_device *dev)
470 {
471         struct drm_driver *driver = dev->driver;
472         struct drm_map_list *r_list, *list_temp;
473
474         DRM_DEBUG("\n");
475
476         if (!dev) {
477                 DRM_ERROR("cleanup called no dev\n");
478                 return;
479         }
480
481         drm_vblank_cleanup(dev);
482
483         drm_lastclose(dev);
484
485         if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
486             dev->agp && dev->agp->agp_mtrr >= 0) {
487                 int retval;
488                 retval = mtrr_del(dev->agp->agp_mtrr,
489                                   dev->agp->agp_info.aper_base,
490                                   dev->agp->agp_info.aper_size * 1024 * 1024);
491                 DRM_DEBUG("mtrr_del=%d\n", retval);
492         }
493
494         if (dev->driver->unload)
495                 dev->driver->unload(dev);
496
497         if (drm_core_has_AGP(dev) && dev->agp) {
498                 drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
499                 dev->agp = NULL;
500         }
501
502         drm_ht_remove(&dev->map_hash);
503         drm_ctxbitmap_cleanup(dev);
504
505         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
506                 drm_rmmap(dev, r_list->map);
507
508         if (drm_core_check_feature(dev, DRIVER_MODESET))
509                 drm_put_minor(&dev->control);
510
511         if (driver->driver_features & DRIVER_GEM)
512                 drm_gem_destroy(dev);
513
514         drm_put_minor(&dev->primary);
515
516         if (dev->devname) {
517                 drm_free(dev->devname, strlen(dev->devname) + 1,
518                          DRM_MEM_DRIVER);
519                 dev->devname = NULL;
520         }
521         drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
522 }
523 EXPORT_SYMBOL(drm_put_dev);