Path: blob/master/arch/powerpc/platforms/pseries/pmem.c
26481 views
// SPDX-License-Identifier: GPL-2.012/*3* Handles hot and cold plug of persistent memory regions on pseries.4*/56#define pr_fmt(fmt) "pseries-pmem: " fmt78#include <linux/kernel.h>9#include <linux/interrupt.h>10#include <linux/delay.h>11#include <linux/sched.h> /* for idle_task_exit */12#include <linux/sched/hotplug.h>13#include <linux/cpu.h>14#include <linux/of.h>15#include <linux/of_platform.h>16#include <linux/slab.h>17#include <asm/rtas.h>18#include <asm/firmware.h>19#include <asm/machdep.h>20#include <asm/vdso_datapage.h>21#include <asm/plpar_wrappers.h>22#include <asm/topology.h>2324#include "pseries.h"2526static struct device_node *pmem_node;2728static ssize_t pmem_drc_add_node(u32 drc_index)29{30struct device_node *dn;31int rc;3233pr_debug("Attempting to add pmem node, drc index: %x\n", drc_index);3435rc = dlpar_acquire_drc(drc_index);36if (rc) {37pr_err("Failed to acquire DRC, rc: %d, drc index: %x\n",38rc, drc_index);39return -EINVAL;40}4142dn = dlpar_configure_connector(cpu_to_be32(drc_index), pmem_node);43if (!dn) {44pr_err("configure-connector failed for drc %x\n", drc_index);45dlpar_release_drc(drc_index);46return -EINVAL;47}4849/* NB: The of reconfig notifier creates platform device from the node */50rc = dlpar_attach_node(dn, pmem_node);51if (rc) {52pr_err("Failed to attach node %pOF, rc: %d, drc index: %x\n",53dn, rc, drc_index);5455if (dlpar_release_drc(drc_index))56dlpar_free_cc_nodes(dn);5758return rc;59}6061pr_info("Successfully added %pOF, drc index: %x\n", dn, drc_index);6263return 0;64}6566static ssize_t pmem_drc_remove_node(u32 drc_index)67{68struct device_node *dn;69uint32_t index;70int rc;7172for_each_child_of_node(pmem_node, dn) {73if (of_property_read_u32(dn, "ibm,my-drc-index", &index))74continue;75if (index == drc_index)76break;77}7879if (!dn) {80pr_err("Attempting to remove unused DRC index %x\n", drc_index);81return -ENODEV;82}8384pr_debug("Attempting to remove %pOF, drc index: %x\n", dn, drc_index);8586/* * NB: tears down the ibm,pmemory device as a side-effect */87rc = dlpar_detach_node(dn);88if (rc)89return rc;9091rc = dlpar_release_drc(drc_index);92if (rc) {93pr_err("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",94drc_index, dn, rc);95dlpar_attach_node(dn, pmem_node);96return rc;97}9899pr_info("Successfully removed PMEM with drc index: %x\n", drc_index);100101return 0;102}103104int dlpar_hp_pmem(struct pseries_hp_errorlog *hp_elog)105{106u32 drc_index;107int rc;108109/* slim chance, but we might get a hotplug event while booting */110if (!pmem_node)111pmem_node = of_find_node_by_type(NULL, "ibm,persistent-memory");112if (!pmem_node) {113pr_err("Hotplug event for a pmem device, but none exists\n");114return -ENODEV;115}116117if (hp_elog->id_type != PSERIES_HP_ELOG_ID_DRC_INDEX) {118pr_err("Unsupported hotplug event type %d\n",119hp_elog->id_type);120return -EINVAL;121}122123drc_index = be32_to_cpu(hp_elog->_drc_u.drc_index);124125lock_device_hotplug();126127if (hp_elog->action == PSERIES_HP_ELOG_ACTION_ADD) {128rc = pmem_drc_add_node(drc_index);129} else if (hp_elog->action == PSERIES_HP_ELOG_ACTION_REMOVE) {130rc = pmem_drc_remove_node(drc_index);131} else {132pr_err("Unsupported hotplug action (%d)\n", hp_elog->action);133rc = -EINVAL;134}135136unlock_device_hotplug();137return rc;138}139140static const struct of_device_id drc_pmem_match[] = {141{ .type = "ibm,persistent-memory", },142{}143};144145static int pseries_pmem_init(void)146{147/*148* Only supported on POWER8 and above.149*/150if (!cpu_has_feature(CPU_FTR_ARCH_207S))151return 0;152153pmem_node = of_find_node_by_type(NULL, "ibm,persistent-memory");154if (!pmem_node)155return 0;156157/*158* The generic OF bus probe/populate handles creating platform devices159* from the child (ibm,pmemory) nodes. The generic code registers an of160* reconfig notifier to handle the hot-add/remove cases too.161*/162of_platform_bus_probe(pmem_node, drc_pmem_match, NULL);163164return 0;165}166machine_arch_initcall(pseries, pseries_pmem_init);167168169