drm_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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/slab.h>
  34. #include <drm/drmP.h>
  35. #include <drm/drm_core.h>
  36. #include "drm_crtc_internal.h"
  37. #include "drm_legacy.h"
  38. #include "drm_internal.h"
  39. #include "drm_crtc_internal.h"
  40. /*
  41. * drm_debug: Enable debug output.
  42. * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
  43. */
  44. unsigned int drm_debug = 0;
  45. EXPORT_SYMBOL(drm_debug);
  46. MODULE_AUTHOR(CORE_AUTHOR);
  47. MODULE_DESCRIPTION(CORE_DESC);
  48. MODULE_LICENSE("GPL and additional rights");
  49. MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
  50. "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
  51. "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
  52. "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
  53. "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
  54. "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
  55. "\t\tBit 5 (0x20) will enable VBL messages (vblank code)");
  56. module_param_named(debug, drm_debug, int, 0600);
  57. static DEFINE_SPINLOCK(drm_minor_lock);
  58. static struct idr drm_minors_idr;
  59. static struct dentry *drm_debugfs_root;
  60. void drm_err(const char *format, ...)
  61. {
  62. struct va_format vaf;
  63. va_list args;
  64. va_start(args, format);
  65. vaf.fmt = format;
  66. vaf.va = &args;
  67. printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
  68. __builtin_return_address(0), &vaf);
  69. va_end(args);
  70. }
  71. EXPORT_SYMBOL(drm_err);
  72. void drm_ut_debug_printk(const char *function_name, const char *format, ...)
  73. {
  74. struct va_format vaf;
  75. va_list args;
  76. va_start(args, format);
  77. vaf.fmt = format;
  78. vaf.va = &args;
  79. printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
  80. va_end(args);
  81. }
  82. EXPORT_SYMBOL(drm_ut_debug_printk);
  83. /*
  84. * DRM Minors
  85. * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
  86. * of them is represented by a drm_minor object. Depending on the capabilities
  87. * of the device-driver, different interfaces are registered.
  88. *
  89. * Minors can be accessed via dev->$minor_name. This pointer is either
  90. * NULL or a valid drm_minor pointer and stays valid as long as the device is
  91. * valid. This means, DRM minors have the same life-time as the underlying
  92. * device. However, this doesn't mean that the minor is active. Minors are
  93. * registered and unregistered dynamically according to device-state.
  94. */
  95. static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
  96. unsigned int type)
  97. {
  98. switch (type) {
  99. case DRM_MINOR_LEGACY:
  100. return &dev->primary;
  101. case DRM_MINOR_RENDER:
  102. return &dev->render;
  103. case DRM_MINOR_CONTROL:
  104. return &dev->control;
  105. default:
  106. return NULL;
  107. }
  108. }
  109. static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
  110. {
  111. struct drm_minor *minor;
  112. unsigned long flags;
  113. int r;
  114. minor = kzalloc(sizeof(*minor), GFP_KERNEL);
  115. if (!minor)
  116. return -ENOMEM;
  117. minor->type = type;
  118. minor->dev = dev;
  119. idr_preload(GFP_KERNEL);
  120. spin_lock_irqsave(&drm_minor_lock, flags);
  121. r = idr_alloc(&drm_minors_idr,
  122. NULL,
  123. 64 * type,
  124. 64 * (type + 1),
  125. GFP_NOWAIT);
  126. spin_unlock_irqrestore(&drm_minor_lock, flags);
  127. idr_preload_end();
  128. if (r < 0)
  129. goto err_free;
  130. minor->index = r;
  131. minor->kdev = drm_sysfs_minor_alloc(minor);
  132. if (IS_ERR(minor->kdev)) {
  133. r = PTR_ERR(minor->kdev);
  134. goto err_index;
  135. }
  136. *drm_minor_get_slot(dev, type) = minor;
  137. return 0;
  138. err_index:
  139. spin_lock_irqsave(&drm_minor_lock, flags);
  140. idr_remove(&drm_minors_idr, minor->index);
  141. spin_unlock_irqrestore(&drm_minor_lock, flags);
  142. err_free:
  143. kfree(minor);
  144. return r;
  145. }
  146. static void drm_minor_free(struct drm_device *dev, unsigned int type)
  147. {
  148. struct drm_minor **slot, *minor;
  149. unsigned long flags;
  150. slot = drm_minor_get_slot(dev, type);
  151. minor = *slot;
  152. if (!minor)
  153. return;
  154. put_device(minor->kdev);
  155. spin_lock_irqsave(&drm_minor_lock, flags);
  156. idr_remove(&drm_minors_idr, minor->index);
  157. spin_unlock_irqrestore(&drm_minor_lock, flags);
  158. kfree(minor);
  159. *slot = NULL;
  160. }
  161. static int drm_minor_register(struct drm_device *dev, unsigned int type)
  162. {
  163. struct drm_minor *minor;
  164. unsigned long flags;
  165. int ret;
  166. DRM_DEBUG("\n");
  167. minor = *drm_minor_get_slot(dev, type);
  168. if (!minor)
  169. return 0;
  170. ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
  171. if (ret) {
  172. DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  173. return ret;
  174. }
  175. ret = device_add(minor->kdev);
  176. if (ret)
  177. goto err_debugfs;
  178. /* replace NULL with @minor so lookups will succeed from now on */
  179. spin_lock_irqsave(&drm_minor_lock, flags);
  180. idr_replace(&drm_minors_idr, minor, minor->index);
  181. spin_unlock_irqrestore(&drm_minor_lock, flags);
  182. DRM_DEBUG("new minor registered %d\n", minor->index);
  183. return 0;
  184. err_debugfs:
  185. drm_debugfs_cleanup(minor);
  186. return ret;
  187. }
  188. static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
  189. {
  190. struct drm_minor *minor;
  191. unsigned long flags;
  192. minor = *drm_minor_get_slot(dev, type);
  193. if (!minor || !device_is_registered(minor->kdev))
  194. return;
  195. /* replace @minor with NULL so lookups will fail from now on */
  196. spin_lock_irqsave(&drm_minor_lock, flags);
  197. idr_replace(&drm_minors_idr, NULL, minor->index);
  198. spin_unlock_irqrestore(&drm_minor_lock, flags);
  199. device_del(minor->kdev);
  200. dev_set_drvdata(minor->kdev, NULL); /* safety belt */
  201. drm_debugfs_cleanup(minor);
  202. }
  203. /**
  204. * drm_minor_acquire - Acquire a DRM minor
  205. * @minor_id: Minor ID of the DRM-minor
  206. *
  207. * Looks up the given minor-ID and returns the respective DRM-minor object. The
  208. * refence-count of the underlying device is increased so you must release this
  209. * object with drm_minor_release().
  210. *
  211. * As long as you hold this minor, it is guaranteed that the object and the
  212. * minor->dev pointer will stay valid! However, the device may get unplugged and
  213. * unregistered while you hold the minor.
  214. *
  215. * Returns:
  216. * Pointer to minor-object with increased device-refcount, or PTR_ERR on
  217. * failure.
  218. */
  219. struct drm_minor *drm_minor_acquire(unsigned int minor_id)
  220. {
  221. struct drm_minor *minor;
  222. unsigned long flags;
  223. spin_lock_irqsave(&drm_minor_lock, flags);
  224. minor = idr_find(&drm_minors_idr, minor_id);
  225. if (minor)
  226. drm_dev_ref(minor->dev);
  227. spin_unlock_irqrestore(&drm_minor_lock, flags);
  228. if (!minor) {
  229. return ERR_PTR(-ENODEV);
  230. } else if (drm_device_is_unplugged(minor->dev)) {
  231. drm_dev_unref(minor->dev);
  232. return ERR_PTR(-ENODEV);
  233. }
  234. return minor;
  235. }
  236. /**
  237. * drm_minor_release - Release DRM minor
  238. * @minor: Pointer to DRM minor object
  239. *
  240. * Release a minor that was previously acquired via drm_minor_acquire().
  241. */
  242. void drm_minor_release(struct drm_minor *minor)
  243. {
  244. drm_dev_unref(minor->dev);
  245. }
  246. /**
  247. * DOC: driver instance overview
  248. *
  249. * A device instance for a drm driver is represented by struct &drm_device. This
  250. * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
  251. * callbacks implemented by the driver. The driver then needs to initialize all
  252. * the various subsystems for the drm device like memory management, vblank
  253. * handling, modesetting support and intial output configuration plus obviously
  254. * initialize all the corresponding hardware bits. Finally when everything is up
  255. * and running and ready for userspace the device instance can be published
  256. * using drm_dev_register().
  257. *
  258. * There is also deprecated support for initalizing device instances using
  259. * bus-specific helpers and the ->load() callback. But due to
  260. * backwards-compatibility needs the device instance have to be published too
  261. * early, which requires unpretty global locking to make safe and is therefore
  262. * only support for existing drivers not yet converted to the new scheme.
  263. *
  264. * When cleaning up a device instance everything needs to be done in reverse:
  265. * First unpublish the device instance with drm_dev_unregister(). Then clean up
  266. * any other resources allocated at device initialization and drop the driver's
  267. * reference to &drm_device using drm_dev_unref().
  268. *
  269. * Note that the lifetime rules for &drm_device instance has still a lot of
  270. * historical baggage. Hence use the reference counting provided by
  271. * drm_dev_ref() and drm_dev_unref() only carefully.
  272. *
  273. * Also note that embedding of &drm_device is currently not (yet) supported (but
  274. * it would be easy to add). Drivers can store driver-private data in the
  275. * dev_priv field of &drm_device.
  276. */
  277. static int drm_dev_set_unique(struct drm_device *dev, const char *name)
  278. {
  279. kfree(dev->unique);
  280. dev->unique = kstrdup(name, GFP_KERNEL);
  281. return dev->unique ? 0 : -ENOMEM;
  282. }
  283. /**
  284. * drm_put_dev - Unregister and release a DRM device
  285. * @dev: DRM device
  286. *
  287. * Called at module unload time or when a PCI device is unplugged.
  288. *
  289. * Cleans up all DRM device, calling drm_lastclose().
  290. *
  291. * Note: Use of this function is deprecated. It will eventually go away
  292. * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
  293. * instead to make sure that the device isn't userspace accessible any more
  294. * while teardown is in progress, ensuring that userspace can't access an
  295. * inconsistent state.
  296. */
  297. void drm_put_dev(struct drm_device *dev)
  298. {
  299. DRM_DEBUG("\n");
  300. if (!dev) {
  301. DRM_ERROR("cleanup called no dev\n");
  302. return;
  303. }
  304. drm_dev_unregister(dev);
  305. drm_dev_unref(dev);
  306. }
  307. EXPORT_SYMBOL(drm_put_dev);
  308. void drm_unplug_dev(struct drm_device *dev)
  309. {
  310. /* for a USB device */
  311. drm_dev_unregister(dev);
  312. mutex_lock(&drm_global_mutex);
  313. drm_device_set_unplugged(dev);
  314. if (dev->open_count == 0) {
  315. drm_put_dev(dev);
  316. }
  317. mutex_unlock(&drm_global_mutex);
  318. }
  319. EXPORT_SYMBOL(drm_unplug_dev);
  320. /*
  321. * DRM internal mount
  322. * We want to be able to allocate our own "struct address_space" to control
  323. * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
  324. * stand-alone address_space objects, so we need an underlying inode. As there
  325. * is no way to allocate an independent inode easily, we need a fake internal
  326. * VFS mount-point.
  327. *
  328. * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
  329. * frees it again. You are allowed to use iget() and iput() to get references to
  330. * the inode. But each drm_fs_inode_new() call must be paired with exactly one
  331. * drm_fs_inode_free() call (which does not have to be the last iput()).
  332. * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
  333. * between multiple inode-users. You could, technically, call
  334. * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
  335. * iput(), but this way you'd end up with a new vfsmount for each inode.
  336. */
  337. static int drm_fs_cnt;
  338. static struct vfsmount *drm_fs_mnt;
  339. static const struct dentry_operations drm_fs_dops = {
  340. .d_dname = simple_dname,
  341. };
  342. static const struct super_operations drm_fs_sops = {
  343. .statfs = simple_statfs,
  344. };
  345. static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
  346. const char *dev_name, void *data)
  347. {
  348. return mount_pseudo(fs_type,
  349. "drm:",
  350. &drm_fs_sops,
  351. &drm_fs_dops,
  352. 0x010203ff);
  353. }
  354. static struct file_system_type drm_fs_type = {
  355. .name = "drm",
  356. .owner = THIS_MODULE,
  357. .mount = drm_fs_mount,
  358. .kill_sb = kill_anon_super,
  359. };
  360. static struct inode *drm_fs_inode_new(void)
  361. {
  362. struct inode *inode;
  363. int r;
  364. r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
  365. if (r < 0) {
  366. DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
  367. return ERR_PTR(r);
  368. }
  369. inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
  370. if (IS_ERR(inode))
  371. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  372. return inode;
  373. }
  374. static void drm_fs_inode_free(struct inode *inode)
  375. {
  376. if (inode) {
  377. iput(inode);
  378. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  379. }
  380. }
  381. /**
  382. * drm_dev_init - Initialise new DRM device
  383. * @dev: DRM device
  384. * @driver: DRM driver
  385. * @parent: Parent device object
  386. *
  387. * Initialize a new DRM device. No device registration is done.
  388. * Call drm_dev_register() to advertice the device to user space and register it
  389. * with other core subsystems. This should be done last in the device
  390. * initialization sequence to make sure userspace can't access an inconsistent
  391. * state.
  392. *
  393. * The initial ref-count of the object is 1. Use drm_dev_ref() and
  394. * drm_dev_unref() to take and drop further ref-counts.
  395. *
  396. * Note that for purely virtual devices @parent can be NULL.
  397. *
  398. * Drivers that do not want to allocate their own device struct
  399. * embedding struct &drm_device can call drm_dev_alloc() instead.
  400. *
  401. * RETURNS:
  402. * 0 on success, or error code on failure.
  403. */
  404. int drm_dev_init(struct drm_device *dev,
  405. struct drm_driver *driver,
  406. struct device *parent)
  407. {
  408. int ret;
  409. kref_init(&dev->ref);
  410. dev->dev = parent;
  411. dev->driver = driver;
  412. INIT_LIST_HEAD(&dev->filelist);
  413. INIT_LIST_HEAD(&dev->ctxlist);
  414. INIT_LIST_HEAD(&dev->vmalist);
  415. INIT_LIST_HEAD(&dev->maplist);
  416. INIT_LIST_HEAD(&dev->vblank_event_list);
  417. spin_lock_init(&dev->buf_lock);
  418. spin_lock_init(&dev->event_lock);
  419. mutex_init(&dev->struct_mutex);
  420. mutex_init(&dev->filelist_mutex);
  421. mutex_init(&dev->ctxlist_mutex);
  422. mutex_init(&dev->master_mutex);
  423. dev->anon_inode = drm_fs_inode_new();
  424. if (IS_ERR(dev->anon_inode)) {
  425. ret = PTR_ERR(dev->anon_inode);
  426. DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
  427. goto err_free;
  428. }
  429. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  430. ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
  431. if (ret)
  432. goto err_minors;
  433. }
  434. if (drm_core_check_feature(dev, DRIVER_RENDER)) {
  435. ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
  436. if (ret)
  437. goto err_minors;
  438. }
  439. ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
  440. if (ret)
  441. goto err_minors;
  442. ret = drm_ht_create(&dev->map_hash, 12);
  443. if (ret)
  444. goto err_minors;
  445. drm_legacy_ctxbitmap_init(dev);
  446. if (drm_core_check_feature(dev, DRIVER_GEM)) {
  447. ret = drm_gem_init(dev);
  448. if (ret) {
  449. DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  450. goto err_ctxbitmap;
  451. }
  452. }
  453. /* Use the parent device name as DRM device unique identifier, but fall
  454. * back to the driver name for virtual devices like vgem. */
  455. ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name);
  456. if (ret)
  457. goto err_setunique;
  458. return 0;
  459. err_setunique:
  460. if (drm_core_check_feature(dev, DRIVER_GEM))
  461. drm_gem_destroy(dev);
  462. err_ctxbitmap:
  463. drm_legacy_ctxbitmap_cleanup(dev);
  464. drm_ht_remove(&dev->map_hash);
  465. err_minors:
  466. drm_minor_free(dev, DRM_MINOR_LEGACY);
  467. drm_minor_free(dev, DRM_MINOR_RENDER);
  468. drm_minor_free(dev, DRM_MINOR_CONTROL);
  469. drm_fs_inode_free(dev->anon_inode);
  470. err_free:
  471. mutex_destroy(&dev->master_mutex);
  472. return ret;
  473. }
  474. EXPORT_SYMBOL(drm_dev_init);
  475. /**
  476. * drm_dev_alloc - Allocate new DRM device
  477. * @driver: DRM driver to allocate device for
  478. * @parent: Parent device object
  479. *
  480. * Allocate and initialize a new DRM device. No device registration is done.
  481. * Call drm_dev_register() to advertice the device to user space and register it
  482. * with other core subsystems. This should be done last in the device
  483. * initialization sequence to make sure userspace can't access an inconsistent
  484. * state.
  485. *
  486. * The initial ref-count of the object is 1. Use drm_dev_ref() and
  487. * drm_dev_unref() to take and drop further ref-counts.
  488. *
  489. * Note that for purely virtual devices @parent can be NULL.
  490. *
  491. * Drivers that wish to subclass or embed struct &drm_device into their
  492. * own struct should look at using drm_dev_init() instead.
  493. *
  494. * RETURNS:
  495. * Pointer to new DRM device, or NULL if out of memory.
  496. */
  497. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  498. struct device *parent)
  499. {
  500. struct drm_device *dev;
  501. int ret;
  502. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  503. if (!dev)
  504. return NULL;
  505. ret = drm_dev_init(dev, driver, parent);
  506. if (ret) {
  507. kfree(dev);
  508. return NULL;
  509. }
  510. return dev;
  511. }
  512. EXPORT_SYMBOL(drm_dev_alloc);
  513. static void drm_dev_release(struct kref *ref)
  514. {
  515. struct drm_device *dev = container_of(ref, struct drm_device, ref);
  516. if (drm_core_check_feature(dev, DRIVER_GEM))
  517. drm_gem_destroy(dev);
  518. drm_legacy_ctxbitmap_cleanup(dev);
  519. drm_ht_remove(&dev->map_hash);
  520. drm_fs_inode_free(dev->anon_inode);
  521. drm_minor_free(dev, DRM_MINOR_LEGACY);
  522. drm_minor_free(dev, DRM_MINOR_RENDER);
  523. drm_minor_free(dev, DRM_MINOR_CONTROL);
  524. mutex_destroy(&dev->master_mutex);
  525. kfree(dev->unique);
  526. kfree(dev);
  527. }
  528. /**
  529. * drm_dev_ref - Take reference of a DRM device
  530. * @dev: device to take reference of or NULL
  531. *
  532. * This increases the ref-count of @dev by one. You *must* already own a
  533. * reference when calling this. Use drm_dev_unref() to drop this reference
  534. * again.
  535. *
  536. * This function never fails. However, this function does not provide *any*
  537. * guarantee whether the device is alive or running. It only provides a
  538. * reference to the object and the memory associated with it.
  539. */
  540. void drm_dev_ref(struct drm_device *dev)
  541. {
  542. if (dev)
  543. kref_get(&dev->ref);
  544. }
  545. EXPORT_SYMBOL(drm_dev_ref);
  546. /**
  547. * drm_dev_unref - Drop reference of a DRM device
  548. * @dev: device to drop reference of or NULL
  549. *
  550. * This decreases the ref-count of @dev by one. The device is destroyed if the
  551. * ref-count drops to zero.
  552. */
  553. void drm_dev_unref(struct drm_device *dev)
  554. {
  555. if (dev)
  556. kref_put(&dev->ref, drm_dev_release);
  557. }
  558. EXPORT_SYMBOL(drm_dev_unref);
  559. /**
  560. * drm_dev_register - Register DRM device
  561. * @dev: Device to register
  562. * @flags: Flags passed to the driver's .load() function
  563. *
  564. * Register the DRM device @dev with the system, advertise device to user-space
  565. * and start normal device operation. @dev must be allocated via drm_dev_alloc()
  566. * previously.
  567. *
  568. * Never call this twice on any device!
  569. *
  570. * NOTE: To ensure backward compatibility with existing drivers method this
  571. * function calls the ->load() method after registering the device nodes,
  572. * creating race conditions. Usage of the ->load() methods is therefore
  573. * deprecated, drivers must perform all initialization before calling
  574. * drm_dev_register().
  575. *
  576. * RETURNS:
  577. * 0 on success, negative error code on failure.
  578. */
  579. int drm_dev_register(struct drm_device *dev, unsigned long flags)
  580. {
  581. int ret;
  582. mutex_lock(&drm_global_mutex);
  583. ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
  584. if (ret)
  585. goto err_minors;
  586. ret = drm_minor_register(dev, DRM_MINOR_RENDER);
  587. if (ret)
  588. goto err_minors;
  589. ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
  590. if (ret)
  591. goto err_minors;
  592. if (dev->driver->load) {
  593. ret = dev->driver->load(dev, flags);
  594. if (ret)
  595. goto err_minors;
  596. }
  597. if (drm_core_check_feature(dev, DRIVER_MODESET))
  598. drm_modeset_register_all(dev);
  599. ret = 0;
  600. goto out_unlock;
  601. err_minors:
  602. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  603. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  604. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  605. out_unlock:
  606. mutex_unlock(&drm_global_mutex);
  607. return ret;
  608. }
  609. EXPORT_SYMBOL(drm_dev_register);
  610. /**
  611. * drm_dev_unregister - Unregister DRM device
  612. * @dev: Device to unregister
  613. *
  614. * Unregister the DRM device from the system. This does the reverse of
  615. * drm_dev_register() but does not deallocate the device. The caller must call
  616. * drm_dev_unref() to drop their final reference.
  617. *
  618. * This should be called first in the device teardown code to make sure
  619. * userspace can't access the device instance any more.
  620. */
  621. void drm_dev_unregister(struct drm_device *dev)
  622. {
  623. struct drm_map_list *r_list, *list_temp;
  624. drm_lastclose(dev);
  625. if (drm_core_check_feature(dev, DRIVER_MODESET))
  626. drm_modeset_unregister_all(dev);
  627. if (dev->driver->unload)
  628. dev->driver->unload(dev);
  629. if (dev->agp)
  630. drm_pci_agp_destroy(dev);
  631. drm_vblank_cleanup(dev);
  632. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  633. drm_legacy_rmmap(dev, r_list->map);
  634. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  635. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  636. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  637. }
  638. EXPORT_SYMBOL(drm_dev_unregister);
  639. /*
  640. * DRM Core
  641. * The DRM core module initializes all global DRM objects and makes them
  642. * available to drivers. Once setup, drivers can probe their respective
  643. * devices.
  644. * Currently, core management includes:
  645. * - The "DRM-Global" key/value database
  646. * - Global ID management for connectors
  647. * - DRM major number allocation
  648. * - DRM minor management
  649. * - DRM sysfs class
  650. * - DRM debugfs root
  651. *
  652. * Furthermore, the DRM core provides dynamic char-dev lookups. For each
  653. * interface registered on a DRM device, you can request minor numbers from DRM
  654. * core. DRM core takes care of major-number management and char-dev
  655. * registration. A stub ->open() callback forwards any open() requests to the
  656. * registered minor.
  657. */
  658. static int drm_stub_open(struct inode *inode, struct file *filp)
  659. {
  660. const struct file_operations *new_fops;
  661. struct drm_minor *minor;
  662. int err;
  663. DRM_DEBUG("\n");
  664. mutex_lock(&drm_global_mutex);
  665. minor = drm_minor_acquire(iminor(inode));
  666. if (IS_ERR(minor)) {
  667. err = PTR_ERR(minor);
  668. goto out_unlock;
  669. }
  670. new_fops = fops_get(minor->dev->driver->fops);
  671. if (!new_fops) {
  672. err = -ENODEV;
  673. goto out_release;
  674. }
  675. replace_fops(filp, new_fops);
  676. if (filp->f_op->open)
  677. err = filp->f_op->open(inode, filp);
  678. else
  679. err = 0;
  680. out_release:
  681. drm_minor_release(minor);
  682. out_unlock:
  683. mutex_unlock(&drm_global_mutex);
  684. return err;
  685. }
  686. static const struct file_operations drm_stub_fops = {
  687. .owner = THIS_MODULE,
  688. .open = drm_stub_open,
  689. .llseek = noop_llseek,
  690. };
  691. static int __init drm_core_init(void)
  692. {
  693. int ret = -ENOMEM;
  694. drm_global_init();
  695. drm_connector_ida_init();
  696. idr_init(&drm_minors_idr);
  697. if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
  698. goto err_p1;
  699. ret = drm_sysfs_init();
  700. if (ret < 0) {
  701. printk(KERN_ERR "DRM: Error creating drm class.\n");
  702. goto err_p2;
  703. }
  704. drm_debugfs_root = debugfs_create_dir("dri", NULL);
  705. if (!drm_debugfs_root) {
  706. DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
  707. ret = -1;
  708. goto err_p3;
  709. }
  710. DRM_INFO("Initialized %s %d.%d.%d %s\n",
  711. CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  712. return 0;
  713. err_p3:
  714. drm_sysfs_destroy();
  715. err_p2:
  716. unregister_chrdev(DRM_MAJOR, "drm");
  717. idr_destroy(&drm_minors_idr);
  718. err_p1:
  719. return ret;
  720. }
  721. static void __exit drm_core_exit(void)
  722. {
  723. debugfs_remove(drm_debugfs_root);
  724. drm_sysfs_destroy();
  725. unregister_chrdev(DRM_MAJOR, "drm");
  726. drm_connector_ida_destroy();
  727. idr_destroy(&drm_minors_idr);
  728. }
  729. module_init(drm_core_init);
  730. module_exit(drm_core_exit);