blob: 894b534669d000d810914c2408b4b700d9c2e876 [file] [log] [blame]
Stephen Rothwellc81014f2006-05-19 17:00:04 +10001/*
2 * Copyright (c) 2005-2006 Michael Ellerman, IBM Corporation
3 *
4 * Description:
5 * This file contains all the routines to build a flattened device
6 * tree for a legacy iSeries machine.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#undef DEBUG
15
16#include <linux/types.h>
17#include <linux/init.h>
18#include <linux/pci.h>
19#include <linux/pci_regs.h>
20#include <linux/pci_ids.h>
21#include <linux/threads.h>
22#include <linux/bitops.h>
23#include <linux/string.h>
24#include <linux/kernel.h>
25#include <linux/if_ether.h> /* ETH_ALEN */
26
27#include <asm/machdep.h>
28#include <asm/prom.h>
29#include <asm/lppaca.h>
Stephen Rothwellc81014f2006-05-19 17:00:04 +100030#include <asm/cputable.h>
31#include <asm/abs_addr.h>
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100032#include <asm/system.h>
Stephen Rothwellc81014f2006-05-19 17:00:04 +100033#include <asm/iseries/hv_types.h>
34#include <asm/iseries/hv_lp_config.h>
35#include <asm/iseries/hv_call_xm.h>
36#include <asm/iseries/it_exp_vpd_panel.h>
37#include <asm/udbg.h>
38
39#include "processor_vpd.h"
40#include "call_hpt.h"
41#include "call_pci.h"
42#include "pci.h"
43
44#ifdef DEBUG
45#define DBG(fmt...) udbg_printf(fmt)
46#else
47#define DBG(fmt...)
48#endif
49
Stephen Rothwell7499bf12006-05-19 17:06:38 +100050/*
51 * These are created by the linker script at the start and end
52 * of the section containing all the strings from this file.
53 */
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100054extern char __dt_strings_start[];
55extern char __dt_strings_end[];
56
Stephen Rothwellc81014f2006-05-19 17:00:04 +100057struct iseries_flat_dt {
58 struct boot_param_header header;
59 u64 reserve_map[2];
Stephen Rothwellc81014f2006-05-19 17:00:04 +100060};
61
Stephen Rothwell7499bf12006-05-19 17:06:38 +100062static void * __initdata dt_data;
63
64/*
65 * Putting these strings here keeps them out of the section
66 * that we rename to .dt_strings using objcopy and capture
67 * for the strings blob of the flattened device tree.
68 */
69static char __initdata device_type_cpu[] = "cpu";
70static char __initdata device_type_memory[] = "memory";
71static char __initdata device_type_serial[] = "serial";
72static char __initdata device_type_network[] = "network";
73static char __initdata device_type_block[] = "block";
74static char __initdata device_type_byte[] = "byte";
75static char __initdata device_type_pci[] = "pci";
76static char __initdata device_type_vdevice[] = "vdevice";
77static char __initdata device_type_vscsi[] = "vscsi";
Stephen Rothwellc81014f2006-05-19 17:00:04 +100078
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100079static struct iseries_flat_dt * __init dt_init(void)
Stephen Rothwellc81014f2006-05-19 17:00:04 +100080{
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100081 struct iseries_flat_dt *dt;
82 unsigned long str_len;
83
84 str_len = __dt_strings_end - __dt_strings_start;
85 dt = (struct iseries_flat_dt *)ALIGN(klimit, 8);
Stephen Rothwellc81014f2006-05-19 17:00:04 +100086 dt->header.off_mem_rsvmap =
87 offsetof(struct iseries_flat_dt, reserve_map);
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100088 dt->header.off_dt_strings = ALIGN(sizeof(*dt), 8);
89 dt->header.off_dt_struct = dt->header.off_dt_strings
90 + ALIGN(str_len, 8);
Stephen Rothwell7499bf12006-05-19 17:06:38 +100091 dt_data = (void *)((unsigned long)dt + dt->header.off_dt_struct);
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100092 dt->header.dt_strings_size = str_len;
Stephen Rothwellc81014f2006-05-19 17:00:04 +100093
94 /* There is no notion of hardware cpu id on iSeries */
95 dt->header.boot_cpuid_phys = smp_processor_id();
96
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100097 memcpy((char *)dt + dt->header.off_dt_strings, __dt_strings_start,
98 str_len);
Stephen Rothwellc81014f2006-05-19 17:00:04 +100099
100 dt->header.magic = OF_DT_HEADER;
101 dt->header.version = 0x10;
102 dt->header.last_comp_version = 0x10;
103
104 dt->reserve_map[0] = 0;
105 dt->reserve_map[1] = 0;
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000106
107 return dt;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000108}
109
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000110static void __init dt_push_u32(struct iseries_flat_dt *dt, u32 value)
111{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000112 *((u32 *)dt_data) = value;
113 dt_data += sizeof(u32);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000114}
115
116#ifdef notyet
117static void __init dt_push_u64(struct iseries_flat_dt *dt, u64 value)
118{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000119 *((u64 *)dt_data) = value;
120 dt_data += sizeof(u64);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000121}
122#endif
123
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000124static void __init dt_push_bytes(struct iseries_flat_dt *dt, const char *data,
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000125 int len)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000126{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000127 memcpy(dt_data, data, len);
128 dt_data += ALIGN(len, 4);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000129}
130
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000131static void __init dt_start_node(struct iseries_flat_dt *dt, const char *name)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000132{
133 dt_push_u32(dt, OF_DT_BEGIN_NODE);
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000134 dt_push_bytes(dt, name, strlen(name) + 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000135}
136
137#define dt_end_node(dt) dt_push_u32(dt, OF_DT_END_NODE)
138
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000139static void __init dt_prop(struct iseries_flat_dt *dt, const char *name,
140 const void *data, int len)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000141{
142 unsigned long offset;
143
144 dt_push_u32(dt, OF_DT_PROP);
145
146 /* Length of the data */
147 dt_push_u32(dt, len);
148
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000149 offset = name - __dt_strings_start;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000150
151 /* The offset of the properties name in the string blob. */
152 dt_push_u32(dt, (u32)offset);
153
154 /* The actual data. */
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000155 dt_push_bytes(dt, data, len);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000156}
157
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000158static void __init dt_prop_str(struct iseries_flat_dt *dt, const char *name,
159 const char *data)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000160{
161 dt_prop(dt, name, data, strlen(data) + 1); /* + 1 for NULL */
162}
163
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000164static void __init dt_prop_u32(struct iseries_flat_dt *dt, const char *name,
165 u32 data)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000166{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000167 dt_prop(dt, name, &data, sizeof(u32));
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000168}
169
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000170#ifdef notyet
171static void __init dt_prop_u64(struct iseries_flat_dt *dt, const char *name,
172 u64 data)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000173{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000174 dt_prop(dt, name, &data, sizeof(u64));
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000175}
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000176#endif
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000177
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000178static void __init dt_prop_u64_list(struct iseries_flat_dt *dt,
179 const char *name, u64 *data, int n)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000180{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000181 dt_prop(dt, name, data, sizeof(u64) * n);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000182}
183
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000184static void __init dt_prop_u32_list(struct iseries_flat_dt *dt,
185 const char *name, u32 *data, int n)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000186{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000187 dt_prop(dt, name, data, sizeof(u32) * n);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000188}
189
190#ifdef notyet
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000191static void __init dt_prop_empty(struct iseries_flat_dt *dt, const char *name)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000192{
193 dt_prop(dt, name, NULL, 0);
194}
195#endif
196
197static void __init dt_cpus(struct iseries_flat_dt *dt)
198{
199 unsigned char buf[32];
200 unsigned char *p;
201 unsigned int i, index;
202 struct IoHriProcessorVpd *d;
203 u32 pft_size[2];
204
205 /* yuck */
206 snprintf(buf, 32, "PowerPC,%s", cur_cpu_spec->cpu_name);
207 p = strchr(buf, ' ');
208 if (!p) p = buf + strlen(buf);
209
210 dt_start_node(dt, "cpus");
211 dt_prop_u32(dt, "#address-cells", 1);
212 dt_prop_u32(dt, "#size-cells", 0);
213
214 pft_size[0] = 0; /* NUMA CEC cookie, 0 for non NUMA */
215 pft_size[1] = __ilog2(HvCallHpt_getHptPages() * HW_PAGE_SIZE);
216
217 for (i = 0; i < NR_CPUS; i++) {
218 if (lppaca[i].dyn_proc_status >= 2)
219 continue;
220
221 snprintf(p, 32 - (p - buf), "@%d", i);
222 dt_start_node(dt, buf);
223
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000224 dt_prop_str(dt, "device_type", device_type_cpu);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000225
226 index = lppaca[i].dyn_hv_phys_proc_index;
227 d = &xIoHriProcessorVpd[index];
228
229 dt_prop_u32(dt, "i-cache-size", d->xInstCacheSize * 1024);
230 dt_prop_u32(dt, "i-cache-line-size", d->xInstCacheOperandSize);
231
232 dt_prop_u32(dt, "d-cache-size", d->xDataL1CacheSizeKB * 1024);
233 dt_prop_u32(dt, "d-cache-line-size", d->xDataCacheOperandSize);
234
235 /* magic conversions to Hz copied from old code */
236 dt_prop_u32(dt, "clock-frequency",
237 ((1UL << 34) * 1000000) / d->xProcFreq);
238 dt_prop_u32(dt, "timebase-frequency",
239 ((1UL << 32) * 1000000) / d->xTimeBaseFreq);
240
241 dt_prop_u32(dt, "reg", i);
242
243 dt_prop_u32_list(dt, "ibm,pft-size", pft_size, 2);
244
245 dt_end_node(dt);
246 }
247
248 dt_end_node(dt);
249}
250
251static void __init dt_model(struct iseries_flat_dt *dt)
252{
253 char buf[16] = "IBM,";
254
Stephen Rothwell16e9f992006-06-29 15:07:42 +1000255 /* N.B. lparcfg.c knows about the "IBM," prefixes ... */
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000256 /* "IBM," + mfgId[2:3] + systemSerial[1:5] */
257 strne2a(buf + 4, xItExtVpdPanel.mfgID + 2, 2);
258 strne2a(buf + 6, xItExtVpdPanel.systemSerial + 1, 5);
259 buf[11] = '\0';
260 dt_prop_str(dt, "system-id", buf);
261
262 /* "IBM," + machineType[0:4] */
263 strne2a(buf + 4, xItExtVpdPanel.machineType, 4);
264 buf[8] = '\0';
265 dt_prop_str(dt, "model", buf);
266
267 dt_prop_str(dt, "compatible", "IBM,iSeries");
Stephen Rothwell16e9f992006-06-29 15:07:42 +1000268 dt_prop_u32(dt, "ibm,partition-no", HvLpConfig_getLpIndex());
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000269}
270
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000271static void __init dt_do_vdevice(struct iseries_flat_dt *dt,
272 const char *name, u32 reg, int unit,
273 const char *type, const char *compat, int end)
274{
275 char buf[32];
276
277 snprintf(buf, 32, "%s@%08x", name, reg + ((unit >= 0) ? unit : 0));
278 dt_start_node(dt, buf);
279 dt_prop_str(dt, "device_type", type);
280 if (compat)
281 dt_prop_str(dt, "compatible", compat);
282 dt_prop_u32(dt, "reg", reg + ((unit >= 0) ? unit : 0));
283 if (unit >= 0)
284 dt_prop_u32(dt, "linux,unit_address", unit);
285 if (end)
286 dt_end_node(dt);
287}
288
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000289static void __init dt_vdevices(struct iseries_flat_dt *dt)
290{
291 u32 reg = 0;
292 HvLpIndexMap vlan_map;
293 int i;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000294
295 dt_start_node(dt, "vdevice");
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000296 dt_prop_str(dt, "device_type", device_type_vdevice);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000297 dt_prop_str(dt, "compatible", "IBM,iSeries-vdevice");
298 dt_prop_u32(dt, "#address-cells", 1);
299 dt_prop_u32(dt, "#size-cells", 0);
300
Stephen Rothwell8bff05b2006-07-13 18:51:22 +1000301 dt_do_vdevice(dt, "vty", reg, -1, device_type_serial,
302 "IBM,iSeries-vty", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000303 reg++;
304
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000305 dt_do_vdevice(dt, "v-scsi", reg, -1, device_type_vscsi,
306 "IBM,v-scsi", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000307 reg++;
308
309 vlan_map = HvLpConfig_getVirtualLanIndexMap();
310 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
311 unsigned char mac_addr[ETH_ALEN];
312
313 if ((vlan_map & (0x8000 >> i)) == 0)
314 continue;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000315 dt_do_vdevice(dt, "l-lan", reg, i, device_type_network,
316 "IBM,iSeries-l-lan", 0);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000317 mac_addr[0] = 0x02;
318 mac_addr[1] = 0x01;
319 mac_addr[2] = 0xff;
320 mac_addr[3] = i;
321 mac_addr[4] = 0xff;
322 mac_addr[5] = HvLpConfig_getLpIndex_outline();
323 dt_prop(dt, "local-mac-address", (char *)mac_addr, ETH_ALEN);
324 dt_prop(dt, "mac-address", (char *)mac_addr, ETH_ALEN);
325 dt_prop_u32(dt, "max-frame-size", 9000);
326 dt_prop_u32(dt, "address-bits", 48);
327
328 dt_end_node(dt);
329 }
330 reg += HVMAXARCHITECTEDVIRTUALLANS;
331
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000332 for (i = 0; i < HVMAXARCHITECTEDVIRTUALDISKS; i++)
333 dt_do_vdevice(dt, "viodasd", reg, i, device_type_block,
334 "IBM,iSeries-viodasd", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000335 reg += HVMAXARCHITECTEDVIRTUALDISKS;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000336
337 for (i = 0; i < HVMAXARCHITECTEDVIRTUALCDROMS; i++)
338 dt_do_vdevice(dt, "viocd", reg, i, device_type_block,
339 "IBM,iSeries-viocd", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000340 reg += HVMAXARCHITECTEDVIRTUALCDROMS;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000341
342 for (i = 0; i < HVMAXARCHITECTEDVIRTUALTAPES; i++)
343 dt_do_vdevice(dt, "viotape", reg, i, device_type_byte,
344 "IBM,iSeries-viotape", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000345
346 dt_end_node(dt);
347}
348
349struct pci_class_name {
350 u16 code;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000351 const char *name;
352 const char *type;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000353};
354
355static struct pci_class_name __initdata pci_class_name[] = {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000356 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", device_type_network },
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000357};
358
359static struct pci_class_name * __init dt_find_pci_class_name(u16 class_code)
360{
361 struct pci_class_name *cp;
362
363 for (cp = pci_class_name;
364 cp < &pci_class_name[ARRAY_SIZE(pci_class_name)]; cp++)
365 if (cp->code == class_code)
366 return cp;
367 return NULL;
368}
369
370/*
371 * This assumes that the node slot is always on the primary bus!
372 */
373static void __init scan_bridge_slot(struct iseries_flat_dt *dt,
374 HvBusNumber bus, struct HvCallPci_BridgeInfo *bridge_info)
375{
376 HvSubBusNumber sub_bus = bridge_info->subBusNumber;
377 u16 vendor_id;
378 u16 device_id;
379 u32 class_id;
380 int err;
381 char buf[32];
382 u32 reg[5];
383 int id_sel = ISERIES_GET_DEVICE_FROM_SUBBUS(sub_bus);
384 int function = ISERIES_GET_FUNCTION_FROM_SUBBUS(sub_bus);
385 HvAgentId eads_id_sel = ISERIES_PCI_AGENTID(id_sel, function);
386 u8 devfn;
387 struct pci_class_name *cp;
388
389 /*
390 * Connect all functions of any device found.
391 */
392 for (id_sel = 1; id_sel <= bridge_info->maxAgents; id_sel++) {
393 for (function = 0; function < 8; function++) {
394 HvAgentId agent_id = ISERIES_PCI_AGENTID(id_sel,
395 function);
396 err = HvCallXm_connectBusUnit(bus, sub_bus,
397 agent_id, 0);
398 if (err) {
399 if (err != 0x302)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000400 DBG("connectBusUnit(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000401 bus, sub_bus, agent_id, err);
402 continue;
403 }
404
405 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
406 PCI_VENDOR_ID, &vendor_id);
407 if (err) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000408 DBG("ReadVendor(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000409 bus, sub_bus, agent_id, err);
410 continue;
411 }
412 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
413 PCI_DEVICE_ID, &device_id);
414 if (err) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000415 DBG("ReadDevice(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000416 bus, sub_bus, agent_id, err);
417 continue;
418 }
419 err = HvCallPci_configLoad32(bus, sub_bus, agent_id,
420 PCI_CLASS_REVISION , &class_id);
421 if (err) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000422 DBG("ReadClass(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000423 bus, sub_bus, agent_id, err);
424 continue;
425 }
426
427 devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(eads_id_sel),
428 function);
429 cp = dt_find_pci_class_name(class_id >> 16);
430 if (cp && cp->name)
431 strncpy(buf, cp->name, sizeof(buf) - 1);
432 else
433 snprintf(buf, sizeof(buf), "pci%x,%x",
434 vendor_id, device_id);
435 buf[sizeof(buf) - 1] = '\0';
436 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
437 "@%x", PCI_SLOT(devfn));
438 buf[sizeof(buf) - 1] = '\0';
439 if (function != 0)
440 snprintf(buf + strlen(buf),
441 sizeof(buf) - strlen(buf),
442 ",%x", function);
443 dt_start_node(dt, buf);
444 reg[0] = (bus << 16) | (devfn << 8);
445 reg[1] = 0;
446 reg[2] = 0;
447 reg[3] = 0;
448 reg[4] = 0;
449 dt_prop_u32_list(dt, "reg", reg, 5);
450 if (cp && (cp->type || cp->name))
451 dt_prop_str(dt, "device_type",
452 cp->type ? cp->type : cp->name);
453 dt_prop_u32(dt, "vendor-id", vendor_id);
454 dt_prop_u32(dt, "device-id", device_id);
455 dt_prop_u32(dt, "class-code", class_id >> 8);
456 dt_prop_u32(dt, "revision-id", class_id & 0xff);
457 dt_prop_u32(dt, "linux,subbus", sub_bus);
458 dt_prop_u32(dt, "linux,agent-id", agent_id);
459 dt_prop_u32(dt, "linux,logical-slot-number",
460 bridge_info->logicalSlotNumber);
461 dt_end_node(dt);
462
463 }
464 }
465}
466
467static void __init scan_bridge(struct iseries_flat_dt *dt, HvBusNumber bus,
468 HvSubBusNumber sub_bus, int id_sel)
469{
470 struct HvCallPci_BridgeInfo bridge_info;
471 HvAgentId agent_id;
472 int function;
473 int ret;
474
475 /* Note: hvSubBus and irq is always be 0 at this level! */
476 for (function = 0; function < 8; ++function) {
477 agent_id = ISERIES_PCI_AGENTID(id_sel, function);
478 ret = HvCallXm_connectBusUnit(bus, sub_bus, agent_id, 0);
479 if (ret != 0) {
480 if (ret != 0xb)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000481 DBG("connectBusUnit(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000482 bus, sub_bus, agent_id, ret);
483 continue;
484 }
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000485 DBG("found device at bus %d idsel %d func %d (AgentId %x)\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000486 bus, id_sel, function, agent_id);
487 ret = HvCallPci_getBusUnitInfo(bus, sub_bus, agent_id,
488 iseries_hv_addr(&bridge_info),
489 sizeof(struct HvCallPci_BridgeInfo));
490 if (ret != 0)
491 continue;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000492 DBG("bridge info: type %x subbus %x "
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000493 "maxAgents %x maxsubbus %x logslot %x\n",
494 bridge_info.busUnitInfo.deviceType,
495 bridge_info.subBusNumber,
496 bridge_info.maxAgents,
497 bridge_info.maxSubBusNumber,
498 bridge_info.logicalSlotNumber);
499 if (bridge_info.busUnitInfo.deviceType ==
500 HvCallPci_BridgeDevice)
501 scan_bridge_slot(dt, bus, &bridge_info);
502 else
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000503 DBG("PCI: Invalid Bridge Configuration(0x%02X)",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000504 bridge_info.busUnitInfo.deviceType);
505 }
506}
507
508static void __init scan_phb(struct iseries_flat_dt *dt, HvBusNumber bus)
509{
510 struct HvCallPci_DeviceInfo dev_info;
511 const HvSubBusNumber sub_bus = 0; /* EADs is always 0. */
512 int err;
513 int id_sel;
514 const int max_agents = 8;
515
516 /*
517 * Probe for EADs Bridges
518 */
519 for (id_sel = 1; id_sel < max_agents; ++id_sel) {
520 err = HvCallPci_getDeviceInfo(bus, sub_bus, id_sel,
521 iseries_hv_addr(&dev_info),
522 sizeof(struct HvCallPci_DeviceInfo));
523 if (err) {
524 if (err != 0x302)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000525 DBG("getDeviceInfo(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000526 bus, sub_bus, id_sel, err);
527 continue;
528 }
529 if (dev_info.deviceType != HvCallPci_NodeDevice) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000530 DBG("PCI: Invalid System Configuration"
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000531 "(0x%02X) for bus 0x%02x id 0x%02x.\n",
532 dev_info.deviceType, bus, id_sel);
533 continue;
534 }
535 scan_bridge(dt, bus, sub_bus, id_sel);
536 }
537}
538
539static void __init dt_pci_devices(struct iseries_flat_dt *dt)
540{
541 HvBusNumber bus;
542 char buf[32];
543 u32 buses[2];
544 int phb_num = 0;
545
546 /* Check all possible buses. */
547 for (bus = 0; bus < 256; bus++) {
548 int err = HvCallXm_testBus(bus);
549
550 if (err) {
551 /*
552 * Check for Unexpected Return code, a clue that
553 * something has gone wrong.
554 */
555 if (err != 0x0301)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000556 DBG("Unexpected Return on Probe(0x%02X) "
557 "0x%04X\n", bus, err);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000558 continue;
559 }
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000560 DBG("bus %d appears to exist\n", bus);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000561 snprintf(buf, 32, "pci@%d", phb_num);
562 dt_start_node(dt, buf);
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000563 dt_prop_str(dt, "device_type", device_type_pci);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000564 dt_prop_str(dt, "compatible", "IBM,iSeries-Logical-PHB");
565 dt_prop_u32(dt, "#address-cells", 3);
566 dt_prop_u32(dt, "#size-cells", 2);
567 buses[0] = buses[1] = bus;
568 dt_prop_u32_list(dt, "bus-range", buses, 2);
569 scan_phb(dt, bus);
570 dt_end_node(dt);
571 phb_num++;
572 }
573}
574
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000575static void dt_finish(struct iseries_flat_dt *dt)
576{
577 dt_push_u32(dt, OF_DT_END);
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000578 dt->header.totalsize = (unsigned long)dt_data - (unsigned long)dt;
579 klimit = ALIGN((unsigned long)dt_data, 8);
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000580}
581
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000582void * __init build_flat_dt(unsigned long phys_mem_size)
583{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000584 struct iseries_flat_dt *iseries_dt;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000585 u64 tmp[2];
586
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000587 iseries_dt = dt_init();
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000588
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000589 dt_start_node(iseries_dt, "");
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000590
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000591 dt_prop_u32(iseries_dt, "#address-cells", 2);
592 dt_prop_u32(iseries_dt, "#size-cells", 2);
593 dt_model(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000594
595 /* /memory */
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000596 dt_start_node(iseries_dt, "memory@0");
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000597 dt_prop_str(iseries_dt, "device_type", device_type_memory);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000598 tmp[0] = 0;
599 tmp[1] = phys_mem_size;
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000600 dt_prop_u64_list(iseries_dt, "reg", tmp, 2);
601 dt_end_node(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000602
603 /* /chosen */
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000604 dt_start_node(iseries_dt, "chosen");
605 dt_prop_str(iseries_dt, "bootargs", cmd_line);
606 dt_end_node(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000607
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000608 dt_cpus(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000609
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000610 dt_vdevices(iseries_dt);
611 dt_pci_devices(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000612
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000613 dt_end_node(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000614
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000615 dt_finish(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000616
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000617 return iseries_dt;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000618}