drm_stub.c 8.3 KB

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