Files
Oza Pawandeep bd873ab56a lib: utils: mpxy: Fix RPMI service ID lookup
mpxy_find_rpmi_srvid() incorrectly used the current service ID to
control iteration through the service table. This could stop the
search prematurely or read beyond the table.

Bound the search using num_services so every registered service is
checked without accessing entries past the end of the array.

Fixes: c5a8b15e39 ("lib: utils/mpxy: Add RPMI client driver for MPXY")
Reviewed-by: Rahul Pathak <rahul.pathak@oss.qualcomm.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Oza Pawandeep <pawandeep.oza@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20261118215549.2499040-7-pawandeep.oza@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
2026-11-19 18:22:27 +05:30

540 lines
14 KiB
C

/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Ventana Micro Systems Inc.
*
* Authors:
* Rahul Pathak <rpathak@ventanamicro.com>
* Anup Patel <apatel@ventanamicro.com>
*/
#include <libfdt.h>
#include <sbi/sbi_domain.h>
#include <sbi/sbi_error.h>
#include <sbi/sbi_heap.h>
#include <sbi/sbi_mpxy.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/mailbox/fdt_mailbox.h>
#include <sbi_utils/mpxy/fdt_mpxy.h>
#include <sbi_utils/mpxy/fdt_mpxy_rpmi.h>
/** Convert the mpxy attribute ID to attribute array index */
#define attr_id2index(attr_id) (attr_id - SBI_MPXY_ATTR_MSGPROTO_ATTR_START)
enum mpxy_msgprot_rpmi_attr_id {
MPXY_MSGPROT_RPMI_ATTR_SERVICEGROUP_ID = SBI_MPXY_ATTR_MSGPROTO_ATTR_START,
MPXY_MSGPROT_RPMI_ATTR_SERVICEGROUP_VERSION,
MPXY_MSGPROT_RPMI_ATTR_IMPL_ID,
MPXY_MSGPROT_RPMI_ATTR_IMPL_VERSION,
MPXY_MSGPROT_RPMI_ATTR_MAX_ID
};
/**
* MPXY message protocol attributes for RPMI
* Order of attribute fields must follow the
* attribute IDs in `enum mpxy_msgprot_rpmi_attr_id`
*/
struct mpxy_rpmi_channel_attrs {
u32 servicegrp_id;
u32 servicegrp_ver;
u32 impl_id;
u32 impl_ver;
};
/** Make sure all attributes are packed for direct memcpy */
#define assert_field_offset(field, attr_offset) \
_Static_assert( \
((offsetof(struct mpxy_rpmi_channel_attrs, field)) / \
sizeof(u32)) == (attr_offset - SBI_MPXY_ATTR_MSGPROTO_ATTR_START),\
"field " #field \
" from struct mpxy_rpmi_channel_attrs invalid offset, expected " #attr_offset)
assert_field_offset(servicegrp_id, MPXY_MSGPROT_RPMI_ATTR_SERVICEGROUP_ID);
assert_field_offset(servicegrp_ver, MPXY_MSGPROT_RPMI_ATTR_SERVICEGROUP_VERSION);
assert_field_offset(impl_id, MPXY_MSGPROT_RPMI_ATTR_IMPL_ID);
assert_field_offset(impl_ver, MPXY_MSGPROT_RPMI_ATTR_IMPL_VERSION);
/**
* MPXY RPMI instance per MPXY channel. This ties
* an MPXY channel with an RPMI Service group.
*/
struct mpxy_rpmi {
struct mbox_chan *chan;
const struct mpxy_rpmi_data *data;
struct mpxy_rpmi_channel_attrs msgprot_attrs;
struct sbi_mpxy_channel channel;
void *group_context;
};
/**
* Discover the RPMI service data using message_id
* MPXY message_id == RPMI service_id
*/
static const struct mpxy_rpmi_service_data *mpxy_find_rpmi_srvid(u32 message_id,
const struct mpxy_rpmi_data *data)
{
const struct mpxy_rpmi_service_data *srv = data->service_data;
int mid = 0;
for (mid = 0; mid < data->num_services; mid++) {
if (srv[mid].id == (u8)message_id)
return &srv[mid];
}
return NULL;
}
/** Copy attributes word size */
static void mpxy_copy_attrs(u32 *outmem, u32 *inmem, u32 count)
{
u32 idx;
for (idx = 0; idx < count; idx++)
outmem[idx] = cpu_to_le32(inmem[idx]);
}
static int mpxy_read_attributes(struct sbi_mpxy_channel *channel,
u32 *outmem, u32 base_attr_id, u32 attr_count)
{
struct mpxy_rpmi *rmb = container_of(channel, struct mpxy_rpmi, channel);
u32 *attr_array = (u32 *)&rmb->msgprot_attrs;
u32 end_id = base_attr_id + attr_count - 1;
if (end_id >= MPXY_MSGPROT_RPMI_ATTR_MAX_ID)
return SBI_EBAD_RANGE;
mpxy_copy_attrs(outmem, &attr_array[attr_id2index(base_attr_id)],
attr_count);
return SBI_OK;
}
/**
* Verify the channel standard attribute wrt to write permission
* and the value to be set if valid or not.
* Only attributes needs to be checked which are defined Read/Write
* permission. Other with Readonly permission will result in error.
*
* Attributes values to be written must also be checked because
* before writing a range of attributes, we need to make sure that
* either complete range of attributes is written successfully or not
* at all.
*/
static int mpxy_check_write_attr(u32 attr_id, u32 attr_val)
{
int ret = SBI_OK;
switch(attr_id) {
/** All RO access attributes falls under default */
default:
ret = SBI_EBAD_RANGE;
};
return ret;
}
static void mpxy_write_attr(struct mpxy_rpmi_channel_attrs *attrs,
u32 attr_id, u32 attr_val)
{
/* No writable attributes in RPMI */
}
static int mpxy_write_attributes(struct sbi_mpxy_channel *channel,
u32 *outmem, u32 base_attr_id,
u32 attr_count)
{
struct mpxy_rpmi *rmb = container_of(channel, struct mpxy_rpmi, channel);
u32 end_id = base_attr_id + attr_count - 1;
u32 attr_val, idx;
int ret, mem_idx;
if (end_id >= MPXY_MSGPROT_RPMI_ATTR_MAX_ID)
return SBI_EBAD_RANGE;
mem_idx = 0;
for (idx = base_attr_id; idx <= end_id; idx++) {
attr_val = le32_to_cpu(outmem[mem_idx++]);
ret = mpxy_check_write_attr(idx, attr_val);
if (ret)
return ret;
}
mem_idx = 0;
for (idx = base_attr_id; idx <= end_id; idx++) {
attr_val = le32_to_cpu(outmem[mem_idx++]);
mpxy_write_attr(&rmb->msgprot_attrs, idx, attr_val);
}
return SBI_OK;
}
static int __mpxy_send_message(struct sbi_mpxy_channel *channel,
u32 message_id, void *tx, u32 tx_len,
void *rx, u32 rx_max_len, unsigned long *ack_len)
{
struct mpxy_rpmi *rmb = container_of(channel, struct mpxy_rpmi, channel);
const struct mpxy_rpmi_data *data = rmb->data;
const struct mpxy_rpmi_service_data *srv =
mpxy_find_rpmi_srvid(message_id, data);
struct rpmi_message_args args = {0};
struct mbox_xfer xfer;
u32 rx_len = 0;
int ret;
if (!srv)
return SBI_ENOTSUPP;
/** message data size to be sent is in the
* supported service data size range */
if (tx_len < srv->min_tx_len || tx_len > srv->max_tx_len)
return SBI_EFAIL;
if (ack_len) {
/** MPXY buffer cannnot hold service min response data */
if (rx_max_len < srv->min_rx_len)
return SBI_EFAIL;
/* Max rx data size it can expect */
if (srv->max_rx_len < channel->attrs.msg_data_maxlen)
rx_len = srv->max_rx_len;
else
rx_len = channel->attrs.msg_data_maxlen;
args.type = RPMI_MSG_NORMAL_REQUEST;
args.flags = (rx) ? 0 : RPMI_MSG_FLAGS_NO_RX;
args.service_id = srv->id;
mbox_xfer_init_txrx(&xfer, &args,
tx, tx_len, RPMI_DEF_TX_TIMEOUT,
rx, rx_len, RPMI_DEF_RX_TIMEOUT);
}
else {
args.type = RPMI_MSG_POSTED_REQUEST;
args.flags = RPMI_MSG_FLAGS_NO_RX;
args.service_id = srv->id;
mbox_xfer_init_tx(&xfer, &args,
tx, tx_len, RPMI_DEF_TX_TIMEOUT);
}
if (data->xfer_group)
ret = data->xfer_group(rmb->group_context, rmb->chan, &xfer);
else
ret = mbox_chan_xfer(rmb->chan, &xfer);
if (ret)
return ret;
if (ack_len)
*ack_len = args.rx_data_len;
return SBI_OK;
}
static int mpxy_send_message_withresp(struct sbi_mpxy_channel *channel,
u32 message_id, void *tx, u32 tx_len,
void *rx, u32 rx_max_len,
unsigned long *ack_len)
{
return __mpxy_send_message(channel, message_id, tx, tx_len,
rx, rx_max_len, ack_len);
}
static int mpxy_send_message_withoutresp(struct sbi_mpxy_channel *channel,
u32 message_id, void *tx, u32 tx_len)
{
return __mpxy_send_message(channel, message_id, tx, tx_len,
NULL, 0, NULL);
}
static int mpxy_rpmi_get_attribute(struct mpxy_rpmi *rmb,
enum rpmi_channel_attribute_id attr_id,
u32 *out_value)
{
const struct mpxy_rpmi_data *data = rmb->data;
struct mbox_chan *chan = rmb->chan;
int rc;
if (data->get_attribute_group)
rc = data->get_attribute_group(rmb->group_context, chan, attr_id, out_value);
else
rc = mbox_chan_get_attribute(chan, attr_id, out_value);
return rc;
}
static int __mpxy_rpmi_init(const struct mpxy_rpmi_data *data,
struct mbox_chan *chan, u32 mpxy_channel_id,
struct sbi_domain *dom)
{
u32 servicegrp_ver, pro_ver, max_data_len, tx_tout, rx_tout;
struct mpxy_rpmi *rmb;
u32 impl_id, impl_ver;
int rc;
if (!data || !dom)
return SBI_EINVAL;
/*
* When mailbox channel is not available, both get_attribute_group() and
* xfer_group() callbacks must be available.
*/
if (!chan && (!data->get_attribute_group || !data->xfer_group))
return SBI_EINVAL;
/* Allocate context for MPXY RPMI instance */
rmb = sbi_zalloc(sizeof(*rmb));
if (!rmb)
return SBI_ENOMEM;
rmb->data = data;
rmb->chan = chan;
/* Setup RPMI service group context */
if (data->setup_group) {
rc = data->setup_group(&rmb->group_context, chan, dom, data);
if (rc)
goto fail_free_client;
}
/* Get channel protocol version */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_PROTOCOL_VERSION,
&pro_ver);
if (rc)
goto fail_cleanup_group;
/* Get channel maximum data length */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_MAX_DATA_LEN,
&max_data_len);
if (rc)
goto fail_cleanup_group;
/* Get channel Tx timeout */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_TX_TIMEOUT,
&tx_tout);
if (rc)
goto fail_cleanup_group;
/* Get channel Rx timeout */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_RX_TIMEOUT,
&rx_tout);
if (rc)
goto fail_cleanup_group;
/* Get channel service group version */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_SERVICEGROUP_VERSION,
&servicegrp_ver);
if (rc)
goto fail_cleanup_group;
/* Get channel implementation id */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_IMPL_ID,
&impl_id);
if (rc)
goto fail_cleanup_group;
/* Get channel implementation version */
rc = mpxy_rpmi_get_attribute(rmb, RPMI_CHANNEL_ATTR_IMPL_VERSION,
&impl_ver);
if (rc)
goto fail_cleanup_group;
/* Setup MPXY RPMI channel */
/* Channel ID*/
rmb->channel.channel_id = mpxy_channel_id;
/* Set the owner domain */
rmb->channel.owner_domain = dom;
/* Callback for read RPMI attributes */
rmb->channel.read_attributes = mpxy_read_attributes;
/* Callback for write RPMI attributes */
rmb->channel.write_attributes = mpxy_write_attributes;
/* Callback for sending RPMI message */
rmb->channel.send_message_with_response = mpxy_send_message_withresp;
rmb->channel.send_message_without_response = mpxy_send_message_withoutresp;
/* RPMI Message Protocol ID */
rmb->channel.attrs.msg_proto_id = SBI_MPXY_MSGPROTO_RPMI_ID;
/* RPMI Message Protocol Version */
rmb->channel.attrs.msg_proto_version = pro_ver;
/*
* RPMI supported max message data length(bytes), same for
* all service groups
*/
rmb->channel.attrs.msg_data_maxlen = max_data_len;
/*
* RPMI message send timeout(milliseconds)
* same for all service groups
*/
rmb->channel.attrs.msg_send_timeout = tx_tout;
rmb->channel.attrs.msg_completion_timeout = tx_tout + rx_tout;
/* RPMI service group attributes */
rmb->msgprot_attrs.servicegrp_id = data->servicegrp_id;
rmb->msgprot_attrs.servicegrp_ver = servicegrp_ver;
rmb->msgprot_attrs.impl_id = impl_id;
rmb->msgprot_attrs.impl_ver = impl_ver;
/* Register RPMI service group */
rc = sbi_mpxy_register_channel(&rmb->channel);
if (rc)
goto fail_cleanup_group;
return SBI_OK;
fail_cleanup_group:
if (data->cleanup_group)
data->cleanup_group(rmb->group_context);
fail_free_client:
sbi_free(rmb);
return rc;
}
struct __mpxy_rpmi_defer_init_priv {
const struct mpxy_rpmi_data *data;
struct mbox_chan *chan;
u32 mpxy_channel_id;
const char *dom_name;
};
static int __mpxy_rpmi_defer_notif(struct sbi_domain *dom,
enum sbi_domain_notify_event event,
void *priv)
{
struct __mpxy_rpmi_defer_init_priv *ipriv = priv;
int rc = 0;
/* Do nothing if domain name does not match */
if (sbi_strcmp(dom->name, ipriv->dom_name))
return 0;
/* Unregister notifier */
rc = sbi_domain_unregister_notifier(__mpxy_rpmi_defer_notif,
SBI_DOMAIN_NOTIFY_EVENT_REGISTER, ipriv);
if (rc)
goto done;
/* Create MPXY RPMI instance */
rc = __mpxy_rpmi_init(ipriv->data, ipriv->chan, ipriv->mpxy_channel_id, dom);
done:
sbi_free(ipriv);
return rc;
}
static int __mpxy_rpmi_defer_init(const struct mpxy_rpmi_data *data,
struct mbox_chan *chan, u32 mpxy_channel_id,
const char *dom_name)
{
struct __mpxy_rpmi_defer_init_priv *ipriv;
int rc;
if (!data || !dom_name)
return SBI_EINVAL;
/* Allocate defer init private data */
ipriv = sbi_zalloc(sizeof(*ipriv));
if (!ipriv)
return SBI_ENOMEM;
ipriv->data = data;
ipriv->chan = chan;
ipriv->mpxy_channel_id = mpxy_channel_id;
ipriv->dom_name = dom_name;
/* Register notifier */
rc = sbi_domain_register_notifier(__mpxy_rpmi_defer_notif,
SBI_DOMAIN_NOTIFY_EVENT_REGISTER, ipriv);
if (rc) {
sbi_free(ipriv);
return rc;
}
return 0;
}
int mpxy_rpmi_no_mbox_init(const struct mpxy_rpmi_data *data, u32 mpxy_channel_id,
const char *dom_name)
{
struct sbi_domain *dom;
/* Direct/Deferred creation of MPXY RPMI instance with no mailbox channel */
dom = sbi_domain_find_by_name(dom_name);
if (dom)
return __mpxy_rpmi_init(data, NULL, mpxy_channel_id, dom);
else
return __mpxy_rpmi_defer_init(data, NULL, mpxy_channel_id, dom_name);
}
int fdt_mpxy_rpmi_init(const void *fdt, int nodeoff, const struct fdt_match *match)
{
const struct mpxy_rpmi_data *data = match->data;
struct mbox_chan *chan = NULL;
struct sbi_domain *dom;
const char *dom_name;
const fdt32_t *val;
int rc, noff, len;
u32 channel_id;
/* Request mailbox channel when available */
val = fdt_getprop(fdt, nodeoff, "mboxes", &len);
if (val) {
/*
* If channel request failed then other end does not support
* service group so do nothing.
*/
rc = fdt_mailbox_request_chan(fdt, nodeoff, 0, &chan);
if (rc)
return SBI_ENODEV;
/* Match channel service group id */
if (data->servicegrp_id != chan->chan_args[0]) {
rc = SBI_EINVAL;
goto fail_free_chan;
}
}
/*
* The "riscv,sbi-mpxy-channel-id" DT property is mandatory
* for MPXY RPMI drivers so if this is not present then try
* other drivers.
*/
val = fdt_getprop(fdt, nodeoff, "riscv,sbi-mpxy-channel-id", &len);
if (len > 0 && val) {
channel_id = fdt32_to_cpu(*val);
} else {
rc = SBI_ENODEV;
goto fail_free_chan;
}
/*
* The "riscv,domain" DT property is optional for MPXY RPMI
* for MPXY RPMI drivers so if this is not present then assume
* root domain.
*/
val = fdt_getprop(fdt, nodeoff, "riscv,domain", &len);
if (len > 0 && val) {
noff = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*val));
if (noff < 0) {
rc = noff;
goto fail_free_chan;
}
dom_name = fdt_get_name(fdt, noff, NULL);
} else {
dom_name = "root";
}
/*
* Direct/Deferred creation of MPXY RPMI instance with optional
* mailbox channel discovered from DT
*/
dom = sbi_domain_find_by_name(dom_name);
if (dom)
rc = __mpxy_rpmi_init(data, chan, channel_id, dom);
else
rc = __mpxy_rpmi_defer_init(data, chan, channel_id, dom_name);
if (rc)
goto fail_free_chan;
return SBI_OK;
fail_free_chan:
if (chan)
mbox_controller_free_chan(chan);
return rc;
}