drm_stub.c 11 KB

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