1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
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...
13 * The ocxl module provides a kernel API, to allow other drivers to
14 * reuse common code. A bit like a in-kernel library.
17 #define OCXL_AFU_NAME_SZ (24+1) /* add 1 for NULL termination */
20 struct ocxl_afu_config
{
22 int dvsec_afu_control_pos
; /* offset of AFU control DVSEC */
23 char name
[OCXL_AFU_NAME_SZ
];
29 u8 global_mmio_bar
; /* global MMIO area */
30 u64 global_mmio_offset
;
32 u8 pp_mmio_bar
; /* per-process MMIO area */
37 u64 special_purpose_mem_offset
;
38 u64 special_purpose_mem_size
;
39 u8 pasid_supported_log
;
43 struct ocxl_fn_config
{
44 int dvsec_tl_pos
; /* offset of the Transaction Layer DVSEC */
45 int dvsec_function_pos
; /* offset of the Function DVSEC */
46 int dvsec_afu_info_pos
; /* offset of the AFU information DVSEC */
52 OCXL_BIG_ENDIAN
= 0, /**< AFU data is big-endian */
53 OCXL_LITTLE_ENDIAN
= 1, /**< AFU data is little-endian */
54 OCXL_HOST_ENDIAN
= 2, /**< AFU data is the same endianness as the host */
57 // These are opaque outside the ocxl driver
62 // Device detection & initialisation
65 * Open an OpenCAPI function on an OpenCAPI device
67 * @dev: The PCI device that contains the function
69 * Returns an opaque pointer to the function, or an error pointer (check with IS_ERR)
71 struct ocxl_fn
*ocxl_function_open(struct pci_dev
*dev
);
74 * Get the list of AFUs associated with a PCI function device
76 * Returns a list of struct ocxl_afu *
78 * @fn: The OpenCAPI function containing the AFUs
80 struct list_head
*ocxl_function_afu_list(struct ocxl_fn
*fn
);
83 * Fetch an AFU instance from an OpenCAPI function
85 * @fn: The OpenCAPI function to get the AFU from
86 * @afu_idx: The index of the AFU to get
88 * If successful, the AFU should be released with ocxl_afu_put()
90 * Returns a pointer to the AFU, or NULL on error
92 struct ocxl_afu
*ocxl_function_fetch_afu(struct ocxl_fn
*fn
, u8 afu_idx
);
95 * Take a reference to an AFU
97 * @afu: The AFU to increment the reference count on
99 void ocxl_afu_get(struct ocxl_afu
*afu
);
102 * Release a reference to an AFU
104 * @afu: The AFU to decrement the reference count on
106 void ocxl_afu_put(struct ocxl_afu
*afu
);
110 * Get the configuration information for an OpenCAPI function
112 * @fn: The OpenCAPI function to get the config for
114 * Returns the function config, or NULL on error
116 const struct ocxl_fn_config
*ocxl_function_config(struct ocxl_fn
*fn
);
119 * Close an OpenCAPI function
121 * This will free any AFUs previously retrieved from the function, and
122 * detach and associated contexts. The contexts must by freed by the caller.
124 * @fn: The OpenCAPI function to close
127 void ocxl_function_close(struct ocxl_fn
*fn
);
129 // Context allocation
132 * Allocate an OpenCAPI context
134 * @context: The OpenCAPI context to allocate, must be freed with ocxl_context_free
135 * @afu: The AFU the context belongs to
136 * @mapping: The mapping to unmap when the context is closed (may be NULL)
138 int ocxl_context_alloc(struct ocxl_context
**context
, struct ocxl_afu
*afu
,
139 struct address_space
*mapping
);
142 * Free an OpenCAPI context
144 * @ctx: The OpenCAPI context to free
146 void ocxl_context_free(struct ocxl_context
*ctx
);
149 * Grant access to an MM to an OpenCAPI context
150 * @ctx: The OpenCAPI context to attach
151 * @amr: The value of the AMR register to restrict access
152 * @mm: The mm to attach to the context
154 * Returns 0 on success, negative on failure
156 int ocxl_context_attach(struct ocxl_context
*ctx
, u64 amr
,
157 struct mm_struct
*mm
);
160 * Detach an MM from an OpenCAPI context
161 * @ctx: The OpenCAPI context to attach
163 * Returns 0 on success, negative on failure
165 int ocxl_context_detach(struct ocxl_context
*ctx
);
170 * Allocate an IRQ associated with an AFU context
171 * @ctx: the AFU context
172 * @irq_id: out, the IRQ ID
174 * Returns 0 on success, negative on failure
176 extern int ocxl_afu_irq_alloc(struct ocxl_context
*ctx
, int *irq_id
);
179 * Frees an IRQ associated with an AFU context
180 * @ctx: the AFU context
181 * @irq_id: the IRQ ID
183 * Returns 0 on success, negative on failure
185 extern int ocxl_afu_irq_free(struct ocxl_context
*ctx
, int irq_id
);
188 * Gets the address of the trigger page for an IRQ
189 * This can then be provided to an AFU which will write to that
190 * page to trigger the IRQ.
191 * @ctx: The AFU context that the IRQ is associated with
192 * @irq_id: The IRQ ID
194 * returns the trigger page address, or 0 if the IRQ is not valid
196 extern u64
ocxl_afu_irq_get_addr(struct ocxl_context
*ctx
, int irq_id
);
199 * Provide a callback to be called when an IRQ is triggered
200 * @ctx: The AFU context that the IRQ is associated with
201 * @irq_id: The IRQ ID
202 * @handler: the callback to be called when the IRQ is triggered
203 * @free_private: the callback to be called when the IRQ is freed (may be NULL)
204 * @private: Private data to be passed to the callbacks
206 * Returns 0 on success, negative on failure
208 int ocxl_irq_set_handler(struct ocxl_context
*ctx
, int irq_id
,
209 irqreturn_t (*handler
)(void *private),
210 void (*free_private
)(void *private),
216 * Get a pointer to the config for an AFU
218 * @afu: a pointer to the AFU to get the config for
220 * Returns a pointer to the AFU config
222 struct ocxl_afu_config
*ocxl_afu_config(struct ocxl_afu
*afu
);
225 * Assign opaque hardware specific information to an OpenCAPI AFU.
227 * @dev: The PCI device associated with the OpenCAPI device
228 * @private: the opaque hardware specific information to assign to the driver
230 void ocxl_afu_set_private(struct ocxl_afu
*afu
, void *private);
233 * Fetch the hardware specific information associated with an external OpenCAPI
234 * AFU. This may be consumed by an external OpenCAPI driver.
238 * Returns the opaque pointer associated with the device, or NULL if not set
240 void *ocxl_afu_get_private(struct ocxl_afu
*dev
);
244 * Read a 32 bit value from global MMIO
247 * @offset: The Offset from the start of MMIO
248 * @endian: the endianness that the MMIO data is in
249 * @val: returns the value
251 * Returns 0 for success, negative on error
253 int ocxl_global_mmio_read32(struct ocxl_afu
*afu
, size_t offset
,
254 enum ocxl_endian endian
, u32
*val
);
257 * Read a 64 bit value from global MMIO
260 * @offset: The Offset from the start of MMIO
261 * @endian: the endianness that the MMIO data is in
262 * @val: returns the value
264 * Returns 0 for success, negative on error
266 int ocxl_global_mmio_read64(struct ocxl_afu
*afu
, size_t offset
,
267 enum ocxl_endian endian
, u64
*val
);
270 * Write a 32 bit value to global MMIO
273 * @offset: The Offset from the start of MMIO
274 * @endian: the endianness that the MMIO data is in
275 * @val: The value to write
277 * Returns 0 for success, negative on error
279 int ocxl_global_mmio_write32(struct ocxl_afu
*afu
, size_t offset
,
280 enum ocxl_endian endian
, u32 val
);
283 * Write a 64 bit value to global MMIO
286 * @offset: The Offset from the start of MMIO
287 * @endian: the endianness that the MMIO data is in
288 * @val: The value to write
290 * Returns 0 for success, negative on error
292 int ocxl_global_mmio_write64(struct ocxl_afu
*afu
, size_t offset
,
293 enum ocxl_endian endian
, u64 val
);
296 * Set bits in a 32 bit global MMIO register
299 * @offset: The Offset from the start of MMIO
300 * @endian: the endianness that the MMIO data is in
301 * @mask: a mask of the bits to set
303 * Returns 0 for success, negative on error
305 int ocxl_global_mmio_set32(struct ocxl_afu
*afu
, size_t offset
,
306 enum ocxl_endian endian
, u32 mask
);
309 * Set bits in a 64 bit global MMIO register
312 * @offset: The Offset from the start of MMIO
313 * @endian: the endianness that the MMIO data is in
314 * @mask: a mask of the bits to set
316 * Returns 0 for success, negative on error
318 int ocxl_global_mmio_set64(struct ocxl_afu
*afu
, size_t offset
,
319 enum ocxl_endian endian
, u64 mask
);
322 * Set bits in a 32 bit global MMIO register
325 * @offset: The Offset from the start of MMIO
326 * @endian: the endianness that the MMIO data is in
327 * @mask: a mask of the bits to set
329 * Returns 0 for success, negative on error
331 int ocxl_global_mmio_clear32(struct ocxl_afu
*afu
, size_t offset
,
332 enum ocxl_endian endian
, u32 mask
);
335 * Set bits in a 64 bit global MMIO register
338 * @offset: The Offset from the start of MMIO
339 * @endian: the endianness that the MMIO data is in
340 * @mask: a mask of the bits to set
342 * Returns 0 for success, negative on error
344 int ocxl_global_mmio_clear64(struct ocxl_afu
*afu
, size_t offset
,
345 enum ocxl_endian endian
, u64 mask
);
347 // Functions left here are for compatibility with the cxlflash driver
350 * Read the configuration space of a function for the AFU specified by
351 * the index 'afu_idx'. Fills in a ocxl_afu_config structure
353 int ocxl_config_read_afu(struct pci_dev
*dev
,
354 struct ocxl_fn_config
*fn
,
355 struct ocxl_afu_config
*afu
,
359 * Tell an AFU, by writing in the configuration space, the PASIDs that
360 * it can use. Range starts at 'pasid_base' and its size is a multiple
363 * 'afu_control_offset' is the offset of the AFU control DVSEC which
364 * can be found in the function configuration
366 void ocxl_config_set_afu_pasid(struct pci_dev
*dev
,
367 int afu_control_offset
,
368 int pasid_base
, u32 pasid_count_log
);
371 * Get the actag configuration for the function:
372 * 'base' is the first actag value that can be used.
373 * 'enabled' it the number of actags available, starting from base.
374 * 'supported' is the total number of actags desired by all the AFUs
377 int ocxl_config_get_actag_info(struct pci_dev
*dev
,
378 u16
*base
, u16
*enabled
, u16
*supported
);
381 * Tell a function, by writing in the configuration space, the actags
384 * 'func_offset' is the offset of the Function DVSEC that can found in
385 * the function configuration
387 void ocxl_config_set_actag(struct pci_dev
*dev
, int func_offset
,
388 u32 actag_base
, u32 actag_count
);
391 * Tell an AFU, by writing in the configuration space, the actags it
394 * 'afu_control_offset' is the offset of the AFU control DVSEC for the
395 * desired AFU. It can be found in the AFU configuration
397 void ocxl_config_set_afu_actag(struct pci_dev
*dev
,
398 int afu_control_offset
,
399 int actag_base
, int actag_count
);
402 * Enable/disable an AFU, by writing in the configuration space.
404 * 'afu_control_offset' is the offset of the AFU control DVSEC for the
405 * desired AFU. It can be found in the AFU configuration
407 void ocxl_config_set_afu_state(struct pci_dev
*dev
,
408 int afu_control_offset
, int enable
);
411 * Set the Transaction Layer configuration in the configuration space.
412 * Only needed for function 0.
414 * It queries the host TL capabilities, find some common ground
415 * between the host and device, and set the Transaction Layer on both
418 int ocxl_config_set_TL(struct pci_dev
*dev
, int tl_dvsec
);
421 * Request an AFU to terminate a PASID.
422 * Will return once the AFU has acked the request, or an error in case
425 * The hardware can only terminate one PASID at a time, so caller must
426 * guarantee some kind of serialization.
428 * 'afu_control_offset' is the offset of the AFU control DVSEC for the
429 * desired AFU. It can be found in the AFU configuration
431 int ocxl_config_terminate_pasid(struct pci_dev
*dev
,
432 int afu_control_offset
, int pasid
);
435 * Read the configuration space of a function and fill in a
436 * ocxl_fn_config structure with all the function details
438 int ocxl_config_read_function(struct pci_dev
*dev
,
439 struct ocxl_fn_config
*fn
);
442 * Set up the opencapi link for the function.
444 * When called for the first time for a link, it sets up the Shared
445 * Process Area for the link and the interrupt handler to process
446 * translation faults.
448 * Returns a 'link handle' that should be used for further calls for
451 int ocxl_link_setup(struct pci_dev
*dev
, int PE_mask
,
455 * Remove the association between the function and its link.
457 void ocxl_link_release(struct pci_dev
*dev
, void *link_handle
);
460 * Add a Process Element to the Shared Process Area for a link.
461 * The process is defined by its PASID, pid, tid and its mm_struct.
463 * 'xsl_err_cb' is an optional callback if the driver wants to be
464 * notified when the translation fault interrupt handler detects an
466 * 'xsl_err_data' is an argument passed to the above callback, if
469 int ocxl_link_add_pe(void *link_handle
, int pasid
, u32 pidr
, u32 tidr
,
470 u64 amr
, struct mm_struct
*mm
,
471 void (*xsl_err_cb
)(void *data
, u64 addr
, u64 dsisr
),
475 * Remove a Process Element from the Shared Process Area for a link
477 int ocxl_link_remove_pe(void *link_handle
, int pasid
);
480 * Allocate an AFU interrupt associated to the link.
482 * 'hw_irq' is the hardware interrupt number
483 * 'obj_handle' is the 64-bit object handle to be passed to the AFU to
484 * trigger the interrupt.
485 * On P9, 'obj_handle' is an address, which, if written, triggers the
486 * interrupt. It is an MMIO address which needs to be remapped (one
489 int ocxl_link_irq_alloc(void *link_handle
, int *hw_irq
,
493 * Free a previously allocated AFU interrupt
495 void ocxl_link_free_irq(void *link_handle
, int hw_irq
);
497 #endif /* _MISC_OCXL_H_ */