1 #include <common/cpu.h>
2 #include <common/kprint.h>
3 #include <common/printk.h>
4 // #pragma GCC optimize("O0")
5 // cpu支持的最大cpuid指令的基础主功能号
6 uint Cpu_cpuid_max_Basic_mop;
7 // cpu支持的最大cpuid指令的扩展主功能号
8 uint Cpu_cpuid_max_Extended_mop;
9 // cpu制造商信息
10 char Cpu_Manufacturer_Name[17] = {0};
11 // 处理器名称信息
12 char Cpu_BrandName[49] = {0};
13 // 处理器家族ID
14 uint Cpu_Family_ID;
15 // 处理器扩展家族ID
16 uint Cpu_Extended_Family_ID;
17 // 处理器模式ID
18 uint Cpu_Model_ID;
19 // 处理器扩展模式ID
20 uint Cpu_Extended_Model_ID;
21 // 处理器步进ID
22 uint Cpu_Stepping_ID;
23 // 处理器类型
24 uint Cpu_Processor_Type;
25 // 处理器支持的最大物理地址可寻址地址线宽度
26 uint Cpu_max_phys_addrline_size;
27 // 处理器支持的最大线性地址可寻址地址线宽度
28 uint Cpu_max_linear_addrline_size;
29 // 处理器的tsc频率(单位:hz)(HPET定时器在测定apic频率时,顺便测定了这个值)
30 uint64_t Cpu_tsc_freq = 0;
31
32 struct cpu_core_info_t cpu_core_info[MAX_CPU_NUM];
cpu_init(void)33 void cpu_init(void)
34 {
35 // 获取处理器制造商信息
36 uint tmp_info[4] = {0};
37 cpu_cpuid(0, 0, &tmp_info[0], &tmp_info[1], &tmp_info[2], &tmp_info[3]);
38
39 // 保存CPU支持的最大cpuid指令主功能号
40 Cpu_cpuid_max_Basic_mop = tmp_info[0];
41 // 保存制造商名称
42 *(uint *)&Cpu_Manufacturer_Name[0] = tmp_info[1];
43 *(uint *)&Cpu_Manufacturer_Name[4] = tmp_info[3];
44 *(uint *)&Cpu_Manufacturer_Name[8] = tmp_info[2];
45 Cpu_Manufacturer_Name[12] = '\0';
46 kinfo("CPU manufacturer: %s", Cpu_Manufacturer_Name);
47
48 // 获取处理器型号信息
49 int count = 0;
50 for (uint i = 0x80000002; i < 0x80000005; ++i)
51 {
52 cpu_cpuid(i, 0, &tmp_info[0], &tmp_info[1], &tmp_info[2], &tmp_info[3]);
53 for (int j = 0; j <= 3; ++j)
54 {
55 *(uint *)&Cpu_BrandName[4 * count] = tmp_info[j];
56 ++count;
57 }
58 }
59 Cpu_BrandName[48] = '\0';
60
61 kinfo("CPU Brand Name: %s", Cpu_BrandName);
62
63 // 使用cpuid主功能号0x01进行查询(未保存ebx ecx edx的信息,具体参见白皮书)
64 cpu_cpuid(1, 0, &tmp_info[0], &tmp_info[1], &tmp_info[2], &tmp_info[3]);
65
66 // EAX中包含 Version Informatin Type,Family,Model,and Stepping ID
67 Cpu_Stepping_ID = tmp_info[0] & 0xf;
68 Cpu_Model_ID = (tmp_info[0] >> 4) & 0xf;
69 Cpu_Family_ID = (tmp_info[0] >> 8) & 0xf;
70 Cpu_Processor_Type = (tmp_info[0] >> 12) & 0x3;
71 // 14-15位保留
72 Cpu_Extended_Model_ID = (tmp_info[0] >> 16) & 0xf;
73 Cpu_Extended_Family_ID = (tmp_info[0] >> 20) & 0xff;
74 // 31-25位保留
75 kinfo("Family ID=%#03lx\t Extended Family ID=%#03lx\t Processor Type=%#03lx\t", Cpu_Family_ID, Cpu_Extended_Family_ID, Cpu_Processor_Type);
76 kinfo("Model ID=%#03lx\t Extended Model ID=%#03lx\tStepping ID=%#03lx\t", Cpu_Model_ID, Cpu_Extended_Model_ID, Cpu_Stepping_ID);
77
78 // 使用0x80000008主功能号,查询处理器支持的最大可寻址地址线宽度
79 cpu_cpuid(0x80000008, 0, &tmp_info[0], &tmp_info[1], &tmp_info[2], &tmp_info[3]);
80 Cpu_max_phys_addrline_size = tmp_info[0] & 0xff;
81 Cpu_max_linear_addrline_size = (tmp_info[0] >> 8) & 0xff;
82
83 kinfo("Cpu_max_phys_addrline_size = %d", Cpu_max_phys_addrline_size);
84 kinfo("Cpu_max_linear_addrline_size = %d", Cpu_max_linear_addrline_size);
85
86 cpu_cpuid(0x80000000, 0, &tmp_info[0], &tmp_info[1], &tmp_info[2], &tmp_info[3]);
87 Cpu_cpuid_max_Extended_mop = tmp_info[0];
88
89 kinfo("Max basic mop=%#05lx", Cpu_cpuid_max_Basic_mop);
90 kinfo("Max extended mop=%#05lx", Cpu_cpuid_max_Extended_mop);
91 return;
92 }
93
cpu_cpuid(uint32_t mop,uint32_t sop,uint32_t * eax,uint32_t * ebx,uint32_t * ecx,uint32_t * edx)94 void cpu_cpuid(uint32_t mop, uint32_t sop, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
95 {
96 // 向eax和ecx分别输入主功能号和子功能号
97 // 结果输出到eax, ebx, ecx, edx
98 __asm__ __volatile__("cpuid \n\t"
99 : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
100 : "0"(mop), "2"(sop)
101 : "memory");
102 }
103
104 /**
105 * @brief 获取当前cpu核心晶振频率(是一个Write-on-box的值)
106 *
107 * hint: 某些cpu无法提供该数据,返回值为0
108 * @return uint32_t 当前cpu核心晶振频率
109 */
cpu_get_core_crysral_freq()110 uint32_t cpu_get_core_crysral_freq()
111 {
112 uint32_t a = 0, b = 0, c = 0, d = 0;
113
114 // cpu_cpuid(0x15, 0, &a, &b, &c, &d);
115 __asm__ __volatile__("cpuid \n\t"
116 : "=a"(a), "=b"(b), "=c"(c), "=d"(d)
117 : "0"(0x15), "2"(0)
118 : "memory");
119 // kdebug("Cpu_cpuid_max_Basic_mop = %#03x, a=%ld, b=%ld, c=%ld, d=%ld", Cpu_cpuid_max_Basic_mop, a, b, c, d);
120
121 return c;
122 }
123