spapr_drc.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * QEMU SPAPR Dynamic Reconfiguration Connector Implementation
  3. *
  4. * Copyright IBM Corp. 2014
  5. *
  6. * Authors:
  7. * Michael Roth <mdroth@linux.vnet.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qapi/error.h"
  14. #include "qobject/qnull.h"
  15. #include "qemu/cutils.h"
  16. #include "hw/ppc/spapr_drc.h"
  17. #include "qom/object.h"
  18. #include "migration/vmstate.h"
  19. #include "qapi/qapi-events-qdev.h"
  20. #include "qapi/visitor.h"
  21. #include "qemu/error-report.h"
  22. #include "hw/ppc/spapr.h" /* for RTAS return codes */
  23. #include "hw/pci-host/spapr.h" /* spapr_phb_remove_pci_device_cb callback */
  24. #include "hw/ppc/spapr_nvdimm.h"
  25. #include "system/device_tree.h"
  26. #include "system/reset.h"
  27. #include "trace.h"
  28. #define DRC_CONTAINER_PATH "dr-connector"
  29. #define DRC_INDEX_TYPE_SHIFT 28
  30. #define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
  31. SpaprDrcType spapr_drc_type(SpaprDrc *drc)
  32. {
  33. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  34. return 1 << drck->typeshift;
  35. }
  36. uint32_t spapr_drc_index(SpaprDrc *drc)
  37. {
  38. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  39. /* no set format for a drc index: it only needs to be globally
  40. * unique. this is how we encode the DRC type on bare-metal
  41. * however, so might as well do that here
  42. */
  43. return (drck->typeshift << DRC_INDEX_TYPE_SHIFT)
  44. | (drc->id & DRC_INDEX_ID_MASK);
  45. }
  46. static void spapr_drc_release(SpaprDrc *drc)
  47. {
  48. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  49. drck->release(drc->dev);
  50. drc->unplug_requested = false;
  51. g_free(drc->fdt);
  52. drc->fdt = NULL;
  53. drc->fdt_start_offset = 0;
  54. object_property_del(OBJECT(drc), "device");
  55. drc->dev = NULL;
  56. }
  57. static uint32_t drc_isolate_physical(SpaprDrc *drc)
  58. {
  59. switch (drc->state) {
  60. case SPAPR_DRC_STATE_PHYSICAL_POWERON:
  61. return RTAS_OUT_SUCCESS; /* Nothing to do */
  62. case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
  63. break; /* see below */
  64. case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
  65. return RTAS_OUT_PARAM_ERROR; /* not allowed */
  66. default:
  67. g_assert_not_reached();
  68. }
  69. drc->state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
  70. if (drc->unplug_requested) {
  71. uint32_t drc_index = spapr_drc_index(drc);
  72. trace_spapr_drc_set_isolation_state_finalizing(drc_index);
  73. spapr_drc_release(drc);
  74. }
  75. return RTAS_OUT_SUCCESS;
  76. }
  77. static uint32_t drc_unisolate_physical(SpaprDrc *drc)
  78. {
  79. switch (drc->state) {
  80. case SPAPR_DRC_STATE_PHYSICAL_UNISOLATE:
  81. case SPAPR_DRC_STATE_PHYSICAL_CONFIGURED:
  82. return RTAS_OUT_SUCCESS; /* Nothing to do */
  83. case SPAPR_DRC_STATE_PHYSICAL_POWERON:
  84. break; /* see below */
  85. default:
  86. g_assert_not_reached();
  87. }
  88. /* cannot unisolate a non-existent resource, and, or resources
  89. * which are in an 'UNUSABLE' allocation state. (PAPR 2.7,
  90. * 13.5.3.5)
  91. */
  92. if (!drc->dev) {
  93. return RTAS_OUT_NO_SUCH_INDICATOR;
  94. }
  95. drc->state = SPAPR_DRC_STATE_PHYSICAL_UNISOLATE;
  96. drc->ccs_offset = drc->fdt_start_offset;
  97. drc->ccs_depth = 0;
  98. return RTAS_OUT_SUCCESS;
  99. }
  100. static uint32_t drc_isolate_logical(SpaprDrc *drc)
  101. {
  102. switch (drc->state) {
  103. case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
  104. case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
  105. return RTAS_OUT_SUCCESS; /* Nothing to do */
  106. case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
  107. break; /* see below */
  108. case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
  109. return RTAS_OUT_PARAM_ERROR; /* not allowed */
  110. default:
  111. g_assert_not_reached();
  112. }
  113. /*
  114. * Fail any requests to ISOLATE the LMB DRC if this LMB doesn't
  115. * belong to a DIMM device that is marked for removal.
  116. *
  117. * Currently the guest userspace tool drmgr that drives the memory
  118. * hotplug/unplug will just try to remove a set of 'removable' LMBs
  119. * in response to a hot unplug request that is based on drc-count.
  120. * If the LMB being removed doesn't belong to a DIMM device that is
  121. * actually being unplugged, fail the isolation request here.
  122. */
  123. if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB
  124. && !drc->unplug_requested) {
  125. return RTAS_OUT_HW_ERROR;
  126. }
  127. drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
  128. return RTAS_OUT_SUCCESS;
  129. }
  130. static uint32_t drc_unisolate_logical(SpaprDrc *drc)
  131. {
  132. SpaprMachineState *spapr = NULL;
  133. switch (drc->state) {
  134. case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
  135. case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
  136. /*
  137. * Unisolating a logical DRC that was marked for unplug
  138. * means that the kernel is refusing the removal.
  139. */
  140. if (drc->unplug_requested && drc->dev) {
  141. if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB) {
  142. spapr = SPAPR_MACHINE(qdev_get_machine());
  143. spapr_memory_unplug_rollback(spapr, drc->dev);
  144. }
  145. drc->unplug_requested = false;
  146. if (drc->dev->id) {
  147. error_report("Device hotunplug rejected by the guest "
  148. "for device %s", drc->dev->id);
  149. }
  150. qapi_event_send_device_unplug_guest_error(drc->dev->id,
  151. drc->dev->canonical_path);
  152. }
  153. return RTAS_OUT_SUCCESS; /* Nothing to do */
  154. case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
  155. break; /* see below */
  156. case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
  157. return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
  158. default:
  159. g_assert_not_reached();
  160. }
  161. /* Move to AVAILABLE state should have ensured device was present */
  162. g_assert(drc->dev);
  163. drc->state = SPAPR_DRC_STATE_LOGICAL_UNISOLATE;
  164. drc->ccs_offset = drc->fdt_start_offset;
  165. drc->ccs_depth = 0;
  166. return RTAS_OUT_SUCCESS;
  167. }
  168. static uint32_t drc_set_usable(SpaprDrc *drc)
  169. {
  170. switch (drc->state) {
  171. case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
  172. case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
  173. case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
  174. return RTAS_OUT_SUCCESS; /* Nothing to do */
  175. case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
  176. break; /* see below */
  177. default:
  178. g_assert_not_reached();
  179. }
  180. /* if there's no resource/device associated with the DRC, there's
  181. * no way for us to put it in an allocation state consistent with
  182. * being 'USABLE'. PAPR 2.7, 13.5.3.4 documents that this should
  183. * result in an RTAS return code of -3 / "no such indicator"
  184. */
  185. if (!drc->dev) {
  186. return RTAS_OUT_NO_SUCH_INDICATOR;
  187. }
  188. if (drc->unplug_requested) {
  189. /* Don't allow the guest to move a device away from UNUSABLE
  190. * state when we want to unplug it */
  191. return RTAS_OUT_NO_SUCH_INDICATOR;
  192. }
  193. drc->state = SPAPR_DRC_STATE_LOGICAL_AVAILABLE;
  194. return RTAS_OUT_SUCCESS;
  195. }
  196. static uint32_t drc_set_unusable(SpaprDrc *drc)
  197. {
  198. switch (drc->state) {
  199. case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
  200. return RTAS_OUT_SUCCESS; /* Nothing to do */
  201. case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
  202. break; /* see below */
  203. case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
  204. case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
  205. return RTAS_OUT_NO_SUCH_INDICATOR; /* not allowed */
  206. default:
  207. g_assert_not_reached();
  208. }
  209. drc->state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
  210. if (drc->unplug_requested) {
  211. uint32_t drc_index = spapr_drc_index(drc);
  212. trace_spapr_drc_set_allocation_state_finalizing(drc_index);
  213. spapr_drc_release(drc);
  214. }
  215. return RTAS_OUT_SUCCESS;
  216. }
  217. static char *spapr_drc_name(SpaprDrc *drc)
  218. {
  219. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  220. /* human-readable name for a DRC to encode into the DT
  221. * description. this is mainly only used within a guest in place
  222. * of the unique DRC index.
  223. *
  224. * in the case of VIO/PCI devices, it corresponds to a "location
  225. * code" that maps a logical device/function (DRC index) to a
  226. * physical (or virtual in the case of VIO) location in the system
  227. * by chaining together the "location label" for each
  228. * encapsulating component.
  229. *
  230. * since this is more to do with diagnosing physical hardware
  231. * issues than guest compatibility, we choose location codes/DRC
  232. * names that adhere to the documented format, but avoid encoding
  233. * the entire topology information into the label/code, instead
  234. * just using the location codes based on the labels for the
  235. * endpoints (VIO/PCI adaptor connectors), which is basically just
  236. * "C" followed by an integer ID.
  237. *
  238. * DRC names as documented by PAPR+ v2.7, 13.5.2.4
  239. * location codes as documented by PAPR+ v2.7, 12.3.1.5
  240. */
  241. return g_strdup_printf("%s%d", drck->drc_name_prefix, drc->id);
  242. }
  243. /*
  244. * dr-entity-sense sensor value
  245. * returned via get-sensor-state RTAS calls
  246. * as expected by state diagram in PAPR+ 2.7, 13.4
  247. * based on the current allocation/indicator/power states
  248. * for the DR connector.
  249. */
  250. static SpaprDREntitySense physical_entity_sense(SpaprDrc *drc)
  251. {
  252. /* this assumes all PCI devices are assigned to a 'live insertion'
  253. * power domain, where QEMU manages power state automatically as
  254. * opposed to the guest. present, non-PCI resources are unaffected
  255. * by power state.
  256. */
  257. return drc->dev ? SPAPR_DR_ENTITY_SENSE_PRESENT
  258. : SPAPR_DR_ENTITY_SENSE_EMPTY;
  259. }
  260. static SpaprDREntitySense logical_entity_sense(SpaprDrc *drc)
  261. {
  262. switch (drc->state) {
  263. case SPAPR_DRC_STATE_LOGICAL_UNUSABLE:
  264. return SPAPR_DR_ENTITY_SENSE_UNUSABLE;
  265. case SPAPR_DRC_STATE_LOGICAL_AVAILABLE:
  266. case SPAPR_DRC_STATE_LOGICAL_UNISOLATE:
  267. case SPAPR_DRC_STATE_LOGICAL_CONFIGURED:
  268. g_assert(drc->dev);
  269. return SPAPR_DR_ENTITY_SENSE_PRESENT;
  270. default:
  271. g_assert_not_reached();
  272. }
  273. }
  274. static void prop_get_index(Object *obj, Visitor *v, const char *name,
  275. void *opaque, Error **errp)
  276. {
  277. SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
  278. uint32_t value = spapr_drc_index(drc);
  279. visit_type_uint32(v, name, &value, errp);
  280. }
  281. static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
  282. void *opaque, Error **errp)
  283. {
  284. SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
  285. QNull *null = NULL;
  286. int fdt_offset_next, fdt_offset, fdt_depth;
  287. void *fdt;
  288. if (!drc->fdt) {
  289. visit_type_null(v, NULL, &null, errp);
  290. qobject_unref(null);
  291. return;
  292. }
  293. fdt = drc->fdt;
  294. fdt_offset = drc->fdt_start_offset;
  295. fdt_depth = 0;
  296. do {
  297. const char *dt_name = NULL;
  298. const struct fdt_property *prop = NULL;
  299. int prop_len = 0, name_len = 0;
  300. uint32_t tag;
  301. bool ok;
  302. tag = fdt_next_tag(fdt, fdt_offset, &fdt_offset_next);
  303. switch (tag) {
  304. case FDT_BEGIN_NODE:
  305. fdt_depth++;
  306. dt_name = fdt_get_name(fdt, fdt_offset, &name_len);
  307. if (!visit_start_struct(v, dt_name, NULL, 0, errp)) {
  308. return;
  309. }
  310. break;
  311. case FDT_END_NODE:
  312. /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
  313. g_assert(fdt_depth > 0);
  314. ok = visit_check_struct(v, errp);
  315. visit_end_struct(v, NULL);
  316. if (!ok) {
  317. return;
  318. }
  319. fdt_depth--;
  320. break;
  321. case FDT_PROP: {
  322. int i;
  323. prop = fdt_get_property_by_offset(fdt, fdt_offset, &prop_len);
  324. dt_name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  325. if (!visit_start_list(v, dt_name, NULL, 0, errp)) {
  326. return;
  327. }
  328. for (i = 0; i < prop_len; i++) {
  329. if (!visit_type_uint8(v, NULL, (uint8_t *)&prop->data[i],
  330. errp)) {
  331. return;
  332. }
  333. }
  334. ok = visit_check_list(v, errp);
  335. visit_end_list(v, NULL);
  336. if (!ok) {
  337. return;
  338. }
  339. break;
  340. }
  341. default:
  342. error_report("device FDT in unexpected state: %d", tag);
  343. abort();
  344. }
  345. fdt_offset = fdt_offset_next;
  346. } while (fdt_depth != 0);
  347. }
  348. void spapr_drc_attach(SpaprDrc *drc, DeviceState *d)
  349. {
  350. trace_spapr_drc_attach(spapr_drc_index(drc));
  351. g_assert(!drc->dev);
  352. g_assert((drc->state == SPAPR_DRC_STATE_LOGICAL_UNUSABLE)
  353. || (drc->state == SPAPR_DRC_STATE_PHYSICAL_POWERON));
  354. drc->dev = d;
  355. object_property_add_link(OBJECT(drc), "device",
  356. object_get_typename(OBJECT(drc->dev)),
  357. (Object **)(&drc->dev),
  358. NULL, 0);
  359. }
  360. void spapr_drc_unplug_request(SpaprDrc *drc)
  361. {
  362. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  363. trace_spapr_drc_unplug_request(spapr_drc_index(drc));
  364. g_assert(drc->dev);
  365. drc->unplug_requested = true;
  366. if (drc->state != drck->empty_state) {
  367. trace_spapr_drc_awaiting_quiesce(spapr_drc_index(drc));
  368. return;
  369. }
  370. spapr_drc_release(drc);
  371. }
  372. bool spapr_drc_reset(SpaprDrc *drc)
  373. {
  374. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  375. bool unplug_completed = false;
  376. trace_spapr_drc_reset(spapr_drc_index(drc));
  377. /* immediately upon reset we can safely assume DRCs whose devices
  378. * are pending removal can be safely removed.
  379. */
  380. if (drc->unplug_requested) {
  381. spapr_drc_release(drc);
  382. unplug_completed = true;
  383. }
  384. if (drc->dev) {
  385. /* A device present at reset is ready to go, same as coldplugged */
  386. drc->state = drck->ready_state;
  387. /*
  388. * Ensure that we are able to send the FDT fragment again
  389. * via configure-connector call if the guest requests.
  390. */
  391. drc->ccs_offset = drc->fdt_start_offset;
  392. drc->ccs_depth = 0;
  393. } else {
  394. drc->state = drck->empty_state;
  395. drc->ccs_offset = -1;
  396. drc->ccs_depth = -1;
  397. }
  398. return unplug_completed;
  399. }
  400. static bool spapr_drc_unplug_requested_needed(void *opaque)
  401. {
  402. return spapr_drc_unplug_requested(opaque);
  403. }
  404. static const VMStateDescription vmstate_spapr_drc_unplug_requested = {
  405. .name = "spapr_drc/unplug_requested",
  406. .version_id = 1,
  407. .minimum_version_id = 1,
  408. .needed = spapr_drc_unplug_requested_needed,
  409. .fields = (const VMStateField []) {
  410. VMSTATE_BOOL(unplug_requested, SpaprDrc),
  411. VMSTATE_END_OF_LIST()
  412. }
  413. };
  414. static bool spapr_drc_needed(void *opaque)
  415. {
  416. SpaprDrc *drc = opaque;
  417. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  418. /*
  419. * If no dev is plugged in there is no need to migrate the DRC state
  420. * nor to reset the DRC at CAS.
  421. */
  422. if (!drc->dev) {
  423. return false;
  424. }
  425. /*
  426. * We need to reset the DRC at CAS or to migrate the DRC state if it's
  427. * not equal to the expected long-term state, which is the same as the
  428. * coldplugged initial state, or if an unplug request is pending.
  429. */
  430. return drc->state != drck->ready_state ||
  431. spapr_drc_unplug_requested(drc);
  432. }
  433. static const VMStateDescription vmstate_spapr_drc = {
  434. .name = "spapr_drc",
  435. .version_id = 1,
  436. .minimum_version_id = 1,
  437. .needed = spapr_drc_needed,
  438. .fields = (const VMStateField []) {
  439. VMSTATE_UINT32(state, SpaprDrc),
  440. VMSTATE_END_OF_LIST()
  441. },
  442. .subsections = (const VMStateDescription * const []) {
  443. &vmstate_spapr_drc_unplug_requested,
  444. NULL
  445. }
  446. };
  447. static void drc_container_create(void)
  448. {
  449. object_property_add_new_container(object_get_root(), DRC_CONTAINER_PATH);
  450. }
  451. static Object *drc_container_get(void)
  452. {
  453. return object_resolve_path_component(object_get_root(), DRC_CONTAINER_PATH);
  454. }
  455. static void drc_realize(DeviceState *d, Error **errp)
  456. {
  457. SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
  458. g_autofree gchar *link_name = g_strdup_printf("%x", spapr_drc_index(drc));
  459. Object *root_container;
  460. const char *child_name;
  461. trace_spapr_drc_realize(spapr_drc_index(drc));
  462. /* NOTE: we do this as part of realize/unrealize due to the fact
  463. * that the guest will communicate with the DRC via RTAS calls
  464. * referencing the global DRC index. By unlinking the DRC
  465. * from DRC_CONTAINER_PATH/<drc_index> we effectively make it
  466. * inaccessible by the guest, since lookups rely on this path
  467. * existing in the composition tree
  468. */
  469. root_container = drc_container_get();
  470. child_name = object_get_canonical_path_component(OBJECT(drc));
  471. trace_spapr_drc_realize_child(spapr_drc_index(drc), child_name);
  472. object_property_add_alias(root_container, link_name,
  473. drc->owner, child_name);
  474. vmstate_register(VMSTATE_IF(drc), spapr_drc_index(drc), &vmstate_spapr_drc,
  475. drc);
  476. trace_spapr_drc_realize_complete(spapr_drc_index(drc));
  477. }
  478. static void drc_unrealize(DeviceState *d)
  479. {
  480. SpaprDrc *drc = SPAPR_DR_CONNECTOR(d);
  481. g_autofree gchar *name = g_strdup_printf("%x", spapr_drc_index(drc));
  482. trace_spapr_drc_unrealize(spapr_drc_index(drc));
  483. vmstate_unregister(VMSTATE_IF(drc), &vmstate_spapr_drc, drc);
  484. object_property_del(drc_container_get(), name);
  485. }
  486. SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type,
  487. uint32_t id)
  488. {
  489. SpaprDrc *drc = SPAPR_DR_CONNECTOR(object_new(type));
  490. g_autofree char *prop_name = NULL;
  491. drc->id = id;
  492. drc->owner = owner;
  493. prop_name = g_strdup_printf("dr-connector[%"PRIu32"]",
  494. spapr_drc_index(drc));
  495. object_property_add_child(owner, prop_name, OBJECT(drc));
  496. object_unref(OBJECT(drc));
  497. qdev_realize(DEVICE(drc), NULL, NULL);
  498. return drc;
  499. }
  500. static void spapr_dr_connector_instance_init(Object *obj)
  501. {
  502. SpaprDrc *drc = SPAPR_DR_CONNECTOR(obj);
  503. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  504. object_property_add_uint32_ptr(obj, "id", &drc->id, OBJ_PROP_FLAG_READ);
  505. object_property_add(obj, "index", "uint32", prop_get_index,
  506. NULL, NULL, NULL);
  507. object_property_add(obj, "fdt", "struct", prop_get_fdt,
  508. NULL, NULL, NULL);
  509. drc->state = drck->empty_state;
  510. }
  511. static void spapr_dr_connector_class_init(ObjectClass *k, void *data)
  512. {
  513. DeviceClass *dk = DEVICE_CLASS(k);
  514. drc_container_create();
  515. dk->realize = drc_realize;
  516. dk->unrealize = drc_unrealize;
  517. /*
  518. * Reason: DR connector needs to be wired to either the machine or to a
  519. * PHB in spapr_dr_connector_new().
  520. */
  521. dk->user_creatable = false;
  522. }
  523. static bool drc_physical_needed(void *opaque)
  524. {
  525. SpaprDrcPhysical *drcp = (SpaprDrcPhysical *)opaque;
  526. SpaprDrc *drc = SPAPR_DR_CONNECTOR(drcp);
  527. if ((drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_ACTIVE))
  528. || (!drc->dev && (drcp->dr_indicator == SPAPR_DR_INDICATOR_INACTIVE))) {
  529. return false;
  530. }
  531. return true;
  532. }
  533. static const VMStateDescription vmstate_spapr_drc_physical = {
  534. .name = "spapr_drc/physical",
  535. .version_id = 1,
  536. .minimum_version_id = 1,
  537. .needed = drc_physical_needed,
  538. .fields = (const VMStateField []) {
  539. VMSTATE_UINT32(dr_indicator, SpaprDrcPhysical),
  540. VMSTATE_END_OF_LIST()
  541. }
  542. };
  543. static void drc_physical_reset(void *opaque)
  544. {
  545. SpaprDrc *drc = SPAPR_DR_CONNECTOR(opaque);
  546. SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(drc);
  547. if (drc->dev) {
  548. drcp->dr_indicator = SPAPR_DR_INDICATOR_ACTIVE;
  549. } else {
  550. drcp->dr_indicator = SPAPR_DR_INDICATOR_INACTIVE;
  551. }
  552. }
  553. static void realize_physical(DeviceState *d, Error **errp)
  554. {
  555. SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
  556. Error *local_err = NULL;
  557. drc_realize(d, &local_err);
  558. if (local_err) {
  559. error_propagate(errp, local_err);
  560. return;
  561. }
  562. vmstate_register(VMSTATE_IF(drcp),
  563. spapr_drc_index(SPAPR_DR_CONNECTOR(drcp)),
  564. &vmstate_spapr_drc_physical, drcp);
  565. qemu_register_reset(drc_physical_reset, drcp);
  566. }
  567. static void unrealize_physical(DeviceState *d)
  568. {
  569. SpaprDrcPhysical *drcp = SPAPR_DRC_PHYSICAL(d);
  570. drc_unrealize(d);
  571. vmstate_unregister(VMSTATE_IF(drcp), &vmstate_spapr_drc_physical, drcp);
  572. qemu_unregister_reset(drc_physical_reset, drcp);
  573. }
  574. static void spapr_drc_physical_class_init(ObjectClass *k, void *data)
  575. {
  576. DeviceClass *dk = DEVICE_CLASS(k);
  577. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  578. dk->realize = realize_physical;
  579. dk->unrealize = unrealize_physical;
  580. drck->dr_entity_sense = physical_entity_sense;
  581. drck->isolate = drc_isolate_physical;
  582. drck->unisolate = drc_unisolate_physical;
  583. drck->ready_state = SPAPR_DRC_STATE_PHYSICAL_CONFIGURED;
  584. drck->empty_state = SPAPR_DRC_STATE_PHYSICAL_POWERON;
  585. }
  586. static void spapr_drc_logical_class_init(ObjectClass *k, void *data)
  587. {
  588. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  589. drck->dr_entity_sense = logical_entity_sense;
  590. drck->isolate = drc_isolate_logical;
  591. drck->unisolate = drc_unisolate_logical;
  592. drck->ready_state = SPAPR_DRC_STATE_LOGICAL_CONFIGURED;
  593. drck->empty_state = SPAPR_DRC_STATE_LOGICAL_UNUSABLE;
  594. }
  595. static void spapr_drc_cpu_class_init(ObjectClass *k, void *data)
  596. {
  597. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  598. drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_CPU;
  599. drck->typename = "CPU";
  600. drck->drc_name_prefix = "CPU ";
  601. drck->release = spapr_core_release;
  602. drck->dt_populate = spapr_core_dt_populate;
  603. }
  604. static void spapr_drc_pci_class_init(ObjectClass *k, void *data)
  605. {
  606. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  607. drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PCI;
  608. drck->typename = "28";
  609. drck->drc_name_prefix = "C";
  610. drck->release = spapr_phb_remove_pci_device_cb;
  611. drck->dt_populate = spapr_pci_dt_populate;
  612. }
  613. static void spapr_drc_lmb_class_init(ObjectClass *k, void *data)
  614. {
  615. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  616. drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_LMB;
  617. drck->typename = "MEM";
  618. drck->drc_name_prefix = "LMB ";
  619. drck->release = spapr_lmb_release;
  620. drck->dt_populate = spapr_lmb_dt_populate;
  621. }
  622. static void spapr_drc_phb_class_init(ObjectClass *k, void *data)
  623. {
  624. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  625. drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PHB;
  626. drck->typename = "PHB";
  627. drck->drc_name_prefix = "PHB ";
  628. drck->release = spapr_phb_release;
  629. drck->dt_populate = spapr_phb_dt_populate;
  630. }
  631. static void spapr_drc_pmem_class_init(ObjectClass *k, void *data)
  632. {
  633. SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_CLASS(k);
  634. drck->typeshift = SPAPR_DR_CONNECTOR_TYPE_SHIFT_PMEM;
  635. drck->typename = "PMEM";
  636. drck->drc_name_prefix = "PMEM ";
  637. drck->release = NULL;
  638. drck->dt_populate = spapr_pmem_dt_populate;
  639. }
  640. static const TypeInfo spapr_dr_connector_info = {
  641. .name = TYPE_SPAPR_DR_CONNECTOR,
  642. .parent = TYPE_DEVICE,
  643. .instance_size = sizeof(SpaprDrc),
  644. .instance_init = spapr_dr_connector_instance_init,
  645. .class_size = sizeof(SpaprDrcClass),
  646. .class_init = spapr_dr_connector_class_init,
  647. .abstract = true,
  648. };
  649. static const TypeInfo spapr_drc_physical_info = {
  650. .name = TYPE_SPAPR_DRC_PHYSICAL,
  651. .parent = TYPE_SPAPR_DR_CONNECTOR,
  652. .instance_size = sizeof(SpaprDrcPhysical),
  653. .class_init = spapr_drc_physical_class_init,
  654. .abstract = true,
  655. };
  656. static const TypeInfo spapr_drc_logical_info = {
  657. .name = TYPE_SPAPR_DRC_LOGICAL,
  658. .parent = TYPE_SPAPR_DR_CONNECTOR,
  659. .class_init = spapr_drc_logical_class_init,
  660. .abstract = true,
  661. };
  662. static const TypeInfo spapr_drc_cpu_info = {
  663. .name = TYPE_SPAPR_DRC_CPU,
  664. .parent = TYPE_SPAPR_DRC_LOGICAL,
  665. .class_init = spapr_drc_cpu_class_init,
  666. };
  667. static const TypeInfo spapr_drc_pci_info = {
  668. .name = TYPE_SPAPR_DRC_PCI,
  669. .parent = TYPE_SPAPR_DRC_PHYSICAL,
  670. .class_init = spapr_drc_pci_class_init,
  671. };
  672. static const TypeInfo spapr_drc_lmb_info = {
  673. .name = TYPE_SPAPR_DRC_LMB,
  674. .parent = TYPE_SPAPR_DRC_LOGICAL,
  675. .class_init = spapr_drc_lmb_class_init,
  676. };
  677. static const TypeInfo spapr_drc_phb_info = {
  678. .name = TYPE_SPAPR_DRC_PHB,
  679. .parent = TYPE_SPAPR_DRC_LOGICAL,
  680. .instance_size = sizeof(SpaprDrc),
  681. .class_init = spapr_drc_phb_class_init,
  682. };
  683. static const TypeInfo spapr_drc_pmem_info = {
  684. .name = TYPE_SPAPR_DRC_PMEM,
  685. .parent = TYPE_SPAPR_DRC_LOGICAL,
  686. .class_init = spapr_drc_pmem_class_init,
  687. };
  688. /* helper functions for external users */
  689. SpaprDrc *spapr_drc_by_index(uint32_t index)
  690. {
  691. Object *obj;
  692. g_autofree gchar *name = g_strdup_printf("%x", index);
  693. obj = object_resolve_path_component(drc_container_get(), name);
  694. return !obj ? NULL : SPAPR_DR_CONNECTOR(obj);
  695. }
  696. SpaprDrc *spapr_drc_by_id(const char *type, uint32_t id)
  697. {
  698. SpaprDrcClass *drck
  699. = SPAPR_DR_CONNECTOR_CLASS(object_class_by_name(type));
  700. return spapr_drc_by_index(drck->typeshift << DRC_INDEX_TYPE_SHIFT
  701. | (id & DRC_INDEX_ID_MASK));
  702. }
  703. /**
  704. * spapr_dt_drc
  705. *
  706. * @fdt: libfdt device tree
  707. * @path: path in the DT to generate properties
  708. * @owner: parent Object/DeviceState for which to generate DRC
  709. * descriptions for
  710. * @drc_type_mask: mask of SpaprDrcType values corresponding
  711. * to the types of DRCs to generate entries for
  712. *
  713. * generate OF properties to describe DRC topology/indices to guests
  714. *
  715. * as documented in PAPR+ v2.1, 13.5.2
  716. */
  717. int spapr_dt_drc(void *fdt, int offset, Object *owner, uint32_t drc_type_mask)
  718. {
  719. Object *root_container;
  720. ObjectProperty *prop;
  721. ObjectPropertyIterator iter;
  722. uint32_t drc_count = 0;
  723. g_autoptr(GArray) drc_indexes = g_array_new(false, true,
  724. sizeof(uint32_t));
  725. g_autoptr(GArray) drc_power_domains = g_array_new(false, true,
  726. sizeof(uint32_t));
  727. g_autoptr(GString) drc_names = g_string_set_size(g_string_new(NULL),
  728. sizeof(uint32_t));
  729. g_autoptr(GString) drc_types = g_string_set_size(g_string_new(NULL),
  730. sizeof(uint32_t));
  731. int ret;
  732. /*
  733. * This should really be only called once per node since it overwrites
  734. * the OF properties if they already exist.
  735. */
  736. g_assert(!fdt_get_property(fdt, offset, "ibm,drc-indexes", NULL));
  737. /* the first entry of each properties is a 32-bit integer encoding
  738. * the number of elements in the array. we won't know this until
  739. * we complete the iteration through all the matching DRCs, but
  740. * reserve the space now and set the offsets accordingly so we
  741. * can fill them in later.
  742. */
  743. drc_indexes = g_array_set_size(drc_indexes, 1);
  744. drc_power_domains = g_array_set_size(drc_power_domains, 1);
  745. /* aliases for all DRConnector objects will be rooted in QOM
  746. * composition tree at DRC_CONTAINER_PATH
  747. */
  748. root_container = drc_container_get();
  749. object_property_iter_init(&iter, root_container);
  750. while ((prop = object_property_iter_next(&iter))) {
  751. Object *obj;
  752. SpaprDrc *drc;
  753. SpaprDrcClass *drck;
  754. g_autofree char *drc_name = NULL;
  755. uint32_t drc_index, drc_power_domain;
  756. if (!strstart(prop->type, "link<", NULL)) {
  757. continue;
  758. }
  759. obj = object_property_get_link(root_container, prop->name,
  760. &error_abort);
  761. drc = SPAPR_DR_CONNECTOR(obj);
  762. drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  763. if (owner && (drc->owner != owner)) {
  764. continue;
  765. }
  766. if ((spapr_drc_type(drc) & drc_type_mask) == 0) {
  767. continue;
  768. }
  769. drc_count++;
  770. /* ibm,drc-indexes */
  771. drc_index = cpu_to_be32(spapr_drc_index(drc));
  772. g_array_append_val(drc_indexes, drc_index);
  773. /* ibm,drc-power-domains */
  774. drc_power_domain = cpu_to_be32(-1);
  775. g_array_append_val(drc_power_domains, drc_power_domain);
  776. /* ibm,drc-names */
  777. drc_name = spapr_drc_name(drc);
  778. drc_names = g_string_append(drc_names, drc_name);
  779. drc_names = g_string_insert_len(drc_names, -1, "\0", 1);
  780. /* ibm,drc-types */
  781. drc_types = g_string_append(drc_types, drck->typename);
  782. drc_types = g_string_insert_len(drc_types, -1, "\0", 1);
  783. }
  784. /* now write the drc count into the space we reserved at the
  785. * beginning of the arrays previously
  786. */
  787. *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count);
  788. *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count);
  789. *(uint32_t *)drc_names->str = cpu_to_be32(drc_count);
  790. *(uint32_t *)drc_types->str = cpu_to_be32(drc_count);
  791. ret = fdt_setprop(fdt, offset, "ibm,drc-indexes",
  792. drc_indexes->data,
  793. drc_indexes->len * sizeof(uint32_t));
  794. if (ret) {
  795. error_report("Couldn't create ibm,drc-indexes property");
  796. return ret;
  797. }
  798. ret = fdt_setprop(fdt, offset, "ibm,drc-power-domains",
  799. drc_power_domains->data,
  800. drc_power_domains->len * sizeof(uint32_t));
  801. if (ret) {
  802. error_report("Couldn't finalize ibm,drc-power-domains property");
  803. return ret;
  804. }
  805. ret = fdt_setprop(fdt, offset, "ibm,drc-names",
  806. drc_names->str, drc_names->len);
  807. if (ret) {
  808. error_report("Couldn't finalize ibm,drc-names property");
  809. return ret;
  810. }
  811. ret = fdt_setprop(fdt, offset, "ibm,drc-types",
  812. drc_types->str, drc_types->len);
  813. if (ret) {
  814. error_report("Couldn't finalize ibm,drc-types property");
  815. }
  816. return ret;
  817. }
  818. void spapr_drc_reset_all(SpaprMachineState *spapr)
  819. {
  820. Object *drc_container;
  821. ObjectProperty *prop;
  822. ObjectPropertyIterator iter;
  823. drc_container = drc_container_get();
  824. restart:
  825. object_property_iter_init(&iter, drc_container);
  826. while ((prop = object_property_iter_next(&iter))) {
  827. SpaprDrc *drc;
  828. if (!strstart(prop->type, "link<", NULL)) {
  829. continue;
  830. }
  831. drc = SPAPR_DR_CONNECTOR(object_property_get_link(drc_container,
  832. prop->name,
  833. &error_abort));
  834. /*
  835. * This will complete any pending plug/unplug requests.
  836. * In case of a unplugged PHB or PCI bridge, this will
  837. * cause some DRCs to be destroyed and thus potentially
  838. * invalidate the iterator.
  839. */
  840. if (spapr_drc_reset(drc)) {
  841. goto restart;
  842. }
  843. }
  844. }
  845. /*
  846. * RTAS calls
  847. */
  848. static uint32_t rtas_set_isolation_state(uint32_t idx, uint32_t state)
  849. {
  850. SpaprDrc *drc = spapr_drc_by_index(idx);
  851. SpaprDrcClass *drck;
  852. if (!drc) {
  853. return RTAS_OUT_NO_SUCH_INDICATOR;
  854. }
  855. trace_spapr_drc_set_isolation_state(spapr_drc_index(drc), state);
  856. drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  857. switch (state) {
  858. case SPAPR_DR_ISOLATION_STATE_ISOLATED:
  859. return drck->isolate(drc);
  860. case SPAPR_DR_ISOLATION_STATE_UNISOLATED:
  861. return drck->unisolate(drc);
  862. default:
  863. return RTAS_OUT_PARAM_ERROR;
  864. }
  865. }
  866. static uint32_t rtas_set_allocation_state(uint32_t idx, uint32_t state)
  867. {
  868. SpaprDrc *drc = spapr_drc_by_index(idx);
  869. if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_LOGICAL)) {
  870. return RTAS_OUT_NO_SUCH_INDICATOR;
  871. }
  872. trace_spapr_drc_set_allocation_state(spapr_drc_index(drc), state);
  873. switch (state) {
  874. case SPAPR_DR_ALLOCATION_STATE_USABLE:
  875. return drc_set_usable(drc);
  876. case SPAPR_DR_ALLOCATION_STATE_UNUSABLE:
  877. return drc_set_unusable(drc);
  878. default:
  879. return RTAS_OUT_PARAM_ERROR;
  880. }
  881. }
  882. static uint32_t rtas_set_dr_indicator(uint32_t idx, uint32_t state)
  883. {
  884. SpaprDrc *drc = spapr_drc_by_index(idx);
  885. if (!drc || !object_dynamic_cast(OBJECT(drc), TYPE_SPAPR_DRC_PHYSICAL)) {
  886. return RTAS_OUT_NO_SUCH_INDICATOR;
  887. }
  888. if ((state != SPAPR_DR_INDICATOR_INACTIVE)
  889. && (state != SPAPR_DR_INDICATOR_ACTIVE)
  890. && (state != SPAPR_DR_INDICATOR_IDENTIFY)
  891. && (state != SPAPR_DR_INDICATOR_ACTION)) {
  892. return RTAS_OUT_PARAM_ERROR; /* bad state parameter */
  893. }
  894. trace_spapr_drc_set_dr_indicator(idx, state);
  895. SPAPR_DRC_PHYSICAL(drc)->dr_indicator = state;
  896. return RTAS_OUT_SUCCESS;
  897. }
  898. static void rtas_set_indicator(PowerPCCPU *cpu, SpaprMachineState *spapr,
  899. uint32_t token,
  900. uint32_t nargs, target_ulong args,
  901. uint32_t nret, target_ulong rets)
  902. {
  903. uint32_t type, idx, state;
  904. uint32_t ret = RTAS_OUT_SUCCESS;
  905. if (nargs != 3 || nret != 1) {
  906. ret = RTAS_OUT_PARAM_ERROR;
  907. goto out;
  908. }
  909. type = rtas_ld(args, 0);
  910. idx = rtas_ld(args, 1);
  911. state = rtas_ld(args, 2);
  912. switch (type) {
  913. case RTAS_SENSOR_TYPE_ISOLATION_STATE:
  914. ret = rtas_set_isolation_state(idx, state);
  915. break;
  916. case RTAS_SENSOR_TYPE_DR:
  917. ret = rtas_set_dr_indicator(idx, state);
  918. break;
  919. case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
  920. ret = rtas_set_allocation_state(idx, state);
  921. break;
  922. default:
  923. ret = RTAS_OUT_NOT_SUPPORTED;
  924. }
  925. out:
  926. rtas_st(rets, 0, ret);
  927. }
  928. static void rtas_get_sensor_state(PowerPCCPU *cpu, SpaprMachineState *spapr,
  929. uint32_t token, uint32_t nargs,
  930. target_ulong args, uint32_t nret,
  931. target_ulong rets)
  932. {
  933. uint32_t sensor_type;
  934. uint32_t sensor_index;
  935. uint32_t sensor_state = 0;
  936. SpaprDrc *drc;
  937. SpaprDrcClass *drck;
  938. uint32_t ret = RTAS_OUT_SUCCESS;
  939. if (nargs != 2 || nret != 2) {
  940. ret = RTAS_OUT_PARAM_ERROR;
  941. goto out;
  942. }
  943. sensor_type = rtas_ld(args, 0);
  944. sensor_index = rtas_ld(args, 1);
  945. if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
  946. /* currently only DR-related sensors are implemented */
  947. trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
  948. sensor_type);
  949. ret = RTAS_OUT_NOT_SUPPORTED;
  950. goto out;
  951. }
  952. drc = spapr_drc_by_index(sensor_index);
  953. if (!drc) {
  954. trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
  955. ret = RTAS_OUT_PARAM_ERROR;
  956. goto out;
  957. }
  958. drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  959. sensor_state = drck->dr_entity_sense(drc);
  960. out:
  961. rtas_st(rets, 0, ret);
  962. rtas_st(rets, 1, sensor_state);
  963. }
  964. /* configure-connector work area offsets, int32_t units for field
  965. * indexes, bytes for field offset/len values.
  966. *
  967. * as documented by PAPR+ v2.7, 13.5.3.5
  968. */
  969. #define CC_IDX_NODE_NAME_OFFSET 2
  970. #define CC_IDX_PROP_NAME_OFFSET 2
  971. #define CC_IDX_PROP_LEN 3
  972. #define CC_IDX_PROP_DATA_OFFSET 4
  973. #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
  974. #define CC_WA_LEN 4096
  975. static void configure_connector_st(target_ulong addr, target_ulong offset,
  976. const void *buf, size_t len)
  977. {
  978. cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
  979. buf, MIN(len, CC_WA_LEN - offset));
  980. }
  981. static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
  982. SpaprMachineState *spapr,
  983. uint32_t token, uint32_t nargs,
  984. target_ulong args, uint32_t nret,
  985. target_ulong rets)
  986. {
  987. uint64_t wa_addr;
  988. uint64_t wa_offset;
  989. uint32_t drc_index;
  990. SpaprDrc *drc;
  991. SpaprDrcClass *drck;
  992. SpaprDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
  993. int rc;
  994. if (nargs != 2 || nret != 1) {
  995. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  996. return;
  997. }
  998. wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
  999. drc_index = rtas_ld(wa_addr, 0);
  1000. drc = spapr_drc_by_index(drc_index);
  1001. if (!drc) {
  1002. trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
  1003. rc = RTAS_OUT_PARAM_ERROR;
  1004. goto out;
  1005. }
  1006. if ((drc->state != SPAPR_DRC_STATE_LOGICAL_UNISOLATE)
  1007. && (drc->state != SPAPR_DRC_STATE_PHYSICAL_UNISOLATE)
  1008. && (drc->state != SPAPR_DRC_STATE_LOGICAL_CONFIGURED)
  1009. && (drc->state != SPAPR_DRC_STATE_PHYSICAL_CONFIGURED)) {
  1010. /*
  1011. * Need to unisolate the device before configuring
  1012. * or it should already be in configured state to
  1013. * allow configure-connector be called repeatedly.
  1014. */
  1015. rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
  1016. goto out;
  1017. }
  1018. drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
  1019. /*
  1020. * This indicates that the kernel is reconfiguring a LMB due to
  1021. * a failed hotunplug. Rollback the DIMM unplug process.
  1022. */
  1023. if (spapr_drc_type(drc) == SPAPR_DR_CONNECTOR_TYPE_LMB &&
  1024. drc->unplug_requested) {
  1025. spapr_memory_unplug_rollback(spapr, drc->dev);
  1026. }
  1027. if (!drc->fdt) {
  1028. void *fdt;
  1029. int fdt_size;
  1030. fdt = create_device_tree(&fdt_size);
  1031. if (drck->dt_populate(drc, spapr, fdt, &drc->fdt_start_offset,
  1032. NULL)) {
  1033. g_free(fdt);
  1034. rc = SPAPR_DR_CC_RESPONSE_ERROR;
  1035. goto out;
  1036. }
  1037. drc->fdt = fdt;
  1038. drc->ccs_offset = drc->fdt_start_offset;
  1039. drc->ccs_depth = 0;
  1040. }
  1041. do {
  1042. uint32_t tag;
  1043. const char *name;
  1044. const struct fdt_property *prop;
  1045. int fdt_offset_next, prop_len;
  1046. tag = fdt_next_tag(drc->fdt, drc->ccs_offset, &fdt_offset_next);
  1047. switch (tag) {
  1048. case FDT_BEGIN_NODE:
  1049. drc->ccs_depth++;
  1050. name = fdt_get_name(drc->fdt, drc->ccs_offset, NULL);
  1051. /* provide the name of the next OF node */
  1052. wa_offset = CC_VAL_DATA_OFFSET;
  1053. rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
  1054. configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
  1055. resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
  1056. break;
  1057. case FDT_END_NODE:
  1058. drc->ccs_depth--;
  1059. if (drc->ccs_depth == 0) {
  1060. /* done sending the device tree, move to configured state */
  1061. trace_spapr_drc_set_configured(drc_index);
  1062. drc->state = drck->ready_state;
  1063. /*
  1064. * Ensure that we are able to send the FDT fragment
  1065. * again via configure-connector call if the guest requests.
  1066. */
  1067. drc->ccs_offset = drc->fdt_start_offset;
  1068. drc->ccs_depth = 0;
  1069. fdt_offset_next = drc->fdt_start_offset;
  1070. resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
  1071. } else {
  1072. resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
  1073. }
  1074. break;
  1075. case FDT_PROP:
  1076. prop = fdt_get_property_by_offset(drc->fdt, drc->ccs_offset,
  1077. &prop_len);
  1078. name = fdt_string(drc->fdt, fdt32_to_cpu(prop->nameoff));
  1079. /* provide the name of the next OF property */
  1080. wa_offset = CC_VAL_DATA_OFFSET;
  1081. rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
  1082. configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
  1083. /* provide the length and value of the OF property. data gets
  1084. * placed immediately after NULL terminator of the OF property's
  1085. * name string
  1086. */
  1087. wa_offset += strlen(name) + 1,
  1088. rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
  1089. rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
  1090. configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
  1091. resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
  1092. break;
  1093. case FDT_END:
  1094. resp = SPAPR_DR_CC_RESPONSE_ERROR;
  1095. default:
  1096. /* keep seeking for an actionable tag */
  1097. break;
  1098. }
  1099. if (drc->ccs_offset >= 0) {
  1100. drc->ccs_offset = fdt_offset_next;
  1101. }
  1102. } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
  1103. rc = resp;
  1104. out:
  1105. rtas_st(rets, 0, rc);
  1106. }
  1107. static void spapr_drc_register_types(void)
  1108. {
  1109. type_register_static(&spapr_dr_connector_info);
  1110. type_register_static(&spapr_drc_physical_info);
  1111. type_register_static(&spapr_drc_logical_info);
  1112. type_register_static(&spapr_drc_cpu_info);
  1113. type_register_static(&spapr_drc_pci_info);
  1114. type_register_static(&spapr_drc_lmb_info);
  1115. type_register_static(&spapr_drc_phb_info);
  1116. type_register_static(&spapr_drc_pmem_info);
  1117. spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
  1118. rtas_set_indicator);
  1119. spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
  1120. rtas_get_sensor_state);
  1121. spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
  1122. rtas_ibm_configure_connector);
  1123. }
  1124. type_init(spapr_drc_register_types)