1ee3a71e3SShivaprasad G Bhat /* 2ee3a71e3SShivaprasad G Bhat * QEMU PAPR Storage Class Memory Interfaces 3ee3a71e3SShivaprasad G Bhat * 4ee3a71e3SShivaprasad G Bhat * Copyright (c) 2019-2020, IBM Corporation. 5ee3a71e3SShivaprasad G Bhat * 6ee3a71e3SShivaprasad G Bhat * Permission is hereby granted, free of charge, to any person obtaining a copy 7ee3a71e3SShivaprasad G Bhat * of this software and associated documentation files (the "Software"), to deal 8ee3a71e3SShivaprasad G Bhat * in the Software without restriction, including without limitation the rights 9ee3a71e3SShivaprasad G Bhat * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10ee3a71e3SShivaprasad G Bhat * copies of the Software, and to permit persons to whom the Software is 11ee3a71e3SShivaprasad G Bhat * furnished to do so, subject to the following conditions: 12ee3a71e3SShivaprasad G Bhat * 13ee3a71e3SShivaprasad G Bhat * The above copyright notice and this permission notice shall be included in 14ee3a71e3SShivaprasad G Bhat * all copies or substantial portions of the Software. 15ee3a71e3SShivaprasad G Bhat * 16ee3a71e3SShivaprasad G Bhat * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17ee3a71e3SShivaprasad G Bhat * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18ee3a71e3SShivaprasad G Bhat * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19ee3a71e3SShivaprasad G Bhat * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20ee3a71e3SShivaprasad G Bhat * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21ee3a71e3SShivaprasad G Bhat * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22ee3a71e3SShivaprasad G Bhat * THE SOFTWARE. 23ee3a71e3SShivaprasad G Bhat */ 24ee3a71e3SShivaprasad G Bhat #include "qemu/osdep.h" 25ee3a71e3SShivaprasad G Bhat #include "qapi/error.h" 26ee3a71e3SShivaprasad G Bhat #include "hw/ppc/spapr_drc.h" 27ee3a71e3SShivaprasad G Bhat #include "hw/ppc/spapr_nvdimm.h" 28ee3a71e3SShivaprasad G Bhat #include "hw/mem/nvdimm.h" 29ee3a71e3SShivaprasad G Bhat #include "qemu/nvdimm-utils.h" 3028f5a716SDaniel Henrique Barboza #include "qemu/option.h" 31ee3a71e3SShivaprasad G Bhat #include "hw/ppc/fdt.h" 32b5fca656SShivaprasad G Bhat #include "qemu/range.h" 3328f5a716SDaniel Henrique Barboza #include "sysemu/sysemu.h" 34ee3a71e3SShivaprasad G Bhat 35beb6073fSDaniel Henrique Barboza void spapr_nvdimm_validate(HotplugHandler *hotplug_dev, NVDIMMDevice *nvdimm, 36beb6073fSDaniel Henrique Barboza uint64_t size, Error **errp) 37ee3a71e3SShivaprasad G Bhat { 38beb6073fSDaniel Henrique Barboza const MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev); 3928f5a716SDaniel Henrique Barboza const MachineState *ms = MACHINE(hotplug_dev); 4028f5a716SDaniel Henrique Barboza const char *nvdimm_opt = qemu_opt_get(qemu_get_machine_opts(), "nvdimm"); 4190d282d0SDaniel Henrique Barboza g_autofree char *uuidstr = NULL; 42ee3a71e3SShivaprasad G Bhat QemuUUID uuid; 43af7084e7SShivaprasad G Bhat int ret; 44ee3a71e3SShivaprasad G Bhat 45beb6073fSDaniel Henrique Barboza if (!mc->nvdimm_supported) { 46beb6073fSDaniel Henrique Barboza error_setg(errp, "NVDIMM hotplug not supported for this machine"); 47beb6073fSDaniel Henrique Barboza return; 48beb6073fSDaniel Henrique Barboza } 49beb6073fSDaniel Henrique Barboza 5028f5a716SDaniel Henrique Barboza /* 5128f5a716SDaniel Henrique Barboza * NVDIMM support went live in 5.1 without considering that, in 5228f5a716SDaniel Henrique Barboza * other archs, the user needs to enable NVDIMM support with the 5328f5a716SDaniel Henrique Barboza * 'nvdimm' machine option and the default behavior is NVDIMM 5428f5a716SDaniel Henrique Barboza * support disabled. It is too late to roll back to the standard 5528f5a716SDaniel Henrique Barboza * behavior without breaking 5.1 guests. What we can do is to 5628f5a716SDaniel Henrique Barboza * ensure that, if the user sets nvdimm=off, we error out 5728f5a716SDaniel Henrique Barboza * regardless of being 5.1 or newer. 5828f5a716SDaniel Henrique Barboza */ 5928f5a716SDaniel Henrique Barboza if (!ms->nvdimms_state->is_enabled && nvdimm_opt) { 6028f5a716SDaniel Henrique Barboza error_setg(errp, "nvdimm device found but 'nvdimm=off' was set"); 6128f5a716SDaniel Henrique Barboza return; 6228f5a716SDaniel Henrique Barboza } 6328f5a716SDaniel Henrique Barboza 6470fc9cb0SDaniel Henrique Barboza if (object_property_get_int(OBJECT(nvdimm), NVDIMM_LABEL_SIZE_PROP, 6570fc9cb0SDaniel Henrique Barboza &error_abort) == 0) { 666c0f0cb3SDavid Gibson error_setg(errp, "PAPR requires NVDIMM devices to have label-size set"); 6770fc9cb0SDaniel Henrique Barboza return; 6870fc9cb0SDaniel Henrique Barboza } 6970fc9cb0SDaniel Henrique Barboza 70ee3a71e3SShivaprasad G Bhat if (size % SPAPR_MINIMUM_SCM_BLOCK_SIZE) { 716c0f0cb3SDavid Gibson error_setg(errp, "PAPR requires NVDIMM memory size (excluding label)" 726c0f0cb3SDavid Gibson " to be a multiple of %" PRIu64 "MB", 73ee3a71e3SShivaprasad G Bhat SPAPR_MINIMUM_SCM_BLOCK_SIZE / MiB); 74ee3a71e3SShivaprasad G Bhat return; 75ee3a71e3SShivaprasad G Bhat } 76ee3a71e3SShivaprasad G Bhat 77af7084e7SShivaprasad G Bhat uuidstr = object_property_get_str(OBJECT(nvdimm), NVDIMM_UUID_PROP, 78af7084e7SShivaprasad G Bhat &error_abort); 79af7084e7SShivaprasad G Bhat ret = qemu_uuid_parse(uuidstr, &uuid); 80af7084e7SShivaprasad G Bhat g_assert(!ret); 81ee3a71e3SShivaprasad G Bhat 82ee3a71e3SShivaprasad G Bhat if (qemu_uuid_is_null(&uuid)) { 83ee3a71e3SShivaprasad G Bhat error_setg(errp, "NVDIMM device requires the uuid to be set"); 84ee3a71e3SShivaprasad G Bhat return; 85ee3a71e3SShivaprasad G Bhat } 86ee3a71e3SShivaprasad G Bhat } 87ee3a71e3SShivaprasad G Bhat 88ee3a71e3SShivaprasad G Bhat 89ee3a71e3SShivaprasad G Bhat void spapr_add_nvdimm(DeviceState *dev, uint64_t slot, Error **errp) 90ee3a71e3SShivaprasad G Bhat { 91ee3a71e3SShivaprasad G Bhat SpaprDrc *drc; 92ee3a71e3SShivaprasad G Bhat bool hotplugged = spapr_drc_hotplugged(dev); 93ee3a71e3SShivaprasad G Bhat Error *local_err = NULL; 94ee3a71e3SShivaprasad G Bhat 95ee3a71e3SShivaprasad G Bhat drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot); 96ee3a71e3SShivaprasad G Bhat g_assert(drc); 97ee3a71e3SShivaprasad G Bhat 98ee3a71e3SShivaprasad G Bhat spapr_drc_attach(drc, dev, &local_err); 99ee3a71e3SShivaprasad G Bhat if (local_err) { 100ee3a71e3SShivaprasad G Bhat error_propagate(errp, local_err); 101ee3a71e3SShivaprasad G Bhat return; 102ee3a71e3SShivaprasad G Bhat } 103ee3a71e3SShivaprasad G Bhat 104ee3a71e3SShivaprasad G Bhat if (hotplugged) { 105ee3a71e3SShivaprasad G Bhat spapr_hotplug_req_add_by_index(drc); 106ee3a71e3SShivaprasad G Bhat } 107ee3a71e3SShivaprasad G Bhat } 108ee3a71e3SShivaprasad G Bhat 109ee3a71e3SShivaprasad G Bhat void spapr_create_nvdimm_dr_connectors(SpaprMachineState *spapr) 110ee3a71e3SShivaprasad G Bhat { 111ee3a71e3SShivaprasad G Bhat MachineState *machine = MACHINE(spapr); 112ee3a71e3SShivaprasad G Bhat int i; 113ee3a71e3SShivaprasad G Bhat 114ee3a71e3SShivaprasad G Bhat for (i = 0; i < machine->ram_slots; i++) { 115ee3a71e3SShivaprasad G Bhat spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_PMEM, i); 116ee3a71e3SShivaprasad G Bhat } 117ee3a71e3SShivaprasad G Bhat } 118ee3a71e3SShivaprasad G Bhat 119ee3a71e3SShivaprasad G Bhat 120*6ee1d62eSDaniel Henrique Barboza static int spapr_dt_nvdimm(void *fdt, int parent_offset, 121ee3a71e3SShivaprasad G Bhat NVDIMMDevice *nvdimm) 122ee3a71e3SShivaprasad G Bhat { 123ee3a71e3SShivaprasad G Bhat int child_offset; 124ee3a71e3SShivaprasad G Bhat char *buf; 125ee3a71e3SShivaprasad G Bhat SpaprDrc *drc; 126ee3a71e3SShivaprasad G Bhat uint32_t drc_idx; 127ee3a71e3SShivaprasad G Bhat uint32_t node = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_NODE_PROP, 128ee3a71e3SShivaprasad G Bhat &error_abort); 129ee3a71e3SShivaprasad G Bhat uint64_t slot = object_property_get_uint(OBJECT(nvdimm), PC_DIMM_SLOT_PROP, 130ee3a71e3SShivaprasad G Bhat &error_abort); 131ee3a71e3SShivaprasad G Bhat uint32_t associativity[] = { 132ee3a71e3SShivaprasad G Bhat cpu_to_be32(0x4), /* length */ 133ee3a71e3SShivaprasad G Bhat cpu_to_be32(0x0), cpu_to_be32(0x0), 134ee3a71e3SShivaprasad G Bhat cpu_to_be32(0x0), cpu_to_be32(node) 135ee3a71e3SShivaprasad G Bhat }; 136ee3a71e3SShivaprasad G Bhat uint64_t lsize = nvdimm->label_size; 137ee3a71e3SShivaprasad G Bhat uint64_t size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP, 138ee3a71e3SShivaprasad G Bhat NULL); 139ee3a71e3SShivaprasad G Bhat 140ee3a71e3SShivaprasad G Bhat drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PMEM, slot); 141ee3a71e3SShivaprasad G Bhat g_assert(drc); 142ee3a71e3SShivaprasad G Bhat 143ee3a71e3SShivaprasad G Bhat drc_idx = spapr_drc_index(drc); 144ee3a71e3SShivaprasad G Bhat 145ee3a71e3SShivaprasad G Bhat buf = g_strdup_printf("ibm,pmemory@%x", drc_idx); 146ee3a71e3SShivaprasad G Bhat child_offset = fdt_add_subnode(fdt, parent_offset, buf); 147ee3a71e3SShivaprasad G Bhat g_free(buf); 148ee3a71e3SShivaprasad G Bhat 149ee3a71e3SShivaprasad G Bhat _FDT(child_offset); 150ee3a71e3SShivaprasad G Bhat 151ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_cell(fdt, child_offset, "reg", drc_idx))); 152ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_string(fdt, child_offset, "compatible", "ibm,pmemory"))); 153ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_string(fdt, child_offset, "device_type", "ibm,pmemory"))); 154ee3a71e3SShivaprasad G Bhat 155ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop(fdt, child_offset, "ibm,associativity", associativity, 156ee3a71e3SShivaprasad G Bhat sizeof(associativity)))); 157ee3a71e3SShivaprasad G Bhat 158ee3a71e3SShivaprasad G Bhat buf = qemu_uuid_unparse_strdup(&nvdimm->uuid); 159ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_string(fdt, child_offset, "ibm,unit-guid", buf))); 160ee3a71e3SShivaprasad G Bhat g_free(buf); 161ee3a71e3SShivaprasad G Bhat 162ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,my-drc-index", drc_idx))); 163ee3a71e3SShivaprasad G Bhat 164ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,block-size", 165ee3a71e3SShivaprasad G Bhat SPAPR_MINIMUM_SCM_BLOCK_SIZE))); 166ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_u64(fdt, child_offset, "ibm,number-of-blocks", 167ee3a71e3SShivaprasad G Bhat size / SPAPR_MINIMUM_SCM_BLOCK_SIZE))); 168ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_cell(fdt, child_offset, "ibm,metadata-size", lsize))); 169ee3a71e3SShivaprasad G Bhat 170ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_string(fdt, child_offset, "ibm,pmem-application", 171ee3a71e3SShivaprasad G Bhat "operating-system"))); 172ee3a71e3SShivaprasad G Bhat _FDT(fdt_setprop(fdt, child_offset, "ibm,cache-flush-required", NULL, 0)); 173ee3a71e3SShivaprasad G Bhat 174ee3a71e3SShivaprasad G Bhat return child_offset; 175ee3a71e3SShivaprasad G Bhat } 176ee3a71e3SShivaprasad G Bhat 177*6ee1d62eSDaniel Henrique Barboza int spapr_pmem_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr, 178*6ee1d62eSDaniel Henrique Barboza void *fdt, int *fdt_start_offset, Error **errp) 179*6ee1d62eSDaniel Henrique Barboza { 180*6ee1d62eSDaniel Henrique Barboza NVDIMMDevice *nvdimm = NVDIMM(drc->dev); 181*6ee1d62eSDaniel Henrique Barboza 182*6ee1d62eSDaniel Henrique Barboza *fdt_start_offset = spapr_dt_nvdimm(fdt, 0, nvdimm); 183*6ee1d62eSDaniel Henrique Barboza 184*6ee1d62eSDaniel Henrique Barboza return 0; 185*6ee1d62eSDaniel Henrique Barboza } 186*6ee1d62eSDaniel Henrique Barboza 187ee3a71e3SShivaprasad G Bhat void spapr_dt_persistent_memory(void *fdt) 188ee3a71e3SShivaprasad G Bhat { 189ee3a71e3SShivaprasad G Bhat int offset = fdt_subnode_offset(fdt, 0, "persistent-memory"); 190ee3a71e3SShivaprasad G Bhat GSList *iter, *nvdimms = nvdimm_get_device_list(); 191ee3a71e3SShivaprasad G Bhat 192ee3a71e3SShivaprasad G Bhat if (offset < 0) { 193ee3a71e3SShivaprasad G Bhat offset = fdt_add_subnode(fdt, 0, "persistent-memory"); 194ee3a71e3SShivaprasad G Bhat _FDT(offset); 195ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0x1))); 196ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0x0))); 197ee3a71e3SShivaprasad G Bhat _FDT((fdt_setprop_string(fdt, offset, "device_type", 198ee3a71e3SShivaprasad G Bhat "ibm,persistent-memory"))); 199ee3a71e3SShivaprasad G Bhat } 200ee3a71e3SShivaprasad G Bhat 201ee3a71e3SShivaprasad G Bhat /* Create DT entries for cold plugged NVDIMM devices */ 202ee3a71e3SShivaprasad G Bhat for (iter = nvdimms; iter; iter = iter->next) { 203ee3a71e3SShivaprasad G Bhat NVDIMMDevice *nvdimm = iter->data; 204ee3a71e3SShivaprasad G Bhat 205ee3a71e3SShivaprasad G Bhat spapr_dt_nvdimm(fdt, offset, nvdimm); 206ee3a71e3SShivaprasad G Bhat } 207ee3a71e3SShivaprasad G Bhat g_slist_free(nvdimms); 208ee3a71e3SShivaprasad G Bhat 209ee3a71e3SShivaprasad G Bhat return; 210ee3a71e3SShivaprasad G Bhat } 211b5fca656SShivaprasad G Bhat 212b5fca656SShivaprasad G Bhat static target_ulong h_scm_read_metadata(PowerPCCPU *cpu, 213b5fca656SShivaprasad G Bhat SpaprMachineState *spapr, 214b5fca656SShivaprasad G Bhat target_ulong opcode, 215b5fca656SShivaprasad G Bhat target_ulong *args) 216b5fca656SShivaprasad G Bhat { 217b5fca656SShivaprasad G Bhat uint32_t drc_index = args[0]; 218b5fca656SShivaprasad G Bhat uint64_t offset = args[1]; 219b5fca656SShivaprasad G Bhat uint64_t len = args[2]; 220b5fca656SShivaprasad G Bhat SpaprDrc *drc = spapr_drc_by_index(drc_index); 221b5fca656SShivaprasad G Bhat NVDIMMDevice *nvdimm; 222b5fca656SShivaprasad G Bhat NVDIMMClass *ddc; 223b5fca656SShivaprasad G Bhat uint64_t data = 0; 224b5fca656SShivaprasad G Bhat uint8_t buf[8] = { 0 }; 225b5fca656SShivaprasad G Bhat 226b5fca656SShivaprasad G Bhat if (!drc || !drc->dev || 227b5fca656SShivaprasad G Bhat spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) { 228b5fca656SShivaprasad G Bhat return H_PARAMETER; 229b5fca656SShivaprasad G Bhat } 230b5fca656SShivaprasad G Bhat 231b5fca656SShivaprasad G Bhat if (len != 1 && len != 2 && 232b5fca656SShivaprasad G Bhat len != 4 && len != 8) { 233b5fca656SShivaprasad G Bhat return H_P3; 234b5fca656SShivaprasad G Bhat } 235b5fca656SShivaprasad G Bhat 236b5fca656SShivaprasad G Bhat nvdimm = NVDIMM(drc->dev); 237b5fca656SShivaprasad G Bhat if ((offset + len < offset) || 238b5fca656SShivaprasad G Bhat (nvdimm->label_size < len + offset)) { 239b5fca656SShivaprasad G Bhat return H_P2; 240b5fca656SShivaprasad G Bhat } 241b5fca656SShivaprasad G Bhat 242b5fca656SShivaprasad G Bhat ddc = NVDIMM_GET_CLASS(nvdimm); 243b5fca656SShivaprasad G Bhat ddc->read_label_data(nvdimm, buf, len, offset); 244b5fca656SShivaprasad G Bhat 245b5fca656SShivaprasad G Bhat switch (len) { 246b5fca656SShivaprasad G Bhat case 1: 247b5fca656SShivaprasad G Bhat data = ldub_p(buf); 248b5fca656SShivaprasad G Bhat break; 249b5fca656SShivaprasad G Bhat case 2: 250b5fca656SShivaprasad G Bhat data = lduw_be_p(buf); 251b5fca656SShivaprasad G Bhat break; 252b5fca656SShivaprasad G Bhat case 4: 253b5fca656SShivaprasad G Bhat data = ldl_be_p(buf); 254b5fca656SShivaprasad G Bhat break; 255b5fca656SShivaprasad G Bhat case 8: 256b5fca656SShivaprasad G Bhat data = ldq_be_p(buf); 257b5fca656SShivaprasad G Bhat break; 258b5fca656SShivaprasad G Bhat default: 259b5fca656SShivaprasad G Bhat g_assert_not_reached(); 260b5fca656SShivaprasad G Bhat } 261b5fca656SShivaprasad G Bhat 262b5fca656SShivaprasad G Bhat args[0] = data; 263b5fca656SShivaprasad G Bhat 264b5fca656SShivaprasad G Bhat return H_SUCCESS; 265b5fca656SShivaprasad G Bhat } 266b5fca656SShivaprasad G Bhat 267b5fca656SShivaprasad G Bhat static target_ulong h_scm_write_metadata(PowerPCCPU *cpu, 268b5fca656SShivaprasad G Bhat SpaprMachineState *spapr, 269b5fca656SShivaprasad G Bhat target_ulong opcode, 270b5fca656SShivaprasad G Bhat target_ulong *args) 271b5fca656SShivaprasad G Bhat { 272b5fca656SShivaprasad G Bhat uint32_t drc_index = args[0]; 273b5fca656SShivaprasad G Bhat uint64_t offset = args[1]; 274b5fca656SShivaprasad G Bhat uint64_t data = args[2]; 275b5fca656SShivaprasad G Bhat uint64_t len = args[3]; 276b5fca656SShivaprasad G Bhat SpaprDrc *drc = spapr_drc_by_index(drc_index); 277b5fca656SShivaprasad G Bhat NVDIMMDevice *nvdimm; 278b5fca656SShivaprasad G Bhat NVDIMMClass *ddc; 279b5fca656SShivaprasad G Bhat uint8_t buf[8] = { 0 }; 280b5fca656SShivaprasad G Bhat 281b5fca656SShivaprasad G Bhat if (!drc || !drc->dev || 282b5fca656SShivaprasad G Bhat spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) { 283b5fca656SShivaprasad G Bhat return H_PARAMETER; 284b5fca656SShivaprasad G Bhat } 285b5fca656SShivaprasad G Bhat 286b5fca656SShivaprasad G Bhat if (len != 1 && len != 2 && 287b5fca656SShivaprasad G Bhat len != 4 && len != 8) { 288b5fca656SShivaprasad G Bhat return H_P4; 289b5fca656SShivaprasad G Bhat } 290b5fca656SShivaprasad G Bhat 291b5fca656SShivaprasad G Bhat nvdimm = NVDIMM(drc->dev); 292b5fca656SShivaprasad G Bhat if ((offset + len < offset) || 293b5fca656SShivaprasad G Bhat (nvdimm->label_size < len + offset)) { 294b5fca656SShivaprasad G Bhat return H_P2; 295b5fca656SShivaprasad G Bhat } 296b5fca656SShivaprasad G Bhat 297b5fca656SShivaprasad G Bhat switch (len) { 298b5fca656SShivaprasad G Bhat case 1: 299b5fca656SShivaprasad G Bhat if (data & 0xffffffffffffff00) { 300b5fca656SShivaprasad G Bhat return H_P2; 301b5fca656SShivaprasad G Bhat } 302b5fca656SShivaprasad G Bhat stb_p(buf, data); 303b5fca656SShivaprasad G Bhat break; 304b5fca656SShivaprasad G Bhat case 2: 305b5fca656SShivaprasad G Bhat if (data & 0xffffffffffff0000) { 306b5fca656SShivaprasad G Bhat return H_P2; 307b5fca656SShivaprasad G Bhat } 308b5fca656SShivaprasad G Bhat stw_be_p(buf, data); 309b5fca656SShivaprasad G Bhat break; 310b5fca656SShivaprasad G Bhat case 4: 311b5fca656SShivaprasad G Bhat if (data & 0xffffffff00000000) { 312b5fca656SShivaprasad G Bhat return H_P2; 313b5fca656SShivaprasad G Bhat } 314b5fca656SShivaprasad G Bhat stl_be_p(buf, data); 315b5fca656SShivaprasad G Bhat break; 316b5fca656SShivaprasad G Bhat case 8: 317b5fca656SShivaprasad G Bhat stq_be_p(buf, data); 318b5fca656SShivaprasad G Bhat break; 319b5fca656SShivaprasad G Bhat default: 320b5fca656SShivaprasad G Bhat g_assert_not_reached(); 321b5fca656SShivaprasad G Bhat } 322b5fca656SShivaprasad G Bhat 323b5fca656SShivaprasad G Bhat ddc = NVDIMM_GET_CLASS(nvdimm); 324b5fca656SShivaprasad G Bhat ddc->write_label_data(nvdimm, buf, len, offset); 325b5fca656SShivaprasad G Bhat 326b5fca656SShivaprasad G Bhat return H_SUCCESS; 327b5fca656SShivaprasad G Bhat } 328b5fca656SShivaprasad G Bhat 329b5fca656SShivaprasad G Bhat static target_ulong h_scm_bind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr, 330b5fca656SShivaprasad G Bhat target_ulong opcode, target_ulong *args) 331b5fca656SShivaprasad G Bhat { 332b5fca656SShivaprasad G Bhat uint32_t drc_index = args[0]; 333b5fca656SShivaprasad G Bhat uint64_t starting_idx = args[1]; 334b5fca656SShivaprasad G Bhat uint64_t no_of_scm_blocks_to_bind = args[2]; 335b5fca656SShivaprasad G Bhat uint64_t target_logical_mem_addr = args[3]; 336b5fca656SShivaprasad G Bhat uint64_t continue_token = args[4]; 337b5fca656SShivaprasad G Bhat uint64_t size; 338b5fca656SShivaprasad G Bhat uint64_t total_no_of_scm_blocks; 339b5fca656SShivaprasad G Bhat SpaprDrc *drc = spapr_drc_by_index(drc_index); 340b5fca656SShivaprasad G Bhat hwaddr addr; 341b5fca656SShivaprasad G Bhat NVDIMMDevice *nvdimm; 342b5fca656SShivaprasad G Bhat 343b5fca656SShivaprasad G Bhat if (!drc || !drc->dev || 344b5fca656SShivaprasad G Bhat spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) { 345b5fca656SShivaprasad G Bhat return H_PARAMETER; 346b5fca656SShivaprasad G Bhat } 347b5fca656SShivaprasad G Bhat 348b5fca656SShivaprasad G Bhat /* 349b5fca656SShivaprasad G Bhat * Currently continue token should be zero qemu has already bound 350b5fca656SShivaprasad G Bhat * everything and this hcall doesnt return H_BUSY. 351b5fca656SShivaprasad G Bhat */ 352b5fca656SShivaprasad G Bhat if (continue_token > 0) { 353b5fca656SShivaprasad G Bhat return H_P5; 354b5fca656SShivaprasad G Bhat } 355b5fca656SShivaprasad G Bhat 356b5fca656SShivaprasad G Bhat /* Currently qemu assigns the address. */ 357b5fca656SShivaprasad G Bhat if (target_logical_mem_addr != 0xffffffffffffffff) { 358b5fca656SShivaprasad G Bhat return H_OVERLAP; 359b5fca656SShivaprasad G Bhat } 360b5fca656SShivaprasad G Bhat 361b5fca656SShivaprasad G Bhat nvdimm = NVDIMM(drc->dev); 362b5fca656SShivaprasad G Bhat 363b5fca656SShivaprasad G Bhat size = object_property_get_uint(OBJECT(nvdimm), 364b5fca656SShivaprasad G Bhat PC_DIMM_SIZE_PROP, &error_abort); 365b5fca656SShivaprasad G Bhat 366b5fca656SShivaprasad G Bhat total_no_of_scm_blocks = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE; 367b5fca656SShivaprasad G Bhat 368b5fca656SShivaprasad G Bhat if (starting_idx > total_no_of_scm_blocks) { 369b5fca656SShivaprasad G Bhat return H_P2; 370b5fca656SShivaprasad G Bhat } 371b5fca656SShivaprasad G Bhat 372b5fca656SShivaprasad G Bhat if (((starting_idx + no_of_scm_blocks_to_bind) < starting_idx) || 373b5fca656SShivaprasad G Bhat ((starting_idx + no_of_scm_blocks_to_bind) > total_no_of_scm_blocks)) { 374b5fca656SShivaprasad G Bhat return H_P3; 375b5fca656SShivaprasad G Bhat } 376b5fca656SShivaprasad G Bhat 377b5fca656SShivaprasad G Bhat addr = object_property_get_uint(OBJECT(nvdimm), 378b5fca656SShivaprasad G Bhat PC_DIMM_ADDR_PROP, &error_abort); 379b5fca656SShivaprasad G Bhat 380b5fca656SShivaprasad G Bhat addr += starting_idx * SPAPR_MINIMUM_SCM_BLOCK_SIZE; 381b5fca656SShivaprasad G Bhat 382b5fca656SShivaprasad G Bhat /* Already bound, Return target logical address in R5 */ 383b5fca656SShivaprasad G Bhat args[1] = addr; 384b5fca656SShivaprasad G Bhat args[2] = no_of_scm_blocks_to_bind; 385b5fca656SShivaprasad G Bhat 386b5fca656SShivaprasad G Bhat return H_SUCCESS; 387b5fca656SShivaprasad G Bhat } 388b5fca656SShivaprasad G Bhat 389b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_mem(PowerPCCPU *cpu, SpaprMachineState *spapr, 390b5fca656SShivaprasad G Bhat target_ulong opcode, target_ulong *args) 391b5fca656SShivaprasad G Bhat { 392b5fca656SShivaprasad G Bhat uint32_t drc_index = args[0]; 393b5fca656SShivaprasad G Bhat uint64_t starting_scm_logical_addr = args[1]; 394b5fca656SShivaprasad G Bhat uint64_t no_of_scm_blocks_to_unbind = args[2]; 395b5fca656SShivaprasad G Bhat uint64_t continue_token = args[3]; 396b5fca656SShivaprasad G Bhat uint64_t size_to_unbind; 397b5fca656SShivaprasad G Bhat Range blockrange = range_empty; 398b5fca656SShivaprasad G Bhat Range nvdimmrange = range_empty; 399b5fca656SShivaprasad G Bhat SpaprDrc *drc = spapr_drc_by_index(drc_index); 400b5fca656SShivaprasad G Bhat NVDIMMDevice *nvdimm; 401b5fca656SShivaprasad G Bhat uint64_t size, addr; 402b5fca656SShivaprasad G Bhat 403b5fca656SShivaprasad G Bhat if (!drc || !drc->dev || 404b5fca656SShivaprasad G Bhat spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) { 405b5fca656SShivaprasad G Bhat return H_PARAMETER; 406b5fca656SShivaprasad G Bhat } 407b5fca656SShivaprasad G Bhat 408b5fca656SShivaprasad G Bhat /* continue_token should be zero as this hcall doesn't return H_BUSY. */ 409b5fca656SShivaprasad G Bhat if (continue_token > 0) { 410b5fca656SShivaprasad G Bhat return H_P4; 411b5fca656SShivaprasad G Bhat } 412b5fca656SShivaprasad G Bhat 413b5fca656SShivaprasad G Bhat /* Check if starting_scm_logical_addr is block aligned */ 414b5fca656SShivaprasad G Bhat if (!QEMU_IS_ALIGNED(starting_scm_logical_addr, 415b5fca656SShivaprasad G Bhat SPAPR_MINIMUM_SCM_BLOCK_SIZE)) { 416b5fca656SShivaprasad G Bhat return H_P2; 417b5fca656SShivaprasad G Bhat } 418b5fca656SShivaprasad G Bhat 419b5fca656SShivaprasad G Bhat size_to_unbind = no_of_scm_blocks_to_unbind * SPAPR_MINIMUM_SCM_BLOCK_SIZE; 420b5fca656SShivaprasad G Bhat if (no_of_scm_blocks_to_unbind == 0 || no_of_scm_blocks_to_unbind != 421b5fca656SShivaprasad G Bhat size_to_unbind / SPAPR_MINIMUM_SCM_BLOCK_SIZE) { 422b5fca656SShivaprasad G Bhat return H_P3; 423b5fca656SShivaprasad G Bhat } 424b5fca656SShivaprasad G Bhat 425b5fca656SShivaprasad G Bhat nvdimm = NVDIMM(drc->dev); 426b5fca656SShivaprasad G Bhat size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP, 427b5fca656SShivaprasad G Bhat &error_abort); 428b5fca656SShivaprasad G Bhat addr = object_property_get_int(OBJECT(nvdimm), PC_DIMM_ADDR_PROP, 429b5fca656SShivaprasad G Bhat &error_abort); 430b5fca656SShivaprasad G Bhat 431b5fca656SShivaprasad G Bhat range_init_nofail(&nvdimmrange, addr, size); 432b5fca656SShivaprasad G Bhat range_init_nofail(&blockrange, starting_scm_logical_addr, size_to_unbind); 433b5fca656SShivaprasad G Bhat 434b5fca656SShivaprasad G Bhat if (!range_contains_range(&nvdimmrange, &blockrange)) { 435b5fca656SShivaprasad G Bhat return H_P3; 436b5fca656SShivaprasad G Bhat } 437b5fca656SShivaprasad G Bhat 438b5fca656SShivaprasad G Bhat args[1] = no_of_scm_blocks_to_unbind; 439b5fca656SShivaprasad G Bhat 440b5fca656SShivaprasad G Bhat /* let unplug take care of actual unbind */ 441b5fca656SShivaprasad G Bhat return H_SUCCESS; 442b5fca656SShivaprasad G Bhat } 443b5fca656SShivaprasad G Bhat 444b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_ALL 0x1 445b5fca656SShivaprasad G Bhat #define H_UNBIND_SCOPE_DRC 0x2 446b5fca656SShivaprasad G Bhat 447b5fca656SShivaprasad G Bhat static target_ulong h_scm_unbind_all(PowerPCCPU *cpu, SpaprMachineState *spapr, 448b5fca656SShivaprasad G Bhat target_ulong opcode, target_ulong *args) 449b5fca656SShivaprasad G Bhat { 450b5fca656SShivaprasad G Bhat uint64_t target_scope = args[0]; 451b5fca656SShivaprasad G Bhat uint32_t drc_index = args[1]; 452b5fca656SShivaprasad G Bhat uint64_t continue_token = args[2]; 453b5fca656SShivaprasad G Bhat NVDIMMDevice *nvdimm; 454b5fca656SShivaprasad G Bhat uint64_t size; 455b5fca656SShivaprasad G Bhat uint64_t no_of_scm_blocks_unbound = 0; 456b5fca656SShivaprasad G Bhat 457b5fca656SShivaprasad G Bhat /* continue_token should be zero as this hcall doesn't return H_BUSY. */ 458b5fca656SShivaprasad G Bhat if (continue_token > 0) { 459b5fca656SShivaprasad G Bhat return H_P4; 460b5fca656SShivaprasad G Bhat } 461b5fca656SShivaprasad G Bhat 462b5fca656SShivaprasad G Bhat if (target_scope == H_UNBIND_SCOPE_DRC) { 463b5fca656SShivaprasad G Bhat SpaprDrc *drc = spapr_drc_by_index(drc_index); 464b5fca656SShivaprasad G Bhat 465b5fca656SShivaprasad G Bhat if (!drc || !drc->dev || 466b5fca656SShivaprasad G Bhat spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PMEM) { 467b5fca656SShivaprasad G Bhat return H_P2; 468b5fca656SShivaprasad G Bhat } 469b5fca656SShivaprasad G Bhat 470b5fca656SShivaprasad G Bhat nvdimm = NVDIMM(drc->dev); 471b5fca656SShivaprasad G Bhat size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP, 472b5fca656SShivaprasad G Bhat &error_abort); 473b5fca656SShivaprasad G Bhat 474b5fca656SShivaprasad G Bhat no_of_scm_blocks_unbound = size / SPAPR_MINIMUM_SCM_BLOCK_SIZE; 475b5fca656SShivaprasad G Bhat } else if (target_scope == H_UNBIND_SCOPE_ALL) { 476b5fca656SShivaprasad G Bhat GSList *list, *nvdimms; 477b5fca656SShivaprasad G Bhat 478b5fca656SShivaprasad G Bhat nvdimms = nvdimm_get_device_list(); 479b5fca656SShivaprasad G Bhat for (list = nvdimms; list; list = list->next) { 480b5fca656SShivaprasad G Bhat nvdimm = list->data; 481b5fca656SShivaprasad G Bhat size = object_property_get_int(OBJECT(nvdimm), PC_DIMM_SIZE_PROP, 482b5fca656SShivaprasad G Bhat &error_abort); 483b5fca656SShivaprasad G Bhat 484b5fca656SShivaprasad G Bhat no_of_scm_blocks_unbound += size / SPAPR_MINIMUM_SCM_BLOCK_SIZE; 485b5fca656SShivaprasad G Bhat } 486b5fca656SShivaprasad G Bhat g_slist_free(nvdimms); 487b5fca656SShivaprasad G Bhat } else { 488b5fca656SShivaprasad G Bhat return H_PARAMETER; 489b5fca656SShivaprasad G Bhat } 490b5fca656SShivaprasad G Bhat 491b5fca656SShivaprasad G Bhat args[1] = no_of_scm_blocks_unbound; 492b5fca656SShivaprasad G Bhat 493b5fca656SShivaprasad G Bhat /* let unplug take care of actual unbind */ 494b5fca656SShivaprasad G Bhat return H_SUCCESS; 495b5fca656SShivaprasad G Bhat } 496b5fca656SShivaprasad G Bhat 497b5fca656SShivaprasad G Bhat static void spapr_scm_register_types(void) 498b5fca656SShivaprasad G Bhat { 499b5fca656SShivaprasad G Bhat /* qemu/scm specific hcalls */ 500b5fca656SShivaprasad G Bhat spapr_register_hypercall(H_SCM_READ_METADATA, h_scm_read_metadata); 501b5fca656SShivaprasad G Bhat spapr_register_hypercall(H_SCM_WRITE_METADATA, h_scm_write_metadata); 502b5fca656SShivaprasad G Bhat spapr_register_hypercall(H_SCM_BIND_MEM, h_scm_bind_mem); 503b5fca656SShivaprasad G Bhat spapr_register_hypercall(H_SCM_UNBIND_MEM, h_scm_unbind_mem); 504b5fca656SShivaprasad G Bhat spapr_register_hypercall(H_SCM_UNBIND_ALL, h_scm_unbind_all); 505b5fca656SShivaprasad G Bhat } 506b5fca656SShivaprasad G Bhat 507b5fca656SShivaprasad G Bhat type_init(spapr_scm_register_types) 508