1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_S390_PCI_H
3 #define __ASM_S390_PCI_H
4 
5 #include <linux/pci.h>
6 #include <linux/mutex.h>
7 #include <linux/iommu.h>
8 #include <linux/pci_hotplug.h>
9 #include <asm/pci_clp.h>
10 #include <asm/pci_debug.h>
11 #include <asm/pci_insn.h>
12 #include <asm/sclp.h>
13 
14 #define ARCH_GENERIC_PCI_MMAP_RESOURCE	1
15 #define arch_can_pci_mmap_wc()		1
16 
17 #define PCIBIOS_MIN_IO		0x1000
18 #define PCIBIOS_MIN_MEM		0x10000000
19 
20 #define pcibios_assign_all_busses()	(0)
21 
22 void __iomem *pci_iomap(struct pci_dev *, int, unsigned long);
23 void pci_iounmap(struct pci_dev *, void __iomem *);
24 int pci_domain_nr(struct pci_bus *);
25 int pci_proc_domain(struct pci_bus *);
26 
27 #define ZPCI_BUS_NR			0	/* default bus number */
28 
29 #define ZPCI_NR_DMA_SPACES		1
30 #define ZPCI_NR_DEVICES			CONFIG_PCI_NR_FUNCTIONS
31 #define ZPCI_DOMAIN_BITMAP_SIZE		(1 << 16)
32 
33 #ifdef PCI
34 #if (ZPCI_NR_DEVICES > ZPCI_DOMAIN_BITMAP_SIZE)
35 # error ZPCI_NR_DEVICES can not be bigger than ZPCI_DOMAIN_BITMAP_SIZE
36 #endif
37 #endif /* PCI */
38 
39 /* PCI Function Controls */
40 #define ZPCI_FC_FN_ENABLED		0x80
41 #define ZPCI_FC_ERROR			0x40
42 #define ZPCI_FC_BLOCKED			0x20
43 #define ZPCI_FC_DMA_ENABLED		0x10
44 
45 #define ZPCI_FMB_DMA_COUNTER_VALID	(1 << 23)
46 
47 struct zpci_fmb_fmt0 {
48 	u64 dma_rbytes;
49 	u64 dma_wbytes;
50 };
51 
52 struct zpci_fmb_fmt1 {
53 	u64 rx_bytes;
54 	u64 rx_packets;
55 	u64 tx_bytes;
56 	u64 tx_packets;
57 };
58 
59 struct zpci_fmb_fmt2 {
60 	u64 consumed_work_units;
61 	u64 max_work_units;
62 };
63 
64 struct zpci_fmb_fmt3 {
65 	u64 tx_bytes;
66 };
67 
68 struct zpci_fmb {
69 	u32 format	: 8;
70 	u32 fmt_ind	: 24;
71 	u32 samples;
72 	u64 last_update;
73 	/* common counters */
74 	u64 ld_ops;
75 	u64 st_ops;
76 	u64 stb_ops;
77 	u64 rpcit_ops;
78 	/* format specific counters */
79 	union {
80 		struct zpci_fmb_fmt0 fmt0;
81 		struct zpci_fmb_fmt1 fmt1;
82 		struct zpci_fmb_fmt2 fmt2;
83 		struct zpci_fmb_fmt3 fmt3;
84 	};
85 } __packed __aligned(128);
86 
87 enum zpci_state {
88 	ZPCI_FN_STATE_STANDBY = 0,
89 	ZPCI_FN_STATE_CONFIGURED = 1,
90 	ZPCI_FN_STATE_RESERVED = 2,
91 };
92 
93 struct zpci_bar_struct {
94 	struct resource *res;		/* bus resource */
95 	void __iomem	*mio_wb;
96 	void __iomem	*mio_wt;
97 	u32		val;		/* bar start & 3 flag bits */
98 	u16		map_idx;	/* index into bar mapping array */
99 	u8		size;		/* order 2 exponent */
100 };
101 
102 struct kvm_zdev;
103 
104 #define ZPCI_FUNCTIONS_PER_BUS 256
105 struct zpci_bus {
106 	struct kref		kref;
107 	struct pci_bus		*bus;
108 	struct zpci_dev		*function[ZPCI_FUNCTIONS_PER_BUS];
109 	struct list_head	resources;
110 	struct list_head	bus_next;
111 	struct resource		bus_resource;
112 	int			topo;		/* TID if topo_is_tid, PCHID otherwise */
113 	int			domain_nr;
114 	u8			multifunction	: 1;
115 	u8			topo_is_tid	: 1;
116 	enum pci_bus_speed	max_bus_speed;
117 };
118 
119 /* Private data per function */
120 struct zpci_dev {
121 	struct zpci_bus *zbus;
122 	struct list_head entry;		/* list of all zpci_devices, needed for hotplug, etc. */
123 	struct list_head iommu_list;
124 	struct kref kref;
125 	struct rcu_head rcu;
126 	struct hotplug_slot hotplug_slot;
127 
128 	struct mutex state_lock;	/* protect state changes */
129 	enum zpci_state state;
130 	u32		fid;		/* function ID, used by sclp */
131 	u32		fh;		/* function handle, used by insn's */
132 	u32		gisa;		/* GISA designation for passthrough */
133 	u16		vfn;		/* virtual function number */
134 	u16		pchid;		/* physical channel ID */
135 	u16		maxstbl;	/* Maximum store block size */
136 	u16		rid;		/* RID as supplied by firmware */
137 	u16		tid;		/* Topology for which RID is valid */
138 	u8		pfgid;		/* function group ID */
139 	u8		pft;		/* pci function type */
140 	u8		port;
141 	u8		fidparm;
142 	u8		dtsm;		/* Supported DT mask */
143 	u8		rid_available	: 1;
144 	u8		has_hp_slot	: 1;
145 	u8		has_resources	: 1;
146 	u8		is_physfn	: 1;
147 	u8		util_str_avail	: 1;
148 	u8		irqs_registered	: 1;
149 	u8		tid_avail	: 1;
150 	u8		rtr_avail	: 1; /* Relaxed translation allowed */
151 	unsigned int	devfn;		/* DEVFN part of the RID*/
152 
153 	u8 pfip[CLP_PFIP_NR_SEGMENTS];	/* pci function internal path */
154 	u32 uid;			/* user defined id */
155 	u8 util_str[CLP_UTIL_STR_LEN];	/* utility string */
156 
157 	/* IRQ stuff */
158 	u64		msi_addr;	/* MSI address */
159 	unsigned int	max_msi;	/* maximum number of MSI's */
160 	unsigned int	msi_first_bit;
161 	unsigned int	msi_nr_irqs;
162 	struct airq_iv *aibv;		/* adapter interrupt bit vector */
163 	unsigned long	aisb;		/* number of the summary bit */
164 
165 	/* DMA stuff */
166 	unsigned long	*dma_table;
167 	int		tlb_refresh;
168 
169 	struct iommu_device iommu_dev;  /* IOMMU core handle */
170 
171 	char res_name[16];
172 	bool mio_capable;
173 	struct zpci_bar_struct bars[PCI_STD_NUM_BARS];
174 
175 	u64		start_dma;	/* Start of available DMA addresses */
176 	u64		end_dma;	/* End of available DMA addresses */
177 	u64		dma_mask;	/* DMA address space mask */
178 
179 	/* Function measurement block */
180 	struct mutex fmb_lock;
181 	struct zpci_fmb *fmb;
182 	u16		fmb_update;	/* update interval */
183 	u16		fmb_length;
184 
185 	u8		version;
186 	enum pci_bus_speed max_bus_speed;
187 
188 	struct dentry	*debugfs_dev;
189 
190 	/* IOMMU and passthrough */
191 	struct iommu_domain *s390_domain; /* attached IOMMU domain */
192 	struct kvm_zdev *kzdev;
193 	struct mutex kzdev_lock;
194 	spinlock_t dom_lock;		/* protect s390_domain change */
195 };
196 
197 static inline bool zdev_enabled(struct zpci_dev *zdev)
198 {
199 	return (zdev->fh & (1UL << 31)) ? true : false;
200 }
201 
202 extern const struct attribute_group zpci_attr_group;
203 extern const struct attribute_group pfip_attr_group;
204 extern const struct attribute_group zpci_ident_attr_group;
205 
206 #define ARCH_PCI_DEV_GROUPS &zpci_attr_group,		 \
207 			    &pfip_attr_group,		 \
208 			    &zpci_ident_attr_group,
209 
210 extern unsigned int s390_pci_force_floating __initdata;
211 extern unsigned int s390_pci_no_rid;
212 
213 extern union zpci_sic_iib *zpci_aipb;
214 extern struct airq_iv *zpci_aif_sbv;
215 
216 /* -----------------------------------------------------------------------------
217   Prototypes
218 ----------------------------------------------------------------------------- */
219 /* Base stuff */
220 struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state);
221 int zpci_add_device(struct zpci_dev *zdev);
222 int zpci_enable_device(struct zpci_dev *);
223 int zpci_reenable_device(struct zpci_dev *zdev);
224 int zpci_disable_device(struct zpci_dev *);
225 int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh);
226 int zpci_deconfigure_device(struct zpci_dev *zdev);
227 void zpci_device_reserved(struct zpci_dev *zdev);
228 bool zpci_is_device_configured(struct zpci_dev *zdev);
229 int zpci_scan_devices(void);
230 
231 int zpci_hot_reset_device(struct zpci_dev *zdev);
232 int zpci_register_ioat(struct zpci_dev *, u8, u64, u64, u64, u8 *);
233 int zpci_unregister_ioat(struct zpci_dev *, u8);
234 void zpci_remove_reserved_devices(void);
235 void zpci_update_fh(struct zpci_dev *zdev, u32 fh);
236 
237 /* CLP */
238 int clp_setup_writeback_mio(void);
239 int clp_scan_pci_devices(struct list_head *scan_list);
240 int clp_query_pci_fn(struct zpci_dev *zdev);
241 int clp_enable_fh(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as);
242 int clp_disable_fh(struct zpci_dev *zdev, u32 *fh);
243 int clp_get_state(u32 fid, enum zpci_state *state);
244 int clp_refresh_fh(u32 fid, u32 *fh);
245 
246 /* UID */
247 void update_uid_checking(bool new);
248 
249 /* IOMMU Interface */
250 int zpci_init_iommu(struct zpci_dev *zdev);
251 void zpci_destroy_iommu(struct zpci_dev *zdev);
252 int zpci_iommu_register_ioat(struct zpci_dev *zdev, u8 *status);
253 
254 #ifdef CONFIG_PCI
255 static inline bool zpci_use_mio(struct zpci_dev *zdev)
256 {
257 	return static_branch_likely(&have_mio) && zdev->mio_capable;
258 }
259 
260 /* Error handling and recovery */
261 void zpci_event_error(void *);
262 void zpci_event_availability(void *);
263 bool zpci_is_enabled(void);
264 #else /* CONFIG_PCI */
265 static inline void zpci_event_error(void *e) {}
266 static inline void zpci_event_availability(void *e) {}
267 #endif /* CONFIG_PCI */
268 
269 #ifdef CONFIG_HOTPLUG_PCI_S390
270 int zpci_init_slot(struct zpci_dev *);
271 void zpci_exit_slot(struct zpci_dev *);
272 #else /* CONFIG_HOTPLUG_PCI_S390 */
273 static inline int zpci_init_slot(struct zpci_dev *zdev)
274 {
275 	return 0;
276 }
277 static inline void zpci_exit_slot(struct zpci_dev *zdev) {}
278 #endif /* CONFIG_HOTPLUG_PCI_S390 */
279 
280 /* Helpers */
281 static inline struct zpci_dev *to_zpci(struct pci_dev *pdev)
282 {
283 	struct zpci_bus *zbus = pdev->sysdata;
284 
285 	return zbus->function[pdev->devfn];
286 }
287 
288 static inline struct zpci_dev *to_zpci_dev(struct device *dev)
289 {
290 	return to_zpci(to_pci_dev(dev));
291 }
292 
293 struct zpci_dev *get_zdev_by_fid(u32);
294 
295 /* DMA */
296 int zpci_dma_init(void);
297 void zpci_dma_exit(void);
298 int zpci_dma_init_device(struct zpci_dev *zdev);
299 int zpci_dma_exit_device(struct zpci_dev *zdev);
300 
301 /* IRQ */
302 int __init zpci_irq_init(void);
303 void __init zpci_irq_exit(void);
304 
305 /* FMB */
306 int zpci_fmb_enable_device(struct zpci_dev *);
307 int zpci_fmb_disable_device(struct zpci_dev *);
308 
309 /* Debug */
310 int zpci_debug_init(void);
311 void zpci_debug_exit(void);
312 void zpci_debug_init_device(struct zpci_dev *, const char *);
313 void zpci_debug_exit_device(struct zpci_dev *);
314 
315 /* Error handling */
316 int zpci_report_error(struct pci_dev *, struct zpci_report_error_header *);
317 int zpci_clear_error_state(struct zpci_dev *zdev);
318 int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
319 
320 #ifdef CONFIG_NUMA
321 
322 /* Returns the node based on PCI bus */
323 static inline int __pcibus_to_node(const struct pci_bus *bus)
324 {
325 	return NUMA_NO_NODE;
326 }
327 
328 static inline const struct cpumask *
329 cpumask_of_pcibus(const struct pci_bus *bus)
330 {
331 	return cpu_online_mask;
332 }
333 
334 #endif /* CONFIG_NUMA */
335 
336 #endif
337