brintos

brintos / linux-shallow public Read only

0
0
Text · 18.3 KiB · d1e14d5 Raw
446 lines · plain
1=======================================2Oracle Data Analytics Accelerator (DAX)3=======================================4 5DAX is a coprocessor which resides on the SPARC M7 (DAX1) and M86(DAX2) processor chips, and has direct access to the CPU's L3 caches7as well as physical memory. It can perform several operations on data8streams with various input and output formats.  A driver provides a9transport mechanism and has limited knowledge of the various opcodes10and data formats. A user space library provides high level services11and translates these into low level commands which are then passed12into the driver and subsequently the Hypervisor and the coprocessor.13The library is the recommended way for applications to use the14coprocessor, and the driver interface is not intended for general use.15This document describes the general flow of the driver, its16structures, and its programmatic interface. It also provides example17code sufficient to write user or kernel applications that use DAX18functionality.19 20The user library is open source and available at:21 22    https://oss.oracle.com/git/gitweb.cgi?p=libdax.git23 24The Hypervisor interface to the coprocessor is described in detail in25the accompanying document, dax-hv-api.txt, which is a plain text26excerpt of the (Oracle internal) "UltraSPARC Virtual Machine27Specification" version 3.0.20+15, dated 2017-09-25.28 29 30High Level Overview31===================32 33A coprocessor request is described by a Command Control Block34(CCB). The CCB contains an opcode and various parameters. The opcode35specifies what operation is to be done, and the parameters specify36options, flags, sizes, and addresses.  The CCB (or an array of CCBs)37is passed to the Hypervisor, which handles queueing and scheduling of38requests to the available coprocessor execution units. A status code39returned indicates if the request was submitted successfully or if40there was an error.  One of the addresses given in each CCB is a41pointer to a "completion area", which is a 128 byte memory block that42is written by the coprocessor to provide execution status. No43interrupt is generated upon completion; the completion area must be44polled by software to find out when a transaction has finished, but45the M7 and later processors provide a mechanism to pause the virtual46processor until the completion status has been updated by the47coprocessor. This is done using the monitored load and mwait48instructions, which are described in more detail later.  The DAX49coprocessor was designed so that after a request is submitted, the50kernel is no longer involved in the processing of it.  The polling is51done at the user level, which results in almost zero latency between52completion of a request and resumption of execution of the requesting53thread.54 55 56Addressing Memory57=================58 59The kernel does not have access to physical memory in the Sun4v60architecture, as there is an additional level of memory virtualization61present. This intermediate level is called "real" memory, and the62kernel treats this as if it were physical.  The Hypervisor handles the63translations between real memory and physical so that each logical64domain (LDOM) can have a partition of physical memory that is isolated65from that of other LDOMs.  When the kernel sets up a virtual mapping,66it specifies a virtual address and the real address to which it should67be mapped.68 69The DAX coprocessor can only operate on physical memory, so before a70request can be fed to the coprocessor, all the addresses in a CCB must71be converted into physical addresses. The kernel cannot do this since72it has no visibility into physical addresses. So a CCB may contain73either the virtual or real addresses of the buffers or a combination74of them. An "address type" field is available for each address that75may be given in the CCB. In all cases, the Hypervisor will translate76all the addresses to physical before dispatching to hardware. Address77translations are performed using the context of the process initiating78the request.79 80 81The Driver API82==============83 84An application makes requests to the driver via the write() system85call, and gets results (if any) via read(). The completion areas are86made accessible via mmap(), and are read-only for the application.87 88The request may either be an immediate command or an array of CCBs to89be submitted to the hardware.90 91Each open instance of the device is exclusive to the thread that92opened it, and must be used by that thread for all subsequent93operations. The driver open function creates a new context for the94thread and initializes it for use.  This context contains pointers and95values used internally by the driver to keep track of submitted96requests. The completion area buffer is also allocated, and this is97large enough to contain the completion areas for many concurrent98requests.  When the device is closed, any outstanding transactions are99flushed and the context is cleaned up.100 101On a DAX1 system (M7), the device will be called "oradax1", while on a102DAX2 system (M8) it will be "oradax2". If an application requires one103or the other, it should simply attempt to open the appropriate104device. Only one of the devices will exist on any given system, so the105name can be used to determine what the platform supports.106 107The immediate commands are CCB_DEQUEUE, CCB_KILL, and CCB_INFO. For108all of these, success is indicated by a return value from write()109equal to the number of bytes given in the call. Otherwise -1 is110returned and errno is set.111 112CCB_DEQUEUE113-----------114 115Tells the driver to clean up resources associated with past116requests. Since no interrupt is generated upon the completion of a117request, the driver must be told when it may reclaim resources.  No118further status information is returned, so the user should not119subsequently call read().120 121CCB_KILL122--------123 124Kills a CCB during execution. The CCB is guaranteed to not continue125executing once this call returns successfully. On success, read() must126be called to retrieve the result of the action.127 128CCB_INFO129--------130 131Retrieves information about a currently executing CCB. Note that some132Hypervisors might return 'notfound' when the CCB is in 'inprogress'133state. To ensure a CCB in the 'notfound' state will never be executed,134CCB_KILL must be invoked on that CCB. Upon success, read() must be135called to retrieve the details of the action.136 137Submission of an array of CCBs for execution138---------------------------------------------139 140A write() whose length is a multiple of the CCB size is treated as a141submit operation. The file offset is treated as the index of the142completion area to use, and may be set via lseek() or using the143pwrite() system call. If -1 is returned then errno is set to indicate144the error. Otherwise, the return value is the length of the array that145was actually accepted by the coprocessor. If the accepted length is146equal to the requested length, then the submission was completely147successful and there is no further status needed; hence, the user148should not subsequently call read(). Partial acceptance of the CCB149array is indicated by a return value less than the requested length,150and read() must be called to retrieve further status information.  The151status will reflect the error caused by the first CCB that was not152accepted, and status_data will provide additional data in some cases.153 154MMAP155----156 157The mmap() function provides access to the completion area allocated158in the driver.  Note that the completion area is not writeable by the159user process, and the mmap call must not specify PROT_WRITE.160 161 162Completion of a Request163=======================164 165The first byte in each completion area is the command status which is166updated by the coprocessor hardware. Software may take advantage of167new M7/M8 processor capabilities to efficiently poll this status byte.168First, a "monitored load" is achieved via a Load from Alternate Space169(ldxa, lduba, etc.) with ASI 0x84 (ASI_MONITOR_PRIMARY).  Second, a170"monitored wait" is achieved via the mwait instruction (a write to171%asr28). This instruction is like pause in that it suspends execution172of the virtual processor for the given number of nanoseconds, but in173addition will terminate early when one of several events occur. If the174block of data containing the monitored location is modified, then the175mwait terminates. This causes software to resume execution immediately176(without a context switch or kernel to user transition) after a177transaction completes. Thus the latency between transaction completion178and resumption of execution may be just a few nanoseconds.179 180 181Application Life Cycle of a DAX Submission182==========================================183 184 - open dax device185 - call mmap() to get the completion area address186 - allocate a CCB and fill in the opcode, flags, parameters, addresses, etc.187 - submit CCB via write() or pwrite()188 - go into a loop executing monitored load + monitored wait and189   terminate when the command status indicates the request is complete190   (CCB_KILL or CCB_INFO may be used any time as necessary)191 - perform a CCB_DEQUEUE192 - call munmap() for completion area193 - close the dax device194 195 196Memory Constraints197==================198 199The DAX hardware operates only on physical addresses. Therefore, it is200not aware of virtual memory mappings and the discontiguities that may201exist in the physical memory that a virtual buffer maps to. There is202no I/O TLB or any scatter/gather mechanism. All buffers, whether input203or output, must reside in a physically contiguous region of memory.204 205The Hypervisor translates all addresses within a CCB to physical206before handing off the CCB to DAX. The Hypervisor determines the207virtual page size for each virtual address given, and uses this to208program a size limit for each address. This prevents the coprocessor209from reading or writing beyond the bound of the virtual page, even210though it is accessing physical memory directly. A simpler way of211saying this is that a DAX operation will never "cross" a virtual page212boundary. If an 8k virtual page is used, then the data is strictly213limited to 8k. If a user's buffer is larger than 8k, then a larger214page size must be used, or the transaction size will be truncated to2158k.216 217Huge pages. A user may allocate huge pages using standard interfaces.218Memory buffers residing on huge pages may be used to achieve much219larger DAX transaction sizes, but the rules must still be followed,220and no transaction will cross a page boundary, even a huge page.  A221major caveat is that Linux on Sparc presents 8Mb as one of the huge222page sizes. Sparc does not actually provide a 8Mb hardware page size,223and this size is synthesized by pasting together two 4Mb pages. The224reasons for this are historical, and it creates an issue because only225half of this 8Mb page can actually be used for any given buffer in a226DAX request, and it must be either the first half or the second half;227it cannot be a 4Mb chunk in the middle, since that crosses a228(hardware) page boundary. Note that this entire issue may be hidden by229higher level libraries.230 231 232CCB Structure233-------------234A CCB is an array of 8 64-bit words. Several of these words provide235command opcodes, parameters, flags, etc., and the rest are addresses236for the completion area, output buffer, and various inputs::237 238   struct ccb {239       u64   control;240       u64   completion;241       u64   input0;242       u64   access;243       u64   input1;244       u64   op_data;245       u64   output;246       u64   table;247   };248 249See libdax/common/sys/dax1/dax1_ccb.h for a detailed description of250each of these fields, and see dax-hv-api.txt for a complete description251of the Hypervisor API available to the guest OS (ie, Linux kernel).252 253The first word (control) is examined by the driver for the following:254 - CCB version, which must be consistent with hardware version255 - Opcode, which must be one of the documented allowable commands256 - Address types, which must be set to "virtual" for all the addresses257   given by the user, thereby ensuring that the application can258   only access memory that it owns259 260 261Example Code262============263 264The DAX is accessible to both user and kernel code.  The kernel code265can make hypercalls directly while the user code must use wrappers266provided by the driver. The setup of the CCB is nearly identical for267both; the only difference is in preparation of the completion area. An268example of user code is given now, with kernel code afterwards.269 270In order to program using the driver API, the file271arch/sparc/include/uapi/asm/oradax.h must be included.272 273First, the proper device must be opened. For M7 it will be274/dev/oradax1 and for M8 it will be /dev/oradax2. The simplest275procedure is to attempt to open both, as only one will succeed::276 277	fd = open("/dev/oradax1", O_RDWR);278	if (fd < 0)279		fd = open("/dev/oradax2", O_RDWR);280	if (fd < 0)281	       /* No DAX found */282 283Next, the completion area must be mapped::284 285      completion_area = mmap(NULL, DAX_MMAP_LEN, PROT_READ, MAP_SHARED, fd, 0);286 287All input and output buffers must be fully contained in one hardware288page, since as explained above, the DAX is strictly constrained by289virtual page boundaries.  In addition, the output buffer must be29064-byte aligned and its size must be a multiple of 64 bytes because291the coprocessor writes in units of cache lines.292 293This example demonstrates the DAX Scan command, which takes as input a294vector and a match value, and produces a bitmap as the output. For295each input element that matches the value, the corresponding bit is296set in the output.297 298In this example, the input vector consists of a series of single bits,299and the match value is 0. So each 0 bit in the input will produce a 1300in the output, and vice versa, which produces an output bitmap which301is the input bitmap inverted.302 303For details of all the parameters and bits used in this CCB, please304refer to section 36.2.1.3 of the DAX Hypervisor API document, which305describes the Scan command in detail::306 307	ccb->control =       /* Table 36.1, CCB Header Format */308		  (2L << 48)     /* command = Scan Value */309		| (3L << 40)     /* output address type = primary virtual */310		| (3L << 34)     /* primary input address type = primary virtual */311		             /* Section 36.2.1, Query CCB Command Formats */312		| (1 << 28)     /* 36.2.1.1.1 primary input format = fixed width bit packed */313		| (0 << 23)     /* 36.2.1.1.2 primary input element size = 0 (1 bit) */314		| (8 << 10)     /* 36.2.1.1.6 output format = bit vector */315		| (0 <<  5)	/* 36.2.1.3 First scan criteria size = 0 (1 byte) */316		| (31 << 0);	/* 36.2.1.3 Disable second scan criteria */317 318	ccb->completion = 0;    /* Completion area address, to be filled in by driver */319 320	ccb->input0 = (unsigned long) input; /* primary input address */321 322	ccb->access =       /* Section 36.2.1.2, Data Access Control */323		  (2 << 24)    /* Primary input length format = bits */324		| (nbits - 1); /* number of bits in primary input stream, minus 1 */325 326	ccb->input1 = 0;       /* secondary input address, unused */327 328	ccb->op_data = 0;      /* scan criteria (value to be matched) */329 330	ccb->output = (unsigned long) output;	/* output address */331 332	ccb->table = 0;	       /* table address, unused */333 334The CCB submission is a write() or pwrite() system call to the335driver. If the call fails, then a read() must be used to retrieve the336status::337 338	if (pwrite(fd, ccb, 64, 0) != 64) {339		struct ccb_exec_result status;340		read(fd, &status, sizeof(status));341		/* bail out */342	}343 344After a successful submission of the CCB, the completion area may be345polled to determine when the DAX is finished. Detailed information on346the contents of the completion area can be found in section 36.2.2 of347the DAX HV API document::348 349	while (1) {350		/* Monitored Load */351		__asm__ __volatile__("lduba [%1] 0x84, %0\n"352				     : "=r" (status)353				     : "r"  (completion_area));354 355		if (status)	     /* 0 indicates command in progress */356			break;357 358		/* MWAIT */359		__asm__ __volatile__("wr %%g0, 1000, %%asr28\n" ::);    /* 1000 ns */360	}361 362A completion area status of 1 indicates successful completion of the363CCB and validity of the output bitmap, which may be used immediately.364All other non-zero values indicate error conditions which are365described in section 36.2.2::366 367	if (completion_area[0] != 1) {	/* section 36.2.2, 1 = command ran and succeeded */368		/* completion_area[0] contains the completion status */369		/* completion_area[1] contains an error code, see 36.2.2 */370	}371 372After the completion area has been processed, the driver must be373notified that it can release any resources associated with the374request. This is done via the dequeue operation::375 376	struct dax_command cmd;377	cmd.command = CCB_DEQUEUE;378	if (write(fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {379		/* bail out */380	}381 382Finally, normal program cleanup should be done, i.e., unmapping383completion area, closing the dax device, freeing memory etc.384 385Kernel example386--------------387 388The only difference in using the DAX in kernel code is the treatment389of the completion area. Unlike user applications which mmap the390completion area allocated by the driver, kernel code must allocate its391own memory to use for the completion area, and this address and its392type must be given in the CCB::393 394	ccb->control |=      /* Table 36.1, CCB Header Format */395	        (3L << 32);     /* completion area address type = primary virtual */396 397	ccb->completion = (unsigned long) completion_area;   /* Completion area address */398 399The dax submit hypercall is made directly. The flags used in the400ccb_submit call are documented in the DAX HV API in section 36.3.1/401 402::403 404  #include <asm/hypervisor.h>405 406	hv_rv = sun4v_ccb_submit((unsigned long)ccb, 64,407				 HV_CCB_QUERY_CMD |408				 HV_CCB_ARG0_PRIVILEGED | HV_CCB_ARG0_TYPE_PRIMARY |409				 HV_CCB_VA_PRIVILEGED,410				 0, &bytes_accepted, &status_data);411 412	if (hv_rv != HV_EOK) {413		/* hv_rv is an error code, status_data contains */414		/* potential additional status, see 36.3.1.1 */415	}416 417After the submission, the completion area polling code is identical to418that in user land::419 420	while (1) {421		/* Monitored Load */422		__asm__ __volatile__("lduba [%1] 0x84, %0\n"423				     : "=r" (status)424				     : "r"  (completion_area));425 426		if (status)	     /* 0 indicates command in progress */427			break;428 429		/* MWAIT */430		__asm__ __volatile__("wr %%g0, 1000, %%asr28\n" ::);    /* 1000 ns */431	}432 433	if (completion_area[0] != 1) {	/* section 36.2.2, 1 = command ran and succeeded */434		/* completion_area[0] contains the completion status */435		/* completion_area[1] contains an error code, see 36.2.2 */436	}437 438The output bitmap is ready for consumption immediately after the439completion status indicates success.440 441Excer[t from UltraSPARC Virtual Machine Specification442=====================================================443 444 .. include:: dax-hv-api.txt445    :literal:446