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