drm_stub.c 8.3 KB

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