1bbf5c878SMichael Roth /* 2bbf5c878SMichael Roth * QEMU SPAPR Dynamic Reconfiguration Connector Implementation 3bbf5c878SMichael Roth * 4bbf5c878SMichael Roth * Copyright IBM Corp. 2014 5bbf5c878SMichael Roth * 6bbf5c878SMichael Roth * Authors: 7bbf5c878SMichael Roth * Michael Roth <mdroth@linux.vnet.ibm.com> 8bbf5c878SMichael Roth * 9bbf5c878SMichael Roth * This work is licensed under the terms of the GNU GPL, version 2 or later. 10bbf5c878SMichael Roth * See the COPYING file in the top-level directory. 11bbf5c878SMichael Roth */ 12bbf5c878SMichael Roth 130d75590dSPeter Maydell #include "qemu/osdep.h" 14da34e65cSMarkus Armbruster #include "qapi/error.h" 154771d756SPaolo Bonzini #include "cpu.h" 16f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 17bbf5c878SMichael Roth #include "hw/ppc/spapr_drc.h" 18bbf5c878SMichael Roth #include "qom/object.h" 19bbf5c878SMichael Roth #include "hw/qdev.h" 20bbf5c878SMichael Roth #include "qapi/visitor.h" 21bbf5c878SMichael Roth #include "qemu/error-report.h" 220cb688d2SMichael Roth #include "hw/ppc/spapr.h" /* for RTAS return codes */ 2331834723SDaniel Henrique Barboza #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */ 2424ac7755SLaurent Vivier #include "trace.h" 25bbf5c878SMichael Roth 26bbf5c878SMichael Roth #define DRC_CONTAINER_PATH "/dr-connector" 27bbf5c878SMichael Roth #define DRC_INDEX_TYPE_SHIFT 28 28627c2ef7SDavid Gibson #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1) 29bbf5c878SMichael Roth 30b89b3d39SDavid Gibson static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr, 31b89b3d39SDavid Gibson uint32_t drc_index) 32b89b3d39SDavid Gibson { 33b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs = NULL; 34b89b3d39SDavid Gibson 35b89b3d39SDavid Gibson QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) { 36b89b3d39SDavid Gibson if (ccs->drc_index == drc_index) { 37b89b3d39SDavid Gibson break; 38b89b3d39SDavid Gibson } 39b89b3d39SDavid Gibson } 40b89b3d39SDavid Gibson 41b89b3d39SDavid Gibson return ccs; 42b89b3d39SDavid Gibson } 43b89b3d39SDavid Gibson 44b89b3d39SDavid Gibson static void spapr_ccs_add(sPAPRMachineState *spapr, 45b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs) 46b89b3d39SDavid Gibson { 47b89b3d39SDavid Gibson g_assert(!spapr_ccs_find(spapr, ccs->drc_index)); 48b89b3d39SDavid Gibson QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next); 49b89b3d39SDavid Gibson } 50b89b3d39SDavid Gibson 51b89b3d39SDavid Gibson static void spapr_ccs_remove(sPAPRMachineState *spapr, 52b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs) 53b89b3d39SDavid Gibson { 54b89b3d39SDavid Gibson QTAILQ_REMOVE(&spapr->ccs_list, ccs, next); 55b89b3d39SDavid Gibson g_free(ccs); 56b89b3d39SDavid Gibson } 57b89b3d39SDavid Gibson 58bbf5c878SMichael Roth static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type) 59bbf5c878SMichael Roth { 60bbf5c878SMichael Roth uint32_t shift = 0; 61bbf5c878SMichael Roth 62bbf5c878SMichael Roth /* make sure this isn't SPAPR_DR_CONNECTOR_TYPE_ANY, or some 63bbf5c878SMichael Roth * other wonky value. 64bbf5c878SMichael Roth */ 65bbf5c878SMichael Roth g_assert(is_power_of_2(type)); 66bbf5c878SMichael Roth 67bbf5c878SMichael Roth while (type != (1 << shift)) { 68bbf5c878SMichael Roth shift++; 69bbf5c878SMichael Roth } 70bbf5c878SMichael Roth return shift; 71bbf5c878SMichael Roth } 72bbf5c878SMichael Roth 73*0b55aa91SDavid Gibson uint32_t spapr_drc_index(sPAPRDRConnector *drc) 74bbf5c878SMichael Roth { 75bbf5c878SMichael Roth /* no set format for a drc index: it only needs to be globally 76bbf5c878SMichael Roth * unique. this is how we encode the DRC type on bare-metal 77bbf5c878SMichael Roth * however, so might as well do that here 78bbf5c878SMichael Roth */ 79bbf5c878SMichael Roth return (get_type_shift(drc->type) << DRC_INDEX_TYPE_SHIFT) | 80bbf5c878SMichael Roth (drc->id & DRC_INDEX_ID_MASK); 81bbf5c878SMichael Roth } 82bbf5c878SMichael Roth 830cb688d2SMichael Roth static uint32_t set_isolation_state(sPAPRDRConnector *drc, 84bbf5c878SMichael Roth sPAPRDRIsolationState state) 85bbf5c878SMichael Roth { 86bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 87bbf5c878SMichael Roth 88*0b55aa91SDavid Gibson trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state); 89bbf5c878SMichael Roth 909d1852ceSMichael Roth if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) { 91b12227afSStefan Weil /* cannot unisolate a non-existent resource, and, or resources 929d1852ceSMichael Roth * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5) 939d1852ceSMichael Roth */ 949d1852ceSMichael Roth if (!drc->dev || 959d1852ceSMichael Roth drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 969d1852ceSMichael Roth return RTAS_OUT_NO_SUCH_INDICATOR; 979d1852ceSMichael Roth } 989d1852ceSMichael Roth } 999d1852ceSMichael Roth 100cf632463SBharata B Rao /* 101cf632463SBharata B Rao * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't 102cf632463SBharata B Rao * belong to a DIMM device that is marked for removal. 103cf632463SBharata B Rao * 104cf632463SBharata B Rao * Currently the guest userspace tool drmgr that drives the memory 105cf632463SBharata B Rao * hotplug/unplug will just try to remove a set of 'removable' LMBs 106cf632463SBharata B Rao * in response to a hot unplug request that is based on drc-count. 107cf632463SBharata B Rao * If the LMB being removed doesn't belong to a DIMM device that is 108cf632463SBharata B Rao * actually being unplugged, fail the isolation request here. 109cf632463SBharata B Rao */ 110cf632463SBharata B Rao if (drc->type == SPAPR_DR_CONNECTOR_TYPE_LMB) { 111cf632463SBharata B Rao if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) && 112cf632463SBharata B Rao !drc->awaiting_release) { 113cf632463SBharata B Rao return RTAS_OUT_HW_ERROR; 114cf632463SBharata B Rao } 115cf632463SBharata B Rao } 116cf632463SBharata B Rao 117bbf5c878SMichael Roth drc->isolation_state = state; 118bbf5c878SMichael Roth 119bbf5c878SMichael Roth if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) { 120bbf5c878SMichael Roth /* if we're awaiting release, but still in an unconfigured state, 121bbf5c878SMichael Roth * it's likely the guest is still in the process of configuring 122bbf5c878SMichael Roth * the device and is transitioning the devices to an ISOLATED 123bbf5c878SMichael Roth * state as a part of that process. so we only complete the 124bbf5c878SMichael Roth * removal when this transition happens for a device in a 125bbf5c878SMichael Roth * configured state, as suggested by the state diagram from 126bbf5c878SMichael Roth * PAPR+ 2.7, 13.4 127bbf5c878SMichael Roth */ 128bbf5c878SMichael Roth if (drc->awaiting_release) { 129*0b55aa91SDavid Gibson uint32_t drc_index = spapr_drc_index(drc); 130bbf5c878SMichael Roth if (drc->configured) { 131*0b55aa91SDavid Gibson trace_spapr_drc_set_isolation_state_finalizing(drc_index); 13231834723SDaniel Henrique Barboza drck->detach(drc, DEVICE(drc->dev), NULL); 133bbf5c878SMichael Roth } else { 134*0b55aa91SDavid Gibson trace_spapr_drc_set_isolation_state_deferring(drc_index); 135bbf5c878SMichael Roth } 136bbf5c878SMichael Roth } 137bbf5c878SMichael Roth drc->configured = false; 138bbf5c878SMichael Roth } 139bbf5c878SMichael Roth 1400cb688d2SMichael Roth return RTAS_OUT_SUCCESS; 141bbf5c878SMichael Roth } 142bbf5c878SMichael Roth 1430cb688d2SMichael Roth static uint32_t set_indicator_state(sPAPRDRConnector *drc, 144bbf5c878SMichael Roth sPAPRDRIndicatorState state) 145bbf5c878SMichael Roth { 146*0b55aa91SDavid Gibson trace_spapr_drc_set_indicator_state(spapr_drc_index(drc), state); 147bbf5c878SMichael Roth drc->indicator_state = state; 1480cb688d2SMichael Roth return RTAS_OUT_SUCCESS; 149bbf5c878SMichael Roth } 150bbf5c878SMichael Roth 1510cb688d2SMichael Roth static uint32_t set_allocation_state(sPAPRDRConnector *drc, 152bbf5c878SMichael Roth sPAPRDRAllocationState state) 153bbf5c878SMichael Roth { 154bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 155bbf5c878SMichael Roth 156*0b55aa91SDavid Gibson trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state); 157bbf5c878SMichael Roth 1589d1852ceSMichael Roth if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) { 1599d1852ceSMichael Roth /* if there's no resource/device associated with the DRC, there's 1609d1852ceSMichael Roth * no way for us to put it in an allocation state consistent with 1619d1852ceSMichael Roth * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should 1629d1852ceSMichael Roth * result in an RTAS return code of -3 / "no such indicator" 1639d1852ceSMichael Roth */ 1649d1852ceSMichael Roth if (!drc->dev) { 1659d1852ceSMichael Roth return RTAS_OUT_NO_SUCH_INDICATOR; 1669d1852ceSMichael Roth } 167fe6824d1SLaurent Vivier if (drc->awaiting_release && drc->awaiting_allocation) { 168fe6824d1SLaurent Vivier /* kernel is acknowledging a previous hotplug event 169fe6824d1SLaurent Vivier * while we are already removing it. 170fe6824d1SLaurent Vivier * it's safe to ignore awaiting_allocation here since we know the 171fe6824d1SLaurent Vivier * situation is predicated on the guest either already having done 172fe6824d1SLaurent Vivier * so (boot-time hotplug), or never being able to acquire in the 173fe6824d1SLaurent Vivier * first place (hotplug followed by immediate unplug). 174fe6824d1SLaurent Vivier */ 175fe6824d1SLaurent Vivier drc->awaiting_allocation_skippable = true; 176fe6824d1SLaurent Vivier return RTAS_OUT_NO_SUCH_INDICATOR; 177fe6824d1SLaurent Vivier } 1789d1852ceSMichael Roth } 1799d1852ceSMichael Roth 180bbf5c878SMichael Roth if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) { 181bbf5c878SMichael Roth drc->allocation_state = state; 182bbf5c878SMichael Roth if (drc->awaiting_release && 183bbf5c878SMichael Roth drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 184*0b55aa91SDavid Gibson uint32_t drc_index = spapr_drc_index(drc); 185*0b55aa91SDavid Gibson trace_spapr_drc_set_allocation_state_finalizing(drc_index); 18631834723SDaniel Henrique Barboza drck->detach(drc, DEVICE(drc->dev), NULL); 187aab99135SBharata B Rao } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) { 188aab99135SBharata B Rao drc->awaiting_allocation = false; 189bbf5c878SMichael Roth } 190bbf5c878SMichael Roth } 1910cb688d2SMichael Roth return RTAS_OUT_SUCCESS; 192bbf5c878SMichael Roth } 193bbf5c878SMichael Roth 194*0b55aa91SDavid Gibson sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc) 195bbf5c878SMichael Roth { 196bbf5c878SMichael Roth return drc->type; 197bbf5c878SMichael Roth } 198bbf5c878SMichael Roth 199bbf5c878SMichael Roth static const char *get_name(sPAPRDRConnector *drc) 200bbf5c878SMichael Roth { 201bbf5c878SMichael Roth return drc->name; 202bbf5c878SMichael Roth } 203bbf5c878SMichael Roth 204f40eb921SMichael Roth /* has the guest been notified of device attachment? */ 205f40eb921SMichael Roth static void set_signalled(sPAPRDRConnector *drc) 206f40eb921SMichael Roth { 207f40eb921SMichael Roth drc->signalled = true; 208f40eb921SMichael Roth } 209f40eb921SMichael Roth 210bbf5c878SMichael Roth /* 211bbf5c878SMichael Roth * dr-entity-sense sensor value 212bbf5c878SMichael Roth * returned via get-sensor-state RTAS calls 213bbf5c878SMichael Roth * as expected by state diagram in PAPR+ 2.7, 13.4 214bbf5c878SMichael Roth * based on the current allocation/indicator/power states 215bbf5c878SMichael Roth * for the DR connector. 216bbf5c878SMichael Roth */ 2170cb688d2SMichael Roth static uint32_t entity_sense(sPAPRDRConnector *drc, sPAPRDREntitySense *state) 218bbf5c878SMichael Roth { 219bbf5c878SMichael Roth if (drc->dev) { 220bbf5c878SMichael Roth if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI && 221bbf5c878SMichael Roth drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 222bbf5c878SMichael Roth /* for logical DR, we return a state of UNUSABLE 223bbf5c878SMichael Roth * iff the allocation state UNUSABLE. 224bbf5c878SMichael Roth * Otherwise, report the state as USABLE/PRESENT, 225bbf5c878SMichael Roth * as we would for PCI. 226bbf5c878SMichael Roth */ 2270cb688d2SMichael Roth *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE; 228bbf5c878SMichael Roth } else { 229bbf5c878SMichael Roth /* this assumes all PCI devices are assigned to 230bbf5c878SMichael Roth * a 'live insertion' power domain, where QEMU 231bbf5c878SMichael Roth * manages power state automatically as opposed 232bbf5c878SMichael Roth * to the guest. present, non-PCI resources are 233bbf5c878SMichael Roth * unaffected by power state. 234bbf5c878SMichael Roth */ 2350cb688d2SMichael Roth *state = SPAPR_DR_ENTITY_SENSE_PRESENT; 236bbf5c878SMichael Roth } 237bbf5c878SMichael Roth } else { 238bbf5c878SMichael Roth if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 239bbf5c878SMichael Roth /* PCI devices, and only PCI devices, use EMPTY 240bbf5c878SMichael Roth * in cases where we'd otherwise use UNUSABLE 241bbf5c878SMichael Roth */ 2420cb688d2SMichael Roth *state = SPAPR_DR_ENTITY_SENSE_EMPTY; 243bbf5c878SMichael Roth } else { 2440cb688d2SMichael Roth *state = SPAPR_DR_ENTITY_SENSE_UNUSABLE; 245bbf5c878SMichael Roth } 246bbf5c878SMichael Roth } 247bbf5c878SMichael Roth 248*0b55aa91SDavid Gibson trace_spapr_drc_entity_sense(spapr_drc_index(drc), *state); 2490cb688d2SMichael Roth return RTAS_OUT_SUCCESS; 250bbf5c878SMichael Roth } 251bbf5c878SMichael Roth 252d7bce999SEric Blake static void prop_get_index(Object *obj, Visitor *v, const char *name, 253d7bce999SEric Blake void *opaque, Error **errp) 254bbf5c878SMichael Roth { 255bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 256*0b55aa91SDavid Gibson uint32_t value = spapr_drc_index(drc); 25751e72bc1SEric Blake visit_type_uint32(v, name, &value, errp); 258bbf5c878SMichael Roth } 259bbf5c878SMichael Roth 260d7bce999SEric Blake static void prop_get_type(Object *obj, Visitor *v, const char *name, 261d7bce999SEric Blake void *opaque, Error **errp) 262bbf5c878SMichael Roth { 263bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 264*0b55aa91SDavid Gibson uint32_t value = (uint32_t)spapr_drc_type(drc); 26551e72bc1SEric Blake visit_type_uint32(v, name, &value, errp); 266bbf5c878SMichael Roth } 267bbf5c878SMichael Roth 268bbf5c878SMichael Roth static char *prop_get_name(Object *obj, Error **errp) 269bbf5c878SMichael Roth { 270bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 271bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 272bbf5c878SMichael Roth return g_strdup(drck->get_name(drc)); 273bbf5c878SMichael Roth } 274bbf5c878SMichael Roth 275d7bce999SEric Blake static void prop_get_entity_sense(Object *obj, Visitor *v, const char *name, 276d7bce999SEric Blake void *opaque, Error **errp) 277bbf5c878SMichael Roth { 278bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 279bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 2800cb688d2SMichael Roth uint32_t value; 2810cb688d2SMichael Roth 2820cb688d2SMichael Roth drck->entity_sense(drc, &value); 28351e72bc1SEric Blake visit_type_uint32(v, name, &value, errp); 284bbf5c878SMichael Roth } 285bbf5c878SMichael Roth 286d7bce999SEric Blake static void prop_get_fdt(Object *obj, Visitor *v, const char *name, 287d7bce999SEric Blake void *opaque, Error **errp) 288bbf5c878SMichael Roth { 289bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 290c75304a1SMarkus Armbruster Error *err = NULL; 291bbf5c878SMichael Roth int fdt_offset_next, fdt_offset, fdt_depth; 292bbf5c878SMichael Roth void *fdt; 293bbf5c878SMichael Roth 294bbf5c878SMichael Roth if (!drc->fdt) { 295a543a554SEric Blake visit_type_null(v, NULL, errp); 296bbf5c878SMichael Roth return; 297bbf5c878SMichael Roth } 298bbf5c878SMichael Roth 299bbf5c878SMichael Roth fdt = drc->fdt; 300bbf5c878SMichael Roth fdt_offset = drc->fdt_start_offset; 301bbf5c878SMichael Roth fdt_depth = 0; 302bbf5c878SMichael Roth 303bbf5c878SMichael Roth do { 304bbf5c878SMichael Roth const char *name = NULL; 305bbf5c878SMichael Roth const struct fdt_property *prop = NULL; 306bbf5c878SMichael Roth int prop_len = 0, name_len = 0; 307bbf5c878SMichael Roth uint32_t tag; 308bbf5c878SMichael Roth 309bbf5c878SMichael Roth tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next); 310bbf5c878SMichael Roth switch (tag) { 311bbf5c878SMichael Roth case FDT_BEGIN_NODE: 312bbf5c878SMichael Roth fdt_depth++; 313bbf5c878SMichael Roth name = fdt_get_name(fdt, fdt_offset, &name_len); 314337283dfSEric Blake visit_start_struct(v, name, NULL, 0, &err); 315c75304a1SMarkus Armbruster if (err) { 316c75304a1SMarkus Armbruster error_propagate(errp, err); 317c75304a1SMarkus Armbruster return; 318c75304a1SMarkus Armbruster } 319bbf5c878SMichael Roth break; 320bbf5c878SMichael Roth case FDT_END_NODE: 321bbf5c878SMichael Roth /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */ 322bbf5c878SMichael Roth g_assert(fdt_depth > 0); 32315c2f669SEric Blake visit_check_struct(v, &err); 3241158bb2aSEric Blake visit_end_struct(v, NULL); 325c75304a1SMarkus Armbruster if (err) { 326c75304a1SMarkus Armbruster error_propagate(errp, err); 327c75304a1SMarkus Armbruster return; 328c75304a1SMarkus Armbruster } 329bbf5c878SMichael Roth fdt_depth--; 330bbf5c878SMichael Roth break; 331bbf5c878SMichael Roth case FDT_PROP: { 332bbf5c878SMichael Roth int i; 333bbf5c878SMichael Roth prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len); 334bbf5c878SMichael Roth name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); 335d9f62ddeSEric Blake visit_start_list(v, name, NULL, 0, &err); 336c75304a1SMarkus Armbruster if (err) { 337c75304a1SMarkus Armbruster error_propagate(errp, err); 338c75304a1SMarkus Armbruster return; 339bbf5c878SMichael Roth } 340c75304a1SMarkus Armbruster for (i = 0; i < prop_len; i++) { 34151e72bc1SEric Blake visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err); 342c75304a1SMarkus Armbruster if (err) { 343c75304a1SMarkus Armbruster error_propagate(errp, err); 344c75304a1SMarkus Armbruster return; 345c75304a1SMarkus Armbruster } 346c75304a1SMarkus Armbruster } 347a4a1c70dSMarkus Armbruster visit_check_list(v, &err); 3481158bb2aSEric Blake visit_end_list(v, NULL); 349a4a1c70dSMarkus Armbruster if (err) { 350a4a1c70dSMarkus Armbruster error_propagate(errp, err); 351a4a1c70dSMarkus Armbruster return; 352a4a1c70dSMarkus Armbruster } 353bbf5c878SMichael Roth break; 354bbf5c878SMichael Roth } 355bbf5c878SMichael Roth default: 356bbf5c878SMichael Roth error_setg(&error_abort, "device FDT in unexpected state: %d", tag); 357bbf5c878SMichael Roth } 358bbf5c878SMichael Roth fdt_offset = fdt_offset_next; 359bbf5c878SMichael Roth } while (fdt_depth != 0); 360bbf5c878SMichael Roth } 361bbf5c878SMichael Roth 362bbf5c878SMichael Roth static void attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt, 363bbf5c878SMichael Roth int fdt_start_offset, bool coldplug, Error **errp) 364bbf5c878SMichael Roth { 365*0b55aa91SDavid Gibson trace_spapr_drc_attach(spapr_drc_index(drc)); 366bbf5c878SMichael Roth 367bbf5c878SMichael Roth if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) { 368bbf5c878SMichael Roth error_setg(errp, "an attached device is still awaiting release"); 369bbf5c878SMichael Roth return; 370bbf5c878SMichael Roth } 371bbf5c878SMichael Roth if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 372bbf5c878SMichael Roth g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE); 373bbf5c878SMichael Roth } 374bbf5c878SMichael Roth g_assert(fdt || coldplug); 375bbf5c878SMichael Roth 376bbf5c878SMichael Roth /* NOTE: setting initial isolation state to UNISOLATED means we can't 377bbf5c878SMichael Roth * detach unless guest has a userspace/kernel that moves this state 378bbf5c878SMichael Roth * back to ISOLATED in response to an unplug event, or this is done 379bbf5c878SMichael Roth * manually by the admin prior. if we force things while the guest 380bbf5c878SMichael Roth * may be accessing the device, we can easily crash the guest, so we 381bbf5c878SMichael Roth * we defer completion of removal in such cases to the reset() hook. 382bbf5c878SMichael Roth */ 383bbf5c878SMichael Roth if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 384bbf5c878SMichael Roth drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED; 385bbf5c878SMichael Roth } 386bbf5c878SMichael Roth drc->indicator_state = SPAPR_DR_INDICATOR_STATE_ACTIVE; 387bbf5c878SMichael Roth 388bbf5c878SMichael Roth drc->dev = d; 389bbf5c878SMichael Roth drc->fdt = fdt; 390bbf5c878SMichael Roth drc->fdt_start_offset = fdt_start_offset; 391785652dcSLaurent Vivier drc->configured = coldplug; 392df18b2dbSMichael Roth /* 'logical' DR resources such as memory/cpus are in some cases treated 393df18b2dbSMichael Roth * as a pool of resources from which the guest is free to choose from 394df18b2dbSMichael Roth * based on only a count. for resources that can be assigned in this 395df18b2dbSMichael Roth * fashion, we must assume the resource is signalled immediately 396df18b2dbSMichael Roth * since a single hotplug request might make an arbitrary number of 397df18b2dbSMichael Roth * such attached resources available to the guest, as opposed to 398df18b2dbSMichael Roth * 'physical' DR resources such as PCI where each device/resource is 399df18b2dbSMichael Roth * signalled individually. 400df18b2dbSMichael Roth */ 401df18b2dbSMichael Roth drc->signalled = (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) 402df18b2dbSMichael Roth ? true : coldplug; 403bbf5c878SMichael Roth 404aab99135SBharata B Rao if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI) { 405aab99135SBharata B Rao drc->awaiting_allocation = true; 406aab99135SBharata B Rao } 407aab99135SBharata B Rao 408bbf5c878SMichael Roth object_property_add_link(OBJECT(drc), "device", 409bbf5c878SMichael Roth object_get_typename(OBJECT(drc->dev)), 410bbf5c878SMichael Roth (Object **)(&drc->dev), 411bbf5c878SMichael Roth NULL, 0, NULL); 412bbf5c878SMichael Roth } 413bbf5c878SMichael Roth 41431834723SDaniel Henrique Barboza static void detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp) 415bbf5c878SMichael Roth { 416*0b55aa91SDavid Gibson trace_spapr_drc_detach(spapr_drc_index(drc)); 417bbf5c878SMichael Roth 418f40eb921SMichael Roth /* if we've signalled device presence to the guest, or if the guest 419f40eb921SMichael Roth * has gone ahead and configured the device (via manually-executed 420f40eb921SMichael Roth * device add via drmgr in guest, namely), we need to wait 421f40eb921SMichael Roth * for the guest to quiesce the device before completing detach. 422f40eb921SMichael Roth * Otherwise, we can assume the guest hasn't seen it and complete the 423f40eb921SMichael Roth * detach immediately. Note that there is a small race window 424f40eb921SMichael Roth * just before, or during, configuration, which is this context 425f40eb921SMichael Roth * refers mainly to fetching the device tree via RTAS. 426f40eb921SMichael Roth * During this window the device access will be arbitrated by 427f40eb921SMichael Roth * associated DRC, which will simply fail the RTAS calls as invalid. 428f40eb921SMichael Roth * This is recoverable within guest and current implementations of 429f40eb921SMichael Roth * drmgr should be able to cope. 430f40eb921SMichael Roth */ 431f40eb921SMichael Roth if (!drc->signalled && !drc->configured) { 432f40eb921SMichael Roth /* if the guest hasn't seen the device we can't rely on it to 433f40eb921SMichael Roth * set it back to an isolated state via RTAS, so do it here manually 434f40eb921SMichael Roth */ 435f40eb921SMichael Roth drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED; 436f40eb921SMichael Roth } 437f40eb921SMichael Roth 438bbf5c878SMichael Roth if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) { 439*0b55aa91SDavid Gibson trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc)); 440bbf5c878SMichael Roth drc->awaiting_release = true; 441bbf5c878SMichael Roth return; 442bbf5c878SMichael Roth } 443bbf5c878SMichael Roth 444bbf5c878SMichael Roth if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI && 445bbf5c878SMichael Roth drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 446*0b55aa91SDavid Gibson trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc)); 447bbf5c878SMichael Roth drc->awaiting_release = true; 448bbf5c878SMichael Roth return; 449bbf5c878SMichael Roth } 450bbf5c878SMichael Roth 451aab99135SBharata B Rao if (drc->awaiting_allocation) { 452fe6824d1SLaurent Vivier if (!drc->awaiting_allocation_skippable) { 453aab99135SBharata B Rao drc->awaiting_release = true; 454*0b55aa91SDavid Gibson trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc)); 455aab99135SBharata B Rao return; 456aab99135SBharata B Rao } 457fe6824d1SLaurent Vivier } 458aab99135SBharata B Rao 459bbf5c878SMichael Roth drc->indicator_state = SPAPR_DR_INDICATOR_STATE_INACTIVE; 460bbf5c878SMichael Roth 46131834723SDaniel Henrique Barboza /* Calling release callbacks based on drc->type. */ 46231834723SDaniel Henrique Barboza switch (drc->type) { 46331834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_CPU: 46431834723SDaniel Henrique Barboza spapr_core_release(drc->dev); 46531834723SDaniel Henrique Barboza break; 46631834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PCI: 46731834723SDaniel Henrique Barboza spapr_phb_remove_pci_device_cb(drc->dev); 46831834723SDaniel Henrique Barboza break; 46931834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_LMB: 47031834723SDaniel Henrique Barboza spapr_lmb_release(drc->dev); 47131834723SDaniel Henrique Barboza break; 47231834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PHB: 47331834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_VIO: 47431834723SDaniel Henrique Barboza default: 47531834723SDaniel Henrique Barboza g_assert(false); 476bbf5c878SMichael Roth } 477bbf5c878SMichael Roth 478bbf5c878SMichael Roth drc->awaiting_release = false; 479fe6824d1SLaurent Vivier drc->awaiting_allocation_skippable = false; 480bbf5c878SMichael Roth g_free(drc->fdt); 481bbf5c878SMichael Roth drc->fdt = NULL; 482bbf5c878SMichael Roth drc->fdt_start_offset = 0; 483bbf5c878SMichael Roth object_property_del(OBJECT(drc), "device", NULL); 484bbf5c878SMichael Roth drc->dev = NULL; 485bbf5c878SMichael Roth } 486bbf5c878SMichael Roth 487bbf5c878SMichael Roth static bool release_pending(sPAPRDRConnector *drc) 488bbf5c878SMichael Roth { 489bbf5c878SMichael Roth return drc->awaiting_release; 490bbf5c878SMichael Roth } 491bbf5c878SMichael Roth 492bbf5c878SMichael Roth static void reset(DeviceState *d) 493bbf5c878SMichael Roth { 494bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 495bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 496f40eb921SMichael Roth sPAPRDREntitySense state; 497bbf5c878SMichael Roth 498*0b55aa91SDavid Gibson trace_spapr_drc_reset(spapr_drc_index(drc)); 499bbf5c878SMichael Roth /* immediately upon reset we can safely assume DRCs whose devices 500bbf5c878SMichael Roth * are pending removal can be safely removed, and that they will 501bbf5c878SMichael Roth * subsequently be left in an ISOLATED state. move the DRC to this 502bbf5c878SMichael Roth * state in these cases (which will in turn complete any pending 503bbf5c878SMichael Roth * device removals) 504bbf5c878SMichael Roth */ 505bbf5c878SMichael Roth if (drc->awaiting_release) { 506bbf5c878SMichael Roth drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED); 507bbf5c878SMichael Roth /* generally this should also finalize the removal, but if the device 508bbf5c878SMichael Roth * hasn't yet been configured we normally defer removal under the 509bbf5c878SMichael Roth * assumption that this transition is taking place as part of device 510bbf5c878SMichael Roth * configuration. so check if we're still waiting after this, and 511bbf5c878SMichael Roth * force removal if we are 512bbf5c878SMichael Roth */ 513bbf5c878SMichael Roth if (drc->awaiting_release) { 51431834723SDaniel Henrique Barboza drck->detach(drc, DEVICE(drc->dev), NULL); 515bbf5c878SMichael Roth } 516bbf5c878SMichael Roth 517bbf5c878SMichael Roth /* non-PCI devices may be awaiting a transition to UNUSABLE */ 518bbf5c878SMichael Roth if (drc->type != SPAPR_DR_CONNECTOR_TYPE_PCI && 519bbf5c878SMichael Roth drc->awaiting_release) { 520bbf5c878SMichael Roth drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE); 521bbf5c878SMichael Roth } 522bbf5c878SMichael Roth } 523f40eb921SMichael Roth 524f40eb921SMichael Roth drck->entity_sense(drc, &state); 525f40eb921SMichael Roth if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { 526f40eb921SMichael Roth drck->set_signalled(drc); 527f40eb921SMichael Roth } 528bbf5c878SMichael Roth } 529bbf5c878SMichael Roth 530a50919ddSDaniel Henrique Barboza static bool spapr_drc_needed(void *opaque) 531a50919ddSDaniel Henrique Barboza { 532a50919ddSDaniel Henrique Barboza sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque; 533a50919ddSDaniel Henrique Barboza sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 534a50919ddSDaniel Henrique Barboza bool rc = false; 535a50919ddSDaniel Henrique Barboza sPAPRDREntitySense value; 536a50919ddSDaniel Henrique Barboza drck->entity_sense(drc, &value); 537a50919ddSDaniel Henrique Barboza 538a50919ddSDaniel Henrique Barboza /* If no dev is plugged in there is no need to migrate the DRC state */ 539a50919ddSDaniel Henrique Barboza if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) { 540a50919ddSDaniel Henrique Barboza return false; 541a50919ddSDaniel Henrique Barboza } 542a50919ddSDaniel Henrique Barboza 543a50919ddSDaniel Henrique Barboza /* 544a50919ddSDaniel Henrique Barboza * If there is dev plugged in, we need to migrate the DRC state when 545a50919ddSDaniel Henrique Barboza * it is different from cold-plugged state 546a50919ddSDaniel Henrique Barboza */ 547a50919ddSDaniel Henrique Barboza switch (drc->type) { 548a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PCI: 549a50919ddSDaniel Henrique Barboza rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) && 550a50919ddSDaniel Henrique Barboza (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) && 551a50919ddSDaniel Henrique Barboza drc->configured && drc->signalled && !drc->awaiting_release); 552a50919ddSDaniel Henrique Barboza break; 553a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_CPU: 554a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_LMB: 555a50919ddSDaniel Henrique Barboza rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) && 556a50919ddSDaniel Henrique Barboza (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) && 557a50919ddSDaniel Henrique Barboza drc->configured && drc->signalled && !drc->awaiting_release); 558a50919ddSDaniel Henrique Barboza break; 559a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PHB: 560a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_VIO: 561a50919ddSDaniel Henrique Barboza default: 562a50919ddSDaniel Henrique Barboza g_assert(false); 563a50919ddSDaniel Henrique Barboza } 564a50919ddSDaniel Henrique Barboza return rc; 565a50919ddSDaniel Henrique Barboza } 566a50919ddSDaniel Henrique Barboza 567a50919ddSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_drc = { 568a50919ddSDaniel Henrique Barboza .name = "spapr_drc", 569a50919ddSDaniel Henrique Barboza .version_id = 1, 570a50919ddSDaniel Henrique Barboza .minimum_version_id = 1, 571a50919ddSDaniel Henrique Barboza .needed = spapr_drc_needed, 572a50919ddSDaniel Henrique Barboza .fields = (VMStateField []) { 573a50919ddSDaniel Henrique Barboza VMSTATE_UINT32(isolation_state, sPAPRDRConnector), 574a50919ddSDaniel Henrique Barboza VMSTATE_UINT32(allocation_state, sPAPRDRConnector), 575a50919ddSDaniel Henrique Barboza VMSTATE_UINT32(indicator_state, sPAPRDRConnector), 576a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(configured, sPAPRDRConnector), 577a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(awaiting_release, sPAPRDRConnector), 578a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector), 579a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(signalled, sPAPRDRConnector), 580a50919ddSDaniel Henrique Barboza VMSTATE_END_OF_LIST() 581a50919ddSDaniel Henrique Barboza } 582a50919ddSDaniel Henrique Barboza }; 583a50919ddSDaniel Henrique Barboza 584bbf5c878SMichael Roth static void realize(DeviceState *d, Error **errp) 585bbf5c878SMichael Roth { 586bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 587bbf5c878SMichael Roth Object *root_container; 588bbf5c878SMichael Roth char link_name[256]; 589bbf5c878SMichael Roth gchar *child_name; 590bbf5c878SMichael Roth Error *err = NULL; 591bbf5c878SMichael Roth 592*0b55aa91SDavid Gibson trace_spapr_drc_realize(spapr_drc_index(drc)); 593bbf5c878SMichael Roth /* NOTE: we do this as part of realize/unrealize due to the fact 594bbf5c878SMichael Roth * that the guest will communicate with the DRC via RTAS calls 595bbf5c878SMichael Roth * referencing the global DRC index. By unlinking the DRC 596bbf5c878SMichael Roth * from DRC_CONTAINER_PATH/<drc_index> we effectively make it 597bbf5c878SMichael Roth * inaccessible by the guest, since lookups rely on this path 598bbf5c878SMichael Roth * existing in the composition tree 599bbf5c878SMichael Roth */ 600bbf5c878SMichael Roth root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 601*0b55aa91SDavid Gibson snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc)); 602bbf5c878SMichael Roth child_name = object_get_canonical_path_component(OBJECT(drc)); 603*0b55aa91SDavid Gibson trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name); 604bbf5c878SMichael Roth object_property_add_alias(root_container, link_name, 605bbf5c878SMichael Roth drc->owner, child_name, &err); 606bbf5c878SMichael Roth if (err) { 6074fffeb5eSMarkus Armbruster error_report_err(err); 608bbf5c878SMichael Roth object_unref(OBJECT(drc)); 609bbf5c878SMichael Roth } 610586d2142SGonglei g_free(child_name); 611*0b55aa91SDavid Gibson vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc, 612a50919ddSDaniel Henrique Barboza drc); 613*0b55aa91SDavid Gibson trace_spapr_drc_realize_complete(spapr_drc_index(drc)); 614bbf5c878SMichael Roth } 615bbf5c878SMichael Roth 616bbf5c878SMichael Roth static void unrealize(DeviceState *d, Error **errp) 617bbf5c878SMichael Roth { 618bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 619bbf5c878SMichael Roth Object *root_container; 620bbf5c878SMichael Roth char name[256]; 621bbf5c878SMichael Roth Error *err = NULL; 622bbf5c878SMichael Roth 623*0b55aa91SDavid Gibson trace_spapr_drc_unrealize(spapr_drc_index(drc)); 624bbf5c878SMichael Roth root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 625*0b55aa91SDavid Gibson snprintf(name, sizeof(name), "%x", spapr_drc_index(drc)); 626bbf5c878SMichael Roth object_property_del(root_container, name, &err); 627bbf5c878SMichael Roth if (err) { 6284fffeb5eSMarkus Armbruster error_report_err(err); 629bbf5c878SMichael Roth object_unref(OBJECT(drc)); 630bbf5c878SMichael Roth } 631bbf5c878SMichael Roth } 632bbf5c878SMichael Roth 633bbf5c878SMichael Roth sPAPRDRConnector *spapr_dr_connector_new(Object *owner, 634bbf5c878SMichael Roth sPAPRDRConnectorType type, 635bbf5c878SMichael Roth uint32_t id) 636bbf5c878SMichael Roth { 637bbf5c878SMichael Roth sPAPRDRConnector *drc = 638bbf5c878SMichael Roth SPAPR_DR_CONNECTOR(object_new(TYPE_SPAPR_DR_CONNECTOR)); 63994649d42SDavid Gibson char *prop_name; 640bbf5c878SMichael Roth 641bbf5c878SMichael Roth g_assert(type); 642bbf5c878SMichael Roth 643bbf5c878SMichael Roth drc->type = type; 644bbf5c878SMichael Roth drc->id = id; 645bbf5c878SMichael Roth drc->owner = owner; 646*0b55aa91SDavid Gibson prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", 647*0b55aa91SDavid Gibson spapr_drc_index(drc)); 64894649d42SDavid Gibson object_property_add_child(owner, prop_name, OBJECT(drc), NULL); 649bbf5c878SMichael Roth object_property_set_bool(OBJECT(drc), true, "realized", NULL); 65094649d42SDavid Gibson g_free(prop_name); 651bbf5c878SMichael Roth 652bbf5c878SMichael Roth /* human-readable name for a DRC to encode into the DT 653bbf5c878SMichael Roth * description. this is mainly only used within a guest in place 654bbf5c878SMichael Roth * of the unique DRC index. 655bbf5c878SMichael Roth * 656bbf5c878SMichael Roth * in the case of VIO/PCI devices, it corresponds to a 657bbf5c878SMichael Roth * "location code" that maps a logical device/function (DRC index) 658bbf5c878SMichael Roth * to a physical (or virtual in the case of VIO) location in the 659bbf5c878SMichael Roth * system by chaining together the "location label" for each 660bbf5c878SMichael Roth * encapsulating component. 661bbf5c878SMichael Roth * 662bbf5c878SMichael Roth * since this is more to do with diagnosing physical hardware 663bbf5c878SMichael Roth * issues than guest compatibility, we choose location codes/DRC 664bbf5c878SMichael Roth * names that adhere to the documented format, but avoid encoding 665bbf5c878SMichael Roth * the entire topology information into the label/code, instead 666bbf5c878SMichael Roth * just using the location codes based on the labels for the 667bbf5c878SMichael Roth * endpoints (VIO/PCI adaptor connectors), which is basically 668bbf5c878SMichael Roth * just "C" followed by an integer ID. 669bbf5c878SMichael Roth * 670bbf5c878SMichael Roth * DRC names as documented by PAPR+ v2.7, 13.5.2.4 671bbf5c878SMichael Roth * location codes as documented by PAPR+ v2.7, 12.3.1.5 672bbf5c878SMichael Roth */ 673bbf5c878SMichael Roth switch (drc->type) { 674bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_CPU: 675bbf5c878SMichael Roth drc->name = g_strdup_printf("CPU %d", id); 676bbf5c878SMichael Roth break; 677bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_PHB: 678bbf5c878SMichael Roth drc->name = g_strdup_printf("PHB %d", id); 679bbf5c878SMichael Roth break; 680bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_VIO: 681bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_PCI: 682bbf5c878SMichael Roth drc->name = g_strdup_printf("C%d", id); 683bbf5c878SMichael Roth break; 684bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_LMB: 685bbf5c878SMichael Roth drc->name = g_strdup_printf("LMB %d", id); 686bbf5c878SMichael Roth break; 687bbf5c878SMichael Roth default: 688bbf5c878SMichael Roth g_assert(false); 689bbf5c878SMichael Roth } 690bbf5c878SMichael Roth 691bbf5c878SMichael Roth /* PCI slot always start in a USABLE state, and stay there */ 692bbf5c878SMichael Roth if (drc->type == SPAPR_DR_CONNECTOR_TYPE_PCI) { 693bbf5c878SMichael Roth drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE; 694bbf5c878SMichael Roth } 695bbf5c878SMichael Roth 696bbf5c878SMichael Roth return drc; 697bbf5c878SMichael Roth } 698bbf5c878SMichael Roth 699bbf5c878SMichael Roth static void spapr_dr_connector_instance_init(Object *obj) 700bbf5c878SMichael Roth { 701bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 702bbf5c878SMichael Roth 703bbf5c878SMichael Roth object_property_add_uint32_ptr(obj, "isolation-state", 704bbf5c878SMichael Roth &drc->isolation_state, NULL); 705bbf5c878SMichael Roth object_property_add_uint32_ptr(obj, "indicator-state", 706bbf5c878SMichael Roth &drc->indicator_state, NULL); 707bbf5c878SMichael Roth object_property_add_uint32_ptr(obj, "allocation-state", 708bbf5c878SMichael Roth &drc->allocation_state, NULL); 709bbf5c878SMichael Roth object_property_add_uint32_ptr(obj, "id", &drc->id, NULL); 710bbf5c878SMichael Roth object_property_add(obj, "index", "uint32", prop_get_index, 711bbf5c878SMichael Roth NULL, NULL, NULL, NULL); 712bbf5c878SMichael Roth object_property_add(obj, "connector_type", "uint32", prop_get_type, 713bbf5c878SMichael Roth NULL, NULL, NULL, NULL); 714bbf5c878SMichael Roth object_property_add_str(obj, "name", prop_get_name, NULL, NULL); 715bbf5c878SMichael Roth object_property_add(obj, "entity-sense", "uint32", prop_get_entity_sense, 716bbf5c878SMichael Roth NULL, NULL, NULL, NULL); 717bbf5c878SMichael Roth object_property_add(obj, "fdt", "struct", prop_get_fdt, 718bbf5c878SMichael Roth NULL, NULL, NULL, NULL); 719bbf5c878SMichael Roth } 720bbf5c878SMichael Roth 721bbf5c878SMichael Roth static void spapr_dr_connector_class_init(ObjectClass *k, void *data) 722bbf5c878SMichael Roth { 723bbf5c878SMichael Roth DeviceClass *dk = DEVICE_CLASS(k); 724bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 725bbf5c878SMichael Roth 726bbf5c878SMichael Roth dk->reset = reset; 727bbf5c878SMichael Roth dk->realize = realize; 728bbf5c878SMichael Roth dk->unrealize = unrealize; 729bbf5c878SMichael Roth drck->set_isolation_state = set_isolation_state; 730bbf5c878SMichael Roth drck->set_indicator_state = set_indicator_state; 731bbf5c878SMichael Roth drck->set_allocation_state = set_allocation_state; 732bbf5c878SMichael Roth drck->get_name = get_name; 733bbf5c878SMichael Roth drck->entity_sense = entity_sense; 734bbf5c878SMichael Roth drck->attach = attach; 735bbf5c878SMichael Roth drck->detach = detach; 736bbf5c878SMichael Roth drck->release_pending = release_pending; 737f40eb921SMichael Roth drck->set_signalled = set_signalled; 738c401ae8cSMarkus Armbruster /* 739c401ae8cSMarkus Armbruster * Reason: it crashes FIXME find and document the real reason 740c401ae8cSMarkus Armbruster */ 741e90f2a8cSEduardo Habkost dk->user_creatable = false; 742bbf5c878SMichael Roth } 743bbf5c878SMichael Roth 744bbf5c878SMichael Roth static const TypeInfo spapr_dr_connector_info = { 745bbf5c878SMichael Roth .name = TYPE_SPAPR_DR_CONNECTOR, 746bbf5c878SMichael Roth .parent = TYPE_DEVICE, 747bbf5c878SMichael Roth .instance_size = sizeof(sPAPRDRConnector), 748bbf5c878SMichael Roth .instance_init = spapr_dr_connector_instance_init, 749bbf5c878SMichael Roth .class_size = sizeof(sPAPRDRConnectorClass), 750bbf5c878SMichael Roth .class_init = spapr_dr_connector_class_init, 751bbf5c878SMichael Roth }; 752bbf5c878SMichael Roth 753bbf5c878SMichael Roth /* helper functions for external users */ 754bbf5c878SMichael Roth 755bbf5c878SMichael Roth sPAPRDRConnector *spapr_dr_connector_by_index(uint32_t index) 756bbf5c878SMichael Roth { 757bbf5c878SMichael Roth Object *obj; 758bbf5c878SMichael Roth char name[256]; 759bbf5c878SMichael Roth 760bbf5c878SMichael Roth snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index); 761bbf5c878SMichael Roth obj = object_resolve_path(name, NULL); 762bbf5c878SMichael Roth 763bbf5c878SMichael Roth return !obj ? NULL : SPAPR_DR_CONNECTOR(obj); 764bbf5c878SMichael Roth } 765bbf5c878SMichael Roth 766bbf5c878SMichael Roth sPAPRDRConnector *spapr_dr_connector_by_id(sPAPRDRConnectorType type, 767bbf5c878SMichael Roth uint32_t id) 768bbf5c878SMichael Roth { 769bbf5c878SMichael Roth return spapr_dr_connector_by_index( 770bbf5c878SMichael Roth (get_type_shift(type) << DRC_INDEX_TYPE_SHIFT) | 771bbf5c878SMichael Roth (id & DRC_INDEX_ID_MASK)); 772bbf5c878SMichael Roth } 773e4b798bbSMichael Roth 774e4b798bbSMichael Roth /* generate a string the describes the DRC to encode into the 775e4b798bbSMichael Roth * device tree. 776e4b798bbSMichael Roth * 777e4b798bbSMichael Roth * as documented by PAPR+ v2.7, 13.5.2.6 and C.6.1 778e4b798bbSMichael Roth */ 779e4b798bbSMichael Roth static const char *spapr_drc_get_type_str(sPAPRDRConnectorType type) 780e4b798bbSMichael Roth { 781e4b798bbSMichael Roth switch (type) { 782e4b798bbSMichael Roth case SPAPR_DR_CONNECTOR_TYPE_CPU: 783e4b798bbSMichael Roth return "CPU"; 784e4b798bbSMichael Roth case SPAPR_DR_CONNECTOR_TYPE_PHB: 785e4b798bbSMichael Roth return "PHB"; 786e4b798bbSMichael Roth case SPAPR_DR_CONNECTOR_TYPE_VIO: 787e4b798bbSMichael Roth return "SLOT"; 788e4b798bbSMichael Roth case SPAPR_DR_CONNECTOR_TYPE_PCI: 789e4b798bbSMichael Roth return "28"; 790e4b798bbSMichael Roth case SPAPR_DR_CONNECTOR_TYPE_LMB: 791e4b798bbSMichael Roth return "MEM"; 792e4b798bbSMichael Roth default: 793e4b798bbSMichael Roth g_assert(false); 794e4b798bbSMichael Roth } 795e4b798bbSMichael Roth 796e4b798bbSMichael Roth return NULL; 797e4b798bbSMichael Roth } 798e4b798bbSMichael Roth 799e4b798bbSMichael Roth /** 800e4b798bbSMichael Roth * spapr_drc_populate_dt 801e4b798bbSMichael Roth * 802e4b798bbSMichael Roth * @fdt: libfdt device tree 803e4b798bbSMichael Roth * @path: path in the DT to generate properties 804e4b798bbSMichael Roth * @owner: parent Object/DeviceState for which to generate DRC 805e4b798bbSMichael Roth * descriptions for 806e4b798bbSMichael Roth * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding 807e4b798bbSMichael Roth * to the types of DRCs to generate entries for 808e4b798bbSMichael Roth * 809e4b798bbSMichael Roth * generate OF properties to describe DRC topology/indices to guests 810e4b798bbSMichael Roth * 811e4b798bbSMichael Roth * as documented in PAPR+ v2.1, 13.5.2 812e4b798bbSMichael Roth */ 813e4b798bbSMichael Roth int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner, 814e4b798bbSMichael Roth uint32_t drc_type_mask) 815e4b798bbSMichael Roth { 816e4b798bbSMichael Roth Object *root_container; 817e4b798bbSMichael Roth ObjectProperty *prop; 8187746abd8SDaniel P. Berrange ObjectPropertyIterator iter; 819e4b798bbSMichael Roth uint32_t drc_count = 0; 820e4b798bbSMichael Roth GArray *drc_indexes, *drc_power_domains; 821e4b798bbSMichael Roth GString *drc_names, *drc_types; 822e4b798bbSMichael Roth int ret; 823e4b798bbSMichael Roth 824e4b798bbSMichael Roth /* the first entry of each properties is a 32-bit integer encoding 825e4b798bbSMichael Roth * the number of elements in the array. we won't know this until 826e4b798bbSMichael Roth * we complete the iteration through all the matching DRCs, but 827e4b798bbSMichael Roth * reserve the space now and set the offsets accordingly so we 828e4b798bbSMichael Roth * can fill them in later. 829e4b798bbSMichael Roth */ 830e4b798bbSMichael Roth drc_indexes = g_array_new(false, true, sizeof(uint32_t)); 831e4b798bbSMichael Roth drc_indexes = g_array_set_size(drc_indexes, 1); 832e4b798bbSMichael Roth drc_power_domains = g_array_new(false, true, sizeof(uint32_t)); 833e4b798bbSMichael Roth drc_power_domains = g_array_set_size(drc_power_domains, 1); 834e4b798bbSMichael Roth drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); 835e4b798bbSMichael Roth drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); 836e4b798bbSMichael Roth 837e4b798bbSMichael Roth /* aliases for all DRConnector objects will be rooted in QOM 838e4b798bbSMichael Roth * composition tree at DRC_CONTAINER_PATH 839e4b798bbSMichael Roth */ 840e4b798bbSMichael Roth root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 841e4b798bbSMichael Roth 8427746abd8SDaniel P. Berrange object_property_iter_init(&iter, root_container); 8437746abd8SDaniel P. Berrange while ((prop = object_property_iter_next(&iter))) { 844e4b798bbSMichael Roth Object *obj; 845e4b798bbSMichael Roth sPAPRDRConnector *drc; 846e4b798bbSMichael Roth sPAPRDRConnectorClass *drck; 847e4b798bbSMichael Roth uint32_t drc_index, drc_power_domain; 848e4b798bbSMichael Roth 849e4b798bbSMichael Roth if (!strstart(prop->type, "link<", NULL)) { 850e4b798bbSMichael Roth continue; 851e4b798bbSMichael Roth } 852e4b798bbSMichael Roth 853e4b798bbSMichael Roth obj = object_property_get_link(root_container, prop->name, NULL); 854e4b798bbSMichael Roth drc = SPAPR_DR_CONNECTOR(obj); 855e4b798bbSMichael Roth drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 856e4b798bbSMichael Roth 857e4b798bbSMichael Roth if (owner && (drc->owner != owner)) { 858e4b798bbSMichael Roth continue; 859e4b798bbSMichael Roth } 860e4b798bbSMichael Roth 861e4b798bbSMichael Roth if ((drc->type & drc_type_mask) == 0) { 862e4b798bbSMichael Roth continue; 863e4b798bbSMichael Roth } 864e4b798bbSMichael Roth 865e4b798bbSMichael Roth drc_count++; 866e4b798bbSMichael Roth 867e4b798bbSMichael Roth /* ibm,drc-indexes */ 868*0b55aa91SDavid Gibson drc_index = cpu_to_be32(spapr_drc_index(drc)); 869e4b798bbSMichael Roth g_array_append_val(drc_indexes, drc_index); 870e4b798bbSMichael Roth 871e4b798bbSMichael Roth /* ibm,drc-power-domains */ 872e4b798bbSMichael Roth drc_power_domain = cpu_to_be32(-1); 873e4b798bbSMichael Roth g_array_append_val(drc_power_domains, drc_power_domain); 874e4b798bbSMichael Roth 875e4b798bbSMichael Roth /* ibm,drc-names */ 876e4b798bbSMichael Roth drc_names = g_string_append(drc_names, drck->get_name(drc)); 877e4b798bbSMichael Roth drc_names = g_string_insert_len(drc_names, -1, "\0", 1); 878e4b798bbSMichael Roth 879e4b798bbSMichael Roth /* ibm,drc-types */ 880e4b798bbSMichael Roth drc_types = g_string_append(drc_types, 881e4b798bbSMichael Roth spapr_drc_get_type_str(drc->type)); 882e4b798bbSMichael Roth drc_types = g_string_insert_len(drc_types, -1, "\0", 1); 883e4b798bbSMichael Roth } 884e4b798bbSMichael Roth 885e4b798bbSMichael Roth /* now write the drc count into the space we reserved at the 886e4b798bbSMichael Roth * beginning of the arrays previously 887e4b798bbSMichael Roth */ 888e4b798bbSMichael Roth *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count); 889e4b798bbSMichael Roth *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count); 890e4b798bbSMichael Roth *(uint32_t *)drc_names->str = cpu_to_be32(drc_count); 891e4b798bbSMichael Roth *(uint32_t *)drc_types->str = cpu_to_be32(drc_count); 892e4b798bbSMichael Roth 893e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes", 894e4b798bbSMichael Roth drc_indexes->data, 895e4b798bbSMichael Roth drc_indexes->len * sizeof(uint32_t)); 896e4b798bbSMichael Roth if (ret) { 897ce9863b7SCédric Le Goater error_report("Couldn't create ibm,drc-indexes property"); 898e4b798bbSMichael Roth goto out; 899e4b798bbSMichael Roth } 900e4b798bbSMichael Roth 901e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains", 902e4b798bbSMichael Roth drc_power_domains->data, 903e4b798bbSMichael Roth drc_power_domains->len * sizeof(uint32_t)); 904e4b798bbSMichael Roth if (ret) { 905ce9863b7SCédric Le Goater error_report("Couldn't finalize ibm,drc-power-domains property"); 906e4b798bbSMichael Roth goto out; 907e4b798bbSMichael Roth } 908e4b798bbSMichael Roth 909e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names", 910e4b798bbSMichael Roth drc_names->str, drc_names->len); 911e4b798bbSMichael Roth if (ret) { 912ce9863b7SCédric Le Goater error_report("Couldn't finalize ibm,drc-names property"); 913e4b798bbSMichael Roth goto out; 914e4b798bbSMichael Roth } 915e4b798bbSMichael Roth 916e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types", 917e4b798bbSMichael Roth drc_types->str, drc_types->len); 918e4b798bbSMichael Roth if (ret) { 919ce9863b7SCédric Le Goater error_report("Couldn't finalize ibm,drc-types property"); 920e4b798bbSMichael Roth goto out; 921e4b798bbSMichael Roth } 922e4b798bbSMichael Roth 923e4b798bbSMichael Roth out: 924e4b798bbSMichael Roth g_array_free(drc_indexes, true); 925e4b798bbSMichael Roth g_array_free(drc_power_domains, true); 926e4b798bbSMichael Roth g_string_free(drc_names, true); 927e4b798bbSMichael Roth g_string_free(drc_types, true); 928e4b798bbSMichael Roth 929e4b798bbSMichael Roth return ret; 930e4b798bbSMichael Roth } 931b89b3d39SDavid Gibson 932b89b3d39SDavid Gibson /* 933b89b3d39SDavid Gibson * RTAS calls 934b89b3d39SDavid Gibson */ 935b89b3d39SDavid Gibson 936b89b3d39SDavid Gibson static bool sensor_type_is_dr(uint32_t sensor_type) 937b89b3d39SDavid Gibson { 938b89b3d39SDavid Gibson switch (sensor_type) { 939b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_ISOLATION_STATE: 940b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_DR: 941b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_ALLOCATION_STATE: 942b89b3d39SDavid Gibson return true; 943b89b3d39SDavid Gibson } 944b89b3d39SDavid Gibson 945b89b3d39SDavid Gibson return false; 946b89b3d39SDavid Gibson } 947b89b3d39SDavid Gibson 948b89b3d39SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr, 949b89b3d39SDavid Gibson uint32_t token, uint32_t nargs, 950b89b3d39SDavid Gibson target_ulong args, uint32_t nret, 951b89b3d39SDavid Gibson target_ulong rets) 952b89b3d39SDavid Gibson { 953b89b3d39SDavid Gibson uint32_t sensor_type; 954b89b3d39SDavid Gibson uint32_t sensor_index; 955b89b3d39SDavid Gibson uint32_t sensor_state; 956b89b3d39SDavid Gibson uint32_t ret = RTAS_OUT_SUCCESS; 957b89b3d39SDavid Gibson sPAPRDRConnector *drc; 958b89b3d39SDavid Gibson sPAPRDRConnectorClass *drck; 959b89b3d39SDavid Gibson 960b89b3d39SDavid Gibson if (nargs != 3 || nret != 1) { 961b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 962b89b3d39SDavid Gibson goto out; 963b89b3d39SDavid Gibson } 964b89b3d39SDavid Gibson 965b89b3d39SDavid Gibson sensor_type = rtas_ld(args, 0); 966b89b3d39SDavid Gibson sensor_index = rtas_ld(args, 1); 967b89b3d39SDavid Gibson sensor_state = rtas_ld(args, 2); 968b89b3d39SDavid Gibson 969b89b3d39SDavid Gibson if (!sensor_type_is_dr(sensor_type)) { 970b89b3d39SDavid Gibson goto out_unimplemented; 971b89b3d39SDavid Gibson } 972b89b3d39SDavid Gibson 973b89b3d39SDavid Gibson /* if this is a DR sensor we can assume sensor_index == drc_index */ 974b89b3d39SDavid Gibson drc = spapr_dr_connector_by_index(sensor_index); 975b89b3d39SDavid Gibson if (!drc) { 976b89b3d39SDavid Gibson trace_spapr_rtas_set_indicator_invalid(sensor_index); 977b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 978b89b3d39SDavid Gibson goto out; 979b89b3d39SDavid Gibson } 980b89b3d39SDavid Gibson drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 981b89b3d39SDavid Gibson 982b89b3d39SDavid Gibson switch (sensor_type) { 983b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_ISOLATION_STATE: 984b89b3d39SDavid Gibson /* if the guest is configuring a device attached to this 985b89b3d39SDavid Gibson * DRC, we should reset the configuration state at this 986b89b3d39SDavid Gibson * point since it may no longer be reliable (guest released 987b89b3d39SDavid Gibson * device and needs to start over, or unplug occurred so 988b89b3d39SDavid Gibson * the FDT is no longer valid) 989b89b3d39SDavid Gibson */ 990b89b3d39SDavid Gibson if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) { 991b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr, 992b89b3d39SDavid Gibson sensor_index); 993b89b3d39SDavid Gibson if (ccs) { 994b89b3d39SDavid Gibson spapr_ccs_remove(spapr, ccs); 995b89b3d39SDavid Gibson } 996b89b3d39SDavid Gibson } 997b89b3d39SDavid Gibson ret = drck->set_isolation_state(drc, sensor_state); 998b89b3d39SDavid Gibson break; 999b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_DR: 1000b89b3d39SDavid Gibson ret = drck->set_indicator_state(drc, sensor_state); 1001b89b3d39SDavid Gibson break; 1002b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_ALLOCATION_STATE: 1003b89b3d39SDavid Gibson ret = drck->set_allocation_state(drc, sensor_state); 1004b89b3d39SDavid Gibson break; 1005b89b3d39SDavid Gibson default: 1006b89b3d39SDavid Gibson goto out_unimplemented; 1007b89b3d39SDavid Gibson } 1008b89b3d39SDavid Gibson 1009b89b3d39SDavid Gibson out: 1010b89b3d39SDavid Gibson rtas_st(rets, 0, ret); 1011b89b3d39SDavid Gibson return; 1012b89b3d39SDavid Gibson 1013b89b3d39SDavid Gibson out_unimplemented: 1014b89b3d39SDavid Gibson /* currently only DR-related sensors are implemented */ 1015b89b3d39SDavid Gibson trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type); 1016b89b3d39SDavid Gibson rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED); 1017b89b3d39SDavid Gibson } 1018b89b3d39SDavid Gibson 1019b89b3d39SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr, 1020b89b3d39SDavid Gibson uint32_t token, uint32_t nargs, 1021b89b3d39SDavid Gibson target_ulong args, uint32_t nret, 1022b89b3d39SDavid Gibson target_ulong rets) 1023b89b3d39SDavid Gibson { 1024b89b3d39SDavid Gibson uint32_t sensor_type; 1025b89b3d39SDavid Gibson uint32_t sensor_index; 1026b89b3d39SDavid Gibson uint32_t sensor_state = 0; 1027b89b3d39SDavid Gibson sPAPRDRConnector *drc; 1028b89b3d39SDavid Gibson sPAPRDRConnectorClass *drck; 1029b89b3d39SDavid Gibson uint32_t ret = RTAS_OUT_SUCCESS; 1030b89b3d39SDavid Gibson 1031b89b3d39SDavid Gibson if (nargs != 2 || nret != 2) { 1032b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 1033b89b3d39SDavid Gibson goto out; 1034b89b3d39SDavid Gibson } 1035b89b3d39SDavid Gibson 1036b89b3d39SDavid Gibson sensor_type = rtas_ld(args, 0); 1037b89b3d39SDavid Gibson sensor_index = rtas_ld(args, 1); 1038b89b3d39SDavid Gibson 1039b89b3d39SDavid Gibson if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) { 1040b89b3d39SDavid Gibson /* currently only DR-related sensors are implemented */ 1041b89b3d39SDavid Gibson trace_spapr_rtas_get_sensor_state_not_supported(sensor_index, 1042b89b3d39SDavid Gibson sensor_type); 1043b89b3d39SDavid Gibson ret = RTAS_OUT_NOT_SUPPORTED; 1044b89b3d39SDavid Gibson goto out; 1045b89b3d39SDavid Gibson } 1046b89b3d39SDavid Gibson 1047b89b3d39SDavid Gibson drc = spapr_dr_connector_by_index(sensor_index); 1048b89b3d39SDavid Gibson if (!drc) { 1049b89b3d39SDavid Gibson trace_spapr_rtas_get_sensor_state_invalid(sensor_index); 1050b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 1051b89b3d39SDavid Gibson goto out; 1052b89b3d39SDavid Gibson } 1053b89b3d39SDavid Gibson drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 1054b89b3d39SDavid Gibson ret = drck->entity_sense(drc, &sensor_state); 1055b89b3d39SDavid Gibson 1056b89b3d39SDavid Gibson out: 1057b89b3d39SDavid Gibson rtas_st(rets, 0, ret); 1058b89b3d39SDavid Gibson rtas_st(rets, 1, sensor_state); 1059b89b3d39SDavid Gibson } 1060b89b3d39SDavid Gibson 1061b89b3d39SDavid Gibson /* configure-connector work area offsets, int32_t units for field 1062b89b3d39SDavid Gibson * indexes, bytes for field offset/len values. 1063b89b3d39SDavid Gibson * 1064b89b3d39SDavid Gibson * as documented by PAPR+ v2.7, 13.5.3.5 1065b89b3d39SDavid Gibson */ 1066b89b3d39SDavid Gibson #define CC_IDX_NODE_NAME_OFFSET 2 1067b89b3d39SDavid Gibson #define CC_IDX_PROP_NAME_OFFSET 2 1068b89b3d39SDavid Gibson #define CC_IDX_PROP_LEN 3 1069b89b3d39SDavid Gibson #define CC_IDX_PROP_DATA_OFFSET 4 1070b89b3d39SDavid Gibson #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4) 1071b89b3d39SDavid Gibson #define CC_WA_LEN 4096 1072b89b3d39SDavid Gibson 1073b89b3d39SDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset, 1074b89b3d39SDavid Gibson const void *buf, size_t len) 1075b89b3d39SDavid Gibson { 1076b89b3d39SDavid Gibson cpu_physical_memory_write(ppc64_phys_to_real(addr + offset), 1077b89b3d39SDavid Gibson buf, MIN(len, CC_WA_LEN - offset)); 1078b89b3d39SDavid Gibson } 1079b89b3d39SDavid Gibson 1080b89b3d39SDavid Gibson void spapr_ccs_reset_hook(void *opaque) 1081b89b3d39SDavid Gibson { 1082b89b3d39SDavid Gibson sPAPRMachineState *spapr = opaque; 1083b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs, *ccs_tmp; 1084b89b3d39SDavid Gibson 1085b89b3d39SDavid Gibson QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) { 1086b89b3d39SDavid Gibson spapr_ccs_remove(spapr, ccs); 1087b89b3d39SDavid Gibson } 1088b89b3d39SDavid Gibson } 1089b89b3d39SDavid Gibson 1090b89b3d39SDavid Gibson static void rtas_ibm_configure_connector(PowerPCCPU *cpu, 1091b89b3d39SDavid Gibson sPAPRMachineState *spapr, 1092b89b3d39SDavid Gibson uint32_t token, uint32_t nargs, 1093b89b3d39SDavid Gibson target_ulong args, uint32_t nret, 1094b89b3d39SDavid Gibson target_ulong rets) 1095b89b3d39SDavid Gibson { 1096b89b3d39SDavid Gibson uint64_t wa_addr; 1097b89b3d39SDavid Gibson uint64_t wa_offset; 1098b89b3d39SDavid Gibson uint32_t drc_index; 1099b89b3d39SDavid Gibson sPAPRDRConnector *drc; 1100b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs; 1101b89b3d39SDavid Gibson sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE; 1102b89b3d39SDavid Gibson int rc; 1103b89b3d39SDavid Gibson 1104b89b3d39SDavid Gibson if (nargs != 2 || nret != 1) { 1105b89b3d39SDavid Gibson rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 1106b89b3d39SDavid Gibson return; 1107b89b3d39SDavid Gibson } 1108b89b3d39SDavid Gibson 1109b89b3d39SDavid Gibson wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0); 1110b89b3d39SDavid Gibson 1111b89b3d39SDavid Gibson drc_index = rtas_ld(wa_addr, 0); 1112b89b3d39SDavid Gibson drc = spapr_dr_connector_by_index(drc_index); 1113b89b3d39SDavid Gibson if (!drc) { 1114b89b3d39SDavid Gibson trace_spapr_rtas_ibm_configure_connector_invalid(drc_index); 1115b89b3d39SDavid Gibson rc = RTAS_OUT_PARAM_ERROR; 1116b89b3d39SDavid Gibson goto out; 1117b89b3d39SDavid Gibson } 1118b89b3d39SDavid Gibson 111988af6ea5SDavid Gibson if (!drc->fdt) { 1120b89b3d39SDavid Gibson trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index); 1121b89b3d39SDavid Gibson rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE; 1122b89b3d39SDavid Gibson goto out; 1123b89b3d39SDavid Gibson } 1124b89b3d39SDavid Gibson 1125b89b3d39SDavid Gibson ccs = spapr_ccs_find(spapr, drc_index); 1126b89b3d39SDavid Gibson if (!ccs) { 1127b89b3d39SDavid Gibson ccs = g_new0(sPAPRConfigureConnectorState, 1); 112888af6ea5SDavid Gibson ccs->fdt_offset = drc->fdt_start_offset; 1129b89b3d39SDavid Gibson ccs->drc_index = drc_index; 1130b89b3d39SDavid Gibson spapr_ccs_add(spapr, ccs); 1131b89b3d39SDavid Gibson } 1132b89b3d39SDavid Gibson 1133b89b3d39SDavid Gibson do { 1134b89b3d39SDavid Gibson uint32_t tag; 1135b89b3d39SDavid Gibson const char *name; 1136b89b3d39SDavid Gibson const struct fdt_property *prop; 1137b89b3d39SDavid Gibson int fdt_offset_next, prop_len; 1138b89b3d39SDavid Gibson 113988af6ea5SDavid Gibson tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next); 1140b89b3d39SDavid Gibson 1141b89b3d39SDavid Gibson switch (tag) { 1142b89b3d39SDavid Gibson case FDT_BEGIN_NODE: 1143b89b3d39SDavid Gibson ccs->fdt_depth++; 114488af6ea5SDavid Gibson name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL); 1145b89b3d39SDavid Gibson 1146b89b3d39SDavid Gibson /* provide the name of the next OF node */ 1147b89b3d39SDavid Gibson wa_offset = CC_VAL_DATA_OFFSET; 1148b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset); 1149b89b3d39SDavid Gibson configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1); 1150b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD; 1151b89b3d39SDavid Gibson break; 1152b89b3d39SDavid Gibson case FDT_END_NODE: 1153b89b3d39SDavid Gibson ccs->fdt_depth--; 1154b89b3d39SDavid Gibson if (ccs->fdt_depth == 0) { 11554f65ce00SDavid Gibson sPAPRDRIsolationState state = drc->isolation_state; 1156*0b55aa91SDavid Gibson uint32_t drc_index = spapr_drc_index(drc); 1157b89b3d39SDavid Gibson /* done sending the device tree, don't need to track 1158b89b3d39SDavid Gibson * the state anymore 1159b89b3d39SDavid Gibson */ 1160*0b55aa91SDavid Gibson trace_spapr_drc_set_configured(drc_index); 11614f65ce00SDavid Gibson if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) { 11624f65ce00SDavid Gibson drc->configured = true; 11634f65ce00SDavid Gibson } else { 11644f65ce00SDavid Gibson /* guest should be not configuring an isolated device */ 1165*0b55aa91SDavid Gibson trace_spapr_drc_set_configured_skipping(drc_index); 11664f65ce00SDavid Gibson } 1167b89b3d39SDavid Gibson spapr_ccs_remove(spapr, ccs); 1168b89b3d39SDavid Gibson ccs = NULL; 1169b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_SUCCESS; 1170b89b3d39SDavid Gibson } else { 1171b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT; 1172b89b3d39SDavid Gibson } 1173b89b3d39SDavid Gibson break; 1174b89b3d39SDavid Gibson case FDT_PROP: 117588af6ea5SDavid Gibson prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset, 1176b89b3d39SDavid Gibson &prop_len); 117788af6ea5SDavid Gibson name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff)); 1178b89b3d39SDavid Gibson 1179b89b3d39SDavid Gibson /* provide the name of the next OF property */ 1180b89b3d39SDavid Gibson wa_offset = CC_VAL_DATA_OFFSET; 1181b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset); 1182b89b3d39SDavid Gibson configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1); 1183b89b3d39SDavid Gibson 1184b89b3d39SDavid Gibson /* provide the length and value of the OF property. data gets 1185b89b3d39SDavid Gibson * placed immediately after NULL terminator of the OF property's 1186b89b3d39SDavid Gibson * name string 1187b89b3d39SDavid Gibson */ 1188b89b3d39SDavid Gibson wa_offset += strlen(name) + 1, 1189b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len); 1190b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset); 1191b89b3d39SDavid Gibson configure_connector_st(wa_addr, wa_offset, prop->data, prop_len); 1192b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY; 1193b89b3d39SDavid Gibson break; 1194b89b3d39SDavid Gibson case FDT_END: 1195b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_ERROR; 1196b89b3d39SDavid Gibson default: 1197b89b3d39SDavid Gibson /* keep seeking for an actionable tag */ 1198b89b3d39SDavid Gibson break; 1199b89b3d39SDavid Gibson } 1200b89b3d39SDavid Gibson if (ccs) { 1201b89b3d39SDavid Gibson ccs->fdt_offset = fdt_offset_next; 1202b89b3d39SDavid Gibson } 1203b89b3d39SDavid Gibson } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE); 1204b89b3d39SDavid Gibson 1205b89b3d39SDavid Gibson rc = resp; 1206b89b3d39SDavid Gibson out: 1207b89b3d39SDavid Gibson rtas_st(rets, 0, rc); 1208b89b3d39SDavid Gibson } 1209b89b3d39SDavid Gibson 1210b89b3d39SDavid Gibson static void spapr_drc_register_types(void) 1211b89b3d39SDavid Gibson { 1212b89b3d39SDavid Gibson type_register_static(&spapr_dr_connector_info); 1213b89b3d39SDavid Gibson 1214b89b3d39SDavid Gibson spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator", 1215b89b3d39SDavid Gibson rtas_set_indicator); 1216b89b3d39SDavid Gibson spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state", 1217b89b3d39SDavid Gibson rtas_get_sensor_state); 1218b89b3d39SDavid Gibson spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector", 1219b89b3d39SDavid Gibson rtas_ibm_configure_connector); 1220b89b3d39SDavid Gibson } 1221b89b3d39SDavid Gibson type_init(spapr_drc_register_types) 1222