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; 40 }; 41 42 sPAPROptionVector *spapr_ovec_new(void) 43 { 44 sPAPROptionVector *ov; 45 46 ov = g_new0(sPAPROptionVector, 1); 47 ov->bitmap = bitmap_new(OV_MAXBITS); 48 49 return ov; 50 } 51 52 sPAPROptionVector *spapr_ovec_clone(sPAPROptionVector *ov_orig) 53 { 54 sPAPROptionVector *ov; 55 56 g_assert(ov_orig); 57 58 ov = spapr_ovec_new(); 59 bitmap_copy(ov->bitmap, ov_orig->bitmap, OV_MAXBITS); 60 61 return ov; 62 } 63 64 void spapr_ovec_intersect(sPAPROptionVector *ov, 65 sPAPROptionVector *ov1, 66 sPAPROptionVector *ov2) 67 { 68 g_assert(ov); 69 g_assert(ov1); 70 g_assert(ov2); 71 72 bitmap_and(ov->bitmap, ov1->bitmap, ov2->bitmap, OV_MAXBITS); 73 } 74 75 /* returns true if options bits were removed, false otherwise */ 76 bool spapr_ovec_diff(sPAPROptionVector *ov, 77 sPAPROptionVector *ov_old, 78 sPAPROptionVector *ov_new) 79 { 80 unsigned long *change_mask = bitmap_new(OV_MAXBITS); 81 unsigned long *removed_bits = bitmap_new(OV_MAXBITS); 82 bool bits_were_removed = false; 83 84 g_assert(ov); 85 g_assert(ov_old); 86 g_assert(ov_new); 87 88 bitmap_xor(change_mask, ov_old->bitmap, ov_new->bitmap, OV_MAXBITS); 89 bitmap_and(ov->bitmap, ov_new->bitmap, change_mask, OV_MAXBITS); 90 bitmap_and(removed_bits, ov_old->bitmap, change_mask, OV_MAXBITS); 91 92 if (!bitmap_empty(removed_bits, OV_MAXBITS)) { 93 bits_were_removed = true; 94 } 95 96 g_free(change_mask); 97 g_free(removed_bits); 98 99 return bits_were_removed; 100 } 101 102 void spapr_ovec_cleanup(sPAPROptionVector *ov) 103 { 104 if (ov) { 105 g_free(ov->bitmap); 106 g_free(ov); 107 } 108 } 109 110 void spapr_ovec_set(sPAPROptionVector *ov, long bitnr) 111 { 112 g_assert(ov); 113 g_assert_cmpint(bitnr, <, OV_MAXBITS); 114 115 set_bit(bitnr, ov->bitmap); 116 } 117 118 void spapr_ovec_clear(sPAPROptionVector *ov, long bitnr) 119 { 120 g_assert(ov); 121 g_assert_cmpint(bitnr, <, OV_MAXBITS); 122 123 clear_bit(bitnr, ov->bitmap); 124 } 125 126 bool spapr_ovec_test(sPAPROptionVector *ov, long bitnr) 127 { 128 g_assert(ov); 129 g_assert_cmpint(bitnr, <, OV_MAXBITS); 130 131 return test_bit(bitnr, ov->bitmap) ? true : false; 132 } 133 134 static void guest_byte_to_bitmap(uint8_t entry, unsigned long *bitmap, 135 long bitmap_offset) 136 { 137 int i; 138 139 for (i = 0; i < BITS_PER_BYTE; i++) { 140 if (entry & (1 << (BITS_PER_BYTE - 1 - i))) { 141 bitmap_set(bitmap, bitmap_offset + i, 1); 142 } 143 } 144 } 145 146 static uint8_t guest_byte_from_bitmap(unsigned long *bitmap, long bitmap_offset) 147 { 148 uint8_t entry = 0; 149 int i; 150 151 for (i = 0; i < BITS_PER_BYTE; i++) { 152 if (test_bit(bitmap_offset + i, bitmap)) { 153 entry |= (1 << (BITS_PER_BYTE - 1 - i)); 154 } 155 } 156 157 return entry; 158 } 159 160 static target_ulong vector_addr(target_ulong table_addr, int vector) 161 { 162 uint16_t vector_count, vector_len; 163 int i; 164 165 vector_count = ldub_phys(&address_space_memory, table_addr) + 1; 166 if (vector > vector_count) { 167 return 0; 168 } 169 table_addr++; /* skip nr option vectors */ 170 171 for (i = 0; i < vector - 1; i++) { 172 vector_len = ldub_phys(&address_space_memory, table_addr) + 1; 173 table_addr += vector_len + 1; /* bit-vector + length byte */ 174 } 175 return table_addr; 176 } 177 178 sPAPROptionVector *spapr_ovec_parse_vector(target_ulong table_addr, int vector) 179 { 180 sPAPROptionVector *ov; 181 target_ulong addr; 182 uint16_t vector_len; 183 int i; 184 185 g_assert(table_addr); 186 g_assert_cmpint(vector, >=, 1); /* vector numbering starts at 1 */ 187 188 addr = vector_addr(table_addr, vector); 189 if (!addr) { 190 /* specified vector isn't present */ 191 return NULL; 192 } 193 194 vector_len = ldub_phys(&address_space_memory, addr++) + 1; 195 g_assert_cmpint(vector_len, <=, OV_MAXBYTES); 196 ov = spapr_ovec_new(); 197 198 for (i = 0; i < vector_len; i++) { 199 uint8_t entry = ldub_phys(&address_space_memory, addr + i); 200 if (entry) { 201 DPRINTFN("read guest vector %2d, byte %3d / %3d: 0x%.2x", 202 vector, i + 1, vector_len, entry); 203 guest_byte_to_bitmap(entry, ov->bitmap, i * BITS_PER_BYTE); 204 } 205 } 206 207 return ov; 208 } 209 210 int spapr_ovec_populate_dt(void *fdt, int fdt_offset, 211 sPAPROptionVector *ov, const char *name) 212 { 213 uint8_t vec[OV_MAXBYTES + 1]; 214 uint16_t vec_len; 215 unsigned long lastbit; 216 int i; 217 218 g_assert(ov); 219 220 lastbit = find_last_bit(ov->bitmap, OV_MAXBITS); 221 /* if no bits are set, include at least 1 byte of the vector so we can 222 * still encoded this in the device tree while abiding by the same 223 * encoding/sizing expected in ibm,client-architecture-support 224 */ 225 vec_len = (lastbit == OV_MAXBITS) ? 1 : lastbit / BITS_PER_BYTE + 1; 226 g_assert_cmpint(vec_len, <=, OV_MAXBYTES); 227 /* guest expects vector len encoded as vec_len - 1, since the length byte 228 * is assumed and not included, and the first byte of the vector 229 * is assumed as well 230 */ 231 vec[0] = vec_len - 1; 232 233 for (i = 1; i < vec_len + 1; i++) { 234 vec[i] = guest_byte_from_bitmap(ov->bitmap, (i - 1) * BITS_PER_BYTE); 235 if (vec[i]) { 236 DPRINTFN("encoding guest vector byte %3d / %3d: 0x%.2x", 237 i, vec_len, vec[i]); 238 } 239 } 240 241 return fdt_setprop(fdt, fdt_offset, name, vec, vec_len); 242 } 243