1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Copyright 2015 IBM Corp. 4*4882a593Smuzhiyun */ 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun #ifndef _MISC_CXL_H 7*4882a593Smuzhiyun #define _MISC_CXL_H 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #include <linux/pci.h> 10*4882a593Smuzhiyun #include <linux/poll.h> 11*4882a593Smuzhiyun #include <linux/interrupt.h> 12*4882a593Smuzhiyun #include <uapi/misc/cxl.h> 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun /* 15*4882a593Smuzhiyun * This documents the in kernel API for driver to use CXL. It allows kernel 16*4882a593Smuzhiyun * drivers to bind to AFUs using an AFU configuration record exposed as a PCI 17*4882a593Smuzhiyun * configuration record. 18*4882a593Smuzhiyun * 19*4882a593Smuzhiyun * This API enables control over AFU and contexts which can't be part of the 20*4882a593Smuzhiyun * generic PCI API. This API is agnostic to the actual AFU. 21*4882a593Smuzhiyun */ 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun /* Get the AFU associated with a pci_dev */ 24*4882a593Smuzhiyun struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev); 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun /* Get the AFU conf record number associated with a pci_dev */ 27*4882a593Smuzhiyun unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev); 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun /* 31*4882a593Smuzhiyun * Context lifetime overview: 32*4882a593Smuzhiyun * 33*4882a593Smuzhiyun * An AFU context may be inited and then started and stoppped multiple times 34*4882a593Smuzhiyun * before it's released. ie. 35*4882a593Smuzhiyun * - cxl_dev_context_init() 36*4882a593Smuzhiyun * - cxl_start_context() 37*4882a593Smuzhiyun * - cxl_stop_context() 38*4882a593Smuzhiyun * - cxl_start_context() 39*4882a593Smuzhiyun * - cxl_stop_context() 40*4882a593Smuzhiyun * ...repeat... 41*4882a593Smuzhiyun * - cxl_release_context() 42*4882a593Smuzhiyun * Once released, a context can't be started again. 43*4882a593Smuzhiyun * 44*4882a593Smuzhiyun * One context is inited by the cxl driver for every pci_dev. This is to be 45*4882a593Smuzhiyun * used as a default kernel context. cxl_get_context() will get this 46*4882a593Smuzhiyun * context. This context will be released by PCI hot unplug, so doesn't need to 47*4882a593Smuzhiyun * be released explicitly by drivers. 48*4882a593Smuzhiyun * 49*4882a593Smuzhiyun * Additional kernel contexts may be inited using cxl_dev_context_init(). 50*4882a593Smuzhiyun * These must be released using cxl_context_detach(). 51*4882a593Smuzhiyun * 52*4882a593Smuzhiyun * Once a context has been inited, IRQs may be configured. Firstly these IRQs 53*4882a593Smuzhiyun * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to 54*4882a593Smuzhiyun * specific handlers (cxl_map_afu_irq()). 55*4882a593Smuzhiyun * 56*4882a593Smuzhiyun * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released 57*4882a593Smuzhiyun * (cxl_free_afu_irqs()). 58*4882a593Smuzhiyun * 59*4882a593Smuzhiyun * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU 60*4882a593Smuzhiyun * hardware to lose track of all contexts. It's upto the caller of 61*4882a593Smuzhiyun * cxl_afu_reset() to restart these contexts. 62*4882a593Smuzhiyun */ 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun /* 65*4882a593Smuzhiyun * On pci_enabled_device(), the cxl driver will init a single cxl context for 66*4882a593Smuzhiyun * use by the driver. It doesn't start this context (as that will likely 67*4882a593Smuzhiyun * generate DMA traffic for most AFUs). 68*4882a593Smuzhiyun * 69*4882a593Smuzhiyun * This gets the default context associated with this pci_dev. This context 70*4882a593Smuzhiyun * doesn't need to be released as this will be done by the PCI subsystem on hot 71*4882a593Smuzhiyun * unplug. 72*4882a593Smuzhiyun */ 73*4882a593Smuzhiyun struct cxl_context *cxl_get_context(struct pci_dev *dev); 74*4882a593Smuzhiyun /* 75*4882a593Smuzhiyun * Allocate and initalise a context associated with a AFU PCI device. This 76*4882a593Smuzhiyun * doesn't start the context in the AFU. 77*4882a593Smuzhiyun */ 78*4882a593Smuzhiyun struct cxl_context *cxl_dev_context_init(struct pci_dev *dev); 79*4882a593Smuzhiyun /* 80*4882a593Smuzhiyun * Release and free a context. Context should be stopped before calling. 81*4882a593Smuzhiyun */ 82*4882a593Smuzhiyun int cxl_release_context(struct cxl_context *ctx); 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun /* 85*4882a593Smuzhiyun * Set and get private data associated with a context. Allows drivers to have a 86*4882a593Smuzhiyun * back pointer to some useful structure. 87*4882a593Smuzhiyun */ 88*4882a593Smuzhiyun int cxl_set_priv(struct cxl_context *ctx, void *priv); 89*4882a593Smuzhiyun void *cxl_get_priv(struct cxl_context *ctx); 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun /* 92*4882a593Smuzhiyun * Allocate AFU interrupts for this context. num=0 will allocate the default 93*4882a593Smuzhiyun * for this AFU as given in the AFU descriptor. This number doesn't include the 94*4882a593Smuzhiyun * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be 95*4882a593Smuzhiyun * used must map a handler with cxl_map_afu_irq. 96*4882a593Smuzhiyun */ 97*4882a593Smuzhiyun int cxl_allocate_afu_irqs(struct cxl_context *cxl, int num); 98*4882a593Smuzhiyun /* Free allocated interrupts */ 99*4882a593Smuzhiyun void cxl_free_afu_irqs(struct cxl_context *cxl); 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun /* 102*4882a593Smuzhiyun * Map a handler for an AFU interrupt associated with a particular context. AFU 103*4882a593Smuzhiyun * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie 104*4882a593Smuzhiyun * is private data is that will be provided to the interrupt handler. 105*4882a593Smuzhiyun */ 106*4882a593Smuzhiyun int cxl_map_afu_irq(struct cxl_context *cxl, int num, 107*4882a593Smuzhiyun irq_handler_t handler, void *cookie, char *name); 108*4882a593Smuzhiyun /* unmap mapped IRQ handlers */ 109*4882a593Smuzhiyun void cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie); 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun /* 112*4882a593Smuzhiyun * Start work on the AFU. This starts an cxl context and associates it with a 113*4882a593Smuzhiyun * task. task == NULL will make it a kernel context. 114*4882a593Smuzhiyun */ 115*4882a593Smuzhiyun int cxl_start_context(struct cxl_context *ctx, u64 wed, 116*4882a593Smuzhiyun struct task_struct *task); 117*4882a593Smuzhiyun /* 118*4882a593Smuzhiyun * Stop a context and remove it from the PSL 119*4882a593Smuzhiyun */ 120*4882a593Smuzhiyun int cxl_stop_context(struct cxl_context *ctx); 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun /* Reset the AFU */ 123*4882a593Smuzhiyun int cxl_afu_reset(struct cxl_context *ctx); 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun /* 126*4882a593Smuzhiyun * Set a context as a master context. 127*4882a593Smuzhiyun * This sets the default problem space area mapped as the full space, rather 128*4882a593Smuzhiyun * than just the per context area (for slaves). 129*4882a593Smuzhiyun */ 130*4882a593Smuzhiyun void cxl_set_master(struct cxl_context *ctx); 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun /* 133*4882a593Smuzhiyun * Map and unmap the AFU Problem Space area. The amount and location mapped 134*4882a593Smuzhiyun * depends on if this context is a master or slave. 135*4882a593Smuzhiyun */ 136*4882a593Smuzhiyun void __iomem *cxl_psa_map(struct cxl_context *ctx); 137*4882a593Smuzhiyun void cxl_psa_unmap(void __iomem *addr); 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun /* Get the process element for this context */ 140*4882a593Smuzhiyun int cxl_process_element(struct cxl_context *ctx); 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun /* 143*4882a593Smuzhiyun * These calls allow drivers to create their own file descriptors and make them 144*4882a593Smuzhiyun * identical to the cxl file descriptor user API. An example use case: 145*4882a593Smuzhiyun * 146*4882a593Smuzhiyun * struct file_operations cxl_my_fops = {}; 147*4882a593Smuzhiyun * ...... 148*4882a593Smuzhiyun * // Init the context 149*4882a593Smuzhiyun * ctx = cxl_dev_context_init(dev); 150*4882a593Smuzhiyun * if (IS_ERR(ctx)) 151*4882a593Smuzhiyun * return PTR_ERR(ctx); 152*4882a593Smuzhiyun * // Create and attach a new file descriptor to my file ops 153*4882a593Smuzhiyun * file = cxl_get_fd(ctx, &cxl_my_fops, &fd); 154*4882a593Smuzhiyun * // Start context 155*4882a593Smuzhiyun * rc = cxl_start_work(ctx, &work.work); 156*4882a593Smuzhiyun * if (rc) { 157*4882a593Smuzhiyun * fput(file); 158*4882a593Smuzhiyun * put_unused_fd(fd); 159*4882a593Smuzhiyun * return -ENODEV; 160*4882a593Smuzhiyun * } 161*4882a593Smuzhiyun * // No error paths after installing the fd 162*4882a593Smuzhiyun * fd_install(fd, file); 163*4882a593Smuzhiyun * return fd; 164*4882a593Smuzhiyun * 165*4882a593Smuzhiyun * This inits a context, and gets a file descriptor and associates some file 166*4882a593Smuzhiyun * ops to that file descriptor. If the file ops are blank, the cxl driver will 167*4882a593Smuzhiyun * fill them in with the default ones that mimic the standard user API. Once 168*4882a593Smuzhiyun * completed, the file descriptor can be installed. Once the file descriptor is 169*4882a593Smuzhiyun * installed, it's visible to the user so no errors must occur past this point. 170*4882a593Smuzhiyun * 171*4882a593Smuzhiyun * If cxl_fd_release() file op call is installed, the context will be stopped 172*4882a593Smuzhiyun * and released when the fd is released. Hence the driver won't need to manage 173*4882a593Smuzhiyun * this itself. 174*4882a593Smuzhiyun */ 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun /* 177*4882a593Smuzhiyun * Take a context and associate it with my file ops. Returns the associated 178*4882a593Smuzhiyun * file and file descriptor. Any file ops which are blank are filled in by the 179*4882a593Smuzhiyun * cxl driver with the default ops to mimic the standard API. 180*4882a593Smuzhiyun */ 181*4882a593Smuzhiyun struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops, 182*4882a593Smuzhiyun int *fd); 183*4882a593Smuzhiyun /* Get the context associated with this file */ 184*4882a593Smuzhiyun struct cxl_context *cxl_fops_get_context(struct file *file); 185*4882a593Smuzhiyun /* 186*4882a593Smuzhiyun * Start a context associated a struct cxl_ioctl_start_work used by the 187*4882a593Smuzhiyun * standard cxl user API. 188*4882a593Smuzhiyun */ 189*4882a593Smuzhiyun int cxl_start_work(struct cxl_context *ctx, 190*4882a593Smuzhiyun struct cxl_ioctl_start_work *work); 191*4882a593Smuzhiyun /* 192*4882a593Smuzhiyun * Export all the existing fops so drivers can use them 193*4882a593Smuzhiyun */ 194*4882a593Smuzhiyun int cxl_fd_open(struct inode *inode, struct file *file); 195*4882a593Smuzhiyun int cxl_fd_release(struct inode *inode, struct file *file); 196*4882a593Smuzhiyun long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 197*4882a593Smuzhiyun int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm); 198*4882a593Smuzhiyun __poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll); 199*4882a593Smuzhiyun ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count, 200*4882a593Smuzhiyun loff_t *off); 201*4882a593Smuzhiyun 202*4882a593Smuzhiyun /* 203*4882a593Smuzhiyun * For EEH, a driver may want to assert a PERST will reload the same image 204*4882a593Smuzhiyun * from flash into the FPGA. 205*4882a593Smuzhiyun * 206*4882a593Smuzhiyun * This is a property of the entire adapter, not a single AFU, so drivers 207*4882a593Smuzhiyun * should set this property with care! 208*4882a593Smuzhiyun */ 209*4882a593Smuzhiyun void cxl_perst_reloads_same_image(struct cxl_afu *afu, 210*4882a593Smuzhiyun bool perst_reloads_same_image); 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun /* 213*4882a593Smuzhiyun * Read the VPD for the card where the AFU resides 214*4882a593Smuzhiyun */ 215*4882a593Smuzhiyun ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count); 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun /* 218*4882a593Smuzhiyun * AFU driver ops allow an AFU driver to create their own events to pass to 219*4882a593Smuzhiyun * userspace through the file descriptor as a simpler alternative to overriding 220*4882a593Smuzhiyun * the read() and poll() calls that works with the generic cxl events. These 221*4882a593Smuzhiyun * events are given priority over the generic cxl events, so they will be 222*4882a593Smuzhiyun * delivered first if multiple types of events are pending. 223*4882a593Smuzhiyun * 224*4882a593Smuzhiyun * The AFU driver must call cxl_context_events_pending() to notify the cxl 225*4882a593Smuzhiyun * driver that new events are ready to be delivered for a specific context. 226*4882a593Smuzhiyun * cxl_context_events_pending() will adjust the current count of AFU driver 227*4882a593Smuzhiyun * events for this context, and wake up anyone waiting on the context wait 228*4882a593Smuzhiyun * queue. 229*4882a593Smuzhiyun * 230*4882a593Smuzhiyun * The cxl driver will then call fetch_event() to get a structure defining 231*4882a593Smuzhiyun * the size and address of the driver specific event data. The cxl driver 232*4882a593Smuzhiyun * will build a cxl header with type and process_element fields filled in, 233*4882a593Smuzhiyun * and header.size set to sizeof(struct cxl_event_header) + data_size. 234*4882a593Smuzhiyun * The total size of the event is limited to CXL_READ_MIN_SIZE (4K). 235*4882a593Smuzhiyun * 236*4882a593Smuzhiyun * fetch_event() is called with a spin lock held, so it must not sleep. 237*4882a593Smuzhiyun * 238*4882a593Smuzhiyun * The cxl driver will then deliver the event to userspace, and finally 239*4882a593Smuzhiyun * call event_delivered() to return the status of the operation, identified 240*4882a593Smuzhiyun * by cxl context and AFU driver event data pointers. 241*4882a593Smuzhiyun * 0 Success 242*4882a593Smuzhiyun * -EFAULT copy_to_user() has failed 243*4882a593Smuzhiyun * -EINVAL Event data pointer is NULL, or event size is greater than 244*4882a593Smuzhiyun * CXL_READ_MIN_SIZE. 245*4882a593Smuzhiyun */ 246*4882a593Smuzhiyun struct cxl_afu_driver_ops { 247*4882a593Smuzhiyun struct cxl_event_afu_driver_reserved *(*fetch_event) ( 248*4882a593Smuzhiyun struct cxl_context *ctx); 249*4882a593Smuzhiyun void (*event_delivered) (struct cxl_context *ctx, 250*4882a593Smuzhiyun struct cxl_event_afu_driver_reserved *event, 251*4882a593Smuzhiyun int rc); 252*4882a593Smuzhiyun }; 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun /* 255*4882a593Smuzhiyun * Associate the above driver ops with a specific context. 256*4882a593Smuzhiyun * Reset the current count of AFU driver events. 257*4882a593Smuzhiyun */ 258*4882a593Smuzhiyun void cxl_set_driver_ops(struct cxl_context *ctx, 259*4882a593Smuzhiyun struct cxl_afu_driver_ops *ops); 260*4882a593Smuzhiyun 261*4882a593Smuzhiyun /* Notify cxl driver that new events are ready to be delivered for context */ 262*4882a593Smuzhiyun void cxl_context_events_pending(struct cxl_context *ctx, 263*4882a593Smuzhiyun unsigned int new_events); 264*4882a593Smuzhiyun 265*4882a593Smuzhiyun #endif /* _MISC_CXL_H */ 266