drm_drv.c 24 KB

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