drm_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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_minor_unregister(dev, DRM_MINOR_LEGACY);
  312. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  313. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  314. mutex_lock(&drm_global_mutex);
  315. drm_device_set_unplugged(dev);
  316. if (dev->open_count == 0) {
  317. drm_put_dev(dev);
  318. }
  319. mutex_unlock(&drm_global_mutex);
  320. }
  321. EXPORT_SYMBOL(drm_unplug_dev);
  322. /*
  323. * DRM internal mount
  324. * We want to be able to allocate our own "struct address_space" to control
  325. * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
  326. * stand-alone address_space objects, so we need an underlying inode. As there
  327. * is no way to allocate an independent inode easily, we need a fake internal
  328. * VFS mount-point.
  329. *
  330. * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
  331. * frees it again. You are allowed to use iget() and iput() to get references to
  332. * the inode. But each drm_fs_inode_new() call must be paired with exactly one
  333. * drm_fs_inode_free() call (which does not have to be the last iput()).
  334. * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
  335. * between multiple inode-users. You could, technically, call
  336. * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
  337. * iput(), but this way you'd end up with a new vfsmount for each inode.
  338. */
  339. static int drm_fs_cnt;
  340. static struct vfsmount *drm_fs_mnt;
  341. static const struct dentry_operations drm_fs_dops = {
  342. .d_dname = simple_dname,
  343. };
  344. static const struct super_operations drm_fs_sops = {
  345. .statfs = simple_statfs,
  346. };
  347. static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
  348. const char *dev_name, void *data)
  349. {
  350. return mount_pseudo(fs_type,
  351. "drm:",
  352. &drm_fs_sops,
  353. &drm_fs_dops,
  354. 0x010203ff);
  355. }
  356. static struct file_system_type drm_fs_type = {
  357. .name = "drm",
  358. .owner = THIS_MODULE,
  359. .mount = drm_fs_mount,
  360. .kill_sb = kill_anon_super,
  361. };
  362. static struct inode *drm_fs_inode_new(void)
  363. {
  364. struct inode *inode;
  365. int r;
  366. r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
  367. if (r < 0) {
  368. DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
  369. return ERR_PTR(r);
  370. }
  371. inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
  372. if (IS_ERR(inode))
  373. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  374. return inode;
  375. }
  376. static void drm_fs_inode_free(struct inode *inode)
  377. {
  378. if (inode) {
  379. iput(inode);
  380. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  381. }
  382. }
  383. /**
  384. * drm_dev_init - Initialise new DRM device
  385. * @dev: DRM device
  386. * @driver: DRM driver
  387. * @parent: Parent device object
  388. *
  389. * Initialize a new DRM device. No device registration is done.
  390. * Call drm_dev_register() to advertice the device to user space and register it
  391. * with other core subsystems. This should be done last in the device
  392. * initialization sequence to make sure userspace can't access an inconsistent
  393. * state.
  394. *
  395. * The initial ref-count of the object is 1. Use drm_dev_ref() and
  396. * drm_dev_unref() to take and drop further ref-counts.
  397. *
  398. * Note that for purely virtual devices @parent can be NULL.
  399. *
  400. * Drivers that do not want to allocate their own device struct
  401. * embedding struct &drm_device can call drm_dev_alloc() instead.
  402. *
  403. * RETURNS:
  404. * 0 on success, or error code on failure.
  405. */
  406. int drm_dev_init(struct drm_device *dev,
  407. struct drm_driver *driver,
  408. struct device *parent)
  409. {
  410. int ret;
  411. kref_init(&dev->ref);
  412. dev->dev = parent;
  413. dev->driver = driver;
  414. INIT_LIST_HEAD(&dev->filelist);
  415. INIT_LIST_HEAD(&dev->ctxlist);
  416. INIT_LIST_HEAD(&dev->vmalist);
  417. INIT_LIST_HEAD(&dev->maplist);
  418. INIT_LIST_HEAD(&dev->vblank_event_list);
  419. spin_lock_init(&dev->buf_lock);
  420. spin_lock_init(&dev->event_lock);
  421. mutex_init(&dev->struct_mutex);
  422. mutex_init(&dev->filelist_mutex);
  423. mutex_init(&dev->ctxlist_mutex);
  424. mutex_init(&dev->master_mutex);
  425. dev->anon_inode = drm_fs_inode_new();
  426. if (IS_ERR(dev->anon_inode)) {
  427. ret = PTR_ERR(dev->anon_inode);
  428. DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
  429. goto err_free;
  430. }
  431. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  432. ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
  433. if (ret)
  434. goto err_minors;
  435. }
  436. if (drm_core_check_feature(dev, DRIVER_RENDER)) {
  437. ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
  438. if (ret)
  439. goto err_minors;
  440. }
  441. ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
  442. if (ret)
  443. goto err_minors;
  444. ret = drm_ht_create(&dev->map_hash, 12);
  445. if (ret)
  446. goto err_minors;
  447. drm_legacy_ctxbitmap_init(dev);
  448. if (drm_core_check_feature(dev, DRIVER_GEM)) {
  449. ret = drm_gem_init(dev);
  450. if (ret) {
  451. DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  452. goto err_ctxbitmap;
  453. }
  454. }
  455. /* Use the parent device name as DRM device unique identifier, but fall
  456. * back to the driver name for virtual devices like vgem. */
  457. ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name);
  458. if (ret)
  459. goto err_setunique;
  460. return 0;
  461. err_setunique:
  462. if (drm_core_check_feature(dev, DRIVER_GEM))
  463. drm_gem_destroy(dev);
  464. err_ctxbitmap:
  465. drm_legacy_ctxbitmap_cleanup(dev);
  466. drm_ht_remove(&dev->map_hash);
  467. err_minors:
  468. drm_minor_free(dev, DRM_MINOR_LEGACY);
  469. drm_minor_free(dev, DRM_MINOR_RENDER);
  470. drm_minor_free(dev, DRM_MINOR_CONTROL);
  471. drm_fs_inode_free(dev->anon_inode);
  472. err_free:
  473. mutex_destroy(&dev->master_mutex);
  474. return ret;
  475. }
  476. EXPORT_SYMBOL(drm_dev_init);
  477. /**
  478. * drm_dev_alloc - Allocate new DRM device
  479. * @driver: DRM driver to allocate device for
  480. * @parent: Parent device object
  481. *
  482. * Allocate and initialize a new DRM device. No device registration is done.
  483. * Call drm_dev_register() to advertice the device to user space and register it
  484. * with other core subsystems. This should be done last in the device
  485. * initialization sequence to make sure userspace can't access an inconsistent
  486. * state.
  487. *
  488. * The initial ref-count of the object is 1. Use drm_dev_ref() and
  489. * drm_dev_unref() to take and drop further ref-counts.
  490. *
  491. * Note that for purely virtual devices @parent can be NULL.
  492. *
  493. * Drivers that wish to subclass or embed struct &drm_device into their
  494. * own struct should look at using drm_dev_init() instead.
  495. *
  496. * RETURNS:
  497. * Pointer to new DRM device, or NULL if out of memory.
  498. */
  499. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  500. struct device *parent)
  501. {
  502. struct drm_device *dev;
  503. int ret;
  504. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  505. if (!dev)
  506. return NULL;
  507. ret = drm_dev_init(dev, driver, parent);
  508. if (ret) {
  509. kfree(dev);
  510. return NULL;
  511. }
  512. return dev;
  513. }
  514. EXPORT_SYMBOL(drm_dev_alloc);
  515. static void drm_dev_release(struct kref *ref)
  516. {
  517. struct drm_device *dev = container_of(ref, struct drm_device, ref);
  518. if (drm_core_check_feature(dev, DRIVER_GEM))
  519. drm_gem_destroy(dev);
  520. drm_legacy_ctxbitmap_cleanup(dev);
  521. drm_ht_remove(&dev->map_hash);
  522. drm_fs_inode_free(dev->anon_inode);
  523. drm_minor_free(dev, DRM_MINOR_LEGACY);
  524. drm_minor_free(dev, DRM_MINOR_RENDER);
  525. drm_minor_free(dev, DRM_MINOR_CONTROL);
  526. mutex_destroy(&dev->master_mutex);
  527. kfree(dev->unique);
  528. kfree(dev);
  529. }
  530. /**
  531. * drm_dev_ref - Take reference of a DRM device
  532. * @dev: device to take reference of or NULL
  533. *
  534. * This increases the ref-count of @dev by one. You *must* already own a
  535. * reference when calling this. Use drm_dev_unref() to drop this reference
  536. * again.
  537. *
  538. * This function never fails. However, this function does not provide *any*
  539. * guarantee whether the device is alive or running. It only provides a
  540. * reference to the object and the memory associated with it.
  541. */
  542. void drm_dev_ref(struct drm_device *dev)
  543. {
  544. if (dev)
  545. kref_get(&dev->ref);
  546. }
  547. EXPORT_SYMBOL(drm_dev_ref);
  548. /**
  549. * drm_dev_unref - Drop reference of a DRM device
  550. * @dev: device to drop reference of or NULL
  551. *
  552. * This decreases the ref-count of @dev by one. The device is destroyed if the
  553. * ref-count drops to zero.
  554. */
  555. void drm_dev_unref(struct drm_device *dev)
  556. {
  557. if (dev)
  558. kref_put(&dev->ref, drm_dev_release);
  559. }
  560. EXPORT_SYMBOL(drm_dev_unref);
  561. /**
  562. * drm_dev_register - Register DRM device
  563. * @dev: Device to register
  564. * @flags: Flags passed to the driver's .load() function
  565. *
  566. * Register the DRM device @dev with the system, advertise device to user-space
  567. * and start normal device operation. @dev must be allocated via drm_dev_alloc()
  568. * previously.
  569. *
  570. * Never call this twice on any device!
  571. *
  572. * NOTE: To ensure backward compatibility with existing drivers method this
  573. * function calls the ->load() method after registering the device nodes,
  574. * creating race conditions. Usage of the ->load() methods is therefore
  575. * deprecated, drivers must perform all initialization before calling
  576. * drm_dev_register().
  577. *
  578. * RETURNS:
  579. * 0 on success, negative error code on failure.
  580. */
  581. int drm_dev_register(struct drm_device *dev, unsigned long flags)
  582. {
  583. int ret;
  584. mutex_lock(&drm_global_mutex);
  585. ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
  586. if (ret)
  587. goto err_minors;
  588. ret = drm_minor_register(dev, DRM_MINOR_RENDER);
  589. if (ret)
  590. goto err_minors;
  591. ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
  592. if (ret)
  593. goto err_minors;
  594. if (dev->driver->load) {
  595. ret = dev->driver->load(dev, flags);
  596. if (ret)
  597. goto err_minors;
  598. }
  599. if (drm_core_check_feature(dev, DRIVER_MODESET))
  600. drm_modeset_register_all(dev);
  601. ret = 0;
  602. goto out_unlock;
  603. err_minors:
  604. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  605. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  606. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  607. out_unlock:
  608. mutex_unlock(&drm_global_mutex);
  609. return ret;
  610. }
  611. EXPORT_SYMBOL(drm_dev_register);
  612. /**
  613. * drm_dev_unregister - Unregister DRM device
  614. * @dev: Device to unregister
  615. *
  616. * Unregister the DRM device from the system. This does the reverse of
  617. * drm_dev_register() but does not deallocate the device. The caller must call
  618. * drm_dev_unref() to drop their final reference.
  619. *
  620. * This should be called first in the device teardown code to make sure
  621. * userspace can't access the device instance any more.
  622. */
  623. void drm_dev_unregister(struct drm_device *dev)
  624. {
  625. struct drm_map_list *r_list, *list_temp;
  626. drm_lastclose(dev);
  627. if (drm_core_check_feature(dev, DRIVER_MODESET))
  628. drm_modeset_unregister_all(dev);
  629. if (dev->driver->unload)
  630. dev->driver->unload(dev);
  631. if (dev->agp)
  632. drm_pci_agp_destroy(dev);
  633. drm_vblank_cleanup(dev);
  634. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  635. drm_legacy_rmmap(dev, r_list->map);
  636. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  637. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  638. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  639. }
  640. EXPORT_SYMBOL(drm_dev_unregister);
  641. /*
  642. * DRM Core
  643. * The DRM core module initializes all global DRM objects and makes them
  644. * available to drivers. Once setup, drivers can probe their respective
  645. * devices.
  646. * Currently, core management includes:
  647. * - The "DRM-Global" key/value database
  648. * - Global ID management for connectors
  649. * - DRM major number allocation
  650. * - DRM minor management
  651. * - DRM sysfs class
  652. * - DRM debugfs root
  653. *
  654. * Furthermore, the DRM core provides dynamic char-dev lookups. For each
  655. * interface registered on a DRM device, you can request minor numbers from DRM
  656. * core. DRM core takes care of major-number management and char-dev
  657. * registration. A stub ->open() callback forwards any open() requests to the
  658. * registered minor.
  659. */
  660. static int drm_stub_open(struct inode *inode, struct file *filp)
  661. {
  662. const struct file_operations *new_fops;
  663. struct drm_minor *minor;
  664. int err;
  665. DRM_DEBUG("\n");
  666. mutex_lock(&drm_global_mutex);
  667. minor = drm_minor_acquire(iminor(inode));
  668. if (IS_ERR(minor)) {
  669. err = PTR_ERR(minor);
  670. goto out_unlock;
  671. }
  672. new_fops = fops_get(minor->dev->driver->fops);
  673. if (!new_fops) {
  674. err = -ENODEV;
  675. goto out_release;
  676. }
  677. replace_fops(filp, new_fops);
  678. if (filp->f_op->open)
  679. err = filp->f_op->open(inode, filp);
  680. else
  681. err = 0;
  682. out_release:
  683. drm_minor_release(minor);
  684. out_unlock:
  685. mutex_unlock(&drm_global_mutex);
  686. return err;
  687. }
  688. static const struct file_operations drm_stub_fops = {
  689. .owner = THIS_MODULE,
  690. .open = drm_stub_open,
  691. .llseek = noop_llseek,
  692. };
  693. static int __init drm_core_init(void)
  694. {
  695. int ret = -ENOMEM;
  696. drm_global_init();
  697. drm_connector_ida_init();
  698. idr_init(&drm_minors_idr);
  699. if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
  700. goto err_p1;
  701. ret = drm_sysfs_init();
  702. if (ret < 0) {
  703. printk(KERN_ERR "DRM: Error creating drm class.\n");
  704. goto err_p2;
  705. }
  706. drm_debugfs_root = debugfs_create_dir("dri", NULL);
  707. if (!drm_debugfs_root) {
  708. DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
  709. ret = -1;
  710. goto err_p3;
  711. }
  712. DRM_INFO("Initialized %s %d.%d.%d %s\n",
  713. CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  714. return 0;
  715. err_p3:
  716. drm_sysfs_destroy();
  717. err_p2:
  718. unregister_chrdev(DRM_MAJOR, "drm");
  719. idr_destroy(&drm_minors_idr);
  720. err_p1:
  721. return ret;
  722. }
  723. static void __exit drm_core_exit(void)
  724. {
  725. debugfs_remove(drm_debugfs_root);
  726. drm_sysfs_destroy();
  727. unregister_chrdev(DRM_MAJOR, "drm");
  728. drm_connector_ida_destroy();
  729. idr_destroy(&drm_minors_idr);
  730. }
  731. module_init(drm_core_init);
  732. module_exit(drm_core_exit);