aboutsummaryrefslogtreecommitdiff
path: root/drivers/mailbox/zynqmp-ipi.c
blob: eb86847bbe2354e0bfda3d4551e64066ec9c3f38 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// SPDX-License-Identifier: GPL-2.0+
/*
 * Xilinx Zynq MPSoC Mailbox driver
 *
 * Copyright (C) 2018-2019 Xilinx, Inc.
 */

#include <common.h>
#include <log.h>
#include <asm/io.h>
#include <asm/system.h>
#include <dm.h>
#include <mailbox-uclass.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <dm/of_access.h>
#include <linux/arm-smccc.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <wait_bit.h>
#include <zynqmp_firmware.h>

/* IPI bitmasks, register base */
/* TODO: move reg base to DT */
#define IPI_BIT_MASK_PMU0     0x10000
#define IPI_INT_REG_BASE_APU  0xFF300000

/* IPI agent ID any */
#define IPI_ID_ANY 0xFFUL

/* indicate if ZynqMP IPI mailbox driver uses SMC calls or HVC calls */
#define USE_SMC 0

/* Default IPI SMC function IDs */
#define SMC_IPI_MAILBOX_OPEN		0x82001000U
#define SMC_IPI_MAILBOX_RELEASE		0x82001001U
#define SMC_IPI_MAILBOX_STATUS_ENQUIRY	0x82001002U
#define SMC_IPI_MAILBOX_NOTIFY		0x82001003U
#define SMC_IPI_MAILBOX_ACK		0x82001004U
#define SMC_IPI_MAILBOX_ENABLE_IRQ	0x82001005U
#define SMC_IPI_MAILBOX_DISABLE_IRQ	0x82001006U

/* IPI SMC Macros */

/*
 * Flag to indicate if notification interrupt
 * to be disabled.
 */
#define IPI_SMC_ENQUIRY_DIRQ_MASK	BIT(0)

/*
 * Flag to indicate if notification interrupt
 * to be enabled.
 */
#define IPI_SMC_ACK_EIRQ_MASK		BIT(0)

/* IPI mailbox status */
#define IPI_MB_STATUS_IDLE		0
#define IPI_MB_STATUS_SEND_PENDING	1
#define IPI_MB_STATUS_RECV_PENDING	2

#define IPI_MB_CHNL_TX	0 /* IPI mailbox TX channel */
#define IPI_MB_CHNL_RX	1 /* IPI mailbox RX channel */

struct ipi_int_regs {
	u32 trig; /* 0x0  */
	u32 obs;  /* 0x4  */
	u32 dummy0;
	u32 dummy1;
	u32 isr;  /* 0x10  */
	u32 imr;  /* 0x14  */
	u32 ier;  /* 0x18 */
	u32 idr;  /* 0x1C */
};

#define ipi_int_apu ((struct ipi_int_regs *)IPI_INT_REG_BASE_APU)

struct zynqmp_ipi {
	void __iomem *local_req_regs;
	void __iomem *local_res_regs;
	void __iomem *remote_req_regs;
	void __iomem *remote_res_regs;
	u32 remote_id;
	u32 local_id;
	bool el3_supported;
};

static int zynqmp_ipi_fw_call(struct zynqmp_ipi *ipi_mbox,
			      unsigned long a0, unsigned long a3)
{
	struct arm_smccc_res res = {0};
	unsigned long a1, a2;

	a1 = ipi_mbox->local_id;
	a2 = ipi_mbox->remote_id;
	arm_smccc_smc(a0, a1, a2, a3, 0, 0, 0, 0, &res);

	return (int)res.a0;
}

static int zynqmp_ipi_send(struct mbox_chan *chan, const void *data)
{
	const struct zynqmp_ipi_msg *msg = (struct zynqmp_ipi_msg *)data;
	struct zynqmp_ipi *zynqmp = dev_get_priv(chan->dev);
	u32 ret;
	u32 *mbx = (u32 *)zynqmp->local_req_regs;

	for (size_t i = 0; i < msg->len; i++)
		writel(msg->buf[i], &mbx[i]);

	/* Use SMC calls for Exception Level less than 3 where TF-A is available */
	if (!IS_ENABLED(CONFIG_SPL_BUILD) && current_el() < 3) {
		ret = zynqmp_ipi_fw_call(zynqmp, SMC_IPI_MAILBOX_NOTIFY, 0);

		debug("%s, send %ld bytes\n", __func__, msg->len);

		return ret;
	}

	/* Return if EL3 is not supported */
	if (!zynqmp->el3_supported) {
		dev_err(chan->dev, "mailbox in EL3 only supported for zynqmp");
		return -EOPNOTSUPP;
	}

	/* Write trigger interrupt */
	writel(IPI_BIT_MASK_PMU0, &ipi_int_apu->trig);

	/* Wait until observation bit is cleared */
	ret = wait_for_bit_le32(&ipi_int_apu->obs, IPI_BIT_MASK_PMU0, false,
				1000, false);

	debug("%s, send %ld bytes\n", __func__, msg->len);
	return ret;
};

