drm_file.c 29 KB

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