cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

reset.c (8592B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 * System Control and Management Interface (SCMI) Reset Protocol
      4 *
      5 * Copyright (C) 2019-2022 ARM Ltd.
      6 */
      7
      8#define pr_fmt(fmt) "SCMI Notifications RESET - " fmt
      9
     10#include <linux/module.h>
     11#include <linux/scmi_protocol.h>
     12
     13#include "protocols.h"
     14#include "notify.h"
     15
     16enum scmi_reset_protocol_cmd {
     17	RESET_DOMAIN_ATTRIBUTES = 0x3,
     18	RESET = 0x4,
     19	RESET_NOTIFY = 0x5,
     20	RESET_DOMAIN_NAME_GET = 0x6,
     21};
     22
     23#define NUM_RESET_DOMAIN_MASK	0xffff
     24#define RESET_NOTIFY_ENABLE	BIT(0)
     25
     26struct scmi_msg_resp_reset_domain_attributes {
     27	__le32 attributes;
     28#define SUPPORTS_ASYNC_RESET(x)		((x) & BIT(31))
     29#define SUPPORTS_NOTIFY_RESET(x)	((x) & BIT(30))
     30#define SUPPORTS_EXTENDED_NAMES(x)	((x) & BIT(29))
     31	__le32 latency;
     32	u8 name[SCMI_SHORT_NAME_MAX_SIZE];
     33};
     34
     35struct scmi_msg_reset_domain_reset {
     36	__le32 domain_id;
     37	__le32 flags;
     38#define AUTONOMOUS_RESET	BIT(0)
     39#define EXPLICIT_RESET_ASSERT	BIT(1)
     40#define ASYNCHRONOUS_RESET	BIT(2)
     41	__le32 reset_state;
     42#define ARCH_COLD_RESET		0
     43};
     44
     45struct scmi_msg_reset_notify {
     46	__le32 id;
     47	__le32 event_control;
     48#define RESET_TP_NOTIFY_ALL	BIT(0)
     49};
     50
     51struct scmi_reset_issued_notify_payld {
     52	__le32 agent_id;
     53	__le32 domain_id;
     54	__le32 reset_state;
     55};
     56
     57struct reset_dom_info {
     58	bool async_reset;
     59	bool reset_notify;
     60	u32 latency_us;
     61	char name[SCMI_MAX_STR_SIZE];
     62};
     63
     64struct scmi_reset_info {
     65	u32 version;
     66	int num_domains;
     67	struct reset_dom_info *dom_info;
     68};
     69
     70static int scmi_reset_attributes_get(const struct scmi_protocol_handle *ph,
     71				     struct scmi_reset_info *pi)
     72{
     73	int ret;
     74	struct scmi_xfer *t;
     75	u32 attr;
     76
     77	ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
     78				      0, sizeof(attr), &t);
     79	if (ret)
     80		return ret;
     81
     82	ret = ph->xops->do_xfer(ph, t);
     83	if (!ret) {
     84		attr = get_unaligned_le32(t->rx.buf);
     85		pi->num_domains = attr & NUM_RESET_DOMAIN_MASK;
     86	}
     87
     88	ph->xops->xfer_put(ph, t);
     89	return ret;
     90}
     91
     92static int
     93scmi_reset_domain_attributes_get(const struct scmi_protocol_handle *ph,
     94				 u32 domain, struct reset_dom_info *dom_info,
     95				 u32 version)
     96{
     97	int ret;
     98	u32 attributes;
     99	struct scmi_xfer *t;
    100	struct scmi_msg_resp_reset_domain_attributes *attr;
    101
    102	ret = ph->xops->xfer_get_init(ph, RESET_DOMAIN_ATTRIBUTES,
    103				      sizeof(domain), sizeof(*attr), &t);
    104	if (ret)
    105		return ret;
    106
    107	put_unaligned_le32(domain, t->tx.buf);
    108	attr = t->rx.buf;
    109
    110	ret = ph->xops->do_xfer(ph, t);
    111	if (!ret) {
    112		attributes = le32_to_cpu(attr->attributes);
    113
    114		dom_info->async_reset = SUPPORTS_ASYNC_RESET(attributes);
    115		dom_info->reset_notify = SUPPORTS_NOTIFY_RESET(attributes);
    116		dom_info->latency_us = le32_to_cpu(attr->latency);
    117		if (dom_info->latency_us == U32_MAX)
    118			dom_info->latency_us = 0;
    119		strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
    120	}
    121
    122	ph->xops->xfer_put(ph, t);
    123
    124	/*
    125	 * If supported overwrite short name with the extended one;
    126	 * on error just carry on and use already provided short name.
    127	 */
    128	if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
    129	    SUPPORTS_EXTENDED_NAMES(attributes))
    130		ph->hops->extended_name_get(ph, RESET_DOMAIN_NAME_GET, domain,
    131					    dom_info->name, SCMI_MAX_STR_SIZE);
    132
    133	return ret;
    134}
    135
    136static int scmi_reset_num_domains_get(const struct scmi_protocol_handle *ph)
    137{
    138	struct scmi_reset_info *pi = ph->get_priv(ph);
    139
    140	return pi->num_domains;
    141}
    142
    143static const char *
    144scmi_reset_name_get(const struct scmi_protocol_handle *ph, u32 domain)
    145{
    146	struct scmi_reset_info *pi = ph->get_priv(ph);
    147
    148	struct reset_dom_info *dom = pi->dom_info + domain;
    149
    150	return dom->name;
    151}
    152
    153static int scmi_reset_latency_get(const struct scmi_protocol_handle *ph,
    154				  u32 domain)
    155{
    156	struct scmi_reset_info *pi = ph->get_priv(ph);
    157	struct reset_dom_info *dom = pi->dom_info + domain;
    158
    159	return dom->latency_us;
    160}
    161
    162static int scmi_domain_reset(const struct scmi_protocol_handle *ph, u32 domain,
    163			     u32 flags, u32 state)
    164{
    165	int ret;
    166	struct scmi_xfer *t;
    167	struct scmi_msg_reset_domain_reset *dom;
    168	struct scmi_reset_info *pi = ph->get_priv(ph);
    169	struct reset_dom_info *rdom = pi->dom_info + domain;
    170
    171	if (rdom->async_reset)
    172		flags |= ASYNCHRONOUS_RESET;
    173
    174	ret = ph->xops->xfer_get_init(ph, RESET, sizeof(*dom), 0, &t);
    175	if (ret)
    176		return ret;
    177
    178	dom = t->tx.buf;
    179	dom->domain_id = cpu_to_le32(domain);
    180	dom->flags = cpu_to_le32(flags);
    181	dom->reset_state = cpu_to_le32(state);
    182
    183	if (rdom->async_reset)
    184		ret = ph->xops->do_xfer_with_response(ph, t);
    185	else
    186		ret = ph->xops->do_xfer(ph, t);
    187
    188	ph->xops->xfer_put(ph, t);
    189	return ret;
    190}
    191
    192static int scmi_reset_domain_reset(const struct scmi_protocol_handle *ph,
    193				   u32 domain)
    194{
    195	return scmi_domain_reset(ph, domain, AUTONOMOUS_RESET,
    196				 ARCH_COLD_RESET);
    197}
    198
    199static int
    200scmi_reset_domain_assert(const struct scmi_protocol_handle *ph, u32 domain)
    201{
    202	return scmi_domain_reset(ph, domain, EXPLICIT_RESET_ASSERT,
    203				 ARCH_COLD_RESET);
    204}
    205
    206static int
    207scmi_reset_domain_deassert(const struct scmi_protocol_handle *ph, u32 domain)
    208{
    209	return scmi_domain_reset(ph, domain, 0, ARCH_COLD_RESET);
    210}
    211
    212static const struct scmi_reset_proto_ops reset_proto_ops = {
    213	.num_domains_get = scmi_reset_num_domains_get,
    214	.name_get = scmi_reset_name_get,
    215	.latency_get = scmi_reset_latency_get,
    216	.reset = scmi_reset_domain_reset,
    217	.assert = scmi_reset_domain_assert,
    218	.deassert = scmi_reset_domain_deassert,
    219};
    220
    221static int scmi_reset_notify(const struct scmi_protocol_handle *ph,
    222			     u32 domain_id, bool enable)
    223{
    224	int ret;
    225	u32 evt_cntl = enable ? RESET_TP_NOTIFY_ALL : 0;
    226	struct scmi_xfer *t;
    227	struct scmi_msg_reset_notify *cfg;
    228
    229	ret = ph->xops->xfer_get_init(ph, RESET_NOTIFY, sizeof(*cfg), 0, &t);
    230	if (ret)
    231		return ret;
    232
    233	cfg = t->tx.buf;
    234	cfg->id = cpu_to_le32(domain_id);
    235	cfg->event_control = cpu_to_le32(evt_cntl);
    236
    237	ret = ph->xops->do_xfer(ph, t);
    238
    239	ph->xops->xfer_put(ph, t);
    240	return ret;
    241}
    242
    243static int scmi_reset_set_notify_enabled(const struct scmi_protocol_handle *ph,
    244					 u8 evt_id, u32 src_id, bool enable)
    245{
    246	int ret;
    247
    248	ret = scmi_reset_notify(ph, src_id, enable);
    249	if (ret)
    250		pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
    251			 evt_id, src_id, ret);
    252
    253	return ret;
    254}
    255
    256static void *
    257scmi_reset_fill_custom_report(const struct scmi_protocol_handle *ph,
    258			      u8 evt_id, ktime_t timestamp,
    259			      const void *payld, size_t payld_sz,
    260			      void *report, u32 *src_id)
    261{
    262	const struct scmi_reset_issued_notify_payld *p = payld;
    263	struct scmi_reset_issued_report *r = report;
    264
    265	if (evt_id != SCMI_EVENT_RESET_ISSUED || sizeof(*p) != payld_sz)
    266		return NULL;
    267
    268	r->timestamp = timestamp;
    269	r->agent_id = le32_to_cpu(p->agent_id);
    270	r->domain_id = le32_to_cpu(p->domain_id);
    271	r->reset_state = le32_to_cpu(p->reset_state);
    272	*src_id = r->domain_id;
    273
    274	return r;
    275}
    276
    277static int scmi_reset_get_num_sources(const struct scmi_protocol_handle *ph)
    278{
    279	struct scmi_reset_info *pinfo = ph->get_priv(ph);
    280
    281	if (!pinfo)
    282		return -EINVAL;
    283
    284	return pinfo->num_domains;
    285}
    286
    287static const struct scmi_event reset_events[] = {
    288	{
    289		.id = SCMI_EVENT_RESET_ISSUED,
    290		.max_payld_sz = sizeof(struct scmi_reset_issued_notify_payld),
    291		.max_report_sz = sizeof(struct scmi_reset_issued_report),
    292	},
    293};
    294
    295static const struct scmi_event_ops reset_event_ops = {
    296	.get_num_sources = scmi_reset_get_num_sources,
    297	.set_notify_enabled = scmi_reset_set_notify_enabled,
    298	.fill_custom_report = scmi_reset_fill_custom_report,
    299};
    300
    301static const struct scmi_protocol_events reset_protocol_events = {
    302	.queue_sz = SCMI_PROTO_QUEUE_SZ,
    303	.ops = &reset_event_ops,
    304	.evts = reset_events,
    305	.num_events = ARRAY_SIZE(reset_events),
    306};
    307
    308static int scmi_reset_protocol_init(const struct scmi_protocol_handle *ph)
    309{
    310	int domain, ret;
    311	u32 version;
    312	struct scmi_reset_info *pinfo;
    313
    314	ret = ph->xops->version_get(ph, &version);
    315	if (ret)
    316		return ret;
    317
    318	dev_dbg(ph->dev, "Reset Version %d.%d\n",
    319		PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
    320
    321	pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
    322	if (!pinfo)
    323		return -ENOMEM;
    324
    325	ret = scmi_reset_attributes_get(ph, pinfo);
    326	if (ret)
    327		return ret;
    328
    329	pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
    330				       sizeof(*pinfo->dom_info), GFP_KERNEL);
    331	if (!pinfo->dom_info)
    332		return -ENOMEM;
    333
    334	for (domain = 0; domain < pinfo->num_domains; domain++) {
    335		struct reset_dom_info *dom = pinfo->dom_info + domain;
    336
    337		scmi_reset_domain_attributes_get(ph, domain, dom, version);
    338	}
    339
    340	pinfo->version = version;
    341	return ph->set_priv(ph, pinfo);
    342}
    343
    344static const struct scmi_protocol scmi_reset = {
    345	.id = SCMI_PROTOCOL_RESET,
    346	.owner = THIS_MODULE,
    347	.instance_init = &scmi_reset_protocol_init,
    348	.ops = &reset_proto_ops,
    349	.events = &reset_protocol_events,
    350};
    351
    352DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(reset, scmi_reset)