Statistics
| Branch: | Revision:

root / hw / spapr_nvram.c @ 9c17d615

History | View | Annotate | Download (5.5 kB)

1
/*
2
 * QEMU sPAPR NVRAM emulation
3
 *
4
 * Copyright (C) 2012 David Gibson, IBM Corporation.
5
 *
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:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
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
22
 * THE SOFTWARE.
23
 */
24
#include <sys/mman.h>
25
#include <libfdt.h>
26

    
27
#include "sysemu/device_tree.h"
28
#include "hw/sysbus.h"
29
#include "hw/spapr.h"
30
#include "hw/spapr_vio.h"
31

    
32
typedef struct sPAPRNVRAM {
33
    VIOsPAPRDevice sdev;
34
    uint32_t size;
35
    uint8_t *buf;
36
    BlockDriverState *drive;
37
} sPAPRNVRAM;
38

    
39
#define MIN_NVRAM_SIZE 8192
40
#define DEFAULT_NVRAM_SIZE 65536
41
#define MAX_NVRAM_SIZE (UINT16_MAX * 16)
42

    
43
static void rtas_nvram_fetch(sPAPREnvironment *spapr,
44
                             uint32_t token, uint32_t nargs,
45
                             target_ulong args,
46
                             uint32_t nret, target_ulong rets)
47
{
48
    sPAPRNVRAM *nvram = spapr->nvram;
49
    hwaddr offset, buffer, len;
50
    int alen;
51
    void *membuf;
52

    
53
    if ((nargs != 3) || (nret != 2)) {
54
        rtas_st(rets, 0, -3);
55
        return;
56
    }
57

    
58
    if (!nvram) {
59
        rtas_st(rets, 0, -1);
60
        rtas_st(rets, 1, 0);
61
        return;
62
    }
63

    
64
    offset = rtas_ld(args, 0);
65
    buffer = rtas_ld(args, 1);
66
    len = rtas_ld(args, 2);
67

    
68
    if (((offset + len) < offset)
69
        || ((offset + len) > nvram->size)) {
70
        rtas_st(rets, 0, -3);
71
        rtas_st(rets, 1, 0);
72
        return;
73
    }
74

    
75
    membuf = cpu_physical_memory_map(buffer, &len, 1);
76
    if (nvram->drive) {
77
        alen = bdrv_pread(nvram->drive, offset, membuf, len);
78
    } else {
79
        assert(nvram->buf);
80

    
81
        memcpy(membuf, nvram->buf + offset, len);
82
        alen = len;
83
    }
84
    cpu_physical_memory_unmap(membuf, len, 1, len);
85

    
86
    rtas_st(rets, 0, (alen < len) ? -1 : 0);
87
    rtas_st(rets, 1, (alen < 0) ? 0 : alen);
88
}
89

    
90
static void rtas_nvram_store(sPAPREnvironment *spapr,
91
                             uint32_t token, uint32_t nargs,
92
                             target_ulong args,
93
                             uint32_t nret, target_ulong rets)
94
{
95
    sPAPRNVRAM *nvram = spapr->nvram;
96
    hwaddr offset, buffer, len;
97
    int alen;
98
    void *membuf;
99

    
100
    if ((nargs != 3) || (nret != 2)) {
101
        rtas_st(rets, 0, -3);
102
        return;
103
    }
104

    
105
    if (!nvram) {
106
        rtas_st(rets, 0, -1);
107
        return;
108
    }
109

    
110
    offset = rtas_ld(args, 0);
111
    buffer = rtas_ld(args, 1);
112
    len = rtas_ld(args, 2);
113

    
114
    if (((offset + len) < offset)
115
        || ((offset + len) > nvram->size)) {
116
        rtas_st(rets, 0, -3);
117
        return;
118
    }
119

    
120
    membuf = cpu_physical_memory_map(buffer, &len, 0);
121
    if (nvram->drive) {
122
        alen = bdrv_pwrite(nvram->drive, offset, membuf, len);
123
    } else {
124
        assert(nvram->buf);
125

    
126
        memcpy(nvram->buf + offset, membuf, len);
127
        alen = len;
128
    }
129
    cpu_physical_memory_unmap(membuf, len, 0, len);
130

    
131
    rtas_st(rets, 0, (alen < len) ? -1 : 0);
132
    rtas_st(rets, 1, (alen < 0) ? 0 : alen);
133
}
134

    
135
static int spapr_nvram_init(VIOsPAPRDevice *dev)
136
{
137
    sPAPRNVRAM *nvram = (sPAPRNVRAM *)dev;
138

    
139
    if (nvram->drive) {
140
        nvram->size = bdrv_getlength(nvram->drive);
141
    } else {
142
        nvram->size = DEFAULT_NVRAM_SIZE;
143
        nvram->buf = g_malloc0(nvram->size);
144
    }
145

    
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);
149
        return -1;
150
    }
151

    
152
    spapr_rtas_register("nvram-fetch", rtas_nvram_fetch);
153
    spapr_rtas_register("nvram-store", rtas_nvram_store);
154

    
155
    return 0;
156
}
157

    
158
static int spapr_nvram_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
159
{
160
    sPAPRNVRAM *nvram = (sPAPRNVRAM *)dev;
161

    
162
    return fdt_setprop_cell(fdt, node_off, "#bytes", nvram->size);
163
}
164

    
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(),
169
};
170

    
171
static void spapr_nvram_class_init(ObjectClass *klass, void *data)
172
{
173
    DeviceClass *dc = DEVICE_CLASS(klass);
174
    VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
175

    
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;
182
}
183

    
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,
189
};
190

    
191
static void spapr_nvram_register_types(void)
192
{
193
    type_register_static(&spapr_nvram_type_info);
194
}
195

    
196
type_init(spapr_nvram_register_types)