static int zynqmp_ipi_recv(struct mbox_chan *chan, void *data)
{
	struct zynqmp_ipi_msg *msg = (struct zynqmp_ipi_msg *)data;
	struct zynqmp_ipi *zynqmp = dev_get_priv(chan->dev);
	u32 *mbx = (u32 *)zynqmp->local_res_regs;
	int ret = 0;

	/*
	 * PMU Firmware does not trigger IPI interrupt for API call responses so
	 * there is no need to check ISR flags for EL3.
	 */
	for (size_t i = 0; i < msg->len; i++)
		msg->buf[i] = readl(&mbx[i]);

	/* Ack to remote if EL is not 3 */
	if (!IS_ENABLED(CONFIG_SPL_BUILD) && current_el() < 3) {
		ret = zynqmp_ipi_fw_call(zynqmp, SMC_IPI_MAILBOX_ACK,
					 IPI_SMC_ACK_EIRQ_MASK);
	}

	debug("%s, recv %ld bytes\n", __func__, msg->len);
	return ret;
};

static int zynqmp_ipi_dest_probe(struct udevice *dev)
{
	struct zynqmp_ipi *zynqmp = dev_get_priv(dev);
	struct resource res;
	ofnode node;
	int ret;

	debug("%s(dev=%p)\n", __func__, dev);

	node = dev_ofnode(dev);

	if (IS_ENABLED(CONFIG_SPL_BUILD) || of_machine_is_compatible("xlnx,zynqmp"))
		zynqmp->el3_supported = true;

	ret = dev_read_u32(dev->parent, "xlnx,ipi-id", &zynqmp->local_id);
	if (ret) {
		dev_err(dev, "can't get local ipi id\n");
		return ret;
	}

	ret = ofnode_read_u32(node, "xlnx,ipi-id", &zynqmp->remote_id);
	if (ret) {
		dev_err(dev, "can't get remote ipi id\n");
		return ret;
	}

	if (ofnode_read_resource_byname(node, "local_request_region", &res)) {
		dev_err(dev, "No reg property for local_request_region\n");
		return -EINVAL;
	};
	zynqmp->local_req_regs = devm_ioremap(dev, res.start,
					      (res.start - res.end));
	if (!zynqmp->local_req_regs)
		return -EINVAL;

	if (ofnode_read_resource_byname(node, "local_response_region", &res)) {
		dev_err(dev, "No reg property for local_response_region\n");
		return -EINVAL;
	};
	zynqmp->local_res_regs = devm_ioremap(dev, res.start,
					      (res.start - res.end));
	if (!zynqmp->local_res_regs)
		return -EINVAL;

	if (ofnode_read_resource_byname(node, "remote_request_region", &res)) {
		dev_err(dev, "No reg property for remote_request_region\n");
		return -EINVAL;
	};
	zynqmp->remote_req_regs = devm_ioremap(dev, res.start,
					       (res.start - res.end));
	if (!zynqmp->remote_req_regs)
		return -EINVAL;

	if (ofnode_read_resource_byname(node, "remote_response_region", &res)) {
		dev_err(dev, "No reg property for remote_response_region\n");
		return -EINVAL;
	};
	zynqmp->remote_res_regs = devm_ioremap(dev, res.start,
					       (res.start - res.end));
	if (!zynqmp->remote_res_regs)
		return -EINVAL;

	return 0;
};

static int zynqmp_ipi_probe(struct udevice *dev)
{
	struct udevice *cdev;
	ofnode cnode;
	int ret;

	debug("%s(dev=%p)\n", __func__, dev);

	dev_for_each_subnode(cnode, dev) {
		ret = device_bind_driver_to_node(dev, "zynqmp_ipi_dest",
						 ofnode_get_name(cnode),
						 cnode, &cdev);
		if (ret)
			return ret;
	}

	return 0;
};

struct mbox_ops zynqmp_ipi_dest_mbox_ops = {
	.send = zynqmp_ipi_send,
	.recv = zynqmp_ipi_recv,
};

static const struct udevice_id zynqmp_ipi_dest_ids[] = {
	{ .compatible = "xlnx,zynqmp-ipi-dest-mailbox" },
	{ }
};

U_BOOT_DRIVER(zynqmp_ipi_dest) = {
	.name = "zynqmp_ipi_dest",
	.id = UCLASS_MAILBOX,
	.of_match = zynqmp_ipi_dest_ids,
	.probe = zynqmp_ipi_dest_probe,
	.priv_auto = sizeof(struct zynqmp_ipi),
	.ops = &zynqmp_ipi_dest_mbox_ops,
};

static const struct udevice_id zynqmp_ipi_ids[] = {
	{ .compatible = "xlnx,zynqmp-ipi-mailbox" },
	{ }
};

U_BOOT_DRIVER(zynqmp_ipi) = {
	.name = "zynqmp_ipi",
	.id = UCLASS_NOP,
	.of_match = zynqmp_ipi_ids,
	.probe = zynqmp_ipi_probe,
	.flags = DM_FLAG_PROBE_AFTER_BIND,
};