Debian Codegraph @

kernel/drivers/processor.c

raw
1 /*
2 * libcaca Colour ASCII-Art library
3 * Copyright (c) 2006 Sam Hocevar <[email protected]>
4 * 2009 Jean-Yves Lamoureux <[email protected]>
5 * All Rights Reserved
6 *
7 * This library is free software. It comes without any warranty, to
8 * the extent permitted by applicable law. You can redistribute it
9 * and/or modify it under the terms of the Do What the Fuck You Want
10 * to Public License, Version 2, as published by Sam Hocevar. See
11 * http://www.wtfpl.net/ for more details.
12 */
13
14 #include "kernel.h"
15 #include "klibc.h"
16 #include "drivers/timer.h"
17 #include "processor.h"
18
19
20 int processor_get_info(struct processor_info *processor_info)
21 {
22 processor_info->id = 0;
23
24 /* Vendor String */
25 int code = CPUID_GETVENDORSTRING;
26 unsigned int where[5];
27 char *s;
28 asm volatile ("cpuid":"=a" (*where), "=b"(*(where + 1)),
29 "=c"(*(where + 2)), "=d"(*(where + 3)):"0"(code));
30
31 s = (char *)where;
32
33 unsigned int a = 0;
34 unsigned int b = where[1];
35 unsigned int d = where[3];
36 unsigned int c = where[2];
37
38 where[0] = b;
39 where[1] = d;
40 where[2] = c;
41 where[3] = 0;
42
43 memcpy(processor_info->vendor, where, 13);
44
45 /* Features */
46 code = CPUID_GETFEATURES;
47 asm volatile ("cpuid":"=a" (a), "=d"(d):"0"(code):"ecx", "ebx");
48 processor_info->features = a;
49
50 processor_info->frequency = 0;
51 processor_info->frequency = processor_get_frequency(processor_info);
52
53 return 0;
54 }
55
56 u32 processor_get_frequency(struct processor_info * processor_info)
57 {
58 if (processor_info->frequency)
59 return processor_info->frequency;
60 u64 srdtsc64, erdtsc64;
61 u32 srdtsc_l, srdtsc_h;
62 u32 erdtsc_l, erdtsc_h;
63
64 rdtsc(srdtsc_l, srdtsc_h); /* Get RDTSC */
65 sleep(2); /* Sleep for 2 seconds */
66 rdtsc(erdtsc_l, erdtsc_h); /* Get RDTSC again */
67
68 srdtsc64 = srdtsc_h;
69 srdtsc64 <<= 32;
70 srdtsc64 |= srdtsc_l;
71 erdtsc64 = erdtsc_h;
72 erdtsc64 <<= 32;
73 erdtsc64 |= erdtsc_l;
74
75
76 u32 diff = erdtsc64 - srdtsc64; /* Cycle count for 2 seconds */
77 diff /= 2; /* Divide by 2 to get cycles per sec */
78 return diff;
79 }
80
81 void processor_print_info(struct processor_info *processor_info)
82 {
83 printf("CPU%d\n", processor_info->id);
84 printf("Vendor ID : %s\n", processor_info->vendor);
85 printf("Frequency : ");
86 if (processor_info->frequency > 1000000000)
87 {
88 printf("%dGhz",
89 processor_info->frequency / 1000000000);
90 }
91 else if (processor_info->frequency > 1000000)
92 {
93 printf("%dMhz",
94 processor_info->frequency / 1000000);
95 }
96 else if (processor_info->frequency > 1000)
97 {
98 printf("%dKhz\n",
99 processor_info->frequency / 1000);
100 }
101 else
102 {
103 printf("%dhz\n",
104 processor_info->frequency);
105 }
106 printf(" (or something like that)\n");
107 printf("Features : 0x%x\n", processor_info->features);
108 }