drm_stub.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * \file drm_stub.h
  3. * Stub support
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. */
  7. /*
  8. * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
  9. *
  10. * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include "drmP.h"
  35. #include "drm_core.h"
  36. unsigned int drm_cards_limit = 16; /* Enough for one machine */
  37. unsigned int drm_debug = 0; /* 1 to enable debug output */
  38. EXPORT_SYMBOL(drm_debug);
  39. MODULE_AUTHOR(CORE_AUTHOR);
  40. MODULE_DESCRIPTION(CORE_DESC);
  41. MODULE_LICENSE("GPL and additional rights");
  42. MODULE_PARM_DESC(cards_limit, "Maximum number of graphics cards");
  43. MODULE_PARM_DESC(debug, "Enable debug output");
  44. module_param_named(cards_limit, drm_cards_limit, int, 0444);
  45. module_param_named(debug, drm_debug, int, 0600);
  46. struct drm_head **drm_heads;
  47. struct class *drm_class;
  48. struct proc_dir_entry *drm_proc_root;
  49. static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
  50. const struct pci_device_id *ent,
  51. struct drm_driver *driver)
  52. {
  53. int retcode;
  54. INIT_LIST_HEAD(&dev->filelist);
  55. INIT_LIST_HEAD(&dev->ctxlist);
  56. INIT_LIST_HEAD(&dev->vmalist);
  57. INIT_LIST_HEAD(&dev->maplist);
  58. spin_lock_init(&dev->count_lock);
  59. spin_lock_init(&dev->drw_lock);
  60. spin_lock_init(&dev->tasklet_lock);
  61. spin_lock_init(&dev->lock.spinlock);
  62. init_timer(&dev->timer);
  63. mutex_init(&dev->struct_mutex);
  64. mutex_init(&dev->ctxlist_mutex);
  65. idr_init(&dev->drw_idr);
  66. dev->pdev = pdev;
  67. dev->pci_device = pdev->device;
  68. dev->pci_vendor = pdev->vendor;
  69. #ifdef __alpha__
  70. dev->hose = pdev->sysdata;
  71. #endif
  72. dev->irq = pdev->irq;
  73. if (drm_ht_create(&dev->map_hash, 12)) {
  74. return -ENOMEM;
  75. }
  76. /* the DRM has 6 basic counters */
  77. dev->counters = 6;
  78. dev->types[0] = _DRM_STAT_LOCK;
  79. dev->types[1] = _DRM_STAT_OPENS;
  80. dev->types[2] = _DRM_STAT_CLOSES;
  81. dev->types[3] = _DRM_STAT_IOCTLS;
  82. dev->types[4] = _DRM_STAT_LOCKS;
  83. dev->types[5] = _DRM_STAT_UNLOCKS;
  84. dev->driver = driver;
  85. if (dev->driver->load)
  86. if ((retcode = dev->driver->load(dev, ent->driver_data)))
  87. goto error_out_unreg;
  88. if (drm_core_has_AGP(dev)) {
  89. if (drm_device_is_agp(dev))
  90. dev->agp = drm_agp_init(dev);
  91. if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
  92. && (dev->agp == NULL)) {
  93. DRM_ERROR("Cannot initialize the agpgart module.\n");
  94. retcode = -EINVAL;
  95. goto error_out_unreg;
  96. }
  97. if (drm_core_has_MTRR(dev)) {
  98. if (dev->agp)
  99. dev->agp->agp_mtrr =
  100. mtrr_add(dev->agp->agp_info.aper_base,
  101. dev->agp->agp_info.aper_size *
  102. 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
  103. }
  104. }
  105. retcode = drm_ctxbitmap_init(dev);
  106. if (retcode) {
  107. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  108. goto error_out_unreg;
  109. }
  110. return 0;
  111. error_out_unreg:
  112. drm_lastclose(dev);
  113. return retcode;
  114. }
  115. /**
  116. * Get a secondary minor number.
  117. *
  118. * \param dev device data structure
  119. * \param sec-minor structure to hold the assigned minor
  120. * \return negative number on failure.
  121. *
  122. * Search an empty entry and initialize it to the given parameters, and
  123. * create the proc init entry via proc_init(). This routines assigns
  124. * minor numbers to secondary heads of multi-headed cards
  125. */
  126. static int drm_get_head(struct drm_device * dev, struct drm_head * head)
  127. {
  128. struct drm_head **heads = drm_heads;
  129. int ret;
  130. int minor;
  131. DRM_DEBUG("\n");
  132. for (minor = 0; minor < drm_cards_limit; minor++, heads++) {
  133. if (!*heads) {
  134. *head = (struct drm_head) {
  135. .dev = dev,.device =
  136. MKDEV(DRM_MAJOR, minor),.minor = minor,};
  137. if ((ret =
  138. drm_proc_init(dev, minor, drm_proc_root,
  139. &head->dev_root))) {
  140. printk(KERN_ERR
  141. "DRM: Failed to initialize /proc/dri.\n");
  142. goto err_g1;
  143. }
  144. head->dev_class = drm_sysfs_device_add(drm_class, head);
  145. if (IS_ERR(head->dev_class)) {
  146. printk(KERN_ERR
  147. "DRM: Error sysfs_device_add.\n");
  148. ret = PTR_ERR(head->dev_class);
  149. goto err_g2;
  150. }
  151. *heads = head;
  152. DRM_DEBUG("new minor assigned %d\n", minor);
  153. return 0;
  154. }
  155. }
  156. DRM_ERROR("out of minors\n");
  157. return -ENOMEM;
  158. err_g2:
  159. drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
  160. err_g1:
  161. *head = (struct drm_head) {
  162. .dev = NULL};
  163. return ret;
  164. }
  165. /**
  166. * Register.
  167. *
  168. * \param pdev - PCI device structure
  169. * \param ent entry from the PCI ID table with device type flags
  170. * \return zero on success or a negative number on failure.
  171. *
  172. * Attempt to gets inter module "drm" information. If we are first
  173. * then register the character device and inter module information.
  174. * Try and register, if we fail to register, backout previous work.
  175. */
  176. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  177. struct drm_driver *driver)
  178. {
  179. struct drm_device *dev;
  180. int ret;
  181. DRM_DEBUG("\n");
  182. dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
  183. if (!dev)
  184. return -ENOMEM;
  185. ret = pci_enable_device(pdev);
  186. if (ret)
  187. goto err_g1;
  188. if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
  189. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  190. goto err_g2;
  191. }
  192. if ((ret = drm_get_head(dev, &dev->primary)))
  193. goto err_g2;
  194. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
  195. driver->name, driver->major, driver->minor, driver->patchlevel,
  196. driver->date, dev->primary.minor);
  197. return 0;
  198. err_g2:
  199. pci_disable_device(pdev);
  200. err_g1:
  201. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  202. return ret;
  203. }
  204. /**
  205. * Put a device minor number.
  206. *
  207. * \param dev device data structure
  208. * \return always zero
  209. *
  210. * Cleans up the proc resources. If it is the last minor then release the foreign
  211. * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
  212. * unregisters the character device.
  213. */
  214. int drm_put_dev(struct drm_device * dev)
  215. {
  216. DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
  217. if (dev->unique) {
  218. drm_free(dev->unique, strlen(dev->unique) + 1, DRM_MEM_DRIVER);
  219. dev->unique = NULL;
  220. dev->unique_len = 0;
  221. }
  222. if (dev->devname) {
  223. drm_free(dev->devname, strlen(dev->devname) + 1,
  224. DRM_MEM_DRIVER);
  225. dev->devname = NULL;
  226. }
  227. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  228. return 0;
  229. }
  230. /**
  231. * Put a secondary minor number.
  232. *
  233. * \param sec_minor - structure to be released
  234. * \return always zero
  235. *
  236. * Cleans up the proc resources. Not legal for this to be the
  237. * last minor released.
  238. *
  239. */
  240. int drm_put_head(struct drm_head * head)
  241. {
  242. int minor = head->minor;
  243. DRM_DEBUG("release secondary minor %d\n", minor);
  244. drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
  245. drm_sysfs_device_remove(head->dev_class);
  246. *head = (struct drm_head) {.dev = NULL};
  247. drm_heads[minor] = NULL;
  248. return 0;
  249. }