]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/hpet.c
2 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 #include <mach/vm_param.h>
33 #include <mach/vm_prot.h>
34 #include <mach/machine.h>
35 #include <mach/time_value.h>
37 #include <kern/assert.h>
38 #include <kern/debug.h>
39 #include <kern/misc_protos.h>
40 #include <kern/startup.h>
41 #include <kern/clock.h>
42 #include <kern/cpu_data.h>
43 #include <kern/processor.h>
44 #include <vm/vm_page.h>
46 #include <vm/vm_kern.h>
47 #include <i386/pmap.h>
48 #include <i386/misc_protos.h>
49 #include <i386/cpuid.h>
51 #include <i386/machine_cpu.h>
52 #include <i386/machine_routines.h>
53 #include <i386/io_map_entries.h>
54 #include <architecture/i386/pio.h>
55 #include <i386/cpuid.h>
56 #include <i386/apic.h>
58 #include <i386/hpet.h>
59 #include <i386/pmCPU.h>
60 #include <pexpert/device_tree.h>
62 #include <ddb/db_aout.h>
63 #include <ddb/db_access.h>
64 #include <ddb/db_sym.h>
65 #include <ddb/db_variables.h>
66 #include <ddb/db_command.h>
67 #include <ddb/db_output.h>
68 #include <ddb/db_expr.h>
73 #define kilo (1000ULL)
74 #define Mega (kilo * kilo)
75 #define Giga (kilo * Mega)
76 #define Tera (kilo * Giga)
77 #define Peta (kilo * Tera)
79 uint32_t hpetArea
= 0;
80 uint32_t hpetAreap
= 0;
81 uint64_t hpetFemto
= 0;
82 uint64_t hpetFreq
= 0;
83 uint64_t hpetCvt
= 0; /* (TAKE OUT LATER) */
84 uint64_t hpetCvtt2n
= 0;
85 uint64_t hpetCvtn2t
= 0;
86 uint64_t tsc2hpet
= 0;
87 uint64_t hpet2tsc
= 0;
88 uint64_t bus2hpet
= 0;
89 uint64_t hpet2bus
= 0;
91 uint32_t rcbaArea
= 0;
92 uint32_t rcbaAreap
= 0;
95 #define DBG(x...) kprintf("DBG: " x)
107 * Get RCBA area physical address and map it
109 outl(cfgAdr
, lpcCfg
| (0xF0 & 0xFC));
110 rcbaAreap
= inl(cfgDat
| (0xF0 & 0x03));
111 rcbaArea
= io_map_spec(rcbaAreap
& -4096, PAGE_SIZE
* 4, VM_WIMG_IO
);
112 kprintf("RCBA: vaddr = %08X, paddr = %08X\n", rcbaArea
, rcbaAreap
);
116 * Initialize the HPET
126 * Is the HPET memory already enabled?
127 * If not, set address and enable.
129 xmod
= (uint32_t *)(rcbaArea
+ 0x3404); /* Point to the HPTC */
130 uint32_t hptc
= *xmod
; /* Get HPET config */
131 DBG(" current RCBA.HPTC: %08X\n", *xmod
);
132 if(!(hptc
& hptcAE
)) {
133 DBG("HPET memory is not enabled, "
134 "enabling and assigning to 0xFED00000 (hope that's ok)\n");
135 *xmod
= (hptc
& ~3) | hptcAE
;
139 * Get physical address of HPET and map it.
141 hpetAreap
= hpetAddr
| ((hptc
& 3) << 12);
142 hpetArea
= io_map_spec(hpetAreap
& -4096, PAGE_SIZE
* 4, VM_WIMG_IO
);
143 kprintf("HPET: vaddr = %08X, paddr = %08X\n", hpetArea
, hpetAreap
);
146 * Extract the HPET tick rate.
147 * The period of the HPET is reported in femtoseconds (10**-15s)
148 * and convert to frequency in hertz.
150 hpetFemto
= (uint32_t)(((hpetReg_t
*)hpetArea
)->GCAP_ID
>> 32);
151 hpetFreq
= (1 * Peta
) / hpetFemto
;
154 * The conversion factor is the number of nanoseconds per HPET tick
155 * with about 32 bits of fraction. The value is converted to a
156 * base-2 fixed point number. To convert from HPET to nanoseconds,
157 * multiply the value by the conversion factor using 96-bit arithmetic,
158 * then shift right 32 bits. If the value is known to be small,
159 * 64-bit arithmetic will work.
163 * Begin conversion of base 10 femtoseconds to base 2, calculate:
164 * - HPET ticks to nanoseconds conversion in base 2 fraction (* 2**32)
165 * - nanoseconds to HPET ticks conversion
167 hpetCvtt2n
= (uint64_t)hpetFemto
<< 32;
168 hpetCvtt2n
= hpetCvtt2n
/ 1000000ULL;
169 hpetCvtn2t
= 0xFFFFFFFFFFFFFFFFULL
/ hpetCvtt2n
;
170 kprintf("HPET: Frequency = %6d.%04dMHz, "
171 "cvtt2n = %08X.%08X, cvtn2t = %08X.%08X\n",
172 (uint32_t)(hpetFreq
/ Mega
), (uint32_t)(hpetFreq
% Mega
),
173 (uint32_t)(hpetCvtt2n
>> 32), (uint32_t)hpetCvtt2n
,
174 (uint32_t)(hpetCvtn2t
>> 32), (uint32_t)hpetCvtn2t
);
178 * Begin conversion of base 10 femtoseconds to base 2
179 * HPET ticks to nanoseconds in base 2 fraction (times 1048576)
181 hpetCvt
= (uint64_t)hpetFemto
<< 20;
182 hpetCvt
= hpetCvt
/ 1000000ULL;
184 /* Calculate conversion from TSC to HPET */
185 tsc2hpet
= tmrCvt(tscFCvtt2n
, hpetCvtn2t
);
186 DBG(" CVT: TSC to HPET = %08X.%08X\n",
187 (uint32_t)(tsc2hpet
>> 32), (uint32_t)tsc2hpet
);
189 /* Calculate conversion from HPET to TSC */
190 hpet2tsc
= tmrCvt(hpetCvtt2n
, tscFCvtn2t
);
191 DBG(" CVT: HPET to TSC = %08X.%08X\n",
192 (uint32_t)(hpet2tsc
>> 32), (uint32_t)hpet2tsc
);
194 /* Calculate conversion from BUS to HPET */
195 bus2hpet
= tmrCvt(busFCvtt2n
, hpetCvtn2t
);
196 DBG(" CVT: BUS to HPET = %08X.%08X\n",
197 (uint32_t)(bus2hpet
>> 32), (uint32_t)bus2hpet
);
199 /* Calculate conversion from HPET to BUS */
200 hpet2bus
= tmrCvt(hpetCvtt2n
, busFCvtn2t
);
201 DBG(" CVT: HPET to BUS = %08X.%08X\n",
202 (uint32_t)(hpet2bus
>> 32), (uint32_t)hpet2bus
);
204 /* Make sure the counter is off in the HPET configuration flags */
205 uint64_t hpetcon
= ((hpetReg_t
*)hpetArea
)->GEN_CONF
;
206 hpetcon
= hpetcon
& ~1;
207 ((hpetReg_t
*)hpetArea
)->GEN_CONF
= hpetcon
;
210 * Convert current TSC to HPET value,
211 * set it, and start it ticking.
213 uint64_t currtsc
= rdtsc64();
214 uint64_t tscInHPET
= tmrCvt(currtsc
, tsc2hpet
);
215 ((hpetReg_t
*)hpetArea
)->MAIN_CNT
= tscInHPET
;
216 hpetcon
= hpetcon
| 1;
217 ((hpetReg_t
*)hpetArea
)->GEN_CONF
= hpetcon
;
218 kprintf("HPET started: TSC = %08X.%08X, HPET = %08X.%08X\n",
219 (uint32_t)(currtsc
>> 32), (uint32_t)currtsc
,
220 (uint32_t)(tscInHPET
>> 32), (uint32_t)tscInHPET
);
223 db_display_hpet((hpetReg_t
*)hpetArea
); /* (BRINGUP) */
228 * This routine is used to get various information about the HPET
229 * without having to export gobs of globals. It fills in a data
230 * structure with the info.
233 hpet_get_info(hpetInfo_t
*info
)
235 info
->hpetCvtt2n
= hpetCvtt2n
;
236 info
->hpetCvtn2t
= hpetCvtn2t
;
237 info
->tsc2hpet
= tsc2hpet
;
238 info
->hpet2tsc
= hpet2tsc
;
239 info
->bus2hpet
= bus2hpet
;
240 info
->hpet2bus
= hpet2bus
;
241 info
->rcbaArea
= rcbaArea
;
242 info
->rcbaAreap
= rcbaAreap
;
247 * This routine is called by the HPET driver
248 * when it assigns an HPET timer to a processor
252 ml_hpet_cfg(uint32_t cpu
, uint32_t hpetVect
)
258 panic("ml_hpet_cfg: invalid cpu = %d\n", cpu
);
261 /* Calculate address of the HPET for this processor */
262 hpetVaddr
= (uint64_t *)(((uint32_t)&(((hpetReg_t
*)hpetArea
)->TIM1_CONF
)) + (cpu
<< 5));
264 DBG("ml_hpet_cfg: HPET for cpu %d at %08X, vector = %d\n",
265 cpu
, hpetVaddr
, hpetVect
);
267 /* Save the address and vector of the HPET for this processor */
268 cpu_data_ptr
[cpu
]->cpu_pmHpet
= (uint64_t *)hpetVaddr
;
269 cpu_data_ptr
[cpu
]->cpu_pmHpetVec
= hpetVect
;
271 /* Enable the interruptions now that we have a vector */
272 hpetcnf
= *hpetVaddr
;
273 hpetcnf
= hpetcnf
| Tn_INT_ENB_CNF
;
274 *hpetVaddr
= hpetcnf
;
276 /* Save the configuration */
277 cpu_data_ptr
[cpu
]->cpu_pmStats
.pmHpetCfg
= hpetcnf
;
278 cpu_data_ptr
[cpu
]->cpu_pmStats
.pmHpetCmp
= 0;
280 /* See if nap policy has changed now */
281 machine_nap_policy();
286 * This is the HPET interrupt handler.
288 * We really don't want to be here, but so far, I haven't figured out
289 * a way to cancel the interrupt. Hopefully, some day we will figure out
290 * how to do that or switch all timers to the HPET.
296 /* All we do here is to bump the count */
297 current_cpu_datap()->cpu_pmStats
.pmHPETRupt
++;
299 /* Return and show that the 'rupt has been handled... */
304 static hpetReg_t saved_hpet
;
306 void hpet_save( void )
308 hpetReg_t
*from
= (hpetReg_t
*) hpetArea
;
309 hpetReg_t
*to
= &saved_hpet
;
311 to
->GEN_CONF
= from
->GEN_CONF
;
312 to
->TIM0_CONF
= from
->TIM0_CONF
;
313 to
->TIM0_COMP
= from
->TIM0_COMP
;
314 to
->TIM1_CONF
= from
->TIM1_CONF
;
315 to
->TIM1_COMP
= from
->TIM1_COMP
;
316 to
->TIM2_CONF
= from
->TIM2_CONF
;
317 to
->TIM2_COMP
= from
->TIM2_COMP
;
318 to
->MAIN_CNT
= from
->MAIN_CNT
;
321 void hpet_restore( void )
323 hpetReg_t
*from
= &saved_hpet
;
324 hpetReg_t
*to
= (hpetReg_t
*) hpetArea
;
327 * Is the HPET memory already enabled?
328 * If not, set address and enable.
330 uint32_t *hptcp
= (uint32_t *)(rcbaArea
+ 0x3404);
331 uint32_t hptc
= *hptcp
;
332 if(!(hptc
& hptcAE
)) {
333 DBG("HPET memory is not enabled, "
334 "enabling and assigning to 0xFED00000 (hope that's ok)\n");
335 *hptcp
= (hptc
& ~3) | hptcAE
;
338 to
->GEN_CONF
= from
->GEN_CONF
& ~1;
340 to
->TIM0_CONF
= from
->TIM0_CONF
;
341 to
->TIM0_COMP
= from
->TIM0_COMP
;
342 to
->TIM1_CONF
= from
->TIM1_CONF
;
343 to
->TIM1_COMP
= from
->TIM1_COMP
;
344 to
->TIM2_CONF
= from
->TIM2_CONF
;
345 to
->TIM2_COMP
= from
->TIM2_COMP
;
346 to
->GINTR_STA
= -1ULL;
347 to
->MAIN_CNT
= from
->MAIN_CNT
;
349 to
->GEN_CONF
= from
->GEN_CONF
;
353 * Read the HPET timer
359 hpetReg_t
*hpetp
= (hpetReg_t
*) hpetArea
;
360 volatile uint32_t *regp
= (uint32_t *) &hpetp
->MAIN_CNT
;
367 } while (high
!= *(regp
+ 1));
369 return (((uint64_t) high
) << 32) | low
;
374 #define HI32(x) ((uint32_t)(((x) >> 32) & 0xFFFFFFFF))
375 #define LO32(x) ((uint32_t)((x) & 0xFFFFFFFF))
378 * Displays HPET memory mapped area
382 db_hpet(__unused db_expr_t addr
, __unused
int have_addr
, __unused db_expr_t count
, __unused
char *modif
)
385 db_display_hpet((hpetReg_t
*) hpetArea
); /* Dump out the HPET
391 db_display_hpet(hpetReg_t
* hpt
)
396 cmain
= hpt
->MAIN_CNT
; /* Get the main timer */
398 /* General capabilities */
399 db_printf(" GCAP_ID = %08X.%08X\n",
400 HI32(hpt
->GCAP_ID
), LO32(hpt
->GCAP_ID
));
401 /* General configuration */
402 db_printf(" GEN_CONF = %08X.%08X\n",
403 HI32(hpt
->GEN_CONF
), LO32(hpt
->GEN_CONF
));
404 /* General Interrupt status */
405 db_printf("GINTR_STA = %08X.%08X\n",
406 HI32(hpt
->GINTR_STA
), LO32(hpt
->GINTR_STA
));
408 db_printf(" MAIN_CNT = %08X.%08X\n",
409 HI32(cmain
), LO32(cmain
));
410 /* Timer 0 config and cap */
411 db_printf("TIM0_CONF = %08X.%08X\n",
412 HI32(hpt
->TIM0_CONF
), LO32(hpt
->TIM0_CONF
));
413 /* Timer 0 comparator */
414 db_printf("TIM0_COMP = %08X.%08X\n",
415 HI32(hpt
->TIM0_COMP
), LO32(hpt
->TIM0_COMP
));
416 /* Timer 1 config and cap */
417 db_printf("TIM0_CONF = %08X.%08X\n",
418 HI32(hpt
->TIM1_CONF
), LO32(hpt
->TIM1_CONF
));
419 /* Timer 1 comparator */
420 db_printf("TIM1_COMP = %08X.%08X\n",
421 HI32(hpt
->TIM1_COMP
), LO32(hpt
->TIM1_COMP
));
422 /* Timer 2 config and cap */
423 db_printf("TIM2_CONF = %08X.%08X\n",
424 HI32(hpt
->TIM2_CONF
), LO32(hpt
->TIM2_CONF
));
425 /* Timer 2 comparator */
426 db_printf("TIM2_COMP = %08X.%08X\n",
427 HI32(hpt
->TIM2_COMP
), LO32(hpt
->TIM2_COMP
));
429 db_printf("\nHPET Frequency = %d.%05dMHz\n",
430 (uint32_t) (hpetFreq
/ 1000000), (uint32_t) (hpetFreq
% 1000000));