qemu/hw/mem/nvdimm.c
<<
>>
Prefs
   1/*
   2 * Non-Volatile Dual In-line Memory Module Virtualization Implementation
   3 *
   4 * Copyright(C) 2015 Intel Corporation.
   5 *
   6 * Author:
   7 *  Xiao Guangrong <guangrong.xiao@linux.intel.com>
   8 *
   9 * Currently, it only supports PMEM Virtualization.
  10 *
  11 * This library is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU Lesser General Public
  13 * License as published by the Free Software Foundation; either
  14 * version 2 of the License, or (at your option) any later version.
  15 *
  16 * This library is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19 * Lesser General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU Lesser General Public
  22 * License along with this library; if not, see <http://www.gnu.org/licenses/>
  23 */
  24
  25#include "qemu/osdep.h"
  26#include "qemu/pmem.h"
  27#include "qapi/error.h"
  28#include "qapi/visitor.h"
  29#include "hw/mem/nvdimm.h"
  30#include "hw/mem/memory-device.h"
  31
  32static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *name,
  33                                  void *opaque, Error **errp)
  34{
  35    NVDIMMDevice *nvdimm = NVDIMM(obj);
  36    uint64_t value = nvdimm->label_size;
  37
  38    visit_type_size(v, name, &value, errp);
  39}
  40
  41static void nvdimm_set_label_size(Object *obj, Visitor *v, const char *name,
  42                                  void *opaque, Error **errp)
  43{
  44    NVDIMMDevice *nvdimm = NVDIMM(obj);
  45    Error *local_err = NULL;
  46    uint64_t value;
  47
  48    if (nvdimm->nvdimm_mr) {
  49        error_setg(&local_err, "cannot change property value");
  50        goto out;
  51    }
  52
  53    visit_type_size(v, name, &value, &local_err);
  54    if (local_err) {
  55        goto out;
  56    }
  57    if (value < MIN_NAMESPACE_LABEL_SIZE) {
  58        error_setg(&local_err, "Property '%s.%s' (0x%" PRIx64 ") is required"
  59                   " at least 0x%lx", object_get_typename(obj),
  60                   name, value, MIN_NAMESPACE_LABEL_SIZE);
  61        goto out;
  62    }
  63
  64    nvdimm->label_size = value;
  65out:
  66    error_propagate(errp, local_err);
  67}
  68
  69static void nvdimm_init(Object *obj)
  70{
  71    object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int",
  72                        nvdimm_get_label_size, nvdimm_set_label_size, NULL,
  73                        NULL, NULL);
  74}
  75
  76static void nvdimm_finalize(Object *obj)
  77{
  78    NVDIMMDevice *nvdimm = NVDIMM(obj);
  79
  80    g_free(nvdimm->nvdimm_mr);
  81}
  82
  83static void nvdimm_prepare_memory_region(NVDIMMDevice *nvdimm, Error **errp)
  84{
  85    PCDIMMDevice *dimm = PC_DIMM(nvdimm);
  86    uint64_t align, pmem_size, size;
  87    MemoryRegion *mr;
  88
  89    g_assert(!nvdimm->nvdimm_mr);
  90
  91    if (!dimm->hostmem) {
  92        error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
  93        return;
  94    }
  95
  96    mr = host_memory_backend_get_memory(dimm->hostmem);
  97    align = memory_region_get_alignment(mr);
  98    size = memory_region_size(mr);
  99
 100    pmem_size = size - nvdimm->label_size;
 101    nvdimm->label_data = memory_region_get_ram_ptr(mr) + pmem_size;
 102    pmem_size = QEMU_ALIGN_DOWN(pmem_size, align);
 103
 104    if (size <= nvdimm->label_size || !pmem_size) {
 105        HostMemoryBackend *hostmem = dimm->hostmem;
 106        char *path = object_get_canonical_path_component(OBJECT(hostmem));
 107
 108        error_setg(errp, "the size of memdev %s (0x%" PRIx64 ") is too "
 109                   "small to contain nvdimm label (0x%" PRIx64 ") and "
 110                   "aligned PMEM (0x%" PRIx64 ")",
 111                   path, memory_region_size(mr), nvdimm->label_size, align);
 112        g_free(path);
 113        return;
 114    }
 115
 116    nvdimm->nvdimm_mr = g_new(MemoryRegion, 1);
 117    memory_region_init_alias(nvdimm->nvdimm_mr, OBJECT(dimm),
 118                             "nvdimm-memory", mr, 0, pmem_size);
 119    memory_region_set_nonvolatile(nvdimm->nvdimm_mr, true);
 120    nvdimm->nvdimm_mr->align = align;
 121}
 122
 123static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
 124                                                 Error **errp)
 125{
 126    NVDIMMDevice *nvdimm = NVDIMM(md);
 127    Error *local_err = NULL;
 128
 129    if (!nvdimm->nvdimm_mr) {
 130        nvdimm_prepare_memory_region(nvdimm, &local_err);
 131        if (local_err) {
 132            error_propagate(errp, local_err);
 133            return NULL;
 134        }
 135    }
 136    return nvdimm->nvdimm_mr;
 137}
 138
 139static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp)
 140{
 141    NVDIMMDevice *nvdimm = NVDIMM(dimm);
 142
 143    if (!nvdimm->nvdimm_mr) {
 144        nvdimm_prepare_memory_region(nvdimm, errp);
 145    }
 146}
 147
 148/*
 149 * the caller should check the input parameters before calling
 150 * label read/write functions.
 151 */
 152static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size,
 153                                        uint64_t offset)
 154{
 155    assert((nvdimm->label_size >= size + offset) && (offset + size > offset));
 156}
 157
 158static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf,
 159                                   uint64_t size, uint64_t offset)
 160{
 161    nvdimm_validate_rw_label_data(nvdimm, size, offset);
 162
 163    memcpy(buf, nvdimm->label_data + offset, size);
 164}
 165
 166static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf,
 167                                    uint64_t size, uint64_t offset)
 168{
 169    MemoryRegion *mr;
 170    PCDIMMDevice *dimm = PC_DIMM(nvdimm);
 171    bool is_pmem = object_property_get_bool(OBJECT(dimm->hostmem),
 172                                            "pmem", NULL);
 173    uint64_t backend_offset;
 174
 175    nvdimm_validate_rw_label_data(nvdimm, size, offset);
 176
 177    if (!is_pmem) {
 178        memcpy(nvdimm->label_data + offset, buf, size);
 179    } else {
 180        pmem_memcpy_persist(nvdimm->label_data + offset, buf, size);
 181    }
 182
 183    mr = host_memory_backend_get_memory(dimm->hostmem);
 184    backend_offset = memory_region_size(mr) - nvdimm->label_size + offset;
 185    memory_region_set_dirty(mr, backend_offset, size);
 186}
 187
 188static Property nvdimm_properties[] = {
 189    DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP, NVDIMMDevice, unarmed, false),
 190    DEFINE_PROP_END_OF_LIST(),
 191};
 192
 193static void nvdimm_class_init(ObjectClass *oc, void *data)
 194{
 195    PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
 196    MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
 197    NVDIMMClass *nvc = NVDIMM_CLASS(oc);
 198    DeviceClass *dc = DEVICE_CLASS(oc);
 199
 200    ddc->realize = nvdimm_realize;
 201    mdc->get_memory_region = nvdimm_md_get_memory_region;
 202    dc->props = nvdimm_properties;
 203
 204    nvc->read_label_data = nvdimm_read_label_data;
 205    nvc->write_label_data = nvdimm_write_label_data;
 206}
 207
 208static TypeInfo nvdimm_info = {
 209    .name          = TYPE_NVDIMM,
 210    .parent        = TYPE_PC_DIMM,
 211    .class_size    = sizeof(NVDIMMClass),
 212    .class_init    = nvdimm_class_init,
 213    .instance_size = sizeof(NVDIMMDevice),
 214    .instance_init = nvdimm_init,
 215    .instance_finalize = nvdimm_finalize,
 216};
 217
 218static void nvdimm_register_types(void)
 219{
 220    type_register_static(&nvdimm_info);
 221}
 222
 223type_init(nvdimm_register_types)
 224