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