sifive_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * QEMU SiFive Test Finisher
  3. *
  4. * Copyright (c) 2018 SiFive, Inc.
  5. *
  6. * Test finisher memory mapped device used to exit simulation
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2 or later, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/sysbus.h"
  22. #include "qapi/error.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "system/runstate.h"
  26. #include "hw/misc/sifive_test.h"
  27. #include "system/system.h"
  28. static uint64_t sifive_test_read(void *opaque, hwaddr addr, unsigned int size)
  29. {
  30. return 0;
  31. }
  32. static void sifive_test_write(void *opaque, hwaddr addr,
  33. uint64_t val64, unsigned int size)
  34. {
  35. if (addr == 0) {
  36. int status = val64 & 0xffff;
  37. int code = (val64 >> 16) & 0xffff;
  38. switch (status) {
  39. case FINISHER_FAIL:
  40. qemu_system_shutdown_request_with_code(
  41. SHUTDOWN_CAUSE_GUEST_PANIC, code);
  42. return;
  43. case FINISHER_PASS:
  44. qemu_system_shutdown_request_with_code(
  45. SHUTDOWN_CAUSE_GUEST_SHUTDOWN, code);
  46. return;
  47. case FINISHER_RESET:
  48. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  49. return;
  50. default:
  51. break;
  52. }
  53. }
  54. qemu_log_mask(LOG_GUEST_ERROR, "%s: write: addr=0x%x val=0x%016" PRIx64 "\n",
  55. __func__, (int)addr, val64);
  56. }
  57. static const MemoryRegionOps sifive_test_ops = {
  58. .read = sifive_test_read,
  59. .write = sifive_test_write,
  60. .endianness = DEVICE_NATIVE_ENDIAN,
  61. .valid = {
  62. .min_access_size = 2,
  63. .max_access_size = 4
  64. }
  65. };
  66. static void sifive_test_init(Object *obj)
  67. {
  68. SiFiveTestState *s = SIFIVE_TEST(obj);
  69. memory_region_init_io(&s->mmio, obj, &sifive_test_ops, s,
  70. TYPE_SIFIVE_TEST, 0x1000);
  71. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  72. }
  73. static const TypeInfo sifive_test_info = {
  74. .name = TYPE_SIFIVE_TEST,
  75. .parent = TYPE_SYS_BUS_DEVICE,
  76. .instance_size = sizeof(SiFiveTestState),
  77. .instance_init = sifive_test_init,
  78. };
  79. static void sifive_test_register_types(void)
  80. {
  81. type_register_static(&sifive_test_info);
  82. }
  83. type_init(sifive_test_register_types)
  84. /*
  85. * Create Test device.
  86. */
  87. DeviceState *sifive_test_create(hwaddr addr)
  88. {
  89. DeviceState *dev = qdev_new(TYPE_SIFIVE_TEST);
  90. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  91. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  92. return dev;
  93. }