Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / cpu / amd / dualcore / amd_sibling.c
1 /* 2004.12 yhlu add dual core support */
2
3 #include <console/console.h>
4 #include <cpu/cpu.h>
5 #include <cpu/x86/lapic.h>
6 #include <cpu/amd/multicore.h>
7 #include <device/device.h>
8 #include <device/pci.h>
9 #include <pc80/mc146818rtc.h>
10 #include <smp/spinlock.h>
11 #include <cpu/x86/mtrr.h>
12 #include <cpu/amd/model_fxx_msr.h>
13 #include <cpu/amd/model_fxx_rev.h>
14 #include <cpu/amd/amdk8_sysconf.h>
15
16 static int disable_siblings = !CONFIG_LOGICAL_CPUS;
17
18 #include "dualcore_id.c"
19
20 static int get_max_siblings(int nodes)
21 {
22         device_t dev;
23         int nodeid;
24         int siblings=0;
25
26         //get max siblings from all the nodes
27         for(nodeid=0; nodeid<nodes; nodeid++){
28                 int j;
29                 dev = dev_find_slot(0, PCI_DEVFN(0x18+nodeid, 3));
30                 j = (pci_read_config32(dev, 0xe8) >> 12) & 3;
31                 if(siblings < j) {
32                         siblings = j;
33                 }
34         }
35
36         return siblings;
37 }
38
39 static void enable_apic_ext_id(int nodes)
40 {
41         device_t dev;
42         int nodeid;
43
44         //enable APIC_EXIT_ID all the nodes
45         for(nodeid=0; nodeid<nodes; nodeid++){
46                 uint32_t val;
47                 dev = dev_find_slot(0, PCI_DEVFN(0x18+nodeid, 0));
48                 val = pci_read_config32(dev, 0x68);
49                 val |= (1<<17)|(1<<18);
50                 pci_write_config32(dev, 0x68, val);
51         }
52 }
53
54
55 unsigned get_apicid_base(unsigned ioapic_num)
56 {
57         device_t dev;
58         int nodes;
59         unsigned apicid_base;
60         int siblings;
61         unsigned nb_cfg_54;
62         int bsp_apic_id = lapicid(); // bsp apicid
63
64         get_option(&disable_siblings, "multi_core");
65
66         //get the nodes number
67         dev = dev_find_slot(0, PCI_DEVFN(0x18,0));
68         nodes = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1;
69
70         siblings = get_max_siblings(nodes);
71
72         if(bsp_apic_id > 0) { // io apic could start from 0
73                 return 0;
74         } else if(pci_read_config32(dev, 0x68) & ( (1<<17) | (1<<18)) )  { // enabled ext id but bsp = 0
75                 return 1;
76         }
77
78         nb_cfg_54 = read_nb_cfg_54();
79
80 #if 0
81         //it is for all e0 single core and nc_cfg_54 low is set, but in the romstage.c stage we do not set that bit for it.
82         if(nb_cfg_54 && (!disable_siblings) && (siblings == 0)) {
83                 //we need to check if e0 single core is there
84                 int i;
85                 for(i=0; i<nodes; i++) {
86                         if(is_e0_later_in_bsp(i)) {
87                                 siblings = 1;
88                                 break;
89                         }
90                 }
91         }
92 #endif
93
94         //contruct apicid_base
95
96         if((!disable_siblings) && (siblings>0) ) {
97                 /* for 8 way dual core, we will used up apicid 16:16, actualy 16 is not allowed by current kernel
98                 and the kernel will try to get one that is small than 16 to make io apic work.
99                 I don't know when the kernel can support 256 apic id. (APIC_EXT_ID is enabled) */
100
101                 //4:10 for two way  8:12 for four way 16:16 for eight way
102                 //Use CONFIG_MAX_PHYSICAL_CPUS instead of nodes for better consistency?
103                 apicid_base = nb_cfg_54 ? (siblings+1) * nodes :  8 * siblings + nodes;
104
105         }
106         else {
107                 apicid_base = nodes;
108         }
109
110         if((apicid_base+ioapic_num-1)>0xf) {
111                 // We need to enable APIC EXT ID
112                 printk(BIOS_INFO, "if the IO APIC device doesn't support 256 apic id, \n you need to set CONFIG_ENABLE_APIC_EXT_ID in romstage.c so you can spare 16 id for ioapic\n");
113                 enable_apic_ext_id(nodes);
114         }
115
116         return apicid_base;
117 }
118
119 #if 0
120 static int first_time = 1;
121
122 void amd_sibling_init(device_t cpu)
123 {
124         unsigned i, siblings;
125         struct cpuid_result result;
126         unsigned nb_cfg_54;
127         struct node_core_id id;
128
129         /* On the bootstrap processor see if I want sibling cpus enabled */
130         if (first_time) {
131                 first_time = 0;
132                 get_option(&disable_siblings, "multi_core");
133         }
134         result = cpuid(0x80000008);
135         /* See how many sibling cpus we have */
136         /* Is dualcore supported */
137         siblings = (result.ecx & 0xff);
138         if ( siblings < 1) {
139                 return;
140         }
141
142 #if 1
143         printk(BIOS_DEBUG, "CPU: %u %d siblings\n",
144                 cpu->path.apic.apic_id,
145                 siblings);
146 #endif
147
148         nb_cfg_54 = read_nb_cfg_54();
149 #if 1
150         id = get_node_core_id(nb_cfg_54); // pre e0 nb_cfg_54 can not be set
151
152         /* See if I am a sibling cpu */
153         //if ((cpu->path.apic.apic_id>>(nb_cfg_54?0:3)) & siblings ) { // siblings = 1, 3, 7, 15,....
154         //if ( ( (cpu->path.apic.apic_id>>(nb_cfg_54?0:3)) % (siblings+1) ) != 0 ) {
155         if(id.coreid != 0) {
156                 if (disable_siblings) {
157                         cpu->enabled = 0;
158                 }
159                 return;
160         }
161 #endif
162
163         /* I am the primary cpu start up my siblings */
164
165         for(i = 1; i <= siblings; i++) {
166                 struct device_path cpu_path;
167                 device_t new;
168                 /* Build the cpu device path */
169                 cpu_path.type = DEVICE_PATH_APIC;
170                 cpu_path.apic.apic_id = cpu->path.apic.apic_id + i * (nb_cfg_54?1:8);
171                 if(id.nodeid == 0) {
172                         // need some special processing, because may the bsp is not lifted, but the core1 is lifted
173                         //defined in northbridge.c
174                         if(sysconf.enabled_apic_ext_id && (!sysconf.lift_bsp_apicid)) {
175                                 cpu->path.apic.apic_id += sysconf.apicid_offset;
176                         }
177
178                 }
179
180
181                 /* See if I can find the cpu */
182                 new = find_dev_path(cpu->bus, &cpu_path);
183                 /* Allocate the new cpu device structure */
184                 if(!new) {
185                         new = alloc_dev(cpu->bus, &cpu_path);
186                         new->enabled = 1;
187                         new->initialized = 0;
188                 }
189
190                 new->path.apic.node_id = cpu->path.apic.node_id;
191                 new->path.apic.core_id = i;
192
193 #if 1
194                 printk(BIOS_DEBUG, "CPU: %u has sibling %u\n",
195                         cpu->path.apic.apic_id,
196                         new->path.apic.apic_id);
197 #endif
198
199                 if(new->enabled && !new->initialized)
200                         start_cpu(new);
201         }
202 }
203 #endif
204