aboutsummaryrefslogtreecommitdiff
path: root/drivers/firmware/scmi/mailbox_agent.c
blob: e63b67c5ee8ccc1751c4819468483dac067e8699 (plain) (blame)
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2020 Linaro Limited.
 */

#define LOG_CATEGORY UCLASS_SCMI_AGENT

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <mailbox.h>
#include <scmi_agent.h>
#include <scmi_agent-uclass.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <linux/compat.h>

#include "smt.h"

#define TIMEOUT_US_10MS			10000

/**
 * struct scmi_mbox_channel - Description of an SCMI mailbox transport
 * @smt:	Shared memory buffer
 * @mbox:	Mailbox channel description
 * @timeout_us:	Timeout in microseconds for the mailbox transfer
 */
struct scmi_mbox_channel {
	struct scmi_smt smt;
	struct mbox_chan mbox;
	ulong timeout_us;
};

/**
 * struct scmi_channel - Channel instance referenced in SCMI drivers
 * @ref: Reference to local channel instance
 **/
struct scmi_channel {
	struct scmi_mbox_channel ref;
};

static int scmi_mbox_process_msg(struct udevice *dev,
				 struct scmi_channel *channel,
				 struct scmi_msg *msg)
{
	struct scmi_mbox_channel *chan = dev_get_plat(dev);
	int ret;

	/* Support SCMI drivers upgraded to of_get_channel operator */
	if (channel)
		chan = &channel->ref;

	ret = scmi_write_msg_to_smt(dev, &chan->smt, msg);
	if (ret)
		return ret;

	/* Give shm addr to mbox in case it is meaningful */
	ret = mbox_send(&chan->mbox, chan->smt.buf);
	if (ret) {
		dev_err(dev, "Message send failed: %d\n", ret);
		goto out;
	}

	/* Receive the response */
	ret = mbox_recv(&chan->mbox, chan->smt.buf, chan->timeout_us);
	if (ret) {
		dev_err(dev, "Response failed: %d, abort\n", ret);
		goto out;
	}

	ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);

out:
	scmi_clear_smt_channel(&chan->smt);

	return ret;
}

static int setup_channel(struct udevice *dev, struct scmi_mbox_channel *chan)
{
	int ret;

	ret = mbox_get_by_index(dev, 0, &chan->mbox);
	if (ret) {
		dev_err(dev, "Failed to find mailbox: %d\n", ret);
		return ret;
	}

	ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
	if (ret) {
		dev_err(dev, "Failed to get shm resources: %d\n", ret);
		return ret;
	}

	chan->timeout_us = TIMEOUT_US_10MS;

	return 0;
}

static int scmi_mbox_get_channel(struct udevice *dev,
				 struct scmi_channel **channel)
{
	struct scmi_mbox_channel *base_chan = dev_get_plat(dev->parent);
	struct scmi_mbox_channel *chan;
	int ret;

	if (!dev_read_prop(dev, "shmem", NULL)) {
		/* Uses agent base channel */
		*channel = container_of(base_chan, struct scmi_channel, ref);

		return 0;
	}

	chan = calloc(1, sizeof(*chan));
	if (!chan)
		return -ENOMEM;

	/* Setup a dedicated channel for the protocol */
	ret = setup_channel(dev, chan);
	if (ret) {
		free(chan);
		return ret;
	}

	*channel = (void *)chan;

	return 0;
}

int scmi_mbox_of_to_plat(struct udevice *dev)
{
	struct scmi_mbox_channel *chan = dev_get_plat(dev);

	return setup_channel(dev, chan);
}

static const struct udevice_id scmi_mbox_ids[] = {
	{ .compatible = "arm,scmi" },
	{ }
};

static const struct scmi_agent_ops scmi_mbox_ops = {
	.of_get_channel = scmi_mbox_get_channel,
	.process_msg = scmi_mbox_process_msg,
};

U_BOOT_DRIVER(scmi_mbox) = {
	.name		= "scmi-over-mailbox",
	.id		= UCLASS_SCMI_AGENT,
	.of_match	= scmi_mbox_ids,
	.plat_auto	= sizeof(struct scmi_mbox_channel),
	.of_to_plat	= scmi_mbox_of_to_plat,
	.ops		= &scmi_mbox_ops,
};