drm_stub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. struct va_format vaf;
  85. va_list args;
  86. if (drm_debug & request_level) {
  87. va_start(args, format);
  88. vaf.fmt = format;
  89. vaf.va = &args;
  90. if (function_name)
  91. printk(KERN_DEBUG "[%s:%s], %pV", prefix,
  92. function_name, &vaf);
  93. else
  94. printk(KERN_DEBUG "%pV", &vaf);
  95. va_end(args);
  96. }
  97. }
  98. EXPORT_SYMBOL(drm_ut_debug_printk);
  99. static int drm_minor_get_id(struct drm_device *dev, int type)
  100. {
  101. int ret;
  102. int base = 0, limit = 63;
  103. if (type == DRM_MINOR_CONTROL) {
  104. base += 64;
  105. limit = base + 63;
  106. } else if (type == DRM_MINOR_RENDER) {
  107. base += 128;
  108. limit = base + 63;
  109. }
  110. mutex_lock(&dev->struct_mutex);
  111. ret = idr_alloc(&drm_minors_idr, NULL, base, limit, GFP_KERNEL);
  112. mutex_unlock(&dev->struct_mutex);
  113. return ret == -ENOSPC ? -EINVAL : ret;
  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 = 0;
  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. return -EINVAL;
  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. return ret;
  196. }
  197. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  198. struct drm_file *file_priv)
  199. {
  200. if (!file_priv->is_master)
  201. return -EINVAL;
  202. if (!file_priv->minor->master)
  203. return -EINVAL;
  204. mutex_lock(&dev->struct_mutex);
  205. if (dev->driver->master_drop)
  206. dev->driver->master_drop(dev, file_priv, false);
  207. drm_master_put(&file_priv->minor->master);
  208. file_priv->is_master = 0;
  209. mutex_unlock(&dev->struct_mutex);
  210. return 0;
  211. }
  212. /**
  213. * drm_get_minor - Allocate and register new DRM minor
  214. * @dev: DRM device
  215. * @minor: Pointer to where new minor is stored
  216. * @type: Type of minor
  217. *
  218. * Allocate a new minor of the given type and register it. A pointer to the new
  219. * minor is returned in @minor.
  220. * Caller must hold the global DRM mutex.
  221. *
  222. * RETURNS:
  223. * 0 on success, negative error code on failure.
  224. */
  225. static int drm_get_minor(struct drm_device *dev, struct drm_minor **minor,
  226. int type)
  227. {
  228. struct drm_minor *new_minor;
  229. int ret;
  230. int minor_id;
  231. DRM_DEBUG("\n");
  232. minor_id = drm_minor_get_id(dev, type);
  233. if (minor_id < 0)
  234. return minor_id;
  235. new_minor = kzalloc(sizeof(struct drm_minor), GFP_KERNEL);
  236. if (!new_minor) {
  237. ret = -ENOMEM;
  238. goto err_idr;
  239. }
  240. new_minor->type = type;
  241. new_minor->device = MKDEV(DRM_MAJOR, minor_id);
  242. new_minor->dev = dev;
  243. new_minor->index = minor_id;
  244. INIT_LIST_HEAD(&new_minor->master_list);
  245. idr_replace(&drm_minors_idr, new_minor, minor_id);
  246. #if defined(CONFIG_DEBUG_FS)
  247. ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
  248. if (ret) {
  249. DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  250. goto err_mem;
  251. }
  252. #endif
  253. ret = drm_sysfs_device_add(new_minor);
  254. if (ret) {
  255. printk(KERN_ERR
  256. "DRM: Error sysfs_device_add.\n");
  257. goto err_debugfs;
  258. }
  259. *minor = new_minor;
  260. DRM_DEBUG("new minor assigned %d\n", minor_id);
  261. return 0;
  262. err_debugfs:
  263. #if defined(CONFIG_DEBUG_FS)
  264. drm_debugfs_cleanup(new_minor);
  265. err_mem:
  266. #endif
  267. kfree(new_minor);
  268. err_idr:
  269. idr_remove(&drm_minors_idr, minor_id);
  270. *minor = NULL;
  271. return ret;
  272. }
  273. /**
  274. * drm_unplug_minor - Unplug DRM minor
  275. * @minor: Minor to unplug
  276. *
  277. * Unplugs the given DRM minor but keeps the object. So after this returns,
  278. * minor->dev is still valid so existing open-files can still access it to get
  279. * device information from their drm_file ojects.
  280. * If the minor is already unplugged or if @minor is NULL, nothing is done.
  281. * The global DRM mutex must be held by the caller.
  282. */
  283. static void drm_unplug_minor(struct drm_minor *minor)
  284. {
  285. if (!minor || !minor->kdev)
  286. return;
  287. #if defined(CONFIG_DEBUG_FS)
  288. drm_debugfs_cleanup(minor);
  289. #endif
  290. drm_sysfs_device_remove(minor);
  291. idr_remove(&drm_minors_idr, minor->index);
  292. }
  293. /**
  294. * drm_put_minor - Destroy DRM minor
  295. * @minor: Minor to destroy
  296. *
  297. * This calls drm_unplug_minor() on the given minor and then frees it. Nothing
  298. * is done if @minor is NULL. It is fine to call this on already unplugged
  299. * minors.
  300. * The global DRM mutex must be held by the caller.
  301. */
  302. static void drm_put_minor(struct drm_minor *minor)
  303. {
  304. if (!minor)
  305. return;
  306. DRM_DEBUG("release secondary minor %d\n", minor->index);
  307. drm_unplug_minor(minor);
  308. kfree(minor);
  309. }
  310. /**
  311. * Called via drm_exit() at module unload time or when pci device is
  312. * unplugged.
  313. *
  314. * Cleans up all DRM device, calling drm_lastclose().
  315. *
  316. */
  317. void drm_put_dev(struct drm_device *dev)
  318. {
  319. DRM_DEBUG("\n");
  320. if (!dev) {
  321. DRM_ERROR("cleanup called no dev\n");
  322. return;
  323. }
  324. drm_dev_unregister(dev);
  325. drm_dev_free(dev);
  326. }
  327. EXPORT_SYMBOL(drm_put_dev);
  328. void drm_unplug_dev(struct drm_device *dev)
  329. {
  330. /* for a USB device */
  331. if (drm_core_check_feature(dev, DRIVER_MODESET))
  332. drm_unplug_minor(dev->control);
  333. if (dev->render)
  334. drm_unplug_minor(dev->render);
  335. drm_unplug_minor(dev->primary);
  336. mutex_lock(&drm_global_mutex);
  337. drm_device_set_unplugged(dev);
  338. if (dev->open_count == 0) {
  339. drm_put_dev(dev);
  340. }
  341. mutex_unlock(&drm_global_mutex);
  342. }
  343. EXPORT_SYMBOL(drm_unplug_dev);
  344. /**
  345. * drm_dev_alloc - Allocate new drm device
  346. * @driver: DRM driver to allocate device for
  347. * @parent: Parent device object
  348. *
  349. * Allocate and initialize a new DRM device. No device registration is done.
  350. * Call drm_dev_register() to advertice the device to user space and register it
  351. * with other core subsystems.
  352. *
  353. * RETURNS:
  354. * Pointer to new DRM device, or NULL if out of memory.
  355. */
  356. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  357. struct device *parent)
  358. {
  359. struct drm_device *dev;
  360. int ret;
  361. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  362. if (!dev)
  363. return NULL;
  364. dev->dev = parent;
  365. dev->driver = driver;
  366. INIT_LIST_HEAD(&dev->filelist);
  367. INIT_LIST_HEAD(&dev->ctxlist);
  368. INIT_LIST_HEAD(&dev->vmalist);
  369. INIT_LIST_HEAD(&dev->maplist);
  370. INIT_LIST_HEAD(&dev->vblank_event_list);
  371. spin_lock_init(&dev->count_lock);
  372. spin_lock_init(&dev->event_lock);
  373. mutex_init(&dev->struct_mutex);
  374. mutex_init(&dev->ctxlist_mutex);
  375. if (drm_ht_create(&dev->map_hash, 12))
  376. goto err_free;
  377. ret = drm_ctxbitmap_init(dev);
  378. if (ret) {
  379. DRM_ERROR("Cannot allocate memory for context bitmap.\n");
  380. goto err_ht;
  381. }
  382. if (driver->driver_features & DRIVER_GEM) {
  383. ret = drm_gem_init(dev);
  384. if (ret) {
  385. DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  386. goto err_ctxbitmap;
  387. }
  388. }
  389. return dev;
  390. err_ctxbitmap:
  391. drm_ctxbitmap_cleanup(dev);
  392. err_ht:
  393. drm_ht_remove(&dev->map_hash);
  394. err_free:
  395. kfree(dev);
  396. return NULL;
  397. }
  398. EXPORT_SYMBOL(drm_dev_alloc);
  399. /**
  400. * drm_dev_free - Free DRM device
  401. * @dev: DRM device to free
  402. *
  403. * Free a DRM device that has previously been allocated via drm_dev_alloc().
  404. * You must not use kfree() instead or you will leak memory.
  405. *
  406. * This must not be called once the device got registered. Use drm_put_dev()
  407. * instead, which then calls drm_dev_free().
  408. */
  409. void drm_dev_free(struct drm_device *dev)
  410. {
  411. drm_put_minor(dev->control);
  412. drm_put_minor(dev->render);
  413. drm_put_minor(dev->primary);
  414. if (dev->driver->driver_features & DRIVER_GEM)
  415. drm_gem_destroy(dev);
  416. drm_ctxbitmap_cleanup(dev);
  417. drm_ht_remove(&dev->map_hash);
  418. kfree(dev->devname);
  419. kfree(dev);
  420. }
  421. EXPORT_SYMBOL(drm_dev_free);
  422. /**
  423. * drm_dev_register - Register DRM device
  424. * @dev: Device to register
  425. *
  426. * Register the DRM device @dev with the system, advertise device to user-space
  427. * and start normal device operation. @dev must be allocated via drm_dev_alloc()
  428. * previously.
  429. *
  430. * Never call this twice on any device!
  431. *
  432. * RETURNS:
  433. * 0 on success, negative error code on failure.
  434. */
  435. int drm_dev_register(struct drm_device *dev, unsigned long flags)
  436. {
  437. int ret;
  438. mutex_lock(&drm_global_mutex);
  439. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  440. ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
  441. if (ret)
  442. goto out_unlock;
  443. }
  444. if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
  445. ret = drm_get_minor(dev, &dev->render, DRM_MINOR_RENDER);
  446. if (ret)
  447. goto err_control_node;
  448. }
  449. ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
  450. if (ret)
  451. goto err_render_node;
  452. if (dev->driver->load) {
  453. ret = dev->driver->load(dev, flags);
  454. if (ret)
  455. goto err_primary_node;
  456. }
  457. /* setup grouping for legacy outputs */
  458. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  459. ret = drm_mode_group_init_legacy_group(dev,
  460. &dev->primary->mode_group);
  461. if (ret)
  462. goto err_unload;
  463. }
  464. ret = 0;
  465. goto out_unlock;
  466. err_unload:
  467. if (dev->driver->unload)
  468. dev->driver->unload(dev);
  469. err_primary_node:
  470. drm_unplug_minor(dev->primary);
  471. err_render_node:
  472. drm_unplug_minor(dev->render);
  473. err_control_node:
  474. drm_unplug_minor(dev->control);
  475. out_unlock:
  476. mutex_unlock(&drm_global_mutex);
  477. return ret;
  478. }
  479. EXPORT_SYMBOL(drm_dev_register);
  480. /**
  481. * drm_dev_unregister - Unregister DRM device
  482. * @dev: Device to unregister
  483. *
  484. * Unregister the DRM device from the system. This does the reverse of
  485. * drm_dev_register() but does not deallocate the device. The caller must call
  486. * drm_dev_free() to free all resources.
  487. */
  488. void drm_dev_unregister(struct drm_device *dev)
  489. {
  490. struct drm_map_list *r_list, *list_temp;
  491. drm_lastclose(dev);
  492. if (dev->driver->unload)
  493. dev->driver->unload(dev);
  494. if (dev->agp)
  495. drm_pci_agp_destroy(dev);
  496. drm_vblank_cleanup(dev);
  497. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  498. drm_rmmap(dev, r_list->map);
  499. drm_unplug_minor(dev->control);
  500. drm_unplug_minor(dev->render);
  501. drm_unplug_minor(dev->primary);
  502. }
  503. EXPORT_SYMBOL(drm_dev_unregister);