요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GPL-2.0
.. _VAS-API:
===================================================
Virtual Accelerator Switchboard (VAS) userspace API
===================================================
Introduction
============
Power9 processor introduced Virtual Accelerator Switchboard (VAS) which
allows both userspace and kernel communicate to co-processor
(hardware accelerator) referred to as the Nest Accelerator (NX). The NX
unit comprises of one or more hardware engines or co-processor types
such as 842 compression, GZIP compression and encryption. On power9,
userspace applications will have access to only GZIP Compression engine
which supports ZLIB and GZIP compression algorithms in the hardware.
To communicate with NX, kernel has to establish a channel or window and
then requests can be submitted directly without kernel involvement.
Requests to the GZIP engine must be formatted as a co-processor Request
Block (CRB) and these CRBs must be submitted to the NX using COPY/PASTE
instructions to paste the CRB to hardware address that is associated with
the engine's request queue.
The GZIP engine provides two priority levels of requests: Normal and
High. Only Normal requests are supported from userspace right now.
This document explains userspace API that is used to interact with
kernel to setup channel / window which can be used to send compression
requests directly to NX accelerator.
Overview
========
Application access to the GZIP engine is provided through
/dev/crypto/nx-gzip device node implemented by the VAS/NX device driver.
An application must open the /dev/crypto/nx-gzip device to obtain a file
descriptor (fd). Then should issue VAS_TX_WIN_OPEN ioctl with this fd to
establish connection to the engine. It means send window is opened on GZIP
engine for this process. Once a connection is established, the application
should use the mmap() system call to map the hardware address of engine's
request queue into the application's virtual address space.
The application can then submit one or more requests to the engine by
using copy/paste instructions and pasting the CRBs to the virtual address
(aka paste_address) returned by mmap(). User space can close the
established connection or send window by closing the file descriptor
(close(fd)) or upon the process exit.
Note that applications can send several requests with the same window or
can establish multiple windows, but one window for each file descriptor.
Following sections provide additional details and references about the
individual steps.
NX-GZIP Device Node
===================
There is one /dev/crypto/nx-gzip node in the system and it provides
access to all GZIP engines in the system. The only valid operations on
/dev/crypto/nx-gzip are:
* open() the device for read and write.
* issue VAS_TX_WIN_OPEN ioctl
* mmap() the engine's request queue into application's virtual
address space (i.e. get a paste_address for the co-processor
engine).
* close the device node.
Other file operations on this device node are undefined.
Note that the copy and paste operations go directly to the hardware and
do not go through this device. Refer COPY/PASTE document for more
details.
Although a system may have several instances of the NX co-processor
engines (typically, one per P9 chip) there is just one
/dev/crypto/nx-gzip device node in the system. When the nx-gzip device
node is opened, Kernel opens send window on a suitable instance of NX
accelerator. It finds CPU on which the user process is executing and
determine the NX instance for the corresponding chip on which this CPU
belongs.
Applications may chose a specific instance of the NX co-processor using
the vas_id field in the VAS_TX_WIN_OPEN ioctl as detailed below.
A userspace library libnxz is available here but still in development:
https://github.com/abalib/power-gzip
Applications that use inflate / deflate calls can link with libnxz
instead of libz and use NX GZIP compression without any modification.
Open /dev/crypto/nx-gzip
========================
The nx-gzip device should be opened for read and write. No special
privileges are needed to open the device. Each window corresponds to one
file descriptor. So if the userspace process needs multiple windows,
several open calls have to be issued.
See open(2) system call man pages for other details such as return values,
error codes and restrictions.
VAS_TX_WIN_OPEN ioctl
=====================
Applications should use the VAS_TX_WIN_OPEN ioctl as follows to establish
a connection with NX co-processor engine:
::
struct vas_tx_win_open_attr {
__u32 version;
__s16 vas_id; /* specific instance of vas or -1
for default */
__u16 reserved1;
__u64 flags; /* For future use */
__u64 reserved2[6];
};
version:
The version field must be currently set to 1.
vas_id:
If '-1' is passed, kernel will make a best-effort attempt
to assign an optimal instance of NX for the process. To
select the specific VAS instance, refer
"Discovery of available VAS engines" section below.
flags, reserved1 and reserved2[6] fields are for future extension
and must be set to 0.
The attributes attr for the VAS_TX_WIN_OPEN ioctl are defined as
follows::
#define VAS_MAGIC 'v'
#define VAS_TX_WIN_OPEN _IOW(VAS_MAGIC, 1,
struct vas_tx_win_open_attr)
struct vas_tx_win_open_attr attr;
rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
The VAS_TX_WIN_OPEN ioctl returns 0 on success. On errors, it
returns -1 and sets the errno variable to indicate the error.
Error conditions:
====== ================================================
EINVAL fd does not refer to a valid VAS device.
EINVAL Invalid vas ID
EINVAL version is not set with proper value
EEXIST Window is already opened for the given fd
ENOMEM Memory is not available to allocate window
ENOSPC System has too many active windows (connections)
opened
EINVAL reserved fields are not set to 0.
====== ================================================
See the ioctl(2) man page for more details, error codes and
restrictions.
mmap() NX-GZIP device
=====================
The mmap() system call for a NX-GZIP device fd returns a paste_address
that the application can use to copy/paste its CRB to the hardware engines.
::
paste_addr = mmap(addr, size, prot, flags, fd, offset);
Only restrictions on mmap for a NX-GZIP device fd are:
* size should be PAGE_SIZE
* offset parameter should be 0ULL
Refer to mmap(2) man page for additional details/restrictions.
In addition to the error conditions listed on the mmap(2) man
page, can also fail with one of the following error codes:
====== =============================================
EINVAL fd is not associated with an open window
(i.e mmap() does not follow a successful call
to the VAS_TX_WIN_OPEN ioctl).
EINVAL offset field is not 0ULL.
====== =============================================
Discovery of available VAS engines
==================================
Each available VAS instance in the system will have a device tree node
like /proc/device-tree/vas@* or /proc/device-tree/xscom@*/vas@*.
Determine the chip or VAS instance and use the corresponding ibm,vas-id
property value in this node to select specific VAS instance.
Copy/Paste operations
=====================
Applications should use the copy and paste instructions to send CRB to NX.
Refer section 4.4 in PowerISA for Copy/Paste instructions:
https://openpowerfoundation.org/?resource_lib=power-isa-version-3-0
CRB Specification and use NX
============================
Applications should format requests to the co-processor using the
co-processor Request Block (CRBs). Refer NX-GZIP user's manual for the format
of CRB and use NX from userspace such as sending requests and checking
request status.
NX Fault handling
=================
Applications send requests to NX and wait for the status by polling on
co-processor Status Block (CSB) flags. NX updates status in CSB after each
request is processed. Refer NX-GZIP user's manual for the format of CSB and
status flags.
In case if NX encounters translation error (called NX page fault) on CSB
address or any request buffer, raises an interrupt on the CPU to handle the
fault. Page fault can happen if an application passes invalid addresses or
request buffers are not in memory. The operating system handles the fault by
updating CSB with the following data::
csb.flags = CSB_V;
csb.cc = CSB_CC_FAULT_ADDRESS;
csb.ce = CSB_CE_TERMINATION;
csb.address = fault_address;
When an application receives translation error, it can touch or access
the page that has a fault address so that this page will be in memory. Then
the application can resend this request to NX.
If the OS can not update CSB due to invalid CSB address, sends SEGV signal
to the process who opened the send window on which the original request was
issued. This signal returns with the following siginfo struct::
siginfo.si_signo = SIGSEGV;
siginfo.si_errno = EFAULT;
siginfo.si_code = SEGV_MAPERR;
siginfo.si_addr = CSB address;
In the case of multi-thread applications, NX send windows can be shared
across all threads. For example, a child thread can open a send window,
but other threads can send requests to NX using this window. These
requests will be successful even in the case of OS handling faults as long
as CSB address is valid. If the NX request contains an invalid CSB address,
the signal will be sent to the child thread that opened the window. But if
the thread is exited without closing the window and the request is issued
using this window. the signal will be issued to the thread group leader
(tgid). It is up to the application whether to ignore or handle these
signals.
NX-GZIP User's Manual:
https://github.com/libnxz/power-gzip/blob/master/doc/power_nx_gzip_um.pdf
Simple example
==============
::
int use_nx_gzip()
{
int rc, fd;
void *addr;
struct vas_setup_attr txattr;
fd = open("/dev/crypto/nx-gzip", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open nx-gzip failed\n");
return -1;
}
memset(&txattr, 0, sizeof(txattr));
txattr.version = 1;
txattr.vas_id = -1
rc = ioctl(fd, VAS_TX_WIN_OPEN,
(unsigned long)&txattr);
if (rc < 0) {
fprintf(stderr, "ioctl() n %d, error %d\n",
rc, errno);
return rc;
}
addr = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0ULL);
if (addr == MAP_FAILED) {
fprintf(stderr, "mmap() failed, errno %d\n",
errno);
return -errno;
}
do {
//Format CRB request with compression or
//uncompression
// Refer tests for vas_copy/vas_paste
vas_copy((&crb, 0, 1);
vas_paste(addr, 0, 1);
// Poll on csb.flags with timeout
// csb address is listed in CRB
} while (true)
close(fd) or window can be closed upon process exit
}
Refer https://github.com/libnxz/power-gzip for tests or more
use cases.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
Virtual Accelerator Switchboard userspace API
1-7이 문서는 POWER9 Virtual Accelerator Switchboard(VAS)를 통해 userspace가 NX GZIP hardware accelerator에 직접 request를 보내는 API를 설명합니다.
VAS와 NX 소개
8-33POWER9의 VAS는 userspace와 kernel이 Nest Accelerator(NX) co-processor와 통신하도록 합니다. NX에는 842 compression, GZIP compression, encryption 같은 하나 이상의 hardware engine이 있습니다.
POWER9 userspace application은 현재 ZLIB 및 GZIP algorithm을 hardware로 처리하는 GZIP Compression engine만 사용할 수 있습니다.
Kernel은 NX와의 channel 또는 window를 먼저 설정합니다. 이후 request는 kernel 개입 없이 직접 제출할 수 있습니다. GZIP request는 co-processor Request Block(CRB) 형식이어야 하며 `COPY`/`PASTE` instruction으로 engine request queue에 대응하는 hardware address에 paste합니다.
GZIP engine은 Normal과 High 두 priority를 제공하지만 현재 userspace에서는 Normal request만 지원합니다.
이 API는 compression request를 NX accelerator에 직접 보낼 channel/window를 kernel과 설정하는 절차를 정의합니다.
Userspace 연결 lifecycle
34-57Application은 VAS/NX driver가 제공하는 `/dev/crypto/nx-gzip`을 open해 file descriptor를 얻고, 그 fd로 `VAS_TX_WIN_OPEN` ioctl을 실행하여 process용 GZIP send window를 엽니다.
연결 뒤 `mmap()`으로 engine request queue의 hardware address를 application virtual address space에 mapping합니다. 반환된 `paste_address`에 CRB를 copy/paste하여 여러 request를 보냅니다.
`close(fd)` 또는 process exit로 window를 닫습니다. 같은 window로 여러 request를 보낼 수 있고 여러 window도 만들 수 있지만 file descriptor 하나에는 window 하나만 대응합니다.
Device open부터 direct COPY/PASTE request와 window close까지의 순서입니다.
NX-GZIP device node
58-95System에는 `/dev/crypto/nx-gzip` node가 하나만 있으며 모든 GZIP engine에 접근합니다. Valid operation은 다음뿐이고 다른 file operation은 undefined입니다.
| Operation | 역할 |
|---|---|
| `open()` | Device를 read/write로 열어 window별 file descriptor 생성 |
| `VAS_TX_WIN_OPEN` | NX GZIP send window 연결 |
| `mmap()` | Engine request queue의 `paste_address` mapping |
| `close()` | Send window와 device descriptor 종료 |
COPY/PASTE는 device node를 거치지 않고 hardware로 직접 갑니다. 세부 instruction은 COPY/PASTE 문서를 따라야 합니다.
P9 chip마다 NX instance가 있을 수 있지만 node는 하나입니다. Device open 시 kernel은 user process가 실행 중인 CPU와 그 CPU가 속한 chip을 확인해 적합한 NX instance에 send window를 엽니다. Application은 `VAS_TX_WIN_OPEN`의 `vas_id`로 특정 instance를 선택할 수도 있습니다.
개발 중인 `libnxz` userspace library를 사용하면 `inflate`/`deflate` application이 `libz` 대신 link하여 source 수정 없이 NX GZIP compression을 사용할 수 있습니다.
하나의 device node가 process CPU의 chip 또는 명시한 vas_id에 따라 NX instance를 선택합니다.
Device open
96-106NX-GZIP device는 read/write로 open하며 특별한 privilege가 필요하지 않습니다. Window 하나가 fd 하나에 대응하므로 여러 window가 필요하면 여러 번 open해야 합니다.
Return value, error code와 restriction의 일반 사항은 `open(2)` man page를 따릅니다.
VAS_TX_WIN_OPEN ioctl
107-163NX co-processor engine과 connection을 만들기 위해 다음 attribute structure와 ioctl을 사용합니다.
struct vas_tx_win_open_attr {
__u32 version;
__s16 vas_id; /* specific instance of vas or -1
for default */
__u16 reserved1;
__u64 flags; /* For future use */
__u64 reserved2[6];
};
| Field | 규칙 |
|---|---|
| `version` | 현재 반드시 `1` |
| `vas_id` | `-1`이면 kernel이 optimal NX instance를 best-effort 선택 |
| `reserved1` | Future extension용, `0` |
| `flags` | Future use, `0` |
| `reserved2[6]` | Future extension용, 모두 `0` |
`vas_id=-1`이면 kernel이 process에 optimal한 NX instance를 best-effort로 배정합니다. 특정 VAS 선택에는 아래 discovery 절차의 `ibm,vas-id` 값을 사용합니다.
#define VAS_MAGIC 'v'
#define VAS_TX_WIN_OPEN _IOW(VAS_MAGIC, 1,
struct vas_tx_win_open_attr)
struct vas_tx_win_open_attr attr;
rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
`VAS_TX_WIN_OPEN`은 성공 시 0, 실패 시 -1을 반환하고 `errno`를 설정합니다.
| errno | 조건 |
|---|---|
| `EINVAL` | `fd`가 valid VAS device가 아님 |
| `EINVAL` | Invalid VAS ID |
| `EINVAL` | `version` 값이 올바르지 않음 |
| `EEXIST` | 해당 `fd`에 window가 이미 열림 |
| `ENOMEM` | Window 할당 memory 부족 |
| `ENOSPC` | System active window 수가 너무 많음 |
| `EINVAL` | Reserved field가 0이 아님 |
추가 ioctl 제약은 `ioctl(2)` man page를 따릅니다.
NX-GZIP mmap
164-189NX-GZIP fd의 `mmap()`은 CRB를 hardware engine에 copy/paste할 `paste_address`를 반환합니다.
paste_addr = mmap(addr, size, prot, flags, fd, offset);
| Parameter | Restriction |
|---|---|
| `size` | `PAGE_SIZE` |
| `offset` | `0ULL` |
일반 `mmap(2)` error 외에 다음 NX-GZIP-specific `EINVAL` 조건이 있습니다.
| errno | 조건 |
|---|---|
| `EINVAL` | `fd`가 open window와 연결되지 않음 |
| `EINVAL` | `offset`이 `0ULL`이 아님 |
사용 가능한 VAS engine discovery
190-197각 VAS instance에는 `/proc/device-tree/vas@*` 또는 `/proc/device-tree/xscom@*/vas@*` 형태의 device-tree node가 있습니다. Chip/VAS instance를 찾고 node의 `ibm,vas-id` property를 `vas_id`에 사용합니다.
COPY/PASTE operation
198-204Application은 COPY와 PASTE instruction으로 CRB를 NX에 보냅니다. 자세한 instruction은 PowerISA section 4.4를 참고합니다.
CRB specification
205-212Co-processor request는 CRB 형식으로 작성해야 합니다. CRB format, userspace request 제출과 status 확인은 NX-GZIP user's manual을 따릅니다.
NX fault handling
213-258Application은 NX request를 보내고 co-processor Status Block(CSB) flag를 polling합니다. NX는 request 처리 뒤 CSB status를 update합니다.
CSB address나 request buffer에서 translation error, 즉 NX page fault가 발생하면 NX가 CPU interrupt를 올립니다. Invalid address를 전달했거나 request buffer가 memory에 없을 때 발생할 수 있습니다.
OS가 CSB에 기록하는 fault data는 다음과 같습니다.
csb.flags = CSB_V;
csb.cc = CSB_CC_FAULT_ADDRESS;
csb.ce = CSB_CE_TERMINATION;
csb.address = fault_address;
Application이 translation error를 받으면 fault address page를 touch하여 memory에 올린 뒤 request를 NX에 다시 제출할 수 있습니다.
Invalid CSB address 때문에 OS가 CSB를 update할 수 없으면 send window를 연 process에 `SIGSEGV`를 보냅니다.
siginfo.si_signo = SIGSEGV;
siginfo.si_errno = EFAULT;
siginfo.si_code = SEGV_MAPERR;
siginfo.si_addr = CSB address;
| 상태 | OS 처리 |
|---|---|
| CSB address valid | OS가 CSB에 `CSB_CC_FAULT_ADDRESS`, `CSB_CE_TERMINATION`, fault address 기록 |
| CSB address invalid | Window를 연 thread 또는 그 thread가 종료된 경우 thread-group leader에 `SIGSEGV` |
Multithread application에서는 NX send window를 모든 thread가 share할 수 있습니다. CSB address가 valid하면 다른 thread의 request도 OS fault handling과 함께 성공합니다.
Invalid CSB request의 signal은 window를 연 child thread로 갑니다. 그 thread가 window를 닫지 않고 exit한 뒤 request가 발행되면 thread-group leader(`tgid`)에게 signal을 보냅니다. Signal을 무시하거나 처리할지는 application 책임입니다.
CSB validity에 따라 retry 가능한 status update 또는 SIGSEGV delivery로 나뉩니다.
Simple example
259-305다음 원문 예제는 device open, attribute 초기화, ioctl, mmap, CRB copy/paste, CSB polling과 close의 전체 흐름을 보여 줍니다.
int use_nx_gzip()
{
int rc, fd;
void *addr;
struct vas_setup_attr txattr;
fd = open("/dev/crypto/nx-gzip", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open nx-gzip failed\n");
return -1;
}
memset(&txattr, 0, sizeof(txattr));
txattr.version = 1;
txattr.vas_id = -1
rc = ioctl(fd, VAS_TX_WIN_OPEN,
(unsigned long)&txattr);
if (rc < 0) {
fprintf(stderr, "ioctl() n %d, error %d\n",
rc, errno);
return rc;
}
addr = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0ULL);
if (addr == MAP_FAILED) {
fprintf(stderr, "mmap() failed, errno %d\n",
errno);
return -errno;
}
do {
//Format CRB request with compression or
//uncompression
// Refer tests for vas_copy/vas_paste
vas_copy((&crb, 0, 1);
vas_paste(addr, 0, 1);
// Poll on csb.flags with timeout
// csb address is listed in CRB
} while (true)
close(fd) or window can be closed upon process exit
}
예제의 type name, 괄호와 세미콜론 표기는 원문 그대로 보존했습니다. 실제 build에는 현재 UAPI header와 library helper prototype에 맞춘 수정이 필요할 수 있습니다.
요약과 해설
vas-api.rst:1-305Application은 nx-gzip device를 열고 `VAS_TX_WIN_OPEN`으로 send window를 만든 뒤 `mmap()`으로 얻은 paste address에 CRB를 COPY/PASTE합니다. Translation fault는 valid CSB이면 retry 정보로, invalid CSB이면 SIGSEGV로 전달됩니다.