drm_stub.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. drm_head_t **drm_heads;
  47. struct class *drm_class;
  48. struct proc_dir_entry *drm_proc_root;
  49. static int drm_fill_in_dev(drm_device_t * dev, struct pci_dev *pdev,
  50. const struct pci_device_id *ent,
  51. struct drm_driver *driver)
  52. {
  53. int retcode;
  54. spin_lock_init(&dev->count_lock);
  55. init_timer(&dev->timer);
  56. mutex_init(&dev->struct_mutex);
  57. mutex_init(&dev->ctxlist_mutex);
  58. dev->pdev = pdev;
  59. dev->pci_device = pdev->device;
  60. dev->pci_vendor = pdev->vendor;
  61. #ifdef __alpha__
  62. dev->hose = pdev->sysdata;
  63. #endif
  64. dev->irq = pdev->irq;
  65. dev->maplist = drm_calloc(1, sizeof(*dev->maplist), DRM_MEM_MAPS);
  66. if (dev->maplist == NULL)
  67. return -ENOMEM;
  68. INIT_LIST_HEAD(&dev->maplist->head);
  69. if (drm_ht_create(&dev->map_hash, 12)) {
  70. drm_free(dev->maplist, sizeof(*dev->maplist), DRM_MEM_MAPS);
  71. return -ENOMEM;
  72. }
  73. /* the DRM has 6 basic counters */
  74. dev->counters = 6;
  75. dev->types[0] = _DRM_STAT_LOCK;
  76. dev->types[1] = _DRM_STAT_OPENS;
  77. dev->types[2] = _DRM_STAT_CLOSES;
  78. dev->types[3] = _DRM_STAT_IOCTLS;
  79. dev->types[4] = _DRM_STAT_LOCKS;
  80. dev->types[5] = _DRM_STAT_UNLOCKS;
  81. dev->driver = driver;
  82. if (dev->driver->load)
  83. if ((retcode = dev->driver->load(dev, ent->driver_data)))
  84. goto error_out_unreg;
  85. if (drm_core_has_AGP(dev)) {
  86. if (drm_device_is_agp(dev))
  87. dev->agp = drm_agp_init(dev);
  88. if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
  89. && (dev->agp == NULL)) {
  90. DRM_ERROR("Cannot initialize the agpgart module.\n");
  91. retcode = -EINVAL;
  92. goto error_out_unreg;
  93. }
  94. if (drm_core_has_MTRR(dev)) {
  95. if (dev->agp)
  96. dev->agp->agp_mtrr =
  97. mtrr_add(dev->agp->agp_info.aper_base,
  98. dev->agp->agp_info.aper_size *
  99. 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
  100. }
  101. }
  102. retcode = drm_ctxbitmap_init(dev);
  103. if (retcode) {
  104. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  105. goto error_out_unreg;
  106. }
  107. return 0;
  108. error_out_unreg:
  109. drm_lastclose(dev);
  110. return retcode;
  111. }
  112. /**
  113. * Get a secondary minor number.
  114. *
  115. * \param dev device data structure
  116. * \param sec-minor structure to hold the assigned minor
  117. * \return negative number on failure.
  118. *
  119. * Search an empty entry and initialize it to the given parameters, and
  120. * create the proc init entry via proc_init(). This routines assigns
  121. * minor numbers to secondary heads of multi-headed cards
  122. */
  123. static int drm_get_head(drm_device_t * dev, drm_head_t * head)
  124. {
  125. drm_head_t **heads = drm_heads;
  126. int ret;
  127. int minor;
  128. DRM_DEBUG("\n");
  129. for (minor = 0; minor < drm_cards_limit; minor++, heads++) {
  130. if (!*heads) {
  131. *head = (drm_head_t) {
  132. .dev = dev,.device =
  133. MKDEV(DRM_MAJOR, minor),.minor = minor,};
  134. if ((ret =
  135. drm_proc_init(dev, minor, drm_proc_root,
  136. &head->dev_root))) {
  137. printk(KERN_ERR
  138. "DRM: Failed to initialize /proc/dri.\n");
  139. goto err_g1;
  140. }
  141. head->dev_class = drm_sysfs_device_add(drm_class, head);
  142. if (IS_ERR(head->dev_class)) {
  143. printk(KERN_ERR
  144. "DRM: Error sysfs_device_add.\n");
  145. ret = PTR_ERR(head->dev_class);
  146. goto err_g2;
  147. }
  148. *heads = head;
  149. DRM_DEBUG("new minor assigned %d\n", minor);
  150. return 0;
  151. }
  152. }
  153. DRM_ERROR("out of minors\n");
  154. return -ENOMEM;
  155. err_g2:
  156. drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
  157. err_g1:
  158. *head = (drm_head_t) {
  159. .dev = NULL};
  160. return ret;
  161. }
  162. /**
  163. * Register.
  164. *
  165. * \param pdev - PCI device structure
  166. * \param ent entry from the PCI ID table with device type flags
  167. * \return zero on success or a negative number on failure.
  168. *
  169. * Attempt to gets inter module "drm" information. If we are first
  170. * then register the character device and inter module information.
  171. * Try and register, if we fail to register, backout previous work.
  172. */
  173. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  174. struct drm_driver *driver)
  175. {
  176. drm_device_t *dev;
  177. int ret;
  178. DRM_DEBUG("\n");
  179. dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
  180. if (!dev)
  181. return -ENOMEM;
  182. pci_enable_device(pdev);
  183. if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
  184. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  185. goto err_g1;
  186. }
  187. if ((ret = drm_get_head(dev, &dev->primary)))
  188. goto err_g1;
  189. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
  190. driver->name, driver->major, driver->minor, driver->patchlevel,
  191. driver->date, dev->primary.minor);
  192. return 0;
  193. err_g1:
  194. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  195. return ret;
  196. }
  197. /**
  198. * Put a device minor number.
  199. *
  200. * \param dev device data structure
  201. * \return always zero
  202. *
  203. * Cleans up the proc resources. If it is the last minor then release the foreign
  204. * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
  205. * unregisters the character device.
  206. */
  207. int drm_put_dev(drm_device_t * dev)
  208. {
  209. DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
  210. if (dev->unique) {
  211. drm_free(dev->unique, strlen(dev->unique) + 1, DRM_MEM_DRIVER);
  212. dev->unique = NULL;
  213. dev->unique_len = 0;
  214. }
  215. if (dev->devname) {
  216. drm_free(dev->devname, strlen(dev->devname) + 1,
  217. DRM_MEM_DRIVER);
  218. dev->devname = NULL;
  219. }
  220. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  221. return 0;
  222. }
  223. /**
  224. * Put a secondary minor number.
  225. *
  226. * \param sec_minor - structure to be released
  227. * \return always zero
  228. *
  229. * Cleans up the proc resources. Not legal for this to be the
  230. * last minor released.
  231. *
  232. */
  233. int drm_put_head(drm_head_t * head)
  234. {
  235. int minor = head->minor;
  236. DRM_DEBUG("release secondary minor %d\n", minor);
  237. drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
  238. drm_sysfs_device_remove(head->dev_class);
  239. *head = (drm_head_t) {.dev = NULL};
  240. drm_heads[minor] = NULL;
  241. return 0;
  242. }