drm_stub.c 22 KB

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