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