From: Gleb Natapov <gleb@qumranet.com>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v4 6/8] Pass cpu speed into SM BIOS.
Date: Mon, 1 Sep 2008 10:46:20 +0300 [thread overview]
Message-ID: <20080901074620.GK6192@minantech.com> (raw)
In-Reply-To: <20080901072601.17854.43871.stgit@gleb-debian.qumranet.com.qumranet.com>
Sorry, use this one instead.
---
Signed-off-by: Gleb Natapov <gleb@qumranet.com>
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.
next prev parent reply other threads:[~2008-09-01 7:46 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-01 7:25 [Qemu-devel] [PATCH v4 0/8] Add new firmware configuration mechanism Gleb Natapov
2008-09-01 7:25 ` [Qemu-devel] [PATCH v4 1/8] Key/value based qemu<->guest firmware communication mechanism Gleb Natapov
2008-09-01 7:44 ` Gleb Natapov
2008-09-01 7:25 ` [Qemu-devel] [PATCH v4 2/8] Add -uuid command line option Gleb Natapov
2008-09-01 7:25 ` [Qemu-devel] [PATCH v4 3/8] Add "info uuid" command to monitor Gleb Natapov
2008-09-01 7:25 ` [Qemu-devel] [PATCH v4 4/8] Use libuuid if available Gleb Natapov
2008-09-01 14:25 ` Blue Swirl
2008-09-01 14:49 ` Andreas Färber
2008-09-01 15:15 ` Tomas Carnecky
2008-09-01 16:29 ` M. Warner Losh
2008-09-01 15:30 ` Jamie Lokier
2008-09-01 16:32 ` Andreas Färber
2008-09-01 15:50 ` Andreas Schwab
2008-09-01 16:29 ` M. Warner Losh
2008-09-01 7:25 ` [Qemu-devel] [PATCH v4 5/8] Add UUID to firmware configuration info Gleb Natapov
2008-09-01 7:26 ` [Qemu-devel] [PATCH v4 6/8] Pass cpu speed into SM BIOS Gleb Natapov
2008-09-01 7:46 ` Gleb Natapov [this message]
2008-09-01 7:26 ` [Qemu-devel] [PATCH v4 7/8] Add common keys to firmware configuration Gleb Natapov
2008-09-01 7:26 ` [Qemu-devel] [PATCH v4 8/8] Add sparc " Gleb Natapov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080901074620.GK6192@minantech.com \
--to=gleb@qumranet.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.