]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/misc/cxl.h
cxl: Add preliminary workaround for CX4 interrupt limitation
[mirror_ubuntu-bionic-kernel.git] / include / misc / cxl.h
CommitLineData
6f7f0b3d
MN
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
4e56f858
IM
27#define CXL_SLOT_FLAG_DMA 0x1
28
29/*
30 * Checks if the given card is in a cxl capable slot. Pass CXL_SLOT_FLAG_DMA if
31 * the card requires CAPP DMA mode to also check if the system supports it.
32 * This is intended to be used by bi-modal devices to determine if they can use
33 * cxl mode or if they should continue running in PCI mode.
34 *
35 * Note that this only checks if the slot is cxl capable - it does not
36 * currently check if the CAPP is currently available for chips where it can be
37 * assigned to different PHBs on a first come first serve basis (i.e. P8)
38 */
39bool cxl_slot_is_supported(struct pci_dev *dev, int flags);
40
41
6f7f0b3d
MN
42/* Get the AFU associated with a pci_dev */
43struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev);
44
45/* Get the AFU conf record number associated with a pci_dev */
46unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev);
47
6f7f0b3d
MN
48
49/*
50 * Context lifetime overview:
51 *
52 * An AFU context may be inited and then started and stoppped multiple times
53 * before it's released. ie.
54 * - cxl_dev_context_init()
55 * - cxl_start_context()
56 * - cxl_stop_context()
57 * - cxl_start_context()
58 * - cxl_stop_context()
59 * ...repeat...
60 * - cxl_release_context()
61 * Once released, a context can't be started again.
62 *
63 * One context is inited by the cxl driver for every pci_dev. This is to be
64 * used as a default kernel context. cxl_get_context() will get this
65 * context. This context will be released by PCI hot unplug, so doesn't need to
66 * be released explicitly by drivers.
67 *
68 * Additional kernel contexts may be inited using cxl_dev_context_init().
69 * These must be released using cxl_context_detach().
70 *
71 * Once a context has been inited, IRQs may be configured. Firstly these IRQs
72 * must be allocated (cxl_allocate_afu_irqs()), then individually mapped to
73 * specific handlers (cxl_map_afu_irq()).
74 *
75 * These IRQs can be unmapped (cxl_unmap_afu_irq()) and finally released
76 * (cxl_free_afu_irqs()).
77 *
78 * The AFU can be reset (cxl_afu_reset()). This will cause the PSL/AFU
79 * hardware to lose track of all contexts. It's upto the caller of
80 * cxl_afu_reset() to restart these contexts.
81 */
82
83/*
84 * On pci_enabled_device(), the cxl driver will init a single cxl context for
85 * use by the driver. It doesn't start this context (as that will likely
86 * generate DMA traffic for most AFUs).
87 *
88 * This gets the default context associated with this pci_dev. This context
89 * doesn't need to be released as this will be done by the PCI subsystem on hot
90 * unplug.
91 */
92struct cxl_context *cxl_get_context(struct pci_dev *dev);
93/*
94 * Allocate and initalise a context associated with a AFU PCI device. This
95 * doesn't start the context in the AFU.
96 */
97struct cxl_context *cxl_dev_context_init(struct pci_dev *dev);
98/*
99 * Release and free a context. Context should be stopped before calling.
100 */
101int cxl_release_context(struct cxl_context *ctx);
102
ad42de85
MN
103/*
104 * Set and get private data associated with a context. Allows drivers to have a
105 * back pointer to some useful structure.
106 */
107int cxl_set_priv(struct cxl_context *ctx, void *priv);
108void *cxl_get_priv(struct cxl_context *ctx);
109
6f7f0b3d
MN
110/*
111 * Allocate AFU interrupts for this context. num=0 will allocate the default
112 * for this AFU as given in the AFU descriptor. This number doesn't include the
113 * interrupt 0 (CAIA defines AFU IRQ 0 for page faults). Each interrupt to be
114 * used must map a handler with cxl_map_afu_irq.
115 */
116int cxl_allocate_afu_irqs(struct cxl_context *cxl, int num);
117/* Free allocated interrupts */
118void cxl_free_afu_irqs(struct cxl_context *cxl);
119
120/*
121 * Map a handler for an AFU interrupt associated with a particular context. AFU
122 * IRQS numbers start from 1 (CAIA defines AFU IRQ 0 for page faults). cookie
123 * is private data is that will be provided to the interrupt handler.
124 */
125int cxl_map_afu_irq(struct cxl_context *cxl, int num,
126 irq_handler_t handler, void *cookie, char *name);
127/* unmap mapped IRQ handlers */
128void cxl_unmap_afu_irq(struct cxl_context *cxl, int num, void *cookie);
129
130/*
131 * Start work on the AFU. This starts an cxl context and associates it with a
132 * task. task == NULL will make it a kernel context.
133 */
134int cxl_start_context(struct cxl_context *ctx, u64 wed,
135 struct task_struct *task);
136/*
137 * Stop a context and remove it from the PSL
138 */
139int cxl_stop_context(struct cxl_context *ctx);
140
141/* Reset the AFU */
142int cxl_afu_reset(struct cxl_context *ctx);
143
144/*
145 * Set a context as a master context.
146 * This sets the default problem space area mapped as the full space, rather
147 * than just the per context area (for slaves).
148 */
149void cxl_set_master(struct cxl_context *ctx);
150
7a0d85d3
IM
151/*
152 * Sets the context to use real mode memory accesses to operate with
153 * translation disabled. Note that this only makes sense for kernel contexts
154 * under bare metal, and will not work with virtualisation. May only be
155 * performed on stopped contexts.
156 */
157int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode);
158
6f7f0b3d
MN
159/*
160 * Map and unmap the AFU Problem Space area. The amount and location mapped
161 * depends on if this context is a master or slave.
162 */
163void __iomem *cxl_psa_map(struct cxl_context *ctx);
164void cxl_psa_unmap(void __iomem *addr);
165
166/* Get the process element for this context */
167int cxl_process_element(struct cxl_context *ctx);
168
79384e4b
IM
169/*
170 * Limit the number of interrupts that a single context can allocate via
171 * cxl_start_work. If using the api with a real phb, this may be used to
172 * request that additional default contexts be created when allocating
173 * interrupts via pci_enable_msix_range. These will be set to the same running
174 * state as the default context, and if that is running it will reuse the
175 * parameters previously passed to cxl_start_context for the default context.
176 */
177int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs);
178int cxl_get_max_irqs_per_process(struct pci_dev *dev);
6f7f0b3d 179
cbce0917
IM
180/*
181 * Use to simultaneously iterate over hardware interrupt numbers, contexts and
182 * afu interrupt numbers allocated for the device via pci_enable_msix_range and
183 * is a useful convenience function when working with hardware that has
184 * limitations on the number of interrupts per process. *ctx and *afu_irq
185 * should be NULL and 0 to start the iteration.
186 */
187int cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq);
188
6f7f0b3d
MN
189/*
190 * These calls allow drivers to create their own file descriptors and make them
191 * identical to the cxl file descriptor user API. An example use case:
192 *
193 * struct file_operations cxl_my_fops = {};
194 * ......
195 * // Init the context
196 * ctx = cxl_dev_context_init(dev);
197 * if (IS_ERR(ctx))
198 * return PTR_ERR(ctx);
199 * // Create and attach a new file descriptor to my file ops
200 * file = cxl_get_fd(ctx, &cxl_my_fops, &fd);
201 * // Start context
202 * rc = cxl_start_work(ctx, &work.work);
203 * if (rc) {
204 * fput(file);
205 * put_unused_fd(fd);
206 * return -ENODEV;
207 * }
208 * // No error paths after installing the fd
209 * fd_install(fd, file);
210 * return fd;
211 *
212 * This inits a context, and gets a file descriptor and associates some file
213 * ops to that file descriptor. If the file ops are blank, the cxl driver will
214 * fill them in with the default ones that mimic the standard user API. Once
215 * completed, the file descriptor can be installed. Once the file descriptor is
216 * installed, it's visible to the user so no errors must occur past this point.
217 *
218 * If cxl_fd_release() file op call is installed, the context will be stopped
219 * and released when the fd is released. Hence the driver won't need to manage
220 * this itself.
221 */
222
223/*
224 * Take a context and associate it with my file ops. Returns the associated
225 * file and file descriptor. Any file ops which are blank are filled in by the
226 * cxl driver with the default ops to mimic the standard API.
227 */
228struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
229 int *fd);
230/* Get the context associated with this file */
231struct cxl_context *cxl_fops_get_context(struct file *file);
232/*
233 * Start a context associated a struct cxl_ioctl_start_work used by the
234 * standard cxl user API.
235 */
236int cxl_start_work(struct cxl_context *ctx,
237 struct cxl_ioctl_start_work *work);
238/*
239 * Export all the existing fops so drivers can use them
240 */
241int cxl_fd_open(struct inode *inode, struct file *file);
242int cxl_fd_release(struct inode *inode, struct file *file);
243long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
244int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm);
245unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll);
246ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
247 loff_t *off);
248
13e68d8b
DA
249/*
250 * For EEH, a driver may want to assert a PERST will reload the same image
251 * from flash into the FPGA.
252 *
253 * This is a property of the entire adapter, not a single AFU, so drivers
254 * should set this property with care!
255 */
256void cxl_perst_reloads_same_image(struct cxl_afu *afu,
257 bool perst_reloads_same_image);
258
d601ea91
FB
259/*
260 * Read the VPD for the card where the AFU resides
261 */
262ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
263
b810253b
PB
264/*
265 * AFU driver ops allow an AFU driver to create their own events to pass to
266 * userspace through the file descriptor as a simpler alternative to overriding
267 * the read() and poll() calls that works with the generic cxl events. These
268 * events are given priority over the generic cxl events, so they will be
269 * delivered first if multiple types of events are pending.
270 *
271 * The AFU driver must call cxl_context_events_pending() to notify the cxl
272 * driver that new events are ready to be delivered for a specific context.
273 * cxl_context_events_pending() will adjust the current count of AFU driver
274 * events for this context, and wake up anyone waiting on the context wait
275 * queue.
276 *
277 * The cxl driver will then call fetch_event() to get a structure defining
278 * the size and address of the driver specific event data. The cxl driver
279 * will build a cxl header with type and process_element fields filled in,
280 * and header.size set to sizeof(struct cxl_event_header) + data_size.
281 * The total size of the event is limited to CXL_READ_MIN_SIZE (4K).
282 *
283 * fetch_event() is called with a spin lock held, so it must not sleep.
284 *
285 * The cxl driver will then deliver the event to userspace, and finally
286 * call event_delivered() to return the status of the operation, identified
287 * by cxl context and AFU driver event data pointers.
288 * 0 Success
289 * -EFAULT copy_to_user() has failed
290 * -EINVAL Event data pointer is NULL, or event size is greater than
291 * CXL_READ_MIN_SIZE.
292 */
293struct cxl_afu_driver_ops {
294 struct cxl_event_afu_driver_reserved *(*fetch_event) (
295 struct cxl_context *ctx);
296 void (*event_delivered) (struct cxl_context *ctx,
297 struct cxl_event_afu_driver_reserved *event,
298 int rc);
299};
300
301/*
302 * Associate the above driver ops with a specific context.
303 * Reset the current count of AFU driver events.
304 */
305void cxl_set_driver_ops(struct cxl_context *ctx,
306 struct cxl_afu_driver_ops *ops);
307
308/* Notify cxl driver that new events are ready to be delivered for context */
309void cxl_context_events_pending(struct cxl_context *ctx,
310 unsigned int new_events);
311
6f7f0b3d 312#endif /* _MISC_CXL_H */