Add an implementation of cpu_est_clockrate(9). This function estimates the

current clock frequency for the given CPU id in units of Hz.
This commit is contained in:
Nate Lawson 2005-02-04 05:32:56 +00:00
parent a07c38209c
commit 4c4381e288
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=141237
8 changed files with 155 additions and 16 deletions

View file

@ -98,6 +98,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/eventhandler.h>
#include <sys/imgact.h>
#include <sys/kdb.h>
@ -1718,6 +1719,14 @@ cpu_boot(int howto)
{
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
return (ENXIO);
}
/*
* Shutdown the CPU as much as possible
*/

View file

@ -56,8 +56,12 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/signalvar.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/cpu.h>
#include <sys/eventhandler.h>
#include <sys/imgact.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
@ -69,19 +73,17 @@ __FBSDID("$FreeBSD$");
#include <sys/mutex.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/callout.h>
#include <sys/msgbuf.h>
#include <sys/sched.h>
#include <sys/signalvar.h>
#include <sys/sysent.h>
#include <sys/sysctl.h>
#include <sys/sysproto.h>
#include <sys/ucontext.h>
#include <sys/vmmeter.h>
#include <sys/bus.h>
#include <sys/eventhandler.h>
#include <machine/clock.h>
#include <machine/pcb.h>
#include <vm/vm.h>
@ -450,6 +452,44 @@ cpu_boot(int howto)
{
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
uint64_t tsc1, tsc2;
if (pcpu_find(cpu_id) == NULL || rate == NULL)
return (EINVAL);
/* If we're booting, trust the rate calibrated moments ago. */
if (cold) {
*rate = tsc_freq;
return (0);
}
#ifdef SMP
/* Schedule ourselves on the indicated cpu. */
mtx_lock_spin(&sched_lock);
sched_bind(curthread, cpu_id);
mtx_unlock_spin(&sched_lock);
#endif
/* Calibrate by measuring a short delay. */
tsc1 = rdtsc();
DELAY(1000);
tsc2 = rdtsc();
#ifdef SMP
mtx_lock_spin(&sched_lock);
sched_unbind(curthread);
mtx_unlock_spin(&sched_lock);
#endif
tsc_freq = (tsc2 - tsc1) * 1000;
*rate = tsc_freq;
return (0);
}
/*
* Shutdown the CPU as much as possible
*/

View file

@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/sysproto.h>
#include <sys/signalvar.h>
#include <sys/imgact.h>
@ -236,6 +237,14 @@ cpu_startup(void *dummy)
SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
return (ENXIO);
}
void
cpu_idle(void)
{

View file

@ -56,8 +56,12 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/signalvar.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/cpu.h>
#include <sys/eventhandler.h>
#include <sys/imgact.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
@ -66,21 +70,18 @@ __FBSDID("$FreeBSD$");
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/memrange.h>
#include <sys/msgbuf.h>
#include <sys/mutex.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/callout.h>
#include <sys/msgbuf.h>
#include <sys/sched.h>
#include <sys/sysent.h>
#include <sys/signalvar.h>
#include <sys/sysctl.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/ucontext.h>
#include <sys/vmmeter.h>
#include <sys/bus.h>
#include <sys/eventhandler.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
@ -104,6 +105,7 @@ __FBSDID("$FreeBSD$");
#include <net/netisr.h>
#include <machine/clock.h>
#include <machine/cpu.h>
#include <machine/cputypes.h>
#include <machine/reg.h>
@ -1022,6 +1024,46 @@ cpu_boot(int howto)
{
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
uint64_t tsc1, tsc2;
if (pcpu_find(cpu_id) == NULL || rate == NULL)
return (EINVAL);
if (!tsc_present)
return (EOPNOTSUPP);
/* If we're booting, trust the rate calibrated moments ago. */
if (cold) {
*rate = tsc_freq;
return (0);
}
#ifdef SMP
/* Schedule ourselves on the indicated cpu. */
mtx_lock_spin(&sched_lock);
sched_bind(curthread, cpu_id);
mtx_unlock_spin(&sched_lock);
#endif
/* Calibrate by measuring a short delay. */
tsc1 = rdtsc();
DELAY(1000);
tsc2 = rdtsc();
#ifdef SMP
mtx_lock_spin(&sched_lock);
sched_unbind(curthread);
mtx_unlock_spin(&sched_lock);
#endif
tsc_freq = (tsc2 - tsc1) * 1000;
*rate = tsc_freq;
return (0);
}
/*
* Shutdown the CPU as much as possible
*/

View file

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/eventhandler.h>
#include <sys/kdb.h>
#include <sys/sysproto.h>
@ -287,6 +288,17 @@ cpu_boot(int howto)
efi_reset_system();
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
if (pcpu_find(cpu_id) == NULL || rate == NULL)
return (EINVAL);
*rate = processor_frequency;
return (0);
}
void
cpu_halt()
{

View file

@ -64,6 +64,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/kdb.h>
#include <sys/eventhandler.h>
#include <sys/imgact.h>
@ -700,6 +701,14 @@ cpu_boot(int howto)
{
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
return (ENXIO);
}
/*
* Shutdown the CPU as much as possible.
*/

View file

@ -64,6 +64,7 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/kdb.h>
#include <sys/eventhandler.h>
#include <sys/imgact.h>
@ -700,6 +701,14 @@ cpu_boot(int howto)
{
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
return (ENXIO);
}
/*
* Shutdown the CPU as much as possible.
*/

View file

@ -44,6 +44,7 @@
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cons.h>
#include <sys/cpu.h>
#include <sys/imgact.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
@ -669,6 +670,14 @@ cpu_shutdown(void *args)
openfirmware_exit(args);
}
/* Get current clock frequency for the given cpu id. */
int
cpu_est_clockrate(int cpu_id, uint64_t *rate)
{
return (ENXIO);
}
/*
* Duplicate OF_exit() with a different firmware call function that restores
* the trap table, otherwise a RED state exception is triggered in at least