drm_stub.c 8.2 KB

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