drm_stub.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 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,
  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. sema_init(&dev->struct_sem, 1);
  57. sema_init(&dev->ctxlist_sem, 1);
  58. dev->pdev = pdev;
  59. #ifdef __alpha__
  60. dev->hose = pdev->sysdata;
  61. dev->pci_domain = dev->hose->bus->number;
  62. #else
  63. dev->pci_domain = 0;
  64. #endif
  65. dev->pci_bus = pdev->bus->number;
  66. dev->pci_slot = PCI_SLOT(pdev->devfn);
  67. dev->pci_func = PCI_FUNC(pdev->devfn);
  68. dev->irq = pdev->irq;
  69. dev->maplist = drm_calloc(1, sizeof(*dev->maplist), DRM_MEM_MAPS);
  70. if (dev->maplist == NULL)
  71. return -ENOMEM;
  72. INIT_LIST_HEAD(&dev->maplist->head);
  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->preinit)
  83. if ((retcode = dev->driver->preinit(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_takedown(dev);
  110. return retcode;
  111. }
  112. /**
  113. * File \c open operation.
  114. *
  115. * \param inode device inode.
  116. * \param filp file pointer.
  117. *
  118. * Puts the dev->fops corresponding to the device minor number into
  119. * \p filp, call the \c open method, and restore the file operations.
  120. */
  121. int drm_stub_open(struct inode *inode, struct file *filp)
  122. {
  123. drm_device_t *dev = NULL;
  124. int minor = iminor(inode);
  125. int err = -ENODEV;
  126. struct file_operations *old_fops;
  127. DRM_DEBUG("\n");
  128. if (!((minor >= 0) && (minor < drm_cards_limit)))
  129. return -ENODEV;
  130. if (!drm_heads[minor])
  131. return -ENODEV;
  132. if (!(dev = drm_heads[minor]->dev))
  133. return -ENODEV;
  134. old_fops = filp->f_op;
  135. filp->f_op = fops_get(&dev->driver->fops);
  136. if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
  137. fops_put(filp->f_op);
  138. filp->f_op = fops_get(old_fops);
  139. }
  140. fops_put(old_fops);
  141. return err;
  142. }
  143. /**
  144. * Get a secondary minor number.
  145. *
  146. * \param dev device data structure
  147. * \param sec-minor structure to hold the assigned minor
  148. * \return negative number on failure.
  149. *
  150. * Search an empty entry and initialize it to the given parameters, and
  151. * create the proc init entry via proc_init(). This routines assigns
  152. * minor numbers to secondary heads of multi-headed cards
  153. */
  154. static int drm_get_head(drm_device_t * dev, drm_head_t * head)
  155. {
  156. drm_head_t **heads = drm_heads;
  157. int ret;
  158. int minor;
  159. DRM_DEBUG("\n");
  160. for (minor = 0; minor < drm_cards_limit; minor++, heads++) {
  161. if (!*heads) {
  162. *head = (drm_head_t) {
  163. .dev = dev,.device =
  164. MKDEV(DRM_MAJOR, minor),.minor = minor,};
  165. if ((ret =
  166. drm_proc_init(dev, minor, drm_proc_root,
  167. &head->dev_root))) {
  168. printk(KERN_ERR
  169. "DRM: Failed to initialize /proc/dri.\n");
  170. goto err_g1;
  171. }
  172. head->dev_class = drm_sysfs_device_add(drm_class,
  173. MKDEV(DRM_MAJOR,
  174. minor),
  175. &dev->pdev->dev,
  176. "card%d", minor);
  177. if (IS_ERR(head->dev_class)) {
  178. printk(KERN_ERR
  179. "DRM: Error sysfs_device_add.\n");
  180. ret = PTR_ERR(head->dev_class);
  181. goto err_g2;
  182. }
  183. *heads = head;
  184. DRM_DEBUG("new minor assigned %d\n", minor);
  185. return 0;
  186. }
  187. }
  188. DRM_ERROR("out of minors\n");
  189. return -ENOMEM;
  190. err_g2:
  191. drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
  192. err_g1:
  193. *head = (drm_head_t) {
  194. .dev = NULL};
  195. return ret;
  196. }
  197. /**
  198. * Register.
  199. *
  200. * \param pdev - PCI device structure
  201. * \param ent entry from the PCI ID table with device type flags
  202. * \return zero on success or a negative number on failure.
  203. *
  204. * Attempt to gets inter module "drm" information. If we are first
  205. * then register the character device and inter module information.
  206. * Try and register, if we fail to register, backout previous work.
  207. */
  208. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  209. struct drm_driver *driver)
  210. {
  211. drm_device_t *dev;
  212. int ret;
  213. DRM_DEBUG("\n");
  214. dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
  215. if (!dev)
  216. return -ENOMEM;
  217. pci_enable_device(pdev);
  218. if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
  219. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  220. goto err_g1;
  221. }
  222. if ((ret = drm_get_head(dev, &dev->primary)))
  223. goto err_g1;
  224. /* postinit is a required function to display the signon banner */
  225. /* drivers add secondary heads here if needed */
  226. if ((ret = dev->driver->postinit(dev, ent->driver_data)))
  227. goto err_g1;
  228. return 0;
  229. err_g1:
  230. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  231. return ret;
  232. }
  233. EXPORT_SYMBOL(drm_get_dev);
  234. /**
  235. * Put a device minor number.
  236. *
  237. * \param dev device data structure
  238. * \return always zero
  239. *
  240. * Cleans up the proc resources. If it is the last minor then release the foreign
  241. * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
  242. * unregisters the character device.
  243. */
  244. int drm_put_dev(drm_device_t * dev)
  245. {
  246. DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
  247. if (dev->unique) {
  248. drm_free(dev->unique, strlen(dev->unique) + 1, DRM_MEM_DRIVER);
  249. dev->unique = NULL;
  250. dev->unique_len = 0;
  251. }
  252. if (dev->devname) {
  253. drm_free(dev->devname, strlen(dev->devname) + 1,
  254. DRM_MEM_DRIVER);
  255. dev->devname = NULL;
  256. }
  257. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  258. return 0;
  259. }
  260. /**
  261. * Put a secondary minor number.
  262. *
  263. * \param sec_minor - structure to be released
  264. * \return always zero
  265. *
  266. * Cleans up the proc resources. Not legal for this to be the
  267. * last minor released.
  268. *
  269. */
  270. int drm_put_head(drm_head_t * head)
  271. {
  272. int minor = head->minor;
  273. DRM_DEBUG("release secondary minor %d\n", minor);
  274. drm_proc_cleanup(minor, drm_proc_root, head->dev_root);
  275. drm_sysfs_device_remove(MKDEV(DRM_MAJOR, head->minor));
  276. *head = (drm_head_t) {
  277. .dev = NULL};
  278. drm_heads[minor] = NULL;
  279. return 0;
  280. }