xref: /openbmc/linux/include/misc/ocxl.h (revision 06014661)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #ifndef _MISC_OCXL_H_
4 #define _MISC_OCXL_H_
5 
6 #include <linux/pci.h>
7 
8 /*
9  * Opencapi drivers all need some common facilities, like parsing the
10  * device configuration space, adding a Process Element to the Shared
11  * Process Area, etc...
12  *
13  * The ocxl module provides a kernel API, to allow other drivers to
14  * reuse common code. A bit like a in-kernel library.
15  */
16 
17 #define OCXL_AFU_NAME_SZ      (24+1)  /* add 1 for NULL termination */
18 
19 
20 struct ocxl_afu_config {
21 	u8 idx;
22 	int dvsec_afu_control_pos; /* offset of AFU control DVSEC */
23 	char name[OCXL_AFU_NAME_SZ];
24 	u8 version_major;
25 	u8 version_minor;
26 	u8 afuc_type;
27 	u8 afum_type;
28 	u8 profile;
29 	u8 global_mmio_bar;     /* global MMIO area */
30 	u64 global_mmio_offset;
31 	u32 global_mmio_size;
32 	u8 pp_mmio_bar;         /* per-process MMIO area */
33 	u64 pp_mmio_offset;
34 	u32 pp_mmio_stride;
35 	u8 log_mem_size;
36 	u8 pasid_supported_log;
37 	u16 actag_supported;
38 };
39 
40 struct ocxl_fn_config {
41 	int dvsec_tl_pos;       /* offset of the Transaction Layer DVSEC */
42 	int dvsec_function_pos; /* offset of the Function DVSEC */
43 	int dvsec_afu_info_pos; /* offset of the AFU information DVSEC */
44 	s8 max_pasid_log;
45 	s8 max_afu_index;
46 };
47 
48 // These are opaque outside the ocxl driver
49 struct ocxl_afu;
50 struct ocxl_fn;
51 struct ocxl_context;
52 
53 // Device detection & initialisation
54 
55 /**
56  * Open an OpenCAPI function on an OpenCAPI device
57  *
58  * @dev: The PCI device that contains the function
59  *
60  * Returns an opaque pointer to the function, or an error pointer (check with IS_ERR)
61  */
62 struct ocxl_fn *ocxl_function_open(struct pci_dev *dev);
63 
64 /**
65  * Get the list of AFUs associated with a PCI function device
66  *
67  * Returns a list of struct ocxl_afu *
68  *
69  * @fn: The OpenCAPI function containing the AFUs
70  */
71 struct list_head *ocxl_function_afu_list(struct ocxl_fn *fn);
72 
73 /**
74  * Fetch an AFU instance from an OpenCAPI function
75  *
76  * @fn: The OpenCAPI function to get the AFU from
77  * @afu_idx: The index of the AFU to get
78  *
79  * If successful, the AFU should be released with ocxl_afu_put()
80  *
81  * Returns a pointer to the AFU, or NULL on error
82  */
83 struct ocxl_afu *ocxl_function_fetch_afu(struct ocxl_fn *fn, u8 afu_idx);
84 
85 /**
86  * Take a reference to an AFU
87  *
88  * @afu: The AFU to increment the reference count on
89  */
90 void ocxl_afu_get(struct ocxl_afu *afu);
91 
92 /**
93  * Release a reference to an AFU
94  *
95  * @afu: The AFU to decrement the reference count on
96  */
97 void ocxl_afu_put(struct ocxl_afu *afu);
98 
99 
100 /**
101  * Get the configuration information for an OpenCAPI function
102  *
103  * @fn: The OpenCAPI function to get the config for
104  *
105  * Returns the function config, or NULL on error
106  */
107 const struct ocxl_fn_config *ocxl_function_config(struct ocxl_fn *fn);
108 
109 /**
110  * Close an OpenCAPI function
111  *
112  * This will free any AFUs previously retrieved from the function, and
113  * detach and associated contexts. The contexts must by freed by the caller.
114  *
115  * @fn: The OpenCAPI function to close
116  *
117  */
118 void ocxl_function_close(struct ocxl_fn *fn);
119 
120 // Context allocation
121 
122 /**
123  * Allocate an OpenCAPI context
124  *
125  * @context: The OpenCAPI context to allocate, must be freed with ocxl_context_free
126  * @afu: The AFU the context belongs to
127  * @mapping: The mapping to unmap when the context is closed (may be NULL)
128  */
129 int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,
130 			struct address_space *mapping);
131 
132 /**
133  * Free an OpenCAPI context
134  *
135  * @ctx: The OpenCAPI context to free
136  */
137 void ocxl_context_free(struct ocxl_context *ctx);
138 
139 /**
140  * Grant access to an MM to an OpenCAPI context
141  * @ctx: The OpenCAPI context to attach
142  * @amr: The value of the AMR register to restrict access
143  * @mm: The mm to attach to the context
144  *
145  * Returns 0 on success, negative on failure
146  */
147 int ocxl_context_attach(struct ocxl_context *ctx, u64 amr,
148 				struct mm_struct *mm);
149 
150 /**
151  * Detach an MM from an OpenCAPI context
152  * @ctx: The OpenCAPI context to attach
153  *
154  * Returns 0 on success, negative on failure
155  */
156 int ocxl_context_detach(struct ocxl_context *ctx);
157 
158 // AFU IRQs
159 
160 /**
161  * Allocate an IRQ associated with an AFU context
162  * @ctx: the AFU context
163  * @irq_id: out, the IRQ ID
164  *
165  * Returns 0 on success, negative on failure
166  */
167 extern int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id);
168 
169 /**
170  * Frees an IRQ associated with an AFU context
171  * @ctx: the AFU context
172  * @irq_id: the IRQ ID
173  *
174  * Returns 0 on success, negative on failure
175  */
176 extern int ocxl_afu_irq_free(struct ocxl_context *ctx, int irq_id);
177 
178 /**
179  * Gets the address of the trigger page for an IRQ
180  * This can then be provided to an AFU which will write to that
181  * page to trigger the IRQ.
182  * @ctx: The AFU context that the IRQ is associated with
183  * @irq_id: The IRQ ID
184  *
185  * returns the trigger page address, or 0 if the IRQ is not valid
186  */
187 extern u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id);
188 
189 /**
190  * Provide a callback to be called when an IRQ is triggered
191  * @ctx: The AFU context that the IRQ is associated with
192  * @irq_id: The IRQ ID
193  * @handler: the callback to be called when the IRQ is triggered
194  * @free_private: the callback to be called when the IRQ is freed (may be NULL)
195  * @private: Private data to be passed to the callbacks
196  *
197  * Returns 0 on success, negative on failure
198  */
199 int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,
200 		irqreturn_t (*handler)(void *private),
201 		void (*free_private)(void *private),
202 		void *private);
203 
204 // AFU Metadata
205 
206 /**
207  * Get a pointer to the config for an AFU
208  *
209  * @afu: a pointer to the AFU to get the config for
210  *
211  * Returns a pointer to the AFU config
212  */
213 struct ocxl_afu_config *ocxl_afu_config(struct ocxl_afu *afu);
214 
215 /**
216  * Assign opaque hardware specific information to an OpenCAPI AFU.
217  *
218  * @dev: The PCI device associated with the OpenCAPI device
219  * @private: the opaque hardware specific information to assign to the driver
220  */
221 void ocxl_afu_set_private(struct ocxl_afu *afu, void *private);
222 
223 /**
224  * Fetch the hardware specific information associated with an external OpenCAPI
225  * AFU. This may be consumed by an external OpenCAPI driver.
226  *
227  * @afu: The AFU
228  *
229  * Returns the opaque pointer associated with the device, or NULL if not set
230  */
231 void *ocxl_afu_get_private(struct ocxl_afu *dev);
232 
233 
234 // Functions left here are for compatibility with the cxlflash driver
235 
236 /*
237  * Read the configuration space of a function for the AFU specified by
238  * the index 'afu_idx'. Fills in a ocxl_afu_config structure
239  */
240 int ocxl_config_read_afu(struct pci_dev *dev,
241 				struct ocxl_fn_config *fn,
242 				struct ocxl_afu_config *afu,
243 				u8 afu_idx);
244 
245 /*
246  * Tell an AFU, by writing in the configuration space, the PASIDs that
247  * it can use. Range starts at 'pasid_base' and its size is a multiple
248  * of 2
249  *
250  * 'afu_control_offset' is the offset of the AFU control DVSEC which
251  * can be found in the function configuration
252  */
253 void ocxl_config_set_afu_pasid(struct pci_dev *dev,
254 				int afu_control_offset,
255 				int pasid_base, u32 pasid_count_log);
256 
257 /*
258  * Get the actag configuration for the function:
259  * 'base' is the first actag value that can be used.
260  * 'enabled' it the number of actags available, starting from base.
261  * 'supported' is the total number of actags desired by all the AFUs
262  *             of the function.
263  */
264 int ocxl_config_get_actag_info(struct pci_dev *dev,
265 				u16 *base, u16 *enabled, u16 *supported);
266 
267 /*
268  * Tell a function, by writing in the configuration space, the actags
269  * it can use.
270  *
271  * 'func_offset' is the offset of the Function DVSEC that can found in
272  * the function configuration
273  */
274 void ocxl_config_set_actag(struct pci_dev *dev, int func_offset,
275 				u32 actag_base, u32 actag_count);
276 
277 /*
278  * Tell an AFU, by writing in the configuration space, the actags it
279  * can use.
280  *
281  * 'afu_control_offset' is the offset of the AFU control DVSEC for the
282  * desired AFU. It can be found in the AFU configuration
283  */
284 void ocxl_config_set_afu_actag(struct pci_dev *dev,
285 				int afu_control_offset,
286 				int actag_base, int actag_count);
287 
288 /*
289  * Enable/disable an AFU, by writing in the configuration space.
290  *
291  * 'afu_control_offset' is the offset of the AFU control DVSEC for the
292  * desired AFU. It can be found in the AFU configuration
293  */
294 void ocxl_config_set_afu_state(struct pci_dev *dev,
295 				int afu_control_offset, int enable);
296 
297 /*
298  * Set the Transaction Layer configuration in the configuration space.
299  * Only needed for function 0.
300  *
301  * It queries the host TL capabilities, find some common ground
302  * between the host and device, and set the Transaction Layer on both
303  * accordingly.
304  */
305 int ocxl_config_set_TL(struct pci_dev *dev, int tl_dvsec);
306 
307 /*
308  * Request an AFU to terminate a PASID.
309  * Will return once the AFU has acked the request, or an error in case
310  * of timeout.
311  *
312  * The hardware can only terminate one PASID at a time, so caller must
313  * guarantee some kind of serialization.
314  *
315  * 'afu_control_offset' is the offset of the AFU control DVSEC for the
316  * desired AFU. It can be found in the AFU configuration
317  */
318 int ocxl_config_terminate_pasid(struct pci_dev *dev,
319 				int afu_control_offset, int pasid);
320 
321 /*
322  * Read the configuration space of a function and fill in a
323  * ocxl_fn_config structure with all the function details
324  */
325 int ocxl_config_read_function(struct pci_dev *dev,
326 				struct ocxl_fn_config *fn);
327 
328 /*
329  * Set up the opencapi link for the function.
330  *
331  * When called for the first time for a link, it sets up the Shared
332  * Process Area for the link and the interrupt handler to process
333  * translation faults.
334  *
335  * Returns a 'link handle' that should be used for further calls for
336  * the link
337  */
338 int ocxl_link_setup(struct pci_dev *dev, int PE_mask,
339 			void **link_handle);
340 
341 /*
342  * Remove the association between the function and its link.
343  */
344 void ocxl_link_release(struct pci_dev *dev, void *link_handle);
345 
346 /*
347  * Add a Process Element to the Shared Process Area for a link.
348  * The process is defined by its PASID, pid, tid and its mm_struct.
349  *
350  * 'xsl_err_cb' is an optional callback if the driver wants to be
351  * notified when the translation fault interrupt handler detects an
352  * address error.
353  * 'xsl_err_data' is an argument passed to the above callback, if
354  * defined
355  */
356 int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
357 		u64 amr, struct mm_struct *mm,
358 		void (*xsl_err_cb)(void *data, u64 addr, u64 dsisr),
359 		void *xsl_err_data);
360 
361 /*
362  * Remove a Process Element from the Shared Process Area for a link
363  */
364 int ocxl_link_remove_pe(void *link_handle, int pasid);
365 
366 /*
367  * Allocate an AFU interrupt associated to the link.
368  *
369  * 'hw_irq' is the hardware interrupt number
370  * 'obj_handle' is the 64-bit object handle to be passed to the AFU to
371  * trigger the interrupt.
372  * On P9, 'obj_handle' is an address, which, if written, triggers the
373  * interrupt. It is an MMIO address which needs to be remapped (one
374  * page).
375  */
376 int ocxl_link_irq_alloc(void *link_handle, int *hw_irq,
377 			u64 *obj_handle);
378 
379 /*
380  * Free a previously allocated AFU interrupt
381  */
382 void ocxl_link_free_irq(void *link_handle, int hw_irq);
383 
384 #endif /* _MISC_OCXL_H_ */
385