// SPDX-License-Identifier: GPL-2.0 /* * PCI Endpoint *Controller* (EPC) library * * Copyright (C) 2017 Texas Instruments * Author: Kishon Vijay Abraham I <[email protected]> */ #include <linux/device.h> #include <linux/slab.h> #include <linux/module.h> #include <linux/pci-epc.h> #include <linux/pci-epf.h> #include <linux/pci-ep-cfs.h> static const struct class pci_epc_class = …; static void devm_pci_epc_release(struct device *dev, void *res) { … } static int devm_pci_epc_match(struct device *dev, void *res, void *match_data) { … } /** * pci_epc_put() - release the PCI endpoint controller * @epc: epc returned by pci_epc_get() * * release the refcount the caller obtained by invoking pci_epc_get() */ void pci_epc_put(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_get() - get the PCI endpoint controller * @epc_name: device name of the endpoint controller * * Invoke to get struct pci_epc * corresponding to the device name of the * endpoint controller */ struct pci_epc *pci_epc_get(const char *epc_name) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_get_first_free_bar() - helper to get first unreserved BAR * @epc_features: pci_epc_features structure that holds the reserved bar bitmap * * Invoke to get the first unreserved BAR that can be used by the endpoint * function. */ enum pci_barno pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar * @epc_features: pci_epc_features structure that holds the reserved bar bitmap * @bar: the starting BAR number from where unreserved BAR should be searched * * Invoke to get the next unreserved BAR starting from @bar that can be used * for endpoint function. */ enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features *epc_features, enum pci_barno bar) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_get_features() - get the features supported by EPC * @epc: the features supported by *this* EPC device will be returned * @func_no: the features supported by the EPC device specific to the * endpoint function with func_no will be returned * @vfunc_no: the features supported by the EPC device specific to the * virtual endpoint function with vfunc_no will be returned * * Invoke to get the features provided by the EPC which may be * specific to an endpoint function. Returns pci_epc_features on success * and NULL for any failures. */ const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_stop() - stop the PCI link * @epc: the link of the EPC device that has to be stopped * * Invoke to stop the PCI link */ void pci_epc_stop(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_start() - start the PCI link * @epc: the link of *this* EPC device has to be started * * Invoke to start the PCI link */ int pci_epc_start(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_raise_irq() - interrupt the host system * @epc: the EPC device which has to interrupt the host * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @type: specify the type of interrupt; INTX, MSI or MSI-X * @interrupt_num: the MSI or MSI-X interrupt number with range (1-N) * * Invoke to raise an INTX, MSI or MSI-X interrupt */ int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no, unsigned int type, u16 interrupt_num) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_map_msi_irq() - Map physical address to MSI address and return * MSI data * @epc: the EPC device which has the MSI capability * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @phys_addr: the physical address of the outbound region * @interrupt_num: the MSI interrupt number with range (1-N) * @entry_size: Size of Outbound address region for each interrupt * @msi_data: the data that should be written in order to raise MSI interrupt * with interrupt number as 'interrupt num' * @msi_addr_offset: Offset of MSI address from the aligned outbound address * to which the MSI address is mapped * * Invoke to map physical address to MSI address and return MSI data. The * physical address should be an address in the outbound region. This is * required to implement doorbell functionality of NTB wherein EPC on either * side of the interface (primary and secondary) can directly write to the * physical address (in outbound region) of the other interface to ring * doorbell. */ int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no, phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size, u32 *msi_data, u32 *msi_addr_offset) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated * @epc: the EPC device to which MSI interrupts was requested * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * * Invoke to get the number of MSI interrupts allocated by the RC */ int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_set_msi() - set the number of MSI interrupt numbers required * @epc: the EPC device on which MSI has to be configured * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @interrupts: number of MSI interrupts required by the EPF * * Invoke to set the required number of MSI interrupts. */ int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated * @epc: the EPC device to which MSI-X interrupts was requested * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * * Invoke to get the number of MSI-X interrupts allocated by the RC */ int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required * @epc: the EPC device on which MSI-X has to be configured * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @interrupts: number of MSI-X interrupts required by the EPF * @bir: BAR where the MSI-X table resides * @offset: Offset pointing to the start of MSI-X table * * Invoke to set the required number of MSI-X interrupts. */ int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u16 interrupts, enum pci_barno bir, u32 offset) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_unmap_addr() - unmap CPU address from PCI address * @epc: the EPC device on which address is allocated * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @phys_addr: physical address of the local system * * Invoke to unmap the CPU address from PCI address. */ void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, phys_addr_t phys_addr) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_map_addr() - map CPU address to PCI address * @epc: the EPC device on which address is allocated * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @phys_addr: physical address of the local system * @pci_addr: PCI address to which the physical address should be mapped * @size: the size of the allocation * * Invoke to map CPU address with PCI address. */ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, phys_addr_t phys_addr, u64 pci_addr, size_t size) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_clear_bar() - reset the BAR * @epc: the EPC device for which the BAR has to be cleared * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @epf_bar: the struct epf_bar that contains the BAR information * * Invoke to reset the BAR of the endpoint device. */ void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, struct pci_epf_bar *epf_bar) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space * @epc: the EPC device on which BAR has to be configured * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @epf_bar: the struct epf_bar that contains the BAR information * * Invoke to configure the BAR of the endpoint device. */ int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, struct pci_epf_bar *epf_bar) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_write_header() - write standard configuration header * @epc: the EPC device to which the configuration header should be written * @func_no: the physical endpoint function number in the EPC device * @vfunc_no: the virtual endpoint function number in the physical function * @header: standard configuration header fields * * Invoke to write the configuration header to the endpoint controller. Every * endpoint controller will have a dedicated location to which the standard * configuration header would be written. The callback function should write * the header fields to this dedicated location. */ int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no, struct pci_epf_header *header) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller * @epc: the EPC device to which the endpoint function should be added * @epf: the endpoint function to be added * @type: Identifies if the EPC is connected to the primary or secondary * interface of EPF * * A PCI endpoint device can have one or more functions. In the case of PCIe, * the specification allows up to 8 PCIe endpoint functions. Invoke * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller. */ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf, enum pci_epc_interface_type type) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller * @epc: the EPC device from which the endpoint function should be removed * @epf: the endpoint function to be removed * @type: identifies if the EPC is connected to the primary or secondary * interface of EPF * * Invoke to remove PCI endpoint function from the endpoint controller. */ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf, enum pci_epc_interface_type type) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_linkup() - Notify the EPF device that EPC device has established a * connection with the Root Complex. * @epc: the EPC device which has established link with the host * * Invoke to Notify the EPF device that the EPC device has established a * connection with the Root Complex. */ void pci_epc_linkup(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_linkdown() - Notify the EPF device that EPC device has dropped the * connection with the Root Complex. * @epc: the EPC device which has dropped the link with the host * * Invoke to Notify the EPF device that the EPC device has dropped the * connection with the Root Complex. */ void pci_epc_linkdown(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_init_notify() - Notify the EPF device that EPC device initialization * is completed. * @epc: the EPC device whose initialization is completed * * Invoke to Notify the EPF device that the EPC device's initialization * is completed. */ void pci_epc_init_notify(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_notify_pending_init() - Notify the pending EPC device initialization * complete to the EPF device * @epc: the EPC device whose initialization is pending to be notified * @epf: the EPF device to be notified * * Invoke to notify the pending EPC device initialization complete to the EPF * device. This is used to deliver the notification if the EPC initialization * got completed before the EPF driver bind. */ void pci_epc_notify_pending_init(struct pci_epc *epc, struct pci_epf *epf) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_deinit_notify() - Notify the EPF device about EPC deinitialization * @epc: the EPC device whose deinitialization is completed * * Invoke to notify the EPF device that the EPC deinitialization is completed. */ void pci_epc_deinit_notify(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_bus_master_enable_notify() - Notify the EPF device that the EPC * device has received the Bus Master * Enable event from the Root complex * @epc: the EPC device that received the Bus Master Enable event * * Notify the EPF device that the EPC device has generated the Bus Master Enable * event due to host setting the Bus Master Enable bit in the Command register. */ void pci_epc_bus_master_enable_notify(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * pci_epc_destroy() - destroy the EPC device * @epc: the EPC device that has to be destroyed * * Invoke to destroy the PCI EPC device */ void pci_epc_destroy(struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); /** * devm_pci_epc_destroy() - destroy the EPC device * @dev: device that wants to destroy the EPC * @epc: the EPC device that has to be destroyed * * Invoke to destroy the devres associated with this * pci_epc and destroy the EPC device. */ void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc) { … } EXPORT_SYMBOL_GPL(…); static void pci_epc_release(struct device *dev) { … } /** * __pci_epc_create() - create a new endpoint controller (EPC) device * @dev: device that is creating the new EPC * @ops: function pointers for performing EPC operations * @owner: the owner of the module that creates the EPC device * * Invoke to create a new EPC device and add it to pci_epc class. */ struct pci_epc * __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, struct module *owner) { … } EXPORT_SYMBOL_GPL(…); /** * __devm_pci_epc_create() - create a new endpoint controller (EPC) device * @dev: device that is creating the new EPC * @ops: function pointers for performing EPC operations * @owner: the owner of the module that creates the EPC device * * Invoke to create a new EPC device and add it to pci_epc class. * While at that, it also associates the device with the pci_epc using devres. * On driver detach, release function is invoked on the devres data, * then, devres data is freed. */ struct pci_epc * __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, struct module *owner) { … } EXPORT_SYMBOL_GPL(…); static int __init pci_epc_init(void) { … } module_init(…) …; static void __exit pci_epc_exit(void) { … } module_exit(pci_epc_exit); MODULE_DESCRIPTION(…) …; MODULE_AUTHOR(…) …;