cxl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /* Get the AFU associated with a pci_dev */
  24. struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev);
  25. /* Get the AFU conf record number associated with a pci_dev */
  26. unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev);
  27. /*
  28. * Context lifetime overview:
  29. *
  30. * An AFU context may be inited and then started and stoppped multiple times
  31. * before it's released. ie.
  32. * - cxl_dev_context_init()
  33. * - cxl_start_context()
  34. * - cxl_stop_context()
  35. * - cxl_start_context()
  36. * - cxl_stop_context()
  37. * ...repeat...
  38. * - cxl_release_context()
  39. * Once released, a context can't be started again.
  40. *
  41. * One context is inited by the cxl driver for every pci_dev. This is to be
  42. * used as a default kernel context. cxl_get_context() will get this
  43. * context. This context will be released by PCI hot unplug, so doesn't need to
  44. * be released explicitly by drivers.
  45. *
  46. * Additional kernel contexts may be inited using cxl_dev_context_init().
  47. * These must be released using cxl_context_detach().
  48. *
  49. * Once a context has been inited, IRQs may be configured. Firstly these IRQs
  50. * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to
  51. * specific handlers (cxl_map_afu_irq()).
  52. *
  53. * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released
  54. * (cxl_free_afu_irqs()).
  55. *
  56. * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU
  57. * hardware to lose track of all contexts. It's upto the caller of
  58. * cxl_afu_reset() to restart these contexts.
  59. */
  60. /*
  61. * On pci_enabled_device(), the cxl driver will init a single cxl context for
  62. * use by the driver. It doesn't start this context (as that will likely
  63. * generate DMA traffic for most AFUs).
  64. *
  65. * This gets the default context associated with this pci_dev. This context
  66. * doesn't need to be released as this will be done by the PCI subsystem on hot
  67. * unplug.
  68. */
  69. struct cxl_context *cxl_get_context(struct pci_dev *dev);
  70. /*
  71. * Allocate and initalise a context associated with a AFU PCI device. This
  72. * doesn't start the context in the AFU.
  73. */
  74. struct cxl_context *cxl_dev_context_init(struct pci_dev *dev);
  75. /*
  76. * Release and free a context. Context should be stopped before calling.
  77. */
  78. int cxl_release_context(struct cxl_context *ctx);
  79. /*
  80. * Allocate AFU interrupts for this context. num=0 will allocate the default
  81. * for this AFU as given in the AFU descriptor. This number doesn't include the
  82. * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be
  83. * used must map a handler with cxl_map_afu_irq.
  84. */
  85. int cxl_allocate_afu_irqs(struct cxl_context *cxl, int num);
  86. /* Free allocated interrupts */
  87. void cxl_free_afu_irqs(struct cxl_context *cxl);
  88. /*
  89. * Map a handler for an AFU interrupt associated with a particular context. AFU
  90. * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie
  91. * is private data is that will be provided to the interrupt handler.
  92. */
  93. int cxl_map_afu_irq(struct cxl_context *cxl, int num,
  94. irq_handler_t handler, void *cookie, char *name);
  95. /* unmap mapped IRQ handlers */
  96. void cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie);
  97. /*
  98. * Start work on the AFU. This starts an cxl context and associates it with a
  99. * task. task == NULL will make it a kernel context.
  100. */
  101. int cxl_start_context(struct cxl_context *ctx, u64 wed,
  102. struct task_struct *task);
  103. /*
  104. * Stop a context and remove it from the PSL
  105. */
  106. int cxl_stop_context(struct cxl_context *ctx);
  107. /* Reset the AFU */
  108. int cxl_afu_reset(struct cxl_context *ctx);
  109. /*
  110. * Set a context as a master context.
  111. * This sets the default problem space area mapped as the full space, rather
  112. * than just the per context area (for slaves).
  113. */
  114. void cxl_set_master(struct cxl_context *ctx);
  115. /*
  116. * Sets the context to use real mode memory accesses to operate with
  117. * translation disabled. Note that this only makes sense for kernel contexts
  118. * under bare metal, and will not work with virtualisation. May only be
  119. * performed on stopped contexts.
  120. */
  121. int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode);
  122. /*
  123. * Map and unmap the AFU Problem Space area. The amount and location mapped
  124. * depends on if this context is a master or slave.
  125. */
  126. void __iomem *cxl_psa_map(struct cxl_context *ctx);
  127. void cxl_psa_unmap(void __iomem *addr);
  128. /* Get the process element for this context */
  129. int cxl_process_element(struct cxl_context *ctx);
  130. /*
  131. * These calls allow drivers to create their own file descriptors and make them
  132. * identical to the cxl file descriptor user API. An example use case:
  133. *
  134. * struct file_operations cxl_my_fops = {};
  135. * ......
  136. * // Init the context
  137. * ctx = cxl_dev_context_init(dev);
  138. * if (IS_ERR(ctx))
  139. * return PTR_ERR(ctx);
  140. * // Create and attach a new file descriptor to my file ops
  141. * file = cxl_get_fd(ctx, &cxl_my_fops, &fd);
  142. * // Start context
  143. * rc = cxl_start_work(ctx, &work.work);
  144. * if (rc) {
  145. * fput(file);
  146. * put_unused_fd(fd);
  147. * return -ENODEV;
  148. * }
  149. * // No error paths after installing the fd
  150. * fd_install(fd, file);
  151. * return fd;
  152. *
  153. * This inits a context, and gets a file descriptor and associates some file
  154. * ops to that file descriptor. If the file ops are blank, the cxl driver will
  155. * fill them in with the default ones that mimic the standard user API. Once
  156. * completed, the file descriptor can be installed. Once the file descriptor is
  157. * installed, it's visible to the user so no errors must occur past this point.
  158. *
  159. * If cxl_fd_release() file op call is installed, the context will be stopped
  160. * and released when the fd is released. Hence the driver won't need to manage
  161. * this itself.
  162. */
  163. /*
  164. * Take a context and associate it with my file ops. Returns the associated
  165. * file and file descriptor. Any file ops which are blank are filled in by the
  166. * cxl driver with the default ops to mimic the standard API.
  167. */
  168. struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
  169. int *fd);
  170. /* Get the context associated with this file */
  171. struct cxl_context *cxl_fops_get_context(struct file *file);
  172. /*
  173. * Start a context associated a struct cxl_ioctl_start_work used by the
  174. * standard cxl user API.
  175. */
  176. int cxl_start_work(struct cxl_context *ctx,
  177. struct cxl_ioctl_start_work *work);
  178. /*
  179. * Export all the existing fops so drivers can use them
  180. */
  181. int cxl_fd_open(struct inode *inode, struct file *file);
  182. int cxl_fd_release(struct inode *inode, struct file *file);
  183. long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  184. int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm);
  185. unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll);
  186. ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
  187. loff_t *off);
  188. /*
  189. * For EEH, a driver may want to assert a PERST will reload the same image
  190. * from flash into the FPGA.
  191. *
  192. * This is a property of the entire adapter, not a single AFU, so drivers
  193. * should set this property with care!
  194. */
  195. void cxl_perst_reloads_same_image(struct cxl_afu *afu,
  196. bool perst_reloads_same_image);
  197. /*
  198. * Read the VPD for the card where the AFU resides
  199. */
  200. ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
  201. #endif /* _MISC_CXL_H */