qemu/hw/acpi/memory_hotplug.c
<<
>>
Prefs
   1#include "qemu/osdep.h"
   2#include "hw/acpi/memory_hotplug.h"
   3#include "hw/acpi/pc-hotplug.h"
   4#include "hw/mem/pc-dimm.h"
   5#include "hw/boards.h"
   6#include "hw/qdev-core.h"
   7#include "trace.h"
   8#include "qapi-event.h"
   9
  10static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)
  11{
  12    ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
  13
  14    info->slot_type = ACPI_SLOT_TYPE_DIMM;
  15    info->slot = g_strdup_printf("%d", slot);
  16    info->source = mdev->ost_event;
  17    info->status = mdev->ost_status;
  18    if (mdev->dimm) {
  19        DeviceState *dev = DEVICE(mdev->dimm);
  20        if (dev->id) {
  21            info->device = g_strdup(dev->id);
  22            info->has_device = true;
  23        }
  24    }
  25    return info;
  26}
  27
  28void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
  29{
  30    int i;
  31
  32    for (i = 0; i < mem_st->dev_count; i++) {
  33        ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
  34        elem->value = acpi_memory_device_status(i, &mem_st->devs[i]);
  35        elem->next = NULL;
  36        **list = elem;
  37        *list = &elem->next;
  38    }
  39}
  40
  41static uint64_t acpi_memory_hotplug_read(void *opaque, hwaddr addr,
  42                                         unsigned int size)
  43{
  44    uint32_t val = 0;
  45    MemHotplugState *mem_st = opaque;
  46    MemStatus *mdev;
  47    Object *o;
  48
  49    if (mem_st->selector >= mem_st->dev_count) {
  50        trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
  51        return 0;
  52    }
  53
  54    mdev = &mem_st->devs[mem_st->selector];
  55    o = OBJECT(mdev->dimm);
  56    switch (addr) {
  57    case 0x0: /* Lo part of phys address where DIMM is mapped */
  58        val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) : 0;
  59        trace_mhp_acpi_read_addr_lo(mem_st->selector, val);
  60        break;
  61    case 0x4: /* Hi part of phys address where DIMM is mapped */
  62        val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) >> 32 : 0;
  63        trace_mhp_acpi_read_addr_hi(mem_st->selector, val);
  64        break;
  65    case 0x8: /* Lo part of DIMM size */
  66        val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) : 0;
  67        trace_mhp_acpi_read_size_lo(mem_st->selector, val);
  68        break;
  69    case 0xc: /* Hi part of DIMM size */
  70        val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) >> 32 : 0;
  71        trace_mhp_acpi_read_size_hi(mem_st->selector, val);
  72        break;
  73    case 0x10: /* node proximity for _PXM method */
  74        val = o ? object_property_get_int(o, PC_DIMM_NODE_PROP, NULL) : 0;
  75        trace_mhp_acpi_read_pxm(mem_st->selector, val);
  76        break;
  77    case 0x14: /* pack and return is_* fields */
  78        val |= mdev->is_enabled   ? 1 : 0;
  79        val |= mdev->is_inserting ? 2 : 0;
  80        val |= mdev->is_removing  ? 4 : 0;
  81        trace_mhp_acpi_read_flags(mem_st->selector, val);
  82        break;
  83    default:
  84        val = ~0;
  85        break;
  86    }
  87    return val;
  88}
  89
  90static void acpi_memory_hotplug_write(void *opaque, hwaddr addr, uint64_t data,
  91                                      unsigned int size)
  92{
  93    MemHotplugState *mem_st = opaque;
  94    MemStatus *mdev;
  95    ACPIOSTInfo *info;
  96    DeviceState *dev = NULL;
  97    HotplugHandler *hotplug_ctrl = NULL;
  98    Error *local_err = NULL;
  99
 100    if (!mem_st->dev_count) {
 101        return;
 102    }
 103
 104    if (addr) {
 105        if (mem_st->selector >= mem_st->dev_count) {
 106            trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
 107            return;
 108        }
 109    }
 110
 111    switch (addr) {
 112    case 0x0: /* DIMM slot selector */
 113        mem_st->selector = data;
 114        trace_mhp_acpi_write_slot(mem_st->selector);
 115        break;
 116    case 0x4: /* _OST event  */
 117        mdev = &mem_st->devs[mem_st->selector];
 118        if (data == 1) {
 119            /* TODO: handle device insert OST event */
 120        } else if (data == 3) {
 121            /* TODO: handle device remove OST event */
 122        }
 123        mdev->ost_event = data;
 124        trace_mhp_acpi_write_ost_ev(mem_st->selector, mdev->ost_event);
 125        break;
 126    case 0x8: /* _OST status */
 127        mdev = &mem_st->devs[mem_st->selector];
 128        mdev->ost_status = data;
 129        trace_mhp_acpi_write_ost_status(mem_st->selector, mdev->ost_status);
 130        /* TODO: implement memory removal on guest signal */
 131
 132        info = acpi_memory_device_status(mem_st->selector, mdev);
 133        qapi_event_send_acpi_device_ost(info, &error_abort);
 134        qapi_free_ACPIOSTInfo(info);
 135        break;
 136    case 0x14: /* set is_* fields  */
 137        mdev = &mem_st->devs[mem_st->selector];
 138        if (data & 2) { /* clear insert event */
 139            mdev->is_inserting  = false;
 140            trace_mhp_acpi_clear_insert_evt(mem_st->selector);
 141        } else if (data & 4) {
 142            mdev->is_removing = false;
 143            trace_mhp_acpi_clear_remove_evt(mem_st->selector);
 144        } else if (data & 8) {
 145            if (!mdev->is_enabled) {
 146                trace_mhp_acpi_ejecting_invalid_slot(mem_st->selector);
 147                break;
 148            }
 149
 150            dev = DEVICE(mdev->dimm);
 151            hotplug_ctrl = qdev_get_hotplug_handler(dev);
 152            /* call pc-dimm unplug cb */
 153            hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
 154            if (local_err) {
 155                trace_mhp_acpi_pc_dimm_delete_failed(mem_st->selector);
 156                qapi_event_send_mem_unplug_error(dev->id,
 157                                                 error_get_pretty(local_err),
 158                                                 &error_abort);
 159                error_free(local_err);
 160                break;
 161            }
 162            trace_mhp_acpi_pc_dimm_deleted(mem_st->selector);
 163        }
 164        break;
 165    default:
 166        break;
 167    }
 168
 169}
 170static const MemoryRegionOps acpi_memory_hotplug_ops = {
 171    .read = acpi_memory_hotplug_read,
 172    .write = acpi_memory_hotplug_write,
 173    .endianness = DEVICE_LITTLE_ENDIAN,
 174    .valid = {
 175        .min_access_size = 1,
 176        .max_access_size = 4,
 177    },
 178};
 179
 180void acpi_memory_hotplug_init(MemoryRegion *as, Object *owner,
 181                              MemHotplugState *state)
 182{
 183    MachineState *machine = MACHINE(qdev_get_machine());
 184
 185    state->dev_count = machine->ram_slots;
 186    if (!state->dev_count) {
 187        return;
 188    }
 189
 190    state->devs = g_malloc0(sizeof(*state->devs) * state->dev_count);
 191    memory_region_init_io(&state->io, owner, &acpi_memory_hotplug_ops, state,
 192                          "acpi-mem-hotplug", ACPI_MEMORY_HOTPLUG_IO_LEN);
 193    memory_region_add_subregion(as, ACPI_MEMORY_HOTPLUG_BASE, &state->io);
 194}
 195
 196/**
 197 * acpi_memory_slot_status:
 198 * @mem_st: memory hotplug state
 199 * @dev: device
 200 * @errp: set in case of an error
 201 *
 202 * Obtain a single memory slot status.
 203 *
 204 * This function will be called by memory unplug request cb and unplug cb.
 205 */
 206static MemStatus *
 207acpi_memory_slot_status(MemHotplugState *mem_st,
 208                        DeviceState *dev, Error **errp)
 209{
 210    Error *local_err = NULL;
 211    int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
 212                                       &local_err);
 213
 214    if (local_err) {
 215        error_propagate(errp, local_err);
 216        return NULL;
 217    }
 218
 219    if (slot >= mem_st->dev_count) {
 220        char *dev_path = object_get_canonical_path(OBJECT(dev));
 221        error_setg(errp, "acpi_memory_slot_status: "
 222                   "device [%s] returned invalid memory slot[%d]",
 223                    dev_path, slot);
 224        g_free(dev_path);
 225        return NULL;
 226    }
 227
 228    return &mem_st->devs[slot];
 229}
 230
 231void acpi_memory_plug_cb(ACPIREGS *ar, qemu_irq irq, MemHotplugState *mem_st,
 232                         DeviceState *dev, Error **errp)
 233{
 234    MemStatus *mdev;
 235    DeviceClass *dc = DEVICE_GET_CLASS(dev);
 236
 237    if (!dc->hotpluggable) {
 238        return;
 239    }
 240
 241    mdev = acpi_memory_slot_status(mem_st, dev, errp);
 242    if (!mdev) {
 243        return;
 244    }
 245
 246    mdev->dimm = dev;
 247    mdev->is_enabled = true;
 248    if (dev->hotplugged) {
 249        mdev->is_inserting = true;
 250
 251        /* do ACPI magic */
 252        acpi_send_gpe_event(ar, irq, ACPI_MEMORY_HOTPLUG_STATUS);
 253    }
 254}
 255
 256void acpi_memory_unplug_request_cb(ACPIREGS *ar, qemu_irq irq,
 257                                   MemHotplugState *mem_st,
 258                                   DeviceState *dev, Error **errp)
 259{
 260    MemStatus *mdev;
 261
 262    mdev = acpi_memory_slot_status(mem_st, dev, errp);
 263    if (!mdev) {
 264        return;
 265    }
 266
 267    mdev->is_removing = true;
 268
 269    /* Do ACPI magic */
 270    acpi_send_gpe_event(ar, irq, ACPI_MEMORY_HOTPLUG_STATUS);
 271}
 272
 273void acpi_memory_unplug_cb(MemHotplugState *mem_st,
 274                           DeviceState *dev, Error **errp)
 275{
 276    MemStatus *mdev;
 277
 278    mdev = acpi_memory_slot_status(mem_st, dev, errp);
 279    if (!mdev) {
 280        return;
 281    }
 282
 283    mdev->is_enabled = false;
 284    mdev->dimm = NULL;
 285}
 286
 287static const VMStateDescription vmstate_memhp_sts = {
 288    .name = "memory hotplug device state",
 289    .version_id = 1,
 290    .minimum_version_id = 1,
 291    .minimum_version_id_old = 1,
 292    .fields      = (VMStateField[]) {
 293        VMSTATE_BOOL(is_enabled, MemStatus),
 294        VMSTATE_BOOL(is_inserting, MemStatus),
 295        VMSTATE_UINT32(ost_event, MemStatus),
 296        VMSTATE_UINT32(ost_status, MemStatus),
 297        VMSTATE_END_OF_LIST()
 298    }
 299};
 300
 301const VMStateDescription vmstate_memory_hotplug = {
 302    .name = "memory hotplug state",
 303    .version_id = 1,
 304    .minimum_version_id = 1,
 305    .minimum_version_id_old = 1,
 306    .fields      = (VMStateField[]) {
 307        VMSTATE_UINT32(selector, MemHotplugState),
 308        VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, MemHotplugState, dev_count,
 309                                             vmstate_memhp_sts, MemStatus),
 310        VMSTATE_END_OF_LIST()
 311    }
 312};
 313