Commit | Line | Data |
---|---|---|
280b983c FB |
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 | ||
75ca758a | 19 | |
280b983c FB |
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 | ||
75ca758a AS |
48 | // These are opaque outside the ocxl driver |
49 | struct ocxl_afu; | |
50 | struct ocxl_fn; | |
b9721d27 | 51 | struct ocxl_context; |
75ca758a AS |
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) | |
280b983c | 61 | */ |
75ca758a AS |
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 | ||
b9721d27 AS |
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 | ||
75ca758a AS |
158 | // AFU Metadata |
159 | ||
160 | /** | |
161 | * Get a pointer to the config for an AFU | |
162 | * | |
163 | * @afu: a pointer to the AFU to get the config for | |
164 | * | |
165 | * Returns a pointer to the AFU config | |
166 | */ | |
167 | struct ocxl_afu_config *ocxl_afu_config(struct ocxl_afu *afu); | |
168 | ||
169 | /** | |
170 | * Assign opaque hardware specific information to an OpenCAPI AFU. | |
171 | * | |
172 | * @dev: The PCI device associated with the OpenCAPI device | |
173 | * @private: the opaque hardware specific information to assign to the driver | |
174 | */ | |
175 | void ocxl_afu_set_private(struct ocxl_afu *afu, void *private); | |
176 | ||
177 | /** | |
178 | * Fetch the hardware specific information associated with an external OpenCAPI | |
179 | * AFU. This may be consumed by an external OpenCAPI driver. | |
180 | * | |
181 | * @afu: The AFU | |
182 | * | |
183 | * Returns the opaque pointer associated with the device, or NULL if not set | |
184 | */ | |
185 | void *ocxl_afu_get_private(struct ocxl_afu *dev); | |
186 | ||
187 | ||
188 | // Functions left here are for compatibility with the cxlflash driver | |
280b983c | 189 | |
280b983c FB |
190 | /* |
191 | * Read the configuration space of a function for the AFU specified by | |
192 | * the index 'afu_idx'. Fills in a ocxl_afu_config structure | |
193 | */ | |
53e3e745 | 194 | int ocxl_config_read_afu(struct pci_dev *dev, |
280b983c FB |
195 | struct ocxl_fn_config *fn, |
196 | struct ocxl_afu_config *afu, | |
197 | u8 afu_idx); | |
198 | ||
280b983c FB |
199 | /* |
200 | * Tell an AFU, by writing in the configuration space, the PASIDs that | |
201 | * it can use. Range starts at 'pasid_base' and its size is a multiple | |
202 | * of 2 | |
203 | * | |
204 | * 'afu_control_offset' is the offset of the AFU control DVSEC which | |
205 | * can be found in the function configuration | |
206 | */ | |
53e3e745 | 207 | void ocxl_config_set_afu_pasid(struct pci_dev *dev, |
280b983c FB |
208 | int afu_control_offset, |
209 | int pasid_base, u32 pasid_count_log); | |
210 | ||
211 | /* | |
212 | * Get the actag configuration for the function: | |
213 | * 'base' is the first actag value that can be used. | |
214 | * 'enabled' it the number of actags available, starting from base. | |
215 | * 'supported' is the total number of actags desired by all the AFUs | |
216 | * of the function. | |
217 | */ | |
53e3e745 | 218 | int ocxl_config_get_actag_info(struct pci_dev *dev, |
280b983c FB |
219 | u16 *base, u16 *enabled, u16 *supported); |
220 | ||
221 | /* | |
222 | * Tell a function, by writing in the configuration space, the actags | |
223 | * it can use. | |
224 | * | |
225 | * 'func_offset' is the offset of the Function DVSEC that can found in | |
226 | * the function configuration | |
227 | */ | |
53e3e745 | 228 | void ocxl_config_set_actag(struct pci_dev *dev, int func_offset, |
280b983c FB |
229 | u32 actag_base, u32 actag_count); |
230 | ||
231 | /* | |
232 | * Tell an AFU, by writing in the configuration space, the actags it | |
233 | * can use. | |
234 | * | |
235 | * 'afu_control_offset' is the offset of the AFU control DVSEC for the | |
236 | * desired AFU. It can be found in the AFU configuration | |
237 | */ | |
53e3e745 | 238 | void ocxl_config_set_afu_actag(struct pci_dev *dev, |
280b983c FB |
239 | int afu_control_offset, |
240 | int actag_base, int actag_count); | |
241 | ||
242 | /* | |
243 | * Enable/disable an AFU, by writing in the configuration space. | |
244 | * | |
245 | * 'afu_control_offset' is the offset of the AFU control DVSEC for the | |
246 | * desired AFU. It can be found in the AFU configuration | |
247 | */ | |
53e3e745 | 248 | void ocxl_config_set_afu_state(struct pci_dev *dev, |
280b983c FB |
249 | int afu_control_offset, int enable); |
250 | ||
251 | /* | |
252 | * Set the Transaction Layer configuration in the configuration space. | |
253 | * Only needed for function 0. | |
254 | * | |
255 | * It queries the host TL capabilities, find some common ground | |
256 | * between the host and device, and set the Transaction Layer on both | |
257 | * accordingly. | |
258 | */ | |
53e3e745 | 259 | int ocxl_config_set_TL(struct pci_dev *dev, int tl_dvsec); |
280b983c FB |
260 | |
261 | /* | |
262 | * Request an AFU to terminate a PASID. | |
263 | * Will return once the AFU has acked the request, or an error in case | |
264 | * of timeout. | |
265 | * | |
266 | * The hardware can only terminate one PASID at a time, so caller must | |
267 | * guarantee some kind of serialization. | |
268 | * | |
269 | * 'afu_control_offset' is the offset of the AFU control DVSEC for the | |
270 | * desired AFU. It can be found in the AFU configuration | |
271 | */ | |
53e3e745 | 272 | int ocxl_config_terminate_pasid(struct pci_dev *dev, |
280b983c FB |
273 | int afu_control_offset, int pasid); |
274 | ||
75ca758a AS |
275 | /* |
276 | * Read the configuration space of a function and fill in a | |
277 | * ocxl_fn_config structure with all the function details | |
278 | */ | |
279 | int ocxl_config_read_function(struct pci_dev *dev, | |
280 | struct ocxl_fn_config *fn); | |
281 | ||
280b983c FB |
282 | /* |
283 | * Set up the opencapi link for the function. | |
284 | * | |
285 | * When called for the first time for a link, it sets up the Shared | |
286 | * Process Area for the link and the interrupt handler to process | |
287 | * translation faults. | |
288 | * | |
289 | * Returns a 'link handle' that should be used for further calls for | |
290 | * the link | |
291 | */ | |
53e3e745 | 292 | int ocxl_link_setup(struct pci_dev *dev, int PE_mask, |
280b983c FB |
293 | void **link_handle); |
294 | ||
295 | /* | |
296 | * Remove the association between the function and its link. | |
297 | */ | |
53e3e745 | 298 | void ocxl_link_release(struct pci_dev *dev, void *link_handle); |
280b983c FB |
299 | |
300 | /* | |
301 | * Add a Process Element to the Shared Process Area for a link. | |
302 | * The process is defined by its PASID, pid, tid and its mm_struct. | |
303 | * | |
304 | * 'xsl_err_cb' is an optional callback if the driver wants to be | |
305 | * notified when the translation fault interrupt handler detects an | |
306 | * address error. | |
307 | * 'xsl_err_data' is an argument passed to the above callback, if | |
308 | * defined | |
309 | */ | |
53e3e745 | 310 | int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr, |
280b983c FB |
311 | u64 amr, struct mm_struct *mm, |
312 | void (*xsl_err_cb)(void *data, u64 addr, u64 dsisr), | |
313 | void *xsl_err_data); | |
314 | ||
315 | /* | |
316 | * Remove a Process Element from the Shared Process Area for a link | |
317 | */ | |
53e3e745 | 318 | int ocxl_link_remove_pe(void *link_handle, int pasid); |
280b983c FB |
319 | |
320 | /* | |
321 | * Allocate an AFU interrupt associated to the link. | |
322 | * | |
323 | * 'hw_irq' is the hardware interrupt number | |
324 | * 'obj_handle' is the 64-bit object handle to be passed to the AFU to | |
325 | * trigger the interrupt. | |
326 | * On P9, 'obj_handle' is an address, which, if written, triggers the | |
327 | * interrupt. It is an MMIO address which needs to be remapped (one | |
328 | * page). | |
329 | */ | |
53e3e745 | 330 | int ocxl_link_irq_alloc(void *link_handle, int *hw_irq, |
280b983c FB |
331 | u64 *obj_handle); |
332 | ||
333 | /* | |
334 | * Free a previously allocated AFU interrupt | |
335 | */ | |
53e3e745 | 336 | void ocxl_link_free_irq(void *link_handle, int hw_irq); |
280b983c FB |
337 | |
338 | #endif /* _MISC_OCXL_H_ */ |