system.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  3. */
  4. #ifndef _ASM_POWERPC_SYSTEM_H
  5. #define _ASM_POWERPC_SYSTEM_H
  6. #include <linux/config.h>
  7. #include <linux/kernel.h>
  8. #include <asm/hw_irq.h>
  9. #include <asm/atomic.h>
  10. /*
  11. * Memory barrier.
  12. * The sync instruction guarantees that all memory accesses initiated
  13. * by this processor have been performed (with respect to all other
  14. * mechanisms that access memory). The eieio instruction is a barrier
  15. * providing an ordering (separately) for (a) cacheable stores and (b)
  16. * loads and stores to non-cacheable memory (e.g. I/O devices).
  17. *
  18. * mb() prevents loads and stores being reordered across this point.
  19. * rmb() prevents loads being reordered across this point.
  20. * wmb() prevents stores being reordered across this point.
  21. * read_barrier_depends() prevents data-dependent loads being reordered
  22. * across this point (nop on PPC).
  23. *
  24. * We have to use the sync instructions for mb(), since lwsync doesn't
  25. * order loads with respect to previous stores. Lwsync is fine for
  26. * rmb(), though. Note that lwsync is interpreted as sync by
  27. * 32-bit and older 64-bit CPUs.
  28. *
  29. * For wmb(), we use sync since wmb is used in drivers to order
  30. * stores to system memory with respect to writes to the device.
  31. * However, smp_wmb() can be a lighter-weight eieio barrier on
  32. * SMP since it is only used to order updates to system memory.
  33. */
  34. #define mb() __asm__ __volatile__ ("sync" : : : "memory")
  35. #define rmb() __asm__ __volatile__ ("lwsync" : : : "memory")
  36. #define wmb() __asm__ __volatile__ ("sync" : : : "memory")
  37. #define read_barrier_depends() do { } while(0)
  38. #define set_mb(var, value) do { var = value; mb(); } while (0)
  39. #define set_wmb(var, value) do { var = value; wmb(); } while (0)
  40. #ifdef CONFIG_SMP
  41. #define smp_mb() mb()
  42. #define smp_rmb() rmb()
  43. #define smp_wmb() __asm__ __volatile__ ("eieio" : : : "memory")
  44. #define smp_read_barrier_depends() read_barrier_depends()
  45. #else
  46. #define smp_mb() barrier()
  47. #define smp_rmb() barrier()
  48. #define smp_wmb() barrier()
  49. #define smp_read_barrier_depends() do { } while(0)
  50. #endif /* CONFIG_SMP */
  51. #ifdef __KERNEL__
  52. struct task_struct;
  53. struct pt_regs;
  54. #ifdef CONFIG_DEBUGGER
  55. extern int (*__debugger)(struct pt_regs *regs);
  56. extern int (*__debugger_ipi)(struct pt_regs *regs);
  57. extern int (*__debugger_bpt)(struct pt_regs *regs);
  58. extern int (*__debugger_sstep)(struct pt_regs *regs);
  59. extern int (*__debugger_iabr_match)(struct pt_regs *regs);
  60. extern int (*__debugger_dabr_match)(struct pt_regs *regs);
  61. extern int (*__debugger_fault_handler)(struct pt_regs *regs);
  62. #define DEBUGGER_BOILERPLATE(__NAME) \
  63. static inline int __NAME(struct pt_regs *regs) \
  64. { \
  65. if (unlikely(__ ## __NAME)) \
  66. return __ ## __NAME(regs); \
  67. return 0; \
  68. }
  69. DEBUGGER_BOILERPLATE(debugger)
  70. DEBUGGER_BOILERPLATE(debugger_ipi)
  71. DEBUGGER_BOILERPLATE(debugger_bpt)
  72. DEBUGGER_BOILERPLATE(debugger_sstep)
  73. DEBUGGER_BOILERPLATE(debugger_iabr_match)
  74. DEBUGGER_BOILERPLATE(debugger_dabr_match)
  75. DEBUGGER_BOILERPLATE(debugger_fault_handler)
  76. #ifdef CONFIG_XMON
  77. extern void xmon_init(int enable);
  78. #endif
  79. #else
  80. static inline int debugger(struct pt_regs *regs) { return 0; }
  81. static inline int debugger_ipi(struct pt_regs *regs) { return 0; }
  82. static inline int debugger_bpt(struct pt_regs *regs) { return 0; }
  83. static inline int debugger_sstep(struct pt_regs *regs) { return 0; }
  84. static inline int debugger_iabr_match(struct pt_regs *regs) { return 0; }
  85. static inline int debugger_dabr_match(struct pt_regs *regs) { return 0; }
  86. static inline int debugger_fault_handler(struct pt_regs *regs) { return 0; }
  87. #endif
  88. extern int set_dabr(unsigned long dabr);
  89. extern void print_backtrace(unsigned long *);
  90. extern void show_regs(struct pt_regs * regs);
  91. extern void flush_instruction_cache(void);
  92. extern void hard_reset_now(void);
  93. extern void poweroff_now(void);
  94. #ifdef CONFIG_6xx
  95. extern long _get_L2CR(void);
  96. extern long _get_L3CR(void);
  97. extern void _set_L2CR(unsigned long);
  98. extern void _set_L3CR(unsigned long);
  99. #else
  100. #define _get_L2CR() 0L
  101. #define _get_L3CR() 0L
  102. #define _set_L2CR(val) do { } while(0)
  103. #define _set_L3CR(val) do { } while(0)
  104. #endif
  105. extern void via_cuda_init(void);
  106. extern void read_rtc_time(void);
  107. extern void pmac_find_display(void);
  108. extern void giveup_fpu(struct task_struct *);
  109. extern void disable_kernel_fp(void);
  110. extern void enable_kernel_fp(void);
  111. extern void flush_fp_to_thread(struct task_struct *);
  112. extern void enable_kernel_altivec(void);
  113. extern void giveup_altivec(struct task_struct *);
  114. extern void load_up_altivec(struct task_struct *);
  115. extern int emulate_altivec(struct pt_regs *);
  116. extern void giveup_spe(struct task_struct *);
  117. extern void load_up_spe(struct task_struct *);
  118. extern int fix_alignment(struct pt_regs *);
  119. extern void cvt_fd(float *from, double *to, struct thread_struct *thread);
  120. extern void cvt_df(double *from, float *to, struct thread_struct *thread);
  121. #ifdef CONFIG_ALTIVEC
  122. extern void flush_altivec_to_thread(struct task_struct *);
  123. #else
  124. static inline void flush_altivec_to_thread(struct task_struct *t)
  125. {
  126. }
  127. #endif
  128. #ifdef CONFIG_SPE
  129. extern void flush_spe_to_thread(struct task_struct *);
  130. #else
  131. static inline void flush_spe_to_thread(struct task_struct *t)
  132. {
  133. }
  134. #endif
  135. extern int call_rtas(const char *, int, int, unsigned long *, ...);
  136. extern void cacheable_memzero(void *p, unsigned int nb);
  137. extern void *cacheable_memcpy(void *, const void *, unsigned int);
  138. extern int do_page_fault(struct pt_regs *, unsigned long, unsigned long);
  139. extern void bad_page_fault(struct pt_regs *, unsigned long, int);
  140. extern int die(const char *, struct pt_regs *, long);
  141. extern void _exception(int, struct pt_regs *, int, unsigned long);
  142. #ifdef CONFIG_BOOKE_WDT
  143. extern u32 booke_wdt_enabled;
  144. extern u32 booke_wdt_period;
  145. #endif /* CONFIG_BOOKE_WDT */
  146. /* EBCDIC -> ASCII conversion for [0-9A-Z] on iSeries */
  147. extern unsigned char e2a(unsigned char);
  148. struct device_node;
  149. extern void note_scsi_host(struct device_node *, void *);
  150. extern struct task_struct *__switch_to(struct task_struct *,
  151. struct task_struct *);
  152. #define switch_to(prev, next, last) ((last) = __switch_to((prev), (next)))
  153. struct thread_struct;
  154. extern struct task_struct *_switch(struct thread_struct *prev,
  155. struct thread_struct *next);
  156. extern unsigned int rtas_data;
  157. extern int mem_init_done; /* set on boot once kmalloc can be called */
  158. extern unsigned long memory_limit;
  159. extern unsigned long klimit;
  160. extern int powersave_nap; /* set if nap mode can be used in idle loop */
  161. /*
  162. * Atomic exchange
  163. *
  164. * Changes the memory location '*ptr' to be val and returns
  165. * the previous value stored there.
  166. */
  167. static __inline__ unsigned long
  168. __xchg_u32(volatile void *p, unsigned long val)
  169. {
  170. unsigned long prev;
  171. __asm__ __volatile__(
  172. EIEIO_ON_SMP
  173. "1: lwarx %0,0,%2 \n"
  174. PPC405_ERR77(0,%2)
  175. " stwcx. %3,0,%2 \n\
  176. bne- 1b"
  177. ISYNC_ON_SMP
  178. : "=&r" (prev), "=m" (*(volatile unsigned int *)p)
  179. : "r" (p), "r" (val), "m" (*(volatile unsigned int *)p)
  180. : "cc", "memory");
  181. return prev;
  182. }
  183. #ifdef CONFIG_PPC64
  184. static __inline__ unsigned long
  185. __xchg_u64(volatile void *p, unsigned long val)
  186. {
  187. unsigned long prev;
  188. __asm__ __volatile__(
  189. EIEIO_ON_SMP
  190. "1: ldarx %0,0,%2 \n"
  191. PPC405_ERR77(0,%2)
  192. " stdcx. %3,0,%2 \n\
  193. bne- 1b"
  194. ISYNC_ON_SMP
  195. : "=&r" (prev), "=m" (*(volatile unsigned long *)p)
  196. : "r" (p), "r" (val), "m" (*(volatile unsigned long *)p)
  197. : "cc", "memory");
  198. return prev;
  199. }
  200. #endif
  201. /*
  202. * This function doesn't exist, so you'll get a linker error
  203. * if something tries to do an invalid xchg().
  204. */
  205. extern void __xchg_called_with_bad_pointer(void);
  206. static __inline__ unsigned long
  207. __xchg(volatile void *ptr, unsigned long x, unsigned int size)
  208. {
  209. switch (size) {
  210. case 4:
  211. return __xchg_u32(ptr, x);
  212. #ifdef CONFIG_PPC64
  213. case 8:
  214. return __xchg_u64(ptr, x);
  215. #endif
  216. }
  217. __xchg_called_with_bad_pointer();
  218. return x;
  219. }
  220. #define xchg(ptr,x) \
  221. ({ \
  222. __typeof__(*(ptr)) _x_ = (x); \
  223. (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
  224. })
  225. #define tas(ptr) (xchg((ptr),1))
  226. /*
  227. * Compare and exchange - if *p == old, set it to new,
  228. * and return the old value of *p.
  229. */
  230. #define __HAVE_ARCH_CMPXCHG 1
  231. static __inline__ unsigned long
  232. __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)
  233. {
  234. unsigned int prev;
  235. __asm__ __volatile__ (
  236. EIEIO_ON_SMP
  237. "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
  238. cmpw 0,%0,%3\n\
  239. bne- 2f\n"
  240. PPC405_ERR77(0,%2)
  241. " stwcx. %4,0,%2\n\
  242. bne- 1b"
  243. ISYNC_ON_SMP
  244. "\n\
  245. 2:"
  246. : "=&r" (prev), "=m" (*p)
  247. : "r" (p), "r" (old), "r" (new), "m" (*p)
  248. : "cc", "memory");
  249. return prev;
  250. }
  251. #ifdef CONFIG_PPC64
  252. static __inline__ unsigned long
  253. __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)
  254. {
  255. unsigned long prev;
  256. __asm__ __volatile__ (
  257. EIEIO_ON_SMP
  258. "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
  259. cmpd 0,%0,%3\n\
  260. bne- 2f\n\
  261. stdcx. %4,0,%2\n\
  262. bne- 1b"
  263. ISYNC_ON_SMP
  264. "\n\
  265. 2:"
  266. : "=&r" (prev), "=m" (*p)
  267. : "r" (p), "r" (old), "r" (new), "m" (*p)
  268. : "cc", "memory");
  269. return prev;
  270. }
  271. #endif
  272. /* This function doesn't exist, so you'll get a linker error
  273. if something tries to do an invalid cmpxchg(). */
  274. extern void __cmpxchg_called_with_bad_pointer(void);
  275. static __inline__ unsigned long
  276. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new,
  277. unsigned int size)
  278. {
  279. switch (size) {
  280. case 4:
  281. return __cmpxchg_u32(ptr, old, new);
  282. #ifdef CONFIG_PPC64
  283. case 8:
  284. return __cmpxchg_u64(ptr, old, new);
  285. #endif
  286. }
  287. __cmpxchg_called_with_bad_pointer();
  288. return old;
  289. }
  290. #define cmpxchg(ptr,o,n) \
  291. ({ \
  292. __typeof__(*(ptr)) _o_ = (o); \
  293. __typeof__(*(ptr)) _n_ = (n); \
  294. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  295. (unsigned long)_n_, sizeof(*(ptr))); \
  296. })
  297. #ifdef CONFIG_PPC64
  298. /*
  299. * We handle most unaligned accesses in hardware. On the other hand
  300. * unaligned DMA can be very expensive on some ppc64 IO chips (it does
  301. * powers of 2 writes until it reaches sufficient alignment).
  302. *
  303. * Based on this we disable the IP header alignment in network drivers.
  304. */
  305. #define NET_IP_ALIGN 0
  306. #endif
  307. #define arch_align_stack(x) (x)
  308. /* Used in very early kernel initialization. */
  309. extern unsigned long reloc_offset(void);
  310. extern unsigned long add_reloc_offset(unsigned long);
  311. extern void reloc_got2(unsigned long);
  312. #define PTRRELOC(x) ((typeof(x)) add_reloc_offset((unsigned long)(x)))
  313. static inline void create_instruction(unsigned long addr, unsigned int instr)
  314. {
  315. unsigned int *p;
  316. p = (unsigned int *)addr;
  317. *p = instr;
  318. asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (p));
  319. }
  320. /* Flags for create_branch:
  321. * "b" == create_branch(addr, target, 0);
  322. * "ba" == create_branch(addr, target, BRANCH_ABSOLUTE);
  323. * "bl" == create_branch(addr, target, BRANCH_SET_LINK);
  324. * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK);
  325. */
  326. #define BRANCH_SET_LINK 0x1
  327. #define BRANCH_ABSOLUTE 0x2
  328. static inline void create_branch(unsigned long addr,
  329. unsigned long target, int flags)
  330. {
  331. unsigned int instruction;
  332. if (! (flags & BRANCH_ABSOLUTE))
  333. target = target - addr;
  334. /* Mask out the flags and target, so they don't step on each other. */
  335. instruction = 0x48000000 | (flags & 0x3) | (target & 0x03FFFFFC);
  336. create_instruction(addr, instruction);
  337. }
  338. static inline void create_function_call(unsigned long addr, void * func)
  339. {
  340. unsigned long func_addr;
  341. #ifdef CONFIG_PPC64
  342. /*
  343. * On PPC64 the function pointer actually points to the function's
  344. * descriptor. The first entry in the descriptor is the address
  345. * of the function text.
  346. */
  347. func_addr = *(unsigned long *)func;
  348. #else
  349. func_addr = (unsigned long)func;
  350. #endif
  351. create_branch(addr, func_addr, BRANCH_SET_LINK);
  352. }
  353. #endif /* __KERNEL__ */
  354. #endif /* _ASM_POWERPC_SYSTEM_H */