drm_stub.c 20 KB

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