2 * Copyright (c) 2007-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <machine/asm.h>
30 #include <arm/proc_reg.h>
32 #include <sys/errno.h>
36 .globl EXT(machine_set_current_thread)
37 LEXT(machine_set_current_thread)
38 mcr p15, 0, r0, c13, c0, 4 // Write TPIDRPRW
39 ldr r1, [r0, TH_CTH_SELF]
40 mrc p15, 0, r2, c13, c0, 3 // Read TPIDRURO
41 and r2, r2, #3 // Extract cpu number
43 mcr p15, 0, r1, c13, c0, 3 // Write TPIDRURO
45 mcr p15, 0, r1, c13, c0, 2 // Write TPIDRURW
49 * void machine_idle(void)
53 .globl EXT(machine_idle)
55 cpsid if // Disable FIQ IRQ
59 cpsie if // Enable FIQ IRQ
63 * void cpu_idle_wfi(boolean_t wfi_fast):
64 * cpu_idle is the only function that should call this.
68 .globl EXT(cpu_idle_wfi)
83 * We export the address of the WFI instruction so that it can be patched; this will be
84 * ugly from a debugging perspective.
87 #if (__ARM_ARCH__ >= 7)
93 mcr p15, 0, r0, c7, c10, 4
96 mcr p15, 0, r0, c7, c0, 4
111 .globl EXT(timer_grab)
114 ldr r2, [r0, TIMER_HIGH]
115 ldr r3, [r0, TIMER_LOW]
119 ldr r1, [r0, TIMER_HIGHCHK]
126 .globl EXT(timer_advance_internal_32)
127 LEXT(timer_advance_internal_32)
128 str r1, [r0, TIMER_HIGHCHK]
132 str r2, [r0, TIMER_LOW]
136 str r1, [r0, TIMER_HIGH]
140 .globl EXT(get_vfp_enabled)
141 LEXT(get_vfp_enabled)
144 and r1, r0, #FPEXC_EN // Extact vfp enable previous state
145 mov r0, r1, LSR #FPEXC_EN_BIT // Return 1 if enabled, 0 if disabled
147 mov r0, #0 // return false
151 /* This is no longer useful (but is exported, so this may require kext cleanup). */
153 .globl EXT(enable_kernel_vfp_context)
154 LEXT(enable_kernel_vfp_context)
157 /* uint32_t get_fpscr(void):
158 * Returns the current state of the FPSCR register.
161 .globl EXT(get_fpscr)
168 .globl EXT(set_fpscr)
169 /* void set_fpscr(uint32_t value):
170 * Set the FPSCR register.
181 * void OSSynchronizeIO(void)
185 .globl EXT(OSSynchronizeIO)
186 LEXT(OSSynchronizeIO)
191 .macro SYNC_TLB_FLUSH
197 * void sync_tlb_flush
199 * Synchronize one or more prior TLB flush operations
203 .globl EXT(sync_tlb_flush)
211 mcr p15, 0, r0, c8, c3, 0 // Invalidate Inner Shareable entire TLBs
213 mcr p15, 0, r0, c8, c7, 0 // Invalidate entire TLB
218 * void flush_mmu_tlb_async(void)
220 * Flush all TLBs, don't wait for completion
224 .globl EXT(flush_mmu_tlb_async)
225 LEXT(flush_mmu_tlb_async)
230 * void flush_mmu_tlb(void)
236 .globl EXT(flush_mmu_tlb)
242 .macro FLUSH_CORE_TLB
244 mcr p15, 0, r0, c8, c7, 0 // Invalidate entire TLB
249 * void flush_core_tlb_async(void)
251 * Flush local core's TLB, don't wait for completion
255 .globl EXT(flush_core_tlb_async)
256 LEXT(flush_core_tlb_async)
261 * void flush_core_tlb(void)
263 * Flush local core's TLB
267 .globl EXT(flush_core_tlb)
273 .macro FLUSH_MMU_TLB_ENTRY
275 mcr p15, 0, r0, c8, c3, 1 // Invalidate TLB Inner Shareableentry
277 mcr p15, 0, r0, c8, c7, 1 // Invalidate TLB entry
281 * void flush_mmu_tlb_entry_async(uint32_t)
283 * Flush TLB entry, don't wait for completion
287 .globl EXT(flush_mmu_tlb_entry_async)
288 LEXT(flush_mmu_tlb_entry_async)
293 * void flush_mmu_tlb_entry(uint32_t)
299 .globl EXT(flush_mmu_tlb_entry)
300 LEXT(flush_mmu_tlb_entry)
305 .macro FLUSH_MMU_TLB_ENTRIES
308 mcr p15, 0, r0, c8, c3, 1 // Invalidate TLB Inner Shareable entry
310 mcr p15, 0, r0, c8, c7, 1 // Invalidate TLB entry
312 add r0, r0, ARM_PGBYTES // Increment to the next page
313 cmp r0, r1 // Loop if current address < end address
318 * void flush_mmu_tlb_entries_async(uint32_t, uint32_t)
320 * Flush TLB entries for address range, don't wait for completion
324 .globl EXT(flush_mmu_tlb_entries_async)
325 LEXT(flush_mmu_tlb_entries_async)
326 FLUSH_MMU_TLB_ENTRIES
330 * void flush_mmu_tlb_entries(uint32_t, uint32_t)
332 * Flush TLB entries for address range
336 .globl EXT(flush_mmu_tlb_entries)
337 LEXT(flush_mmu_tlb_entries)
338 FLUSH_MMU_TLB_ENTRIES
343 .macro FLUSH_MMU_TLB_MVA_ENTRIES
345 mcr p15, 0, r0, c8, c3, 3 // Invalidate TLB Inner Shareable entries by mva
347 mcr p15, 0, r0, c8, c7, 3 // Invalidate TLB Inner Shareable entries by mva
352 * void flush_mmu_tlb_mva_entries_async(uint32_t)
354 * Flush TLB entries for mva, don't wait for completion
358 .globl EXT(flush_mmu_tlb_mva_entries_async)
359 LEXT(flush_mmu_tlb_mva_entries_async)
360 FLUSH_MMU_TLB_MVA_ENTRIES
364 * void flush_mmu_tlb_mva_entries_async(uint32_t)
366 * Flush TLB entries for mva
370 .globl EXT(flush_mmu_tlb_mva_entries)
371 LEXT(flush_mmu_tlb_mva_entries)
372 FLUSH_MMU_TLB_MVA_ENTRIES
376 .macro FLUSH_MMU_TLB_ASID
378 mcr p15, 0, r0, c8, c3, 2 // Invalidate TLB Inner Shareable entries by asid
380 mcr p15, 0, r0, c8, c7, 2 // Invalidate TLB entries by asid
385 * void flush_mmu_tlb_asid_async(uint32_t)
387 * Flush TLB entries for asid, don't wait for completion
391 .globl EXT(flush_mmu_tlb_asid_async)
392 LEXT(flush_mmu_tlb_asid_async)
397 * void flush_mmu_tlb_asid(uint32_t)
399 * Flush TLB entries for asid
403 .globl EXT(flush_mmu_tlb_asid)
404 LEXT(flush_mmu_tlb_asid)
409 .macro FLUSH_CORE_TLB_ASID
410 mcr p15, 0, r0, c8, c7, 2 // Invalidate TLB entries by asid
414 * void flush_core_tlb_asid_async(uint32_t)
416 * Flush local core TLB entries for asid, don't wait for completion
420 .globl EXT(flush_core_tlb_asid_async)
421 LEXT(flush_core_tlb_asid_async)
426 * void flush_core_tlb_asid(uint32_t)
428 * Flush local core TLB entries for asid
432 .globl EXT(flush_core_tlb_asid)
433 LEXT(flush_core_tlb_asid)
439 * Set MMU Translation Table Base
443 .globl EXT(set_mmu_ttb)
445 orr r0, r0, #(TTBR_SETUP & 0xFF) // Setup PTWs memory attribute
446 orr r0, r0, #(TTBR_SETUP & 0xFF00) // Setup PTWs memory attribute
447 mcr p15, 0, r0, c2, c0, 0 // write r0 to translation table 0
453 * Set MMU Translation Table Base Alternate
457 .globl EXT(set_mmu_ttb_alternate)
458 LEXT(set_mmu_ttb_alternate)
459 orr r0, r0, #(TTBR_SETUP & 0xFF) // Setup PTWs memory attribute
460 orr r0, r0, #(TTBR_SETUP & 0xFF00) // Setup PTWs memory attribute
461 mcr p15, 0, r0, c2, c0, 1 // write r0 to translation table 1
467 * Set MMU Translation Table Base
471 .globl EXT(get_mmu_ttb)
473 mrc p15, 0, r0, c2, c0, 0 // translation table to r0
478 * get MMU control register
482 .globl EXT(get_aux_control)
483 LEXT(get_aux_control)
484 mrc p15, 0, r0, c1, c0, 1 // read aux control into r0
485 bx lr // return old bits in r0
488 * set MMU control register
492 .globl EXT(set_aux_control)
493 LEXT(set_aux_control)
494 mcr p15, 0, r0, c1, c0, 1 // write r0 back to aux control
500 * get MMU control register
504 .globl EXT(get_mmu_control)
505 LEXT(get_mmu_control)
506 mrc p15, 0, r0, c1, c0, 0 // read mmu control into r0
507 bx lr // return old bits in r0
510 * set MMU control register
514 .globl EXT(set_mmu_control)
515 LEXT(set_mmu_control)
516 mcr p15, 0, r0, c1, c0, 0 // write r0 back to mmu control
521 * MMU kernel virtual to physical address translation
525 .globl EXT(mmu_kvtop)
527 mrs r3, cpsr // Read cpsr
528 cpsid if // Disable FIQ IRQ
530 mcr p15, 0, r1, c7, c8, 0 // Write V2PCWPR
532 mrc p15, 0, r0, c7, c4, 0 // Read PAR
533 ands r2, r0, #0x1 // Test conversion aborted
534 bne mmu_kvtophys_fail
535 ands r2, r0, #0x2 // Test super section
536 mvnne r2, #0xFF000000
537 moveq r2, #0x000000FF
538 orreq r2, r2, #0x00000F00
539 bics r0, r0, r2 // Clear lower bits
540 beq mmu_kvtophys_fail
547 msr cpsr, r3 // Restore cpsr
551 * MMU user virtual to physical address translation
555 .globl EXT(mmu_uvtop)
557 mrs r3, cpsr // Read cpsr
558 cpsid if // Disable FIQ IRQ
560 mcr p15, 0, r1, c7, c8, 2 // Write V2PCWUR
562 mrc p15, 0, r0, c7, c4, 0 // Read PAR
563 ands r2, r0, #0x1 // Test conversion aborted
564 bne mmu_uvtophys_fail
565 ands r2, r0, #0x2 // Test super section
566 mvnne r2, #0xFF000000
567 moveq r2, #0x000000FF
568 orreq r2, r2, #0x00000F00
569 bics r0, r0, r2 // Clear lower bits
570 beq mmu_uvtophys_fail
577 msr cpsr, r3 // Restore cpsr
581 * MMU kernel virtual to physical address preflight write access
585 .globl EXT(mmu_kvtop_wpreflight)
586 LEXT(mmu_kvtop_wpreflight)
587 mrs r3, cpsr // Read cpsr
588 cpsid if // Disable FIQ IRQ
590 mcr p15, 0, r1, c7, c8, 1 // Write V2PCWPW
592 mrc p15, 0, r0, c7, c4, 0 // Read PAR
593 ands r2, r0, #0x1 // Test conversion aborted
594 bne mmu_kvtophys_wpreflight_fail
595 ands r2, r0, #0x2 // Test super section
596 mvnne r2, #0xFF000000
597 moveq r2, #0x000000FF
598 orreq r2, r2, #0x00000F00
599 bics r0, r0, r2 // Clear lower bits
600 beq mmu_kvtophys_wpreflight_fail // Sanity check: successful access must deliver zero low bits
603 b mmu_kvtophys_wpreflight_ret
604 mmu_kvtophys_wpreflight_fail:
606 mmu_kvtophys_wpreflight_ret:
607 msr cpsr, r3 // Restore cpsr
611 * set context id register
614 * set context id register
618 .globl EXT(set_context_id)
620 mcr p15, 0, r0, c13, c0, 1
625 * arg0: prefix of the external validator function (copyin or copyout)
626 * arg1: 0-based index of highest argument register that must be preserved
628 .macro COPYIO_VALIDATE
629 /* call NAME_validate to check the arguments */
630 push {r0-r$1, r7, lr}
631 add r7, sp, #(($1 + 1) * 4)
634 addne sp, #(($1 + 1) * 4)
640 #define COPYIO_SET_RECOVER() \
641 /* set recovery address */ ;\
642 stmfd sp!, { r4, r5, r6 } ;\
643 adr r3, copyio_error ;\
644 mrc p15, 0, r12, c13, c0, 4 ;\
645 ldr r4, [r12, TH_RECOVER] ;\
646 str r3, [r12, TH_RECOVER]
648 #define COPYIO_TRY_KERNEL() \
649 /* if (current_thread()->map->pmap == kernel_pmap) copyio_kernel() */ ;\
650 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW ;\
651 ldr r3, [r12, ACT_MAP] ;\
652 ldr r3, [r3, MAP_PMAP] ;\
653 LOAD_ADDR(ip, kernel_pmap_store) ;\
657 #if __ARM_USER_PROTECT__
658 #define COPYIO_MAP_USER() \
659 /* disable interrupts to prevent expansion to 2GB at L1 ;\
660 * between loading ttep and storing it in ttbr0.*/ ;\
663 ldr r3, [r12, ACT_UPTW_TTB] ;\
664 mcr p15, 0, r3, c2, c0, 0 ;\
666 ldr r3, [r12, ACT_ASID] ;\
667 mcr p15, 0, r3, c13, c0, 1 ;\
670 #define COPYIO_MAP_USER()
673 #define COPYIO_HEADER() ;\
674 /* test for zero len */ ;\
680 /* if len is less than 16 bytes, just do a simple copy */
683 /* test for src and dest of the same word alignment */
690 /* 16 bytes at a time */
691 ldmia r0!, { r3, r5, r6, r12 }
692 stmia r1!, { r3, r5, r6, r12 }
694 bge L$0_wordwise_loop
695 /* fixup the len and test for completion */
699 /* copy 2 bytes at a time */
710 #if __ARM_USER_PROTECT__
711 #define COPYIO_UNMAP_USER() \
712 mrc p15, 0, r12, c13, c0, 4 ;\
713 ldr r3, [r12, ACT_KPTW_TTB] ;\
714 mcr p15, 0, r3, c2, c0, 0 ;\
716 mcr p15, 0, r3, c13, c0, 1 ;\
719 #define COPYIO_UNMAP_USER() \
720 mrc p15, 0, r12, c13, c0, 4
723 #define COPYIO_RESTORE_RECOVER() \
724 /* restore the recovery address */ ;\
725 str r4, [r12, TH_RECOVER] ;\
726 ldmfd sp!, { r4, r5, r6 }
730 * const user_addr_t user_addr,
737 .globl EXT(copyinstr)
740 moveq r0, #ENAMETOOLONG
744 COPYIO_VALIDATE copyin_user, 3
745 stmfd sp!, { r4, r5, r6 }
748 adr r3, copyinstr_error // Get address for recover
749 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
750 ldr r4, [r12, TH_RECOVER] ;\
751 str r3, [r12, TH_RECOVER]
753 mov r12, #0 // Number of bytes copied so far
755 ldrb r3, [r0], #1 // Load a byte from the source (user)
756 strb r3, [r1], #1 // Store a byte to the destination (kernel)
760 cmp r12, r2 // Room to copy more bytes?
763 // Ran out of space in the destination buffer, so return ENAMETOOLONG.
766 mov r3, #ENAMETOOLONG
769 // When we get here, we have finished copying the string. We came here from
770 // either the "beq copyinstr_done" above, in which case r3 == 0 (which is also
771 // the function result for success), or falling through from copyinstr_too_long,
772 // in which case r3 == ENAMETOOLONG.
774 str r12, [r6] // Save the count for actual
775 mov r0, r3 // Return error code from r3
778 str r4, [r12, TH_RECOVER]
779 ldmfd sp!, { r4, r5, r6 }
783 /* set error, exit routine */
788 * int copyin(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes)
795 COPYIO_VALIDATE copyin, 2
801 COPYIO_RESTORE_RECOVER()
805 * int copyout(const char *kernel_addr, user_addr_t user_addr, vm_size_t nbytes)
812 COPYIO_VALIDATE copyout, 2
818 COPYIO_RESTORE_RECOVER()
823 * int copyin_atomic32(const user_addr_t user_addr, uint32_t *kernel_addr)
829 .globl EXT(copyin_atomic32)
830 LEXT(copyin_atomic32)
831 tst r0, #3 // Test alignment of user address
835 COPYIO_VALIDATE copyin_user, 1
839 ldr r2, [r0] // Load word from user
840 str r2, [r1] // Store to kernel_addr
841 mov r0, #0 // Success
844 COPYIO_RESTORE_RECOVER()
846 2: // misaligned copyin
851 * int copyin_atomic32_wait_if_equals(const char *src, uint32_t value)
857 .globl EXT(copyin_atomic32_wait_if_equals)
858 LEXT(copyin_atomic32_wait_if_equals)
859 tst r0, #3 // Test alignment of user address
864 COPYIO_VALIDATE copyio_user, 1 // validate user address (uses r2, r3)
878 COPYIO_RESTORE_RECOVER()
880 2: // misaligned copyin
885 * int copyin_atomic64(const user_addr_t user_addr, uint64_t *kernel_addr)
891 .globl EXT(copyin_atomic64)
892 LEXT(copyin_atomic64)
893 tst r0, #7 // Test alignment of user address
897 COPYIO_VALIDATE copyin_user, 1
901 1: // ldrex/strex retry loop
902 ldrexd r2, r3, [r0] // Load double word from user
903 strexd r5, r2, r3, [r0] // (the COPYIO_*() macros make r5 safe to use as a scratch register here)
906 stm r1, {r2, r3} // Store to kernel_addr
907 mov r0, #0 // Success
910 COPYIO_RESTORE_RECOVER()
912 2: // misaligned copyin
920 str r4, [r12, TH_RECOVER]
921 ldmfd sp!, { r4, r5, r6 }
926 * int copyout_atomic32(uint32_t value, user_addr_t user_addr)
932 .globl EXT(copyout_atomic32)
933 LEXT(copyout_atomic32)
934 tst r1, #3 // Test alignment of user address
939 COPYIO_VALIDATE copyio_user, 1 // validate user address (uses r2, r3)
943 str r0, [r1] // Store word to user
944 mov r0, #0 // Success
947 COPYIO_RESTORE_RECOVER()
949 2: // misaligned copyout
955 * int copyout_atomic64(uint64_t value, user_addr_t user_addr)
961 .globl EXT(copyout_atomic64)
962 LEXT(copyout_atomic64)
963 tst r2, #7 // Test alignment of user address
967 COPYIO_VALIDATE copyio_user, 2 // validate user address (uses r2, r3)
971 1: // ldrex/strex retry loop
973 strexd r3, r0, r1, [r2] // Atomically store double word to user
977 mov r0, #0 // Success
980 COPYIO_RESTORE_RECOVER()
982 2: // misaligned copyout
988 * int copyin_kern(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes)
992 .globl EXT(copyin_kern)
998 * int copyout_kern(const char *kernel_addr, user_addr_t user_addr, vm_size_t nbytes)
1002 .globl EXT(copyout_kern)
1008 stmfd sp!, { r5, r6 }
1009 COPYIO_BODY copyio_kernel
1010 ldmfd sp!, { r5, r6 }
1014 * int copyinframe(const vm_address_t frame_addr, char *kernel_addr)
1016 * Safely copy eight bytes (the fixed top of an ARM frame) from
1017 * either user or kernel memory.
1021 .globl EXT(copyinframe)
1023 COPYIO_SET_RECOVER()
1030 * uint32_t arm_debug_read_dscr(void)
1034 .globl EXT(arm_debug_read_dscr)
1035 LEXT(arm_debug_read_dscr)
1036 #if __ARM_DEBUG__ >= 6
1037 mrc p14, 0, r0, c0, c1
1044 * void arm_debug_set_cp14(arm_debug_state_t *debug_state)
1046 * Set debug registers to match the current thread state
1047 * (NULL to disable). Assume 6 breakpoints and 2
1048 * watchpoints, since that has been the case in all cores
1053 .globl EXT(arm_debug_set_cp14)
1054 LEXT(arm_debug_set_cp14)
1055 #if __ARM_DEBUG__ >= 6
1056 mrc p15, 0, r1, c13, c0, 4 // Read TPIDRPRW
1057 ldr r2, [r1, ACT_CPUDATAP] // Get current cpu
1058 str r0, [r2, CPU_USER_DEBUG] // Set current user debug
1060 // Lock the debug registers
1063 mcr p14, 0, ip, c1, c0, 4
1065 // enable monitor mode (needed to set and use debug registers)
1066 mrc p14, 0, ip, c0, c1, 0
1067 orr ip, ip, #0x8000 // set MDBGen = 1
1068 #if __ARM_DEBUG__ >= 7
1069 mcr p14, 0, ip, c0, c2, 2
1071 mcr p14, 0, ip, c0, c1, 0
1073 // first turn off all breakpoints/watchpoints
1075 mcr p14, 0, r1, c0, c0, 5 // BCR0
1076 mcr p14, 0, r1, c0, c1, 5 // BCR1
1077 mcr p14, 0, r1, c0, c2, 5 // BCR2
1078 mcr p14, 0, r1, c0, c3, 5 // BCR3
1079 mcr p14, 0, r1, c0, c4, 5 // BCR4
1080 mcr p14, 0, r1, c0, c5, 5 // BCR5
1081 mcr p14, 0, r1, c0, c0, 7 // WCR0
1082 mcr p14, 0, r1, c0, c1, 7 // WCR1
1083 // if (debug_state == NULL) disable monitor mode and return;
1085 biceq ip, ip, #0x8000 // set MDBGen = 0
1086 #if __ARM_DEBUG__ >= 7
1087 mcreq p14, 0, ip, c0, c2, 2
1089 mcreq p14, 0, ip, c0, c1, 0
1092 ldmia r0!, {r1, r2, r3, ip}
1093 mcr p14, 0, r1, c0, c0, 4 // BVR0
1094 mcr p14, 0, r2, c0, c1, 4 // BVR1
1095 mcr p14, 0, r3, c0, c2, 4 // BVR2
1096 mcr p14, 0, ip, c0, c3, 4 // BVR3
1098 mcr p14, 0, r1, c0, c4, 4 // BVR4
1099 mcr p14, 0, r2, c0, c5, 4 // BVR5
1100 add r0, r0, #40 // advance to bcr[0]
1101 ldmia r0!, {r1, r2, r3, ip}
1102 mcr p14, 0, r1, c0, c0, 5 // BCR0
1103 mcr p14, 0, r2, c0, c1, 5 // BCR1
1104 mcr p14, 0, r3, c0, c2, 5 // BCR2
1105 mcr p14, 0, ip, c0, c3, 5 // BCR3
1107 mcr p14, 0, r1, c0, c4, 5 // BCR4
1108 mcr p14, 0, r2, c0, c5, 5 // BCR5
1109 add r0, r0, #40 // advance to wvr[0]
1111 mcr p14, 0, r1, c0, c0, 6 // WVR0
1112 mcr p14, 0, r2, c0, c1, 6 // WVR1
1113 add r0, r0, #56 // advance to wcr[0]
1115 mcr p14, 0, r1, c0, c0, 7 // WCR0
1116 mcr p14, 0, r2, c0, c1, 7 // WCR1
1118 // Unlock debug registers
1120 mcr p14, 0, ip, c1, c0, 4
1125 * void fiq_context_init(boolean_t enable_fiq)
1129 .globl EXT(fiq_context_init)
1130 LEXT(fiq_context_init)
1131 mrs r3, cpsr // Save current CPSR
1132 cmp r0, #0 // Test enable_fiq
1133 bicne r3, r3, #PSR_FIQF // Enable FIQ if not FALSE
1134 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
1135 ldr r2, [r12, ACT_CPUDATAP] // Get current cpu data
1138 /* Despite the fact that we use the physical timebase
1139 * register as the basis for time on our platforms, we
1140 * end up using the virtual timer in order to manage
1141 * deadlines. This is due to the fact that for our
1142 * current platforms, the interrupt generated by the
1143 * physical timer is not hooked up to anything, and is
1144 * therefore dropped on the floor. Therefore, for
1145 * timers to function they MUST be based on the virtual
1149 mov r0, #1 // Enable Timer
1150 mcr p15, 0, r0, c14, c3, 1 // Write to CNTV_CTL
1152 /* Enable USER access to the physical timebase (PL0PCTEN).
1153 * The rationale for providing access to the physical
1154 * timebase being that the virtual timebase is broken for
1155 * some platforms. Maintaining the offset ourselves isn't
1156 * expensive, so mandate that the userspace implementation
1157 * do timebase_phys+offset rather than trying to propogate
1158 * all of the informaiton about what works up to USER.
1160 mcr p15, 0, r0, c14, c1, 0 // Set CNTKCTL.PL0PCTEN (CNTKCTL[0])
1162 #else /* ! __ARM_TIME__ */
1163 msr cpsr_c, #(PSR_FIQ_MODE|PSR_FIQF|PSR_IRQF) // Change mode to FIQ with FIQ/IRQ disabled
1164 mov r8, r2 // Load the BootCPUData address
1165 ldr r9, [r2, CPU_GET_FIQ_HANDLER] // Load fiq function address
1166 ldr r10, [r2, CPU_TBD_HARDWARE_ADDR] // Load the hardware address
1167 ldr r11, [r2, CPU_TBD_HARDWARE_VAL] // Load the hardware value
1168 #endif /* __ARM_TIME__ */
1170 msr cpsr_c, r3 // Restore saved CPSR
1174 * void reenable_async_aborts(void)
1178 .globl EXT(reenable_async_aborts)
1179 LEXT(reenable_async_aborts)
1180 cpsie a // Re-enable async aborts
1184 * uint64_t ml_get_timebase(void)
1188 .globl EXT(ml_get_timebase)
1189 LEXT(ml_get_timebase)
1190 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
1191 ldr r3, [r12, ACT_CPUDATAP] // Get current cpu data
1192 #if __ARM_TIME__ || __ARM_TIME_TIMEBASE_ONLY__
1193 isb // Required by ARMV7C.b section B8.1.2, ARMv8 section D6.1.2.
1195 mrrc p15, 0, r3, r1, c14 // Read the Time Base (CNTPCT), high => r1
1196 mrrc p15, 0, r0, r3, c14 // Read the Time Base (CNTPCT), low => r0
1197 mrrc p15, 0, r3, r2, c14 // Read the Time Base (CNTPCT), high => r2
1199 bne 1b // Loop until both high values are the same
1201 ldr r3, [r12, ACT_CPUDATAP] // Get current cpu data
1202 ldr r2, [r3, CPU_BASE_TIMEBASE_LOW] // Add in the offset to
1203 adds r0, r0, r2 // convert to
1204 ldr r2, [r3, CPU_BASE_TIMEBASE_HIGH] // mach_absolute_time
1206 #else /* ! __ARM_TIME__ || __ARM_TIME_TIMEBASE_ONLY__ */
1208 ldr r2, [r3, CPU_TIMEBASE_HIGH] // Get the saved TBU value
1209 ldr r0, [r3, CPU_TIMEBASE_LOW] // Get the saved TBL value
1210 ldr r1, [r3, CPU_TIMEBASE_HIGH] // Get the saved TBU value
1211 cmp r1, r2 // Make sure TB has not rolled over
1213 #endif /* __ARM_TIME__ */
1218 * uint32_t ml_get_decrementer(void)
1222 .globl EXT(ml_get_decrementer)
1223 LEXT(ml_get_decrementer)
1224 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
1225 ldr r3, [r12, ACT_CPUDATAP] // Get current cpu data
1226 ldr r2, [r3, CPU_GET_DECREMENTER_FUNC] // Get get_decrementer_func
1228 bxne r2 // Call it if there is one
1230 mrc p15, 0, r0, c14, c3, 0 // Read the Decrementer (CNTV_TVAL)
1232 ldr r0, [r3, CPU_DECREMENTER] // Get the saved dec value
1238 * void ml_set_decrementer(uint32_t dec_value)
1242 .globl EXT(ml_set_decrementer)
1243 LEXT(ml_set_decrementer)
1244 mrc p15, 0, r12, c13, c0, 4 // Read TPIDRPRW
1245 ldr r3, [r12, ACT_CPUDATAP] // Get current cpu data
1246 ldr r2, [r3, CPU_SET_DECREMENTER_FUNC] // Get set_decrementer_func
1248 bxne r2 // Call it if there is one
1250 str r0, [r3, CPU_DECREMENTER] // Save the new dec value
1251 mcr p15, 0, r0, c14, c3, 0 // Write the Decrementer (CNTV_TVAL)
1253 mrs r2, cpsr // Save current CPSR
1254 msr cpsr_c, #(PSR_FIQ_MODE|PSR_FIQF|PSR_IRQF) // Change mode to FIQ with FIQ/IRQ disabled.
1255 mov r12, r0 // Set the DEC value
1256 str r12, [r8, CPU_DECREMENTER] // Store DEC
1257 msr cpsr_c, r2 // Restore saved CPSR
1263 * boolean_t ml_get_interrupts_enabled(void)
1267 .globl EXT(ml_get_interrupts_enabled)
1268 LEXT(ml_get_interrupts_enabled)
1271 bic r0, r0, r2, lsr #PSR_IRQFb
1275 * Platform Specific Timebase & Decrementer Functions
1279 #if defined(ARM_BOARD_CLASS_S7002)
1282 .globl EXT(fleh_fiq_s7002)
1283 LEXT(fleh_fiq_s7002)
1284 str r11, [r10, #PMGR_INTERVAL_TMR_CTL_OFFSET] // Clear the decrementer interrupt
1286 str r13, [r8, CPU_DECREMENTER]
1291 .globl EXT(s7002_get_decrementer)
1292 LEXT(s7002_get_decrementer)
1293 ldr ip, [r3, CPU_TBD_HARDWARE_ADDR] // Get the hardware address
1294 add ip, ip, #PMGR_INTERVAL_TMR_OFFSET
1295 ldr r0, [ip] // Get the Decrementer
1300 .globl EXT(s7002_set_decrementer)
1301 LEXT(s7002_set_decrementer)
1302 str r0, [r3, CPU_DECREMENTER] // Save the new dec value
1303 ldr ip, [r3, CPU_TBD_HARDWARE_ADDR] // Get the hardware address
1304 str r0, [ip, #PMGR_INTERVAL_TMR_OFFSET] // Store the new Decrementer
1306 #endif /* defined(ARM_BOARD_CLASS_S7002) */
1308 #if defined(ARM_BOARD_CLASS_T8002)
1311 .globl EXT(fleh_fiq_t8002)
1312 LEXT(fleh_fiq_t8002)
1313 mov r13, #kAICTmrIntStat
1314 str r11, [r10, r13] // Clear the decrementer interrupt
1316 str r13, [r8, CPU_DECREMENTER]
1321 .globl EXT(t8002_get_decrementer)
1322 LEXT(t8002_get_decrementer)
1323 ldr ip, [r3, CPU_TBD_HARDWARE_ADDR] // Get the hardware address
1326 ldr r0, [ip] // Get the Decrementer
1331 .globl EXT(t8002_set_decrementer)
1332 LEXT(t8002_set_decrementer)
1333 str r0, [r3, CPU_DECREMENTER] // Save the new dec value
1334 ldr ip, [r3, CPU_TBD_HARDWARE_ADDR] // Get the hardware address
1336 str r0, [ip, r5] // Store the new Decrementer
1338 #endif /* defined(ARM_BOARD_CLASS_T8002) */
1340 LOAD_ADDR_GEN_DEF(kernel_pmap_store)
1342 #include "globals_asm.h"
1344 /* vim: set ts=4: */