- See Issue Tracker id-4 "lnxi-patch-4"
[coreboot.git] / src / cpu / amd / dualcore / amd_sibling.c
1 /* 2004.12 yhlu add dual core support */
2 /* 24 June 2005 Cleaned up dual core support Eric Biederman */
3
4 #include <console/console.h>
5 #include <cpu/cpu.h>
6 #include <cpu/x86/lapic.h>
7 #include <cpu/amd/dualcore.h>
8 #include <device/device.h>
9 #include <device/pci.h>
10 #include <pc80/mc146818rtc.h>
11 #include <smp/spinlock.h>
12 #include <cpu/x86/mtrr.h>
13 #include "../model_fxx/model_fxx_msr.h"
14 #include "../../../northbridge/amd/amdk8/cpu_rev.c"
15
16 static int first_time = 1;
17 static int disable_siblings = !CONFIG_LOGICAL_CPUS;
18 void amd_sibling_init(device_t cpu, struct node_core_id id)
19 {
20         unsigned long i;
21         unsigned siblings, max_siblings;
22
23         /* On the bootstrap processor see if I want sibling cpus enabled */
24         if (first_time) {
25                 first_time = 0;
26                 get_option(&disable_siblings, "dual_core");
27         }
28
29         siblings = cpuid_ecx(0x80000008) & 0xff;
30         printk_debug("%d Sibling Cores found\n", siblings);
31
32         /* For now assume all cpus have the same number of siblings */
33         max_siblings = siblings + 1;
34
35         /* Wishlist? make dual cores look like hyperthreading */
36
37         /* See if I am a sibling cpu */
38         if (disable_siblings && (id.coreid != 0)) {
39                 cpu->enabled = 0;
40         }
41
42         if (id.coreid == 0) {
43                 /* On the primary cpu find the siblings */
44                 for (i = 1; i <= siblings; i++) {
45                         struct device_path cpu_path;
46                         device_t new;
47                         /* Build the cpu device path */
48                         cpu_path.type = DEVICE_PATH_APIC;
49                         cpu_path.u.apic.apic_id = 
50                                 (0x10 + i*0x10 + id.nodeid);
51                         new = alloc_dev(cpu->bus, &cpu_path);
52                         if (!new) {
53                                 continue;
54                         }
55                         /* Report what I have done */
56                         printk_debug("CPU: %s %s\n",
57                                 dev_path(new), new->enabled?"enabled":"disabled");
58                 }
59         }
60 }
61
62 struct node_core_id get_node_core_id(void)
63 {
64         struct node_core_id id;
65         unsigned siblings;
66         /* Get the apicid at reset */
67         id.nodeid = (cpuid_ebx(1) >> 24) & 0xff;
68         id.coreid = 0;  
69         /* Find out how many siblings we have */
70         siblings = cpuid_ecx(0x80000008) & 0xff;
71         if (siblings) {
72                 unsigned bits;
73                 msr_t msr;
74                 bits = 0;
75                 while ((1 << bits) <= siblings)
76                         bits++;
77
78                 msr = rdmsr(NB_CFG_MSR);
79                 if ((msr.hi >> (54-32)) & 1) {
80                 // when NB_CFG[54] is set, nodeid = ebx[27:25], coreid = ebx[24]
81                         id.coreid = id.nodeid & ((1 << bits) - 1);
82                         id.nodeid >>= bits;
83                 } else {
84                 // when NB_CFG[54] is clear, nodeid = ebx[26:24], coreid = ebx[27]
85                         id.coreid = id.nodeid >> 3;
86                         id.nodeid &= 7;
87                 }
88         } else {
89                 if (!is_cpu_pre_e0()) {
90                         id.nodeid >>= 1;
91                 }
92         }
93         return id;
94 }
95
96
97
98 #if 0
99 static int get_max_siblings(int nodes)
100 {
101         device_t dev;
102         int nodeid;
103         int siblings=0;
104
105         //get max siblings from all the nodes
106         for(nodeid=0; nodeid<nodes; nodeid++){
107                 int j;
108                 dev = dev_find_slot(0, PCI_DEVFN(0x18+nodeid, 3));
109                 j = (pci_read_config32(dev, 0xe8) >> 12) & 3; 
110                 if(siblings < j) {
111                         siblings = j;
112                 }
113         }
114         
115         return siblings;
116 }
117
118 static void enable_apic_ext_id(int nodes)
119 {
120         device_t dev;
121         int nodeid;
122
123         //enable APIC_EXIT_ID all the nodes
124         for(nodeid=0; nodeid<nodes; nodeid++){
125                 uint32_t val;
126                 dev = dev_find_slot(0, PCI_DEVFN(0x18+nodeid, 0));
127                 val = pci_read_config32(dev, 0x68);
128                 val |= (1<<17)|(1<<18);
129                 pci_write_config32(dev, 0x68, val); 
130         }
131 }
132
133
134 unsigned get_apicid_base(unsigned ioapic_num)
135 {
136         device_t dev;
137         int nodes;
138         unsigned apicid_base;
139         int siblings;
140         unsigned nb_cfg_54;
141         int bsp_apic_id = lapicid(); // bsp apicid
142
143         int disable_siblings = !CONFIG_LOGICAL_CPUS;
144
145
146         get_option(&disable_siblings, "dual_core");
147
148         //get the nodes number
149         dev = dev_find_slot(0, PCI_DEVFN(0x18,0));
150         nodes = ((pci_read_config32(dev, 0x60)>>4) & 7) + 1;
151
152         siblings = get_max_siblings(nodes);
153
154         if(bsp_apic_id > 0) { // io apic could start from 0
155                 return 0;
156         } else if(pci_read_config32(dev, 0x68) & ( (1<<17) | (1<<18)) )  { // enabled ext id but bsp = 0
157                 if(!disable_siblings) { return siblings + 1; }
158                 else { return 1; }
159         }
160
161         nb_cfg_54 = read_nb_cfg_54();
162
163 #if 0
164         //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.
165         if(nb_cfg_54 && (!disable_siblings) && (siblings == 0)) {
166                 //we need to check if e0 single core is there
167                 int i;
168                 for(i=0; i<nodes; i++) {
169                         if(is_e0_later_in_bsp(i)) {
170                                 siblings = 1;
171                                 break;
172                         }
173                 }
174         }
175 #endif
176
177         //contruct apicid_base
178
179         if((!disable_siblings) && (siblings>0) ) {
180                 /* for 8 way dual core, we will used up apicid 16:16, actualy 16 is not allowed by current kernel
181                 and the kernel will try to get one that is small than 16 to make io apic work.
182                 I don't know when the kernel can support 256 apic id. (APIC_EXT_ID is enabled) */
183
184                 //4:10 for two way  8:12 for four way 16:16 for eight way
185                 //Use CONFIG_MAX_PHYSICAL_CPUS instead of nodes for better consistency?
186                 apicid_base = nb_cfg_54 ? (siblings+1) * nodes :  8 * siblings + nodes; 
187
188         }
189         else {
190                 apicid_base = nodes;
191         }
192
193         if((apicid_base+ioapic_num-1)>0xf) {
194                 // We need to enable APIC EXT ID
195                 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");
196                 enable_apic_ext_id(nodes);
197         }
198         
199         return apicid_base;
200 }
201
202 #endif