s390-virtio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * QEMU S390 virtio target
  3. *
  4. * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "hw.h"
  20. #include "block.h"
  21. #include "blockdev.h"
  22. #include "sysemu.h"
  23. #include "net.h"
  24. #include "boards.h"
  25. #include "monitor.h"
  26. #include "loader.h"
  27. #include "elf.h"
  28. #include "hw/virtio.h"
  29. #include "hw/sysbus.h"
  30. #include "kvm.h"
  31. #include "exec-memory.h"
  32. #include "hw/s390-virtio-bus.h"
  33. //#define DEBUG_S390
  34. #ifdef DEBUG_S390
  35. #define dprintf(fmt, ...) \
  36. do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
  37. #else
  38. #define dprintf(fmt, ...) \
  39. do { } while (0)
  40. #endif
  41. #define KVM_S390_VIRTIO_NOTIFY 0
  42. #define KVM_S390_VIRTIO_RESET 1
  43. #define KVM_S390_VIRTIO_SET_STATUS 2
  44. #define KERN_IMAGE_START 0x010000UL
  45. #define KERN_PARM_AREA 0x010480UL
  46. #define INITRD_START 0x800000UL
  47. #define INITRD_PARM_START 0x010408UL
  48. #define INITRD_PARM_SIZE 0x010410UL
  49. #define PARMFILE_START 0x001000UL
  50. #define ZIPL_START 0x009000UL
  51. #define ZIPL_LOAD_ADDR 0x009000UL
  52. #define ZIPL_FILENAME "s390-zipl.rom"
  53. #define MAX_BLK_DEVS 10
  54. static VirtIOS390Bus *s390_bus;
  55. static CPUState **ipi_states;
  56. CPUState *s390_cpu_addr2state(uint16_t cpu_addr)
  57. {
  58. if (cpu_addr >= smp_cpus) {
  59. return NULL;
  60. }
  61. return ipi_states[cpu_addr];
  62. }
  63. int s390_virtio_hypercall(CPUState *env, uint64_t mem, uint64_t hypercall)
  64. {
  65. int r = 0, i;
  66. dprintf("KVM hypercall: %ld\n", hypercall);
  67. switch (hypercall) {
  68. case KVM_S390_VIRTIO_NOTIFY:
  69. if (mem > ram_size) {
  70. VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
  71. mem, &i);
  72. if (dev) {
  73. virtio_queue_notify(dev->vdev, i);
  74. } else {
  75. r = -EINVAL;
  76. }
  77. } else {
  78. /* Early printk */
  79. }
  80. break;
  81. case KVM_S390_VIRTIO_RESET:
  82. {
  83. VirtIOS390Device *dev;
  84. dev = s390_virtio_bus_find_mem(s390_bus, mem);
  85. virtio_reset(dev->vdev);
  86. stb_phys(dev->dev_offs + VIRTIO_DEV_OFFS_STATUS, 0);
  87. s390_virtio_device_sync(dev);
  88. break;
  89. }
  90. case KVM_S390_VIRTIO_SET_STATUS:
  91. {
  92. VirtIOS390Device *dev;
  93. dev = s390_virtio_bus_find_mem(s390_bus, mem);
  94. if (dev) {
  95. s390_virtio_device_update_status(dev);
  96. } else {
  97. r = -EINVAL;
  98. }
  99. break;
  100. }
  101. default:
  102. r = -EINVAL;
  103. break;
  104. }
  105. return r;
  106. }
  107. /*
  108. * The number of running CPUs. On s390 a shutdown is the state of all CPUs
  109. * being either stopped or disabled (for interrupts) waiting. We have to
  110. * track this number to call the shutdown sequence accordingly. This
  111. * number is modified either on startup or while holding the big qemu lock.
  112. */
  113. static unsigned s390_running_cpus;
  114. void s390_add_running_cpu(CPUState *env)
  115. {
  116. if (env->halted) {
  117. s390_running_cpus++;
  118. env->halted = 0;
  119. env->exception_index = -1;
  120. }
  121. }
  122. unsigned s390_del_running_cpu(CPUState *env)
  123. {
  124. if (env->halted == 0) {
  125. assert(s390_running_cpus >= 1);
  126. s390_running_cpus--;
  127. env->halted = 1;
  128. env->exception_index = EXCP_HLT;
  129. }
  130. return s390_running_cpus;
  131. }
  132. /* PC hardware initialisation */
  133. static void s390_init(ram_addr_t my_ram_size,
  134. const char *boot_device,
  135. const char *kernel_filename,
  136. const char *kernel_cmdline,
  137. const char *initrd_filename,
  138. const char *cpu_model)
  139. {
  140. CPUState *env = NULL;
  141. MemoryRegion *sysmem = get_system_memory();
  142. MemoryRegion *ram = g_new(MemoryRegion, 1);
  143. ram_addr_t kernel_size = 0;
  144. ram_addr_t initrd_offset;
  145. ram_addr_t initrd_size = 0;
  146. int shift = 0;
  147. uint8_t *storage_keys;
  148. void *virtio_region;
  149. target_phys_addr_t virtio_region_len;
  150. target_phys_addr_t virtio_region_start;
  151. int i;
  152. /* s390x ram size detection needs a 16bit multiplier + an increment. So
  153. guests > 64GB can be specified in 2MB steps etc. */
  154. while ((my_ram_size >> (20 + shift)) > 65535) {
  155. shift++;
  156. }
  157. my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
  158. /* lets propagate the changed ram size into the global variable. */
  159. ram_size = my_ram_size;
  160. /* get a BUS */
  161. s390_bus = s390_virtio_bus_init(&my_ram_size);
  162. /* allocate RAM */
  163. memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size);
  164. memory_region_add_subregion(sysmem, 0, ram);
  165. /* clear virtio region */
  166. virtio_region_len = my_ram_size - ram_size;
  167. virtio_region_start = ram_size;
  168. virtio_region = cpu_physical_memory_map(virtio_region_start,
  169. &virtio_region_len, true);
  170. memset(virtio_region, 0, virtio_region_len);
  171. cpu_physical_memory_unmap(virtio_region, virtio_region_len, 1,
  172. virtio_region_len);
  173. /* allocate storage keys */
  174. storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
  175. /* init CPUs */
  176. if (cpu_model == NULL) {
  177. cpu_model = "host";
  178. }
  179. ipi_states = g_malloc(sizeof(CPUState *) * smp_cpus);
  180. for (i = 0; i < smp_cpus; i++) {
  181. CPUState *tmp_env;
  182. tmp_env = cpu_init(cpu_model);
  183. if (!env) {
  184. env = tmp_env;
  185. }
  186. ipi_states[i] = tmp_env;
  187. tmp_env->halted = 1;
  188. tmp_env->exception_index = EXCP_HLT;
  189. tmp_env->storage_keys = storage_keys;
  190. }
  191. /* One CPU has to run */
  192. s390_add_running_cpu(env);
  193. if (kernel_filename) {
  194. kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0));
  195. if (lduw_be_phys(KERN_IMAGE_START) != 0x0dd0) {
  196. fprintf(stderr, "Specified image is not an s390 boot image\n");
  197. exit(1);
  198. }
  199. env->psw.addr = KERN_IMAGE_START;
  200. env->psw.mask = 0x0000000180000000ULL;
  201. } else {
  202. ram_addr_t bios_size = 0;
  203. char *bios_filename;
  204. /* Load zipl bootloader */
  205. if (bios_name == NULL) {
  206. bios_name = ZIPL_FILENAME;
  207. }
  208. bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  209. bios_size = load_image(bios_filename, qemu_get_ram_ptr(ZIPL_LOAD_ADDR));
  210. g_free(bios_filename);
  211. if ((long)bios_size < 0) {
  212. hw_error("could not load bootloader '%s'\n", bios_name);
  213. }
  214. if (bios_size > 4096) {
  215. hw_error("stage1 bootloader is > 4k\n");
  216. }
  217. env->psw.addr = ZIPL_START;
  218. env->psw.mask = 0x0000000180000000ULL;
  219. }
  220. if (initrd_filename) {
  221. initrd_offset = INITRD_START;
  222. while (kernel_size + 0x100000 > initrd_offset) {
  223. initrd_offset += 0x100000;
  224. }
  225. initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset));
  226. stq_be_phys(INITRD_PARM_START, initrd_offset);
  227. stq_be_phys(INITRD_PARM_SIZE, initrd_size);
  228. }
  229. if (kernel_cmdline) {
  230. cpu_physical_memory_write(KERN_PARM_AREA, kernel_cmdline,
  231. strlen(kernel_cmdline) + 1);
  232. }
  233. /* Create VirtIO network adapters */
  234. for(i = 0; i < nb_nics; i++) {
  235. NICInfo *nd = &nd_table[i];
  236. DeviceState *dev;
  237. if (!nd->model) {
  238. nd->model = g_strdup("virtio");
  239. }
  240. if (strcmp(nd->model, "virtio")) {
  241. fprintf(stderr, "S390 only supports VirtIO nics\n");
  242. exit(1);
  243. }
  244. dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
  245. qdev_set_nic_properties(dev, nd);
  246. qdev_init_nofail(dev);
  247. }
  248. /* Create VirtIO disk drives */
  249. for(i = 0; i < MAX_BLK_DEVS; i++) {
  250. DriveInfo *dinfo;
  251. DeviceState *dev;
  252. dinfo = drive_get(IF_IDE, 0, i);
  253. if (!dinfo) {
  254. continue;
  255. }
  256. dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
  257. qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
  258. qdev_init_nofail(dev);
  259. }
  260. }
  261. static QEMUMachine s390_machine = {
  262. .name = "s390-virtio",
  263. .alias = "s390",
  264. .desc = "VirtIO based S390 machine",
  265. .init = s390_init,
  266. .no_serial = 1,
  267. .no_parallel = 1,
  268. .use_virtcon = 1,
  269. .no_vga = 1,
  270. .max_cpus = 255,
  271. .is_default = 1,
  272. };
  273. static void s390_machine_init(void)
  274. {
  275. qemu_register_machine(&s390_machine);
  276. }
  277. machine_init(s390_machine_init);