drm_drv.c 23 KB

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