Virtual Accelerator Switchboard (VAS) userspace API

Introduction

Power9 processor introduced Virtual Accelerator Switchboard (VAS) whichallows both userspace and kernel communicate to co-processor(hardware accelerator) referred to as the Nest Accelerator (NX). The NXunit comprises of one or more hardware engines or co-processor typessuch as 842 compression, GZIP compression and encryption. On power9,userspace applications will have access to only GZIP Compression enginewhich supports ZLIB and GZIP compression algorithms in the hardware.

To communicate with NX, kernel has to establish a channel or window andthen requests can be submitted directly without kernel involvement.Requests to the GZIP engine must be formatted as a co-processor RequestBlock (CRB) and these CRBs must be submitted to the NX using COPY/PASTEinstructions to paste the CRB to hardware address that is associated withthe engine’s request queue.

The GZIP engine provides two priority levels of requests: Normal andHigh. Only Normal requests are supported from userspace right now.

This document explains userspace API that is used to interact withkernel to setup channel / window which can be used to send compressionrequests 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 filedescriptor (fd). Then should issue VAS_TX_WIN_OPEN ioctl with this fd toestablish connection to the engine. It means send window is opened on GZIPengine for this process. Once a connection is established, the applicationshould use the mmap() system call to map the hardware address of engine’srequest queue into the application’s virtual address space.

The application can then submit one or more requests to the engine byusing copy/paste instructions and pasting the CRBs to the virtual address(aka paste_address) returned by mmap(). User space can close theestablished connection or send window by closing the file descriptior(close(fd)) or upon the process exit.

Note that applications can send several requests with the same window orcan establish multiple windows, but one window for each file descriptor.

Following sections provide additional details and references about theindividual steps.

NX-GZIP Device Node

There is one /dev/crypto/nx-gzip node in the system and it providesaccess 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 virtualaddress space (i.e. get a paste_address for the co-processorengine).
  • 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 anddo not go through this device. Refer COPY/PASTE document for moredetails.

Although a system may have several instances of the NX co-processorengines (typically, one per P9 chip) there is just one/dev/crypto/nx-gzip device node in the system. When the nx-gzip devicenode is opened, Kernel opens send window on a suitable instance of NXaccelerator. It finds CPU on which the user process is executing anddetermine the NX instance for the corresponding chip on which this CPUbelongs.

Applications may chose a specific instance of the NX co-processor usingthe vas_id field in the VAS_TX_WIN_OPEN ioctl as detailed below.

A userspace library libnxz is available here but still in development:

Applications that use inflate / deflate calls can link with libnxzinstead 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 specialprivileges are needed to open the device. Each window corresponds to onefile 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 establisha 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 attemptto assign an optimal instance of NX for the process. Toselect the specific VAS instance, refer“Discovery of available VAS engines” section below.

flags, reserved1 and reserved2[6] fields are for future extensionand must be set to 0.

The attributes attr for the VAS_TX_WIN_OPEN ioctl are defined asfollows:

#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, itreturns -1 and sets the errno variable to indicate the error.

Error conditions:

EINVALfd does not refer to a valid VAS device.
EINVALInvalid vas ID
EINVALversion is not set with proper value
EEXISTWindow is already opened for the given fd
ENOMEMMemory is not available to allocate window
ENOSPCSystem has too many active windows (connections)opened
EINVALreserved fields are not set to 0.

See the ioctl(2) man page for more details, error codes andrestrictions.

mmap() NX-GZIP device

The mmap() system call for a NX-GZIP device fd returns a paste_addressthat 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) manpage, can also fail with one of the following error codes:

EINVALfd is not associated with an open window(i.e mmap() does not follow a successful callto the VAS_TX_WIN_OPEN ioctl).
EINVALoffset field is not 0ULL.

Discovery of available VAS engines

Each available VAS instance in the system will have a device tree nodelike /proc/device-tree/vas@* or /proc/device-tree/xscom@*/vas@*.Determine the chip or VAS instance and use the corresponding ibm,vas-idproperty 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 theco-processor Request Block (CRBs). Refer NX-GZIP user’s manual for the formatof CRB and use NX from userspace such as sending requests and checkingrequest status.

NX Fault handling

Applications send requests to NX and wait for the status by polling onco-processor Status Block (CSB) flags. NX updates status in CSB after eachrequest is processed. Refer NX-GZIP user’s manual for the format of CSB andstatus flags.

In case if NX encounters translation error (called NX page fault) on CSBaddress or any request buffer, raises an interrupt on the CPU to handle thefault. Page fault can happen if an application passes invalid addresses orrequest buffers are not in memory. The operating system handles the fault byupdating 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 accessthe page that has a fault address so that this page will be in memory. Thenthe application can resend this request to NX.

If the OS can not update CSB due to invalid CSB address, sends SEGV signalto the process who opened the send window on which the original request wasissued. 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 adress;

In the case of multi-thread applications, NX send windows can be sharedacross all threads. For example, a child thread can open a send window,but other threads can send requests to NX using this window. Theserequests will be successful even in the case of OS handling faults as longas 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 ifthe thread is exited without closing the window and the request is issuedusing this window. the signal will be issued to the thread group leader(tgid). It is up to the application whether to ignore or handle thesesignals.

NX-GZIP User’s Manual:https://github.com/libnxz/power-gzip/blob/master/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}

Referhttps://github.com/abalib/power-gzip for tests or moreuse cases.