1/*
2 * pci_dn.c
3 *
4 * Copyright (C) 2001 Todd Inglett, IBM Corporation
5 *
6 * PCI manipulation via device_nodes.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 */
22#include <linux/kernel.h>
23#include <linux/pci.h>
24#include <linux/string.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/bootmem.h>
28
29#include <asm/io.h>
30#include <asm/prom.h>
31#include <asm/pci-bridge.h>
32#include <asm/pSeries_reconfig.h>
33#include <asm/ppc-pci.h>
34#include <asm/firmware.h>
35
36/*
37 * Traverse_func that inits the PCI fields of the device node.
38 * NOTE: this *must* be done before read/write config to the device.
39 */
40static void * __devinit update_dn_pci_info(struct device_node *dn, void *data)
41{
42	struct pci_controller *phb = data;
43	const int *type =
44		of_get_property(dn, "ibm,pci-config-space-type", NULL);
45	const u32 *regs;
46	struct pci_dn *pdn;
47
48	if (mem_init_done)
49		pdn = kmalloc(sizeof(*pdn), GFP_KERNEL);
50	else
51		pdn = alloc_bootmem(sizeof(*pdn));
52	if (pdn == NULL)
53		return NULL;
54	memset(pdn, 0, sizeof(*pdn));
55	dn->data = pdn;
56	pdn->node = dn;
57	pdn->phb = phb;
58	regs = of_get_property(dn, "reg", NULL);
59	if (regs) {
60		/* First register entry is addr (00BBSS00)  */
61		pdn->busno = (regs[0] >> 16) & 0xff;
62		pdn->devfn = (regs[0] >> 8) & 0xff;
63	}
64	if (firmware_has_feature(FW_FEATURE_ISERIES)) {
65		const u32 *busp = of_get_property(dn, "linux,subbus", NULL);
66		if (busp)
67			pdn->bussubno = *busp;
68	}
69
70	pdn->pci_ext_config_space = (type && *type == 1);
71	return NULL;
72}
73
74/*
75 * Traverse a device tree stopping each PCI device in the tree.
76 * This is done depth first.  As each node is processed, a "pre"
77 * function is called and the children are processed recursively.
78 *
79 * The "pre" func returns a value.  If non-zero is returned from
80 * the "pre" func, the traversal stops and this value is returned.
81 * This return value is useful when using traverse as a method of
82 * finding a device.
83 *
84 * NOTE: we do not run the func for devices that do not appear to
85 * be PCI except for the start node which we assume (this is good
86 * because the start node is often a phb which may be missing PCI
87 * properties).
88 * We use the class-code as an indicator. If we run into
89 * one of these nodes we also assume its siblings are non-pci for
90 * performance.
91 */
92void *traverse_pci_devices(struct device_node *start, traverse_func pre,
93		void *data)
94{
95	struct device_node *dn, *nextdn;
96	void *ret;
97
98	/* We started with a phb, iterate all childs */
99	for (dn = start->child; dn; dn = nextdn) {
100		const u32 *classp;
101		u32 class;
102
103		nextdn = NULL;
104		classp = of_get_property(dn, "class-code", NULL);
105		class = classp ? *classp : 0;
106
107		if (pre && ((ret = pre(dn, data)) != NULL))
108			return ret;
109
110		/* If we are a PCI bridge, go down */
111		if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
112				  (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
113			/* Depth first...do children */
114			nextdn = dn->child;
115		else if (dn->sibling)
116			/* ok, try next sibling instead. */
117			nextdn = dn->sibling;
118		if (!nextdn) {
119			/* Walk up to next valid sibling. */
120			do {
121				dn = dn->parent;
122				if (dn == start)
123					return NULL;
124			} while (dn->sibling == NULL);
125			nextdn = dn->sibling;
126		}
127	}
128	return NULL;
129}
130
131/**
132 * pci_devs_phb_init_dynamic - setup pci devices under this PHB
133 * phb: pci-to-host bridge (top-level bridge connecting to cpu)
134 *
135 * This routine is called both during boot, (before the memory
136 * subsystem is set up, before kmalloc is valid) and during the
137 * dynamic lpar operation of adding a PHB to a running system.
138 */
139void __devinit pci_devs_phb_init_dynamic(struct pci_controller *phb)
140{
141	struct device_node * dn = (struct device_node *) phb->arch_data;
142	struct pci_dn *pdn;
143
144	/* PHB nodes themselves must not match */
145	update_dn_pci_info(dn, phb);
146	pdn = dn->data;
147	if (pdn) {
148		pdn->devfn = pdn->busno = -1;
149		pdn->phb = phb;
150	}
151
152	/* Update dn->phb ptrs for new phb and children devices */
153	traverse_pci_devices(dn, update_dn_pci_info, phb);
154}
155
156/*
157 * Traversal func that looks for a <busno,devfcn> value.
158 * If found, the pci_dn is returned (thus terminating the traversal).
159 */
160static void *is_devfn_node(struct device_node *dn, void *data)
161{
162	int busno = ((unsigned long)data >> 8) & 0xff;
163	int devfn = ((unsigned long)data) & 0xff;
164	struct pci_dn *pci = dn->data;
165
166	if (pci && (devfn == pci->devfn) && (busno == pci->busno))
167		return dn;
168	return NULL;
169}
170
171/*
172 * This is the "slow" path for looking up a device_node from a
173 * pci_dev.  It will hunt for the device under its parent's
174 * phb and then update sysdata for a future fastpath.
175 *
176 * It may also do fixups on the actual device since this happens
177 * on the first read/write.
178 *
179 * Note that it also must deal with devices that don't exist.
180 * In this case it may probe for real hardware ("just in case")
181 * and add a device_node to the device tree if necessary.
182 *
183 */
184struct device_node *fetch_dev_dn(struct pci_dev *dev)
185{
186	struct device_node *orig_dn = dev->sysdata;
187	struct device_node *dn;
188	unsigned long searchval = (dev->bus->number << 8) | dev->devfn;
189
190	dn = traverse_pci_devices(orig_dn, is_devfn_node, (void *)searchval);
191	if (dn)
192		dev->sysdata = dn;
193	return dn;
194}
195EXPORT_SYMBOL(fetch_dev_dn);
196
197static int pci_dn_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
198{
199	struct device_node *np = node;
200	struct pci_dn *pci = NULL;
201	int err = NOTIFY_OK;
202
203	switch (action) {
204	case PSERIES_RECONFIG_ADD:
205		pci = np->parent->data;
206		if (pci)
207			update_dn_pci_info(np, pci->phb);
208		break;
209	default:
210		err = NOTIFY_DONE;
211		break;
212	}
213	return err;
214}
215
216static struct notifier_block pci_dn_reconfig_nb = {
217	.notifier_call = pci_dn_reconfig_notifier,
218};
219
220/**
221 * pci_devs_phb_init - Initialize phbs and pci devs under them.
222 *
223 * This routine walks over all phb's (pci-host bridges) on the
224 * system, and sets up assorted pci-related structures
225 * (including pci info in the device node structs) for each
226 * pci device found underneath.  This routine runs once,
227 * early in the boot sequence.
228 */
229void __init pci_devs_phb_init(void)
230{
231	struct pci_controller *phb, *tmp;
232
233	/* This must be done first so the device nodes have valid pci info! */
234	list_for_each_entry_safe(phb, tmp, &hose_list, list_node)
235		pci_devs_phb_init_dynamic(phb);
236
237	pSeries_reconfig_notifier_register(&pci_dn_reconfig_nb);
238}
239