drm_drv.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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; /* bitmask of DRM_UT_x */
  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_param_named(debug, drm_debug, int, 0600);
  45. static DEFINE_SPINLOCK(drm_minor_lock);
  46. static struct idr drm_minors_idr;
  47. static struct dentry *drm_debugfs_root;
  48. void drm_err(const char *format, ...)
  49. {
  50. struct va_format vaf;
  51. va_list args;
  52. va_start(args, format);
  53. vaf.fmt = format;
  54. vaf.va = &args;
  55. printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
  56. __builtin_return_address(0), &vaf);
  57. va_end(args);
  58. }
  59. EXPORT_SYMBOL(drm_err);
  60. void drm_ut_debug_printk(const char *function_name, const char *format, ...)
  61. {
  62. struct va_format vaf;
  63. va_list args;
  64. va_start(args, format);
  65. vaf.fmt = format;
  66. vaf.va = &args;
  67. printk(KERN_DEBUG "[" DRM_NAME ":%s] %pV", function_name, &vaf);
  68. va_end(args);
  69. }
  70. EXPORT_SYMBOL(drm_ut_debug_printk);
  71. struct drm_master *drm_master_create(struct drm_minor *minor)
  72. {
  73. struct drm_master *master;
  74. master = kzalloc(sizeof(*master), GFP_KERNEL);
  75. if (!master)
  76. return NULL;
  77. kref_init(&master->refcount);
  78. spin_lock_init(&master->lock.spinlock);
  79. init_waitqueue_head(&master->lock.lock_queue);
  80. idr_init(&master->magic_map);
  81. master->minor = minor;
  82. return master;
  83. }
  84. struct drm_master *drm_master_get(struct drm_master *master)
  85. {
  86. kref_get(&master->refcount);
  87. return master;
  88. }
  89. EXPORT_SYMBOL(drm_master_get);
  90. static void drm_master_destroy(struct kref *kref)
  91. {
  92. struct drm_master *master = container_of(kref, struct drm_master, refcount);
  93. struct drm_device *dev = master->minor->dev;
  94. struct drm_map_list *r_list, *list_temp;
  95. mutex_lock(&dev->struct_mutex);
  96. if (dev->driver->master_destroy)
  97. dev->driver->master_destroy(dev, master);
  98. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  99. if (r_list->master == master) {
  100. drm_legacy_rmmap_locked(dev, r_list->map);
  101. r_list = NULL;
  102. }
  103. }
  104. mutex_unlock(&dev->struct_mutex);
  105. idr_destroy(&master->magic_map);
  106. kfree(master->unique);
  107. kfree(master);
  108. }
  109. void drm_master_put(struct drm_master **master)
  110. {
  111. kref_put(&(*master)->refcount, drm_master_destroy);
  112. *master = NULL;
  113. }
  114. EXPORT_SYMBOL(drm_master_put);
  115. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  116. struct drm_file *file_priv)
  117. {
  118. int ret = 0;
  119. mutex_lock(&dev->master_mutex);
  120. if (file_priv->is_master)
  121. goto out_unlock;
  122. if (file_priv->minor->master) {
  123. ret = -EINVAL;
  124. goto out_unlock;
  125. }
  126. if (!file_priv->master) {
  127. ret = -EINVAL;
  128. goto out_unlock;
  129. }
  130. if (!file_priv->allowed_master) {
  131. ret = drm_new_set_master(dev, file_priv);
  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. * DOC: driver instance overview
  330. *
  331. * A device instance for a drm driver is represented by struct &drm_device. This
  332. * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
  333. * callbacks implemented by the driver. The driver then needs to initialize all
  334. * the various subsystems for the drm device like memory management, vblank
  335. * handling, modesetting support and intial output configuration plus obviously
  336. * initialize all the corresponding hardware bits. An important part of this is
  337. * also calling drm_dev_set_unique() to set the userspace-visible unique name of
  338. * this device instance. Finally when everything is up and running and ready for
  339. * userspace the device instance can be published using drm_dev_register().
  340. *
  341. * There is also deprecated support for initalizing device instances using
  342. * bus-specific helpers and the ->load() callback. But due to
  343. * backwards-compatibility needs the device instance have to be published too
  344. * early, which requires unpretty global locking to make safe and is therefore
  345. * only support for existing drivers not yet converted to the new scheme.
  346. *
  347. * When cleaning up a device instance everything needs to be done in reverse:
  348. * First unpublish the device instance with drm_dev_unregister(). Then clean up
  349. * any other resources allocated at device initialization and drop the driver's
  350. * reference to &drm_device using drm_dev_unref().
  351. *
  352. * Note that the lifetime rules for &drm_device instance has still a lot of
  353. * historical baggage. Hence use the reference counting provided by
  354. * drm_dev_ref() and drm_dev_unref() only carefully.
  355. *
  356. * Also note that embedding of &drm_device is currently not (yet) supported (but
  357. * it would be easy to add). Drivers can store driver-private data in the
  358. * dev_priv field of &drm_device.
  359. */
  360. /**
  361. * drm_put_dev - Unregister and release a DRM device
  362. * @dev: DRM device
  363. *
  364. * Called at module unload time or when a PCI device is unplugged.
  365. *
  366. * Cleans up all DRM device, calling drm_lastclose().
  367. *
  368. * Note: Use of this function is deprecated. It will eventually go away
  369. * completely. Please use drm_dev_unregister() and drm_dev_unref() explicitly
  370. * instead to make sure that the device isn't userspace accessible any more
  371. * while teardown is in progress, ensuring that userspace can't access an
  372. * inconsistent state.
  373. */
  374. void drm_put_dev(struct drm_device *dev)
  375. {
  376. DRM_DEBUG("\n");
  377. if (!dev) {
  378. DRM_ERROR("cleanup called no dev\n");
  379. return;
  380. }
  381. drm_dev_unregister(dev);
  382. drm_dev_unref(dev);
  383. }
  384. EXPORT_SYMBOL(drm_put_dev);
  385. void drm_unplug_dev(struct drm_device *dev)
  386. {
  387. /* for a USB device */
  388. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  389. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  390. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  391. mutex_lock(&drm_global_mutex);
  392. drm_device_set_unplugged(dev);
  393. if (dev->open_count == 0) {
  394. drm_put_dev(dev);
  395. }
  396. mutex_unlock(&drm_global_mutex);
  397. }
  398. EXPORT_SYMBOL(drm_unplug_dev);
  399. /*
  400. * DRM internal mount
  401. * We want to be able to allocate our own "struct address_space" to control
  402. * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
  403. * stand-alone address_space objects, so we need an underlying inode. As there
  404. * is no way to allocate an independent inode easily, we need a fake internal
  405. * VFS mount-point.
  406. *
  407. * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
  408. * frees it again. You are allowed to use iget() and iput() to get references to
  409. * the inode. But each drm_fs_inode_new() call must be paired with exactly one
  410. * drm_fs_inode_free() call (which does not have to be the last iput()).
  411. * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
  412. * between multiple inode-users. You could, technically, call
  413. * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
  414. * iput(), but this way you'd end up with a new vfsmount for each inode.
  415. */
  416. static int drm_fs_cnt;
  417. static struct vfsmount *drm_fs_mnt;
  418. static const struct dentry_operations drm_fs_dops = {
  419. .d_dname = simple_dname,
  420. };
  421. static const struct super_operations drm_fs_sops = {
  422. .statfs = simple_statfs,
  423. };
  424. static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
  425. const char *dev_name, void *data)
  426. {
  427. return mount_pseudo(fs_type,
  428. "drm:",
  429. &drm_fs_sops,
  430. &drm_fs_dops,
  431. 0x010203ff);
  432. }
  433. static struct file_system_type drm_fs_type = {
  434. .name = "drm",
  435. .owner = THIS_MODULE,
  436. .mount = drm_fs_mount,
  437. .kill_sb = kill_anon_super,
  438. };
  439. static struct inode *drm_fs_inode_new(void)
  440. {
  441. struct inode *inode;
  442. int r;
  443. r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
  444. if (r < 0) {
  445. DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
  446. return ERR_PTR(r);
  447. }
  448. inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
  449. if (IS_ERR(inode))
  450. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  451. return inode;
  452. }
  453. static void drm_fs_inode_free(struct inode *inode)
  454. {
  455. if (inode) {
  456. iput(inode);
  457. simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
  458. }
  459. }
  460. /**
  461. * drm_dev_alloc - Allocate new DRM device
  462. * @driver: DRM driver to allocate device for
  463. * @parent: Parent device object
  464. *
  465. * Allocate and initialize a new DRM device. No device registration is done.
  466. * Call drm_dev_register() to advertice the device to user space and register it
  467. * with other core subsystems. This should be done last in the device
  468. * initialization sequence to make sure userspace can't access an inconsistent
  469. * state.
  470. *
  471. * The initial ref-count of the object is 1. Use drm_dev_ref() and
  472. * drm_dev_unref() to take and drop further ref-counts.
  473. *
  474. * Note that for purely virtual devices @parent can be NULL.
  475. *
  476. * RETURNS:
  477. * Pointer to new DRM device, or NULL if out of memory.
  478. */
  479. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  480. struct device *parent)
  481. {
  482. struct drm_device *dev;
  483. int ret;
  484. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  485. if (!dev)
  486. return NULL;
  487. kref_init(&dev->ref);
  488. dev->dev = parent;
  489. dev->driver = driver;
  490. INIT_LIST_HEAD(&dev->filelist);
  491. INIT_LIST_HEAD(&dev->ctxlist);
  492. INIT_LIST_HEAD(&dev->vmalist);
  493. INIT_LIST_HEAD(&dev->maplist);
  494. INIT_LIST_HEAD(&dev->vblank_event_list);
  495. spin_lock_init(&dev->buf_lock);
  496. spin_lock_init(&dev->event_lock);
  497. mutex_init(&dev->struct_mutex);
  498. mutex_init(&dev->ctxlist_mutex);
  499. mutex_init(&dev->master_mutex);
  500. dev->anon_inode = drm_fs_inode_new();
  501. if (IS_ERR(dev->anon_inode)) {
  502. ret = PTR_ERR(dev->anon_inode);
  503. DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
  504. goto err_free;
  505. }
  506. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  507. ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
  508. if (ret)
  509. goto err_minors;
  510. WARN_ON(driver->suspend || driver->resume);
  511. }
  512. if (drm_core_check_feature(dev, DRIVER_RENDER)) {
  513. ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
  514. if (ret)
  515. goto err_minors;
  516. }
  517. ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
  518. if (ret)
  519. goto err_minors;
  520. if (drm_ht_create(&dev->map_hash, 12))
  521. goto err_minors;
  522. drm_legacy_ctxbitmap_init(dev);
  523. if (drm_core_check_feature(dev, DRIVER_GEM)) {
  524. ret = drm_gem_init(dev);
  525. if (ret) {
  526. DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  527. goto err_ctxbitmap;
  528. }
  529. }
  530. if (parent) {
  531. ret = drm_dev_set_unique(dev, dev_name(parent));
  532. if (ret)
  533. goto err_setunique;
  534. }
  535. return dev;
  536. err_setunique:
  537. if (drm_core_check_feature(dev, DRIVER_GEM))
  538. drm_gem_destroy(dev);
  539. err_ctxbitmap:
  540. drm_legacy_ctxbitmap_cleanup(dev);
  541. drm_ht_remove(&dev->map_hash);
  542. err_minors:
  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. drm_fs_inode_free(dev->anon_inode);
  547. err_free:
  548. mutex_destroy(&dev->master_mutex);
  549. kfree(dev);
  550. return NULL;
  551. }
  552. EXPORT_SYMBOL(drm_dev_alloc);
  553. static void drm_dev_release(struct kref *ref)
  554. {
  555. struct drm_device *dev = container_of(ref, struct drm_device, ref);
  556. if (drm_core_check_feature(dev, DRIVER_GEM))
  557. drm_gem_destroy(dev);
  558. drm_legacy_ctxbitmap_cleanup(dev);
  559. drm_ht_remove(&dev->map_hash);
  560. drm_fs_inode_free(dev->anon_inode);
  561. drm_minor_free(dev, DRM_MINOR_LEGACY);
  562. drm_minor_free(dev, DRM_MINOR_RENDER);
  563. drm_minor_free(dev, DRM_MINOR_CONTROL);
  564. mutex_destroy(&dev->master_mutex);
  565. kfree(dev->unique);
  566. kfree(dev);
  567. }
  568. /**
  569. * drm_dev_ref - Take reference of a DRM device
  570. * @dev: device to take reference of or NULL
  571. *
  572. * This increases the ref-count of @dev by one. You *must* already own a
  573. * reference when calling this. Use drm_dev_unref() to drop this reference
  574. * again.
  575. *
  576. * This function never fails. However, this function does not provide *any*
  577. * guarantee whether the device is alive or running. It only provides a
  578. * reference to the object and the memory associated with it.
  579. */
  580. void drm_dev_ref(struct drm_device *dev)
  581. {
  582. if (dev)
  583. kref_get(&dev->ref);
  584. }
  585. EXPORT_SYMBOL(drm_dev_ref);
  586. /**
  587. * drm_dev_unref - Drop reference of a DRM device
  588. * @dev: device to drop reference of or NULL
  589. *
  590. * This decreases the ref-count of @dev by one. The device is destroyed if the
  591. * ref-count drops to zero.
  592. */
  593. void drm_dev_unref(struct drm_device *dev)
  594. {
  595. if (dev)
  596. kref_put(&dev->ref, drm_dev_release);
  597. }
  598. EXPORT_SYMBOL(drm_dev_unref);
  599. /**
  600. * drm_dev_register - Register DRM device
  601. * @dev: Device to register
  602. * @flags: Flags passed to the driver's .load() function
  603. *
  604. * Register the DRM device @dev with the system, advertise device to user-space
  605. * and start normal device operation. @dev must be allocated via drm_dev_alloc()
  606. * previously.
  607. *
  608. * Never call this twice on any device!
  609. *
  610. * NOTE: To ensure backward compatibility with existing drivers method this
  611. * function calls the ->load() method after registering the device nodes,
  612. * creating race conditions. Usage of the ->load() methods is therefore
  613. * deprecated, drivers must perform all initialization before calling
  614. * drm_dev_register().
  615. *
  616. * RETURNS:
  617. * 0 on success, negative error code on failure.
  618. */
  619. int drm_dev_register(struct drm_device *dev, unsigned long flags)
  620. {
  621. int ret;
  622. mutex_lock(&drm_global_mutex);
  623. ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
  624. if (ret)
  625. goto err_minors;
  626. ret = drm_minor_register(dev, DRM_MINOR_RENDER);
  627. if (ret)
  628. goto err_minors;
  629. ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
  630. if (ret)
  631. goto err_minors;
  632. if (dev->driver->load) {
  633. ret = dev->driver->load(dev, flags);
  634. if (ret)
  635. goto err_minors;
  636. }
  637. ret = 0;
  638. goto out_unlock;
  639. err_minors:
  640. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  641. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  642. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  643. out_unlock:
  644. mutex_unlock(&drm_global_mutex);
  645. return ret;
  646. }
  647. EXPORT_SYMBOL(drm_dev_register);
  648. /**
  649. * drm_dev_unregister - Unregister DRM device
  650. * @dev: Device to unregister
  651. *
  652. * Unregister the DRM device from the system. This does the reverse of
  653. * drm_dev_register() but does not deallocate the device. The caller must call
  654. * drm_dev_unref() to drop their final reference.
  655. *
  656. * This should be called first in the device teardown code to make sure
  657. * userspace can't access the device instance any more.
  658. */
  659. void drm_dev_unregister(struct drm_device *dev)
  660. {
  661. struct drm_map_list *r_list, *list_temp;
  662. drm_lastclose(dev);
  663. if (dev->driver->unload)
  664. dev->driver->unload(dev);
  665. if (dev->agp)
  666. drm_pci_agp_destroy(dev);
  667. drm_vblank_cleanup(dev);
  668. list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
  669. drm_legacy_rmmap(dev, r_list->map);
  670. drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  671. drm_minor_unregister(dev, DRM_MINOR_RENDER);
  672. drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  673. }
  674. EXPORT_SYMBOL(drm_dev_unregister);
  675. /**
  676. * drm_dev_set_unique - Set the unique name of a DRM device
  677. * @dev: device of which to set the unique name
  678. * @name: unique name
  679. *
  680. * Sets the unique name of a DRM device using the specified string. Drivers
  681. * can use this at driver probe time if the unique name of the devices they
  682. * drive is static.
  683. *
  684. * Return: 0 on success or a negative error code on failure.
  685. */
  686. int drm_dev_set_unique(struct drm_device *dev, const char *name)
  687. {
  688. kfree(dev->unique);
  689. dev->unique = kstrdup(name, GFP_KERNEL);
  690. return dev->unique ? 0 : -ENOMEM;
  691. }
  692. EXPORT_SYMBOL(drm_dev_set_unique);
  693. /*
  694. * DRM Core
  695. * The DRM core module initializes all global DRM objects and makes them
  696. * available to drivers. Once setup, drivers can probe their respective
  697. * devices.
  698. * Currently, core management includes:
  699. * - The "DRM-Global" key/value database
  700. * - Global ID management for connectors
  701. * - DRM major number allocation
  702. * - DRM minor management
  703. * - DRM sysfs class
  704. * - DRM debugfs root
  705. *
  706. * Furthermore, the DRM core provides dynamic char-dev lookups. For each
  707. * interface registered on a DRM device, you can request minor numbers from DRM
  708. * core. DRM core takes care of major-number management and char-dev
  709. * registration. A stub ->open() callback forwards any open() requests to the
  710. * registered minor.
  711. */
  712. static int drm_stub_open(struct inode *inode, struct file *filp)
  713. {
  714. const struct file_operations *new_fops;
  715. struct drm_minor *minor;
  716. int err;
  717. DRM_DEBUG("\n");
  718. mutex_lock(&drm_global_mutex);
  719. minor = drm_minor_acquire(iminor(inode));
  720. if (IS_ERR(minor)) {
  721. err = PTR_ERR(minor);
  722. goto out_unlock;
  723. }
  724. new_fops = fops_get(minor->dev->driver->fops);
  725. if (!new_fops) {
  726. err = -ENODEV;
  727. goto out_release;
  728. }
  729. replace_fops(filp, new_fops);
  730. if (filp->f_op->open)
  731. err = filp->f_op->open(inode, filp);
  732. else
  733. err = 0;
  734. out_release:
  735. drm_minor_release(minor);
  736. out_unlock:
  737. mutex_unlock(&drm_global_mutex);
  738. return err;
  739. }
  740. static const struct file_operations drm_stub_fops = {
  741. .owner = THIS_MODULE,
  742. .open = drm_stub_open,
  743. .llseek = noop_llseek,
  744. };
  745. static int __init drm_core_init(void)
  746. {
  747. int ret = -ENOMEM;
  748. drm_global_init();
  749. drm_connector_ida_init();
  750. idr_init(&drm_minors_idr);
  751. if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
  752. goto err_p1;
  753. ret = drm_sysfs_init();
  754. if (ret < 0) {
  755. printk(KERN_ERR "DRM: Error creating drm class.\n");
  756. goto err_p2;
  757. }
  758. drm_debugfs_root = debugfs_create_dir("dri", NULL);
  759. if (!drm_debugfs_root) {
  760. DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
  761. ret = -1;
  762. goto err_p3;
  763. }
  764. DRM_INFO("Initialized %s %d.%d.%d %s\n",
  765. CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  766. return 0;
  767. err_p3:
  768. drm_sysfs_destroy();
  769. err_p2:
  770. unregister_chrdev(DRM_MAJOR, "drm");
  771. idr_destroy(&drm_minors_idr);
  772. err_p1:
  773. return ret;
  774. }
  775. static void __exit drm_core_exit(void)
  776. {
  777. debugfs_remove(drm_debugfs_root);
  778. drm_sysfs_destroy();
  779. unregister_chrdev(DRM_MAJOR, "drm");
  780. drm_connector_ida_destroy();
  781. idr_destroy(&drm_minors_idr);
  782. }
  783. module_init(drm_core_init);
  784. module_exit(drm_core_exit);