Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SCLP Support |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Christian Borntraeger <borntraeger@de.ibm.com> |
| 8 | * Heinz Graalfs <graalfs@linux.vnet.ibm.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2 or (at your |
| 11 | * option) any later version. See the COPYING file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include "cpu.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 16 | #include "sysemu/kvm.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 17 | #include "exec/memory.h" |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 18 | #include "sysemu/sysemu.h" |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 19 | #include "exec/address-spaces.h" |
David Hildenbrand | 311467f | 2015-05-29 13:14:50 +0200 | [diff] [blame] | 20 | #include "hw/boards.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 21 | #include "hw/s390x/sclp.h" |
Heinz Graalfs | 477a72a | 2013-12-18 10:10:49 +0100 | [diff] [blame] | 22 | #include "hw/s390x/event-facility.h" |
Frank Blaschka | 8cba80c | 2015-01-09 09:04:38 +0100 | [diff] [blame] | 23 | #include "hw/s390x/s390-pci-bus.h" |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 24 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 25 | static inline SCLPDevice *get_sclp_device(void) |
| 26 | { |
| 27 | return SCLP(object_resolve_path_type("", TYPE_SCLP, NULL)); |
| 28 | } |
| 29 | |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 30 | /* Provide information about the configuration, CPUs and storage */ |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 31 | static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb) |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 32 | { |
| 33 | ReadInfo *read_info = (ReadInfo *) sccb; |
David Hildenbrand | 311467f | 2015-05-29 13:14:50 +0200 | [diff] [blame] | 34 | MachineState *machine = MACHINE(qdev_get_machine()); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 35 | sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev(); |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 36 | CPUState *cpu; |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 37 | int cpu_count = 0; |
| 38 | int i = 0; |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 39 | int rnsize, rnmax; |
David Hildenbrand | 311467f | 2015-05-29 13:14:50 +0200 | [diff] [blame] | 40 | int slots = MIN(machine->ram_slots, s390_get_memslot_count(kvm_state)); |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 41 | |
| 42 | CPU_FOREACH(cpu) { |
| 43 | cpu_count++; |
| 44 | } |
| 45 | |
| 46 | /* CPU information */ |
| 47 | read_info->entries_cpu = cpu_to_be16(cpu_count); |
| 48 | read_info->offset_cpu = cpu_to_be16(offsetof(ReadInfo, entries)); |
| 49 | read_info->highest_cpu = cpu_to_be16(max_cpus); |
| 50 | |
| 51 | for (i = 0; i < cpu_count; i++) { |
| 52 | read_info->entries[i].address = i; |
| 53 | read_info->entries[i].type = 0; |
| 54 | } |
| 55 | |
Frank Blaschka | 8cba80c | 2015-01-09 09:04:38 +0100 | [diff] [blame] | 56 | read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO | |
| 57 | SCLP_HAS_PCI_RECONFIG); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 58 | |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 59 | /* Memory Hotplug is only supported for the ccw machine type */ |
| 60 | if (mhd) { |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 61 | mhd->standby_subregion_size = MEM_SECTION_SIZE; |
| 62 | /* Deduct the memory slot already used for core */ |
| 63 | if (slots > 0) { |
| 64 | while ((mhd->standby_subregion_size * (slots - 1) |
| 65 | < mhd->standby_mem_size)) { |
| 66 | mhd->standby_subregion_size = mhd->standby_subregion_size << 1; |
| 67 | } |
| 68 | } |
| 69 | /* |
| 70 | * Initialize mapping of guest standby memory sections indicating which |
| 71 | * are and are not online. Assume all standby memory begins offline. |
| 72 | */ |
| 73 | if (mhd->standby_state_map == 0) { |
| 74 | if (mhd->standby_mem_size % mhd->standby_subregion_size) { |
| 75 | mhd->standby_state_map = g_malloc0((mhd->standby_mem_size / |
| 76 | mhd->standby_subregion_size + 1) * |
| 77 | (mhd->standby_subregion_size / |
| 78 | MEM_SECTION_SIZE)); |
| 79 | } else { |
| 80 | mhd->standby_state_map = g_malloc0(mhd->standby_mem_size / |
| 81 | MEM_SECTION_SIZE); |
| 82 | } |
| 83 | } |
| 84 | mhd->padded_ram_size = ram_size + mhd->pad_size; |
| 85 | mhd->rzm = 1 << mhd->increment_size; |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 86 | |
| 87 | read_info->facilities |= cpu_to_be64(SCLP_FC_ASSIGN_ATTACH_READ_STOR); |
| 88 | } |
| 89 | |
David Hildenbrand | 71a2fd3 | 2015-06-01 13:03:23 +0200 | [diff] [blame] | 90 | rnsize = 1 << (sclp->increment_size - 20); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 91 | if (rnsize <= 128) { |
| 92 | read_info->rnsize = rnsize; |
| 93 | } else { |
| 94 | read_info->rnsize = 0; |
| 95 | read_info->rnsize2 = cpu_to_be32(rnsize); |
| 96 | } |
| 97 | |
David Hildenbrand | bd80a8a | 2015-06-01 13:04:03 +0200 | [diff] [blame] | 98 | rnmax = machine->maxram_size >> sclp->increment_size; |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 99 | if (rnmax < 0x10000) { |
| 100 | read_info->rnmax = cpu_to_be16(rnmax); |
| 101 | } else { |
| 102 | read_info->rnmax = cpu_to_be16(0); |
| 103 | read_info->rnmax2 = cpu_to_be64(rnmax); |
| 104 | } |
| 105 | |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 106 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); |
| 107 | } |
| 108 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 109 | static void read_storage_element0_info(SCLPDevice *sclp, SCCB *sccb) |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 110 | { |
| 111 | int i, assigned; |
| 112 | int subincrement_id = SCLP_STARTING_SUBINCREMENT_ID; |
| 113 | ReadStorageElementInfo *storage_info = (ReadStorageElementInfo *) sccb; |
| 114 | sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev(); |
| 115 | |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 116 | if (!mhd) { |
| 117 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 118 | return; |
| 119 | } |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 120 | |
| 121 | if ((ram_size >> mhd->increment_size) >= 0x10000) { |
| 122 | sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* Return information regarding core memory */ |
| 127 | storage_info->max_id = cpu_to_be16(mhd->standby_mem_size ? 1 : 0); |
| 128 | assigned = ram_size >> mhd->increment_size; |
| 129 | storage_info->assigned = cpu_to_be16(assigned); |
| 130 | |
| 131 | for (i = 0; i < assigned; i++) { |
| 132 | storage_info->entries[i] = cpu_to_be32(subincrement_id); |
| 133 | subincrement_id += SCLP_INCREMENT_UNIT; |
| 134 | } |
| 135 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); |
| 136 | } |
| 137 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 138 | static void read_storage_element1_info(SCLPDevice *sclp, SCCB *sccb) |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 139 | { |
| 140 | ReadStorageElementInfo *storage_info = (ReadStorageElementInfo *) sccb; |
| 141 | sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev(); |
| 142 | |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 143 | if (!mhd) { |
| 144 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 145 | return; |
| 146 | } |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 147 | |
| 148 | if ((mhd->standby_mem_size >> mhd->increment_size) >= 0x10000) { |
| 149 | sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | /* Return information regarding standby memory */ |
| 154 | storage_info->max_id = cpu_to_be16(mhd->standby_mem_size ? 1 : 0); |
| 155 | storage_info->assigned = cpu_to_be16(mhd->standby_mem_size >> |
| 156 | mhd->increment_size); |
| 157 | storage_info->standby = cpu_to_be16(mhd->standby_mem_size >> |
| 158 | mhd->increment_size); |
| 159 | sccb->h.response_code = cpu_to_be16(SCLP_RC_STANDBY_READ_COMPLETION); |
| 160 | } |
| 161 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 162 | static void attach_storage_element(SCLPDevice *sclp, SCCB *sccb, |
| 163 | uint16_t element) |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 164 | { |
| 165 | int i, assigned, subincrement_id; |
| 166 | AttachStorageElement *attach_info = (AttachStorageElement *) sccb; |
| 167 | sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev(); |
| 168 | |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 169 | if (!mhd) { |
| 170 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 171 | return; |
| 172 | } |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 173 | |
| 174 | if (element != 1) { |
| 175 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | assigned = mhd->standby_mem_size >> mhd->increment_size; |
| 180 | attach_info->assigned = cpu_to_be16(assigned); |
| 181 | subincrement_id = ((ram_size >> mhd->increment_size) << 16) |
| 182 | + SCLP_STARTING_SUBINCREMENT_ID; |
| 183 | for (i = 0; i < assigned; i++) { |
| 184 | attach_info->entries[i] = cpu_to_be32(subincrement_id); |
| 185 | subincrement_id += SCLP_INCREMENT_UNIT; |
| 186 | } |
| 187 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION); |
| 188 | } |
| 189 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 190 | static void assign_storage(SCLPDevice *sclp, SCCB *sccb) |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 191 | { |
| 192 | MemoryRegion *mr = NULL; |
| 193 | uint64_t this_subregion_size; |
| 194 | AssignStorage *assign_info = (AssignStorage *) sccb; |
| 195 | sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev(); |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 196 | ram_addr_t assign_addr; |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 197 | MemoryRegion *sysmem = get_system_memory(); |
| 198 | |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 199 | if (!mhd) { |
| 200 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 201 | return; |
| 202 | } |
| 203 | assign_addr = (assign_info->rn - 1) * mhd->rzm; |
| 204 | |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 205 | if ((assign_addr % MEM_SECTION_SIZE == 0) && |
| 206 | (assign_addr >= mhd->padded_ram_size)) { |
| 207 | /* Re-use existing memory region if found */ |
| 208 | mr = memory_region_find(sysmem, assign_addr, 1).mr; |
David Hildenbrand | 732bdd3 | 2015-06-13 08:46:54 +0200 | [diff] [blame] | 209 | memory_region_unref(mr); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 210 | if (!mr) { |
| 211 | |
| 212 | MemoryRegion *standby_ram = g_new(MemoryRegion, 1); |
| 213 | |
| 214 | /* offset to align to standby_subregion_size for allocation */ |
| 215 | ram_addr_t offset = assign_addr - |
| 216 | (assign_addr - mhd->padded_ram_size) |
| 217 | % mhd->standby_subregion_size; |
| 218 | |
| 219 | /* strlen("standby.ram") + 4 (Max of KVM_MEMORY_SLOTS) + NULL */ |
| 220 | char id[16]; |
| 221 | snprintf(id, 16, "standby.ram%d", |
| 222 | (int)((offset - mhd->padded_ram_size) / |
| 223 | mhd->standby_subregion_size) + 1); |
| 224 | |
| 225 | /* Allocate a subregion of the calculated standby_subregion_size */ |
| 226 | if (offset + mhd->standby_subregion_size > |
| 227 | mhd->padded_ram_size + mhd->standby_mem_size) { |
| 228 | this_subregion_size = mhd->padded_ram_size + |
| 229 | mhd->standby_mem_size - offset; |
| 230 | } else { |
| 231 | this_subregion_size = mhd->standby_subregion_size; |
| 232 | } |
| 233 | |
Markus Armbruster | f8ed85a | 2015-09-11 16:51:43 +0200 | [diff] [blame] | 234 | memory_region_init_ram(standby_ram, NULL, id, this_subregion_size, |
| 235 | &error_fatal); |
David Hildenbrand | 732bdd3 | 2015-06-13 08:46:54 +0200 | [diff] [blame] | 236 | /* This is a hack to make memory hotunplug work again. Once we have |
| 237 | * subdevices, we have to unparent them when unassigning memory, |
| 238 | * instead of doing it via the ref count of the MemoryRegion. */ |
| 239 | object_ref(OBJECT(standby_ram)); |
| 240 | object_unparent(OBJECT(standby_ram)); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 241 | vmstate_register_ram_global(standby_ram); |
| 242 | memory_region_add_subregion(sysmem, offset, standby_ram); |
| 243 | } |
| 244 | /* The specified subregion is no longer in standby */ |
| 245 | mhd->standby_state_map[(assign_addr - mhd->padded_ram_size) |
| 246 | / MEM_SECTION_SIZE] = 1; |
| 247 | } |
| 248 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION); |
| 249 | } |
| 250 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 251 | static void unassign_storage(SCLPDevice *sclp, SCCB *sccb) |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 252 | { |
| 253 | MemoryRegion *mr = NULL; |
| 254 | AssignStorage *assign_info = (AssignStorage *) sccb; |
| 255 | sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev(); |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 256 | ram_addr_t unassign_addr; |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 257 | MemoryRegion *sysmem = get_system_memory(); |
| 258 | |
David Hildenbrand | b02ef3d | 2015-05-29 14:06:39 +0200 | [diff] [blame] | 259 | if (!mhd) { |
| 260 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 261 | return; |
| 262 | } |
| 263 | unassign_addr = (assign_info->rn - 1) * mhd->rzm; |
| 264 | |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 265 | /* if the addr is a multiple of 256 MB */ |
| 266 | if ((unassign_addr % MEM_SECTION_SIZE == 0) && |
| 267 | (unassign_addr >= mhd->padded_ram_size)) { |
| 268 | mhd->standby_state_map[(unassign_addr - |
| 269 | mhd->padded_ram_size) / MEM_SECTION_SIZE] = 0; |
| 270 | |
| 271 | /* find the specified memory region and destroy it */ |
| 272 | mr = memory_region_find(sysmem, unassign_addr, 1).mr; |
David Hildenbrand | 732bdd3 | 2015-06-13 08:46:54 +0200 | [diff] [blame] | 273 | memory_region_unref(mr); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 274 | if (mr) { |
| 275 | int i; |
| 276 | int is_removable = 1; |
| 277 | ram_addr_t map_offset = (unassign_addr - mhd->padded_ram_size - |
| 278 | (unassign_addr - mhd->padded_ram_size) |
| 279 | % mhd->standby_subregion_size); |
| 280 | /* Mark all affected subregions as 'standby' once again */ |
| 281 | for (i = 0; |
| 282 | i < (mhd->standby_subregion_size / MEM_SECTION_SIZE); |
| 283 | i++) { |
| 284 | |
| 285 | if (mhd->standby_state_map[i + map_offset / MEM_SECTION_SIZE]) { |
| 286 | is_removable = 0; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | if (is_removable) { |
| 291 | memory_region_del_subregion(sysmem, mr); |
David Hildenbrand | 732bdd3 | 2015-06-13 08:46:54 +0200 | [diff] [blame] | 292 | object_unref(OBJECT(mr)); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION); |
| 297 | } |
| 298 | |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 299 | /* Provide information about the CPU */ |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 300 | static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb) |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 301 | { |
| 302 | ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb; |
| 303 | CPUState *cpu; |
| 304 | int cpu_count = 0; |
| 305 | int i = 0; |
| 306 | |
| 307 | CPU_FOREACH(cpu) { |
| 308 | cpu_count++; |
| 309 | } |
| 310 | |
| 311 | cpu_info->nr_configured = cpu_to_be16(cpu_count); |
| 312 | cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries)); |
| 313 | cpu_info->nr_standby = cpu_to_be16(0); |
| 314 | |
| 315 | /* The standby offset is 16-byte for each CPU */ |
| 316 | cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured |
| 317 | + cpu_info->nr_configured*sizeof(CPUEntry)); |
| 318 | |
| 319 | for (i = 0; i < cpu_count; i++) { |
| 320 | cpu_info->entries[i].address = i; |
| 321 | cpu_info->entries[i].type = 0; |
| 322 | } |
| 323 | |
| 324 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); |
| 325 | } |
| 326 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 327 | static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code) |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 328 | { |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 329 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
| 330 | SCLPEventFacility *ef = sclp->event_facility; |
Heinz Graalfs | 477a72a | 2013-12-18 10:10:49 +0100 | [diff] [blame] | 331 | SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef); |
Heinz Graalfs | 559a17a | 2012-10-29 02:13:23 +0000 | [diff] [blame] | 332 | |
Jason J. Herne | 5f04c14 | 2014-01-20 14:51:48 -0500 | [diff] [blame] | 333 | switch (code & SCLP_CMD_CODE_MASK) { |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 334 | case SCLP_CMDW_READ_SCP_INFO: |
| 335 | case SCLP_CMDW_READ_SCP_INFO_FORCED: |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 336 | sclp_c->read_SCP_info(sclp, sccb); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 337 | break; |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 338 | case SCLP_CMDW_READ_CPU_INFO: |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 339 | sclp_c->read_cpu_info(sclp, sccb); |
Jason J. Herne | 8cc3aec | 2014-01-20 14:51:49 -0500 | [diff] [blame] | 340 | break; |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 341 | case SCLP_READ_STORAGE_ELEMENT_INFO: |
| 342 | if (code & 0xff00) { |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 343 | sclp_c->read_storage_element1_info(sclp, sccb); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 344 | } else { |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 345 | sclp_c->read_storage_element0_info(sclp, sccb); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 346 | } |
| 347 | break; |
| 348 | case SCLP_ATTACH_STORAGE_ELEMENT: |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 349 | sclp_c->attach_storage_element(sclp, sccb, (code & 0xff00) >> 8); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 350 | break; |
| 351 | case SCLP_ASSIGN_STORAGE: |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 352 | sclp_c->assign_storage(sclp, sccb); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 353 | break; |
| 354 | case SCLP_UNASSIGN_STORAGE: |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 355 | sclp_c->unassign_storage(sclp, sccb); |
Matthew Rosato | 1def665 | 2014-08-28 11:25:35 -0400 | [diff] [blame] | 356 | break; |
Frank Blaschka | 8cba80c | 2015-01-09 09:04:38 +0100 | [diff] [blame] | 357 | case SCLP_CMDW_CONFIGURE_PCI: |
| 358 | s390_pci_sclp_configure(1, sccb); |
| 359 | break; |
| 360 | case SCLP_CMDW_DECONFIGURE_PCI: |
| 361 | s390_pci_sclp_configure(0, sccb); |
| 362 | break; |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 363 | default: |
Heinz Graalfs | 477a72a | 2013-12-18 10:10:49 +0100 | [diff] [blame] | 364 | efc->command_handler(ef, sccb, code); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
Thomas Huth | 6e25280 | 2014-01-13 12:55:55 +0100 | [diff] [blame] | 369 | int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code) |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 370 | { |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 371 | SCLPDevice *sclp = get_sclp_device(); |
| 372 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 373 | int r = 0; |
| 374 | SCCB work_sccb; |
| 375 | |
| 376 | hwaddr sccb_len = sizeof(SCCB); |
| 377 | |
| 378 | /* first some basic checks on program checks */ |
Thomas Huth | 6e25280 | 2014-01-13 12:55:55 +0100 | [diff] [blame] | 379 | if (env->psw.mask & PSW_MASK_PSTATE) { |
| 380 | r = -PGM_PRIVILEGED; |
| 381 | goto out; |
| 382 | } |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 383 | if (cpu_physical_memory_is_io(sccb)) { |
| 384 | r = -PGM_ADDRESSING; |
| 385 | goto out; |
| 386 | } |
Thomas Huth | 6e25280 | 2014-01-13 12:55:55 +0100 | [diff] [blame] | 387 | if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa |
| 388 | || (sccb & ~0x7ffffff8UL) != 0) { |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 389 | r = -PGM_SPECIFICATION; |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * we want to work on a private copy of the sccb, to prevent guests |
| 395 | * from playing dirty tricks by modifying the memory content after |
| 396 | * the host has checked the values |
| 397 | */ |
| 398 | cpu_physical_memory_read(sccb, &work_sccb, sccb_len); |
| 399 | |
| 400 | /* Valid sccb sizes */ |
| 401 | if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader) || |
| 402 | be16_to_cpu(work_sccb.h.length) > SCCB_SIZE) { |
| 403 | r = -PGM_SPECIFICATION; |
| 404 | goto out; |
| 405 | } |
| 406 | |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 407 | sclp_c->execute(sclp, (SCCB *)&work_sccb, code); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 408 | |
| 409 | cpu_physical_memory_write(sccb, &work_sccb, |
| 410 | be16_to_cpu(work_sccb.h.length)); |
| 411 | |
David Hildenbrand | 1723a1b | 2015-05-13 15:06:44 +0200 | [diff] [blame] | 412 | sclp_c->service_interrupt(sclp, sccb); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 413 | |
| 414 | out: |
| 415 | return r; |
| 416 | } |
| 417 | |
David Hildenbrand | 1723a1b | 2015-05-13 15:06:44 +0200 | [diff] [blame] | 418 | static void service_interrupt(SCLPDevice *sclp, uint32_t sccb) |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 419 | { |
David Hildenbrand | 1723a1b | 2015-05-13 15:06:44 +0200 | [diff] [blame] | 420 | SCLPEventFacility *ef = sclp->event_facility; |
Heinz Graalfs | 477a72a | 2013-12-18 10:10:49 +0100 | [diff] [blame] | 421 | SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef); |
| 422 | |
Heinz Graalfs | 559a17a | 2012-10-29 02:13:23 +0000 | [diff] [blame] | 423 | uint32_t param = sccb & ~3; |
| 424 | |
| 425 | /* Indicate whether an event is still pending */ |
Heinz Graalfs | 477a72a | 2013-12-18 10:10:49 +0100 | [diff] [blame] | 426 | param |= efc->event_pending(ef) ? 1 : 0; |
Heinz Graalfs | 559a17a | 2012-10-29 02:13:23 +0000 | [diff] [blame] | 427 | |
| 428 | if (!param) { |
| 429 | /* No need to send an interrupt, there's nothing to be notified about */ |
| 430 | return; |
| 431 | } |
| 432 | s390_sclp_extint(param); |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 433 | } |
| 434 | |
David Hildenbrand | 1723a1b | 2015-05-13 15:06:44 +0200 | [diff] [blame] | 435 | void sclp_service_interrupt(uint32_t sccb) |
| 436 | { |
| 437 | SCLPDevice *sclp = get_sclp_device(); |
| 438 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
| 439 | |
| 440 | sclp_c->service_interrupt(sclp, sccb); |
| 441 | } |
| 442 | |
Heinz Graalfs | f6c98f92 | 2012-10-29 02:13:22 +0000 | [diff] [blame] | 443 | /* qemu object creation and initialization functions */ |
| 444 | |
Heinz Graalfs | 559a17a | 2012-10-29 02:13:23 +0000 | [diff] [blame] | 445 | void s390_sclp_init(void) |
| 446 | { |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 447 | Object *new = object_new(TYPE_SCLP); |
Heinz Graalfs | 559a17a | 2012-10-29 02:13:23 +0000 | [diff] [blame] | 448 | |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 449 | object_property_add_child(qdev_get_machine(), TYPE_SCLP, new, |
| 450 | NULL); |
| 451 | object_unref(OBJECT(new)); |
| 452 | qdev_init_nofail(DEVICE(new)); |
Heinz Graalfs | 559a17a | 2012-10-29 02:13:23 +0000 | [diff] [blame] | 453 | } |
Matthew Rosato | 0844df7 | 2014-08-28 11:25:32 -0400 | [diff] [blame] | 454 | |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 455 | static void sclp_realize(DeviceState *dev, Error **errp) |
| 456 | { |
David Hildenbrand | 1cf065f | 2015-05-29 13:53:08 +0200 | [diff] [blame] | 457 | MachineState *machine = MACHINE(qdev_get_machine()); |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 458 | SCLPDevice *sclp = SCLP(dev); |
| 459 | Error *l_err = NULL; |
David Hildenbrand | 1cf065f | 2015-05-29 13:53:08 +0200 | [diff] [blame] | 460 | uint64_t hw_limit; |
| 461 | int ret; |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 462 | |
| 463 | object_property_set_bool(OBJECT(sclp->event_facility), true, "realized", |
| 464 | &l_err); |
| 465 | if (l_err) { |
| 466 | goto error; |
| 467 | } |
David Hildenbrand | 1cf065f | 2015-05-29 13:53:08 +0200 | [diff] [blame] | 468 | |
| 469 | ret = s390_set_memory_limit(machine->maxram_size, &hw_limit); |
| 470 | if (ret == -E2BIG) { |
| 471 | error_setg(&l_err, "qemu: host supports a maximum of %" PRIu64 " GB", |
| 472 | hw_limit >> 30); |
| 473 | goto error; |
| 474 | } else if (ret) { |
| 475 | error_setg(&l_err, "qemu: setting the guest size failed"); |
| 476 | goto error; |
| 477 | } |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 478 | return; |
| 479 | error: |
| 480 | assert(l_err); |
| 481 | error_propagate(errp, l_err); |
| 482 | } |
| 483 | |
David Hildenbrand | 1cf065f | 2015-05-29 13:53:08 +0200 | [diff] [blame] | 484 | static void sclp_memory_init(SCLPDevice *sclp) |
| 485 | { |
| 486 | MachineState *machine = MACHINE(qdev_get_machine()); |
| 487 | ram_addr_t initial_mem = machine->ram_size; |
| 488 | ram_addr_t max_mem = machine->maxram_size; |
| 489 | ram_addr_t standby_mem = max_mem - initial_mem; |
| 490 | ram_addr_t pad_mem = 0; |
| 491 | int increment_size = 20; |
| 492 | |
| 493 | /* The storage increment size is a multiple of 1M and is a power of 2. |
| 494 | * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer. |
| 495 | * The variable 'increment_size' is an exponent of 2 that can be |
| 496 | * used to calculate the size (in bytes) of an increment. */ |
| 497 | while ((initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) { |
| 498 | increment_size++; |
| 499 | } |
| 500 | if (machine->ram_slots) { |
| 501 | while ((standby_mem >> increment_size) > MAX_STORAGE_INCREMENTS) { |
| 502 | increment_size++; |
| 503 | } |
| 504 | } |
David Hildenbrand | 71a2fd3 | 2015-06-01 13:03:23 +0200 | [diff] [blame] | 505 | sclp->increment_size = increment_size; |
David Hildenbrand | 1cf065f | 2015-05-29 13:53:08 +0200 | [diff] [blame] | 506 | |
| 507 | /* The core and standby memory areas need to be aligned with |
| 508 | * the increment size. In effect, this can cause the |
| 509 | * user-specified memory size to be rounded down to align |
| 510 | * with the nearest increment boundary. */ |
| 511 | initial_mem = initial_mem >> increment_size << increment_size; |
| 512 | standby_mem = standby_mem >> increment_size << increment_size; |
| 513 | |
| 514 | /* If the size of ram is not on a MEM_SECTION_SIZE boundary, |
| 515 | calculate the pad size necessary to force this boundary. */ |
| 516 | if (machine->ram_slots && standby_mem) { |
| 517 | sclpMemoryHotplugDev *mhd = init_sclp_memory_hotplug_dev(); |
| 518 | |
| 519 | if (initial_mem % MEM_SECTION_SIZE) { |
| 520 | pad_mem = MEM_SECTION_SIZE - initial_mem % MEM_SECTION_SIZE; |
| 521 | } |
| 522 | mhd->increment_size = increment_size; |
| 523 | mhd->pad_size = pad_mem; |
| 524 | mhd->standby_mem_size = standby_mem; |
| 525 | } |
| 526 | machine->ram_size = initial_mem; |
| 527 | machine->maxram_size = initial_mem + pad_mem + standby_mem; |
| 528 | /* let's propagate the changed ram size into the global variable. */ |
| 529 | ram_size = initial_mem; |
| 530 | } |
| 531 | |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 532 | static void sclp_init(Object *obj) |
| 533 | { |
| 534 | SCLPDevice *sclp = SCLP(obj); |
| 535 | Object *new; |
| 536 | |
| 537 | new = object_new(TYPE_SCLP_EVENT_FACILITY); |
| 538 | object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new, NULL); |
| 539 | /* qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS */ |
| 540 | qdev_set_parent_bus(DEVICE(new), sysbus_get_default()); |
| 541 | object_unref(new); |
| 542 | sclp->event_facility = EVENT_FACILITY(new); |
David Hildenbrand | 1cf065f | 2015-05-29 13:53:08 +0200 | [diff] [blame] | 543 | |
| 544 | sclp_memory_init(sclp); |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void sclp_class_init(ObjectClass *oc, void *data) |
| 548 | { |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 549 | SCLPDeviceClass *sc = SCLP_CLASS(oc); |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 550 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 551 | |
| 552 | dc->desc = "SCLP (Service-Call Logical Processor)"; |
| 553 | dc->realize = sclp_realize; |
| 554 | dc->hotpluggable = false; |
| 555 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
David Hildenbrand | 25a3c5a | 2015-05-27 10:04:56 +0200 | [diff] [blame] | 556 | |
| 557 | sc->read_SCP_info = read_SCP_info; |
| 558 | sc->read_storage_element0_info = read_storage_element0_info; |
| 559 | sc->read_storage_element1_info = read_storage_element1_info; |
| 560 | sc->attach_storage_element = attach_storage_element; |
| 561 | sc->assign_storage = assign_storage; |
| 562 | sc->unassign_storage = unassign_storage; |
| 563 | sc->read_cpu_info = sclp_read_cpu_info; |
| 564 | sc->execute = sclp_execute; |
David Hildenbrand | 1723a1b | 2015-05-13 15:06:44 +0200 | [diff] [blame] | 565 | sc->service_interrupt = service_interrupt; |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static TypeInfo sclp_info = { |
| 569 | .name = TYPE_SCLP, |
| 570 | .parent = TYPE_DEVICE, |
| 571 | .instance_init = sclp_init, |
| 572 | .instance_size = sizeof(SCLPDevice), |
| 573 | .class_init = sclp_class_init, |
| 574 | .class_size = sizeof(SCLPDeviceClass), |
| 575 | }; |
| 576 | |
Matthew Rosato | 0844df7 | 2014-08-28 11:25:32 -0400 | [diff] [blame] | 577 | sclpMemoryHotplugDev *init_sclp_memory_hotplug_dev(void) |
| 578 | { |
| 579 | DeviceState *dev; |
| 580 | dev = qdev_create(NULL, TYPE_SCLP_MEMORY_HOTPLUG_DEV); |
| 581 | object_property_add_child(qdev_get_machine(), |
| 582 | TYPE_SCLP_MEMORY_HOTPLUG_DEV, |
| 583 | OBJECT(dev), NULL); |
| 584 | qdev_init_nofail(dev); |
| 585 | return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path( |
| 586 | TYPE_SCLP_MEMORY_HOTPLUG_DEV, NULL)); |
| 587 | } |
| 588 | |
| 589 | sclpMemoryHotplugDev *get_sclp_memory_hotplug_dev(void) |
| 590 | { |
| 591 | return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path( |
| 592 | TYPE_SCLP_MEMORY_HOTPLUG_DEV, NULL)); |
| 593 | } |
| 594 | |
Cornelia Huck | 183f6b8 | 2015-03-17 13:44:39 +0100 | [diff] [blame] | 595 | static void sclp_memory_hotplug_dev_class_init(ObjectClass *klass, |
| 596 | void *data) |
| 597 | { |
| 598 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 599 | |
| 600 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 601 | } |
| 602 | |
Matthew Rosato | 0844df7 | 2014-08-28 11:25:32 -0400 | [diff] [blame] | 603 | static TypeInfo sclp_memory_hotplug_dev_info = { |
| 604 | .name = TYPE_SCLP_MEMORY_HOTPLUG_DEV, |
| 605 | .parent = TYPE_SYS_BUS_DEVICE, |
| 606 | .instance_size = sizeof(sclpMemoryHotplugDev), |
Cornelia Huck | 183f6b8 | 2015-03-17 13:44:39 +0100 | [diff] [blame] | 607 | .class_init = sclp_memory_hotplug_dev_class_init, |
Matthew Rosato | 0844df7 | 2014-08-28 11:25:32 -0400 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | static void register_types(void) |
| 611 | { |
| 612 | type_register_static(&sclp_memory_hotplug_dev_info); |
David Hildenbrand | 515190d | 2015-05-27 09:49:43 +0200 | [diff] [blame] | 613 | type_register_static(&sclp_info); |
Matthew Rosato | 0844df7 | 2014-08-28 11:25:32 -0400 | [diff] [blame] | 614 | } |
| 615 | type_init(register_types); |