xref: /linux/arch/s390/pci/pci.c (revision 6ee600bfbe0f818ffb7748d99e9b0c89d0d9f02a)
1adbb3901SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cd248341SJan Glauber /*
3cd248341SJan Glauber  * Copyright IBM Corp. 2012
4cd248341SJan Glauber  *
5cd248341SJan Glauber  * Author(s):
6cd248341SJan Glauber  *   Jan Glauber <jang@linux.vnet.ibm.com>
7cd248341SJan Glauber  *
8cd248341SJan Glauber  * The System z PCI code is a rewrite from a prototype by
9cd248341SJan Glauber  * the following people (Kudoz!):
10bedef755SJan Glauber  *   Alexander Schmidt
11bedef755SJan Glauber  *   Christoph Raisch
12bedef755SJan Glauber  *   Hannes Hering
13bedef755SJan Glauber  *   Hoang-Nam Nguyen
14bedef755SJan Glauber  *   Jan-Bernd Themann
15bedef755SJan Glauber  *   Stefan Roscher
16bedef755SJan Glauber  *   Thomas Klein
17cd248341SJan Glauber  */
18cd248341SJan Glauber 
19896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci"
20896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21cd248341SJan Glauber 
22cd248341SJan Glauber #include <linux/kernel.h>
23cd248341SJan Glauber #include <linux/slab.h>
24cd248341SJan Glauber #include <linux/err.h>
25cd248341SJan Glauber #include <linux/export.h>
26cd248341SJan Glauber #include <linux/delay.h>
27cd248341SJan Glauber #include <linux/seq_file.h>
2871ba41c9SSebastian Ott #include <linux/jump_label.h>
29cd248341SJan Glauber #include <linux/pci.h>
30794b8846SNiklas Schnelle #include <linux/printk.h>
31bcb5d6c7SGerd Bayer #include <linux/lockdep.h>
32cd248341SJan Glauber 
339a4da8a5SJan Glauber #include <asm/isc.h>
349a4da8a5SJan Glauber #include <asm/airq.h>
35cd248341SJan Glauber #include <asm/facility.h>
36cd248341SJan Glauber #include <asm/pci_insn.h>
37a755a45dSJan Glauber #include <asm/pci_clp.h>
38828b35f6SJan Glauber #include <asm/pci_dma.h>
39cd248341SJan Glauber 
4005bc1be6SPierre Morel #include "pci_bus.h"
41abb95b75SNiklas Schnelle #include "pci_iov.h"
4205bc1be6SPierre Morel 
43cd248341SJan Glauber /* list of all detected zpci devices */
4467f43f38SSebastian Ott static LIST_HEAD(zpci_list);
4557b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock);
461f44a225SMartin Schwidefsky 
47969ae01bSNiklas Schnelle static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE);
48cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock);
49cd248341SJan Glauber 
50c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES						\
51c9c13ba4SDenis Efremov 	min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2),	\
52c506fff3SSebastian Ott 	    ZPCI_IOMAP_MAX_ENTRIES)
53c506fff3SSebastian Ott 
546cf17f9aSPierre Morel unsigned int s390_pci_no_rid;
556cf17f9aSPierre Morel 
56cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock);
57c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap;
58cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start;
59cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start);
60cd248341SJan Glauber 
6171ba41c9SSebastian Ott DEFINE_STATIC_KEY_FALSE(have_mio);
6271ba41c9SSebastian Ott 
63d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache;
64d0b08853SJan Glauber 
6598b1d33dSMatthew Rosato /* AEN structures that must be preserved over KVM module re-insertion */
6698b1d33dSMatthew Rosato union zpci_sic_iib *zpci_aipb;
6798b1d33dSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_aipb);
6898b1d33dSMatthew Rosato struct airq_iv *zpci_aif_sbv;
6998b1d33dSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_aif_sbv);
7098b1d33dSMatthew Rosato 
71cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid)
72cd248341SJan Glauber {
73cd248341SJan Glauber 	struct zpci_dev *tmp, *zdev = NULL;
74cd248341SJan Glauber 
7557b5918cSSebastian Ott 	spin_lock(&zpci_list_lock);
76cd248341SJan Glauber 	list_for_each_entry(tmp, &zpci_list, entry) {
77cd248341SJan Glauber 		if (tmp->fid == fid) {
78cd248341SJan Glauber 			zdev = tmp;
79c122383dSNiklas Schnelle 			zpci_zdev_get(zdev);
80cd248341SJan Glauber 			break;
81cd248341SJan Glauber 		}
82cd248341SJan Glauber 	}
8357b5918cSSebastian Ott 	spin_unlock(&zpci_list_lock);
84cd248341SJan Glauber 	return zdev;
85cd248341SJan Glauber }
86cd248341SJan Glauber 
8701553d9aSSebastian Ott void zpci_remove_reserved_devices(void)
8801553d9aSSebastian Ott {
8901553d9aSSebastian Ott 	struct zpci_dev *tmp, *zdev;
9001553d9aSSebastian Ott 	enum zpci_state state;
9101553d9aSSebastian Ott 	LIST_HEAD(remove);
9201553d9aSSebastian Ott 
9301553d9aSSebastian Ott 	spin_lock(&zpci_list_lock);
9401553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
9501553d9aSSebastian Ott 		if (zdev->state == ZPCI_FN_STATE_STANDBY &&
9601553d9aSSebastian Ott 		    !clp_get_state(zdev->fid, &state) &&
9701553d9aSSebastian Ott 		    state == ZPCI_FN_STATE_RESERVED)
9801553d9aSSebastian Ott 			list_move_tail(&zdev->entry, &remove);
9901553d9aSSebastian Ott 	}
10001553d9aSSebastian Ott 	spin_unlock(&zpci_list_lock);
10101553d9aSSebastian Ott 
10201553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &remove, entry)
103a46044a9SNiklas Schnelle 		zpci_device_reserved(zdev);
104cd248341SJan Glauber }
105cd248341SJan Glauber 
106cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus)
107cd248341SJan Glauber {
10805bc1be6SPierre Morel 	return ((struct zpci_bus *) bus->sysdata)->domain_nr;
109cd248341SJan Glauber }
110cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr);
111cd248341SJan Glauber 
112cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus)
113cd248341SJan Glauber {
114cd248341SJan Glauber 	return pci_domain_nr(bus);
115cd248341SJan Glauber }
116cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain);
117cd248341SJan Glauber 
118828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */
119828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
12059bbf596SNiklas Schnelle 		       u64 base, u64 limit, u64 iota, u8 *status)
121828b35f6SJan Glauber {
12272570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
12372570834SSebastian Ott 	struct zpci_fib fib = {0};
12459bbf596SNiklas Schnelle 	u8 cc;
125828b35f6SJan Glauber 
126828b35f6SJan Glauber 	WARN_ON_ONCE(iota & 0x3fff);
12772570834SSebastian Ott 	fib.pba = base;
128c76c067eSNiklas Schnelle 	/* Work around off by one in ISM virt device */
129c76c067eSNiklas Schnelle 	if (zdev->pft == PCI_FUNC_TYPE_ISM && limit > base)
130c76c067eSNiklas Schnelle 		fib.pal = limit + (1 << 12);
131c76c067eSNiklas Schnelle 	else
13272570834SSebastian Ott 		fib.pal = limit;
13372570834SSebastian Ott 	fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
134c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
13559bbf596SNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, status);
1361f3f7681SNiklas Schnelle 	if (cc)
13759bbf596SNiklas Schnelle 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, *status);
1381f3f7681SNiklas Schnelle 	return cc;
139828b35f6SJan Glauber }
14009340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_register_ioat);
141828b35f6SJan Glauber 
142828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */
143828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
144828b35f6SJan Glauber {
14572570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
14672570834SSebastian Ott 	struct zpci_fib fib = {0};
14772570834SSebastian Ott 	u8 cc, status;
148828b35f6SJan Glauber 
149c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
150c68468edSMatthew Rosato 
15172570834SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1521f3f7681SNiklas Schnelle 	if (cc)
1531f3f7681SNiklas Schnelle 		zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
1541f3f7681SNiklas Schnelle 	return cc;
155828b35f6SJan Glauber }
156828b35f6SJan Glauber 
157d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */
158d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev)
159d0b08853SJan Glauber {
1604e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
16192bce97fSNiklas Schnelle 	struct zpci_iommu_ctrs *ctrs;
1624e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1634e5bd780SSebastian Ott 	u8 cc, status;
164d0b08853SJan Glauber 
1650b7589ecSSebastian Ott 	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
166d0b08853SJan Glauber 		return -EINVAL;
167d0b08853SJan Glauber 
16808b42124SWei Yongjun 	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
169d0b08853SJan Glauber 	if (!zdev->fmb)
170d0b08853SJan Glauber 		return -ENOMEM;
171d0b08853SJan Glauber 	WARN_ON((u64) zdev->fmb & 0xf);
172d0b08853SJan Glauber 
1736001018aSSebastian Ott 	/* reset software counters */
17492bce97fSNiklas Schnelle 	ctrs = zpci_get_iommu_ctrs(zdev);
17592bce97fSNiklas Schnelle 	if (ctrs) {
17692bce97fSNiklas Schnelle 		atomic64_set(&ctrs->mapped_pages, 0);
17792bce97fSNiklas Schnelle 		atomic64_set(&ctrs->unmapped_pages, 0);
17892bce97fSNiklas Schnelle 		atomic64_set(&ctrs->global_rpcits, 0);
17992bce97fSNiklas Schnelle 		atomic64_set(&ctrs->sync_map_rpcits, 0);
18092bce97fSNiklas Schnelle 		atomic64_set(&ctrs->sync_rpcits, 0);
18192bce97fSNiklas Schnelle 	}
18292bce97fSNiklas Schnelle 
1836001018aSSebastian Ott 
1844e5bd780SSebastian Ott 	fib.fmb_addr = virt_to_phys(zdev->fmb);
185c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
1864e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1874e5bd780SSebastian Ott 	if (cc) {
1884e5bd780SSebastian Ott 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
1894e5bd780SSebastian Ott 		zdev->fmb = NULL;
1904e5bd780SSebastian Ott 	}
1914e5bd780SSebastian Ott 	return cc ? -EIO : 0;
192d0b08853SJan Glauber }
193d0b08853SJan Glauber 
194d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */
195d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev)
196d0b08853SJan Glauber {
1974e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1984e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1994e5bd780SSebastian Ott 	u8 cc, status;
200d0b08853SJan Glauber 
201d0b08853SJan Glauber 	if (!zdev->fmb)
202d0b08853SJan Glauber 		return -EINVAL;
203d0b08853SJan Glauber 
204c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
205c68468edSMatthew Rosato 
206d0b08853SJan Glauber 	/* Function measurement is disabled if fmb address is zero */
2074e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
2084e5bd780SSebastian Ott 	if (cc == 3) /* Function already gone. */
2094e5bd780SSebastian Ott 		cc = 0;
210d0b08853SJan Glauber 
2114e5bd780SSebastian Ott 	if (!cc) {
212d0b08853SJan Glauber 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
213d0b08853SJan Glauber 		zdev->fmb = NULL;
2144e5bd780SSebastian Ott 	}
2154e5bd780SSebastian Ott 	return cc ? -EIO : 0;
216d0b08853SJan Glauber }
217d0b08853SJan Glauber 
218cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
219cd248341SJan Glauber {
220cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
221cd248341SJan Glauber 	u64 data;
222cd248341SJan Glauber 	int rc;
223cd248341SJan Glauber 
22481deca12SSebastian Ott 	rc = __zpci_load(&data, req, offset);
225b170bad4SSebastian Ott 	if (!rc) {
2265064cd35SSebastian Ott 		data = le64_to_cpu((__force __le64) data);
2275064cd35SSebastian Ott 		data >>= (8 - len) * 8;
228cd248341SJan Glauber 		*val = (u32) data;
229b170bad4SSebastian Ott 	} else
230cd248341SJan Glauber 		*val = 0xffffffff;
231cd248341SJan Glauber 	return rc;
232cd248341SJan Glauber }
233cd248341SJan Glauber 
234cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
235cd248341SJan Glauber {
236cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
237cd248341SJan Glauber 	u64 data = val;
238cd248341SJan Glauber 	int rc;
239cd248341SJan Glauber 
2405064cd35SSebastian Ott 	data <<= (8 - len) * 8;
2415064cd35SSebastian Ott 	data = (__force u64) cpu_to_le64(data);
24281deca12SSebastian Ott 	rc = __zpci_store(data, req, offset);
243cd248341SJan Glauber 	return rc;
244cd248341SJan Glauber }
245cd248341SJan Glauber 
246cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res,
247cd248341SJan Glauber 				       resource_size_t size,
248cd248341SJan Glauber 				       resource_size_t align)
249cd248341SJan Glauber {
250cd248341SJan Glauber 	return 0;
251cd248341SJan Glauber }
252cd248341SJan Glauber 
25387bc359bSJan Glauber /* combine single writes by using store-block insn */
25487bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
25587bc359bSJan Glauber {
25687bc359bSJan Glauber        zpci_memcpy_toio(to, from, count);
25787bc359bSJan Glauber }
25887bc359bSJan Glauber 
259b43b3fffSBaoquan He void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
260b43b3fffSBaoquan He 			   unsigned long prot)
26171ba41c9SSebastian Ott {
262b43b3fffSBaoquan He 	/*
263b43b3fffSBaoquan He 	 * When PCI MIO instructions are unavailable the "physical" address
264b43b3fffSBaoquan He 	 * encodes a hint for accessing the PCI memory space it represents.
265b43b3fffSBaoquan He 	 * Just pass it unchanged such that ioread/iowrite can decode it.
266b43b3fffSBaoquan He 	 */
26771ba41c9SSebastian Ott 	if (!static_branch_unlikely(&have_mio))
268b43b3fffSBaoquan He 		return (void __iomem *)phys_addr;
26971ba41c9SSebastian Ott 
270b43b3fffSBaoquan He 	return generic_ioremap_prot(phys_addr, size, __pgprot(prot));
271b02002ccSNiklas Schnelle }
272b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_prot);
273b02002ccSNiklas Schnelle 
27471ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr)
27571ba41c9SSebastian Ott {
27671ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
277b43b3fffSBaoquan He 		generic_iounmap(addr);
27871ba41c9SSebastian Ott }
27971ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap);
28071ba41c9SSebastian Ott 
281cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */
28271ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
28371ba41c9SSebastian Ott 					unsigned long offset, unsigned long max)
284cd248341SJan Glauber {
285198a5278SSebastian Ott 	struct zpci_dev *zdev =	to_zpci(pdev);
286cd248341SJan Glauber 	int idx;
287cd248341SJan Glauber 
288cd248341SJan Glauber 	idx = zdev->bars[bar].map_idx;
289cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
290f5e44f82SSebastian Ott 	/* Detect overrun */
291f5e44f82SSebastian Ott 	WARN_ON(!++zpci_iomap_start[idx].count);
292cd248341SJan Glauber 	zpci_iomap_start[idx].fh = zdev->fh;
293cd248341SJan Glauber 	zpci_iomap_start[idx].bar = bar;
294cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
295cd248341SJan Glauber 
2969e00caaeSSebastian Ott 	return (void __iomem *) ZPCI_ADDR(idx) + offset;
297cd248341SJan Glauber }
29871ba41c9SSebastian Ott 
29971ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
30071ba41c9SSebastian Ott 					 unsigned long offset,
30171ba41c9SSebastian Ott 					 unsigned long max)
30271ba41c9SSebastian Ott {
30371ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
30471ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
30571ba41c9SSebastian Ott 	void __iomem *iova;
30671ba41c9SSebastian Ott 
30771ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
30871ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
30971ba41c9SSebastian Ott }
31071ba41c9SSebastian Ott 
31171ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
31271ba41c9SSebastian Ott 			      unsigned long offset, unsigned long max)
31371ba41c9SSebastian Ott {
314c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
31571ba41c9SSebastian Ott 		return NULL;
31671ba41c9SSebastian Ott 
31771ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
31871ba41c9SSebastian Ott 		return pci_iomap_range_mio(pdev, bar, offset, max);
31971ba41c9SSebastian Ott 	else
32071ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
32171ba41c9SSebastian Ott }
322d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range);
3238cfc99b5SMichael S. Tsirkin 
3248cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
3258cfc99b5SMichael S. Tsirkin {
3268cfc99b5SMichael S. Tsirkin 	return pci_iomap_range(dev, bar, 0, maxlen);
3278cfc99b5SMichael S. Tsirkin }
3288cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap);
329cd248341SJan Glauber 
33071ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
33171ba41c9SSebastian Ott 					    unsigned long offset, unsigned long max)
33271ba41c9SSebastian Ott {
33371ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
33471ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
33571ba41c9SSebastian Ott 	void __iomem *iova;
33671ba41c9SSebastian Ott 
33771ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
33871ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
33971ba41c9SSebastian Ott }
34071ba41c9SSebastian Ott 
34171ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
34271ba41c9SSebastian Ott 				 unsigned long offset, unsigned long max)
34371ba41c9SSebastian Ott {
344c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
34571ba41c9SSebastian Ott 		return NULL;
34671ba41c9SSebastian Ott 
34771ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
34871ba41c9SSebastian Ott 		return pci_iomap_wc_range_mio(pdev, bar, offset, max);
34971ba41c9SSebastian Ott 	else
35071ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
35171ba41c9SSebastian Ott }
35271ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range);
35371ba41c9SSebastian Ott 
35471ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
35571ba41c9SSebastian Ott {
35671ba41c9SSebastian Ott 	return pci_iomap_wc_range(dev, bar, 0, maxlen);
35771ba41c9SSebastian Ott }
35871ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc);
35971ba41c9SSebastian Ott 
36071ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
361cd248341SJan Glauber {
3629e00caaeSSebastian Ott 	unsigned int idx = ZPCI_IDX(addr);
363cd248341SJan Glauber 
364cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
3658cfc99b5SMichael S. Tsirkin 	/* Detect underrun */
366f5e44f82SSebastian Ott 	WARN_ON(!zpci_iomap_start[idx].count);
3678cfc99b5SMichael S. Tsirkin 	if (!--zpci_iomap_start[idx].count) {
368cd248341SJan Glauber 		zpci_iomap_start[idx].fh = 0;
369cd248341SJan Glauber 		zpci_iomap_start[idx].bar = 0;
3708cfc99b5SMichael S. Tsirkin 	}
371cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
372cd248341SJan Glauber }
37371ba41c9SSebastian Ott 
37471ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
37571ba41c9SSebastian Ott {
37671ba41c9SSebastian Ott 	iounmap(addr);
37771ba41c9SSebastian Ott }
37871ba41c9SSebastian Ott 
37971ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
38071ba41c9SSebastian Ott {
38171ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
38271ba41c9SSebastian Ott 		pci_iounmap_mio(pdev, addr);
38371ba41c9SSebastian Ott 	else
38471ba41c9SSebastian Ott 		pci_iounmap_fh(pdev, addr);
38571ba41c9SSebastian Ott }
386d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap);
387cd248341SJan Glauber 
388cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
389cd248341SJan Glauber 		    int size, u32 *val)
390cd248341SJan Glauber {
3917dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
392cd248341SJan Glauber 
39344510d6fSPierre Morel 	return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
394cd248341SJan Glauber }
395cd248341SJan Glauber 
396cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
397cd248341SJan Glauber 		     int size, u32 val)
398cd248341SJan Glauber {
3997dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
400cd248341SJan Glauber 
40144510d6fSPierre Morel 	return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
402cd248341SJan Glauber }
403cd248341SJan Glauber 
404cd248341SJan Glauber static struct pci_ops pci_root_ops = {
405cd248341SJan Glauber 	.read = pci_read,
406cd248341SJan Glauber 	.write = pci_write,
407cd248341SJan Glauber };
408cd248341SJan Glauber 
4091803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev)
410cd248341SJan Glauber {
41171ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
412cd248341SJan Glauber 	resource_size_t len;
413cd248341SJan Glauber 	int i;
414cd248341SJan Glauber 
415c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
416cd248341SJan Glauber 		len = pci_resource_len(pdev, i);
417cd248341SJan Glauber 		if (!len)
418cd248341SJan Glauber 			continue;
41971ba41c9SSebastian Ott 
420c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
42171ba41c9SSebastian Ott 			pdev->resource[i].start =
422df057c91SNiklas Schnelle 				(resource_size_t __force) zdev->bars[i].mio_wt;
42371ba41c9SSebastian Ott 		else
424c7ff0e91SSebastian Ott 			pdev->resource[i].start = (resource_size_t __force)
425c7ff0e91SSebastian Ott 				pci_iomap_range_fh(pdev, i, 0, 0);
426cd248341SJan Glauber 		pdev->resource[i].end = pdev->resource[i].start + len - 1;
427cd248341SJan Glauber 	}
428cfbb4a7aSSebastian Ott 
429abb95b75SNiklas Schnelle 	zpci_iov_map_resources(pdev);
430944239c5SSebastian Ott }
431944239c5SSebastian Ott 
4321803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev)
433944239c5SSebastian Ott {
434c7ff0e91SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
435944239c5SSebastian Ott 	resource_size_t len;
436944239c5SSebastian Ott 	int i;
437944239c5SSebastian Ott 
438c7ff0e91SSebastian Ott 	if (zpci_use_mio(zdev))
43971ba41c9SSebastian Ott 		return;
44071ba41c9SSebastian Ott 
441c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
442944239c5SSebastian Ott 		len = pci_resource_len(pdev, i);
443944239c5SSebastian Ott 		if (!len)
444944239c5SSebastian Ott 			continue;
445c7ff0e91SSebastian Ott 		pci_iounmap_fh(pdev, (void __iomem __force *)
4465b9f2081SMartin Schwidefsky 			       pdev->resource[i].start);
447944239c5SSebastian Ott 	}
448944239c5SSebastian Ott }
449cd248341SJan Glauber 
450cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev)
451cd248341SJan Glauber {
452bf19c94dSSebastian Ott 	unsigned long entry;
453cd248341SJan Glauber 
454cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
455c506fff3SSebastian Ott 	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
456c506fff3SSebastian Ott 	if (entry == ZPCI_IOMAP_ENTRIES) {
457cd248341SJan Glauber 		spin_unlock(&zpci_iomap_lock);
458cd248341SJan Glauber 		return -ENOSPC;
459cd248341SJan Glauber 	}
460c506fff3SSebastian Ott 	set_bit(entry, zpci_iomap_bitmap);
461cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
462cd248341SJan Glauber 	return entry;
463cd248341SJan Glauber }
464cd248341SJan Glauber 
465cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
466cd248341SJan Glauber {
467cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
468cd248341SJan Glauber 	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
469c506fff3SSebastian Ott 	clear_bit(entry, zpci_iomap_bitmap);
470cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
471cd248341SJan Glauber }
472cd248341SJan Glauber 
4734fe20497SNiklas Schnelle static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
4744fe20497SNiklas Schnelle {
4754fe20497SNiklas Schnelle 	int bar, idx;
4764fe20497SNiklas Schnelle 
4774fe20497SNiklas Schnelle 	spin_lock(&zpci_iomap_lock);
4784fe20497SNiklas Schnelle 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
4794fe20497SNiklas Schnelle 		if (!zdev->bars[bar].size)
4804fe20497SNiklas Schnelle 			continue;
4814fe20497SNiklas Schnelle 		idx = zdev->bars[bar].map_idx;
4824fe20497SNiklas Schnelle 		if (!zpci_iomap_start[idx].count)
4834fe20497SNiklas Schnelle 			continue;
4844fe20497SNiklas Schnelle 		WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
4854fe20497SNiklas Schnelle 	}
4864fe20497SNiklas Schnelle 	spin_unlock(&zpci_iomap_lock);
4874fe20497SNiklas Schnelle }
4884fe20497SNiklas Schnelle 
4894fe20497SNiklas Schnelle void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
4904fe20497SNiklas Schnelle {
4914fe20497SNiklas Schnelle 	if (!fh || zdev->fh == fh)
4924fe20497SNiklas Schnelle 		return;
4934fe20497SNiklas Schnelle 
4944fe20497SNiklas Schnelle 	zdev->fh = fh;
4954fe20497SNiklas Schnelle 	if (zpci_use_mio(zdev))
4964fe20497SNiklas Schnelle 		return;
4974fe20497SNiklas Schnelle 	if (zdev->has_resources && zdev_enabled(zdev))
4984fe20497SNiklas Schnelle 		zpci_do_update_iomap_fh(zdev, fh);
4994fe20497SNiklas Schnelle }
5004fe20497SNiklas Schnelle 
5017a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
5027a572a3aSSebastian Ott 				    unsigned long size, unsigned long flags)
5037a572a3aSSebastian Ott {
5047a572a3aSSebastian Ott 	struct resource *r;
5057a572a3aSSebastian Ott 
5067a572a3aSSebastian Ott 	r = kzalloc(sizeof(*r), GFP_KERNEL);
5077a572a3aSSebastian Ott 	if (!r)
5087a572a3aSSebastian Ott 		return NULL;
5097a572a3aSSebastian Ott 
5107a572a3aSSebastian Ott 	r->start = start;
5117a572a3aSSebastian Ott 	r->end = r->start + size - 1;
5127a572a3aSSebastian Ott 	r->flags = flags;
5137a572a3aSSebastian Ott 	r->name = zdev->res_name;
5147a572a3aSSebastian Ott 
5157a572a3aSSebastian Ott 	if (request_resource(&iomem_resource, r)) {
5167a572a3aSSebastian Ott 		kfree(r);
5177a572a3aSSebastian Ott 		return NULL;
5187a572a3aSSebastian Ott 	}
5197a572a3aSSebastian Ott 	return r;
5207a572a3aSSebastian Ott }
5217a572a3aSSebastian Ott 
522ab909509SNiklas Schnelle int zpci_setup_bus_resources(struct zpci_dev *zdev)
5237a572a3aSSebastian Ott {
5247a572a3aSSebastian Ott 	unsigned long addr, size, flags;
5257a572a3aSSebastian Ott 	struct resource *res;
5267a572a3aSSebastian Ott 	int i, entry;
5277a572a3aSSebastian Ott 
5287a572a3aSSebastian Ott 	snprintf(zdev->res_name, sizeof(zdev->res_name),
52905bc1be6SPierre Morel 		 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
5307a572a3aSSebastian Ott 
531c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
5327a572a3aSSebastian Ott 		if (!zdev->bars[i].size)
5337a572a3aSSebastian Ott 			continue;
5347a572a3aSSebastian Ott 		entry = zpci_alloc_iomap(zdev);
5357a572a3aSSebastian Ott 		if (entry < 0)
5367a572a3aSSebastian Ott 			return entry;
5377a572a3aSSebastian Ott 		zdev->bars[i].map_idx = entry;
5387a572a3aSSebastian Ott 
5397a572a3aSSebastian Ott 		/* only MMIO is supported */
5407a572a3aSSebastian Ott 		flags = IORESOURCE_MEM;
5417a572a3aSSebastian Ott 		if (zdev->bars[i].val & 8)
5427a572a3aSSebastian Ott 			flags |= IORESOURCE_PREFETCH;
5437a572a3aSSebastian Ott 		if (zdev->bars[i].val & 4)
5447a572a3aSSebastian Ott 			flags |= IORESOURCE_MEM_64;
5457a572a3aSSebastian Ott 
546c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
547df057c91SNiklas Schnelle 			addr = (unsigned long) zdev->bars[i].mio_wt;
548dcd33b23SSebastian Ott 		else
5499e00caaeSSebastian Ott 			addr = ZPCI_ADDR(entry);
5507a572a3aSSebastian Ott 		size = 1UL << zdev->bars[i].size;
5517a572a3aSSebastian Ott 
5527a572a3aSSebastian Ott 		res = __alloc_res(zdev, addr, size, flags);
5537a572a3aSSebastian Ott 		if (!res) {
5547a572a3aSSebastian Ott 			zpci_free_iomap(zdev, entry);
5557a572a3aSSebastian Ott 			return -ENOMEM;
5567a572a3aSSebastian Ott 		}
5577a572a3aSSebastian Ott 		zdev->bars[i].res = res;
5587a572a3aSSebastian Ott 	}
559a50297cfSNiklas Schnelle 	zdev->has_resources = 1;
5607a572a3aSSebastian Ott 
5617a572a3aSSebastian Ott 	return 0;
5627a572a3aSSebastian Ott }
5637a572a3aSSebastian Ott 
5647a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
5657a572a3aSSebastian Ott {
566ab909509SNiklas Schnelle 	struct resource *res;
5677a572a3aSSebastian Ott 	int i;
5687a572a3aSSebastian Ott 
569ab909509SNiklas Schnelle 	pci_lock_rescan_remove();
570c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
571ab909509SNiklas Schnelle 		res = zdev->bars[i].res;
572ab909509SNiklas Schnelle 		if (!res)
5737a572a3aSSebastian Ott 			continue;
5747a572a3aSSebastian Ott 
575ab909509SNiklas Schnelle 		release_resource(res);
576ab909509SNiklas Schnelle 		pci_bus_remove_resource(zdev->zbus->bus, res);
5777a572a3aSSebastian Ott 		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
578ab909509SNiklas Schnelle 		zdev->bars[i].res = NULL;
579ab909509SNiklas Schnelle 		kfree(res);
5807a572a3aSSebastian Ott 	}
581a50297cfSNiklas Schnelle 	zdev->has_resources = 0;
582ab909509SNiklas Schnelle 	pci_unlock_rescan_remove();
5837a572a3aSSebastian Ott }
5847a572a3aSSebastian Ott 
58506dc660eSOliver O'Halloran int pcibios_device_add(struct pci_dev *pdev)
586af0a8a84SSebastian Ott {
5872a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
588cb809182SSebastian Ott 	struct resource *res;
589cb809182SSebastian Ott 	int i;
590cb809182SSebastian Ott 
5912a671f77SNiklas Schnelle 	/* The pdev has a reference to the zdev via its bus */
5922a671f77SNiklas Schnelle 	zpci_zdev_get(zdev);
5937dc20ab1SSebastian Ott 	if (pdev->is_physfn)
5947dc20ab1SSebastian Ott 		pdev->no_vf_scan = 1;
5957dc20ab1SSebastian Ott 
596ef4858c6SSebastian Ott 	pdev->dev.groups = zpci_attr_groups;
5971803ba2dSSebastian Ott 	zpci_map_resources(pdev);
598cb809182SSebastian Ott 
599c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
600cb809182SSebastian Ott 		res = &pdev->resource[i];
601cb809182SSebastian Ott 		if (res->parent || !res->flags)
602cb809182SSebastian Ott 			continue;
603cb809182SSebastian Ott 		pci_claim_resource(pdev, i);
604cb809182SSebastian Ott 	}
605cb809182SSebastian Ott 
606cb809182SSebastian Ott 	return 0;
607cb809182SSebastian Ott }
608cb809182SSebastian Ott 
6091803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev)
6101803ba2dSSebastian Ott {
6112a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
6122a671f77SNiklas Schnelle 
6131803ba2dSSebastian Ott 	zpci_unmap_resources(pdev);
6142a671f77SNiklas Schnelle 	zpci_zdev_put(zdev);
6151803ba2dSSebastian Ott }
6161803ba2dSSebastian Ott 
617cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask)
618cb809182SSebastian Ott {
619198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
620af0a8a84SSebastian Ott 
6219a99649fSSebastian Ott 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
622af0a8a84SSebastian Ott 	zpci_fmb_enable_device(zdev);
623af0a8a84SSebastian Ott 
624d7533232SBjorn Helgaas 	return pci_enable_resources(pdev, mask);
625af0a8a84SSebastian Ott }
626af0a8a84SSebastian Ott 
627cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev)
628944239c5SSebastian Ott {
629198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
630944239c5SSebastian Ott 
631944239c5SSebastian Ott 	zpci_fmb_disable_device(zdev);
632944239c5SSebastian Ott 	zpci_debug_exit_device(zdev);
633944239c5SSebastian Ott }
634944239c5SSebastian Ott 
63505bc1be6SPierre Morel static int __zpci_register_domain(int domain)
636cd248341SJan Glauber {
637969ae01bSNiklas Schnelle 	spin_lock(&zpci_domain_lock);
63805bc1be6SPierre Morel 	if (test_bit(domain, zpci_domain)) {
639969ae01bSNiklas Schnelle 		spin_unlock(&zpci_domain_lock);
64005bc1be6SPierre Morel 		pr_err("Domain %04x is already assigned\n", domain);
641312e8462SSebastian Ott 		return -EEXIST;
642312e8462SSebastian Ott 	}
64305bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
644312e8462SSebastian Ott 	spin_unlock(&zpci_domain_lock);
64505bc1be6SPierre Morel 	return domain;
6465c5afd02SSebastian Ott }
64705bc1be6SPierre Morel 
64805bc1be6SPierre Morel static int __zpci_alloc_domain(void)
64905bc1be6SPierre Morel {
65005bc1be6SPierre Morel 	int domain;
65105bc1be6SPierre Morel 
65205bc1be6SPierre Morel 	spin_lock(&zpci_domain_lock);
653969ae01bSNiklas Schnelle 	/*
654969ae01bSNiklas Schnelle 	 * We can always auto allocate domains below ZPCI_NR_DEVICES.
655969ae01bSNiklas Schnelle 	 * There is either a free domain or we have reached the maximum in
656969ae01bSNiklas Schnelle 	 * which case we would have bailed earlier.
657969ae01bSNiklas Schnelle 	 */
65805bc1be6SPierre Morel 	domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
65905bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
660cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
66105bc1be6SPierre Morel 	return domain;
662cd248341SJan Glauber }
663cd248341SJan Glauber 
66405bc1be6SPierre Morel int zpci_alloc_domain(int domain)
66505bc1be6SPierre Morel {
66605bc1be6SPierre Morel 	if (zpci_unique_uid) {
66705bc1be6SPierre Morel 		if (domain)
66805bc1be6SPierre Morel 			return __zpci_register_domain(domain);
66905bc1be6SPierre Morel 		pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
67005bc1be6SPierre Morel 		update_uid_checking(false);
67105bc1be6SPierre Morel 	}
67205bc1be6SPierre Morel 	return __zpci_alloc_domain();
67305bc1be6SPierre Morel }
67405bc1be6SPierre Morel 
67505bc1be6SPierre Morel void zpci_free_domain(int domain)
676cd248341SJan Glauber {
677cd248341SJan Glauber 	spin_lock(&zpci_domain_lock);
67805bc1be6SPierre Morel 	clear_bit(domain, zpci_domain);
679cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
680cd248341SJan Glauber }
681cd248341SJan Glauber 
6827d594322SSebastian Ott 
683a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev)
684a755a45dSJan Glauber {
685cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
6861f3f7681SNiklas Schnelle 	int rc = 0;
687a755a45dSJan Glauber 
6881f3f7681SNiklas Schnelle 	if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
689f7addcddSNiklas Schnelle 		rc = -EIO;
6901f3f7681SNiklas Schnelle 	else
6914fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
692a755a45dSJan Glauber 	return rc;
693a755a45dSJan Glauber }
69409340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_enable_device);
695a755a45dSJan Glauber 
696cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev)
697cb65a669SSebastian Ott {
698cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
6998256addaSNiklas Schnelle 	int cc, rc = 0;
7008256addaSNiklas Schnelle 
701cc049eecSNiklas Schnelle 	cc = clp_disable_fh(zdev, &fh);
702cc049eecSNiklas Schnelle 	if (!cc) {
7034fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
704cc049eecSNiklas Schnelle 	} else if (cc == CLP_RC_SETPCIFN_ALRDY) {
7058256addaSNiklas Schnelle 		pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
7068256addaSNiklas Schnelle 			zdev->fid);
7078256addaSNiklas Schnelle 		/* Function is already disabled - update handle */
708cc049eecSNiklas Schnelle 		rc = clp_refresh_fh(zdev->fid, &fh);
709cc049eecSNiklas Schnelle 		if (!rc) {
7104fe20497SNiklas Schnelle 			zpci_update_fh(zdev, fh);
7118256addaSNiklas Schnelle 			rc = -EINVAL;
712cc049eecSNiklas Schnelle 		}
713cc049eecSNiklas Schnelle 	} else {
7148256addaSNiklas Schnelle 		rc = -EIO;
7158256addaSNiklas Schnelle 	}
7168256addaSNiklas Schnelle 	return rc;
717cb65a669SSebastian Ott }
71809340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_disable_device);
719cb65a669SSebastian Ott 
720ba764dd7SNiklas Schnelle /**
721da995d53SNiklas Schnelle  * zpci_hot_reset_device - perform a reset of the given zPCI function
722da995d53SNiklas Schnelle  * @zdev: the slot which should be reset
723da995d53SNiklas Schnelle  *
724da995d53SNiklas Schnelle  * Performs a low level reset of the zPCI function. The reset is low level in
725da995d53SNiklas Schnelle  * the sense that the zPCI function can be reset without detaching it from the
726da995d53SNiklas Schnelle  * common PCI subsystem. The reset may be performed while under control of
727da995d53SNiklas Schnelle  * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
728da995d53SNiklas Schnelle  * table is reinstated at the end of the reset.
729da995d53SNiklas Schnelle  *
730da995d53SNiklas Schnelle  * After the reset the functions internal state is reset to an initial state
731da995d53SNiklas Schnelle  * equivalent to its state during boot when first probing a driver.
732da995d53SNiklas Schnelle  * Consequently after reset the PCI function requires re-initialization via the
733da995d53SNiklas Schnelle  * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
734bcb5d6c7SGerd Bayer  * and enabling the function via e.g. pci_enable_device_flags(). The caller
735da995d53SNiklas Schnelle  * must guard against concurrent reset attempts.
736da995d53SNiklas Schnelle  *
737da995d53SNiklas Schnelle  * In most cases this function should not be called directly but through
738da995d53SNiklas Schnelle  * pci_reset_function() or pci_reset_bus() which handle the save/restore and
739bcb5d6c7SGerd Bayer  * locking - asserted by lockdep.
740da995d53SNiklas Schnelle  *
741da995d53SNiklas Schnelle  * Return: 0 on success and an error value otherwise
742da995d53SNiklas Schnelle  */
743da995d53SNiklas Schnelle int zpci_hot_reset_device(struct zpci_dev *zdev)
744da995d53SNiklas Schnelle {
74559bbf596SNiklas Schnelle 	u8 status;
746da995d53SNiklas Schnelle 	int rc;
747da995d53SNiklas Schnelle 
748bcb5d6c7SGerd Bayer 	lockdep_assert_held(&zdev->state_lock);
749da995d53SNiklas Schnelle 	zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
750da995d53SNiklas Schnelle 	if (zdev_enabled(zdev)) {
751da995d53SNiklas Schnelle 		/* Disables device access, DMAs and IRQs (reset state) */
752da995d53SNiklas Schnelle 		rc = zpci_disable_device(zdev);
753da995d53SNiklas Schnelle 		/*
754da995d53SNiklas Schnelle 		 * Due to a z/VM vs LPAR inconsistency in the error state the
755da995d53SNiklas Schnelle 		 * FH may indicate an enabled device but disable says the
756da995d53SNiklas Schnelle 		 * device is already disabled don't treat it as an error here.
757da995d53SNiklas Schnelle 		 */
758da995d53SNiklas Schnelle 		if (rc == -EINVAL)
759da995d53SNiklas Schnelle 			rc = 0;
760da995d53SNiklas Schnelle 		if (rc)
761da995d53SNiklas Schnelle 			return rc;
762da995d53SNiklas Schnelle 	}
763da995d53SNiklas Schnelle 
764da995d53SNiklas Schnelle 	rc = zpci_enable_device(zdev);
765da995d53SNiklas Schnelle 	if (rc)
766da995d53SNiklas Schnelle 		return rc;
767da995d53SNiklas Schnelle 
768da995d53SNiklas Schnelle 	if (zdev->dma_table)
769da995d53SNiklas Schnelle 		rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
77059bbf596SNiklas Schnelle 					virt_to_phys(zdev->dma_table), &status);
771da995d53SNiklas Schnelle 	if (rc) {
772da995d53SNiklas Schnelle 		zpci_disable_device(zdev);
773da995d53SNiklas Schnelle 		return rc;
774da995d53SNiklas Schnelle 	}
775da995d53SNiklas Schnelle 
776da995d53SNiklas Schnelle 	return 0;
777da995d53SNiklas Schnelle }
778da995d53SNiklas Schnelle 
779da995d53SNiklas Schnelle /**
780ba764dd7SNiklas Schnelle  * zpci_create_device() - Create a new zpci_dev and add it to the zbus
781ba764dd7SNiklas Schnelle  * @fid: Function ID of the device to be created
782ba764dd7SNiklas Schnelle  * @fh: Current Function Handle of the device to be created
783ba764dd7SNiklas Schnelle  * @state: Initial state after creation either Standby or Configured
784ba764dd7SNiklas Schnelle  *
785ba764dd7SNiklas Schnelle  * Creates a new zpci device and adds it to its, possibly newly created, zbus
786ba764dd7SNiklas Schnelle  * as well as zpci_list.
787ba764dd7SNiklas Schnelle  *
78814c87ba8SNiklas Schnelle  * Returns: the zdev on success or an error pointer otherwise
789ba764dd7SNiklas Schnelle  */
79014c87ba8SNiklas Schnelle struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
791cd248341SJan Glauber {
792ba764dd7SNiklas Schnelle 	struct zpci_dev *zdev;
793cd248341SJan Glauber 	int rc;
794cd248341SJan Glauber 
79552c79e63SNiklas Schnelle 	zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
796ba764dd7SNiklas Schnelle 	zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
797ba764dd7SNiklas Schnelle 	if (!zdev)
79814c87ba8SNiklas Schnelle 		return ERR_PTR(-ENOMEM);
799ba764dd7SNiklas Schnelle 
800ba764dd7SNiklas Schnelle 	/* FID and Function Handle are the static/dynamic identifiers */
801ba764dd7SNiklas Schnelle 	zdev->fid = fid;
802ba764dd7SNiklas Schnelle 	zdev->fh = fh;
803ba764dd7SNiklas Schnelle 
804ba764dd7SNiklas Schnelle 	/* Query function properties and update zdev */
805ba764dd7SNiklas Schnelle 	rc = clp_query_pci_fn(zdev);
806ba764dd7SNiklas Schnelle 	if (rc)
807ba764dd7SNiklas Schnelle 		goto error;
808ba764dd7SNiklas Schnelle 	zdev->state =  state;
809ba764dd7SNiklas Schnelle 
81005bc1be6SPierre Morel 	kref_init(&zdev->kref);
811bcb5d6c7SGerd Bayer 	mutex_init(&zdev->state_lock);
8120d48566dSGerd Bayer 	mutex_init(&zdev->fmb_lock);
81309340b2fSMatthew Rosato 	mutex_init(&zdev->kzdev_lock);
814ba764dd7SNiklas Schnelle 
815ba764dd7SNiklas Schnelle 	rc = zpci_init_iommu(zdev);
816ba764dd7SNiklas Schnelle 	if (rc)
817ba764dd7SNiklas Schnelle 		goto error;
818ba764dd7SNiklas Schnelle 
819ba764dd7SNiklas Schnelle 	rc = zpci_bus_device_register(zdev, &pci_root_ops);
820ba764dd7SNiklas Schnelle 	if (rc)
821a50297cfSNiklas Schnelle 		goto error_destroy_iommu;
82205bc1be6SPierre Morel 
82305bc1be6SPierre Morel 	spin_lock(&zpci_list_lock);
82405bc1be6SPierre Morel 	list_add_tail(&zdev->entry, &zpci_list);
82505bc1be6SPierre Morel 	spin_unlock(&zpci_list_lock);
826cd248341SJan Glauber 
82714c87ba8SNiklas Schnelle 	return zdev;
828cd248341SJan Glauber 
829ba764dd7SNiklas Schnelle error_destroy_iommu:
830f42c2235SJoerg Roedel 	zpci_destroy_iommu(zdev);
831ba764dd7SNiklas Schnelle error:
832ba764dd7SNiklas Schnelle 	zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
833ba764dd7SNiklas Schnelle 	kfree(zdev);
83414c87ba8SNiklas Schnelle 	return ERR_PTR(rc);
835cd248341SJan Glauber }
836cd248341SJan Glauber 
837a46044a9SNiklas Schnelle bool zpci_is_device_configured(struct zpci_dev *zdev)
838a46044a9SNiklas Schnelle {
839a46044a9SNiklas Schnelle 	enum zpci_state state = zdev->state;
840a46044a9SNiklas Schnelle 
841a46044a9SNiklas Schnelle 	return state != ZPCI_FN_STATE_RESERVED &&
842a46044a9SNiklas Schnelle 		state != ZPCI_FN_STATE_STANDBY;
843a46044a9SNiklas Schnelle }
844a46044a9SNiklas Schnelle 
8452631f6b6SNiklas Schnelle /**
846a7f82c36SNiklas Schnelle  * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
8472631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to be configured
8482631f6b6SNiklas Schnelle  * @fh: The general function handle supplied by the platform
8492631f6b6SNiklas Schnelle  *
85061311e32SNiklas Schnelle  * Given a device in the configuration state Configured, enables, scans and
85145e5f0c0SNiklas Schnelle  * adds it to the common code PCI subsystem if possible. If any failure occurs,
85245e5f0c0SNiklas Schnelle  * the zpci_dev is left disabled.
8532631f6b6SNiklas Schnelle  *
8542631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8552631f6b6SNiklas Schnelle  */
856a7f82c36SNiklas Schnelle int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
8572631f6b6SNiklas Schnelle {
8584fe20497SNiklas Schnelle 	zpci_update_fh(zdev, fh);
85945e5f0c0SNiklas Schnelle 	return zpci_bus_scan_device(zdev);
8602631f6b6SNiklas Schnelle }
8612631f6b6SNiklas Schnelle 
8622631f6b6SNiklas Schnelle /**
8632631f6b6SNiklas Schnelle  * zpci_deconfigure_device() - Deconfigure a zpci_dev
8642631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to configure
8652631f6b6SNiklas Schnelle  *
8662631f6b6SNiklas Schnelle  * Deconfigure a zPCI function that is currently configured and possibly known
8672631f6b6SNiklas Schnelle  * to the common code PCI subsystem.
8682631f6b6SNiklas Schnelle  * If any failure occurs the device is left as is.
8692631f6b6SNiklas Schnelle  *
8702631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8712631f6b6SNiklas Schnelle  */
8722631f6b6SNiklas Schnelle int zpci_deconfigure_device(struct zpci_dev *zdev)
8732631f6b6SNiklas Schnelle {
8742631f6b6SNiklas Schnelle 	int rc;
8752631f6b6SNiklas Schnelle 
876bcb5d6c7SGerd Bayer 	lockdep_assert_held(&zdev->state_lock);
877bcb5d6c7SGerd Bayer 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
878bcb5d6c7SGerd Bayer 		return 0;
879bcb5d6c7SGerd Bayer 
8802631f6b6SNiklas Schnelle 	if (zdev->zbus->bus)
88195b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
8822631f6b6SNiklas Schnelle 
8832631f6b6SNiklas Schnelle 	if (zdev_enabled(zdev)) {
8842631f6b6SNiklas Schnelle 		rc = zpci_disable_device(zdev);
8852631f6b6SNiklas Schnelle 		if (rc)
8862631f6b6SNiklas Schnelle 			return rc;
8872631f6b6SNiklas Schnelle 	}
8882631f6b6SNiklas Schnelle 
8892631f6b6SNiklas Schnelle 	rc = sclp_pci_deconfigure(zdev->fid);
8902631f6b6SNiklas Schnelle 	zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
8912631f6b6SNiklas Schnelle 	if (rc)
8922631f6b6SNiklas Schnelle 		return rc;
8932631f6b6SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_STANDBY;
8942631f6b6SNiklas Schnelle 
8952631f6b6SNiklas Schnelle 	return 0;
8962631f6b6SNiklas Schnelle }
8972631f6b6SNiklas Schnelle 
898a46044a9SNiklas Schnelle /**
899a46044a9SNiklas Schnelle  * zpci_device_reserved() - Mark device as resverved
900a46044a9SNiklas Schnelle  * @zdev: the zpci_dev that was reserved
901a46044a9SNiklas Schnelle  *
902a46044a9SNiklas Schnelle  * Handle the case that a given zPCI function was reserved by another system.
903a46044a9SNiklas Schnelle  * After a call to this function the zpci_dev can not be found via
904a46044a9SNiklas Schnelle  * get_zdev_by_fid() anymore but may still be accessible via existing
905a46044a9SNiklas Schnelle  * references though it will not be functional anymore.
906a46044a9SNiklas Schnelle  */
907a46044a9SNiklas Schnelle void zpci_device_reserved(struct zpci_dev *zdev)
908a46044a9SNiklas Schnelle {
909a46044a9SNiklas Schnelle 	/*
910a46044a9SNiklas Schnelle 	 * Remove device from zpci_list as it is going away. This also
911a46044a9SNiklas Schnelle 	 * makes sure we ignore subsequent zPCI events for this device.
912a46044a9SNiklas Schnelle 	 */
913a46044a9SNiklas Schnelle 	spin_lock(&zpci_list_lock);
914a46044a9SNiklas Schnelle 	list_del(&zdev->entry);
915a46044a9SNiklas Schnelle 	spin_unlock(&zpci_list_lock);
916a46044a9SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_RESERVED;
917a46044a9SNiklas Schnelle 	zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
918a46044a9SNiklas Schnelle 	zpci_zdev_put(zdev);
919a46044a9SNiklas Schnelle }
920a46044a9SNiklas Schnelle 
92105bc1be6SPierre Morel void zpci_release_device(struct kref *kref)
922623bd44dSSebastian Ott {
92305bc1be6SPierre Morel 	struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
924a9045c22SNiklas Schnelle 	int ret;
925623bd44dSSebastian Ott 
926*6ee600bfSGerd Bayer 	if (zdev->has_hp_slot)
927*6ee600bfSGerd Bayer 		zpci_exit_slot(zdev);
928*6ee600bfSGerd Bayer 
9292f0230b2SNiklas Schnelle 	if (zdev->zbus->bus)
93095b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
93144510d6fSPierre Morel 
932f6576a1bSNiklas Schnelle 	if (zdev_enabled(zdev))
93305bc1be6SPierre Morel 		zpci_disable_device(zdev);
934f6576a1bSNiklas Schnelle 
935f6576a1bSNiklas Schnelle 	switch (zdev->state) {
936a9045c22SNiklas Schnelle 	case ZPCI_FN_STATE_CONFIGURED:
937a9045c22SNiklas Schnelle 		ret = sclp_pci_deconfigure(zdev->fid);
938a9045c22SNiklas Schnelle 		zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
939a9045c22SNiklas Schnelle 		fallthrough;
94005bc1be6SPierre Morel 	case ZPCI_FN_STATE_STANDBY:
94144510d6fSPierre Morel 		if (zdev->has_hp_slot)
94205bc1be6SPierre Morel 			zpci_exit_slot(zdev);
943a46044a9SNiklas Schnelle 		spin_lock(&zpci_list_lock);
944a46044a9SNiklas Schnelle 		list_del(&zdev->entry);
945a46044a9SNiklas Schnelle 		spin_unlock(&zpci_list_lock);
946a46044a9SNiklas Schnelle 		zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
947a46044a9SNiklas Schnelle 		fallthrough;
948a46044a9SNiklas Schnelle 	case ZPCI_FN_STATE_RESERVED:
94902368b7cSNiklas Schnelle 		if (zdev->has_resources)
95005bc1be6SPierre Morel 			zpci_cleanup_bus_resources(zdev);
95105bc1be6SPierre Morel 		zpci_bus_device_unregister(zdev);
95205bc1be6SPierre Morel 		zpci_destroy_iommu(zdev);
95305bc1be6SPierre Morel 		fallthrough;
95405bc1be6SPierre Morel 	default:
95505bc1be6SPierre Morel 		break;
95605bc1be6SPierre Morel 	}
95705bc1be6SPierre Morel 	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
9582ba8336dSNiklas Schnelle 	kfree_rcu(zdev, rcu);
959623bd44dSSebastian Ott }
960623bd44dSSebastian Ott 
961bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev,
962bd3a1725SMartin Schwidefsky 		      struct zpci_report_error_header *report)
963bd3a1725SMartin Schwidefsky {
964bd3a1725SMartin Schwidefsky 	struct zpci_dev *zdev = to_zpci(pdev);
965bd3a1725SMartin Schwidefsky 
966bd3a1725SMartin Schwidefsky 	return sclp_pci_report(report, zdev->fh, zdev->fid);
967bd3a1725SMartin Schwidefsky }
968bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error);
969bd3a1725SMartin Schwidefsky 
9704cdf2f4eSNiklas Schnelle /**
9714cdf2f4eSNiklas Schnelle  * zpci_clear_error_state() - Clears the zPCI error state of the device
9724cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which the zPCI error state should be reset
9734cdf2f4eSNiklas Schnelle  *
9744cdf2f4eSNiklas Schnelle  * Clear the zPCI error state of the device. If clearing the zPCI error state
9754cdf2f4eSNiklas Schnelle  * fails the device is left in the error state. In this case it may make sense
9764cdf2f4eSNiklas Schnelle  * to call zpci_io_perm_failure() on the associated pdev if it exists.
9774cdf2f4eSNiklas Schnelle  *
9784cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
9794cdf2f4eSNiklas Schnelle  */
9804cdf2f4eSNiklas Schnelle int zpci_clear_error_state(struct zpci_dev *zdev)
9814cdf2f4eSNiklas Schnelle {
9824cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
9834cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
9844cdf2f4eSNiklas Schnelle 	u8 status;
9854cdf2f4eSNiklas Schnelle 	int cc;
9864cdf2f4eSNiklas Schnelle 
9874cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
9884cdf2f4eSNiklas Schnelle 	if (cc) {
9894cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
9904cdf2f4eSNiklas Schnelle 		return -EIO;
9914cdf2f4eSNiklas Schnelle 	}
9924cdf2f4eSNiklas Schnelle 
9934cdf2f4eSNiklas Schnelle 	return 0;
9944cdf2f4eSNiklas Schnelle }
9954cdf2f4eSNiklas Schnelle 
9964cdf2f4eSNiklas Schnelle /**
9974cdf2f4eSNiklas Schnelle  * zpci_reset_load_store_blocked() - Re-enables L/S from error state
9984cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which to unblock load/store access
9994cdf2f4eSNiklas Schnelle  *
10004cdf2f4eSNiklas Schnelle  * Re-enables load/store access for a PCI function in the error state while
10014cdf2f4eSNiklas Schnelle  * keeping DMA blocked. In this state drivers can poke MMIO space to determine
10024cdf2f4eSNiklas Schnelle  * if error recovery is possible while catching any rogue DMA access from the
10034cdf2f4eSNiklas Schnelle  * device.
10044cdf2f4eSNiklas Schnelle  *
10054cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
10064cdf2f4eSNiklas Schnelle  */
10074cdf2f4eSNiklas Schnelle int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
10084cdf2f4eSNiklas Schnelle {
10094cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
10104cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
10114cdf2f4eSNiklas Schnelle 	u8 status;
10124cdf2f4eSNiklas Schnelle 	int cc;
10134cdf2f4eSNiklas Schnelle 
10144cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
10154cdf2f4eSNiklas Schnelle 	if (cc) {
10164cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
10174cdf2f4eSNiklas Schnelle 		return -EIO;
10184cdf2f4eSNiklas Schnelle 	}
10194cdf2f4eSNiklas Schnelle 
10204cdf2f4eSNiklas Schnelle 	return 0;
10214cdf2f4eSNiklas Schnelle }
10224cdf2f4eSNiklas Schnelle 
1023cd248341SJan Glauber static int zpci_mem_init(void)
1024cd248341SJan Glauber {
102580c544deSSebastian Ott 	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
102680c544deSSebastian Ott 		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
102780c544deSSebastian Ott 
1028d0b08853SJan Glauber 	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
102980c544deSSebastian Ott 					   __alignof__(struct zpci_fmb), 0, NULL);
1030d0b08853SJan Glauber 	if (!zdev_fmb_cache)
1031c506fff3SSebastian Ott 		goto error_fmb;
1032d0b08853SJan Glauber 
1033c506fff3SSebastian Ott 	zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
1034c506fff3SSebastian Ott 				   sizeof(*zpci_iomap_start), GFP_KERNEL);
1035cd248341SJan Glauber 	if (!zpci_iomap_start)
10369a4da8a5SJan Glauber 		goto error_iomap;
1037cd248341SJan Glauber 
1038c506fff3SSebastian Ott 	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1039c506fff3SSebastian Ott 				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1040c506fff3SSebastian Ott 	if (!zpci_iomap_bitmap)
1041c506fff3SSebastian Ott 		goto error_iomap_bitmap;
1042c506fff3SSebastian Ott 
1043b02002ccSNiklas Schnelle 	if (static_branch_likely(&have_mio))
1044b02002ccSNiklas Schnelle 		clp_setup_writeback_mio();
1045b02002ccSNiklas Schnelle 
1046c506fff3SSebastian Ott 	return 0;
1047c506fff3SSebastian Ott error_iomap_bitmap:
1048c506fff3SSebastian Ott 	kfree(zpci_iomap_start);
10499a4da8a5SJan Glauber error_iomap:
1050d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1051c506fff3SSebastian Ott error_fmb:
1052cd248341SJan Glauber 	return -ENOMEM;
1053cd248341SJan Glauber }
1054cd248341SJan Glauber 
1055cd248341SJan Glauber static void zpci_mem_exit(void)
1056cd248341SJan Glauber {
1057c506fff3SSebastian Ott 	kfree(zpci_iomap_bitmap);
1058cd248341SJan Glauber 	kfree(zpci_iomap_start);
1059d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1060cd248341SJan Glauber }
1061cd248341SJan Glauber 
10626324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1;
1063fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata;
1064aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized;
1065cd248341SJan Glauber 
1066cd248341SJan Glauber char * __init pcibios_setup(char *str)
1067cd248341SJan Glauber {
1068257608fbSSebastian Ott 	if (!strcmp(str, "off")) {
1069257608fbSSebastian Ott 		s390_pci_probe = 0;
1070cd248341SJan Glauber 		return NULL;
1071cd248341SJan Glauber 	}
107256271303SSebastian Ott 	if (!strcmp(str, "nomio")) {
10733322ba0dSNiklas Schnelle 		S390_lowcore.machine_flags &= ~MACHINE_FLAG_PCI_MIO;
107456271303SSebastian Ott 		return NULL;
107556271303SSebastian Ott 	}
1076fbfe07d4SSebastian Ott 	if (!strcmp(str, "force_floating")) {
1077fbfe07d4SSebastian Ott 		s390_pci_force_floating = 1;
1078fbfe07d4SSebastian Ott 		return NULL;
1079fbfe07d4SSebastian Ott 	}
10806cf17f9aSPierre Morel 	if (!strcmp(str, "norid")) {
10816cf17f9aSPierre Morel 		s390_pci_no_rid = 1;
10826cf17f9aSPierre Morel 		return NULL;
10836cf17f9aSPierre Morel 	}
1084cd248341SJan Glauber 	return str;
1085cd248341SJan Glauber }
1086cd248341SJan Glauber 
1087aa3b7c29SSebastian Ott bool zpci_is_enabled(void)
1088aa3b7c29SSebastian Ott {
1089aa3b7c29SSebastian Ott 	return s390_pci_initialized;
1090aa3b7c29SSebastian Ott }
1091aa3b7c29SSebastian Ott 
1092cd248341SJan Glauber static int __init pci_base_init(void)
1093cd248341SJan Glauber {
1094cd248341SJan Glauber 	int rc;
1095cd248341SJan Glauber 
10961e5635d1SHeiko Carstens 	if (!s390_pci_probe)
1097cd248341SJan Glauber 		return 0;
1098cd248341SJan Glauber 
1099da78693eSNiklas Schnelle 	if (!test_facility(69) || !test_facility(71)) {
1100da78693eSNiklas Schnelle 		pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
1101cd248341SJan Glauber 		return 0;
1102da78693eSNiklas Schnelle 	}
1103cd248341SJan Glauber 
11043322ba0dSNiklas Schnelle 	if (MACHINE_HAS_PCI_MIO) {
110571ba41c9SSebastian Ott 		static_branch_enable(&have_mio);
110699441a38SHeiko Carstens 		system_ctl_set_bit(2, CR2_MIO_ADDRESSING_BIT);
11079964f396SSebastian Ott 	}
110871ba41c9SSebastian Ott 
1109d0b08853SJan Glauber 	rc = zpci_debug_init();
1110d0b08853SJan Glauber 	if (rc)
11111f44a225SMartin Schwidefsky 		goto out;
1112d0b08853SJan Glauber 
1113cd248341SJan Glauber 	rc = zpci_mem_init();
1114cd248341SJan Glauber 	if (rc)
1115cd248341SJan Glauber 		goto out_mem;
1116cd248341SJan Glauber 
11179a4da8a5SJan Glauber 	rc = zpci_irq_init();
11189a4da8a5SJan Glauber 	if (rc)
11199a4da8a5SJan Glauber 		goto out_irq;
11209a4da8a5SJan Glauber 
11211d578966SSebastian Ott 	rc = clp_scan_pci_devices();
1122a755a45dSJan Glauber 	if (rc)
1123a755a45dSJan Glauber 		goto out_find;
112414c87ba8SNiklas Schnelle 	zpci_bus_scan_busses();
1125a755a45dSJan Glauber 
1126aa3b7c29SSebastian Ott 	s390_pci_initialized = 1;
1127cd248341SJan Glauber 	return 0;
1128cd248341SJan Glauber 
1129a755a45dSJan Glauber out_find:
11309a4da8a5SJan Glauber 	zpci_irq_exit();
11319a4da8a5SJan Glauber out_irq:
1132cd248341SJan Glauber 	zpci_mem_exit();
1133cd248341SJan Glauber out_mem:
1134d0b08853SJan Glauber 	zpci_debug_exit();
11351f44a225SMartin Schwidefsky out:
1136cd248341SJan Glauber 	return rc;
1137cd248341SJan Glauber }
113867f43f38SSebastian Ott subsys_initcall_sync(pci_base_init);
1139