nubus-device.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * QEMU Macintosh Nubus
  3. *
  4. * Copyright (c) 2013-2018 Laurent Vivier <laurent@vivier.eu>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/datadir.h"
  12. #include "exec/target_page.h"
  13. #include "hw/irq.h"
  14. #include "hw/loader.h"
  15. #include "hw/nubus/nubus.h"
  16. #include "qapi/error.h"
  17. #include "qemu/error-report.h"
  18. void nubus_set_irq(NubusDevice *nd, int level)
  19. {
  20. NubusBus *nubus = NUBUS_BUS(qdev_get_parent_bus(DEVICE(nd)));
  21. qemu_set_irq(nubus->irqs[nd->slot], level);
  22. }
  23. static void nubus_device_realize(DeviceState *dev, Error **errp)
  24. {
  25. NubusBus *nubus = NUBUS_BUS(qdev_get_parent_bus(dev));
  26. NubusDevice *nd = NUBUS_DEVICE(dev);
  27. char *name, *path;
  28. hwaddr slot_offset;
  29. int64_t size, align_size;
  30. uint8_t *rom_ptr;
  31. int ret;
  32. if (nd->slot < 0 || nd->slot >= NUBUS_SLOT_NB) {
  33. error_setg(errp,
  34. "'slot' value %d out of range (must be between 0 and %d)",
  35. nd->slot, NUBUS_SLOT_NB - 1);
  36. return;
  37. }
  38. /* Super */
  39. slot_offset = nd->slot * NUBUS_SUPER_SLOT_SIZE;
  40. name = g_strdup_printf("nubus-super-slot-%x", nd->slot);
  41. memory_region_init(&nd->super_slot_mem, OBJECT(dev), name,
  42. NUBUS_SUPER_SLOT_SIZE);
  43. memory_region_add_subregion(&nubus->super_slot_io, slot_offset,
  44. &nd->super_slot_mem);
  45. g_free(name);
  46. /* Normal */
  47. slot_offset = nd->slot * NUBUS_SLOT_SIZE;
  48. name = g_strdup_printf("nubus-slot-%x", nd->slot);
  49. memory_region_init(&nd->slot_mem, OBJECT(dev), name, NUBUS_SLOT_SIZE);
  50. memory_region_add_subregion(&nubus->slot_io, slot_offset,
  51. &nd->slot_mem);
  52. g_free(name);
  53. /* Declaration ROM */
  54. if (nd->romfile != NULL) {
  55. path = qemu_find_file(QEMU_FILE_TYPE_BIOS, nd->romfile);
  56. if (path == NULL) {
  57. path = g_strdup(nd->romfile);
  58. }
  59. size = get_image_size(path);
  60. if (size < 0) {
  61. error_setg(errp, "failed to find romfile \"%s\"", nd->romfile);
  62. g_free(path);
  63. return;
  64. } else if (size == 0) {
  65. error_setg(errp, "romfile \"%s\" is empty", nd->romfile);
  66. g_free(path);
  67. return;
  68. } else if (size > NUBUS_DECL_ROM_MAX_SIZE) {
  69. error_setg(errp, "romfile \"%s\" too large (maximum size 128K)",
  70. nd->romfile);
  71. g_free(path);
  72. return;
  73. }
  74. name = g_strdup_printf("nubus-slot-%x-declaration-rom", nd->slot);
  75. /*
  76. * Ensure ROM memory region is aligned to target page size regardless
  77. * of the size of the Declaration ROM image
  78. */
  79. align_size = ROUND_UP(size, qemu_target_page_size());
  80. memory_region_init_rom(&nd->decl_rom, OBJECT(dev), name, align_size,
  81. &error_abort);
  82. rom_ptr = memory_region_get_ram_ptr(&nd->decl_rom);
  83. ret = load_image_size(path, rom_ptr + (uintptr_t)(align_size - size),
  84. size);
  85. g_free(path);
  86. g_free(name);
  87. if (ret < 0) {
  88. error_setg(errp, "could not load romfile \"%s\"", nd->romfile);
  89. return;
  90. }
  91. memory_region_add_subregion(&nd->slot_mem, NUBUS_SLOT_SIZE - align_size,
  92. &nd->decl_rom);
  93. }
  94. }
  95. static const Property nubus_device_properties[] = {
  96. DEFINE_PROP_INT32("slot", NubusDevice, slot, -1),
  97. DEFINE_PROP_STRING("romfile", NubusDevice, romfile),
  98. };
  99. static void nubus_device_class_init(ObjectClass *oc, void *data)
  100. {
  101. DeviceClass *dc = DEVICE_CLASS(oc);
  102. dc->realize = nubus_device_realize;
  103. dc->bus_type = TYPE_NUBUS_BUS;
  104. device_class_set_props(dc, nubus_device_properties);
  105. }
  106. static const TypeInfo nubus_device_type_info = {
  107. .name = TYPE_NUBUS_DEVICE,
  108. .parent = TYPE_DEVICE,
  109. .abstract = true,
  110. .instance_size = sizeof(NubusDevice),
  111. .class_init = nubus_device_class_init,
  112. };
  113. static void nubus_register_types(void)
  114. {
  115. type_register_static(&nubus_device_type_info);
  116. }
  117. type_init(nubus_register_types)