xref: /openbmc/linux/arch/s390/include/asm/pci.h (revision 21c1f902)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c6557e7fSMartin Schwidefsky #ifndef __ASM_S390_PCI_H
3c6557e7fSMartin Schwidefsky #define __ASM_S390_PCI_H
4c6557e7fSMartin Schwidefsky 
557b5918cSSebastian Ott #include <linux/pci.h>
680ed156aSSebastian Ott #include <linux/mutex.h>
7f42c2235SJoerg Roedel #include <linux/iommu.h>
8035f212fSPierre Morel #include <linux/pci_hotplug.h>
9a755a45dSJan Glauber #include <asm/pci_clp.h>
10d0b08853SJan Glauber #include <asm/pci_debug.h>
1198b1d33dSMatthew Rosato #include <asm/pci_insn.h>
12bd3a1725SMartin Schwidefsky #include <asm/sclp.h>
13c6557e7fSMartin Schwidefsky 
14cd248341SJan Glauber #define PCIBIOS_MIN_IO		0x1000
15cd248341SJan Glauber #define PCIBIOS_MIN_MEM		0x10000000
16cd248341SJan Glauber 
17cd248341SJan Glauber #define pcibios_assign_all_busses()	(0)
18cd248341SJan Glauber 
19cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *, int, unsigned long);
20cd248341SJan Glauber void pci_iounmap(struct pci_dev *, void __iomem *);
21cd248341SJan Glauber int pci_domain_nr(struct pci_bus *);
22cd248341SJan Glauber int pci_proc_domain(struct pci_bus *);
23cd248341SJan Glauber 
24cd248341SJan Glauber #define ZPCI_BUS_NR			0	/* default bus number */
25cd248341SJan Glauber 
26c840927cSSebastian Ott #define ZPCI_NR_DMA_SPACES		1
27c840927cSSebastian Ott #define ZPCI_NR_DEVICES			CONFIG_PCI_NR_FUNCTIONS
28969ae01bSNiklas Schnelle #define ZPCI_DOMAIN_BITMAP_SIZE		(1 << 16)
29c840927cSSebastian Ott 
3005bc1be6SPierre Morel #ifdef PCI
3105bc1be6SPierre Morel #if (ZPCI_NR_DEVICES > ZPCI_DOMAIN_BITMAP_SIZE)
3205bc1be6SPierre Morel # error ZPCI_NR_DEVICES can not be bigger than ZPCI_DOMAIN_BITMAP_SIZE
3305bc1be6SPierre Morel #endif
3405bc1be6SPierre Morel #endif /* PCI */
3505bc1be6SPierre Morel 
36cd248341SJan Glauber /* PCI Function Controls */
37cd248341SJan Glauber #define ZPCI_FC_FN_ENABLED		0x80
38cd248341SJan Glauber #define ZPCI_FC_ERROR			0x40
39cd248341SJan Glauber #define ZPCI_FC_BLOCKED			0x20
40cd248341SJan Glauber #define ZPCI_FC_DMA_ENABLED		0x10
41cd248341SJan Glauber 
42f9dc447eSSebastian Ott #define ZPCI_FMB_DMA_COUNTER_VALID	(1 << 23)
43f9dc447eSSebastian Ott 
44f9dc447eSSebastian Ott struct zpci_fmb_fmt0 {
45f9dc447eSSebastian Ott 	u64 dma_rbytes;
46f9dc447eSSebastian Ott 	u64 dma_wbytes;
47f9dc447eSSebastian Ott };
48f9dc447eSSebastian Ott 
49f9dc447eSSebastian Ott struct zpci_fmb_fmt1 {
50f9dc447eSSebastian Ott 	u64 rx_bytes;
51f9dc447eSSebastian Ott 	u64 rx_packets;
52f9dc447eSSebastian Ott 	u64 tx_bytes;
53f9dc447eSSebastian Ott 	u64 tx_packets;
54f9dc447eSSebastian Ott };
55f9dc447eSSebastian Ott 
56f9dc447eSSebastian Ott struct zpci_fmb_fmt2 {
57f9dc447eSSebastian Ott 	u64 consumed_work_units;
58f9dc447eSSebastian Ott 	u64 max_work_units;
59f9dc447eSSebastian Ott };
60f9dc447eSSebastian Ott 
610ac94282SSebastian Ott struct zpci_fmb_fmt3 {
620ac94282SSebastian Ott 	u64 tx_bytes;
630ac94282SSebastian Ott };
640ac94282SSebastian Ott 
65d0b08853SJan Glauber struct zpci_fmb {
66d0b08853SJan Glauber 	u32 format	: 8;
67f9dc447eSSebastian Ott 	u32 fmt_ind	: 24;
68d0b08853SJan Glauber 	u32 samples;
69d0b08853SJan Glauber 	u64 last_update;
70f9dc447eSSebastian Ott 	/* common counters */
71d0b08853SJan Glauber 	u64 ld_ops;
72d0b08853SJan Glauber 	u64 st_ops;
73d0b08853SJan Glauber 	u64 stb_ops;
74d0b08853SJan Glauber 	u64 rpcit_ops;
75f9dc447eSSebastian Ott 	/* format specific counters */
76f9dc447eSSebastian Ott 	union {
77f9dc447eSSebastian Ott 		struct zpci_fmb_fmt0 fmt0;
78f9dc447eSSebastian Ott 		struct zpci_fmb_fmt1 fmt1;
79f9dc447eSSebastian Ott 		struct zpci_fmb_fmt2 fmt2;
800ac94282SSebastian Ott 		struct zpci_fmb_fmt3 fmt3;
81f9dc447eSSebastian Ott 	};
829d89d9e6SSebastian Ott } __packed __aligned(128);
83d0b08853SJan Glauber 
84cd248341SJan Glauber enum zpci_state {
85783684f1SSebastian Ott 	ZPCI_FN_STATE_STANDBY = 0,
86783684f1SSebastian Ott 	ZPCI_FN_STATE_CONFIGURED = 1,
87783684f1SSebastian Ott 	ZPCI_FN_STATE_RESERVED = 2,
88cd248341SJan Glauber };
89cd248341SJan Glauber 
90cd248341SJan Glauber struct zpci_bar_struct {
917a572a3aSSebastian Ott 	struct resource *res;		/* bus resource */
9271ba41c9SSebastian Ott 	void __iomem	*mio_wb;
9371ba41c9SSebastian Ott 	void __iomem	*mio_wt;
94cd248341SJan Glauber 	u32		val;		/* bar start & 3 flag bits */
95cd248341SJan Glauber 	u16		map_idx;	/* index into bar mapping array */
967a572a3aSSebastian Ott 	u8		size;		/* order 2 exponent */
97cd248341SJan Glauber };
98cd248341SJan Glauber 
998128f23cSGerald Schaefer struct s390_domain;
1006438e307SMatthew Rosato struct kvm_zdev;
1018128f23cSGerald Schaefer 
10205bc1be6SPierre Morel #define ZPCI_FUNCTIONS_PER_BUS 256
10305bc1be6SPierre Morel struct zpci_bus {
10405bc1be6SPierre Morel 	struct kref		kref;
10505bc1be6SPierre Morel 	struct pci_bus		*bus;
10605bc1be6SPierre Morel 	struct zpci_dev		*function[ZPCI_FUNCTIONS_PER_BUS];
10705bc1be6SPierre Morel 	struct list_head	resources;
10805bc1be6SPierre Morel 	struct list_head	bus_next;
10965e450a9SPierre Morel 	struct resource		bus_resource;
11005bc1be6SPierre Morel 	int			pchid;
11105bc1be6SPierre Morel 	int			domain_nr;
11244510d6fSPierre Morel 	bool			multifunction;
11305bc1be6SPierre Morel 	enum pci_bus_speed	max_bus_speed;
11405bc1be6SPierre Morel };
11505bc1be6SPierre Morel 
116cd248341SJan Glauber /* Private data per function */
117cd248341SJan Glauber struct zpci_dev {
11805bc1be6SPierre Morel 	struct zpci_bus *zbus;
119cd248341SJan Glauber 	struct list_head entry;		/* list of all zpci_devices, needed for hotplug, etc. */
1201a3a7d64SNiklas Schnelle 	struct list_head iommu_list;
12105bc1be6SPierre Morel 	struct kref kref;
122*2ba8336dSNiklas Schnelle 	struct rcu_head rcu;
123035f212fSPierre Morel 	struct hotplug_slot hotplug_slot;
124cd248341SJan Glauber 
125cd248341SJan Glauber 	enum zpci_state state;
126cd248341SJan Glauber 	u32		fid;		/* function ID, used by sclp */
127cd248341SJan Glauber 	u32		fh;		/* function handle, used by insn's */
128c68468edSMatthew Rosato 	u32		gisa;		/* GISA designation for passthrough */
129ac4995b9SSebastian Ott 	u16		vfn;		/* virtual function number */
130cd248341SJan Glauber 	u16		pchid;		/* physical channel ID */
131d1038467SMatthew Rosato 	u16		maxstbl;	/* Maximum store block size */
132cd248341SJan Glauber 	u8		pfgid;		/* function group ID */
133ac4995b9SSebastian Ott 	u8		pft;		/* pci function type */
134e6ab7490SAlexander Schmidt 	u8		port;
135d1038467SMatthew Rosato 	u8		dtsm;		/* Supported DT mask */
136c9a1752bSPierre Morel 	u8		rid_available	: 1;
13744510d6fSPierre Morel 	u8		has_hp_slot	: 1;
138a50297cfSNiklas Schnelle 	u8		has_resources	: 1;
139e5794cf1SNiklas Schnelle 	u8		is_physfn	: 1;
140517fe298SMatthew Rosato 	u8		util_str_avail	: 1;
141c1e18c17SNiklas Schnelle 	u8		irqs_registered	: 1;
142c1e18c17SNiklas Schnelle 	u8		reserved	: 2;
143c9a1752bSPierre Morel 	unsigned int	devfn;		/* DEVFN part of the RID*/
144cd248341SJan Glauber 
14580ed156aSSebastian Ott 	struct mutex lock;
146ac4995b9SSebastian Ott 	u8 pfip[CLP_PFIP_NR_SEGMENTS];	/* pci function internal path */
147ac4995b9SSebastian Ott 	u32 uid;			/* user defined id */
148ac4995b9SSebastian Ott 	u8 util_str[CLP_UTIL_STR_LEN];	/* utility string */
149ac4995b9SSebastian Ott 
1509a4da8a5SJan Glauber 	/* IRQ stuff */
1519a4da8a5SJan Glauber 	u64		msi_addr;	/* MSI address */
152b19148f6SSebastian Ott 	unsigned int	max_msi;	/* maximum number of MSI's */
153e979ce7bSSebastian Ott 	unsigned int	msi_first_bit;
154e979ce7bSSebastian Ott 	unsigned int	msi_nr_irqs;
1555d0d8f43SMartin Schwidefsky 	struct airq_iv *aibv;		/* adapter interrupt bit vector */
156795818e8SSebastian Ott 	unsigned long	aisb;		/* number of the summary bit */
1579a4da8a5SJan Glauber 
158828b35f6SJan Glauber 	/* DMA stuff */
159828b35f6SJan Glauber 	unsigned long	*dma_table;
160828b35f6SJan Glauber 	int		tlb_refresh;
161828b35f6SJan Glauber 
162828b35f6SJan Glauber 	spinlock_t	iommu_bitmap_lock;
163828b35f6SJan Glauber 	unsigned long	*iommu_bitmap;
16413954fd6SSebastian Ott 	unsigned long	*lazy_bitmap;
165828b35f6SJan Glauber 	unsigned long	iommu_size;
166828b35f6SJan Glauber 	unsigned long	iommu_pages;
167828b35f6SJan Glauber 	unsigned int	next_bit;
168828b35f6SJan Glauber 
169f42c2235SJoerg Roedel 	struct iommu_device iommu_dev;  /* IOMMU core handle */
170f42c2235SJoerg Roedel 
1717a572a3aSSebastian Ott 	char res_name[16];
17271ba41c9SSebastian Ott 	bool mio_capable;
173c9c13ba4SDenis Efremov 	struct zpci_bar_struct bars[PCI_STD_NUM_BARS];
174cd248341SJan Glauber 
175828b35f6SJan Glauber 	u64		start_dma;	/* Start of available DMA addresses */
176828b35f6SJan Glauber 	u64		end_dma;	/* End of available DMA addresses */
177828b35f6SJan Glauber 	u64		dma_mask;	/* DMA address space mask */
178828b35f6SJan Glauber 
179d0b08853SJan Glauber 	/* Function measurement block */
180d0b08853SJan Glauber 	struct zpci_fmb *fmb;
181d0b08853SJan Glauber 	u16		fmb_update;	/* update interval */
1820b7589ecSSebastian Ott 	u16		fmb_length;
1836001018aSSebastian Ott 	/* software counters */
1846001018aSSebastian Ott 	atomic64_t allocated_pages;
1856001018aSSebastian Ott 	atomic64_t mapped_pages;
1866001018aSSebastian Ott 	atomic64_t unmapped_pages;
187d0b08853SJan Glauber 
188dc8c638dSMatthew Rosato 	u8		version;
189cd248341SJan Glauber 	enum pci_bus_speed max_bus_speed;
190d0b08853SJan Glauber 
191d0b08853SJan Glauber 	struct dentry	*debugfs_dev;
1928128f23cSGerald Schaefer 
1936438e307SMatthew Rosato 	/* IOMMU and passthrough */
1948128f23cSGerald Schaefer 	struct s390_domain *s390_domain; /* s390 IOMMU domain data */
1956438e307SMatthew Rosato 	struct kvm_zdev *kzdev;
19609340b2fSMatthew Rosato 	struct mutex kzdev_lock;
197cd248341SJan Glauber };
198cd248341SJan Glauber 
zdev_enabled(struct zpci_dev * zdev)199cd248341SJan Glauber static inline bool zdev_enabled(struct zpci_dev *zdev)
200cd248341SJan Glauber {
201cd248341SJan Glauber 	return (zdev->fh & (1UL << 31)) ? true : false;
202cd248341SJan Glauber }
203cd248341SJan Glauber 
20493065d04SSebastian Ott extern const struct attribute_group *zpci_attr_groups[];
205fbfe07d4SSebastian Ott extern unsigned int s390_pci_force_floating __initdata;
2066cf17f9aSPierre Morel extern unsigned int s390_pci_no_rid;
20793065d04SSebastian Ott 
20898b1d33dSMatthew Rosato extern union zpci_sic_iib *zpci_aipb;
20998b1d33dSMatthew Rosato extern struct airq_iv *zpci_aif_sbv;
21098b1d33dSMatthew Rosato 
211cd248341SJan Glauber /* -----------------------------------------------------------------------------
212cd248341SJan Glauber   Prototypes
213cd248341SJan Glauber ----------------------------------------------------------------------------- */
214cd248341SJan Glauber /* Base stuff */
21514c87ba8SNiklas Schnelle struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state);
216cd248341SJan Glauber int zpci_enable_device(struct zpci_dev *);
217cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *);
218a7f82c36SNiklas Schnelle int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh);
2192631f6b6SNiklas Schnelle int zpci_deconfigure_device(struct zpci_dev *zdev);
220a46044a9SNiklas Schnelle void zpci_device_reserved(struct zpci_dev *zdev);
221a46044a9SNiklas Schnelle bool zpci_is_device_configured(struct zpci_dev *zdev);
2222631f6b6SNiklas Schnelle 
223da995d53SNiklas Schnelle int zpci_hot_reset_device(struct zpci_dev *zdev);
22459bbf596SNiklas Schnelle int zpci_register_ioat(struct zpci_dev *, u8, u64, u64, u64, u8 *);
225828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *, u8);
22601553d9aSSebastian Ott void zpci_remove_reserved_devices(void);
2274fe20497SNiklas Schnelle void zpci_update_fh(struct zpci_dev *zdev, u32 fh);
228cd248341SJan Glauber 
229a755a45dSJan Glauber /* CLP */
230b02002ccSNiklas Schnelle int clp_setup_writeback_mio(void);
2311d578966SSebastian Ott int clp_scan_pci_devices(void);
232ba764dd7SNiklas Schnelle int clp_query_pci_fn(struct zpci_dev *zdev);
233cc049eecSNiklas Schnelle int clp_enable_fh(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as);
234cc049eecSNiklas Schnelle int clp_disable_fh(struct zpci_dev *zdev, u32 *fh);
235783684f1SSebastian Ott int clp_get_state(u32 fid, enum zpci_state *state);
236cc049eecSNiklas Schnelle int clp_refresh_fh(u32 fid, u32 *fh);
237a755a45dSJan Glauber 
2387a11c67aSNiklas Schnelle /* UID */
2397a11c67aSNiklas Schnelle void update_uid_checking(bool new);
2407a11c67aSNiklas Schnelle 
241f42c2235SJoerg Roedel /* IOMMU Interface */
242f42c2235SJoerg Roedel int zpci_init_iommu(struct zpci_dev *zdev);
243f42c2235SJoerg Roedel void zpci_destroy_iommu(struct zpci_dev *zdev);
244f42c2235SJoerg Roedel 
2459a17e972SSebastian Ott #ifdef CONFIG_PCI
zpci_use_mio(struct zpci_dev * zdev)246c7ff0e91SSebastian Ott static inline bool zpci_use_mio(struct zpci_dev *zdev)
247c7ff0e91SSebastian Ott {
248c7ff0e91SSebastian Ott 	return static_branch_likely(&have_mio) && zdev->mio_capable;
249c7ff0e91SSebastian Ott }
250c7ff0e91SSebastian Ott 
251cbc0dd1fSJan Glauber /* Error handling and recovery */
252cbc0dd1fSJan Glauber void zpci_event_error(void *);
253cbc0dd1fSJan Glauber void zpci_event_availability(void *);
254aa3b7c29SSebastian Ott bool zpci_is_enabled(void);
2559a17e972SSebastian Ott #else /* CONFIG_PCI */
zpci_event_error(void * e)2569a17e972SSebastian Ott static inline void zpci_event_error(void *e) {}
zpci_event_availability(void * e)2579a17e972SSebastian Ott static inline void zpci_event_availability(void *e) {}
2589a17e972SSebastian Ott #endif /* CONFIG_PCI */
259cbc0dd1fSJan Glauber 
26067f43f38SSebastian Ott #ifdef CONFIG_HOTPLUG_PCI_S390
26167f43f38SSebastian Ott int zpci_init_slot(struct zpci_dev *);
26267f43f38SSebastian Ott void zpci_exit_slot(struct zpci_dev *);
26367f43f38SSebastian Ott #else /* CONFIG_HOTPLUG_PCI_S390 */
zpci_init_slot(struct zpci_dev * zdev)26467f43f38SSebastian Ott static inline int zpci_init_slot(struct zpci_dev *zdev)
26567f43f38SSebastian Ott {
26667f43f38SSebastian Ott 	return 0;
26767f43f38SSebastian Ott }
zpci_exit_slot(struct zpci_dev * zdev)26867f43f38SSebastian Ott static inline void zpci_exit_slot(struct zpci_dev *zdev) {}
26967f43f38SSebastian Ott #endif /* CONFIG_HOTPLUG_PCI_S390 */
27067f43f38SSebastian Ott 
271cd248341SJan Glauber /* Helpers */
to_zpci(struct pci_dev * pdev)272198a5278SSebastian Ott static inline struct zpci_dev *to_zpci(struct pci_dev *pdev)
273198a5278SSebastian Ott {
27405bc1be6SPierre Morel 	struct zpci_bus *zbus = pdev->sysdata;
27505bc1be6SPierre Morel 
27644510d6fSPierre Morel 	return zbus->function[pdev->devfn];
277198a5278SSebastian Ott }
278198a5278SSebastian Ott 
to_zpci_dev(struct device * dev)279d08d6f5dSPierre Morel static inline struct zpci_dev *to_zpci_dev(struct device *dev)
280d08d6f5dSPierre Morel {
281d08d6f5dSPierre Morel 	return to_zpci(to_pci_dev(dev));
282d08d6f5dSPierre Morel }
283d08d6f5dSPierre Morel 
284cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32);
285cd248341SJan Glauber 
286828b35f6SJan Glauber /* DMA */
287828b35f6SJan Glauber int zpci_dma_init(void);
288828b35f6SJan Glauber void zpci_dma_exit(void);
2891f3f7681SNiklas Schnelle int zpci_dma_init_device(struct zpci_dev *zdev);
2901f3f7681SNiklas Schnelle int zpci_dma_exit_device(struct zpci_dev *zdev);
291828b35f6SJan Glauber 
292c1e18c17SNiklas Schnelle /* IRQ */
293c840927cSSebastian Ott int __init zpci_irq_init(void);
294c840927cSSebastian Ott void __init zpci_irq_exit(void);
295c840927cSSebastian Ott 
296d0b08853SJan Glauber /* FMB */
297d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *);
298d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *);
299d0b08853SJan Glauber 
300d0b08853SJan Glauber /* Debug */
301d0b08853SJan Glauber int zpci_debug_init(void);
302d0b08853SJan Glauber void zpci_debug_exit(void);
3039a99649fSSebastian Ott void zpci_debug_init_device(struct zpci_dev *, const char *);
304d0b08853SJan Glauber void zpci_debug_exit_device(struct zpci_dev *);
305d0b08853SJan Glauber 
3064cdf2f4eSNiklas Schnelle /* Error handling */
307bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *, struct zpci_report_error_header *);
3084cdf2f4eSNiklas Schnelle int zpci_clear_error_state(struct zpci_dev *zdev);
3094cdf2f4eSNiklas Schnelle int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
310bd3a1725SMartin Schwidefsky 
3113a368f74SPhilipp Hachtmann #ifdef CONFIG_NUMA
3123a368f74SPhilipp Hachtmann 
3133a368f74SPhilipp Hachtmann /* Returns the node based on PCI bus */
__pcibus_to_node(const struct pci_bus * bus)3143a368f74SPhilipp Hachtmann static inline int __pcibus_to_node(const struct pci_bus *bus)
3153a368f74SPhilipp Hachtmann {
3163a368f74SPhilipp Hachtmann 	return NUMA_NO_NODE;
3173a368f74SPhilipp Hachtmann }
3183a368f74SPhilipp Hachtmann 
3193a368f74SPhilipp Hachtmann static inline const struct cpumask *
cpumask_of_pcibus(const struct pci_bus * bus)3203a368f74SPhilipp Hachtmann cpumask_of_pcibus(const struct pci_bus *bus)
3213a368f74SPhilipp Hachtmann {
3223a368f74SPhilipp Hachtmann 	return cpu_online_mask;
3233a368f74SPhilipp Hachtmann }
3243a368f74SPhilipp Hachtmann 
3253a368f74SPhilipp Hachtmann #endif /* CONFIG_NUMA */
3263a368f74SPhilipp Hachtmann 
327cd248341SJan Glauber #endif
328