[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 6/6] Pass cpu speed into SM BIOS.
From: |
Gleb Natapov |
Subject: |
[Qemu-devel] [PATCH v2 6/6] Pass cpu speed into SM BIOS. |
Date: |
Mon, 25 Aug 2008 12:58:30 +0300 |
User-agent: |
StGIT/0.14.2 |
Signed-off-by: Gleb Natapov <address@hidden>
---
hw/pc.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 88 insertions(+), 0 deletions(-)
diff --git a/hw/pc.c b/hw/pc.c
index 15a86c5..27e0939 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -63,6 +63,7 @@ typedef enum _BIOSCfgEntryNum{
BIOS_CFG_SIGNATURE,
BIOS_CFG_ID,
BIOS_CFG_UUID,
+ BIOS_CFG_CPUSPEED,
BIOS_CFG_MAX_ENTRY
} BIOSCfgEntryNum;
@@ -75,6 +76,8 @@ typedef struct _BIOSCfgState {
static uint32_t bios_cfg_id = 1;
static BIOSCfgState *bios_params;
+static uint32_t cpu_speed;
+
static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
{
}
@@ -768,6 +771,89 @@ static int bios_cfg_add_data(BIOSCfgState *s,
BIOSCfgEntryNum entry,
return 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
+
/* PC hardware initialisation */
static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
const char *boot_device, DisplayState *ds,
@@ -931,6 +1017,8 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
bios_cfg_add_data(bios_params, BIOS_CFG_SIGNATURE, "QEMU", 4);
bios_cfg_add_data(bios_params, BIOS_CFG_ID, (uint8_t*)&bios_cfg_id, 4);
bios_cfg_add_data(bios_params, BIOS_CFG_UUID, qemu_uuid, 16);
+ cpu_speed = (uint32_t)get_freq(1, 1);
+ bios_cfg_add_data(bios_params, BIOS_CFG_CPUSPEED, (uint8_t*)&cpu_speed, 4);
bochs_bios_init();
- [Qemu-devel] [PATCH v2 2/6] Add -uuid command line option., (continued)
- [Qemu-devel] [PATCH v2 2/6] Add -uuid command line option., Gleb Natapov, 2008/08/25
- [Qemu-devel] [PATCH v2 3/6] Add "info uuid" command to monitor., Gleb Natapov, 2008/08/25
- [Qemu-devel] [PATCH v2 5/6] Add UUID to BIOS configuration info., Gleb Natapov, 2008/08/25
- [Qemu-devel] [PATCH v2 4/6] Use libuuid if available., Gleb Natapov, 2008/08/25
- Re: [Qemu-devel] [PATCH v2 4/6] Use libuuid if available., Andreas Färber, 2008/08/25
- Re: [Qemu-devel] [PATCH v2 4/6] Use libuuid if available., Gleb Natapov, 2008/08/25
- Re: [Qemu-devel] [PATCH v2 4/6] Use libuuid if available., Jamie Lokier, 2008/08/25
- Re: [Qemu-devel] [PATCH v2 4/6] Use libuuid if available., Andreas Färber, 2008/08/25
- Re: [Qemu-devel] [PATCH v2 4/6] Use libuuid if available., Gleb Natapov, 2008/08/25
- [Qemu-devel] Re: [PATCH v2 4/6] Use libuuid if available., Sebastian Herbszt, 2008/08/25
[Qemu-devel] [PATCH v2 6/6] Pass cpu speed into SM BIOS.,
Gleb Natapov <=
- Re: [Qemu-devel] [PATCH v2 6/6] Pass cpu speed into SM BIOS., Anthony Liguori, 2008/08/25
- Re: [Qemu-devel] [PATCH v2 6/6] Pass cpu speed into SM BIOS., Gleb Natapov, 2008/08/25
- [Qemu-devel] Re: [PATCH v2 6/6] Pass cpu speed into SM BIOS., Sebastian Herbszt, 2008/08/25
- Re: [Qemu-devel] Re: [PATCH v2 6/6] Pass cpu speed into SM BIOS., Gleb Natapov, 2008/08/26
- [Qemu-devel] Re: Re: [PATCH v2 6/6] Pass cpu speed into SM BIOS., Sebastian Herbszt, 2008/08/27
- Re: [Qemu-devel] Re: Re: [PATCH v2 6/6] Pass cpu speed into SM BIOS., Gleb Natapov, 2008/08/28
[Qemu-devel] Re: [PATCH v2 6/6] Pass cpu speed into SM BIOS., Sebastian Herbszt, 2008/08/25