2 * Copyright (c) 2005-2006 Michael Ellerman, IBM Corporation
5 * This file contains all the routines to build a flattened device
6 * tree for a legacy iSeries machine.
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.
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 */
27 #include <asm/machdep.h>
29 #include <asm/lppaca.h>
31 #include <asm/cputable.h>
32 #include <asm/abs_addr.h>
33 #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>
39 #include "processor_vpd.h"
45 #define DBG(fmt...) udbg_printf(fmt)
51 unsigned char data
[PAGE_SIZE
* 2];
55 struct iseries_flat_dt
{
56 struct boot_param_header header
;
62 static struct iseries_flat_dt iseries_dt
;
64 static void __init
dt_init(struct iseries_flat_dt
*dt
)
66 dt
->header
.off_mem_rsvmap
=
67 offsetof(struct iseries_flat_dt
, reserve_map
);
68 dt
->header
.off_dt_struct
= offsetof(struct iseries_flat_dt
, dt
);
69 dt
->header
.off_dt_strings
= offsetof(struct iseries_flat_dt
, strings
);
70 dt
->header
.totalsize
= sizeof(struct iseries_flat_dt
);
71 dt
->header
.dt_strings_size
= sizeof(struct blob
);
73 /* There is no notion of hardware cpu id on iSeries */
74 dt
->header
.boot_cpuid_phys
= smp_processor_id();
76 dt
->dt
.next
= (unsigned long)&dt
->dt
.data
;
77 dt
->strings
.next
= (unsigned long)&dt
->strings
.data
;
79 dt
->header
.magic
= OF_DT_HEADER
;
80 dt
->header
.version
= 0x10;
81 dt
->header
.last_comp_version
= 0x10;
83 dt
->reserve_map
[0] = 0;
84 dt
->reserve_map
[1] = 0;
87 static void __init
dt_check_blob(struct blob
*b
)
89 if (b
->next
>= (unsigned long)&b
->next
) {
90 DBG("Ran out of space in flat device tree blob!\n");
95 static void __init
dt_push_u32(struct iseries_flat_dt
*dt
, u32 value
)
97 *((u32
*)dt
->dt
.next
) = value
;
98 dt
->dt
.next
+= sizeof(u32
);
100 dt_check_blob(&dt
->dt
);
104 static void __init
dt_push_u64(struct iseries_flat_dt
*dt
, u64 value
)
106 *((u64
*)dt
->dt
.next
) = value
;
107 dt
->dt
.next
+= sizeof(u64
);
109 dt_check_blob(&dt
->dt
);
113 static unsigned long __init
dt_push_bytes(struct blob
*blob
, char *data
, int len
)
115 unsigned long start
= blob
->next
- (unsigned long)blob
->data
;
117 memcpy((char *)blob
->next
, data
, len
);
118 blob
->next
= _ALIGN(blob
->next
+ len
, 4);
125 static void __init
dt_start_node(struct iseries_flat_dt
*dt
, char *name
)
127 dt_push_u32(dt
, OF_DT_BEGIN_NODE
);
128 dt_push_bytes(&dt
->dt
, name
, strlen(name
) + 1);
131 #define dt_end_node(dt) dt_push_u32(dt, OF_DT_END_NODE)
133 static void __init
dt_prop(struct iseries_flat_dt
*dt
, char *name
,
136 unsigned long offset
;
138 dt_push_u32(dt
, OF_DT_PROP
);
140 /* Length of the data */
141 dt_push_u32(dt
, len
);
143 /* Put the property name in the string blob. */
144 offset
= dt_push_bytes(&dt
->strings
, name
, strlen(name
) + 1);
146 /* The offset of the properties name in the string blob. */
147 dt_push_u32(dt
, (u32
)offset
);
149 /* The actual data. */
150 dt_push_bytes(&dt
->dt
, data
, len
);
153 static void __init
dt_prop_str(struct iseries_flat_dt
*dt
, char *name
,
156 dt_prop(dt
, name
, data
, strlen(data
) + 1); /* + 1 for NULL */
159 static void __init
dt_prop_u32(struct iseries_flat_dt
*dt
, char *name
, u32 data
)
161 dt_prop(dt
, name
, (char *)&data
, sizeof(u32
));
164 static void __init
dt_prop_u64(struct iseries_flat_dt
*dt
, char *name
, u64 data
)
166 dt_prop(dt
, name
, (char *)&data
, sizeof(u64
));
169 static void __init
dt_prop_u64_list(struct iseries_flat_dt
*dt
, char *name
,
172 dt_prop(dt
, name
, (char *)data
, sizeof(u64
) * n
);
175 static void __init
dt_prop_u32_list(struct iseries_flat_dt
*dt
, char *name
,
178 dt_prop(dt
, name
, (char *)data
, sizeof(u32
) * n
);
182 static void __init
dt_prop_empty(struct iseries_flat_dt
*dt
, char *name
)
184 dt_prop(dt
, name
, NULL
, 0);
188 static void __init
dt_cpus(struct iseries_flat_dt
*dt
)
190 unsigned char buf
[32];
192 unsigned int i
, index
;
193 struct IoHriProcessorVpd
*d
;
197 snprintf(buf
, 32, "PowerPC,%s", cur_cpu_spec
->cpu_name
);
198 p
= strchr(buf
, ' ');
199 if (!p
) p
= buf
+ strlen(buf
);
201 dt_start_node(dt
, "cpus");
202 dt_prop_u32(dt
, "#address-cells", 1);
203 dt_prop_u32(dt
, "#size-cells", 0);
205 pft_size
[0] = 0; /* NUMA CEC cookie, 0 for non NUMA */
206 pft_size
[1] = __ilog2(HvCallHpt_getHptPages() * HW_PAGE_SIZE
);
208 for (i
= 0; i
< NR_CPUS
; i
++) {
209 if (lppaca
[i
].dyn_proc_status
>= 2)
212 snprintf(p
, 32 - (p
- buf
), "@%d", i
);
213 dt_start_node(dt
, buf
);
215 dt_prop_str(dt
, "device_type", "cpu");
217 index
= lppaca
[i
].dyn_hv_phys_proc_index
;
218 d
= &xIoHriProcessorVpd
[index
];
220 dt_prop_u32(dt
, "i-cache-size", d
->xInstCacheSize
* 1024);
221 dt_prop_u32(dt
, "i-cache-line-size", d
->xInstCacheOperandSize
);
223 dt_prop_u32(dt
, "d-cache-size", d
->xDataL1CacheSizeKB
* 1024);
224 dt_prop_u32(dt
, "d-cache-line-size", d
->xDataCacheOperandSize
);
226 /* magic conversions to Hz copied from old code */
227 dt_prop_u32(dt
, "clock-frequency",
228 ((1UL << 34) * 1000000) / d
->xProcFreq
);
229 dt_prop_u32(dt
, "timebase-frequency",
230 ((1UL << 32) * 1000000) / d
->xTimeBaseFreq
);
232 dt_prop_u32(dt
, "reg", i
);
234 dt_prop_u32_list(dt
, "ibm,pft-size", pft_size
, 2);
242 static void __init
dt_model(struct iseries_flat_dt
*dt
)
244 char buf
[16] = "IBM,";
246 /* "IBM," + mfgId[2:3] + systemSerial[1:5] */
247 strne2a(buf
+ 4, xItExtVpdPanel
.mfgID
+ 2, 2);
248 strne2a(buf
+ 6, xItExtVpdPanel
.systemSerial
+ 1, 5);
250 dt_prop_str(dt
, "system-id", buf
);
252 /* "IBM," + machineType[0:4] */
253 strne2a(buf
+ 4, xItExtVpdPanel
.machineType
, 4);
255 dt_prop_str(dt
, "model", buf
);
257 dt_prop_str(dt
, "compatible", "IBM,iSeries");
260 static void __init
dt_vdevices(struct iseries_flat_dt
*dt
)
263 HvLpIndexMap vlan_map
;
267 dt_start_node(dt
, "vdevice");
268 dt_prop_str(dt
, "device_type", "vdevice");
269 dt_prop_str(dt
, "compatible", "IBM,iSeries-vdevice");
270 dt_prop_u32(dt
, "#address-cells", 1);
271 dt_prop_u32(dt
, "#size-cells", 0);
273 snprintf(buf
, sizeof(buf
), "vty@%08x", reg
);
274 dt_start_node(dt
, buf
);
275 dt_prop_str(dt
, "device_type", "serial");
276 dt_prop_u32(dt
, "reg", reg
);
280 snprintf(buf
, sizeof(buf
), "v-scsi@%08x", reg
);
281 dt_start_node(dt
, buf
);
282 dt_prop_str(dt
, "device_type", "vscsi");
283 dt_prop_str(dt
, "compatible", "IBM,v-scsi");
284 dt_prop_u32(dt
, "reg", reg
);
288 vlan_map
= HvLpConfig_getVirtualLanIndexMap();
289 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALLANS
; i
++) {
290 unsigned char mac_addr
[ETH_ALEN
];
292 if ((vlan_map
& (0x8000 >> i
)) == 0)
294 snprintf(buf
, 32, "l-lan@%08x", reg
+ i
);
295 dt_start_node(dt
, buf
);
296 dt_prop_str(dt
, "device_type", "network");
297 dt_prop_str(dt
, "compatible", "IBM,iSeries-l-lan");
298 dt_prop_u32(dt
, "reg", reg
+ i
);
299 dt_prop_u32(dt
, "linux,unit_address", i
);
306 mac_addr
[5] = HvLpConfig_getLpIndex_outline();
307 dt_prop(dt
, "local-mac-address", (char *)mac_addr
, ETH_ALEN
);
308 dt_prop(dt
, "mac-address", (char *)mac_addr
, ETH_ALEN
);
309 dt_prop_u32(dt
, "max-frame-size", 9000);
310 dt_prop_u32(dt
, "address-bits", 48);
314 reg
+= HVMAXARCHITECTEDVIRTUALLANS
;
316 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALDISKS
; i
++) {
317 snprintf(buf
, 32, "viodasd@%08x", reg
+ i
);
318 dt_start_node(dt
, buf
);
319 dt_prop_str(dt
, "device_type", "block");
320 dt_prop_str(dt
, "compatible", "IBM,iSeries-viodasd");
321 dt_prop_u32(dt
, "reg", reg
+ i
);
322 dt_prop_u32(dt
, "linux,unit_address", i
);
325 reg
+= HVMAXARCHITECTEDVIRTUALDISKS
;
326 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALCDROMS
; i
++) {
327 snprintf(buf
, 32, "viocd@%08x", reg
+ i
);
328 dt_start_node(dt
, buf
);
329 dt_prop_str(dt
, "device_type", "block");
330 dt_prop_str(dt
, "compatible", "IBM,iSeries-viocd");
331 dt_prop_u32(dt
, "reg", reg
+ i
);
332 dt_prop_u32(dt
, "linux,unit_address", i
);
335 reg
+= HVMAXARCHITECTEDVIRTUALCDROMS
;
336 for (i
= 0; i
< HVMAXARCHITECTEDVIRTUALTAPES
; i
++) {
337 snprintf(buf
, 32, "viotape@%08x", reg
+ i
);
338 dt_start_node(dt
, buf
);
339 dt_prop_str(dt
, "device_type", "byte");
340 dt_prop_str(dt
, "compatible", "IBM,iSeries-viotape");
341 dt_prop_u32(dt
, "reg", reg
+ i
);
342 dt_prop_u32(dt
, "linux,unit_address", i
);
349 struct pci_class_name
{
355 static struct pci_class_name __initdata pci_class_name
[] = {
356 { PCI_CLASS_NETWORK_ETHERNET
, "ethernet", "network" },
359 static struct pci_class_name
* __init
dt_find_pci_class_name(u16 class_code
)
361 struct pci_class_name
*cp
;
363 for (cp
= pci_class_name
;
364 cp
< &pci_class_name
[ARRAY_SIZE(pci_class_name
)]; cp
++)
365 if (cp
->code
== class_code
)
371 * This assumes that the node slot is always on the primary bus!
373 static void __init
scan_bridge_slot(struct iseries_flat_dt
*dt
,
374 HvBusNumber bus
, struct HvCallPci_BridgeInfo
*bridge_info
)
376 HvSubBusNumber sub_bus
= bridge_info
->subBusNumber
;
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
);
387 struct pci_class_name
*cp
;
390 * Connect all functions of any device found.
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
,
396 err
= HvCallXm_connectBusUnit(bus
, sub_bus
,
401 "connectBusUnit(%x, %x, %x) "
403 bus
, sub_bus
, agent_id
, err
);
407 err
= HvCallPci_configLoad16(bus
, sub_bus
, agent_id
,
408 PCI_VENDOR_ID
, &vendor_id
);
411 "ReadVendor(%x, %x, %x) == %x\n",
412 bus
, sub_bus
, agent_id
, err
);
415 err
= HvCallPci_configLoad16(bus
, sub_bus
, agent_id
,
416 PCI_DEVICE_ID
, &device_id
);
419 "ReadDevice(%x, %x, %x) == %x\n",
420 bus
, sub_bus
, agent_id
, err
);
423 err
= HvCallPci_configLoad32(bus
, sub_bus
, agent_id
,
424 PCI_CLASS_REVISION
, &class_id
);
427 "ReadClass(%x, %x, %x) == %x\n",
428 bus
, sub_bus
, agent_id
, err
);
432 devfn
= PCI_DEVFN(ISERIES_ENCODE_DEVICE(eads_id_sel
),
434 cp
= dt_find_pci_class_name(class_id
>> 16);
436 strncpy(buf
, cp
->name
, sizeof(buf
) - 1);
438 snprintf(buf
, sizeof(buf
), "pci%x,%x",
439 vendor_id
, device_id
);
440 buf
[sizeof(buf
) - 1] = '\0';
441 snprintf(buf
+ strlen(buf
), sizeof(buf
) - strlen(buf
),
442 "@%x", PCI_SLOT(devfn
));
443 buf
[sizeof(buf
) - 1] = '\0';
445 snprintf(buf
+ strlen(buf
),
446 sizeof(buf
) - strlen(buf
),
448 dt_start_node(dt
, buf
);
449 reg
[0] = (bus
<< 16) | (devfn
<< 8);
454 dt_prop_u32_list(dt
, "reg", reg
, 5);
455 if (cp
&& (cp
->type
|| cp
->name
))
456 dt_prop_str(dt
, "device_type",
457 cp
->type
? cp
->type
: cp
->name
);
458 dt_prop_u32(dt
, "vendor-id", vendor_id
);
459 dt_prop_u32(dt
, "device-id", device_id
);
460 dt_prop_u32(dt
, "class-code", class_id
>> 8);
461 dt_prop_u32(dt
, "revision-id", class_id
& 0xff);
462 dt_prop_u32(dt
, "linux,subbus", sub_bus
);
463 dt_prop_u32(dt
, "linux,agent-id", agent_id
);
464 dt_prop_u32(dt
, "linux,logical-slot-number",
465 bridge_info
->logicalSlotNumber
);
472 static void __init
scan_bridge(struct iseries_flat_dt
*dt
, HvBusNumber bus
,
473 HvSubBusNumber sub_bus
, int id_sel
)
475 struct HvCallPci_BridgeInfo bridge_info
;
480 /* Note: hvSubBus and irq is always be 0 at this level! */
481 for (function
= 0; function
< 8; ++function
) {
482 agent_id
= ISERIES_PCI_AGENTID(id_sel
, function
);
483 ret
= HvCallXm_connectBusUnit(bus
, sub_bus
, agent_id
, 0);
486 printk(KERN_DEBUG
"connectBusUnit(%x, %x, %x) "
488 bus
, sub_bus
, agent_id
, ret
);
491 printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
492 bus
, id_sel
, function
, agent_id
);
493 ret
= HvCallPci_getBusUnitInfo(bus
, sub_bus
, agent_id
,
494 iseries_hv_addr(&bridge_info
),
495 sizeof(struct HvCallPci_BridgeInfo
));
498 printk("bridge info: type %x subbus %x "
499 "maxAgents %x maxsubbus %x logslot %x\n",
500 bridge_info
.busUnitInfo
.deviceType
,
501 bridge_info
.subBusNumber
,
502 bridge_info
.maxAgents
,
503 bridge_info
.maxSubBusNumber
,
504 bridge_info
.logicalSlotNumber
);
505 if (bridge_info
.busUnitInfo
.deviceType
==
506 HvCallPci_BridgeDevice
)
507 scan_bridge_slot(dt
, bus
, &bridge_info
);
509 printk("PCI: Invalid Bridge Configuration(0x%02X)",
510 bridge_info
.busUnitInfo
.deviceType
);
514 static void __init
scan_phb(struct iseries_flat_dt
*dt
, HvBusNumber bus
)
516 struct HvCallPci_DeviceInfo dev_info
;
517 const HvSubBusNumber sub_bus
= 0; /* EADs is always 0. */
520 const int max_agents
= 8;
523 * Probe for EADs Bridges
525 for (id_sel
= 1; id_sel
< max_agents
; ++id_sel
) {
526 err
= HvCallPci_getDeviceInfo(bus
, sub_bus
, id_sel
,
527 iseries_hv_addr(&dev_info
),
528 sizeof(struct HvCallPci_DeviceInfo
));
531 printk(KERN_DEBUG
"getDeviceInfo(%x, %x, %x) "
533 bus
, sub_bus
, id_sel
, err
);
536 if (dev_info
.deviceType
!= HvCallPci_NodeDevice
) {
537 printk(KERN_DEBUG
"PCI: Invalid System Configuration"
538 "(0x%02X) for bus 0x%02x id 0x%02x.\n",
539 dev_info
.deviceType
, bus
, id_sel
);
542 scan_bridge(dt
, bus
, sub_bus
, id_sel
);
546 static void __init
dt_pci_devices(struct iseries_flat_dt
*dt
)
553 /* Check all possible buses. */
554 for (bus
= 0; bus
< 256; bus
++) {
555 int err
= HvCallXm_testBus(bus
);
559 * Check for Unexpected Return code, a clue that
560 * something has gone wrong.
563 printk(KERN_ERR
"Unexpected Return on Probe"
564 "(0x%02X): 0x%04X", bus
, err
);
567 printk("bus %d appears to exist\n", bus
);
568 snprintf(buf
, 32, "pci@%d", phb_num
);
569 dt_start_node(dt
, buf
);
570 dt_prop_str(dt
, "device_type", "pci");
571 dt_prop_str(dt
, "compatible", "IBM,iSeries-Logical-PHB");
572 dt_prop_u32(dt
, "#address-cells", 3);
573 dt_prop_u32(dt
, "#size-cells", 2);
574 buses
[0] = buses
[1] = bus
;
575 dt_prop_u32_list(dt
, "bus-range", buses
, 2);
582 void * __init
build_flat_dt(unsigned long phys_mem_size
)
586 dt_init(&iseries_dt
);
588 dt_start_node(&iseries_dt
, "");
590 dt_prop_u32(&iseries_dt
, "#address-cells", 2);
591 dt_prop_u32(&iseries_dt
, "#size-cells", 2);
592 dt_model(&iseries_dt
);
595 dt_start_node(&iseries_dt
, "memory@0");
596 dt_prop_str(&iseries_dt
, "name", "memory");
597 dt_prop_str(&iseries_dt
, "device_type", "memory");
599 tmp
[1] = phys_mem_size
;
600 dt_prop_u64_list(&iseries_dt
, "reg", tmp
, 2);
601 dt_end_node(&iseries_dt
);
604 dt_start_node(&iseries_dt
, "chosen");
605 dt_prop_str(&iseries_dt
, "bootargs", cmd_line
);
606 dt_end_node(&iseries_dt
);
608 dt_cpus(&iseries_dt
);
610 dt_vdevices(&iseries_dt
);
611 dt_pci_devices(&iseries_dt
);
613 dt_end_node(&iseries_dt
);
615 dt_push_u32(&iseries_dt
, OF_DT_END
);