drm_stub.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 proc_dir_entry *drm_proc_root;
  62. struct dentry *drm_debugfs_root;
  63. int drm_err(const char *func, const char *format, ...)
  64. {
  65. struct va_format vaf;
  66. va_list args;
  67. int r;
  68. va_start(args, format);
  69. vaf.fmt = format;
  70. vaf.va = &args;
  71. r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
  72. va_end(args);
  73. return r;
  74. }
  75. EXPORT_SYMBOL(drm_err);
  76. void drm_ut_debug_printk(unsigned int request_level,
  77. const char *prefix,
  78. const char *function_name,
  79. const char *format, ...)
  80. {
  81. va_list args;
  82. if (drm_debug & request_level) {
  83. if (function_name)
  84. printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
  85. va_start(args, format);
  86. vprintk(format, args);
  87. va_end(args);
  88. }
  89. }
  90. EXPORT_SYMBOL(drm_ut_debug_printk);
  91. static int drm_minor_get_id(struct drm_device *dev, int type)
  92. {
  93. int ret;
  94. int base = 0, limit = 63;
  95. if (type == DRM_MINOR_CONTROL) {
  96. base += 64;
  97. limit = base + 127;
  98. } else if (type == DRM_MINOR_RENDER) {
  99. base += 128;
  100. limit = base + 255;
  101. }
  102. mutex_lock(&dev->struct_mutex);
  103. ret = idr_alloc(&drm_minors_idr, NULL, base, limit, GFP_KERNEL);
  104. mutex_unlock(&dev->struct_mutex);
  105. return ret == -ENOSPC ? -EINVAL : ret;
  106. }
  107. struct drm_master *drm_master_create(struct drm_minor *minor)
  108. {
  109. struct drm_master *master;
  110. master = kzalloc(sizeof(*master), GFP_KERNEL);
  111. if (!master)
  112. return NULL;
  113. kref_init(&master->refcount);
  114. spin_lock_init(&master->lock.spinlock);
  115. init_waitqueue_head(&master->lock.lock_queue);
  116. drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
  117. INIT_LIST_HEAD(&master->magicfree);
  118. master->minor = minor;
  119. list_add_tail(&master->head, &minor->master_list);
  120. return master;
  121. }
  122. struct drm_master *drm_master_get(struct drm_master *master)
  123. {
  124. kref_get(&master->refcount);
  125. return master;
  126. }
  127. EXPORT_SYMBOL(drm_master_get);
  128. static void drm_master_destroy(struct kref *kref)
  129. {
  130. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  131. struct drm_magic_entry *pt, *next;
  132. struct drm_device *dev = master->minor->dev;
  133. struct drm_map_list *r_list, *list_temp;
  134. list_del(&master->head);
  135. if (dev->driver->master_destroy)
  136. dev->driver->master_destroy(dev, master);
  137. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  138. if (r_list->master == master) {
  139. drm_rmmap_locked(dev, r_list->map);
  140. r_list = NULL;
  141. }
  142. }
  143. if (master->unique) {
  144. kfree(master->unique);
  145. master->unique = NULL;
  146. master->unique_len = 0;
  147. }
  148. kfree(dev->devname);
  149. dev->devname = NULL;
  150. list_for_each_entry_safe(pt, next, &master->magicfree, head) {
  151. list_del(&pt->head);
  152. drm_ht_remove_item(&master->magiclist, &pt->hash_item);
  153. kfree(pt);
  154. }
  155. drm_ht_remove(&master->magiclist);
  156. kfree(master);
  157. }
  158. void drm_master_put(struct drm_master **master)
  159. {
  160. kref_put(&(*master)->refcount, drm_master_destroy);
  161. *master = NULL;
  162. }
  163. EXPORT_SYMBOL(drm_master_put);
  164. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  165. struct drm_file *file_priv)
  166. {
  167. int ret;
  168. if (file_priv->is_master)
  169. return 0;
  170. if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
  171. return -EINVAL;
  172. if (!file_priv->master)
  173. return -EINVAL;
  174. if (file_priv->minor->master)
  175. return -EINVAL;
  176. mutex_lock(&dev->struct_mutex);
  177. file_priv->minor->master = drm_master_get(file_priv->master);
  178. file_priv->is_master = 1;
  179. if (dev->driver->master_set) {
  180. ret = dev->driver->master_set(dev, file_priv, false);
  181. if (unlikely(ret != 0)) {
  182. file_priv->is_master = 0;
  183. drm_master_put(&file_priv->minor->master);
  184. }
  185. }
  186. mutex_unlock(&dev->struct_mutex);
  187. return 0;
  188. }
  189. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  190. struct drm_file *file_priv)
  191. {
  192. if (!file_priv->is_master)
  193. return -EINVAL;
  194. if (!file_priv->minor->master)
  195. return -EINVAL;
  196. mutex_lock(&dev->struct_mutex);
  197. if (dev->driver->master_drop)
  198. dev->driver->master_drop(dev, file_priv, false);
  199. drm_master_put(&file_priv->minor->master);
  200. file_priv->is_master = 0;
  201. mutex_unlock(&dev->struct_mutex);
  202. return 0;
  203. }
  204. int drm_fill_in_dev(struct drm_device *dev,
  205. const struct pci_device_id *ent,
  206. struct drm_driver *driver)
  207. {
  208. int retcode;
  209. INIT_LIST_HEAD(&dev->filelist);
  210. INIT_LIST_HEAD(&dev->ctxlist);
  211. INIT_LIST_HEAD(&dev->vmalist);
  212. INIT_LIST_HEAD(&dev->maplist);
  213. INIT_LIST_HEAD(&dev->vblank_event_list);
  214. spin_lock_init(&dev->count_lock);
  215. spin_lock_init(&dev->event_lock);
  216. mutex_init(&dev->struct_mutex);
  217. mutex_init(&dev->ctxlist_mutex);
  218. if (drm_ht_create(&dev->map_hash, 12)) {
  219. return -ENOMEM;
  220. }
  221. /* the DRM has 6 basic counters */
  222. dev->counters = 6;
  223. dev->types[0] = _DRM_STAT_LOCK;
  224. dev->types[1] = _DRM_STAT_OPENS;
  225. dev->types[2] = _DRM_STAT_CLOSES;
  226. dev->types[3] = _DRM_STAT_IOCTLS;
  227. dev->types[4] = _DRM_STAT_LOCKS;
  228. dev->types[5] = _DRM_STAT_UNLOCKS;
  229. dev->driver = driver;
  230. if (dev->driver->bus->agp_init) {
  231. retcode = dev->driver->bus->agp_init(dev);
  232. if (retcode)
  233. goto error_out_unreg;
  234. }
  235. retcode = drm_ctxbitmap_init(dev);
  236. if (retcode) {
  237. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  238. goto error_out_unreg;
  239. }
  240. if (driver->driver_features & DRIVER_GEM) {
  241. retcode = drm_gem_init(dev);
  242. if (retcode) {
  243. DRM_ERROR("Cannot initialize graphics execution "
  244. "manager (GEM)\n");
  245. goto error_out_unreg;
  246. }
  247. }
  248. return 0;
  249. error_out_unreg:
  250. drm_lastclose(dev);
  251. return retcode;
  252. }
  253. EXPORT_SYMBOL(drm_fill_in_dev);
  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. EXPORT_SYMBOL(drm_get_minor);
  320. /**
  321. * Put a secondary minor number.
  322. *
  323. * \param sec_minor - structure to be released
  324. * \return always zero
  325. *
  326. * Cleans up the proc resources. Not legal for this to be the
  327. * last minor released.
  328. *
  329. */
  330. int drm_put_minor(struct drm_minor **minor_p)
  331. {
  332. struct drm_minor *minor = *minor_p;
  333. DRM_DEBUG("release secondary minor %d\n", minor->index);
  334. if (minor->type == DRM_MINOR_LEGACY)
  335. drm_proc_cleanup(minor, drm_proc_root);
  336. #if defined(CONFIG_DEBUG_FS)
  337. drm_debugfs_cleanup(minor);
  338. #endif
  339. drm_sysfs_device_remove(minor);
  340. idr_remove(&drm_minors_idr, minor->index);
  341. kfree(minor);
  342. *minor_p = NULL;
  343. return 0;
  344. }
  345. EXPORT_SYMBOL(drm_put_minor);
  346. static void drm_unplug_minor(struct drm_minor *minor)
  347. {
  348. drm_sysfs_device_remove(minor);
  349. }
  350. /**
  351. * Called via drm_exit() at module unload time or when pci device is
  352. * unplugged.
  353. *
  354. * Cleans up all DRM device, calling drm_lastclose().
  355. *
  356. */
  357. void drm_put_dev(struct drm_device *dev)
  358. {
  359. struct drm_driver *driver;
  360. struct drm_map_list *r_list, *list_temp;
  361. DRM_DEBUG("\n");
  362. if (!dev) {
  363. DRM_ERROR("cleanup called no dev\n");
  364. return;
  365. }
  366. driver = dev->driver;
  367. drm_lastclose(dev);
  368. if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
  369. dev->agp && dev->agp->agp_mtrr >= 0) {
  370. int retval;
  371. retval = mtrr_del(dev->agp->agp_mtrr,
  372. dev->agp->agp_info.aper_base,
  373. dev->agp->agp_info.aper_size * 1024 * 1024);
  374. DRM_DEBUG("mtrr_del=%d\n", retval);
  375. }
  376. if (dev->driver->unload)
  377. dev->driver->unload(dev);
  378. if (drm_core_has_AGP(dev) && dev->agp) {
  379. kfree(dev->agp);
  380. dev->agp = NULL;
  381. }
  382. drm_vblank_cleanup(dev);
  383. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  384. drm_rmmap(dev, r_list->map);
  385. drm_ht_remove(&dev->map_hash);
  386. drm_ctxbitmap_cleanup(dev);
  387. if (drm_core_check_feature(dev, DRIVER_MODESET))
  388. drm_put_minor(&dev->control);
  389. if (driver->driver_features & DRIVER_GEM)
  390. drm_gem_destroy(dev);
  391. drm_put_minor(&dev->primary);
  392. list_del(&dev->driver_item);
  393. kfree(dev->devname);
  394. kfree(dev);
  395. }
  396. EXPORT_SYMBOL(drm_put_dev);
  397. void drm_unplug_dev(struct drm_device *dev)
  398. {
  399. /* for a USB device */
  400. if (drm_core_check_feature(dev, DRIVER_MODESET))
  401. drm_unplug_minor(dev->control);
  402. drm_unplug_minor(dev->primary);
  403. mutex_lock(&drm_global_mutex);
  404. drm_device_set_unplugged(dev);
  405. if (dev->open_count == 0) {
  406. drm_put_dev(dev);
  407. }
  408. mutex_unlock(&drm_global_mutex);
  409. }
  410. EXPORT_SYMBOL(drm_unplug_dev);