2 * This file contains miscellaneous low-level functions.
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
15 #include <linux/config.h>
16 #include <linux/sys.h>
17 #include <asm/unistd.h>
18 #include <asm/errno.h>
21 #include <asm/cache.h>
22 #include <asm/cputable.h>
24 #include <asm/ppc_asm.h>
25 #include <asm/thread_info.h>
26 #include <asm/asm-offsets.h>
39 * This returns the high 64 bits of the product of two 64-bit numbers.
51 1: beqlr cr1 /* all done if high part of A is 0 */
66 * Returns (address we're running at) - (address we were linked at)
67 * for use before the text and data are mapped to KERNELBASE.
79 * add_reloc_offset(x) returns x + reloc_offset().
81 _GLOBAL(add_reloc_offset)
92 * sub_reloc_offset(x) returns x - reloc_offset().
94 _GLOBAL(sub_reloc_offset)
106 * reloc_got2 runs through the .got2 section adding an offset
111 lis r7,__got2_start@ha
112 addi r7,r7,__got2_start@l
114 addi r8,r8,__got2_end@l
135 * called with r3 = data offset and r4 = CPU number
138 _GLOBAL(identify_cpu)
139 addis r8,r3,cpu_specs@ha
140 addi r8,r8,cpu_specs@l
143 lwz r5,CPU_SPEC_PVR_MASK(r8)
145 lwz r6,CPU_SPEC_PVR_VALUE(r8)
148 addi r8,r8,CPU_SPEC_ENTRY_SIZE
151 addis r6,r3,cur_cpu_spec@ha
152 addi r6,r6,cur_cpu_spec@l
158 * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
159 * and writes nop's over sections of code that don't apply for this cpu.
160 * r3 = data offset (not changed)
162 _GLOBAL(do_cpu_ftr_fixups)
163 /* Get CPU 0 features */
164 addis r6,r3,cur_cpu_spec@ha
165 addi r6,r6,cur_cpu_spec@l
168 lwz r4,CPU_SPEC_FEATURES(r4)
170 /* Get the fixup table */
171 addis r6,r3,__start___ftr_fixup@ha
172 addi r6,r6,__start___ftr_fixup@l
173 addis r7,r3,__stop___ftr_fixup@ha
174 addi r7,r7,__stop___ftr_fixup@l
180 lwz r8,-16(r6) /* mask */
182 lwz r9,-12(r6) /* value */
185 lwz r8,-8(r6) /* section begin */
186 lwz r9,-4(r6) /* section end */
189 /* write nops over the section of code */
190 /* todo: if large section, add a branch at the start of it */
194 lis r0,0x60000000@h /* nop */
196 andi. r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
198 dcbst 0,r8 /* suboptimal, but simpler */
203 sync /* additional sync needed on g4 */
208 * call_setup_cpu - call the setup_cpu function for this cpu
209 * r3 = data offset, r24 = cpu number
211 * Setup function is called with:
213 * r4 = ptr to CPU spec (relocated)
215 _GLOBAL(call_setup_cpu)
216 addis r4,r3,cur_cpu_spec@ha
217 addi r4,r4,cur_cpu_spec@l
220 lwz r5,CPU_SPEC_SETUP(r4)
227 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
229 /* This gets called by via-pmu.c to switch the PLL selection
230 * on 750fx CPU. This function should really be moved to some
231 * other place (as most of the cpufreq code in via-pmu
233 _GLOBAL(low_choose_750fx_pll)
239 /* If switching to PLL1, disable HID0:BTIC */
250 /* Calc new HID1 value */
251 mfspr r4,SPRN_HID1 /* Build a HID1:PS bit from parameter */
252 rlwinm r5,r3,16,15,15 /* Clear out HID1:PS from value read */
253 rlwinm r4,r4,0,16,14 /* Could have I used rlwimi here ? */
257 /* Store new HID1 image */
261 addis r6,r6,nap_save_hid1@ha
262 stw r4,nap_save_hid1@l(r6)
264 /* If switching to PLL0, enable HID0:BTIC */
279 _GLOBAL(low_choose_7447a_dfs)
285 /* Calc new HID1 value */
287 insrwi r4,r3,1,9 /* insert parameter into bit 9 */
297 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
300 * complement mask on the msr then "or" some values on.
301 * _nmask_and_or_msr(nmask, value_to_or)
303 _GLOBAL(_nmask_and_or_msr)
304 mfmsr r0 /* Get current msr */
305 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
306 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
307 SYNC /* Some chip revs have problems here... */
308 mtmsr r0 /* Update machine state */
317 #if defined(CONFIG_40x)
318 sync /* Flush to memory before changing mapping */
320 isync /* Flush shadow TLB */
321 #elif defined(CONFIG_44x)
325 /* Load high watermark */
326 lis r4,tlb_44x_hwater@ha
327 lwz r5,tlb_44x_hwater@l(r4)
329 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
335 #elif defined(CONFIG_FSL_BOOKE)
336 /* Invalidate all entries in TLB0 */
339 /* Invalidate all entries in TLB1 */
342 /* Invalidate all entries in TLB2 */
345 /* Invalidate all entries in TLB3 */
351 #endif /* CONFIG_SMP */
352 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
353 #if defined(CONFIG_SMP)
359 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
360 rlwinm r0,r0,0,28,26 /* clear DR */
364 lis r9,mmu_hash_lock@h
365 ori r9,r9,mmu_hash_lock@l
377 stw r0,0(r9) /* clear mmu_hash_lock */
381 #else /* CONFIG_SMP */
385 #endif /* CONFIG_SMP */
386 #endif /* ! defined(CONFIG_40x) */
390 * Flush MMU TLB for a particular address
393 #if defined(CONFIG_40x)
397 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
398 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
400 tlbwe r3, r3, TLB_TAG
403 #elif defined(CONFIG_44x)
405 mfspr r5,SPRN_PID /* Get PID */
406 rlwimi r4,r5,0,24,31 /* Set TID */
412 /* There are only 64 TLB entries, so r3 < 64,
413 * which means bit 22, is clear. Since 22 is
414 * the V bit in the TLB_PAGEID, loading this
415 * value will invalidate the TLB entry.
417 tlbwe r3, r3, PPC44x_TLB_PAGEID
420 #elif defined(CONFIG_FSL_BOOKE)
421 rlwinm r4, r3, 0, 0, 19
422 ori r5, r4, 0x08 /* TLBSEL = 1 */
423 ori r6, r4, 0x10 /* TLBSEL = 2 */
424 ori r7, r4, 0x18 /* TLBSEL = 3 */
430 #if defined(CONFIG_SMP)
432 #endif /* CONFIG_SMP */
433 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
434 #if defined(CONFIG_SMP)
440 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
441 rlwinm r0,r0,0,28,26 /* clear DR */
445 lis r9,mmu_hash_lock@h
446 ori r9,r9,mmu_hash_lock@l
458 stw r0,0(r9) /* clear mmu_hash_lock */
462 #else /* CONFIG_SMP */
465 #endif /* CONFIG_SMP */
466 #endif /* ! CONFIG_40x */
470 * Flush instruction cache.
471 * This is a no-op on the 601.
473 _GLOBAL(flush_instruction_cache)
474 #if defined(CONFIG_8xx)
477 mtspr SPRN_IC_CST, r5
478 #elif defined(CONFIG_4xx)
490 #elif CONFIG_FSL_BOOKE
493 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
494 /* msync; isync recommended here */
498 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
500 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
504 rlwinm r3,r3,16,16,31
506 beqlr /* for 601, do nothing */
507 /* 603/604 processor - use invalidate-all bit in HID0 */
511 #endif /* CONFIG_8xx/4xx */
516 * Write any modified data cache blocks out to memory
517 * and invalidate the corresponding instruction cache blocks.
518 * This is a no-op on the 601.
520 * flush_icache_range(unsigned long start, unsigned long stop)
522 _GLOBAL(__flush_icache_range)
524 blr /* for 601, do nothing */
525 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
526 li r5,L1_CACHE_BYTES-1
530 srwi. r4,r4,L1_CACHE_SHIFT
535 addi r3,r3,L1_CACHE_BYTES
537 sync /* wait for dcbst's to get to ram */
540 addi r6,r6,L1_CACHE_BYTES
542 sync /* additional sync needed on g4 */
546 * Write any modified data cache blocks out to memory.
547 * Does not invalidate the corresponding cache lines (especially for
548 * any corresponding instruction cache).
550 * clean_dcache_range(unsigned long start, unsigned long stop)
552 _GLOBAL(clean_dcache_range)
553 li r5,L1_CACHE_BYTES-1
557 srwi. r4,r4,L1_CACHE_SHIFT
562 addi r3,r3,L1_CACHE_BYTES
564 sync /* wait for dcbst's to get to ram */
568 * Write any modified data cache blocks out to memory and invalidate them.
569 * Does not invalidate the corresponding instruction cache blocks.
571 * flush_dcache_range(unsigned long start, unsigned long stop)
573 _GLOBAL(flush_dcache_range)
574 li r5,L1_CACHE_BYTES-1
578 srwi. r4,r4,L1_CACHE_SHIFT
583 addi r3,r3,L1_CACHE_BYTES
585 sync /* wait for dcbst's to get to ram */
589 * Like above, but invalidate the D-cache. This is used by the 8xx
590 * to invalidate the cache so the PPC core doesn't get stale data
591 * from the CPM (no cache snooping here :-).
593 * invalidate_dcache_range(unsigned long start, unsigned long stop)
595 _GLOBAL(invalidate_dcache_range)
596 li r5,L1_CACHE_BYTES-1
600 srwi. r4,r4,L1_CACHE_SHIFT
605 addi r3,r3,L1_CACHE_BYTES
607 sync /* wait for dcbi's to get to ram */
611 * Flush a particular page from the data cache to RAM.
612 * Note: this is necessary because the instruction cache does *not*
613 * snoop from the data cache.
614 * This is a no-op on the 601 which has a unified cache.
616 * void __flush_dcache_icache(void *page)
618 _GLOBAL(__flush_dcache_icache)
620 blr /* for 601, do nothing */
621 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
622 rlwinm r3,r3,0,0,19 /* Get page base address */
623 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
626 0: dcbst 0,r3 /* Write line to ram */
627 addi r3,r3,L1_CACHE_BYTES
632 addi r6,r6,L1_CACHE_BYTES
639 * Flush a particular page from the data cache to RAM, identified
640 * by its physical address. We turn off the MMU so we can just use
641 * the physical address (this may be a highmem page without a kernel
644 * void __flush_dcache_icache_phys(unsigned long physaddr)
646 _GLOBAL(__flush_dcache_icache_phys)
648 blr /* for 601, do nothing */
649 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
651 rlwinm r0,r10,0,28,26 /* clear DR */
654 rlwinm r3,r3,0,0,19 /* Get page base address */
655 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
658 0: dcbst 0,r3 /* Write line to ram */
659 addi r3,r3,L1_CACHE_BYTES
664 addi r6,r6,L1_CACHE_BYTES
667 mtmsr r10 /* restore DR */
672 * Clear pages using the dcbz instruction, which doesn't cause any
673 * memory traffic (except to write out any cache lines which get
674 * displaced). This only works on cacheable memory.
676 * void clear_pages(void *page, int order) ;
679 li r0,4096/L1_CACHE_BYTES
691 addi r3,r3,L1_CACHE_BYTES
696 * Copy a whole page. We use the dcbz instruction on the destination
697 * to reduce memory traffic (it eliminates the unnecessary reads of
698 * the destination into cache). This requires that the destination
701 #define COPY_16_BYTES \
716 /* don't use prefetch on 8xx */
717 li r0,4096/L1_CACHE_BYTES
723 #else /* not 8xx, we can prefetch */
726 #if MAX_COPY_PREFETCH > 1
727 li r0,MAX_COPY_PREFETCH
731 addi r11,r11,L1_CACHE_BYTES
733 #else /* MAX_COPY_PREFETCH == 1 */
735 li r11,L1_CACHE_BYTES+4
736 #endif /* MAX_COPY_PREFETCH */
737 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
745 #if L1_CACHE_BYTES >= 32
747 #if L1_CACHE_BYTES >= 64
750 #if L1_CACHE_BYTES >= 128
760 crnot 4*cr0+eq,4*cr0+eq
761 li r0,MAX_COPY_PREFETCH
764 #endif /* CONFIG_8xx */
767 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
768 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
770 _GLOBAL(atomic_clear_mask)
777 _GLOBAL(atomic_set_mask)
786 * I/O string operations
788 * insb(port, buf, len)
789 * outsb(port, buf, len)
790 * insw(port, buf, len)
791 * outsw(port, buf, len)
792 * insl(port, buf, len)
793 * outsl(port, buf, len)
794 * insw_ns(port, buf, len)
795 * outsw_ns(port, buf, len)
796 * insl_ns(port, buf, len)
797 * outsl_ns(port, buf, len)
799 * The *_ns versions don't do byte-swapping.
867 _GLOBAL(__ide_mm_insw)
879 _GLOBAL(__ide_mm_outsw)
891 _GLOBAL(__ide_mm_insl)
903 _GLOBAL(__ide_mm_outsl)
916 * Extended precision shifts.
918 * Updated to be valid for shift counts from 0 to 63 inclusive.
921 * R3/R4 has 64 bit value
925 * ashrdi3: arithmetic right shift (sign propagation)
926 * lshrdi3: logical right shift
927 * ashldi3: left shift
931 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
932 addi r7,r5,32 # could be xori, or addi with -32
933 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
934 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
935 sraw r7,r3,r7 # t2 = MSW >> (count-32)
936 or r4,r4,r6 # LSW |= t1
937 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
938 sraw r3,r3,r5 # MSW = MSW >> count
939 or r4,r4,r7 # LSW |= t2
944 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
945 addi r7,r5,32 # could be xori, or addi with -32
946 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
947 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
948 or r3,r3,r6 # MSW |= t1
949 slw r4,r4,r5 # LSW = LSW << count
950 or r3,r3,r7 # MSW |= t2
955 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
956 addi r7,r5,32 # could be xori, or addi with -32
957 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
958 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
959 or r4,r4,r6 # LSW |= t1
960 srw r3,r3,r5 # MSW = MSW >> count
961 or r4,r4,r7 # LSW |= t2
971 mr r3,r1 /* Close enough */
975 * Create a kernel thread
976 * kernel_thread(fn, arg, flags)
978 _GLOBAL(kernel_thread)
982 mr r30,r3 /* function */
983 mr r31,r4 /* argument */
984 ori r3,r5,CLONE_VM /* flags */
985 oris r3,r3,CLONE_UNTRACED>>16
986 li r4,0 /* new sp (unused) */
989 cmpwi 0,r3,0 /* parent or child? */
990 bne 1f /* return if parent */
991 li r0,0 /* make top-level stack frame */
993 mtlr r30 /* fn addr in lr */
994 mr r3,r31 /* load arg and call fn */
997 li r0,__NR_exit /* exit if function returns */
1013 * This routine is just here to keep GCC happy - sigh...