drm_stub.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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_rnodes = 0; /* 1 to enable experimental render nodes API */
  40. EXPORT_SYMBOL(drm_rnodes);
  41. unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
  42. EXPORT_SYMBOL(drm_vblank_offdelay);
  43. unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
  44. EXPORT_SYMBOL(drm_timestamp_precision);
  45. /*
  46. * Default to use monotonic timestamps for wait-for-vblank and page-flip
  47. * complete events.
  48. */
  49. unsigned int drm_timestamp_monotonic = 1;
  50. MODULE_AUTHOR(CORE_AUTHOR);
  51. MODULE_DESCRIPTION(CORE_DESC);
  52. MODULE_LICENSE("GPL and additional rights");
  53. MODULE_PARM_DESC(debug, "Enable debug output");
  54. MODULE_PARM_DESC(rnodes, "Enable experimental render nodes API");
  55. MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
  56. MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
  57. MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
  58. module_param_named(debug, drm_debug, int, 0600);
  59. module_param_named(rnodes, drm_rnodes, int, 0600);
  60. module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
  61. module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
  62. module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
  63. struct idr drm_minors_idr;
  64. struct class *drm_class;
  65. struct dentry *drm_debugfs_root;
  66. int drm_err(const char *func, const char *format, ...)
  67. {
  68. struct va_format vaf;
  69. va_list args;
  70. int r;
  71. va_start(args, format);
  72. vaf.fmt = format;
  73. vaf.va = &args;
  74. r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
  75. va_end(args);
  76. return r;
  77. }
  78. EXPORT_SYMBOL(drm_err);
  79. void drm_ut_debug_printk(unsigned int request_level,
  80. const char *prefix,
  81. const char *function_name,
  82. const char *format, ...)
  83. {
  84. va_list args;
  85. if (drm_debug & request_level) {
  86. if (function_name)
  87. printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
  88. va_start(args, format);
  89. vprintk(format, args);
  90. va_end(args);
  91. }
  92. }
  93. EXPORT_SYMBOL(drm_ut_debug_printk);
  94. static int drm_minor_get_id(struct drm_device *dev, int type)
  95. {
  96. int ret;
  97. int base = 0, limit = 63;
  98. if (type == DRM_MINOR_CONTROL) {
  99. base += 64;
  100. limit = base + 63;
  101. } else if (type == DRM_MINOR_RENDER) {
  102. base += 128;
  103. limit = base + 63;
  104. }
  105. mutex_lock(&dev->struct_mutex);
  106. ret = idr_alloc(&drm_minors_idr, NULL, base, limit, GFP_KERNEL);
  107. mutex_unlock(&dev->struct_mutex);
  108. return ret == -ENOSPC ? -EINVAL : ret;
  109. }
  110. struct drm_master *drm_master_create(struct drm_minor *minor)
  111. {
  112. struct drm_master *master;
  113. master = kzalloc(sizeof(*master), GFP_KERNEL);
  114. if (!master)
  115. return NULL;
  116. kref_init(&master->refcount);
  117. spin_lock_init(&master->lock.spinlock);
  118. init_waitqueue_head(&master->lock.lock_queue);
  119. drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
  120. INIT_LIST_HEAD(&master->magicfree);
  121. master->minor = minor;
  122. list_add_tail(&master->head, &minor->master_list);
  123. return master;
  124. }
  125. struct drm_master *drm_master_get(struct drm_master *master)
  126. {
  127. kref_get(&master->refcount);
  128. return master;
  129. }
  130. EXPORT_SYMBOL(drm_master_get);
  131. static void drm_master_destroy(struct kref *kref)
  132. {
  133. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  134. struct drm_magic_entry *pt, *next;
  135. struct drm_device *dev = master->minor->dev;
  136. struct drm_map_list *r_list, *list_temp;
  137. list_del(&master->head);
  138. if (dev->driver->master_destroy)
  139. dev->driver->master_destroy(dev, master);
  140. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  141. if (r_list->master == master) {
  142. drm_rmmap_locked(dev, r_list->map);
  143. r_list = NULL;
  144. }
  145. }
  146. if (master->unique) {
  147. kfree(master->unique);
  148. master->unique = NULL;
  149. master->unique_len = 0;
  150. }
  151. kfree(dev->devname);
  152. dev->devname = NULL;
  153. list_for_each_entry_safe(pt, next, &master->magicfree, head) {
  154. list_del(&pt->head);
  155. drm_ht_remove_item(&master->magiclist, &pt->hash_item);
  156. kfree(pt);
  157. }
  158. drm_ht_remove(&master->magiclist);
  159. kfree(master);
  160. }
  161. void drm_master_put(struct drm_master **master)
  162. {
  163. kref_put(&(*master)->refcount, drm_master_destroy);
  164. *master = NULL;
  165. }
  166. EXPORT_SYMBOL(drm_master_put);
  167. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  168. struct drm_file *file_priv)
  169. {
  170. int ret = 0;
  171. if (file_priv->is_master)
  172. return 0;
  173. if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
  174. return -EINVAL;
  175. if (!file_priv->master)
  176. return -EINVAL;
  177. if (file_priv->minor->master)
  178. return -EINVAL;
  179. mutex_lock(&dev->struct_mutex);
  180. file_priv->minor->master = drm_master_get(file_priv->master);
  181. file_priv->is_master = 1;
  182. if (dev->driver->master_set) {
  183. ret = dev->driver->master_set(dev, file_priv, false);
  184. if (unlikely(ret != 0)) {
  185. file_priv->is_master = 0;
  186. drm_master_put(&file_priv->minor->master);
  187. }
  188. }
  189. mutex_unlock(&dev->struct_mutex);
  190. return ret;
  191. }
  192. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  193. struct drm_file *file_priv)
  194. {
  195. if (!file_priv->is_master)
  196. return -EINVAL;
  197. if (!file_priv->minor->master)
  198. return -EINVAL;
  199. mutex_lock(&dev->struct_mutex);
  200. if (dev->driver->master_drop)
  201. dev->driver->master_drop(dev, file_priv, false);
  202. drm_master_put(&file_priv->minor->master);
  203. file_priv->is_master = 0;
  204. mutex_unlock(&dev->struct_mutex);
  205. return 0;
  206. }
  207. /**
  208. * Get a secondary minor number.
  209. *
  210. * \param dev device data structure
  211. * \param sec-minor structure to hold the assigned minor
  212. * \return negative number on failure.
  213. *
  214. * Search an empty entry and initialize it to the given parameters. This
  215. * routines assigns minor numbers to secondary heads of multi-headed cards
  216. */
  217. int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type)
  218. {
  219. struct drm_minor *new_minor;
  220. int ret;
  221. int minor_id;
  222. DRM_DEBUG("\n");
  223. minor_id = drm_minor_get_id(dev, type);
  224. if (minor_id < 0)
  225. return minor_id;
  226. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  227. if (!new_minor) {
  228. ret = -ENOMEM;
  229. goto err_idr;
  230. }
  231. new_minor->type = type;
  232. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  233. new_minor->dev = dev;
  234. new_minor->index = minor_id;
  235. INIT_LIST_HEAD(&new_minor->master_list);
  236. idr_replace(&drm_minors_idr, new_minor, minor_id);
  237. #if defined(CONFIG_DEBUG_FS)
  238. ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
  239. if (ret) {
  240. DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  241. goto err_mem;
  242. }
  243. #endif
  244. ret = drm_sysfs_device_add(new_minor);
  245. if (ret) {
  246. printk(KERN_ERR
  247. "DRM: Error sysfs_device_add.\n");
  248. goto err_debugfs;
  249. }
  250. *minor = new_minor;
  251. DRM_DEBUG("new minor assigned %d\n", minor_id);
  252. return 0;
  253. err_debugfs:
  254. #if defined(CONFIG_DEBUG_FS)
  255. drm_debugfs_cleanup(new_minor);
  256. err_mem:
  257. #endif
  258. kfree(new_minor);
  259. err_idr:
  260. idr_remove(&drm_minors_idr, minor_id);
  261. *minor = NULL;
  262. return ret;
  263. }
  264. EXPORT_SYMBOL(drm_get_minor);
  265. /**
  266. * Put a secondary minor number.
  267. *
  268. * \param sec_minor - structure to be released
  269. * \return always zero
  270. */
  271. int drm_put_minor(struct drm_minor **minor_p)
  272. {
  273. struct drm_minor *minor = *minor_p;
  274. DRM_DEBUG("release secondary minor %d\n", minor->index);
  275. #if defined(CONFIG_DEBUG_FS)
  276. drm_debugfs_cleanup(minor);
  277. #endif
  278. drm_sysfs_device_remove(minor);
  279. idr_remove(&drm_minors_idr, minor->index);
  280. kfree(minor);
  281. *minor_p = NULL;
  282. return 0;
  283. }
  284. EXPORT_SYMBOL(drm_put_minor);
  285. static void drm_unplug_minor(struct drm_minor *minor)
  286. {
  287. drm_sysfs_device_remove(minor);
  288. }
  289. /**
  290. * Called via drm_exit() at module unload time or when pci device is
  291. * unplugged.
  292. *
  293. * Cleans up all DRM device, calling drm_lastclose().
  294. *
  295. */
  296. void drm_put_dev(struct drm_device *dev)
  297. {
  298. DRM_DEBUG("\n");
  299. if (!dev) {
  300. DRM_ERROR("cleanup called no dev\n");
  301. return;
  302. }
  303. drm_dev_unregister(dev);
  304. drm_dev_free(dev);
  305. }
  306. EXPORT_SYMBOL(drm_put_dev);
  307. void drm_unplug_dev(struct drm_device *dev)
  308. {
  309. /* for a USB device */
  310. if (drm_core_check_feature(dev, DRIVER_MODESET))
  311. drm_unplug_minor(dev->control);
  312. if (dev->render)
  313. drm_unplug_minor(dev->render);
  314. drm_unplug_minor(dev->primary);
  315. mutex_lock(&drm_global_mutex);
  316. drm_device_set_unplugged(dev);
  317. if (dev->open_count == 0) {
  318. drm_put_dev(dev);
  319. }
  320. mutex_unlock(&drm_global_mutex);
  321. }
  322. EXPORT_SYMBOL(drm_unplug_dev);
  323. /**
  324. * drm_dev_alloc - Allocate new drm device
  325. * @driver: DRM driver to allocate device for
  326. * @parent: Parent device object
  327. *
  328. * Allocate and initialize a new DRM device. No device registration is done.
  329. * Call drm_dev_register() to advertice the device to user space and register it
  330. * with other core subsystems.
  331. *
  332. * RETURNS:
  333. * Pointer to new DRM device, or NULL if out of memory.
  334. */
  335. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  336. struct device *parent)
  337. {
  338. struct drm_device *dev;
  339. int ret;
  340. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  341. if (!dev)
  342. return NULL;
  343. dev->dev = parent;
  344. dev->driver = driver;
  345. INIT_LIST_HEAD(&dev->filelist);
  346. INIT_LIST_HEAD(&dev->ctxlist);
  347. INIT_LIST_HEAD(&dev->vmalist);
  348. INIT_LIST_HEAD(&dev->maplist);
  349. INIT_LIST_HEAD(&dev->vblank_event_list);
  350. spin_lock_init(&dev->count_lock);
  351. spin_lock_init(&dev->event_lock);
  352. mutex_init(&dev->struct_mutex);
  353. mutex_init(&dev->ctxlist_mutex);
  354. if (drm_ht_create(&dev->map_hash, 12))
  355. goto err_free;
  356. ret = drm_ctxbitmap_init(dev);
  357. if (ret) {
  358. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  359. goto err_ht;
  360. }
  361. if (driver->driver_features & DRIVER_GEM) {
  362. ret = drm_gem_init(dev);
  363. if (ret) {
  364. DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  365. goto err_ctxbitmap;
  366. }
  367. }
  368. return dev;
  369. err_ctxbitmap:
  370. drm_ctxbitmap_cleanup(dev);
  371. err_ht:
  372. drm_ht_remove(&dev->map_hash);
  373. err_free:
  374. kfree(dev);
  375. return NULL;
  376. }
  377. EXPORT_SYMBOL(drm_dev_alloc);
  378. /**
  379. * drm_dev_free - Free DRM device
  380. * @dev: DRM device to free
  381. *
  382. * Free a DRM device that has previously been allocated via drm_dev_alloc().
  383. * You must not use kfree() instead or you will leak memory.
  384. *
  385. * This must not be called once the device got registered. Use drm_put_dev()
  386. * instead, which then calls drm_dev_free().
  387. */
  388. void drm_dev_free(struct drm_device *dev)
  389. {
  390. if (dev->driver->driver_features & DRIVER_GEM)
  391. drm_gem_destroy(dev);
  392. drm_ctxbitmap_cleanup(dev);
  393. drm_ht_remove(&dev->map_hash);
  394. kfree(dev->devname);
  395. kfree(dev);
  396. }
  397. EXPORT_SYMBOL(drm_dev_free);
  398. /**
  399. * drm_dev_register - Register DRM device
  400. * @dev: Device to register
  401. *
  402. * Register the DRM device @dev with the system, advertise device to user-space
  403. * and start normal device operation. @dev must be allocated via drm_dev_alloc()
  404. * previously.
  405. *
  406. * Never call this twice on any device!
  407. *
  408. * RETURNS:
  409. * 0 on success, negative error code on failure.
  410. */
  411. int drm_dev_register(struct drm_device *dev, unsigned long flags)
  412. {
  413. int ret;
  414. mutex_lock(&drm_global_mutex);
  415. if (dev->driver->bus->agp_init) {
  416. ret = dev->driver->bus->agp_init(dev);
  417. if (ret)
  418. goto out_unlock;
  419. }
  420. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  421. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  422. if (ret)
  423. goto err_agp;
  424. }
  425. if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
  426. ret = drm_get_minor(dev, &dev->render, DRM_MINOR_RENDER);
  427. if (ret)
  428. goto err_control_node;
  429. }
  430. ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
  431. if (ret)
  432. goto err_render_node;
  433. if (dev->driver->load) {
  434. ret = dev->driver->load(dev, flags);
  435. if (ret)
  436. goto err_primary_node;
  437. }
  438. /* setup grouping for legacy outputs */
  439. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  440. ret = drm_mode_group_init_legacy_group(dev,
  441. &dev->primary->mode_group);
  442. if (ret)
  443. goto err_unload;
  444. }
  445. list_add_tail(&dev->driver_item, &dev->driver->device_list);
  446. ret = 0;
  447. goto out_unlock;
  448. err_unload:
  449. if (dev->driver->unload)
  450. dev->driver->unload(dev);
  451. err_primary_node:
  452. drm_put_minor(&dev->primary);
  453. err_render_node:
  454. if (dev->render)
  455. drm_put_minor(&dev->render);
  456. err_control_node:
  457. if (dev->control)
  458. drm_put_minor(&dev->control);
  459. err_agp:
  460. if (dev->driver->bus->agp_destroy)
  461. dev->driver->bus->agp_destroy(dev);
  462. out_unlock:
  463. mutex_unlock(&drm_global_mutex);
  464. return ret;
  465. }
  466. EXPORT_SYMBOL(drm_dev_register);
  467. /**
  468. * drm_dev_unregister - Unregister DRM device
  469. * @dev: Device to unregister
  470. *
  471. * Unregister the DRM device from the system. This does the reverse of
  472. * drm_dev_register() but does not deallocate the device. The caller must call
  473. * drm_dev_free() to free all resources.
  474. */
  475. void drm_dev_unregister(struct drm_device *dev)
  476. {
  477. struct drm_map_list *r_list, *list_temp;
  478. drm_lastclose(dev);
  479. if (dev->driver->unload)
  480. dev->driver->unload(dev);
  481. if (dev->driver->bus->agp_destroy)
  482. dev->driver->bus->agp_destroy(dev);
  483. drm_vblank_cleanup(dev);
  484. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  485. drm_rmmap(dev, r_list->map);
  486. if (dev->control)
  487. drm_put_minor(&dev->control);
  488. if (dev->render)
  489. drm_put_minor(&dev->render);
  490. drm_put_minor(&dev->primary);
  491. list_del(&dev->driver_item);
  492. }
  493. EXPORT_SYMBOL(drm_dev_unregister);