drm_file.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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 <drm/drm_client.h>
  40. #include <drm/drm_drv.h>
  41. #include <drm/drm_file.h>
  42. #include <drm/drm_print.h>
  43. #include "drm_crtc_internal.h"
  44. #include "drm_internal.h"
  45. #include "drm_legacy.h"
  46. #if defined(CONFIG_MMU) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  47. #include <uapi/asm/mman.h>
  48. #include <drm/drm_vma_manager.h>
  49. #endif
  50. /* from BKL pushdown */
  51. DEFINE_MUTEX(drm_global_mutex);
  52. bool drm_dev_needs_global_mutex(struct drm_device *dev)
  53. {
  54. /*
  55. * Legacy drivers rely on all kinds of BKL locking semantics, don't
  56. * bother. They also still need BKL locking for their ioctls, so better
  57. * safe than sorry.
  58. */
  59. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  60. return true;
  61. /*
  62. * The deprecated ->load callback must be called after the driver is
  63. * already registered. This means such drivers rely on the BKL to make
  64. * sure an open can't proceed until the driver is actually fully set up.
  65. * Similar hilarity holds for the unload callback.
  66. */
  67. if (dev->driver->load || dev->driver->unload)
  68. return true;
  69. /*
  70. * Drivers with the lastclose callback assume that it's synchronized
  71. * against concurrent opens, which again needs the BKL. The proper fix
  72. * is to use the drm_client infrastructure with proper locking for each
  73. * client.
  74. */
  75. if (dev->driver->lastclose)
  76. return true;
  77. return false;
  78. }
  79. /**
  80. * DOC: file operations
  81. *
  82. * Drivers must define the file operations structure that forms the DRM
  83. * userspace API entry point, even though most of those operations are
  84. * implemented in the DRM core. The resulting &struct file_operations must be
  85. * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
  86. * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
  87. * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
  88. * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
  89. * that require 32/64 bit compatibility support must provide their own
  90. * &file_operations.compat_ioctl handler that processes private ioctls and calls
  91. * drm_compat_ioctl() for core ioctls.
  92. *
  93. * In addition drm_read() and drm_poll() provide support for DRM events. DRM
  94. * events are a generic and extensible means to send asynchronous events to
  95. * userspace through the file descriptor. They are used to send vblank event and
  96. * page flip completions by the KMS API. But drivers can also use it for their
  97. * own needs, e.g. to signal completion of rendering.
  98. *
  99. * For the driver-side event interface see drm_event_reserve_init() and
  100. * drm_send_event() as the main starting points.
  101. *
  102. * The memory mapping implementation will vary depending on how the driver
  103. * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
  104. * function, modern drivers should use one of the provided memory-manager
  105. * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
  106. * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
  107. *
  108. * No other file operations are supported by the DRM userspace API. Overall the
  109. * following is an example &file_operations structure::
  110. *
  111. * static const example_drm_fops = {
  112. * .owner = THIS_MODULE,
  113. * .open = drm_open,
  114. * .release = drm_release,
  115. * .unlocked_ioctl = drm_ioctl,
  116. * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
  117. * .poll = drm_poll,
  118. * .read = drm_read,
  119. * .llseek = no_llseek,
  120. * .mmap = drm_gem_mmap,
  121. * };
  122. *
  123. * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
  124. * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
  125. * simpler.
  126. *
  127. * The driver's &file_operations must be stored in &drm_driver.fops.
  128. *
  129. * For driver-private IOCTL handling see the more detailed discussion in
  130. * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
  131. */
  132. /**
  133. * drm_file_alloc - allocate file context
  134. * @minor: minor to allocate on
  135. *
  136. * This allocates a new DRM file context. It is not linked into any context and
  137. * can be used by the caller freely. Note that the context keeps a pointer to
  138. * @minor, so it must be freed before @minor is.
  139. *
  140. * RETURNS:
  141. * Pointer to newly allocated context, ERR_PTR on failure.
  142. */
  143. struct drm_file *drm_file_alloc(struct drm_minor *minor)
  144. {
  145. struct drm_device *dev = minor->dev;
  146. struct drm_file *file;
  147. int ret;
  148. file = kzalloc(sizeof(*file), GFP_KERNEL);
  149. if (!file)
  150. return ERR_PTR(-ENOMEM);
  151. file->pid = get_pid(task_pid(current));
  152. file->minor = minor;
  153. /* for compatibility root is always authenticated */
  154. file->authenticated = capable(CAP_SYS_ADMIN);
  155. INIT_LIST_HEAD(&file->lhead);
  156. INIT_LIST_HEAD(&file->fbs);
  157. mutex_init(&file->fbs_lock);
  158. INIT_LIST_HEAD(&file->blobs);
  159. INIT_LIST_HEAD(&file->pending_event_list);
  160. INIT_LIST_HEAD(&file->event_list);
  161. init_waitqueue_head(&file->event_wait);
  162. file->event_space = 4096; /* set aside 4k for event buffer */
  163. mutex_init(&file->event_read_lock);
  164. if (drm_core_check_feature(dev, DRIVER_GEM))
  165. drm_gem_open(dev, file);
  166. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  167. drm_syncobj_open(file);
  168. drm_prime_init_file_private(&file->prime);
  169. if (dev->driver->open) {
  170. ret = dev->driver->open(dev, file);
  171. if (ret < 0)
  172. goto out_prime_destroy;
  173. }
  174. return file;
  175. out_prime_destroy:
  176. drm_prime_destroy_file_private(&file->prime);
  177. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  178. drm_syncobj_release(file);
  179. if (drm_core_check_feature(dev, DRIVER_GEM))
  180. drm_gem_release(dev, file);
  181. put_pid(file->pid);
  182. kfree(file);
  183. return ERR_PTR(ret);
  184. }
  185. static void drm_events_release(struct drm_file *file_priv)
  186. {
  187. struct drm_device *dev = file_priv->minor->dev;
  188. struct drm_pending_event *e, *et;
  189. unsigned long flags;
  190. spin_lock_irqsave(&dev->event_lock, flags);
  191. /* Unlink pending events */
  192. list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
  193. pending_link) {
  194. list_del(&e->pending_link);
  195. e->file_priv = NULL;
  196. }
  197. /* Remove unconsumed events */
  198. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  199. list_del(&e->link);
  200. kfree(e);
  201. }
  202. spin_unlock_irqrestore(&dev->event_lock, flags);
  203. }
  204. /**
  205. * drm_file_free - free file context
  206. * @file: context to free, or NULL
  207. *
  208. * This destroys and deallocates a DRM file context previously allocated via
  209. * drm_file_alloc(). The caller must make sure to unlink it from any contexts
  210. * before calling this.
  211. *
  212. * If NULL is passed, this is a no-op.
  213. *
  214. * RETURNS:
  215. * 0 on success, or error code on failure.
  216. */
  217. void drm_file_free(struct drm_file *file)
  218. {
  219. struct drm_device *dev;
  220. if (!file)
  221. return;
  222. dev = file->minor->dev;
  223. DRM_DEBUG("comm=\"%s\", pid=%d, dev=0x%lx, open_count=%d\n",
  224. current->comm, task_pid_nr(current),
  225. (long)old_encode_dev(file->minor->kdev->devt),
  226. atomic_read(&dev->open_count));
  227. if (drm_core_check_feature(dev, DRIVER_LEGACY) &&
  228. dev->driver->preclose)
  229. dev->driver->preclose(dev, file);
  230. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  231. drm_legacy_lock_release(dev, file->filp);
  232. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  233. drm_legacy_reclaim_buffers(dev, file);
  234. drm_events_release(file);
  235. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  236. drm_fb_release(file);
  237. drm_property_destroy_user_blobs(dev, file);
  238. }
  239. if (drm_core_check_feature(dev, DRIVER_SYNCOBJ))
  240. drm_syncobj_release(file);
  241. if (drm_core_check_feature(dev, DRIVER_GEM))
  242. drm_gem_release(dev, file);
  243. drm_legacy_ctxbitmap_flush(dev, file);
  244. if (drm_is_primary_client(file))
  245. drm_master_release(file);
  246. if (dev->driver->postclose)
  247. dev->driver->postclose(dev, file);
  248. drm_prime_destroy_file_private(&file->prime);
  249. WARN_ON(!list_empty(&file->event_list));
  250. put_pid(file->pid);
  251. kfree(file);
  252. }
  253. static void drm_close_helper(struct file *filp)
  254. {
  255. struct drm_file *file_priv = filp->private_data;
  256. struct drm_device *dev = file_priv->minor->dev;
  257. mutex_lock(&dev->filelist_mutex);
  258. list_del(&file_priv->lhead);
  259. mutex_unlock(&dev->filelist_mutex);
  260. drm_file_free(file_priv);
  261. }
  262. /*
  263. * Check whether DRI will run on this CPU.
  264. *
  265. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  266. */
  267. static int drm_cpu_valid(void)
  268. {
  269. #if defined(__sparc__) && !defined(__sparc_v9__)
  270. return 0; /* No cmpxchg before v9 sparc. */
  271. #endif
  272. return 1;
  273. }
  274. /*
  275. * Called whenever a process opens a drm node
  276. *
  277. * \param filp file pointer.
  278. * \param minor acquired minor-object.
  279. * \return zero on success or a negative number on failure.
  280. *
  281. * Creates and initializes a drm_file structure for the file private data in \p
  282. * filp and add it into the double linked list in \p dev.
  283. */
  284. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  285. {
  286. struct drm_device *dev = minor->dev;
  287. struct drm_file *priv;
  288. int ret;
  289. if (filp->f_flags & O_EXCL)
  290. return -EBUSY; /* No exclusive opens */
  291. if (!drm_cpu_valid())
  292. return -EINVAL;
  293. if (dev->switch_power_state != DRM_SWITCH_POWER_ON &&
  294. dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  295. return -EINVAL;
  296. DRM_DEBUG("comm=\"%s\", pid=%d, minor=%d\n", current->comm,
  297. task_pid_nr(current), minor->index);
  298. priv = drm_file_alloc(minor);
  299. if (IS_ERR(priv))
  300. return PTR_ERR(priv);
  301. if (drm_is_primary_client(priv)) {
  302. ret = drm_master_open(priv);
  303. if (ret) {
  304. drm_file_free(priv);
  305. return ret;
  306. }
  307. }
  308. filp->private_data = priv;
  309. filp->f_mode |= FMODE_UNSIGNED_OFFSET;
  310. priv->filp = filp;
  311. mutex_lock(&dev->filelist_mutex);
  312. list_add(&priv->lhead, &dev->filelist);
  313. mutex_unlock(&dev->filelist_mutex);
  314. #ifdef __alpha__
  315. /*
  316. * Default the hose
  317. */
  318. if (!dev->hose) {
  319. struct pci_dev *pci_dev;
  320. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  321. if (pci_dev) {
  322. dev->hose = pci_dev->sysdata;
  323. pci_dev_put(pci_dev);
  324. }
  325. if (!dev->hose) {
  326. struct pci_bus *b = list_entry(pci_root_buses.next,
  327. struct pci_bus, node);
  328. if (b)
  329. dev->hose = b->sysdata;
  330. }
  331. }
  332. #endif
  333. return 0;
  334. }
  335. /**
  336. * drm_open - open method for DRM file
  337. * @inode: device inode
  338. * @filp: file pointer.
  339. *
  340. * This function must be used by drivers as their &file_operations.open method.
  341. * It looks up the correct DRM device and instantiates all the per-file
  342. * resources for it. It also calls the &drm_driver.open driver callback.
  343. *
  344. * RETURNS:
  345. *
  346. * 0 on success or negative errno value on falure.
  347. */
  348. int drm_open(struct inode *inode, struct file *filp)
  349. {
  350. struct drm_device *dev;
  351. struct drm_minor *minor;
  352. int retcode;
  353. int need_setup = 0;
  354. minor = drm_minor_acquire(iminor(inode));
  355. if (IS_ERR(minor))
  356. return PTR_ERR(minor);
  357. dev = minor->dev;
  358. if (drm_dev_needs_global_mutex(dev))
  359. mutex_lock(&drm_global_mutex);
  360. if (!atomic_fetch_inc(&dev->open_count))
  361. need_setup = 1;
  362. /* share address_space across all char-devs of a single device */
  363. filp->f_mapping = dev->anon_inode->i_mapping;
  364. retcode = drm_open_helper(filp, minor);
  365. if (retcode)
  366. goto err_undo;
  367. if (need_setup) {
  368. retcode = drm_legacy_setup(dev);
  369. if (retcode) {
  370. drm_close_helper(filp);
  371. goto err_undo;
  372. }
  373. }
  374. if (drm_dev_needs_global_mutex(dev))
  375. mutex_unlock(&drm_global_mutex);
  376. return 0;
  377. err_undo:
  378. atomic_dec(&dev->open_count);
  379. if (drm_dev_needs_global_mutex(dev))
  380. mutex_unlock(&drm_global_mutex);
  381. drm_minor_release(minor);
  382. return retcode;
  383. }
  384. EXPORT_SYMBOL(drm_open);
  385. void drm_lastclose(struct drm_device * dev)
  386. {
  387. DRM_DEBUG("\n");
  388. if (dev->driver->lastclose)
  389. dev->driver->lastclose(dev);
  390. DRM_DEBUG("driver lastclose completed\n");
  391. if (drm_core_check_feature(dev, DRIVER_LEGACY))
  392. drm_legacy_dev_reinit(dev);
  393. drm_client_dev_restore(dev);
  394. }
  395. /**
  396. * drm_release - release method for DRM file
  397. * @inode: device inode
  398. * @filp: file pointer.
  399. *
  400. * This function must be used by drivers as their &file_operations.release
  401. * method. It frees any resources associated with the open file, and calls the
  402. * &drm_driver.postclose driver callback. If this is the last open file for the
  403. * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
  404. *
  405. * RETURNS:
  406. *
  407. * Always succeeds and returns 0.
  408. */
  409. int drm_release(struct inode *inode, struct file *filp)
  410. {
  411. struct drm_file *file_priv = filp->private_data;
  412. struct drm_minor *minor = file_priv->minor;
  413. struct drm_device *dev = minor->dev;
  414. if (drm_dev_needs_global_mutex(dev))
  415. mutex_lock(&drm_global_mutex);
  416. DRM_DEBUG("open_count = %d\n", atomic_read(&dev->open_count));
  417. drm_close_helper(filp);
  418. if (atomic_dec_and_test(&dev->open_count))
  419. drm_lastclose(dev);
  420. if (drm_dev_needs_global_mutex(dev))
  421. mutex_unlock(&drm_global_mutex);
  422. drm_minor_release(minor);
  423. return 0;
  424. }
  425. EXPORT_SYMBOL(drm_release);
  426. /**
  427. * drm_release_noglobal - release method for DRM file
  428. * @inode: device inode
  429. * @filp: file pointer.
  430. *
  431. * This function may be used by drivers as their &file_operations.release
  432. * method. It frees any resources associated with the open file prior to taking
  433. * the drm_global_mutex, which then calls the &drm_driver.postclose driver
  434. * callback. If this is the last open file for the DRM device also proceeds to
  435. * call the &drm_driver.lastclose driver callback.
  436. *
  437. * RETURNS:
  438. *
  439. * Always succeeds and returns 0.
  440. */
  441. int drm_release_noglobal(struct inode *inode, struct file *filp)
  442. {
  443. struct drm_file *file_priv = filp->private_data;
  444. struct drm_minor *minor = file_priv->minor;
  445. struct drm_device *dev = minor->dev;
  446. drm_close_helper(filp);
  447. if (atomic_dec_and_mutex_lock(&dev->open_count, &drm_global_mutex)) {
  448. drm_lastclose(dev);
  449. mutex_unlock(&drm_global_mutex);
  450. }
  451. drm_minor_release(minor);
  452. return 0;
  453. }
  454. EXPORT_SYMBOL(drm_release_noglobal);
  455. /**
  456. * drm_read - read method for DRM file
  457. * @filp: file pointer
  458. * @buffer: userspace destination pointer for the read
  459. * @count: count in bytes to read
  460. * @offset: offset to read
  461. *
  462. * This function must be used by drivers as their &file_operations.read
  463. * method iff they use DRM events for asynchronous signalling to userspace.
  464. * Since events are used by the KMS API for vblank and page flip completion this
  465. * means all modern display drivers must use it.
  466. *
  467. * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
  468. * must set the &file_operation.llseek to no_llseek(). Polling support is
  469. * provided by drm_poll().
  470. *
  471. * This function will only ever read a full event. Therefore userspace must
  472. * supply a big enough buffer to fit any event to ensure forward progress. Since
  473. * the maximum event space is currently 4K it's recommended to just use that for
  474. * safety.
  475. *
  476. * RETURNS:
  477. *
  478. * Number of bytes read (always aligned to full events, and can be 0) or a
  479. * negative error code on failure.
  480. */
  481. ssize_t drm_read(struct file *filp, char __user *buffer,
  482. size_t count, loff_t *offset)
  483. {
  484. struct drm_file *file_priv = filp->private_data;
  485. struct drm_device *dev = file_priv->minor->dev;
  486. ssize_t ret;
  487. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  488. if (ret)
  489. return ret;
  490. for (;;) {
  491. struct drm_pending_event *e = NULL;
  492. spin_lock_irq(&dev->event_lock);
  493. if (!list_empty(&file_priv->event_list)) {
  494. e = list_first_entry(&file_priv->event_list,
  495. struct drm_pending_event, link);
  496. file_priv->event_space += e->event->length;
  497. list_del(&e->link);
  498. }
  499. spin_unlock_irq(&dev->event_lock);
  500. if (e == NULL) {
  501. if (ret)
  502. break;
  503. if (filp->f_flags & O_NONBLOCK) {
  504. ret = -EAGAIN;
  505. break;
  506. }
  507. mutex_unlock(&file_priv->event_read_lock);
  508. ret = wait_event_interruptible(file_priv->event_wait,
  509. !list_empty(&file_priv->event_list));
  510. if (ret >= 0)
  511. ret = mutex_lock_interruptible(&file_priv->event_read_lock);
  512. if (ret)
  513. return ret;
  514. } else {
  515. unsigned length = e->event->length;
  516. if (length > count - ret) {
  517. put_back_event:
  518. spin_lock_irq(&dev->event_lock);
  519. file_priv->event_space -= length;
  520. list_add(&e->link, &file_priv->event_list);
  521. spin_unlock_irq(&dev->event_lock);
  522. wake_up_interruptible_poll(&file_priv->event_wait,
  523. EPOLLIN | EPOLLRDNORM);
  524. break;
  525. }
  526. if (copy_to_user(buffer + ret, e->event, length)) {
  527. if (ret == 0)
  528. ret = -EFAULT;
  529. goto put_back_event;
  530. }
  531. ret += length;
  532. kfree(e);
  533. }
  534. }
  535. mutex_unlock(&file_priv->event_read_lock);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL(drm_read);
  539. /**
  540. * drm_poll - poll method for DRM file
  541. * @filp: file pointer
  542. * @wait: poll waiter table
  543. *
  544. * This function must be used by drivers as their &file_operations.read method
  545. * iff they use DRM events for asynchronous signalling to userspace. Since
  546. * events are used by the KMS API for vblank and page flip completion this means
  547. * all modern display drivers must use it.
  548. *
  549. * See also drm_read().
  550. *
  551. * RETURNS:
  552. *
  553. * Mask of POLL flags indicating the current status of the file.
  554. */
  555. __poll_t drm_poll(struct file *filp, struct poll_table_struct *wait)
  556. {
  557. struct drm_file *file_priv = filp->private_data;
  558. __poll_t mask = 0;
  559. poll_wait(filp, &file_priv->event_wait, wait);
  560. if (!list_empty(&file_priv->event_list))
  561. mask |= EPOLLIN | EPOLLRDNORM;
  562. return mask;
  563. }
  564. EXPORT_SYMBOL(drm_poll);
  565. /**
  566. * drm_event_reserve_init_locked - init a DRM event and reserve space for it
  567. * @dev: DRM device
  568. * @file_priv: DRM file private data
  569. * @p: tracking structure for the pending event
  570. * @e: actual event data to deliver to userspace
  571. *
  572. * This function prepares the passed in event for eventual delivery. If the event
  573. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  574. * anything) then the even must be cancelled and freed using
  575. * drm_event_cancel_free(). Successfully initialized events should be sent out
  576. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  577. * asynchronous event to userspace.
  578. *
  579. * If callers embedded @p into a larger structure it must be allocated with
  580. * kmalloc and @p must be the first member element.
  581. *
  582. * This is the locked version of drm_event_reserve_init() for callers which
  583. * already hold &drm_device.event_lock.
  584. *
  585. * RETURNS:
  586. *
  587. * 0 on success or a negative error code on failure.
  588. */
  589. int drm_event_reserve_init_locked(struct drm_device *dev,
  590. struct drm_file *file_priv,
  591. struct drm_pending_event *p,
  592. struct drm_event *e)
  593. {
  594. if (file_priv->event_space < e->length)
  595. return -ENOMEM;
  596. file_priv->event_space -= e->length;
  597. p->event = e;
  598. list_add(&p->pending_link, &file_priv->pending_event_list);
  599. p->file_priv = file_priv;
  600. return 0;
  601. }
  602. EXPORT_SYMBOL(drm_event_reserve_init_locked);
  603. /**
  604. * drm_event_reserve_init - init a DRM event and reserve space for it
  605. * @dev: DRM device
  606. * @file_priv: DRM file private data
  607. * @p: tracking structure for the pending event
  608. * @e: actual event data to deliver to userspace
  609. *
  610. * This function prepares the passed in event for eventual delivery. If the event
  611. * doesn't get delivered (because the IOCTL fails later on, before queuing up
  612. * anything) then the even must be cancelled and freed using
  613. * drm_event_cancel_free(). Successfully initialized events should be sent out
  614. * using drm_send_event() or drm_send_event_locked() to signal completion of the
  615. * asynchronous event to userspace.
  616. *
  617. * If callers embedded @p into a larger structure it must be allocated with
  618. * kmalloc and @p must be the first member element.
  619. *
  620. * Callers which already hold &drm_device.event_lock should use
  621. * drm_event_reserve_init_locked() instead.
  622. *
  623. * RETURNS:
  624. *
  625. * 0 on success or a negative error code on failure.
  626. */
  627. int drm_event_reserve_init(struct drm_device *dev,
  628. struct drm_file *file_priv,
  629. struct drm_pending_event *p,
  630. struct drm_event *e)
  631. {
  632. unsigned long flags;
  633. int ret;
  634. spin_lock_irqsave(&dev->event_lock, flags);
  635. ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
  636. spin_unlock_irqrestore(&dev->event_lock, flags);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL(drm_event_reserve_init);
  640. /**
  641. * drm_event_cancel_free - free a DRM event and release its space
  642. * @dev: DRM device
  643. * @p: tracking structure for the pending event
  644. *
  645. * This function frees the event @p initialized with drm_event_reserve_init()
  646. * and releases any allocated space. It is used to cancel an event when the
  647. * nonblocking operation could not be submitted and needed to be aborted.
  648. */
  649. void drm_event_cancel_free(struct drm_device *dev,
  650. struct drm_pending_event *p)
  651. {
  652. unsigned long flags;
  653. spin_lock_irqsave(&dev->event_lock, flags);
  654. if (p->file_priv) {
  655. p->file_priv->event_space += p->event->length;
  656. list_del(&p->pending_link);
  657. }
  658. spin_unlock_irqrestore(&dev->event_lock, flags);
  659. if (p->fence)
  660. dma_fence_put(p->fence);
  661. kfree(p);
  662. }
  663. EXPORT_SYMBOL(drm_event_cancel_free);
  664. /**
  665. * drm_send_event_helper - send DRM event to file descriptor
  666. * @dev: DRM device
  667. * @e: DRM event to deliver
  668. * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
  669. * time domain
  670. *
  671. * This helper function sends the event @e, initialized with
  672. * drm_event_reserve_init(), to its associated userspace DRM file.
  673. * The timestamp variant of dma_fence_signal is used when the caller
  674. * sends a valid timestamp.
  675. */
  676. void drm_send_event_helper(struct drm_device *dev,
  677. struct drm_pending_event *e, ktime_t timestamp)
  678. {
  679. assert_spin_locked(&dev->event_lock);
  680. if (e->completion) {
  681. complete_all(e->completion);
  682. e->completion_release(e->completion);
  683. e->completion = NULL;
  684. }
  685. if (e->fence) {
  686. if (timestamp)
  687. dma_fence_signal_timestamp(e->fence, timestamp);
  688. else
  689. dma_fence_signal(e->fence);
  690. dma_fence_put(e->fence);
  691. }
  692. if (!e->file_priv) {
  693. kfree(e);
  694. return;
  695. }
  696. list_del(&e->pending_link);
  697. list_add_tail(&e->link,
  698. &e->file_priv->event_list);
  699. wake_up_interruptible_poll(&e->file_priv->event_wait,
  700. EPOLLIN | EPOLLRDNORM);
  701. }
  702. /**
  703. * drm_send_event_timestamp_locked - send DRM event to file descriptor
  704. * @dev: DRM device
  705. * @e: DRM event to deliver
  706. * @timestamp: timestamp to set for the fence event in kernel's CLOCK_MONOTONIC
  707. * time domain
  708. *
  709. * This function sends the event @e, initialized with drm_event_reserve_init(),
  710. * to its associated userspace DRM file. Callers must already hold
  711. * &drm_device.event_lock.
  712. *
  713. * Note that the core will take care of unlinking and disarming events when the
  714. * corresponding DRM file is closed. Drivers need not worry about whether the
  715. * DRM file for this event still exists and can call this function upon
  716. * completion of the asynchronous work unconditionally.
  717. */
  718. void drm_send_event_timestamp_locked(struct drm_device *dev,
  719. struct drm_pending_event *e, ktime_t timestamp)
  720. {
  721. drm_send_event_helper(dev, e, timestamp);
  722. }
  723. EXPORT_SYMBOL(drm_send_event_timestamp_locked);
  724. /**
  725. * drm_send_event_locked - send DRM event to file descriptor
  726. * @dev: DRM device
  727. * @e: DRM event to deliver
  728. *
  729. * This function sends the event @e, initialized with drm_event_reserve_init(),
  730. * to its associated userspace DRM file. Callers must already hold
  731. * &drm_device.event_lock, see drm_send_event() for the unlocked version.
  732. *
  733. * Note that the core will take care of unlinking and disarming events when the
  734. * corresponding DRM file is closed. Drivers need not worry about whether the
  735. * DRM file for this event still exists and can call this function upon
  736. * completion of the asynchronous work unconditionally.
  737. */
  738. void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
  739. {
  740. drm_send_event_helper(dev, e, 0);
  741. }
  742. EXPORT_SYMBOL(drm_send_event_locked);
  743. /**
  744. * drm_send_event - send DRM event to file descriptor
  745. * @dev: DRM device
  746. * @e: DRM event to deliver
  747. *
  748. * This function sends the event @e, initialized with drm_event_reserve_init(),
  749. * to its associated userspace DRM file. This function acquires
  750. * &drm_device.event_lock, see drm_send_event_locked() for callers which already
  751. * hold this lock.
  752. *
  753. * Note that the core will take care of unlinking and disarming events when the
  754. * corresponding DRM file is closed. Drivers need not worry about whether the
  755. * DRM file for this event still exists and can call this function upon
  756. * completion of the asynchronous work unconditionally.
  757. */
  758. void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
  759. {
  760. unsigned long irqflags;
  761. spin_lock_irqsave(&dev->event_lock, irqflags);
  762. drm_send_event_helper(dev, e, 0);
  763. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  764. }
  765. EXPORT_SYMBOL(drm_send_event);
  766. /**
  767. * mock_drm_getfile - Create a new struct file for the drm device
  768. * @minor: drm minor to wrap (e.g. #drm_device.primary)
  769. * @flags: file creation mode (O_RDWR etc)
  770. *
  771. * This create a new struct file that wraps a DRM file context around a
  772. * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
  773. * invoking userspace. The struct file may be operated on using its f_op
  774. * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
  775. * to userspace facing functions as an internal/anonymous client.
  776. *
  777. * RETURNS:
  778. * Pointer to newly created struct file, ERR_PTR on failure.
  779. */
  780. struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags)
  781. {
  782. struct drm_device *dev = minor->dev;
  783. struct drm_file *priv;
  784. struct file *file;
  785. priv = drm_file_alloc(minor);
  786. if (IS_ERR(priv))
  787. return ERR_CAST(priv);
  788. file = anon_inode_getfile("drm", dev->driver->fops, priv, flags);
  789. if (IS_ERR(file)) {
  790. drm_file_free(priv);
  791. return file;
  792. }
  793. /* Everyone shares a single global address space */
  794. file->f_mapping = dev->anon_inode->i_mapping;
  795. drm_dev_get(dev);
  796. priv->filp = file;
  797. return file;
  798. }
  799. EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile);
  800. #ifdef CONFIG_MMU
  801. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  802. /*
  803. * drm_addr_inflate() attempts to construct an aligned area by inflating
  804. * the area size and skipping the unaligned start of the area.
  805. * adapted from shmem_get_unmapped_area()
  806. */
  807. static unsigned long drm_addr_inflate(unsigned long addr,
  808. unsigned long len,
  809. unsigned long pgoff,
  810. unsigned long flags,
  811. unsigned long huge_size)
  812. {
  813. unsigned long offset, inflated_len;
  814. unsigned long inflated_addr;
  815. unsigned long inflated_offset;
  816. offset = (pgoff << PAGE_SHIFT) & (huge_size - 1);
  817. if (offset && offset + len < 2 * huge_size)
  818. return addr;
  819. if ((addr & (huge_size - 1)) == offset)
  820. return addr;
  821. inflated_len = len + huge_size - PAGE_SIZE;
  822. if (inflated_len > TASK_SIZE)
  823. return addr;
  824. if (inflated_len < len)
  825. return addr;
  826. inflated_addr = current->mm->get_unmapped_area(NULL, 0, inflated_len,
  827. 0, flags);
  828. if (IS_ERR_VALUE(inflated_addr))
  829. return addr;
  830. if (inflated_addr & ~PAGE_MASK)
  831. return addr;
  832. inflated_offset = inflated_addr & (huge_size - 1);
  833. inflated_addr += offset - inflated_offset;
  834. if (inflated_offset > offset)
  835. inflated_addr += huge_size;
  836. if (inflated_addr > TASK_SIZE - len)
  837. return addr;
  838. return inflated_addr;
  839. }
  840. /**
  841. * drm_get_unmapped_area() - Get an unused user-space virtual memory area
  842. * suitable for huge page table entries.
  843. * @file: The struct file representing the address space being mmap()'d.
  844. * @uaddr: Start address suggested by user-space.
  845. * @len: Length of the area.
  846. * @pgoff: The page offset into the address space.
  847. * @flags: mmap flags
  848. * @mgr: The address space manager used by the drm driver. This argument can
  849. * probably be removed at some point when all drivers use the same
  850. * address space manager.
  851. *
  852. * This function attempts to find an unused user-space virtual memory area
  853. * that can accommodate the size we want to map, and that is properly
  854. * aligned to facilitate huge page table entries matching actual
  855. * huge pages or huge page aligned memory in buffer objects. Buffer objects
  856. * are assumed to start at huge page boundary pfns (io memory) or be
  857. * populated by huge pages aligned to the start of the buffer object
  858. * (system- or coherent memory). Adapted from shmem_get_unmapped_area.
  859. *
  860. * Return: aligned user-space address.
  861. */
  862. unsigned long drm_get_unmapped_area(struct file *file,
  863. unsigned long uaddr, unsigned long len,
  864. unsigned long pgoff, unsigned long flags,
  865. struct drm_vma_offset_manager *mgr)
  866. {
  867. unsigned long addr;
  868. unsigned long inflated_addr;
  869. struct drm_vma_offset_node *node;
  870. if (len > TASK_SIZE)
  871. return -ENOMEM;
  872. /*
  873. * @pgoff is the file page-offset the huge page boundaries of
  874. * which typically aligns to physical address huge page boundaries.
  875. * That's not true for DRM, however, where physical address huge
  876. * page boundaries instead are aligned with the offset from
  877. * buffer object start. So adjust @pgoff to be the offset from
  878. * buffer object start.
  879. */
  880. drm_vma_offset_lock_lookup(mgr);
  881. node = drm_vma_offset_lookup_locked(mgr, pgoff, 1);
  882. if (node)
  883. pgoff -= node->vm_node.start;
  884. drm_vma_offset_unlock_lookup(mgr);
  885. addr = current->mm->get_unmapped_area(file, uaddr, len, pgoff, flags);
  886. if (IS_ERR_VALUE(addr))
  887. return addr;
  888. if (addr & ~PAGE_MASK)
  889. return addr;
  890. if (addr > TASK_SIZE - len)
  891. return addr;
  892. if (len < HPAGE_PMD_SIZE)
  893. return addr;
  894. if (flags & MAP_FIXED)
  895. return addr;
  896. /*
  897. * Our priority is to support MAP_SHARED mapped hugely;
  898. * and support MAP_PRIVATE mapped hugely too, until it is COWed.
  899. * But if caller specified an address hint, respect that as before.
  900. */
  901. if (uaddr)
  902. return addr;
  903. inflated_addr = drm_addr_inflate(addr, len, pgoff, flags,
  904. HPAGE_PMD_SIZE);
  905. if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) &&
  906. len >= HPAGE_PUD_SIZE)
  907. inflated_addr = drm_addr_inflate(inflated_addr, len, pgoff,
  908. flags, HPAGE_PUD_SIZE);
  909. return inflated_addr;
  910. }
  911. #else /* CONFIG_TRANSPARENT_HUGEPAGE */
  912. unsigned long drm_get_unmapped_area(struct file *file,
  913. unsigned long uaddr, unsigned long len,
  914. unsigned long pgoff, unsigned long flags,
  915. struct drm_vma_offset_manager *mgr)
  916. {
  917. return current->mm->get_unmapped_area(file, uaddr, len, pgoff, flags);
  918. }
  919. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  920. EXPORT_SYMBOL_GPL(drm_get_unmapped_area);
  921. #endif /* CONFIG_MMU */