dma-fence: Add some more fence-merge-unwrap tests
[drm/drm-misc.git] / drivers / remoteproc / st_remoteproc.c
blob5df99bae7131a832c0c03c9bf8619812d9eb570d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ST's Remote Processor Control Driver
5 * Copyright (C) 2015 STMicroelectronics - All Rights Reserved
7 * Author: Ludovic Barre <ludovic.barre@st.com>
8 */
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mailbox_client.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_reserved_mem.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23 #include <linux/remoteproc.h>
24 #include <linux/reset.h>
26 #include "remoteproc_internal.h"
28 #define ST_RPROC_VQ0 0
29 #define ST_RPROC_VQ1 1
30 #define ST_RPROC_MAX_VRING 2
32 #define MBOX_RX 0
33 #define MBOX_TX 1
34 #define MBOX_MAX 2
36 struct st_rproc_config {
37 bool sw_reset;
38 bool pwr_reset;
39 unsigned long bootaddr_mask;
42 struct st_rproc {
43 struct st_rproc_config *config;
44 struct reset_control *sw_reset;
45 struct reset_control *pwr_reset;
46 struct clk *clk;
47 u32 clk_rate;
48 struct regmap *boot_base;
49 u32 boot_offset;
50 struct mbox_chan *mbox_chan[ST_RPROC_MAX_VRING * MBOX_MAX];
51 struct mbox_client mbox_client_vq0;
52 struct mbox_client mbox_client_vq1;
55 static void st_rproc_mbox_callback(struct device *dev, u32 msg)
57 struct rproc *rproc = dev_get_drvdata(dev);
59 if (rproc_vq_interrupt(rproc, msg) == IRQ_NONE)
60 dev_dbg(dev, "no message was found in vqid %d\n", msg);
63 static
64 void st_rproc_mbox_callback_vq0(struct mbox_client *mbox_client, void *data)
66 st_rproc_mbox_callback(mbox_client->dev, 0);
69 static
70 void st_rproc_mbox_callback_vq1(struct mbox_client *mbox_client, void *data)
72 st_rproc_mbox_callback(mbox_client->dev, 1);
75 static void st_rproc_kick(struct rproc *rproc, int vqid)
77 struct st_rproc *ddata = rproc->priv;
78 struct device *dev = rproc->dev.parent;
79 int ret;
81 /* send the index of the triggered virtqueue in the mailbox payload */
82 if (WARN_ON(vqid >= ST_RPROC_MAX_VRING))
83 return;
85 ret = mbox_send_message(ddata->mbox_chan[vqid * MBOX_MAX + MBOX_TX],
86 (void *)&vqid);
87 if (ret < 0)
88 dev_err(dev, "failed to send message via mbox: %d\n", ret);
91 static int st_rproc_mem_alloc(struct rproc *rproc,
92 struct rproc_mem_entry *mem)
94 struct device *dev = rproc->dev.parent;
95 void *va;
97 va = ioremap_wc(mem->dma, mem->len);
98 if (!va) {
99 dev_err(dev, "Unable to map memory region: %pa+%zx\n",
100 &mem->dma, mem->len);
101 return -ENOMEM;
104 /* Update memory entry va */
105 mem->va = va;
107 return 0;
110 static int st_rproc_mem_release(struct rproc *rproc,
111 struct rproc_mem_entry *mem)
113 iounmap(mem->va);
115 return 0;
118 static int st_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
120 struct device *dev = rproc->dev.parent;
121 struct device_node *np = dev->of_node;
122 struct rproc_mem_entry *mem;
123 struct reserved_mem *rmem;
124 struct of_phandle_iterator it;
125 int index = 0;
127 of_phandle_iterator_init(&it, np, "memory-region", NULL, 0);
128 while (of_phandle_iterator_next(&it) == 0) {
129 rmem = of_reserved_mem_lookup(it.node);
130 if (!rmem) {
131 of_node_put(it.node);
132 dev_err(dev, "unable to acquire memory-region\n");
133 return -EINVAL;
136 /* No need to map vdev buffer */
137 if (strcmp(it.node->name, "vdev0buffer")) {
138 /* Register memory region */
139 mem = rproc_mem_entry_init(dev, NULL,
140 (dma_addr_t)rmem->base,
141 rmem->size, rmem->base,
142 st_rproc_mem_alloc,
143 st_rproc_mem_release,
144 it.node->name);
145 } else {
146 /* Register reserved memory for vdev buffer allocation */
147 mem = rproc_of_resm_mem_entry_init(dev, index,
148 rmem->size,
149 rmem->base,
150 it.node->name);
153 if (!mem) {
154 of_node_put(it.node);
155 return -ENOMEM;
158 rproc_add_carveout(rproc, mem);
159 index++;
162 return rproc_elf_load_rsc_table(rproc, fw);
165 static int st_rproc_start(struct rproc *rproc)
167 struct st_rproc *ddata = rproc->priv;
168 int err;
170 regmap_update_bits(ddata->boot_base, ddata->boot_offset,
171 ddata->config->bootaddr_mask, rproc->bootaddr);
173 err = clk_enable(ddata->clk);
174 if (err) {
175 dev_err(&rproc->dev, "Failed to enable clock\n");
176 return err;
179 if (ddata->config->sw_reset) {
180 err = reset_control_deassert(ddata->sw_reset);
181 if (err) {
182 dev_err(&rproc->dev, "Failed to deassert S/W Reset\n");
183 goto sw_reset_fail;
187 if (ddata->config->pwr_reset) {
188 err = reset_control_deassert(ddata->pwr_reset);
189 if (err) {
190 dev_err(&rproc->dev, "Failed to deassert Power Reset\n");
191 goto pwr_reset_fail;
195 dev_info(&rproc->dev, "Started from 0x%llx\n", rproc->bootaddr);
197 return 0;
200 pwr_reset_fail:
201 if (ddata->config->pwr_reset)
202 reset_control_assert(ddata->sw_reset);
203 sw_reset_fail:
204 clk_disable(ddata->clk);
206 return err;
209 static int st_rproc_stop(struct rproc *rproc)
211 struct st_rproc *ddata = rproc->priv;
212 int sw_err = 0, pwr_err = 0;
214 if (ddata->config->sw_reset) {
215 sw_err = reset_control_assert(ddata->sw_reset);
216 if (sw_err)
217 dev_err(&rproc->dev, "Failed to assert S/W Reset\n");
220 if (ddata->config->pwr_reset) {
221 pwr_err = reset_control_assert(ddata->pwr_reset);
222 if (pwr_err)
223 dev_err(&rproc->dev, "Failed to assert Power Reset\n");
226 clk_disable(ddata->clk);
228 return sw_err ?: pwr_err;
231 static const struct rproc_ops st_rproc_ops = {
232 .kick = st_rproc_kick,
233 .start = st_rproc_start,
234 .stop = st_rproc_stop,
235 .parse_fw = st_rproc_parse_fw,
236 .load = rproc_elf_load_segments,
237 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
238 .sanity_check = rproc_elf_sanity_check,
239 .get_boot_addr = rproc_elf_get_boot_addr,
243 * Fetch state of the processor: 0 is off, 1 is on.
245 static int st_rproc_state(struct platform_device *pdev)
247 struct rproc *rproc = platform_get_drvdata(pdev);
248 struct st_rproc *ddata = rproc->priv;
249 int reset_sw = 0, reset_pwr = 0;
251 if (ddata->config->sw_reset)
252 reset_sw = reset_control_status(ddata->sw_reset);
254 if (ddata->config->pwr_reset)
255 reset_pwr = reset_control_status(ddata->pwr_reset);
257 if (reset_sw < 0 || reset_pwr < 0)
258 return -EINVAL;
260 return !reset_sw && !reset_pwr;
263 static const struct st_rproc_config st40_rproc_cfg = {
264 .sw_reset = true,
265 .pwr_reset = true,
266 .bootaddr_mask = GENMASK(28, 1),
269 static const struct st_rproc_config st231_rproc_cfg = {
270 .sw_reset = true,
271 .pwr_reset = false,
272 .bootaddr_mask = GENMASK(31, 6),
275 static const struct of_device_id st_rproc_match[] = {
276 { .compatible = "st,st40-rproc", .data = &st40_rproc_cfg },
277 { .compatible = "st,st231-rproc", .data = &st231_rproc_cfg },
280 MODULE_DEVICE_TABLE(of, st_rproc_match);
282 static int st_rproc_parse_dt(struct platform_device *pdev)
284 struct device *dev = &pdev->dev;
285 struct rproc *rproc = platform_get_drvdata(pdev);
286 struct st_rproc *ddata = rproc->priv;
287 struct device_node *np = dev->of_node;
288 int err;
290 if (ddata->config->sw_reset) {
291 ddata->sw_reset = devm_reset_control_get_exclusive(dev,
292 "sw_reset");
293 if (IS_ERR(ddata->sw_reset)) {
294 dev_err(dev, "Failed to get S/W Reset\n");
295 return PTR_ERR(ddata->sw_reset);
299 if (ddata->config->pwr_reset) {
300 ddata->pwr_reset = devm_reset_control_get_exclusive(dev,
301 "pwr_reset");
302 if (IS_ERR(ddata->pwr_reset)) {
303 dev_err(dev, "Failed to get Power Reset\n");
304 return PTR_ERR(ddata->pwr_reset);
308 ddata->clk = devm_clk_get(dev, NULL);
309 if (IS_ERR(ddata->clk)) {
310 dev_err(dev, "Failed to get clock\n");
311 return PTR_ERR(ddata->clk);
314 err = of_property_read_u32(np, "clock-frequency", &ddata->clk_rate);
315 if (err) {
316 dev_err(dev, "failed to get clock frequency\n");
317 return err;
320 ddata->boot_base = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
321 if (IS_ERR(ddata->boot_base)) {
322 dev_err(dev, "Boot base not found\n");
323 return PTR_ERR(ddata->boot_base);
326 err = of_property_read_u32_index(np, "st,syscfg", 1,
327 &ddata->boot_offset);
328 if (err) {
329 dev_err(dev, "Boot offset not found\n");
330 return -EINVAL;
333 err = clk_prepare(ddata->clk);
334 if (err)
335 dev_err(dev, "failed to get clock\n");
337 return err;
340 static int st_rproc_probe(struct platform_device *pdev)
342 struct device *dev = &pdev->dev;
343 struct st_rproc *ddata;
344 struct device_node *np = dev->of_node;
345 struct rproc *rproc;
346 struct mbox_chan *chan;
347 int enabled;
348 int ret, i;
350 rproc = devm_rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata));
351 if (!rproc)
352 return -ENOMEM;
354 rproc->has_iommu = false;
355 ddata = rproc->priv;
356 ddata->config = (struct st_rproc_config *)device_get_match_data(dev);
357 if (!ddata->config)
358 return -ENODEV;
360 platform_set_drvdata(pdev, rproc);
362 ret = st_rproc_parse_dt(pdev);
363 if (ret)
364 return ret;
366 enabled = st_rproc_state(pdev);
367 if (enabled < 0) {
368 ret = enabled;
369 goto free_clk;
372 if (enabled) {
373 atomic_inc(&rproc->power);
374 rproc->state = RPROC_RUNNING;
375 } else {
376 clk_set_rate(ddata->clk, ddata->clk_rate);
379 if (of_property_present(np, "mbox-names")) {
380 ddata->mbox_client_vq0.dev = dev;
381 ddata->mbox_client_vq0.tx_done = NULL;
382 ddata->mbox_client_vq0.tx_block = false;
383 ddata->mbox_client_vq0.knows_txdone = false;
384 ddata->mbox_client_vq0.rx_callback = st_rproc_mbox_callback_vq0;
386 ddata->mbox_client_vq1.dev = dev;
387 ddata->mbox_client_vq1.tx_done = NULL;
388 ddata->mbox_client_vq1.tx_block = false;
389 ddata->mbox_client_vq1.knows_txdone = false;
390 ddata->mbox_client_vq1.rx_callback = st_rproc_mbox_callback_vq1;
393 * To control a co-processor without IPC mechanism.
394 * This driver can be used without mbox and rpmsg.
396 chan = mbox_request_channel_byname(&ddata->mbox_client_vq0, "vq0_rx");
397 if (IS_ERR(chan)) {
398 dev_err(&rproc->dev, "failed to request mbox chan 0\n");
399 ret = PTR_ERR(chan);
400 goto free_clk;
402 ddata->mbox_chan[ST_RPROC_VQ0 * MBOX_MAX + MBOX_RX] = chan;
404 chan = mbox_request_channel_byname(&ddata->mbox_client_vq0, "vq0_tx");
405 if (IS_ERR(chan)) {
406 dev_err(&rproc->dev, "failed to request mbox chan 0\n");
407 ret = PTR_ERR(chan);
408 goto free_mbox;
410 ddata->mbox_chan[ST_RPROC_VQ0 * MBOX_MAX + MBOX_TX] = chan;
412 chan = mbox_request_channel_byname(&ddata->mbox_client_vq1, "vq1_rx");
413 if (IS_ERR(chan)) {
414 dev_err(&rproc->dev, "failed to request mbox chan 1\n");
415 ret = PTR_ERR(chan);
416 goto free_mbox;
418 ddata->mbox_chan[ST_RPROC_VQ1 * MBOX_MAX + MBOX_RX] = chan;
420 chan = mbox_request_channel_byname(&ddata->mbox_client_vq1, "vq1_tx");
421 if (IS_ERR(chan)) {
422 dev_err(&rproc->dev, "failed to request mbox chan 1\n");
423 ret = PTR_ERR(chan);
424 goto free_mbox;
426 ddata->mbox_chan[ST_RPROC_VQ1 * MBOX_MAX + MBOX_TX] = chan;
429 ret = rproc_add(rproc);
430 if (ret)
431 goto free_mbox;
433 return 0;
435 free_mbox:
436 for (i = 0; i < ST_RPROC_MAX_VRING * MBOX_MAX; i++)
437 mbox_free_channel(ddata->mbox_chan[i]);
438 free_clk:
439 clk_unprepare(ddata->clk);
441 return ret;
444 static void st_rproc_remove(struct platform_device *pdev)
446 struct rproc *rproc = platform_get_drvdata(pdev);
447 struct st_rproc *ddata = rproc->priv;
448 int i;
450 rproc_del(rproc);
452 clk_disable_unprepare(ddata->clk);
454 for (i = 0; i < ST_RPROC_MAX_VRING * MBOX_MAX; i++)
455 mbox_free_channel(ddata->mbox_chan[i]);
458 static struct platform_driver st_rproc_driver = {
459 .probe = st_rproc_probe,
460 .remove = st_rproc_remove,
461 .driver = {
462 .name = "st-rproc",
463 .of_match_table = of_match_ptr(st_rproc_match),
466 module_platform_driver(st_rproc_driver);
468 MODULE_DESCRIPTION("ST Remote Processor Control Driver");
469 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
470 MODULE_LICENSE("GPL v2");