18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2015 IBM Corp.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef _MISC_CXL_H
78c2ecf20Sopenharmony_ci#define _MISC_CXL_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/pci.h>
108c2ecf20Sopenharmony_ci#include <linux/poll.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <uapi/misc/cxl.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * This documents the in kernel API for driver to use CXL. It allows kernel
168c2ecf20Sopenharmony_ci * drivers to bind to AFUs using an AFU configuration record exposed as a PCI
178c2ecf20Sopenharmony_ci * configuration record.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * This API enables control over AFU and contexts which can't be part of the
208c2ecf20Sopenharmony_ci * generic PCI API. This API is agnostic to the actual AFU.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Get the AFU associated with a pci_dev */
248c2ecf20Sopenharmony_cistruct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Get the AFU conf record number associated with a pci_dev */
278c2ecf20Sopenharmony_ciunsigned int cxl_pci_to_cfg_record(struct pci_dev *dev);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Context lifetime overview:
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * An AFU context may be inited and then started and stoppped multiple times
348c2ecf20Sopenharmony_ci * before it's released. ie.
358c2ecf20Sopenharmony_ci *    - cxl_dev_context_init()
368c2ecf20Sopenharmony_ci *      - cxl_start_context()
378c2ecf20Sopenharmony_ci *      - cxl_stop_context()
388c2ecf20Sopenharmony_ci *      - cxl_start_context()
398c2ecf20Sopenharmony_ci *      - cxl_stop_context()
408c2ecf20Sopenharmony_ci *     ...repeat...
418c2ecf20Sopenharmony_ci *    - cxl_release_context()
428c2ecf20Sopenharmony_ci * Once released, a context can't be started again.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * One context is inited by the cxl driver for every pci_dev. This is to be
458c2ecf20Sopenharmony_ci * used as a default kernel context. cxl_get_context() will get this
468c2ecf20Sopenharmony_ci * context. This context will be released by PCI hot unplug, so doesn't need to
478c2ecf20Sopenharmony_ci * be released explicitly by drivers.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * Additional kernel contexts may be inited using cxl_dev_context_init().
508c2ecf20Sopenharmony_ci * These must be released using cxl_context_detach().
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * Once a context has been inited, IRQs may be configured. Firstly these IRQs
538c2ecf20Sopenharmony_ci * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to
548c2ecf20Sopenharmony_ci * specific handlers (cxl_map_afu_irq()).
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released
578c2ecf20Sopenharmony_ci * (cxl_free_afu_irqs()).
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU
608c2ecf20Sopenharmony_ci * hardware to lose track of all contexts. It's upto the caller of
618c2ecf20Sopenharmony_ci * cxl_afu_reset() to restart these contexts.
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/*
658c2ecf20Sopenharmony_ci * On pci_enabled_device(), the cxl driver will init a single cxl context for
668c2ecf20Sopenharmony_ci * use by the driver. It doesn't start this context (as that will likely
678c2ecf20Sopenharmony_ci * generate DMA traffic for most AFUs).
688c2ecf20Sopenharmony_ci *
698c2ecf20Sopenharmony_ci * This gets the default context associated with this pci_dev.  This context
708c2ecf20Sopenharmony_ci * doesn't need to be released as this will be done by the PCI subsystem on hot
718c2ecf20Sopenharmony_ci * unplug.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_cistruct cxl_context *cxl_get_context(struct pci_dev *dev);
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * Allocate and initalise a context associated with a AFU PCI device. This
768c2ecf20Sopenharmony_ci * doesn't start the context in the AFU.
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_cistruct cxl_context *cxl_dev_context_init(struct pci_dev *dev);
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Release and free a context. Context should be stopped before calling.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ciint cxl_release_context(struct cxl_context *ctx);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * Set and get private data associated with a context. Allows drivers to have a
868c2ecf20Sopenharmony_ci * back pointer to some useful structure.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_ciint cxl_set_priv(struct cxl_context *ctx, void *priv);
898c2ecf20Sopenharmony_civoid *cxl_get_priv(struct cxl_context *ctx);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/*
928c2ecf20Sopenharmony_ci * Allocate AFU interrupts for this context. num=0 will allocate the default
938c2ecf20Sopenharmony_ci * for this AFU as given in the AFU descriptor. This number doesn't include the
948c2ecf20Sopenharmony_ci * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be
958c2ecf20Sopenharmony_ci * used must map a handler with cxl_map_afu_irq.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ciint cxl_allocate_afu_irqs(struct cxl_context *cxl, int num);
988c2ecf20Sopenharmony_ci/* Free allocated interrupts */
998c2ecf20Sopenharmony_civoid cxl_free_afu_irqs(struct cxl_context *cxl);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * Map a handler for an AFU interrupt associated with a particular context. AFU
1038c2ecf20Sopenharmony_ci * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie
1048c2ecf20Sopenharmony_ci * is private data is that will be provided to the interrupt handler.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ciint cxl_map_afu_irq(struct cxl_context *cxl, int num,
1078c2ecf20Sopenharmony_ci		    irq_handler_t handler, void *cookie, char *name);
1088c2ecf20Sopenharmony_ci/* unmap mapped IRQ handlers */
1098c2ecf20Sopenharmony_civoid cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci * Start work on the AFU. This starts an cxl context and associates it with a
1138c2ecf20Sopenharmony_ci * task. task == NULL will make it a kernel context.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_ciint cxl_start_context(struct cxl_context *ctx, u64 wed,
1168c2ecf20Sopenharmony_ci		      struct task_struct *task);
1178c2ecf20Sopenharmony_ci/*
1188c2ecf20Sopenharmony_ci * Stop a context and remove it from the PSL
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_ciint cxl_stop_context(struct cxl_context *ctx);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/* Reset the AFU */
1238c2ecf20Sopenharmony_ciint cxl_afu_reset(struct cxl_context *ctx);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/*
1268c2ecf20Sopenharmony_ci * Set a context as a master context.
1278c2ecf20Sopenharmony_ci * This sets the default problem space area mapped as the full space, rather
1288c2ecf20Sopenharmony_ci * than just the per context area (for slaves).
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_civoid cxl_set_master(struct cxl_context *ctx);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*
1338c2ecf20Sopenharmony_ci * Map and unmap the AFU Problem Space area. The amount and location mapped
1348c2ecf20Sopenharmony_ci * depends on if this context is a master or slave.
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_civoid __iomem *cxl_psa_map(struct cxl_context *ctx);
1378c2ecf20Sopenharmony_civoid cxl_psa_unmap(void __iomem *addr);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/*  Get the process element for this context */
1408c2ecf20Sopenharmony_ciint cxl_process_element(struct cxl_context *ctx);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/*
1438c2ecf20Sopenharmony_ci * These calls allow drivers to create their own file descriptors and make them
1448c2ecf20Sopenharmony_ci * identical to the cxl file descriptor user API. An example use case:
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * struct file_operations cxl_my_fops = {};
1478c2ecf20Sopenharmony_ci * ......
1488c2ecf20Sopenharmony_ci *	// Init the context
1498c2ecf20Sopenharmony_ci *	ctx = cxl_dev_context_init(dev);
1508c2ecf20Sopenharmony_ci *	if (IS_ERR(ctx))
1518c2ecf20Sopenharmony_ci *		return PTR_ERR(ctx);
1528c2ecf20Sopenharmony_ci *	// Create and attach a new file descriptor to my file ops
1538c2ecf20Sopenharmony_ci *	file = cxl_get_fd(ctx, &cxl_my_fops, &fd);
1548c2ecf20Sopenharmony_ci *	// Start context
1558c2ecf20Sopenharmony_ci *	rc = cxl_start_work(ctx, &work.work);
1568c2ecf20Sopenharmony_ci *	if (rc) {
1578c2ecf20Sopenharmony_ci *		fput(file);
1588c2ecf20Sopenharmony_ci *		put_unused_fd(fd);
1598c2ecf20Sopenharmony_ci *		return -ENODEV;
1608c2ecf20Sopenharmony_ci *	}
1618c2ecf20Sopenharmony_ci *	// No error paths after installing the fd
1628c2ecf20Sopenharmony_ci *	fd_install(fd, file);
1638c2ecf20Sopenharmony_ci *	return fd;
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * This inits a context, and gets a file descriptor and associates some file
1668c2ecf20Sopenharmony_ci * ops to that file descriptor. If the file ops are blank, the cxl driver will
1678c2ecf20Sopenharmony_ci * fill them in with the default ones that mimic the standard user API.  Once
1688c2ecf20Sopenharmony_ci * completed, the file descriptor can be installed. Once the file descriptor is
1698c2ecf20Sopenharmony_ci * installed, it's visible to the user so no errors must occur past this point.
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * If cxl_fd_release() file op call is installed, the context will be stopped
1728c2ecf20Sopenharmony_ci * and released when the fd is released. Hence the driver won't need to manage
1738c2ecf20Sopenharmony_ci * this itself.
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/*
1778c2ecf20Sopenharmony_ci * Take a context and associate it with my file ops. Returns the associated
1788c2ecf20Sopenharmony_ci * file and file descriptor. Any file ops which are blank are filled in by the
1798c2ecf20Sopenharmony_ci * cxl driver with the default ops to mimic the standard API.
1808c2ecf20Sopenharmony_ci */
1818c2ecf20Sopenharmony_cistruct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
1828c2ecf20Sopenharmony_ci			int *fd);
1838c2ecf20Sopenharmony_ci/* Get the context associated with this file */
1848c2ecf20Sopenharmony_cistruct cxl_context *cxl_fops_get_context(struct file *file);
1858c2ecf20Sopenharmony_ci/*
1868c2ecf20Sopenharmony_ci * Start a context associated a struct cxl_ioctl_start_work used by the
1878c2ecf20Sopenharmony_ci * standard cxl user API.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_ciint cxl_start_work(struct cxl_context *ctx,
1908c2ecf20Sopenharmony_ci		   struct cxl_ioctl_start_work *work);
1918c2ecf20Sopenharmony_ci/*
1928c2ecf20Sopenharmony_ci * Export all the existing fops so drivers can use them
1938c2ecf20Sopenharmony_ci */
1948c2ecf20Sopenharmony_ciint cxl_fd_open(struct inode *inode, struct file *file);
1958c2ecf20Sopenharmony_ciint cxl_fd_release(struct inode *inode, struct file *file);
1968c2ecf20Sopenharmony_cilong cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1978c2ecf20Sopenharmony_ciint cxl_fd_mmap(struct file *file, struct vm_area_struct *vm);
1988c2ecf20Sopenharmony_ci__poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll);
1998c2ecf20Sopenharmony_cissize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
2008c2ecf20Sopenharmony_ci			   loff_t *off);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci/*
2038c2ecf20Sopenharmony_ci * For EEH, a driver may want to assert a PERST will reload the same image
2048c2ecf20Sopenharmony_ci * from flash into the FPGA.
2058c2ecf20Sopenharmony_ci *
2068c2ecf20Sopenharmony_ci * This is a property of the entire adapter, not a single AFU, so drivers
2078c2ecf20Sopenharmony_ci * should set this property with care!
2088c2ecf20Sopenharmony_ci */
2098c2ecf20Sopenharmony_civoid cxl_perst_reloads_same_image(struct cxl_afu *afu,
2108c2ecf20Sopenharmony_ci				  bool perst_reloads_same_image);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * Read the VPD for the card where the AFU resides
2148c2ecf20Sopenharmony_ci */
2158c2ecf20Sopenharmony_cissize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/*
2188c2ecf20Sopenharmony_ci * AFU driver ops allow an AFU driver to create their own events to pass to
2198c2ecf20Sopenharmony_ci * userspace through the file descriptor as a simpler alternative to overriding
2208c2ecf20Sopenharmony_ci * the read() and poll() calls that works with the generic cxl events. These
2218c2ecf20Sopenharmony_ci * events are given priority over the generic cxl events, so they will be
2228c2ecf20Sopenharmony_ci * delivered first if multiple types of events are pending.
2238c2ecf20Sopenharmony_ci *
2248c2ecf20Sopenharmony_ci * The AFU driver must call cxl_context_events_pending() to notify the cxl
2258c2ecf20Sopenharmony_ci * driver that new events are ready to be delivered for a specific context.
2268c2ecf20Sopenharmony_ci * cxl_context_events_pending() will adjust the current count of AFU driver
2278c2ecf20Sopenharmony_ci * events for this context, and wake up anyone waiting on the context wait
2288c2ecf20Sopenharmony_ci * queue.
2298c2ecf20Sopenharmony_ci *
2308c2ecf20Sopenharmony_ci * The cxl driver will then call fetch_event() to get a structure defining
2318c2ecf20Sopenharmony_ci * the size and address of the driver specific event data. The cxl driver
2328c2ecf20Sopenharmony_ci * will build a cxl header with type and process_element fields filled in,
2338c2ecf20Sopenharmony_ci * and header.size set to sizeof(struct cxl_event_header) + data_size.
2348c2ecf20Sopenharmony_ci * The total size of the event is limited to CXL_READ_MIN_SIZE (4K).
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * fetch_event() is called with a spin lock held, so it must not sleep.
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * The cxl driver will then deliver the event to userspace, and finally
2398c2ecf20Sopenharmony_ci * call event_delivered() to return the status of the operation, identified
2408c2ecf20Sopenharmony_ci * by cxl context and AFU driver event data pointers.
2418c2ecf20Sopenharmony_ci *   0        Success
2428c2ecf20Sopenharmony_ci *   -EFAULT  copy_to_user() has failed
2438c2ecf20Sopenharmony_ci *   -EINVAL  Event data pointer is NULL, or event size is greater than
2448c2ecf20Sopenharmony_ci *            CXL_READ_MIN_SIZE.
2458c2ecf20Sopenharmony_ci */
2468c2ecf20Sopenharmony_cistruct cxl_afu_driver_ops {
2478c2ecf20Sopenharmony_ci	struct cxl_event_afu_driver_reserved *(*fetch_event) (
2488c2ecf20Sopenharmony_ci						struct cxl_context *ctx);
2498c2ecf20Sopenharmony_ci	void (*event_delivered) (struct cxl_context *ctx,
2508c2ecf20Sopenharmony_ci				 struct cxl_event_afu_driver_reserved *event,
2518c2ecf20Sopenharmony_ci				 int rc);
2528c2ecf20Sopenharmony_ci};
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/*
2558c2ecf20Sopenharmony_ci * Associate the above driver ops with a specific context.
2568c2ecf20Sopenharmony_ci * Reset the current count of AFU driver events.
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_civoid cxl_set_driver_ops(struct cxl_context *ctx,
2598c2ecf20Sopenharmony_ci			struct cxl_afu_driver_ops *ops);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/* Notify cxl driver that new events are ready to be delivered for context */
2628c2ecf20Sopenharmony_civoid cxl_context_events_pending(struct cxl_context *ctx,
2638c2ecf20Sopenharmony_ci				unsigned int new_events);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci#endif /* _MISC_CXL_H */
266