gntdev.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR MIT) */
  2. /******************************************************************************
  3. * gntdev.h
  4. *
  5. * Interface to /dev/xen/gntdev.
  6. *
  7. * Copyright (c) 2007, D G Murray
  8. * Copyright (c) 2018, Oleksandr Andrushchenko, EPAM Systems Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation; or, when distributed
  13. * separately from the Linux kernel or incorporated into other
  14. * software packages, subject to the following license:
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this source file (the "Software"), to deal in the Software without
  18. * restriction, including without limitation the rights to use, copy, modify,
  19. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  20. * and to permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. * IN THE SOFTWARE.
  33. */
  34. #ifndef __LINUX_PUBLIC_GNTDEV_H__
  35. #define __LINUX_PUBLIC_GNTDEV_H__
  36. #include <linux/types.h>
  37. struct ioctl_gntdev_grant_ref {
  38. /* The domain ID of the grant to be mapped. */
  39. __u32 domid;
  40. /* The grant reference of the grant to be mapped. */
  41. __u32 ref;
  42. };
  43. /*
  44. * Inserts the grant references into the mapping table of an instance
  45. * of gntdev. N.B. This does not perform the mapping, which is deferred
  46. * until mmap() is called with @index as the offset. @index should be
  47. * considered opaque to userspace, with one exception: if no grant
  48. * references have ever been inserted into the mapping table of this
  49. * instance, @index will be set to 0. This is necessary to use gntdev
  50. * with userspace APIs that expect a file descriptor that can be
  51. * mmap()'d at offset 0, such as Wayland. If @count is set to 0, this
  52. * ioctl will fail.
  53. */
  54. #define IOCTL_GNTDEV_MAP_GRANT_REF \
  55. _IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_gntdev_map_grant_ref))
  56. struct ioctl_gntdev_map_grant_ref {
  57. /* IN parameters */
  58. /* The number of grants to be mapped. */
  59. __u32 count;
  60. __u32 pad;
  61. /* OUT parameters */
  62. /* The offset to be used on a subsequent call to mmap(). */
  63. __u64 index;
  64. /* Variable IN parameter. */
  65. /* Array of grant references, of size @count. */
  66. struct ioctl_gntdev_grant_ref refs[1];
  67. };
  68. /*
  69. * Removes the grant references from the mapping table of an instance of
  70. * gntdev. N.B. munmap() must be called on the relevant virtual address(es)
  71. * before this ioctl is called, or an error will result.
  72. */
  73. #define IOCTL_GNTDEV_UNMAP_GRANT_REF \
  74. _IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_gntdev_unmap_grant_ref))
  75. struct ioctl_gntdev_unmap_grant_ref {
  76. /* IN parameters */
  77. /* The offset was returned by the corresponding map operation. */
  78. __u64 index;
  79. /* The number of pages to be unmapped. */
  80. __u32 count;
  81. __u32 pad;
  82. };
  83. /*
  84. * Returns the offset in the driver's address space that corresponds
  85. * to @vaddr. This can be used to perform a munmap(), followed by an
  86. * UNMAP_GRANT_REF ioctl, where no state about the offset is retained by
  87. * the caller. The number of pages that were allocated at the same time as
  88. * @vaddr is returned in @count.
  89. *
  90. * N.B. Where more than one page has been mapped into a contiguous range, the
  91. * supplied @vaddr must correspond to the start of the range; otherwise
  92. * an error will result. It is only possible to munmap() the entire
  93. * contiguously-allocated range at once, and not any subrange thereof.
  94. */
  95. #define IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR \
  96. _IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_gntdev_get_offset_for_vaddr))
  97. struct ioctl_gntdev_get_offset_for_vaddr {
  98. /* IN parameters */
  99. /* The virtual address of the first mapped page in a range. */
  100. __u64 vaddr;
  101. /* OUT parameters */
  102. /* The offset that was used in the initial mmap() operation. */
  103. __u64 offset;
  104. /* The number of pages mapped in the VM area that begins at @vaddr. */
  105. __u32 count;
  106. __u32 pad;
  107. };
  108. /*
  109. * Sets the maximum number of grants that may mapped at once by this gntdev
  110. * instance.
  111. *
  112. * N.B. This must be called before any other ioctl is performed on the device.
  113. */
  114. #define IOCTL_GNTDEV_SET_MAX_GRANTS \
  115. _IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_gntdev_set_max_grants))
  116. struct ioctl_gntdev_set_max_grants {
  117. /* IN parameter */
  118. /* The maximum number of grants that may be mapped at once. */
  119. __u32 count;
  120. };
  121. /*
  122. * Sets up an unmap notification within the page, so that the other side can do
  123. * cleanup if this side crashes. Required to implement cross-domain robust
  124. * mutexes or close notification on communication channels.
  125. *
  126. * Each mapped page only supports one notification; multiple calls referring to
  127. * the same page overwrite the previous notification. You must clear the
  128. * notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it
  129. * to occur.
  130. */
  131. #define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \
  132. _IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify))
  133. struct ioctl_gntdev_unmap_notify {
  134. /* IN parameters */
  135. /* Offset in the file descriptor for a byte within the page (same as
  136. * used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to
  137. * be cleared. Otherwise, it can be any byte in the page whose
  138. * notification we are adjusting.
  139. */
  140. __u64 index;
  141. /* Action(s) to take on unmap */
  142. __u32 action;
  143. /* Event channel to notify */
  144. __u32 event_channel_port;
  145. };
  146. struct gntdev_grant_copy_segment {
  147. union {
  148. void *virt;
  149. struct {
  150. grant_ref_t ref;
  151. __u16 offset;
  152. domid_t domid;
  153. } foreign;
  154. } source, dest;
  155. __u16 len;
  156. __u16 flags; /* GNTCOPY_* */
  157. __s16 status; /* GNTST_* */
  158. };
  159. /*
  160. * Copy between grant references and local buffers.
  161. *
  162. * The copy is split into @count @segments, each of which can copy
  163. * to/from one grant reference.
  164. *
  165. * Each segment is similar to struct gnttab_copy in the hypervisor ABI
  166. * except the local buffer is specified using a virtual address
  167. * (instead of a GFN and offset).
  168. *
  169. * The local buffer may cross a Xen page boundary -- the driver will
  170. * split segments into multiple ops if required.
  171. *
  172. * Returns 0 if all segments have been processed and @status in each
  173. * segment is valid. Note that one or more segments may have failed
  174. * (status != GNTST_okay).
  175. *
  176. * If the driver had to split a segment into two or more ops, @status
  177. * includes the status of the first failed op for that segment (or
  178. * GNTST_okay if all ops were successful).
  179. *
  180. * If -1 is returned, the status of all segments is undefined.
  181. *
  182. * EINVAL: A segment has local buffers for both source and
  183. * destination.
  184. * EINVAL: A segment crosses the boundary of a foreign page.
  185. * EFAULT: A segment's local buffer is not accessible.
  186. */
  187. #define IOCTL_GNTDEV_GRANT_COPY \
  188. _IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))
  189. struct ioctl_gntdev_grant_copy {
  190. unsigned int count;
  191. struct gntdev_grant_copy_segment *segments;
  192. };
  193. /* Clear (set to zero) the byte specified by index */
  194. #define UNMAP_NOTIFY_CLEAR_BYTE 0x1
  195. /* Send an interrupt on the indicated event channel */
  196. #define UNMAP_NOTIFY_SEND_EVENT 0x2
  197. /*
  198. * Flags to be used while requesting memory mapping's backing storage
  199. * to be allocated with DMA API.
  200. */
  201. /*
  202. * The buffer is backed with memory allocated with dma_alloc_wc.
  203. */
  204. #define GNTDEV_DMA_FLAG_WC (1 << 0)
  205. /*
  206. * The buffer is backed with memory allocated with dma_alloc_coherent.
  207. */
  208. #define GNTDEV_DMA_FLAG_COHERENT (1 << 1)
  209. /*
  210. * Create a dma-buf [1] from grant references @refs of count @count provided
  211. * by the foreign domain @domid with flags @flags.
  212. *
  213. * By default dma-buf is backed by system memory pages, but by providing
  214. * one of the GNTDEV_DMA_FLAG_XXX flags it can also be created as
  215. * a DMA write-combine or coherent buffer, e.g. allocated with dma_alloc_wc/
  216. * dma_alloc_coherent.
  217. *
  218. * Returns 0 if dma-buf was successfully created and the corresponding
  219. * dma-buf's file descriptor is returned in @fd.
  220. *
  221. * [1] Documentation/driver-api/dma-buf.rst
  222. */
  223. #define IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS \
  224. _IOC(_IOC_NONE, 'G', 9, \
  225. sizeof(struct ioctl_gntdev_dmabuf_exp_from_refs))
  226. struct ioctl_gntdev_dmabuf_exp_from_refs {
  227. /* IN parameters. */
  228. /* Specific options for this dma-buf: see GNTDEV_DMA_FLAG_XXX. */
  229. __u32 flags;
  230. /* Number of grant references in @refs array. */
  231. __u32 count;
  232. /* OUT parameters. */
  233. /* File descriptor of the dma-buf. */
  234. __u32 fd;
  235. /* The domain ID of the grant references to be mapped. */
  236. __u32 domid;
  237. /* Variable IN parameter. */
  238. /* Array of grant references of size @count. */
  239. __u32 refs[1];
  240. };
  241. /*
  242. * This will block until the dma-buf with the file descriptor @fd is
  243. * released. This is only valid for buffers created with
  244. * IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS.
  245. *
  246. * If within @wait_to_ms milliseconds the buffer is not released
  247. * then -ETIMEDOUT error is returned.
  248. * If the buffer with the file descriptor @fd does not exist or has already
  249. * been released, then -ENOENT is returned. For valid file descriptors
  250. * this must not be treated as error.
  251. */
  252. #define IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED \
  253. _IOC(_IOC_NONE, 'G', 10, \
  254. sizeof(struct ioctl_gntdev_dmabuf_exp_wait_released))
  255. struct ioctl_gntdev_dmabuf_exp_wait_released {
  256. /* IN parameters */
  257. __u32 fd;
  258. __u32 wait_to_ms;
  259. };
  260. /*
  261. * Import a dma-buf with file descriptor @fd and export granted references
  262. * to the pages of that dma-buf into array @refs of size @count.
  263. */
  264. #define IOCTL_GNTDEV_DMABUF_IMP_TO_REFS \
  265. _IOC(_IOC_NONE, 'G', 11, \
  266. sizeof(struct ioctl_gntdev_dmabuf_imp_to_refs))
  267. struct ioctl_gntdev_dmabuf_imp_to_refs {
  268. /* IN parameters. */
  269. /* File descriptor of the dma-buf. */
  270. __u32 fd;
  271. /* Number of grant references in @refs array. */
  272. __u32 count;
  273. /* The domain ID for which references to be granted. */
  274. __u32 domid;
  275. /* Reserved - must be zero. */
  276. __u32 reserved;
  277. /* OUT parameters. */
  278. /* Array of grant references of size @count. */
  279. __u32 refs[1];
  280. };
  281. /*
  282. * This will close all references to the imported buffer with file descriptor
  283. * @fd, so it can be released by the owner. This is only valid for buffers
  284. * created with IOCTL_GNTDEV_DMABUF_IMP_TO_REFS.
  285. */
  286. #define IOCTL_GNTDEV_DMABUF_IMP_RELEASE \
  287. _IOC(_IOC_NONE, 'G', 12, \
  288. sizeof(struct ioctl_gntdev_dmabuf_imp_release))
  289. struct ioctl_gntdev_dmabuf_imp_release {
  290. /* IN parameters */
  291. __u32 fd;
  292. __u32 reserved;
  293. };
  294. #endif /* __LINUX_PUBLIC_GNTDEV_H__ */