1b20b7b7aSMichael Roth /*
2b20b7b7aSMichael Roth * QEMU SPAPR Architecture Option Vector Helper Functions
3b20b7b7aSMichael Roth *
4b20b7b7aSMichael Roth * Copyright IBM Corp. 2016
5b20b7b7aSMichael Roth *
6b20b7b7aSMichael Roth * Authors:
7b20b7b7aSMichael Roth * Bharata B Rao <bharata@linux.vnet.ibm.com>
8b20b7b7aSMichael Roth * Michael Roth <mdroth@linux.vnet.ibm.com>
9b20b7b7aSMichael Roth *
10b20b7b7aSMichael Roth * This work is licensed under the terms of the GNU GPL, version 2 or later.
11b20b7b7aSMichael Roth * See the COPYING file in the top-level directory.
12b20b7b7aSMichael Roth */
13b20b7b7aSMichael Roth
14b20b7b7aSMichael Roth #include "qemu/osdep.h"
15b20b7b7aSMichael Roth #include "hw/ppc/spapr_ovec.h"
16d6454270SMarkus Armbruster #include "migration/vmstate.h"
17b20b7b7aSMichael Roth #include "qemu/bitmap.h"
18b20b7b7aSMichael Roth #include "exec/address-spaces.h"
19b20b7b7aSMichael Roth #include "qemu/error-report.h"
205b929608SLaurent Vivier #include "trace.h"
21b20b7b7aSMichael Roth #include <libfdt.h>
22b20b7b7aSMichael Roth
23b20b7b7aSMichael Roth #define OV_MAXBYTES 256 /* not including length byte */
24b20b7b7aSMichael Roth #define OV_MAXBITS (OV_MAXBYTES * BITS_PER_BYTE)
25b20b7b7aSMichael Roth
26b20b7b7aSMichael Roth /* we *could* work with bitmaps directly, but handling the bitmap privately
27b20b7b7aSMichael Roth * allows us to more safely make assumptions about the bitmap size and
28b20b7b7aSMichael Roth * simplify the calling code somewhat
29b20b7b7aSMichael Roth */
30ce2918cbSDavid Gibson struct SpaprOptionVector {
31b20b7b7aSMichael Roth unsigned long *bitmap;
3262ef3760SMichael Roth int32_t bitmap_size; /* only used for migration */
3362ef3760SMichael Roth };
3462ef3760SMichael Roth
3562ef3760SMichael Roth const VMStateDescription vmstate_spapr_ovec = {
3662ef3760SMichael Roth .name = "spapr_option_vector",
3762ef3760SMichael Roth .version_id = 1,
3862ef3760SMichael Roth .minimum_version_id = 1,
39*078ddbc9SRichard Henderson .fields = (const VMStateField[]) {
40ce2918cbSDavid Gibson VMSTATE_BITMAP(bitmap, SpaprOptionVector, 1, bitmap_size),
4162ef3760SMichael Roth VMSTATE_END_OF_LIST()
4262ef3760SMichael Roth }
43b20b7b7aSMichael Roth };
44b20b7b7aSMichael Roth
spapr_ovec_new(void)45ce2918cbSDavid Gibson SpaprOptionVector *spapr_ovec_new(void)
46b20b7b7aSMichael Roth {
47ce2918cbSDavid Gibson SpaprOptionVector *ov;
48b20b7b7aSMichael Roth
49ce2918cbSDavid Gibson ov = g_new0(SpaprOptionVector, 1);
50b20b7b7aSMichael Roth ov->bitmap = bitmap_new(OV_MAXBITS);
5162ef3760SMichael Roth ov->bitmap_size = OV_MAXBITS;
52b20b7b7aSMichael Roth
53b20b7b7aSMichael Roth return ov;
54b20b7b7aSMichael Roth }
55b20b7b7aSMichael Roth
spapr_ovec_clone(SpaprOptionVector * ov_orig)56ce2918cbSDavid Gibson SpaprOptionVector *spapr_ovec_clone(SpaprOptionVector *ov_orig)
57b20b7b7aSMichael Roth {
58ce2918cbSDavid Gibson SpaprOptionVector *ov;
59b20b7b7aSMichael Roth
60b20b7b7aSMichael Roth g_assert(ov_orig);
61b20b7b7aSMichael Roth
62b20b7b7aSMichael Roth ov = spapr_ovec_new();
63b20b7b7aSMichael Roth bitmap_copy(ov->bitmap, ov_orig->bitmap, OV_MAXBITS);
64b20b7b7aSMichael Roth
65b20b7b7aSMichael Roth return ov;
66b20b7b7aSMichael Roth }
67b20b7b7aSMichael Roth
spapr_ovec_intersect(SpaprOptionVector * ov,SpaprOptionVector * ov1,SpaprOptionVector * ov2)68ce2918cbSDavid Gibson void spapr_ovec_intersect(SpaprOptionVector *ov,
69ce2918cbSDavid Gibson SpaprOptionVector *ov1,
70ce2918cbSDavid Gibson SpaprOptionVector *ov2)
71b20b7b7aSMichael Roth {
72b20b7b7aSMichael Roth g_assert(ov);
73b20b7b7aSMichael Roth g_assert(ov1);
74b20b7b7aSMichael Roth g_assert(ov2);
75b20b7b7aSMichael Roth
76b20b7b7aSMichael Roth bitmap_and(ov->bitmap, ov1->bitmap, ov2->bitmap, OV_MAXBITS);
77b20b7b7aSMichael Roth }
78b20b7b7aSMichael Roth
79d1d32d62SDavid Gibson /* returns true if ov1 has a subset of bits in ov2 */
spapr_ovec_subset(SpaprOptionVector * ov1,SpaprOptionVector * ov2)80d1d32d62SDavid Gibson bool spapr_ovec_subset(SpaprOptionVector *ov1, SpaprOptionVector *ov2)
81b20b7b7aSMichael Roth {
82d1d32d62SDavid Gibson unsigned long *tmp = bitmap_new(OV_MAXBITS);
83d1d32d62SDavid Gibson bool result;
84b20b7b7aSMichael Roth
85d1d32d62SDavid Gibson g_assert(ov1);
86d1d32d62SDavid Gibson g_assert(ov2);
87b20b7b7aSMichael Roth
88d1d32d62SDavid Gibson bitmap_andnot(tmp, ov1->bitmap, ov2->bitmap, OV_MAXBITS);
89d1d32d62SDavid Gibson result = bitmap_empty(tmp, OV_MAXBITS);
90b20b7b7aSMichael Roth
91d1d32d62SDavid Gibson g_free(tmp);
92b20b7b7aSMichael Roth
93d1d32d62SDavid Gibson return result;
94b20b7b7aSMichael Roth }
95b20b7b7aSMichael Roth
spapr_ovec_cleanup(SpaprOptionVector * ov)96ce2918cbSDavid Gibson void spapr_ovec_cleanup(SpaprOptionVector *ov)
97b20b7b7aSMichael Roth {
98b20b7b7aSMichael Roth if (ov) {
99b20b7b7aSMichael Roth g_free(ov->bitmap);
100b20b7b7aSMichael Roth g_free(ov);
101b20b7b7aSMichael Roth }
102b20b7b7aSMichael Roth }
103b20b7b7aSMichael Roth
spapr_ovec_set(SpaprOptionVector * ov,long bitnr)104ce2918cbSDavid Gibson void spapr_ovec_set(SpaprOptionVector *ov, long bitnr)
105b20b7b7aSMichael Roth {
106b20b7b7aSMichael Roth g_assert(ov);
107719a3077SMarkus Armbruster g_assert(bitnr < OV_MAXBITS);
108b20b7b7aSMichael Roth
109b20b7b7aSMichael Roth set_bit(bitnr, ov->bitmap);
110b20b7b7aSMichael Roth }
111b20b7b7aSMichael Roth
spapr_ovec_clear(SpaprOptionVector * ov,long bitnr)112ce2918cbSDavid Gibson void spapr_ovec_clear(SpaprOptionVector *ov, long bitnr)
113b20b7b7aSMichael Roth {
114b20b7b7aSMichael Roth g_assert(ov);
115719a3077SMarkus Armbruster g_assert(bitnr < OV_MAXBITS);
116b20b7b7aSMichael Roth
117b20b7b7aSMichael Roth clear_bit(bitnr, ov->bitmap);
118b20b7b7aSMichael Roth }
119b20b7b7aSMichael Roth
spapr_ovec_test(SpaprOptionVector * ov,long bitnr)120ce2918cbSDavid Gibson bool spapr_ovec_test(SpaprOptionVector *ov, long bitnr)
121b20b7b7aSMichael Roth {
122b20b7b7aSMichael Roth g_assert(ov);
123719a3077SMarkus Armbruster g_assert(bitnr < OV_MAXBITS);
124b20b7b7aSMichael Roth
125b20b7b7aSMichael Roth return test_bit(bitnr, ov->bitmap) ? true : false;
126b20b7b7aSMichael Roth }
127b20b7b7aSMichael Roth
spapr_ovec_empty(SpaprOptionVector * ov)12873598c75SGreg Kurz bool spapr_ovec_empty(SpaprOptionVector *ov)
12973598c75SGreg Kurz {
13073598c75SGreg Kurz g_assert(ov);
13173598c75SGreg Kurz
13273598c75SGreg Kurz return bitmap_empty(ov->bitmap, OV_MAXBITS);
13373598c75SGreg Kurz }
13473598c75SGreg Kurz
guest_byte_to_bitmap(uint8_t entry,unsigned long * bitmap,long bitmap_offset)135b20b7b7aSMichael Roth static void guest_byte_to_bitmap(uint8_t entry, unsigned long *bitmap,
136b20b7b7aSMichael Roth long bitmap_offset)
137b20b7b7aSMichael Roth {
138b20b7b7aSMichael Roth int i;
139b20b7b7aSMichael Roth
140b20b7b7aSMichael Roth for (i = 0; i < BITS_PER_BYTE; i++) {
141b20b7b7aSMichael Roth if (entry & (1 << (BITS_PER_BYTE - 1 - i))) {
142b20b7b7aSMichael Roth bitmap_set(bitmap, bitmap_offset + i, 1);
143b20b7b7aSMichael Roth }
144b20b7b7aSMichael Roth }
145b20b7b7aSMichael Roth }
146b20b7b7aSMichael Roth
guest_byte_from_bitmap(unsigned long * bitmap,long bitmap_offset)147b20b7b7aSMichael Roth static uint8_t guest_byte_from_bitmap(unsigned long *bitmap, long bitmap_offset)
148b20b7b7aSMichael Roth {
149b20b7b7aSMichael Roth uint8_t entry = 0;
150b20b7b7aSMichael Roth int i;
151b20b7b7aSMichael Roth
152b20b7b7aSMichael Roth for (i = 0; i < BITS_PER_BYTE; i++) {
153b20b7b7aSMichael Roth if (test_bit(bitmap_offset + i, bitmap)) {
154b20b7b7aSMichael Roth entry |= (1 << (BITS_PER_BYTE - 1 - i));
155b20b7b7aSMichael Roth }
156b20b7b7aSMichael Roth }
157b20b7b7aSMichael Roth
158b20b7b7aSMichael Roth return entry;
159b20b7b7aSMichael Roth }
160b20b7b7aSMichael Roth
vector_addr(target_ulong table_addr,int vector)161b20b7b7aSMichael Roth static target_ulong vector_addr(target_ulong table_addr, int vector)
162b20b7b7aSMichael Roth {
163b20b7b7aSMichael Roth uint16_t vector_count, vector_len;
164b20b7b7aSMichael Roth int i;
165b20b7b7aSMichael Roth
166b20b7b7aSMichael Roth vector_count = ldub_phys(&address_space_memory, table_addr) + 1;
167b20b7b7aSMichael Roth if (vector > vector_count) {
168b20b7b7aSMichael Roth return 0;
169b20b7b7aSMichael Roth }
170b20b7b7aSMichael Roth table_addr++; /* skip nr option vectors */
171b20b7b7aSMichael Roth
172b20b7b7aSMichael Roth for (i = 0; i < vector - 1; i++) {
173b20b7b7aSMichael Roth vector_len = ldub_phys(&address_space_memory, table_addr) + 1;
174b20b7b7aSMichael Roth table_addr += vector_len + 1; /* bit-vector + length byte */
175b20b7b7aSMichael Roth }
176b20b7b7aSMichael Roth return table_addr;
177b20b7b7aSMichael Roth }
178b20b7b7aSMichael Roth
spapr_ovec_parse_vector(target_ulong table_addr,int vector)179ce2918cbSDavid Gibson SpaprOptionVector *spapr_ovec_parse_vector(target_ulong table_addr, int vector)
180b20b7b7aSMichael Roth {
181ce2918cbSDavid Gibson SpaprOptionVector *ov;
182b20b7b7aSMichael Roth target_ulong addr;
183b20b7b7aSMichael Roth uint16_t vector_len;
184b20b7b7aSMichael Roth int i;
185b20b7b7aSMichael Roth
186b20b7b7aSMichael Roth g_assert(table_addr);
187719a3077SMarkus Armbruster g_assert(vector >= 1); /* vector numbering starts at 1 */
188b20b7b7aSMichael Roth
189b20b7b7aSMichael Roth addr = vector_addr(table_addr, vector);
190b20b7b7aSMichael Roth if (!addr) {
191b20b7b7aSMichael Roth /* specified vector isn't present */
192b20b7b7aSMichael Roth return NULL;
193b20b7b7aSMichael Roth }
194b20b7b7aSMichael Roth
195b20b7b7aSMichael Roth vector_len = ldub_phys(&address_space_memory, addr++) + 1;
196719a3077SMarkus Armbruster g_assert(vector_len <= OV_MAXBYTES);
197b20b7b7aSMichael Roth ov = spapr_ovec_new();
198b20b7b7aSMichael Roth
199b20b7b7aSMichael Roth for (i = 0; i < vector_len; i++) {
200b20b7b7aSMichael Roth uint8_t entry = ldub_phys(&address_space_memory, addr + i);
201b20b7b7aSMichael Roth if (entry) {
2025b929608SLaurent Vivier trace_spapr_ovec_parse_vector(vector, i + 1, vector_len, entry);
203b20b7b7aSMichael Roth guest_byte_to_bitmap(entry, ov->bitmap, i * BITS_PER_BYTE);
204b20b7b7aSMichael Roth }
205b20b7b7aSMichael Roth }
206b20b7b7aSMichael Roth
207b20b7b7aSMichael Roth return ov;
208b20b7b7aSMichael Roth }
209b20b7b7aSMichael Roth
spapr_dt_ovec(void * fdt,int fdt_offset,SpaprOptionVector * ov,const char * name)21091335a5eSDavid Gibson int spapr_dt_ovec(void *fdt, int fdt_offset,
211ce2918cbSDavid Gibson SpaprOptionVector *ov, const char *name)
212b20b7b7aSMichael Roth {
213b20b7b7aSMichael Roth uint8_t vec[OV_MAXBYTES + 1];
214b20b7b7aSMichael Roth uint16_t vec_len;
215b20b7b7aSMichael Roth unsigned long lastbit;
216b20b7b7aSMichael Roth int i;
217b20b7b7aSMichael Roth
218b20b7b7aSMichael Roth g_assert(ov);
219b20b7b7aSMichael Roth
220b20b7b7aSMichael Roth lastbit = find_last_bit(ov->bitmap, OV_MAXBITS);
221b20b7b7aSMichael Roth /* if no bits are set, include at least 1 byte of the vector so we can
222b20b7b7aSMichael Roth * still encoded this in the device tree while abiding by the same
223b20b7b7aSMichael Roth * encoding/sizing expected in ibm,client-architecture-support
224b20b7b7aSMichael Roth */
225b20b7b7aSMichael Roth vec_len = (lastbit == OV_MAXBITS) ? 1 : lastbit / BITS_PER_BYTE + 1;
226719a3077SMarkus Armbruster g_assert(vec_len <= OV_MAXBYTES);
227b20b7b7aSMichael Roth /* guest expects vector len encoded as vec_len - 1, since the length byte
228b20b7b7aSMichael Roth * is assumed and not included, and the first byte of the vector
229b20b7b7aSMichael Roth * is assumed as well
230b20b7b7aSMichael Roth */
231b20b7b7aSMichael Roth vec[0] = vec_len - 1;
232b20b7b7aSMichael Roth
233b20b7b7aSMichael Roth for (i = 1; i < vec_len + 1; i++) {
234b20b7b7aSMichael Roth vec[i] = guest_byte_from_bitmap(ov->bitmap, (i - 1) * BITS_PER_BYTE);
235b20b7b7aSMichael Roth if (vec[i]) {
2365b929608SLaurent Vivier trace_spapr_ovec_populate_dt(i, vec_len, vec[i]);
237b20b7b7aSMichael Roth }
238b20b7b7aSMichael Roth }
239b20b7b7aSMichael Roth
240fe93e3e6SSam Bobroff return fdt_setprop(fdt, fdt_offset, name, vec, vec_len + 1);
241b20b7b7aSMichael Roth }
242