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