xref: /linux-6.15/include/linux/cdx/cdx_bus.h (revision 0e439ba3)
12959ab24SNipun Gupta /* SPDX-License-Identifier: GPL-2.0
22959ab24SNipun Gupta  *
32959ab24SNipun Gupta  * CDX bus public interface
42959ab24SNipun Gupta  *
52959ab24SNipun Gupta  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
62959ab24SNipun Gupta  *
72959ab24SNipun Gupta  */
82959ab24SNipun Gupta 
92959ab24SNipun Gupta #ifndef _CDX_BUS_H_
102959ab24SNipun Gupta #define _CDX_BUS_H_
112959ab24SNipun Gupta 
122959ab24SNipun Gupta #include <linux/device.h>
132959ab24SNipun Gupta #include <linux/list.h>
142959ab24SNipun Gupta #include <linux/mod_devicetable.h>
15*0e439ba3SNipun Gupta #include <linux/msi.h>
162959ab24SNipun Gupta 
172959ab24SNipun Gupta #define MAX_CDX_DEV_RESOURCES	4
182959ab24SNipun Gupta #define CDX_CONTROLLER_ID_SHIFT 4
192959ab24SNipun Gupta #define CDX_BUS_NUM_MASK 0xF
202959ab24SNipun Gupta 
212959ab24SNipun Gupta /* Forward declaration for CDX controller */
222959ab24SNipun Gupta struct cdx_controller;
232959ab24SNipun Gupta 
2448a6c7bcSNipun Gupta enum {
25*0e439ba3SNipun Gupta 	CDX_DEV_MSI_CONF,
26a941b784SNipun Gupta 	CDX_DEV_BUS_MASTER_CONF,
2748a6c7bcSNipun Gupta 	CDX_DEV_RESET_CONF,
28*0e439ba3SNipun Gupta 	CDX_DEV_MSI_ENABLE,
29*0e439ba3SNipun Gupta };
30*0e439ba3SNipun Gupta 
31*0e439ba3SNipun Gupta struct cdx_msi_config {
32*0e439ba3SNipun Gupta 	u64 addr;
33*0e439ba3SNipun Gupta 	u32 data;
34*0e439ba3SNipun Gupta 	u16 msi_index;
3548a6c7bcSNipun Gupta };
3648a6c7bcSNipun Gupta 
3748a6c7bcSNipun Gupta struct cdx_device_config {
3848a6c7bcSNipun Gupta 	u8 type;
39*0e439ba3SNipun Gupta 	union {
40*0e439ba3SNipun Gupta 		struct cdx_msi_config msi;
41a941b784SNipun Gupta 		bool bus_master_enable;
42*0e439ba3SNipun Gupta 		bool msi_enable;
43*0e439ba3SNipun Gupta 	};
4448a6c7bcSNipun Gupta };
4548a6c7bcSNipun Gupta 
46e3cfd49cSAbhijit Gangurde typedef int (*cdx_bus_enable_cb)(struct cdx_controller *cdx, u8 bus_num);
47e3cfd49cSAbhijit Gangurde 
48e3cfd49cSAbhijit Gangurde typedef int (*cdx_bus_disable_cb)(struct cdx_controller *cdx, u8 bus_num);
49e3cfd49cSAbhijit Gangurde 
502959ab24SNipun Gupta typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
512959ab24SNipun Gupta 
5248a6c7bcSNipun Gupta typedef int (*cdx_dev_configure_cb)(struct cdx_controller *cdx,
5348a6c7bcSNipun Gupta 				    u8 bus_num, u8 dev_num,
5448a6c7bcSNipun Gupta 				    struct cdx_device_config *dev_config);
5548a6c7bcSNipun Gupta 
562959ab24SNipun Gupta /**
57fa10f413SAbhijit Gangurde  * CDX_DEVICE - macro used to describe a specific CDX device
58fa10f413SAbhijit Gangurde  * @vend: the 16 bit CDX Vendor ID
59fa10f413SAbhijit Gangurde  * @dev: the 16 bit CDX Device ID
60fa10f413SAbhijit Gangurde  *
61fa10f413SAbhijit Gangurde  * This macro is used to create a struct cdx_device_id that matches a
62fa10f413SAbhijit Gangurde  * specific device. The subvendor and subdevice fields will be set to
63fa10f413SAbhijit Gangurde  * CDX_ANY_ID.
64fa10f413SAbhijit Gangurde  */
65fa10f413SAbhijit Gangurde #define CDX_DEVICE(vend, dev) \
66fa10f413SAbhijit Gangurde 	.vendor = (vend), .device = (dev), \
67fa10f413SAbhijit Gangurde 	.subvendor = CDX_ANY_ID, .subdevice = CDX_ANY_ID
68fa10f413SAbhijit Gangurde 
69fa10f413SAbhijit Gangurde /**
702959ab24SNipun Gupta  * CDX_DEVICE_DRIVER_OVERRIDE - macro used to describe a CDX device with
712959ab24SNipun Gupta  *                              override_only flags.
722959ab24SNipun Gupta  * @vend: the 16 bit CDX Vendor ID
732959ab24SNipun Gupta  * @dev: the 16 bit CDX Device ID
742959ab24SNipun Gupta  * @driver_override: the 32 bit CDX Device override_only
752959ab24SNipun Gupta  *
762959ab24SNipun Gupta  * This macro is used to create a struct cdx_device_id that matches only a
77fa10f413SAbhijit Gangurde  * driver_override device. The subvendor and subdevice fields will be set to
78fa10f413SAbhijit Gangurde  * CDX_ANY_ID.
792959ab24SNipun Gupta  */
802959ab24SNipun Gupta #define CDX_DEVICE_DRIVER_OVERRIDE(vend, dev, driver_override) \
81fa10f413SAbhijit Gangurde 	.vendor = (vend), .device = (dev), .subvendor = CDX_ANY_ID,\
82fa10f413SAbhijit Gangurde 	.subdevice = CDX_ANY_ID, .override_only = (driver_override)
832959ab24SNipun Gupta 
842959ab24SNipun Gupta /**
852959ab24SNipun Gupta  * struct cdx_ops - Callbacks supported by CDX controller.
86e3cfd49cSAbhijit Gangurde  * @bus_enable: enable bus on the controller
87e3cfd49cSAbhijit Gangurde  * @bus_disable: disable bus on the controller
882959ab24SNipun Gupta  * @scan: scan the devices on the controller
8948a6c7bcSNipun Gupta  * @dev_configure: configuration like reset, master_enable,
9048a6c7bcSNipun Gupta  *		   msi_config etc for a CDX device
912959ab24SNipun Gupta  */
922959ab24SNipun Gupta struct cdx_ops {
93e3cfd49cSAbhijit Gangurde 	cdx_bus_enable_cb bus_enable;
94e3cfd49cSAbhijit Gangurde 	cdx_bus_disable_cb bus_disable;
952959ab24SNipun Gupta 	cdx_scan_cb scan;
9648a6c7bcSNipun Gupta 	cdx_dev_configure_cb dev_configure;
972959ab24SNipun Gupta };
982959ab24SNipun Gupta 
992959ab24SNipun Gupta /**
1002959ab24SNipun Gupta  * struct cdx_controller: CDX controller object
1012959ab24SNipun Gupta  * @dev: Linux device associated with the CDX controller.
1022959ab24SNipun Gupta  * @priv: private data
103*0e439ba3SNipun Gupta  * @msi_domain: MSI domain
1042959ab24SNipun Gupta  * @id: Controller ID
10554b406e1SAbhijit Gangurde  * @controller_registered: controller registered with bus
1062959ab24SNipun Gupta  * @ops: CDX controller ops
1072959ab24SNipun Gupta  */
1082959ab24SNipun Gupta struct cdx_controller {
1092959ab24SNipun Gupta 	struct device *dev;
1102959ab24SNipun Gupta 	void *priv;
111*0e439ba3SNipun Gupta 	struct irq_domain *msi_domain;
1122959ab24SNipun Gupta 	u32 id;
11354b406e1SAbhijit Gangurde 	bool controller_registered;
1142959ab24SNipun Gupta 	struct cdx_ops *ops;
1152959ab24SNipun Gupta };
1162959ab24SNipun Gupta 
1172959ab24SNipun Gupta /**
1182959ab24SNipun Gupta  * struct cdx_device - CDX device object
1192959ab24SNipun Gupta  * @dev: Linux driver model device object
1202959ab24SNipun Gupta  * @cdx: CDX controller associated with the device
1212959ab24SNipun Gupta  * @vendor: Vendor ID for CDX device
1222959ab24SNipun Gupta  * @device: Device ID for CDX device
123fa10f413SAbhijit Gangurde  * @subsystem_vendor: Subsystem Vendor ID for CDX device
124fa10f413SAbhijit Gangurde  * @subsystem_device: Subsystem Device ID for CDX device
125fa10f413SAbhijit Gangurde  * @class: Class for the CDX device
126fa10f413SAbhijit Gangurde  * @revision: Revision of the CDX device
1272959ab24SNipun Gupta  * @bus_num: Bus number for this CDX device
1282959ab24SNipun Gupta  * @dev_num: Device number for this device
1292959ab24SNipun Gupta  * @res: array of MMIO region entries
1302959ab24SNipun Gupta  * @res_attr: resource binary attribute
131cf60af04SAbhijit Gangurde  * @debugfs_dir: debugfs directory for this device
1322959ab24SNipun Gupta  * @res_count: number of valid MMIO regions
1332959ab24SNipun Gupta  * @dma_mask: Default DMA mask
1342959ab24SNipun Gupta  * @flags: CDX device flags
1352959ab24SNipun Gupta  * @req_id: Requestor ID associated with CDX device
136ce558a39SAbhijit Gangurde  * @is_bus: Is this bus device
137e3cfd49cSAbhijit Gangurde  * @enabled: is this bus enabled
138*0e439ba3SNipun Gupta  * @msi_dev_id: MSI Device ID associated with CDX device
139*0e439ba3SNipun Gupta  * @num_msi: Number of MSI's supported by the device
1402959ab24SNipun Gupta  * @driver_override: driver name to force a match; do not set directly,
1412959ab24SNipun Gupta  *                   because core frees it; use driver_set_override() to
1422959ab24SNipun Gupta  *                   set or clear it.
143*0e439ba3SNipun Gupta  * @irqchip_lock: lock to synchronize irq/msi configuration
144*0e439ba3SNipun Gupta  * @msi_write_pending: MSI write pending for this device
1452959ab24SNipun Gupta  */
1462959ab24SNipun Gupta struct cdx_device {
1472959ab24SNipun Gupta 	struct device dev;
1482959ab24SNipun Gupta 	struct cdx_controller *cdx;
1492959ab24SNipun Gupta 	u16 vendor;
1502959ab24SNipun Gupta 	u16 device;
151fa10f413SAbhijit Gangurde 	u16 subsystem_vendor;
152fa10f413SAbhijit Gangurde 	u16 subsystem_device;
153fa10f413SAbhijit Gangurde 	u32 class;
154fa10f413SAbhijit Gangurde 	u8 revision;
1552959ab24SNipun Gupta 	u8 bus_num;
1562959ab24SNipun Gupta 	u8 dev_num;
1572959ab24SNipun Gupta 	struct resource res[MAX_CDX_DEV_RESOURCES];
158aeda33abSAbhijit Gangurde 	struct bin_attribute *res_attr[MAX_CDX_DEV_RESOURCES];
159cf60af04SAbhijit Gangurde 	struct dentry *debugfs_dir;
1602959ab24SNipun Gupta 	u8 res_count;
1612959ab24SNipun Gupta 	u64 dma_mask;
1622959ab24SNipun Gupta 	u16 flags;
1632959ab24SNipun Gupta 	u32 req_id;
164ce558a39SAbhijit Gangurde 	bool is_bus;
165e3cfd49cSAbhijit Gangurde 	bool enabled;
166*0e439ba3SNipun Gupta 	u32 msi_dev_id;
167*0e439ba3SNipun Gupta 	u32 num_msi;
1682959ab24SNipun Gupta 	const char *driver_override;
169*0e439ba3SNipun Gupta 	struct mutex irqchip_lock;
170*0e439ba3SNipun Gupta 	bool msi_write_pending;
1712959ab24SNipun Gupta };
1722959ab24SNipun Gupta 
1732959ab24SNipun Gupta #define to_cdx_device(_dev) \
1742959ab24SNipun Gupta 	container_of(_dev, struct cdx_device, dev)
1752959ab24SNipun Gupta 
176aeda33abSAbhijit Gangurde #define cdx_resource_start(dev, num)	((dev)->res[(num)].start)
177aeda33abSAbhijit Gangurde #define cdx_resource_end(dev, num)	((dev)->res[(num)].end)
178aeda33abSAbhijit Gangurde #define cdx_resource_flags(dev, num)	((dev)->res[(num)].flags)
179aeda33abSAbhijit Gangurde #define cdx_resource_len(dev, num) \
180aeda33abSAbhijit Gangurde 	((cdx_resource_start((dev), (num)) == 0 &&	\
181aeda33abSAbhijit Gangurde 	  cdx_resource_end((dev), (num)) ==		\
182aeda33abSAbhijit Gangurde 	  cdx_resource_start((dev), (num))) ? 0 :	\
183aeda33abSAbhijit Gangurde 	 (cdx_resource_end((dev), (num)) -		\
184aeda33abSAbhijit Gangurde 	  cdx_resource_start((dev), (num)) + 1))
1852959ab24SNipun Gupta /**
1862959ab24SNipun Gupta  * struct cdx_driver - CDX device driver
1872959ab24SNipun Gupta  * @driver: Generic device driver
1882959ab24SNipun Gupta  * @match_id_table: table of supported device matching Ids
1892959ab24SNipun Gupta  * @probe: Function called when a device is added
1902959ab24SNipun Gupta  * @remove: Function called when a device is removed
1912959ab24SNipun Gupta  * @shutdown: Function called at shutdown time to quiesce the device
19248a6c7bcSNipun Gupta  * @reset_prepare: Function called before is reset to notify driver
19348a6c7bcSNipun Gupta  * @reset_done: Function called after reset is complete to notify driver
1942959ab24SNipun Gupta  * @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA.
1952959ab24SNipun Gupta  *		For most device drivers, no need to care about this flag
1962959ab24SNipun Gupta  *		as long as all DMAs are handled through the kernel DMA API.
1972959ab24SNipun Gupta  *		For some special ones, for example VFIO drivers, they know
1982959ab24SNipun Gupta  *		how to manage the DMA themselves and set this flag so that
1992959ab24SNipun Gupta  *		the IOMMU layer will allow them to setup and manage their
2002959ab24SNipun Gupta  *		own I/O address space.
2012959ab24SNipun Gupta  */
2022959ab24SNipun Gupta struct cdx_driver {
2032959ab24SNipun Gupta 	struct device_driver driver;
2042959ab24SNipun Gupta 	const struct cdx_device_id *match_id_table;
2052959ab24SNipun Gupta 	int (*probe)(struct cdx_device *dev);
2062959ab24SNipun Gupta 	int (*remove)(struct cdx_device *dev);
2072959ab24SNipun Gupta 	void (*shutdown)(struct cdx_device *dev);
20848a6c7bcSNipun Gupta 	void (*reset_prepare)(struct cdx_device *dev);
20948a6c7bcSNipun Gupta 	void (*reset_done)(struct cdx_device *dev);
2102959ab24SNipun Gupta 	bool driver_managed_dma;
2112959ab24SNipun Gupta };
2122959ab24SNipun Gupta 
2132959ab24SNipun Gupta #define to_cdx_driver(_drv) \
2142959ab24SNipun Gupta 	container_of(_drv, struct cdx_driver, driver)
2152959ab24SNipun Gupta 
2162959ab24SNipun Gupta /* Macro to avoid include chaining to get THIS_MODULE */
2172959ab24SNipun Gupta #define cdx_driver_register(drv) \
2182959ab24SNipun Gupta 	__cdx_driver_register(drv, THIS_MODULE)
2192959ab24SNipun Gupta 
2202959ab24SNipun Gupta /**
2212959ab24SNipun Gupta  * __cdx_driver_register - registers a CDX device driver
2222959ab24SNipun Gupta  * @cdx_driver: CDX driver to register
2232959ab24SNipun Gupta  * @owner: module owner
2242959ab24SNipun Gupta  *
2252959ab24SNipun Gupta  * Return: -errno on failure, 0 on success.
2262959ab24SNipun Gupta  */
2272959ab24SNipun Gupta int __must_check __cdx_driver_register(struct cdx_driver *cdx_driver,
2282959ab24SNipun Gupta 				       struct module *owner);
2292959ab24SNipun Gupta 
2302959ab24SNipun Gupta /**
2312959ab24SNipun Gupta  * cdx_driver_unregister - unregisters a device driver from the
2322959ab24SNipun Gupta  * CDX bus.
2332959ab24SNipun Gupta  * @cdx_driver: CDX driver to register
2342959ab24SNipun Gupta  */
2352959ab24SNipun Gupta void cdx_driver_unregister(struct cdx_driver *cdx_driver);
2362959ab24SNipun Gupta 
2372959ab24SNipun Gupta extern struct bus_type cdx_bus_type;
2382959ab24SNipun Gupta 
23948a6c7bcSNipun Gupta /**
24048a6c7bcSNipun Gupta  * cdx_dev_reset - Reset CDX device
24148a6c7bcSNipun Gupta  * @dev: device pointer
24248a6c7bcSNipun Gupta  *
24348a6c7bcSNipun Gupta  * Return: 0 for success, -errno on failure
24448a6c7bcSNipun Gupta  */
24548a6c7bcSNipun Gupta int cdx_dev_reset(struct device *dev);
24648a6c7bcSNipun Gupta 
247a941b784SNipun Gupta /**
248a941b784SNipun Gupta  * cdx_set_master - enables bus-mastering for CDX device
249a941b784SNipun Gupta  * @cdx_dev: the CDX device to enable
250a941b784SNipun Gupta  *
251a941b784SNipun Gupta  * Return: 0 for success, -errno on failure
252a941b784SNipun Gupta  */
253a941b784SNipun Gupta int cdx_set_master(struct cdx_device *cdx_dev);
254a941b784SNipun Gupta 
255a941b784SNipun Gupta /**
256a941b784SNipun Gupta  * cdx_clear_master - disables bus-mastering for CDX device
257a941b784SNipun Gupta  * @cdx_dev: the CDX device to disable
258a941b784SNipun Gupta  *
259a941b784SNipun Gupta  * Return: 0 for success, -errno on failure
260a941b784SNipun Gupta  */
261a941b784SNipun Gupta int cdx_clear_master(struct cdx_device *cdx_dev);
262a941b784SNipun Gupta 
263*0e439ba3SNipun Gupta #ifdef CONFIG_GENERIC_MSI_IRQ
264*0e439ba3SNipun Gupta /**
265*0e439ba3SNipun Gupta  * cdx_enable_msi - Enable MSI for the CDX device.
266*0e439ba3SNipun Gupta  * @cdx_dev: device pointer
267*0e439ba3SNipun Gupta  *
268*0e439ba3SNipun Gupta  * Return: 0 for success, -errno on failure
269*0e439ba3SNipun Gupta  */
270*0e439ba3SNipun Gupta int cdx_enable_msi(struct cdx_device *cdx_dev);
271*0e439ba3SNipun Gupta 
272*0e439ba3SNipun Gupta /**
273*0e439ba3SNipun Gupta  * cdx_disable_msi - Disable MSI for the CDX device.
274*0e439ba3SNipun Gupta  * @cdx_dev: device pointer
275*0e439ba3SNipun Gupta  */
276*0e439ba3SNipun Gupta void cdx_disable_msi(struct cdx_device *cdx_dev);
277*0e439ba3SNipun Gupta 
278*0e439ba3SNipun Gupta #else /* CONFIG_GENERIC_MSI_IRQ */
279*0e439ba3SNipun Gupta 
280*0e439ba3SNipun Gupta static inline int cdx_enable_msi(struct cdx_device *cdx_dev)
281*0e439ba3SNipun Gupta {
282*0e439ba3SNipun Gupta 	return -ENODEV;
283*0e439ba3SNipun Gupta }
284*0e439ba3SNipun Gupta 
285*0e439ba3SNipun Gupta static inline void cdx_disable_msi(struct cdx_device *cdx_dev)
286*0e439ba3SNipun Gupta {
287*0e439ba3SNipun Gupta }
288*0e439ba3SNipun Gupta 
289*0e439ba3SNipun Gupta #endif /* CONFIG_GENERIC_MSI_IRQ */
290*0e439ba3SNipun Gupta 
2912959ab24SNipun Gupta #endif /* _CDX_BUS_H_ */
292