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"
18dfc56946SRichard Henderson #include "system/address-spaces.h"
19*342e313dSPierrick Bouvier #include "system/memory.h"
20b20b7b7aSMichael Roth #include "qemu/error-report.h"
215b929608SLaurent Vivier #include "trace.h"
22b20b7b7aSMichael Roth #include <libfdt.h>
23b20b7b7aSMichael Roth
24b20b7b7aSMichael Roth #define OV_MAXBYTES 256 /* not including length byte */
25b20b7b7aSMichael Roth #define OV_MAXBITS (OV_MAXBYTES * BITS_PER_BYTE)
26b20b7b7aSMichael Roth
27b20b7b7aSMichael Roth /* we *could* work with bitmaps directly, but handling the bitmap privately
28b20b7b7aSMichael Roth * allows us to more safely make assumptions about the bitmap size and
29b20b7b7aSMichael Roth * simplify the calling code somewhat
30b20b7b7aSMichael Roth */
31ce2918cbSDavid Gibson struct SpaprOptionVector {
32b20b7b7aSMichael Roth unsigned long *bitmap;
3362ef3760SMichael Roth int32_t bitmap_size; /* only used for migration */
3462ef3760SMichael Roth };
3562ef3760SMichael Roth
3662ef3760SMichael Roth const VMStateDescription vmstate_spapr_ovec = {
3762ef3760SMichael Roth .name = "spapr_option_vector",
3862ef3760SMichael Roth .version_id = 1,
3962ef3760SMichael Roth .minimum_version_id = 1,
40078ddbc9SRichard Henderson .fields = (const VMStateField[]) {
41ce2918cbSDavid Gibson VMSTATE_BITMAP(bitmap, SpaprOptionVector, 1, bitmap_size),
4262ef3760SMichael Roth VMSTATE_END_OF_LIST()
4362ef3760SMichael Roth }
44b20b7b7aSMichael Roth };
45b20b7b7aSMichael Roth
spapr_ovec_new(void)46ce2918cbSDavid Gibson SpaprOptionVector *spapr_ovec_new(void)
47b20b7b7aSMichael Roth {
48ce2918cbSDavid Gibson SpaprOptionVector *ov;
49b20b7b7aSMichael Roth
50ce2918cbSDavid Gibson ov = g_new0(SpaprOptionVector, 1);
51b20b7b7aSMichael Roth ov->bitmap = bitmap_new(OV_MAXBITS);
5262ef3760SMichael Roth ov->bitmap_size = OV_MAXBITS;
53b20b7b7aSMichael Roth
54b20b7b7aSMichael Roth return ov;
55b20b7b7aSMichael Roth }
56b20b7b7aSMichael Roth
spapr_ovec_clone(SpaprOptionVector * ov_orig)57ce2918cbSDavid Gibson SpaprOptionVector *spapr_ovec_clone(SpaprOptionVector *ov_orig)
58b20b7b7aSMichael Roth {
59ce2918cbSDavid Gibson SpaprOptionVector *ov;
60b20b7b7aSMichael Roth
61b20b7b7aSMichael Roth g_assert(ov_orig);
62b20b7b7aSMichael Roth
63b20b7b7aSMichael Roth ov = spapr_ovec_new();
64b20b7b7aSMichael Roth bitmap_copy(ov->bitmap, ov_orig->bitmap, OV_MAXBITS);
65b20b7b7aSMichael Roth
66b20b7b7aSMichael Roth return ov;
67b20b7b7aSMichael Roth }
68b20b7b7aSMichael Roth
spapr_ovec_intersect(SpaprOptionVector * ov,SpaprOptionVector * ov1,SpaprOptionVector * ov2)69ce2918cbSDavid Gibson void spapr_ovec_intersect(SpaprOptionVector *ov,
70ce2918cbSDavid Gibson SpaprOptionVector *ov1,
71ce2918cbSDavid Gibson SpaprOptionVector *ov2)
72b20b7b7aSMichael Roth {
73b20b7b7aSMichael Roth g_assert(ov);
74b20b7b7aSMichael Roth g_assert(ov1);
75b20b7b7aSMichael Roth g_assert(ov2);
76b20b7b7aSMichael Roth
77b20b7b7aSMichael Roth bitmap_and(ov->bitmap, ov1->bitmap, ov2->bitmap, OV_MAXBITS);
78b20b7b7aSMichael Roth }
79b20b7b7aSMichael Roth
80d1d32d62SDavid Gibson /* returns true if ov1 has a subset of bits in ov2 */
spapr_ovec_subset(SpaprOptionVector * ov1,SpaprOptionVector * ov2)81d1d32d62SDavid Gibson bool spapr_ovec_subset(SpaprOptionVector *ov1, SpaprOptionVector *ov2)
82b20b7b7aSMichael Roth {
83d1d32d62SDavid Gibson unsigned long *tmp = bitmap_new(OV_MAXBITS);
84d1d32d62SDavid Gibson bool result;
85b20b7b7aSMichael Roth
86d1d32d62SDavid Gibson g_assert(ov1);
87d1d32d62SDavid Gibson g_assert(ov2);
88b20b7b7aSMichael Roth
89d1d32d62SDavid Gibson bitmap_andnot(tmp, ov1->bitmap, ov2->bitmap, OV_MAXBITS);
90d1d32d62SDavid Gibson result = bitmap_empty(tmp, OV_MAXBITS);
91b20b7b7aSMichael Roth
92d1d32d62SDavid Gibson g_free(tmp);
93b20b7b7aSMichael Roth
94d1d32d62SDavid Gibson return result;
95b20b7b7aSMichael Roth }
96b20b7b7aSMichael Roth
spapr_ovec_cleanup(SpaprOptionVector * ov)97ce2918cbSDavid Gibson void spapr_ovec_cleanup(SpaprOptionVector *ov)
98b20b7b7aSMichael Roth {
99b20b7b7aSMichael Roth if (ov) {
100b20b7b7aSMichael Roth g_free(ov->bitmap);
101b20b7b7aSMichael Roth g_free(ov);
102b20b7b7aSMichael Roth }
103b20b7b7aSMichael Roth }
104b20b7b7aSMichael Roth
spapr_ovec_set(SpaprOptionVector * ov,long bitnr)105ce2918cbSDavid Gibson void spapr_ovec_set(SpaprOptionVector *ov, long bitnr)
106b20b7b7aSMichael Roth {
107b20b7b7aSMichael Roth g_assert(ov);
108719a3077SMarkus Armbruster g_assert(bitnr < OV_MAXBITS);
109b20b7b7aSMichael Roth
110b20b7b7aSMichael Roth set_bit(bitnr, ov->bitmap);
111b20b7b7aSMichael Roth }
112b20b7b7aSMichael Roth
spapr_ovec_clear(SpaprOptionVector * ov,long bitnr)113ce2918cbSDavid Gibson void spapr_ovec_clear(SpaprOptionVector *ov, long bitnr)
114b20b7b7aSMichael Roth {
115b20b7b7aSMichael Roth g_assert(ov);
116719a3077SMarkus Armbruster g_assert(bitnr < OV_MAXBITS);
117b20b7b7aSMichael Roth
118b20b7b7aSMichael Roth clear_bit(bitnr, ov->bitmap);
119b20b7b7aSMichael Roth }
120b20b7b7aSMichael Roth
spapr_ovec_test(SpaprOptionVector * ov,long bitnr)121ce2918cbSDavid Gibson bool spapr_ovec_test(SpaprOptionVector *ov, long bitnr)
122b20b7b7aSMichael Roth {
123b20b7b7aSMichael Roth g_assert(ov);
124719a3077SMarkus Armbruster g_assert(bitnr < OV_MAXBITS);
125b20b7b7aSMichael Roth
126b20b7b7aSMichael Roth return test_bit(bitnr, ov->bitmap) ? true : false;
127b20b7b7aSMichael Roth }
128b20b7b7aSMichael Roth
spapr_ovec_empty(SpaprOptionVector * ov)12973598c75SGreg Kurz bool spapr_ovec_empty(SpaprOptionVector *ov)
13073598c75SGreg Kurz {
13173598c75SGreg Kurz g_assert(ov);
13273598c75SGreg Kurz
13373598c75SGreg Kurz return bitmap_empty(ov->bitmap, OV_MAXBITS);
13473598c75SGreg Kurz }
13573598c75SGreg Kurz
guest_byte_to_bitmap(uint8_t entry,unsigned long * bitmap,long bitmap_offset)136b20b7b7aSMichael Roth static void guest_byte_to_bitmap(uint8_t entry, unsigned long *bitmap,
137b20b7b7aSMichael Roth long bitmap_offset)
138b20b7b7aSMichael Roth {
139b20b7b7aSMichael Roth int i;
140b20b7b7aSMichael Roth
141b20b7b7aSMichael Roth for (i = 0; i < BITS_PER_BYTE; i++) {
142b20b7b7aSMichael Roth if (entry & (1 << (BITS_PER_BYTE - 1 - i))) {
143b20b7b7aSMichael Roth bitmap_set(bitmap, bitmap_offset + i, 1);
144b20b7b7aSMichael Roth }
145b20b7b7aSMichael Roth }
146b20b7b7aSMichael Roth }
147b20b7b7aSMichael Roth
guest_byte_from_bitmap(unsigned long * bitmap,long bitmap_offset)148b20b7b7aSMichael Roth static uint8_t guest_byte_from_bitmap(unsigned long *bitmap, long bitmap_offset)
149b20b7b7aSMichael Roth {
150b20b7b7aSMichael Roth uint8_t entry = 0;
151b20b7b7aSMichael Roth int i;
152b20b7b7aSMichael Roth
153b20b7b7aSMichael Roth for (i = 0; i < BITS_PER_BYTE; i++) {
154b20b7b7aSMichael Roth if (test_bit(bitmap_offset + i, bitmap)) {
155b20b7b7aSMichael Roth entry |= (1 << (BITS_PER_BYTE - 1 - i));
156b20b7b7aSMichael Roth }
157b20b7b7aSMichael Roth }
158b20b7b7aSMichael Roth
159b20b7b7aSMichael Roth return entry;
160b20b7b7aSMichael Roth }
161b20b7b7aSMichael Roth
vector_addr(target_ulong table_addr,int vector)162b20b7b7aSMichael Roth static target_ulong vector_addr(target_ulong table_addr, int vector)
163b20b7b7aSMichael Roth {
164b20b7b7aSMichael Roth uint16_t vector_count, vector_len;
165b20b7b7aSMichael Roth int i;
166b20b7b7aSMichael Roth
167b20b7b7aSMichael Roth vector_count = ldub_phys(&address_space_memory, table_addr) + 1;
168b20b7b7aSMichael Roth if (vector > vector_count) {
169b20b7b7aSMichael Roth return 0;
170b20b7b7aSMichael Roth }
171b20b7b7aSMichael Roth table_addr++; /* skip nr option vectors */
172b20b7b7aSMichael Roth
173b20b7b7aSMichael Roth for (i = 0; i < vector - 1; i++) {
174b20b7b7aSMichael Roth vector_len = ldub_phys(&address_space_memory, table_addr) + 1;
175b20b7b7aSMichael Roth table_addr += vector_len + 1; /* bit-vector + length byte */
176b20b7b7aSMichael Roth }
177b20b7b7aSMichael Roth return table_addr;
178b20b7b7aSMichael Roth }
179b20b7b7aSMichael Roth
spapr_ovec_parse_vector(target_ulong table_addr,int vector)180ce2918cbSDavid Gibson SpaprOptionVector *spapr_ovec_parse_vector(target_ulong table_addr, int vector)
181b20b7b7aSMichael Roth {
182ce2918cbSDavid Gibson SpaprOptionVector *ov;
183b20b7b7aSMichael Roth target_ulong addr;
184b20b7b7aSMichael Roth uint16_t vector_len;
185b20b7b7aSMichael Roth int i;
186b20b7b7aSMichael Roth
187b20b7b7aSMichael Roth g_assert(table_addr);
188719a3077SMarkus Armbruster g_assert(vector >= 1); /* vector numbering starts at 1 */
189b20b7b7aSMichael Roth
190b20b7b7aSMichael Roth addr = vector_addr(table_addr, vector);
191b20b7b7aSMichael Roth if (!addr) {
192b20b7b7aSMichael Roth /* specified vector isn't present */
193b20b7b7aSMichael Roth return NULL;
194b20b7b7aSMichael Roth }
195b20b7b7aSMichael Roth
196b20b7b7aSMichael Roth vector_len = ldub_phys(&address_space_memory, addr++) + 1;
197719a3077SMarkus Armbruster g_assert(vector_len <= OV_MAXBYTES);
198b20b7b7aSMichael Roth ov = spapr_ovec_new();
199b20b7b7aSMichael Roth
200b20b7b7aSMichael Roth for (i = 0; i < vector_len; i++) {
201b20b7b7aSMichael Roth uint8_t entry = ldub_phys(&address_space_memory, addr + i);
202b20b7b7aSMichael Roth if (entry) {
2035b929608SLaurent Vivier trace_spapr_ovec_parse_vector(vector, i + 1, vector_len, entry);
204b20b7b7aSMichael Roth guest_byte_to_bitmap(entry, ov->bitmap, i * BITS_PER_BYTE);
205b20b7b7aSMichael Roth }
206b20b7b7aSMichael Roth }
207b20b7b7aSMichael Roth
208b20b7b7aSMichael Roth return ov;
209b20b7b7aSMichael Roth }
210b20b7b7aSMichael Roth
spapr_dt_ovec(void * fdt,int fdt_offset,SpaprOptionVector * ov,const char * name)21191335a5eSDavid Gibson int spapr_dt_ovec(void *fdt, int fdt_offset,
212ce2918cbSDavid Gibson SpaprOptionVector *ov, const char *name)
213b20b7b7aSMichael Roth {
214b20b7b7aSMichael Roth uint8_t vec[OV_MAXBYTES + 1];
215b20b7b7aSMichael Roth uint16_t vec_len;
216b20b7b7aSMichael Roth unsigned long lastbit;
217b20b7b7aSMichael Roth int i;
218b20b7b7aSMichael Roth
219b20b7b7aSMichael Roth g_assert(ov);
220b20b7b7aSMichael Roth
221b20b7b7aSMichael Roth lastbit = find_last_bit(ov->bitmap, OV_MAXBITS);
222b20b7b7aSMichael Roth /* if no bits are set, include at least 1 byte of the vector so we can
223b20b7b7aSMichael Roth * still encoded this in the device tree while abiding by the same
224b20b7b7aSMichael Roth * encoding/sizing expected in ibm,client-architecture-support
225b20b7b7aSMichael Roth */
226b20b7b7aSMichael Roth vec_len = (lastbit == OV_MAXBITS) ? 1 : lastbit / BITS_PER_BYTE + 1;
227719a3077SMarkus Armbruster g_assert(vec_len <= OV_MAXBYTES);
228b20b7b7aSMichael Roth /* guest expects vector len encoded as vec_len - 1, since the length byte
229b20b7b7aSMichael Roth * is assumed and not included, and the first byte of the vector
230b20b7b7aSMichael Roth * is assumed as well
231b20b7b7aSMichael Roth */
232b20b7b7aSMichael Roth vec[0] = vec_len - 1;
233b20b7b7aSMichael Roth
234b20b7b7aSMichael Roth for (i = 1; i < vec_len + 1; i++) {
235b20b7b7aSMichael Roth vec[i] = guest_byte_from_bitmap(ov->bitmap, (i - 1) * BITS_PER_BYTE);
236b20b7b7aSMichael Roth if (vec[i]) {
2375b929608SLaurent Vivier trace_spapr_ovec_populate_dt(i, vec_len, vec[i]);
238b20b7b7aSMichael Roth }
239b20b7b7aSMichael Roth }
240b20b7b7aSMichael Roth
241fe93e3e6SSam Bobroff return fdt_setprop(fdt, fdt_offset, name, vec, vec_len + 1);
242b20b7b7aSMichael Roth }
243