2017-06-06 18:32:24 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* System Control and Management Interface (SCMI) Power Protocol
|
|
|
|
*
|
2022-03-30 23:05:38 +08:00
|
|
|
* Copyright (C) 2018-2022 ARM Ltd.
|
2017-06-06 18:32:24 +08:00
|
|
|
*/
|
|
|
|
|
2020-07-01 23:53:44 +08:00
|
|
|
#define pr_fmt(fmt) "SCMI Notifications POWER - " fmt
|
|
|
|
|
2021-03-16 20:49:02 +08:00
|
|
|
#include <linux/module.h>
|
2020-07-01 23:53:44 +08:00
|
|
|
#include <linux/scmi_protocol.h>
|
|
|
|
|
2022-03-30 23:05:38 +08:00
|
|
|
#include "protocols.h"
|
2020-07-01 23:53:44 +08:00
|
|
|
#include "notify.h"
|
2017-06-06 18:32:24 +08:00
|
|
|
|
|
|
|
enum scmi_power_protocol_cmd {
|
|
|
|
POWER_DOMAIN_ATTRIBUTES = 0x3,
|
|
|
|
POWER_STATE_SET = 0x4,
|
|
|
|
POWER_STATE_GET = 0x5,
|
|
|
|
POWER_STATE_NOTIFY = 0x6,
|
2022-03-30 23:05:40 +08:00
|
|
|
POWER_DOMAIN_NAME_GET = 0x8,
|
2017-06-06 18:32:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct scmi_msg_resp_power_attributes {
|
|
|
|
__le16 num_domains;
|
|
|
|
__le16 reserved;
|
|
|
|
__le32 stats_addr_low;
|
|
|
|
__le32 stats_addr_high;
|
|
|
|
__le32 stats_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct scmi_msg_resp_power_domain_attributes {
|
|
|
|
__le32 flags;
|
|
|
|
#define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31))
|
|
|
|
#define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30))
|
|
|
|
#define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29))
|
2022-03-30 23:05:40 +08:00
|
|
|
#define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(27))
|
|
|
|
u8 name[SCMI_SHORT_NAME_MAX_SIZE];
|
2017-06-06 18:32:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct scmi_power_set_state {
|
|
|
|
__le32 flags;
|
|
|
|
#define STATE_SET_ASYNC BIT(0)
|
|
|
|
__le32 domain;
|
|
|
|
__le32 state;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct scmi_power_state_notify {
|
|
|
|
__le32 domain;
|
|
|
|
__le32 notify_enable;
|
|
|
|
};
|
|
|
|
|
2020-07-01 23:53:44 +08:00
|
|
|
struct scmi_power_state_notify_payld {
|
|
|
|
__le32 agent_id;
|
|
|
|
__le32 domain_id;
|
|
|
|
__le32 power_state;
|
|
|
|
};
|
|
|
|
|
2017-06-06 18:32:24 +08:00
|
|
|
struct power_dom_info {
|
|
|
|
bool state_set_sync;
|
|
|
|
bool state_set_async;
|
|
|
|
bool state_set_notify;
|
|
|
|
char name[SCMI_MAX_STR_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct scmi_power_info {
|
2019-11-22 22:48:40 +08:00
|
|
|
u32 version;
|
2017-06-06 18:32:24 +08:00
|
|
|
int num_domains;
|
|
|
|
u64 stats_addr;
|
|
|
|
u32 stats_size;
|
|
|
|
struct power_dom_info *dom_info;
|
|
|
|
};
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph,
|
2017-06-06 18:32:24 +08:00
|
|
|
struct scmi_power_info *pi)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct scmi_xfer *t;
|
|
|
|
struct scmi_msg_resp_power_attributes *attr;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
|
|
|
|
0, sizeof(*attr), &t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
attr = t->rx.buf;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->do_xfer(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (!ret) {
|
|
|
|
pi->num_domains = le16_to_cpu(attr->num_domains);
|
|
|
|
pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
|
|
|
|
(u64)le32_to_cpu(attr->stats_addr_high) << 32;
|
|
|
|
pi->stats_size = le32_to_cpu(attr->stats_size);
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ph->xops->xfer_put(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-03-16 20:48:39 +08:00
|
|
|
scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph,
|
2022-03-30 23:05:40 +08:00
|
|
|
u32 domain, struct power_dom_info *dom_info,
|
|
|
|
u32 version)
|
2017-06-06 18:32:24 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2022-03-30 23:05:40 +08:00
|
|
|
u32 flags;
|
2017-06-06 18:32:24 +08:00
|
|
|
struct scmi_xfer *t;
|
|
|
|
struct scmi_msg_resp_power_domain_attributes *attr;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES,
|
|
|
|
sizeof(domain), sizeof(*attr), &t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-07 20:46:27 +08:00
|
|
|
put_unaligned_le32(domain, t->tx.buf);
|
2017-06-06 18:32:24 +08:00
|
|
|
attr = t->rx.buf;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->do_xfer(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (!ret) {
|
2022-03-30 23:05:40 +08:00
|
|
|
flags = le32_to_cpu(attr->flags);
|
2017-06-06 18:32:24 +08:00
|
|
|
|
|
|
|
dom_info->state_set_notify = SUPPORTS_STATE_SET_NOTIFY(flags);
|
|
|
|
dom_info->state_set_async = SUPPORTS_STATE_SET_ASYNC(flags);
|
|
|
|
dom_info->state_set_sync = SUPPORTS_STATE_SET_SYNC(flags);
|
2022-06-08 17:55:28 +08:00
|
|
|
strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
|
2017-06-06 18:32:24 +08:00
|
|
|
}
|
2021-03-16 20:48:39 +08:00
|
|
|
ph->xops->xfer_put(ph, t);
|
2022-03-30 23:05:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If supported overwrite short name with the extended one;
|
|
|
|
* on error just carry on and use already provided short name.
|
|
|
|
*/
|
|
|
|
if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
|
|
|
|
SUPPORTS_EXTENDED_NAMES(flags)) {
|
|
|
|
ph->hops->extended_name_get(ph, POWER_DOMAIN_NAME_GET,
|
|
|
|
domain, dom_info->name,
|
|
|
|
SCMI_MAX_STR_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:32:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static int scmi_power_state_set(const struct scmi_protocol_handle *ph,
|
|
|
|
u32 domain, u32 state)
|
2017-06-06 18:32:24 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct scmi_xfer *t;
|
|
|
|
struct scmi_power_set_state *st;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
st = t->tx.buf;
|
|
|
|
st->flags = cpu_to_le32(0);
|
|
|
|
st->domain = cpu_to_le32(domain);
|
|
|
|
st->state = cpu_to_le32(state);
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->do_xfer(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ph->xops->xfer_put(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static int scmi_power_state_get(const struct scmi_protocol_handle *ph,
|
|
|
|
u32 domain, u32 *state)
|
2017-06-06 18:32:24 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct scmi_xfer *t;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-07 20:46:27 +08:00
|
|
|
put_unaligned_le32(domain, t->tx.buf);
|
2017-06-06 18:32:24 +08:00
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->do_xfer(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (!ret)
|
2019-08-07 20:46:27 +08:00
|
|
|
*state = get_unaligned_le32(t->rx.buf);
|
2017-06-06 18:32:24 +08:00
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ph->xops->xfer_put(ph, t);
|
2017-06-06 18:32:24 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph)
|
|
|
|
{
|
|
|
|
struct scmi_power_info *pi = ph->get_priv(ph);
|
2017-06-06 18:32:24 +08:00
|
|
|
|
|
|
|
return pi->num_domains;
|
|
|
|
}
|
|
|
|
|
2022-03-30 23:05:35 +08:00
|
|
|
static const char *
|
|
|
|
scmi_power_name_get(const struct scmi_protocol_handle *ph,
|
|
|
|
u32 domain)
|
2021-03-16 20:48:39 +08:00
|
|
|
{
|
|
|
|
struct scmi_power_info *pi = ph->get_priv(ph);
|
2017-06-06 18:32:24 +08:00
|
|
|
struct power_dom_info *dom = pi->dom_info + domain;
|
|
|
|
|
|
|
|
return dom->name;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static const struct scmi_power_proto_ops power_proto_ops = {
|
2017-06-06 18:32:24 +08:00
|
|
|
.num_domains_get = scmi_power_num_domains_get,
|
|
|
|
.name_get = scmi_power_name_get,
|
|
|
|
.state_set = scmi_power_state_set,
|
|
|
|
.state_get = scmi_power_state_get,
|
|
|
|
};
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static int scmi_power_request_notify(const struct scmi_protocol_handle *ph,
|
2020-07-01 23:53:44 +08:00
|
|
|
u32 domain, bool enable)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct scmi_xfer *t;
|
|
|
|
struct scmi_power_state_notify *notify;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY,
|
|
|
|
sizeof(*notify), 0, &t);
|
2020-07-01 23:53:44 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
notify = t->tx.buf;
|
|
|
|
notify->domain = cpu_to_le32(domain);
|
|
|
|
notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = ph->xops->do_xfer(ph, t);
|
2020-07-01 23:53:44 +08:00
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ph->xops->xfer_put(ph, t);
|
2020-07-01 23:53:44 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:59 +08:00
|
|
|
static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph,
|
2020-07-01 23:53:44 +08:00
|
|
|
u8 evt_id, u32 src_id, bool enable)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
ret = scmi_power_request_notify(ph, src_id, enable);
|
2020-07-01 23:53:44 +08:00
|
|
|
if (ret)
|
|
|
|
pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n",
|
|
|
|
evt_id, src_id, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:59 +08:00
|
|
|
static void *
|
|
|
|
scmi_power_fill_custom_report(const struct scmi_protocol_handle *ph,
|
|
|
|
u8 evt_id, ktime_t timestamp,
|
|
|
|
const void *payld, size_t payld_sz,
|
|
|
|
void *report, u32 *src_id)
|
2020-07-01 23:53:44 +08:00
|
|
|
{
|
|
|
|
const struct scmi_power_state_notify_payld *p = payld;
|
|
|
|
struct scmi_power_state_changed_report *r = report;
|
|
|
|
|
|
|
|
if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
r->timestamp = timestamp;
|
|
|
|
r->agent_id = le32_to_cpu(p->agent_id);
|
|
|
|
r->domain_id = le32_to_cpu(p->domain_id);
|
|
|
|
r->power_state = le32_to_cpu(p->power_state);
|
|
|
|
*src_id = r->domain_id;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:59 +08:00
|
|
|
static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph)
|
2021-03-16 20:48:31 +08:00
|
|
|
{
|
2021-03-16 20:48:59 +08:00
|
|
|
struct scmi_power_info *pinfo = ph->get_priv(ph);
|
2021-03-16 20:48:31 +08:00
|
|
|
|
|
|
|
if (!pinfo)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return pinfo->num_domains;
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:53:44 +08:00
|
|
|
static const struct scmi_event power_events[] = {
|
|
|
|
{
|
|
|
|
.id = SCMI_EVENT_POWER_STATE_CHANGED,
|
|
|
|
.max_payld_sz = sizeof(struct scmi_power_state_notify_payld),
|
|
|
|
.max_report_sz =
|
|
|
|
sizeof(struct scmi_power_state_changed_report),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct scmi_event_ops power_event_ops = {
|
2021-03-16 20:48:31 +08:00
|
|
|
.get_num_sources = scmi_power_get_num_sources,
|
2020-07-01 23:53:44 +08:00
|
|
|
.set_notify_enabled = scmi_power_set_notify_enabled,
|
|
|
|
.fill_custom_report = scmi_power_fill_custom_report,
|
|
|
|
};
|
|
|
|
|
2021-03-16 20:48:31 +08:00
|
|
|
static const struct scmi_protocol_events power_protocol_events = {
|
|
|
|
.queue_sz = SCMI_PROTO_QUEUE_SZ,
|
|
|
|
.ops = &power_event_ops,
|
|
|
|
.evts = power_events,
|
|
|
|
.num_events = ARRAY_SIZE(power_events),
|
|
|
|
};
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph)
|
2017-06-06 18:32:24 +08:00
|
|
|
{
|
2022-03-30 23:05:31 +08:00
|
|
|
int domain, ret;
|
2017-06-06 18:32:24 +08:00
|
|
|
u32 version;
|
|
|
|
struct scmi_power_info *pinfo;
|
|
|
|
|
2022-03-30 23:05:31 +08:00
|
|
|
ret = ph->xops->version_get(ph, &version);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-06-06 18:32:24 +08:00
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
dev_dbg(ph->dev, "Power Version %d.%d\n",
|
2017-06-06 18:32:24 +08:00
|
|
|
PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
|
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
|
2017-06-06 18:32:24 +08:00
|
|
|
if (!pinfo)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2022-03-30 23:05:31 +08:00
|
|
|
ret = scmi_power_attributes_get(ph, pinfo);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-06-06 18:32:24 +08:00
|
|
|
|
2021-03-16 20:48:39 +08:00
|
|
|
pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
|
2017-06-06 18:32:24 +08:00
|
|
|
sizeof(*pinfo->dom_info), GFP_KERNEL);
|
|
|
|
if (!pinfo->dom_info)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (domain = 0; domain < pinfo->num_domains; domain++) {
|
|
|
|
struct power_dom_info *dom = pinfo->dom_info + domain;
|
|
|
|
|
2022-03-30 23:05:40 +08:00
|
|
|
scmi_power_domain_attributes_get(ph, domain, dom, version);
|
2017-06-06 18:32:24 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:48:40 +08:00
|
|
|
pinfo->version = version;
|
2021-03-16 20:48:39 +08:00
|
|
|
|
|
|
|
return ph->set_priv(ph, pinfo);
|
2017-06-06 18:32:24 +08:00
|
|
|
}
|
|
|
|
|
2021-03-16 20:48:26 +08:00
|
|
|
static const struct scmi_protocol scmi_power = {
|
|
|
|
.id = SCMI_PROTOCOL_POWER,
|
2021-03-16 20:49:02 +08:00
|
|
|
.owner = THIS_MODULE,
|
2021-03-16 20:48:39 +08:00
|
|
|
.instance_init = &scmi_power_protocol_init,
|
|
|
|
.ops = &power_proto_ops,
|
2021-03-16 20:48:31 +08:00
|
|
|
.events = &power_protocol_events,
|
2021-03-16 20:48:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power)
|