Blame view

drivers/pci/ecam.c 3.99 KB
8cfab3cf6   Bjorn Helgaas   PCI: Add SPDX GPL...
1
  // SPDX-License-Identifier: GPL-2.0
35ff9477d   Jayachandran C   PCI: Provide comm...
2
3
  /*
   * Copyright 2016 Broadcom
35ff9477d   Jayachandran C   PCI: Provide comm...
4
5
6
7
8
9
10
   */
  
  #include <linux/device.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/pci.h>
80955f9ee   Jayachandran C   PCI: Move ecam.h ...
11
  #include <linux/pci-ecam.h>
35ff9477d   Jayachandran C   PCI: Provide comm...
12
  #include <linux/slab.h>
35ff9477d   Jayachandran C   PCI: Provide comm...
13
14
15
16
17
  /*
   * On 64-bit systems, we do a single ioremap for the whole config space
   * since we have enough virtual address range available.  On 32-bit, we
   * ioremap the config space for each bus individually.
   */
97f2645f3   Masahiro Yamada   tree-wide: replac...
18
  static const bool per_bus_mapping = !IS_ENABLED(CONFIG_64BIT);
35ff9477d   Jayachandran C   PCI: Provide comm...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  
  /*
   * Create a PCI config space window
   *  - reserve mem region
   *  - alloc struct pci_config_window with space for all mappings
   *  - ioremap the config space
   */
  struct pci_config_window *pci_ecam_create(struct device *dev,
  		struct resource *cfgres, struct resource *busr,
  		struct pci_ecam_ops *ops)
  {
  	struct pci_config_window *cfg;
  	unsigned int bus_range, bus_range_max, bsz;
  	struct resource *conflict;
  	int i, err;
  
  	if (busr->start > busr->end)
  		return ERR_PTR(-EINVAL);
  
  	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  	if (!cfg)
  		return ERR_PTR(-ENOMEM);
5c3d14f76   Jayachandran C   PCI: Add parent d...
41
  	cfg->parent = dev;
35ff9477d   Jayachandran C   PCI: Provide comm...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  	cfg->ops = ops;
  	cfg->busr.start = busr->start;
  	cfg->busr.end = busr->end;
  	cfg->busr.flags = IORESOURCE_BUS;
  	bus_range = resource_size(&cfg->busr);
  	bus_range_max = resource_size(cfgres) >> ops->bus_shift;
  	if (bus_range > bus_range_max) {
  		bus_range = bus_range_max;
  		cfg->busr.end = busr->start + bus_range - 1;
  		dev_warn(dev, "ECAM area %pR can only accommodate %pR (reduced from %pR desired)
  ",
  			 cfgres, &cfg->busr, busr);
  	}
  	bsz = 1 << ops->bus_shift;
  
  	cfg->res.start = cfgres->start;
  	cfg->res.end = cfgres->end;
  	cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  	cfg->res.name = "PCI ECAM";
  
  	conflict = request_resource_conflict(&iomem_resource, &cfg->res);
  	if (conflict) {
  		err = -EBUSY;
  		dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR
  ",
  			&cfg->res, conflict->name, conflict);
  		goto err_exit;
  	}
  
  	if (per_bus_mapping) {
  		cfg->winp = kcalloc(bus_range, sizeof(*cfg->winp), GFP_KERNEL);
  		if (!cfg->winp)
  			goto err_exit_malloc;
  		for (i = 0; i < bus_range; i++) {
053497cec   Lorenzo Pieralisi   PCI: ECAM: Map co...
76
77
78
  			cfg->winp[i] =
  				pci_remap_cfgspace(cfgres->start + i * bsz,
  						   bsz);
35ff9477d   Jayachandran C   PCI: Provide comm...
79
80
81
82
  			if (!cfg->winp[i])
  				goto err_exit_iomap;
  		}
  	} else {
053497cec   Lorenzo Pieralisi   PCI: ECAM: Map co...
83
  		cfg->win = pci_remap_cfgspace(cfgres->start, bus_range * bsz);
35ff9477d   Jayachandran C   PCI: Provide comm...
84
85
86
87
88
  		if (!cfg->win)
  			goto err_exit_iomap;
  	}
  
  	if (ops->init) {
5c3d14f76   Jayachandran C   PCI: Add parent d...
89
  		err = ops->init(cfg);
35ff9477d   Jayachandran C   PCI: Provide comm...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  		if (err)
  			goto err_exit;
  	}
  	dev_info(dev, "ECAM at %pR for %pR
  ", &cfg->res, &cfg->busr);
  	return cfg;
  
  err_exit_iomap:
  	dev_err(dev, "ECAM ioremap failed
  ");
  err_exit_malloc:
  	err = -ENOMEM;
  err_exit:
  	pci_ecam_free(cfg);
  	return ERR_PTR(err);
  }
  
  void pci_ecam_free(struct pci_config_window *cfg)
  {
  	int i;
  
  	if (per_bus_mapping) {
  		if (cfg->winp) {
  			for (i = 0; i < resource_size(&cfg->busr); i++)
  				if (cfg->winp[i])
  					iounmap(cfg->winp[i]);
  			kfree(cfg->winp);
  		}
  	} else {
  		if (cfg->win)
  			iounmap(cfg->win);
  	}
  	if (cfg->res.parent)
  		release_resource(&cfg->res);
  	kfree(cfg);
  }
  
  /*
   * Function to implement the pci_ops ->map_bus method
   */
  void __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
  			       int where)
  {
  	struct pci_config_window *cfg = bus->sysdata;
  	unsigned int devfn_shift = cfg->ops->bus_shift - 8;
  	unsigned int busn = bus->number;
  	void __iomem *base;
  
  	if (busn < cfg->busr.start || busn > cfg->busr.end)
  		return NULL;
  
  	busn -= cfg->busr.start;
  	if (per_bus_mapping)
  		base = cfg->winp[busn];
  	else
  		base = cfg->win + (busn << cfg->ops->bus_shift);
  	return base + (devfn << devfn_shift) + where;
  }
  
  /* ECAM ops */
  struct pci_ecam_ops pci_generic_ecam_ops = {
  	.bus_shift	= 20,
  	.pci_ops	= {
  		.map_bus	= pci_ecam_map_bus,
  		.read		= pci_generic_config_read,
  		.write		= pci_generic_config_write,
  	}
  };
2ca5b8ddc   Christopher Covington   PCI: Add MCFG qui...
158
159
160
161
162
163
164
165
166
167
168
169
  
  #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
  /* ECAM ops for 32-bit access only (non-compliant) */
  struct pci_ecam_ops pci_32b_ops = {
  	.bus_shift	= 20,
  	.pci_ops	= {
  		.map_bus	= pci_ecam_map_bus,
  		.read		= pci_generic_config_read32,
  		.write		= pci_generic_config_write32,
  	}
  };
  #endif