1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) System Power Protocol 4 * 5 * Copyright (C) 2020-2022 ARM Ltd. 6 */ 7 8 #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt 9 10 #include <linux/module.h> 11 #include <linux/scmi_protocol.h> 12 13 #include "protocols.h" 14 #include "notify.h" 15 16 /* Updated only after ALL the mandatory features for that version are merged */ 17 #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x20001 18 19 #define SCMI_SYSTEM_NUM_SOURCES 1 20 21 enum scmi_system_protocol_cmd { 22 SYSTEM_POWER_STATE_NOTIFY = 0x5, 23 }; 24 25 struct scmi_system_power_state_notify { 26 __le32 notify_enable; 27 }; 28 29 struct scmi_system_power_state_notifier_payld { 30 __le32 agent_id; 31 __le32 flags; 32 __le32 system_state; 33 __le32 timeout; 34 }; 35 36 struct scmi_system_info { 37 bool graceful_timeout_supported; 38 bool power_state_notify_cmd; 39 }; 40 41 static bool scmi_system_notify_supported(const struct scmi_protocol_handle *ph, 42 u8 evt_id, u32 src_id) 43 { 44 struct scmi_system_info *pinfo = ph->get_priv(ph); 45 46 if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER) 47 return false; 48 49 return pinfo->power_state_notify_cmd; 50 } 51 52 static int scmi_system_request_notify(const struct scmi_protocol_handle *ph, 53 bool enable) 54 { 55 int ret; 56 struct scmi_xfer *t; 57 struct scmi_system_power_state_notify *notify; 58 59 ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY, 60 sizeof(*notify), 0, &t); 61 if (ret) 62 return ret; 63 64 notify = t->tx.buf; 65 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0; 66 67 ret = ph->xops->do_xfer(ph, t); 68 69 ph->xops->xfer_put(ph, t); 70 return ret; 71 } 72 73 static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph, 74 u8 evt_id, u32 src_id, bool enable) 75 { 76 int ret; 77 78 ret = scmi_system_request_notify(ph, enable); 79 if (ret) 80 pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret); 81 82 return ret; 83 } 84 85 static void * 86 scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph, 87 u8 evt_id, ktime_t timestamp, 88 const void *payld, size_t payld_sz, 89 void *report, u32 *src_id) 90 { 91 size_t expected_sz; 92 const struct scmi_system_power_state_notifier_payld *p = payld; 93 struct scmi_system_power_state_notifier_report *r = report; 94 struct scmi_system_info *pinfo = ph->get_priv(ph); 95 96 expected_sz = pinfo->graceful_timeout_supported ? 97 sizeof(*p) : sizeof(*p) - sizeof(__le32); 98 if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER || 99 payld_sz != expected_sz) 100 return NULL; 101 102 r->timestamp = timestamp; 103 r->agent_id = le32_to_cpu(p->agent_id); 104 r->flags = le32_to_cpu(p->flags); 105 r->system_state = le32_to_cpu(p->system_state); 106 if (pinfo->graceful_timeout_supported && 107 r->system_state == SCMI_SYSTEM_SHUTDOWN && 108 SCMI_SYSPOWER_IS_REQUEST_GRACEFUL(r->flags)) 109 r->timeout = le32_to_cpu(p->timeout); 110 else 111 r->timeout = 0x00; 112 *src_id = 0; 113 114 return r; 115 } 116 117 static const struct scmi_event system_events[] = { 118 { 119 .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER, 120 .max_payld_sz = 121 sizeof(struct scmi_system_power_state_notifier_payld), 122 .max_report_sz = 123 sizeof(struct scmi_system_power_state_notifier_report), 124 }, 125 }; 126 127 static const struct scmi_event_ops system_event_ops = { 128 .is_notify_supported = scmi_system_notify_supported, 129 .set_notify_enabled = scmi_system_set_notify_enabled, 130 .fill_custom_report = scmi_system_fill_custom_report, 131 }; 132 133 static const struct scmi_protocol_events system_protocol_events = { 134 .queue_sz = SCMI_PROTO_QUEUE_SZ, 135 .ops = &system_event_ops, 136 .evts = system_events, 137 .num_events = ARRAY_SIZE(system_events), 138 .num_sources = SCMI_SYSTEM_NUM_SOURCES, 139 }; 140 141 static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph) 142 { 143 struct scmi_system_info *pinfo; 144 145 dev_dbg(ph->dev, "System Power Version %d.%d\n", 146 PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version)); 147 148 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL); 149 if (!pinfo) 150 return -ENOMEM; 151 152 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x2) 153 pinfo->graceful_timeout_supported = true; 154 155 if (!ph->hops->protocol_msg_check(ph, SYSTEM_POWER_STATE_NOTIFY, NULL)) 156 pinfo->power_state_notify_cmd = true; 157 158 return ph->set_priv(ph, pinfo); 159 } 160 161 static const struct scmi_protocol scmi_system = { 162 .id = SCMI_PROTOCOL_SYSTEM, 163 .owner = THIS_MODULE, 164 .instance_init = &scmi_system_protocol_init, 165 .ops = NULL, 166 .events = &system_protocol_events, 167 .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION, 168 }; 169 170 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system) 171