[PATCH] x86-64: Clean up asm-x86_64/bugs.h
[linux-drm-fsl-dcu.git] / include / asm-x86_64 / alternative.h
1 #ifndef _X86_64_ALTERNATIVE_H
2 #define _X86_64_ALTERNATIVE_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/types.h>
7 #include <linux/stddef.h>
8 #include <asm/cpufeature.h>
9
10 struct alt_instr {
11         u8 *instr;              /* original instruction */
12         u8 *replacement;
13         u8  cpuid;              /* cpuid bit set for replacement */
14         u8  instrlen;           /* length of original instruction */
15         u8  replacementlen;     /* length of new instruction, <= instrlen */
16         u8  pad[5];
17 };
18
19 extern void alternative_instructions(void);
20 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
21
22 struct module;
23
24 #ifdef CONFIG_SMP
25 extern void alternatives_smp_module_add(struct module *mod, char *name,
26                                         void *locks, void *locks_end,
27                                         void *text, void *text_end);
28 extern void alternatives_smp_module_del(struct module *mod);
29 extern void alternatives_smp_switch(int smp);
30 #else
31 static inline void alternatives_smp_module_add(struct module *mod, char *name,
32                                         void *locks, void *locks_end,
33                                         void *text, void *text_end) {}
34 static inline void alternatives_smp_module_del(struct module *mod) {}
35 static inline void alternatives_smp_switch(int smp) {}
36 #endif
37
38 #endif
39
40 /*
41  * Alternative instructions for different CPU types or capabilities.
42  *
43  * This allows to use optimized instructions even on generic binary
44  * kernels.
45  *
46  * length of oldinstr must be longer or equal the length of newinstr
47  * It can be padded with nops as needed.
48  *
49  * For non barrier like inlines please define new variants
50  * without volatile and memory clobber.
51  */
52 #define alternative(oldinstr, newinstr, feature)        \
53         asm volatile ("661:\n\t" oldinstr "\n662:\n"                 \
54                       ".section .altinstructions,\"a\"\n"            \
55                       "  .align 8\n"                                   \
56                       "  .quad 661b\n"            /* label */          \
57                       "  .quad 663f\n"            /* new instruction */ \
58                       "  .byte %c0\n"             /* feature bit */    \
59                       "  .byte 662b-661b\n"       /* sourcelen */      \
60                       "  .byte 664f-663f\n"       /* replacementlen */ \
61                       ".previous\n"                                     \
62                       ".section .altinstr_replacement,\"ax\"\n"         \
63                       "663:\n\t" newinstr "\n664:\n"   /* replacement */ \
64                       ".previous" :: "i" (feature) : "memory")
65
66 /*
67  * Alternative inline assembly with input.
68  *
69  * Pecularities:
70  * No memory clobber here.
71  * Argument numbers start with 1.
72  * Best is to use constraints that are fixed size (like (%1) ... "r")
73  * If you use variable sized constraints like "m" or "g" in the
74  * replacement make sure to pad to the worst case length.
75  */
76 #define alternative_input(oldinstr, newinstr, feature, input...)        \
77         asm volatile ("661:\n\t" oldinstr "\n662:\n"                    \
78                       ".section .altinstructions,\"a\"\n"               \
79                       "  .align 8\n"                                    \
80                       "  .quad 661b\n"            /* label */           \
81                       "  .quad 663f\n"            /* new instruction */ \
82                       "  .byte %c0\n"             /* feature bit */     \
83                       "  .byte 662b-661b\n"       /* sourcelen */       \
84                       "  .byte 664f-663f\n"       /* replacementlen */  \
85                       ".previous\n"                                     \
86                       ".section .altinstr_replacement,\"ax\"\n"         \
87                       "663:\n\t" newinstr "\n664:\n"   /* replacement */ \
88                       ".previous" :: "i" (feature), ##input)
89
90 /* Like alternative_input, but with a single output argument */
91 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
92         asm volatile ("661:\n\t" oldinstr "\n662:\n"                    \
93                       ".section .altinstructions,\"a\"\n"               \
94                       "  .align 8\n"                                    \
95                       "  .quad 661b\n"            /* label */           \
96                       "  .quad 663f\n"            /* new instruction */ \
97                       "  .byte %c[feat]\n"        /* feature bit */     \
98                       "  .byte 662b-661b\n"       /* sourcelen */       \
99                       "  .byte 664f-663f\n"       /* replacementlen */  \
100                       ".previous\n"                                     \
101                       ".section .altinstr_replacement,\"ax\"\n"         \
102                       "663:\n\t" newinstr "\n664:\n"   /* replacement */ \
103                       ".previous" : output : [feat] "i" (feature), ##input)
104
105 /*
106  * Alternative inline assembly for SMP.
107  *
108  * The LOCK_PREFIX macro defined here replaces the LOCK and
109  * LOCK_PREFIX macros used everywhere in the source tree.
110  *
111  * SMP alternatives use the same data structures as the other
112  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
113  * UP system running a SMP kernel.  The existing apply_alternatives()
114  * works fine for patching a SMP kernel for UP.
115  *
116  * The SMP alternative tables can be kept after boot and contain both
117  * UP and SMP versions of the instructions to allow switching back to
118  * SMP at runtime, when hotplugging in a new CPU, which is especially
119  * useful in virtualized environments.
120  *
121  * The very common lock prefix is handled as special case in a
122  * separate table which is a pure address list without replacement ptr
123  * and size information.  That keeps the table sizes small.
124  */
125
126 #ifdef CONFIG_SMP
127 #define LOCK_PREFIX \
128                 ".section .smp_locks,\"a\"\n"   \
129                 "  .align 8\n"                  \
130                 "  .quad 661f\n" /* address */  \
131                 ".previous\n"                   \
132                 "661:\n\tlock; "
133
134 #else /* ! CONFIG_SMP */
135 #define LOCK_PREFIX ""
136 #endif
137
138 struct paravirt_patch;
139 #ifdef CONFIG_PARAVIRT
140 void apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end);
141 #else
142 static inline void
143 apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end)
144 {}
145 #define __start_parainstructions NULL
146 #define __stop_parainstructions NULL
147 #endif
148
149 #endif /* _X86_64_ALTERNATIVE_H */