1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * OMAP Remote Processor driver
4 *
5 * Copyright (C) 2011-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Copyright (C) 2011 Google, Inc.
7 *
8 * Ohad Ben-Cohen <ohad@wizery.com>
9 * Brian Swetland <swetland@google.com>
10 * Fernando Guzman Lugo <fernando.lugo@ti.com>
11 * Mark Grosen <mgrosen@ti.com>
12 * Suman Anna <s-anna@ti.com>
13 * Hari Kanigeri <h-kanigeri2@ti.com>
14 */
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/of_device.h>
20 #include <linux/of_address.h>
21 #include <linux/of_reserved_mem.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/remoteproc.h>
25 #include <linux/mailbox_client.h>
26 #include <linux/omap-mailbox.h>
27 #include <linux/regmap.h>
28 #include <linux/mfd/syscon.h>
30 #include <linux/platform_data/remoteproc-omap.h>
32 #include "omap_remoteproc.h"
33 #include "remoteproc_internal.h"
35 #define OMAP_RPROC_DSP_LOCAL_MEM_OFFSET (0x00800000)
36 #define OMAP_RPROC_IPU_L2RAM_DEV_ADDR (0x20000000)
38 /**
39 * struct omap_rproc_boot_data - boot data structure for the DSP omap rprocs
40 * @syscon: regmap handle for the system control configuration module
41 * @boot_reg: boot register offset within the @syscon regmap
42 * @boot_reg_shift: bit-field shift required for the boot address value in
43 * @boot_reg
44 */
45 struct omap_rproc_boot_data {
46 struct regmap *syscon;
47 unsigned int boot_reg;
48 unsigned int boot_reg_shift;
49 };
51 /**
52 * struct omap_rproc_mem - internal memory structure
53 * @cpu_addr: MPU virtual address of the memory region
54 * @bus_addr: bus address used to access the memory region
55 * @dev_addr: device address of the memory region from DSP view
56 * @size: size of the memory region
57 */
58 struct omap_rproc_mem {
59 void __iomem *cpu_addr;
60 phys_addr_t bus_addr;
61 u32 dev_addr;
62 size_t size;
63 };
65 /**
66 * struct omap_rproc - omap remote processor state
67 * @mbox: mailbox channel handle
68 * @client: mailbox client to request the mailbox channel
69 * @boot_data: boot data structure for setting processor boot address
70 * @mem: internal memory regions data
71 * @num_mems: number of internal memory regions
72 * @rproc: rproc handle
73 */
74 struct omap_rproc {
75 struct mbox_chan *mbox;
76 struct mbox_client client;
77 struct omap_rproc_boot_data *boot_data;
78 struct omap_rproc_mem *mem;
79 int num_mems;
80 struct rproc *rproc;
81 };
83 /**
84 * struct omap_rproc_dev_data - device data for the omap remote processor
85 * @device_name: device name of the remote processor
86 * @fw_name: firmware name to use
87 */
88 struct omap_rproc_dev_data {
89 const char *device_name;
90 const char *fw_name;
91 };
93 /**
94 * omap_rproc_mbox_callback() - inbound mailbox message handler
95 * @client: mailbox client pointer used for requesting the mailbox channel
96 * @data: mailbox payload
97 *
98 * This handler is invoked by omap's mailbox driver whenever a mailbox
99 * message is received. Usually, the mailbox payload simply contains
100 * the index of the virtqueue that is kicked by the remote processor,
101 * and we let remoteproc core handle it.
102 *
103 * In addition to virtqueue indices, we also have some out-of-band values
104 * that indicates different events. Those values are deliberately very
105 * big so they don't coincide with virtqueue indices.
106 */
107 static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
108 {
109 struct omap_rproc *oproc = container_of(client, struct omap_rproc,
110 client);
111 struct device *dev = oproc->rproc->dev.parent;
112 const char *name = oproc->rproc->name;
113 u32 msg = (u32)data;
115 dev_dbg(dev, "mbox msg: 0x%x\n", msg);
117 switch (msg) {
118 case RP_MBOX_CRASH:
119 /* just log this for now. later, we'll also do recovery */
120 dev_err(dev, "omap rproc %s crashed\n", name);
121 break;
122 case RP_MBOX_ECHO_REPLY:
123 dev_info(dev, "received echo reply from %s\n", name);
124 break;
125 default:
126 /* msg contains the index of the triggered vring */
127 if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
128 dev_dbg(dev, "no message was found in vqid %d\n", msg);
129 }
130 }
132 /* kick a virtqueue */
133 static void omap_rproc_kick(struct rproc *rproc, int vqid)
134 {
135 struct omap_rproc *oproc = rproc->priv;
136 struct device *dev = rproc->dev.parent;
137 int ret;
139 /* send the index of the triggered virtqueue in the mailbox payload */
140 ret = mbox_send_message(oproc->mbox, (void *)vqid);
141 if (ret < 0)
142 dev_err(dev, "failed to send mailbox message, status = %d\n",
143 ret);
144 }
146 /**
147 * omap_rproc_write_dsp_boot_addr - set boot address for a DSP remote processor
148 * @rproc: handle of a remote processor
149 *
150 * Set boot address for a supported DSP remote processor.
151 */
152 static int omap_rproc_write_dsp_boot_addr(struct rproc *rproc)
153 {
154 struct device *dev = rproc->dev.parent;
155 struct omap_rproc *oproc = rproc->priv;
156 struct omap_rproc_boot_data *bdata = oproc->boot_data;
157 u32 offset = bdata->boot_reg;
158 unsigned int value = rproc->bootaddr;
159 unsigned int mask = ~(SZ_1K - 1);
161 if (value & (SZ_1K - 1)) {
162 dev_err(dev, "invalid boot address 0x%x, must be aligned on a 1KB boundary\n",
163 value);
164 return -EINVAL;
165 }
167 value >>= bdata->boot_reg_shift;
168 mask >>= bdata->boot_reg_shift;
170 regmap_update_bits(bdata->syscon, offset, mask, value);
172 return 0;
173 }
175 /*
176 * Power up the remote processor.
177 *
178 * This function will be invoked only after the firmware for this rproc
179 * was loaded, parsed successfully, and all of its resource requirements
180 * were met.
181 */
182 static int omap_rproc_start(struct rproc *rproc)
183 {
184 struct omap_rproc *oproc = rproc->priv;
185 struct device *dev = rproc->dev.parent;
186 struct platform_device *pdev = to_platform_device(dev);
187 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
188 int ret;
189 struct mbox_client *client = &oproc->client;
191 if (oproc->boot_data) {
192 ret = omap_rproc_write_dsp_boot_addr(rproc);
193 if (ret)
194 return ret;
195 }
197 client->dev = dev;
198 client->tx_done = NULL;
199 client->rx_callback = omap_rproc_mbox_callback;
200 client->tx_block = false;
201 client->knows_txdone = false;
203 oproc->mbox = mbox_request_channel(client, 0);
204 if (IS_ERR(oproc->mbox)) {
205 ret = -EBUSY;
206 dev_err(dev, "mbox_request_channel failed: %ld\n",
207 PTR_ERR(oproc->mbox));
208 return ret;
209 }
211 /*
212 * Ping the remote processor. this is only for sanity-sake;
213 * there is no functional effect whatsoever.
214 *
215 * Note that the reply will _not_ arrive immediately: this message
216 * will wait in the mailbox fifo until the remote processor is booted.
217 */
218 ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
219 if (ret < 0) {
220 dev_err(dev, "mbox_send_message failed: %d\n", ret);
221 goto put_mbox;
222 }
224 ret = pdata->device_enable(pdev);
225 if (ret) {
226 dev_err(dev, "omap_device_enable failed: %d\n", ret);
227 goto put_mbox;
228 }
230 return 0;
232 put_mbox:
233 mbox_free_channel(oproc->mbox);
234 return ret;
235 }
237 /* power off the remote processor */
238 static int omap_rproc_stop(struct rproc *rproc)
239 {
240 struct device *dev = rproc->dev.parent;
241 struct platform_device *pdev = to_platform_device(dev);
242 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
243 struct omap_rproc *oproc = rproc->priv;
244 int ret;
246 ret = pdata->device_shutdown(pdev);
247 if (ret)
248 return ret;
250 mbox_free_channel(oproc->mbox);
252 return 0;
253 }
255 /*
256 * Internal Memory translation helper
257 *
258 * Custom function implementing the rproc .da_to_va ops to provide address
259 * translation (device address to kernel virtual address) for internal RAMs
260 * present in a DSP or IPU device). The translated addresses can be used
261 * either by the remoteproc core for loading, or by any rpmsg bus drivers.
262 */
263 static void *omap_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
264 u32 flags)
265 {
266 struct omap_rproc *oproc = rproc->priv;
267 void *va = NULL;
268 int i;
269 u32 offset;
271 if (len <= 0)
272 return NULL;
274 if (!oproc->num_mems)
275 return NULL;
277 for (i = 0; i < oproc->num_mems; i++) {
278 if (da >= oproc->mem[i].dev_addr && da + len <=
279 oproc->mem[i].dev_addr + oproc->mem[i].size) {
280 offset = da - oproc->mem[i].dev_addr;
281 /* __force to make sparse happy with type conversion */
282 va = (__force void *)(oproc->mem[i].cpu_addr + offset);
283 break;
284 }
285 }
287 return va;
288 }
290 static const struct rproc_ops omap_rproc_ops = {
291 .start = omap_rproc_start,
292 .stop = omap_rproc_stop,
293 .kick = omap_rproc_kick,
294 .da_to_va = omap_rproc_da_to_va,
295 };
297 static const struct omap_rproc_dev_data omap4_dsp_dev_data = {
298 .device_name = "dsp",
299 .fw_name = "omap4-dsp-fw.xe64T",
300 };
302 static const struct omap_rproc_dev_data omap4_ipu_dev_data = {
303 .device_name = "ipu",
304 .fw_name = "omap4-ipu-fw.xem3",
305 };
307 static const struct omap_rproc_dev_data omap5_dsp_dev_data = {
308 .device_name = "dsp",
309 .fw_name = "omap5-dsp-fw.xe64T",
310 };
312 static const struct omap_rproc_dev_data omap5_ipu_dev_data = {
313 .device_name = "ipu",
314 .fw_name = "omap5-ipu-fw.xem4",
315 };
317 static const struct omap_rproc_dev_data dra7_rproc_dev_data[] = {
318 {
319 .device_name = "40800000.dsp",
320 .fw_name = "dra7-dsp1-fw.xe66",
321 },
322 {
323 .device_name = "41000000.dsp",
324 .fw_name = "dra7-dsp2-fw.xe66",
325 },
326 {
327 .device_name = "55020000.ipu",
328 .fw_name = "dra7-ipu2-fw.xem4",
329 },
330 {
331 .device_name = "58820000.ipu",
332 .fw_name = "dra7-ipu1-fw.xem4",
333 },
334 {
335 /* sentinel */
336 },
337 };
339 static const struct of_device_id omap_rproc_of_match[] = {
340 {
341 .compatible = "ti,omap4-dsp",
342 .data = &omap4_dsp_dev_data,
343 },
344 {
345 .compatible = "ti,omap4-ipu",
346 .data = &omap4_ipu_dev_data,
347 },
348 {
349 .compatible = "ti,omap5-dsp",
350 .data = &omap5_dsp_dev_data,
351 },
352 {
353 .compatible = "ti,omap5-ipu",
354 .data = &omap5_ipu_dev_data,
355 },
356 {
357 .compatible = "ti,dra7-dsp",
358 .data = dra7_rproc_dev_data,
359 },
360 {
361 .compatible = "ti,dra7-ipu",
362 .data = dra7_rproc_dev_data,
363 },
364 {
365 /* end */
366 },
367 };
368 MODULE_DEVICE_TABLE(of, omap_rproc_of_match);
370 static const char *omap_rproc_get_firmware(struct platform_device *pdev)
371 {
372 struct device_node *np = pdev->dev.of_node;
373 const struct omap_rproc_dev_data *data;
375 data = of_device_get_match_data(&pdev->dev);
376 if (!data)
377 return ERR_PTR(-ENODEV);
379 if (!of_device_is_compatible(np, "ti,dra7-dsp") &&
380 !of_device_is_compatible(np, "ti,dra7-ipu"))
381 return data->fw_name;
383 for (; data && data->device_name; data++) {
384 if (!strcmp(dev_name(&pdev->dev), data->device_name))
385 return data->fw_name;
386 }
388 return ERR_PTR(-ENOENT);
389 }
391 static int omap_rproc_get_boot_data(struct platform_device *pdev,
392 struct rproc *rproc)
393 {
394 struct device_node *np = pdev->dev.of_node;
395 struct omap_rproc *oproc = rproc->priv;
396 int ret;
398 if (!of_device_is_compatible(np, "ti,omap4-dsp") &&
399 !of_device_is_compatible(np, "ti,omap5-dsp") &&
400 !of_device_is_compatible(np, "ti,dra7-dsp"))
401 return 0;
403 oproc->boot_data = devm_kzalloc(&pdev->dev, sizeof(*oproc->boot_data),
404 GFP_KERNEL);
405 if (!oproc->boot_data)
406 return -ENOMEM;
408 if (!of_property_read_bool(np, "syscon-bootreg")) {
409 dev_err(&pdev->dev, "syscon-bootreg property is missing\n");
410 return -EINVAL;
411 }
413 oproc->boot_data->syscon =
414 syscon_regmap_lookup_by_phandle(np, "syscon-bootreg");
415 if (IS_ERR(oproc->boot_data->syscon)) {
416 ret = PTR_ERR(oproc->boot_data->syscon);
417 return ret;
418 }
420 if (of_property_read_u32_index(np, "syscon-bootreg", 1,
421 &oproc->boot_data->boot_reg)) {
422 dev_err(&pdev->dev, "couldn't get the boot register\n");
423 return -EINVAL;
424 }
426 if (of_device_is_compatible(np, "ti,dra7-dsp"))
427 oproc->boot_data->boot_reg_shift = 10;
429 return 0;
430 }
432 static int omap_rproc_of_get_internal_memories(struct platform_device *pdev,
433 struct rproc *rproc)
434 {
435 static const char * const ipu_mem_names[] = {"l2ram"};
436 static const char * const dra7_dsp_mem_names[] = {"l2ram", "l1pram",
437 "l1dram"};
438 struct device_node *np = pdev->dev.of_node;
439 struct omap_rproc *oproc = rproc->priv;
440 struct device *dev = &pdev->dev;
441 const char * const *mem_names;
442 struct resource *res;
443 int num_mems;
444 const __be32 *addrp;
445 u32 l4_offset = 0;
446 u64 size;
447 int i;
449 /* OMAP4 and OMAP5 DSPs do not have support for flat SRAM */
450 if (of_device_is_compatible(np, "ti,omap4-dsp") ||
451 of_device_is_compatible(np, "ti,omap5-dsp"))
452 return 0;
454 /* DRA7 DSPs have two additional SRAMs at L1 level */
455 if (of_device_is_compatible(np, "ti,dra7-dsp")) {
456 mem_names = dra7_dsp_mem_names;
457 num_mems = ARRAY_SIZE(dra7_dsp_mem_names);
458 } else {
459 mem_names = ipu_mem_names;
460 num_mems = ARRAY_SIZE(ipu_mem_names);
461 }
463 oproc->mem = devm_kcalloc(dev, num_mems, sizeof(*oproc->mem),
464 GFP_KERNEL);
465 if (!oproc->mem)
466 return -ENOMEM;
468 for (i = 0; i < num_mems; i++) {
469 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
470 mem_names[i]);
471 oproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
472 if (IS_ERR(oproc->mem[i].cpu_addr)) {
473 dev_err(dev, "failed to parse and map %s memory\n",
474 mem_names[i]);
475 return PTR_ERR(oproc->mem[i].cpu_addr);
476 }
477 oproc->mem[i].bus_addr = res->start;
479 /*
480 * The DSPs have the internal memories starting at a fixed
481 * offset of 0x800000 from address 0, and this corresponds to
482 * L2RAM. The L3 address view has the L2RAM bus address as the
483 * starting address for the IP, so the L2RAM memory region needs
484 * to be processed first, and the device addresses for each
485 * memory region can be computed using the relative offset
486 * from this base address.
487 */
488 if (of_device_is_compatible(np, "ti,dra7-dsp") &&
489 !strcmp(mem_names[i], "l2ram")) {
490 addrp = of_get_address(dev->of_node, i, &size, NULL);
491 l4_offset = be32_to_cpu(*addrp);
492 }
493 oproc->mem[i].dev_addr =
494 of_device_is_compatible(np, "ti,dra7-dsp") ?
495 res->start - l4_offset +
496 OMAP_RPROC_DSP_LOCAL_MEM_OFFSET :
497 OMAP_RPROC_IPU_L2RAM_DEV_ADDR;
498 oproc->mem[i].size = resource_size(res);
500 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%x va %p da 0x%x\n",
501 mem_names[i], &oproc->mem[i].bus_addr,
502 oproc->mem[i].size, oproc->mem[i].cpu_addr,
503 oproc->mem[i].dev_addr);
504 }
505 oproc->num_mems = num_mems;
507 return 0;
508 }
510 static int omap_rproc_probe(struct platform_device *pdev)
511 {
512 struct omap_rproc_pdata *pdata = dev_get_platdata(&pdev->dev);
513 struct device_node *np = pdev->dev.of_node;
514 struct omap_rproc *oproc;
515 struct rproc *rproc;
516 const char *firmware;
517 int ret;
519 if (!np) {
520 dev_err(&pdev->dev, "only DT-based devices are supported\n");
521 return -ENODEV;
522 }
524 if (!pdata || !pdata->device_enable || !pdata->device_shutdown) {
525 dev_err(&pdev->dev, "platform data is either missing or incomplete\n");
526 return -ENODEV;
527 }
529 firmware = omap_rproc_get_firmware(pdev);
530 if (IS_ERR(firmware))
531 return PTR_ERR(firmware);
533 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
534 if (ret) {
535 dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
536 return ret;
537 }
539 rproc = rproc_alloc(&pdev->dev, dev_name(&pdev->dev), &omap_rproc_ops,
540 firmware, sizeof(*oproc));
541 if (!rproc)
542 return -ENOMEM;
544 oproc = rproc->priv;
545 oproc->rproc = rproc;
546 /* All existing OMAP IPU and DSP processors have an MMU */
547 rproc->has_iommu = true;
549 ret = omap_rproc_of_get_internal_memories(pdev, rproc);
550 if (ret)
551 goto free_rproc;
553 ret = omap_rproc_get_boot_data(pdev, rproc);
554 if (ret)
555 goto free_rproc;
557 ret = of_reserved_mem_device_init(&pdev->dev);
558 if (ret) {
559 dev_err(&pdev->dev, "device does not have specific CMA pool\n");
560 goto free_rproc;
561 }
563 platform_set_drvdata(pdev, rproc);
565 ret = rproc_add(rproc);
566 if (ret)
567 goto release_mem;
569 return 0;
571 release_mem:
572 of_reserved_mem_device_release(&pdev->dev);
573 free_rproc:
574 rproc_free(rproc);
575 return ret;
576 }
578 static int omap_rproc_remove(struct platform_device *pdev)
579 {
580 struct rproc *rproc = platform_get_drvdata(pdev);
582 rproc_del(rproc);
583 rproc_free(rproc);
584 of_reserved_mem_device_release(&pdev->dev);
586 return 0;
587 }
589 static struct platform_driver omap_rproc_driver = {
590 .probe = omap_rproc_probe,
591 .remove = omap_rproc_remove,
592 .driver = {
593 .name = "omap-rproc",
594 .of_match_table = omap_rproc_of_match,
595 },
596 };
598 module_platform_driver(omap_rproc_driver);
600 MODULE_LICENSE("GPL v2");
601 MODULE_DESCRIPTION("OMAP Remote Processor control driver");