drm_drv.c 29 KB

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