xref: /openbmc/linux/include/linux/cdx/cdx_bus.h (revision 55b24334)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * CDX bus public interface
4  *
5  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
6  *
7  */
8 
9 #ifndef _CDX_BUS_H_
10 #define _CDX_BUS_H_
11 
12 #include <linux/device.h>
13 #include <linux/list.h>
14 #include <linux/mod_devicetable.h>
15 
16 #define MAX_CDX_DEV_RESOURCES	4
17 #define CDX_ANY_ID (0xFFFF)
18 #define CDX_CONTROLLER_ID_SHIFT 4
19 #define CDX_BUS_NUM_MASK 0xF
20 
21 /* Forward declaration for CDX controller */
22 struct cdx_controller;
23 
24 enum {
25 	CDX_DEV_RESET_CONF,
26 };
27 
28 struct cdx_device_config {
29 	u8 type;
30 };
31 
32 typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
33 
34 typedef int (*cdx_dev_configure_cb)(struct cdx_controller *cdx,
35 				    u8 bus_num, u8 dev_num,
36 				    struct cdx_device_config *dev_config);
37 
38 /**
39  * CDX_DEVICE_DRIVER_OVERRIDE - macro used to describe a CDX device with
40  *                              override_only flags.
41  * @vend: the 16 bit CDX Vendor ID
42  * @dev: the 16 bit CDX Device ID
43  * @driver_override: the 32 bit CDX Device override_only
44  *
45  * This macro is used to create a struct cdx_device_id that matches only a
46  * driver_override device.
47  */
48 #define CDX_DEVICE_DRIVER_OVERRIDE(vend, dev, driver_override) \
49 	.vendor = (vend), .device = (dev), .override_only = (driver_override)
50 
51 /**
52  * struct cdx_ops - Callbacks supported by CDX controller.
53  * @scan: scan the devices on the controller
54  * @dev_configure: configuration like reset, master_enable,
55  *		   msi_config etc for a CDX device
56  */
57 struct cdx_ops {
58 	cdx_scan_cb scan;
59 	cdx_dev_configure_cb dev_configure;
60 };
61 
62 /**
63  * struct cdx_controller: CDX controller object
64  * @dev: Linux device associated with the CDX controller.
65  * @priv: private data
66  * @id: Controller ID
67  * @ops: CDX controller ops
68  */
69 struct cdx_controller {
70 	struct device *dev;
71 	void *priv;
72 	u32 id;
73 	struct cdx_ops *ops;
74 };
75 
76 /**
77  * struct cdx_device - CDX device object
78  * @dev: Linux driver model device object
79  * @cdx: CDX controller associated with the device
80  * @vendor: Vendor ID for CDX device
81  * @device: Device ID for CDX device
82  * @bus_num: Bus number for this CDX device
83  * @dev_num: Device number for this device
84  * @res: array of MMIO region entries
85  * @res_attr: resource binary attribute
86  * @res_count: number of valid MMIO regions
87  * @dma_mask: Default DMA mask
88  * @flags: CDX device flags
89  * @req_id: Requestor ID associated with CDX device
90  * @driver_override: driver name to force a match; do not set directly,
91  *                   because core frees it; use driver_set_override() to
92  *                   set or clear it.
93  */
94 struct cdx_device {
95 	struct device dev;
96 	struct cdx_controller *cdx;
97 	u16 vendor;
98 	u16 device;
99 	u8 bus_num;
100 	u8 dev_num;
101 	struct resource res[MAX_CDX_DEV_RESOURCES];
102 	u8 res_count;
103 	u64 dma_mask;
104 	u16 flags;
105 	u32 req_id;
106 	const char *driver_override;
107 };
108 
109 #define to_cdx_device(_dev) \
110 	container_of(_dev, struct cdx_device, dev)
111 
112 /**
113  * struct cdx_driver - CDX device driver
114  * @driver: Generic device driver
115  * @match_id_table: table of supported device matching Ids
116  * @probe: Function called when a device is added
117  * @remove: Function called when a device is removed
118  * @shutdown: Function called at shutdown time to quiesce the device
119  * @reset_prepare: Function called before is reset to notify driver
120  * @reset_done: Function called after reset is complete to notify driver
121  * @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA.
122  *		For most device drivers, no need to care about this flag
123  *		as long as all DMAs are handled through the kernel DMA API.
124  *		For some special ones, for example VFIO drivers, they know
125  *		how to manage the DMA themselves and set this flag so that
126  *		the IOMMU layer will allow them to setup and manage their
127  *		own I/O address space.
128  */
129 struct cdx_driver {
130 	struct device_driver driver;
131 	const struct cdx_device_id *match_id_table;
132 	int (*probe)(struct cdx_device *dev);
133 	int (*remove)(struct cdx_device *dev);
134 	void (*shutdown)(struct cdx_device *dev);
135 	void (*reset_prepare)(struct cdx_device *dev);
136 	void (*reset_done)(struct cdx_device *dev);
137 	bool driver_managed_dma;
138 };
139 
140 #define to_cdx_driver(_drv) \
141 	container_of(_drv, struct cdx_driver, driver)
142 
143 /* Macro to avoid include chaining to get THIS_MODULE */
144 #define cdx_driver_register(drv) \
145 	__cdx_driver_register(drv, THIS_MODULE)
146 
147 /**
148  * __cdx_driver_register - registers a CDX device driver
149  * @cdx_driver: CDX driver to register
150  * @owner: module owner
151  *
152  * Return: -errno on failure, 0 on success.
153  */
154 int __must_check __cdx_driver_register(struct cdx_driver *cdx_driver,
155 				       struct module *owner);
156 
157 /**
158  * cdx_driver_unregister - unregisters a device driver from the
159  * CDX bus.
160  * @cdx_driver: CDX driver to register
161  */
162 void cdx_driver_unregister(struct cdx_driver *cdx_driver);
163 
164 extern struct bus_type cdx_bus_type;
165 
166 /**
167  * cdx_dev_reset - Reset CDX device
168  * @dev: device pointer
169  *
170  * Return: 0 for success, -errno on failure
171  */
172 int cdx_dev_reset(struct device *dev);
173 
174 #endif /* _CDX_BUS_H_ */
175