system.h 15 KB

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