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 302d335818SDavid Gibson sPAPRDRConnectorType spapr_drc_type(sPAPRDRConnector *drc) 312d335818SDavid Gibson { 322d335818SDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 332d335818SDavid Gibson 342d335818SDavid Gibson return 1 << drck->typeshift; 352d335818SDavid Gibson } 362d335818SDavid Gibson 370b55aa91SDavid Gibson uint32_t spapr_drc_index(sPAPRDRConnector *drc) 38bbf5c878SMichael Roth { 392d335818SDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 402d335818SDavid Gibson 41bbf5c878SMichael Roth /* no set format for a drc index: it only needs to be globally 42bbf5c878SMichael Roth * unique. this is how we encode the DRC type on bare-metal 43bbf5c878SMichael Roth * however, so might as well do that here 44bbf5c878SMichael Roth */ 452d335818SDavid Gibson return (drck->typeshift << DRC_INDEX_TYPE_SHIFT) 462d335818SDavid Gibson | (drc->id & DRC_INDEX_ID_MASK); 47bbf5c878SMichael Roth } 48bbf5c878SMichael Roth 490cb688d2SMichael Roth static uint32_t set_isolation_state(sPAPRDRConnector *drc, 50bbf5c878SMichael Roth sPAPRDRIsolationState state) 51bbf5c878SMichael Roth { 520b55aa91SDavid Gibson trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state); 53bbf5c878SMichael Roth 54b8fdd530SDavid Gibson /* if the guest is configuring a device attached to this DRC, we 55b8fdd530SDavid Gibson * should reset the configuration state at this point since it may 56b8fdd530SDavid Gibson * no longer be reliable (guest released device and needs to start 57b8fdd530SDavid Gibson * over, or unplug occurred so the FDT is no longer valid) 58b8fdd530SDavid Gibson */ 59b8fdd530SDavid Gibson if (state == SPAPR_DR_ISOLATION_STATE_ISOLATED) { 60b8fdd530SDavid Gibson g_free(drc->ccs); 61b8fdd530SDavid Gibson drc->ccs = NULL; 62b8fdd530SDavid Gibson } 63b8fdd530SDavid Gibson 649d1852ceSMichael Roth if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) { 65b12227afSStefan Weil /* cannot unisolate a non-existent resource, and, or resources 669d1852ceSMichael Roth * which are in an 'UNUSABLE' allocation state. (PAPR 2.7, 13.5.3.5) 679d1852ceSMichael Roth */ 689d1852ceSMichael Roth if (!drc->dev || 699d1852ceSMichael Roth drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 709d1852ceSMichael Roth return RTAS_OUT_NO_SUCH_INDICATOR; 719d1852ceSMichael Roth } 729d1852ceSMichael Roth } 739d1852ceSMichael Roth 74cf632463SBharata B Rao /* 75cf632463SBharata B Rao * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't 76cf632463SBharata B Rao * belong to a DIMM device that is marked for removal. 77cf632463SBharata B Rao * 78cf632463SBharata B Rao * Currently the guest userspace tool drmgr that drives the memory 79cf632463SBharata B Rao * hotplug/unplug will just try to remove a set of 'removable' LMBs 80cf632463SBharata B Rao * in response to a hot unplug request that is based on drc-count. 81cf632463SBharata B Rao * If the LMB being removed doesn't belong to a DIMM device that is 82cf632463SBharata B Rao * actually being unplugged, fail the isolation request here. 83cf632463SBharata B Rao */ 842d335818SDavid Gibson if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) { 85cf632463SBharata B Rao if ((state == SPAPR_DR_ISOLATION_STATE_ISOLATED) && 86cf632463SBharata B Rao !drc->awaiting_release) { 87cf632463SBharata B Rao return RTAS_OUT_HW_ERROR; 88cf632463SBharata B Rao } 89cf632463SBharata B Rao } 90cf632463SBharata B Rao 91bbf5c878SMichael Roth drc->isolation_state = state; 92bbf5c878SMichael Roth 93bbf5c878SMichael Roth if (drc->isolation_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) { 94bbf5c878SMichael Roth /* if we're awaiting release, but still in an unconfigured state, 95bbf5c878SMichael Roth * it's likely the guest is still in the process of configuring 96bbf5c878SMichael Roth * the device and is transitioning the devices to an ISOLATED 97bbf5c878SMichael Roth * state as a part of that process. so we only complete the 98bbf5c878SMichael Roth * removal when this transition happens for a device in a 99bbf5c878SMichael Roth * configured state, as suggested by the state diagram from 100bbf5c878SMichael Roth * PAPR+ 2.7, 13.4 101bbf5c878SMichael Roth */ 102bbf5c878SMichael Roth if (drc->awaiting_release) { 1030b55aa91SDavid Gibson uint32_t drc_index = spapr_drc_index(drc); 104bbf5c878SMichael Roth if (drc->configured) { 1050b55aa91SDavid Gibson trace_spapr_drc_set_isolation_state_finalizing(drc_index); 106*0be4e886SDavid Gibson spapr_drc_detach(drc, DEVICE(drc->dev), NULL); 107bbf5c878SMichael Roth } else { 1080b55aa91SDavid Gibson trace_spapr_drc_set_isolation_state_deferring(drc_index); 109bbf5c878SMichael Roth } 110bbf5c878SMichael Roth } 111bbf5c878SMichael Roth drc->configured = false; 112bbf5c878SMichael Roth } 113bbf5c878SMichael Roth 1140cb688d2SMichael Roth return RTAS_OUT_SUCCESS; 115bbf5c878SMichael Roth } 116bbf5c878SMichael Roth 1170cb688d2SMichael Roth static uint32_t set_allocation_state(sPAPRDRConnector *drc, 118bbf5c878SMichael Roth sPAPRDRAllocationState state) 119bbf5c878SMichael Roth { 1200b55aa91SDavid Gibson trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state); 121bbf5c878SMichael Roth 1229d1852ceSMichael Roth if (state == SPAPR_DR_ALLOCATION_STATE_USABLE) { 1239d1852ceSMichael Roth /* if there's no resource/device associated with the DRC, there's 1249d1852ceSMichael Roth * no way for us to put it in an allocation state consistent with 1259d1852ceSMichael Roth * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should 1269d1852ceSMichael Roth * result in an RTAS return code of -3 / "no such indicator" 1279d1852ceSMichael Roth */ 1289d1852ceSMichael Roth if (!drc->dev) { 1299d1852ceSMichael Roth return RTAS_OUT_NO_SUCH_INDICATOR; 1309d1852ceSMichael Roth } 131fe6824d1SLaurent Vivier if (drc->awaiting_release && drc->awaiting_allocation) { 132fe6824d1SLaurent Vivier /* kernel is acknowledging a previous hotplug event 133fe6824d1SLaurent Vivier * while we are already removing it. 134fe6824d1SLaurent Vivier * it's safe to ignore awaiting_allocation here since we know the 135fe6824d1SLaurent Vivier * situation is predicated on the guest either already having done 136fe6824d1SLaurent Vivier * so (boot-time hotplug), or never being able to acquire in the 137fe6824d1SLaurent Vivier * first place (hotplug followed by immediate unplug). 138fe6824d1SLaurent Vivier */ 139fe6824d1SLaurent Vivier drc->awaiting_allocation_skippable = true; 140fe6824d1SLaurent Vivier return RTAS_OUT_NO_SUCH_INDICATOR; 141fe6824d1SLaurent Vivier } 1429d1852ceSMichael Roth } 1439d1852ceSMichael Roth 1442d335818SDavid Gibson if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) { 145bbf5c878SMichael Roth drc->allocation_state = state; 146bbf5c878SMichael Roth if (drc->awaiting_release && 147bbf5c878SMichael Roth drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 1480b55aa91SDavid Gibson uint32_t drc_index = spapr_drc_index(drc); 1490b55aa91SDavid Gibson trace_spapr_drc_set_allocation_state_finalizing(drc_index); 150*0be4e886SDavid Gibson spapr_drc_detach(drc, DEVICE(drc->dev), NULL); 151aab99135SBharata B Rao } else if (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) { 152aab99135SBharata B Rao drc->awaiting_allocation = false; 153bbf5c878SMichael Roth } 154bbf5c878SMichael Roth } 1550cb688d2SMichael Roth return RTAS_OUT_SUCCESS; 156bbf5c878SMichael Roth } 157bbf5c878SMichael Roth 158bbf5c878SMichael Roth static const char *get_name(sPAPRDRConnector *drc) 159bbf5c878SMichael Roth { 160bbf5c878SMichael Roth return drc->name; 161bbf5c878SMichael Roth } 162bbf5c878SMichael Roth 163f40eb921SMichael Roth /* has the guest been notified of device attachment? */ 164f40eb921SMichael Roth static void set_signalled(sPAPRDRConnector *drc) 165f40eb921SMichael Roth { 166f40eb921SMichael Roth drc->signalled = true; 167f40eb921SMichael Roth } 168f40eb921SMichael Roth 169bbf5c878SMichael Roth /* 170bbf5c878SMichael Roth * dr-entity-sense sensor value 171bbf5c878SMichael Roth * returned via get-sensor-state RTAS calls 172bbf5c878SMichael Roth * as expected by state diagram in PAPR+ 2.7, 13.4 173bbf5c878SMichael Roth * based on the current allocation/indicator/power states 174bbf5c878SMichael Roth * for the DR connector. 175bbf5c878SMichael Roth */ 176f224d35bSDavid Gibson static sPAPRDREntitySense physical_entity_sense(sPAPRDRConnector *drc) 177bbf5c878SMichael Roth { 178f224d35bSDavid Gibson /* this assumes all PCI devices are assigned to a 'live insertion' 179f224d35bSDavid Gibson * power domain, where QEMU manages power state automatically as 180f224d35bSDavid Gibson * opposed to the guest. present, non-PCI resources are unaffected 181f224d35bSDavid Gibson * by power state. 182bbf5c878SMichael Roth */ 183f224d35bSDavid Gibson return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT 184f224d35bSDavid Gibson : SPAPR_DR_ENTITY_SENSE_EMPTY; 185bbf5c878SMichael Roth } 186bbf5c878SMichael Roth 187f224d35bSDavid Gibson static sPAPRDREntitySense logical_entity_sense(sPAPRDRConnector *drc) 188f224d35bSDavid Gibson { 189f224d35bSDavid Gibson if (drc->dev 190f224d35bSDavid Gibson && (drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE)) { 191f224d35bSDavid Gibson return SPAPR_DR_ENTITY_SENSE_PRESENT; 192f224d35bSDavid Gibson } else { 193f224d35bSDavid Gibson return SPAPR_DR_ENTITY_SENSE_UNUSABLE; 194f224d35bSDavid Gibson } 195bbf5c878SMichael Roth } 196bbf5c878SMichael Roth 197d7bce999SEric Blake static void prop_get_index(Object *obj, Visitor *v, const char *name, 198d7bce999SEric Blake void *opaque, Error **errp) 199bbf5c878SMichael Roth { 200bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 2010b55aa91SDavid Gibson uint32_t value = spapr_drc_index(drc); 20251e72bc1SEric Blake visit_type_uint32(v, name, &value, errp); 203bbf5c878SMichael Roth } 204bbf5c878SMichael Roth 205bbf5c878SMichael Roth static char *prop_get_name(Object *obj, Error **errp) 206bbf5c878SMichael Roth { 207bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 208bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 209bbf5c878SMichael Roth return g_strdup(drck->get_name(drc)); 210bbf5c878SMichael Roth } 211bbf5c878SMichael Roth 212d7bce999SEric Blake static void prop_get_fdt(Object *obj, Visitor *v, const char *name, 213d7bce999SEric Blake void *opaque, Error **errp) 214bbf5c878SMichael Roth { 215bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 216c75304a1SMarkus Armbruster Error *err = NULL; 217bbf5c878SMichael Roth int fdt_offset_next, fdt_offset, fdt_depth; 218bbf5c878SMichael Roth void *fdt; 219bbf5c878SMichael Roth 220bbf5c878SMichael Roth if (!drc->fdt) { 221a543a554SEric Blake visit_type_null(v, NULL, errp); 222bbf5c878SMichael Roth return; 223bbf5c878SMichael Roth } 224bbf5c878SMichael Roth 225bbf5c878SMichael Roth fdt = drc->fdt; 226bbf5c878SMichael Roth fdt_offset = drc->fdt_start_offset; 227bbf5c878SMichael Roth fdt_depth = 0; 228bbf5c878SMichael Roth 229bbf5c878SMichael Roth do { 230bbf5c878SMichael Roth const char *name = NULL; 231bbf5c878SMichael Roth const struct fdt_property *prop = NULL; 232bbf5c878SMichael Roth int prop_len = 0, name_len = 0; 233bbf5c878SMichael Roth uint32_t tag; 234bbf5c878SMichael Roth 235bbf5c878SMichael Roth tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next); 236bbf5c878SMichael Roth switch (tag) { 237bbf5c878SMichael Roth case FDT_BEGIN_NODE: 238bbf5c878SMichael Roth fdt_depth++; 239bbf5c878SMichael Roth name = fdt_get_name(fdt, fdt_offset, &name_len); 240337283dfSEric Blake visit_start_struct(v, name, NULL, 0, &err); 241c75304a1SMarkus Armbruster if (err) { 242c75304a1SMarkus Armbruster error_propagate(errp, err); 243c75304a1SMarkus Armbruster return; 244c75304a1SMarkus Armbruster } 245bbf5c878SMichael Roth break; 246bbf5c878SMichael Roth case FDT_END_NODE: 247bbf5c878SMichael Roth /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */ 248bbf5c878SMichael Roth g_assert(fdt_depth > 0); 24915c2f669SEric Blake visit_check_struct(v, &err); 2501158bb2aSEric Blake visit_end_struct(v, NULL); 251c75304a1SMarkus Armbruster if (err) { 252c75304a1SMarkus Armbruster error_propagate(errp, err); 253c75304a1SMarkus Armbruster return; 254c75304a1SMarkus Armbruster } 255bbf5c878SMichael Roth fdt_depth--; 256bbf5c878SMichael Roth break; 257bbf5c878SMichael Roth case FDT_PROP: { 258bbf5c878SMichael Roth int i; 259bbf5c878SMichael Roth prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len); 260bbf5c878SMichael Roth name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); 261d9f62ddeSEric Blake visit_start_list(v, name, NULL, 0, &err); 262c75304a1SMarkus Armbruster if (err) { 263c75304a1SMarkus Armbruster error_propagate(errp, err); 264c75304a1SMarkus Armbruster return; 265bbf5c878SMichael Roth } 266c75304a1SMarkus Armbruster for (i = 0; i < prop_len; i++) { 26751e72bc1SEric Blake visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i], &err); 268c75304a1SMarkus Armbruster if (err) { 269c75304a1SMarkus Armbruster error_propagate(errp, err); 270c75304a1SMarkus Armbruster return; 271c75304a1SMarkus Armbruster } 272c75304a1SMarkus Armbruster } 273a4a1c70dSMarkus Armbruster visit_check_list(v, &err); 2741158bb2aSEric Blake visit_end_list(v, NULL); 275a4a1c70dSMarkus Armbruster if (err) { 276a4a1c70dSMarkus Armbruster error_propagate(errp, err); 277a4a1c70dSMarkus Armbruster return; 278a4a1c70dSMarkus Armbruster } 279bbf5c878SMichael Roth break; 280bbf5c878SMichael Roth } 281bbf5c878SMichael Roth default: 282bbf5c878SMichael Roth error_setg(&error_abort, "device FDT in unexpected state: %d", tag); 283bbf5c878SMichael Roth } 284bbf5c878SMichael Roth fdt_offset = fdt_offset_next; 285bbf5c878SMichael Roth } while (fdt_depth != 0); 286bbf5c878SMichael Roth } 287bbf5c878SMichael Roth 288*0be4e886SDavid Gibson void spapr_drc_attach(sPAPRDRConnector *drc, DeviceState *d, void *fdt, 289bbf5c878SMichael Roth int fdt_start_offset, bool coldplug, Error **errp) 290bbf5c878SMichael Roth { 2910b55aa91SDavid Gibson trace_spapr_drc_attach(spapr_drc_index(drc)); 292bbf5c878SMichael Roth 293bbf5c878SMichael Roth if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) { 294bbf5c878SMichael Roth error_setg(errp, "an attached device is still awaiting release"); 295bbf5c878SMichael Roth return; 296bbf5c878SMichael Roth } 2972d335818SDavid Gibson if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) { 298bbf5c878SMichael Roth g_assert(drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE); 299bbf5c878SMichael Roth } 300bbf5c878SMichael Roth g_assert(fdt || coldplug); 301bbf5c878SMichael Roth 302bbf5c878SMichael Roth /* NOTE: setting initial isolation state to UNISOLATED means we can't 303bbf5c878SMichael Roth * detach unless guest has a userspace/kernel that moves this state 304bbf5c878SMichael Roth * back to ISOLATED in response to an unplug event, or this is done 305bbf5c878SMichael Roth * manually by the admin prior. if we force things while the guest 306bbf5c878SMichael Roth * may be accessing the device, we can easily crash the guest, so we 307bbf5c878SMichael Roth * we defer completion of removal in such cases to the reset() hook. 308bbf5c878SMichael Roth */ 3092d335818SDavid Gibson if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) { 310bbf5c878SMichael Roth drc->isolation_state = SPAPR_DR_ISOLATION_STATE_UNISOLATED; 311bbf5c878SMichael Roth } 312cd74d27eSDavid Gibson drc->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE; 313bbf5c878SMichael Roth 314bbf5c878SMichael Roth drc->dev = d; 315bbf5c878SMichael Roth drc->fdt = fdt; 316bbf5c878SMichael Roth drc->fdt_start_offset = fdt_start_offset; 317785652dcSLaurent Vivier drc->configured = coldplug; 318df18b2dbSMichael Roth /* 'logical' DR resources such as memory/cpus are in some cases treated 319df18b2dbSMichael Roth * as a pool of resources from which the guest is free to choose from 320df18b2dbSMichael Roth * based on only a count. for resources that can be assigned in this 321df18b2dbSMichael Roth * fashion, we must assume the resource is signalled immediately 322df18b2dbSMichael Roth * since a single hotplug request might make an arbitrary number of 323df18b2dbSMichael Roth * such attached resources available to the guest, as opposed to 324df18b2dbSMichael Roth * 'physical' DR resources such as PCI where each device/resource is 325df18b2dbSMichael Roth * signalled individually. 326df18b2dbSMichael Roth */ 3272d335818SDavid Gibson drc->signalled = (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) 328df18b2dbSMichael Roth ? true : coldplug; 329bbf5c878SMichael Roth 3302d335818SDavid Gibson if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI) { 331aab99135SBharata B Rao drc->awaiting_allocation = true; 332aab99135SBharata B Rao } 333aab99135SBharata B Rao 334bbf5c878SMichael Roth object_property_add_link(OBJECT(drc), "device", 335bbf5c878SMichael Roth object_get_typename(OBJECT(drc->dev)), 336bbf5c878SMichael Roth (Object **)(&drc->dev), 337bbf5c878SMichael Roth NULL, 0, NULL); 338bbf5c878SMichael Roth } 339bbf5c878SMichael Roth 340*0be4e886SDavid Gibson void spapr_drc_detach(sPAPRDRConnector *drc, DeviceState *d, Error **errp) 341bbf5c878SMichael Roth { 3420b55aa91SDavid Gibson trace_spapr_drc_detach(spapr_drc_index(drc)); 343bbf5c878SMichael Roth 344f40eb921SMichael Roth /* if we've signalled device presence to the guest, or if the guest 345f40eb921SMichael Roth * has gone ahead and configured the device (via manually-executed 346f40eb921SMichael Roth * device add via drmgr in guest, namely), we need to wait 347f40eb921SMichael Roth * for the guest to quiesce the device before completing detach. 348f40eb921SMichael Roth * Otherwise, we can assume the guest hasn't seen it and complete the 349f40eb921SMichael Roth * detach immediately. Note that there is a small race window 350f40eb921SMichael Roth * just before, or during, configuration, which is this context 351f40eb921SMichael Roth * refers mainly to fetching the device tree via RTAS. 352f40eb921SMichael Roth * During this window the device access will be arbitrated by 353f40eb921SMichael Roth * associated DRC, which will simply fail the RTAS calls as invalid. 354f40eb921SMichael Roth * This is recoverable within guest and current implementations of 355f40eb921SMichael Roth * drmgr should be able to cope. 356f40eb921SMichael Roth */ 357f40eb921SMichael Roth if (!drc->signalled && !drc->configured) { 358f40eb921SMichael Roth /* if the guest hasn't seen the device we can't rely on it to 359f40eb921SMichael Roth * set it back to an isolated state via RTAS, so do it here manually 360f40eb921SMichael Roth */ 361f40eb921SMichael Roth drc->isolation_state = SPAPR_DR_ISOLATION_STATE_ISOLATED; 362f40eb921SMichael Roth } 363f40eb921SMichael Roth 364bbf5c878SMichael Roth if (drc->isolation_state != SPAPR_DR_ISOLATION_STATE_ISOLATED) { 3650b55aa91SDavid Gibson trace_spapr_drc_awaiting_isolated(spapr_drc_index(drc)); 366bbf5c878SMichael Roth drc->awaiting_release = true; 367bbf5c878SMichael Roth return; 368bbf5c878SMichael Roth } 369bbf5c878SMichael Roth 3702d335818SDavid Gibson if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI && 371bbf5c878SMichael Roth drc->allocation_state != SPAPR_DR_ALLOCATION_STATE_UNUSABLE) { 3720b55aa91SDavid Gibson trace_spapr_drc_awaiting_unusable(spapr_drc_index(drc)); 373bbf5c878SMichael Roth drc->awaiting_release = true; 374bbf5c878SMichael Roth return; 375bbf5c878SMichael Roth } 376bbf5c878SMichael Roth 377aab99135SBharata B Rao if (drc->awaiting_allocation) { 378fe6824d1SLaurent Vivier if (!drc->awaiting_allocation_skippable) { 379aab99135SBharata B Rao drc->awaiting_release = true; 3800b55aa91SDavid Gibson trace_spapr_drc_awaiting_allocation(spapr_drc_index(drc)); 381aab99135SBharata B Rao return; 382aab99135SBharata B Rao } 383fe6824d1SLaurent Vivier } 384aab99135SBharata B Rao 385cd74d27eSDavid Gibson drc->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE; 386bbf5c878SMichael Roth 3872d335818SDavid Gibson /* Calling release callbacks based on spapr_drc_type(drc). */ 3882d335818SDavid Gibson switch (spapr_drc_type(drc)) { 38931834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_CPU: 39031834723SDaniel Henrique Barboza spapr_core_release(drc->dev); 39131834723SDaniel Henrique Barboza break; 39231834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PCI: 39331834723SDaniel Henrique Barboza spapr_phb_remove_pci_device_cb(drc->dev); 39431834723SDaniel Henrique Barboza break; 39531834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_LMB: 39631834723SDaniel Henrique Barboza spapr_lmb_release(drc->dev); 39731834723SDaniel Henrique Barboza break; 39831834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PHB: 39931834723SDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_VIO: 40031834723SDaniel Henrique Barboza default: 40131834723SDaniel Henrique Barboza g_assert(false); 402bbf5c878SMichael Roth } 403bbf5c878SMichael Roth 404bbf5c878SMichael Roth drc->awaiting_release = false; 405fe6824d1SLaurent Vivier drc->awaiting_allocation_skippable = false; 406bbf5c878SMichael Roth g_free(drc->fdt); 407bbf5c878SMichael Roth drc->fdt = NULL; 408bbf5c878SMichael Roth drc->fdt_start_offset = 0; 409bbf5c878SMichael Roth object_property_del(OBJECT(drc), "device", NULL); 410bbf5c878SMichael Roth drc->dev = NULL; 411bbf5c878SMichael Roth } 412bbf5c878SMichael Roth 413bbf5c878SMichael Roth static bool release_pending(sPAPRDRConnector *drc) 414bbf5c878SMichael Roth { 415bbf5c878SMichael Roth return drc->awaiting_release; 416bbf5c878SMichael Roth } 417bbf5c878SMichael Roth 418bbf5c878SMichael Roth static void reset(DeviceState *d) 419bbf5c878SMichael Roth { 420bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 421bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 422bbf5c878SMichael Roth 4230b55aa91SDavid Gibson trace_spapr_drc_reset(spapr_drc_index(drc)); 424b8fdd530SDavid Gibson 425b8fdd530SDavid Gibson g_free(drc->ccs); 426b8fdd530SDavid Gibson drc->ccs = NULL; 427b8fdd530SDavid Gibson 428bbf5c878SMichael Roth /* immediately upon reset we can safely assume DRCs whose devices 429bbf5c878SMichael Roth * are pending removal can be safely removed, and that they will 430bbf5c878SMichael Roth * subsequently be left in an ISOLATED state. move the DRC to this 431bbf5c878SMichael Roth * state in these cases (which will in turn complete any pending 432bbf5c878SMichael Roth * device removals) 433bbf5c878SMichael Roth */ 434bbf5c878SMichael Roth if (drc->awaiting_release) { 435bbf5c878SMichael Roth drck->set_isolation_state(drc, SPAPR_DR_ISOLATION_STATE_ISOLATED); 436bbf5c878SMichael Roth /* generally this should also finalize the removal, but if the device 437bbf5c878SMichael Roth * hasn't yet been configured we normally defer removal under the 438bbf5c878SMichael Roth * assumption that this transition is taking place as part of device 439bbf5c878SMichael Roth * configuration. so check if we're still waiting after this, and 440bbf5c878SMichael Roth * force removal if we are 441bbf5c878SMichael Roth */ 442bbf5c878SMichael Roth if (drc->awaiting_release) { 443*0be4e886SDavid Gibson spapr_drc_detach(drc, DEVICE(drc->dev), NULL); 444bbf5c878SMichael Roth } 445bbf5c878SMichael Roth 446bbf5c878SMichael Roth /* non-PCI devices may be awaiting a transition to UNUSABLE */ 4472d335818SDavid Gibson if (spapr_drc_type(drc) != SPAPR_DR_CONNECTOR_TYPE_PCI && 448bbf5c878SMichael Roth drc->awaiting_release) { 449bbf5c878SMichael Roth drck->set_allocation_state(drc, SPAPR_DR_ALLOCATION_STATE_UNUSABLE); 450bbf5c878SMichael Roth } 451bbf5c878SMichael Roth } 452f40eb921SMichael Roth 453f224d35bSDavid Gibson if (drck->dr_entity_sense(drc) == SPAPR_DR_ENTITY_SENSE_PRESENT) { 454f40eb921SMichael Roth drck->set_signalled(drc); 455f40eb921SMichael Roth } 456bbf5c878SMichael Roth } 457bbf5c878SMichael Roth 458a50919ddSDaniel Henrique Barboza static bool spapr_drc_needed(void *opaque) 459a50919ddSDaniel Henrique Barboza { 460a50919ddSDaniel Henrique Barboza sPAPRDRConnector *drc = (sPAPRDRConnector *)opaque; 461a50919ddSDaniel Henrique Barboza sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 462a50919ddSDaniel Henrique Barboza bool rc = false; 463f224d35bSDavid Gibson sPAPRDREntitySense value = drck->dr_entity_sense(drc); 464a50919ddSDaniel Henrique Barboza 465a50919ddSDaniel Henrique Barboza /* If no dev is plugged in there is no need to migrate the DRC state */ 466a50919ddSDaniel Henrique Barboza if (value != SPAPR_DR_ENTITY_SENSE_PRESENT) { 467a50919ddSDaniel Henrique Barboza return false; 468a50919ddSDaniel Henrique Barboza } 469a50919ddSDaniel Henrique Barboza 470a50919ddSDaniel Henrique Barboza /* 471a50919ddSDaniel Henrique Barboza * If there is dev plugged in, we need to migrate the DRC state when 472a50919ddSDaniel Henrique Barboza * it is different from cold-plugged state 473a50919ddSDaniel Henrique Barboza */ 4742d335818SDavid Gibson switch (spapr_drc_type(drc)) { 475a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PCI: 476a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_CPU: 477a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_LMB: 478a32e900bSGreg Kurz rc = !((drc->isolation_state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) && 479a32e900bSGreg Kurz (drc->allocation_state == SPAPR_DR_ALLOCATION_STATE_USABLE) && 480a50919ddSDaniel Henrique Barboza drc->configured && drc->signalled && !drc->awaiting_release); 481a50919ddSDaniel Henrique Barboza break; 482a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_PHB: 483a50919ddSDaniel Henrique Barboza case SPAPR_DR_CONNECTOR_TYPE_VIO: 484a50919ddSDaniel Henrique Barboza default: 485a32e900bSGreg Kurz g_assert_not_reached(); 486a50919ddSDaniel Henrique Barboza } 487a50919ddSDaniel Henrique Barboza return rc; 488a50919ddSDaniel Henrique Barboza } 489a50919ddSDaniel Henrique Barboza 490a50919ddSDaniel Henrique Barboza static const VMStateDescription vmstate_spapr_drc = { 491a50919ddSDaniel Henrique Barboza .name = "spapr_drc", 492a50919ddSDaniel Henrique Barboza .version_id = 1, 493a50919ddSDaniel Henrique Barboza .minimum_version_id = 1, 494a50919ddSDaniel Henrique Barboza .needed = spapr_drc_needed, 495a50919ddSDaniel Henrique Barboza .fields = (VMStateField []) { 496a50919ddSDaniel Henrique Barboza VMSTATE_UINT32(isolation_state, sPAPRDRConnector), 497a50919ddSDaniel Henrique Barboza VMSTATE_UINT32(allocation_state, sPAPRDRConnector), 498cd74d27eSDavid Gibson VMSTATE_UINT32(dr_indicator, sPAPRDRConnector), 499a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(configured, sPAPRDRConnector), 500a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(awaiting_release, sPAPRDRConnector), 501a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(awaiting_allocation, sPAPRDRConnector), 502a50919ddSDaniel Henrique Barboza VMSTATE_BOOL(signalled, sPAPRDRConnector), 503a50919ddSDaniel Henrique Barboza VMSTATE_END_OF_LIST() 504a50919ddSDaniel Henrique Barboza } 505a50919ddSDaniel Henrique Barboza }; 506a50919ddSDaniel Henrique Barboza 507bbf5c878SMichael Roth static void realize(DeviceState *d, Error **errp) 508bbf5c878SMichael Roth { 509bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 510bbf5c878SMichael Roth Object *root_container; 511bbf5c878SMichael Roth char link_name[256]; 512bbf5c878SMichael Roth gchar *child_name; 513bbf5c878SMichael Roth Error *err = NULL; 514bbf5c878SMichael Roth 5150b55aa91SDavid Gibson trace_spapr_drc_realize(spapr_drc_index(drc)); 516bbf5c878SMichael Roth /* NOTE: we do this as part of realize/unrealize due to the fact 517bbf5c878SMichael Roth * that the guest will communicate with the DRC via RTAS calls 518bbf5c878SMichael Roth * referencing the global DRC index. By unlinking the DRC 519bbf5c878SMichael Roth * from DRC_CONTAINER_PATH/<drc_index> we effectively make it 520bbf5c878SMichael Roth * inaccessible by the guest, since lookups rely on this path 521bbf5c878SMichael Roth * existing in the composition tree 522bbf5c878SMichael Roth */ 523bbf5c878SMichael Roth root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 5240b55aa91SDavid Gibson snprintf(link_name, sizeof(link_name), "%x", spapr_drc_index(drc)); 525bbf5c878SMichael Roth child_name = object_get_canonical_path_component(OBJECT(drc)); 5260b55aa91SDavid Gibson trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name); 527bbf5c878SMichael Roth object_property_add_alias(root_container, link_name, 528bbf5c878SMichael Roth drc->owner, child_name, &err); 529bbf5c878SMichael Roth if (err) { 5304fffeb5eSMarkus Armbruster error_report_err(err); 531bbf5c878SMichael Roth object_unref(OBJECT(drc)); 532bbf5c878SMichael Roth } 533586d2142SGonglei g_free(child_name); 5340b55aa91SDavid Gibson vmstate_register(DEVICE(drc), spapr_drc_index(drc), &vmstate_spapr_drc, 535a50919ddSDaniel Henrique Barboza drc); 5360b55aa91SDavid Gibson trace_spapr_drc_realize_complete(spapr_drc_index(drc)); 537bbf5c878SMichael Roth } 538bbf5c878SMichael Roth 539bbf5c878SMichael Roth static void unrealize(DeviceState *d, Error **errp) 540bbf5c878SMichael Roth { 541bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(d); 542bbf5c878SMichael Roth Object *root_container; 543bbf5c878SMichael Roth char name[256]; 544bbf5c878SMichael Roth Error *err = NULL; 545bbf5c878SMichael Roth 5460b55aa91SDavid Gibson trace_spapr_drc_unrealize(spapr_drc_index(drc)); 547bbf5c878SMichael Roth root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 5480b55aa91SDavid Gibson snprintf(name, sizeof(name), "%x", spapr_drc_index(drc)); 549bbf5c878SMichael Roth object_property_del(root_container, name, &err); 550bbf5c878SMichael Roth if (err) { 5514fffeb5eSMarkus Armbruster error_report_err(err); 552bbf5c878SMichael Roth object_unref(OBJECT(drc)); 553bbf5c878SMichael Roth } 554bbf5c878SMichael Roth } 555bbf5c878SMichael Roth 5562d335818SDavid Gibson sPAPRDRConnector *spapr_dr_connector_new(Object *owner, const char *type, 557bbf5c878SMichael Roth uint32_t id) 558bbf5c878SMichael Roth { 5592d335818SDavid Gibson sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(object_new(type)); 56094649d42SDavid Gibson char *prop_name; 561bbf5c878SMichael Roth 562bbf5c878SMichael Roth drc->id = id; 563bbf5c878SMichael Roth drc->owner = owner; 5640b55aa91SDavid Gibson prop_name = g_strdup_printf("dr-connector[%"PRIu32"]", 5650b55aa91SDavid Gibson spapr_drc_index(drc)); 56694649d42SDavid Gibson object_property_add_child(owner, prop_name, OBJECT(drc), NULL); 567bbf5c878SMichael Roth object_property_set_bool(OBJECT(drc), true, "realized", NULL); 56894649d42SDavid Gibson g_free(prop_name); 569bbf5c878SMichael Roth 570bbf5c878SMichael Roth /* human-readable name for a DRC to encode into the DT 571bbf5c878SMichael Roth * description. this is mainly only used within a guest in place 572bbf5c878SMichael Roth * of the unique DRC index. 573bbf5c878SMichael Roth * 574bbf5c878SMichael Roth * in the case of VIO/PCI devices, it corresponds to a 575bbf5c878SMichael Roth * "location code" that maps a logical device/function (DRC index) 576bbf5c878SMichael Roth * to a physical (or virtual in the case of VIO) location in the 577bbf5c878SMichael Roth * system by chaining together the "location label" for each 578bbf5c878SMichael Roth * encapsulating component. 579bbf5c878SMichael Roth * 580bbf5c878SMichael Roth * since this is more to do with diagnosing physical hardware 581bbf5c878SMichael Roth * issues than guest compatibility, we choose location codes/DRC 582bbf5c878SMichael Roth * names that adhere to the documented format, but avoid encoding 583bbf5c878SMichael Roth * the entire topology information into the label/code, instead 584bbf5c878SMichael Roth * just using the location codes based on the labels for the 585bbf5c878SMichael Roth * endpoints (VIO/PCI adaptor connectors), which is basically 586bbf5c878SMichael Roth * just "C" followed by an integer ID. 587bbf5c878SMichael Roth * 588bbf5c878SMichael Roth * DRC names as documented by PAPR+ v2.7, 13.5.2.4 589bbf5c878SMichael Roth * location codes as documented by PAPR+ v2.7, 12.3.1.5 590bbf5c878SMichael Roth */ 5912d335818SDavid Gibson switch (spapr_drc_type(drc)) { 592bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_CPU: 593bbf5c878SMichael Roth drc->name = g_strdup_printf("CPU %d", id); 594bbf5c878SMichael Roth break; 595bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_PHB: 596bbf5c878SMichael Roth drc->name = g_strdup_printf("PHB %d", id); 597bbf5c878SMichael Roth break; 598bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_VIO: 599bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_PCI: 600bbf5c878SMichael Roth drc->name = g_strdup_printf("C%d", id); 601bbf5c878SMichael Roth break; 602bbf5c878SMichael Roth case SPAPR_DR_CONNECTOR_TYPE_LMB: 603bbf5c878SMichael Roth drc->name = g_strdup_printf("LMB %d", id); 604bbf5c878SMichael Roth break; 605bbf5c878SMichael Roth default: 606bbf5c878SMichael Roth g_assert(false); 607bbf5c878SMichael Roth } 608bbf5c878SMichael Roth 609bbf5c878SMichael Roth /* PCI slot always start in a USABLE state, and stay there */ 6102d335818SDavid Gibson if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_PCI) { 611bbf5c878SMichael Roth drc->allocation_state = SPAPR_DR_ALLOCATION_STATE_USABLE; 612bbf5c878SMichael Roth } 613bbf5c878SMichael Roth 614bbf5c878SMichael Roth return drc; 615bbf5c878SMichael Roth } 616bbf5c878SMichael Roth 617bbf5c878SMichael Roth static void spapr_dr_connector_instance_init(Object *obj) 618bbf5c878SMichael Roth { 619bbf5c878SMichael Roth sPAPRDRConnector *drc = SPAPR_DR_CONNECTOR(obj); 620bbf5c878SMichael Roth 621bbf5c878SMichael Roth object_property_add_uint32_ptr(obj, "id", &drc->id, NULL); 622bbf5c878SMichael Roth object_property_add(obj, "index", "uint32", prop_get_index, 623bbf5c878SMichael Roth NULL, NULL, NULL, NULL); 624bbf5c878SMichael Roth object_property_add_str(obj, "name", prop_get_name, NULL, NULL); 625bbf5c878SMichael Roth object_property_add(obj, "fdt", "struct", prop_get_fdt, 626bbf5c878SMichael Roth NULL, NULL, NULL, NULL); 627bbf5c878SMichael Roth } 628bbf5c878SMichael Roth 629bbf5c878SMichael Roth static void spapr_dr_connector_class_init(ObjectClass *k, void *data) 630bbf5c878SMichael Roth { 631bbf5c878SMichael Roth DeviceClass *dk = DEVICE_CLASS(k); 632bbf5c878SMichael Roth sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 633bbf5c878SMichael Roth 634bbf5c878SMichael Roth dk->reset = reset; 635bbf5c878SMichael Roth dk->realize = realize; 636bbf5c878SMichael Roth dk->unrealize = unrealize; 637bbf5c878SMichael Roth drck->set_isolation_state = set_isolation_state; 638bbf5c878SMichael Roth drck->set_allocation_state = set_allocation_state; 639bbf5c878SMichael Roth drck->get_name = get_name; 640bbf5c878SMichael Roth drck->release_pending = release_pending; 641f40eb921SMichael Roth drck->set_signalled = set_signalled; 642c401ae8cSMarkus Armbruster /* 643c401ae8cSMarkus Armbruster * Reason: it crashes FIXME find and document the real reason 644c401ae8cSMarkus Armbruster */ 645e90f2a8cSEduardo Habkost dk->user_creatable = false; 646bbf5c878SMichael Roth } 647bbf5c878SMichael Roth 648f224d35bSDavid Gibson static void spapr_drc_physical_class_init(ObjectClass *k, void *data) 649f224d35bSDavid Gibson { 650f224d35bSDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 651f224d35bSDavid Gibson 652f224d35bSDavid Gibson drck->dr_entity_sense = physical_entity_sense; 653f224d35bSDavid Gibson } 654f224d35bSDavid Gibson 655f224d35bSDavid Gibson static void spapr_drc_logical_class_init(ObjectClass *k, void *data) 656f224d35bSDavid Gibson { 657f224d35bSDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 658f224d35bSDavid Gibson 659f224d35bSDavid Gibson drck->dr_entity_sense = logical_entity_sense; 660f224d35bSDavid Gibson } 661f224d35bSDavid Gibson 6622d335818SDavid Gibson static void spapr_drc_cpu_class_init(ObjectClass *k, void *data) 6632d335818SDavid Gibson { 6642d335818SDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 6652d335818SDavid Gibson 6662d335818SDavid Gibson drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU; 6671693ea16SDavid Gibson drck->typename = "CPU"; 6682d335818SDavid Gibson } 6692d335818SDavid Gibson 6702d335818SDavid Gibson static void spapr_drc_pci_class_init(ObjectClass *k, void *data) 6712d335818SDavid Gibson { 6722d335818SDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 6732d335818SDavid Gibson 6742d335818SDavid Gibson drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI; 6751693ea16SDavid Gibson drck->typename = "28"; 6762d335818SDavid Gibson } 6772d335818SDavid Gibson 6782d335818SDavid Gibson static void spapr_drc_lmb_class_init(ObjectClass *k, void *data) 6792d335818SDavid Gibson { 6802d335818SDavid Gibson sPAPRDRConnectorClass *drck = SPAPR_DR_CONNECTOR_CLASS(k); 6812d335818SDavid Gibson 6822d335818SDavid Gibson drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB; 6831693ea16SDavid Gibson drck->typename = "MEM"; 6842d335818SDavid Gibson } 6852d335818SDavid Gibson 686bbf5c878SMichael Roth static const TypeInfo spapr_dr_connector_info = { 687bbf5c878SMichael Roth .name = TYPE_SPAPR_DR_CONNECTOR, 688bbf5c878SMichael Roth .parent = TYPE_DEVICE, 689bbf5c878SMichael Roth .instance_size = sizeof(sPAPRDRConnector), 690bbf5c878SMichael Roth .instance_init = spapr_dr_connector_instance_init, 691bbf5c878SMichael Roth .class_size = sizeof(sPAPRDRConnectorClass), 692bbf5c878SMichael Roth .class_init = spapr_dr_connector_class_init, 6932d335818SDavid Gibson .abstract = true, 6942d335818SDavid Gibson }; 6952d335818SDavid Gibson 6962d335818SDavid Gibson static const TypeInfo spapr_drc_physical_info = { 6972d335818SDavid Gibson .name = TYPE_SPAPR_DRC_PHYSICAL, 6982d335818SDavid Gibson .parent = TYPE_SPAPR_DR_CONNECTOR, 6992d335818SDavid Gibson .instance_size = sizeof(sPAPRDRConnector), 700f224d35bSDavid Gibson .class_init = spapr_drc_physical_class_init, 7012d335818SDavid Gibson .abstract = true, 7022d335818SDavid Gibson }; 7032d335818SDavid Gibson 7042d335818SDavid Gibson static const TypeInfo spapr_drc_logical_info = { 7052d335818SDavid Gibson .name = TYPE_SPAPR_DRC_LOGICAL, 7062d335818SDavid Gibson .parent = TYPE_SPAPR_DR_CONNECTOR, 7072d335818SDavid Gibson .instance_size = sizeof(sPAPRDRConnector), 708f224d35bSDavid Gibson .class_init = spapr_drc_logical_class_init, 7092d335818SDavid Gibson .abstract = true, 7102d335818SDavid Gibson }; 7112d335818SDavid Gibson 7122d335818SDavid Gibson static const TypeInfo spapr_drc_cpu_info = { 7132d335818SDavid Gibson .name = TYPE_SPAPR_DRC_CPU, 7142d335818SDavid Gibson .parent = TYPE_SPAPR_DRC_LOGICAL, 7152d335818SDavid Gibson .instance_size = sizeof(sPAPRDRConnector), 7162d335818SDavid Gibson .class_init = spapr_drc_cpu_class_init, 7172d335818SDavid Gibson }; 7182d335818SDavid Gibson 7192d335818SDavid Gibson static const TypeInfo spapr_drc_pci_info = { 7202d335818SDavid Gibson .name = TYPE_SPAPR_DRC_PCI, 7212d335818SDavid Gibson .parent = TYPE_SPAPR_DRC_PHYSICAL, 7222d335818SDavid Gibson .instance_size = sizeof(sPAPRDRConnector), 7232d335818SDavid Gibson .class_init = spapr_drc_pci_class_init, 7242d335818SDavid Gibson }; 7252d335818SDavid Gibson 7262d335818SDavid Gibson static const TypeInfo spapr_drc_lmb_info = { 7272d335818SDavid Gibson .name = TYPE_SPAPR_DRC_LMB, 7282d335818SDavid Gibson .parent = TYPE_SPAPR_DRC_LOGICAL, 7292d335818SDavid Gibson .instance_size = sizeof(sPAPRDRConnector), 7302d335818SDavid Gibson .class_init = spapr_drc_lmb_class_init, 731bbf5c878SMichael Roth }; 732bbf5c878SMichael Roth 733bbf5c878SMichael Roth /* helper functions for external users */ 734bbf5c878SMichael Roth 735fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_index(uint32_t index) 736bbf5c878SMichael Roth { 737bbf5c878SMichael Roth Object *obj; 738bbf5c878SMichael Roth char name[256]; 739bbf5c878SMichael Roth 740bbf5c878SMichael Roth snprintf(name, sizeof(name), "%s/%x", DRC_CONTAINER_PATH, index); 741bbf5c878SMichael Roth obj = object_resolve_path(name, NULL); 742bbf5c878SMichael Roth 743bbf5c878SMichael Roth return !obj ? NULL : SPAPR_DR_CONNECTOR(obj); 744bbf5c878SMichael Roth } 745bbf5c878SMichael Roth 746fbf55397SDavid Gibson sPAPRDRConnector *spapr_drc_by_id(const char *type, uint32_t id) 747bbf5c878SMichael Roth { 748fbf55397SDavid Gibson sPAPRDRConnectorClass *drck 749fbf55397SDavid Gibson = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type)); 750fbf55397SDavid Gibson 751fbf55397SDavid Gibson return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT 752fbf55397SDavid Gibson | (id & DRC_INDEX_ID_MASK)); 753bbf5c878SMichael Roth } 754e4b798bbSMichael Roth 755e4b798bbSMichael Roth /** 756e4b798bbSMichael Roth * spapr_drc_populate_dt 757e4b798bbSMichael Roth * 758e4b798bbSMichael Roth * @fdt: libfdt device tree 759e4b798bbSMichael Roth * @path: path in the DT to generate properties 760e4b798bbSMichael Roth * @owner: parent Object/DeviceState for which to generate DRC 761e4b798bbSMichael Roth * descriptions for 762e4b798bbSMichael Roth * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding 763e4b798bbSMichael Roth * to the types of DRCs to generate entries for 764e4b798bbSMichael Roth * 765e4b798bbSMichael Roth * generate OF properties to describe DRC topology/indices to guests 766e4b798bbSMichael Roth * 767e4b798bbSMichael Roth * as documented in PAPR+ v2.1, 13.5.2 768e4b798bbSMichael Roth */ 769e4b798bbSMichael Roth int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner, 770e4b798bbSMichael Roth uint32_t drc_type_mask) 771e4b798bbSMichael Roth { 772e4b798bbSMichael Roth Object *root_container; 773e4b798bbSMichael Roth ObjectProperty *prop; 7747746abd8SDaniel P. Berrange ObjectPropertyIterator iter; 775e4b798bbSMichael Roth uint32_t drc_count = 0; 776e4b798bbSMichael Roth GArray *drc_indexes, *drc_power_domains; 777e4b798bbSMichael Roth GString *drc_names, *drc_types; 778e4b798bbSMichael Roth int ret; 779e4b798bbSMichael Roth 780e4b798bbSMichael Roth /* the first entry of each properties is a 32-bit integer encoding 781e4b798bbSMichael Roth * the number of elements in the array. we won't know this until 782e4b798bbSMichael Roth * we complete the iteration through all the matching DRCs, but 783e4b798bbSMichael Roth * reserve the space now and set the offsets accordingly so we 784e4b798bbSMichael Roth * can fill them in later. 785e4b798bbSMichael Roth */ 786e4b798bbSMichael Roth drc_indexes = g_array_new(false, true, sizeof(uint32_t)); 787e4b798bbSMichael Roth drc_indexes = g_array_set_size(drc_indexes, 1); 788e4b798bbSMichael Roth drc_power_domains = g_array_new(false, true, sizeof(uint32_t)); 789e4b798bbSMichael Roth drc_power_domains = g_array_set_size(drc_power_domains, 1); 790e4b798bbSMichael Roth drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); 791e4b798bbSMichael Roth drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); 792e4b798bbSMichael Roth 793e4b798bbSMichael Roth /* aliases for all DRConnector objects will be rooted in QOM 794e4b798bbSMichael Roth * composition tree at DRC_CONTAINER_PATH 795e4b798bbSMichael Roth */ 796e4b798bbSMichael Roth root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); 797e4b798bbSMichael Roth 7987746abd8SDaniel P. Berrange object_property_iter_init(&iter, root_container); 7997746abd8SDaniel P. Berrange while ((prop = object_property_iter_next(&iter))) { 800e4b798bbSMichael Roth Object *obj; 801e4b798bbSMichael Roth sPAPRDRConnector *drc; 802e4b798bbSMichael Roth sPAPRDRConnectorClass *drck; 803e4b798bbSMichael Roth uint32_t drc_index, drc_power_domain; 804e4b798bbSMichael Roth 805e4b798bbSMichael Roth if (!strstart(prop->type, "link<", NULL)) { 806e4b798bbSMichael Roth continue; 807e4b798bbSMichael Roth } 808e4b798bbSMichael Roth 809e4b798bbSMichael Roth obj = object_property_get_link(root_container, prop->name, NULL); 810e4b798bbSMichael Roth drc = SPAPR_DR_CONNECTOR(obj); 811e4b798bbSMichael Roth drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 812e4b798bbSMichael Roth 813e4b798bbSMichael Roth if (owner && (drc->owner != owner)) { 814e4b798bbSMichael Roth continue; 815e4b798bbSMichael Roth } 816e4b798bbSMichael Roth 8172d335818SDavid Gibson if ((spapr_drc_type(drc) & drc_type_mask) == 0) { 818e4b798bbSMichael Roth continue; 819e4b798bbSMichael Roth } 820e4b798bbSMichael Roth 821e4b798bbSMichael Roth drc_count++; 822e4b798bbSMichael Roth 823e4b798bbSMichael Roth /* ibm,drc-indexes */ 8240b55aa91SDavid Gibson drc_index = cpu_to_be32(spapr_drc_index(drc)); 825e4b798bbSMichael Roth g_array_append_val(drc_indexes, drc_index); 826e4b798bbSMichael Roth 827e4b798bbSMichael Roth /* ibm,drc-power-domains */ 828e4b798bbSMichael Roth drc_power_domain = cpu_to_be32(-1); 829e4b798bbSMichael Roth g_array_append_val(drc_power_domains, drc_power_domain); 830e4b798bbSMichael Roth 831e4b798bbSMichael Roth /* ibm,drc-names */ 832e4b798bbSMichael Roth drc_names = g_string_append(drc_names, drck->get_name(drc)); 833e4b798bbSMichael Roth drc_names = g_string_insert_len(drc_names, -1, "\0", 1); 834e4b798bbSMichael Roth 835e4b798bbSMichael Roth /* ibm,drc-types */ 8361693ea16SDavid Gibson drc_types = g_string_append(drc_types, drck->typename); 837e4b798bbSMichael Roth drc_types = g_string_insert_len(drc_types, -1, "\0", 1); 838e4b798bbSMichael Roth } 839e4b798bbSMichael Roth 840e4b798bbSMichael Roth /* now write the drc count into the space we reserved at the 841e4b798bbSMichael Roth * beginning of the arrays previously 842e4b798bbSMichael Roth */ 843e4b798bbSMichael Roth *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count); 844e4b798bbSMichael Roth *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count); 845e4b798bbSMichael Roth *(uint32_t *)drc_names->str = cpu_to_be32(drc_count); 846e4b798bbSMichael Roth *(uint32_t *)drc_types->str = cpu_to_be32(drc_count); 847e4b798bbSMichael Roth 848e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes", 849e4b798bbSMichael Roth drc_indexes->data, 850e4b798bbSMichael Roth drc_indexes->len * sizeof(uint32_t)); 851e4b798bbSMichael Roth if (ret) { 852ce9863b7SCédric Le Goater error_report("Couldn't create ibm,drc-indexes property"); 853e4b798bbSMichael Roth goto out; 854e4b798bbSMichael Roth } 855e4b798bbSMichael Roth 856e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains", 857e4b798bbSMichael Roth drc_power_domains->data, 858e4b798bbSMichael Roth drc_power_domains->len * sizeof(uint32_t)); 859e4b798bbSMichael Roth if (ret) { 860ce9863b7SCédric Le Goater error_report("Couldn't finalize ibm,drc-power-domains property"); 861e4b798bbSMichael Roth goto out; 862e4b798bbSMichael Roth } 863e4b798bbSMichael Roth 864e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names", 865e4b798bbSMichael Roth drc_names->str, drc_names->len); 866e4b798bbSMichael Roth if (ret) { 867ce9863b7SCédric Le Goater error_report("Couldn't finalize ibm,drc-names property"); 868e4b798bbSMichael Roth goto out; 869e4b798bbSMichael Roth } 870e4b798bbSMichael Roth 871e4b798bbSMichael Roth ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types", 872e4b798bbSMichael Roth drc_types->str, drc_types->len); 873e4b798bbSMichael Roth if (ret) { 874ce9863b7SCédric Le Goater error_report("Couldn't finalize ibm,drc-types property"); 875e4b798bbSMichael Roth goto out; 876e4b798bbSMichael Roth } 877e4b798bbSMichael Roth 878e4b798bbSMichael Roth out: 879e4b798bbSMichael Roth g_array_free(drc_indexes, true); 880e4b798bbSMichael Roth g_array_free(drc_power_domains, true); 881e4b798bbSMichael Roth g_string_free(drc_names, true); 882e4b798bbSMichael Roth g_string_free(drc_types, true); 883e4b798bbSMichael Roth 884e4b798bbSMichael Roth return ret; 885e4b798bbSMichael Roth } 886b89b3d39SDavid Gibson 887b89b3d39SDavid Gibson /* 888b89b3d39SDavid Gibson * RTAS calls 889b89b3d39SDavid Gibson */ 890b89b3d39SDavid Gibson 8917b7258f8SDavid Gibson static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state) 892b89b3d39SDavid Gibson { 8937b7258f8SDavid Gibson sPAPRDRConnector *drc = spapr_drc_by_index(idx); 8947b7258f8SDavid Gibson sPAPRDRConnectorClass *drck; 8957b7258f8SDavid Gibson 8967b7258f8SDavid Gibson if (!drc) { 8977b7258f8SDavid Gibson return RTAS_OUT_PARAM_ERROR; 898b89b3d39SDavid Gibson } 899b89b3d39SDavid Gibson 9007b7258f8SDavid Gibson drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 9017b7258f8SDavid Gibson return drck->set_isolation_state(drc, state); 9027b7258f8SDavid Gibson } 9037b7258f8SDavid Gibson 9047b7258f8SDavid Gibson static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state) 9057b7258f8SDavid Gibson { 9067b7258f8SDavid Gibson sPAPRDRConnector *drc = spapr_drc_by_index(idx); 9077b7258f8SDavid Gibson sPAPRDRConnectorClass *drck; 9087b7258f8SDavid Gibson 9097b7258f8SDavid Gibson if (!drc) { 9107b7258f8SDavid Gibson return RTAS_OUT_PARAM_ERROR; 9117b7258f8SDavid Gibson } 9127b7258f8SDavid Gibson 9137b7258f8SDavid Gibson drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 9147b7258f8SDavid Gibson return drck->set_allocation_state(drc, state); 9157b7258f8SDavid Gibson } 9167b7258f8SDavid Gibson 917cd74d27eSDavid Gibson static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state) 9187b7258f8SDavid Gibson { 9197b7258f8SDavid Gibson sPAPRDRConnector *drc = spapr_drc_by_index(idx); 9207b7258f8SDavid Gibson 9217b7258f8SDavid Gibson if (!drc) { 9227b7258f8SDavid Gibson return RTAS_OUT_PARAM_ERROR; 9237b7258f8SDavid Gibson } 9247b7258f8SDavid Gibson 925cd74d27eSDavid Gibson trace_spapr_drc_set_dr_indicator(idx, state); 926cd74d27eSDavid Gibson drc->dr_indicator = state; 927cd74d27eSDavid Gibson return RTAS_OUT_SUCCESS; 928b89b3d39SDavid Gibson } 929b89b3d39SDavid Gibson 930b89b3d39SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr, 9317b7258f8SDavid Gibson uint32_t token, 9327b7258f8SDavid Gibson uint32_t nargs, target_ulong args, 9337b7258f8SDavid Gibson uint32_t nret, target_ulong rets) 934b89b3d39SDavid Gibson { 9357b7258f8SDavid Gibson uint32_t type, idx, state; 936b89b3d39SDavid Gibson uint32_t ret = RTAS_OUT_SUCCESS; 937b89b3d39SDavid Gibson 938b89b3d39SDavid Gibson if (nargs != 3 || nret != 1) { 939b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 940b89b3d39SDavid Gibson goto out; 941b89b3d39SDavid Gibson } 942b89b3d39SDavid Gibson 9437b7258f8SDavid Gibson type = rtas_ld(args, 0); 9447b7258f8SDavid Gibson idx = rtas_ld(args, 1); 9457b7258f8SDavid Gibson state = rtas_ld(args, 2); 946b89b3d39SDavid Gibson 9477b7258f8SDavid Gibson switch (type) { 948b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_ISOLATION_STATE: 9497b7258f8SDavid Gibson ret = rtas_set_isolation_state(idx, state); 950b89b3d39SDavid Gibson break; 951b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_DR: 952cd74d27eSDavid Gibson ret = rtas_set_dr_indicator(idx, state); 953b89b3d39SDavid Gibson break; 954b89b3d39SDavid Gibson case RTAS_SENSOR_TYPE_ALLOCATION_STATE: 9557b7258f8SDavid Gibson ret = rtas_set_allocation_state(idx, state); 956b89b3d39SDavid Gibson break; 957b89b3d39SDavid Gibson default: 9587b7258f8SDavid Gibson ret = RTAS_OUT_NOT_SUPPORTED; 959b89b3d39SDavid Gibson } 960b89b3d39SDavid Gibson 961b89b3d39SDavid Gibson out: 962b89b3d39SDavid Gibson rtas_st(rets, 0, ret); 963b89b3d39SDavid Gibson } 964b89b3d39SDavid Gibson 965b89b3d39SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr, 966b89b3d39SDavid Gibson uint32_t token, uint32_t nargs, 967b89b3d39SDavid Gibson target_ulong args, uint32_t nret, 968b89b3d39SDavid Gibson target_ulong rets) 969b89b3d39SDavid Gibson { 970b89b3d39SDavid Gibson uint32_t sensor_type; 971b89b3d39SDavid Gibson uint32_t sensor_index; 972b89b3d39SDavid Gibson uint32_t sensor_state = 0; 973b89b3d39SDavid Gibson sPAPRDRConnector *drc; 974b89b3d39SDavid Gibson sPAPRDRConnectorClass *drck; 975b89b3d39SDavid Gibson uint32_t ret = RTAS_OUT_SUCCESS; 976b89b3d39SDavid Gibson 977b89b3d39SDavid Gibson if (nargs != 2 || nret != 2) { 978b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 979b89b3d39SDavid Gibson goto out; 980b89b3d39SDavid Gibson } 981b89b3d39SDavid Gibson 982b89b3d39SDavid Gibson sensor_type = rtas_ld(args, 0); 983b89b3d39SDavid Gibson sensor_index = rtas_ld(args, 1); 984b89b3d39SDavid Gibson 985b89b3d39SDavid Gibson if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) { 986b89b3d39SDavid Gibson /* currently only DR-related sensors are implemented */ 987b89b3d39SDavid Gibson trace_spapr_rtas_get_sensor_state_not_supported(sensor_index, 988b89b3d39SDavid Gibson sensor_type); 989b89b3d39SDavid Gibson ret = RTAS_OUT_NOT_SUPPORTED; 990b89b3d39SDavid Gibson goto out; 991b89b3d39SDavid Gibson } 992b89b3d39SDavid Gibson 993fbf55397SDavid Gibson drc = spapr_drc_by_index(sensor_index); 994b89b3d39SDavid Gibson if (!drc) { 995b89b3d39SDavid Gibson trace_spapr_rtas_get_sensor_state_invalid(sensor_index); 996b89b3d39SDavid Gibson ret = RTAS_OUT_PARAM_ERROR; 997b89b3d39SDavid Gibson goto out; 998b89b3d39SDavid Gibson } 999b89b3d39SDavid Gibson drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); 1000f224d35bSDavid Gibson sensor_state = drck->dr_entity_sense(drc); 1001b89b3d39SDavid Gibson 1002b89b3d39SDavid Gibson out: 1003b89b3d39SDavid Gibson rtas_st(rets, 0, ret); 1004b89b3d39SDavid Gibson rtas_st(rets, 1, sensor_state); 1005b89b3d39SDavid Gibson } 1006b89b3d39SDavid Gibson 1007b89b3d39SDavid Gibson /* configure-connector work area offsets, int32_t units for field 1008b89b3d39SDavid Gibson * indexes, bytes for field offset/len values. 1009b89b3d39SDavid Gibson * 1010b89b3d39SDavid Gibson * as documented by PAPR+ v2.7, 13.5.3.5 1011b89b3d39SDavid Gibson */ 1012b89b3d39SDavid Gibson #define CC_IDX_NODE_NAME_OFFSET 2 1013b89b3d39SDavid Gibson #define CC_IDX_PROP_NAME_OFFSET 2 1014b89b3d39SDavid Gibson #define CC_IDX_PROP_LEN 3 1015b89b3d39SDavid Gibson #define CC_IDX_PROP_DATA_OFFSET 4 1016b89b3d39SDavid Gibson #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4) 1017b89b3d39SDavid Gibson #define CC_WA_LEN 4096 1018b89b3d39SDavid Gibson 1019b89b3d39SDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset, 1020b89b3d39SDavid Gibson const void *buf, size_t len) 1021b89b3d39SDavid Gibson { 1022b89b3d39SDavid Gibson cpu_physical_memory_write(ppc64_phys_to_real(addr + offset), 1023b89b3d39SDavid Gibson buf, MIN(len, CC_WA_LEN - offset)); 1024b89b3d39SDavid Gibson } 1025b89b3d39SDavid Gibson 1026b89b3d39SDavid Gibson static void rtas_ibm_configure_connector(PowerPCCPU *cpu, 1027b89b3d39SDavid Gibson sPAPRMachineState *spapr, 1028b89b3d39SDavid Gibson uint32_t token, uint32_t nargs, 1029b89b3d39SDavid Gibson target_ulong args, uint32_t nret, 1030b89b3d39SDavid Gibson target_ulong rets) 1031b89b3d39SDavid Gibson { 1032b89b3d39SDavid Gibson uint64_t wa_addr; 1033b89b3d39SDavid Gibson uint64_t wa_offset; 1034b89b3d39SDavid Gibson uint32_t drc_index; 1035b89b3d39SDavid Gibson sPAPRDRConnector *drc; 1036b89b3d39SDavid Gibson sPAPRConfigureConnectorState *ccs; 1037b89b3d39SDavid Gibson sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE; 1038b89b3d39SDavid Gibson int rc; 1039b89b3d39SDavid Gibson 1040b89b3d39SDavid Gibson if (nargs != 2 || nret != 1) { 1041b89b3d39SDavid Gibson rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 1042b89b3d39SDavid Gibson return; 1043b89b3d39SDavid Gibson } 1044b89b3d39SDavid Gibson 1045b89b3d39SDavid Gibson wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0); 1046b89b3d39SDavid Gibson 1047b89b3d39SDavid Gibson drc_index = rtas_ld(wa_addr, 0); 1048fbf55397SDavid Gibson drc = spapr_drc_by_index(drc_index); 1049b89b3d39SDavid Gibson if (!drc) { 1050b89b3d39SDavid Gibson trace_spapr_rtas_ibm_configure_connector_invalid(drc_index); 1051b89b3d39SDavid Gibson rc = RTAS_OUT_PARAM_ERROR; 1052b89b3d39SDavid Gibson goto out; 1053b89b3d39SDavid Gibson } 1054b89b3d39SDavid Gibson 105588af6ea5SDavid Gibson if (!drc->fdt) { 1056b89b3d39SDavid Gibson trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index); 1057b89b3d39SDavid Gibson rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE; 1058b89b3d39SDavid Gibson goto out; 1059b89b3d39SDavid Gibson } 1060b89b3d39SDavid Gibson 1061b8fdd530SDavid Gibson ccs = drc->ccs; 1062b89b3d39SDavid Gibson if (!ccs) { 1063b89b3d39SDavid Gibson ccs = g_new0(sPAPRConfigureConnectorState, 1); 106488af6ea5SDavid Gibson ccs->fdt_offset = drc->fdt_start_offset; 1065b8fdd530SDavid Gibson drc->ccs = ccs; 1066b89b3d39SDavid Gibson } 1067b89b3d39SDavid Gibson 1068b89b3d39SDavid Gibson do { 1069b89b3d39SDavid Gibson uint32_t tag; 1070b89b3d39SDavid Gibson const char *name; 1071b89b3d39SDavid Gibson const struct fdt_property *prop; 1072b89b3d39SDavid Gibson int fdt_offset_next, prop_len; 1073b89b3d39SDavid Gibson 107488af6ea5SDavid Gibson tag = fdt_next_tag(drc->fdt, ccs->fdt_offset, &fdt_offset_next); 1075b89b3d39SDavid Gibson 1076b89b3d39SDavid Gibson switch (tag) { 1077b89b3d39SDavid Gibson case FDT_BEGIN_NODE: 1078b89b3d39SDavid Gibson ccs->fdt_depth++; 107988af6ea5SDavid Gibson name = fdt_get_name(drc->fdt, ccs->fdt_offset, NULL); 1080b89b3d39SDavid Gibson 1081b89b3d39SDavid Gibson /* provide the name of the next OF node */ 1082b89b3d39SDavid Gibson wa_offset = CC_VAL_DATA_OFFSET; 1083b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset); 1084b89b3d39SDavid Gibson configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1); 1085b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD; 1086b89b3d39SDavid Gibson break; 1087b89b3d39SDavid Gibson case FDT_END_NODE: 1088b89b3d39SDavid Gibson ccs->fdt_depth--; 1089b89b3d39SDavid Gibson if (ccs->fdt_depth == 0) { 10904f65ce00SDavid Gibson sPAPRDRIsolationState state = drc->isolation_state; 10910b55aa91SDavid Gibson uint32_t drc_index = spapr_drc_index(drc); 1092b89b3d39SDavid Gibson /* done sending the device tree, don't need to track 1093b89b3d39SDavid Gibson * the state anymore 1094b89b3d39SDavid Gibson */ 10950b55aa91SDavid Gibson trace_spapr_drc_set_configured(drc_index); 10964f65ce00SDavid Gibson if (state == SPAPR_DR_ISOLATION_STATE_UNISOLATED) { 10974f65ce00SDavid Gibson drc->configured = true; 10984f65ce00SDavid Gibson } else { 10994f65ce00SDavid Gibson /* guest should be not configuring an isolated device */ 11000b55aa91SDavid Gibson trace_spapr_drc_set_configured_skipping(drc_index); 11014f65ce00SDavid Gibson } 1102b8fdd530SDavid Gibson g_free(ccs); 1103b8fdd530SDavid Gibson drc->ccs = NULL; 1104b89b3d39SDavid Gibson ccs = NULL; 1105b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_SUCCESS; 1106b89b3d39SDavid Gibson } else { 1107b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT; 1108b89b3d39SDavid Gibson } 1109b89b3d39SDavid Gibson break; 1110b89b3d39SDavid Gibson case FDT_PROP: 111188af6ea5SDavid Gibson prop = fdt_get_property_by_offset(drc->fdt, ccs->fdt_offset, 1112b89b3d39SDavid Gibson &prop_len); 111388af6ea5SDavid Gibson name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff)); 1114b89b3d39SDavid Gibson 1115b89b3d39SDavid Gibson /* provide the name of the next OF property */ 1116b89b3d39SDavid Gibson wa_offset = CC_VAL_DATA_OFFSET; 1117b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset); 1118b89b3d39SDavid Gibson configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1); 1119b89b3d39SDavid Gibson 1120b89b3d39SDavid Gibson /* provide the length and value of the OF property. data gets 1121b89b3d39SDavid Gibson * placed immediately after NULL terminator of the OF property's 1122b89b3d39SDavid Gibson * name string 1123b89b3d39SDavid Gibson */ 1124b89b3d39SDavid Gibson wa_offset += strlen(name) + 1, 1125b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len); 1126b89b3d39SDavid Gibson rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset); 1127b89b3d39SDavid Gibson configure_connector_st(wa_addr, wa_offset, prop->data, prop_len); 1128b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY; 1129b89b3d39SDavid Gibson break; 1130b89b3d39SDavid Gibson case FDT_END: 1131b89b3d39SDavid Gibson resp = SPAPR_DR_CC_RESPONSE_ERROR; 1132b89b3d39SDavid Gibson default: 1133b89b3d39SDavid Gibson /* keep seeking for an actionable tag */ 1134b89b3d39SDavid Gibson break; 1135b89b3d39SDavid Gibson } 1136b89b3d39SDavid Gibson if (ccs) { 1137b89b3d39SDavid Gibson ccs->fdt_offset = fdt_offset_next; 1138b89b3d39SDavid Gibson } 1139b89b3d39SDavid Gibson } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE); 1140b89b3d39SDavid Gibson 1141b89b3d39SDavid Gibson rc = resp; 1142b89b3d39SDavid Gibson out: 1143b89b3d39SDavid Gibson rtas_st(rets, 0, rc); 1144b89b3d39SDavid Gibson } 1145b89b3d39SDavid Gibson 1146b89b3d39SDavid Gibson static void spapr_drc_register_types(void) 1147b89b3d39SDavid Gibson { 1148b89b3d39SDavid Gibson type_register_static(&spapr_dr_connector_info); 11492d335818SDavid Gibson type_register_static(&spapr_drc_physical_info); 11502d335818SDavid Gibson type_register_static(&spapr_drc_logical_info); 11512d335818SDavid Gibson type_register_static(&spapr_drc_cpu_info); 11522d335818SDavid Gibson type_register_static(&spapr_drc_pci_info); 11532d335818SDavid Gibson type_register_static(&spapr_drc_lmb_info); 1154b89b3d39SDavid Gibson 1155b89b3d39SDavid Gibson spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator", 1156b89b3d39SDavid Gibson rtas_set_indicator); 1157b89b3d39SDavid Gibson spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state", 1158b89b3d39SDavid Gibson rtas_get_sensor_state); 1159b89b3d39SDavid Gibson spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector", 1160b89b3d39SDavid Gibson rtas_ibm_configure_connector); 1161b89b3d39SDavid Gibson } 1162b89b3d39SDavid Gibson type_init(spapr_drc_register_types) 1163