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