drm_drv.c 22 KB

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