Path: blob/master/arch/powerpc/platforms/pseries/papr_scm.c
26481 views
// SPDX-License-Identifier: GPL-2.012#define pr_fmt(fmt) "papr-scm: " fmt34#include <linux/of.h>5#include <linux/kernel.h>6#include <linux/module.h>7#include <linux/ioport.h>8#include <linux/seq_file.h>9#include <linux/slab.h>10#include <linux/ndctl.h>11#include <linux/sched.h>12#include <linux/libnvdimm.h>13#include <linux/platform_device.h>14#include <linux/delay.h>15#include <linux/seq_buf.h>16#include <linux/nd.h>1718#include <asm/plpar_wrappers.h>19#include <uapi/linux/papr_pdsm.h>20#include <linux/papr_scm.h>21#include <asm/mce.h>22#include <linux/unaligned.h>23#include <linux/perf_event.h>2425#define BIND_ANY_ADDR (~0ul)2627#define PAPR_SCM_DIMM_CMD_MASK \28((1ul << ND_CMD_GET_CONFIG_SIZE) | \29(1ul << ND_CMD_GET_CONFIG_DATA) | \30(1ul << ND_CMD_SET_CONFIG_DATA) | \31(1ul << ND_CMD_CALL))3233/* Struct holding a single performance metric */34struct papr_scm_perf_stat {35u8 stat_id[8];36__be64 stat_val;37} __packed;3839/* Struct exchanged between kernel and PHYP for fetching drc perf stats */40struct papr_scm_perf_stats {41u8 eye_catcher[8];42/* Should be PAPR_SCM_PERF_STATS_VERSION */43__be32 stats_version;44/* Number of stats following */45__be32 num_statistics;46/* zero or more performance matrics */47struct papr_scm_perf_stat scm_statistic[];48} __packed;4950/* private struct associated with each region */51struct papr_scm_priv {52struct platform_device *pdev;53struct device_node *dn;54uint32_t drc_index;55uint64_t blocks;56uint64_t block_size;57int metadata_size;58bool is_volatile;59bool hcall_flush_required;6061uint64_t bound_addr;6263struct nvdimm_bus_descriptor bus_desc;64struct nvdimm_bus *bus;65struct nvdimm *nvdimm;66struct resource res;67struct nd_region *region;68struct nd_interleave_set nd_set;69struct list_head region_list;7071/* Protect dimm health data from concurrent read/writes */72struct mutex health_mutex;7374/* Last time the health information of the dimm was updated */75unsigned long lasthealth_jiffies;7677/* Health information for the dimm */78u64 health_bitmap;7980/* Holds the last known dirty shutdown counter value */81u64 dirty_shutdown_counter;8283/* length of the stat buffer as expected by phyp */84size_t stat_buffer_len;8586/* The bits which needs to be overridden */87u64 health_bitmap_inject_mask;88};8990static int papr_scm_pmem_flush(struct nd_region *nd_region,91struct bio *bio __maybe_unused)92{93struct papr_scm_priv *p = nd_region_provider_data(nd_region);94unsigned long ret_buf[PLPAR_HCALL_BUFSIZE], token = 0;95long rc;9697dev_dbg(&p->pdev->dev, "flush drc 0x%x", p->drc_index);9899do {100rc = plpar_hcall(H_SCM_FLUSH, ret_buf, p->drc_index, token);101token = ret_buf[0];102103/* Check if we are stalled for some time */104if (H_IS_LONG_BUSY(rc)) {105msleep(get_longbusy_msecs(rc));106rc = H_BUSY;107} else if (rc == H_BUSY) {108cond_resched();109}110} while (rc == H_BUSY);111112if (rc) {113dev_err(&p->pdev->dev, "flush error: %ld", rc);114rc = -EIO;115} else {116dev_dbg(&p->pdev->dev, "flush drc 0x%x complete", p->drc_index);117}118119return rc;120}121122static LIST_HEAD(papr_nd_regions);123static DEFINE_MUTEX(papr_ndr_lock);124125static int drc_pmem_bind(struct papr_scm_priv *p)126{127unsigned long ret[PLPAR_HCALL_BUFSIZE];128uint64_t saved = 0;129uint64_t token;130int64_t rc;131132/*133* When the hypervisor cannot map all the requested memory in a single134* hcall it returns H_BUSY and we call again with the token until135* we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS136* leave the system in an undefined state, so we wait.137*/138token = 0;139140do {141rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,142p->blocks, BIND_ANY_ADDR, token);143token = ret[0];144if (!saved)145saved = ret[1];146cond_resched();147} while (rc == H_BUSY);148149if (rc)150return rc;151152p->bound_addr = saved;153dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",154p->drc_index, (unsigned long)saved);155return rc;156}157158static void drc_pmem_unbind(struct papr_scm_priv *p)159{160unsigned long ret[PLPAR_HCALL_BUFSIZE];161uint64_t token = 0;162int64_t rc;163164dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);165166/* NB: unbind has the same retry requirements as drc_pmem_bind() */167do {168169/* Unbind of all SCM resources associated with drcIndex */170rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,171p->drc_index, token);172token = ret[0];173174/* Check if we are stalled for some time */175if (H_IS_LONG_BUSY(rc)) {176msleep(get_longbusy_msecs(rc));177rc = H_BUSY;178} else if (rc == H_BUSY) {179cond_resched();180}181182} while (rc == H_BUSY);183184if (rc)185dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);186else187dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",188p->drc_index);189190return;191}192193static int drc_pmem_query_n_bind(struct papr_scm_priv *p)194{195unsigned long start_addr;196unsigned long end_addr;197unsigned long ret[PLPAR_HCALL_BUFSIZE];198int64_t rc;199200201rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,202p->drc_index, 0);203if (rc)204goto err_out;205start_addr = ret[0];206207/* Make sure the full region is bound. */208rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,209p->drc_index, p->blocks - 1);210if (rc)211goto err_out;212end_addr = ret[0];213214if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))215goto err_out;216217p->bound_addr = start_addr;218dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);219return rc;220221err_out:222dev_info(&p->pdev->dev,223"Failed to query, trying an unbind followed by bind");224drc_pmem_unbind(p);225return drc_pmem_bind(p);226}227228/*229* Query the Dimm performance stats from PHYP and copy them (if returned) to230* provided struct papr_scm_perf_stats instance 'stats' that can hold atleast231* (num_stats + header) bytes.232* - If buff_stats == NULL the return value is the size in bytes of the buffer233* needed to hold all supported performance-statistics.234* - If buff_stats != NULL and num_stats == 0 then we copy all known235* performance-statistics to 'buff_stat' and expect to be large enough to236* hold them.237* - if buff_stats != NULL and num_stats > 0 then copy the requested238* performance-statistics to buff_stats.239*/240static ssize_t drc_pmem_query_stats(struct papr_scm_priv *p,241struct papr_scm_perf_stats *buff_stats,242unsigned int num_stats)243{244unsigned long ret[PLPAR_HCALL_BUFSIZE];245size_t size;246s64 rc;247248/* Setup the out buffer */249if (buff_stats) {250memcpy(buff_stats->eye_catcher,251PAPR_SCM_PERF_STATS_EYECATCHER, 8);252buff_stats->stats_version =253cpu_to_be32(PAPR_SCM_PERF_STATS_VERSION);254buff_stats->num_statistics =255cpu_to_be32(num_stats);256257/*258* Calculate the buffer size based on num-stats provided259* or use the prefetched max buffer length260*/261if (num_stats)262/* Calculate size from the num_stats */263size = sizeof(struct papr_scm_perf_stats) +264num_stats * sizeof(struct papr_scm_perf_stat);265else266size = p->stat_buffer_len;267} else {268/* In case of no out buffer ignore the size */269size = 0;270}271272/* Do the HCALL asking PHYP for info */273rc = plpar_hcall(H_SCM_PERFORMANCE_STATS, ret, p->drc_index,274buff_stats ? virt_to_phys(buff_stats) : 0,275size);276277/* Check if the error was due to an unknown stat-id */278if (rc == H_PARTIAL) {279dev_err(&p->pdev->dev,280"Unknown performance stats, Err:0x%016lX\n", ret[0]);281return -ENOENT;282} else if (rc == H_AUTHORITY) {283dev_info(&p->pdev->dev,284"Permission denied while accessing performance stats");285return -EPERM;286} else if (rc == H_UNSUPPORTED) {287dev_dbg(&p->pdev->dev, "Performance stats unsupported\n");288return -EOPNOTSUPP;289} else if (rc != H_SUCCESS) {290dev_err(&p->pdev->dev,291"Failed to query performance stats, Err:%lld\n", rc);292return -EIO;293294} else if (!size) {295/* Handle case where stat buffer size was requested */296dev_dbg(&p->pdev->dev,297"Performance stats size %ld\n", ret[0]);298return ret[0];299}300301/* Successfully fetched the requested stats from phyp */302dev_dbg(&p->pdev->dev,303"Performance stats returned %d stats\n",304be32_to_cpu(buff_stats->num_statistics));305return 0;306}307308#ifdef CONFIG_PERF_EVENTS309#define to_nvdimm_pmu(_pmu) container_of(_pmu, struct nvdimm_pmu, pmu)310311static const char * const nvdimm_events_map[] = {312[1] = "CtlResCt",313[2] = "CtlResTm",314[3] = "PonSecs ",315[4] = "MemLife ",316[5] = "CritRscU",317[6] = "HostLCnt",318[7] = "HostSCnt",319[8] = "HostSDur",320[9] = "HostLDur",321[10] = "MedRCnt ",322[11] = "MedWCnt ",323[12] = "MedRDur ",324[13] = "MedWDur ",325[14] = "CchRHCnt",326[15] = "CchWHCnt",327[16] = "FastWCnt",328};329330static int papr_scm_pmu_get_value(struct perf_event *event, struct device *dev, u64 *count)331{332struct papr_scm_perf_stat *stat;333struct papr_scm_perf_stats *stats;334struct papr_scm_priv *p = dev_get_drvdata(dev);335int rc, size;336337/* Invalid eventcode */338if (event->attr.config == 0 || event->attr.config >= ARRAY_SIZE(nvdimm_events_map))339return -EINVAL;340341/* Allocate request buffer enough to hold single performance stat */342size = sizeof(struct papr_scm_perf_stats) +343sizeof(struct papr_scm_perf_stat);344345if (!p)346return -EINVAL;347348stats = kzalloc(size, GFP_KERNEL);349if (!stats)350return -ENOMEM;351352stat = &stats->scm_statistic[0];353memcpy(&stat->stat_id,354nvdimm_events_map[event->attr.config],355sizeof(stat->stat_id));356stat->stat_val = 0;357358rc = drc_pmem_query_stats(p, stats, 1);359if (rc < 0) {360kfree(stats);361return rc;362}363364*count = be64_to_cpu(stat->stat_val);365kfree(stats);366return 0;367}368369static int papr_scm_pmu_event_init(struct perf_event *event)370{371struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);372struct papr_scm_priv *p;373374if (!nd_pmu)375return -EINVAL;376377/* test the event attr type for PMU enumeration */378if (event->attr.type != event->pmu->type)379return -ENOENT;380381/* it does not support event sampling mode */382if (is_sampling_event(event))383return -EOPNOTSUPP;384385/* no branch sampling */386if (has_branch_stack(event))387return -EOPNOTSUPP;388389p = (struct papr_scm_priv *)nd_pmu->dev->driver_data;390if (!p)391return -EINVAL;392393/* Invalid eventcode */394if (event->attr.config == 0 || event->attr.config > 16)395return -EINVAL;396397return 0;398}399400static int papr_scm_pmu_add(struct perf_event *event, int flags)401{402u64 count;403int rc;404struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);405406if (!nd_pmu)407return -EINVAL;408409if (flags & PERF_EF_START) {410rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &count);411if (rc)412return rc;413414local64_set(&event->hw.prev_count, count);415}416417return 0;418}419420static void papr_scm_pmu_read(struct perf_event *event)421{422u64 prev, now;423int rc;424struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);425426if (!nd_pmu)427return;428429rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &now);430if (rc)431return;432433prev = local64_xchg(&event->hw.prev_count, now);434local64_add(now - prev, &event->count);435}436437static void papr_scm_pmu_del(struct perf_event *event, int flags)438{439papr_scm_pmu_read(event);440}441442static void papr_scm_pmu_register(struct papr_scm_priv *p)443{444struct nvdimm_pmu *nd_pmu;445int rc, nodeid;446447nd_pmu = kzalloc(sizeof(*nd_pmu), GFP_KERNEL);448if (!nd_pmu) {449rc = -ENOMEM;450goto pmu_err_print;451}452453if (!p->stat_buffer_len) {454rc = -ENOENT;455goto pmu_check_events_err;456}457458nd_pmu->pmu.task_ctx_nr = perf_invalid_context;459nd_pmu->pmu.name = nvdimm_name(p->nvdimm);460nd_pmu->pmu.event_init = papr_scm_pmu_event_init;461nd_pmu->pmu.read = papr_scm_pmu_read;462nd_pmu->pmu.add = papr_scm_pmu_add;463nd_pmu->pmu.del = papr_scm_pmu_del;464465nd_pmu->pmu.capabilities = PERF_PMU_CAP_NO_INTERRUPT |466PERF_PMU_CAP_NO_EXCLUDE;467468/*updating the cpumask variable */469nodeid = numa_map_to_online_node(dev_to_node(&p->pdev->dev));470nd_pmu->arch_cpumask = *cpumask_of_node(nodeid);471472rc = register_nvdimm_pmu(nd_pmu, p->pdev);473if (rc)474goto pmu_check_events_err;475476/*477* Set archdata.priv value to nvdimm_pmu structure, to handle the478* unregistering of pmu device.479*/480p->pdev->archdata.priv = nd_pmu;481return;482483pmu_check_events_err:484kfree(nd_pmu);485pmu_err_print:486dev_info(&p->pdev->dev, "nvdimm pmu didn't register rc=%d\n", rc);487}488489#else490static void papr_scm_pmu_register(struct papr_scm_priv *p) { }491#endif492493/*494* Issue hcall to retrieve dimm health info and populate papr_scm_priv with the495* health information.496*/497static int __drc_pmem_query_health(struct papr_scm_priv *p)498{499unsigned long ret[PLPAR_HCALL_BUFSIZE];500u64 bitmap = 0;501long rc;502503/* issue the hcall */504rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index);505if (rc == H_SUCCESS)506bitmap = ret[0] & ret[1];507else if (rc == H_FUNCTION)508dev_info_once(&p->pdev->dev,509"Hcall H_SCM_HEALTH not implemented, assuming empty health bitmap");510else {511512dev_err(&p->pdev->dev,513"Failed to query health information, Err:%ld\n", rc);514return -ENXIO;515}516517p->lasthealth_jiffies = jiffies;518/* Allow injecting specific health bits via inject mask. */519if (p->health_bitmap_inject_mask)520bitmap = (bitmap & ~p->health_bitmap_inject_mask) |521p->health_bitmap_inject_mask;522WRITE_ONCE(p->health_bitmap, bitmap);523dev_dbg(&p->pdev->dev,524"Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n",525ret[0], ret[1]);526527return 0;528}529530/* Min interval in seconds for assuming stable dimm health */531#define MIN_HEALTH_QUERY_INTERVAL 60532533/* Query cached health info and if needed call drc_pmem_query_health */534static int drc_pmem_query_health(struct papr_scm_priv *p)535{536unsigned long cache_timeout;537int rc;538539/* Protect concurrent modifications to papr_scm_priv */540rc = mutex_lock_interruptible(&p->health_mutex);541if (rc)542return rc;543544/* Jiffies offset for which the health data is assumed to be same */545cache_timeout = p->lasthealth_jiffies +546secs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL);547548/* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */549if (time_after(jiffies, cache_timeout))550rc = __drc_pmem_query_health(p);551else552/* Assume cached health data is valid */553rc = 0;554555mutex_unlock(&p->health_mutex);556return rc;557}558559static int papr_scm_meta_get(struct papr_scm_priv *p,560struct nd_cmd_get_config_data_hdr *hdr)561{562unsigned long data[PLPAR_HCALL_BUFSIZE];563unsigned long offset, data_offset;564int len, read;565int64_t ret;566567if ((hdr->in_offset + hdr->in_length) > p->metadata_size)568return -EINVAL;569570for (len = hdr->in_length; len; len -= read) {571572data_offset = hdr->in_length - len;573offset = hdr->in_offset + data_offset;574575if (len >= 8)576read = 8;577else if (len >= 4)578read = 4;579else if (len >= 2)580read = 2;581else582read = 1;583584ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,585offset, read);586587if (ret == H_PARAMETER) /* bad DRC index */588return -ENODEV;589if (ret)590return -EINVAL; /* other invalid parameter */591592switch (read) {593case 8:594*(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);595break;596case 4:597*(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);598break;599600case 2:601*(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);602break;603604case 1:605*(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);606break;607}608}609return 0;610}611612static int papr_scm_meta_set(struct papr_scm_priv *p,613struct nd_cmd_set_config_hdr *hdr)614{615unsigned long offset, data_offset;616int len, wrote;617unsigned long data;618__be64 data_be;619int64_t ret;620621if ((hdr->in_offset + hdr->in_length) > p->metadata_size)622return -EINVAL;623624for (len = hdr->in_length; len; len -= wrote) {625626data_offset = hdr->in_length - len;627offset = hdr->in_offset + data_offset;628629if (len >= 8) {630data = *(uint64_t *)(hdr->in_buf + data_offset);631data_be = cpu_to_be64(data);632wrote = 8;633} else if (len >= 4) {634data = *(uint32_t *)(hdr->in_buf + data_offset);635data &= 0xffffffff;636data_be = cpu_to_be32(data);637wrote = 4;638} else if (len >= 2) {639data = *(uint16_t *)(hdr->in_buf + data_offset);640data &= 0xffff;641data_be = cpu_to_be16(data);642wrote = 2;643} else {644data_be = *(uint8_t *)(hdr->in_buf + data_offset);645data_be &= 0xff;646wrote = 1;647}648649ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,650offset, data_be, wrote);651if (ret == H_PARAMETER) /* bad DRC index */652return -ENODEV;653if (ret)654return -EINVAL; /* other invalid parameter */655}656657return 0;658}659660/*661* Do a sanity checks on the inputs args to dimm-control function and return662* '0' if valid. Validation of PDSM payloads happens later in663* papr_scm_service_pdsm.664*/665static int is_cmd_valid(struct nvdimm *nvdimm, unsigned int cmd, void *buf,666unsigned int buf_len)667{668unsigned long cmd_mask = PAPR_SCM_DIMM_CMD_MASK;669struct nd_cmd_pkg *nd_cmd;670struct papr_scm_priv *p;671enum papr_pdsm pdsm;672673/* Only dimm-specific calls are supported atm */674if (!nvdimm)675return -EINVAL;676677/* get the provider data from struct nvdimm */678p = nvdimm_provider_data(nvdimm);679680if (!test_bit(cmd, &cmd_mask)) {681dev_dbg(&p->pdev->dev, "Unsupported cmd=%u\n", cmd);682return -EINVAL;683}684685/* For CMD_CALL verify pdsm request */686if (cmd == ND_CMD_CALL) {687/* Verify the envelope and envelop size */688if (!buf ||689buf_len < (sizeof(struct nd_cmd_pkg) + ND_PDSM_HDR_SIZE)) {690dev_dbg(&p->pdev->dev, "Invalid pkg size=%u\n",691buf_len);692return -EINVAL;693}694695/* Verify that the nd_cmd_pkg.nd_family is correct */696nd_cmd = (struct nd_cmd_pkg *)buf;697698if (nd_cmd->nd_family != NVDIMM_FAMILY_PAPR) {699dev_dbg(&p->pdev->dev, "Invalid pkg family=0x%llx\n",700nd_cmd->nd_family);701return -EINVAL;702}703704pdsm = (enum papr_pdsm)nd_cmd->nd_command;705706/* Verify if the pdsm command is valid */707if (pdsm <= PAPR_PDSM_MIN || pdsm >= PAPR_PDSM_MAX) {708dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid PDSM\n",709pdsm);710return -EINVAL;711}712713/* Have enough space to hold returned 'nd_pkg_pdsm' header */714if (nd_cmd->nd_size_out < ND_PDSM_HDR_SIZE) {715dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid payload\n",716pdsm);717return -EINVAL;718}719}720721/* Let the command be further processed */722return 0;723}724725static int papr_pdsm_fuel_gauge(struct papr_scm_priv *p,726union nd_pdsm_payload *payload)727{728int rc, size;729u64 statval;730struct papr_scm_perf_stat *stat;731struct papr_scm_perf_stats *stats;732733/* Silently fail if fetching performance metrics isn't supported */734if (!p->stat_buffer_len)735return 0;736737/* Allocate request buffer enough to hold single performance stat */738size = sizeof(struct papr_scm_perf_stats) +739sizeof(struct papr_scm_perf_stat);740741stats = kzalloc(size, GFP_KERNEL);742if (!stats)743return -ENOMEM;744745stat = &stats->scm_statistic[0];746memcpy(&stat->stat_id, "MemLife ", sizeof(stat->stat_id));747stat->stat_val = 0;748749/* Fetch the fuel gauge and populate it in payload */750rc = drc_pmem_query_stats(p, stats, 1);751if (rc < 0) {752dev_dbg(&p->pdev->dev, "Err(%d) fetching fuel gauge\n", rc);753goto free_stats;754}755756statval = be64_to_cpu(stat->stat_val);757dev_dbg(&p->pdev->dev,758"Fetched fuel-gauge %llu", statval);759payload->health.extension_flags |=760PDSM_DIMM_HEALTH_RUN_GAUGE_VALID;761payload->health.dimm_fuel_gauge = statval;762763rc = sizeof(struct nd_papr_pdsm_health);764765free_stats:766kfree(stats);767return rc;768}769770/* Add the dirty-shutdown-counter value to the pdsm */771static int papr_pdsm_dsc(struct papr_scm_priv *p,772union nd_pdsm_payload *payload)773{774payload->health.extension_flags |= PDSM_DIMM_DSC_VALID;775payload->health.dimm_dsc = p->dirty_shutdown_counter;776777return sizeof(struct nd_papr_pdsm_health);778}779780/* Fetch the DIMM health info and populate it in provided package. */781static int papr_pdsm_health(struct papr_scm_priv *p,782union nd_pdsm_payload *payload)783{784int rc;785786/* Ensure dimm health mutex is taken preventing concurrent access */787rc = mutex_lock_interruptible(&p->health_mutex);788if (rc)789goto out;790791/* Always fetch upto date dimm health data ignoring cached values */792rc = __drc_pmem_query_health(p);793if (rc) {794mutex_unlock(&p->health_mutex);795goto out;796}797798/* update health struct with various flags derived from health bitmap */799payload->health = (struct nd_papr_pdsm_health) {800.extension_flags = 0,801.dimm_unarmed = !!(p->health_bitmap & PAPR_PMEM_UNARMED_MASK),802.dimm_bad_shutdown = !!(p->health_bitmap & PAPR_PMEM_BAD_SHUTDOWN_MASK),803.dimm_bad_restore = !!(p->health_bitmap & PAPR_PMEM_BAD_RESTORE_MASK),804.dimm_scrubbed = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),805.dimm_locked = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),806.dimm_encrypted = !!(p->health_bitmap & PAPR_PMEM_ENCRYPTED),807.dimm_health = PAPR_PDSM_DIMM_HEALTHY,808};809810/* Update field dimm_health based on health_bitmap flags */811if (p->health_bitmap & PAPR_PMEM_HEALTH_FATAL)812payload->health.dimm_health = PAPR_PDSM_DIMM_FATAL;813else if (p->health_bitmap & PAPR_PMEM_HEALTH_CRITICAL)814payload->health.dimm_health = PAPR_PDSM_DIMM_CRITICAL;815else if (p->health_bitmap & PAPR_PMEM_HEALTH_UNHEALTHY)816payload->health.dimm_health = PAPR_PDSM_DIMM_UNHEALTHY;817818/* struct populated hence can release the mutex now */819mutex_unlock(&p->health_mutex);820821/* Populate the fuel gauge meter in the payload */822papr_pdsm_fuel_gauge(p, payload);823/* Populate the dirty-shutdown-counter field */824papr_pdsm_dsc(p, payload);825826rc = sizeof(struct nd_papr_pdsm_health);827828out:829return rc;830}831832/* Inject a smart error Add the dirty-shutdown-counter value to the pdsm */833static int papr_pdsm_smart_inject(struct papr_scm_priv *p,834union nd_pdsm_payload *payload)835{836int rc;837u32 supported_flags = 0;838u64 inject_mask = 0, clear_mask = 0;839u64 mask;840841/* Check for individual smart error flags and update inject/clear masks */842if (payload->smart_inject.flags & PDSM_SMART_INJECT_HEALTH_FATAL) {843supported_flags |= PDSM_SMART_INJECT_HEALTH_FATAL;844if (payload->smart_inject.fatal_enable)845inject_mask |= PAPR_PMEM_HEALTH_FATAL;846else847clear_mask |= PAPR_PMEM_HEALTH_FATAL;848}849850if (payload->smart_inject.flags & PDSM_SMART_INJECT_BAD_SHUTDOWN) {851supported_flags |= PDSM_SMART_INJECT_BAD_SHUTDOWN;852if (payload->smart_inject.unsafe_shutdown_enable)853inject_mask |= PAPR_PMEM_SHUTDOWN_DIRTY;854else855clear_mask |= PAPR_PMEM_SHUTDOWN_DIRTY;856}857858dev_dbg(&p->pdev->dev, "[Smart-inject] inject_mask=%#llx clear_mask=%#llx\n",859inject_mask, clear_mask);860861/* Prevent concurrent access to dimm health bitmap related members */862rc = mutex_lock_interruptible(&p->health_mutex);863if (rc)864return rc;865866/* Use inject/clear masks to set health_bitmap_inject_mask */867mask = READ_ONCE(p->health_bitmap_inject_mask);868mask = (mask & ~clear_mask) | inject_mask;869WRITE_ONCE(p->health_bitmap_inject_mask, mask);870871/* Invalidate cached health bitmap */872p->lasthealth_jiffies = 0;873874mutex_unlock(&p->health_mutex);875876/* Return the supported flags back to userspace */877payload->smart_inject.flags = supported_flags;878879return sizeof(struct nd_papr_pdsm_health);880}881882/*883* 'struct pdsm_cmd_desc'884* Identifies supported PDSMs' expected length of in/out payloads885* and pdsm service function.886*887* size_in : Size of input payload if any in the PDSM request.888* size_out : Size of output payload if any in the PDSM request.889* service : Service function for the PDSM request. Return semantics:890* rc < 0 : Error servicing PDSM and rc indicates the error.891* rc >=0 : Serviced successfully and 'rc' indicate number of892* bytes written to payload.893*/894struct pdsm_cmd_desc {895u32 size_in;896u32 size_out;897int (*service)(struct papr_scm_priv *dimm,898union nd_pdsm_payload *payload);899};900901/* Holds all supported PDSMs' command descriptors */902static const struct pdsm_cmd_desc __pdsm_cmd_descriptors[] = {903[PAPR_PDSM_MIN] = {904.size_in = 0,905.size_out = 0,906.service = NULL,907},908/* New PDSM command descriptors to be added below */909910[PAPR_PDSM_HEALTH] = {911.size_in = 0,912.size_out = sizeof(struct nd_papr_pdsm_health),913.service = papr_pdsm_health,914},915916[PAPR_PDSM_SMART_INJECT] = {917.size_in = sizeof(struct nd_papr_pdsm_smart_inject),918.size_out = sizeof(struct nd_papr_pdsm_smart_inject),919.service = papr_pdsm_smart_inject,920},921/* Empty */922[PAPR_PDSM_MAX] = {923.size_in = 0,924.size_out = 0,925.service = NULL,926},927};928929/* Given a valid pdsm cmd return its command descriptor else return NULL */930static inline const struct pdsm_cmd_desc *pdsm_cmd_desc(enum papr_pdsm cmd)931{932if (cmd >= 0 || cmd < ARRAY_SIZE(__pdsm_cmd_descriptors))933return &__pdsm_cmd_descriptors[cmd];934935return NULL;936}937938/*939* For a given pdsm request call an appropriate service function.940* Returns errors if any while handling the pdsm command package.941*/942static int papr_scm_service_pdsm(struct papr_scm_priv *p,943struct nd_cmd_pkg *pkg)944{945/* Get the PDSM header and PDSM command */946struct nd_pkg_pdsm *pdsm_pkg = (struct nd_pkg_pdsm *)pkg->nd_payload;947enum papr_pdsm pdsm = (enum papr_pdsm)pkg->nd_command;948const struct pdsm_cmd_desc *pdsc;949int rc;950951/* Fetch corresponding pdsm descriptor for validation and servicing */952pdsc = pdsm_cmd_desc(pdsm);953954/* Validate pdsm descriptor */955/* Ensure that reserved fields are 0 */956if (pdsm_pkg->reserved[0] || pdsm_pkg->reserved[1]) {957dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid reserved field\n",958pdsm);959return -EINVAL;960}961962/* If pdsm expects some input, then ensure that the size_in matches */963if (pdsc->size_in &&964pkg->nd_size_in != (pdsc->size_in + ND_PDSM_HDR_SIZE)) {965dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_in=%d\n",966pdsm, pkg->nd_size_in);967return -EINVAL;968}969970/* If pdsm wants to return data, then ensure that size_out matches */971if (pdsc->size_out &&972pkg->nd_size_out != (pdsc->size_out + ND_PDSM_HDR_SIZE)) {973dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_out=%d\n",974pdsm, pkg->nd_size_out);975return -EINVAL;976}977978/* Service the pdsm */979if (pdsc->service) {980dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Servicing..\n", pdsm);981982rc = pdsc->service(p, &pdsm_pkg->payload);983984if (rc < 0) {985/* error encountered while servicing pdsm */986pdsm_pkg->cmd_status = rc;987pkg->nd_fw_size = ND_PDSM_HDR_SIZE;988} else {989/* pdsm serviced and 'rc' bytes written to payload */990pdsm_pkg->cmd_status = 0;991pkg->nd_fw_size = ND_PDSM_HDR_SIZE + rc;992}993} else {994dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Unsupported PDSM request\n",995pdsm);996pdsm_pkg->cmd_status = -ENOENT;997pkg->nd_fw_size = ND_PDSM_HDR_SIZE;998}9991000return pdsm_pkg->cmd_status;1001}10021003static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc,1004struct nvdimm *nvdimm, unsigned int cmd, void *buf,1005unsigned int buf_len, int *cmd_rc)1006{1007struct nd_cmd_get_config_size *get_size_hdr;1008struct nd_cmd_pkg *call_pkg = NULL;1009struct papr_scm_priv *p;1010int rc;10111012rc = is_cmd_valid(nvdimm, cmd, buf, buf_len);1013if (rc) {1014pr_debug("Invalid cmd=0x%x. Err=%d\n", cmd, rc);1015return rc;1016}10171018/* Use a local variable in case cmd_rc pointer is NULL */1019if (!cmd_rc)1020cmd_rc = &rc;10211022p = nvdimm_provider_data(nvdimm);10231024switch (cmd) {1025case ND_CMD_GET_CONFIG_SIZE:1026get_size_hdr = buf;10271028get_size_hdr->status = 0;1029get_size_hdr->max_xfer = 8;1030get_size_hdr->config_size = p->metadata_size;1031*cmd_rc = 0;1032break;10331034case ND_CMD_GET_CONFIG_DATA:1035*cmd_rc = papr_scm_meta_get(p, buf);1036break;10371038case ND_CMD_SET_CONFIG_DATA:1039*cmd_rc = papr_scm_meta_set(p, buf);1040break;10411042case ND_CMD_CALL:1043call_pkg = (struct nd_cmd_pkg *)buf;1044*cmd_rc = papr_scm_service_pdsm(p, call_pkg);1045break;10461047default:1048dev_dbg(&p->pdev->dev, "Unknown command = %d\n", cmd);1049return -EINVAL;1050}10511052dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);10531054return 0;1055}10561057static ssize_t health_bitmap_inject_show(struct device *dev,1058struct device_attribute *attr,1059char *buf)1060{1061struct nvdimm *dimm = to_nvdimm(dev);1062struct papr_scm_priv *p = nvdimm_provider_data(dimm);10631064return sprintf(buf, "%#llx\n",1065READ_ONCE(p->health_bitmap_inject_mask));1066}10671068static DEVICE_ATTR_ADMIN_RO(health_bitmap_inject);10691070static ssize_t perf_stats_show(struct device *dev,1071struct device_attribute *attr, char *buf)1072{1073int index;1074ssize_t rc;1075struct seq_buf s;1076struct papr_scm_perf_stat *stat;1077struct papr_scm_perf_stats *stats;1078struct nvdimm *dimm = to_nvdimm(dev);1079struct papr_scm_priv *p = nvdimm_provider_data(dimm);10801081if (!p->stat_buffer_len)1082return -ENOENT;10831084/* Allocate the buffer for phyp where stats are written */1085stats = kzalloc(p->stat_buffer_len, GFP_KERNEL);1086if (!stats)1087return -ENOMEM;10881089/* Ask phyp to return all dimm perf stats */1090rc = drc_pmem_query_stats(p, stats, 0);1091if (rc)1092goto free_stats;1093/*1094* Go through the returned output buffer and print stats and1095* values. Since stat_id is essentially a char string of1096* 8 bytes, simply use the string format specifier to print it.1097*/1098seq_buf_init(&s, buf, PAGE_SIZE);1099for (index = 0, stat = stats->scm_statistic;1100index < be32_to_cpu(stats->num_statistics);1101++index, ++stat) {1102seq_buf_printf(&s, "%.8s = 0x%016llX\n",1103stat->stat_id,1104be64_to_cpu(stat->stat_val));1105}11061107free_stats:1108kfree(stats);1109return rc ? rc : (ssize_t)seq_buf_used(&s);1110}1111static DEVICE_ATTR_ADMIN_RO(perf_stats);11121113static ssize_t flags_show(struct device *dev,1114struct device_attribute *attr, char *buf)1115{1116struct nvdimm *dimm = to_nvdimm(dev);1117struct papr_scm_priv *p = nvdimm_provider_data(dimm);1118struct seq_buf s;1119u64 health;1120int rc;11211122rc = drc_pmem_query_health(p);1123if (rc)1124return rc;11251126/* Copy health_bitmap locally, check masks & update out buffer */1127health = READ_ONCE(p->health_bitmap);11281129seq_buf_init(&s, buf, PAGE_SIZE);1130if (health & PAPR_PMEM_UNARMED_MASK)1131seq_buf_printf(&s, "not_armed ");11321133if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK)1134seq_buf_printf(&s, "flush_fail ");11351136if (health & PAPR_PMEM_BAD_RESTORE_MASK)1137seq_buf_printf(&s, "restore_fail ");11381139if (health & PAPR_PMEM_ENCRYPTED)1140seq_buf_printf(&s, "encrypted ");11411142if (health & PAPR_PMEM_SMART_EVENT_MASK)1143seq_buf_printf(&s, "smart_notify ");11441145if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED)1146seq_buf_printf(&s, "scrubbed locked ");11471148if (seq_buf_used(&s))1149seq_buf_printf(&s, "\n");11501151return seq_buf_used(&s);1152}1153DEVICE_ATTR_RO(flags);11541155static ssize_t dirty_shutdown_show(struct device *dev,1156struct device_attribute *attr, char *buf)1157{1158struct nvdimm *dimm = to_nvdimm(dev);1159struct papr_scm_priv *p = nvdimm_provider_data(dimm);11601161return sysfs_emit(buf, "%llu\n", p->dirty_shutdown_counter);1162}1163DEVICE_ATTR_RO(dirty_shutdown);11641165static umode_t papr_nd_attribute_visible(struct kobject *kobj,1166struct attribute *attr, int n)1167{1168struct device *dev = kobj_to_dev(kobj);1169struct nvdimm *nvdimm = to_nvdimm(dev);1170struct papr_scm_priv *p = nvdimm_provider_data(nvdimm);11711172/* For if perf-stats not available remove perf_stats sysfs */1173if (attr == &dev_attr_perf_stats.attr && p->stat_buffer_len == 0)1174return 0;11751176return attr->mode;1177}11781179/* papr_scm specific dimm attributes */1180static struct attribute *papr_nd_attributes[] = {1181&dev_attr_flags.attr,1182&dev_attr_perf_stats.attr,1183&dev_attr_dirty_shutdown.attr,1184&dev_attr_health_bitmap_inject.attr,1185NULL,1186};11871188static const struct attribute_group papr_nd_attribute_group = {1189.name = "papr",1190.is_visible = papr_nd_attribute_visible,1191.attrs = papr_nd_attributes,1192};11931194static const struct attribute_group *papr_nd_attr_groups[] = {1195&papr_nd_attribute_group,1196NULL,1197};11981199static int papr_scm_nvdimm_init(struct papr_scm_priv *p)1200{1201struct device *dev = &p->pdev->dev;1202struct nd_mapping_desc mapping;1203struct nd_region_desc ndr_desc;1204unsigned long dimm_flags;1205int target_nid, online_nid;12061207p->bus_desc.ndctl = papr_scm_ndctl;1208p->bus_desc.module = THIS_MODULE;1209p->bus_desc.of_node = p->pdev->dev.of_node;1210p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);12111212/* Set the dimm command family mask to accept PDSMs */1213set_bit(NVDIMM_FAMILY_PAPR, &p->bus_desc.dimm_family_mask);12141215if (!p->bus_desc.provider_name)1216return -ENOMEM;12171218p->bus = nvdimm_bus_register(NULL, &p->bus_desc);1219if (!p->bus) {1220dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);1221kfree(p->bus_desc.provider_name);1222return -ENXIO;1223}12241225dimm_flags = 0;1226set_bit(NDD_LABELING, &dimm_flags);12271228/*1229* Check if the nvdimm is unarmed. No locking needed as we are still1230* initializing. Ignore error encountered if any.1231*/1232__drc_pmem_query_health(p);12331234if (p->health_bitmap & PAPR_PMEM_UNARMED_MASK)1235set_bit(NDD_UNARMED, &dimm_flags);12361237p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups,1238dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);1239if (!p->nvdimm) {1240dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);1241goto err;1242}12431244if (nvdimm_bus_check_dimm_count(p->bus, 1))1245goto err;12461247/* now add the region */12481249memset(&mapping, 0, sizeof(mapping));1250mapping.nvdimm = p->nvdimm;1251mapping.start = 0;1252mapping.size = p->blocks * p->block_size; // XXX: potential overflow?12531254memset(&ndr_desc, 0, sizeof(ndr_desc));1255target_nid = dev_to_node(&p->pdev->dev);1256online_nid = numa_map_to_online_node(target_nid);1257ndr_desc.numa_node = online_nid;1258ndr_desc.target_node = target_nid;1259ndr_desc.res = &p->res;1260ndr_desc.of_node = p->dn;1261ndr_desc.provider_data = p;1262ndr_desc.mapping = &mapping;1263ndr_desc.num_mappings = 1;1264ndr_desc.nd_set = &p->nd_set;12651266if (p->hcall_flush_required) {1267set_bit(ND_REGION_ASYNC, &ndr_desc.flags);1268ndr_desc.flush = papr_scm_pmem_flush;1269}12701271if (p->is_volatile)1272p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);1273else {1274set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);1275p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);1276}1277if (!p->region) {1278dev_err(dev, "Error registering region %pR from %pOF\n",1279ndr_desc.res, p->dn);1280goto err;1281}1282if (target_nid != online_nid)1283dev_info(dev, "Region registered with target node %d and online node %d",1284target_nid, online_nid);12851286mutex_lock(&papr_ndr_lock);1287list_add_tail(&p->region_list, &papr_nd_regions);1288mutex_unlock(&papr_ndr_lock);12891290return 0;12911292err: nvdimm_bus_unregister(p->bus);1293kfree(p->bus_desc.provider_name);1294return -ENXIO;1295}12961297static void papr_scm_add_badblock(struct nd_region *region,1298struct nvdimm_bus *bus, u64 phys_addr)1299{1300u64 aligned_addr = ALIGN_DOWN(phys_addr, L1_CACHE_BYTES);13011302if (nvdimm_bus_add_badrange(bus, aligned_addr, L1_CACHE_BYTES)) {1303pr_err("Bad block registration for 0x%llx failed\n", phys_addr);1304return;1305}13061307pr_debug("Add memory range (0x%llx - 0x%llx) as bad range\n",1308aligned_addr, aligned_addr + L1_CACHE_BYTES);13091310nvdimm_region_notify(region, NVDIMM_REVALIDATE_POISON);1311}13121313static int handle_mce_ue(struct notifier_block *nb, unsigned long val,1314void *data)1315{1316struct machine_check_event *evt = data;1317struct papr_scm_priv *p;1318u64 phys_addr;1319bool found = false;13201321if (evt->error_type != MCE_ERROR_TYPE_UE)1322return NOTIFY_DONE;13231324if (list_empty(&papr_nd_regions))1325return NOTIFY_DONE;13261327/*1328* The physical address obtained here is PAGE_SIZE aligned, so get the1329* exact address from the effective address1330*/1331phys_addr = evt->u.ue_error.physical_address +1332(evt->u.ue_error.effective_address & ~PAGE_MASK);13331334if (!evt->u.ue_error.physical_address_provided ||1335!is_zone_device_page(pfn_to_page(phys_addr >> PAGE_SHIFT)))1336return NOTIFY_DONE;13371338/* mce notifier is called from a process context, so mutex is safe */1339mutex_lock(&papr_ndr_lock);1340list_for_each_entry(p, &papr_nd_regions, region_list) {1341if (phys_addr >= p->res.start && phys_addr <= p->res.end) {1342found = true;1343break;1344}1345}13461347if (found)1348papr_scm_add_badblock(p->region, p->bus, phys_addr);13491350mutex_unlock(&papr_ndr_lock);13511352return found ? NOTIFY_OK : NOTIFY_DONE;1353}13541355static struct notifier_block mce_ue_nb = {1356.notifier_call = handle_mce_ue1357};13581359static int papr_scm_probe(struct platform_device *pdev)1360{1361struct device_node *dn = pdev->dev.of_node;1362u32 drc_index, metadata_size;1363u64 blocks, block_size;1364struct papr_scm_priv *p;1365u8 uuid_raw[UUID_SIZE];1366const char *uuid_str;1367ssize_t stat_size;1368uuid_t uuid;1369int rc;13701371/* check we have all the required DT properties */1372if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {1373dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);1374return -ENODEV;1375}13761377if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {1378dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);1379return -ENODEV;1380}13811382if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {1383dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);1384return -ENODEV;1385}13861387if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {1388dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);1389return -ENODEV;1390}13911392/*1393* open firmware platform device create won't update the NUMA1394* distance table. For PAPR SCM devices we use numa_map_to_online_node()1395* to find the nearest online NUMA node and that requires correct1396* distance table information.1397*/1398update_numa_distance(dn);13991400p = kzalloc(sizeof(*p), GFP_KERNEL);1401if (!p)1402return -ENOMEM;14031404/* Initialize the dimm mutex */1405mutex_init(&p->health_mutex);14061407/* optional DT properties */1408of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);14091410p->dn = dn;1411p->drc_index = drc_index;1412p->block_size = block_size;1413p->blocks = blocks;1414p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");1415p->hcall_flush_required = of_property_read_bool(dn, "ibm,hcall-flush-required");14161417if (of_property_read_u64(dn, "ibm,persistence-failed-count",1418&p->dirty_shutdown_counter))1419p->dirty_shutdown_counter = 0;14201421/* We just need to ensure that set cookies are unique across */1422uuid_parse(uuid_str, &uuid);14231424/*1425* The cookie1 and cookie2 are not really little endian.1426* We store a raw buffer representation of the1427* uuid string so that we can compare this with the label1428* area cookie irrespective of the endian configuration1429* with which the kernel is built.1430*1431* Historically we stored the cookie in the below format.1432* for a uuid string 72511b67-0b3b-42fd-8d1d-5be3cae8bcaa1433* cookie1 was 0xfd423b0b671b51721434* cookie2 was 0xaabce8cae35b1d8d1435*/1436export_uuid(uuid_raw, &uuid);1437p->nd_set.cookie1 = get_unaligned_le64(&uuid_raw[0]);1438p->nd_set.cookie2 = get_unaligned_le64(&uuid_raw[8]);14391440/* might be zero */1441p->metadata_size = metadata_size;1442p->pdev = pdev;14431444/* request the hypervisor to bind this region to somewhere in memory */1445rc = drc_pmem_bind(p);14461447/* If phyp says drc memory still bound then force unbound and retry */1448if (rc == H_OVERLAP)1449rc = drc_pmem_query_n_bind(p);14501451if (rc != H_SUCCESS) {1452dev_err(&p->pdev->dev, "bind err: %d\n", rc);1453rc = -ENXIO;1454goto err;1455}14561457/* setup the resource for the newly bound range */1458p->res.start = p->bound_addr;1459p->res.end = p->bound_addr + p->blocks * p->block_size - 1;1460p->res.name = pdev->name;1461p->res.flags = IORESOURCE_MEM;14621463/* Try retrieving the stat buffer and see if its supported */1464stat_size = drc_pmem_query_stats(p, NULL, 0);1465if (stat_size > 0) {1466p->stat_buffer_len = stat_size;1467dev_dbg(&p->pdev->dev, "Max perf-stat size %lu-bytes\n",1468p->stat_buffer_len);1469}14701471rc = papr_scm_nvdimm_init(p);1472if (rc)1473goto err2;14741475platform_set_drvdata(pdev, p);1476papr_scm_pmu_register(p);14771478return 0;14791480err2: drc_pmem_unbind(p);1481err: kfree(p);1482return rc;1483}14841485static void papr_scm_remove(struct platform_device *pdev)1486{1487struct papr_scm_priv *p = platform_get_drvdata(pdev);14881489mutex_lock(&papr_ndr_lock);1490list_del(&p->region_list);1491mutex_unlock(&papr_ndr_lock);14921493nvdimm_bus_unregister(p->bus);1494drc_pmem_unbind(p);14951496if (pdev->archdata.priv)1497unregister_nvdimm_pmu(pdev->archdata.priv);14981499pdev->archdata.priv = NULL;1500kfree(p->bus_desc.provider_name);1501kfree(p);1502}15031504static const struct of_device_id papr_scm_match[] = {1505{ .compatible = "ibm,pmemory" },1506{ .compatible = "ibm,pmemory-v2" },1507{ },1508};15091510static struct platform_driver papr_scm_driver = {1511.probe = papr_scm_probe,1512.remove = papr_scm_remove,1513.driver = {1514.name = "papr_scm",1515.of_match_table = papr_scm_match,1516},1517};15181519static int __init papr_scm_init(void)1520{1521int ret;15221523ret = platform_driver_register(&papr_scm_driver);1524if (!ret)1525mce_register_notifier(&mce_ue_nb);15261527return ret;1528}1529module_init(papr_scm_init);15301531static void __exit papr_scm_exit(void)1532{1533mce_unregister_notifier(&mce_ue_nb);1534platform_driver_unregister(&papr_scm_driver);1535}1536module_exit(papr_scm_exit);15371538MODULE_DEVICE_TABLE(of, papr_scm_match);1539MODULE_DESCRIPTION("PAPR Storage Class Memory interface driver");1540MODULE_LICENSE("GPL");1541MODULE_AUTHOR("IBM Corporation");154215431544