Skip to content

Commit 98d9f30

Browse files
committed
pci/of: Match PCI devices to OF nodes dynamically
powerpc has two different ways of matching PCI devices to their corresponding OF node (if any) for historical reasons. The ppc64 one does a scan looking for matching bus/dev/fn, while the ppc32 one does a scan looking only for matching dev/fn on each level in order to be agnostic to busses being renumbered (which Linux does on some platforms). This removes both and instead moves the matching code to the PCI core itself. It's the most logical place to do it: when a pci_dev is created, we know the parent and thus can do a single level scan for the matching device_node (if any). The benefit is that all archs now get the matching for free. There's one hook the arch might want to provide to match a PHB bus to its device node. A default weak implementation is provided that looks for the parent device device node, but it's not entirely reliable on powerpc for various reasons so powerpc provides its own. Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org> Acked-by: Michal Simek <monstr@monstr.eu> Acked-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent 1fa7b6a commit 98d9f30

22 files changed

Lines changed: 275 additions & 376 deletions

File tree

‎arch/microblaze/include/asm/pci-bridge.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ struct pci_controller {
105105
};
106106

107107
#ifdef CONFIG_PCI
108-
static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus)
108+
static inline struct device_node *pci_device_to_OF_node(struct pci_dev *dev)
109109
{
110-
return bus->sysdata;
110+
return dev->dev.of_node;
111111
}
112112

113113
static inline struct device_node *pci_bus_to_OF_node(struct pci_bus *bus)
114114
{
115-
struct pci_controller *host;
115+
return bus->dev.of_node;
116+
}
116117

117-
if (bus->self)
118-
return pci_device_to_OF_node(bus->self);
119-
host = pci_bus_to_host(bus);
120-
return host ? host->dn : NULL;
118+
static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus)
119+
{
120+
return bus->sysdata;
121121
}
122122

123123
static inline int isa_vaddr_is_ioport(void __iomem *address)

‎arch/microblaze/include/asm/prom.h‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@
2929
extern int early_uartlite_console(void);
3030
extern int early_uart16550_console(void);
3131

32-
#ifdef CONFIG_PCI
33-
/*
34-
* PCI <-> OF matching functions
35-
* (XXX should these be here?)
36-
*/
37-
struct pci_bus;
38-
struct pci_dev;
39-
extern int pci_device_from_OF_node(struct device_node *node,
40-
u8 *bus, u8 *devfn);
41-
extern struct device_node *pci_busdev_to_OF_node(struct pci_bus *bus,
42-
int devfn);
43-
extern struct device_node *pci_device_to_OF_node(struct pci_dev *dev);
44-
extern void pci_create_OF_bus_map(void);
45-
#endif
46-
4732
/*
4833
* OF address retreival & translation
4934
*/

‎arch/microblaze/pci/pci_32.c‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -210,38 +210,6 @@ static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
210210
return np;
211211
}
212212

213-
/*
214-
* Scans the OF tree for a device node matching a PCI device
215-
*/
216-
struct device_node *
217-
pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
218-
{
219-
struct device_node *parent, *np;
220-
221-
pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
222-
parent = scan_OF_for_pci_bus(bus);
223-
if (parent == NULL)
224-
return NULL;
225-
pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
226-
np = scan_OF_for_pci_dev(parent, devfn);
227-
of_node_put(parent);
228-
pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
229-
230-
/* XXX most callers don't release the returned node
231-
* mostly because ppc64 doesn't increase the refcount,
232-
* we need to fix that.
233-
*/
234-
return np;
235-
}
236-
EXPORT_SYMBOL(pci_busdev_to_OF_node);
237-
238-
struct device_node*
239-
pci_device_to_OF_node(struct pci_dev *dev)
240-
{
241-
return pci_busdev_to_OF_node(dev->bus, dev->devfn);
242-
}
243-
EXPORT_SYMBOL(pci_device_to_OF_node);
244-
245213
static int
246214
find_OF_pci_device_filter(struct device_node *node, void *data)
247215
{
@@ -315,6 +283,13 @@ pci_create_OF_bus_map(void)
315283
}
316284
}
317285

286+
struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
287+
{
288+
struct pci_controller *hose = bus->sysdata;
289+
290+
return of_node_get(hose->dn);
291+
}
292+
318293
static void __devinit pcibios_scan_phb(struct pci_controller *hose)
319294
{
320295
struct pci_bus *bus;
@@ -332,7 +307,6 @@ static void __devinit pcibios_scan_phb(struct pci_controller *hose)
332307
hose->global_number);
333308
return;
334309
}
335-
bus.dev->of_node = of_node_get(node);
336310
bus->secondary = hose->first_busno;
337311
hose->bus = bus;
338312

‎arch/powerpc/include/asm/pci-bridge.h‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,22 @@ static inline struct pci_controller *pci_bus_to_host(const struct pci_bus *bus)
169169
return bus->sysdata;
170170
}
171171

172-
#ifndef CONFIG_PPC64
172+
static inline struct device_node *pci_device_to_OF_node(struct pci_dev *dev)
173+
{
174+
return dev->dev.of_node;
175+
}
173176

174177
static inline struct device_node *pci_bus_to_OF_node(struct pci_bus *bus)
175178
{
176-
struct pci_controller *host;
177-
178-
if (bus->self)
179-
return pci_device_to_OF_node(bus->self);
180-
host = pci_bus_to_host(bus);
181-
return host ? host->dn : NULL;
179+
return bus->dev.of_node;
182180
}
183181

182+
#ifndef CONFIG_PPC64
183+
184+
extern int pci_device_from_OF_node(struct device_node *node,
185+
u8 *bus, u8 *devfn);
186+
extern void pci_create_OF_bus_map(void);
187+
184188
static inline int isa_vaddr_is_ioport(void __iomem *address)
185189
{
186190
/* No specific ISA handling on ppc32 at this stage, it
@@ -223,17 +227,8 @@ struct pci_dn {
223227
/* Get the pointer to a device_node's pci_dn */
224228
#define PCI_DN(dn) ((struct pci_dn *) (dn)->data)
225229

226-
extern struct device_node *fetch_dev_dn(struct pci_dev *dev);
227230
extern void * update_dn_pci_info(struct device_node *dn, void *data);
228231

229-
/* Get a device_node from a pci_dev. This code must be fast except
230-
* in the case where the sysdata is incorrect and needs to be fixed
231-
* up (this will only happen once). */
232-
static inline struct device_node *pci_device_to_OF_node(struct pci_dev *dev)
233-
{
234-
return dev->dev.of_node ? dev->dev.of_node : fetch_dev_dn(dev);
235-
}
236-
237232
static inline int pci_device_from_OF_node(struct device_node *np,
238233
u8 *bus, u8 *devfn)
239234
{
@@ -244,14 +239,6 @@ static inline int pci_device_from_OF_node(struct device_node *np,
244239
return 0;
245240
}
246241

247-
static inline struct device_node *pci_bus_to_OF_node(struct pci_bus *bus)
248-
{
249-
if (bus->self)
250-
return pci_device_to_OF_node(bus->self);
251-
else
252-
return bus->dev.of_node; /* Must be root bus (PHB) */
253-
}
254-
255242
/** Find the bus corresponding to the indicated device node */
256243
extern struct pci_bus *pcibios_find_pci_bus(struct device_node *dn);
257244

‎arch/powerpc/include/asm/pci.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ extern int remove_phb_dynamic(struct pci_controller *phb);
179179
extern struct pci_dev *of_create_pci_dev(struct device_node *node,
180180
struct pci_bus *bus, int devfn);
181181

182-
extern void of_scan_pci_bridge(struct device_node *node,
183-
struct pci_dev *dev);
182+
extern void of_scan_pci_bridge(struct pci_dev *dev);
184183

185184
extern void of_scan_bus(struct device_node *node, struct pci_bus *bus);
186185
extern void of_rescan_bus(struct device_node *node, struct pci_bus *bus);

‎arch/powerpc/include/asm/prom.h‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@
2222

2323
#define HAVE_ARCH_DEVTREE_FIXUPS
2424

25-
#ifdef CONFIG_PPC32
26-
/*
27-
* PCI <-> OF matching functions
28-
* (XXX should these be here?)
29-
*/
30-
struct pci_bus;
31-
struct pci_dev;
32-
extern int pci_device_from_OF_node(struct device_node *node,
33-
u8* bus, u8* devfn);
34-
extern struct device_node* pci_busdev_to_OF_node(struct pci_bus *, int);
35-
extern struct device_node* pci_device_to_OF_node(struct pci_dev *);
36-
extern void pci_create_OF_bus_map(void);
37-
#endif
38-
3925
/*
4026
* OF address retreival & translation
4127
*/

‎arch/powerpc/kernel/pci-common.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,6 @@ void __devinit pcibios_setup_bus_devices(struct pci_bus *bus)
10971097
if (dev->is_added)
10981098
continue;
10991099

1100-
/* Setup OF node pointer in the device */
1101-
dev->dev.of_node = pci_device_to_OF_node(dev);
1102-
11031100
/* Fixup NUMA node as it may not be setup yet by the generic
11041101
* code and is needed by the DMA init
11051102
*/
@@ -1685,6 +1682,13 @@ int early_find_capability(struct pci_controller *hose, int bus, int devfn,
16851682
return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
16861683
}
16871684

1685+
struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
1686+
{
1687+
struct pci_controller *hose = bus->sysdata;
1688+
1689+
return of_node_get(hose->dn);
1690+
}
1691+
16881692
/**
16891693
* pci_scan_phb - Given a pci_controller, setup and scan the PCI bus
16901694
* @hose: Pointer to the PCI host controller instance structure
@@ -1705,7 +1709,6 @@ void __devinit pcibios_scan_phb(struct pci_controller *hose)
17051709
hose->global_number);
17061710
return;
17071711
}
1708-
bus->dev.of_node = of_node_get(node);
17091712
bus->secondary = hose->first_busno;
17101713
hose->bus = bus;
17111714

‎arch/powerpc/kernel/pci_32.c‎

Lines changed: 13 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -167,150 +167,26 @@ pcibios_make_OF_bus_map(void)
167167
#endif
168168
}
169169

170-
typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data);
171-
172-
static struct device_node*
173-
scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data)
174-
{
175-
struct device_node *node;
176-
struct device_node* sub_node;
177-
178-
for_each_child_of_node(parent, node) {
179-
const unsigned int *class_code;
180-
181-
if (filter(node, data)) {
182-
of_node_put(node);
183-
return node;
184-
}
185-
186-
/* For PCI<->PCI bridges or CardBus bridges, we go down
187-
* Note: some OFs create a parent node "multifunc-device" as
188-
* a fake root for all functions of a multi-function device,
189-
* we go down them as well.
190-
*/
191-
class_code = of_get_property(node, "class-code", NULL);
192-
if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
193-
(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
194-
strcmp(node->name, "multifunc-device"))
195-
continue;
196-
sub_node = scan_OF_pci_childs(node, filter, data);
197-
if (sub_node) {
198-
of_node_put(node);
199-
return sub_node;
200-
}
201-
}
202-
return NULL;
203-
}
204-
205-
static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
206-
unsigned int devfn)
207-
{
208-
struct device_node *np, *cnp;
209-
const u32 *reg;
210-
unsigned int psize;
211-
212-
for_each_child_of_node(parent, np) {
213-
reg = of_get_property(np, "reg", &psize);
214-
if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
215-
return np;
216-
217-
/* Note: some OFs create a parent node "multifunc-device" as
218-
* a fake root for all functions of a multi-function device,
219-
* we go down them as well. */
220-
if (!strcmp(np->name, "multifunc-device")) {
221-
cnp = scan_OF_for_pci_dev(np, devfn);
222-
if (cnp)
223-
return cnp;
224-
}
225-
}
226-
return NULL;
227-
}
228-
229-
230-
static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
231-
{
232-
struct device_node *parent, *np;
233-
234-
/* Are we a root bus ? */
235-
if (bus->self == NULL || bus->parent == NULL) {
236-
struct pci_controller *hose = pci_bus_to_host(bus);
237-
if (hose == NULL)
238-
return NULL;
239-
return of_node_get(hose->dn);
240-
}
241-
242-
/* not a root bus, we need to get our parent */
243-
parent = scan_OF_for_pci_bus(bus->parent);
244-
if (parent == NULL)
245-
return NULL;
246-
247-
/* now iterate for children for a match */
248-
np = scan_OF_for_pci_dev(parent, bus->self->devfn);
249-
of_node_put(parent);
250-
251-
return np;
252-
}
253-
254-
/*
255-
* Scans the OF tree for a device node matching a PCI device
256-
*/
257-
struct device_node *
258-
pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
259-
{
260-
struct device_node *parent, *np;
261-
262-
pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
263-
parent = scan_OF_for_pci_bus(bus);
264-
if (parent == NULL)
265-
return NULL;
266-
pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
267-
np = scan_OF_for_pci_dev(parent, devfn);
268-
of_node_put(parent);
269-
pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
270-
271-
/* XXX most callers don't release the returned node
272-
* mostly because ppc64 doesn't increase the refcount,
273-
* we need to fix that.
274-
*/
275-
return np;
276-
}
277-
EXPORT_SYMBOL(pci_busdev_to_OF_node);
278-
279-
struct device_node*
280-
pci_device_to_OF_node(struct pci_dev *dev)
281-
{
282-
return pci_busdev_to_OF_node(dev->bus, dev->devfn);
283-
}
284-
EXPORT_SYMBOL(pci_device_to_OF_node);
285-
286-
static int
287-
find_OF_pci_device_filter(struct device_node* node, void* data)
288-
{
289-
return ((void *)node == data);
290-
}
291170

292171
/*
293172
* Returns the PCI device matching a given OF node
294173
*/
295-
int
296-
pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
174+
int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
297175
{
298-
const unsigned int *reg;
299-
struct pci_controller* hose;
300-
struct pci_dev* dev = NULL;
301-
302-
/* Make sure it's really a PCI device */
303-
hose = pci_find_hose_for_OF_device(node);
304-
if (!hose || !hose->dn)
305-
return -ENODEV;
306-
if (!scan_OF_pci_childs(hose->dn,
307-
find_OF_pci_device_filter, (void *)node))
176+
struct pci_dev *dev = NULL;
177+
const __be32 *reg;
178+
int size;
179+
180+
/* Check if it might have a chance to be a PCI device */
181+
if (!pci_find_hose_for_OF_device(node))
308182
return -ENODEV;
309-
reg = of_get_property(node, "reg", NULL);
310-
if (!reg)
183+
184+
reg = of_get_property(node, "reg", &size);
185+
if (!reg || size < 5 * sizeof(u32))
311186
return -ENODEV;
312-
*bus = (reg[0] >> 16) & 0xff;
313-
*devfn = ((reg[0] >> 8) & 0xff);
187+
188+
*bus = (be32_to_cpup(&reg[0]) >> 16) & 0xff;
189+
*devfn = (be32_to_cpup(&reg[0]) >> 8) & 0xff;
314190

315191
/* Ok, here we need some tweak. If we have already renumbered
316192
* all busses, we can't rely on the OF bus number any more.

0 commit comments

Comments
 (0)