drm_drv.c 25 KB

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