tz-msc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * ARM TrustZone master security controller emulation
  3. *
  4. * Copyright (c) 2018 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  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.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qemu/log.h"
  13. #include "qemu/module.h"
  14. #include "qapi/error.h"
  15. #include "trace.h"
  16. #include "hw/sysbus.h"
  17. #include "migration/vmstate.h"
  18. #include "hw/registerfields.h"
  19. #include "hw/irq.h"
  20. #include "hw/misc/tz-msc.h"
  21. #include "hw/qdev-properties.h"
  22. static void tz_msc_update_irq(TZMSC *s)
  23. {
  24. bool level = s->irq_status;
  25. trace_tz_msc_update_irq(level);
  26. qemu_set_irq(s->irq, level);
  27. }
  28. static void tz_msc_cfg_nonsec(void *opaque, int n, int level)
  29. {
  30. TZMSC *s = TZ_MSC(opaque);
  31. trace_tz_msc_cfg_nonsec(level);
  32. s->cfg_nonsec = level;
  33. }
  34. static void tz_msc_cfg_sec_resp(void *opaque, int n, int level)
  35. {
  36. TZMSC *s = TZ_MSC(opaque);
  37. trace_tz_msc_cfg_sec_resp(level);
  38. s->cfg_sec_resp = level;
  39. }
  40. static void tz_msc_irq_clear(void *opaque, int n, int level)
  41. {
  42. TZMSC *s = TZ_MSC(opaque);
  43. trace_tz_msc_irq_clear(level);
  44. s->irq_clear = level;
  45. if (level) {
  46. s->irq_status = false;
  47. tz_msc_update_irq(s);
  48. }
  49. }
  50. /* The MSC may either block a transaction by aborting it, block a
  51. * transaction by making it RAZ/WI, allow it through with
  52. * MemTxAttrs indicating a secure transaction, or allow it with
  53. * MemTxAttrs indicating a non-secure transaction.
  54. */
  55. typedef enum MSCAction {
  56. MSCBlockAbort,
  57. MSCBlockRAZWI,
  58. MSCAllowSecure,
  59. MSCAllowNonSecure,
  60. } MSCAction;
  61. static MSCAction tz_msc_check(TZMSC *s, hwaddr addr)
  62. {
  63. /*
  64. * Check whether to allow an access from the bus master, returning
  65. * an MSCAction indicating the required behaviour. If the transaction
  66. * is blocked, the caller must check cfg_sec_resp to determine
  67. * whether to abort or RAZ/WI the transaction.
  68. */
  69. IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(s->idau);
  70. IDAUInterface *ii = IDAU_INTERFACE(s->idau);
  71. bool idau_exempt = false, idau_ns = true, idau_nsc = true;
  72. int idau_region = IREGION_NOTVALID;
  73. iic->check(ii, addr, &idau_region, &idau_exempt, &idau_ns, &idau_nsc);
  74. if (idau_exempt) {
  75. /*
  76. * Uncheck region -- OK, transaction type depends on
  77. * whether bus master is configured as Secure or NonSecure
  78. */
  79. return s->cfg_nonsec ? MSCAllowNonSecure : MSCAllowSecure;
  80. }
  81. if (idau_ns) {
  82. /* NonSecure region -- always forward as NS transaction */
  83. return MSCAllowNonSecure;
  84. }
  85. if (!s->cfg_nonsec) {
  86. /* Access to Secure region by Secure bus master: OK */
  87. return MSCAllowSecure;
  88. }
  89. /* Attempted access to Secure region by NS bus master: block */
  90. trace_tz_msc_access_blocked(addr);
  91. if (!s->cfg_sec_resp) {
  92. return MSCBlockRAZWI;
  93. }
  94. /*
  95. * The TRM isn't clear on behaviour if irq_clear is high when a
  96. * transaction is blocked. We assume that the MSC behaves like the
  97. * PPC, where holding irq_clear high suppresses the interrupt.
  98. */
  99. if (!s->irq_clear) {
  100. s->irq_status = true;
  101. tz_msc_update_irq(s);
  102. }
  103. return MSCBlockAbort;
  104. }
  105. static MemTxResult tz_msc_read(void *opaque, hwaddr addr, uint64_t *pdata,
  106. unsigned size, MemTxAttrs attrs)
  107. {
  108. TZMSC *s = opaque;
  109. AddressSpace *as = &s->downstream_as;
  110. uint64_t data;
  111. MemTxResult res;
  112. switch (tz_msc_check(s, addr)) {
  113. case MSCBlockAbort:
  114. return MEMTX_ERROR;
  115. case MSCBlockRAZWI:
  116. *pdata = 0;
  117. return MEMTX_OK;
  118. case MSCAllowSecure:
  119. attrs.secure = 1;
  120. attrs.unspecified = 0;
  121. break;
  122. case MSCAllowNonSecure:
  123. attrs.secure = 0;
  124. attrs.unspecified = 0;
  125. break;
  126. }
  127. switch (size) {
  128. case 1:
  129. data = address_space_ldub(as, addr, attrs, &res);
  130. break;
  131. case 2:
  132. data = address_space_lduw_le(as, addr, attrs, &res);
  133. break;
  134. case 4:
  135. data = address_space_ldl_le(as, addr, attrs, &res);
  136. break;
  137. case 8:
  138. data = address_space_ldq_le(as, addr, attrs, &res);
  139. break;
  140. default:
  141. g_assert_not_reached();
  142. }
  143. *pdata = data;
  144. return res;
  145. }
  146. static MemTxResult tz_msc_write(void *opaque, hwaddr addr, uint64_t val,
  147. unsigned size, MemTxAttrs attrs)
  148. {
  149. TZMSC *s = opaque;
  150. AddressSpace *as = &s->downstream_as;
  151. MemTxResult res;
  152. switch (tz_msc_check(s, addr)) {
  153. case MSCBlockAbort:
  154. return MEMTX_ERROR;
  155. case MSCBlockRAZWI:
  156. return MEMTX_OK;
  157. case MSCAllowSecure:
  158. attrs.secure = 1;
  159. attrs.unspecified = 0;
  160. break;
  161. case MSCAllowNonSecure:
  162. attrs.secure = 0;
  163. attrs.unspecified = 0;
  164. break;
  165. }
  166. switch (size) {
  167. case 1:
  168. address_space_stb(as, addr, val, attrs, &res);
  169. break;
  170. case 2:
  171. address_space_stw_le(as, addr, val, attrs, &res);
  172. break;
  173. case 4:
  174. address_space_stl_le(as, addr, val, attrs, &res);
  175. break;
  176. case 8:
  177. address_space_stq_le(as, addr, val, attrs, &res);
  178. break;
  179. default:
  180. g_assert_not_reached();
  181. }
  182. return res;
  183. }
  184. static const MemoryRegionOps tz_msc_ops = {
  185. .read_with_attrs = tz_msc_read,
  186. .write_with_attrs = tz_msc_write,
  187. .endianness = DEVICE_LITTLE_ENDIAN,
  188. };
  189. static void tz_msc_reset(DeviceState *dev)
  190. {
  191. TZMSC *s = TZ_MSC(dev);
  192. trace_tz_msc_reset();
  193. s->cfg_sec_resp = false;
  194. s->cfg_nonsec = false;
  195. s->irq_clear = 0;
  196. s->irq_status = 0;
  197. }
  198. static void tz_msc_init(Object *obj)
  199. {
  200. DeviceState *dev = DEVICE(obj);
  201. TZMSC *s = TZ_MSC(obj);
  202. qdev_init_gpio_in_named(dev, tz_msc_cfg_nonsec, "cfg_nonsec", 1);
  203. qdev_init_gpio_in_named(dev, tz_msc_cfg_sec_resp, "cfg_sec_resp", 1);
  204. qdev_init_gpio_in_named(dev, tz_msc_irq_clear, "irq_clear", 1);
  205. qdev_init_gpio_out_named(dev, &s->irq, "irq", 1);
  206. }
  207. static void tz_msc_realize(DeviceState *dev, Error **errp)
  208. {
  209. Object *obj = OBJECT(dev);
  210. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  211. TZMSC *s = TZ_MSC(dev);
  212. const char *name = "tz-msc-downstream";
  213. uint64_t size;
  214. /*
  215. * We can't create the upstream end of the port until realize,
  216. * as we don't know the size of the MR used as the downstream until then.
  217. * We insist on having a downstream, to avoid complicating the
  218. * code with handling the "don't know how big this is" case. It's easy
  219. * enough for the user to create an unimplemented_device as downstream
  220. * if they have nothing else to plug into this.
  221. */
  222. if (!s->downstream) {
  223. error_setg(errp, "MSC 'downstream' link not set");
  224. return;
  225. }
  226. if (!s->idau) {
  227. error_setg(errp, "MSC 'idau' link not set");
  228. return;
  229. }
  230. size = memory_region_size(s->downstream);
  231. address_space_init(&s->downstream_as, s->downstream, name);
  232. memory_region_init_io(&s->upstream, obj, &tz_msc_ops, s, name, size);
  233. sysbus_init_mmio(sbd, &s->upstream);
  234. }
  235. static const VMStateDescription tz_msc_vmstate = {
  236. .name = "tz-msc",
  237. .version_id = 1,
  238. .minimum_version_id = 1,
  239. .fields = (const VMStateField[]) {
  240. VMSTATE_BOOL(cfg_nonsec, TZMSC),
  241. VMSTATE_BOOL(cfg_sec_resp, TZMSC),
  242. VMSTATE_BOOL(irq_clear, TZMSC),
  243. VMSTATE_BOOL(irq_status, TZMSC),
  244. VMSTATE_END_OF_LIST()
  245. }
  246. };
  247. static const Property tz_msc_properties[] = {
  248. DEFINE_PROP_LINK("downstream", TZMSC, downstream,
  249. TYPE_MEMORY_REGION, MemoryRegion *),
  250. DEFINE_PROP_LINK("idau", TZMSC, idau,
  251. TYPE_IDAU_INTERFACE, IDAUInterface *),
  252. };
  253. static void tz_msc_class_init(ObjectClass *klass, void *data)
  254. {
  255. DeviceClass *dc = DEVICE_CLASS(klass);
  256. dc->realize = tz_msc_realize;
  257. dc->vmsd = &tz_msc_vmstate;
  258. device_class_set_legacy_reset(dc, tz_msc_reset);
  259. device_class_set_props(dc, tz_msc_properties);
  260. }
  261. static const TypeInfo tz_msc_info = {
  262. .name = TYPE_TZ_MSC,
  263. .parent = TYPE_SYS_BUS_DEVICE,
  264. .instance_size = sizeof(TZMSC),
  265. .instance_init = tz_msc_init,
  266. .class_init = tz_msc_class_init,
  267. };
  268. static void tz_msc_register_types(void)
  269. {
  270. type_register_static(&tz_msc_info);
  271. }
  272. type_init(tz_msc_register_types);