2 * QEMU sPAPR NVRAM emulation
4 * Copyright (C) 2012 David Gibson, IBM Corporation.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "sysemu/device_tree.h"
28 #include "hw/sysbus.h"
30 #include "hw/spapr_vio.h"
32 typedef struct sPAPRNVRAM
{
36 BlockDriverState
*drive
;
39 #define MIN_NVRAM_SIZE 8192
40 #define DEFAULT_NVRAM_SIZE 65536
41 #define MAX_NVRAM_SIZE (UINT16_MAX * 16)
43 static void rtas_nvram_fetch(sPAPREnvironment
*spapr
,
44 uint32_t token
, uint32_t nargs
,
46 uint32_t nret
, target_ulong rets
)
48 sPAPRNVRAM
*nvram
= spapr
->nvram
;
49 hwaddr offset
, buffer
, len
;
53 if ((nargs
!= 3) || (nret
!= 2)) {
64 offset
= rtas_ld(args
, 0);
65 buffer
= rtas_ld(args
, 1);
66 len
= rtas_ld(args
, 2);
68 if (((offset
+ len
) < offset
)
69 || ((offset
+ len
) > nvram
->size
)) {
75 membuf
= cpu_physical_memory_map(buffer
, &len
, 1);
77 alen
= bdrv_pread(nvram
->drive
, offset
, membuf
, len
);
81 memcpy(membuf
, nvram
->buf
+ offset
, len
);
84 cpu_physical_memory_unmap(membuf
, len
, 1, len
);
86 rtas_st(rets
, 0, (alen
< len
) ? -1 : 0);
87 rtas_st(rets
, 1, (alen
< 0) ? 0 : alen
);
90 static void rtas_nvram_store(sPAPREnvironment
*spapr
,
91 uint32_t token
, uint32_t nargs
,
93 uint32_t nret
, target_ulong rets
)
95 sPAPRNVRAM
*nvram
= spapr
->nvram
;
96 hwaddr offset
, buffer
, len
;
100 if ((nargs
!= 3) || (nret
!= 2)) {
101 rtas_st(rets
, 0, -3);
106 rtas_st(rets
, 0, -1);
110 offset
= rtas_ld(args
, 0);
111 buffer
= rtas_ld(args
, 1);
112 len
= rtas_ld(args
, 2);
114 if (((offset
+ len
) < offset
)
115 || ((offset
+ len
) > nvram
->size
)) {
116 rtas_st(rets
, 0, -3);
120 membuf
= cpu_physical_memory_map(buffer
, &len
, 0);
122 alen
= bdrv_pwrite(nvram
->drive
, offset
, membuf
, len
);
126 memcpy(nvram
->buf
+ offset
, membuf
, len
);
129 cpu_physical_memory_unmap(membuf
, len
, 0, len
);
131 rtas_st(rets
, 0, (alen
< len
) ? -1 : 0);
132 rtas_st(rets
, 1, (alen
< 0) ? 0 : alen
);
135 static int spapr_nvram_init(VIOsPAPRDevice
*dev
)
137 sPAPRNVRAM
*nvram
= (sPAPRNVRAM
*)dev
;
140 nvram
->size
= bdrv_getlength(nvram
->drive
);
142 nvram
->size
= DEFAULT_NVRAM_SIZE
;
143 nvram
->buf
= g_malloc0(nvram
->size
);
146 if ((nvram
->size
< MIN_NVRAM_SIZE
) || (nvram
->size
> MAX_NVRAM_SIZE
)) {
147 fprintf(stderr
, "spapr-nvram must be between %d and %d bytes in size\n",
148 MIN_NVRAM_SIZE
, MAX_NVRAM_SIZE
);
152 spapr_rtas_register("nvram-fetch", rtas_nvram_fetch
);
153 spapr_rtas_register("nvram-store", rtas_nvram_store
);
158 static int spapr_nvram_devnode(VIOsPAPRDevice
*dev
, void *fdt
, int node_off
)
160 sPAPRNVRAM
*nvram
= (sPAPRNVRAM
*)dev
;
162 return fdt_setprop_cell(fdt
, node_off
, "#bytes", nvram
->size
);
165 static Property spapr_nvram_properties
[] = {
166 DEFINE_SPAPR_PROPERTIES(sPAPRNVRAM
, sdev
),
167 DEFINE_PROP_DRIVE("drive", sPAPRNVRAM
, drive
),
168 DEFINE_PROP_END_OF_LIST(),
171 static void spapr_nvram_class_init(ObjectClass
*klass
, void *data
)
173 DeviceClass
*dc
= DEVICE_CLASS(klass
);
174 VIOsPAPRDeviceClass
*k
= VIO_SPAPR_DEVICE_CLASS(klass
);
176 k
->init
= spapr_nvram_init
;
177 k
->devnode
= spapr_nvram_devnode
;
178 k
->dt_name
= "nvram";
179 k
->dt_type
= "nvram";
180 k
->dt_compatible
= "qemu,spapr-nvram";
181 dc
->props
= spapr_nvram_properties
;
184 static const TypeInfo spapr_nvram_type_info
= {
185 .name
= "spapr-nvram",
186 .parent
= TYPE_VIO_SPAPR_DEVICE
,
187 .instance_size
= sizeof(sPAPRNVRAM
),
188 .class_init
= spapr_nvram_class_init
,
191 static void spapr_nvram_register_types(void)
193 type_register_static(&spapr_nvram_type_info
);
196 type_init(spapr_nvram_register_types
)