s390-virtio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "hw/s390-virtio-bus.h"
  32. //#define DEBUG_S390
  33. #ifdef DEBUG_S390
  34. #define dprintf(fmt, ...) \
  35. do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
  36. #else
  37. #define dprintf(fmt, ...) \
  38. do { } while (0)
  39. #endif
  40. #define KVM_S390_VIRTIO_NOTIFY 0
  41. #define KVM_S390_VIRTIO_RESET 1
  42. #define KVM_S390_VIRTIO_SET_STATUS 2
  43. #define KERN_IMAGE_START 0x010000UL
  44. #define KERN_PARM_AREA 0x010480UL
  45. #define INITRD_START 0x800000UL
  46. #define INITRD_PARM_START 0x010408UL
  47. #define INITRD_PARM_SIZE 0x010410UL
  48. #define PARMFILE_START 0x001000UL
  49. #define ZIPL_START 0x009000UL
  50. #define ZIPL_LOAD_ADDR 0x009000UL
  51. #define ZIPL_FILENAME "s390-zipl.rom"
  52. #define MAX_BLK_DEVS 10
  53. static VirtIOS390Bus *s390_bus;
  54. static CPUState **ipi_states;
  55. void irq_info(Monitor *mon);
  56. void pic_info(Monitor *mon);
  57. void irq_info(Monitor *mon)
  58. {
  59. }
  60. void pic_info(Monitor *mon)
  61. {
  62. }
  63. CPUState *s390_cpu_addr2state(uint16_t cpu_addr)
  64. {
  65. if (cpu_addr >= smp_cpus) {
  66. return NULL;
  67. }
  68. return ipi_states[cpu_addr];
  69. }
  70. int s390_virtio_hypercall(CPUState *env, uint64_t mem, uint64_t hypercall)
  71. {
  72. int r = 0, i;
  73. dprintf("KVM hypercall: %ld\n", hypercall);
  74. switch (hypercall) {
  75. case KVM_S390_VIRTIO_NOTIFY:
  76. if (mem > ram_size) {
  77. VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
  78. mem, &i);
  79. if (dev) {
  80. virtio_queue_notify(dev->vdev, i);
  81. } else {
  82. r = -EINVAL;
  83. }
  84. } else {
  85. /* Early printk */
  86. }
  87. break;
  88. case KVM_S390_VIRTIO_RESET:
  89. {
  90. VirtIOS390Device *dev;
  91. dev = s390_virtio_bus_find_mem(s390_bus, mem);
  92. virtio_reset(dev->vdev);
  93. s390_virtio_device_sync(dev);
  94. break;
  95. }
  96. case KVM_S390_VIRTIO_SET_STATUS:
  97. {
  98. VirtIOS390Device *dev;
  99. dev = s390_virtio_bus_find_mem(s390_bus, mem);
  100. if (dev) {
  101. s390_virtio_device_update_status(dev);
  102. } else {
  103. r = -EINVAL;
  104. }
  105. break;
  106. }
  107. default:
  108. r = -EINVAL;
  109. break;
  110. }
  111. return r;
  112. }
  113. /* PC hardware initialisation */
  114. static void s390_init(ram_addr_t my_ram_size,
  115. const char *boot_device,
  116. const char *kernel_filename,
  117. const char *kernel_cmdline,
  118. const char *initrd_filename,
  119. const char *cpu_model)
  120. {
  121. CPUState *env = NULL;
  122. ram_addr_t ram_addr;
  123. ram_addr_t kernel_size = 0;
  124. ram_addr_t initrd_offset;
  125. ram_addr_t initrd_size = 0;
  126. int shift = 0;
  127. uint8_t *storage_keys;
  128. int i;
  129. /* s390x ram size detection needs a 16bit multiplier + an increment. So
  130. guests > 64GB can be specified in 2MB steps etc. */
  131. while ((my_ram_size >> (20 + shift)) > 65535) {
  132. shift++;
  133. }
  134. my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
  135. /* lets propagate the changed ram size into the global variable. */
  136. ram_size = my_ram_size;
  137. /* get a BUS */
  138. s390_bus = s390_virtio_bus_init(&my_ram_size);
  139. /* allocate RAM */
  140. ram_addr = qemu_ram_alloc(NULL, "s390.ram", my_ram_size);
  141. cpu_register_physical_memory(0, my_ram_size, ram_addr);
  142. /* allocate storage keys */
  143. storage_keys = qemu_mallocz(my_ram_size / TARGET_PAGE_SIZE);
  144. /* init CPUs */
  145. if (cpu_model == NULL) {
  146. cpu_model = "host";
  147. }
  148. ipi_states = qemu_malloc(sizeof(CPUState *) * smp_cpus);
  149. for (i = 0; i < smp_cpus; i++) {
  150. CPUState *tmp_env;
  151. tmp_env = cpu_init(cpu_model);
  152. if (!env) {
  153. env = tmp_env;
  154. }
  155. ipi_states[i] = tmp_env;
  156. tmp_env->halted = 1;
  157. tmp_env->exception_index = EXCP_HLT;
  158. tmp_env->storage_keys = storage_keys;
  159. }
  160. env->halted = 0;
  161. env->exception_index = 0;
  162. if (kernel_filename) {
  163. kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0));
  164. if (lduw_be_phys(KERN_IMAGE_START) != 0x0dd0) {
  165. fprintf(stderr, "Specified image is not an s390 boot image\n");
  166. exit(1);
  167. }
  168. env->psw.addr = KERN_IMAGE_START;
  169. env->psw.mask = 0x0000000180000000ULL;
  170. } else {
  171. ram_addr_t bios_size = 0;
  172. char *bios_filename;
  173. /* Load zipl bootloader */
  174. if (bios_name == NULL) {
  175. bios_name = ZIPL_FILENAME;
  176. }
  177. bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  178. bios_size = load_image(bios_filename, qemu_get_ram_ptr(ZIPL_LOAD_ADDR));
  179. qemu_free(bios_filename);
  180. if ((long)bios_size < 0) {
  181. hw_error("could not load bootloader '%s'\n", bios_name);
  182. }
  183. if (bios_size > 4096) {
  184. hw_error("stage1 bootloader is > 4k\n");
  185. }
  186. env->psw.addr = ZIPL_START;
  187. env->psw.mask = 0x0000000180000000ULL;
  188. }
  189. if (initrd_filename) {
  190. initrd_offset = INITRD_START;
  191. while (kernel_size + 0x100000 > initrd_offset) {
  192. initrd_offset += 0x100000;
  193. }
  194. initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset));
  195. stq_be_phys(INITRD_PARM_START, initrd_offset);
  196. stq_be_phys(INITRD_PARM_SIZE, initrd_size);
  197. }
  198. if (kernel_cmdline) {
  199. cpu_physical_memory_write(KERN_PARM_AREA, kernel_cmdline,
  200. strlen(kernel_cmdline));
  201. }
  202. /* Create VirtIO network adapters */
  203. for(i = 0; i < nb_nics; i++) {
  204. NICInfo *nd = &nd_table[i];
  205. DeviceState *dev;
  206. if (!nd->model) {
  207. nd->model = qemu_strdup("virtio");
  208. }
  209. if (strcmp(nd->model, "virtio")) {
  210. fprintf(stderr, "S390 only supports VirtIO nics\n");
  211. exit(1);
  212. }
  213. dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
  214. qdev_set_nic_properties(dev, nd);
  215. qdev_init_nofail(dev);
  216. }
  217. /* Create VirtIO disk drives */
  218. for(i = 0; i < MAX_BLK_DEVS; i++) {
  219. DriveInfo *dinfo;
  220. DeviceState *dev;
  221. dinfo = drive_get(IF_IDE, 0, i);
  222. if (!dinfo) {
  223. continue;
  224. }
  225. dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
  226. qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
  227. qdev_init_nofail(dev);
  228. }
  229. }
  230. static QEMUMachine s390_machine = {
  231. .name = "s390-virtio",
  232. .alias = "s390",
  233. .desc = "VirtIO based S390 machine",
  234. .init = s390_init,
  235. .no_serial = 1,
  236. .no_parallel = 1,
  237. .use_virtcon = 1,
  238. .no_vga = 1,
  239. .max_cpus = 255,
  240. .is_default = 1,
  241. };
  242. static void s390_machine_init(void)
  243. {
  244. qemu_register_machine(&s390_machine);
  245. }
  246. machine_init(s390_machine_init);