2 /* i386-specific clock functions. */
4 #include <machine/ports.h>
5 #include <minix/portio.h>
7 #include "kernel/kernel.h"
9 #include "kernel/clock.h"
10 #include "kernel/interrupt.h"
11 #include <minix/u64.h>
13 #include "kernel/profile.h"
20 #include "kernel/spinlock.h"
23 #include "kernel/smp.h"
26 #define CLOCK_ACK_BIT 0x80 /* PS/2 clock interrupt acknowledge bit */
28 /* Clock parameters. */
29 #define COUNTER_FREQ (2*TIMER_FREQ) /* counter frequency using square wave */
30 #define LATCH_COUNT 0x00 /* cc00xxxx, c = channel, x = any */
31 #define SQUARE_WAVE 0x36 /* ccaammmb, a = access, m = mode, b = BCD */
32 /* 11x11, 11 = LSB then MSB, x11 = sq wave */
33 #define TIMER_FREQ 1193182 /* clock frequency for timer in PC and AT */
34 #define TIMER_COUNT(freq) (TIMER_FREQ/(freq)) /* initial value for counter*/
36 static irq_hook_t pic_timer_hook
; /* interrupt handler hook */
38 static unsigned probe_ticks
;
39 static u64_t tsc0
, tsc1
;
40 #define PROBE_TICKS (system_hz / 10)
42 static unsigned tsc_per_ms
[CONFIG_MAX_CPUS
];
44 /*===========================================================================*
46 *===========================================================================*/
47 int init_8253A_timer(const unsigned freq
)
49 /* Initialize channel 0 of the 8253A timer to, e.g., 60 Hz,
50 * and register the CLOCK task's interrupt handler to be run
51 * on every clock tick.
53 outb(TIMER_MODE
, SQUARE_WAVE
); /* run continuously */
54 outb(TIMER0
, (TIMER_COUNT(freq
) & 0xff)); /* timer low byte */
55 outb(TIMER0
, TIMER_COUNT(freq
) >> 8); /* timer high byte */
60 /*===========================================================================*
62 *===========================================================================*/
63 void stop_8253A_timer(void)
65 /* Reset the clock to the BIOS rate. (For rebooting.) */
66 outb(TIMER_MODE
, 0x36);
71 void arch_timer_int_handler(void)
75 static int calib_cpu_handler(irq_hook_t
* UNUSED(hook
))
83 if (probe_ticks
== 1) {
86 else if (probe_ticks
== PROBE_TICKS
) {
90 /* just in case we are in an SMP single cpu fallback mode */
95 static void estimate_cpu_freq(void)
100 irq_hook_t calib_cpu
;
102 /* set the probe, we use the legacy timer, IRQ 0 */
103 put_irq_handler(&calib_cpu
, CLOCK_IRQ
, calib_cpu_handler
);
105 /* just in case we are in an SMP single cpu fallback mode */
107 /* set the PIC timer to get some time */
110 /* loop for some time to get a sample */
111 while(probe_ticks
< PROBE_TICKS
) {
116 /* just in case we are in an SMP single cpu fallback mode */
119 /* remove the probe */
120 rm_irq_handler(&calib_cpu
);
122 tsc_delta
= tsc1
- tsc0
;
124 cpu_freq
= (tsc_delta
/ (PROBE_TICKS
- 1)) * system_hz
;
125 cpu_set_freq(cpuid
, cpu_freq
);
126 cpu_info
[cpuid
].freq
= (unsigned long)(cpu_freq
/ 1000000);
127 BOOT_VERBOSE(cpu_print_freq(cpuid
));
130 int init_local_timer(unsigned freq
)
133 /* if we know the address, lapic is enabled and we should use it */
135 unsigned cpu
= cpuid
;
136 tsc_per_ms
[cpu
] = (unsigned long)(cpu_get_freq(cpu
) / 1000);
137 lapic_set_timer_one_shot(1000000 / system_hz
);
139 BOOT_VERBOSE(printf("Initiating legacy i8253 timer\n"));
143 init_8253A_timer(freq
);
145 /* always only 1 cpu in the system */
146 tsc_per_ms
[0] = (unsigned long)(cpu_get_freq(0) / 1000);
152 void stop_local_timer(void)
165 void restart_local_timer(void)
169 lapic_restart_timer();
174 int register_local_timer_handler(const irq_handler_t handler
)
178 /* Using APIC, it is configured in apic_idt_init() */
179 BOOT_VERBOSE(printf("Using LAPIC timer as tick source\n"));
183 /* Using PIC, Initialize the CLOCK's interrupt hook. */
184 pic_timer_hook
.proc_nr_e
= NONE
;
185 pic_timer_hook
.irq
= CLOCK_IRQ
;
187 put_irq_handler(&pic_timer_hook
, CLOCK_IRQ
, handler
);
193 void cycles_accounting_init(void)
196 unsigned cpu
= cpuid
;
199 read_tsc_64(get_cpu_var_ptr(cpu
, tsc_ctr_switch
));
201 get_cpu_var(cpu
, cpu_last_tsc
) = 0;
202 get_cpu_var(cpu
, cpu_last_idle
) = 0;
205 void context_stop(struct proc
* p
)
207 u64_t tsc
, tsc_delta
;
208 u64_t
* __tsc_ctr_switch
= get_cpulocal_var_ptr(tsc_ctr_switch
);
210 unsigned cpu
= cpuid
;
211 int must_bkl_unlock
= 0;
214 * This function is called only if we switch from kernel to user or idle
215 * or back. Therefore this is a perfect location to place the big kernel
216 * lock which will hopefully disappear soon.
218 * If we stop accounting for KERNEL we must unlock the BKL. If account
219 * for IDLE we must not hold the lock
221 if (p
== proc_addr(KERNEL
)) {
225 tmp
= tsc
- *__tsc_ctr_switch
;
226 kernel_ticks
[cpu
] = kernel_ticks
[cpu
] + tmp
;
227 p
->p_cycles
= p
->p_cycles
+ tmp
;
233 read_tsc_64(&bkl_tsc
);
234 /* this only gives a good estimate */
235 succ
= big_kernel_lock
.val
;
241 bkl_ticks
[cpu
] = bkl_ticks
[cpu
] + tsc
- bkl_tsc
;
243 bkl_succ
[cpu
] += !(!(succ
== 0));
245 p
->p_cycles
= p
->p_cycles
+ tsc
- *__tsc_ctr_switch
;
249 * Since at the time we got a scheduling IPI we might have been
250 * waiting for BKL already, we may miss it due to a similar IPI to
251 * the cpu which is already waiting for us to handle its. This
252 * results in a live-lock of these two cpus.
254 * Therefore we always check if there is one pending and if so,
255 * we handle it straight away so the other cpu can continue and
256 * we do not deadlock.
263 p
->p_cycles
= p
->p_cycles
+ tsc
- *__tsc_ctr_switch
;
266 tsc_delta
= tsc
- *__tsc_ctr_switch
;
269 kbill_ipc
->p_kipc_cycles
=
270 kbill_ipc
->p_kipc_cycles
+ tsc_delta
;
275 kbill_kcall
->p_kcall_cycles
=
276 kbill_kcall
->p_kcall_cycles
+ tsc_delta
;
281 * deduct the just consumed cpu cycles from the cpu time left for this
282 * process during its current quantum. Skip IDLE and other pseudo kernel
285 if (p
->p_endpoint
>= 0) {
287 p
->p_cpu_time_left
= 0;
289 /* if (tsc_delta < p->p_cpu_time_left) in 64bit */
290 if (ex64hi(tsc_delta
) < ex64hi(p
->p_cpu_time_left
) ||
291 (ex64hi(tsc_delta
) == ex64hi(p
->p_cpu_time_left
) &&
292 ex64lo(tsc_delta
) < ex64lo(p
->p_cpu_time_left
)))
293 p
->p_cpu_time_left
= p
->p_cpu_time_left
- tsc_delta
;
295 p
->p_cpu_time_left
= 0;
300 *__tsc_ctr_switch
= tsc
;
303 if(must_bkl_unlock
) {
309 void context_stop_idle(void)
313 unsigned cpu
= cpuid
;
316 is_idle
= get_cpu_var(cpu
, cpu_is_idle
);
317 get_cpu_var(cpu
, cpu_is_idle
) = 0;
319 context_stop(get_cpulocal_var_ptr(idle_proc
));
322 restart_local_timer();
325 get_cpulocal_var(idle_interrupted
) = 1;
329 u64_t
ms_2_cpu_time(unsigned ms
)
331 return (u64_t
)tsc_per_ms
[cpuid
] * ms
;
334 unsigned cpu_time_2_ms(u64_t cpu_time
)
336 return (unsigned long)(cpu_time
/ tsc_per_ms
[cpuid
]);
341 u64_t current_tsc
, *current_idle
;
342 u64_t tsc_delta
, idle_delta
, busy
;
346 unsigned cpu
= cpuid
;
349 u64_t
*last_tsc
, *last_idle
;
351 last_tsc
= get_cpu_var_ptr(cpu
, cpu_last_tsc
);
352 last_idle
= get_cpu_var_ptr(cpu
, cpu_last_idle
);
354 idle
= get_cpu_var_ptr(cpu
, idle_proc
);;
355 read_tsc_64(¤t_tsc
);
356 current_idle
= &idle
->p_cycles
; /* ptr to idle proc */
358 /* calculate load since last cpu_load invocation */
360 tsc_delta
= current_tsc
- *last_tsc
;
361 idle_delta
= *current_idle
- *last_idle
;
363 busy
= tsc_delta
- idle_delta
;
365 load
= ex64lo(busy
/ tsc_delta
);
372 *last_tsc
= current_tsc
;
373 *last_idle
= *current_idle
;
377 void busy_delay_ms(int ms
)
379 u64_t cycles
= ms_2_cpu_time(ms
), tsc0
, tsc
, tsc1
;
381 tsc1
= tsc0
+ cycles
;
382 do { read_tsc_64(&tsc
); } while(tsc
< tsc1
);