system.c 3.2 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/*
 * System Control and Management Interface (SCMI) System Power Protocol
 *
5
 * Copyright (C) 2020-2021 ARM Ltd.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */

#define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt

#include <linux/scmi_protocol.h>

#include "common.h"
#include "notify.h"

#define SCMI_SYSTEM_NUM_SOURCES		1

enum scmi_system_protocol_cmd {
	SYSTEM_POWER_STATE_NOTIFY = 0x5,
};

struct scmi_system_power_state_notify {
	__le32 notify_enable;
};

struct scmi_system_power_state_notifier_payld {
	__le32 agent_id;
	__le32 flags;
	__le32 system_state;
};

struct scmi_system_info {
	u32 version;
};

35
static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
36 37 38 39 40 41
				      bool enable)
{
	int ret;
	struct scmi_xfer *t;
	struct scmi_system_power_state_notify *notify;

42 43
	ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
				      sizeof(*notify), 0, &t);
44 45 46 47 48 49
	if (ret)
		return ret;

	notify = t->tx.buf;
	notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;

50
	ret = ph->xops->do_xfer(ph, t);
51

52
	ph->xops->xfer_put(ph, t);
53 54 55
	return ret;
}

56
static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
57 58 59 60
					  u8 evt_id, u32 src_id, bool enable)
{
	int ret;

61
	ret = scmi_system_request_notify(ph, enable);
62 63 64 65 66 67
	if (ret)
		pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);

	return ret;
}

68 69 70 71 72
static void *
scmi_system_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)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	const struct scmi_system_power_state_notifier_payld *p = payld;
	struct scmi_system_power_state_notifier_report *r = report;

	if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
	    sizeof(*p) != payld_sz)
		return NULL;

	r->timestamp = timestamp;
	r->agent_id = le32_to_cpu(p->agent_id);
	r->flags = le32_to_cpu(p->flags);
	r->system_state = le32_to_cpu(p->system_state);
	*src_id = 0;

	return r;
}

static const struct scmi_event system_events[] = {
	{
		.id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
		.max_payld_sz =
			sizeof(struct scmi_system_power_state_notifier_payld),
		.max_report_sz =
			sizeof(struct scmi_system_power_state_notifier_report),
	},
};

static const struct scmi_event_ops system_event_ops = {
	.set_notify_enabled = scmi_system_set_notify_enabled,
	.fill_custom_report = scmi_system_fill_custom_report,
};

105 106 107 108 109 110 111 112
static const struct scmi_protocol_events system_protocol_events = {
	.queue_sz = SCMI_PROTO_QUEUE_SZ,
	.ops = &system_event_ops,
	.evts = system_events,
	.num_events = ARRAY_SIZE(system_events),
	.num_sources = SCMI_SYSTEM_NUM_SOURCES,
};

113
static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
114 115 116 117
{
	u32 version;
	struct scmi_system_info *pinfo;

118
	ph->xops->version_get(ph, &version);
119

120
	dev_dbg(ph->dev, "System Power Version %d.%d\n",
121 122
		PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));

123
	pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
124 125 126 127
	if (!pinfo)
		return -ENOMEM;

	pinfo->version = version;
128
	return ph->set_priv(ph, pinfo);
129 130
}

131 132
static const struct scmi_protocol scmi_system = {
	.id = SCMI_PROTOCOL_SYSTEM,
133
	.instance_init = &scmi_system_protocol_init,
134
	.ops = NULL,
135
	.events = &system_protocol_events,
136 137 138
};

DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)