Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright 2015 IBM Corp.
0004  */
0005 
0006 #ifndef _MISC_CXL_H
0007 #define _MISC_CXL_H
0008 
0009 #include <linux/pci.h>
0010 #include <linux/poll.h>
0011 #include <linux/interrupt.h>
0012 #include <uapi/misc/cxl.h>
0013 
0014 /*
0015  * This documents the in kernel API for driver to use CXL. It allows kernel
0016  * drivers to bind to AFUs using an AFU configuration record exposed as a PCI
0017  * configuration record.
0018  *
0019  * This API enables control over AFU and contexts which can't be part of the
0020  * generic PCI API. This API is agnostic to the actual AFU.
0021  */
0022 
0023 /* Get the AFU associated with a pci_dev */
0024 struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev);
0025 
0026 /* Get the AFU conf record number associated with a pci_dev */
0027 unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev);
0028 
0029 
0030 /*
0031  * Context lifetime overview:
0032  *
0033  * An AFU context may be inited and then started and stoppped multiple times
0034  * before it's released. ie.
0035  *    - cxl_dev_context_init()
0036  *      - cxl_start_context()
0037  *      - cxl_stop_context()
0038  *      - cxl_start_context()
0039  *      - cxl_stop_context()
0040  *     ...repeat...
0041  *    - cxl_release_context()
0042  * Once released, a context can't be started again.
0043  *
0044  * One context is inited by the cxl driver for every pci_dev. This is to be
0045  * used as a default kernel context. cxl_get_context() will get this
0046  * context. This context will be released by PCI hot unplug, so doesn't need to
0047  * be released explicitly by drivers.
0048  *
0049  * Additional kernel contexts may be inited using cxl_dev_context_init().
0050  * These must be released using cxl_context_detach().
0051  *
0052  * Once a context has been inited, IRQs may be configured. Firstly these IRQs
0053  * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to
0054  * specific handlers (cxl_map_afu_irq()).
0055  *
0056  * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released
0057  * (cxl_free_afu_irqs()).
0058  *
0059  * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU
0060  * hardware to lose track of all contexts. It's upto the caller of
0061  * cxl_afu_reset() to restart these contexts.
0062  */
0063 
0064 /*
0065  * On pci_enabled_device(), the cxl driver will init a single cxl context for
0066  * use by the driver. It doesn't start this context (as that will likely
0067  * generate DMA traffic for most AFUs).
0068  *
0069  * This gets the default context associated with this pci_dev.  This context
0070  * doesn't need to be released as this will be done by the PCI subsystem on hot
0071  * unplug.
0072  */
0073 struct cxl_context *cxl_get_context(struct pci_dev *dev);
0074 /*
0075  * Allocate and initalise a context associated with a AFU PCI device. This
0076  * doesn't start the context in the AFU.
0077  */
0078 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev);
0079 /*
0080  * Release and free a context. Context should be stopped before calling.
0081  */
0082 int cxl_release_context(struct cxl_context *ctx);
0083 
0084 /*
0085  * Set and get private data associated with a context. Allows drivers to have a
0086  * back pointer to some useful structure.
0087  */
0088 int cxl_set_priv(struct cxl_context *ctx, void *priv);
0089 void *cxl_get_priv(struct cxl_context *ctx);
0090 
0091 /*
0092  * Allocate AFU interrupts for this context. num=0 will allocate the default
0093  * for this AFU as given in the AFU descriptor. This number doesn't include the
0094  * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be
0095  * used must map a handler with cxl_map_afu_irq.
0096  */
0097 int cxl_allocate_afu_irqs(struct cxl_context *cxl, int num);
0098 /* Free allocated interrupts */
0099 void cxl_free_afu_irqs(struct cxl_context *cxl);
0100 
0101 /*
0102  * Map a handler for an AFU interrupt associated with a particular context. AFU
0103  * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie
0104  * is private data is that will be provided to the interrupt handler.
0105  */
0106 int cxl_map_afu_irq(struct cxl_context *cxl, int num,
0107             irq_handler_t handler, void *cookie, char *name);
0108 /* unmap mapped IRQ handlers */
0109 void cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie);
0110 
0111 /*
0112  * Start work on the AFU. This starts an cxl context and associates it with a
0113  * task. task == NULL will make it a kernel context.
0114  */
0115 int cxl_start_context(struct cxl_context *ctx, u64 wed,
0116               struct task_struct *task);
0117 /*
0118  * Stop a context and remove it from the PSL
0119  */
0120 int cxl_stop_context(struct cxl_context *ctx);
0121 
0122 /* Reset the AFU */
0123 int cxl_afu_reset(struct cxl_context *ctx);
0124 
0125 /*
0126  * Set a context as a master context.
0127  * This sets the default problem space area mapped as the full space, rather
0128  * than just the per context area (for slaves).
0129  */
0130 void cxl_set_master(struct cxl_context *ctx);
0131 
0132 /*
0133  * Map and unmap the AFU Problem Space area. The amount and location mapped
0134  * depends on if this context is a master or slave.
0135  */
0136 void __iomem *cxl_psa_map(struct cxl_context *ctx);
0137 void cxl_psa_unmap(void __iomem *addr);
0138 
0139 /*  Get the process element for this context */
0140 int cxl_process_element(struct cxl_context *ctx);
0141 
0142 /*
0143  * These calls allow drivers to create their own file descriptors and make them
0144  * identical to the cxl file descriptor user API. An example use case:
0145  *
0146  * struct file_operations cxl_my_fops = {};
0147  * ......
0148  *  // Init the context
0149  *  ctx = cxl_dev_context_init(dev);
0150  *  if (IS_ERR(ctx))
0151  *      return PTR_ERR(ctx);
0152  *  // Create and attach a new file descriptor to my file ops
0153  *  file = cxl_get_fd(ctx, &cxl_my_fops, &fd);
0154  *  // Start context
0155  *  rc = cxl_start_work(ctx, &work.work);
0156  *  if (rc) {
0157  *      fput(file);
0158  *      put_unused_fd(fd);
0159  *      return -ENODEV;
0160  *  }
0161  *  // No error paths after installing the fd
0162  *  fd_install(fd, file);
0163  *  return fd;
0164  *
0165  * This inits a context, and gets a file descriptor and associates some file
0166  * ops to that file descriptor. If the file ops are blank, the cxl driver will
0167  * fill them in with the default ones that mimic the standard user API.  Once
0168  * completed, the file descriptor can be installed. Once the file descriptor is
0169  * installed, it's visible to the user so no errors must occur past this point.
0170  *
0171  * If cxl_fd_release() file op call is installed, the context will be stopped
0172  * and released when the fd is released. Hence the driver won't need to manage
0173  * this itself.
0174  */
0175 
0176 /*
0177  * Take a context and associate it with my file ops. Returns the associated
0178  * file and file descriptor. Any file ops which are blank are filled in by the
0179  * cxl driver with the default ops to mimic the standard API.
0180  */
0181 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
0182             int *fd);
0183 /* Get the context associated with this file */
0184 struct cxl_context *cxl_fops_get_context(struct file *file);
0185 /*
0186  * Start a context associated a struct cxl_ioctl_start_work used by the
0187  * standard cxl user API.
0188  */
0189 int cxl_start_work(struct cxl_context *ctx,
0190            struct cxl_ioctl_start_work *work);
0191 /*
0192  * Export all the existing fops so drivers can use them
0193  */
0194 int cxl_fd_open(struct inode *inode, struct file *file);
0195 int cxl_fd_release(struct inode *inode, struct file *file);
0196 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
0197 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm);
0198 __poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll);
0199 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
0200                loff_t *off);
0201 
0202 /*
0203  * For EEH, a driver may want to assert a PERST will reload the same image
0204  * from flash into the FPGA.
0205  *
0206  * This is a property of the entire adapter, not a single AFU, so drivers
0207  * should set this property with care!
0208  */
0209 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
0210                   bool perst_reloads_same_image);
0211 
0212 /*
0213  * Read the VPD for the card where the AFU resides
0214  */
0215 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
0216 
0217 /*
0218  * AFU driver ops allow an AFU driver to create their own events to pass to
0219  * userspace through the file descriptor as a simpler alternative to overriding
0220  * the read() and poll() calls that works with the generic cxl events. These
0221  * events are given priority over the generic cxl events, so they will be
0222  * delivered first if multiple types of events are pending.
0223  *
0224  * The AFU driver must call cxl_context_events_pending() to notify the cxl
0225  * driver that new events are ready to be delivered for a specific context.
0226  * cxl_context_events_pending() will adjust the current count of AFU driver
0227  * events for this context, and wake up anyone waiting on the context wait
0228  * queue.
0229  *
0230  * The cxl driver will then call fetch_event() to get a structure defining
0231  * the size and address of the driver specific event data. The cxl driver
0232  * will build a cxl header with type and process_element fields filled in,
0233  * and header.size set to sizeof(struct cxl_event_header) + data_size.
0234  * The total size of the event is limited to CXL_READ_MIN_SIZE (4K).
0235  *
0236  * fetch_event() is called with a spin lock held, so it must not sleep.
0237  *
0238  * The cxl driver will then deliver the event to userspace, and finally
0239  * call event_delivered() to return the status of the operation, identified
0240  * by cxl context and AFU driver event data pointers.
0241  *   0        Success
0242  *   -EFAULT  copy_to_user() has failed
0243  *   -EINVAL  Event data pointer is NULL, or event size is greater than
0244  *            CXL_READ_MIN_SIZE.
0245  */
0246 struct cxl_afu_driver_ops {
0247     struct cxl_event_afu_driver_reserved *(*fetch_event) (
0248                         struct cxl_context *ctx);
0249     void (*event_delivered) (struct cxl_context *ctx,
0250                  struct cxl_event_afu_driver_reserved *event,
0251                  int rc);
0252 };
0253 
0254 /*
0255  * Associate the above driver ops with a specific context.
0256  * Reset the current count of AFU driver events.
0257  */
0258 void cxl_set_driver_ops(struct cxl_context *ctx,
0259             struct cxl_afu_driver_ops *ops);
0260 
0261 /* Notify cxl driver that new events are ready to be delivered for context */
0262 void cxl_context_events_pending(struct cxl_context *ctx,
0263                 unsigned int new_events);
0264 
0265 #endif /* _MISC_CXL_H */