Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * System Control and Management Interface (SCMI) System Power Protocol |
| 4 | * |
Cristian Marussi | 48dc16e | 2021-03-16 12:48:26 +0000 | [diff] [blame] | 5 | * Copyright (C) 2020-2021 ARM Ltd. |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt |
| 9 | |
Cristian Marussi | f5800e0 | 2021-03-16 12:49:02 +0000 | [diff] [blame] | 10 | #include <linux/module.h> |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 11 | #include <linux/scmi_protocol.h> |
| 12 | |
| 13 | #include "common.h" |
| 14 | #include "notify.h" |
| 15 | |
| 16 | #define SCMI_SYSTEM_NUM_SOURCES 1 |
| 17 | |
| 18 | enum scmi_system_protocol_cmd { |
| 19 | SYSTEM_POWER_STATE_NOTIFY = 0x5, |
| 20 | }; |
| 21 | |
| 22 | struct scmi_system_power_state_notify { |
| 23 | __le32 notify_enable; |
| 24 | }; |
| 25 | |
| 26 | struct scmi_system_power_state_notifier_payld { |
| 27 | __le32 agent_id; |
| 28 | __le32 flags; |
| 29 | __le32 system_state; |
| 30 | }; |
| 31 | |
| 32 | struct scmi_system_info { |
| 33 | u32 version; |
| 34 | }; |
| 35 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 36 | static int scmi_system_request_notify(const struct scmi_protocol_handle *ph, |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 37 | bool enable) |
| 38 | { |
| 39 | int ret; |
| 40 | struct scmi_xfer *t; |
| 41 | struct scmi_system_power_state_notify *notify; |
| 42 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 43 | ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY, |
| 44 | sizeof(*notify), 0, &t); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 45 | if (ret) |
| 46 | return ret; |
| 47 | |
| 48 | notify = t->tx.buf; |
| 49 | notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0; |
| 50 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 51 | ret = ph->xops->do_xfer(ph, t); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 52 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 53 | ph->xops->xfer_put(ph, t); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 54 | return ret; |
| 55 | } |
| 56 | |
Cristian Marussi | 3cb8c95 | 2021-03-16 12:48:59 +0000 | [diff] [blame] | 57 | static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph, |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 58 | u8 evt_id, u32 src_id, bool enable) |
| 59 | { |
| 60 | int ret; |
| 61 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 62 | ret = scmi_system_request_notify(ph, enable); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 63 | if (ret) |
| 64 | pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
Cristian Marussi | 3cb8c95 | 2021-03-16 12:48:59 +0000 | [diff] [blame] | 69 | static void * |
| 70 | scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph, |
| 71 | u8 evt_id, ktime_t timestamp, |
| 72 | const void *payld, size_t payld_sz, |
| 73 | void *report, u32 *src_id) |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 74 | { |
| 75 | const struct scmi_system_power_state_notifier_payld *p = payld; |
| 76 | struct scmi_system_power_state_notifier_report *r = report; |
| 77 | |
| 78 | if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER || |
| 79 | sizeof(*p) != payld_sz) |
| 80 | return NULL; |
| 81 | |
| 82 | r->timestamp = timestamp; |
| 83 | r->agent_id = le32_to_cpu(p->agent_id); |
| 84 | r->flags = le32_to_cpu(p->flags); |
| 85 | r->system_state = le32_to_cpu(p->system_state); |
| 86 | *src_id = 0; |
| 87 | |
| 88 | return r; |
| 89 | } |
| 90 | |
| 91 | static const struct scmi_event system_events[] = { |
| 92 | { |
| 93 | .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER, |
| 94 | .max_payld_sz = |
| 95 | sizeof(struct scmi_system_power_state_notifier_payld), |
| 96 | .max_report_sz = |
| 97 | sizeof(struct scmi_system_power_state_notifier_report), |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | static const struct scmi_event_ops system_event_ops = { |
| 102 | .set_notify_enabled = scmi_system_set_notify_enabled, |
| 103 | .fill_custom_report = scmi_system_fill_custom_report, |
| 104 | }; |
| 105 | |
Cristian Marussi | 533c709 | 2021-03-16 12:48:31 +0000 | [diff] [blame] | 106 | static const struct scmi_protocol_events system_protocol_events = { |
| 107 | .queue_sz = SCMI_PROTO_QUEUE_SZ, |
| 108 | .ops = &system_event_ops, |
| 109 | .evts = system_events, |
| 110 | .num_events = ARRAY_SIZE(system_events), |
| 111 | .num_sources = SCMI_SYSTEM_NUM_SOURCES, |
| 112 | }; |
| 113 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 114 | static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph) |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 115 | { |
| 116 | u32 version; |
| 117 | struct scmi_system_info *pinfo; |
| 118 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 119 | ph->xops->version_get(ph, &version); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 120 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 121 | dev_dbg(ph->dev, "System Power Version %d.%d\n", |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 122 | PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version)); |
| 123 | |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 124 | pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 125 | if (!pinfo) |
| 126 | return -ENOMEM; |
| 127 | |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 128 | pinfo->version = version; |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 129 | return ph->set_priv(ph, pinfo); |
Cristian Marussi | a880305 | 2020-09-07 18:46:55 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Cristian Marussi | 48dc16e | 2021-03-16 12:48:26 +0000 | [diff] [blame] | 132 | static const struct scmi_protocol scmi_system = { |
| 133 | .id = SCMI_PROTOCOL_SYSTEM, |
Cristian Marussi | f5800e0 | 2021-03-16 12:49:02 +0000 | [diff] [blame] | 134 | .owner = THIS_MODULE, |
Cristian Marussi | b46d852 | 2021-03-16 12:48:52 +0000 | [diff] [blame] | 135 | .instance_init = &scmi_system_protocol_init, |
Cristian Marussi | 48dc16e | 2021-03-16 12:48:26 +0000 | [diff] [blame] | 136 | .ops = NULL, |
Cristian Marussi | 533c709 | 2021-03-16 12:48:31 +0000 | [diff] [blame] | 137 | .events = &system_protocol_events, |
Cristian Marussi | 48dc16e | 2021-03-16 12:48:26 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system) |