drm_stub.c 8.4 KB

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