drm_stub.c 11 KB

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