Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * s390 PCI instructions |
| 3 | * |
| 4 | * Copyright 2014 IBM Corp. |
| 5 | * Author(s): Frank Blaschka <frank.blaschka@de.ibm.com> |
| 6 | * Hong Bo Li <lihbbj@cn.ibm.com> |
| 7 | * Yi Min Zhao <zyimin@cn.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or (at |
| 10 | * your option) any later version. See the COPYING file in the top-level |
| 11 | * directory. |
| 12 | */ |
| 13 | |
Peter Maydell | 9615495 | 2016-01-26 18:17:00 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Paolo Bonzini | 4771d75 | 2016-01-19 21:51:44 +0100 | [diff] [blame] | 15 | #include "qemu-common.h" |
| 16 | #include "cpu.h" |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 17 | #include "s390-pci-inst.h" |
| 18 | #include "s390-pci-bus.h" |
Markus Armbruster | a9c9427 | 2016-06-22 19:11:19 +0200 | [diff] [blame] | 19 | #include "exec/memory-internal.h" |
| 20 | #include "qemu/error-report.h" |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 21 | #include "sysemu/hw_accel.h" |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 22 | |
Danil Antonov | 229913f | 2017-04-01 16:56:30 +0300 | [diff] [blame] | 23 | #ifndef DEBUG_S390PCI_INST |
| 24 | #define DEBUG_S390PCI_INST 0 |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 25 | #endif |
| 26 | |
Danil Antonov | 229913f | 2017-04-01 16:56:30 +0300 | [diff] [blame] | 27 | #define DPRINTF(fmt, ...) \ |
| 28 | do { \ |
| 29 | if (DEBUG_S390PCI_INST) { \ |
| 30 | fprintf(stderr, "s390pci-inst: " fmt, ## __VA_ARGS__); \ |
| 31 | } \ |
| 32 | } while (0) |
| 33 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 34 | static void s390_set_status_code(CPUS390XState *env, |
| 35 | uint8_t r, uint64_t status_code) |
| 36 | { |
| 37 | env->regs[r] &= ~0xff000000ULL; |
| 38 | env->regs[r] |= (status_code & 0xff) << 24; |
| 39 | } |
| 40 | |
| 41 | static int list_pci(ClpReqRspListPci *rrb, uint8_t *cc) |
| 42 | { |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 43 | S390PCIBusDevice *pbdev = NULL; |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 44 | S390pciState *s = s390_get_phb(); |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 45 | uint32_t res_code, initial_l2, g_l2; |
| 46 | int rc, i; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 47 | uint64_t resume_token; |
| 48 | |
| 49 | rc = 0; |
| 50 | if (lduw_p(&rrb->request.hdr.len) != 32) { |
| 51 | res_code = CLP_RC_LEN; |
| 52 | rc = -EINVAL; |
| 53 | goto out; |
| 54 | } |
| 55 | |
| 56 | if ((ldl_p(&rrb->request.fmt) & CLP_MASK_FMT) != 0) { |
| 57 | res_code = CLP_RC_FMT; |
| 58 | rc = -EINVAL; |
| 59 | goto out; |
| 60 | } |
| 61 | |
| 62 | if ((ldl_p(&rrb->request.fmt) & ~CLP_MASK_FMT) != 0 || |
Yi Min Zhao | bf32839 | 2016-05-11 15:22:42 +0800 | [diff] [blame] | 63 | ldq_p(&rrb->request.reserved1) != 0) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 64 | res_code = CLP_RC_RESNOT0; |
| 65 | rc = -EINVAL; |
| 66 | goto out; |
| 67 | } |
| 68 | |
| 69 | resume_token = ldq_p(&rrb->request.resume_token); |
| 70 | |
| 71 | if (resume_token) { |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 72 | pbdev = s390_pci_find_dev_by_idx(s, resume_token); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 73 | if (!pbdev) { |
| 74 | res_code = CLP_RC_LISTPCI_BADRT; |
| 75 | rc = -EINVAL; |
| 76 | goto out; |
| 77 | } |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 78 | } else { |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 79 | pbdev = s390_pci_find_next_avail_dev(s, NULL); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (lduw_p(&rrb->response.hdr.len) < 48) { |
| 83 | res_code = CLP_RC_8K; |
| 84 | rc = -EINVAL; |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | initial_l2 = lduw_p(&rrb->response.hdr.len); |
| 89 | if ((initial_l2 - LIST_PCI_HDR_LEN) % sizeof(ClpFhListEntry) |
| 90 | != 0) { |
| 91 | res_code = CLP_RC_LEN; |
| 92 | rc = -EINVAL; |
| 93 | *cc = 3; |
| 94 | goto out; |
| 95 | } |
| 96 | |
| 97 | stl_p(&rrb->response.fmt, 0); |
| 98 | stq_p(&rrb->response.reserved1, 0); |
Yi Min Zhao | c188e30 | 2016-05-13 16:16:30 +0800 | [diff] [blame] | 99 | stl_p(&rrb->response.mdd, FH_MASK_SHM); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 100 | stw_p(&rrb->response.max_fn, PCI_MAX_FUNCTIONS); |
Yi Min Zhao | bf32839 | 2016-05-11 15:22:42 +0800 | [diff] [blame] | 101 | rrb->response.flags = UID_CHECKING_ENABLED; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 102 | rrb->response.entry_size = sizeof(ClpFhListEntry); |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 103 | |
| 104 | i = 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 105 | g_l2 = LIST_PCI_HDR_LEN; |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 106 | while (g_l2 < initial_l2 && pbdev) { |
| 107 | stw_p(&rrb->response.fh_list[i].device_id, |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 108 | pci_get_word(pbdev->pdev->config + PCI_DEVICE_ID)); |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 109 | stw_p(&rrb->response.fh_list[i].vendor_id, |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 110 | pci_get_word(pbdev->pdev->config + PCI_VENDOR_ID)); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 111 | /* Ignore RESERVED devices. */ |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 112 | stl_p(&rrb->response.fh_list[i].config, |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 113 | pbdev->state == ZPCI_FS_STANDBY ? 0 : 1 << 31); |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 114 | stl_p(&rrb->response.fh_list[i].fid, pbdev->fid); |
| 115 | stl_p(&rrb->response.fh_list[i].fh, pbdev->fh); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 116 | |
| 117 | g_l2 += sizeof(ClpFhListEntry); |
| 118 | /* Add endian check for DPRINTF? */ |
| 119 | DPRINTF("g_l2 %d vendor id 0x%x device id 0x%x fid 0x%x fh 0x%x\n", |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 120 | g_l2, |
| 121 | lduw_p(&rrb->response.fh_list[i].vendor_id), |
| 122 | lduw_p(&rrb->response.fh_list[i].device_id), |
| 123 | ldl_p(&rrb->response.fh_list[i].fid), |
| 124 | ldl_p(&rrb->response.fh_list[i].fh)); |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 125 | pbdev = s390_pci_find_next_avail_dev(s, pbdev); |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 126 | i++; |
| 127 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 128 | |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 129 | if (!pbdev) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 130 | resume_token = 0; |
| 131 | } else { |
Yi Min Zhao | 4e3bfc1 | 2016-06-03 15:16:01 +0800 | [diff] [blame] | 132 | resume_token = pbdev->fh & FH_MASK_INDEX; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 133 | } |
| 134 | stq_p(&rrb->response.resume_token, resume_token); |
| 135 | stw_p(&rrb->response.hdr.len, g_l2); |
| 136 | stw_p(&rrb->response.hdr.rsp, CLP_RC_OK); |
| 137 | out: |
| 138 | if (rc) { |
| 139 | DPRINTF("list pci failed rc 0x%x\n", rc); |
| 140 | stw_p(&rrb->response.hdr.rsp, res_code); |
| 141 | } |
| 142 | return rc; |
| 143 | } |
| 144 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 145 | int clp_service_call(S390CPU *cpu, uint8_t r2, uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 146 | { |
| 147 | ClpReqHdr *reqh; |
| 148 | ClpRspHdr *resh; |
| 149 | S390PCIBusDevice *pbdev; |
| 150 | uint32_t req_len; |
| 151 | uint32_t res_len; |
| 152 | uint8_t buffer[4096 * 2]; |
| 153 | uint8_t cc = 0; |
| 154 | CPUS390XState *env = &cpu->env; |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 155 | S390pciState *s = s390_get_phb(); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 156 | int i; |
| 157 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 158 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 159 | s390_program_interrupt(env, PGM_PRIVILEGED, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 163 | if (s390_cpu_virt_mem_read(cpu, env->regs[r2], r2, buffer, sizeof(*reqh))) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 164 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 165 | return 0; |
| 166 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 167 | reqh = (ClpReqHdr *)buffer; |
| 168 | req_len = lduw_p(&reqh->len); |
| 169 | if (req_len < 16 || req_len > 8184 || (req_len % 8 != 0)) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 170 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 174 | if (s390_cpu_virt_mem_read(cpu, env->regs[r2], r2, buffer, |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 175 | req_len + sizeof(*resh))) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 176 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 179 | resh = (ClpRspHdr *)(buffer + req_len); |
| 180 | res_len = lduw_p(&resh->len); |
| 181 | if (res_len < 8 || res_len > 8176 || (res_len % 8 != 0)) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 182 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | if ((req_len + res_len) > 8192) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 186 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 190 | if (s390_cpu_virt_mem_read(cpu, env->regs[r2], r2, buffer, |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 191 | req_len + res_len)) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 192 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 193 | return 0; |
| 194 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 195 | |
| 196 | if (req_len != 32) { |
| 197 | stw_p(&resh->rsp, CLP_RC_LEN); |
| 198 | goto out; |
| 199 | } |
| 200 | |
| 201 | switch (lduw_p(&reqh->cmd)) { |
| 202 | case CLP_LIST_PCI: { |
| 203 | ClpReqRspListPci *rrb = (ClpReqRspListPci *)buffer; |
| 204 | list_pci(rrb, &cc); |
| 205 | break; |
| 206 | } |
| 207 | case CLP_SET_PCI_FN: { |
| 208 | ClpReqSetPci *reqsetpci = (ClpReqSetPci *)reqh; |
| 209 | ClpRspSetPci *ressetpci = (ClpRspSetPci *)resh; |
| 210 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 211 | pbdev = s390_pci_find_dev_by_fh(s, ldl_p(&reqsetpci->fh)); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 212 | if (!pbdev) { |
| 213 | stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FH); |
| 214 | goto out; |
| 215 | } |
| 216 | |
| 217 | switch (reqsetpci->oc) { |
| 218 | case CLP_SET_ENABLE_PCI_FN: |
Yi Min Zhao | bd49768 | 2016-06-15 17:02:36 +0800 | [diff] [blame] | 219 | switch (reqsetpci->ndas) { |
| 220 | case 0: |
| 221 | stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_DMAAS); |
| 222 | goto out; |
| 223 | case 1: |
| 224 | break; |
| 225 | default: |
| 226 | stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_RES); |
| 227 | goto out; |
| 228 | } |
| 229 | |
| 230 | if (pbdev->fh & FH_MASK_ENABLE) { |
| 231 | stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FHOP); |
| 232 | goto out; |
| 233 | } |
| 234 | |
Yi Min Zhao | c188e30 | 2016-05-13 16:16:30 +0800 | [diff] [blame] | 235 | pbdev->fh |= FH_MASK_ENABLE; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 236 | pbdev->state = ZPCI_FS_ENABLED; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 237 | stl_p(&ressetpci->fh, pbdev->fh); |
| 238 | stw_p(&ressetpci->hdr.rsp, CLP_RC_OK); |
| 239 | break; |
| 240 | case CLP_SET_DISABLE_PCI_FN: |
Yi Min Zhao | bd49768 | 2016-06-15 17:02:36 +0800 | [diff] [blame] | 241 | if (!(pbdev->fh & FH_MASK_ENABLE)) { |
| 242 | stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FHOP); |
| 243 | goto out; |
| 244 | } |
| 245 | device_reset(DEVICE(pbdev)); |
Yi Min Zhao | c188e30 | 2016-05-13 16:16:30 +0800 | [diff] [blame] | 246 | pbdev->fh &= ~FH_MASK_ENABLE; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 247 | pbdev->state = ZPCI_FS_DISABLED; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 248 | stl_p(&ressetpci->fh, pbdev->fh); |
| 249 | stw_p(&ressetpci->hdr.rsp, CLP_RC_OK); |
| 250 | break; |
| 251 | default: |
| 252 | DPRINTF("unknown set pci command\n"); |
| 253 | stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FHOP); |
| 254 | break; |
| 255 | } |
| 256 | break; |
| 257 | } |
| 258 | case CLP_QUERY_PCI_FN: { |
| 259 | ClpReqQueryPci *reqquery = (ClpReqQueryPci *)reqh; |
| 260 | ClpRspQueryPci *resquery = (ClpRspQueryPci *)resh; |
| 261 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 262 | pbdev = s390_pci_find_dev_by_fh(s, ldl_p(&reqquery->fh)); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 263 | if (!pbdev) { |
| 264 | DPRINTF("query pci no pci dev\n"); |
| 265 | stw_p(&resquery->hdr.rsp, CLP_RC_SETPCIFN_FH); |
| 266 | goto out; |
| 267 | } |
| 268 | |
| 269 | for (i = 0; i < PCI_BAR_COUNT; i++) { |
| 270 | uint32_t data = pci_get_long(pbdev->pdev->config + |
| 271 | PCI_BASE_ADDRESS_0 + (i * 4)); |
| 272 | |
| 273 | stl_p(&resquery->bar[i], data); |
| 274 | resquery->bar_size[i] = pbdev->pdev->io_regions[i].size ? |
| 275 | ctz64(pbdev->pdev->io_regions[i].size) : 0; |
| 276 | DPRINTF("bar %d addr 0x%x size 0x%" PRIx64 "barsize 0x%x\n", i, |
| 277 | ldl_p(&resquery->bar[i]), |
| 278 | pbdev->pdev->io_regions[i].size, |
| 279 | resquery->bar_size[i]); |
| 280 | } |
| 281 | |
| 282 | stq_p(&resquery->sdma, ZPCI_SDMA_ADDR); |
| 283 | stq_p(&resquery->edma, ZPCI_EDMA_ADDR); |
Yi Min Zhao | 67aad50 | 2016-06-13 19:28:38 +0800 | [diff] [blame] | 284 | stl_p(&resquery->fid, pbdev->fid); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 285 | stw_p(&resquery->pchid, 0); |
| 286 | stw_p(&resquery->ug, 1); |
Yi Min Zhao | bf32839 | 2016-05-11 15:22:42 +0800 | [diff] [blame] | 287 | stl_p(&resquery->uid, pbdev->uid); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 288 | stw_p(&resquery->hdr.rsp, CLP_RC_OK); |
| 289 | break; |
| 290 | } |
| 291 | case CLP_QUERY_PCI_FNGRP: { |
| 292 | ClpRspQueryPciGrp *resgrp = (ClpRspQueryPciGrp *)resh; |
| 293 | resgrp->fr = 1; |
| 294 | stq_p(&resgrp->dasm, 0); |
| 295 | stq_p(&resgrp->msia, ZPCI_MSI_ADDR); |
| 296 | stw_p(&resgrp->mui, 0); |
| 297 | stw_p(&resgrp->i, 128); |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 298 | stw_p(&resgrp->maxstbl, 128); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 299 | resgrp->version = 0; |
| 300 | |
| 301 | stw_p(&resgrp->hdr.rsp, CLP_RC_OK); |
| 302 | break; |
| 303 | } |
| 304 | default: |
| 305 | DPRINTF("unknown clp command\n"); |
| 306 | stw_p(&resh->rsp, CLP_RC_CMD); |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | out: |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 311 | if (s390_cpu_virt_mem_write(cpu, env->regs[r2], r2, buffer, |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 312 | req_len + res_len)) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 313 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 314 | return 0; |
| 315 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 316 | setcc(cpu, cc); |
| 317 | return 0; |
| 318 | } |
| 319 | |
Pierre Morel | c748814 | 2017-11-30 13:55:24 +0100 | [diff] [blame] | 320 | /** |
| 321 | * Swap data contained in s390x big endian registers to little endian |
| 322 | * PCI bars. |
| 323 | * |
| 324 | * @ptr: a pointer to a uint64_t data field |
| 325 | * @len: the length of the valid data, must be 1,2,4 or 8 |
| 326 | */ |
| 327 | static int zpci_endian_swap(uint64_t *ptr, uint8_t len) |
| 328 | { |
| 329 | uint64_t data = *ptr; |
| 330 | |
| 331 | switch (len) { |
| 332 | case 1: |
| 333 | break; |
| 334 | case 2: |
| 335 | data = bswap16(data); |
| 336 | break; |
| 337 | case 4: |
| 338 | data = bswap32(data); |
| 339 | break; |
| 340 | case 8: |
| 341 | data = bswap64(data); |
| 342 | break; |
| 343 | default: |
| 344 | return -EINVAL; |
| 345 | } |
| 346 | *ptr = data; |
| 347 | return 0; |
| 348 | } |
| 349 | |
Pierre Morel | 4f6482b | 2017-11-30 13:55:30 +0100 | [diff] [blame] | 350 | static MemoryRegion *s390_get_subregion(MemoryRegion *mr, uint64_t offset, |
| 351 | uint8_t len) |
| 352 | { |
| 353 | MemoryRegion *subregion; |
| 354 | uint64_t subregion_size; |
| 355 | |
| 356 | QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) { |
| 357 | subregion_size = int128_get64(subregion->size); |
| 358 | if ((offset >= subregion->addr) && |
| 359 | (offset + len) <= (subregion->addr + subregion_size)) { |
| 360 | mr = subregion; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | return mr; |
| 365 | } |
| 366 | |
Pierre Morel | ab0380c | 2017-11-30 13:55:28 +0100 | [diff] [blame] | 367 | static MemTxResult zpci_read_bar(S390PCIBusDevice *pbdev, uint8_t pcias, |
| 368 | uint64_t offset, uint64_t *data, uint8_t len) |
| 369 | { |
| 370 | MemoryRegion *mr; |
| 371 | |
| 372 | mr = pbdev->pdev->io_regions[pcias].memory; |
Pierre Morel | 4f6482b | 2017-11-30 13:55:30 +0100 | [diff] [blame] | 373 | mr = s390_get_subregion(mr, offset, len); |
| 374 | offset -= mr->addr; |
Pierre Morel | ab0380c | 2017-11-30 13:55:28 +0100 | [diff] [blame] | 375 | return memory_region_dispatch_read(mr, offset, data, len, |
| 376 | MEMTXATTRS_UNSPECIFIED); |
| 377 | } |
| 378 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 379 | int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 380 | { |
| 381 | CPUS390XState *env = &cpu->env; |
| 382 | S390PCIBusDevice *pbdev; |
| 383 | uint64_t offset; |
| 384 | uint64_t data; |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 385 | MemTxResult result; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 386 | uint8_t len; |
| 387 | uint32_t fh; |
| 388 | uint8_t pcias; |
| 389 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 390 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 391 | s390_program_interrupt(env, PGM_PRIVILEGED, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | if (r2 & 0x1) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 396 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | fh = env->regs[r2] >> 32; |
| 401 | pcias = (env->regs[r2] >> 16) & 0xf; |
| 402 | len = env->regs[r2] & 0xf; |
| 403 | offset = env->regs[r2 + 1]; |
| 404 | |
Pierre Morel | 8cbd6aa | 2017-11-30 13:55:26 +0100 | [diff] [blame] | 405 | if (!(fh & FH_MASK_ENABLE)) { |
| 406 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 407 | return 0; |
| 408 | } |
| 409 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 410 | pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 411 | if (!pbdev) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 412 | DPRINTF("pcilg no pci dev\n"); |
| 413 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 414 | return 0; |
| 415 | } |
| 416 | |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 417 | switch (pbdev->state) { |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 418 | case ZPCI_FS_PERMANENT_ERROR: |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 419 | case ZPCI_FS_ERROR: |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 420 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 421 | s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED); |
| 422 | return 0; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 423 | default: |
| 424 | break; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Pierre Morel | 8cbd6aa | 2017-11-30 13:55:26 +0100 | [diff] [blame] | 427 | switch (pcias) { |
| 428 | case ZPCI_IO_BAR_MIN...ZPCI_IO_BAR_MAX: |
| 429 | if (!len || (len > (8 - (offset & 0x7)))) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 430 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 431 | return 0; |
| 432 | } |
Pierre Morel | ab0380c | 2017-11-30 13:55:28 +0100 | [diff] [blame] | 433 | result = zpci_read_bar(pbdev, pcias, offset, &data, len); |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 434 | if (result != MEMTX_OK) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 435 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 436 | return 0; |
| 437 | } |
Pierre Morel | 8cbd6aa | 2017-11-30 13:55:26 +0100 | [diff] [blame] | 438 | break; |
| 439 | case ZPCI_CONFIG_BAR: |
| 440 | if (!len || (len > (4 - (offset & 0x3))) || len == 3) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 441 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | data = pci_host_config_read_common( |
| 445 | pbdev->pdev, offset, pci_config_size(pbdev->pdev), len); |
| 446 | |
Pierre Morel | c748814 | 2017-11-30 13:55:24 +0100 | [diff] [blame] | 447 | if (zpci_endian_swap(&data, len)) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 448 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 449 | return 0; |
| 450 | } |
Pierre Morel | 8cbd6aa | 2017-11-30 13:55:26 +0100 | [diff] [blame] | 451 | break; |
| 452 | default: |
| 453 | DPRINTF("pcilg invalid space\n"); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 454 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 455 | s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | env->regs[r1] = data; |
| 460 | setcc(cpu, ZPCI_PCI_LS_OK); |
| 461 | return 0; |
| 462 | } |
| 463 | |
Pierre Morel | 8af27a9 | 2017-11-30 13:55:29 +0100 | [diff] [blame] | 464 | static MemTxResult zpci_write_bar(S390PCIBusDevice *pbdev, uint8_t pcias, |
| 465 | uint64_t offset, uint64_t data, uint8_t len) |
| 466 | { |
| 467 | MemoryRegion *mr; |
| 468 | |
Pierre Morel | 4f6482b | 2017-11-30 13:55:30 +0100 | [diff] [blame] | 469 | mr = pbdev->pdev->io_regions[pcias].memory; |
| 470 | mr = s390_get_subregion(mr, offset, len); |
| 471 | offset -= mr->addr; |
Pierre Morel | 8af27a9 | 2017-11-30 13:55:29 +0100 | [diff] [blame] | 472 | return memory_region_dispatch_write(mr, offset, data, len, |
| 473 | MEMTXATTRS_UNSPECIFIED); |
| 474 | } |
| 475 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 476 | int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 477 | { |
| 478 | CPUS390XState *env = &cpu->env; |
| 479 | uint64_t offset, data; |
| 480 | S390PCIBusDevice *pbdev; |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 481 | MemTxResult result; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 482 | uint8_t len; |
| 483 | uint32_t fh; |
| 484 | uint8_t pcias; |
| 485 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 486 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 487 | s390_program_interrupt(env, PGM_PRIVILEGED, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | if (r2 & 0x1) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 492 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | fh = env->regs[r2] >> 32; |
| 497 | pcias = (env->regs[r2] >> 16) & 0xf; |
| 498 | len = env->regs[r2] & 0xf; |
| 499 | offset = env->regs[r2 + 1]; |
Pierre Morel | 7645b9a | 2017-11-30 13:55:25 +0100 | [diff] [blame] | 500 | data = env->regs[r1]; |
| 501 | |
| 502 | if (!(fh & FH_MASK_ENABLE)) { |
| 503 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 504 | return 0; |
| 505 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 506 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 507 | pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 508 | if (!pbdev) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 509 | DPRINTF("pcistg no pci dev\n"); |
| 510 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 511 | return 0; |
| 512 | } |
| 513 | |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 514 | switch (pbdev->state) { |
Pierre Morel | 7645b9a | 2017-11-30 13:55:25 +0100 | [diff] [blame] | 515 | /* ZPCI_FS_RESERVED, ZPCI_FS_STANDBY and ZPCI_FS_DISABLED |
| 516 | * are already covered by the FH_MASK_ENABLE check above |
| 517 | */ |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 518 | case ZPCI_FS_PERMANENT_ERROR: |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 519 | case ZPCI_FS_ERROR: |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 520 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 521 | s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED); |
| 522 | return 0; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 523 | default: |
| 524 | break; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Pierre Morel | 7645b9a | 2017-11-30 13:55:25 +0100 | [diff] [blame] | 527 | switch (pcias) { |
| 528 | /* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */ |
| 529 | case ZPCI_IO_BAR_MIN...ZPCI_IO_BAR_MAX: |
| 530 | /* Check length: |
| 531 | * A length of 0 is invalid and length should not cross a double word |
| 532 | */ |
| 533 | if (!len || (len > (8 - (offset & 0x7)))) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 534 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 535 | return 0; |
| 536 | } |
Pierre Morel | 205e5de | 2016-08-24 12:46:34 +0200 | [diff] [blame] | 537 | |
Pierre Morel | 8af27a9 | 2017-11-30 13:55:29 +0100 | [diff] [blame] | 538 | result = zpci_write_bar(pbdev, pcias, offset, data, len); |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 539 | if (result != MEMTX_OK) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 540 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 541 | return 0; |
| 542 | } |
Pierre Morel | 7645b9a | 2017-11-30 13:55:25 +0100 | [diff] [blame] | 543 | break; |
| 544 | case ZPCI_CONFIG_BAR: |
| 545 | /* ZPCI uses the pseudo BAR number 15 as configuration space */ |
| 546 | /* possible access lengths are 1,2,4 and must not cross a word */ |
| 547 | if (!len || (len > (4 - (offset & 0x3))) || len == 3) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 548 | s390_program_interrupt(env, PGM_OPERAND, 4, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 549 | return 0; |
| 550 | } |
Pierre Morel | 7645b9a | 2017-11-30 13:55:25 +0100 | [diff] [blame] | 551 | /* len = 1,2,4 so we do not need to test */ |
| 552 | zpci_endian_swap(&data, len); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 553 | pci_host_config_write_common(pbdev->pdev, offset, |
| 554 | pci_config_size(pbdev->pdev), |
| 555 | data, len); |
Pierre Morel | 7645b9a | 2017-11-30 13:55:25 +0100 | [diff] [blame] | 556 | break; |
| 557 | default: |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 558 | DPRINTF("pcistg invalid space\n"); |
| 559 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 560 | s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS); |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | setcc(cpu, ZPCI_PCI_LS_OK); |
| 565 | return 0; |
| 566 | } |
| 567 | |
Yi Min Zhao | b3f05d8 | 2018-02-05 15:22:57 +0800 | [diff] [blame] | 568 | static void s390_pci_update_iotlb(S390PCIIOMMU *iommu, S390IOTLBEntry *entry) |
| 569 | { |
| 570 | S390IOTLBEntry *cache = g_hash_table_lookup(iommu->iotlb, &entry->iova); |
| 571 | IOMMUTLBEntry notify = { |
| 572 | .target_as = &address_space_memory, |
| 573 | .iova = entry->iova, |
| 574 | .translated_addr = entry->translated_addr, |
| 575 | .perm = entry->perm, |
| 576 | .addr_mask = ~PAGE_MASK, |
| 577 | }; |
| 578 | |
| 579 | if (entry->perm == IOMMU_NONE) { |
| 580 | if (!cache) { |
| 581 | return; |
| 582 | } |
| 583 | g_hash_table_remove(iommu->iotlb, &entry->iova); |
| 584 | } else { |
| 585 | if (cache) { |
| 586 | if (cache->perm == entry->perm && |
| 587 | cache->translated_addr == entry->translated_addr) { |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | notify.perm = IOMMU_NONE; |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame] | 592 | memory_region_notify_iommu(&iommu->iommu_mr, 0, notify); |
Yi Min Zhao | b3f05d8 | 2018-02-05 15:22:57 +0800 | [diff] [blame] | 593 | notify.perm = entry->perm; |
| 594 | } |
| 595 | |
| 596 | cache = g_new(S390IOTLBEntry, 1); |
| 597 | cache->iova = entry->iova; |
| 598 | cache->translated_addr = entry->translated_addr; |
| 599 | cache->len = PAGE_SIZE; |
| 600 | cache->perm = entry->perm; |
| 601 | g_hash_table_replace(iommu->iotlb, &cache->iova, cache); |
| 602 | } |
| 603 | |
Peter Maydell | cb1efcf | 2018-06-15 14:57:16 +0100 | [diff] [blame] | 604 | memory_region_notify_iommu(&iommu->iommu_mr, 0, notify); |
Yi Min Zhao | b3f05d8 | 2018-02-05 15:22:57 +0800 | [diff] [blame] | 605 | } |
| 606 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 607 | int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 608 | { |
| 609 | CPUS390XState *env = &cpu->env; |
| 610 | uint32_t fh; |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 611 | uint16_t error = 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 612 | S390PCIBusDevice *pbdev; |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 613 | S390PCIIOMMU *iommu; |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 614 | S390IOTLBEntry entry; |
Yi Min Zhao | 4e99a0f | 2015-01-19 15:15:56 +0800 | [diff] [blame] | 615 | hwaddr start, end; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 616 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 617 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 618 | s390_program_interrupt(env, PGM_PRIVILEGED, 4, ra); |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 619 | return 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | if (r2 & 0x1) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 623 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 624 | return 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | fh = env->regs[r1] >> 32; |
Yi Min Zhao | 4e99a0f | 2015-01-19 15:15:56 +0800 | [diff] [blame] | 628 | start = env->regs[r2]; |
| 629 | end = start + env->regs[r2 + 1]; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 630 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 631 | pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 632 | if (!pbdev) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 633 | DPRINTF("rpcit no pci dev\n"); |
| 634 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 635 | return 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 636 | } |
| 637 | |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 638 | switch (pbdev->state) { |
| 639 | case ZPCI_FS_RESERVED: |
| 640 | case ZPCI_FS_STANDBY: |
| 641 | case ZPCI_FS_DISABLED: |
| 642 | case ZPCI_FS_PERMANENT_ERROR: |
| 643 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 644 | return 0; |
| 645 | case ZPCI_FS_ERROR: |
| 646 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 647 | s390_set_status_code(env, r1, ZPCI_MOD_ST_ERROR_RECOVER); |
| 648 | return 0; |
| 649 | default: |
| 650 | break; |
| 651 | } |
| 652 | |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 653 | iommu = pbdev->iommu; |
| 654 | if (!iommu->g_iota) { |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 655 | error = ERR_EVENT_INVALAS; |
| 656 | goto err; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 657 | } |
| 658 | |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 659 | if (end < iommu->pba || start > iommu->pal) { |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 660 | error = ERR_EVENT_OORANGE; |
| 661 | goto err; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 662 | } |
| 663 | |
Yi Min Zhao | 4e99a0f | 2015-01-19 15:15:56 +0800 | [diff] [blame] | 664 | while (start < end) { |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 665 | error = s390_guest_io_table_walk(iommu->g_iota, start, &entry); |
| 666 | if (error) { |
| 667 | break; |
Yi Min Zhao | 4e99a0f | 2015-01-19 15:15:56 +0800 | [diff] [blame] | 668 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 669 | |
Yi Min Zhao | b3f05d8 | 2018-02-05 15:22:57 +0800 | [diff] [blame] | 670 | start += entry.len; |
| 671 | while (entry.iova < start && entry.iova < end) { |
| 672 | s390_pci_update_iotlb(iommu, &entry); |
| 673 | entry.iova += PAGE_SIZE; |
| 674 | entry.translated_addr += PAGE_SIZE; |
| 675 | } |
| 676 | } |
Yi Min Zhao | 0125861 | 2018-02-05 15:22:56 +0800 | [diff] [blame] | 677 | err: |
| 678 | if (error) { |
| 679 | pbdev->state = ZPCI_FS_ERROR; |
| 680 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 681 | s390_set_status_code(env, r1, ZPCI_PCI_ST_FUNC_IN_ERR); |
| 682 | s390_pci_generate_error_event(error, pbdev->fh, pbdev->fid, start, 0); |
| 683 | } else { |
| 684 | setcc(cpu, ZPCI_PCI_LS_OK); |
| 685 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 689 | int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 690 | uint8_t ar, uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 691 | { |
| 692 | CPUS390XState *env = &cpu->env; |
| 693 | S390PCIBusDevice *pbdev; |
| 694 | MemoryRegion *mr; |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 695 | MemTxResult result; |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 696 | uint64_t offset; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 697 | int i; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 698 | uint32_t fh; |
| 699 | uint8_t pcias; |
| 700 | uint8_t len; |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 701 | uint8_t buffer[128]; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 702 | |
| 703 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 704 | s390_program_interrupt(env, PGM_PRIVILEGED, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | fh = env->regs[r1] >> 32; |
| 709 | pcias = (env->regs[r1] >> 16) & 0xf; |
| 710 | len = env->regs[r1] & 0xff; |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 711 | offset = env->regs[r3]; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 712 | |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 713 | if (!(fh & FH_MASK_ENABLE)) { |
| 714 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 718 | pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 719 | if (!pbdev) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 720 | DPRINTF("pcistb no pci dev fh 0x%x\n", fh); |
| 721 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 722 | return 0; |
| 723 | } |
| 724 | |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 725 | switch (pbdev->state) { |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 726 | case ZPCI_FS_PERMANENT_ERROR: |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 727 | case ZPCI_FS_ERROR: |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 728 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 729 | s390_set_status_code(env, r1, ZPCI_PCI_ST_BLOCKED); |
| 730 | return 0; |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 731 | default: |
| 732 | break; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 735 | if (pcias > ZPCI_IO_BAR_MAX) { |
| 736 | DPRINTF("pcistb invalid space\n"); |
| 737 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 738 | s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | /* Verify the address, offset and length */ |
| 743 | /* offset must be a multiple of 8 */ |
| 744 | if (offset % 8) { |
| 745 | goto specification_error; |
| 746 | } |
| 747 | /* Length must be greater than 8, a multiple of 8 */ |
| 748 | /* and not greater than maxstbl */ |
| 749 | if ((len <= 8) || (len % 8) || (len > pbdev->maxstbl)) { |
| 750 | goto specification_error; |
| 751 | } |
| 752 | /* Do not cross a 4K-byte boundary */ |
| 753 | if (((offset & 0xfff) + len) > 0x1000) { |
| 754 | goto specification_error; |
| 755 | } |
| 756 | /* Guest address must be double word aligned */ |
| 757 | if (gaddr & 0x07UL) { |
| 758 | goto specification_error; |
| 759 | } |
| 760 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 761 | mr = pbdev->pdev->io_regions[pcias].memory; |
Pierre Morel | 4f6482b | 2017-11-30 13:55:30 +0100 | [diff] [blame] | 762 | mr = s390_get_subregion(mr, offset, len); |
| 763 | offset -= mr->addr; |
| 764 | |
Peter Maydell | 6d7b9a6 | 2018-05-31 14:50:52 +0100 | [diff] [blame] | 765 | if (!memory_region_access_valid(mr, offset, len, true, |
| 766 | MEMTXATTRS_UNSPECIFIED)) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 767 | s390_program_interrupt(env, PGM_OPERAND, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 768 | return 0; |
| 769 | } |
| 770 | |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 771 | if (s390_cpu_virt_mem_read(cpu, gaddr, ar, buffer, len)) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 772 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 776 | for (i = 0; i < len / 8; i++) { |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 777 | result = memory_region_dispatch_write(mr, offset + i * 8, |
| 778 | ldq_p(buffer + i * 8), 8, |
| 779 | MEMTXATTRS_UNSPECIFIED); |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 780 | if (result != MEMTX_OK) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 781 | s390_program_interrupt(env, PGM_OPERAND, 6, ra); |
Pierre Morel | 88ee13c | 2016-08-24 12:53:31 +0200 | [diff] [blame] | 782 | return 0; |
| 783 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | setcc(cpu, ZPCI_PCI_LS_OK); |
| 787 | return 0; |
Pierre Morel | 0e7c259 | 2017-11-30 13:55:27 +0100 | [diff] [blame] | 788 | |
| 789 | specification_error: |
| 790 | s390_program_interrupt(env, PGM_SPECIFICATION, 6, ra); |
| 791 | return 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static int reg_irqs(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib) |
| 795 | { |
Yi Min Zhao | 8581c11 | 2016-01-28 13:26:43 +0800 | [diff] [blame] | 796 | int ret, len; |
Fei Li | dde522b | 2016-11-24 11:10:39 +0100 | [diff] [blame] | 797 | uint8_t isc = FIB_DATA_ISC(ldl_p(&fib.data)); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 798 | |
Fei Li | dde522b | 2016-11-24 11:10:39 +0100 | [diff] [blame] | 799 | pbdev->routes.adapter.adapter_id = css_get_adapter_id( |
| 800 | CSS_IO_ADAPTER_PCI, isc); |
Yi Min Zhao | 8581c11 | 2016-01-28 13:26:43 +0800 | [diff] [blame] | 801 | pbdev->summary_ind = get_indicator(ldq_p(&fib.aisb), sizeof(uint64_t)); |
| 802 | len = BITS_TO_LONGS(FIB_DATA_NOI(ldl_p(&fib.data))) * sizeof(unsigned long); |
| 803 | pbdev->indicator = get_indicator(ldq_p(&fib.aibv), len); |
| 804 | |
Yi Min Zhao | bac45d5 | 2016-04-26 19:26:32 +0800 | [diff] [blame] | 805 | ret = map_indicator(&pbdev->routes.adapter, pbdev->summary_ind); |
| 806 | if (ret) { |
| 807 | goto out; |
| 808 | } |
| 809 | |
| 810 | ret = map_indicator(&pbdev->routes.adapter, pbdev->indicator); |
| 811 | if (ret) { |
| 812 | goto out; |
| 813 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 814 | |
| 815 | pbdev->routes.adapter.summary_addr = ldq_p(&fib.aisb); |
| 816 | pbdev->routes.adapter.summary_offset = FIB_DATA_AISBO(ldl_p(&fib.data)); |
| 817 | pbdev->routes.adapter.ind_addr = ldq_p(&fib.aibv); |
| 818 | pbdev->routes.adapter.ind_offset = FIB_DATA_AIBVO(ldl_p(&fib.data)); |
Fei Li | dde522b | 2016-11-24 11:10:39 +0100 | [diff] [blame] | 819 | pbdev->isc = isc; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 820 | pbdev->noi = FIB_DATA_NOI(ldl_p(&fib.data)); |
| 821 | pbdev->sum = FIB_DATA_SUM(ldl_p(&fib.data)); |
| 822 | |
| 823 | DPRINTF("reg_irqs adapter id %d\n", pbdev->routes.adapter.adapter_id); |
| 824 | return 0; |
Yi Min Zhao | bac45d5 | 2016-04-26 19:26:32 +0800 | [diff] [blame] | 825 | out: |
| 826 | release_indicator(&pbdev->routes.adapter, pbdev->summary_ind); |
| 827 | release_indicator(&pbdev->routes.adapter, pbdev->indicator); |
| 828 | pbdev->summary_ind = NULL; |
| 829 | pbdev->indicator = NULL; |
| 830 | return ret; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 831 | } |
| 832 | |
Yi Min Zhao | e141dba | 2016-04-26 14:50:16 +0800 | [diff] [blame] | 833 | int pci_dereg_irqs(S390PCIBusDevice *pbdev) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 834 | { |
Yi Min Zhao | 8581c11 | 2016-01-28 13:26:43 +0800 | [diff] [blame] | 835 | release_indicator(&pbdev->routes.adapter, pbdev->summary_ind); |
| 836 | release_indicator(&pbdev->routes.adapter, pbdev->indicator); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 837 | |
Yi Min Zhao | 8581c11 | 2016-01-28 13:26:43 +0800 | [diff] [blame] | 838 | pbdev->summary_ind = NULL; |
| 839 | pbdev->indicator = NULL; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 840 | pbdev->routes.adapter.summary_addr = 0; |
| 841 | pbdev->routes.adapter.summary_offset = 0; |
| 842 | pbdev->routes.adapter.ind_addr = 0; |
| 843 | pbdev->routes.adapter.ind_offset = 0; |
| 844 | pbdev->isc = 0; |
| 845 | pbdev->noi = 0; |
| 846 | pbdev->sum = 0; |
| 847 | |
| 848 | DPRINTF("dereg_irqs adapter id %d\n", pbdev->routes.adapter.adapter_id); |
| 849 | return 0; |
| 850 | } |
| 851 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 852 | static int reg_ioat(CPUS390XState *env, S390PCIIOMMU *iommu, ZpciFib fib, |
| 853 | uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 854 | { |
| 855 | uint64_t pba = ldq_p(&fib.pba); |
| 856 | uint64_t pal = ldq_p(&fib.pal); |
| 857 | uint64_t g_iota = ldq_p(&fib.iota); |
| 858 | uint8_t dt = (g_iota >> 2) & 0x7; |
| 859 | uint8_t t = (g_iota >> 11) & 0x1; |
| 860 | |
Yi Min Zhao | f9125e3 | 2018-02-05 15:22:58 +0800 | [diff] [blame] | 861 | pba &= ~0xfff; |
| 862 | pal |= 0xfff; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 863 | if (pba > pal || pba < ZPCI_SDMA_ADDR || pal > ZPCI_EDMA_ADDR) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 864 | s390_program_interrupt(env, PGM_OPERAND, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 865 | return -EINVAL; |
| 866 | } |
| 867 | |
| 868 | /* currently we only support designation type 1 with translation */ |
| 869 | if (!(dt == ZPCI_IOTA_RTTO && t)) { |
| 870 | error_report("unsupported ioat dt %d t %d", dt, t); |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 871 | s390_program_interrupt(env, PGM_OPERAND, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 872 | return -EINVAL; |
| 873 | } |
| 874 | |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 875 | iommu->pba = pba; |
| 876 | iommu->pal = pal; |
| 877 | iommu->g_iota = g_iota; |
Yi Min Zhao | f0a399d | 2015-11-04 15:50:45 +0800 | [diff] [blame] | 878 | |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 879 | s390_pci_iommu_enable(iommu); |
Yi Min Zhao | f0a399d | 2015-11-04 15:50:45 +0800 | [diff] [blame] | 880 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 881 | return 0; |
| 882 | } |
| 883 | |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 884 | void pci_dereg_ioat(S390PCIIOMMU *iommu) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 885 | { |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 886 | s390_pci_iommu_disable(iommu); |
| 887 | iommu->pba = 0; |
| 888 | iommu->pal = 0; |
| 889 | iommu->g_iota = 0; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 890 | } |
| 891 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 892 | int mpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar, |
| 893 | uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 894 | { |
| 895 | CPUS390XState *env = &cpu->env; |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 896 | uint8_t oc, dmaas; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 897 | uint32_t fh; |
| 898 | ZpciFib fib; |
| 899 | S390PCIBusDevice *pbdev; |
| 900 | uint64_t cc = ZPCI_PCI_LS_OK; |
| 901 | |
| 902 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 903 | s390_program_interrupt(env, PGM_PRIVILEGED, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | oc = env->regs[r1] & 0xff; |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 908 | dmaas = (env->regs[r1] >> 16) & 0xff; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 909 | fh = env->regs[r1] >> 32; |
| 910 | |
| 911 | if (fiba & 0x7) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 912 | s390_program_interrupt(env, PGM_SPECIFICATION, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 916 | pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 917 | if (!pbdev) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 918 | DPRINTF("mpcifc no pci dev fh 0x%x\n", fh); |
| 919 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 920 | return 0; |
| 921 | } |
| 922 | |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 923 | switch (pbdev->state) { |
| 924 | case ZPCI_FS_RESERVED: |
| 925 | case ZPCI_FS_STANDBY: |
| 926 | case ZPCI_FS_DISABLED: |
| 927 | case ZPCI_FS_PERMANENT_ERROR: |
| 928 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 929 | return 0; |
| 930 | default: |
| 931 | break; |
| 932 | } |
| 933 | |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 934 | if (s390_cpu_virt_mem_read(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 935 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 936 | return 0; |
| 937 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 938 | |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 939 | if (fib.fmt != 0) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 940 | s390_program_interrupt(env, PGM_OPERAND, 6, ra); |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 944 | switch (oc) { |
| 945 | case ZPCI_MOD_FC_REG_INT: |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 946 | if (pbdev->summary_ind) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 947 | cc = ZPCI_PCI_LS_ERR; |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 948 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
| 949 | } else if (reg_irqs(env, pbdev, fib)) { |
| 950 | cc = ZPCI_PCI_LS_ERR; |
| 951 | s390_set_status_code(env, r1, ZPCI_MOD_ST_RES_NOT_AVAIL); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 952 | } |
| 953 | break; |
| 954 | case ZPCI_MOD_FC_DEREG_INT: |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 955 | if (!pbdev->summary_ind) { |
| 956 | cc = ZPCI_PCI_LS_ERR; |
| 957 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
| 958 | } else { |
| 959 | pci_dereg_irqs(pbdev); |
| 960 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 961 | break; |
| 962 | case ZPCI_MOD_FC_REG_IOAT: |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 963 | if (dmaas != 0) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 964 | cc = ZPCI_PCI_LS_ERR; |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 965 | s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL); |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 966 | } else if (pbdev->iommu->enabled) { |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 967 | cc = ZPCI_PCI_LS_ERR; |
| 968 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 969 | } else if (reg_ioat(env, pbdev->iommu, fib, ra)) { |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 970 | cc = ZPCI_PCI_LS_ERR; |
| 971 | s390_set_status_code(env, r1, ZPCI_MOD_ST_INSUF_RES); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 972 | } |
| 973 | break; |
| 974 | case ZPCI_MOD_FC_DEREG_IOAT: |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 975 | if (dmaas != 0) { |
| 976 | cc = ZPCI_PCI_LS_ERR; |
| 977 | s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL); |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 978 | } else if (!pbdev->iommu->enabled) { |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 979 | cc = ZPCI_PCI_LS_ERR; |
| 980 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
| 981 | } else { |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 982 | pci_dereg_ioat(pbdev->iommu); |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 983 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 984 | break; |
| 985 | case ZPCI_MOD_FC_REREG_IOAT: |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 986 | if (dmaas != 0) { |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 987 | cc = ZPCI_PCI_LS_ERR; |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 988 | s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL); |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 989 | } else if (!pbdev->iommu->enabled) { |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 990 | cc = ZPCI_PCI_LS_ERR; |
| 991 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
| 992 | } else { |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 993 | pci_dereg_ioat(pbdev->iommu); |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 994 | if (reg_ioat(env, pbdev->iommu, fib, ra)) { |
Yi Min Zhao | a6d9d4f | 2016-04-28 13:24:07 +0800 | [diff] [blame] | 995 | cc = ZPCI_PCI_LS_ERR; |
| 996 | s390_set_status_code(env, r1, ZPCI_MOD_ST_INSUF_RES); |
| 997 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 998 | } |
| 999 | break; |
| 1000 | case ZPCI_MOD_FC_RESET_ERROR: |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 1001 | switch (pbdev->state) { |
| 1002 | case ZPCI_FS_BLOCKED: |
| 1003 | case ZPCI_FS_ERROR: |
| 1004 | pbdev->state = ZPCI_FS_ENABLED; |
| 1005 | break; |
| 1006 | default: |
| 1007 | cc = ZPCI_PCI_LS_ERR; |
| 1008 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
| 1009 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1010 | break; |
| 1011 | case ZPCI_MOD_FC_RESET_BLOCK: |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 1012 | switch (pbdev->state) { |
| 1013 | case ZPCI_FS_ERROR: |
| 1014 | pbdev->state = ZPCI_FS_BLOCKED; |
| 1015 | break; |
| 1016 | default: |
| 1017 | cc = ZPCI_PCI_LS_ERR; |
| 1018 | s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE); |
| 1019 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1020 | break; |
| 1021 | case ZPCI_MOD_FC_SET_MEASURE: |
| 1022 | pbdev->fmb_addr = ldq_p(&fib.fmb_addr); |
| 1023 | break; |
| 1024 | default: |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 1025 | s390_program_interrupt(&cpu->env, PGM_OPERAND, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1026 | cc = ZPCI_PCI_LS_ERR; |
| 1027 | } |
| 1028 | |
| 1029 | setcc(cpu, cc); |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 1033 | int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar, |
| 1034 | uintptr_t ra) |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1035 | { |
| 1036 | CPUS390XState *env = &cpu->env; |
Yi Min Zhao | 0a608a6 | 2016-06-15 17:09:10 +0800 | [diff] [blame] | 1037 | uint8_t dmaas; |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1038 | uint32_t fh; |
| 1039 | ZpciFib fib; |
| 1040 | S390PCIBusDevice *pbdev; |
| 1041 | uint32_t data; |
| 1042 | uint64_t cc = ZPCI_PCI_LS_OK; |
| 1043 | |
| 1044 | if (env->psw.mask & PSW_MASK_PSTATE) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 1045 | s390_program_interrupt(env, PGM_PRIVILEGED, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | fh = env->regs[r1] >> 32; |
Yi Min Zhao | 0a608a6 | 2016-06-15 17:09:10 +0800 | [diff] [blame] | 1050 | dmaas = (env->regs[r1] >> 16) & 0xff; |
| 1051 | |
| 1052 | if (dmaas) { |
| 1053 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 1054 | s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_INVAL_DMAAS); |
| 1055 | return 0; |
| 1056 | } |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1057 | |
| 1058 | if (fiba & 0x7) { |
David Hildenbrand | 468a938 | 2017-11-30 17:27:33 +0100 | [diff] [blame] | 1059 | s390_program_interrupt(env, PGM_SPECIFICATION, 6, ra); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1060 | return 0; |
| 1061 | } |
| 1062 | |
Yi Min Zhao | a975a24 | 2016-11-23 11:08:29 +0800 | [diff] [blame] | 1063 | pbdev = s390_pci_find_dev_by_idx(s390_get_phb(), fh & FH_MASK_INDEX); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1064 | if (!pbdev) { |
| 1065 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | memset(&fib, 0, sizeof(fib)); |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 1070 | |
| 1071 | switch (pbdev->state) { |
| 1072 | case ZPCI_FS_RESERVED: |
| 1073 | case ZPCI_FS_STANDBY: |
| 1074 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 1075 | return 0; |
| 1076 | case ZPCI_FS_DISABLED: |
| 1077 | if (fh & FH_MASK_ENABLE) { |
| 1078 | setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE); |
| 1079 | return 0; |
| 1080 | } |
| 1081 | goto out; |
| 1082 | /* BLOCKED bit is set to one coincident with the setting of ERROR bit. |
| 1083 | * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */ |
| 1084 | case ZPCI_FS_ERROR: |
| 1085 | fib.fc |= 0x20; |
| 1086 | case ZPCI_FS_BLOCKED: |
| 1087 | fib.fc |= 0x40; |
| 1088 | case ZPCI_FS_ENABLED: |
| 1089 | fib.fc |= 0x80; |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 1090 | if (pbdev->iommu->enabled) { |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 1091 | fib.fc |= 0x10; |
| 1092 | } |
| 1093 | if (!(fh & FH_MASK_ENABLE)) { |
| 1094 | env->regs[r1] |= 1ULL << 63; |
| 1095 | } |
| 1096 | break; |
| 1097 | case ZPCI_FS_PERMANENT_ERROR: |
| 1098 | setcc(cpu, ZPCI_PCI_LS_ERR); |
| 1099 | s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_PERM_ERROR); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Yi Min Zhao | de91ea9 | 2016-12-08 13:02:24 +0800 | [diff] [blame] | 1103 | stq_p(&fib.pba, pbdev->iommu->pba); |
| 1104 | stq_p(&fib.pal, pbdev->iommu->pal); |
| 1105 | stq_p(&fib.iota, pbdev->iommu->g_iota); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1106 | stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr); |
| 1107 | stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr); |
| 1108 | stq_p(&fib.fmb_addr, pbdev->fmb_addr); |
| 1109 | |
Frank Blaschka | c0eb33a | 2015-01-21 16:50:29 +0100 | [diff] [blame] | 1110 | data = ((uint32_t)pbdev->isc << 28) | ((uint32_t)pbdev->noi << 16) | |
| 1111 | ((uint32_t)pbdev->routes.adapter.ind_offset << 8) | |
| 1112 | ((uint32_t)pbdev->sum << 7) | pbdev->routes.adapter.summary_offset; |
| 1113 | stl_p(&fib.data, data); |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1114 | |
Yi Min Zhao | 5d1abf2 | 2016-04-19 15:03:13 +0800 | [diff] [blame] | 1115 | out: |
Alexander Yarygin | 6cb1e49 | 2015-03-05 12:36:48 +0300 | [diff] [blame] | 1116 | if (s390_cpu_virt_mem_write(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) { |
David Hildenbrand | 98ee9be | 2017-11-30 17:27:35 +0100 | [diff] [blame] | 1117 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
Frank Blaschka | 63ceef6 | 2015-02-12 18:09:41 +0100 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Frank Blaschka | 863f6f5 | 2015-01-09 09:04:39 +0100 | [diff] [blame] | 1121 | setcc(cpu, cc); |
| 1122 | return 0; |
| 1123 | } |