drm_drv.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*
  2. * Created: Fri Jan 19 10:48:35 2001 by [email protected]
  3. *
  4. * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
  5. * All Rights Reserved.
  6. *
  7. * Author Rickard E. (Rik) Faith <[email protected]>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <linux/debugfs.h>
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/mount.h>
  33. #include <linux/pseudo_fs.h>
  34. #include <linux/slab.h>
  35. #include <linux/srcu.h>
  36. #include <drm/drm_cache.h>
  37. #include <drm/drm_client.h>
  38. #include <drm/drm_color_mgmt.h>
  39. #include <drm/drm_drv.h>
  40. #include <drm/drm_file.h>
  41. #include <drm/drm_managed.h>
  42. #include <drm/drm_mode_object.h>
  43. #include <drm/drm_print.h>
  44. #include <drm/drm_privacy_screen_machine.h>
  45. #include "drm_crtc_internal.h"
  46. #include "drm_internal.h"
  47. #include "drm_legacy.h"
  48. MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
  49. MODULE_DESCRIPTION("DRM shared core routines");
  50. MODULE_LICENSE("GPL and additional rights");
  51. static DEFINE_SPINLOCK(drm_minor_lock);
  52. static struct idr drm_minors_idr;
  53. /*
  54. * If the drm core fails to init for whatever reason,
  55. * we should prevent any drivers from registering with it.
  56. * It's best to check this at drm_dev_init(), as some drivers
  57. * prefer to embed struct drm_device into their own device
  58. * structure and call drm_dev_init() themselves.
  59. */
  60. static bool drm_core_init_complete;
  61. static struct dentry *drm_debugfs_root;
  62. DEFINE_STATIC_SRCU(drm_unplug_srcu);
  63. /*
  64. * DRM Minors
  65. * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
  66. * of them is represented by a drm_minor object. Depending on the capabilities
  67. * of the device-driver, different interfaces are registered.
  68. *
  69. * Minors can be accessed via dev->$minor_name. This pointer is either
  70. * NULL or a valid drm_minor pointer and stays valid as long as the device is
  71. * valid. This means, DRM minors have the same life-time as the underlying
  72. * device. However, this doesn't mean that the minor is active. Minors are
  73. * registered and unregistered dynamically according to device-state.
  74. */
  75. static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
  76. unsigned int type)
  77. {
  78. switch (type) {
  79. case DRM_MINOR_PRIMARY:
  80. return &dev->primary;
  81. case DRM_MINOR_RENDER:
  82. return &dev->render;
  83. default:
  84. BUG();
  85. }
  86. }
  87. static void drm_minor_alloc_release(struct drm_device *dev, void *data)
  88. {
  89. struct drm_minor *minor = data;
  90. unsigned long flags;
  91. WARN_ON(dev != minor->dev);
  92. put_device(minor->kdev);
  93. spin_lock_irqsave(&drm_minor_lock, flags);
  94. idr_remove(&drm_minors_idr, minor->index);
  95. spin_unlock_irqrestore(&drm_minor_lock, flags);
  96. }
  97. static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
  98. {
  99. struct drm_minor *minor;
  100. unsigned long flags;
  101. int r;
  102. minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL);
  103. if (!minor)
  104. return -ENOMEM;
  105. minor->type = type;
  106. minor->dev = dev;
  107. idr_preload(GFP_KERNEL);
  108. spin_lock_irqsave(&drm_minor_lock, flags);
  109. r = idr_alloc(&drm_minors_idr,
  110. NULL,
  111. 64 * type,
  112. 64 * (type + 1),
  113. GFP_NOWAIT);
  114. spin_unlock_irqrestore(&drm_minor_lock, flags);
  115. idr_preload_end();
  116. if (r < 0)
  117. return r;
  118. minor->index = r;
  119. r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor);
  120. if (r)
  121. return r;
  122. minor->kdev = drm_sysfs_minor_alloc(minor);
  123. if (IS_ERR(minor->kdev))
  124. return PTR_ERR(minor->kdev);
  125. *drm_minor_get_slot(dev, type) = minor;
  126. return 0;
  127. }
  128. static int drm_minor_register(struct drm_device *dev, unsigned int type)
  129. {
  130. struct drm_minor *minor;
  131. unsigned long flags;
  132. int ret;
  133. DRM_DEBUG("\n");
  134. minor = *drm_minor_get_slot(dev, type);
  135. if (!minor)
  136. return 0;
  137. ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
  138. if (ret) {
  139. DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  140. goto err_debugfs;
  141. }
  142. ret = device_add(minor->kdev);
  143. if (ret)
  144. goto err_debugfs;
  145. /* replace NULL with @minor so lookups will succeed from now on */
  146. spin_lock_irqsave(&drm_minor_lock, flags);
  147. idr_replace(&drm_minors_idr, minor, minor->index);
  148. spin_unlock_irqrestore(&drm_minor_lock, flags);
  149. DRM_DEBUG("new minor registered %d\n", minor->index);
  150. return 0;
  151. err_debugfs:
  152. drm_debugfs_cleanup(minor);
  153. return ret;
  154. }
  155. static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
  156. {
  157. struct drm_minor *minor;
  158. unsigned long flags;
  159. minor = *drm_minor_get_slot(dev, type);
  160. if (!minor || !device_is_registered(minor->kdev))
  161. return;
  162. /* replace @minor with NULL so lookups will fail from now on */
  163. spin_lock_irqsave(&drm_minor_lock, flags);
  164. idr_replace(&drm_minors_idr, NULL, minor->index);
  165. spin_unlock_irqrestore(&drm_minor_lock, flags);
  166. device_del(minor->kdev);
  167. dev_set_drvdata(minor->kdev, NULL); /* safety belt */
  168. drm_debugfs_cleanup(minor);
  169. }
  170. /*
  171. * Looks up the given minor-ID and returns the respective DRM-minor object. The
  172. * refence-count of the underlying device is increased so you must release this
  173. * object with drm_minor_release().
  174. *
  175. * As long as you hold this minor, it is guaranteed that the object and the
  176. * minor->dev pointer will stay valid! However, the device may get unplugged and
  177. * unregistered while you hold the minor.
  178. */
  179. struct drm_minor *drm_minor_acquire(unsigned int minor_id)
  180. {
  181. struct drm_minor *minor;
  182. unsigned long flags;
  183. spin_lock_irqsave(&drm_minor_lock, flags);
  184. minor = idr_find(&drm_minors_idr, minor_id);
  185. if (minor)
  186. drm_dev_get(minor->dev);
  187. spin_unlock_irqrestore(&drm_minor_lock, flags);
  188. if (!minor) {
  189. return ERR_PTR(-ENODEV);
  190. } else if (drm_dev_is_unplugged(minor->dev)) {
  191. drm_dev_put(minor->dev);
  192. return ERR_PTR(-ENODEV);
  193. }
  194. return minor;
  195. }
  196. void drm_minor_release(struct drm_minor *minor)
  197. {
  198. drm_dev_put(minor->dev);
  199. }
  200. /**
  201. * DOC: driver instance overview
  202. *
  203. * A device instance for a drm driver is represented by &struct drm_device. This
  204. * is allocated and initialized with devm_drm_dev_alloc(), usually from
  205. * bus-specific ->probe() callbacks implemented by the driver. The driver then
  206. * needs to initialize all the various subsystems for the drm device like memory
  207. * management, vblank handling, modesetting support and initial output
  208. * configuration plus obviously initialize all the corresponding hardware bits.
  209. * Finally when everything is up and running and ready for userspace the device
  210. * instance can be published using drm_dev_register().
  211. *
  212. * There is also deprecated support for initializing device instances using
  213. * bus-specific helpers and the &drm_driver.load callback. But due to
  214. * backwards-compatibility needs the device instance have to be published too
  215. * early, which requires unpretty global locking to make safe and is therefore
  216. * only support for existing drivers not yet converted to the new scheme.
  217. *
  218. * When cleaning up a device instance everything needs to be done in reverse:
  219. * First unpublish the device instance with drm_dev_unregister(). Then clean up
  220. * any other resources allocated at device initialization and drop the driver's
  221. * reference to &drm_device using drm_dev_put().
  222. *
  223. * Note that any allocation or resource which is visible to userspace must be
  224. * released only when the final drm_dev_put() is called, and not when the
  225. * driver is unbound from the underlying physical struct &device. Best to use
  226. * &drm_device managed resources with drmm_add_action(), drmm_kmalloc() and
  227. * related functions.
  228. *
  229. * devres managed resources like devm_kmalloc() can only be used for resources
  230. * directly related to the underlying hardware device, and only used in code
  231. * paths fully protected by drm_dev_enter() and drm_dev_exit().
  232. *
  233. * Display driver example
  234. * ~~~~~~~~~~~~~~~~~~~~~~
  235. *
  236. * The following example shows a typical structure of a DRM display driver.
  237. * The example focus on the probe() function and the other functions that is
  238. * almost always present and serves as a demonstration of devm_drm_dev_alloc().
  239. *
  240. * .. code-block:: c
  241. *
  242. * struct driver_device {
  243. * struct drm_device drm;
  244. * void *userspace_facing;
  245. * struct clk *pclk;
  246. * };
  247. *
  248. * static const struct drm_driver driver_drm_driver = {
  249. * [...]
  250. * };
  251. *
  252. * static int driver_probe(struct platform_device *pdev)
  253. * {
  254. * struct driver_device *priv;
  255. * struct drm_device *drm;
  256. * int ret;
  257. *
  258. * priv = devm_drm_dev_alloc(&pdev->dev, &driver_drm_driver,
  259. * struct driver_device, drm);
  260. * if (IS_ERR(priv))
  261. * return PTR_ERR(priv);
  262. * drm = &priv->drm;
  263. *
  264. * ret = drmm_mode_config_init(drm);
  265. * if (ret)
  266. * return ret;
  267. *
  268. * priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL);
  269. * if (!priv->userspace_facing)
  270. * return -ENOMEM;
  271. *
  272. * priv->pclk = devm_clk_get(dev, "PCLK");
  273. * if (IS_ERR(priv->pclk))
  274. * return PTR_ERR(priv->pclk);
  275. *
  276. * // Further setup, display pipeline etc
  277. *
  278. * platform_set_drvdata(pdev, drm);
  279. *
  280. * drm_mode_config_reset(drm);
  281. *
  282. * ret = drm_dev_register(drm);
  283. * if (ret)
  284. * return ret;
  285. *
  286. * drm_fbdev_generic_setup(drm, 32);
  287. *
  288. * return 0;
  289. * }
  290. *
  291. * // This function is called before the devm_ resources are released
  292. * static int driver_remove(struct platform_device *pdev)
  293. * {
  294. * struct drm_device *drm = platform_get_drvdata(pdev);
  295. *
  296. * drm_dev_unregister(drm);
  297. * drm_atomic_helper_shutdown(drm)
  298. *
  299. * return 0;
  300. * }
  301. *
  302. * // This function is called on kernel restart and shutdown
  303. * static void driver_shutdown(struct platform_device *pdev)
  304. * {
  305. * drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  306. * }
  307. *
  308. * static int __maybe_unused driver_pm_suspend(struct device *dev)
  309. * {
  310. * return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
  311. * }
  312. *
  313. * static int __maybe_unused driver_pm_resume(struct device *dev)
  314. * {
  315. * drm_mode_config_helper_resume(dev_get_drvdata(dev));
  316. *
  317. * return 0;
  318. * }
  319. *
  320. * static const struct dev_pm_ops driver_pm_ops = {
  321. * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
  322. * };
  323. *
  324. * static struct platform_driver driver_driver = {
  325. * .driver = {
  326. * [...]
  327. * .pm = &driver_pm_ops,
  328. * },
  329. * .probe = driver_probe,
  330. * .remove = driver_remove,
  331. * .shutdown = driver_shutdown,
  332. * };
  333. * module_platform_driver(driver_driver);
  334. *
  335. * Drivers that want to support device unplugging (USB, DT overlay unload) should
  336. * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
  337. * regions that is accessing device resources to prevent use after they're
  338. * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
  339. * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
  340. * drm_atomic_helper_shutdown() is called. This means that if the disable code
  341. * paths are protected, they will not run on regular driver module unload,
  342. * possibly leaving the hardware enabled.
  343. */
  344. /**
  345. * drm_put_dev - Unregister and release a DRM device
  346. * @dev: DRM device
  347. *
  348. * Called at module unload time or when a PCI device is unplugged.
  349. *
  350. * Cleans up all DRM device, calling drm_lastclose().
  351. *
  352. * Note: Use of this function is deprecated. It will eventually go away
  353. * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
  354. * instead to make sure that the device isn't userspace accessible any more
  355. * while teardown is in progress, ensuring that userspace can't access an
  356. * inconsistent state.
  357. */
  358. void drm_put_dev(struct drm_device *dev)
  359. {
  360. DRM_DEBUG("\n");
  361. if (!dev) {
  362. DRM_ERROR("cleanup called no dev\n");
  363. return;
  364. }
  365. drm_dev_unregister(dev);
  366. drm_dev_put(dev);
  367. }
  368. EXPORT_SYMBOL(drm_put_dev);
  369. /**
  370. * drm_dev_enter - Enter device critical section
  371. * @dev: DRM device
  372. * @idx: Pointer to index that will be passed to the matching drm_dev_exit()
  373. *
  374. * This function marks and protects the beginning of a section that should not
  375. * be entered after the device has been unplugged. The section end is marked
  376. * with drm_dev_exit(). Calls to this function can be nested.
  377. *
  378. * Returns:
  379. * True if it is OK to enter the section, false otherwise.
  380. */
  381. bool drm_dev_enter(struct drm_device *dev, int *idx)
  382. {
  383. *idx = srcu_read_lock(&drm_unplug_srcu);
  384. if (dev->unplugged) {
  385. srcu_read_unlock(&drm_unplug_srcu, *idx);
  386. return false;
  387. }
  388. return true;
  389. }
  390. EXPORT_SYMBOL(drm_dev_enter);
  391. /**
  392. * drm_dev_exit - Exit device critical section
  393. * @idx: index returned from drm_dev_enter()
  394. *
  395. * This function marks the end of a section that should not be entered after
  396. * the device has been unplugged.
  397. */
  398. void drm_dev_exit(int idx)
  399. {
  400. srcu_read_unlock(&drm_unplug_srcu, idx);
  401. }
  402. EXPORT_SYMBOL(drm_dev_exit);
  403. /**
  404. * drm_dev_unplug - unplug a DRM device
  405. * @dev: DRM device
  406. *
  407. * This unplugs a hotpluggable DRM device, which makes it inaccessible to
  408. * userspace operations. Entry-points can use drm_dev_enter() and
  409. * drm_dev_exit() to protect device resources in a race free manner. This
  410. * essentially unregisters the device like drm_dev_unregister(), but can be
  411. * called while there are still open users of @dev.
  412. */
  413. void drm_dev_unplug(struct drm_device *dev)
  414. {
  415. /*
  416. * After synchronizing any critical read section is guaranteed to see
  417. * the new value of ->unplugged, and any critical section which might
  418. * still have seen the old value of ->unplugged is guaranteed to have
  419. * finished.
  420. */
  421. dev->unplugged = true;
  422. synchronize_srcu(&drm_unplug_srcu);
  423. drm_dev_unregister(dev);
  424. /* Clear all CPU mappings pointing to this device */
  425. unmap_mapping_range(dev->anon_inode->i_mapping, 0, 0, 1);
  426. }
  427. EXPORT_SYMBOL(drm_dev_unplug);
  428. /*
  429. * DRM internal mount
  430. * We want to be able to allocate our own "struct address_space" to control
  431. * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
  432. * stand-alone address_space objects, so we need an underlying inode. As there
  433. * is no way to allocate an independent inode easily, we need a fake internal
  434. * VFS mount-point.
  435. *
  436. * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
  437. * frees it again. You are allowed to use iget() and iput() to get references to
  438. * the inode. But each drm_fs_inode_new() call must be paired with exactly one
  439. * drm_fs_inode_free() call (which does not have to be the last iput()).
  440. * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
  441. * between multiple inode-users. You could, technically, call
  442. * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
  443. * iput(), but this way you'd end up with a new vfsmount for each inode.
  444. */
  445. static int drm_fs_cnt;
  446. static struct vfsmount *drm_fs_mnt;
  447. static int drm_fs_init_fs_context(struct fs_context *fc)
  448. {
  449. return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
  450. }
  451. static struct file_system_type drm_fs_type = {
  452. .name = "drm",
  453. .owner = THIS_MODULE,
  454. .init_fs_context = drm_fs_init_fs_context,
  455. .kill_sb = kill_anon_super,
  456. };
  457. static struct inode *drm_fs_inode_new(void)
  458. {
  459. struct inode *inode;
  460. int r;
  461. r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
  462. if (r < 0) {
  463. DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
  464. return ERR_PTR(r);
  465. }
  466. inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
  467. if (IS_ERR(inode))
  468. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  469. return inode;
  470. }
  471. static void drm_fs_inode_free(struct inode *inode)
  472. {
  473. if (inode) {
  474. iput(inode);
  475. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  476. }
  477. }
  478. /**
  479. * DOC: component helper usage recommendations
  480. *
  481. * DRM drivers that drive hardware where a logical device consists of a pile of
  482. * independent hardware blocks are recommended to use the :ref:`component helper
  483. * library<component>`. For consistency and better options for code reuse the
  484. * following guidelines apply:
  485. *
  486. * - The entire device initialization procedure should be run from the
  487. * &component_master_ops.master_bind callback, starting with
  488. * devm_drm_dev_alloc(), then binding all components with
  489. * component_bind_all() and finishing with drm_dev_register().
  490. *
  491. * - The opaque pointer passed to all components through component_bind_all()
  492. * should point at &struct drm_device of the device instance, not some driver
  493. * specific private structure.
  494. *
  495. * - The component helper fills the niche where further standardization of
  496. * interfaces is not practical. When there already is, or will be, a
  497. * standardized interface like &drm_bridge or &drm_panel, providing its own
  498. * functions to find such components at driver load time, like
  499. * drm_of_find_panel_or_bridge(), then the component helper should not be
  500. * used.
  501. */
  502. static void drm_dev_init_release(struct drm_device *dev, void *res)
  503. {
  504. drm_legacy_ctxbitmap_cleanup(dev);
  505. drm_legacy_remove_map_hash(dev);
  506. drm_fs_inode_free(dev->anon_inode);
  507. put_device(dev->dev);
  508. /* Prevent use-after-free in drm_managed_release when debugging is
  509. * enabled. Slightly awkward, but can't really be helped. */
  510. dev->dev = NULL;
  511. mutex_destroy(&dev->master_mutex);
  512. mutex_destroy(&dev->clientlist_mutex);
  513. mutex_destroy(&dev->filelist_mutex);
  514. mutex_destroy(&dev->struct_mutex);
  515. drm_legacy_destroy_members(dev);
  516. }
  517. static int drm_dev_init(struct drm_device *dev,
  518. const struct drm_driver *driver,
  519. struct device *parent)
  520. {
  521. struct inode *inode;
  522. int ret;
  523. if (!drm_core_init_complete) {
  524. DRM_ERROR("DRM core is not initialized\n");
  525. return -ENODEV;
  526. }
  527. if (WARN_ON(!parent))
  528. return -EINVAL;
  529. kref_init(&dev->ref);
  530. dev->dev = get_device(parent);
  531. dev->driver = driver;
  532. INIT_LIST_HEAD(&dev->managed.resources);
  533. spin_lock_init(&dev->managed.lock);
  534. /* no per-device feature limits by default */
  535. dev->driver_features = ~0u;
  536. drm_legacy_init_members(dev);
  537. INIT_LIST_HEAD(&dev->filelist);
  538. INIT_LIST_HEAD(&dev->filelist_internal);
  539. INIT_LIST_HEAD(&dev->clientlist);
  540. INIT_LIST_HEAD(&dev->vblank_event_list);
  541. spin_lock_init(&dev->event_lock);
  542. mutex_init(&dev->struct_mutex);
  543. mutex_init(&dev->filelist_mutex);
  544. mutex_init(&dev->clientlist_mutex);
  545. mutex_init(&dev->master_mutex);
  546. ret = drmm_add_action_or_reset(dev, drm_dev_init_release, NULL);
  547. if (ret)
  548. return ret;
  549. inode = drm_fs_inode_new();
  550. if (IS_ERR(inode)) {
  551. ret = PTR_ERR(inode);
  552. DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
  553. goto err;
  554. }
  555. dev->anon_inode = inode;
  556. if (drm_core_check_feature(dev, DRIVER_RENDER)) {
  557. ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
  558. if (ret)
  559. goto err;
  560. }
  561. ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
  562. if (ret)
  563. goto err;
  564. ret = drm_legacy_create_map_hash(dev);
  565. if (ret)
  566. goto err;
  567. drm_legacy_ctxbitmap_init(dev);
  568. if (drm_core_check_feature(dev, DRIVER_GEM)) {
  569. ret = drm_gem_init(dev);
  570. if (ret) {
  571. DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  572. goto err;
  573. }
  574. }
  575. ret = drm_dev_set_unique(dev, dev_name(parent));
  576. if (ret)
  577. goto err;
  578. return 0;
  579. err:
  580. drm_managed_release(dev);
  581. return ret;
  582. }
  583. static void devm_drm_dev_init_release(void *data)
  584. {
  585. drm_dev_put(data);
  586. }
  587. static int devm_drm_dev_init(struct device *parent,
  588. struct drm_device *dev,
  589. const struct drm_driver *driver)
  590. {
  591. int ret;
  592. ret = drm_dev_init(dev, driver, parent);
  593. if (ret)
  594. return ret;
  595. return devm_add_action_or_reset(parent,
  596. devm_drm_dev_init_release, dev);
  597. }
  598. void *__devm_drm_dev_alloc(struct device *parent,
  599. const struct drm_driver *driver,
  600. size_t size, size_t offset)
  601. {
  602. void *container;
  603. struct drm_device *drm;
  604. int ret;
  605. container = kzalloc(size, GFP_KERNEL);
  606. if (!container)
  607. return ERR_PTR(-ENOMEM);
  608. drm = container + offset;
  609. ret = devm_drm_dev_init(parent, drm, driver);
  610. if (ret) {
  611. kfree(container);
  612. return ERR_PTR(ret);
  613. }
  614. drmm_add_final_kfree(drm, container);
  615. return container;
  616. }
  617. EXPORT_SYMBOL(__devm_drm_dev_alloc);
  618. /**
  619. * drm_dev_alloc - Allocate new DRM device
  620. * @driver: DRM driver to allocate device for
  621. * @parent: Parent device object
  622. *
  623. * This is the deprecated version of devm_drm_dev_alloc(), which does not support
  624. * subclassing through embedding the struct &drm_device in a driver private
  625. * structure, and which does not support automatic cleanup through devres.
  626. *
  627. * RETURNS:
  628. * Pointer to new DRM device, or ERR_PTR on failure.
  629. */
  630. struct drm_device *drm_dev_alloc(const struct drm_driver *driver,
  631. struct device *parent)
  632. {
  633. struct drm_device *dev;
  634. int ret;
  635. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  636. if (!dev)
  637. return ERR_PTR(-ENOMEM);
  638. ret = drm_dev_init(dev, driver, parent);
  639. if (ret) {
  640. kfree(dev);
  641. return ERR_PTR(ret);
  642. }
  643. drmm_add_final_kfree(dev, dev);
  644. return dev;
  645. }
  646. EXPORT_SYMBOL(drm_dev_alloc);
  647. static void drm_dev_release(struct kref *ref)
  648. {
  649. struct drm_device *dev = container_of(ref, struct drm_device, ref);
  650. if (dev->driver->release)
  651. dev->driver->release(dev);
  652. drm_managed_release(dev);
  653. kfree(dev->managed.final_kfree);
  654. }
  655. /**
  656. * drm_dev_get - Take reference of a DRM device
  657. * @dev: device to take reference of or NULL
  658. *
  659. * This increases the ref-count of @dev by one. You *must* already own a
  660. * reference when calling this. Use drm_dev_put() to drop this reference
  661. * again.
  662. *
  663. * This function never fails. However, this function does not provide *any*
  664. * guarantee whether the device is alive or running. It only provides a
  665. * reference to the object and the memory associated with it.
  666. */
  667. void drm_dev_get(struct drm_device *dev)
  668. {
  669. if (dev)
  670. kref_get(&dev->ref);
  671. }
  672. EXPORT_SYMBOL(drm_dev_get);
  673. /**
  674. * drm_dev_put - Drop reference of a DRM device
  675. * @dev: device to drop reference of or NULL
  676. *
  677. * This decreases the ref-count of @dev by one. The device is destroyed if the
  678. * ref-count drops to zero.
  679. */
  680. void drm_dev_put(struct drm_device *dev)
  681. {
  682. if (dev)
  683. kref_put(&dev->ref, drm_dev_release);
  684. }
  685. EXPORT_SYMBOL(drm_dev_put);
  686. static int create_compat_control_link(struct drm_device *dev)
  687. {
  688. struct drm_minor *minor;
  689. char *name;
  690. int ret;
  691. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  692. return 0;
  693. minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
  694. if (!minor)
  695. return 0;
  696. /*
  697. * Some existing userspace out there uses the existing of the controlD*
  698. * sysfs files to figure out whether it's a modeset driver. It only does
  699. * readdir, hence a symlink is sufficient (and the least confusing
  700. * option). Otherwise controlD* is entirely unused.
  701. *
  702. * Old controlD chardev have been allocated in the range
  703. * 64-127.
  704. */
  705. name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
  706. if (!name)
  707. return -ENOMEM;
  708. ret = sysfs_create_link(minor->kdev->kobj.parent,
  709. &minor->kdev->kobj,
  710. name);
  711. kfree(name);
  712. return ret;
  713. }
  714. static void remove_compat_control_link(struct drm_device *dev)
  715. {
  716. struct drm_minor *minor;
  717. char *name;
  718. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  719. return;
  720. minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
  721. if (!minor)
  722. return;
  723. name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
  724. if (!name)
  725. return;
  726. sysfs_remove_link(minor->kdev->kobj.parent, name);
  727. kfree(name);
  728. }
  729. /**
  730. * drm_dev_register - Register DRM device
  731. * @dev: Device to register
  732. * @flags: Flags passed to the driver's .load() function
  733. *
  734. * Register the DRM device @dev with the system, advertise device to user-space
  735. * and start normal device operation. @dev must be initialized via drm_dev_init()
  736. * previously.
  737. *
  738. * Never call this twice on any device!
  739. *
  740. * NOTE: To ensure backward compatibility with existing drivers method this
  741. * function calls the &drm_driver.load method after registering the device
  742. * nodes, creating race conditions. Usage of the &drm_driver.load methods is
  743. * therefore deprecated, drivers must perform all initialization before calling
  744. * drm_dev_register().
  745. *
  746. * RETURNS:
  747. * 0 on success, negative error code on failure.
  748. */
  749. int drm_dev_register(struct drm_device *dev, unsigned long flags)
  750. {
  751. const struct drm_driver *driver = dev->driver;
  752. int ret;
  753. if (!driver->load)
  754. drm_mode_config_validate(dev);
  755. WARN_ON(!dev->managed.final_kfree);
  756. if (drm_dev_needs_global_mutex(dev))
  757. mutex_lock(&drm_global_mutex);
  758. ret = drm_minor_register(dev, DRM_MINOR_RENDER);
  759. if (ret)
  760. goto err_minors;
  761. ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
  762. if (ret)
  763. goto err_minors;
  764. ret = create_compat_control_link(dev);
  765. if (ret)
  766. goto err_minors;
  767. dev->registered = true;
  768. if (dev->driver->load) {
  769. ret = dev->driver->load(dev, flags);
  770. if (ret)
  771. goto err_minors;
  772. }
  773. if (drm_core_check_feature(dev, DRIVER_MODESET))
  774. drm_modeset_register_all(dev);
  775. DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
  776. driver->name, driver->major, driver->minor,
  777. driver->patchlevel, driver->date,
  778. dev->dev ? dev_name(dev->dev) : "virtual device",
  779. dev->primary->index);
  780. goto out_unlock;
  781. err_minors:
  782. remove_compat_control_link(dev);
  783. drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
  784. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  785. out_unlock:
  786. if (drm_dev_needs_global_mutex(dev))
  787. mutex_unlock(&drm_global_mutex);
  788. return ret;
  789. }
  790. EXPORT_SYMBOL(drm_dev_register);
  791. /**
  792. * drm_dev_unregister - Unregister DRM device
  793. * @dev: Device to unregister
  794. *
  795. * Unregister the DRM device from the system. This does the reverse of
  796. * drm_dev_register() but does not deallocate the device. The caller must call
  797. * drm_dev_put() to drop their final reference.
  798. *
  799. * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
  800. * which can be called while there are still open users of @dev.
  801. *
  802. * This should be called first in the device teardown code to make sure
  803. * userspace can't access the device instance any more.
  804. */
  805. void drm_dev_unregister(struct drm_device *dev)
  806. {
  807. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  808. drm_lastclose(dev);
  809. dev->registered = false;
  810. drm_client_dev_unregister(dev);
  811. if (drm_core_check_feature(dev, DRIVER_MODESET))
  812. drm_modeset_unregister_all(dev);
  813. if (dev->driver->unload)
  814. dev->driver->unload(dev);
  815. drm_legacy_pci_agp_destroy(dev);
  816. drm_legacy_rmmaps(dev);
  817. remove_compat_control_link(dev);
  818. drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
  819. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  820. }
  821. EXPORT_SYMBOL(drm_dev_unregister);
  822. /**
  823. * drm_dev_set_unique - Set the unique name of a DRM device
  824. * @dev: device of which to set the unique name
  825. * @name: unique name
  826. *
  827. * Sets the unique name of a DRM device using the specified string. This is
  828. * already done by drm_dev_init(), drivers should only override the default
  829. * unique name for backwards compatibility reasons.
  830. *
  831. * Return: 0 on success or a negative error code on failure.
  832. */
  833. int drm_dev_set_unique(struct drm_device *dev, const char *name)
  834. {
  835. drmm_kfree(dev, dev->unique);
  836. dev->unique = drmm_kstrdup(dev, name, GFP_KERNEL);
  837. return dev->unique ? 0 : -ENOMEM;
  838. }
  839. EXPORT_SYMBOL(drm_dev_set_unique);
  840. /*
  841. * DRM Core
  842. * The DRM core module initializes all global DRM objects and makes them
  843. * available to drivers. Once setup, drivers can probe their respective
  844. * devices.
  845. * Currently, core management includes:
  846. * - The "DRM-Global" key/value database
  847. * - Global ID management for connectors
  848. * - DRM major number allocation
  849. * - DRM minor management
  850. * - DRM sysfs class
  851. * - DRM debugfs root
  852. *
  853. * Furthermore, the DRM core provides dynamic char-dev lookups. For each
  854. * interface registered on a DRM device, you can request minor numbers from DRM
  855. * core. DRM core takes care of major-number management and char-dev
  856. * registration. A stub ->open() callback forwards any open() requests to the
  857. * registered minor.
  858. */
  859. static int drm_stub_open(struct inode *inode, struct file *filp)
  860. {
  861. const struct file_operations *new_fops;
  862. struct drm_minor *minor;
  863. int err;
  864. DRM_DEBUG("\n");
  865. minor = drm_minor_acquire(iminor(inode));
  866. if (IS_ERR(minor))
  867. return PTR_ERR(minor);
  868. new_fops = fops_get(minor->dev->driver->fops);
  869. if (!new_fops) {
  870. err = -ENODEV;
  871. goto out;
  872. }
  873. replace_fops(filp, new_fops);
  874. if (filp->f_op->open)
  875. err = filp->f_op->open(inode, filp);
  876. else
  877. err = 0;
  878. out:
  879. drm_minor_release(minor);
  880. return err;
  881. }
  882. static const struct file_operations drm_stub_fops = {
  883. .owner = THIS_MODULE,
  884. .open = drm_stub_open,
  885. .llseek = noop_llseek,
  886. };
  887. static void drm_core_exit(void)
  888. {
  889. drm_privacy_screen_lookup_exit();
  890. unregister_chrdev(DRM_MAJOR, "drm");
  891. debugfs_remove(drm_debugfs_root);
  892. drm_sysfs_destroy();
  893. idr_destroy(&drm_minors_idr);
  894. drm_connector_ida_destroy();
  895. }
  896. static int __init drm_core_init(void)
  897. {
  898. int ret;
  899. drm_connector_ida_init();
  900. idr_init(&drm_minors_idr);
  901. drm_memcpy_init_early();
  902. ret = drm_sysfs_init();
  903. if (ret < 0) {
  904. DRM_ERROR("Cannot create DRM class: %d\n", ret);
  905. goto error;
  906. }
  907. drm_debugfs_root = debugfs_create_dir("dri", NULL);
  908. ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
  909. if (ret < 0)
  910. goto error;
  911. drm_privacy_screen_lookup_init();
  912. drm_core_init_complete = true;
  913. DRM_DEBUG("Initialized\n");
  914. return 0;
  915. error:
  916. drm_core_exit();
  917. return ret;
  918. }
  919. module_init(drm_core_init);
  920. module_exit(drm_core_exit);