cxl.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. 1. AFU character devices
  89. For AFUs operating in AFU directed mode, two character device
  90. files will be created. /dev/cxl/afu0.0m will correspond to a
  91. master context and /dev/cxl/afu0.0s will correspond to a slave
  92. context. Master contexts have access to the full MMIO space an
  93. AFU provides. Slave contexts have access to only the per process
  94. MMIO space an AFU provides.
  95. For AFUs operating in dedicated process mode, the driver will
  96. only create a single character device per AFU called
  97. /dev/cxl/afu0.0d. This will have access to the entire MMIO space
  98. that the AFU provides (like master contexts in AFU directed).
  99. The types described below are defined in include/uapi/misc/cxl.h
  100. The following file operations are supported on both slave and
  101. master devices.
  102. A userspace library libcxl is available here:
  103. https://github.com/ibm-capi/libcxl
  104. This provides a C interface to this kernel API.
  105. open
  106. ----
  107. Opens the device and allocates a file descriptor to be used with
  108. the rest of the API.
  109. A dedicated mode AFU only has one context and only allows the
  110. device to be opened once.
  111. An AFU directed mode AFU can have many contexts, the device can be
  112. opened once for each context that is available.
  113. When all available contexts are allocated the open call will fail
  114. and return -ENOSPC.
  115. Note: IRQs need to be allocated for each context, which may limit
  116. the number of contexts that can be created, and therefore
  117. how many times the device can be opened. The POWER8 CAPP
  118. supports 2040 IRQs and 3 are used by the kernel, so 2037 are
  119. left. If 1 IRQ is needed per context, then only 2037
  120. contexts can be allocated. If 4 IRQs are needed per context,
  121. then only 2037/4 = 509 contexts can be allocated.
  122. ioctl
  123. -----
  124. CXL_IOCTL_START_WORK:
  125. Starts the AFU context and associates it with the current
  126. process. Once this ioctl is successfully executed, all memory
  127. mapped into this process is accessible to this AFU context
  128. using the same effective addresses. No additional calls are
  129. required to map/unmap memory. The AFU memory context will be
  130. updated as userspace allocates and frees memory. This ioctl
  131. returns once the AFU context is started.
  132. Takes a pointer to a struct cxl_ioctl_start_work:
  133. struct cxl_ioctl_start_work {
  134. __u64 flags;
  135. __u64 work_element_descriptor;
  136. __u64 amr;
  137. __s16 num_interrupts;
  138. __s16 reserved1;
  139. __s32 reserved2;
  140. __u64 reserved3;
  141. __u64 reserved4;
  142. __u64 reserved5;
  143. __u64 reserved6;
  144. };
  145. flags:
  146. Indicates which optional fields in the structure are
  147. valid.
  148. work_element_descriptor:
  149. The Work Element Descriptor (WED) is a 64-bit argument
  150. defined by the AFU. Typically this is an effective
  151. address pointing to an AFU specific structure
  152. describing what work to perform.
  153. amr:
  154. Authority Mask Register (AMR), same as the powerpc
  155. AMR. This field is only used by the kernel when the
  156. corresponding CXL_START_WORK_AMR value is specified in
  157. flags. If not specified the kernel will use a default
  158. value of 0.
  159. num_interrupts:
  160. Number of userspace interrupts to request. This field
  161. is only used by the kernel when the corresponding
  162. CXL_START_WORK_NUM_IRQS value is specified in flags.
  163. If not specified the minimum number required by the
  164. AFU will be allocated. The min and max number can be
  165. obtained from sysfs.
  166. reserved fields:
  167. For ABI padding and future extensions
  168. CXL_IOCTL_GET_PROCESS_ELEMENT:
  169. Get the current context id, also known as the process element.
  170. The value is returned from the kernel as a __u32.
  171. mmap
  172. ----
  173. An AFU may have an MMIO space to facilitate communication with the
  174. AFU. If it does, the MMIO space can be accessed via mmap. The size
  175. and contents of this area are specific to the particular AFU. The
  176. size can be discovered via sysfs.
  177. In AFU directed mode, master contexts are allowed to map all of
  178. the MMIO space and slave contexts are allowed to only map the per
  179. process MMIO space associated with the context. In dedicated
  180. process mode the entire MMIO space can always be mapped.
  181. This mmap call must be done after the START_WORK ioctl.
  182. Care should be taken when accessing MMIO space. Only 32 and 64-bit
  183. accesses are supported by POWER8. Also, the AFU will be designed
  184. with a specific endianness, so all MMIO accesses should consider
  185. endianness (recommend endian(3) variants like: le64toh(),
  186. be64toh() etc). These endian issues equally apply to shared memory
  187. queues the WED may describe.
  188. read
  189. ----
  190. Reads events from the AFU. Blocks if no events are pending
  191. (unless O_NONBLOCK is supplied). Returns -EIO in the case of an
  192. unrecoverable error or if the card is removed.
  193. read() will always return an integral number of events.
  194. The buffer passed to read() must be at least 4K bytes.
  195. The result of the read will be a buffer of one or more events,
  196. each event is of type struct cxl_event, of varying size.
  197. struct cxl_event {
  198. struct cxl_event_header header;
  199. union {
  200. struct cxl_event_afu_interrupt irq;
  201. struct cxl_event_data_storage fault;
  202. struct cxl_event_afu_error afu_error;
  203. };
  204. };
  205. The struct cxl_event_header is defined as:
  206. struct cxl_event_header {
  207. __u16 type;
  208. __u16 size;
  209. __u16 process_element;
  210. __u16 reserved1;
  211. };
  212. type:
  213. This defines the type of event. The type determines how
  214. the rest of the event is structured. These types are
  215. described below and defined by enum cxl_event_type.
  216. size:
  217. This is the size of the event in bytes including the
  218. struct cxl_event_header. The start of the next event can
  219. be found at this offset from the start of the current
  220. event.
  221. process_element:
  222. Context ID of the event.
  223. reserved field:
  224. For future extensions and padding.
  225. If the event type is CXL_EVENT_AFU_INTERRUPT then the event
  226. structure is defined as:
  227. struct cxl_event_afu_interrupt {
  228. __u16 flags;
  229. __u16 irq; /* Raised AFU interrupt number */
  230. __u32 reserved1;
  231. };
  232. flags:
  233. These flags indicate which optional fields are present
  234. in this struct. Currently all fields are mandatory.
  235. irq:
  236. The IRQ number sent by the AFU.
  237. reserved field:
  238. For future extensions and padding.
  239. If the event type is CXL_EVENT_DATA_STORAGE then the event
  240. structure is defined as:
  241. struct cxl_event_data_storage {
  242. __u16 flags;
  243. __u16 reserved1;
  244. __u32 reserved2;
  245. __u64 addr;
  246. __u64 dsisr;
  247. __u64 reserved3;
  248. };
  249. flags:
  250. These flags indicate which optional fields are present in
  251. this struct. Currently all fields are mandatory.
  252. address:
  253. The address that the AFU unsuccessfully attempted to
  254. access. Valid accesses will be handled transparently by the
  255. kernel but invalid accesses will generate this event.
  256. dsisr:
  257. This field gives information on the type of fault. It is a
  258. copy of the DSISR from the PSL hardware when the address
  259. fault occurred. The form of the DSISR is as defined in the
  260. CAIA.
  261. reserved fields:
  262. For future extensions
  263. If the event type is CXL_EVENT_AFU_ERROR then the event structure
  264. is defined as:
  265. struct cxl_event_afu_error {
  266. __u16 flags;
  267. __u16 reserved1;
  268. __u32 reserved2;
  269. __u64 error;
  270. };
  271. flags:
  272. These flags indicate which optional fields are present in
  273. this struct. Currently all fields are Mandatory.
  274. error:
  275. Error status from the AFU. Defined by the AFU.
  276. reserved fields:
  277. For future extensions and padding
  278. 2. Card character device (powerVM guest only)
  279. In a powerVM guest, an extra character device is created for the
  280. card. The device is only used to write (flash) a new image on the
  281. FPGA accelerator. Once the image is written and verified, the
  282. device tree is updated and the card is reset to reload the updated
  283. image.
  284. open
  285. ----
  286. Opens the device and allocates a file descriptor to be used with
  287. the rest of the API. The device can only be opened once.
  288. ioctl
  289. -----
  290. CXL_IOCTL_DOWNLOAD_IMAGE:
  291. CXL_IOCTL_VALIDATE_IMAGE:
  292. Starts and controls flashing a new FPGA image. Partial
  293. reconfiguration is not supported (yet), so the image must contain
  294. a copy of the PSL and AFU(s). Since an image can be quite large,
  295. the caller may have to iterate, splitting the image in smaller
  296. chunks.
  297. Takes a pointer to a struct cxl_adapter_image:
  298. struct cxl_adapter_image {
  299. __u64 flags;
  300. __u64 data;
  301. __u64 len_data;
  302. __u64 len_image;
  303. __u64 reserved1;
  304. __u64 reserved2;
  305. __u64 reserved3;
  306. __u64 reserved4;
  307. };
  308. flags:
  309. These flags indicate which optional fields are present in
  310. this struct. Currently all fields are mandatory.
  311. data:
  312. Pointer to a buffer with part of the image to write to the
  313. card.
  314. len_data:
  315. Size of the buffer pointed to by data.
  316. len_image:
  317. Full size of the image.
  318. Sysfs Class
  319. ===========
  320. A cxl sysfs class is added under /sys/class/cxl to facilitate
  321. enumeration and tuning of the accelerators. Its layout is
  322. described in Documentation/ABI/testing/sysfs-class-cxl
  323. Udev rules
  324. ==========
  325. The following udev rules could be used to create a symlink to the
  326. most logical chardev to use in any programming mode (afuX.Yd for
  327. dedicated, afuX.Ys for afu directed), since the API is virtually
  328. identical for each:
  329. SUBSYSTEM=="cxl", ATTRS{mode}=="dedicated_process", SYMLINK="cxl/%b"
  330. SUBSYSTEM=="cxl", ATTRS{mode}=="afu_directed", \
  331. KERNEL=="afu[0-9]*.[0-9]*s", SYMLINK="cxl/%b"