The device tree node for the cpu is supposed to have a "clocks"
property that references its clock.  Using the generic clock framework
code we can get the frequency of that clock and report it.

Note that some device trees are incomplete and won't have a "clocks"
property.  In that case hw.cpuspeed will not be available unless a
SoC-specific driver overrides cpu_cpuspeed.

ok?

P.S. I'm planning an equivalent diff for armv7.


Index: arch/arm64/arm64/cpu.c
===================================================================
RCS file: /cvs/src/sys/arch/arm64/arm64/cpu.c,v
retrieving revision 1.7
diff -u -p -r1.7 cpu.c
--- arch/arm64/arm64/cpu.c      20 Aug 2017 04:22:57 -0000      1.7
+++ arch/arm64/arm64/cpu.c      23 Dec 2017 15:12:17 -0000
@@ -20,9 +20,12 @@
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/device.h>
+#include <sys/sysctl.h>
+
 #include <machine/fdt.h>
 
 #include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_clock.h>
 #include <dev/ofw/fdt.h>
 
 /* CPU Identification */
@@ -87,6 +90,7 @@ const struct implementers {
 };
 
 char cpu_model[64];
+int cpu_node;
 
 int    cpu_match(struct device *, void *, void *);
 void   cpu_attach(struct device *, struct device *, void *);
@@ -143,6 +147,8 @@ cpu_identify(struct cpu_info *ci)
        }
 }
 
+int    cpu_clockspeed(int *);
+
 int
 cpu_match(struct device *parent, void *cfdata, void *aux)
 {
@@ -172,9 +178,21 @@ cpu_attach(struct device *parent, struct
 
                printf(":");
                cpu_identify(ci);
+               
+               if (OF_getproplen(faa->fa_node, "clocks") > 0) {
+                       cpu_node = faa->fa_node;
+                       cpu_cpuspeed = cpu_clockspeed;
+               }
        } else {
                printf(": not configured");
        }
 
        printf("\n");
+}
+
+int
+cpu_clockspeed(int *freq)
+{
+       *freq = clock_get_frequency(cpu_node, NULL) / 1000000;
+       return 0;
 }

Reply via email to