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 <linux/slab.h>
  35. #include "drmP.h"
  36. #include "drm_core.h"
  37. unsigned int drm_debug = 0; /* 1 to enable debug output */
  38. EXPORT_SYMBOL(drm_debug);
  39. MODULE_AUTHOR(CORE_AUTHOR);
  40. MODULE_DESCRIPTION(CORE_DESC);
  41. MODULE_LICENSE("GPL and additional rights");
  42. MODULE_PARM_DESC(debug, "Enable debug output");
  43. module_param_named(debug, drm_debug, int, 0600);
  44. struct idr drm_minors_idr;
  45. struct class *drm_class;
  46. struct proc_dir_entry *drm_proc_root;
  47. struct dentry *drm_debugfs_root;
  48. void drm_ut_debug_printk(unsigned int request_level,
  49. const char *prefix,
  50. const char *function_name,
  51. const char *format, ...)
  52. {
  53. va_list args;
  54. if (drm_debug & request_level) {
  55. if (function_name)
  56. printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
  57. va_start(args, format);
  58. vprintk(format, args);
  59. va_end(args);
  60. }
  61. }
  62. EXPORT_SYMBOL(drm_ut_debug_printk);
  63. static int drm_minor_get_id(struct drm_device *dev, int type)
  64. {
  65. int new_id;
  66. int ret;
  67. int base = 0, limit = 63;
  68. if (type == DRM_MINOR_CONTROL) {
  69. base += 64;
  70. limit = base + 127;
  71. } else if (type == DRM_MINOR_RENDER) {
  72. base += 128;
  73. limit = base + 255;
  74. }
  75. again:
  76. if (idr_pre_get(&drm_minors_idr, GFP_KERNEL) == 0) {
  77. DRM_ERROR("Out of memory expanding drawable idr\n");
  78. return -ENOMEM;
  79. }
  80. mutex_lock(&dev->struct_mutex);
  81. ret = idr_get_new_above(&drm_minors_idr, NULL,
  82. base, &new_id);
  83. mutex_unlock(&dev->struct_mutex);
  84. if (ret == -EAGAIN) {
  85. goto again;
  86. } else if (ret) {
  87. return ret;
  88. }
  89. if (new_id >= limit) {
  90. idr_remove(&drm_minors_idr, new_id);
  91. return -EINVAL;
  92. }
  93. return new_id;
  94. }
  95. struct drm_master *drm_master_create(struct drm_minor *minor)
  96. {
  97. struct drm_master *master;
  98. master = kzalloc(sizeof(*master), GFP_KERNEL);
  99. if (!master)
  100. return NULL;
  101. kref_init(&master->refcount);
  102. spin_lock_init(&master->lock.spinlock);
  103. init_waitqueue_head(&master->lock.lock_queue);
  104. drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
  105. INIT_LIST_HEAD(&master->magicfree);
  106. master->minor = minor;
  107. list_add_tail(&master->head, &minor->master_list);
  108. return master;
  109. }
  110. struct drm_master *drm_master_get(struct drm_master *master)
  111. {
  112. kref_get(&master->refcount);
  113. return master;
  114. }
  115. EXPORT_SYMBOL(drm_master_get);
  116. static void drm_master_destroy(struct kref *kref)
  117. {
  118. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  119. struct drm_magic_entry *pt, *next;
  120. struct drm_device *dev = master->minor->dev;
  121. struct drm_map_list *r_list, *list_temp;
  122. list_del(&master->head);
  123. if (dev->driver->master_destroy)
  124. dev->driver->master_destroy(dev, master);
  125. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  126. if (r_list->master == master) {
  127. drm_rmmap_locked(dev, r_list->map);
  128. r_list = NULL;
  129. }
  130. }
  131. if (master->unique) {
  132. kfree(master->unique);
  133. master->unique = NULL;
  134. master->unique_len = 0;
  135. }
  136. kfree(dev->devname);
  137. dev->devname = NULL;
  138. list_for_each_entry_safe(pt, next, &master->magicfree, head) {
  139. list_del(&pt->head);
  140. drm_ht_remove_item(&master->magiclist, &pt->hash_item);
  141. kfree(pt);
  142. }
  143. drm_ht_remove(&master->magiclist);
  144. kfree(master);
  145. }
  146. void drm_master_put(struct drm_master **master)
  147. {
  148. kref_put(&(*master)->refcount, drm_master_destroy);
  149. *master = NULL;
  150. }
  151. EXPORT_SYMBOL(drm_master_put);
  152. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  153. struct drm_file *file_priv)
  154. {
  155. int ret = 0;
  156. if (file_priv->is_master)
  157. return 0;
  158. if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
  159. return -EINVAL;
  160. if (!file_priv->master)
  161. return -EINVAL;
  162. if (!file_priv->minor->master &&
  163. file_priv->minor->master != file_priv->master) {
  164. mutex_lock(&dev->struct_mutex);
  165. file_priv->minor->master = drm_master_get(file_priv->master);
  166. file_priv->is_master = 1;
  167. if (dev->driver->master_set) {
  168. ret = dev->driver->master_set(dev, file_priv, false);
  169. if (unlikely(ret != 0)) {
  170. file_priv->is_master = 0;
  171. drm_master_put(&file_priv->minor->master);
  172. }
  173. }
  174. mutex_unlock(&dev->struct_mutex);
  175. }
  176. return 0;
  177. }
  178. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  179. struct drm_file *file_priv)
  180. {
  181. if (!file_priv->is_master)
  182. return -EINVAL;
  183. if (!file_priv->minor->master)
  184. return -EINVAL;
  185. mutex_lock(&dev->struct_mutex);
  186. if (dev->driver->master_drop)
  187. dev->driver->master_drop(dev, file_priv, false);
  188. drm_master_put(&file_priv->minor->master);
  189. file_priv->is_master = 0;
  190. mutex_unlock(&dev->struct_mutex);
  191. return 0;
  192. }
  193. int drm_fill_in_dev(struct drm_device *dev,
  194. const struct pci_device_id *ent,
  195. struct drm_driver *driver)
  196. {
  197. int retcode;
  198. INIT_LIST_HEAD(&dev->filelist);
  199. INIT_LIST_HEAD(&dev->ctxlist);
  200. INIT_LIST_HEAD(&dev->vmalist);
  201. INIT_LIST_HEAD(&dev->maplist);
  202. INIT_LIST_HEAD(&dev->vblank_event_list);
  203. spin_lock_init(&dev->count_lock);
  204. spin_lock_init(&dev->event_lock);
  205. mutex_init(&dev->struct_mutex);
  206. mutex_init(&dev->ctxlist_mutex);
  207. if (drm_ht_create(&dev->map_hash, 12)) {
  208. return -ENOMEM;
  209. }
  210. /* the DRM has 6 basic counters */
  211. dev->counters = 6;
  212. dev->types[0] = _DRM_STAT_LOCK;
  213. dev->types[1] = _DRM_STAT_OPENS;
  214. dev->types[2] = _DRM_STAT_CLOSES;
  215. dev->types[3] = _DRM_STAT_IOCTLS;
  216. dev->types[4] = _DRM_STAT_LOCKS;
  217. dev->types[5] = _DRM_STAT_UNLOCKS;
  218. dev->driver = driver;
  219. if (drm_core_has_AGP(dev)) {
  220. if (drm_device_is_agp(dev))
  221. dev->agp = drm_agp_init(dev);
  222. if (drm_core_check_feature(dev, DRIVER_REQUIRE_AGP)
  223. && (dev->agp == NULL)) {
  224. DRM_ERROR("Cannot initialize the agpgart module.\n");
  225. retcode = -EINVAL;
  226. goto error_out_unreg;
  227. }
  228. if (drm_core_has_MTRR(dev)) {
  229. if (dev->agp)
  230. dev->agp->agp_mtrr =
  231. mtrr_add(dev->agp->agp_info.aper_base,
  232. dev->agp->agp_info.aper_size *
  233. 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
  234. }
  235. }
  236. retcode = drm_ctxbitmap_init(dev);
  237. if (retcode) {
  238. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  239. goto error_out_unreg;
  240. }
  241. if (driver->driver_features & DRIVER_GEM) {
  242. retcode = drm_gem_init(dev);
  243. if (retcode) {
  244. DRM_ERROR("Cannot initialize graphics execution "
  245. "manager (GEM)\n");
  246. goto error_out_unreg;
  247. }
  248. }
  249. return 0;
  250. error_out_unreg:
  251. drm_lastclose(dev);
  252. return retcode;
  253. }
  254. /**
  255. * Get a secondary minor number.
  256. *
  257. * \param dev device data structure
  258. * \param sec-minor structure to hold the assigned minor
  259. * \return negative number on failure.
  260. *
  261. * Search an empty entry and initialize it to the given parameters, and
  262. * create the proc init entry via proc_init(). This routines assigns
  263. * minor numbers to secondary heads of multi-headed cards
  264. */
  265. int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
  266. {
  267. struct drm_minor *new_minor;
  268. int ret;
  269. int minor_id;
  270. DRM_DEBUG("\n");
  271. minor_id = drm_minor_get_id(dev, type);
  272. if (minor_id < 0)
  273. return minor_id;
  274. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  275. if (!new_minor) {
  276. ret = -ENOMEM;
  277. goto err_idr;
  278. }
  279. new_minor->type = type;
  280. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  281. new_minor->dev = dev;
  282. new_minor->index = minor_id;
  283. INIT_LIST_HEAD(&new_minor->master_list);
  284. idr_replace(&drm_minors_idr, new_minor, minor_id);
  285. if (type == DRM_MINOR_LEGACY) {
  286. ret = drm_proc_init(new_minor, minor_id, drm_proc_root);
  287. if (ret) {
  288. DRM_ERROR("DRM: Failed to initialize /proc/dri.\n");
  289. goto err_mem;
  290. }
  291. } else
  292. new_minor->proc_root = NULL;
  293. #if defined(CONFIG_DEBUG_FS)
  294. ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
  295. if (ret) {
  296. DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  297. goto err_g2;
  298. }
  299. #endif
  300. ret = drm_sysfs_device_add(new_minor);
  301. if (ret) {
  302. printk(KERN_ERR
  303. "DRM: Error sysfs_device_add.\n");
  304. goto err_g2;
  305. }
  306. *minor = new_minor;
  307. DRM_DEBUG("new minor assigned %d\n", minor_id);
  308. return 0;
  309. err_g2:
  310. if (new_minor->type == DRM_MINOR_LEGACY)
  311. drm_proc_cleanup(new_minor, drm_proc_root);
  312. err_mem:
  313. kfree(new_minor);
  314. err_idr:
  315. idr_remove(&drm_minors_idr, minor_id);
  316. *minor = NULL;
  317. return ret;
  318. }
  319. /**
  320. * Put a secondary minor number.
  321. *
  322. * \param sec_minor - structure to be released
  323. * \return always zero
  324. *
  325. * Cleans up the proc resources. Not legal for this to be the
  326. * last minor released.
  327. *
  328. */
  329. int drm_put_minor(struct drm_minor **minor_p)
  330. {
  331. struct drm_minor *minor = *minor_p;
  332. DRM_DEBUG("release secondary minor %d\n", minor->index);
  333. if (minor->type == DRM_MINOR_LEGACY)
  334. drm_proc_cleanup(minor, drm_proc_root);
  335. #if defined(CONFIG_DEBUG_FS)
  336. drm_debugfs_cleanup(minor);
  337. #endif
  338. drm_sysfs_device_remove(minor);
  339. idr_remove(&drm_minors_idr, minor->index);
  340. kfree(minor);
  341. *minor_p = NULL;
  342. return 0;
  343. }
  344. /**
  345. * Called via drm_exit() at module unload time or when pci device is
  346. * unplugged.
  347. *
  348. * Cleans up all DRM device, calling drm_lastclose().
  349. *
  350. * \sa drm_init
  351. */
  352. void drm_put_dev(struct drm_device *dev)
  353. {
  354. struct drm_driver *driver;
  355. struct drm_map_list *r_list, *list_temp;
  356. DRM_DEBUG("\n");
  357. if (!dev) {
  358. DRM_ERROR("cleanup called no dev\n");
  359. return;
  360. }
  361. driver = dev->driver;
  362. drm_lastclose(dev);
  363. if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
  364. dev->agp && dev->agp->agp_mtrr >= 0) {
  365. int retval;
  366. retval = mtrr_del(dev->agp->agp_mtrr,
  367. dev->agp->agp_info.aper_base,
  368. dev->agp->agp_info.aper_size * 1024 * 1024);
  369. DRM_DEBUG("mtrr_del=%d\n", retval);
  370. }
  371. if (dev->driver->unload)
  372. dev->driver->unload(dev);
  373. if (drm_core_has_AGP(dev) && dev->agp) {
  374. kfree(dev->agp);
  375. dev->agp = NULL;
  376. }
  377. drm_vblank_cleanup(dev);
  378. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  379. drm_rmmap(dev, r_list->map);
  380. drm_ht_remove(&dev->map_hash);
  381. drm_ctxbitmap_cleanup(dev);
  382. if (drm_core_check_feature(dev, DRIVER_MODESET))
  383. drm_put_minor(&dev->control);
  384. if (driver->driver_features & DRIVER_GEM)
  385. drm_gem_destroy(dev);
  386. drm_put_minor(&dev->primary);
  387. if (dev->devname) {
  388. kfree(dev->devname);
  389. dev->devname = NULL;
  390. }
  391. kfree(dev);
  392. }
  393. EXPORT_SYMBOL(drm_put_dev);