summaryrefslogtreecommitdiffstats
path: root/hw/misc/mmio_interface.c
blob: da154e5c955a6a27918a8d816c3f8750e924eaca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * mmio_interface.c
 *
 *  Copyright (C) 2017 : GreenSocs
 *      http://www.greensocs.com/ , email: info@greensocs.com
 *
 *  Developed by :
 *  Frederic Konrad   <fred.konrad@greensocs.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option)any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "qemu/osdep.h"
#include "qemu/log.h"
#include "trace.h"
#include "hw/qdev-properties.h"
#include "hw/misc/mmio_interface.h"
#include "qapi/error.h"

#ifndef DEBUG_MMIO_INTERFACE
#define DEBUG_MMIO_INTERFACE 0
#endif

static uint64_t mmio_interface_counter;

#define DPRINTF(fmt, ...) do {                                                 \
    if (DEBUG_MMIO_INTERFACE) {                                                \
        qemu_log("mmio_interface: 0x%" PRIX64 ": " fmt, s->id, ## __VA_ARGS__);\
    }                                                                          \
} while (0);

static void mmio_interface_init(Object *obj)
{
    MMIOInterface *s = MMIO_INTERFACE(obj);

    if (DEBUG_MMIO_INTERFACE) {
        s->id = mmio_interface_counter++;
    }

    DPRINTF("interface created\n");
    s->host_ptr = 0;
    s->subregion = 0;
}

static void mmio_interface_realize(DeviceState *dev, Error **errp)
{
    MMIOInterface *s = MMIO_INTERFACE(dev);

    DPRINTF("realize from 0x%" PRIX64 " to 0x%" PRIX64 " map host pointer"
            " %p\n", s->start, s->end, s->host_ptr);

    if (!s->host_ptr) {
        error_setg(errp, "host_ptr property must be set");
        return;
    }

    if (!s->subregion) {
        error_setg(errp, "subregion property must be set");
        return;
    }

    memory_region_init_ram_ptr(&s->ram_mem, OBJECT(s), "ram",
                               s->end - s->start + 1, s->host_ptr);
    memory_region_set_readonly(&s->ram_mem, s->ro);
    memory_region_add_subregion(s->subregion, s->start, &s->ram_mem);
}

static void mmio_interface_unrealize(DeviceState *dev, Error **errp)
{
    MMIOInterface *s = MMIO_INTERFACE(dev);

    DPRINTF("unrealize from 0x%" PRIX64 " to 0x%" PRIX64 " map host pointer"
            " %p\n", s->start, s->end, s->host_ptr);
    memory_region_del_subregion(s->subregion, &s->ram_mem);
}

static void mmio_interface_finalize(Object *obj)
{
    MMIOInterface *s = MMIO_INTERFACE(obj);

    DPRINTF("finalize from 0x%" PRIX64 " to 0x%" PRIX64 " map host pointer"
            " %p\n", s->start, s->end, s->host_ptr);
    object_unparent(OBJECT(&s->ram_mem));
}

static Property mmio_interface_properties[] = {
    DEFINE_PROP_UINT64("start", MMIOInterface, start, 0),
    DEFINE_PROP_UINT64("end", MMIOInterface, end, 0),
    DEFINE_PROP_PTR("host_ptr", MMIOInterface, host_ptr),
    DEFINE_PROP_BOOL("ro", MMIOInterface, ro, false),
    DEFINE_PROP_MEMORY_REGION("subregion", MMIOInterface, subregion),
    DEFINE_PROP_END_OF_LIST(),
};

static void mmio_interface_class_init(ObjectClass *oc, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(oc);

    dc->realize = mmio_interface_realize;
    dc->unrealize = mmio_interface_unrealize;
    dc->props = mmio_interface_properties;
}

static const TypeInfo mmio_interface_info = {
    .name          = TYPE_MMIO_INTERFACE,
    .parent        = TYPE_DEVICE,
    .instance_size = sizeof(MMIOInterface),
    .instance_init = mmio_interface_init,
    .instance_finalize = mmio_interface_finalize,
    .class_init    = mmio_interface_class_init,
};

static void mmio_interface_register_types(void)
{
    type_register_static(&mmio_interface_info);
}

type_init(mmio_interface_register_types)