xref: /linux/arch/loongarch/pci/acpi.c (revision 2ed2e359dea752e7758d29a423033031a0b96584)
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 
pcibios_add_bus(struct pci_bus * bus)22 void pcibios_add_bus(struct pci_bus *bus)
23 {
24 	acpi_pci_add_bus(bus);
25 }
26 
pcibios_root_bridge_prepare(struct pci_host_bridge * bridge)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 
acpi_pci_bus_find_domain_nr(struct pci_bus * bus)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 
acpi_release_root_info(struct acpi_pci_root_info * ci)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 
acpi_prepare_root_resources(struct acpi_pci_root_info * ci)61 static int acpi_prepare_root_resources(struct acpi_pci_root_info *ci)
62 {
63 	int status;
64 	unsigned long long pci_h = 0;
65 	struct resource_entry *entry, *tmp;
66 	struct acpi_device *device = ci->bridge;
67 
68 	acpi_remove_early_pio();
69 
70 	status = acpi_pci_probe_root_resources(ci);
71 	if (status > 0) {
72 		acpi_evaluate_integer(device->handle, "PCIH", NULL, &pci_h);
73 		if (pci_h)
74 			return status;
75 
76 		resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
77 			if (entry->res->flags & IORESOURCE_MEM) {
78 				entry->offset = ci->root->mcfg_addr & GENMASK_ULL(63, 40);
79 				entry->res->start |= entry->offset;
80 				entry->res->end   |= entry->offset;
81 			}
82 		}
83 		return status;
84 	}
85 
86 	resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
87 		dev_dbg(&device->dev,
88 			   "host bridge window %pR (ignored)\n", entry->res);
89 		resource_list_destroy_entry(entry);
90 	}
91 
92 	return 0;
93 }
94 
95 /*
96  * Create a PCI config space window
97  *  - reserve mem region
98  *  - alloc struct pci_config_window with space for all mappings
99  *  - ioremap the config space
100  */
arch_pci_ecam_create(struct device * dev,struct resource * cfgres,struct resource * busr,const struct pci_ecam_ops * ops)101 static struct pci_config_window *arch_pci_ecam_create(struct device *dev,
102 		struct resource *cfgres, struct resource *busr, const struct pci_ecam_ops *ops)
103 {
104 	int bsz, bus_range, err;
105 	struct resource *conflict;
106 	struct pci_config_window *cfg;
107 
108 	if (busr->start > busr->end)
109 		return ERR_PTR(-EINVAL);
110 
111 	cfg = kzalloc_obj(*cfg);
112 	if (!cfg)
113 		return ERR_PTR(-ENOMEM);
114 
115 	cfg->parent = dev;
116 	cfg->ops = ops;
117 	cfg->busr.start = busr->start;
118 	cfg->busr.end = busr->end;
119 	cfg->busr.flags = IORESOURCE_BUS;
120 	bus_range = resource_size(cfgres) >> ops->bus_shift;
121 
122 	bsz = 1 << ops->bus_shift;
123 
124 	cfg->res.start = cfgres->start;
125 	cfg->res.end = cfgres->end;
126 	cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
127 	cfg->res.name = "PCI ECAM";
128 
129 	conflict = request_resource_conflict(&iomem_resource, &cfg->res);
130 	if (conflict) {
131 		err = -EBUSY;
132 		dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
133 			&cfg->res, conflict->name, conflict);
134 		goto err_exit;
135 	}
136 
137 	cfg->win = pci_remap_cfgspace(cfgres->start, bus_range * bsz);
138 	if (!cfg->win)
139 		goto err_exit_iomap;
140 
141 	if (ops->init) {
142 		err = ops->init(cfg);
143 		if (err)
144 			goto err_exit;
145 	}
146 	dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
147 
148 	return cfg;
149 
150 err_exit_iomap:
151 	err = -ENOMEM;
152 	dev_err(dev, "ECAM ioremap failed\n");
153 err_exit:
154 	pci_ecam_free(cfg);
155 	return ERR_PTR(err);
156 }
157 
158 /*
159  * Lookup the bus range for the domain in MCFG, and set up config space
160  * mapping.
161  */
162 static struct pci_config_window *
pci_acpi_setup_ecam_mapping(struct acpi_pci_root * root)163 pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
164 {
165 	int ret, bus_shift;
166 	u16 seg = root->segment;
167 	struct device *dev = &root->device->dev;
168 	struct resource cfgres;
169 	struct resource *bus_res = &root->secondary;
170 	struct pci_config_window *cfg;
171 	const struct pci_ecam_ops *ecam_ops;
172 
173 	ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
174 	if (ret < 0) {
175 		dev_err(dev, "%04x:%pR ECAM region not found, use default value\n", seg, bus_res);
176 		ecam_ops = &loongson_pci_ecam_ops;
177 		root->mcfg_addr = mcfg_addr_init(0);
178 	}
179 
180 	bus_shift = ecam_ops->bus_shift ? : 20;
181 
182 	if (bus_shift == 20)
183 		cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
184 	else {
185 		cfgres.start = root->mcfg_addr + (bus_res->start << bus_shift);
186 		cfgres.end = cfgres.start + (resource_size(bus_res) << bus_shift) - 1;
187 		cfgres.end |= BIT(28) + (((PCI_CFG_SPACE_EXP_SIZE - 1) & 0xf00) << 16);
188 		cfgres.flags = IORESOURCE_MEM;
189 		cfg = arch_pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
190 	}
191 
192 	if (IS_ERR(cfg)) {
193 		dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res, PTR_ERR(cfg));
194 		return NULL;
195 	}
196 
197 	return cfg;
198 }
199 
pci_acpi_scan_root(struct acpi_pci_root * root)200 struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
201 {
202 	struct pci_bus *bus;
203 	struct pci_root_info *info;
204 	struct pci_host_bridge *host;
205 	struct acpi_pci_root_ops *root_ops;
206 	int domain = root->segment;
207 	int busnum = root->secondary.start;
208 
209 	info = kzalloc_obj(*info);
210 	if (!info) {
211 		pr_warn("pci_bus %04x:%02x: ignored (out of memory)\n", domain, busnum);
212 		return NULL;
213 	}
214 
215 	root_ops = kzalloc_obj(*root_ops);
216 	if (!root_ops) {
217 		kfree(info);
218 		return NULL;
219 	}
220 
221 	info->cfg = pci_acpi_setup_ecam_mapping(root);
222 	if (!info->cfg) {
223 		kfree(info);
224 		kfree(root_ops);
225 		return NULL;
226 	}
227 
228 	root_ops->release_info = acpi_release_root_info;
229 	root_ops->prepare_resources = acpi_prepare_root_resources;
230 	root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
231 
232 	bus = pci_find_bus(domain, busnum);
233 	if (bus) {
234 		memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
235 		kfree(info);
236 		kfree(root_ops);
237 	} else {
238 		struct pci_bus *child;
239 
240 		bus = acpi_pci_root_create(root, root_ops,
241 					   &info->common, info->cfg);
242 		if (!bus) {
243 			kfree(info);
244 			kfree(root_ops);
245 			return NULL;
246 		}
247 
248 		/* If we must preserve the resource configuration, claim now */
249 		host = pci_find_host_bridge(bus);
250 		if (host->preserve_config)
251 			pci_bus_claim_resources(bus);
252 
253 		/*
254 		 * Assign whatever was left unassigned. If we didn't claim above,
255 		 * this will reassign everything.
256 		 */
257 		pci_assign_unassigned_root_bus_resources(bus);
258 
259 		list_for_each_entry(child, &bus->children, node)
260 			pcie_bus_configure_settings(child);
261 	}
262 
263 	return bus;
264 }
265