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 */
610 #ifdef CONFIG_NOT_COHERENT_CACHE
612 * 40x cores have 8K or 16K dcache and 32 byte line size.
613 * 44x has a 32K dcache and 32 byte line size.
614 * 8xx has 1, 2, 4, 8K variants.
615 * For now, cover the worst case of the 44x.
616 * Must be called with external interrupts disabled.
618 #define CACHE_NWAYS 64
619 #define CACHE_NLINES 16
621 _GLOBAL(flush_dcache_all)
622 li r4, (2 * CACHE_NWAYS * CACHE_NLINES)
625 1: lwz r3, 0(r5) /* Load one word from every line */
626 addi r5, r5, L1_CACHE_BYTES
629 #endif /* CONFIG_NOT_COHERENT_CACHE */
632 * Flush a particular page from the data cache to RAM.
633 * Note: this is necessary because the instruction cache does *not*
634 * snoop from the data cache.
635 * This is a no-op on the 601 which has a unified cache.
637 * void __flush_dcache_icache(void *page)
639 _GLOBAL(__flush_dcache_icache)
641 blr /* for 601, do nothing */
642 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
643 rlwinm r3,r3,0,0,19 /* Get page base address */
644 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
647 0: dcbst 0,r3 /* Write line to ram */
648 addi r3,r3,L1_CACHE_BYTES
653 addi r6,r6,L1_CACHE_BYTES
660 * Flush a particular page from the data cache to RAM, identified
661 * by its physical address. We turn off the MMU so we can just use
662 * the physical address (this may be a highmem page without a kernel
665 * void __flush_dcache_icache_phys(unsigned long physaddr)
667 _GLOBAL(__flush_dcache_icache_phys)
669 blr /* for 601, do nothing */
670 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
672 rlwinm r0,r10,0,28,26 /* clear DR */
675 rlwinm r3,r3,0,0,19 /* Get page base address */
676 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
679 0: dcbst 0,r3 /* Write line to ram */
680 addi r3,r3,L1_CACHE_BYTES
685 addi r6,r6,L1_CACHE_BYTES
688 mtmsr r10 /* restore DR */
693 * Clear pages using the dcbz instruction, which doesn't cause any
694 * memory traffic (except to write out any cache lines which get
695 * displaced). This only works on cacheable memory.
697 * void clear_pages(void *page, int order) ;
700 li r0,4096/L1_CACHE_BYTES
712 addi r3,r3,L1_CACHE_BYTES
717 * Copy a whole page. We use the dcbz instruction on the destination
718 * to reduce memory traffic (it eliminates the unnecessary reads of
719 * the destination into cache). This requires that the destination
722 #define COPY_16_BYTES \
737 /* don't use prefetch on 8xx */
738 li r0,4096/L1_CACHE_BYTES
744 #else /* not 8xx, we can prefetch */
747 #if MAX_COPY_PREFETCH > 1
748 li r0,MAX_COPY_PREFETCH
752 addi r11,r11,L1_CACHE_BYTES
754 #else /* MAX_COPY_PREFETCH == 1 */
756 li r11,L1_CACHE_BYTES+4
757 #endif /* MAX_COPY_PREFETCH */
758 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
766 #if L1_CACHE_BYTES >= 32
768 #if L1_CACHE_BYTES >= 64
771 #if L1_CACHE_BYTES >= 128
781 crnot 4*cr0+eq,4*cr0+eq
782 li r0,MAX_COPY_PREFETCH
785 #endif /* CONFIG_8xx */
788 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
789 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
791 _GLOBAL(atomic_clear_mask)
798 _GLOBAL(atomic_set_mask)
807 * I/O string operations
809 * insb(port, buf, len)
810 * outsb(port, buf, len)
811 * insw(port, buf, len)
812 * outsw(port, buf, len)
813 * insl(port, buf, len)
814 * outsl(port, buf, len)
815 * insw_ns(port, buf, len)
816 * outsw_ns(port, buf, len)
817 * insl_ns(port, buf, len)
818 * outsl_ns(port, buf, len)
820 * The *_ns versions don't do byte-swapping.
888 _GLOBAL(__ide_mm_insw)
900 _GLOBAL(__ide_mm_outsw)
912 _GLOBAL(__ide_mm_insl)
924 _GLOBAL(__ide_mm_outsl)
937 * Extended precision shifts.
939 * Updated to be valid for shift counts from 0 to 63 inclusive.
942 * R3/R4 has 64 bit value
946 * ashrdi3: arithmetic right shift (sign propagation)
947 * lshrdi3: logical right shift
948 * ashldi3: left shift
952 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
953 addi r7,r5,32 # could be xori, or addi with -32
954 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
955 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
956 sraw r7,r3,r7 # t2 = MSW >> (count-32)
957 or r4,r4,r6 # LSW |= t1
958 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
959 sraw r3,r3,r5 # MSW = MSW >> count
960 or r4,r4,r7 # LSW |= t2
965 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
966 addi r7,r5,32 # could be xori, or addi with -32
967 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
968 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
969 or r3,r3,r6 # MSW |= t1
970 slw r4,r4,r5 # LSW = LSW << count
971 or r3,r3,r7 # MSW |= t2
976 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
977 addi r7,r5,32 # could be xori, or addi with -32
978 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
979 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
980 or r4,r4,r6 # LSW |= t1
981 srw r3,r3,r5 # MSW = MSW >> count
982 or r4,r4,r7 # LSW |= t2
992 mr r3,r1 /* Close enough */
996 * Create a kernel thread
997 * kernel_thread(fn, arg, flags)
999 _GLOBAL(kernel_thread)
1003 mr r30,r3 /* function */
1004 mr r31,r4 /* argument */
1005 ori r3,r5,CLONE_VM /* flags */
1006 oris r3,r3,CLONE_UNTRACED>>16
1007 li r4,0 /* new sp (unused) */
1010 cmpwi 0,r3,0 /* parent or child? */
1011 bne 1f /* return if parent */
1012 li r0,0 /* make top-level stack frame */
1014 mtlr r30 /* fn addr in lr */
1015 mr r3,r31 /* load arg and call fn */
1018 li r0,__NR_exit /* exit if function returns */
1034 * This routine is just here to keep GCC happy - sigh...