Path: blob/master/drivers/firmware/arm_scmi/protocols.h
26444 views
/* SPDX-License-Identifier: GPL-2.0 */1/*2* System Control and Management Interface (SCMI) Message Protocol3* protocols common header file containing some definitions, structures4* and function prototypes used in all the different SCMI protocols.5*6* Copyright (C) 2022 ARM Ltd.7*/8#ifndef _SCMI_PROTOCOLS_H9#define _SCMI_PROTOCOLS_H1011#include <linux/bitfield.h>12#include <linux/completion.h>13#include <linux/device.h>14#include <linux/errno.h>15#include <linux/kernel.h>16#include <linux/hashtable.h>17#include <linux/list.h>18#include <linux/module.h>19#include <linux/refcount.h>20#include <linux/scmi_protocol.h>21#include <linux/spinlock.h>22#include <linux/types.h>2324#include <linux/unaligned.h>2526#define PROTOCOL_REV_MINOR_MASK GENMASK(15, 0)27#define PROTOCOL_REV_MAJOR_MASK GENMASK(31, 16)28#define PROTOCOL_REV_MAJOR(x) ((u16)(FIELD_GET(PROTOCOL_REV_MAJOR_MASK, (x))))29#define PROTOCOL_REV_MINOR(x) ((u16)(FIELD_GET(PROTOCOL_REV_MINOR_MASK, (x))))3031#define SCMI_PROTOCOL_VENDOR_BASE 0x803233#define MSG_SUPPORTS_FASTCHANNEL(x) ((x) & BIT(0))3435enum scmi_common_cmd {36PROTOCOL_VERSION = 0x0,37PROTOCOL_ATTRIBUTES = 0x1,38PROTOCOL_MESSAGE_ATTRIBUTES = 0x2,39NEGOTIATE_PROTOCOL_VERSION = 0x10,40};4142/**43* struct scmi_msg_resp_prot_version - Response for a message44*45* @minor_version: Minor version of the ABI that firmware supports46* @major_version: Major version of the ABI that firmware supports47*48* In general, ABI version changes follow the rule that minor version increments49* are backward compatible. Major revision changes in ABI may not be50* backward compatible.51*52* Response to a generic message with message type SCMI_MSG_VERSION53*/54struct scmi_msg_resp_prot_version {55__le16 minor_version;56__le16 major_version;57};5859/**60* struct scmi_msg - Message(Tx/Rx) structure61*62* @buf: Buffer pointer63* @len: Length of data in the Buffer64*/65struct scmi_msg {66void *buf;67size_t len;68};6970/**71* struct scmi_msg_hdr - Message(Tx/Rx) header72*73* @id: The identifier of the message being sent74* @protocol_id: The identifier of the protocol used to send @id message75* @type: The SCMI type for this message76* @seq: The token to identify the message. When a message returns, the77* platform returns the whole message header unmodified including the78* token79* @status: Status of the transfer once it's complete80* @poll_completion: Indicate if the transfer needs to be polled for81* completion or interrupt mode is used82*/83struct scmi_msg_hdr {84u8 id;85u8 protocol_id;86u8 type;87u16 seq;88u32 status;89bool poll_completion;90};9192/**93* struct scmi_xfer - Structure representing a message flow94*95* @transfer_id: Unique ID for debug & profiling purpose96* @hdr: Transmit message header97* @tx: Transmit message98* @rx: Receive message, the buffer should be pre-allocated to store99* message. If request-ACK protocol is used, we can reuse the same100* buffer for the rx path as we use for the tx path.101* @done: command message transmit completion event102* @async_done: pointer to delayed response message received event completion103* @pending: True for xfers added to @pending_xfers hashtable104* @node: An hlist_node reference used to store this xfer, alternatively, on105* the free list @free_xfers or in the @pending_xfers hashtable106* @users: A refcount to track the active users for this xfer.107* This is meant to protect against the possibility that, when a command108* transaction times out concurrently with the reception of a valid109* response message, the xfer could be finally put on the TX path, and110* so vanish, while on the RX path scmi_rx_callback() is still111* processing it: in such a case this refcounting will ensure that, even112* though the timed-out transaction will anyway cause the command113* request to be reported as failed by time-out, the underlying xfer114* cannot be discarded and possibly reused until the last one user on115* the RX path has released it.116* @busy: An atomic flag to ensure exclusive write access to this xfer117* @state: The current state of this transfer, with states transitions deemed118* valid being:119* - SCMI_XFER_SENT_OK -> SCMI_XFER_RESP_OK [ -> SCMI_XFER_DRESP_OK ]120* - SCMI_XFER_SENT_OK -> SCMI_XFER_DRESP_OK121* (Missing synchronous response is assumed OK and ignored)122* @flags: Optional flags associated to this xfer.123* @lock: A spinlock to protect state and busy fields.124* @priv: A pointer for transport private usage.125*/126struct scmi_xfer {127int transfer_id;128struct scmi_msg_hdr hdr;129struct scmi_msg tx;130struct scmi_msg rx;131struct completion done;132struct completion *async_done;133bool pending;134struct hlist_node node;135refcount_t users;136#define SCMI_XFER_FREE 0137#define SCMI_XFER_BUSY 1138atomic_t busy;139#define SCMI_XFER_SENT_OK 0140#define SCMI_XFER_RESP_OK 1141#define SCMI_XFER_DRESP_OK 2142int state;143#define SCMI_XFER_FLAG_IS_RAW BIT(0)144#define SCMI_XFER_IS_RAW(x) ((x)->flags & SCMI_XFER_FLAG_IS_RAW)145#define SCMI_XFER_FLAG_CHAN_SET BIT(1)146#define SCMI_XFER_IS_CHAN_SET(x) \147((x)->flags & SCMI_XFER_FLAG_CHAN_SET)148int flags;149/* A lock to protect state and busy fields */150spinlock_t lock;151void *priv;152};153154struct scmi_xfer_ops;155struct scmi_proto_helpers_ops;156157/**158* struct scmi_protocol_handle - Reference to an initialized protocol instance159*160* @dev: A reference to the associated SCMI instance device (handle->dev).161* @xops: A reference to a struct holding refs to the core xfer operations that162* can be used by the protocol implementation to generate SCMI messages.163* @set_priv: A method to set protocol private data for this instance.164* @get_priv: A method to get protocol private data previously set.165*166* This structure represents a protocol initialized against specific SCMI167* instance and it will be used as follows:168* - as a parameter fed from the core to the protocol initialization code so169* that it can access the core xfer operations to build and generate SCMI170* messages exclusively for the specific underlying protocol instance.171* - as an opaque handle fed by an SCMI driver user when it tries to access172* this protocol through its own protocol operations.173* In this case this handle will be returned as an opaque object together174* with the related protocol operations when the SCMI driver tries to access175* the protocol.176*/177struct scmi_protocol_handle {178struct device *dev;179const struct scmi_xfer_ops *xops;180const struct scmi_proto_helpers_ops *hops;181int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv,182u32 version);183void *(*get_priv)(const struct scmi_protocol_handle *ph);184};185186/**187* struct scmi_iterator_state - Iterator current state descriptor188* @desc_index: Starting index for the current mulit-part request.189* @num_returned: Number of returned items in the last multi-part reply.190* @num_remaining: Number of remaining items in the multi-part message.191* @max_resources: Maximum acceptable number of items, configured by the caller192* depending on the underlying resources that it is querying.193* @loop_idx: The iterator loop index in the current multi-part reply.194* @rx_len: Size in bytes of the currenly processed message; it can be used by195* the user of the iterator to verify a reply size.196* @priv: Optional pointer to some additional state-related private data setup197* by the caller during the iterations.198*/199struct scmi_iterator_state {200unsigned int desc_index;201unsigned int num_returned;202unsigned int num_remaining;203unsigned int max_resources;204unsigned int loop_idx;205size_t rx_len;206void *priv;207};208209/**210* struct scmi_iterator_ops - Custom iterator operations211* @prepare_message: An operation to provide the custom logic to fill in the212* SCMI command request pointed by @message. @desc_index is213* a reference to the next index to use in the multi-part214* request.215* @update_state: An operation to provide the custom logic to update the216* iterator state from the actual message response.217* @process_response: An operation to provide the custom logic needed to process218* each chunk of the multi-part message.219*/220struct scmi_iterator_ops {221void (*prepare_message)(void *message, unsigned int desc_index,222const void *priv);223int (*update_state)(struct scmi_iterator_state *st,224const void *response, void *priv);225int (*process_response)(const struct scmi_protocol_handle *ph,226const void *response,227struct scmi_iterator_state *st, void *priv);228};229230struct scmi_fc_db_info {231int width;232u64 set;233u64 mask;234void __iomem *addr;235};236237struct scmi_fc_info {238void __iomem *set_addr;239void __iomem *get_addr;240struct scmi_fc_db_info *set_db;241u32 rate_limit;242};243244/**245* struct scmi_proto_helpers_ops - References to common protocol helpers246* @extended_name_get: A common helper function to retrieve extended naming247* for the specified resource using the specified command.248* Result is returned as a NULL terminated string in the249* pre-allocated area pointed to by @name with maximum250* capacity of @len bytes.251* @iter_response_init: A common helper to initialize a generic iterator to252* parse multi-message responses: when run the iterator253* will take care to send the initial command request as254* specified by @msg_id and @tx_size and then to parse the255* multi-part responses using the custom operations256* provided in @ops.257* @iter_response_run: A common helper to trigger the run of a previously258* initialized iterator.259* @protocol_msg_check: A common helper to check is a specific protocol message260* is supported.261* @fastchannel_init: A common helper used to initialize FC descriptors by262* gathering FC descriptions from the SCMI platform server.263* @fastchannel_db_ring: A common helper to ring a FC doorbell.264* @get_max_msg_size: A common helper to get the maximum message size.265*/266struct scmi_proto_helpers_ops {267int (*extended_name_get)(const struct scmi_protocol_handle *ph,268u8 cmd_id, u32 res_id, u32 *flags, char *name,269size_t len);270void *(*iter_response_init)(const struct scmi_protocol_handle *ph,271struct scmi_iterator_ops *ops,272unsigned int max_resources, u8 msg_id,273size_t tx_size, void *priv);274int (*iter_response_run)(void *iter);275int (*protocol_msg_check)(const struct scmi_protocol_handle *ph,276u32 message_id, u32 *attributes);277void (*fastchannel_init)(const struct scmi_protocol_handle *ph,278u8 describe_id, u32 message_id,279u32 valid_size, u32 domain,280void __iomem **p_addr,281struct scmi_fc_db_info **p_db,282u32 *rate_limit);283void (*fastchannel_db_ring)(struct scmi_fc_db_info *db);284int (*get_max_msg_size)(const struct scmi_protocol_handle *ph);285};286287/**288* struct scmi_xfer_ops - References to the core SCMI xfer operations.289* @version_get: Get this version protocol.290* @xfer_get_init: Initialize one struct xfer if any xfer slot is free.291* @reset_rx_to_maxsz: Reset rx size to max transport size.292* @do_xfer: Do the SCMI transfer.293* @do_xfer_with_response: Do the SCMI transfer waiting for a response.294* @xfer_put: Free the xfer slot.295*296* Note that all this operations expect a protocol handle as first parameter;297* they then internally use it to infer the underlying protocol number: this298* way is not possible for a protocol implementation to forge messages for299* another protocol.300*/301struct scmi_xfer_ops {302int (*version_get)(const struct scmi_protocol_handle *ph, u32 *version);303int (*xfer_get_init)(const struct scmi_protocol_handle *ph, u8 msg_id,304size_t tx_size, size_t rx_size,305struct scmi_xfer **p);306void (*reset_rx_to_maxsz)(const struct scmi_protocol_handle *ph,307struct scmi_xfer *xfer);308int (*do_xfer)(const struct scmi_protocol_handle *ph,309struct scmi_xfer *xfer);310int (*do_xfer_with_response)(const struct scmi_protocol_handle *ph,311struct scmi_xfer *xfer);312void (*xfer_put)(const struct scmi_protocol_handle *ph,313struct scmi_xfer *xfer);314};315316typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *);317318/**319* struct scmi_protocol - Protocol descriptor320* @id: Protocol ID.321* @owner: Module reference if any.322* @instance_init: Mandatory protocol initialization function.323* @instance_deinit: Optional protocol de-initialization function.324* @ops: Optional reference to the operations provided by the protocol and325* exposed in scmi_protocol.h.326* @events: An optional reference to the events supported by this protocol.327* @supported_version: The highest version currently supported for this328* protocol by the agent. Each protocol implementation329* in the agent is supposed to downgrade to match the330* protocol version supported by the platform.331* @vendor_id: A firmware vendor string for vendor protocols matching.332* Ignored when @id identifies a standard protocol, cannot be NULL333* otherwise.334* @sub_vendor_id: A firmware sub_vendor string for vendor protocols matching.335* Ignored if NULL or when @id identifies a standard protocol.336* @impl_ver: A firmware implementation version for vendor protocols matching.337* Ignored if zero or if @id identifies a standard protocol.338*339* Note that vendor protocols matching at load time is performed by attempting340* the closest match first against the tuple (vendor, sub_vendor, impl_ver)341*/342struct scmi_protocol {343const u8 id;344struct module *owner;345const scmi_prot_init_ph_fn_t instance_init;346const scmi_prot_init_ph_fn_t instance_deinit;347const void *ops;348const struct scmi_protocol_events *events;349unsigned int supported_version;350char *vendor_id;351char *sub_vendor_id;352u32 impl_ver;353};354355#define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(name, proto) \356static const struct scmi_protocol *__this_proto = &(proto); \357\358int __init scmi_##name##_register(void) \359{ \360return scmi_protocol_register(__this_proto); \361} \362\363void __exit scmi_##name##_unregister(void) \364{ \365scmi_protocol_unregister(__this_proto); \366}367368#define DECLARE_SCMI_REGISTER_UNREGISTER(func) \369int __init scmi_##func##_register(void); \370void __exit scmi_##func##_unregister(void)371DECLARE_SCMI_REGISTER_UNREGISTER(base);372DECLARE_SCMI_REGISTER_UNREGISTER(clock);373DECLARE_SCMI_REGISTER_UNREGISTER(perf);374DECLARE_SCMI_REGISTER_UNREGISTER(pinctrl);375DECLARE_SCMI_REGISTER_UNREGISTER(power);376DECLARE_SCMI_REGISTER_UNREGISTER(reset);377DECLARE_SCMI_REGISTER_UNREGISTER(sensors);378DECLARE_SCMI_REGISTER_UNREGISTER(voltage);379DECLARE_SCMI_REGISTER_UNREGISTER(system);380DECLARE_SCMI_REGISTER_UNREGISTER(powercap);381382#endif /* _SCMI_PROTOCOLS_H */383384385