drm_drv.c 22 KB

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