1*28ad9e6dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fb9aa6f1SThomas Gleixner /*
3fb9aa6f1SThomas Gleixner * Copyright (C) 2004 Matthew Wilcox <matthew@wil.cx>
4fb9aa6f1SThomas Gleixner * Copyright (C) 2004 Intel Corp.
5fb9aa6f1SThomas Gleixner */
6fb9aa6f1SThomas Gleixner
7fb9aa6f1SThomas Gleixner /*
8fb9aa6f1SThomas Gleixner * mmconfig.c - Low-level direct PCI config space access via MMCONFIG
9fb9aa6f1SThomas Gleixner */
10fb9aa6f1SThomas Gleixner
11fb9aa6f1SThomas Gleixner #include <linux/pci.h>
12fb9aa6f1SThomas Gleixner #include <linux/init.h>
13376f70acSJiang Liu #include <linux/rcupdate.h>
1466441bd3SIngo Molnar #include <asm/e820/api.h>
1582487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
16fb9aa6f1SThomas Gleixner
17fb9aa6f1SThomas Gleixner /* Assume systems with more busses have correct MCFG */
18fb9aa6f1SThomas Gleixner #define mmcfg_virt_addr ((void __iomem *) fix_to_virt(FIX_PCIE_MCFG))
19fb9aa6f1SThomas Gleixner
20fb9aa6f1SThomas Gleixner /* The base address of the last MMCONFIG device accessed */
21fb9aa6f1SThomas Gleixner static u32 mmcfg_last_accessed_device;
22fb9aa6f1SThomas Gleixner static int mmcfg_last_accessed_cpu;
23fb9aa6f1SThomas Gleixner
24fb9aa6f1SThomas Gleixner /*
25fb9aa6f1SThomas Gleixner * Functions for accessing PCI configuration space with MMCONFIG accesses
26fb9aa6f1SThomas Gleixner */
get_base_addr(unsigned int seg,int bus,unsigned devfn)27fb9aa6f1SThomas Gleixner static u32 get_base_addr(unsigned int seg, int bus, unsigned devfn)
28fb9aa6f1SThomas Gleixner {
29f6e1d8ccSBjorn Helgaas struct pci_mmcfg_region *cfg = pci_mmconfig_lookup(seg, bus);
30fb9aa6f1SThomas Gleixner
31f6e1d8ccSBjorn Helgaas if (cfg)
32fb9aa6f1SThomas Gleixner return cfg->address;
33fb9aa6f1SThomas Gleixner return 0;
34fb9aa6f1SThomas Gleixner }
35fb9aa6f1SThomas Gleixner
36fb9aa6f1SThomas Gleixner /*
37fb9aa6f1SThomas Gleixner * This is always called under pci_config_lock
38fb9aa6f1SThomas Gleixner */
pci_exp_set_dev_base(unsigned int base,int bus,int devfn)39fb9aa6f1SThomas Gleixner static void pci_exp_set_dev_base(unsigned int base, int bus, int devfn)
40fb9aa6f1SThomas Gleixner {
41df5eb1d6SBjorn Helgaas u32 dev_base = base | PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12);
42fb9aa6f1SThomas Gleixner int cpu = smp_processor_id();
43fb9aa6f1SThomas Gleixner if (dev_base != mmcfg_last_accessed_device ||
44fb9aa6f1SThomas Gleixner cpu != mmcfg_last_accessed_cpu) {
45fb9aa6f1SThomas Gleixner mmcfg_last_accessed_device = dev_base;
46fb9aa6f1SThomas Gleixner mmcfg_last_accessed_cpu = cpu;
47fb9aa6f1SThomas Gleixner set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
48fb9aa6f1SThomas Gleixner }
49fb9aa6f1SThomas Gleixner }
50fb9aa6f1SThomas Gleixner
pci_mmcfg_read(unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,u32 * value)51fb9aa6f1SThomas Gleixner static int pci_mmcfg_read(unsigned int seg, unsigned int bus,
52fb9aa6f1SThomas Gleixner unsigned int devfn, int reg, int len, u32 *value)
53fb9aa6f1SThomas Gleixner {
54fb9aa6f1SThomas Gleixner unsigned long flags;
55fb9aa6f1SThomas Gleixner u32 base;
56fb9aa6f1SThomas Gleixner
57fb9aa6f1SThomas Gleixner if ((bus > 255) || (devfn > 255) || (reg > 4095)) {
58a0ca9909SIvan Kokshaysky err: *value = -1;
59fb9aa6f1SThomas Gleixner return -EINVAL;
60fb9aa6f1SThomas Gleixner }
61fb9aa6f1SThomas Gleixner
62376f70acSJiang Liu rcu_read_lock();
63fb9aa6f1SThomas Gleixner base = get_base_addr(seg, bus, devfn);
64376f70acSJiang Liu if (!base) {
65376f70acSJiang Liu rcu_read_unlock();
66a0ca9909SIvan Kokshaysky goto err;
67376f70acSJiang Liu }
68fb9aa6f1SThomas Gleixner
69d19f61f0SThomas Gleixner raw_spin_lock_irqsave(&pci_config_lock, flags);
70fb9aa6f1SThomas Gleixner
71fb9aa6f1SThomas Gleixner pci_exp_set_dev_base(base, bus, devfn);
72fb9aa6f1SThomas Gleixner
73fb9aa6f1SThomas Gleixner switch (len) {
74fb9aa6f1SThomas Gleixner case 1:
75fb9aa6f1SThomas Gleixner *value = mmio_config_readb(mmcfg_virt_addr + reg);
76fb9aa6f1SThomas Gleixner break;
77fb9aa6f1SThomas Gleixner case 2:
78fb9aa6f1SThomas Gleixner *value = mmio_config_readw(mmcfg_virt_addr + reg);
79fb9aa6f1SThomas Gleixner break;
80fb9aa6f1SThomas Gleixner case 4:
81fb9aa6f1SThomas Gleixner *value = mmio_config_readl(mmcfg_virt_addr + reg);
82fb9aa6f1SThomas Gleixner break;
83fb9aa6f1SThomas Gleixner }
84d19f61f0SThomas Gleixner raw_spin_unlock_irqrestore(&pci_config_lock, flags);
85376f70acSJiang Liu rcu_read_unlock();
86fb9aa6f1SThomas Gleixner
87fb9aa6f1SThomas Gleixner return 0;
88fb9aa6f1SThomas Gleixner }
89fb9aa6f1SThomas Gleixner
pci_mmcfg_write(unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,u32 value)90fb9aa6f1SThomas Gleixner static int pci_mmcfg_write(unsigned int seg, unsigned int bus,
91fb9aa6f1SThomas Gleixner unsigned int devfn, int reg, int len, u32 value)
92fb9aa6f1SThomas Gleixner {
93fb9aa6f1SThomas Gleixner unsigned long flags;
94fb9aa6f1SThomas Gleixner u32 base;
95fb9aa6f1SThomas Gleixner
96fb9aa6f1SThomas Gleixner if ((bus > 255) || (devfn > 255) || (reg > 4095))
97fb9aa6f1SThomas Gleixner return -EINVAL;
98fb9aa6f1SThomas Gleixner
99376f70acSJiang Liu rcu_read_lock();
100fb9aa6f1SThomas Gleixner base = get_base_addr(seg, bus, devfn);
101376f70acSJiang Liu if (!base) {
102376f70acSJiang Liu rcu_read_unlock();
103a0ca9909SIvan Kokshaysky return -EINVAL;
104376f70acSJiang Liu }
105fb9aa6f1SThomas Gleixner
106d19f61f0SThomas Gleixner raw_spin_lock_irqsave(&pci_config_lock, flags);
107fb9aa6f1SThomas Gleixner
108fb9aa6f1SThomas Gleixner pci_exp_set_dev_base(base, bus, devfn);
109fb9aa6f1SThomas Gleixner
110fb9aa6f1SThomas Gleixner switch (len) {
111fb9aa6f1SThomas Gleixner case 1:
112fb9aa6f1SThomas Gleixner mmio_config_writeb(mmcfg_virt_addr + reg, value);
113fb9aa6f1SThomas Gleixner break;
114fb9aa6f1SThomas Gleixner case 2:
115fb9aa6f1SThomas Gleixner mmio_config_writew(mmcfg_virt_addr + reg, value);
116fb9aa6f1SThomas Gleixner break;
117fb9aa6f1SThomas Gleixner case 4:
118fb9aa6f1SThomas Gleixner mmio_config_writel(mmcfg_virt_addr + reg, value);
119fb9aa6f1SThomas Gleixner break;
120fb9aa6f1SThomas Gleixner }
121d19f61f0SThomas Gleixner raw_spin_unlock_irqrestore(&pci_config_lock, flags);
122376f70acSJiang Liu rcu_read_unlock();
123fb9aa6f1SThomas Gleixner
124fb9aa6f1SThomas Gleixner return 0;
125fb9aa6f1SThomas Gleixner }
126fb9aa6f1SThomas Gleixner
127c0fa4078SJiang Liu const struct pci_raw_ops pci_mmcfg = {
128fb9aa6f1SThomas Gleixner .read = pci_mmcfg_read,
129fb9aa6f1SThomas Gleixner .write = pci_mmcfg_write,
130fb9aa6f1SThomas Gleixner };
131fb9aa6f1SThomas Gleixner
pci_mmcfg_arch_init(void)132fb9aa6f1SThomas Gleixner int __init pci_mmcfg_arch_init(void)
133fb9aa6f1SThomas Gleixner {
134b6ce068aSMatthew Wilcox printk(KERN_INFO "PCI: Using MMCONFIG for extended config space\n");
135b6ce068aSMatthew Wilcox raw_pci_ext_ops = &pci_mmcfg;
136fb9aa6f1SThomas Gleixner return 1;
137fb9aa6f1SThomas Gleixner }
1380b64ad71SYinghai Lu
pci_mmcfg_arch_free(void)1390b64ad71SYinghai Lu void __init pci_mmcfg_arch_free(void)
1400b64ad71SYinghai Lu {
1410b64ad71SYinghai Lu }
1429cf0105dSJiang Liu
pci_mmcfg_arch_map(struct pci_mmcfg_region * cfg)143a18e3690SGreg Kroah-Hartman int pci_mmcfg_arch_map(struct pci_mmcfg_region *cfg)
1449cf0105dSJiang Liu {
1459cf0105dSJiang Liu return 0;
1469cf0105dSJiang Liu }
1479cf0105dSJiang Liu
pci_mmcfg_arch_unmap(struct pci_mmcfg_region * cfg)1489cf0105dSJiang Liu void pci_mmcfg_arch_unmap(struct pci_mmcfg_region *cfg)
1499cf0105dSJiang Liu {
1509cf0105dSJiang Liu unsigned long flags;
1519cf0105dSJiang Liu
1529cf0105dSJiang Liu /* Invalidate the cached mmcfg map entry. */
1539cf0105dSJiang Liu raw_spin_lock_irqsave(&pci_config_lock, flags);
1549cf0105dSJiang Liu mmcfg_last_accessed_device = 0;
1559cf0105dSJiang Liu raw_spin_unlock_irqrestore(&pci_config_lock, flags);
1569cf0105dSJiang Liu }
157