qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v4 6/8] Pass cpu speed into SM BIOS.


From: Gleb Natapov
Subject: Re: [Qemu-devel] [PATCH v4 6/8] Pass cpu speed into SM BIOS.
Date: Mon, 1 Sep 2008 10:46:20 +0300

Sorry, use this one instead.

---
    Signed-off-by: Gleb Natapov <address@hidden>

diff --git a/hw/pc.c b/hw/pc.c
index 933e936..aae9fc6 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -49,6 +49,8 @@
 
 #define MAX_IDE_BUS 2
 
+#define FW_CFG_PC_CPUSPEED (FW_CFG_ARCH_LOCAL + 0x00)
+
 static fdctrl_t *floppy_controller;
 static RTCState *rtc_state;
 static PITState *pit;
@@ -369,6 +371,89 @@ static uint32_t ioport92_read(void *opaque, uint32_t addr)
     return ioport_get_a20() << 1;
 }
 
+#ifdef __linux__
+/* get_freq () function is taken from conky source code */
+#define CPUFREQ_PREFIX "/sys/devices/system/cpu"
+#define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
+
+/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
+static double get_freq(int divisor, unsigned int cpu)
+{
+       FILE *f;
+       char frequency[32];
+       char s[256];
+       double freq = 0;
+
+       if (divisor <= 0)
+               return 0;
+
+       snprintf(s, 256, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu - 1, 
CPUFREQ_POSTFIX);
+       f = fopen(s, "r");
+       if (f) {
+               /* if there's a cpufreq /sys node, read the current frequency 
from
+                * this node and divide by 1000 to get Mhz. */
+               if (fgets(s, sizeof(s), f)) {
+                       s[strlen(s) - 1] = '\0';
+                       freq = strtod(s, NULL);
+               }
+               fclose(f);
+               return (freq / 1000) / divisor;
+       }
+
+       // open the CPU information file
+       f = fopen("/proc/cpuinfo", "r");
+       if (!f) {
+               perror("Failed to access '/proc/cpuinfo' at get_freq()");
+               return 0;
+       }
+
+       // read the file
+       while (fgets(s, sizeof(s), f) != NULL) {
+
+#if defined(__i386) || defined(__x86_64)
+               // and search for the cpu mhz
+               if (strncmp(s, "cpu MHz", 7) == 0 && cpu == 0) {
+#else
+#if defined(__alpha)
+               // different on alpha
+               if (strncmp(s, "cycle frequency [Hz]", 20) == 0 && cpu == 0) {
+#else
+               // this is different on ppc for some reason
+               if (strncmp(s, "clock", 5) == 0 && cpu == 0) {
+#endif // defined(__alpha)
+#endif // defined(__i386) || defined(__x86_64)
+
+                       // copy just the number
+                       strcpy(frequency, strchr(s, ':') + 2);
+#if defined(__alpha)
+                       // strip " est.\n"
+                       frequency[strlen(frequency) - 6] = '\0';
+                       // kernel reports in Hz
+                       freq = strtod(frequency, NULL) / 1000000;
+#else
+                       // strip \n
+                       frequency[strlen(frequency) - 1] = '\0';
+                       freq = strtod(frequency, NULL);
+#endif
+                       break;
+               }
+               if (strncmp(s, "processor", 9) == 0) {
+                       cpu--;
+                       continue;
+               }
+       }
+
+       fclose(f);
+       return freq / divisor;
+}
+#else
+static double get_freq(int divisor, unsigned int cpu)
+{
+    return 0;
+}
+#endif
+
+
 /***********************************************************/
 /* Bochs BIOS debug ports */
 
@@ -418,6 +503,7 @@ static void bochs_bios_write(void *opaque, uint32_t addr, 
uint32_t val)
 
 static void bochs_bios_init(void)
 {
+    uint16_t cpu_speed;
     void *fw_cfg;
 
     register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
@@ -433,6 +519,8 @@ static void bochs_bios_init(void)
 
     fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, 0);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
+    cpu_speed = (uint16_t)get_freq(1, 1);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_PC_CPUSPEED, cpu_speed);
 }
 
 /* Generate an initial boot sector which sets state and jump to
--
                        Gleb.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]