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>
19 #include <asm/processor.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>
29 #define ISYNC_8xx isync
44 * Returns (address we're running at) - (address we were linked at)
45 * for use before the text and data are mapped to KERNELBASE.
58 * add_reloc_offset(x) returns x + reloc_offset().
60 _GLOBAL(add_reloc_offset)
72 * sub_reloc_offset(x) returns x - reloc_offset().
74 _GLOBAL(sub_reloc_offset)
86 * reloc_got2 runs through the .got2 section adding an offset
91 lis r7,__got2_start@ha
92 addi r7,r7,__got2_start@l
94 addi r8,r8,__got2_end@l
115 * called with r3 = data offset and r4 = CPU number
118 _GLOBAL(identify_cpu)
119 addis r8,r3,cpu_specs@ha
120 addi r8,r8,cpu_specs@l
123 lwz r5,CPU_SPEC_PVR_MASK(r8)
125 lwz r6,CPU_SPEC_PVR_VALUE(r8)
128 addi r8,r8,CPU_SPEC_ENTRY_SIZE
131 addis r6,r3,cur_cpu_spec@ha
132 addi r6,r6,cur_cpu_spec@l
138 * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
139 * and writes nop's over sections of code that don't apply for this cpu.
140 * r3 = data offset (not changed)
142 _GLOBAL(do_cpu_ftr_fixups)
143 /* Get CPU 0 features */
144 addis r6,r3,cur_cpu_spec@ha
145 addi r6,r6,cur_cpu_spec@l
148 lwz r4,CPU_SPEC_FEATURES(r4)
150 /* Get the fixup table */
151 addis r6,r3,__start___ftr_fixup@ha
152 addi r6,r6,__start___ftr_fixup@l
153 addis r7,r3,__stop___ftr_fixup@ha
154 addi r7,r7,__stop___ftr_fixup@l
160 lwz r8,-16(r6) /* mask */
162 lwz r9,-12(r6) /* value */
165 lwz r8,-8(r6) /* section begin */
166 lwz r9,-4(r6) /* section end */
169 /* write nops over the section of code */
170 /* todo: if large section, add a branch at the start of it */
174 lis r0,0x60000000@h /* nop */
176 andi. r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
178 dcbst 0,r8 /* suboptimal, but simpler */
183 sync /* additional sync needed on g4 */
188 * call_setup_cpu - call the setup_cpu function for this cpu
189 * r3 = data offset, r24 = cpu number
191 * Setup function is called with:
193 * r4 = ptr to CPU spec (relocated)
195 _GLOBAL(call_setup_cpu)
196 addis r4,r3,cur_cpu_spec@ha
197 addi r4,r4,cur_cpu_spec@l
200 lwz r5,CPU_SPEC_SETUP(r4)
208 * complement mask on the msr then "or" some values on.
209 * _nmask_and_or_msr(nmask, value_to_or)
211 _GLOBAL(_nmask_and_or_msr)
212 mfmsr r0 /* Get current msr */
213 andc r0,r0,r3 /* And off the bits set in r3 (first parm) */
214 or r0,r0,r4 /* Or on the bits in r4 (second parm) */
215 SYNC /* Some chip revs have problems here... */
216 mtmsr r0 /* Update machine state */
225 #if defined(CONFIG_40x)
226 sync /* Flush to memory before changing mapping */
228 isync /* Flush shadow TLB */
229 #elif defined(CONFIG_44x)
233 /* Load high watermark */
234 lis r4,tlb_44x_hwater@ha
235 lwz r5,tlb_44x_hwater@l(r4)
237 1: tlbwe r3,r3,PPC44x_TLB_PAGEID
243 #elif defined(CONFIG_FSL_BOOKE)
244 /* Invalidate all entries in TLB0 */
247 /* Invalidate all entries in TLB1 */
250 /* Invalidate all entries in TLB2 */
253 /* Invalidate all entries in TLB3 */
259 #endif /* CONFIG_SMP */
260 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
261 #if defined(CONFIG_SMP)
267 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
268 rlwinm r0,r0,0,28,26 /* clear DR */
272 lis r9,mmu_hash_lock@h
273 ori r9,r9,mmu_hash_lock@l
285 stw r0,0(r9) /* clear mmu_hash_lock */
289 #else /* CONFIG_SMP */
293 #endif /* CONFIG_SMP */
294 #endif /* ! defined(CONFIG_40x) */
298 * Flush MMU TLB for a particular address
301 #if defined(CONFIG_40x)
305 /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
306 * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
308 tlbwe r3, r3, TLB_TAG
311 #elif defined(CONFIG_44x)
313 mfspr r5,SPRN_PID /* Get PID */
314 rlwimi r4,r5,0,24,31 /* Set TID */
320 /* There are only 64 TLB entries, so r3 < 64,
321 * which means bit 22, is clear. Since 22 is
322 * the V bit in the TLB_PAGEID, loading this
323 * value will invalidate the TLB entry.
325 tlbwe r3, r3, PPC44x_TLB_PAGEID
328 #elif defined(CONFIG_FSL_BOOKE)
329 rlwinm r4, r3, 0, 0, 19
330 ori r5, r4, 0x08 /* TLBSEL = 1 */
331 ori r6, r4, 0x10 /* TLBSEL = 2 */
332 ori r7, r4, 0x18 /* TLBSEL = 3 */
338 #if defined(CONFIG_SMP)
340 #endif /* CONFIG_SMP */
341 #else /* !(CONFIG_40x || CONFIG_44x || CONFIG_FSL_BOOKE) */
342 #if defined(CONFIG_SMP)
348 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
349 rlwinm r0,r0,0,28,26 /* clear DR */
353 lis r9,mmu_hash_lock@h
354 ori r9,r9,mmu_hash_lock@l
366 stw r0,0(r9) /* clear mmu_hash_lock */
370 #else /* CONFIG_SMP */
373 #endif /* CONFIG_SMP */
374 #endif /* ! CONFIG_40x */
378 * Flush instruction cache.
379 * This is a no-op on the 601.
381 _GLOBAL(flush_instruction_cache)
382 #if defined(CONFIG_8xx)
385 mtspr SPRN_IC_CST, r5
386 #elif defined(CONFIG_4xx)
398 #elif CONFIG_FSL_BOOKE
401 ori r3,r3,L1CSR0_CFI|L1CSR0_CLFC
402 /* msync; isync recommended here */
406 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
408 ori r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
412 rlwinm r3,r3,16,16,31
414 beqlr /* for 601, do nothing */
415 /* 603/604 processor - use invalidate-all bit in HID0 */
419 #endif /* CONFIG_8xx/4xx */
424 * Write any modified data cache blocks out to memory
425 * and invalidate the corresponding instruction cache blocks.
426 * This is a no-op on the 601.
428 * __flush_icache_range(unsigned long start, unsigned long stop)
430 _GLOBAL(__flush_icache_range)
432 blr /* for 601, do nothing */
433 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
434 li r5,L1_CACHE_BYTES-1
438 srwi. r4,r4,L1_CACHE_SHIFT
443 addi r3,r3,L1_CACHE_BYTES
445 sync /* wait for dcbst's to get to ram */
448 addi r6,r6,L1_CACHE_BYTES
450 sync /* additional sync needed on g4 */
454 * Write any modified data cache blocks out to memory.
455 * Does not invalidate the corresponding cache lines (especially for
456 * any corresponding instruction cache).
458 * clean_dcache_range(unsigned long start, unsigned long stop)
460 _GLOBAL(clean_dcache_range)
461 li r5,L1_CACHE_BYTES-1
465 srwi. r4,r4,L1_CACHE_SHIFT
470 addi r3,r3,L1_CACHE_BYTES
472 sync /* wait for dcbst's to get to ram */
476 * Write any modified data cache blocks out to memory and invalidate them.
477 * Does not invalidate the corresponding instruction cache blocks.
479 * flush_dcache_range(unsigned long start, unsigned long stop)
481 _GLOBAL(flush_dcache_range)
482 li r5,L1_CACHE_BYTES-1
486 srwi. r4,r4,L1_CACHE_SHIFT
491 addi r3,r3,L1_CACHE_BYTES
493 sync /* wait for dcbst's to get to ram */
497 * Like above, but invalidate the D-cache. This is used by the 8xx
498 * to invalidate the cache so the PPC core doesn't get stale data
499 * from the CPM (no cache snooping here :-).
501 * invalidate_dcache_range(unsigned long start, unsigned long stop)
503 _GLOBAL(invalidate_dcache_range)
504 li r5,L1_CACHE_BYTES-1
508 srwi. r4,r4,L1_CACHE_SHIFT
513 addi r3,r3,L1_CACHE_BYTES
515 sync /* wait for dcbi's to get to ram */
518 #ifdef CONFIG_NOT_COHERENT_CACHE
520 * 40x cores have 8K or 16K dcache and 32 byte line size.
521 * 44x has a 32K dcache and 32 byte line size.
522 * 8xx has 1, 2, 4, 8K variants.
523 * For now, cover the worst case of the 44x.
524 * Must be called with external interrupts disabled.
526 #define CACHE_NWAYS 64
527 #define CACHE_NLINES 16
529 _GLOBAL(flush_dcache_all)
530 li r4, (2 * CACHE_NWAYS * CACHE_NLINES)
533 1: lwz r3, 0(r5) /* Load one word from every line */
534 addi r5, r5, L1_CACHE_BYTES
537 #endif /* CONFIG_NOT_COHERENT_CACHE */
540 * Flush a particular page from the data cache to RAM.
541 * Note: this is necessary because the instruction cache does *not*
542 * snoop from the data cache.
543 * This is a no-op on the 601 which has a unified cache.
545 * void __flush_dcache_icache(void *page)
547 _GLOBAL(__flush_dcache_icache)
549 blr /* for 601, do nothing */
550 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
551 rlwinm r3,r3,0,0,19 /* Get page base address */
552 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
555 0: dcbst 0,r3 /* Write line to ram */
556 addi r3,r3,L1_CACHE_BYTES
561 addi r6,r6,L1_CACHE_BYTES
568 * Flush a particular page from the data cache to RAM, identified
569 * by its physical address. We turn off the MMU so we can just use
570 * the physical address (this may be a highmem page without a kernel
573 * void __flush_dcache_icache_phys(unsigned long physaddr)
575 _GLOBAL(__flush_dcache_icache_phys)
577 blr /* for 601, do nothing */
578 END_FTR_SECTION_IFCLR(CPU_FTR_SPLIT_ID_CACHE)
580 rlwinm r0,r10,0,28,26 /* clear DR */
583 rlwinm r3,r3,0,0,19 /* Get page base address */
584 li r4,4096/L1_CACHE_BYTES /* Number of lines in a page */
587 0: dcbst 0,r3 /* Write line to ram */
588 addi r3,r3,L1_CACHE_BYTES
593 addi r6,r6,L1_CACHE_BYTES
596 mtmsr r10 /* restore DR */
601 * Clear pages using the dcbz instruction, which doesn't cause any
602 * memory traffic (except to write out any cache lines which get
603 * displaced). This only works on cacheable memory.
605 * void clear_pages(void *page, int order) ;
608 li r0,4096/L1_CACHE_BYTES
620 addi r3,r3,L1_CACHE_BYTES
625 * Copy a whole page. We use the dcbz instruction on the destination
626 * to reduce memory traffic (it eliminates the unnecessary reads of
627 * the destination into cache). This requires that the destination
630 #define COPY_16_BYTES \
645 /* don't use prefetch on 8xx */
646 li r0,4096/L1_CACHE_BYTES
652 #else /* not 8xx, we can prefetch */
655 #if MAX_COPY_PREFETCH > 1
656 li r0,MAX_COPY_PREFETCH
660 addi r11,r11,L1_CACHE_BYTES
662 #else /* MAX_COPY_PREFETCH == 1 */
664 li r11,L1_CACHE_BYTES+4
665 #endif /* MAX_COPY_PREFETCH */
666 li r0,4096/L1_CACHE_BYTES - MAX_COPY_PREFETCH
674 #if L1_CACHE_BYTES >= 32
676 #if L1_CACHE_BYTES >= 64
679 #if L1_CACHE_BYTES >= 128
689 crnot 4*cr0+eq,4*cr0+eq
690 li r0,MAX_COPY_PREFETCH
693 #endif /* CONFIG_8xx */
696 * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
697 * void atomic_set_mask(atomic_t mask, atomic_t *addr);
699 _GLOBAL(atomic_clear_mask)
706 _GLOBAL(atomic_set_mask)
715 * I/O string operations
717 * insb(port, buf, len)
718 * outsb(port, buf, len)
719 * insw(port, buf, len)
720 * outsw(port, buf, len)
721 * insl(port, buf, len)
722 * outsl(port, buf, len)
723 * insw_ns(port, buf, len)
724 * outsw_ns(port, buf, len)
725 * insl_ns(port, buf, len)
726 * outsl_ns(port, buf, len)
728 * The *_ns versions don't do byte-swapping.
742 .section __ex_table, "a"
763 .section __ex_table, "a"
784 .section __ex_table, "a"
805 .section __ex_table, "a"
826 .section __ex_table, "a"
847 .section __ex_table, "a"
856 _GLOBAL(__ide_mm_insw)
869 .section __ex_table, "a"
878 _GLOBAL(__ide_mm_outsw)
891 .section __ex_table, "a"
900 _GLOBAL(__ide_mm_insl)
913 .section __ex_table, "a"
922 _GLOBAL(__ide_mm_outsl)
935 .section __ex_table, "a"
945 * Extended precision shifts.
947 * Updated to be valid for shift counts from 0 to 63 inclusive.
950 * R3/R4 has 64 bit value
954 * ashrdi3: arithmetic right shift (sign propagation)
955 * lshrdi3: logical right shift
956 * ashldi3: left shift
960 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
961 addi r7,r5,32 # could be xori, or addi with -32
962 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
963 rlwinm r8,r7,0,32 # t3 = (count < 32) ? 32 : 0
964 sraw r7,r3,r7 # t2 = MSW >> (count-32)
965 or r4,r4,r6 # LSW |= t1
966 slw r7,r7,r8 # t2 = (count < 32) ? 0 : t2
967 sraw r3,r3,r5 # MSW = MSW >> count
968 or r4,r4,r7 # LSW |= t2
973 slw r3,r3,r5 # MSW = count > 31 ? 0 : MSW << count
974 addi r7,r5,32 # could be xori, or addi with -32
975 srw r6,r4,r6 # t1 = count > 31 ? 0 : LSW >> (32-count)
976 slw r7,r4,r7 # t2 = count < 32 ? 0 : LSW << (count-32)
977 or r3,r3,r6 # MSW |= t1
978 slw r4,r4,r5 # LSW = LSW << count
979 or r3,r3,r7 # MSW |= t2
984 srw r4,r4,r5 # LSW = count > 31 ? 0 : LSW >> count
985 addi r7,r5,32 # could be xori, or addi with -32
986 slw r6,r3,r6 # t1 = count > 31 ? 0 : MSW << (32-count)
987 srw r7,r3,r7 # t2 = count < 32 ? 0 : MSW >> (count-32)
988 or r4,r4,r6 # LSW |= t1
989 srw r3,r3,r5 # MSW = MSW >> count
990 or r4,r4,r7 # LSW |= t2
1000 mr r3,r1 /* Close enough */
1004 * Create a kernel thread
1005 * kernel_thread(fn, arg, flags)
1007 _GLOBAL(kernel_thread)
1011 mr r30,r3 /* function */
1012 mr r31,r4 /* argument */
1013 ori r3,r5,CLONE_VM /* flags */
1014 oris r3,r3,CLONE_UNTRACED>>16
1015 li r4,0 /* new sp (unused) */
1018 cmpwi 0,r3,0 /* parent or child? */
1019 bne 1f /* return if parent */
1020 li r0,0 /* make top-level stack frame */
1022 mtlr r30 /* fn addr in lr */
1023 mr r3,r31 /* load arg and call fn */
1026 li r0,__NR_exit /* exit if function returns */
1035 * This routine is just here to keep GCC happy - sigh...
1040 #define SYSCALL(name) \
1042 li r0,__NR_##name; \
1046 stw r3,errno@l(r4); \