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