drm_drv.c 22 KB

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