summaryrefslogtreecommitdiffstats
path: root/drivers/firmware/tegra/bpmp-tegra210.c
blob: c32754055c60bf44e89a5d8d8b4fe2931db47ceb (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2018, NVIDIA CORPORATION.
 */

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/platform_device.h>

#include <soc/tegra/bpmp.h>

#include "bpmp-private.h"

#define TRIGGER_OFFSET		0x000
#define RESULT_OFFSET(id)	(0xc00 + id * 4)
#define TRIGGER_ID_SHIFT	16
#define TRIGGER_CMD_GET		4

#define STA_OFFSET		0
#define SET_OFFSET		4
#define CLR_OFFSET		8

#define CH_MASK(ch)	(0x3 << ((ch) * 2))
#define SL_SIGL(ch)	(0x0 << ((ch) * 2))
#define SL_QUED(ch)	(0x1 << ((ch) * 2))
#define MA_FREE(ch)	(0x2 << ((ch) * 2))
#define MA_ACKD(ch)	(0x3 << ((ch) * 2))

struct tegra210_bpmp {
	void __iomem *atomics;
	void __iomem *arb_sema;
	struct irq_data *tx_irq_data;
};

static u32 bpmp_channel_status(struct tegra_bpmp *bpmp, unsigned int index)
{
	struct tegra210_bpmp *priv = bpmp->priv;

	return __raw_readl(priv->arb_sema + STA_OFFSET) & CH_MASK(index);
}

static bool tegra210_bpmp_is_response_ready(struct tegra_bpmp_channel *channel)
{
	unsigned int index = channel->index;

	return bpmp_channel_status(channel->bpmp, index) == MA_ACKD(index);
}

static bool tegra210_bpmp_is_request_ready(struct tegra_bpmp_channel *channel)
{
	unsigned int index = channel->index;

	return bpmp_channel_status(channel->bpmp, index) == SL_SIGL(index);
}

static bool
tegra210_bpmp_is_request_channel_free(struct tegra_bpmp_channel *channel)
{
	unsigned int index = channel->index;

	return bpmp_channel_status(channel->bpmp, index) == MA_FREE(index);
}

static bool
tegra210_bpmp_is_response_channel_free(struct tegra_bpmp_channel *channel)
{
	unsigned int index = channel->index;

	return bpmp_channel_status(channel->bpmp, index) == SL_QUED(index);
}

static int tegra210_bpmp_post_request(struct tegra_bpmp_channel *channel)
{
	struct tegra210_bpmp *priv = channel->bpmp->priv;

	__raw_writel(CH_MASK(channel->index), priv->arb_sema + CLR_OFFSET);

	return 0;
}

static int tegra210_bpmp_post_response(struct tegra_bpmp_channel *channel)
{
	struct tegra210_bpmp *priv = channel->bpmp->priv;

	__raw_writel(MA_ACKD(channel->index), priv->arb_sema + SET_OFFSET);

	return 0;
}

static int tegra210_bpmp_ack_response(struct tegra_bpmp_channel *channel)
{
	struct tegra210_bpmp *priv = channel->bpmp->priv;

	__raw_writel(MA_ACKD(channel->index) ^ MA_FREE(channel->index),
		     priv->arb_sema + CLR_OFFSET);

	return 0;
}

static int tegra210_bpmp_ack_request(struct tegra_bpmp_channel *channel)
{
	struct tegra210_bpmp *priv = channel->bpmp->priv;

	__raw_writel(SL_QUED(channel->index), priv->arb_sema + SET_OFFSET);

	return 0;
}

static int tegra210_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
{
	struct tegra210_bpmp *priv = bpmp->priv;
	struct irq_data *irq_data = priv->tx_irq_data;

	/*
	 * Tegra Legacy Interrupt Controller (LIC) is used to notify BPMP of
	 * available messages
	 */
	if (irq_data->chip->irq_retrigger)
		return irq_data->chip->irq_retrigger(irq_data);

	return -EINVAL;
}

static irqreturn_t rx_irq(int irq, void *data)
{
	struct tegra_bpmp *bpmp = data;

	tegra_bpmp_handle_rx(bpmp);

	return IRQ_HANDLED;
}

static int tegra210_bpmp_channel_init(struct tegra_bpmp_channel *channel,
				      struct tegra_bpmp *bpmp,
				      unsigned int index)
{
	struct tegra210_bpmp *priv = bpmp->priv;
	u32 address;
	void *p;

	/* Retrieve channel base address from BPMP */
	writel(index << TRIGGER_ID_SHIFT | TRIGGER_CMD_GET,
	       priv->atomics + TRIGGER_OFFSET);
	address = readl(priv->atomics + RESULT_OFFSET(index));

	p = devm_ioremap(bpmp->dev, address, 0x80);
	if (!p)
		return -ENOMEM;

	channel->ib = p;
	channel->ob = p;
	channel->index = index;
	init_completion(&channel->completion);
	channel->bpmp = bpmp;

	return 0;
}

static int tegra210_bpmp_init(struct tegra_bpmp *bpmp)
{
	struct platform_device *pdev = to_platform_device(bpmp->dev);
	struct tegra210_bpmp *priv;
	struct resource *res;
	unsigned int i;
	int err;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	bpmp->priv = priv;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	priv->atomics = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(priv->atomics))
		return PTR_ERR(priv->atomics);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	priv->arb_sema = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(priv->arb_sema))
		return PTR_ERR(priv->arb_sema);

	err = tegra210_bpmp_channel_init(bpmp->tx_channel, bpmp,
					 bpmp->soc->channels.cpu_tx.offset);
	if (err < 0)
		return err;

	err = tegra210_bpmp_channel_init(bpmp->rx_channel, bpmp,
					 bpmp->soc->channels.cpu_rx.offset);
	if (err < 0)
		return err;

	for (i = 0; i < bpmp->threaded.count; i++) {
		unsigned int index = bpmp->soc->channels.thread.offset + i;

		err = tegra210_bpmp_channel_init(&bpmp->threaded_channels[i],
						 bpmp, index);
		if (err < 0)
			return err;
	}

	err = platform_get_irq_byname(pdev, "tx");
	if (err < 0) {
		dev_err(&pdev->dev, "failed to get TX IRQ: %d\n", err);
		return err;
	}

	priv->tx_irq_data = irq_get_irq_data(err);
	if (!priv->tx_irq_data) {
		dev_err(&pdev->dev, "failed to get IRQ data for TX IRQ\n");
		return -ENOENT;
	}

	err = platform_get_irq_byname(pdev, "rx");
	if (err < 0) {
		dev_err(&pdev->dev, "failed to get rx IRQ: %d\n", err);
		return err;
	}

	err = devm_request_irq(&pdev->dev, err, rx_irq,
			       IRQF_NO_SUSPEND, dev_name(&pdev->dev), bpmp);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
		return err;
	}

	return 0;
}

const struct tegra_bpmp_ops tegra210_bpmp_ops = {
	.init = tegra210_bpmp_init,
	.is_response_ready = tegra210_bpmp_is_response_ready,
	.is_request_ready = tegra210_bpmp_is_request_ready,
	.ack_response = tegra210_bpmp_ack_response,
	.ack_request = tegra210_bpmp_ack_request,
	.is_response_channel_free = tegra210_bpmp_is_response_channel_free,
	.is_request_channel_free = tegra210_bpmp_is_request_channel_free,
	.post_response = tegra210_bpmp_post_response,
	.post_request = tegra210_bpmp_post_request,
	.ring_doorbell = tegra210_bpmp_ring_doorbell,
};