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