cxl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright 2015 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #ifndef _MISC_CXL_H
  10. #define _MISC_CXL_H
  11. #include <linux/pci.h>
  12. #include <linux/poll.h>
  13. #include <linux/interrupt.h>
  14. #include <uapi/misc/cxl.h>
  15. /*
  16. * This documents the in kernel API for driver to use CXL. It allows kernel
  17. * drivers to bind to AFUs using an AFU configuration record exposed as a PCI
  18. * configuration record.
  19. *
  20. * This API enables control over AFU and contexts which can't be part of the
  21. * generic PCI API. This API is agnostic to the actual AFU.
  22. */
  23. #define CXL_SLOT_FLAG_DMA 0x1
  24. /*
  25. * Checks if the given card is in a cxl capable slot. Pass CXL_SLOT_FLAG_DMA if
  26. * the card requires CAPP DMA mode to also check if the system supports it.
  27. * This is intended to be used by bi-modal devices to determine if they can use
  28. * cxl mode or if they should continue running in PCI mode.
  29. *
  30. * Note that this only checks if the slot is cxl capable - it does not
  31. * currently check if the CAPP is currently available for chips where it can be
  32. * assigned to different PHBs on a first come first serve basis (i.e. P8)
  33. */
  34. bool cxl_slot_is_supported(struct pci_dev *dev, int flags);
  35. #define CXL_BIMODE_CXL 1
  36. #define CXL_BIMODE_PCI 2
  37. /*
  38. * Check the mode that the given bi-modal CXL adapter is currently in and
  39. * change it if necessary. This does not apply to AFU drivers.
  40. *
  41. * If the mode matches the requested mode this function will return 0 - if the
  42. * driver was expecting the generic CXL driver to have bound to the adapter and
  43. * it gets this return value it should fail the probe function to give the CXL
  44. * driver a chance to probe it.
  45. *
  46. * If the mode does not match it will start a background task to unplug the
  47. * device from Linux and switch its mode, and will return -EBUSY. At this
  48. * point the calling driver should make sure it has released the device and
  49. * fail its probe function.
  50. *
  51. * The offset of the CXL VSEC can be provided to this function. If 0 is passed,
  52. * this function will search for a CXL VSEC with ID 0x1280 and return -ENODEV
  53. * if it is not found.
  54. */
  55. #ifdef CONFIG_CXL_BIMODAL
  56. int cxl_check_and_switch_mode(struct pci_dev *dev, int mode, int vsec);
  57. #endif
  58. /* Get the AFU associated with a pci_dev */
  59. struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev);
  60. /* Get the AFU conf record number associated with a pci_dev */
  61. unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev);
  62. /*
  63. * Context lifetime overview:
  64. *
  65. * An AFU context may be inited and then started and stoppped multiple times
  66. * before it's released. ie.
  67. * - cxl_dev_context_init()
  68. * - cxl_start_context()
  69. * - cxl_stop_context()
  70. * - cxl_start_context()
  71. * - cxl_stop_context()
  72. * ...repeat...
  73. * - cxl_release_context()
  74. * Once released, a context can't be started again.
  75. *
  76. * One context is inited by the cxl driver for every pci_dev. This is to be
  77. * used as a default kernel context. cxl_get_context() will get this
  78. * context. This context will be released by PCI hot unplug, so doesn't need to
  79. * be released explicitly by drivers.
  80. *
  81. * Additional kernel contexts may be inited using cxl_dev_context_init().
  82. * These must be released using cxl_context_detach().
  83. *
  84. * Once a context has been inited, IRQs may be configured. Firstly these IRQs
  85. * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to
  86. * specific handlers (cxl_map_afu_irq()).
  87. *
  88. * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released
  89. * (cxl_free_afu_irqs()).
  90. *
  91. * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU
  92. * hardware to lose track of all contexts. It's upto the caller of
  93. * cxl_afu_reset() to restart these contexts.
  94. */
  95. /*
  96. * On pci_enabled_device(), the cxl driver will init a single cxl context for
  97. * use by the driver. It doesn't start this context (as that will likely
  98. * generate DMA traffic for most AFUs).
  99. *
  100. * This gets the default context associated with this pci_dev. This context
  101. * doesn't need to be released as this will be done by the PCI subsystem on hot
  102. * unplug.
  103. */
  104. struct cxl_context *cxl_get_context(struct pci_dev *dev);
  105. /*
  106. * Allocate and initalise a context associated with a AFU PCI device. This
  107. * doesn't start the context in the AFU.
  108. */
  109. struct cxl_context *cxl_dev_context_init(struct pci_dev *dev);
  110. /*
  111. * Release and free a context. Context should be stopped before calling.
  112. */
  113. int cxl_release_context(struct cxl_context *ctx);
  114. /*
  115. * Set and get private data associated with a context. Allows drivers to have a
  116. * back pointer to some useful structure.
  117. */
  118. int cxl_set_priv(struct cxl_context *ctx, void *priv);
  119. void *cxl_get_priv(struct cxl_context *ctx);
  120. /*
  121. * Allocate AFU interrupts for this context. num=0 will allocate the default
  122. * for this AFU as given in the AFU descriptor. This number doesn't include the
  123. * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be
  124. * used must map a handler with cxl_map_afu_irq.
  125. */
  126. int cxl_allocate_afu_irqs(struct cxl_context *cxl, int num);
  127. /* Free allocated interrupts */
  128. void cxl_free_afu_irqs(struct cxl_context *cxl);
  129. /*
  130. * Map a handler for an AFU interrupt associated with a particular context. AFU
  131. * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie
  132. * is private data is that will be provided to the interrupt handler.
  133. */
  134. int cxl_map_afu_irq(struct cxl_context *cxl, int num,
  135. irq_handler_t handler, void *cookie, char *name);
  136. /* unmap mapped IRQ handlers */
  137. void cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie);
  138. /*
  139. * Start work on the AFU. This starts an cxl context and associates it with a
  140. * task. task == NULL will make it a kernel context.
  141. */
  142. int cxl_start_context(struct cxl_context *ctx, u64 wed,
  143. struct task_struct *task);
  144. /*
  145. * Stop a context and remove it from the PSL
  146. */
  147. int cxl_stop_context(struct cxl_context *ctx);
  148. /* Reset the AFU */
  149. int cxl_afu_reset(struct cxl_context *ctx);
  150. /*
  151. * Set a context as a master context.
  152. * This sets the default problem space area mapped as the full space, rather
  153. * than just the per context area (for slaves).
  154. */
  155. void cxl_set_master(struct cxl_context *ctx);
  156. /*
  157. * Sets the context to use real mode memory accesses to operate with
  158. * translation disabled. Note that this only makes sense for kernel contexts
  159. * under bare metal, and will not work with virtualisation. May only be
  160. * performed on stopped contexts.
  161. */
  162. int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode);
  163. /*
  164. * Map and unmap the AFU Problem Space area. The amount and location mapped
  165. * depends on if this context is a master or slave.
  166. */
  167. void __iomem *cxl_psa_map(struct cxl_context *ctx);
  168. void cxl_psa_unmap(void __iomem *addr);
  169. /* Get the process element for this context */
  170. int cxl_process_element(struct cxl_context *ctx);
  171. /*
  172. * Limit the number of interrupts that a single context can allocate via
  173. * cxl_start_work. If using the api with a real phb, this may be used to
  174. * request that additional default contexts be created when allocating
  175. * interrupts via pci_enable_msix_range. These will be set to the same running
  176. * state as the default context, and if that is running it will reuse the
  177. * parameters previously passed to cxl_start_context for the default context.
  178. */
  179. int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs);
  180. int cxl_get_max_irqs_per_process(struct pci_dev *dev);
  181. /*
  182. * Use to simultaneously iterate over hardware interrupt numbers, contexts and
  183. * afu interrupt numbers allocated for the device via pci_enable_msix_range and
  184. * is a useful convenience function when working with hardware that has
  185. * limitations on the number of interrupts per process. *ctx and *afu_irq
  186. * should be NULL and 0 to start the iteration.
  187. */
  188. int cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq);
  189. /*
  190. * These calls allow drivers to create their own file descriptors and make them
  191. * identical to the cxl file descriptor user API. An example use case:
  192. *
  193. * struct file_operations cxl_my_fops = {};
  194. * ......
  195. * // Init the context
  196. * ctx = cxl_dev_context_init(dev);
  197. * if (IS_ERR(ctx))
  198. * return PTR_ERR(ctx);
  199. * // Create and attach a new file descriptor to my file ops
  200. * file = cxl_get_fd(ctx, &cxl_my_fops, &fd);
  201. * // Start context
  202. * rc = cxl_start_work(ctx, &work.work);
  203. * if (rc) {
  204. * fput(file);
  205. * put_unused_fd(fd);
  206. * return -ENODEV;
  207. * }
  208. * // No error paths after installing the fd
  209. * fd_install(fd, file);
  210. * return fd;
  211. *
  212. * This inits a context, and gets a file descriptor and associates some file
  213. * ops to that file descriptor. If the file ops are blank, the cxl driver will
  214. * fill them in with the default ones that mimic the standard user API. Once
  215. * completed, the file descriptor can be installed. Once the file descriptor is
  216. * installed, it's visible to the user so no errors must occur past this point.
  217. *
  218. * If cxl_fd_release() file op call is installed, the context will be stopped
  219. * and released when the fd is released. Hence the driver won't need to manage
  220. * this itself.
  221. */
  222. /*
  223. * Take a context and associate it with my file ops. Returns the associated
  224. * file and file descriptor. Any file ops which are blank are filled in by the
  225. * cxl driver with the default ops to mimic the standard API.
  226. */
  227. struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
  228. int *fd);
  229. /* Get the context associated with this file */
  230. struct cxl_context *cxl_fops_get_context(struct file *file);
  231. /*
  232. * Start a context associated a struct cxl_ioctl_start_work used by the
  233. * standard cxl user API.
  234. */
  235. int cxl_start_work(struct cxl_context *ctx,
  236. struct cxl_ioctl_start_work *work);
  237. /*
  238. * Export all the existing fops so drivers can use them
  239. */
  240. int cxl_fd_open(struct inode *inode, struct file *file);
  241. int cxl_fd_release(struct inode *inode, struct file *file);
  242. long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  243. int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm);
  244. unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll);
  245. ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
  246. loff_t *off);
  247. /*
  248. * For EEH, a driver may want to assert a PERST will reload the same image
  249. * from flash into the FPGA.
  250. *
  251. * This is a property of the entire adapter, not a single AFU, so drivers
  252. * should set this property with care!
  253. */
  254. void cxl_perst_reloads_same_image(struct cxl_afu *afu,
  255. bool perst_reloads_same_image);
  256. /*
  257. * Read the VPD for the card where the AFU resides
  258. */
  259. ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
  260. /*
  261. * AFU driver ops allow an AFU driver to create their own events to pass to
  262. * userspace through the file descriptor as a simpler alternative to overriding
  263. * the read() and poll() calls that works with the generic cxl events. These
  264. * events are given priority over the generic cxl events, so they will be
  265. * delivered first if multiple types of events are pending.
  266. *
  267. * The AFU driver must call cxl_context_events_pending() to notify the cxl
  268. * driver that new events are ready to be delivered for a specific context.
  269. * cxl_context_events_pending() will adjust the current count of AFU driver
  270. * events for this context, and wake up anyone waiting on the context wait
  271. * queue.
  272. *
  273. * The cxl driver will then call fetch_event() to get a structure defining
  274. * the size and address of the driver specific event data. The cxl driver
  275. * will build a cxl header with type and process_element fields filled in,
  276. * and header.size set to sizeof(struct cxl_event_header) + data_size.
  277. * The total size of the event is limited to CXL_READ_MIN_SIZE (4K).
  278. *
  279. * fetch_event() is called with a spin lock held, so it must not sleep.
  280. *
  281. * The cxl driver will then deliver the event to userspace, and finally
  282. * call event_delivered() to return the status of the operation, identified
  283. * by cxl context and AFU driver event data pointers.
  284. * 0 Success
  285. * -EFAULT copy_to_user() has failed
  286. * -EINVAL Event data pointer is NULL, or event size is greater than
  287. * CXL_READ_MIN_SIZE.
  288. */
  289. struct cxl_afu_driver_ops {
  290. struct cxl_event_afu_driver_reserved *(*fetch_event) (
  291. struct cxl_context *ctx);
  292. void (*event_delivered) (struct cxl_context *ctx,
  293. struct cxl_event_afu_driver_reserved *event,
  294. int rc);
  295. };
  296. /*
  297. * Associate the above driver ops with a specific context.
  298. * Reset the current count of AFU driver events.
  299. */
  300. void cxl_set_driver_ops(struct cxl_context *ctx,
  301. struct cxl_afu_driver_ops *ops);
  302. /* Notify cxl driver that new events are ready to be delivered for context */
  303. void cxl_context_events_pending(struct cxl_context *ctx,
  304. unsigned int new_events);
  305. #endif /* _MISC_CXL_H */