drm_drv.c 22 KB

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