gntdev.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.
  47. */
  48. #define IOCTL_GNTDEV_MAP_GRANT_REF \
  49. _IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_gntdev_map_grant_ref))
  50. struct ioctl_gntdev_map_grant_ref {
  51. /* IN parameters */
  52. /* The number of grants to be mapped. */
  53. __u32 count;
  54. __u32 pad;
  55. /* OUT parameters */
  56. /* The offset to be used on a subsequent call to mmap(). */
  57. __u64 index;
  58. /* Variable IN parameter. */
  59. /* Array of grant references, of size @count. */
  60. struct ioctl_gntdev_grant_ref refs[1];
  61. };
  62. /*
  63. * Removes the grant references from the mapping table of an instance of
  64. * of gntdev. N.B. munmap() must be called on the relevant virtual address(es)
  65. * before this ioctl is called, or an error will result.
  66. */
  67. #define IOCTL_GNTDEV_UNMAP_GRANT_REF \
  68. _IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_gntdev_unmap_grant_ref))
  69. struct ioctl_gntdev_unmap_grant_ref {
  70. /* IN parameters */
  71. /* The offset was returned by the corresponding map operation. */
  72. __u64 index;
  73. /* The number of pages to be unmapped. */
  74. __u32 count;
  75. __u32 pad;
  76. };
  77. /*
  78. * Returns the offset in the driver's address space that corresponds
  79. * to @vaddr. This can be used to perform a munmap(), followed by an
  80. * UNMAP_GRANT_REF ioctl, where no state about the offset is retained by
  81. * the caller. The number of pages that were allocated at the same time as
  82. * @vaddr is returned in @count.
  83. *
  84. * N.B. Where more than one page has been mapped into a contiguous range, the
  85. * supplied @vaddr must correspond to the start of the range; otherwise
  86. * an error will result. It is only possible to munmap() the entire
  87. * contiguously-allocated range at once, and not any subrange thereof.
  88. */
  89. #define IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR \
  90. _IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_gntdev_get_offset_for_vaddr))
  91. struct ioctl_gntdev_get_offset_for_vaddr {
  92. /* IN parameters */
  93. /* The virtual address of the first mapped page in a range. */
  94. __u64 vaddr;
  95. /* OUT parameters */
  96. /* The offset that was used in the initial mmap() operation. */
  97. __u64 offset;
  98. /* The number of pages mapped in the VM area that begins at @vaddr. */
  99. __u32 count;
  100. __u32 pad;
  101. };
  102. /*
  103. * Sets the maximum number of grants that may mapped at once by this gntdev
  104. * instance.
  105. *
  106. * N.B. This must be called before any other ioctl is performed on the device.
  107. */
  108. #define IOCTL_GNTDEV_SET_MAX_GRANTS \
  109. _IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_gntdev_set_max_grants))
  110. struct ioctl_gntdev_set_max_grants {
  111. /* IN parameter */
  112. /* The maximum number of grants that may be mapped at once. */
  113. __u32 count;
  114. };
  115. /*
  116. * Sets up an unmap notification within the page, so that the other side can do
  117. * cleanup if this side crashes. Required to implement cross-domain robust
  118. * mutexes or close notification on communication channels.
  119. *
  120. * Each mapped page only supports one notification; multiple calls referring to
  121. * the same page overwrite the previous notification. You must clear the
  122. * notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it
  123. * to occur.
  124. */
  125. #define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \
  126. _IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify))
  127. struct ioctl_gntdev_unmap_notify {
  128. /* IN parameters */
  129. /* Offset in the file descriptor for a byte within the page (same as
  130. * used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to
  131. * be cleared. Otherwise, it can be any byte in the page whose
  132. * notification we are adjusting.
  133. */
  134. __u64 index;
  135. /* Action(s) to take on unmap */
  136. __u32 action;
  137. /* Event channel to notify */
  138. __u32 event_channel_port;
  139. };
  140. struct gntdev_grant_copy_segment {
  141. union {
  142. void *virt;
  143. struct {
  144. grant_ref_t ref;
  145. __u16 offset;
  146. domid_t domid;
  147. } foreign;
  148. } source, dest;
  149. __u16 len;
  150. __u16 flags; /* GNTCOPY_* */
  151. __s16 status; /* GNTST_* */
  152. };
  153. /*
  154. * Copy between grant references and local buffers.
  155. *
  156. * The copy is split into @count @segments, each of which can copy
  157. * to/from one grant reference.
  158. *
  159. * Each segment is similar to struct gnttab_copy in the hypervisor ABI
  160. * except the local buffer is specified using a virtual address
  161. * (instead of a GFN and offset).
  162. *
  163. * The local buffer may cross a Xen page boundary -- the driver will
  164. * split segments into multiple ops if required.
  165. *
  166. * Returns 0 if all segments have been processed and @status in each
  167. * segment is valid. Note that one or more segments may have failed
  168. * (status != GNTST_okay).
  169. *
  170. * If the driver had to split a segment into two or more ops, @status
  171. * includes the status of the first failed op for that segment (or
  172. * GNTST_okay if all ops were successful).
  173. *
  174. * If -1 is returned, the status of all segments is undefined.
  175. *
  176. * EINVAL: A segment has local buffers for both source and
  177. * destination.
  178. * EINVAL: A segment crosses the boundary of a foreign page.
  179. * EFAULT: A segment's local buffer is not accessible.
  180. */
  181. #define IOCTL_GNTDEV_GRANT_COPY \
  182. _IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))
  183. struct ioctl_gntdev_grant_copy {
  184. unsigned int count;
  185. struct gntdev_grant_copy_segment *segments;
  186. };
  187. /* Clear (set to zero) the byte specified by index */
  188. #define UNMAP_NOTIFY_CLEAR_BYTE 0x1
  189. /* Send an interrupt on the indicated event channel */
  190. #define UNMAP_NOTIFY_SEND_EVENT 0x2
  191. /*
  192. * Flags to be used while requesting memory mapping's backing storage
  193. * to be allocated with DMA API.
  194. */
  195. /*
  196. * The buffer is backed with memory allocated with dma_alloc_wc.
  197. */
  198. #define GNTDEV_DMA_FLAG_WC (1 << 0)
  199. /*
  200. * The buffer is backed with memory allocated with dma_alloc_coherent.
  201. */
  202. #define GNTDEV_DMA_FLAG_COHERENT (1 << 1)
  203. /*
  204. * Create a dma-buf [1] from grant references @refs of count @count provided
  205. * by the foreign domain @domid with flags @flags.
  206. *
  207. * By default dma-buf is backed by system memory pages, but by providing
  208. * one of the GNTDEV_DMA_FLAG_XXX flags it can also be created as
  209. * a DMA write-combine or coherent buffer, e.g. allocated with dma_alloc_wc/
  210. * dma_alloc_coherent.
  211. *
  212. * Returns 0 if dma-buf was successfully created and the corresponding
  213. * dma-buf's file descriptor is returned in @fd.
  214. *
  215. * [1] Documentation/driver-api/dma-buf.rst
  216. */
  217. #define IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS \
  218. _IOC(_IOC_NONE, 'G', 9, \
  219. sizeof(struct ioctl_gntdev_dmabuf_exp_from_refs))
  220. struct ioctl_gntdev_dmabuf_exp_from_refs {
  221. /* IN parameters. */
  222. /* Specific options for this dma-buf: see GNTDEV_DMA_FLAG_XXX. */
  223. __u32 flags;
  224. /* Number of grant references in @refs array. */
  225. __u32 count;
  226. /* OUT parameters. */
  227. /* File descriptor of the dma-buf. */
  228. __u32 fd;
  229. /* The domain ID of the grant references to be mapped. */
  230. __u32 domid;
  231. /* Variable IN parameter. */
  232. /* Array of grant references of size @count. */
  233. __u32 refs[1];
  234. };
  235. /*
  236. * This will block until the dma-buf with the file descriptor @fd is
  237. * released. This is only valid for buffers created with
  238. * IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS.
  239. *
  240. * If within @wait_to_ms milliseconds the buffer is not released
  241. * then -ETIMEDOUT error is returned.
  242. * If the buffer with the file descriptor @fd does not exist or has already
  243. * been released, then -ENOENT is returned. For valid file descriptors
  244. * this must not be treated as error.
  245. */
  246. #define IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED \
  247. _IOC(_IOC_NONE, 'G', 10, \
  248. sizeof(struct ioctl_gntdev_dmabuf_exp_wait_released))
  249. struct ioctl_gntdev_dmabuf_exp_wait_released {
  250. /* IN parameters */
  251. __u32 fd;
  252. __u32 wait_to_ms;
  253. };
  254. /*
  255. * Import a dma-buf with file descriptor @fd and export granted references
  256. * to the pages of that dma-buf into array @refs of size @count.
  257. */
  258. #define IOCTL_GNTDEV_DMABUF_IMP_TO_REFS \
  259. _IOC(_IOC_NONE, 'G', 11, \
  260. sizeof(struct ioctl_gntdev_dmabuf_imp_to_refs))
  261. struct ioctl_gntdev_dmabuf_imp_to_refs {
  262. /* IN parameters. */
  263. /* File descriptor of the dma-buf. */
  264. __u32 fd;
  265. /* Number of grant references in @refs array. */
  266. __u32 count;
  267. /* The domain ID for which references to be granted. */
  268. __u32 domid;
  269. /* Reserved - must be zero. */
  270. __u32 reserved;
  271. /* OUT parameters. */
  272. /* Array of grant references of size @count. */
  273. __u32 refs[1];
  274. };
  275. /*
  276. * This will close all references to the imported buffer with file descriptor
  277. * @fd, so it can be released by the owner. This is only valid for buffers
  278. * created with IOCTL_GNTDEV_DMABUF_IMP_TO_REFS.
  279. */
  280. #define IOCTL_GNTDEV_DMABUF_IMP_RELEASE \
  281. _IOC(_IOC_NONE, 'G', 12, \
  282. sizeof(struct ioctl_gntdev_dmabuf_imp_release))
  283. struct ioctl_gntdev_dmabuf_imp_release {
  284. /* IN parameters */
  285. __u32 fd;
  286. __u32 reserved;
  287. };
  288. #endif /* __LINUX_PUBLIC_GNTDEV_H__ */