Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/loongarch/pci/acpi.c
26424 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4
*/
5
#include <linux/pci.h>
6
#include <linux/acpi.h>
7
#include <linux/init.h>
8
#include <linux/irq.h>
9
#include <linux/slab.h>
10
#include <linux/pci-acpi.h>
11
#include <linux/pci-ecam.h>
12
13
#include <asm/pci.h>
14
#include <asm/numa.h>
15
#include <asm/loongson.h>
16
17
struct pci_root_info {
18
struct acpi_pci_root_info common;
19
struct pci_config_window *cfg;
20
};
21
22
void pcibios_add_bus(struct pci_bus *bus)
23
{
24
acpi_pci_add_bus(bus);
25
}
26
27
int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
28
{
29
struct acpi_device *adev = NULL;
30
struct device *bus_dev = &bridge->bus->dev;
31
struct pci_config_window *cfg = bridge->bus->sysdata;
32
33
if (!acpi_disabled)
34
adev = to_acpi_device(cfg->parent);
35
36
ACPI_COMPANION_SET(&bridge->dev, adev);
37
set_dev_node(bus_dev, pa_to_nid(cfg->res.start));
38
39
return 0;
40
}
41
42
int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
43
{
44
struct pci_config_window *cfg = bus->sysdata;
45
struct acpi_device *adev = to_acpi_device(cfg->parent);
46
struct acpi_pci_root *root = acpi_driver_data(adev);
47
48
return root->segment;
49
}
50
51
static void acpi_release_root_info(struct acpi_pci_root_info *ci)
52
{
53
struct pci_root_info *info;
54
55
info = container_of(ci, struct pci_root_info, common);
56
pci_ecam_free(info->cfg);
57
kfree(ci->ops);
58
kfree(info);
59
}
60
61
static int acpi_prepare_root_resources(struct acpi_pci_root_info *ci)
62
{
63
int status;
64
struct resource_entry *entry, *tmp;
65
struct acpi_device *device = ci->bridge;
66
67
status = acpi_pci_probe_root_resources(ci);
68
if (status > 0) {
69
resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
70
if (entry->res->flags & IORESOURCE_MEM) {
71
entry->offset = ci->root->mcfg_addr & GENMASK_ULL(63, 40);
72
entry->res->start |= entry->offset;
73
entry->res->end |= entry->offset;
74
}
75
}
76
return status;
77
}
78
79
resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
80
dev_dbg(&device->dev,
81
"host bridge window %pR (ignored)\n", entry->res);
82
resource_list_destroy_entry(entry);
83
}
84
85
return 0;
86
}
87
88
/*
89
* Create a PCI config space window
90
* - reserve mem region
91
* - alloc struct pci_config_window with space for all mappings
92
* - ioremap the config space
93
*/
94
static struct pci_config_window *arch_pci_ecam_create(struct device *dev,
95
struct resource *cfgres, struct resource *busr, const struct pci_ecam_ops *ops)
96
{
97
int bsz, bus_range, err;
98
struct resource *conflict;
99
struct pci_config_window *cfg;
100
101
if (busr->start > busr->end)
102
return ERR_PTR(-EINVAL);
103
104
cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
105
if (!cfg)
106
return ERR_PTR(-ENOMEM);
107
108
cfg->parent = dev;
109
cfg->ops = ops;
110
cfg->busr.start = busr->start;
111
cfg->busr.end = busr->end;
112
cfg->busr.flags = IORESOURCE_BUS;
113
bus_range = resource_size(cfgres) >> ops->bus_shift;
114
115
bsz = 1 << ops->bus_shift;
116
117
cfg->res.start = cfgres->start;
118
cfg->res.end = cfgres->end;
119
cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
120
cfg->res.name = "PCI ECAM";
121
122
conflict = request_resource_conflict(&iomem_resource, &cfg->res);
123
if (conflict) {
124
err = -EBUSY;
125
dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
126
&cfg->res, conflict->name, conflict);
127
goto err_exit;
128
}
129
130
cfg->win = pci_remap_cfgspace(cfgres->start, bus_range * bsz);
131
if (!cfg->win)
132
goto err_exit_iomap;
133
134
if (ops->init) {
135
err = ops->init(cfg);
136
if (err)
137
goto err_exit;
138
}
139
dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
140
141
return cfg;
142
143
err_exit_iomap:
144
err = -ENOMEM;
145
dev_err(dev, "ECAM ioremap failed\n");
146
err_exit:
147
pci_ecam_free(cfg);
148
return ERR_PTR(err);
149
}
150
151
/*
152
* Lookup the bus range for the domain in MCFG, and set up config space
153
* mapping.
154
*/
155
static struct pci_config_window *
156
pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
157
{
158
int ret, bus_shift;
159
u16 seg = root->segment;
160
struct device *dev = &root->device->dev;
161
struct resource cfgres;
162
struct resource *bus_res = &root->secondary;
163
struct pci_config_window *cfg;
164
const struct pci_ecam_ops *ecam_ops;
165
166
ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
167
if (ret < 0) {
168
dev_err(dev, "%04x:%pR ECAM region not found, use default value\n", seg, bus_res);
169
ecam_ops = &loongson_pci_ecam_ops;
170
root->mcfg_addr = mcfg_addr_init(0);
171
}
172
173
bus_shift = ecam_ops->bus_shift ? : 20;
174
175
if (bus_shift == 20)
176
cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
177
else {
178
cfgres.start = root->mcfg_addr + (bus_res->start << bus_shift);
179
cfgres.end = cfgres.start + (resource_size(bus_res) << bus_shift) - 1;
180
cfgres.end |= BIT(28) + (((PCI_CFG_SPACE_EXP_SIZE - 1) & 0xf00) << 16);
181
cfgres.flags = IORESOURCE_MEM;
182
cfg = arch_pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
183
}
184
185
if (IS_ERR(cfg)) {
186
dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res, PTR_ERR(cfg));
187
return NULL;
188
}
189
190
return cfg;
191
}
192
193
struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
194
{
195
struct pci_bus *bus;
196
struct pci_root_info *info;
197
struct pci_host_bridge *host;
198
struct acpi_pci_root_ops *root_ops;
199
int domain = root->segment;
200
int busnum = root->secondary.start;
201
202
info = kzalloc(sizeof(*info), GFP_KERNEL);
203
if (!info) {
204
pr_warn("pci_bus %04x:%02x: ignored (out of memory)\n", domain, busnum);
205
return NULL;
206
}
207
208
root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL);
209
if (!root_ops) {
210
kfree(info);
211
return NULL;
212
}
213
214
info->cfg = pci_acpi_setup_ecam_mapping(root);
215
if (!info->cfg) {
216
kfree(info);
217
kfree(root_ops);
218
return NULL;
219
}
220
221
root_ops->release_info = acpi_release_root_info;
222
root_ops->prepare_resources = acpi_prepare_root_resources;
223
root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
224
225
bus = pci_find_bus(domain, busnum);
226
if (bus) {
227
memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
228
kfree(info);
229
kfree(root_ops);
230
} else {
231
struct pci_bus *child;
232
233
bus = acpi_pci_root_create(root, root_ops,
234
&info->common, info->cfg);
235
if (!bus) {
236
kfree(info);
237
kfree(root_ops);
238
return NULL;
239
}
240
241
/* If we must preserve the resource configuration, claim now */
242
host = pci_find_host_bridge(bus);
243
if (host->preserve_config)
244
pci_bus_claim_resources(bus);
245
246
/*
247
* Assign whatever was left unassigned. If we didn't claim above,
248
* this will reassign everything.
249
*/
250
pci_assign_unassigned_root_bus_resources(bus);
251
252
list_for_each_entry(child, &bus->children, node)
253
pcie_bus_configure_settings(child);
254
}
255
256
return bus;
257
}
258
259