vas-api.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _VAS-API:
  3. ===================================================
  4. Virtual Accelerator Switchboard (VAS) userspace API
  5. ===================================================
  6. Introduction
  7. ============
  8. Power9 processor introduced Virtual Accelerator Switchboard (VAS) which
  9. allows both userspace and kernel communicate to co-processor
  10. (hardware accelerator) referred to as the Nest Accelerator (NX). The NX
  11. unit comprises of one or more hardware engines or co-processor types
  12. such as 842 compression, GZIP compression and encryption. On power9,
  13. userspace applications will have access to only GZIP Compression engine
  14. which supports ZLIB and GZIP compression algorithms in the hardware.
  15. To communicate with NX, kernel has to establish a channel or window and
  16. then requests can be submitted directly without kernel involvement.
  17. Requests to the GZIP engine must be formatted as a co-processor Request
  18. Block (CRB) and these CRBs must be submitted to the NX using COPY/PASTE
  19. instructions to paste the CRB to hardware address that is associated with
  20. the engine's request queue.
  21. The GZIP engine provides two priority levels of requests: Normal and
  22. High. Only Normal requests are supported from userspace right now.
  23. This document explains userspace API that is used to interact with
  24. kernel to setup channel / window which can be used to send compression
  25. requests directly to NX accelerator.
  26. Overview
  27. ========
  28. Application access to the GZIP engine is provided through
  29. /dev/crypto/nx-gzip device node implemented by the VAS/NX device driver.
  30. An application must open the /dev/crypto/nx-gzip device to obtain a file
  31. descriptor (fd). Then should issue VAS_TX_WIN_OPEN ioctl with this fd to
  32. establish connection to the engine. It means send window is opened on GZIP
  33. engine for this process. Once a connection is established, the application
  34. should use the mmap() system call to map the hardware address of engine's
  35. request queue into the application's virtual address space.
  36. The application can then submit one or more requests to the engine by
  37. using copy/paste instructions and pasting the CRBs to the virtual address
  38. (aka paste_address) returned by mmap(). User space can close the
  39. established connection or send window by closing the file descriptior
  40. (close(fd)) or upon the process exit.
  41. Note that applications can send several requests with the same window or
  42. can establish multiple windows, but one window for each file descriptor.
  43. Following sections provide additional details and references about the
  44. individual steps.
  45. NX-GZIP Device Node
  46. ===================
  47. There is one /dev/crypto/nx-gzip node in the system and it provides
  48. access to all GZIP engines in the system. The only valid operations on
  49. /dev/crypto/nx-gzip are:
  50. * open() the device for read and write.
  51. * issue VAS_TX_WIN_OPEN ioctl
  52. * mmap() the engine's request queue into application's virtual
  53. address space (i.e. get a paste_address for the co-processor
  54. engine).
  55. * close the device node.
  56. Other file operations on this device node are undefined.
  57. Note that the copy and paste operations go directly to the hardware and
  58. do not go through this device. Refer COPY/PASTE document for more
  59. details.
  60. Although a system may have several instances of the NX co-processor
  61. engines (typically, one per P9 chip) there is just one
  62. /dev/crypto/nx-gzip device node in the system. When the nx-gzip device
  63. node is opened, Kernel opens send window on a suitable instance of NX
  64. accelerator. It finds CPU on which the user process is executing and
  65. determine the NX instance for the corresponding chip on which this CPU
  66. belongs.
  67. Applications may chose a specific instance of the NX co-processor using
  68. the vas_id field in the VAS_TX_WIN_OPEN ioctl as detailed below.
  69. A userspace library libnxz is available here but still in development:
  70. https://github.com/abalib/power-gzip
  71. Applications that use inflate / deflate calls can link with libnxz
  72. instead of libz and use NX GZIP compression without any modification.
  73. Open /dev/crypto/nx-gzip
  74. ========================
  75. The nx-gzip device should be opened for read and write. No special
  76. privileges are needed to open the device. Each window corresponds to one
  77. file descriptor. So if the userspace process needs multiple windows,
  78. several open calls have to be issued.
  79. See open(2) system call man pages for other details such as return values,
  80. error codes and restrictions.
  81. VAS_TX_WIN_OPEN ioctl
  82. =====================
  83. Applications should use the VAS_TX_WIN_OPEN ioctl as follows to establish
  84. a connection with NX co-processor engine:
  85. ::
  86. struct vas_tx_win_open_attr {
  87. __u32 version;
  88. __s16 vas_id; /* specific instance of vas or -1
  89. for default */
  90. __u16 reserved1;
  91. __u64 flags; /* For future use */
  92. __u64 reserved2[6];
  93. };
  94. version:
  95. The version field must be currently set to 1.
  96. vas_id:
  97. If '-1' is passed, kernel will make a best-effort attempt
  98. to assign an optimal instance of NX for the process. To
  99. select the specific VAS instance, refer
  100. "Discovery of available VAS engines" section below.
  101. flags, reserved1 and reserved2[6] fields are for future extension
  102. and must be set to 0.
  103. The attributes attr for the VAS_TX_WIN_OPEN ioctl are defined as
  104. follows::
  105. #define VAS_MAGIC 'v'
  106. #define VAS_TX_WIN_OPEN _IOW(VAS_MAGIC, 1,
  107. struct vas_tx_win_open_attr)
  108. struct vas_tx_win_open_attr attr;
  109. rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
  110. The VAS_TX_WIN_OPEN ioctl returns 0 on success. On errors, it
  111. returns -1 and sets the errno variable to indicate the error.
  112. Error conditions:
  113. ====== ================================================
  114. EINVAL fd does not refer to a valid VAS device.
  115. EINVAL Invalid vas ID
  116. EINVAL version is not set with proper value
  117. EEXIST Window is already opened for the given fd
  118. ENOMEM Memory is not available to allocate window
  119. ENOSPC System has too many active windows (connections)
  120. opened
  121. EINVAL reserved fields are not set to 0.
  122. ====== ================================================
  123. See the ioctl(2) man page for more details, error codes and
  124. restrictions.
  125. mmap() NX-GZIP device
  126. =====================
  127. The mmap() system call for a NX-GZIP device fd returns a paste_address
  128. that the application can use to copy/paste its CRB to the hardware engines.
  129. ::
  130. paste_addr = mmap(addr, size, prot, flags, fd, offset);
  131. Only restrictions on mmap for a NX-GZIP device fd are:
  132. * size should be PAGE_SIZE
  133. * offset parameter should be 0ULL
  134. Refer to mmap(2) man page for additional details/restrictions.
  135. In addition to the error conditions listed on the mmap(2) man
  136. page, can also fail with one of the following error codes:
  137. ====== =============================================
  138. EINVAL fd is not associated with an open window
  139. (i.e mmap() does not follow a successful call
  140. to the VAS_TX_WIN_OPEN ioctl).
  141. EINVAL offset field is not 0ULL.
  142. ====== =============================================
  143. Discovery of available VAS engines
  144. ==================================
  145. Each available VAS instance in the system will have a device tree node
  146. like /proc/device-tree/vas@* or /proc/device-tree/xscom@*/vas@*.
  147. Determine the chip or VAS instance and use the corresponding ibm,vas-id
  148. property value in this node to select specific VAS instance.
  149. Copy/Paste operations
  150. =====================
  151. Applications should use the copy and paste instructions to send CRB to NX.
  152. Refer section 4.4 in PowerISA for Copy/Paste instructions:
  153. https://openpowerfoundation.org/?resource_lib=power-isa-version-3-0
  154. CRB Specification and use NX
  155. ============================
  156. Applications should format requests to the co-processor using the
  157. co-processor Request Block (CRBs). Refer NX-GZIP user's manual for the format
  158. of CRB and use NX from userspace such as sending requests and checking
  159. request status.
  160. NX Fault handling
  161. =================
  162. Applications send requests to NX and wait for the status by polling on
  163. co-processor Status Block (CSB) flags. NX updates status in CSB after each
  164. request is processed. Refer NX-GZIP user's manual for the format of CSB and
  165. status flags.
  166. In case if NX encounters translation error (called NX page fault) on CSB
  167. address or any request buffer, raises an interrupt on the CPU to handle the
  168. fault. Page fault can happen if an application passes invalid addresses or
  169. request buffers are not in memory. The operating system handles the fault by
  170. updating CSB with the following data::
  171. csb.flags = CSB_V;
  172. csb.cc = CSB_CC_FAULT_ADDRESS;
  173. csb.ce = CSB_CE_TERMINATION;
  174. csb.address = fault_address;
  175. When an application receives translation error, it can touch or access
  176. the page that has a fault address so that this page will be in memory. Then
  177. the application can resend this request to NX.
  178. If the OS can not update CSB due to invalid CSB address, sends SEGV signal
  179. to the process who opened the send window on which the original request was
  180. issued. This signal returns with the following siginfo struct::
  181. siginfo.si_signo = SIGSEGV;
  182. siginfo.si_errno = EFAULT;
  183. siginfo.si_code = SEGV_MAPERR;
  184. siginfo.si_addr = CSB adress;
  185. In the case of multi-thread applications, NX send windows can be shared
  186. across all threads. For example, a child thread can open a send window,
  187. but other threads can send requests to NX using this window. These
  188. requests will be successful even in the case of OS handling faults as long
  189. as CSB address is valid. If the NX request contains an invalid CSB address,
  190. the signal will be sent to the child thread that opened the window. But if
  191. the thread is exited without closing the window and the request is issued
  192. using this window. the signal will be issued to the thread group leader
  193. (tgid). It is up to the application whether to ignore or handle these
  194. signals.
  195. NX-GZIP User's Manual:
  196. https://github.com/libnxz/power-gzip/blob/master/power_nx_gzip_um.pdf
  197. Simple example
  198. ==============
  199. ::
  200. int use_nx_gzip()
  201. {
  202. int rc, fd;
  203. void *addr;
  204. struct vas_setup_attr txattr;
  205. fd = open("/dev/crypto/nx-gzip", O_RDWR);
  206. if (fd < 0) {
  207. fprintf(stderr, "open nx-gzip failed\n");
  208. return -1;
  209. }
  210. memset(&txattr, 0, sizeof(txattr));
  211. txattr.version = 1;
  212. txattr.vas_id = -1
  213. rc = ioctl(fd, VAS_TX_WIN_OPEN,
  214. (unsigned long)&txattr);
  215. if (rc < 0) {
  216. fprintf(stderr, "ioctl() n %d, error %d\n",
  217. rc, errno);
  218. return rc;
  219. }
  220. addr = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
  221. MAP_SHARED, fd, 0ULL);
  222. if (addr == MAP_FAILED) {
  223. fprintf(stderr, "mmap() failed, errno %d\n",
  224. errno);
  225. return -errno;
  226. }
  227. do {
  228. //Format CRB request with compression or
  229. //uncompression
  230. // Refer tests for vas_copy/vas_paste
  231. vas_copy((&crb, 0, 1);
  232. vas_paste(addr, 0, 1);
  233. // Poll on csb.flags with timeout
  234. // csb address is listed in CRB
  235. } while (true)
  236. close(fd) or window can be closed upon process exit
  237. }
  238. Refer https://github.com/abalib/power-gzip for tests or more
  239. use cases.