drm_stub.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. if (type == DRM_MINOR_CONTROL) {
  52. base += 64;
  53. limit = base + 127;
  54. } else if (type == DRM_MINOR_RENDER) {
  55. base += 128;
  56. limit = base + 255;
  57. }
  58. again:
  59. if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
  60. DRM_ERROR("Out of memory expanding drawable idr\n");
  61. return -ENOMEM;
  62. }
  63. mutex_lock(&dev->struct_mutex);
  64. ret = idr_get_new_above(&drm_minors_idr, NULL,
  65. base, &new_id);
  66. mutex_unlock(&dev->struct_mutex);
  67. if (ret == -EAGAIN) {
  68. goto again;
  69. } else if (ret) {
  70. return ret;
  71. }
  72. if (new_id >= limit) {
  73. idr_remove(&drm_minors_idr, new_id);
  74. return -EINVAL;
  75. }
  76. return new_id;
  77. }
  78. struct drm_master *drm_master_create(struct drm_minor *minor)
  79. {
  80. struct drm_master *master;
  81. master = drm_calloc(1, sizeof(*master), DRM_MEM_DRIVER);
  82. if (!master)
  83. return NULL;
  84. kref_init(&master->refcount);
  85. spin_lock_init(&master->lock.spinlock);
  86. init_waitqueue_head(&master->lock.lock_queue);
  87. drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
  88. INIT_LIST_HEAD(&master->magicfree);
  89. master->minor = minor;
  90. list_add_tail(&master->head, &minor->master_list);
  91. return master;
  92. }
  93. struct drm_master *drm_master_get(struct drm_master *master)
  94. {
  95. kref_get(&master->refcount);
  96. return master;
  97. }
  98. static void drm_master_destroy(struct kref *kref)
  99. {
  100. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  101. struct drm_magic_entry *pt, *next;
  102. struct drm_device *dev = master->minor->dev;
  103. list_del(&master->head);
  104. if (dev->driver->master_destroy)
  105. dev->driver->master_destroy(dev, master);
  106. if (master->unique) {
  107. drm_free(master->unique, master->unique_size, DRM_MEM_DRIVER);
  108. master->unique = NULL;
  109. master->unique_len = 0;
  110. }
  111. list_for_each_entry_safe(pt, next, &master->magicfree, head) {
  112. list_del(&pt->head);
  113. drm_ht_remove_item(&master->magiclist, &pt->hash_item);
  114. drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
  115. }
  116. drm_ht_remove(&master->magiclist);
  117. if (master->lock.hw_lock) {
  118. if (dev->sigdata.lock == master->lock.hw_lock)
  119. dev->sigdata.lock = NULL;
  120. master->lock.hw_lock = NULL;
  121. master->lock.file_priv = NULL;
  122. wake_up_interruptible(&master->lock.lock_queue);
  123. }
  124. drm_free(master, sizeof(*master), DRM_MEM_DRIVER);
  125. }
  126. void drm_master_put(struct drm_master **master)
  127. {
  128. kref_put(&(*master)->refcount, drm_master_destroy);
  129. *master = NULL;
  130. }
  131. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  132. struct drm_file *file_priv)
  133. {
  134. if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
  135. return -EINVAL;
  136. if (!file_priv->master)
  137. return -EINVAL;
  138. if (!file_priv->minor->master &&
  139. file_priv->minor->master != file_priv->master) {
  140. mutex_lock(&dev->struct_mutex);
  141. file_priv->minor->master = drm_master_get(file_priv->master);
  142. mutex_lock(&dev->struct_mutex);
  143. }
  144. return 0;
  145. }
  146. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  147. struct drm_file *file_priv)
  148. {
  149. if (!file_priv->master)
  150. return -EINVAL;
  151. mutex_lock(&dev->struct_mutex);
  152. drm_master_put(&file_priv->minor->master);
  153. mutex_unlock(&dev->struct_mutex);
  154. return 0;
  155. }
  156. static int drm_fill_in_dev(struct drm_device * dev, struct pci_dev *pdev,
  157. const struct pci_device_id *ent,
  158. struct drm_driver *driver)
  159. {
  160. int retcode;
  161. INIT_LIST_HEAD(&dev->filelist);
  162. INIT_LIST_HEAD(&dev->ctxlist);
  163. INIT_LIST_HEAD(&dev->vmalist);
  164. INIT_LIST_HEAD(&dev->maplist);
  165. spin_lock_init(&dev->count_lock);
  166. spin_lock_init(&dev->drw_lock);
  167. init_timer(&dev->timer);
  168. mutex_init(&dev->struct_mutex);
  169. mutex_init(&dev->ctxlist_mutex);
  170. idr_init(&dev->drw_idr);
  171. dev->pdev = pdev;
  172. dev->pci_device = pdev->device;
  173. dev->pci_vendor = pdev->vendor;
  174. #ifdef __alpha__
  175. dev->hose = pdev->sysdata;
  176. #endif
  177. if (drm_ht_create(&dev->map_hash, 12)) {
  178. return -ENOMEM;
  179. }
  180. /* the DRM has 6 basic counters */
  181. dev->counters = 6;
  182. dev->types[0] = _DRM_STAT_LOCK;
  183. dev->types[1] = _DRM_STAT_OPENS;
  184. dev->types[2] = _DRM_STAT_CLOSES;
  185. dev->types[3] = _DRM_STAT_IOCTLS;
  186. dev->types[4] = _DRM_STAT_LOCKS;
  187. dev->types[5] = _DRM_STAT_UNLOCKS;
  188. dev->driver = driver;
  189. if (drm_core_has_AGP(dev)) {
  190. if (drm_device_is_agp(dev))
  191. dev->agp = drm_agp_init(dev);
  192. if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
  193. && (dev->agp == NULL)) {
  194. DRM_ERROR("Cannot initialize the agpgart module.\n");
  195. retcode = -EINVAL;
  196. goto error_out_unreg;
  197. }
  198. if (drm_core_has_MTRR(dev)) {
  199. if (dev->agp)
  200. dev->agp->agp_mtrr =
  201. mtrr_add(dev->agp->agp_info.aper_base,
  202. dev->agp->agp_info.aper_size *
  203. 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
  204. }
  205. }
  206. retcode = drm_ctxbitmap_init(dev);
  207. if (retcode) {
  208. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  209. goto error_out_unreg;
  210. }
  211. if (driver->driver_features & DRIVER_GEM) {
  212. retcode = drm_gem_init(dev);
  213. if (retcode) {
  214. DRM_ERROR("Cannot initialize graphics execution "
  215. "manager (GEM)\n");
  216. goto error_out_unreg;
  217. }
  218. }
  219. return 0;
  220. error_out_unreg:
  221. drm_lastclose(dev);
  222. return retcode;
  223. }
  224. /**
  225. * Get a secondary minor number.
  226. *
  227. * \param dev device data structure
  228. * \param sec-minor structure to hold the assigned minor
  229. * \return negative number on failure.
  230. *
  231. * Search an empty entry and initialize it to the given parameters, and
  232. * create the proc init entry via proc_init(). This routines assigns
  233. * minor numbers to secondary heads of multi-headed cards
  234. */
  235. static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
  236. {
  237. struct drm_minor *new_minor;
  238. int ret;
  239. int minor_id;
  240. DRM_DEBUG("\n");
  241. minor_id = drm_minor_get_id(dev, type);
  242. if (minor_id < 0)
  243. return minor_id;
  244. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  245. if (!new_minor) {
  246. ret = -ENOMEM;
  247. goto err_idr;
  248. }
  249. new_minor->type = type;
  250. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  251. new_minor->dev = dev;
  252. new_minor->index = minor_id;
  253. INIT_LIST_HEAD(&new_minor->master_list);
  254. idr_replace(&drm_minors_idr, new_minor, minor_id);
  255. if (type == DRM_MINOR_LEGACY) {
  256. ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
  257. if (ret) {
  258. DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
  259. goto err_mem;
  260. }
  261. } else
  262. new_minor->dev_root = NULL;
  263. ret = drm_sysfs_device_add(new_minor);
  264. if (ret) {
  265. printk(KERN_ERR
  266. "DRM: Error sysfs_device_add.\n");
  267. goto err_g2;
  268. }
  269. *minor = new_minor;
  270. DRM_DEBUG("new minor assigned %d\n", minor_id);
  271. return 0;
  272. err_g2:
  273. if (new_minor->type == DRM_MINOR_LEGACY)
  274. drm_proc_cleanup(new_minor, drm_proc_root);
  275. err_mem:
  276. kfree(new_minor);
  277. err_idr:
  278. idr_remove(&drm_minors_idr, minor_id);
  279. *minor = NULL;
  280. return ret;
  281. }
  282. /**
  283. * Register.
  284. *
  285. * \param pdev - PCI device structure
  286. * \param ent entry from the PCI ID table with device type flags
  287. * \return zero on success or a negative number on failure.
  288. *
  289. * Attempt to gets inter module "drm" information. If we are first
  290. * then register the character device and inter module information.
  291. * Try and register, if we fail to register, backout previous work.
  292. */
  293. int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
  294. struct drm_driver *driver)
  295. {
  296. struct drm_device *dev;
  297. int ret;
  298. DRM_DEBUG("\n");
  299. dev = drm_calloc(1, sizeof(*dev), DRM_MEM_STUB);
  300. if (!dev)
  301. return -ENOMEM;
  302. ret = pci_enable_device(pdev);
  303. if (ret)
  304. goto err_g1;
  305. pci_set_master(pdev);
  306. if ((ret = drm_fill_in_dev(dev, pdev, ent, driver))) {
  307. printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
  308. goto err_g2;
  309. }
  310. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  311. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  312. if (ret)
  313. goto err_g2;
  314. }
  315. if ((ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY)))
  316. goto err_g3;
  317. if (dev->driver->load) {
  318. ret = dev->driver->load(dev, ent->driver_data);
  319. if (ret)
  320. goto err_g3;
  321. }
  322. /* setup the grouping for the legacy output */
  323. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  324. ret = drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  325. if (ret)
  326. goto err_g3;
  327. }
  328. list_add_tail(&dev->driver_item, &driver->device_list);
  329. DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
  330. driver->name, driver->major, driver->minor, driver->patchlevel,
  331. driver->date, dev->primary->index);
  332. return 0;
  333. err_g3:
  334. drm_put_minor(&dev->primary);
  335. err_g2:
  336. pci_disable_device(pdev);
  337. err_g1:
  338. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  339. return ret;
  340. }
  341. /**
  342. * Put a device minor number.
  343. *
  344. * \param dev device data structure
  345. * \return always zero
  346. *
  347. * Cleans up the proc resources. If it is the last minor then release the foreign
  348. * "drm" data, otherwise unregisters the "drm" data, frees the dev list and
  349. * unregisters the character device.
  350. */
  351. int drm_put_dev(struct drm_device * dev)
  352. {
  353. DRM_DEBUG("release primary %s\n", dev->driver->pci_driver.name);
  354. if (dev->devname) {
  355. drm_free(dev->devname, strlen(dev->devname) + 1,
  356. DRM_MEM_DRIVER);
  357. dev->devname = NULL;
  358. }
  359. drm_free(dev, sizeof(*dev), DRM_MEM_STUB);
  360. return 0;
  361. }
  362. /**
  363. * Put a secondary minor number.
  364. *
  365. * \param sec_minor - structure to be released
  366. * \return always zero
  367. *
  368. * Cleans up the proc resources. Not legal for this to be the
  369. * last minor released.
  370. *
  371. */
  372. int drm_put_minor(struct drm_minor **minor_p)
  373. {
  374. struct drm_minor *minor = *minor_p;
  375. DRM_DEBUG("release secondary minor %d\n", minor->index);
  376. if (minor->type == DRM_MINOR_LEGACY)
  377. drm_proc_cleanup(minor, drm_proc_root);
  378. drm_sysfs_device_remove(minor);
  379. idr_remove(&drm_minors_idr, minor->index);
  380. kfree(minor);
  381. *minor_p = NULL;
  382. return 0;
  383. }