of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / x86 / lib / msr.c
blob004c861b1648e50ae8c386563b798bb6d50bece7
1 #include <linux/module.h>
2 #include <linux/preempt.h>
3 #include <asm/msr.h>
4 #define CREATE_TRACE_POINTS
5 #include <asm/msr-trace.h>
7 struct msr *msrs_alloc(void)
9 struct msr *msrs = NULL;
11 msrs = alloc_percpu(struct msr);
12 if (!msrs) {
13 pr_warn("%s: error allocating msrs\n", __func__);
14 return NULL;
17 return msrs;
19 EXPORT_SYMBOL(msrs_alloc);
21 void msrs_free(struct msr *msrs)
23 free_percpu(msrs);
25 EXPORT_SYMBOL(msrs_free);
27 /**
28 * Read an MSR with error handling
30 * @msr: MSR to read
31 * @m: value to read into
33 * It returns read data only on success, otherwise it doesn't change the output
34 * argument @m.
37 int msr_read(u32 msr, struct msr *m)
39 int err;
40 u64 val;
42 err = rdmsrl_safe(msr, &val);
43 if (!err)
44 m->q = val;
46 return err;
49 /**
50 * Write an MSR with error handling
52 * @msr: MSR to write
53 * @m: value to write
55 int msr_write(u32 msr, struct msr *m)
57 return wrmsrl_safe(msr, m->q);
60 static inline int __flip_bit(u32 msr, u8 bit, bool set)
62 struct msr m, m1;
63 int err = -EINVAL;
65 if (bit > 63)
66 return err;
68 err = msr_read(msr, &m);
69 if (err)
70 return err;
72 m1 = m;
73 if (set)
74 m1.q |= BIT_64(bit);
75 else
76 m1.q &= ~BIT_64(bit);
78 if (m1.q == m.q)
79 return 0;
81 err = msr_write(msr, &m1);
82 if (err)
83 return err;
85 return 1;
88 /**
89 * Set @bit in a MSR @msr.
91 * Retval:
92 * < 0: An error was encountered.
93 * = 0: Bit was already set.
94 * > 0: Hardware accepted the MSR write.
96 int msr_set_bit(u32 msr, u8 bit)
98 return __flip_bit(msr, bit, true);
102 * Clear @bit in a MSR @msr.
104 * Retval:
105 * < 0: An error was encountered.
106 * = 0: Bit was already cleared.
107 * > 0: Hardware accepted the MSR write.
109 int msr_clear_bit(u32 msr, u8 bit)
111 return __flip_bit(msr, bit, false);
114 #ifdef CONFIG_TRACEPOINTS
115 void do_trace_write_msr(unsigned msr, u64 val, int failed)
117 trace_write_msr(msr, val, failed);
119 EXPORT_SYMBOL(do_trace_write_msr);
120 EXPORT_TRACEPOINT_SYMBOL(write_msr);
122 void do_trace_read_msr(unsigned msr, u64 val, int failed)
124 trace_read_msr(msr, val, failed);
126 EXPORT_SYMBOL(do_trace_read_msr);
127 EXPORT_TRACEPOINT_SYMBOL(read_msr);
129 void do_trace_rdpmc(unsigned counter, u64 val, int failed)
131 trace_rdpmc(counter, val, failed);
133 EXPORT_SYMBOL(do_trace_rdpmc);
134 EXPORT_TRACEPOINT_SYMBOL(rdpmc);
136 #endif