]>
Commit | Line | Data |
---|---|---|
b20b7b7a MR |
1 | /* |
2 | * QEMU SPAPR Architecture Option Vector Helper Functions | |
3 | * | |
4 | * Copyright IBM Corp. 2016 | |
5 | * | |
6 | * Authors: | |
7 | * Bharata B Rao <bharata@linux.vnet.ibm.com> | |
8 | * Michael Roth <mdroth@linux.vnet.ibm.com> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
11 | * See the COPYING file in the top-level directory. | |
12 | */ | |
13 | ||
14 | #include "qemu/osdep.h" | |
15 | #include "hw/ppc/spapr_ovec.h" | |
16 | #include "qemu/bitmap.h" | |
17 | #include "exec/address-spaces.h" | |
18 | #include "qemu/error-report.h" | |
19 | #include <libfdt.h> | |
20 | ||
21 | /* #define DEBUG_SPAPR_OVEC */ | |
22 | ||
23 | #ifdef DEBUG_SPAPR_OVEC | |
24 | #define DPRINTFN(fmt, ...) \ | |
25 | do { fprintf(stderr, fmt "\n", ## __VA_ARGS__); } while (0) | |
26 | #else | |
27 | #define DPRINTFN(fmt, ...) \ | |
28 | do { } while (0) | |
29 | #endif | |
30 | ||
31 | #define OV_MAXBYTES 256 /* not including length byte */ | |
32 | #define OV_MAXBITS (OV_MAXBYTES * BITS_PER_BYTE) | |
33 | ||
34 | /* we *could* work with bitmaps directly, but handling the bitmap privately | |
35 | * allows us to more safely make assumptions about the bitmap size and | |
36 | * simplify the calling code somewhat | |
37 | */ | |
38 | struct sPAPROptionVector { | |
39 | unsigned long *bitmap; | |
62ef3760 MR |
40 | int32_t bitmap_size; /* only used for migration */ |
41 | }; | |
42 | ||
43 | const VMStateDescription vmstate_spapr_ovec = { | |
44 | .name = "spapr_option_vector", | |
45 | .version_id = 1, | |
46 | .minimum_version_id = 1, | |
47 | .fields = (VMStateField[]) { | |
48 | VMSTATE_BITMAP(bitmap, sPAPROptionVector, 1, bitmap_size), | |
49 | VMSTATE_END_OF_LIST() | |
50 | } | |
b20b7b7a MR |
51 | }; |
52 | ||
53 | sPAPROptionVector *spapr_ovec_new(void) | |
54 | { | |
55 | sPAPROptionVector *ov; | |
56 | ||
57 | ov = g_new0(sPAPROptionVector, 1); | |
58 | ov->bitmap = bitmap_new(OV_MAXBITS); | |
62ef3760 | 59 | ov->bitmap_size = OV_MAXBITS; |
b20b7b7a MR |
60 | |
61 | return ov; | |
62 | } | |
63 | ||
64 | sPAPROptionVector *spapr_ovec_clone(sPAPROptionVector *ov_orig) | |
65 | { | |
66 | sPAPROptionVector *ov; | |
67 | ||
68 | g_assert(ov_orig); | |
69 | ||
70 | ov = spapr_ovec_new(); | |
71 | bitmap_copy(ov->bitmap, ov_orig->bitmap, OV_MAXBITS); | |
72 | ||
73 | return ov; | |
74 | } | |
75 | ||
76 | void spapr_ovec_intersect(sPAPROptionVector *ov, | |
77 | sPAPROptionVector *ov1, | |
78 | sPAPROptionVector *ov2) | |
79 | { | |
80 | g_assert(ov); | |
81 | g_assert(ov1); | |
82 | g_assert(ov2); | |
83 | ||
84 | bitmap_and(ov->bitmap, ov1->bitmap, ov2->bitmap, OV_MAXBITS); | |
85 | } | |
86 | ||
87 | /* returns true if options bits were removed, false otherwise */ | |
88 | bool spapr_ovec_diff(sPAPROptionVector *ov, | |
89 | sPAPROptionVector *ov_old, | |
90 | sPAPROptionVector *ov_new) | |
91 | { | |
92 | unsigned long *change_mask = bitmap_new(OV_MAXBITS); | |
93 | unsigned long *removed_bits = bitmap_new(OV_MAXBITS); | |
94 | bool bits_were_removed = false; | |
95 | ||
96 | g_assert(ov); | |
97 | g_assert(ov_old); | |
98 | g_assert(ov_new); | |
99 | ||
100 | bitmap_xor(change_mask, ov_old->bitmap, ov_new->bitmap, OV_MAXBITS); | |
101 | bitmap_and(ov->bitmap, ov_new->bitmap, change_mask, OV_MAXBITS); | |
102 | bitmap_and(removed_bits, ov_old->bitmap, change_mask, OV_MAXBITS); | |
103 | ||
104 | if (!bitmap_empty(removed_bits, OV_MAXBITS)) { | |
105 | bits_were_removed = true; | |
106 | } | |
107 | ||
108 | g_free(change_mask); | |
109 | g_free(removed_bits); | |
110 | ||
111 | return bits_were_removed; | |
112 | } | |
113 | ||
114 | void spapr_ovec_cleanup(sPAPROptionVector *ov) | |
115 | { | |
116 | if (ov) { | |
117 | g_free(ov->bitmap); | |
118 | g_free(ov); | |
119 | } | |
120 | } | |
121 | ||
122 | void spapr_ovec_set(sPAPROptionVector *ov, long bitnr) | |
123 | { | |
124 | g_assert(ov); | |
125 | g_assert_cmpint(bitnr, <, OV_MAXBITS); | |
126 | ||
127 | set_bit(bitnr, ov->bitmap); | |
128 | } | |
129 | ||
130 | void spapr_ovec_clear(sPAPROptionVector *ov, long bitnr) | |
131 | { | |
132 | g_assert(ov); | |
133 | g_assert_cmpint(bitnr, <, OV_MAXBITS); | |
134 | ||
135 | clear_bit(bitnr, ov->bitmap); | |
136 | } | |
137 | ||
138 | bool spapr_ovec_test(sPAPROptionVector *ov, long bitnr) | |
139 | { | |
140 | g_assert(ov); | |
141 | g_assert_cmpint(bitnr, <, OV_MAXBITS); | |
142 | ||
143 | return test_bit(bitnr, ov->bitmap) ? true : false; | |
144 | } | |
145 | ||
146 | static void guest_byte_to_bitmap(uint8_t entry, unsigned long *bitmap, | |
147 | long bitmap_offset) | |
148 | { | |
149 | int i; | |
150 | ||
151 | for (i = 0; i < BITS_PER_BYTE; i++) { | |
152 | if (entry & (1 << (BITS_PER_BYTE - 1 - i))) { | |
153 | bitmap_set(bitmap, bitmap_offset + i, 1); | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | static uint8_t guest_byte_from_bitmap(unsigned long *bitmap, long bitmap_offset) | |
159 | { | |
160 | uint8_t entry = 0; | |
161 | int i; | |
162 | ||
163 | for (i = 0; i < BITS_PER_BYTE; i++) { | |
164 | if (test_bit(bitmap_offset + i, bitmap)) { | |
165 | entry |= (1 << (BITS_PER_BYTE - 1 - i)); | |
166 | } | |
167 | } | |
168 | ||
169 | return entry; | |
170 | } | |
171 | ||
172 | static target_ulong vector_addr(target_ulong table_addr, int vector) | |
173 | { | |
174 | uint16_t vector_count, vector_len; | |
175 | int i; | |
176 | ||
177 | vector_count = ldub_phys(&address_space_memory, table_addr) + 1; | |
178 | if (vector > vector_count) { | |
179 | return 0; | |
180 | } | |
181 | table_addr++; /* skip nr option vectors */ | |
182 | ||
183 | for (i = 0; i < vector - 1; i++) { | |
184 | vector_len = ldub_phys(&address_space_memory, table_addr) + 1; | |
185 | table_addr += vector_len + 1; /* bit-vector + length byte */ | |
186 | } | |
187 | return table_addr; | |
188 | } | |
189 | ||
190 | sPAPROptionVector *spapr_ovec_parse_vector(target_ulong table_addr, int vector) | |
191 | { | |
192 | sPAPROptionVector *ov; | |
193 | target_ulong addr; | |
194 | uint16_t vector_len; | |
195 | int i; | |
196 | ||
197 | g_assert(table_addr); | |
198 | g_assert_cmpint(vector, >=, 1); /* vector numbering starts at 1 */ | |
199 | ||
200 | addr = vector_addr(table_addr, vector); | |
201 | if (!addr) { | |
202 | /* specified vector isn't present */ | |
203 | return NULL; | |
204 | } | |
205 | ||
206 | vector_len = ldub_phys(&address_space_memory, addr++) + 1; | |
207 | g_assert_cmpint(vector_len, <=, OV_MAXBYTES); | |
208 | ov = spapr_ovec_new(); | |
209 | ||
210 | for (i = 0; i < vector_len; i++) { | |
211 | uint8_t entry = ldub_phys(&address_space_memory, addr + i); | |
212 | if (entry) { | |
213 | DPRINTFN("read guest vector %2d, byte %3d / %3d: 0x%.2x", | |
214 | vector, i + 1, vector_len, entry); | |
215 | guest_byte_to_bitmap(entry, ov->bitmap, i * BITS_PER_BYTE); | |
216 | } | |
217 | } | |
218 | ||
219 | return ov; | |
220 | } | |
221 | ||
222 | int spapr_ovec_populate_dt(void *fdt, int fdt_offset, | |
223 | sPAPROptionVector *ov, const char *name) | |
224 | { | |
225 | uint8_t vec[OV_MAXBYTES + 1]; | |
226 | uint16_t vec_len; | |
227 | unsigned long lastbit; | |
228 | int i; | |
229 | ||
230 | g_assert(ov); | |
231 | ||
232 | lastbit = find_last_bit(ov->bitmap, OV_MAXBITS); | |
233 | /* if no bits are set, include at least 1 byte of the vector so we can | |
234 | * still encoded this in the device tree while abiding by the same | |
235 | * encoding/sizing expected in ibm,client-architecture-support | |
236 | */ | |
237 | vec_len = (lastbit == OV_MAXBITS) ? 1 : lastbit / BITS_PER_BYTE + 1; | |
238 | g_assert_cmpint(vec_len, <=, OV_MAXBYTES); | |
239 | /* guest expects vector len encoded as vec_len - 1, since the length byte | |
240 | * is assumed and not included, and the first byte of the vector | |
241 | * is assumed as well | |
242 | */ | |
243 | vec[0] = vec_len - 1; | |
244 | ||
245 | for (i = 1; i < vec_len + 1; i++) { | |
246 | vec[i] = guest_byte_from_bitmap(ov->bitmap, (i - 1) * BITS_PER_BYTE); | |
247 | if (vec[i]) { | |
248 | DPRINTFN("encoding guest vector byte %3d / %3d: 0x%.2x", | |
249 | i, vec_len, vec[i]); | |
250 | } | |
251 | } | |
252 | ||
253 | return fdt_setprop(fdt, fdt_offset, name, vec, vec_len); | |
254 | } |