drm_stub.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. if (driver->driver_features & DRIVER_GEM) {
  133. retcode = drm_gem_init(dev);
  134. if (retcode) {
  135. DRM_ERROR("Cannot initialize graphics execution "
  136. "manager (GEM)\n");
  137. goto error_out_unreg;
  138. }
  139. }
  140. return 0;
  141. error_out_unreg:
  142. drm_lastclose(dev);
  143. return retcode;
  144. }
  145. /**
  146. * Get a secondary minor number.
  147. *
  148. * \param dev device data structure
  149. * \param sec-minor structure to hold the assigned minor
  150. * \return negative number on failure.
  151. *
  152. * Search an empty entry and initialize it to the given parameters, and
  153. * create the proc init entry via proc_init(). This routines assigns
  154. * minor numbers to secondary heads of multi-headed cards
  155. */
  156. static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
  157. {
  158. struct drm_minor *new_minor;
  159. int ret;
  160. int minor_id;
  161. DRM_DEBUG("\n");
  162. minor_id = drm_minor_get_id(dev, type);
  163. if (minor_id < 0)
  164. return minor_id;
  165. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  166. if (!new_minor) {
  167. ret = -ENOMEM;
  168. goto err_idr;
  169. }
  170. new_minor->type = type;
  171. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  172. new_minor->dev = dev;
  173. new_minor->index = minor_id;
  174. idr_replace(&drm_minors_idr, new_minor, minor_id);
  175. if (type == DRM_MINOR_LEGACY) {
  176. ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
  177. if (ret) {
  178. DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
  179. goto err_mem;
  180. }
  181. } else
  182. new_minor->dev_root = NULL;
  183. ret = drm_sysfs_device_add(new_minor);
  184. if (ret) {
  185. printk(KERN_ERR
  186. "DRM: Error sysfs_device_add.\n");
  187. goto err_g2;
  188. }
  189. *minor = new_minor;
  190. DRM_DEBUG("new minor assigned %d\n", minor_id);
  191. return 0;
  192. err_g2:
  193. if (new_minor->type == DRM_MINOR_LEGACY)
  194. drm_proc_cleanup(new_minor, drm_proc_root);
  195. err_mem:
  196. kfree(new_minor);
  197. err_idr:
  198. idr_remove(&drm_minors_idr, minor_id);
  199. *minor = NULL;
  200. return ret;
  201. }
  202. /**
  203. * Register.
  204. *
  205. * \param pdev - PCI device structure
  206. * \param ent entry from the PCI ID table with device type flags
  207. * \return zero on success or a negative number on failure.
  208. *
  209. * Attempt to gets inter module "drm" information. If we are first
  210. * then register the character device and inter module information.
  211. * Try and register, if we fail to register, backout previous work.
  212. */
  213. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  214. struct drm_driver *driver)
  215. {
  216. struct drm_device *dev;
  217. int ret;
  218. DRM_DEBUG("\n");
  219. dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
  220. if (!dev)
  221. return -ENOMEM;
  222. ret = pci_enable_device(pdev);
  223. if (ret)
  224. goto err_g1;
  225. pci_set_master(pdev);
  226. if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
  227. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  228. goto err_g2;
  229. }
  230. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  231. goto err_g2;
  232. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
  233. driver->name, driver->major, driver->minor, driver->patchlevel,
  234. driver->date, dev->primary->index);
  235. return 0;
  236. err_g2:
  237. pci_disable_device(pdev);
  238. err_g1:
  239. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  240. return ret;
  241. }
  242. /**
  243. * Put a device minor number.
  244. *
  245. * \param dev device data structure
  246. * \return always zero
  247. *
  248. * Cleans up the proc resources. If it is the last minor then release the foreign
  249. * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
  250. * unregisters the character device.
  251. */
  252. int drm_put_dev(struct drm_device * dev)
  253. {
  254. DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
  255. if (dev->unique) {
  256. drm_free(dev->unique, strlen(dev->unique) + 1, DRM_MEM_DRIVER);
  257. dev->unique = NULL;
  258. dev->unique_len = 0;
  259. }
  260. if (dev->devname) {
  261. drm_free(dev->devname, strlen(dev->devname) + 1,
  262. DRM_MEM_DRIVER);
  263. dev->devname = NULL;
  264. }
  265. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  266. return 0;
  267. }
  268. /**
  269. * Put a secondary minor number.
  270. *
  271. * \param sec_minor - structure to be released
  272. * \return always zero
  273. *
  274. * Cleans up the proc resources. Not legal for this to be the
  275. * last minor released.
  276. *
  277. */
  278. int drm_put_minor(struct drm_minor **minor_p)
  279. {
  280. struct drm_minor *minor = *minor_p;
  281. DRM_DEBUG("release secondary minor %d\n", minor->index);
  282. if (minor->type == DRM_MINOR_LEGACY)
  283. drm_proc_cleanup(minor, drm_proc_root);
  284. drm_sysfs_device_remove(minor);
  285. idr_remove(&drm_minors_idr, minor->index);
  286. kfree(minor);
  287. *minor_p = NULL;
  288. return 0;
  289. }