2012-11-12 20:46:57 +04:00
|
|
|
/*
|
|
|
|
* QEMU sPAPR NVRAM emulation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 David Gibson, IBM Corporation.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2012-12-20 10:50:41 +04:00
|
|
|
|
2016-01-26 21:16:58 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2016-01-19 23:51:44 +03:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "cpu.h"
|
2012-11-12 20:46:57 +04:00
|
|
|
#include <libfdt.h>
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/device_tree.h"
|
2012-11-12 20:46:57 +04:00
|
|
|
#include "hw/sysbus.h"
|
2016-10-26 10:35:40 +03:00
|
|
|
#include "hw/nvram/chrp_nvram.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/ppc/spapr.h"
|
|
|
|
#include "hw/ppc/spapr_vio.h"
|
2012-11-12 20:46:57 +04:00
|
|
|
|
|
|
|
typedef struct sPAPRNVRAM {
|
|
|
|
VIOsPAPRDevice sdev;
|
|
|
|
uint32_t size;
|
|
|
|
uint8_t *buf;
|
2014-10-07 15:59:18 +04:00
|
|
|
BlockBackend *blk;
|
2016-07-21 15:05:46 +03:00
|
|
|
VMChangeStateEntry *vmstate;
|
2012-11-12 20:46:57 +04:00
|
|
|
} sPAPRNVRAM;
|
|
|
|
|
2013-04-07 23:08:16 +04:00
|
|
|
#define TYPE_VIO_SPAPR_NVRAM "spapr-nvram"
|
|
|
|
#define VIO_SPAPR_NVRAM(obj) \
|
|
|
|
OBJECT_CHECK(sPAPRNVRAM, (obj), TYPE_VIO_SPAPR_NVRAM)
|
|
|
|
|
2012-11-12 20:46:57 +04:00
|
|
|
#define MIN_NVRAM_SIZE 8192
|
2012-12-03 20:42:16 +04:00
|
|
|
#define DEFAULT_NVRAM_SIZE 65536
|
2014-04-04 11:26:26 +04:00
|
|
|
#define MAX_NVRAM_SIZE 1048576
|
2012-11-12 20:46:57 +04:00
|
|
|
|
2015-07-02 09:23:04 +03:00
|
|
|
static void rtas_nvram_fetch(PowerPCCPU *cpu, sPAPRMachineState *spapr,
|
2012-11-12 20:46:57 +04:00
|
|
|
uint32_t token, uint32_t nargs,
|
|
|
|
target_ulong args,
|
|
|
|
uint32_t nret, target_ulong rets)
|
|
|
|
{
|
|
|
|
sPAPRNVRAM *nvram = spapr->nvram;
|
|
|
|
hwaddr offset, buffer, len;
|
|
|
|
void *membuf;
|
|
|
|
|
|
|
|
if ((nargs != 3) || (nret != 2)) {
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
|
2012-11-12 20:46:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nvram) {
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
|
2012-11-12 20:46:57 +04:00
|
|
|
rtas_st(rets, 1, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = rtas_ld(args, 0);
|
|
|
|
buffer = rtas_ld(args, 1);
|
|
|
|
len = rtas_ld(args, 2);
|
|
|
|
|
|
|
|
if (((offset + len) < offset)
|
|
|
|
|| ((offset + len) > nvram->size)) {
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
|
2012-11-12 20:46:57 +04:00
|
|
|
rtas_st(rets, 1, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
assert(nvram->buf);
|
2012-11-12 20:46:57 +04:00
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
membuf = cpu_physical_memory_map(buffer, &len, 1);
|
|
|
|
memcpy(membuf, nvram->buf + offset, len);
|
2012-11-12 20:46:57 +04:00
|
|
|
cpu_physical_memory_unmap(membuf, len, 1, len);
|
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_SUCCESS);
|
|
|
|
rtas_st(rets, 1, len);
|
2012-11-12 20:46:57 +04:00
|
|
|
}
|
|
|
|
|
2015-07-02 09:23:04 +03:00
|
|
|
static void rtas_nvram_store(PowerPCCPU *cpu, sPAPRMachineState *spapr,
|
2012-11-12 20:46:57 +04:00
|
|
|
uint32_t token, uint32_t nargs,
|
|
|
|
target_ulong args,
|
|
|
|
uint32_t nret, target_ulong rets)
|
|
|
|
{
|
|
|
|
sPAPRNVRAM *nvram = spapr->nvram;
|
|
|
|
hwaddr offset, buffer, len;
|
|
|
|
int alen;
|
|
|
|
void *membuf;
|
|
|
|
|
|
|
|
if ((nargs != 3) || (nret != 2)) {
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
|
2012-11-12 20:46:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nvram) {
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
|
2012-11-12 20:46:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = rtas_ld(args, 0);
|
|
|
|
buffer = rtas_ld(args, 1);
|
|
|
|
len = rtas_ld(args, 2);
|
|
|
|
|
|
|
|
if (((offset + len) < offset)
|
|
|
|
|| ((offset + len) > nvram->size)) {
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
|
2012-11-12 20:46:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
membuf = cpu_physical_memory_map(buffer, &len, 0);
|
2014-10-02 13:56:03 +04:00
|
|
|
|
|
|
|
alen = len;
|
2014-10-07 15:59:18 +04:00
|
|
|
if (nvram->blk) {
|
2016-05-06 19:26:27 +03:00
|
|
|
alen = blk_pwrite(nvram->blk, offset, membuf, len, 0);
|
2012-11-12 20:46:57 +04:00
|
|
|
}
|
2014-10-02 13:56:03 +04:00
|
|
|
|
|
|
|
assert(nvram->buf);
|
|
|
|
memcpy(nvram->buf + offset, membuf, len);
|
|
|
|
|
2012-11-12 20:46:57 +04:00
|
|
|
cpu_physical_memory_unmap(membuf, len, 0, len);
|
|
|
|
|
2013-11-19 08:28:54 +04:00
|
|
|
rtas_st(rets, 0, (alen < len) ? RTAS_OUT_HW_ERROR : RTAS_OUT_SUCCESS);
|
2012-11-12 20:46:57 +04:00
|
|
|
rtas_st(rets, 1, (alen < 0) ? 0 : alen);
|
|
|
|
}
|
|
|
|
|
2015-02-27 13:52:17 +03:00
|
|
|
static void spapr_nvram_realize(VIOsPAPRDevice *dev, Error **errp)
|
2012-11-12 20:46:57 +04:00
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
sPAPRNVRAM *nvram = VIO_SPAPR_NVRAM(dev);
|
2012-11-12 20:46:57 +04:00
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
if (nvram->blk) {
|
|
|
|
nvram->size = blk_getlength(nvram->blk);
|
2012-11-12 20:46:57 +04:00
|
|
|
} else {
|
|
|
|
nvram->size = DEFAULT_NVRAM_SIZE;
|
|
|
|
}
|
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
nvram->buf = g_malloc0(nvram->size);
|
|
|
|
|
2012-11-12 20:46:57 +04:00
|
|
|
if ((nvram->size < MIN_NVRAM_SIZE) || (nvram->size > MAX_NVRAM_SIZE)) {
|
2015-02-27 13:52:17 +03:00
|
|
|
error_setg(errp, "spapr-nvram must be between %d and %d bytes in size",
|
|
|
|
MIN_NVRAM_SIZE, MAX_NVRAM_SIZE);
|
|
|
|
return;
|
2012-11-12 20:46:57 +04:00
|
|
|
}
|
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
if (nvram->blk) {
|
|
|
|
int alen = blk_pread(nvram->blk, 0, nvram->buf, nvram->size);
|
|
|
|
|
|
|
|
if (alen != nvram->size) {
|
2015-02-27 13:52:17 +03:00
|
|
|
error_setg(errp, "can't read spapr-nvram contents");
|
|
|
|
return;
|
2014-10-02 13:56:03 +04:00
|
|
|
}
|
2016-10-26 10:35:40 +03:00
|
|
|
} else if (nb_prom_envs > 0) {
|
|
|
|
/* Create a system partition to pass the -prom-env variables */
|
|
|
|
chrp_nvram_create_system_partition(nvram->buf, MIN_NVRAM_SIZE / 4);
|
|
|
|
chrp_nvram_create_free_partition(&nvram->buf[MIN_NVRAM_SIZE / 4],
|
|
|
|
nvram->size - MIN_NVRAM_SIZE / 4);
|
2014-10-02 13:56:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 17:26:32 +04:00
|
|
|
spapr_rtas_register(RTAS_NVRAM_FETCH, "nvram-fetch", rtas_nvram_fetch);
|
|
|
|
spapr_rtas_register(RTAS_NVRAM_STORE, "nvram-store", rtas_nvram_store);
|
2012-11-12 20:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int spapr_nvram_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
|
|
|
|
{
|
2013-04-07 23:08:16 +04:00
|
|
|
sPAPRNVRAM *nvram = VIO_SPAPR_NVRAM(dev);
|
2012-11-12 20:46:57 +04:00
|
|
|
|
|
|
|
return fdt_setprop_cell(fdt, node_off, "#bytes", nvram->size);
|
|
|
|
}
|
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
static int spapr_nvram_pre_load(void *opaque)
|
|
|
|
{
|
|
|
|
sPAPRNVRAM *nvram = VIO_SPAPR_NVRAM(opaque);
|
|
|
|
|
|
|
|
g_free(nvram->buf);
|
|
|
|
nvram->buf = NULL;
|
|
|
|
nvram->size = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-21 15:05:46 +03:00
|
|
|
static void postload_update_cb(void *opaque, int running, RunState state)
|
|
|
|
{
|
|
|
|
sPAPRNVRAM *nvram = opaque;
|
|
|
|
|
|
|
|
/* This is called after bdrv_invalidate_cache_all. */
|
|
|
|
|
|
|
|
qemu_del_vm_change_state_handler(nvram->vmstate);
|
|
|
|
nvram->vmstate = NULL;
|
|
|
|
|
|
|
|
blk_pwrite(nvram->blk, 0, nvram->buf, nvram->size, 0);
|
|
|
|
}
|
|
|
|
|
2014-10-02 13:56:03 +04:00
|
|
|
static int spapr_nvram_post_load(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
sPAPRNVRAM *nvram = VIO_SPAPR_NVRAM(opaque);
|
|
|
|
|
|
|
|
if (nvram->blk) {
|
2016-07-21 15:05:46 +03:00
|
|
|
nvram->vmstate = qemu_add_vm_change_state_handler(postload_update_cb,
|
|
|
|
nvram);
|
2014-10-02 13:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_spapr_nvram = {
|
|
|
|
.name = "spapr_nvram",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.pre_load = spapr_nvram_pre_load,
|
|
|
|
.post_load = spapr_nvram_post_load,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT32(size, sPAPRNVRAM),
|
2017-02-03 20:52:17 +03:00
|
|
|
VMSTATE_VBUFFER_ALLOC_UINT32(buf, sPAPRNVRAM, 1, NULL, size),
|
2014-10-02 13:56:03 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-11-12 20:46:57 +04:00
|
|
|
static Property spapr_nvram_properties[] = {
|
|
|
|
DEFINE_SPAPR_PROPERTIES(sPAPRNVRAM, sdev),
|
2014-10-07 15:59:18 +04:00
|
|
|
DEFINE_PROP_DRIVE("drive", sPAPRNVRAM, blk),
|
2012-11-12 20:46:57 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void spapr_nvram_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
|
|
|
|
|
2015-02-27 13:52:17 +03:00
|
|
|
k->realize = spapr_nvram_realize;
|
2012-11-12 20:46:57 +04:00
|
|
|
k->devnode = spapr_nvram_devnode;
|
|
|
|
k->dt_name = "nvram";
|
|
|
|
k->dt_type = "nvram";
|
|
|
|
k->dt_compatible = "qemu,spapr-nvram";
|
2013-10-11 07:08:20 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
2012-11-12 20:46:57 +04:00
|
|
|
dc->props = spapr_nvram_properties;
|
2014-10-02 13:56:03 +04:00
|
|
|
dc->vmsd = &vmstate_spapr_nvram;
|
2012-11-12 20:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo spapr_nvram_type_info = {
|
2013-04-07 23:08:16 +04:00
|
|
|
.name = TYPE_VIO_SPAPR_NVRAM,
|
2012-11-12 20:46:57 +04:00
|
|
|
.parent = TYPE_VIO_SPAPR_DEVICE,
|
|
|
|
.instance_size = sizeof(sPAPRNVRAM),
|
|
|
|
.class_init = spapr_nvram_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void spapr_nvram_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&spapr_nvram_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(spapr_nvram_register_types)
|