drm_file.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  3. * \author Daryll Strauss <daryll@valinux.com>
  4. * \author Gareth Hughes <gareth@valinux.com>
  5. */
  6. /*
  7. * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
  8. *
  9. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  10. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  11. * All Rights Reserved.
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice (including the next
  21. * paragraph) shall be included in all copies or substantial portions of the
  22. * Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. * OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. #include <linux/anon_inodes.h>
  33. #include <linux/dma-fence.h>
  34. #include <linux/export.h>
  35. #include <linux/file.h>
  36. #include <linux/module.h>
  37. #include <linux/pci.h>
  38. #include <linux/poll.h>
  39. #include <linux/slab.h>
  40. #include <linux/vga_switcheroo.h>
  41. #include <drm/drm_client_event.h>
  42. #include <drm/drm_drv.h>
  43. #include <drm/drm_file.h>
  44. #include <drm/drm_gem.h>
  45. #include <drm/drm_print.h>
  46. #include <drm/drm_debugfs.h>
  47. #include "drm_crtc_internal.h"
  48. #include "drm_internal.h"
  49. /* from BKL pushdown */
  50. DEFINE_MUTEX(drm_global_mutex);
  51. bool drm_dev_needs_global_mutex(struct drm_device *dev)
  52. {
  53. /*
  54. * The deprecated ->load callback must be called after the driver is
  55. * already registered. This means such drivers rely on the BKL to make
  56. * sure an open can't proceed until the driver is actually fully set up.
  57. * Similar hilarity holds for the unload callback.
  58. */
  59. if (dev->driver->load || dev->driver->unload)
  60. return true;
  61. return false;
  62. }
  63. /**
  64. * DOC: file operations
  65. *
  66. * Drivers must define the file operations structure that forms the DRM
  67. * userspace API entry point, even though most of those operations are
  68. * implemented in the DRM core. The resulting &struct file_operations must be
  69. * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
  70. * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
  71. * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
  72. * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
  73. * that require 32/64 bit compatibility support must provide their own
  74. * &file_operations.compat_ioctl handler that processes private ioctls and calls
  75. * drm_compat_ioctl() for core ioctls.
  76. *
  77. * In addition drm_read() and drm_poll() provide support for DRM events. DRM
  78. * events are a generic and extensible means to send asynchronous events to
  79. * userspace through the file descriptor. They are used to send vblank event and
  80. * page flip completions by the KMS API. But drivers can also use it for their
  81. * own needs, e.g. to signal completion of rendering.
  82. *
  83. * For the driver-side event interface see drm_event_reserve_init() and
  84. * drm_send_event() as the main starting points.
  85. *
  86. * The memory mapping implementation will vary depending on how the driver
  87. * manages memory. For GEM-based drivers this is drm_gem_mmap().
  88. *
  89. * No other file operations are supported by the DRM userspace API. Overall the
  90. * following is an example &file_operations structure::
  91. *
  92. * static const example_drm_fops = {
  93. * .owner = THIS_MODULE,
  94. * .open = drm_open,
  95. * .release = drm_release,
  96. * .unlocked_ioctl = drm_ioctl,
  97. * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
  98. * .poll = drm_poll,
  99. * .read = drm_read,
  100. * .mmap = drm_gem_mmap,
  101. * };
  102. *
  103. * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
  104. * DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS() macro to make this
  105. * simpler.
  106. *
  107. * The driver's &file_operations must be stored in &drm_driver.fops.
  108. *
  109. * For driver-private IOCTL handling see the more detailed discussion in
  110. * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
  111. */
  112. /**
  113. * drm_file_alloc - allocate file context
  114. * @minor: minor to allocate on
  115. *
  116. * This allocates a new DRM file context. It is not linked into any context and
  117. * can be used by the caller freely. Note that the context keeps a pointer to
  118. * @minor, so it must be freed before @minor is.
  119. *
  120. * RETURNS:
  121. * Pointer to newly allocated context, ERR_PTR on failure.
  122. */
  123. struct drm_file *drm_file_alloc(struct drm_minor *minor)
  124. {
  125. static atomic64_t ident = ATOMIC64_INIT(0);
  126. struct drm_device *dev = minor->dev;
  127. struct drm_file *file;
  128. int ret;
  129. file = kzalloc_obj(*file);
  130. if (!file)
  131. return ERR_PTR(-ENOMEM);
  132. /* Get a unique identifier for fdinfo: */
  133. file->client_id = atomic64_inc_return(&ident);
  134. rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
  135. file->minor = minor;
  136. /* for compatibility root is always authenticated */
  137. file->authenticated = capable(CAP_SYS_ADMIN);
  138. INIT_LIST_HEAD(&file->lhead);
  139. INIT_LIST_HEAD(&file->fbs);
  140. mutex_init(&file->fbs_lock);
  141. INIT_LIST_HEAD(&file->blobs);
  142. INIT_LIST_HEAD(&file->pending_event_list);
  143. INIT_LIST_HEAD(&file->event_list);
  144. init_waitqueue_head(&file->event_wait);
  145. file->event_space = 4096; /* set aside 4k for event buffer */
  146. spin_lock_init(&file->master_lookup_lock);
  147. mutex_init(&file->event_read_lock);
  148. mutex_init(&file->client_name_lock);
  149. if (drm_core_check_feature(dev, DRIVER_GEM))
  150. drm_gem_open(dev, file);
  151. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  152. drm_syncobj_open(file);
  153. drm_prime_init_file_private(&file->prime);
  154. if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
  155. drm_debugfs_clients_add(file);
  156. if (dev->driver->open) {
  157. ret = dev->driver->open(dev, file);
  158. if (ret < 0)
  159. goto out_prime_destroy;
  160. }
  161. return file;
  162. out_prime_destroy:
  163. drm_prime_destroy_file_private(&file->prime);
  164. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  165. drm_syncobj_release(file);
  166. if (drm_core_check_feature(dev, DRIVER_GEM))
  167. drm_gem_release(dev, file);
  168. if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
  169. drm_debugfs_clients_remove(file);
  170. put_pid(rcu_access_pointer(file->pid));
  171. kfree(file);
  172. return ERR_PTR(ret);
  173. }
  174. static void drm_events_release(struct drm_file *file_priv)
  175. {
  176. struct drm_device *dev = file_priv->minor->dev;
  177. struct drm_pending_event *e, *et;
  178. unsigned long flags;
  179. spin_lock_irqsave(&dev->event_lock, flags);
  180. /* Unlink pending events */
  181. list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
  182. pending_link) {
  183. list_del(&e->pending_link);
  184. e->file_priv = NULL;
  185. }
  186. /* Remove unconsumed events */
  187. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  188. list_del(&e->link);
  189. kfree(e);
  190. }
  191. spin_unlock_irqrestore(&dev->event_lock, flags);
  192. }
  193. /**
  194. * drm_file_free - free file context
  195. * @file: context to free, or NULL
  196. *
  197. * This destroys and deallocates a DRM file context previously allocated via
  198. * drm_file_alloc(). The caller must make sure to unlink it from any contexts
  199. * before calling this.
  200. *
  201. * If NULL is passed, this is a no-op.
  202. */
  203. void drm_file_free(struct drm_file *file)
  204. {
  205. struct drm_device *dev;
  206. if (!file)
  207. return;
  208. dev = file->minor->dev;
  209. drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n",
  210. current->comm, task_pid_nr(current),
  211. (long)old_encode_dev(file->minor->kdev->devt),
  212. atomic_read(&dev->open_count));
  213. if (!drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
  214. drm_debugfs_clients_remove(file);
  215. drm_events_release(file);
  216. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  217. drm_fb_release(file);
  218. drm_property_destroy_user_blobs(dev, file);
  219. }
  220. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  221. drm_syncobj_release(file);
  222. if (drm_core_check_feature(dev, DRIVER_GEM))
  223. drm_gem_release(dev, file);
  224. if (drm_is_primary_client(file))
  225. drm_master_release(file);
  226. if (dev->driver->postclose)
  227. dev->driver->postclose(dev, file);
  228. drm_prime_destroy_file_private(&file->prime);
  229. WARN_ON(!list_empty(&file->event_list));
  230. put_pid(rcu_access_pointer(file->pid));
  231. mutex_destroy(&file->client_name_lock);
  232. kfree(file->client_name);
  233. kfree(file);
  234. }
  235. static void drm_close_helper(struct file *filp)
  236. {
  237. struct drm_file *file_priv = filp->private_data;
  238. struct drm_device *dev = file_priv->minor->dev;
  239. mutex_lock(&dev->filelist_mutex);
  240. list_del(&file_priv->lhead);
  241. mutex_unlock(&dev->filelist_mutex);
  242. drm_file_free(file_priv);
  243. }
  244. /*
  245. * Check whether DRI will run on this CPU.
  246. *
  247. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  248. */
  249. static int drm_cpu_valid(void)
  250. {
  251. #if defined(__sparc__) && !defined(__sparc_v9__)
  252. return 0; /* No cmpxchg before v9 sparc. */
  253. #endif
  254. return 1;
  255. }
  256. /*
  257. * Called whenever a process opens a drm node
  258. *
  259. * \param filp file pointer.
  260. * \param minor acquired minor-object.
  261. * \return zero on success or a negative number on failure.
  262. *
  263. * Creates and initializes a drm_file structure for the file private data in \p
  264. * filp and add it into the double linked list in \p dev.
  265. */
  266. int drm_open_helper(struct file *filp, struct drm_minor *minor)
  267. {
  268. struct drm_device *dev = minor->dev;
  269. struct drm_file *priv;
  270. int ret;
  271. if (filp->f_flags & O_EXCL)
  272. return -EBUSY; /* No exclusive opens */
  273. if (!drm_cpu_valid())
  274. return -EINVAL;
  275. if (dev->switch_power_state != DRM_SWITCH_POWER_ON &&
  276. dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  277. return -EINVAL;
  278. if (WARN_ON_ONCE(!(filp->f_op->fop_flags & FOP_UNSIGNED_OFFSET)))
  279. return -EINVAL;
  280. drm_dbg_core(dev, "comm=\"%s\", pid=%d, minor=%d\n",
  281. current->comm, task_pid_nr(current), minor->index);
  282. priv = drm_file_alloc(minor);
  283. if (IS_ERR(priv))
  284. return PTR_ERR(priv);
  285. if (drm_is_primary_client(priv)) {
  286. ret = drm_master_open(priv);
  287. if (ret) {
  288. drm_file_free(priv);
  289. return ret;
  290. }
  291. }
  292. filp->private_data = priv;
  293. priv->filp = filp;
  294. mutex_lock(&dev->filelist_mutex);
  295. list_add(&priv->lhead, &dev->filelist);
  296. mutex_unlock(&dev->filelist_mutex);
  297. return 0;
  298. }
  299. /**
  300. * drm_open - open method for DRM file
  301. * @inode: device inode
  302. * @filp: file pointer.
  303. *
  304. * This function must be used by drivers as their &file_operations.open method.
  305. * It looks up the correct DRM device and instantiates all the per-file
  306. * resources for it. It also calls the &drm_driver.open driver callback.
  307. *
  308. * RETURNS:
  309. * 0 on success or negative errno value on failure.
  310. */
  311. int drm_open(struct inode *inode, struct file *filp)
  312. {
  313. struct drm_device *dev;
  314. struct drm_minor *minor;
  315. int retcode;
  316. minor = drm_minor_acquire(&drm_minors_xa, iminor(inode));
  317. if (IS_ERR(minor))
  318. return PTR_ERR(minor);
  319. dev = minor->dev;
  320. if (drm_dev_needs_global_mutex(dev))
  321. mutex_lock(&drm_global_mutex);
  322. atomic_fetch_inc(&dev->open_count);
  323. /* share address_space across all char-devs of a single device */
  324. filp->f_mapping = dev->anon_inode->i_mapping;
  325. retcode = drm_open_helper(filp, minor);
  326. if (retcode)
  327. goto err_undo;
  328. if (drm_dev_needs_global_mutex(dev))
  329. mutex_unlock(&drm_global_mutex);
  330. return 0;
  331. err_undo:
  332. atomic_dec(&dev->open_count);
  333. if (drm_dev_needs_global_mutex(dev))
  334. mutex_unlock(&drm_global_mutex);
  335. drm_minor_release(minor);
  336. return retcode;
  337. }
  338. EXPORT_SYMBOL(drm_open);
  339. static void drm_lastclose(struct drm_device *dev)
  340. {
  341. drm_client_dev_restore(dev, false);
  342. if (dev_is_pci(dev->dev))
  343. vga_switcheroo_process_delayed_switch();
  344. }
  345. /**
  346. * drm_release - release method for DRM file
  347. * @inode: device inode
  348. * @filp: file pointer.
  349. *
  350. * This function must be used by drivers as their &file_operations.release
  351. * method. It frees any resources associated with the open file. If this
  352. * is the last open file for the DRM device, it also restores the active
  353. * in-kernel DRM client.
  354. *
  355. * RETURNS:
  356. * Always succeeds and returns 0.
  357. */
  358. int drm_release(struct inode *inode, struct file *filp)
  359. {
  360. struct drm_file *file_priv = filp->private_data;
  361. struct drm_minor *minor = file_priv->minor;
  362. struct drm_device *dev = minor->dev;
  363. if (drm_dev_needs_global_mutex(dev))
  364. mutex_lock(&drm_global_mutex);
  365. drm_dbg_core(dev, "open_count = %d\n", atomic_read(&dev->open_count));
  366. drm_close_helper(filp);
  367. if (atomic_dec_and_test(&dev->open_count))
  368. drm_lastclose(dev);
  369. if (drm_dev_needs_global_mutex(dev))
  370. mutex_unlock(&drm_global_mutex);
  371. drm_minor_release(minor);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(drm_release);
  375. void drm_file_update_pid(struct drm_file *filp)
  376. {
  377. struct drm_device *dev;
  378. struct pid *pid, *old;
  379. /*
  380. * Master nodes need to keep the original ownership in order for
  381. * drm_master_check_perm to keep working correctly. (See comment in
  382. * drm_auth.c.)
  383. */
  384. if (filp->was_master)
  385. return;
  386. pid = task_tgid(current);
  387. /*
  388. * Quick unlocked check since the model is a single handover followed by
  389. * exclusive repeated use.
  390. */
  391. if (pid == rcu_access_pointer(filp->pid))
  392. return;
  393. dev = filp->minor->dev;
  394. mutex_lock(&dev->filelist_mutex);
  395. get_pid(pid);
  396. old = rcu_replace_pointer(filp->pid, pid, 1);
  397. mutex_unlock(&dev->filelist_mutex);
  398. synchronize_rcu();
  399. put_pid(old);
  400. }
  401. /**
  402. * drm_release_noglobal - release method for DRM file
  403. * @inode: device inode
  404. * @filp: file pointer.
  405. *
  406. * This function may be used by drivers as their &file_operations.release
  407. * method. It frees any resources associated with the open file prior to taking
  408. * the drm_global_mutex. If this is the last open file for the DRM device, it
  409. * then restores the active in-kernel DRM client.
  410. *
  411. * RETURNS:
  412. * Always succeeds and returns 0.
  413. */
  414. int drm_release_noglobal(struct inode *inode, struct file *filp)
  415. {
  416. struct drm_file *file_priv = filp->private_data;
  417. struct drm_minor *minor = file_priv->minor;
  418. struct drm_device *dev = minor->dev;
  419. drm_close_helper(filp);
  420. if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
  421. drm_lastclose(dev);
  422. mutex_unlock(&drm_global_mutex);
  423. }
  424. drm_minor_release(minor);
  425. return 0;
  426. }
  427. EXPORT_SYMBOL(drm_release_noglobal);
  428. /**
  429. * drm_read - read method for DRM file
  430. * @filp: file pointer
  431. * @buffer: userspace destination pointer for the read
  432. * @count: count in bytes to read
  433. * @offset: offset to read
  434. *
  435. * This function must be used by drivers as their &file_operations.read
  436. * method if they use DRM events for asynchronous signalling to userspace.
  437. * Since events are used by the KMS API for vblank and page flip completion this
  438. * means all modern display drivers must use it.
  439. *
  440. * @offset is ignored, DRM events are read like a pipe. Polling support is
  441. * provided by drm_poll().
  442. *
  443. * This function will only ever read a full event. Therefore userspace must
  444. * supply a big enough buffer to fit any event to ensure forward progress. Since
  445. * the maximum event space is currently 4K it's recommended to just use that for
  446. * safety.
  447. *
  448. * RETURNS:
  449. * Number of bytes read (always aligned to full events, and can be 0) or a
  450. * negative error code on failure.
  451. */
  452. ssize_t drm_read(struct file *filp, char __user *buffer,
  453. size_t count, loff_t *offset)
  454. {
  455. struct drm_file *file_priv = filp->private_data;
  456. struct drm_device *dev = file_priv->minor->dev;
  457. ssize_t ret;
  458. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  459. if (ret)
  460. return ret;
  461. for (;;) {
  462. struct drm_pending_event *e = NULL;
  463. spin_lock_irq(&dev->event_lock);
  464. if (!list_empty(&file_priv->event_list)) {
  465. e = list_first_entry(&file_priv->event_list,
  466. struct drm_pending_event, link);
  467. file_priv->event_space += e->event->length;
  468. list_del(&e->link);
  469. }
  470. spin_unlock_irq(&dev->event_lock);
  471. if (e == NULL) {
  472. if (ret)
  473. break;
  474. if (filp->f_flags & O_NONBLOCK) {
  475. ret = -EAGAIN;
  476. break;
  477. }
  478. mutex_unlock(&file_priv->event_read_lock);
  479. ret = wait_event_interruptible(file_priv->event_wait,
  480. !list_empty(&file_priv->event_list));
  481. if (ret >= 0)
  482. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  483. if (ret)
  484. return ret;
  485. } else {
  486. unsigned length = e->event->length;
  487. if (length > count - ret) {
  488. put_back_event:
  489. spin_lock_irq(&dev->event_lock);
  490. file_priv->event_space -= length;
  491. list_add(&e->link, &file_priv->event_list);
  492. spin_unlock_irq(&dev->event_lock);
  493. wake_up_interruptible_poll(&file_priv->event_wait,
  494. EPOLLIN | EPOLLRDNORM);
  495. break;
  496. }
  497. if (copy_to_user(buffer + ret, e->event, length)) {
  498. if (ret == 0)
  499. ret = -EFAULT;
  500. goto put_back_event;
  501. }
  502. ret += length;
  503. kfree(e);
  504. }
  505. }
  506. mutex_unlock(&file_priv->event_read_lock);
  507. return ret;
  508. }
  509. EXPORT_SYMBOL(drm_read);
  510. /**
  511. * drm_poll - poll method for DRM file
  512. * @filp: file pointer
  513. * @wait: poll waiter table
  514. *
  515. * This function must be used by drivers as their &file_operations.read method
  516. * if they use DRM events for asynchronous signalling to userspace. Since
  517. * events are used by the KMS API for vblank and page flip completion this means
  518. * all modern display drivers must use it.
  519. *
  520. * See also drm_read().
  521. *
  522. * RETURNS:
  523. * Mask of POLL flags indicating the current status of the file.
  524. */
  525. __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
  526. {
  527. struct drm_file *file_priv = filp->private_data;
  528. __poll_t mask = 0;
  529. poll_wait(filp, &file_priv->event_wait, wait);
  530. if (!list_empty(&file_priv->event_list))
  531. mask |= EPOLLIN | EPOLLRDNORM;
  532. return mask;
  533. }
  534. EXPORT_SYMBOL(drm_poll);
  535. /**
  536. * drm_event_reserve_init_locked - init a DRM event and reserve space for it
  537. * @dev: DRM device
  538. * @file_priv: DRM file private data
  539. * @p: tracking structure for the pending event
  540. * @e: actual event data to deliver to userspace
  541. *
  542. * This function prepares the passed in event for eventual delivery. If the event
  543. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  544. * anything) then the even must be cancelled and freed using
  545. * drm_event_cancel_free(). Successfully initialized events should be sent out
  546. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  547. * asynchronous event to userspace.
  548. *
  549. * If callers embedded @p into a larger structure it must be allocated with
  550. * kmalloc and @p must be the first member element.
  551. *
  552. * This is the locked version of drm_event_reserve_init() for callers which
  553. * already hold &drm_device.event_lock.
  554. *
  555. * RETURNS:
  556. * 0 on success or a negative error code on failure.
  557. */
  558. int drm_event_reserve_init_locked(struct drm_device *dev,
  559. struct drm_file *file_priv,
  560. struct drm_pending_event *p,
  561. struct drm_event *e)
  562. {
  563. if (file_priv->event_space < e->length)
  564. return -ENOMEM;
  565. file_priv->event_space -= e->length;
  566. p->event = e;
  567. list_add(&p->pending_link, &file_priv->pending_event_list);
  568. p->file_priv = file_priv;
  569. return 0;
  570. }
  571. EXPORT_SYMBOL(drm_event_reserve_init_locked);
  572. /**
  573. * drm_event_reserve_init - init a DRM event and reserve space for it
  574. * @dev: DRM device
  575. * @file_priv: DRM file private data
  576. * @p: tracking structure for the pending event
  577. * @e: actual event data to deliver to userspace
  578. *
  579. * This function prepares the passed in event for eventual delivery. If the event
  580. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  581. * anything) then the even must be cancelled and freed using
  582. * drm_event_cancel_free(). Successfully initialized events should be sent out
  583. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  584. * asynchronous event to userspace.
  585. *
  586. * If callers embedded @p into a larger structure it must be allocated with
  587. * kmalloc and @p must be the first member element.
  588. *
  589. * Callers which already hold &drm_device.event_lock should use
  590. * drm_event_reserve_init_locked() instead.
  591. *
  592. * RETURNS:
  593. * 0 on success or a negative error code on failure.
  594. */
  595. int drm_event_reserve_init(struct drm_device *dev,
  596. struct drm_file *file_priv,
  597. struct drm_pending_event *p,
  598. struct drm_event *e)
  599. {
  600. unsigned long flags;
  601. int ret;
  602. spin_lock_irqsave(&dev->event_lock, flags);
  603. ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
  604. spin_unlock_irqrestore(&dev->event_lock, flags);
  605. return ret;
  606. }
  607. EXPORT_SYMBOL(drm_event_reserve_init);
  608. /**
  609. * drm_event_cancel_free - free a DRM event and release its space
  610. * @dev: DRM device
  611. * @p: tracking structure for the pending event
  612. *
  613. * This function frees the event @p initialized with drm_event_reserve_init()
  614. * and releases any allocated space. It is used to cancel an event when the
  615. * nonblocking operation could not be submitted and needed to be aborted.
  616. */
  617. void drm_event_cancel_free(struct drm_device *dev,
  618. struct drm_pending_event *p)
  619. {
  620. unsigned long flags;
  621. spin_lock_irqsave(&dev->event_lock, flags);
  622. if (p->file_priv) {
  623. p->file_priv->event_space += p->event->length;
  624. list_del(&p->pending_link);
  625. }
  626. spin_unlock_irqrestore(&dev->event_lock, flags);
  627. if (p->fence)
  628. dma_fence_put(p->fence);
  629. kfree(p);
  630. }
  631. EXPORT_SYMBOL(drm_event_cancel_free);
  632. static void drm_send_event_helper(struct drm_device *dev,
  633. struct drm_pending_event *e, ktime_t timestamp)
  634. {
  635. assert_spin_locked(&dev->event_lock);
  636. if (e->completion) {
  637. complete_all(e->completion);
  638. e->completion_release(e->completion);
  639. e->completion = NULL;
  640. }
  641. if (e->fence) {
  642. if (timestamp)
  643. dma_fence_signal_timestamp(e->fence, timestamp);
  644. else
  645. dma_fence_signal(e->fence);
  646. dma_fence_put(e->fence);
  647. }
  648. if (!e->file_priv) {
  649. kfree(e);
  650. return;
  651. }
  652. list_del(&e->pending_link);
  653. list_add_tail(&e->link,
  654. &e->file_priv->event_list);
  655. wake_up_interruptible_poll(&e->file_priv->event_wait,
  656. EPOLLIN | EPOLLRDNORM);
  657. }
  658. /**
  659. * drm_send_event_timestamp_locked - send DRM event to file descriptor
  660. * @dev: DRM device
  661. * @e: DRM event to deliver
  662. * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
  663. * time domain
  664. *
  665. * This function sends the event @e, initialized with drm_event_reserve_init(),
  666. * to its associated userspace DRM file. Callers must already hold
  667. * &drm_device.event_lock.
  668. *
  669. * Note that the core will take care of unlinking and disarming events when the
  670. * corresponding DRM file is closed. Drivers need not worry about whether the
  671. * DRM file for this event still exists and can call this function upon
  672. * completion of the asynchronous work unconditionally.
  673. */
  674. void drm_send_event_timestamp_locked(struct drm_device *dev,
  675. struct drm_pending_event *e, ktime_t timestamp)
  676. {
  677. drm_send_event_helper(dev, e, timestamp);
  678. }
  679. EXPORT_SYMBOL(drm_send_event_timestamp_locked);
  680. /**
  681. * drm_send_event_locked - send DRM event to file descriptor
  682. * @dev: DRM device
  683. * @e: DRM event to deliver
  684. *
  685. * This function sends the event @e, initialized with drm_event_reserve_init(),
  686. * to its associated userspace DRM file. Callers must already hold
  687. * &drm_device.event_lock, see drm_send_event() for the unlocked version.
  688. *
  689. * Note that the core will take care of unlinking and disarming events when the
  690. * corresponding DRM file is closed. Drivers need not worry about whether the
  691. * DRM file for this event still exists and can call this function upon
  692. * completion of the asynchronous work unconditionally.
  693. */
  694. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
  695. {
  696. drm_send_event_helper(dev, e, 0);
  697. }
  698. EXPORT_SYMBOL(drm_send_event_locked);
  699. /**
  700. * drm_send_event - send DRM event to file descriptor
  701. * @dev: DRM device
  702. * @e: DRM event to deliver
  703. *
  704. * This function sends the event @e, initialized with drm_event_reserve_init(),
  705. * to its associated userspace DRM file. This function acquires
  706. * &drm_device.event_lock, see drm_send_event_locked() for callers which already
  707. * hold this lock.
  708. *
  709. * Note that the core will take care of unlinking and disarming events when the
  710. * corresponding DRM file is closed. Drivers need not worry about whether the
  711. * DRM file for this event still exists and can call this function upon
  712. * completion of the asynchronous work unconditionally.
  713. */
  714. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  715. {
  716. unsigned long irqflags;
  717. spin_lock_irqsave(&dev->event_lock, irqflags);
  718. drm_send_event_helper(dev, e, 0);
  719. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  720. }
  721. EXPORT_SYMBOL(drm_send_event);
  722. void drm_fdinfo_print_size(struct drm_printer *p,
  723. const char *prefix,
  724. const char *stat,
  725. const char *region,
  726. u64 sz)
  727. {
  728. const char *units[] = {"", " KiB", " MiB"};
  729. unsigned u;
  730. for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
  731. if (sz == 0 || !IS_ALIGNED(sz, SZ_1K))
  732. break;
  733. sz = div_u64(sz, SZ_1K);
  734. }
  735. drm_printf(p, "%s-%s-%s:\t%llu%s\n",
  736. prefix, stat, region, sz, units[u]);
  737. }
  738. EXPORT_SYMBOL(drm_fdinfo_print_size);
  739. int drm_memory_stats_is_zero(const struct drm_memory_stats *stats)
  740. {
  741. return (stats->shared == 0 &&
  742. stats->private == 0 &&
  743. stats->resident == 0 &&
  744. stats->purgeable == 0 &&
  745. stats->active == 0);
  746. }
  747. EXPORT_SYMBOL(drm_memory_stats_is_zero);
  748. /**
  749. * drm_print_memory_stats - A helper to print memory stats
  750. * @p: The printer to print output to
  751. * @stats: The collected memory stats
  752. * @supported_status: Bitmask of optional stats which are available
  753. * @region: The memory region
  754. *
  755. */
  756. void drm_print_memory_stats(struct drm_printer *p,
  757. const struct drm_memory_stats *stats,
  758. enum drm_gem_object_status supported_status,
  759. const char *region)
  760. {
  761. const char *prefix = "drm";
  762. drm_fdinfo_print_size(p, prefix, "total", region,
  763. stats->private + stats->shared);
  764. drm_fdinfo_print_size(p, prefix, "shared", region, stats->shared);
  765. if (supported_status & DRM_GEM_OBJECT_ACTIVE)
  766. drm_fdinfo_print_size(p, prefix, "active", region, stats->active);
  767. if (supported_status & DRM_GEM_OBJECT_RESIDENT)
  768. drm_fdinfo_print_size(p, prefix, "resident", region,
  769. stats->resident);
  770. if (supported_status & DRM_GEM_OBJECT_PURGEABLE)
  771. drm_fdinfo_print_size(p, prefix, "purgeable", region,
  772. stats->purgeable);
  773. }
  774. EXPORT_SYMBOL(drm_print_memory_stats);
  775. /**
  776. * drm_show_memory_stats - Helper to collect and show standard fdinfo memory stats
  777. * @p: the printer to print output to
  778. * @file: the DRM file
  779. *
  780. * Helper to iterate over GEM objects with a handle allocated in the specified
  781. * file.
  782. */
  783. void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
  784. {
  785. struct drm_gem_object *obj;
  786. struct drm_memory_stats status = {};
  787. enum drm_gem_object_status supported_status = 0;
  788. int id;
  789. spin_lock(&file->table_lock);
  790. idr_for_each_entry (&file->object_idr, obj, id) {
  791. enum drm_gem_object_status s = 0;
  792. size_t add_size = (obj->funcs && obj->funcs->rss) ?
  793. obj->funcs->rss(obj) : obj->size;
  794. if (obj->funcs && obj->funcs->status) {
  795. s = obj->funcs->status(obj);
  796. supported_status |= s;
  797. }
  798. if (drm_gem_object_is_shared_for_memory_stats(obj))
  799. status.shared += obj->size;
  800. else
  801. status.private += obj->size;
  802. if (s & DRM_GEM_OBJECT_RESIDENT) {
  803. status.resident += add_size;
  804. } else {
  805. /* If already purged or not yet backed by pages, don't
  806. * count it as purgeable:
  807. */
  808. s &= ~DRM_GEM_OBJECT_PURGEABLE;
  809. }
  810. if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) {
  811. status.active += add_size;
  812. supported_status |= DRM_GEM_OBJECT_ACTIVE;
  813. /* If still active, don't count as purgeable: */
  814. s &= ~DRM_GEM_OBJECT_PURGEABLE;
  815. }
  816. if (s & DRM_GEM_OBJECT_PURGEABLE)
  817. status.purgeable += add_size;
  818. }
  819. spin_unlock(&file->table_lock);
  820. drm_print_memory_stats(p, &status, supported_status, "memory");
  821. }
  822. EXPORT_SYMBOL(drm_show_memory_stats);
  823. /**
  824. * drm_show_fdinfo - helper for drm file fops
  825. * @m: output stream
  826. * @f: the device file instance
  827. *
  828. * Helper to implement fdinfo, for userspace to query usage stats, etc, of a
  829. * process using the GPU. See also &drm_driver.show_fdinfo.
  830. *
  831. * For text output format description please see Documentation/gpu/drm-usage-stats.rst
  832. */
  833. void drm_show_fdinfo(struct seq_file *m, struct file *f)
  834. {
  835. struct drm_file *file = f->private_data;
  836. struct drm_device *dev = file->minor->dev;
  837. struct drm_printer p = drm_seq_file_printer(m);
  838. int idx;
  839. if (!drm_dev_enter(dev, &idx))
  840. return;
  841. drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name);
  842. drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id);
  843. if (dev_is_pci(dev->dev)) {
  844. struct pci_dev *pdev = to_pci_dev(dev->dev);
  845. drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n",
  846. pci_domain_nr(pdev->bus), pdev->bus->number,
  847. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  848. }
  849. mutex_lock(&file->client_name_lock);
  850. if (file->client_name)
  851. drm_printf(&p, "drm-client-name:\t%s\n", file->client_name);
  852. mutex_unlock(&file->client_name_lock);
  853. if (dev->driver->show_fdinfo)
  854. dev->driver->show_fdinfo(&p, file);
  855. drm_dev_exit(idx);
  856. }
  857. EXPORT_SYMBOL(drm_show_fdinfo);
  858. /**
  859. * drm_file_err - log process name, pid and client_name associated with a drm_file
  860. * @file_priv: context of interest for process name and pid
  861. * @fmt: printf() like format string
  862. *
  863. * Helper function for clients which needs to log process details such
  864. * as name and pid etc along with user logs.
  865. */
  866. void drm_file_err(struct drm_file *file_priv, const char *fmt, ...)
  867. {
  868. va_list args;
  869. struct va_format vaf;
  870. struct pid *pid;
  871. struct task_struct *task;
  872. struct drm_device *dev = file_priv->minor->dev;
  873. va_start(args, fmt);
  874. vaf.fmt = fmt;
  875. vaf.va = &args;
  876. mutex_lock(&file_priv->client_name_lock);
  877. rcu_read_lock();
  878. pid = rcu_dereference(file_priv->pid);
  879. task = pid_task(pid, PIDTYPE_TGID);
  880. drm_err(dev, "comm: %s pid: %d client-id:%llu client: %s ... %pV",
  881. task ? task->comm : "Unset",
  882. task ? task->pid : 0, file_priv->client_id,
  883. file_priv->client_name ?: "Unset", &vaf);
  884. va_end(args);
  885. rcu_read_unlock();
  886. mutex_unlock(&file_priv->client_name_lock);
  887. }
  888. EXPORT_SYMBOL(drm_file_err);
  889. /**
  890. * mock_drm_getfile - Create a new struct file for the drm device
  891. * @minor: drm minor to wrap (e.g. #drm_device.primary)
  892. * @flags: file creation mode (O_RDWR etc)
  893. *
  894. * This create a new struct file that wraps a DRM file context around a
  895. * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
  896. * invoking userspace. The struct file may be operated on using its f_op
  897. * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
  898. * to userspace facing functions as an internal/anonymous client.
  899. *
  900. * RETURNS:
  901. * Pointer to newly created struct file, ERR_PTR on failure.
  902. */
  903. struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
  904. {
  905. struct drm_device *dev = minor->dev;
  906. struct drm_file *priv;
  907. struct file *file;
  908. priv = drm_file_alloc(minor);
  909. if (IS_ERR(priv))
  910. return ERR_CAST(priv);
  911. file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
  912. if (IS_ERR(file)) {
  913. drm_file_free(priv);
  914. return file;
  915. }
  916. /* Everyone shares a single global address space */
  917. file->f_mapping = dev->anon_inode->i_mapping;
  918. drm_dev_get(dev);
  919. priv->filp = file;
  920. return file;
  921. }
  922. EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);