drm_drv.c 30 KB

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