drm_stub.c 12 KB

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