oracle-dax.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. =======================================
  2. Oracle Data Analytics Accelerator (DAX)
  3. =======================================
  4. DAX is a coprocessor which resides on the SPARC M7 (DAX1) and M8
  5. (DAX2) processor chips, and has direct access to the CPU's L3 caches
  6. as well as physical memory. It can perform several operations on data
  7. streams with various input and output formats. A driver provides a
  8. transport mechanism and has limited knowledge of the various opcodes
  9. and data formats. A user space library provides high level services
  10. and translates these into low level commands which are then passed
  11. into the driver and subsequently the Hypervisor and the coprocessor.
  12. The library is the recommended way for applications to use the
  13. coprocessor, and the driver interface is not intended for general use.
  14. This document describes the general flow of the driver, its
  15. structures, and its programmatic interface. It also provides example
  16. code sufficient to write user or kernel applications that use DAX
  17. functionality.
  18. The user library is open source and available at:
  19. https://oss.oracle.com/git/gitweb.cgi?p=libdax.git
  20. The Hypervisor interface to the coprocessor is described in detail in
  21. the accompanying document, dax-hv-api.txt, which is a plain text
  22. excerpt of the (Oracle internal) "UltraSPARC Virtual Machine
  23. Specification" version 3.0.20+15, dated 2017-09-25.
  24. High Level Overview
  25. ===================
  26. A coprocessor request is described by a Command Control Block
  27. (CCB). The CCB contains an opcode and various parameters. The opcode
  28. specifies what operation is to be done, and the parameters specify
  29. options, flags, sizes, and addresses. The CCB (or an array of CCBs)
  30. is passed to the Hypervisor, which handles queueing and scheduling of
  31. requests to the available coprocessor execution units. A status code
  32. returned indicates if the request was submitted successfully or if
  33. there was an error. One of the addresses given in each CCB is a
  34. pointer to a "completion area", which is a 128 byte memory block that
  35. is written by the coprocessor to provide execution status. No
  36. interrupt is generated upon completion; the completion area must be
  37. polled by software to find out when a transaction has finished, but
  38. the M7 and later processors provide a mechanism to pause the virtual
  39. processor until the completion status has been updated by the
  40. coprocessor. This is done using the monitored load and mwait
  41. instructions, which are described in more detail later. The DAX
  42. coprocessor was designed so that after a request is submitted, the
  43. kernel is no longer involved in the processing of it. The polling is
  44. done at the user level, which results in almost zero latency between
  45. completion of a request and resumption of execution of the requesting
  46. thread.
  47. Addressing Memory
  48. =================
  49. The kernel does not have access to physical memory in the Sun4v
  50. architecture, as there is an additional level of memory virtualization
  51. present. This intermediate level is called "real" memory, and the
  52. kernel treats this as if it were physical. The Hypervisor handles the
  53. translations between real memory and physical so that each logical
  54. domain (LDOM) can have a partition of physical memory that is isolated
  55. from that of other LDOMs. When the kernel sets up a virtual mapping,
  56. it specifies a virtual address and the real address to which it should
  57. be mapped.
  58. The DAX coprocessor can only operate on physical memory, so before a
  59. request can be fed to the coprocessor, all the addresses in a CCB must
  60. be converted into physical addresses. The kernel cannot do this since
  61. it has no visibility into physical addresses. So a CCB may contain
  62. either the virtual or real addresses of the buffers or a combination
  63. of them. An "address type" field is available for each address that
  64. may be given in the CCB. In all cases, the Hypervisor will translate
  65. all the addresses to physical before dispatching to hardware. Address
  66. translations are performed using the context of the process initiating
  67. the request.
  68. The Driver API
  69. ==============
  70. An application makes requests to the driver via the write() system
  71. call, and gets results (if any) via read(). The completion areas are
  72. made accessible via mmap(), and are read-only for the application.
  73. The request may either be an immediate command or an array of CCBs to
  74. be submitted to the hardware.
  75. Each open instance of the device is exclusive to the thread that
  76. opened it, and must be used by that thread for all subsequent
  77. operations. The driver open function creates a new context for the
  78. thread and initializes it for use. This context contains pointers and
  79. values used internally by the driver to keep track of submitted
  80. requests. The completion area buffer is also allocated, and this is
  81. large enough to contain the completion areas for many concurrent
  82. requests. When the device is closed, any outstanding transactions are
  83. flushed and the context is cleaned up.
  84. On a DAX1 system (M7), the device will be called "oradax1", while on a
  85. DAX2 system (M8) it will be "oradax2". If an application requires one
  86. or the other, it should simply attempt to open the appropriate
  87. device. Only one of the devices will exist on any given system, so the
  88. name can be used to determine what the platform supports.
  89. The immediate commands are CCB_DEQUEUE, CCB_KILL, and CCB_INFO. For
  90. all of these, success is indicated by a return value from write()
  91. equal to the number of bytes given in the call. Otherwise -1 is
  92. returned and errno is set.
  93. CCB_DEQUEUE
  94. -----------
  95. Tells the driver to clean up resources associated with past
  96. requests. Since no interrupt is generated upon the completion of a
  97. request, the driver must be told when it may reclaim resources. No
  98. further status information is returned, so the user should not
  99. subsequently call read().
  100. CCB_KILL
  101. --------
  102. Kills a CCB during execution. The CCB is guaranteed to not continue
  103. executing once this call returns successfully. On success, read() must
  104. be called to retrieve the result of the action.
  105. CCB_INFO
  106. --------
  107. Retrieves information about a currently executing CCB. Note that some
  108. Hypervisors might return 'notfound' when the CCB is in 'inprogress'
  109. state. To ensure a CCB in the 'notfound' state will never be executed,
  110. CCB_KILL must be invoked on that CCB. Upon success, read() must be
  111. called to retrieve the details of the action.
  112. Submission of an array of CCBs for execution
  113. ---------------------------------------------
  114. A write() whose length is a multiple of the CCB size is treated as a
  115. submit operation. The file offset is treated as the index of the
  116. completion area to use, and may be set via lseek() or using the
  117. pwrite() system call. If -1 is returned then errno is set to indicate
  118. the error. Otherwise, the return value is the length of the array that
  119. was actually accepted by the coprocessor. If the accepted length is
  120. equal to the requested length, then the submission was completely
  121. successful and there is no further status needed; hence, the user
  122. should not subsequently call read(). Partial acceptance of the CCB
  123. array is indicated by a return value less than the requested length,
  124. and read() must be called to retrieve further status information. The
  125. status will reflect the error caused by the first CCB that was not
  126. accepted, and status_data will provide additional data in some cases.
  127. MMAP
  128. ----
  129. The mmap() function provides access to the completion area allocated
  130. in the driver. Note that the completion area is not writeable by the
  131. user process, and the mmap call must not specify PROT_WRITE.
  132. Completion of a Request
  133. =======================
  134. The first byte in each completion area is the command status which is
  135. updated by the coprocessor hardware. Software may take advantage of
  136. new M7/M8 processor capabilities to efficiently poll this status byte.
  137. First, a "monitored load" is achieved via a Load from Alternate Space
  138. (ldxa, lduba, etc.) with ASI 0x84 (ASI_MONITOR_PRIMARY). Second, a
  139. "monitored wait" is achieved via the mwait instruction (a write to
  140. %asr28). This instruction is like pause in that it suspends execution
  141. of the virtual processor for the given number of nanoseconds, but in
  142. addition will terminate early when one of several events occur. If the
  143. block of data containing the monitored location is modified, then the
  144. mwait terminates. This causes software to resume execution immediately
  145. (without a context switch or kernel to user transition) after a
  146. transaction completes. Thus the latency between transaction completion
  147. and resumption of execution may be just a few nanoseconds.
  148. Application Life Cycle of a DAX Submission
  149. ==========================================
  150. - open dax device
  151. - call mmap() to get the completion area address
  152. - allocate a CCB and fill in the opcode, flags, parameters, addresses, etc.
  153. - submit CCB via write() or pwrite()
  154. - go into a loop executing monitored load + monitored wait and
  155. terminate when the command status indicates the request is complete
  156. (CCB_KILL or CCB_INFO may be used any time as necessary)
  157. - perform a CCB_DEQUEUE
  158. - call munmap() for completion area
  159. - close the dax device
  160. Memory Constraints
  161. ==================
  162. The DAX hardware operates only on physical addresses. Therefore, it is
  163. not aware of virtual memory mappings and the discontiguities that may
  164. exist in the physical memory that a virtual buffer maps to. There is
  165. no I/O TLB or any scatter/gather mechanism. All buffers, whether input
  166. or output, must reside in a physically contiguous region of memory.
  167. The Hypervisor translates all addresses within a CCB to physical
  168. before handing off the CCB to DAX. The Hypervisor determines the
  169. virtual page size for each virtual address given, and uses this to
  170. program a size limit for each address. This prevents the coprocessor
  171. from reading or writing beyond the bound of the virtual page, even
  172. though it is accessing physical memory directly. A simpler way of
  173. saying this is that a DAX operation will never "cross" a virtual page
  174. boundary. If an 8k virtual page is used, then the data is strictly
  175. limited to 8k. If a user's buffer is larger than 8k, then a larger
  176. page size must be used, or the transaction size will be truncated to
  177. 8k.
  178. Huge pages. A user may allocate huge pages using standard interfaces.
  179. Memory buffers residing on huge pages may be used to achieve much
  180. larger DAX transaction sizes, but the rules must still be followed,
  181. and no transaction will cross a page boundary, even a huge page. A
  182. major caveat is that Linux on Sparc presents 8Mb as one of the huge
  183. page sizes. Sparc does not actually provide a 8Mb hardware page size,
  184. and this size is synthesized by pasting together two 4Mb pages. The
  185. reasons for this are historical, and it creates an issue because only
  186. half of this 8Mb page can actually be used for any given buffer in a
  187. DAX request, and it must be either the first half or the second half;
  188. it cannot be a 4Mb chunk in the middle, since that crosses a
  189. (hardware) page boundary. Note that this entire issue may be hidden by
  190. higher level libraries.
  191. CCB Structure
  192. -------------
  193. A CCB is an array of 8 64-bit words. Several of these words provide
  194. command opcodes, parameters, flags, etc., and the rest are addresses
  195. for the completion area, output buffer, and various inputs::
  196. struct ccb {
  197. u64 control;
  198. u64 completion;
  199. u64 input0;
  200. u64 access;
  201. u64 input1;
  202. u64 op_data;
  203. u64 output;
  204. u64 table;
  205. };
  206. See libdax/common/sys/dax1/dax1_ccb.h for a detailed description of
  207. each of these fields, and see dax-hv-api.txt for a complete description
  208. of the Hypervisor API available to the guest OS (ie, Linux kernel).
  209. The first word (control) is examined by the driver for the following:
  210. - CCB version, which must be consistent with hardware version
  211. - Opcode, which must be one of the documented allowable commands
  212. - Address types, which must be set to "virtual" for all the addresses
  213. given by the user, thereby ensuring that the application can
  214. only access memory that it owns
  215. Example Code
  216. ============
  217. The DAX is accessible to both user and kernel code. The kernel code
  218. can make hypercalls directly while the user code must use wrappers
  219. provided by the driver. The setup of the CCB is nearly identical for
  220. both; the only difference is in preparation of the completion area. An
  221. example of user code is given now, with kernel code afterwards.
  222. In order to program using the driver API, the file
  223. arch/sparc/include/uapi/asm/oradax.h must be included.
  224. First, the proper device must be opened. For M7 it will be
  225. /dev/oradax1 and for M8 it will be /dev/oradax2. The simplest
  226. procedure is to attempt to open both, as only one will succeed::
  227. fd = open("/dev/oradax1", O_RDWR);
  228. if (fd < 0)
  229. fd = open("/dev/oradax2", O_RDWR);
  230. if (fd < 0)
  231. /* No DAX found */
  232. Next, the completion area must be mapped::
  233. completion_area = mmap(NULL, DAX_MMAP_LEN, PROT_READ, MAP_SHARED, fd, 0);
  234. All input and output buffers must be fully contained in one hardware
  235. page, since as explained above, the DAX is strictly constrained by
  236. virtual page boundaries. In addition, the output buffer must be
  237. 64-byte aligned and its size must be a multiple of 64 bytes because
  238. the coprocessor writes in units of cache lines.
  239. This example demonstrates the DAX Scan command, which takes as input a
  240. vector and a match value, and produces a bitmap as the output. For
  241. each input element that matches the value, the corresponding bit is
  242. set in the output.
  243. In this example, the input vector consists of a series of single bits,
  244. and the match value is 0. So each 0 bit in the input will produce a 1
  245. in the output, and vice versa, which produces an output bitmap which
  246. is the input bitmap inverted.
  247. For details of all the parameters and bits used in this CCB, please
  248. refer to section 36.2.1.3 of the DAX Hypervisor API document, which
  249. describes the Scan command in detail::
  250. ccb->control = /* Table 36.1, CCB Header Format */
  251. (2L << 48) /* command = Scan Value */
  252. | (3L << 40) /* output address type = primary virtual */
  253. | (3L << 34) /* primary input address type = primary virtual */
  254. /* Section 36.2.1, Query CCB Command Formats */
  255. | (1 << 28) /* 36.2.1.1.1 primary input format = fixed width bit packed */
  256. | (0 << 23) /* 36.2.1.1.2 primary input element size = 0 (1 bit) */
  257. | (8 << 10) /* 36.2.1.1.6 output format = bit vector */
  258. | (0 << 5) /* 36.2.1.3 First scan criteria size = 0 (1 byte) */
  259. | (31 << 0); /* 36.2.1.3 Disable second scan criteria */
  260. ccb->completion = 0; /* Completion area address, to be filled in by driver */
  261. ccb->input0 = (unsigned long) input; /* primary input address */
  262. ccb->access = /* Section 36.2.1.2, Data Access Control */
  263. (2 << 24) /* Primary input length format = bits */
  264. | (nbits - 1); /* number of bits in primary input stream, minus 1 */
  265. ccb->input1 = 0; /* secondary input address, unused */
  266. ccb->op_data = 0; /* scan criteria (value to be matched) */
  267. ccb->output = (unsigned long) output; /* output address */
  268. ccb->table = 0; /* table address, unused */
  269. The CCB submission is a write() or pwrite() system call to the
  270. driver. If the call fails, then a read() must be used to retrieve the
  271. status::
  272. if (pwrite(fd, ccb, 64, 0) != 64) {
  273. struct ccb_exec_result status;
  274. read(fd, &status, sizeof(status));
  275. /* bail out */
  276. }
  277. After a successful submission of the CCB, the completion area may be
  278. polled to determine when the DAX is finished. Detailed information on
  279. the contents of the completion area can be found in section 36.2.2 of
  280. the DAX HV API document::
  281. while (1) {
  282. /* Monitored Load */
  283. __asm__ __volatile__("lduba [%1] 0x84, %0\n"
  284. : "=r" (status)
  285. : "r" (completion_area));
  286. if (status) /* 0 indicates command in progress */
  287. break;
  288. /* MWAIT */
  289. __asm__ __volatile__("wr %%g0, 1000, %%asr28\n" ::); /* 1000 ns */
  290. }
  291. A completion area status of 1 indicates successful completion of the
  292. CCB and validity of the output bitmap, which may be used immediately.
  293. All other non-zero values indicate error conditions which are
  294. described in section 36.2.2::
  295. if (completion_area[0] != 1) { /* section 36.2.2, 1 = command ran and succeeded */
  296. /* completion_area[0] contains the completion status */
  297. /* completion_area[1] contains an error code, see 36.2.2 */
  298. }
  299. After the completion area has been processed, the driver must be
  300. notified that it can release any resources associated with the
  301. request. This is done via the dequeue operation::
  302. struct dax_command cmd;
  303. cmd.command = CCB_DEQUEUE;
  304. if (write(fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  305. /* bail out */
  306. }
  307. Finally, normal program cleanup should be done, i.e., unmapping
  308. completion area, closing the dax device, freeing memory etc.
  309. Kernel example
  310. --------------
  311. The only difference in using the DAX in kernel code is the treatment
  312. of the completion area. Unlike user applications which mmap the
  313. completion area allocated by the driver, kernel code must allocate its
  314. own memory to use for the completion area, and this address and its
  315. type must be given in the CCB::
  316. ccb->control |= /* Table 36.1, CCB Header Format */
  317. (3L << 32); /* completion area address type = primary virtual */
  318. ccb->completion = (unsigned long) completion_area; /* Completion area address */
  319. The dax submit hypercall is made directly. The flags used in the
  320. ccb_submit call are documented in the DAX HV API in section 36.3.1/
  321. ::
  322. #include <asm/hypervisor.h>
  323. hv_rv = sun4v_ccb_submit((unsigned long)ccb, 64,
  324. HV_CCB_QUERY_CMD |
  325. HV_CCB_ARG0_PRIVILEGED | HV_CCB_ARG0_TYPE_PRIMARY |
  326. HV_CCB_VA_PRIVILEGED,
  327. 0, &bytes_accepted, &status_data);
  328. if (hv_rv != HV_EOK) {
  329. /* hv_rv is an error code, status_data contains */
  330. /* potential additional status, see 36.3.1.1 */
  331. }
  332. After the submission, the completion area polling code is identical to
  333. that in user land::
  334. while (1) {
  335. /* Monitored Load */
  336. __asm__ __volatile__("lduba [%1] 0x84, %0\n"
  337. : "=r" (status)
  338. : "r" (completion_area));
  339. if (status) /* 0 indicates command in progress */
  340. break;
  341. /* MWAIT */
  342. __asm__ __volatile__("wr %%g0, 1000, %%asr28\n" ::); /* 1000 ns */
  343. }
  344. if (completion_area[0] != 1) { /* section 36.2.2, 1 = command ran and succeeded */
  345. /* completion_area[0] contains the completion status */
  346. /* completion_area[1] contains an error code, see 36.2.2 */
  347. }
  348. The output bitmap is ready for consumption immediately after the
  349. completion status indicates success.
  350. Excer[t from UltraSPARC Virtual Machine Specification
  351. =====================================================
  352. .. include:: dax-hv-api.txt
  353. :literal: