cxl.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. Coherent Accelerator Interface (CXL)
  2. ====================================
  3. Introduction
  4. ============
  5. The coherent accelerator interface is designed to allow the
  6. coherent connection of accelerators (FPGAs and other devices) to a
  7. POWER system. These devices need to adhere to the Coherent
  8. Accelerator Interface Architecture (CAIA).
  9. IBM refers to this as the Coherent Accelerator Processor Interface
  10. or CAPI. In the kernel it's referred to by the name CXL to avoid
  11. confusion with the ISDN CAPI subsystem.
  12. Coherent in this context means that the accelerator and CPUs can
  13. both access system memory directly and with the same effective
  14. addresses.
  15. Hardware overview
  16. =================
  17. POWER8 FPGA
  18. +----------+ +---------+
  19. | | | |
  20. | CPU | | AFU |
  21. | | | |
  22. | | | |
  23. | | | |
  24. +----------+ +---------+
  25. | PHB | | |
  26. | +------+ | PSL |
  27. | | CAPP |<------>| |
  28. +---+------+ PCIE +---------+
  29. The POWER8 chip has a Coherently Attached Processor Proxy (CAPP)
  30. unit which is part of the PCIe Host Bridge (PHB). This is managed
  31. by Linux by calls into OPAL. Linux doesn't directly program the
  32. CAPP.
  33. The FPGA (or coherently attached device) consists of two parts.
  34. The POWER Service Layer (PSL) and the Accelerator Function Unit
  35. (AFU). The AFU is used to implement specific functionality behind
  36. the PSL. The PSL, among other things, provides memory address
  37. translation services to allow each AFU direct access to userspace
  38. memory.
  39. The AFU is the core part of the accelerator (eg. the compression,
  40. crypto etc function). The kernel has no knowledge of the function
  41. of the AFU. Only userspace interacts directly with the AFU.
  42. The PSL provides the translation and interrupt services that the
  43. AFU needs. This is what the kernel interacts with. For example, if
  44. the AFU needs to read a particular effective address, it sends
  45. that address to the PSL, the PSL then translates it, fetches the
  46. data from memory and returns it to the AFU. If the PSL has a
  47. translation miss, it interrupts the kernel and the kernel services
  48. the fault. The context to which this fault is serviced is based on
  49. who owns that acceleration function.
  50. AFU Modes
  51. =========
  52. There are two programming modes supported by the AFU. Dedicated
  53. and AFU directed. AFU may support one or both modes.
  54. When using dedicated mode only one MMU context is supported. In
  55. this mode, only one userspace process can use the accelerator at
  56. time.
  57. When using AFU directed mode, up to 16K simultaneous contexts can
  58. be supported. This means up to 16K simultaneous userspace
  59. applications may use the accelerator (although specific AFUs may
  60. support fewer). In this mode, the AFU sends a 16 bit context ID
  61. with each of its requests. This tells the PSL which context is
  62. associated with each operation. If the PSL can't translate an
  63. operation, the ID can also be accessed by the kernel so it can
  64. determine the userspace context associated with an operation.
  65. MMIO space
  66. ==========
  67. A portion of the accelerator MMIO space can be directly mapped
  68. from the AFU to userspace. Either the whole space can be mapped or
  69. just a per context portion. The hardware is self describing, hence
  70. the kernel can determine the offset and size of the per context
  71. portion.
  72. Interrupts
  73. ==========
  74. AFUs may generate interrupts that are destined for userspace. These
  75. are received by the kernel as hardware interrupts and passed onto
  76. userspace by a read syscall documented below.
  77. Data storage faults and error interrupts are handled by the kernel
  78. driver.
  79. Work Element Descriptor (WED)
  80. =============================
  81. The WED is a 64-bit parameter passed to the AFU when a context is
  82. started. Its format is up to the AFU hence the kernel has no
  83. knowledge of what it represents. Typically it will be the
  84. effective address of a work queue or status block where the AFU
  85. and userspace can share control and status information.
  86. User API
  87. ========
  88. For AFUs operating in AFU directed mode, two character device
  89. files will be created. /dev/cxl/afu0.0m will correspond to a
  90. master context and /dev/cxl/afu0.0s will correspond to a slave
  91. context. Master contexts have access to the full MMIO space an
  92. AFU provides. Slave contexts have access to only the per process
  93. MMIO space an AFU provides.
  94. For AFUs operating in dedicated process mode, the driver will
  95. only create a single character device per AFU called
  96. /dev/cxl/afu0.0d. This will have access to the entire MMIO space
  97. that the AFU provides (like master contexts in AFU directed).
  98. The types described below are defined in include/uapi/misc/cxl.h
  99. The following file operations are supported on both slave and
  100. master devices.
  101. A userspace library libcxl is available here:
  102. https://github.com/ibm-capi/libcxl
  103. This provides a C interface to this kernel API.
  104. open
  105. ----
  106. Opens the device and allocates a file descriptor to be used with
  107. the rest of the API.
  108. A dedicated mode AFU only has one context and only allows the
  109. device to be opened once.
  110. An AFU directed mode AFU can have many contexts, the device can be
  111. opened once for each context that is available.
  112. When all available contexts are allocated the open call will fail
  113. and return -ENOSPC.
  114. Note: IRQs need to be allocated for each context, which may limit
  115. the number of contexts that can be created, and therefore
  116. how many times the device can be opened. The POWER8 CAPP
  117. supports 2040 IRQs and 3 are used by the kernel, so 2037 are
  118. left. If 1 IRQ is needed per context, then only 2037
  119. contexts can be allocated. If 4 IRQs are needed per context,
  120. then only 2037/4 = 509 contexts can be allocated.
  121. ioctl
  122. -----
  123. CXL_IOCTL_START_WORK:
  124. Starts the AFU context and associates it with the current
  125. process. Once this ioctl is successfully executed, all memory
  126. mapped into this process is accessible to this AFU context
  127. using the same effective addresses. No additional calls are
  128. required to map/unmap memory. The AFU memory context will be
  129. updated as userspace allocates and frees memory. This ioctl
  130. returns once the AFU context is started.
  131. Takes a pointer to a struct cxl_ioctl_start_work:
  132. struct cxl_ioctl_start_work {
  133. __u64 flags;
  134. __u64 work_element_descriptor;
  135. __u64 amr;
  136. __s16 num_interrupts;
  137. __s16 reserved1;
  138. __s32 reserved2;
  139. __u64 reserved3;
  140. __u64 reserved4;
  141. __u64 reserved5;
  142. __u64 reserved6;
  143. };
  144. flags:
  145. Indicates which optional fields in the structure are
  146. valid.
  147. work_element_descriptor:
  148. The Work Element Descriptor (WED) is a 64-bit argument
  149. defined by the AFU. Typically this is an effective
  150. address pointing to an AFU specific structure
  151. describing what work to perform.
  152. amr:
  153. Authority Mask Register (AMR), same as the powerpc
  154. AMR. This field is only used by the kernel when the
  155. corresponding CXL_START_WORK_AMR value is specified in
  156. flags. If not specified the kernel will use a default
  157. value of 0.
  158. num_interrupts:
  159. Number of userspace interrupts to request. This field
  160. is only used by the kernel when the corresponding
  161. CXL_START_WORK_NUM_IRQS value is specified in flags.
  162. If not specified the minimum number required by the
  163. AFU will be allocated. The min and max number can be
  164. obtained from sysfs.
  165. reserved fields:
  166. For ABI padding and future extensions
  167. CXL_IOCTL_GET_PROCESS_ELEMENT:
  168. Get the current context id, also known as the process element.
  169. The value is returned from the kernel as a __u32.
  170. mmap
  171. ----
  172. An AFU may have an MMIO space to facilitate communication with the
  173. AFU. If it does, the MMIO space can be accessed via mmap. The size
  174. and contents of this area are specific to the particular AFU. The
  175. size can be discovered via sysfs.
  176. In AFU directed mode, master contexts are allowed to map all of
  177. the MMIO space and slave contexts are allowed to only map the per
  178. process MMIO space associated with the context. In dedicated
  179. process mode the entire MMIO space can always be mapped.
  180. This mmap call must be done after the START_WORK ioctl.
  181. Care should be taken when accessing MMIO space. Only 32 and 64-bit
  182. accesses are supported by POWER8. Also, the AFU will be designed
  183. with a specific endianness, so all MMIO accesses should consider
  184. endianness (recommend endian(3) variants like: le64toh(),
  185. be64toh() etc). These endian issues equally apply to shared memory
  186. queues the WED may describe.
  187. read
  188. ----
  189. Reads events from the AFU. Blocks if no events are pending
  190. (unless O_NONBLOCK is supplied). Returns -EIO in the case of an
  191. unrecoverable error or if the card is removed.
  192. read() will always return an integral number of events.
  193. The buffer passed to read() must be at least 4K bytes.
  194. The result of the read will be a buffer of one or more events,
  195. each event is of type struct cxl_event, of varying size.
  196. struct cxl_event {
  197. struct cxl_event_header header;
  198. union {
  199. struct cxl_event_afu_interrupt irq;
  200. struct cxl_event_data_storage fault;
  201. struct cxl_event_afu_error afu_error;
  202. };
  203. };
  204. The struct cxl_event_header is defined as:
  205. struct cxl_event_header {
  206. __u16 type;
  207. __u16 size;
  208. __u16 process_element;
  209. __u16 reserved1;
  210. };
  211. type:
  212. This defines the type of event. The type determines how
  213. the rest of the event is structured. These types are
  214. described below and defined by enum cxl_event_type.
  215. size:
  216. This is the size of the event in bytes including the
  217. struct cxl_event_header. The start of the next event can
  218. be found at this offset from the start of the current
  219. event.
  220. process_element:
  221. Context ID of the event.
  222. reserved field:
  223. For future extensions and padding.
  224. If the event type is CXL_EVENT_AFU_INTERRUPT then the event
  225. structure is defined as:
  226. struct cxl_event_afu_interrupt {
  227. __u16 flags;
  228. __u16 irq; /* Raised AFU interrupt number */
  229. __u32 reserved1;
  230. };
  231. flags:
  232. These flags indicate which optional fields are present
  233. in this struct. Currently all fields are mandatory.
  234. irq:
  235. The IRQ number sent by the AFU.
  236. reserved field:
  237. For future extensions and padding.
  238. If the event type is CXL_EVENT_DATA_STORAGE then the event
  239. structure is defined as:
  240. struct cxl_event_data_storage {
  241. __u16 flags;
  242. __u16 reserved1;
  243. __u32 reserved2;
  244. __u64 addr;
  245. __u64 dsisr;
  246. __u64 reserved3;
  247. };
  248. flags:
  249. These flags indicate which optional fields are present in
  250. this struct. Currently all fields are mandatory.
  251. address:
  252. The address that the AFU unsuccessfully attempted to
  253. access. Valid accesses will be handled transparently by the
  254. kernel but invalid accesses will generate this event.
  255. dsisr:
  256. This field gives information on the type of fault. It is a
  257. copy of the DSISR from the PSL hardware when the address
  258. fault occurred. The form of the DSISR is as defined in the
  259. CAIA.
  260. reserved fields:
  261. For future extensions
  262. If the event type is CXL_EVENT_AFU_ERROR then the event structure
  263. is defined as:
  264. struct cxl_event_afu_error {
  265. __u16 flags;
  266. __u16 reserved1;
  267. __u32 reserved2;
  268. __u64 error;
  269. };
  270. flags:
  271. These flags indicate which optional fields are present in
  272. this struct. Currently all fields are Mandatory.
  273. error:
  274. Error status from the AFU. Defined by the AFU.
  275. reserved fields:
  276. For future extensions and padding
  277. Sysfs Class
  278. ===========
  279. A cxl sysfs class is added under /sys/class/cxl to facilitate
  280. enumeration and tuning of the accelerators. Its layout is
  281. described in Documentation/ABI/testing/sysfs-class-cxl
  282. Udev rules
  283. ==========
  284. The following udev rules could be used to create a symlink to the
  285. most logical chardev to use in any programming mode (afuX.Yd for
  286. dedicated, afuX.Ys for afu directed), since the API is virtually
  287. identical for each:
  288. SUBSYSTEM=="cxl", ATTRS{mode}=="dedicated_process", SYMLINK="cxl/%b"
  289. SUBSYSTEM=="cxl", ATTRS{mode}=="afu_directed", \
  290. KERNEL=="afu[0-9]*.[0-9]*s", SYMLINK="cxl/%b"