blob: 39c676ab9d6dfea96ec9f4c1056caacfa327d375 [file] [log] [blame]
Stephen Rothwellc81014f2006-05-19 17:00:04 +10001/*
Michael Ellermandac411e2006-07-13 17:52:04 +10002 * Copyright (C) 2005-2006 Michael Ellerman, IBM Corporation
3 * Copyright (C) 2000-2004, IBM Corporation
Stephen Rothwellc81014f2006-05-19 17:00:04 +10004 *
5 * Description:
6 * This file contains all the routines to build a flattened device
7 * tree for a legacy iSeries machine.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#undef DEBUG
16
17#include <linux/types.h>
18#include <linux/init.h>
19#include <linux/pci.h>
20#include <linux/pci_regs.h>
21#include <linux/pci_ids.h>
22#include <linux/threads.h>
23#include <linux/bitops.h>
24#include <linux/string.h>
25#include <linux/kernel.h>
26#include <linux/if_ether.h> /* ETH_ALEN */
27
28#include <asm/machdep.h>
29#include <asm/prom.h>
30#include <asm/lppaca.h>
Stephen Rothwellc81014f2006-05-19 17:00:04 +100031#include <asm/cputable.h>
32#include <asm/abs_addr.h>
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100033#include <asm/system.h>
Stephen Rothwellc81014f2006-05-19 17:00:04 +100034#include <asm/iseries/hv_types.h>
35#include <asm/iseries/hv_lp_config.h>
36#include <asm/iseries/hv_call_xm.h>
37#include <asm/iseries/it_exp_vpd_panel.h>
38#include <asm/udbg.h>
39
40#include "processor_vpd.h"
41#include "call_hpt.h"
42#include "call_pci.h"
43#include "pci.h"
44
45#ifdef DEBUG
46#define DBG(fmt...) udbg_printf(fmt)
47#else
48#define DBG(fmt...)
49#endif
50
Stephen Rothwell7499bf12006-05-19 17:06:38 +100051/*
52 * These are created by the linker script at the start and end
53 * of the section containing all the strings from this file.
54 */
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +100055extern char __dt_strings_start[];
56extern char __dt_strings_end[];
57
Stephen Rothwellc81014f2006-05-19 17:00:04 +100058struct iseries_flat_dt {
59 struct boot_param_header header;
60 u64 reserve_map[2];
Stephen Rothwellc81014f2006-05-19 17:00:04 +100061};
62
Stephen Rothwell7499bf12006-05-19 17:06:38 +100063static void * __initdata dt_data;
64
65/*
66 * Putting these strings here keeps them out of the section
67 * that we rename to .dt_strings using objcopy and capture
68 * for the strings blob of the flattened device tree.
69 */
70static char __initdata device_type_cpu[] = "cpu";
71static char __initdata device_type_memory[] = "memory";
72static char __initdata device_type_serial[] = "serial";
73static char __initdata device_type_network[] = "network";
74static char __initdata device_type_block[] = "block";
75static char __initdata device_type_byte[] = "byte";
76static char __initdata device_type_pci[] = "pci";
77static char __initdata device_type_vdevice[] = "vdevice";
78static char __initdata device_type_vscsi[] = "vscsi";
Stephen Rothwellc81014f2006-05-19 17:00:04 +100079
Michael Ellermandac411e2006-07-13 17:52:04 +100080
81/* EBCDIC to ASCII conversion routines */
82
83unsigned char e2a(unsigned char x)
84{
85 switch (x) {
86 case 0xF0:
87 return '0';
88 case 0xF1:
89 return '1';
90 case 0xF2:
91 return '2';
92 case 0xF3:
93 return '3';
94 case 0xF4:
95 return '4';
96 case 0xF5:
97 return '5';
98 case 0xF6:
99 return '6';
100 case 0xF7:
101 return '7';
102 case 0xF8:
103 return '8';
104 case 0xF9:
105 return '9';
106 case 0xC1:
107 return 'A';
108 case 0xC2:
109 return 'B';
110 case 0xC3:
111 return 'C';
112 case 0xC4:
113 return 'D';
114 case 0xC5:
115 return 'E';
116 case 0xC6:
117 return 'F';
118 case 0xC7:
119 return 'G';
120 case 0xC8:
121 return 'H';
122 case 0xC9:
123 return 'I';
124 case 0xD1:
125 return 'J';
126 case 0xD2:
127 return 'K';
128 case 0xD3:
129 return 'L';
130 case 0xD4:
131 return 'M';
132 case 0xD5:
133 return 'N';
134 case 0xD6:
135 return 'O';
136 case 0xD7:
137 return 'P';
138 case 0xD8:
139 return 'Q';
140 case 0xD9:
141 return 'R';
142 case 0xE2:
143 return 'S';
144 case 0xE3:
145 return 'T';
146 case 0xE4:
147 return 'U';
148 case 0xE5:
149 return 'V';
150 case 0xE6:
151 return 'W';
152 case 0xE7:
153 return 'X';
154 case 0xE8:
155 return 'Y';
156 case 0xE9:
157 return 'Z';
158 }
159 return ' ';
160}
161EXPORT_SYMBOL(e2a);
162
163unsigned char* strne2a(unsigned char *dest, const unsigned char *src, size_t n)
164{
165 int i;
166
167 n = strnlen(src, n);
168
169 for (i = 0; i < n; i++)
170 dest[i] = e2a(src[i]);
171
172 return dest;
173}
174
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000175static struct iseries_flat_dt * __init dt_init(void)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000176{
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000177 struct iseries_flat_dt *dt;
178 unsigned long str_len;
179
180 str_len = __dt_strings_end - __dt_strings_start;
181 dt = (struct iseries_flat_dt *)ALIGN(klimit, 8);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000182 dt->header.off_mem_rsvmap =
183 offsetof(struct iseries_flat_dt, reserve_map);
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000184 dt->header.off_dt_strings = ALIGN(sizeof(*dt), 8);
185 dt->header.off_dt_struct = dt->header.off_dt_strings
186 + ALIGN(str_len, 8);
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000187 dt_data = (void *)((unsigned long)dt + dt->header.off_dt_struct);
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000188 dt->header.dt_strings_size = str_len;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000189
190 /* There is no notion of hardware cpu id on iSeries */
191 dt->header.boot_cpuid_phys = smp_processor_id();
192
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000193 memcpy((char *)dt + dt->header.off_dt_strings, __dt_strings_start,
194 str_len);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000195
196 dt->header.magic = OF_DT_HEADER;
197 dt->header.version = 0x10;
198 dt->header.last_comp_version = 0x10;
199
200 dt->reserve_map[0] = 0;
201 dt->reserve_map[1] = 0;
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000202
203 return dt;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000204}
205
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000206static void __init dt_push_u32(struct iseries_flat_dt *dt, u32 value)
207{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000208 *((u32 *)dt_data) = value;
209 dt_data += sizeof(u32);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000210}
211
212#ifdef notyet
213static void __init dt_push_u64(struct iseries_flat_dt *dt, u64 value)
214{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000215 *((u64 *)dt_data) = value;
216 dt_data += sizeof(u64);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000217}
218#endif
219
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000220static void __init dt_push_bytes(struct iseries_flat_dt *dt, const char *data,
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000221 int len)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000222{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000223 memcpy(dt_data, data, len);
224 dt_data += ALIGN(len, 4);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000225}
226
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000227static void __init dt_start_node(struct iseries_flat_dt *dt, const char *name)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000228{
229 dt_push_u32(dt, OF_DT_BEGIN_NODE);
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000230 dt_push_bytes(dt, name, strlen(name) + 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000231}
232
233#define dt_end_node(dt) dt_push_u32(dt, OF_DT_END_NODE)
234
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000235static void __init dt_prop(struct iseries_flat_dt *dt, const char *name,
236 const void *data, int len)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000237{
238 unsigned long offset;
239
240 dt_push_u32(dt, OF_DT_PROP);
241
242 /* Length of the data */
243 dt_push_u32(dt, len);
244
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000245 offset = name - __dt_strings_start;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000246
247 /* The offset of the properties name in the string blob. */
248 dt_push_u32(dt, (u32)offset);
249
250 /* The actual data. */
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000251 dt_push_bytes(dt, data, len);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000252}
253
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000254static void __init dt_prop_str(struct iseries_flat_dt *dt, const char *name,
255 const char *data)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000256{
257 dt_prop(dt, name, data, strlen(data) + 1); /* + 1 for NULL */
258}
259
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000260static void __init dt_prop_u32(struct iseries_flat_dt *dt, const char *name,
261 u32 data)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000262{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000263 dt_prop(dt, name, &data, sizeof(u32));
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000264}
265
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000266#ifdef notyet
267static void __init dt_prop_u64(struct iseries_flat_dt *dt, const char *name,
268 u64 data)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000269{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000270 dt_prop(dt, name, &data, sizeof(u64));
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000271}
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000272#endif
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000273
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000274static void __init dt_prop_u64_list(struct iseries_flat_dt *dt,
275 const char *name, u64 *data, int n)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000276{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000277 dt_prop(dt, name, data, sizeof(u64) * n);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000278}
279
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000280static void __init dt_prop_u32_list(struct iseries_flat_dt *dt,
281 const char *name, u32 *data, int n)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000282{
Stephen Rothwell72a14ea2006-05-19 17:04:12 +1000283 dt_prop(dt, name, data, sizeof(u32) * n);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000284}
285
286#ifdef notyet
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000287static void __init dt_prop_empty(struct iseries_flat_dt *dt, const char *name)
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000288{
289 dt_prop(dt, name, NULL, 0);
290}
291#endif
292
293static void __init dt_cpus(struct iseries_flat_dt *dt)
294{
295 unsigned char buf[32];
296 unsigned char *p;
297 unsigned int i, index;
298 struct IoHriProcessorVpd *d;
299 u32 pft_size[2];
300
301 /* yuck */
302 snprintf(buf, 32, "PowerPC,%s", cur_cpu_spec->cpu_name);
303 p = strchr(buf, ' ');
304 if (!p) p = buf + strlen(buf);
305
306 dt_start_node(dt, "cpus");
307 dt_prop_u32(dt, "#address-cells", 1);
308 dt_prop_u32(dt, "#size-cells", 0);
309
310 pft_size[0] = 0; /* NUMA CEC cookie, 0 for non NUMA */
311 pft_size[1] = __ilog2(HvCallHpt_getHptPages() * HW_PAGE_SIZE);
312
313 for (i = 0; i < NR_CPUS; i++) {
314 if (lppaca[i].dyn_proc_status >= 2)
315 continue;
316
317 snprintf(p, 32 - (p - buf), "@%d", i);
318 dt_start_node(dt, buf);
319
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000320 dt_prop_str(dt, "device_type", device_type_cpu);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000321
322 index = lppaca[i].dyn_hv_phys_proc_index;
323 d = &xIoHriProcessorVpd[index];
324
325 dt_prop_u32(dt, "i-cache-size", d->xInstCacheSize * 1024);
326 dt_prop_u32(dt, "i-cache-line-size", d->xInstCacheOperandSize);
327
328 dt_prop_u32(dt, "d-cache-size", d->xDataL1CacheSizeKB * 1024);
329 dt_prop_u32(dt, "d-cache-line-size", d->xDataCacheOperandSize);
330
331 /* magic conversions to Hz copied from old code */
332 dt_prop_u32(dt, "clock-frequency",
333 ((1UL << 34) * 1000000) / d->xProcFreq);
334 dt_prop_u32(dt, "timebase-frequency",
335 ((1UL << 32) * 1000000) / d->xTimeBaseFreq);
336
337 dt_prop_u32(dt, "reg", i);
338
339 dt_prop_u32_list(dt, "ibm,pft-size", pft_size, 2);
340
341 dt_end_node(dt);
342 }
343
344 dt_end_node(dt);
345}
346
347static void __init dt_model(struct iseries_flat_dt *dt)
348{
349 char buf[16] = "IBM,";
350
Stephen Rothwell16e9f992006-06-29 15:07:42 +1000351 /* N.B. lparcfg.c knows about the "IBM," prefixes ... */
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000352 /* "IBM," + mfgId[2:3] + systemSerial[1:5] */
353 strne2a(buf + 4, xItExtVpdPanel.mfgID + 2, 2);
354 strne2a(buf + 6, xItExtVpdPanel.systemSerial + 1, 5);
355 buf[11] = '\0';
356 dt_prop_str(dt, "system-id", buf);
357
358 /* "IBM," + machineType[0:4] */
359 strne2a(buf + 4, xItExtVpdPanel.machineType, 4);
360 buf[8] = '\0';
361 dt_prop_str(dt, "model", buf);
362
363 dt_prop_str(dt, "compatible", "IBM,iSeries");
Stephen Rothwell16e9f992006-06-29 15:07:42 +1000364 dt_prop_u32(dt, "ibm,partition-no", HvLpConfig_getLpIndex());
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000365}
366
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000367static void __init dt_do_vdevice(struct iseries_flat_dt *dt,
368 const char *name, u32 reg, int unit,
369 const char *type, const char *compat, int end)
370{
371 char buf[32];
372
373 snprintf(buf, 32, "%s@%08x", name, reg + ((unit >= 0) ? unit : 0));
374 dt_start_node(dt, buf);
375 dt_prop_str(dt, "device_type", type);
376 if (compat)
377 dt_prop_str(dt, "compatible", compat);
378 dt_prop_u32(dt, "reg", reg + ((unit >= 0) ? unit : 0));
379 if (unit >= 0)
380 dt_prop_u32(dt, "linux,unit_address", unit);
381 if (end)
382 dt_end_node(dt);
383}
384
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000385static void __init dt_vdevices(struct iseries_flat_dt *dt)
386{
387 u32 reg = 0;
388 HvLpIndexMap vlan_map;
389 int i;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000390
391 dt_start_node(dt, "vdevice");
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000392 dt_prop_str(dt, "device_type", device_type_vdevice);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000393 dt_prop_str(dt, "compatible", "IBM,iSeries-vdevice");
394 dt_prop_u32(dt, "#address-cells", 1);
395 dt_prop_u32(dt, "#size-cells", 0);
396
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000397 dt_do_vdevice(dt, "vty", reg, -1, device_type_serial, NULL, 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000398 reg++;
399
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000400 dt_do_vdevice(dt, "v-scsi", reg, -1, device_type_vscsi,
401 "IBM,v-scsi", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000402 reg++;
403
404 vlan_map = HvLpConfig_getVirtualLanIndexMap();
405 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
406 unsigned char mac_addr[ETH_ALEN];
407
408 if ((vlan_map & (0x8000 >> i)) == 0)
409 continue;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000410 dt_do_vdevice(dt, "l-lan", reg, i, device_type_network,
411 "IBM,iSeries-l-lan", 0);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000412 mac_addr[0] = 0x02;
413 mac_addr[1] = 0x01;
414 mac_addr[2] = 0xff;
415 mac_addr[3] = i;
416 mac_addr[4] = 0xff;
417 mac_addr[5] = HvLpConfig_getLpIndex_outline();
418 dt_prop(dt, "local-mac-address", (char *)mac_addr, ETH_ALEN);
419 dt_prop(dt, "mac-address", (char *)mac_addr, ETH_ALEN);
420 dt_prop_u32(dt, "max-frame-size", 9000);
421 dt_prop_u32(dt, "address-bits", 48);
422
423 dt_end_node(dt);
424 }
425 reg += HVMAXARCHITECTEDVIRTUALLANS;
426
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000427 for (i = 0; i < HVMAXARCHITECTEDVIRTUALDISKS; i++)
428 dt_do_vdevice(dt, "viodasd", reg, i, device_type_block,
429 "IBM,iSeries-viodasd", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000430 reg += HVMAXARCHITECTEDVIRTUALDISKS;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000431
432 for (i = 0; i < HVMAXARCHITECTEDVIRTUALCDROMS; i++)
433 dt_do_vdevice(dt, "viocd", reg, i, device_type_block,
434 "IBM,iSeries-viocd", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000435 reg += HVMAXARCHITECTEDVIRTUALCDROMS;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000436
437 for (i = 0; i < HVMAXARCHITECTEDVIRTUALTAPES; i++)
438 dt_do_vdevice(dt, "viotape", reg, i, device_type_byte,
439 "IBM,iSeries-viotape", 1);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000440
441 dt_end_node(dt);
442}
443
444struct pci_class_name {
445 u16 code;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000446 const char *name;
447 const char *type;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000448};
449
450static struct pci_class_name __initdata pci_class_name[] = {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000451 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", device_type_network },
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000452};
453
454static struct pci_class_name * __init dt_find_pci_class_name(u16 class_code)
455{
456 struct pci_class_name *cp;
457
458 for (cp = pci_class_name;
459 cp < &pci_class_name[ARRAY_SIZE(pci_class_name)]; cp++)
460 if (cp->code == class_code)
461 return cp;
462 return NULL;
463}
464
465/*
466 * This assumes that the node slot is always on the primary bus!
467 */
468static void __init scan_bridge_slot(struct iseries_flat_dt *dt,
469 HvBusNumber bus, struct HvCallPci_BridgeInfo *bridge_info)
470{
471 HvSubBusNumber sub_bus = bridge_info->subBusNumber;
472 u16 vendor_id;
473 u16 device_id;
474 u32 class_id;
475 int err;
476 char buf[32];
477 u32 reg[5];
478 int id_sel = ISERIES_GET_DEVICE_FROM_SUBBUS(sub_bus);
479 int function = ISERIES_GET_FUNCTION_FROM_SUBBUS(sub_bus);
480 HvAgentId eads_id_sel = ISERIES_PCI_AGENTID(id_sel, function);
481 u8 devfn;
482 struct pci_class_name *cp;
483
484 /*
485 * Connect all functions of any device found.
486 */
487 for (id_sel = 1; id_sel <= bridge_info->maxAgents; id_sel++) {
488 for (function = 0; function < 8; function++) {
489 HvAgentId agent_id = ISERIES_PCI_AGENTID(id_sel,
490 function);
491 err = HvCallXm_connectBusUnit(bus, sub_bus,
492 agent_id, 0);
493 if (err) {
494 if (err != 0x302)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000495 DBG("connectBusUnit(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000496 bus, sub_bus, agent_id, err);
497 continue;
498 }
499
500 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
501 PCI_VENDOR_ID, &vendor_id);
502 if (err) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000503 DBG("ReadVendor(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000504 bus, sub_bus, agent_id, err);
505 continue;
506 }
507 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
508 PCI_DEVICE_ID, &device_id);
509 if (err) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000510 DBG("ReadDevice(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000511 bus, sub_bus, agent_id, err);
512 continue;
513 }
514 err = HvCallPci_configLoad32(bus, sub_bus, agent_id,
515 PCI_CLASS_REVISION , &class_id);
516 if (err) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000517 DBG("ReadClass(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000518 bus, sub_bus, agent_id, err);
519 continue;
520 }
521
522 devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(eads_id_sel),
523 function);
524 cp = dt_find_pci_class_name(class_id >> 16);
525 if (cp && cp->name)
526 strncpy(buf, cp->name, sizeof(buf) - 1);
527 else
528 snprintf(buf, sizeof(buf), "pci%x,%x",
529 vendor_id, device_id);
530 buf[sizeof(buf) - 1] = '\0';
531 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
532 "@%x", PCI_SLOT(devfn));
533 buf[sizeof(buf) - 1] = '\0';
534 if (function != 0)
535 snprintf(buf + strlen(buf),
536 sizeof(buf) - strlen(buf),
537 ",%x", function);
538 dt_start_node(dt, buf);
539 reg[0] = (bus << 16) | (devfn << 8);
540 reg[1] = 0;
541 reg[2] = 0;
542 reg[3] = 0;
543 reg[4] = 0;
544 dt_prop_u32_list(dt, "reg", reg, 5);
545 if (cp && (cp->type || cp->name))
546 dt_prop_str(dt, "device_type",
547 cp->type ? cp->type : cp->name);
548 dt_prop_u32(dt, "vendor-id", vendor_id);
549 dt_prop_u32(dt, "device-id", device_id);
550 dt_prop_u32(dt, "class-code", class_id >> 8);
551 dt_prop_u32(dt, "revision-id", class_id & 0xff);
552 dt_prop_u32(dt, "linux,subbus", sub_bus);
553 dt_prop_u32(dt, "linux,agent-id", agent_id);
554 dt_prop_u32(dt, "linux,logical-slot-number",
555 bridge_info->logicalSlotNumber);
556 dt_end_node(dt);
557
558 }
559 }
560}
561
562static void __init scan_bridge(struct iseries_flat_dt *dt, HvBusNumber bus,
563 HvSubBusNumber sub_bus, int id_sel)
564{
565 struct HvCallPci_BridgeInfo bridge_info;
566 HvAgentId agent_id;
567 int function;
568 int ret;
569
570 /* Note: hvSubBus and irq is always be 0 at this level! */
571 for (function = 0; function < 8; ++function) {
572 agent_id = ISERIES_PCI_AGENTID(id_sel, function);
573 ret = HvCallXm_connectBusUnit(bus, sub_bus, agent_id, 0);
574 if (ret != 0) {
575 if (ret != 0xb)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000576 DBG("connectBusUnit(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000577 bus, sub_bus, agent_id, ret);
578 continue;
579 }
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000580 DBG("found device at bus %d idsel %d func %d (AgentId %x)\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000581 bus, id_sel, function, agent_id);
582 ret = HvCallPci_getBusUnitInfo(bus, sub_bus, agent_id,
583 iseries_hv_addr(&bridge_info),
584 sizeof(struct HvCallPci_BridgeInfo));
585 if (ret != 0)
586 continue;
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000587 DBG("bridge info: type %x subbus %x "
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000588 "maxAgents %x maxsubbus %x logslot %x\n",
589 bridge_info.busUnitInfo.deviceType,
590 bridge_info.subBusNumber,
591 bridge_info.maxAgents,
592 bridge_info.maxSubBusNumber,
593 bridge_info.logicalSlotNumber);
594 if (bridge_info.busUnitInfo.deviceType ==
595 HvCallPci_BridgeDevice)
596 scan_bridge_slot(dt, bus, &bridge_info);
597 else
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000598 DBG("PCI: Invalid Bridge Configuration(0x%02X)",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000599 bridge_info.busUnitInfo.deviceType);
600 }
601}
602
603static void __init scan_phb(struct iseries_flat_dt *dt, HvBusNumber bus)
604{
605 struct HvCallPci_DeviceInfo dev_info;
606 const HvSubBusNumber sub_bus = 0; /* EADs is always 0. */
607 int err;
608 int id_sel;
609 const int max_agents = 8;
610
611 /*
612 * Probe for EADs Bridges
613 */
614 for (id_sel = 1; id_sel < max_agents; ++id_sel) {
615 err = HvCallPci_getDeviceInfo(bus, sub_bus, id_sel,
616 iseries_hv_addr(&dev_info),
617 sizeof(struct HvCallPci_DeviceInfo));
618 if (err) {
619 if (err != 0x302)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000620 DBG("getDeviceInfo(%x, %x, %x) %x\n",
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000621 bus, sub_bus, id_sel, err);
622 continue;
623 }
624 if (dev_info.deviceType != HvCallPci_NodeDevice) {
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000625 DBG("PCI: Invalid System Configuration"
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000626 "(0x%02X) for bus 0x%02x id 0x%02x.\n",
627 dev_info.deviceType, bus, id_sel);
628 continue;
629 }
630 scan_bridge(dt, bus, sub_bus, id_sel);
631 }
632}
633
634static void __init dt_pci_devices(struct iseries_flat_dt *dt)
635{
636 HvBusNumber bus;
637 char buf[32];
638 u32 buses[2];
639 int phb_num = 0;
640
641 /* Check all possible buses. */
642 for (bus = 0; bus < 256; bus++) {
643 int err = HvCallXm_testBus(bus);
644
645 if (err) {
646 /*
647 * Check for Unexpected Return code, a clue that
648 * something has gone wrong.
649 */
650 if (err != 0x0301)
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000651 DBG("Unexpected Return on Probe(0x%02X) "
652 "0x%04X\n", bus, err);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000653 continue;
654 }
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000655 DBG("bus %d appears to exist\n", bus);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000656 snprintf(buf, 32, "pci@%d", phb_num);
657 dt_start_node(dt, buf);
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000658 dt_prop_str(dt, "device_type", device_type_pci);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000659 dt_prop_str(dt, "compatible", "IBM,iSeries-Logical-PHB");
660 dt_prop_u32(dt, "#address-cells", 3);
661 dt_prop_u32(dt, "#size-cells", 2);
662 buses[0] = buses[1] = bus;
663 dt_prop_u32_list(dt, "bus-range", buses, 2);
664 scan_phb(dt, bus);
665 dt_end_node(dt);
666 phb_num++;
667 }
668}
669
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000670static void dt_finish(struct iseries_flat_dt *dt)
671{
672 dt_push_u32(dt, OF_DT_END);
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000673 dt->header.totalsize = (unsigned long)dt_data - (unsigned long)dt;
674 klimit = ALIGN((unsigned long)dt_data, 8);
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000675}
676
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000677void * __init build_flat_dt(unsigned long phys_mem_size)
678{
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000679 struct iseries_flat_dt *iseries_dt;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000680 u64 tmp[2];
681
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000682 iseries_dt = dt_init();
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000683
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000684 dt_start_node(iseries_dt, "");
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000685
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000686 dt_prop_u32(iseries_dt, "#address-cells", 2);
687 dt_prop_u32(iseries_dt, "#size-cells", 2);
688 dt_model(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000689
690 /* /memory */
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000691 dt_start_node(iseries_dt, "memory@0");
Stephen Rothwell7499bf12006-05-19 17:06:38 +1000692 dt_prop_str(iseries_dt, "device_type", device_type_memory);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000693 tmp[0] = 0;
694 tmp[1] = phys_mem_size;
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000695 dt_prop_u64_list(iseries_dt, "reg", tmp, 2);
696 dt_end_node(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000697
698 /* /chosen */
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000699 dt_start_node(iseries_dt, "chosen");
700 dt_prop_str(iseries_dt, "bootargs", cmd_line);
701 dt_end_node(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000702
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000703 dt_cpus(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000704
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000705 dt_vdevices(iseries_dt);
706 dt_pci_devices(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000707
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000708 dt_end_node(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000709
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000710 dt_finish(iseries_dt);
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000711
Stephen Rothwellc4e3ea22006-05-19 17:04:48 +1000712 return iseries_dt;
Stephen Rothwellc81014f2006-05-19 17:00:04 +1000713}