4ed770d9817c0bd8f42de46dda0b3260644aaf5c
[coreboot.git] / src / cpu / amd / quadcore / amd_sibling.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2007 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20
21 #include <console/console.h>
22 #include <cpu/cpu.h>
23 #include <cpu/x86/lapic.h>
24 #include <device/device.h>
25 #include <device/pci.h>
26 #include <pc80/mc146818rtc.h>
27 #include <smp/spinlock.h>
28 #include <cpu/x86/mtrr.h>
29 #include <cpu/amd/model_10xxx_msr.h>
30 #include <cpu/amd/model_10xxx_rev.h>
31 #include <cpu/amd/amdfam10_sysconf.h>
32
33 extern device_t get_node_pci(u32 nodeid, u32 fn);
34
35 #if 0
36 static int first_time = 1;
37 #endif
38
39 #include "quadcore_id.c"
40
41 static u32 get_max_siblings(u32 nodes)
42 {
43         device_t dev;
44         u32 nodeid;
45         u32 siblings=0;
46
47         //get max siblings from all the nodes
48         for(nodeid=0; nodeid<nodes; nodeid++){
49                 int j;
50                 dev = get_node_pci(nodeid, 3);
51                 j = (pci_read_config32(dev, 0xe8) >> 12) & 3;
52                 if(siblings < j) {
53                         siblings = j;
54                 }
55         }
56
57         return siblings;
58 }
59
60
61 static void enable_apic_ext_id(u32 nodes)
62 {
63         device_t dev;
64         u32 nodeid;
65
66         //enable APIC_EXIT_ID all the nodes
67         for(nodeid=0; nodeid<nodes; nodeid++){
68                 u32 val;
69                 dev = get_node_pci(nodeid, 0);
70                 val = pci_read_config32(dev, 0x68);
71                 val |= (1<<17)|(1<<18);
72                 pci_write_config32(dev, 0x68, val);
73         }
74 }
75
76
77 u32 get_apicid_base(u32 ioapic_num)
78 {
79         u32 apicid_base;
80         u32 siblings;
81         u32 nb_cfg_54;
82
83         u32 disable_siblings = !CONFIG_LOGICAL_CPUS;
84
85         get_option(&disable_siblings, "quad_core");
86
87         siblings = get_max_siblings(sysconf.nodes);
88
89         if(sysconf.bsp_apicid > 0) { // io apic could start from 0
90                 return 0;
91         } else if (sysconf.enabled_apic_ext_id) { // enabled ext id but bsp = 0
92                 return 1;
93         }
94
95         nb_cfg_54 = read_nb_cfg_54();
96
97
98         //contruct apicid_base
99
100         if((!disable_siblings) && (siblings>0) ) {
101                 /* for 8 way dual core, we will used up apicid 16:16, actualy
102                    16 is not allowed by current kernel and the kernel will try
103                    to get one that is small than 16 to make io apic work. I don't
104                    know when the kernel can support 256 apic id.
105                    (APIC_EXT_ID is enabled) */
106
107                 //4:10 for two way  8:12 for four way 16:16 for eight way
108                 //Use CONFIG_MAX_PHYSICAL_CPUS instead of nodes for better consistency?
109                 apicid_base = nb_cfg_54 ? (siblings+1) * sysconf.nodes :  8 * siblings + sysconf.nodes;
110
111         } else {
112                 apicid_base = sysconf.nodes;
113         }
114
115         if((apicid_base+ioapic_num-1)>0xf) {
116                 // We need to enable APIC EXT ID
117                 printk_spew("if the IO APIC device doesn't support 256 apic id, \r\n you need to set ENABLE_APIC_EXT_ID in MB Option.lb so you can spare 16 id for ioapic\r\n");
118                 enable_apic_ext_id(sysconf.nodes);
119         }
120
121         return apicid_base;
122 }