// SPDX-License-Identifier: GPL-2.0+1// Copyright 2017 IBM Corp.2#ifndef _MISC_OCXL_H_3#define _MISC_OCXL_H_45#include <linux/pci.h>67/*8* Opencapi drivers all need some common facilities, like parsing the9* device configuration space, adding a Process Element to the Shared10* Process Area, etc...11*12* The ocxl module provides a kernel API, to allow other drivers to13* reuse common code. A bit like a in-kernel library.14*/1516#define OCXL_AFU_NAME_SZ (24+1) /* add 1 for NULL termination */171819struct ocxl_afu_config {20u8 idx;21int dvsec_afu_control_pos; /* offset of AFU control DVSEC */22char name[OCXL_AFU_NAME_SZ];23u8 version_major;24u8 version_minor;25u8 afuc_type;26u8 afum_type;27u8 profile;28u8 global_mmio_bar; /* global MMIO area */29u64 global_mmio_offset;30u32 global_mmio_size;31u8 pp_mmio_bar; /* per-process MMIO area */32u64 pp_mmio_offset;33u32 pp_mmio_stride;34u64 lpc_mem_offset;35u64 lpc_mem_size;36u64 special_purpose_mem_offset;37u64 special_purpose_mem_size;38u8 pasid_supported_log;39u16 actag_supported;40};4142struct ocxl_fn_config {43int dvsec_tl_pos; /* offset of the Transaction Layer DVSEC */44int dvsec_function_pos; /* offset of the Function DVSEC */45int dvsec_afu_info_pos; /* offset of the AFU information DVSEC */46s8 max_pasid_log;47s8 max_afu_index;48};4950enum ocxl_endian {51OCXL_BIG_ENDIAN = 0, /**< AFU data is big-endian */52OCXL_LITTLE_ENDIAN = 1, /**< AFU data is little-endian */53OCXL_HOST_ENDIAN = 2, /**< AFU data is the same endianness as the host */54};5556// These are opaque outside the ocxl driver57struct ocxl_afu;58struct ocxl_fn;59struct ocxl_context;6061// Device detection & initialisation6263/**64* ocxl_function_open() - Open an OpenCAPI function on an OpenCAPI device65* @dev: The PCI device that contains the function66*67* Returns an opaque pointer to the function, or an error pointer (check with IS_ERR)68*/69struct ocxl_fn *ocxl_function_open(struct pci_dev *dev);7071/**72* ocxl_function_afu_list() - Get the list of AFUs associated with a PCI function device73* Returns a list of struct ocxl_afu *74*75* @fn: The OpenCAPI function containing the AFUs76*/77struct list_head *ocxl_function_afu_list(struct ocxl_fn *fn);7879/**80* ocxl_function_fetch_afu() - Fetch an AFU instance from an OpenCAPI function81* @fn: The OpenCAPI function to get the AFU from82* @afu_idx: The index of the AFU to get83*84* If successful, the AFU should be released with ocxl_afu_put()85*86* Returns a pointer to the AFU, or NULL on error87*/88struct ocxl_afu *ocxl_function_fetch_afu(struct ocxl_fn *fn, u8 afu_idx);8990/**91* ocxl_afu_get() - Take a reference to an AFU92* @afu: The AFU to increment the reference count on93*/94void ocxl_afu_get(struct ocxl_afu *afu);9596/**97* ocxl_afu_put() - Release a reference to an AFU98* @afu: The AFU to decrement the reference count on99*/100void ocxl_afu_put(struct ocxl_afu *afu);101102103/**104* ocxl_function_config() - Get the configuration information for an OpenCAPI function105* @fn: The OpenCAPI function to get the config for106*107* Returns the function config, or NULL on error108*/109const struct ocxl_fn_config *ocxl_function_config(struct ocxl_fn *fn);110111/**112* ocxl_function_close() - Close an OpenCAPI function113* This will free any AFUs previously retrieved from the function, and114* detach and associated contexts. The contexts must by freed by the caller.115*116* @fn: The OpenCAPI function to close117*118*/119void ocxl_function_close(struct ocxl_fn *fn);120121// Context allocation122123/**124* ocxl_context_alloc() - Allocate an OpenCAPI context125* @context: The OpenCAPI context to allocate, must be freed with ocxl_context_free126* @afu: The AFU the context belongs to127* @mapping: The mapping to unmap when the context is closed (may be NULL)128*/129int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,130struct address_space *mapping);131132/**133* ocxl_context_free() - Free an OpenCAPI context134* @ctx: The OpenCAPI context to free135*/136void ocxl_context_free(struct ocxl_context *ctx);137138/**139* ocxl_context_attach() - Grant access to an MM to an OpenCAPI context140* @ctx: The OpenCAPI context to attach141* @amr: The value of the AMR register to restrict access142* @mm: The mm to attach to the context143*144* Returns 0 on success, negative on failure145*/146int ocxl_context_attach(struct ocxl_context *ctx, u64 amr,147struct mm_struct *mm);148149/**150* ocxl_context_detach() - Detach an MM from an OpenCAPI context151* @ctx: The OpenCAPI context to attach152*153* Returns 0 on success, negative on failure154*/155int ocxl_context_detach(struct ocxl_context *ctx);156157// AFU IRQs158159/**160* ocxl_afu_irq_alloc() - Allocate an IRQ associated with an AFU context161* @ctx: the AFU context162* @irq_id: out, the IRQ ID163*164* Returns 0 on success, negative on failure165*/166int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id);167168/**169* ocxl_afu_irq_free() - Frees an IRQ associated with an AFU context170* @ctx: the AFU context171* @irq_id: the IRQ ID172*173* Returns 0 on success, negative on failure174*/175int ocxl_afu_irq_free(struct ocxl_context *ctx, int irq_id);176177/**178* ocxl_afu_irq_get_addr() - Gets the address of the trigger page for an IRQ179* This can then be provided to an AFU which will write to that180* page to trigger the IRQ.181* @ctx: The AFU context that the IRQ is associated with182* @irq_id: The IRQ ID183*184* returns the trigger page address, or 0 if the IRQ is not valid185*/186u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id);187188/**189* ocxl_irq_set_handler() - Provide a callback to be called when an IRQ is triggered190* @ctx: The AFU context that the IRQ is associated with191* @irq_id: The IRQ ID192* @handler: the callback to be called when the IRQ is triggered193* @free_private: the callback to be called when the IRQ is freed (may be NULL)194* @private: Private data to be passed to the callbacks195*196* Returns 0 on success, negative on failure197*/198int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,199irqreturn_t (*handler)(void *private),200void (*free_private)(void *private),201void *private);202203// AFU Metadata204205/**206* ocxl_afu_config() - Get a pointer to the config for an AFU207* @afu: a pointer to the AFU to get the config for208*209* Returns a pointer to the AFU config210*/211struct ocxl_afu_config *ocxl_afu_config(struct ocxl_afu *afu);212213/**214* ocxl_afu_set_private() - Assign opaque hardware specific information to an OpenCAPI AFU.215* @afu: The OpenCAPI AFU216* @private: the opaque hardware specific information to assign to the driver217*/218void ocxl_afu_set_private(struct ocxl_afu *afu, void *private);219220/**221* ocxl_afu_get_private() - Fetch the hardware specific information associated with222* an external OpenCAPI AFU. This may be consumed by an external OpenCAPI driver.223* @afu: The OpenCAPI AFU224*225* Returns the opaque pointer associated with the device, or NULL if not set226*/227void *ocxl_afu_get_private(struct ocxl_afu *afu);228229// Global MMIO230/**231* ocxl_global_mmio_read32() - Read a 32 bit value from global MMIO232* @afu: The AFU233* @offset: The Offset from the start of MMIO234* @endian: the endianness that the MMIO data is in235* @val: returns the value236*237* Returns 0 for success, negative on error238*/239int ocxl_global_mmio_read32(struct ocxl_afu *afu, size_t offset,240enum ocxl_endian endian, u32 *val);241242/**243* ocxl_global_mmio_read64() - Read a 64 bit value from global MMIO244* @afu: The AFU245* @offset: The Offset from the start of MMIO246* @endian: the endianness that the MMIO data is in247* @val: returns the value248*249* Returns 0 for success, negative on error250*/251int ocxl_global_mmio_read64(struct ocxl_afu *afu, size_t offset,252enum ocxl_endian endian, u64 *val);253254/**255* ocxl_global_mmio_write32() - Write a 32 bit value to global MMIO256* @afu: The AFU257* @offset: The Offset from the start of MMIO258* @endian: the endianness that the MMIO data is in259* @val: The value to write260*261* Returns 0 for success, negative on error262*/263int ocxl_global_mmio_write32(struct ocxl_afu *afu, size_t offset,264enum ocxl_endian endian, u32 val);265266/**267* ocxl_global_mmio_write64() - Write a 64 bit value to global MMIO268* @afu: The AFU269* @offset: The Offset from the start of MMIO270* @endian: the endianness that the MMIO data is in271* @val: The value to write272*273* Returns 0 for success, negative on error274*/275int ocxl_global_mmio_write64(struct ocxl_afu *afu, size_t offset,276enum ocxl_endian endian, u64 val);277278/**279* ocxl_global_mmio_set32() - Set bits in a 32 bit global MMIO register280* @afu: The AFU281* @offset: The Offset from the start of MMIO282* @endian: the endianness that the MMIO data is in283* @mask: a mask of the bits to set284*285* Returns 0 for success, negative on error286*/287int ocxl_global_mmio_set32(struct ocxl_afu *afu, size_t offset,288enum ocxl_endian endian, u32 mask);289290/**291* ocxl_global_mmio_set64() - Set bits in a 64 bit global MMIO register292* @afu: The AFU293* @offset: The Offset from the start of MMIO294* @endian: the endianness that the MMIO data is in295* @mask: a mask of the bits to set296*297* Returns 0 for success, negative on error298*/299int ocxl_global_mmio_set64(struct ocxl_afu *afu, size_t offset,300enum ocxl_endian endian, u64 mask);301302/**303* ocxl_global_mmio_clear32() - Set bits in a 32 bit global MMIO register304* @afu: The AFU305* @offset: The Offset from the start of MMIO306* @endian: the endianness that the MMIO data is in307* @mask: a mask of the bits to set308*309* Returns 0 for success, negative on error310*/311int ocxl_global_mmio_clear32(struct ocxl_afu *afu, size_t offset,312enum ocxl_endian endian, u32 mask);313314/**315* ocxl_global_mmio_clear64() - Set bits in a 64 bit global MMIO register316* @afu: The AFU317* @offset: The Offset from the start of MMIO318* @endian: the endianness that the MMIO data is in319* @mask: a mask of the bits to set320*321* Returns 0 for success, negative on error322*/323int ocxl_global_mmio_clear64(struct ocxl_afu *afu, size_t offset,324enum ocxl_endian endian, u64 mask);325326// Functions left here are for compatibility with the cxlflash driver327328/*329* Read the configuration space of a function for the AFU specified by330* the index 'afu_idx'. Fills in a ocxl_afu_config structure331*/332int ocxl_config_read_afu(struct pci_dev *dev,333struct ocxl_fn_config *fn,334struct ocxl_afu_config *afu,335u8 afu_idx);336337/*338* Tell an AFU, by writing in the configuration space, the PASIDs that339* it can use. Range starts at 'pasid_base' and its size is a multiple340* of 2341*342* 'afu_control_offset' is the offset of the AFU control DVSEC which343* can be found in the function configuration344*/345void ocxl_config_set_afu_pasid(struct pci_dev *dev,346int afu_control_offset,347int pasid_base, u32 pasid_count_log);348349/*350* Get the actag configuration for the function:351* 'base' is the first actag value that can be used.352* 'enabled' it the number of actags available, starting from base.353* 'supported' is the total number of actags desired by all the AFUs354* of the function.355*/356int ocxl_config_get_actag_info(struct pci_dev *dev,357u16 *base, u16 *enabled, u16 *supported);358359/*360* Tell a function, by writing in the configuration space, the actags361* it can use.362*363* 'func_offset' is the offset of the Function DVSEC that can found in364* the function configuration365*/366void ocxl_config_set_actag(struct pci_dev *dev, int func_offset,367u32 actag_base, u32 actag_count);368369/*370* Tell an AFU, by writing in the configuration space, the actags it371* can use.372*373* 'afu_control_offset' is the offset of the AFU control DVSEC for the374* desired AFU. It can be found in the AFU configuration375*/376void ocxl_config_set_afu_actag(struct pci_dev *dev,377int afu_control_offset,378int actag_base, int actag_count);379380/*381* Enable/disable an AFU, by writing in the configuration space.382*383* 'afu_control_offset' is the offset of the AFU control DVSEC for the384* desired AFU. It can be found in the AFU configuration385*/386void ocxl_config_set_afu_state(struct pci_dev *dev,387int afu_control_offset, int enable);388389/*390* Set the Transaction Layer configuration in the configuration space.391* Only needed for function 0.392*393* It queries the host TL capabilities, find some common ground394* between the host and device, and set the Transaction Layer on both395* accordingly.396*/397int ocxl_config_set_TL(struct pci_dev *dev, int tl_dvsec);398399/*400* Request an AFU to terminate a PASID.401* Will return once the AFU has acked the request, or an error in case402* of timeout.403*404* The hardware can only terminate one PASID at a time, so caller must405* guarantee some kind of serialization.406*407* 'afu_control_offset' is the offset of the AFU control DVSEC for the408* desired AFU. It can be found in the AFU configuration409*/410int ocxl_config_terminate_pasid(struct pci_dev *dev,411int afu_control_offset, int pasid);412413/*414* Read the configuration space of a function and fill in a415* ocxl_fn_config structure with all the function details416*/417int ocxl_config_read_function(struct pci_dev *dev,418struct ocxl_fn_config *fn);419420/*421* Set up the opencapi link for the function.422*423* When called for the first time for a link, it sets up the Shared424* Process Area for the link and the interrupt handler to process425* translation faults.426*427* Returns a 'link handle' that should be used for further calls for428* the link429*/430int ocxl_link_setup(struct pci_dev *dev, int PE_mask,431void **link_handle);432433/*434* Remove the association between the function and its link.435*/436void ocxl_link_release(struct pci_dev *dev, void *link_handle);437438/*439* Add a Process Element to the Shared Process Area for a link.440* The process is defined by its PASID, pid, tid and its mm_struct.441*442* 'xsl_err_cb' is an optional callback if the driver wants to be443* notified when the translation fault interrupt handler detects an444* address error.445* 'xsl_err_data' is an argument passed to the above callback, if446* defined447*/448int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,449u64 amr, u16 bdf, struct mm_struct *mm,450void (*xsl_err_cb)(void *data, u64 addr, u64 dsisr),451void *xsl_err_data);452453/*454* Remove a Process Element from the Shared Process Area for a link455*/456int ocxl_link_remove_pe(void *link_handle, int pasid);457458/*459* Allocate an AFU interrupt associated to the link.460*461* 'hw_irq' is the hardware interrupt number462*/463int ocxl_link_irq_alloc(void *link_handle, int *hw_irq);464465/*466* Free a previously allocated AFU interrupt467*/468void ocxl_link_free_irq(void *link_handle, int hw_irq);469470#endif /* _MISC_OCXL_H_ */471472473