drm_stub.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_debug = 0; /* 1 to enable debug output */
  37. EXPORT_SYMBOL(drm_debug);
  38. MODULE_AUTHOR(CORE_AUTHOR);
  39. MODULE_DESCRIPTION(CORE_DESC);
  40. MODULE_LICENSE("GPL and additional rights");
  41. MODULE_PARM_DESC(debug, "Enable debug output");
  42. module_param_named(debug, drm_debug, int, 0600);
  43. struct idr drm_minors_idr;
  44. struct class *drm_class;
  45. struct proc_dir_entry *drm_proc_root;
  46. static int drm_minor_get_id(struct drm_device *dev, int type)
  47. {
  48. int new_id;
  49. int ret;
  50. int base = 0, limit = 63;
  51. again:
  52. if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
  53. DRM_ERROR("Out of memory expanding drawable idr\n");
  54. return -ENOMEM;
  55. }
  56. mutex_lock(&dev->struct_mutex);
  57. ret = idr_get_new_above(&drm_minors_idr, NULL,
  58. base, &new_id);
  59. mutex_unlock(&dev->struct_mutex);
  60. if (ret == -EAGAIN) {
  61. goto again;
  62. } else if (ret) {
  63. return ret;
  64. }
  65. if (new_id >= limit) {
  66. idr_remove(&drm_minors_idr, new_id);
  67. return -EINVAL;
  68. }
  69. return new_id;
  70. }
  71. static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
  72. const struct pci_device_id *ent,
  73. struct drm_driver *driver)
  74. {
  75. int retcode;
  76. INIT_LIST_HEAD(&dev->filelist);
  77. INIT_LIST_HEAD(&dev->ctxlist);
  78. INIT_LIST_HEAD(&dev->vmalist);
  79. INIT_LIST_HEAD(&dev->maplist);
  80. spin_lock_init(&dev->count_lock);
  81. spin_lock_init(&dev->drw_lock);
  82. spin_lock_init(&dev->tasklet_lock);
  83. spin_lock_init(&dev->lock.spinlock);
  84. init_timer(&dev->timer);
  85. mutex_init(&dev->struct_mutex);
  86. mutex_init(&dev->ctxlist_mutex);
  87. idr_init(&dev->drw_idr);
  88. dev->pdev = pdev;
  89. dev->pci_device = pdev->device;
  90. dev->pci_vendor = pdev->vendor;
  91. #ifdef __alpha__
  92. dev->hose = pdev->sysdata;
  93. #endif
  94. dev->irq = pdev->irq;
  95. if (drm_ht_create(&dev->map_hash, 12)) {
  96. return -ENOMEM;
  97. }
  98. /* the DRM has 6 basic counters */
  99. dev->counters = 6;
  100. dev->types[0] = _DRM_STAT_LOCK;
  101. dev->types[1] = _DRM_STAT_OPENS;
  102. dev->types[2] = _DRM_STAT_CLOSES;
  103. dev->types[3] = _DRM_STAT_IOCTLS;
  104. dev->types[4] = _DRM_STAT_LOCKS;
  105. dev->types[5] = _DRM_STAT_UNLOCKS;
  106. dev->driver = driver;
  107. if (drm_core_has_AGP(dev)) {
  108. if (drm_device_is_agp(dev))
  109. dev->agp = drm_agp_init(dev);
  110. if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
  111. && (dev->agp == NULL)) {
  112. DRM_ERROR("Cannot initialize the agpgart module.\n");
  113. retcode = -EINVAL;
  114. goto error_out_unreg;
  115. }
  116. if (drm_core_has_MTRR(dev)) {
  117. if (dev->agp)
  118. dev->agp->agp_mtrr =
  119. mtrr_add(dev->agp->agp_info.aper_base,
  120. dev->agp->agp_info.aper_size *
  121. 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
  122. }
  123. }
  124. if (dev->driver->load)
  125. if ((retcode = dev->driver->load(dev, ent->driver_data)))
  126. goto error_out_unreg;
  127. retcode = drm_ctxbitmap_init(dev);
  128. if (retcode) {
  129. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  130. goto error_out_unreg;
  131. }
  132. return 0;
  133. error_out_unreg:
  134. drm_lastclose(dev);
  135. return retcode;
  136. }
  137. /**
  138. * Get a secondary minor number.
  139. *
  140. * \param dev device data structure
  141. * \param sec-minor structure to hold the assigned minor
  142. * \return negative number on failure.
  143. *
  144. * Search an empty entry and initialize it to the given parameters, and
  145. * create the proc init entry via proc_init(). This routines assigns
  146. * minor numbers to secondary heads of multi-headed cards
  147. */
  148. static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
  149. {
  150. struct drm_minor *new_minor;
  151. int ret;
  152. int minor_id;
  153. DRM_DEBUG("\n");
  154. minor_id = drm_minor_get_id(dev, type);
  155. if (minor_id < 0)
  156. return minor_id;
  157. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  158. if (!new_minor) {
  159. ret = -ENOMEM;
  160. goto err_idr;
  161. }
  162. new_minor->type = type;
  163. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  164. new_minor->dev = dev;
  165. new_minor->index = minor_id;
  166. idr_replace(&drm_minors_idr, new_minor, minor_id);
  167. if (type == DRM_MINOR_LEGACY) {
  168. ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
  169. if (ret) {
  170. DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
  171. goto err_mem;
  172. }
  173. } else
  174. new_minor->dev_root = NULL;
  175. ret = drm_sysfs_device_add(new_minor);
  176. if (ret) {
  177. printk(KERN_ERR
  178. "DRM: Error sysfs_device_add.\n");
  179. goto err_g2;
  180. }
  181. *minor = new_minor;
  182. DRM_DEBUG("new minor assigned %d\n", minor_id);
  183. return 0;
  184. err_g2:
  185. if (new_minor->type == DRM_MINOR_LEGACY)
  186. drm_proc_cleanup(new_minor, drm_proc_root);
  187. err_mem:
  188. kfree(new_minor);
  189. err_idr:
  190. idr_remove(&drm_minors_idr, minor_id);
  191. *minor = NULL;
  192. return ret;
  193. }
  194. /**
  195. * Register.
  196. *
  197. * \param pdev - PCI device structure
  198. * \param ent entry from the PCI ID table with device type flags
  199. * \return zero on success or a negative number on failure.
  200. *
  201. * Attempt to gets inter module "drm" information. If we are first
  202. * then register the character device and inter module information.
  203. * Try and register, if we fail to register, backout previous work.
  204. */
  205. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  206. struct drm_driver *driver)
  207. {
  208. struct drm_device *dev;
  209. int ret;
  210. DRM_DEBUG("\n");
  211. dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
  212. if (!dev)
  213. return -ENOMEM;
  214. ret = pci_enable_device(pdev);
  215. if (ret)
  216. goto err_g1;
  217. pci_set_master(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_g2;
  221. }
  222. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  223. goto err_g2;
  224. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
  225. driver->name, driver->major, driver->minor, driver->patchlevel,
  226. driver->date, dev->primary->index);
  227. return 0;
  228. err_g2:
  229. pci_disable_device(pdev);
  230. err_g1:
  231. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  232. return ret;
  233. }
  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(struct drm_device * 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_minor(struct drm_minor **minor_p)
  271. {
  272. struct drm_minor *minor = *minor_p;
  273. DRM_DEBUG("release secondary minor %d\n", minor->index);
  274. if (minor->type == DRM_MINOR_LEGACY)
  275. drm_proc_cleanup(minor, drm_proc_root);
  276. drm_sysfs_device_remove(minor);
  277. idr_remove(&drm_minors_idr, minor->index);
  278. kfree(minor);
  279. *minor_p = NULL;
  280. return 0;
  281. }