162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright 2015 IBM Corp. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#ifndef _MISC_CXL_H 762306a36Sopenharmony_ci#define _MISC_CXL_H 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/pci.h> 1062306a36Sopenharmony_ci#include <linux/poll.h> 1162306a36Sopenharmony_ci#include <linux/interrupt.h> 1262306a36Sopenharmony_ci#include <uapi/misc/cxl.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci/* 1562306a36Sopenharmony_ci * This documents the in kernel API for driver to use CXL. It allows kernel 1662306a36Sopenharmony_ci * drivers to bind to AFUs using an AFU configuration record exposed as a PCI 1762306a36Sopenharmony_ci * configuration record. 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * This API enables control over AFU and contexts which can't be part of the 2062306a36Sopenharmony_ci * generic PCI API. This API is agnostic to the actual AFU. 2162306a36Sopenharmony_ci */ 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/* Get the AFU associated with a pci_dev */ 2462306a36Sopenharmony_cistruct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev); 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci/* Get the AFU conf record number associated with a pci_dev */ 2762306a36Sopenharmony_ciunsigned int cxl_pci_to_cfg_record(struct pci_dev *dev); 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/* 3162306a36Sopenharmony_ci * Context lifetime overview: 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * An AFU context may be inited and then started and stopped multiple times 3462306a36Sopenharmony_ci * before it's released. ie. 3562306a36Sopenharmony_ci * - cxl_dev_context_init() 3662306a36Sopenharmony_ci * - cxl_start_context() 3762306a36Sopenharmony_ci * - cxl_stop_context() 3862306a36Sopenharmony_ci * - cxl_start_context() 3962306a36Sopenharmony_ci * - cxl_stop_context() 4062306a36Sopenharmony_ci * ...repeat... 4162306a36Sopenharmony_ci * - cxl_release_context() 4262306a36Sopenharmony_ci * Once released, a context can't be started again. 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * One context is inited by the cxl driver for every pci_dev. This is to be 4562306a36Sopenharmony_ci * used as a default kernel context. cxl_get_context() will get this 4662306a36Sopenharmony_ci * context. This context will be released by PCI hot unplug, so doesn't need to 4762306a36Sopenharmony_ci * be released explicitly by drivers. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * Additional kernel contexts may be inited using cxl_dev_context_init(). 5062306a36Sopenharmony_ci * These must be released using cxl_context_detach(). 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * Once a context has been inited, IRQs may be configured. Firstly these IRQs 5362306a36Sopenharmony_ci * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to 5462306a36Sopenharmony_ci * specific handlers (cxl_map_afu_irq()). 5562306a36Sopenharmony_ci * 5662306a36Sopenharmony_ci * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released 5762306a36Sopenharmony_ci * (cxl_free_afu_irqs()). 5862306a36Sopenharmony_ci * 5962306a36Sopenharmony_ci * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU 6062306a36Sopenharmony_ci * hardware to lose track of all contexts. It's upto the caller of 6162306a36Sopenharmony_ci * cxl_afu_reset() to restart these contexts. 6262306a36Sopenharmony_ci */ 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci/* 6562306a36Sopenharmony_ci * On pci_enabled_device(), the cxl driver will init a single cxl context for 6662306a36Sopenharmony_ci * use by the driver. It doesn't start this context (as that will likely 6762306a36Sopenharmony_ci * generate DMA traffic for most AFUs). 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * This gets the default context associated with this pci_dev. This context 7062306a36Sopenharmony_ci * doesn't need to be released as this will be done by the PCI subsystem on hot 7162306a36Sopenharmony_ci * unplug. 7262306a36Sopenharmony_ci */ 7362306a36Sopenharmony_cistruct cxl_context *cxl_get_context(struct pci_dev *dev); 7462306a36Sopenharmony_ci/* 7562306a36Sopenharmony_ci * Allocate and initalise a context associated with a AFU PCI device. This 7662306a36Sopenharmony_ci * doesn't start the context in the AFU. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_cistruct cxl_context *cxl_dev_context_init(struct pci_dev *dev); 7962306a36Sopenharmony_ci/* 8062306a36Sopenharmony_ci * Release and free a context. Context should be stopped before calling. 8162306a36Sopenharmony_ci */ 8262306a36Sopenharmony_ciint cxl_release_context(struct cxl_context *ctx); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/* 8562306a36Sopenharmony_ci * Set and get private data associated with a context. Allows drivers to have a 8662306a36Sopenharmony_ci * back pointer to some useful structure. 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_ciint cxl_set_priv(struct cxl_context *ctx, void *priv); 8962306a36Sopenharmony_civoid *cxl_get_priv(struct cxl_context *ctx); 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/* 9262306a36Sopenharmony_ci * Allocate AFU interrupts for this context. num=0 will allocate the default 9362306a36Sopenharmony_ci * for this AFU as given in the AFU descriptor. This number doesn't include the 9462306a36Sopenharmony_ci * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be 9562306a36Sopenharmony_ci * used must map a handler with cxl_map_afu_irq. 9662306a36Sopenharmony_ci */ 9762306a36Sopenharmony_ciint cxl_allocate_afu_irqs(struct cxl_context *cxl, int num); 9862306a36Sopenharmony_ci/* Free allocated interrupts */ 9962306a36Sopenharmony_civoid cxl_free_afu_irqs(struct cxl_context *cxl); 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci/* 10262306a36Sopenharmony_ci * Map a handler for an AFU interrupt associated with a particular context. AFU 10362306a36Sopenharmony_ci * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie 10462306a36Sopenharmony_ci * is private data is that will be provided to the interrupt handler. 10562306a36Sopenharmony_ci */ 10662306a36Sopenharmony_ciint cxl_map_afu_irq(struct cxl_context *cxl, int num, 10762306a36Sopenharmony_ci irq_handler_t handler, void *cookie, char *name); 10862306a36Sopenharmony_ci/* unmap mapped IRQ handlers */ 10962306a36Sopenharmony_civoid cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/* 11262306a36Sopenharmony_ci * Start work on the AFU. This starts an cxl context and associates it with a 11362306a36Sopenharmony_ci * task. task == NULL will make it a kernel context. 11462306a36Sopenharmony_ci */ 11562306a36Sopenharmony_ciint cxl_start_context(struct cxl_context *ctx, u64 wed, 11662306a36Sopenharmony_ci struct task_struct *task); 11762306a36Sopenharmony_ci/* 11862306a36Sopenharmony_ci * Stop a context and remove it from the PSL 11962306a36Sopenharmony_ci */ 12062306a36Sopenharmony_ciint cxl_stop_context(struct cxl_context *ctx); 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci/* Reset the AFU */ 12362306a36Sopenharmony_ciint cxl_afu_reset(struct cxl_context *ctx); 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci/* 12662306a36Sopenharmony_ci * Set a context as a master context. 12762306a36Sopenharmony_ci * This sets the default problem space area mapped as the full space, rather 12862306a36Sopenharmony_ci * than just the per context area (for slaves). 12962306a36Sopenharmony_ci */ 13062306a36Sopenharmony_civoid cxl_set_master(struct cxl_context *ctx); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci/* 13362306a36Sopenharmony_ci * Map and unmap the AFU Problem Space area. The amount and location mapped 13462306a36Sopenharmony_ci * depends on if this context is a master or slave. 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_civoid __iomem *cxl_psa_map(struct cxl_context *ctx); 13762306a36Sopenharmony_civoid cxl_psa_unmap(void __iomem *addr); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/* Get the process element for this context */ 14062306a36Sopenharmony_ciint cxl_process_element(struct cxl_context *ctx); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci/* 14362306a36Sopenharmony_ci * These calls allow drivers to create their own file descriptors and make them 14462306a36Sopenharmony_ci * identical to the cxl file descriptor user API. An example use case: 14562306a36Sopenharmony_ci * 14662306a36Sopenharmony_ci * struct file_operations cxl_my_fops = {}; 14762306a36Sopenharmony_ci * ...... 14862306a36Sopenharmony_ci * // Init the context 14962306a36Sopenharmony_ci * ctx = cxl_dev_context_init(dev); 15062306a36Sopenharmony_ci * if (IS_ERR(ctx)) 15162306a36Sopenharmony_ci * return PTR_ERR(ctx); 15262306a36Sopenharmony_ci * // Create and attach a new file descriptor to my file ops 15362306a36Sopenharmony_ci * file = cxl_get_fd(ctx, &cxl_my_fops, &fd); 15462306a36Sopenharmony_ci * // Start context 15562306a36Sopenharmony_ci * rc = cxl_start_work(ctx, &work.work); 15662306a36Sopenharmony_ci * if (rc) { 15762306a36Sopenharmony_ci * fput(file); 15862306a36Sopenharmony_ci * put_unused_fd(fd); 15962306a36Sopenharmony_ci * return -ENODEV; 16062306a36Sopenharmony_ci * } 16162306a36Sopenharmony_ci * // No error paths after installing the fd 16262306a36Sopenharmony_ci * fd_install(fd, file); 16362306a36Sopenharmony_ci * return fd; 16462306a36Sopenharmony_ci * 16562306a36Sopenharmony_ci * This inits a context, and gets a file descriptor and associates some file 16662306a36Sopenharmony_ci * ops to that file descriptor. If the file ops are blank, the cxl driver will 16762306a36Sopenharmony_ci * fill them in with the default ones that mimic the standard user API. Once 16862306a36Sopenharmony_ci * completed, the file descriptor can be installed. Once the file descriptor is 16962306a36Sopenharmony_ci * installed, it's visible to the user so no errors must occur past this point. 17062306a36Sopenharmony_ci * 17162306a36Sopenharmony_ci * If cxl_fd_release() file op call is installed, the context will be stopped 17262306a36Sopenharmony_ci * and released when the fd is released. Hence the driver won't need to manage 17362306a36Sopenharmony_ci * this itself. 17462306a36Sopenharmony_ci */ 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci/* 17762306a36Sopenharmony_ci * Take a context and associate it with my file ops. Returns the associated 17862306a36Sopenharmony_ci * file and file descriptor. Any file ops which are blank are filled in by the 17962306a36Sopenharmony_ci * cxl driver with the default ops to mimic the standard API. 18062306a36Sopenharmony_ci */ 18162306a36Sopenharmony_cistruct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops, 18262306a36Sopenharmony_ci int *fd); 18362306a36Sopenharmony_ci/* Get the context associated with this file */ 18462306a36Sopenharmony_cistruct cxl_context *cxl_fops_get_context(struct file *file); 18562306a36Sopenharmony_ci/* 18662306a36Sopenharmony_ci * Start a context associated a struct cxl_ioctl_start_work used by the 18762306a36Sopenharmony_ci * standard cxl user API. 18862306a36Sopenharmony_ci */ 18962306a36Sopenharmony_ciint cxl_start_work(struct cxl_context *ctx, 19062306a36Sopenharmony_ci struct cxl_ioctl_start_work *work); 19162306a36Sopenharmony_ci/* 19262306a36Sopenharmony_ci * Export all the existing fops so drivers can use them 19362306a36Sopenharmony_ci */ 19462306a36Sopenharmony_ciint cxl_fd_open(struct inode *inode, struct file *file); 19562306a36Sopenharmony_ciint cxl_fd_release(struct inode *inode, struct file *file); 19662306a36Sopenharmony_cilong cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 19762306a36Sopenharmony_ciint cxl_fd_mmap(struct file *file, struct vm_area_struct *vm); 19862306a36Sopenharmony_ci__poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll); 19962306a36Sopenharmony_cissize_t cxl_fd_read(struct file *file, char __user *buf, size_t count, 20062306a36Sopenharmony_ci loff_t *off); 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci/* 20362306a36Sopenharmony_ci * For EEH, a driver may want to assert a PERST will reload the same image 20462306a36Sopenharmony_ci * from flash into the FPGA. 20562306a36Sopenharmony_ci * 20662306a36Sopenharmony_ci * This is a property of the entire adapter, not a single AFU, so drivers 20762306a36Sopenharmony_ci * should set this property with care! 20862306a36Sopenharmony_ci */ 20962306a36Sopenharmony_civoid cxl_perst_reloads_same_image(struct cxl_afu *afu, 21062306a36Sopenharmony_ci bool perst_reloads_same_image); 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci/* 21362306a36Sopenharmony_ci * Read the VPD for the card where the AFU resides 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_cissize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count); 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci/* 21862306a36Sopenharmony_ci * AFU driver ops allow an AFU driver to create their own events to pass to 21962306a36Sopenharmony_ci * userspace through the file descriptor as a simpler alternative to overriding 22062306a36Sopenharmony_ci * the read() and poll() calls that works with the generic cxl events. These 22162306a36Sopenharmony_ci * events are given priority over the generic cxl events, so they will be 22262306a36Sopenharmony_ci * delivered first if multiple types of events are pending. 22362306a36Sopenharmony_ci * 22462306a36Sopenharmony_ci * The AFU driver must call cxl_context_events_pending() to notify the cxl 22562306a36Sopenharmony_ci * driver that new events are ready to be delivered for a specific context. 22662306a36Sopenharmony_ci * cxl_context_events_pending() will adjust the current count of AFU driver 22762306a36Sopenharmony_ci * events for this context, and wake up anyone waiting on the context wait 22862306a36Sopenharmony_ci * queue. 22962306a36Sopenharmony_ci * 23062306a36Sopenharmony_ci * The cxl driver will then call fetch_event() to get a structure defining 23162306a36Sopenharmony_ci * the size and address of the driver specific event data. The cxl driver 23262306a36Sopenharmony_ci * will build a cxl header with type and process_element fields filled in, 23362306a36Sopenharmony_ci * and header.size set to sizeof(struct cxl_event_header) + data_size. 23462306a36Sopenharmony_ci * The total size of the event is limited to CXL_READ_MIN_SIZE (4K). 23562306a36Sopenharmony_ci * 23662306a36Sopenharmony_ci * fetch_event() is called with a spin lock held, so it must not sleep. 23762306a36Sopenharmony_ci * 23862306a36Sopenharmony_ci * The cxl driver will then deliver the event to userspace, and finally 23962306a36Sopenharmony_ci * call event_delivered() to return the status of the operation, identified 24062306a36Sopenharmony_ci * by cxl context and AFU driver event data pointers. 24162306a36Sopenharmony_ci * 0 Success 24262306a36Sopenharmony_ci * -EFAULT copy_to_user() has failed 24362306a36Sopenharmony_ci * -EINVAL Event data pointer is NULL, or event size is greater than 24462306a36Sopenharmony_ci * CXL_READ_MIN_SIZE. 24562306a36Sopenharmony_ci */ 24662306a36Sopenharmony_cistruct cxl_afu_driver_ops { 24762306a36Sopenharmony_ci struct cxl_event_afu_driver_reserved *(*fetch_event) ( 24862306a36Sopenharmony_ci struct cxl_context *ctx); 24962306a36Sopenharmony_ci void (*event_delivered) (struct cxl_context *ctx, 25062306a36Sopenharmony_ci struct cxl_event_afu_driver_reserved *event, 25162306a36Sopenharmony_ci int rc); 25262306a36Sopenharmony_ci}; 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci/* 25562306a36Sopenharmony_ci * Associate the above driver ops with a specific context. 25662306a36Sopenharmony_ci * Reset the current count of AFU driver events. 25762306a36Sopenharmony_ci */ 25862306a36Sopenharmony_civoid cxl_set_driver_ops(struct cxl_context *ctx, 25962306a36Sopenharmony_ci struct cxl_afu_driver_ops *ops); 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci/* Notify cxl driver that new events are ready to be delivered for context */ 26262306a36Sopenharmony_civoid cxl_context_events_pending(struct cxl_context *ctx, 26362306a36Sopenharmony_ci unsigned int new_events); 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci#endif /* _MISC_CXL_H */ 266