Skip to content

Commit cbe4097

Browse files
dliviubjorn-helgaas
authored andcommitted
of/pci: Add support for parsing PCI host bridge resources from DT
Provide a function to parse the PCI DT ranges that can be used to create a pci_host_bridge structure together with its associated bus. Signed-off-by: Liviu Dudau <Liviu.Dudau@arm.com> [make io_base parameter optional] Signed-off-by: Robert Richter <rrichter@cavium.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> CC: Arnd Bergmann <arnd@arndb.de> CC: Grant Likely <grant.likely@linaro.org> CC: Rob Herring <robh+dt@kernel.org> CC: Catalin Marinas <catalin.marinas@arm.com>
1 parent 41e5c0f commit cbe4097

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎drivers/of/of_pci.c‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <linux/kernel.h>
22
#include <linux/export.h>
33
#include <linux/of.h>
4+
#include <linux/of_address.h>
45
#include <linux/of_pci.h>
6+
#include <linux/slab.h>
57

68
static inline int __of_pci_pci_compare(struct device_node *node,
79
unsigned int data)
@@ -114,6 +116,121 @@ int of_get_pci_domain_nr(struct device_node *node)
114116
}
115117
EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
116118

119+
#if defined(CONFIG_OF_ADDRESS)
120+
/**
121+
* of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
122+
* @dev: device node of the host bridge having the range property
123+
* @busno: bus number associated with the bridge root bus
124+
* @bus_max: maximum number of buses for this bridge
125+
* @resources: list where the range of resources will be added after DT parsing
126+
* @io_base: pointer to a variable that will contain on return the physical
127+
* address for the start of the I/O range. Can be NULL if the caller doesn't
128+
* expect IO ranges to be present in the device tree.
129+
*
130+
* It is the caller's job to free the @resources list.
131+
*
132+
* This function will parse the "ranges" property of a PCI host bridge device
133+
* node and setup the resource mapping based on its content. It is expected
134+
* that the property conforms with the Power ePAPR document.
135+
*
136+
* It returns zero if the range parsing has been successful or a standard error
137+
* value if it failed.
138+
*/
139+
int of_pci_get_host_bridge_resources(struct device_node *dev,
140+
unsigned char busno, unsigned char bus_max,
141+
struct list_head *resources, resource_size_t *io_base)
142+
{
143+
struct resource *res;
144+
struct resource *bus_range;
145+
struct of_pci_range range;
146+
struct of_pci_range_parser parser;
147+
char range_type[4];
148+
int err;
149+
150+
if (io_base)
151+
*io_base = (resource_size_t)OF_BAD_ADDR;
152+
153+
bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
154+
if (!bus_range)
155+
return -ENOMEM;
156+
157+
pr_info("PCI host bridge %s ranges:\n", dev->full_name);
158+
159+
err = of_pci_parse_bus_range(dev, bus_range);
160+
if (err) {
161+
bus_range->start = busno;
162+
bus_range->end = bus_max;
163+
bus_range->flags = IORESOURCE_BUS;
164+
pr_info(" No bus range found for %s, using %pR\n",
165+
dev->full_name, bus_range);
166+
} else {
167+
if (bus_range->end > bus_range->start + bus_max)
168+
bus_range->end = bus_range->start + bus_max;
169+
}
170+
pci_add_resource(resources, bus_range);
171+
172+
/* Check for ranges property */
173+
err = of_pci_range_parser_init(&parser, dev);
174+
if (err)
175+
goto parse_failed;
176+
177+
pr_debug("Parsing ranges property...\n");
178+
for_each_of_pci_range(&parser, &range) {
179+
/* Read next ranges element */
180+
if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
181+
snprintf(range_type, 4, " IO");
182+
else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
183+
snprintf(range_type, 4, "MEM");
184+
else
185+
snprintf(range_type, 4, "err");
186+
pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
187+
range.cpu_addr, range.cpu_addr + range.size - 1,
188+
range.pci_addr);
189+
190+
/*
191+
* If we failed translation or got a zero-sized region
192+
* then skip this range
193+
*/
194+
if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
195+
continue;
196+
197+
res = kzalloc(sizeof(struct resource), GFP_KERNEL);
198+
if (!res) {
199+
err = -ENOMEM;
200+
goto parse_failed;
201+
}
202+
203+
err = of_pci_range_to_resource(&range, dev, res);
204+
if (err)
205+
goto conversion_failed;
206+
207+
if (resource_type(res) == IORESOURCE_IO) {
208+
if (!io_base) {
209+
pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
210+
dev->full_name);
211+
err = -EINVAL;
212+
goto conversion_failed;
213+
}
214+
if (*io_base != (resource_size_t)OF_BAD_ADDR)
215+
pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
216+
dev->full_name);
217+
*io_base = range.cpu_addr;
218+
}
219+
220+
pci_add_resource_offset(resources, res, res->start - range.pci_addr);
221+
}
222+
223+
return 0;
224+
225+
conversion_failed:
226+
kfree(res);
227+
parse_failed:
228+
pci_free_resource_list(resources);
229+
return err;
230+
}
231+
EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
232+
#endif /* CONFIG_OF_ADDRESS */
233+
117234
#ifdef CONFIG_PCI_MSI
118235

119236
static LIST_HEAD(of_pci_msi_chip_list);

‎include/linux/of_pci.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ of_get_pci_domain_nr(struct device_node *node)
5252
}
5353
#endif
5454

55+
#if defined(CONFIG_OF_ADDRESS)
56+
int of_pci_get_host_bridge_resources(struct device_node *dev,
57+
unsigned char busno, unsigned char bus_max,
58+
struct list_head *resources, resource_size_t *io_base);
59+
#endif
60+
5561
#if defined(CONFIG_OF) && defined(CONFIG_PCI_MSI)
5662
int of_pci_msi_chip_add(struct msi_chip *chip);
5763
void of_pci_msi_chip_remove(struct msi_chip *chip);

0 commit comments

Comments
 (0)