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/sys.h>
16 #include <asm/unistd.h>
17 #include <asm/errno.h>
18 #include <asm/processor.h>
20 #include <asm/cache.h>
21 #include <asm/cputable.h>
23 #include <asm/ppc_asm.h>
24 #include <asm/thread_info.h>
25 #include <asm/asm-offsets.h>
28 #define ISYNC_8xx isync
43 * Returns (address we're running at) - (address we were linked at)
44 * for use before the text and data are mapped to KERNELBASE.
57 * add_reloc_offset(x) returns x + reloc_offset().
59 _GLOBAL(add_reloc_offset)
71 * sub_reloc_offset(x) returns x - reloc_offset().
73 _GLOBAL(sub_reloc_offset)
85 * reloc_got2 runs through the .got2 section adding an offset
90 lis r7,__got2_start@ha
91 addi r7,r7,__got2_start@l
93 addi r8,r8,__got2_end@l
114 * called with r3 = data offset and r4 = CPU number
117 _GLOBAL(identify_cpu)
118 addis r8,r3,cpu_specs@ha
119 addi r8,r8,cpu_specs@l
122 lwz r5,CPU_SPEC_PVR_MASK(r8)
124 lwz r6,CPU_SPEC_PVR_VALUE(r8)
127 addi r8,r8,CPU_SPEC_ENTRY_SIZE
130 addis r6,r3,cur_cpu_spec@ha
131 addi r6,r6,cur_cpu_spec@l
137 * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
138 * and writes nop's over sections of code that don't apply for this cpu.
139 * r3 = data offset (not changed)
141 _GLOBAL(do_cpu_ftr_fixups)
142 /* Get CPU 0 features */
143 addis r6,r3,cur_cpu_spec@ha
144 addi r6,r6,cur_cpu_spec@l
147 lwz r4,CPU_SPEC_FEATURES(r4)
149 /* Get the fixup table */
150 addis r6,r3,__start___ftr_fixup@ha
151 addi r6,r6,__start___ftr_fixup@l
152 addis r7,r3,__stop___ftr_fixup@ha
153 addi r7,r7,__stop___ftr_fixup@l
159 lwz r8,-16(r6) /* mask */
161 lwz r9,-12(r6) /* value */
164 lwz r8,-8(r6) /* section begin */
165 lwz r9,-4(r6) /* section end */
168 /* write nops over the section of code */
169 /* todo: if large section, add a branch at the start of it */
173 lis r0,0x60000000@h /* nop */
175 andi. r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
177 dcbst 0,r8 /* suboptimal, but simpler */
182 sync /* additional sync needed on g4 */
187 * call_setup_cpu - call the setup_cpu function for this cpu
188 * r3 = data offset, r24 = cpu number
190 * Setup function is called with:
192 * r4 = ptr to CPU spec (relocated)
194 _GLOBAL(call_setup_cpu)
195 addis r4,r3,cur_cpu_spec@ha
196 addi r4,r4,cur_cpu_spec@l
199 lwz r5,CPU_SPEC_SETUP(r4)
207 * complement mask on the msr then "or" some values on.
208 * _nmask_and_or_msr(nmask, value_to_or)
210 _GLOBAL(_nmask_and_or_msr)
211 mfmsr r0 /* Get current msr */
212 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
213 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
214 SYNC /* Some chip revs have problems here... */
215 mtmsr r0 /* Update machine state */
224 #if defined(CONFIG_40x)
225 sync /* Flush to memory before changing mapping */
227 isync /* Flush shadow TLB */
228 #elif defined(CONFIG_44x)
232 /* Load high watermark */
233 lis r4,tlb_44x_hwater@ha
234 lwz r5,tlb_44x_hwater@l(r4)
236 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
242 #elif defined(CONFIG_FSL_BOOKE)
243 /* Invalidate all entries in TLB0 */
246 /* Invalidate all entries in TLB1 */
249 /* Invalidate all entries in TLB2 */
252 /* Invalidate all entries in TLB3 */
258 #endif /* CONFIG_SMP */
259 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
260 #if defined(CONFIG_SMP)
266 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
267 rlwinm r0,r0,0,28,26 /* clear DR */
271 lis r9,mmu_hash_lock@h
272 ori r9,r9,mmu_hash_lock@l
284 stw r0,0(r9) /* clear mmu_hash_lock */
288 #else /* CONFIG_SMP */
292 #endif /* CONFIG_SMP */
293 #endif /* ! defined(CONFIG_40x) */
297 * Flush MMU TLB for a particular address
300 #if defined(CONFIG_40x)
304 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
305 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
307 tlbwe r3, r3, TLB_TAG
310 #elif defined(CONFIG_44x)
312 mfspr r5,SPRN_PID /* Get PID */
313 rlwimi r4,r5,0,24,31 /* Set TID */
319 /* There are only 64 TLB entries, so r3 < 64,
320 * which means bit 22, is clear. Since 22 is
321 * the V bit in the TLB_PAGEID, loading this
322 * value will invalidate the TLB entry.
324 tlbwe r3, r3, PPC44x_TLB_PAGEID
327 #elif defined(CONFIG_FSL_BOOKE)
328 rlwinm r4, r3, 0, 0, 19
329 ori r5, r4, 0x08 /* TLBSEL = 1 */
330 ori r6, r4, 0x10 /* TLBSEL = 2 */
331 ori r7, r4, 0x18 /* TLBSEL = 3 */
337 #if defined(CONFIG_SMP)
339 #endif /* CONFIG_SMP */
340 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
341 #if defined(CONFIG_SMP)
347 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
348 rlwinm r0,r0,0,28,26 /* clear DR */
352 lis r9,mmu_hash_lock@h
353 ori r9,r9,mmu_hash_lock@l
365 stw r0,0(r9) /* clear mmu_hash_lock */
369 #else /* CONFIG_SMP */
372 #endif /* CONFIG_SMP */
373 #endif /* ! CONFIG_40x */
377 * Flush instruction cache.
378 * This is a no-op on the 601.
380 _GLOBAL(flush_instruction_cache)
381 #if defined(CONFIG_8xx)
384 mtspr SPRN_IC_CST, r5
385 #elif defined(CONFIG_4xx)
397 #elif CONFIG_FSL_BOOKE
400 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
401 /* msync; isync recommended here */
405 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
407 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
411 rlwinm r3,r3,16,16,31
413 beqlr /* for 601, do nothing */
414 /* 603/604 processor - use invalidate-all bit in HID0 */
418 #endif /* CONFIG_8xx/4xx */
423 * Write any modified data cache blocks out to memory
424 * and invalidate the corresponding instruction cache blocks.
425 * This is a no-op on the 601.
427 * __flush_icache_range(unsigned long start, unsigned long stop)
429 _GLOBAL(__flush_icache_range)
431 blr /* for 601, do nothing */
432 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
433 li r5,L1_CACHE_BYTES-1
437 srwi. r4,r4,L1_CACHE_SHIFT
442 addi r3,r3,L1_CACHE_BYTES
444 sync /* wait for dcbst's to get to ram */
447 addi r6,r6,L1_CACHE_BYTES
449 sync /* additional sync needed on g4 */
453 * Write any modified data cache blocks out to memory.
454 * Does not invalidate the corresponding cache lines (especially for
455 * any corresponding instruction cache).
457 * clean_dcache_range(unsigned long start, unsigned long stop)
459 _GLOBAL(clean_dcache_range)
460 li r5,L1_CACHE_BYTES-1
464 srwi. r4,r4,L1_CACHE_SHIFT
469 addi r3,r3,L1_CACHE_BYTES
471 sync /* wait for dcbst's to get to ram */
475 * Write any modified data cache blocks out to memory and invalidate them.
476 * Does not invalidate the corresponding instruction cache blocks.
478 * flush_dcache_range(unsigned long start, unsigned long stop)
480 _GLOBAL(flush_dcache_range)
481 li r5,L1_CACHE_BYTES-1
485 srwi. r4,r4,L1_CACHE_SHIFT
490 addi r3,r3,L1_CACHE_BYTES
492 sync /* wait for dcbst's to get to ram */
496 * Like above, but invalidate the D-cache. This is used by the 8xx
497 * to invalidate the cache so the PPC core doesn't get stale data
498 * from the CPM (no cache snooping here :-).
500 * invalidate_dcache_range(unsigned long start, unsigned long stop)
502 _GLOBAL(invalidate_dcache_range)
503 li r5,L1_CACHE_BYTES-1
507 srwi. r4,r4,L1_CACHE_SHIFT
512 addi r3,r3,L1_CACHE_BYTES
514 sync /* wait for dcbi's to get to ram */
517 #ifdef CONFIG_NOT_COHERENT_CACHE
519 * 40x cores have 8K or 16K dcache and 32 byte line size.
520 * 44x has a 32K dcache and 32 byte line size.
521 * 8xx has 1, 2, 4, 8K variants.
522 * For now, cover the worst case of the 44x.
523 * Must be called with external interrupts disabled.
525 #define CACHE_NWAYS 64
526 #define CACHE_NLINES 16
528 _GLOBAL(flush_dcache_all)
529 li r4, (2 * CACHE_NWAYS * CACHE_NLINES)
532 1: lwz r3, 0(r5) /* Load one word from every line */
533 addi r5, r5, L1_CACHE_BYTES
536 #endif /* CONFIG_NOT_COHERENT_CACHE */
539 * Flush a particular page from the data cache to RAM.
540 * Note: this is necessary because the instruction cache does *not*
541 * snoop from the data cache.
542 * This is a no-op on the 601 which has a unified cache.
544 * void __flush_dcache_icache(void *page)
546 _GLOBAL(__flush_dcache_icache)
548 blr /* for 601, do nothing */
549 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
550 rlwinm r3,r3,0,0,19 /* Get page base address */
551 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
554 0: dcbst 0,r3 /* Write line to ram */
555 addi r3,r3,L1_CACHE_BYTES
560 addi r6,r6,L1_CACHE_BYTES
567 * Flush a particular page from the data cache to RAM, identified
568 * by its physical address. We turn off the MMU so we can just use
569 * the physical address (this may be a highmem page without a kernel
572 * void __flush_dcache_icache_phys(unsigned long physaddr)
574 _GLOBAL(__flush_dcache_icache_phys)
576 blr /* for 601, do nothing */
577 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
579 rlwinm r0,r10,0,28,26 /* clear DR */
582 rlwinm r3,r3,0,0,19 /* Get page base address */
583 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
586 0: dcbst 0,r3 /* Write line to ram */
587 addi r3,r3,L1_CACHE_BYTES
592 addi r6,r6,L1_CACHE_BYTES
595 mtmsr r10 /* restore DR */
600 * Clear pages using the dcbz instruction, which doesn't cause any
601 * memory traffic (except to write out any cache lines which get
602 * displaced). This only works on cacheable memory.
604 * void clear_pages(void *page, int order) ;
607 li r0,4096/L1_CACHE_BYTES
619 addi r3,r3,L1_CACHE_BYTES
624 * Copy a whole page. We use the dcbz instruction on the destination
625 * to reduce memory traffic (it eliminates the unnecessary reads of
626 * the destination into cache). This requires that the destination
629 #define COPY_16_BYTES \
644 /* don't use prefetch on 8xx */
645 li r0,4096/L1_CACHE_BYTES
651 #else /* not 8xx, we can prefetch */
654 #if MAX_COPY_PREFETCH > 1
655 li r0,MAX_COPY_PREFETCH
659 addi r11,r11,L1_CACHE_BYTES
661 #else /* MAX_COPY_PREFETCH == 1 */
663 li r11,L1_CACHE_BYTES+4
664 #endif /* MAX_COPY_PREFETCH */
665 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
673 #if L1_CACHE_BYTES >= 32
675 #if L1_CACHE_BYTES >= 64
678 #if L1_CACHE_BYTES >= 128
688 crnot 4*cr0+eq,4*cr0+eq
689 li r0,MAX_COPY_PREFETCH
692 #endif /* CONFIG_8xx */
695 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
696 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
698 _GLOBAL(atomic_clear_mask)
705 _GLOBAL(atomic_set_mask)
714 * I/O string operations
716 * insb(port, buf, len)
717 * outsb(port, buf, len)
718 * insw(port, buf, len)
719 * outsw(port, buf, len)
720 * insl(port, buf, len)
721 * outsl(port, buf, len)
722 * insw_ns(port, buf, len)
723 * outsw_ns(port, buf, len)
724 * insl_ns(port, buf, len)
725 * outsl_ns(port, buf, len)
727 * The *_ns versions don't do byte-swapping.
741 .section __ex_table, "a"
762 .section __ex_table, "a"
783 .section __ex_table, "a"
804 .section __ex_table, "a"
825 .section __ex_table, "a"
846 .section __ex_table, "a"
856 * Extended precision shifts.
858 * Updated to be valid for shift counts from 0 to 63 inclusive.
861 * R3/R4 has 64 bit value
865 * ashrdi3: arithmetic right shift (sign propagation)
866 * lshrdi3: logical right shift
867 * ashldi3: left shift
871 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
872 addi r7,r5,32 # could be xori, or addi with -32
873 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
874 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
875 sraw r7,r3,r7 # t2 = MSW >> (count-32)
876 or r4,r4,r6 # LSW |= t1
877 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
878 sraw r3,r3,r5 # MSW = MSW >> count
879 or r4,r4,r7 # LSW |= t2
884 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
885 addi r7,r5,32 # could be xori, or addi with -32
886 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
887 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
888 or r3,r3,r6 # MSW |= t1
889 slw r4,r4,r5 # LSW = LSW << count
890 or r3,r3,r7 # MSW |= t2
895 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
896 addi r7,r5,32 # could be xori, or addi with -32
897 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
898 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
899 or r4,r4,r6 # LSW |= t1
900 srw r3,r3,r5 # MSW = MSW >> count
901 or r4,r4,r7 # LSW |= t2
911 mr r3,r1 /* Close enough */
915 * Create a kernel thread
916 * kernel_thread(fn, arg, flags)
918 _GLOBAL(kernel_thread)
922 mr r30,r3 /* function */
923 mr r31,r4 /* argument */
924 ori r3,r5,CLONE_VM /* flags */
925 oris r3,r3,CLONE_UNTRACED>>16
926 li r4,0 /* new sp (unused) */
929 cmpwi 0,r3,0 /* parent or child? */
930 bne 1f /* return if parent */
931 li r0,0 /* make top-level stack frame */
933 mtlr r30 /* fn addr in lr */
934 mr r3,r31 /* load arg and call fn */
937 li r0,__NR_exit /* exit if function returns */
945 _GLOBAL(kernel_execve)
953 * This routine is just here to keep GCC happy - sigh...