46246baeec1bd69ea8e217b926d5c501541b017e
[fio.git] / arch / arch-ppc.h
1 #ifndef ARCH_PPC_H
2 #define ARCH_PPC_H
3
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8
9 #define FIO_ARCH        (arch_ppc)
10
11 #define nop     do { } while (0)
12
13 #ifdef __powerpc64__
14 #define read_barrier()  __asm__ __volatile__ ("lwsync" : : : "memory")
15 #else
16 #define read_barrier()  __asm__ __volatile__ ("sync" : : : "memory")
17 #endif
18
19 #define write_barrier() __asm__ __volatile__ ("sync" : : : "memory")
20
21 #ifdef __powerpc64__
22 #define PPC_CNTLZL "cntlzd"
23 #else
24 #define PPC_CNTLZL "cntlzw"
25 #endif
26
27 #define ARCH_HAVE_IOURING
28
29 #ifndef __NR_sys_io_uring_setup
30 #define __NR_sys_io_uring_setup         425
31 #endif
32 #ifndef __NR_sys_io_uring_enter
33 #define __NR_sys_io_uring_enter         426
34 #endif
35 #ifndef __NR_sys_io_uring_register
36 #define __NR_sys_io_uring_register      427
37 #endif
38
39 static inline int __ilog2(unsigned long bitmask)
40 {
41         int lz;
42
43         asm (PPC_CNTLZL " %0,%1" : "=r" (lz) : "r" (bitmask));
44         return BITS_PER_LONG - 1 - lz;
45 }
46
47 static inline int arch_ffz(unsigned long bitmask)
48 {
49         if ((bitmask = ~bitmask) == 0)
50                 return BITS_PER_LONG;
51         return  __ilog2(bitmask & -bitmask);
52 }
53
54 static inline unsigned int mfspr(unsigned int reg)
55 {
56         unsigned int val;
57
58         asm volatile("mfspr %0,%1": "=r" (val) : "K" (reg));
59         return val;
60 }
61
62 #define SPRN_TBRL  0x10C /* Time Base Register Lower */
63 #define SPRN_TBRU  0x10D /* Time Base Register Upper */
64 #define SPRN_ATBL  0x20E /* Alternate Time Base Lower */
65 #define SPRN_ATBU  0x20F /* Alternate Time Base Upper */
66
67 #ifdef __powerpc64__
68 static inline unsigned long long get_cpu_clock(void)
69 {
70         unsigned long long rval;
71
72         asm volatile(
73                 "90:    mfspr %0, %1;\n"
74                 "       cmpwi %0,0;\n"
75                 "       beq-  90b;\n"
76         : "=r" (rval)
77         : "i" (SPRN_TBRL)
78         : "cr0");
79
80         return rval;
81 }
82 #else
83 static inline unsigned long long get_cpu_clock(void)
84 {
85         unsigned int tbl, tbu0, tbu1;
86         unsigned long long ret;
87
88         do {
89                 if (arch_flags & ARCH_FLAG_1) {
90                         tbu0 = mfspr(SPRN_ATBU);
91                         tbl = mfspr(SPRN_ATBL);
92                         tbu1 = mfspr(SPRN_ATBU);
93                 } else {
94                         tbu0 = mfspr(SPRN_TBRU);
95                         tbl = mfspr(SPRN_TBRL);
96                         tbu1 = mfspr(SPRN_TBRU);
97                 }
98         } while (tbu0 != tbu1);
99
100         ret = (((unsigned long long)tbu0) << 32) | tbl;
101         return ret;
102 }
103 #endif
104
105 #if 0
106 static void atb_child(void)
107 {
108         arch_flags |= ARCH_FLAG_1;
109         get_cpu_clock();
110         _exit(0);
111 }
112
113 static void atb_clocktest(void)
114 {
115         pid_t pid;
116
117         pid = fork();
118         if (!pid)
119                 atb_child();
120         else if (pid != -1) {
121                 int status;
122
123                 pid = wait(&status);
124                 if (pid == -1 || !WIFEXITED(status))
125                         arch_flags &= ~ARCH_FLAG_1;
126                 else
127                         arch_flags |= ARCH_FLAG_1;
128         }
129 }
130 #endif
131
132 #define ARCH_HAVE_INIT
133 extern bool tsc_reliable;
134
135 static inline int arch_init(char *envp[])
136 {
137 #if 0
138         tsc_reliable = true;
139         atb_clocktest();
140 #endif
141         return 0;
142 }
143
144 #define ARCH_HAVE_FFZ
145
146 /*
147  * We don't have it on all platforms, lets comment this out until we
148  * can handle it more intelligently.
149  *
150  * #define ARCH_HAVE_CPU_CLOCK
151  */
152
153 /*
154  * Let's have it defined for ppc64
155  */
156
157 #ifdef __powerpc64__
158 #define ARCH_HAVE_CPU_CLOCK
159 #endif
160
161 #endif