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