2 * ARM TrustZone peripheral protection controller emulation
4 * Copyright (c) 2018 Linaro Limited
5 * Written by Peter Maydell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 or
9 * (at your option) any later version.
12 #include "qemu/osdep.h"
14 #include "qapi/error.h"
16 #include "hw/sysbus.h"
17 #include "hw/registerfields.h"
18 #include "hw/misc/tz-ppc.h"
20 static void tz_ppc_update_irq(TZPPC
*s
)
22 bool level
= s
->irq_status
&& s
->irq_enable
;
24 trace_tz_ppc_update_irq(level
);
25 qemu_set_irq(s
->irq
, level
);
28 static void tz_ppc_cfg_nonsec(void *opaque
, int n
, int level
)
30 TZPPC
*s
= TZ_PPC(opaque
);
32 assert(n
< TZ_NUM_PORTS
);
33 trace_tz_ppc_cfg_nonsec(n
, level
);
34 s
->cfg_nonsec
[n
] = level
;
37 static void tz_ppc_cfg_ap(void *opaque
, int n
, int level
)
39 TZPPC
*s
= TZ_PPC(opaque
);
41 assert(n
< TZ_NUM_PORTS
);
42 trace_tz_ppc_cfg_ap(n
, level
);
46 static void tz_ppc_cfg_sec_resp(void *opaque
, int n
, int level
)
48 TZPPC
*s
= TZ_PPC(opaque
);
50 trace_tz_ppc_cfg_sec_resp(level
);
51 s
->cfg_sec_resp
= level
;
54 static void tz_ppc_irq_enable(void *opaque
, int n
, int level
)
56 TZPPC
*s
= TZ_PPC(opaque
);
58 trace_tz_ppc_irq_enable(level
);
59 s
->irq_enable
= level
;
63 static void tz_ppc_irq_clear(void *opaque
, int n
, int level
)
65 TZPPC
*s
= TZ_PPC(opaque
);
67 trace_tz_ppc_irq_clear(level
);
71 s
->irq_status
= false;
76 static bool tz_ppc_check(TZPPC
*s
, int n
, MemTxAttrs attrs
)
78 /* Check whether to allow an access to port n; return true if
79 * the check passes, and false if the transaction must be blocked.
80 * If the latter, the caller must check cfg_sec_resp to determine
81 * whether to abort or RAZ/WI the transaction.
83 * + nonsec_mask suppresses any check of the secure attribute
84 * + otherwise, block if cfg_nonsec is 1 and transaction is secure,
85 * or if cfg_nonsec is 0 and transaction is non-secure
86 * + block if transaction is usermode and cfg_ap is 0
88 if ((attrs
.secure
== s
->cfg_nonsec
[n
] && !(s
->nonsec_mask
& (1 << n
))) ||
89 (attrs
.user
&& !s
->cfg_ap
[n
])) {
90 /* Block the transaction. */
92 /* Note that holding irq_clear high suppresses interrupts */
101 static MemTxResult
tz_ppc_read(void *opaque
, hwaddr addr
, uint64_t *pdata
,
102 unsigned size
, MemTxAttrs attrs
)
104 TZPPCPort
*p
= opaque
;
107 AddressSpace
*as
= &p
->downstream_as
;
111 if (!tz_ppc_check(s
, n
, attrs
)) {
112 trace_tz_ppc_read_blocked(n
, addr
, attrs
.secure
, attrs
.user
);
113 if (s
->cfg_sec_resp
) {
123 data
= address_space_ldub(as
, addr
, attrs
, &res
);
126 data
= address_space_lduw_le(as
, addr
, attrs
, &res
);
129 data
= address_space_ldl_le(as
, addr
, attrs
, &res
);
132 data
= address_space_ldq_le(as
, addr
, attrs
, &res
);
135 g_assert_not_reached();
141 static MemTxResult
tz_ppc_write(void *opaque
, hwaddr addr
, uint64_t val
,
142 unsigned size
, MemTxAttrs attrs
)
144 TZPPCPort
*p
= opaque
;
146 AddressSpace
*as
= &p
->downstream_as
;
150 if (!tz_ppc_check(s
, n
, attrs
)) {
151 trace_tz_ppc_write_blocked(n
, addr
, attrs
.secure
, attrs
.user
);
152 if (s
->cfg_sec_resp
) {
161 address_space_stb(as
, addr
, val
, attrs
, &res
);
164 address_space_stw_le(as
, addr
, val
, attrs
, &res
);
167 address_space_stl_le(as
, addr
, val
, attrs
, &res
);
170 address_space_stq_le(as
, addr
, val
, attrs
, &res
);
173 g_assert_not_reached();
178 static const MemoryRegionOps tz_ppc_ops
= {
179 .read_with_attrs
= tz_ppc_read
,
180 .write_with_attrs
= tz_ppc_write
,
181 .endianness
= DEVICE_LITTLE_ENDIAN
,
184 static void tz_ppc_reset(DeviceState
*dev
)
186 TZPPC
*s
= TZ_PPC(dev
);
188 trace_tz_ppc_reset();
189 s
->cfg_sec_resp
= false;
190 memset(s
->cfg_nonsec
, 0, sizeof(s
->cfg_nonsec
));
191 memset(s
->cfg_ap
, 0, sizeof(s
->cfg_ap
));
194 static void tz_ppc_init(Object
*obj
)
196 DeviceState
*dev
= DEVICE(obj
);
197 TZPPC
*s
= TZ_PPC(obj
);
199 qdev_init_gpio_in_named(dev
, tz_ppc_cfg_nonsec
, "cfg_nonsec", TZ_NUM_PORTS
);
200 qdev_init_gpio_in_named(dev
, tz_ppc_cfg_ap
, "cfg_ap", TZ_NUM_PORTS
);
201 qdev_init_gpio_in_named(dev
, tz_ppc_cfg_sec_resp
, "cfg_sec_resp", 1);
202 qdev_init_gpio_in_named(dev
, tz_ppc_irq_enable
, "irq_enable", 1);
203 qdev_init_gpio_in_named(dev
, tz_ppc_irq_clear
, "irq_clear", 1);
204 qdev_init_gpio_out_named(dev
, &s
->irq
, "irq", 1);
207 static void tz_ppc_realize(DeviceState
*dev
, Error
**errp
)
209 Object
*obj
= OBJECT(dev
);
210 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
211 TZPPC
*s
= TZ_PPC(dev
);
214 /* We can't create the upstream end of the port until realize,
215 * as we don't know the size of the MR used as the downstream until then.
217 for (i
= 0; i
< TZ_NUM_PORTS
; i
++) {
218 TZPPCPort
*port
= &s
->port
[i
];
222 if (!port
->downstream
) {
226 name
= g_strdup_printf("tz-ppc-port[%d]", i
);
229 address_space_init(&port
->downstream_as
, port
->downstream
, name
);
231 size
= memory_region_size(port
->downstream
);
232 memory_region_init_io(&port
->upstream
, obj
, &tz_ppc_ops
,
234 sysbus_init_mmio(sbd
, &port
->upstream
);
239 static const VMStateDescription tz_ppc_vmstate
= {
242 .minimum_version_id
= 1,
243 .fields
= (VMStateField
[]) {
244 VMSTATE_BOOL_ARRAY(cfg_nonsec
, TZPPC
, 16),
245 VMSTATE_BOOL_ARRAY(cfg_ap
, TZPPC
, 16),
246 VMSTATE_BOOL(cfg_sec_resp
, TZPPC
),
247 VMSTATE_BOOL(irq_enable
, TZPPC
),
248 VMSTATE_BOOL(irq_clear
, TZPPC
),
249 VMSTATE_BOOL(irq_status
, TZPPC
),
250 VMSTATE_END_OF_LIST()
254 #define DEFINE_PORT(N) \
255 DEFINE_PROP_LINK("port[" #N "]", TZPPC, port[N].downstream, \
256 TYPE_MEMORY_REGION, MemoryRegion *)
258 static Property tz_ppc_properties
[] = {
259 DEFINE_PROP_UINT32("NONSEC_MASK", TZPPC
, nonsec_mask
, 0),
276 DEFINE_PROP_END_OF_LIST(),
279 static void tz_ppc_class_init(ObjectClass
*klass
, void *data
)
281 DeviceClass
*dc
= DEVICE_CLASS(klass
);
283 dc
->realize
= tz_ppc_realize
;
284 dc
->vmsd
= &tz_ppc_vmstate
;
285 dc
->reset
= tz_ppc_reset
;
286 dc
->props
= tz_ppc_properties
;
289 static const TypeInfo tz_ppc_info
= {
291 .parent
= TYPE_SYS_BUS_DEVICE
,
292 .instance_size
= sizeof(TZPPC
),
293 .instance_init
= tz_ppc_init
,
294 .class_init
= tz_ppc_class_init
,
297 static void tz_ppc_register_types(void)
299 type_register_static(&tz_ppc_info
);
302 type_init(tz_ppc_register_types
);