system.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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/kernel.h>
  7. #include <linux/irqflags.h>
  8. #include <asm/hw_irq.h>
  9. /*
  10. * Memory barrier.
  11. * The sync instruction guarantees that all memory accesses initiated
  12. * by this processor have been performed (with respect to all other
  13. * mechanisms that access memory). The eieio instruction is a barrier
  14. * providing an ordering (separately) for (a) cacheable stores and (b)
  15. * loads and stores to non-cacheable memory (e.g. I/O devices).
  16. *
  17. * mb() prevents loads and stores being reordered across this point.
  18. * rmb() prevents loads being reordered across this point.
  19. * wmb() prevents stores being reordered across this point.
  20. * read_barrier_depends() prevents data-dependent loads being reordered
  21. * across this point (nop on PPC).
  22. *
  23. * We have to use the sync instructions for mb(), since lwsync doesn't
  24. * order loads with respect to previous stores. Lwsync is fine for
  25. * rmb(), though. Note that rmb() actually uses a sync on 32-bit
  26. * architectures.
  27. *
  28. * For wmb(), we use sync since wmb is used in drivers to order
  29. * stores to system memory with respect to writes to the device.
  30. * However, smp_wmb() can be a lighter-weight eieio barrier on
  31. * SMP since it is only used to order updates to system memory.
  32. */
  33. #define mb() __asm__ __volatile__ ("sync" : : : "memory")
  34. #define rmb() __asm__ __volatile__ (__stringify(LWSYNC) : : : "memory")
  35. #define wmb() __asm__ __volatile__ ("sync" : : : "memory")
  36. #define read_barrier_depends() do { } while(0)
  37. #define set_mb(var, value) do { var = value; mb(); } while (0)
  38. #ifdef __KERNEL__
  39. #define AT_VECTOR_SIZE_ARCH 6 /* entries in ARCH_DLINFO */
  40. #ifdef CONFIG_SMP
  41. #define smp_mb() mb()
  42. #define smp_rmb() rmb()
  43. #define smp_wmb() eieio()
  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. /*
  52. * This is a barrier which prevents following instructions from being
  53. * started until the value of the argument x is known. For example, if
  54. * x is a variable loaded from memory, this prevents following
  55. * instructions from being executed until the load has been performed.
  56. */
  57. #define data_barrier(x) \
  58. asm volatile("twi 0,%0,0; isync" : : "r" (x) : "memory");
  59. struct task_struct;
  60. struct pt_regs;
  61. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  62. extern int (*__debugger)(struct pt_regs *regs);
  63. extern int (*__debugger_ipi)(struct pt_regs *regs);
  64. extern int (*__debugger_bpt)(struct pt_regs *regs);
  65. extern int (*__debugger_sstep)(struct pt_regs *regs);
  66. extern int (*__debugger_iabr_match)(struct pt_regs *regs);
  67. extern int (*__debugger_dabr_match)(struct pt_regs *regs);
  68. extern int (*__debugger_fault_handler)(struct pt_regs *regs);
  69. #define DEBUGGER_BOILERPLATE(__NAME) \
  70. static inline int __NAME(struct pt_regs *regs) \
  71. { \
  72. if (unlikely(__ ## __NAME)) \
  73. return __ ## __NAME(regs); \
  74. return 0; \
  75. }
  76. DEBUGGER_BOILERPLATE(debugger)
  77. DEBUGGER_BOILERPLATE(debugger_ipi)
  78. DEBUGGER_BOILERPLATE(debugger_bpt)
  79. DEBUGGER_BOILERPLATE(debugger_sstep)
  80. DEBUGGER_BOILERPLATE(debugger_iabr_match)
  81. DEBUGGER_BOILERPLATE(debugger_dabr_match)
  82. DEBUGGER_BOILERPLATE(debugger_fault_handler)
  83. #else
  84. static inline int debugger(struct pt_regs *regs) { return 0; }
  85. static inline int debugger_ipi(struct pt_regs *regs) { return 0; }
  86. static inline int debugger_bpt(struct pt_regs *regs) { return 0; }
  87. static inline int debugger_sstep(struct pt_regs *regs) { return 0; }
  88. static inline int debugger_iabr_match(struct pt_regs *regs) { return 0; }
  89. static inline int debugger_dabr_match(struct pt_regs *regs) { return 0; }
  90. static inline int debugger_fault_handler(struct pt_regs *regs) { return 0; }
  91. #endif
  92. extern int set_dabr(unsigned long dabr);
  93. extern void print_backtrace(unsigned long *);
  94. extern void show_regs(struct pt_regs * regs);
  95. extern void flush_instruction_cache(void);
  96. extern void hard_reset_now(void);
  97. extern void poweroff_now(void);
  98. #ifdef CONFIG_6xx
  99. extern long _get_L2CR(void);
  100. extern long _get_L3CR(void);
  101. extern void _set_L2CR(unsigned long);
  102. extern void _set_L3CR(unsigned long);
  103. #else
  104. #define _get_L2CR() 0L
  105. #define _get_L3CR() 0L
  106. #define _set_L2CR(val) do { } while(0)
  107. #define _set_L3CR(val) do { } while(0)
  108. #endif
  109. extern void via_cuda_init(void);
  110. extern void read_rtc_time(void);
  111. extern void pmac_find_display(void);
  112. extern void giveup_fpu(struct task_struct *);
  113. extern void disable_kernel_fp(void);
  114. extern void enable_kernel_fp(void);
  115. extern void flush_fp_to_thread(struct task_struct *);
  116. extern void enable_kernel_altivec(void);
  117. extern void giveup_altivec(struct task_struct *);
  118. extern void load_up_altivec(struct task_struct *);
  119. extern int emulate_altivec(struct pt_regs *);
  120. extern void enable_kernel_spe(void);
  121. extern void giveup_spe(struct task_struct *);
  122. extern void load_up_spe(struct task_struct *);
  123. extern int fix_alignment(struct pt_regs *);
  124. extern void cvt_fd(float *from, double *to, struct thread_struct *thread);
  125. extern void cvt_df(double *from, float *to, struct thread_struct *thread);
  126. #ifndef CONFIG_SMP
  127. extern void discard_lazy_cpu_state(void);
  128. #else
  129. static inline void discard_lazy_cpu_state(void)
  130. {
  131. }
  132. #endif
  133. #ifdef CONFIG_ALTIVEC
  134. extern void flush_altivec_to_thread(struct task_struct *);
  135. #else
  136. static inline void flush_altivec_to_thread(struct task_struct *t)
  137. {
  138. }
  139. #endif
  140. #ifdef CONFIG_SPE
  141. extern void flush_spe_to_thread(struct task_struct *);
  142. #else
  143. static inline void flush_spe_to_thread(struct task_struct *t)
  144. {
  145. }
  146. #endif
  147. extern int call_rtas(const char *, int, int, unsigned long *, ...);
  148. extern void cacheable_memzero(void *p, unsigned int nb);
  149. extern void *cacheable_memcpy(void *, const void *, unsigned int);
  150. extern int do_page_fault(struct pt_regs *, unsigned long, unsigned long);
  151. extern void bad_page_fault(struct pt_regs *, unsigned long, int);
  152. extern int die(const char *, struct pt_regs *, long);
  153. extern void _exception(int, struct pt_regs *, int, unsigned long);
  154. extern void _nmask_and_or_msr(unsigned long nmask, unsigned long or_val);
  155. #ifdef CONFIG_BOOKE_WDT
  156. extern u32 booke_wdt_enabled;
  157. extern u32 booke_wdt_period;
  158. #endif /* CONFIG_BOOKE_WDT */
  159. struct device_node;
  160. extern void note_scsi_host(struct device_node *, void *);
  161. extern struct task_struct *__switch_to(struct task_struct *,
  162. struct task_struct *);
  163. #define switch_to(prev, next, last) ((last) = __switch_to((prev), (next)))
  164. struct thread_struct;
  165. extern struct task_struct *_switch(struct thread_struct *prev,
  166. struct thread_struct *next);
  167. extern unsigned int rtas_data;
  168. extern int mem_init_done; /* set on boot once kmalloc can be called */
  169. extern unsigned long memory_limit;
  170. extern unsigned long klimit;
  171. extern void *alloc_maybe_bootmem(size_t size, gfp_t mask);
  172. extern void *zalloc_maybe_bootmem(size_t size, gfp_t mask);
  173. extern int powersave_nap; /* set if nap mode can be used in idle loop */
  174. /*
  175. * Atomic exchange
  176. *
  177. * Changes the memory location '*ptr' to be val and returns
  178. * the previous value stored there.
  179. */
  180. static __always_inline unsigned long
  181. __xchg_u32(volatile void *p, unsigned long val)
  182. {
  183. unsigned long prev;
  184. __asm__ __volatile__(
  185. LWSYNC_ON_SMP
  186. "1: lwarx %0,0,%2 \n"
  187. PPC405_ERR77(0,%2)
  188. " stwcx. %3,0,%2 \n\
  189. bne- 1b"
  190. ISYNC_ON_SMP
  191. : "=&r" (prev), "+m" (*(volatile unsigned int *)p)
  192. : "r" (p), "r" (val)
  193. : "cc", "memory");
  194. return prev;
  195. }
  196. /*
  197. * Atomic exchange
  198. *
  199. * Changes the memory location '*ptr' to be val and returns
  200. * the previous value stored there.
  201. */
  202. static __always_inline unsigned long
  203. __xchg_u32_local(volatile void *p, unsigned long val)
  204. {
  205. unsigned long prev;
  206. __asm__ __volatile__(
  207. "1: lwarx %0,0,%2 \n"
  208. PPC405_ERR77(0,%2)
  209. " stwcx. %3,0,%2 \n\
  210. bne- 1b"
  211. : "=&r" (prev), "+m" (*(volatile unsigned int *)p)
  212. : "r" (p), "r" (val)
  213. : "cc", "memory");
  214. return prev;
  215. }
  216. #ifdef CONFIG_PPC64
  217. static __always_inline unsigned long
  218. __xchg_u64(volatile void *p, unsigned long val)
  219. {
  220. unsigned long prev;
  221. __asm__ __volatile__(
  222. LWSYNC_ON_SMP
  223. "1: ldarx %0,0,%2 \n"
  224. PPC405_ERR77(0,%2)
  225. " stdcx. %3,0,%2 \n\
  226. bne- 1b"
  227. ISYNC_ON_SMP
  228. : "=&r" (prev), "+m" (*(volatile unsigned long *)p)
  229. : "r" (p), "r" (val)
  230. : "cc", "memory");
  231. return prev;
  232. }
  233. static __always_inline unsigned long
  234. __xchg_u64_local(volatile void *p, unsigned long val)
  235. {
  236. unsigned long prev;
  237. __asm__ __volatile__(
  238. "1: ldarx %0,0,%2 \n"
  239. PPC405_ERR77(0,%2)
  240. " stdcx. %3,0,%2 \n\
  241. bne- 1b"
  242. : "=&r" (prev), "+m" (*(volatile unsigned long *)p)
  243. : "r" (p), "r" (val)
  244. : "cc", "memory");
  245. return prev;
  246. }
  247. #endif
  248. /*
  249. * This function doesn't exist, so you'll get a linker error
  250. * if something tries to do an invalid xchg().
  251. */
  252. extern void __xchg_called_with_bad_pointer(void);
  253. static __always_inline unsigned long
  254. __xchg(volatile void *ptr, unsigned long x, unsigned int size)
  255. {
  256. switch (size) {
  257. case 4:
  258. return __xchg_u32(ptr, x);
  259. #ifdef CONFIG_PPC64
  260. case 8:
  261. return __xchg_u64(ptr, x);
  262. #endif
  263. }
  264. __xchg_called_with_bad_pointer();
  265. return x;
  266. }
  267. static __always_inline unsigned long
  268. __xchg_local(volatile void *ptr, unsigned long x, unsigned int size)
  269. {
  270. switch (size) {
  271. case 4:
  272. return __xchg_u32_local(ptr, x);
  273. #ifdef CONFIG_PPC64
  274. case 8:
  275. return __xchg_u64_local(ptr, x);
  276. #endif
  277. }
  278. __xchg_called_with_bad_pointer();
  279. return x;
  280. }
  281. #define xchg(ptr,x) \
  282. ({ \
  283. __typeof__(*(ptr)) _x_ = (x); \
  284. (__typeof__(*(ptr))) __xchg((ptr), (unsigned long)_x_, sizeof(*(ptr))); \
  285. })
  286. #define xchg_local(ptr,x) \
  287. ({ \
  288. __typeof__(*(ptr)) _x_ = (x); \
  289. (__typeof__(*(ptr))) __xchg_local((ptr), \
  290. (unsigned long)_x_, sizeof(*(ptr))); \
  291. })
  292. /*
  293. * Compare and exchange - if *p == old, set it to new,
  294. * and return the old value of *p.
  295. */
  296. #define __HAVE_ARCH_CMPXCHG 1
  297. static __always_inline unsigned long
  298. __cmpxchg_u32(volatile unsigned int *p, unsigned long old, unsigned long new)
  299. {
  300. unsigned int prev;
  301. __asm__ __volatile__ (
  302. LWSYNC_ON_SMP
  303. "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
  304. cmpw 0,%0,%3\n\
  305. bne- 2f\n"
  306. PPC405_ERR77(0,%2)
  307. " stwcx. %4,0,%2\n\
  308. bne- 1b"
  309. ISYNC_ON_SMP
  310. "\n\
  311. 2:"
  312. : "=&r" (prev), "+m" (*p)
  313. : "r" (p), "r" (old), "r" (new)
  314. : "cc", "memory");
  315. return prev;
  316. }
  317. static __always_inline unsigned long
  318. __cmpxchg_u32_local(volatile unsigned int *p, unsigned long old,
  319. unsigned long new)
  320. {
  321. unsigned int prev;
  322. __asm__ __volatile__ (
  323. "1: lwarx %0,0,%2 # __cmpxchg_u32\n\
  324. cmpw 0,%0,%3\n\
  325. bne- 2f\n"
  326. PPC405_ERR77(0,%2)
  327. " stwcx. %4,0,%2\n\
  328. bne- 1b"
  329. "\n\
  330. 2:"
  331. : "=&r" (prev), "+m" (*p)
  332. : "r" (p), "r" (old), "r" (new)
  333. : "cc", "memory");
  334. return prev;
  335. }
  336. #ifdef CONFIG_PPC64
  337. static __always_inline unsigned long
  338. __cmpxchg_u64(volatile unsigned long *p, unsigned long old, unsigned long new)
  339. {
  340. unsigned long prev;
  341. __asm__ __volatile__ (
  342. LWSYNC_ON_SMP
  343. "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
  344. cmpd 0,%0,%3\n\
  345. bne- 2f\n\
  346. stdcx. %4,0,%2\n\
  347. bne- 1b"
  348. ISYNC_ON_SMP
  349. "\n\
  350. 2:"
  351. : "=&r" (prev), "+m" (*p)
  352. : "r" (p), "r" (old), "r" (new)
  353. : "cc", "memory");
  354. return prev;
  355. }
  356. static __always_inline unsigned long
  357. __cmpxchg_u64_local(volatile unsigned long *p, unsigned long old,
  358. unsigned long new)
  359. {
  360. unsigned long prev;
  361. __asm__ __volatile__ (
  362. "1: ldarx %0,0,%2 # __cmpxchg_u64\n\
  363. cmpd 0,%0,%3\n\
  364. bne- 2f\n\
  365. stdcx. %4,0,%2\n\
  366. bne- 1b"
  367. "\n\
  368. 2:"
  369. : "=&r" (prev), "+m" (*p)
  370. : "r" (p), "r" (old), "r" (new)
  371. : "cc", "memory");
  372. return prev;
  373. }
  374. #endif
  375. /* This function doesn't exist, so you'll get a linker error
  376. if something tries to do an invalid cmpxchg(). */
  377. extern void __cmpxchg_called_with_bad_pointer(void);
  378. static __always_inline unsigned long
  379. __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new,
  380. unsigned int size)
  381. {
  382. switch (size) {
  383. case 4:
  384. return __cmpxchg_u32(ptr, old, new);
  385. #ifdef CONFIG_PPC64
  386. case 8:
  387. return __cmpxchg_u64(ptr, old, new);
  388. #endif
  389. }
  390. __cmpxchg_called_with_bad_pointer();
  391. return old;
  392. }
  393. static __always_inline unsigned long
  394. __cmpxchg_local(volatile void *ptr, unsigned long old, unsigned long new,
  395. unsigned int size)
  396. {
  397. switch (size) {
  398. case 4:
  399. return __cmpxchg_u32_local(ptr, old, new);
  400. #ifdef CONFIG_PPC64
  401. case 8:
  402. return __cmpxchg_u64_local(ptr, old, new);
  403. #endif
  404. }
  405. __cmpxchg_called_with_bad_pointer();
  406. return old;
  407. }
  408. #define cmpxchg(ptr, o, n) \
  409. ({ \
  410. __typeof__(*(ptr)) _o_ = (o); \
  411. __typeof__(*(ptr)) _n_ = (n); \
  412. (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \
  413. (unsigned long)_n_, sizeof(*(ptr))); \
  414. })
  415. #define cmpxchg_local(ptr, o, n) \
  416. ({ \
  417. __typeof__(*(ptr)) _o_ = (o); \
  418. __typeof__(*(ptr)) _n_ = (n); \
  419. (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \
  420. (unsigned long)_n_, sizeof(*(ptr))); \
  421. })
  422. #ifdef CONFIG_PPC64
  423. /*
  424. * We handle most unaligned accesses in hardware. On the other hand
  425. * unaligned DMA can be very expensive on some ppc64 IO chips (it does
  426. * powers of 2 writes until it reaches sufficient alignment).
  427. *
  428. * Based on this we disable the IP header alignment in network drivers.
  429. * We also modify NET_SKB_PAD to be a cacheline in size, thus maintaining
  430. * cacheline alignment of buffers.
  431. */
  432. #define NET_IP_ALIGN 0
  433. #define NET_SKB_PAD L1_CACHE_BYTES
  434. #define cmpxchg64(ptr, o, n) \
  435. ({ \
  436. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  437. cmpxchg((ptr), (o), (n)); \
  438. })
  439. #define cmpxchg64_local(ptr, o, n) \
  440. ({ \
  441. BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
  442. cmpxchg_local((ptr), (o), (n)); \
  443. })
  444. #else
  445. #include <asm-generic/cmpxchg-local.h>
  446. #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n))
  447. #endif
  448. #define arch_align_stack(x) (x)
  449. /* Used in very early kernel initialization. */
  450. extern unsigned long reloc_offset(void);
  451. extern unsigned long add_reloc_offset(unsigned long);
  452. extern void reloc_got2(unsigned long);
  453. #define PTRRELOC(x) ((typeof(x)) add_reloc_offset((unsigned long)(x)))
  454. static inline void create_instruction(unsigned long addr, unsigned int instr)
  455. {
  456. unsigned int *p;
  457. p = (unsigned int *)addr;
  458. *p = instr;
  459. asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (p));
  460. }
  461. /* Flags for create_branch:
  462. * "b" == create_branch(addr, target, 0);
  463. * "ba" == create_branch(addr, target, BRANCH_ABSOLUTE);
  464. * "bl" == create_branch(addr, target, BRANCH_SET_LINK);
  465. * "bla" == create_branch(addr, target, BRANCH_ABSOLUTE | BRANCH_SET_LINK);
  466. */
  467. #define BRANCH_SET_LINK 0x1
  468. #define BRANCH_ABSOLUTE 0x2
  469. static inline void create_branch(unsigned long addr,
  470. unsigned long target, int flags)
  471. {
  472. unsigned int instruction;
  473. if (! (flags & BRANCH_ABSOLUTE))
  474. target = target - addr;
  475. /* Mask out the flags and target, so they don't step on each other. */
  476. instruction = 0x48000000 | (flags & 0x3) | (target & 0x03FFFFFC);
  477. create_instruction(addr, instruction);
  478. }
  479. static inline void create_function_call(unsigned long addr, void * func)
  480. {
  481. unsigned long func_addr;
  482. #ifdef CONFIG_PPC64
  483. /*
  484. * On PPC64 the function pointer actually points to the function's
  485. * descriptor. The first entry in the descriptor is the address
  486. * of the function text.
  487. */
  488. func_addr = *(unsigned long *)func;
  489. #else
  490. func_addr = (unsigned long)func;
  491. #endif
  492. create_branch(addr, func_addr, BRANCH_SET_LINK);
  493. }
  494. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  495. extern void account_system_vtime(struct task_struct *);
  496. #endif
  497. extern struct dentry *powerpc_debugfs_root;
  498. #endif /* __KERNEL__ */
  499. #endif /* _ASM_POWERPC_SYSTEM_H */