drm_stub.c 12 KB

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