1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7 #include <linux/interrupt.h>
8 #include <linux/list.h>
11 #include <linux/irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_wakeirq.h>
18 #include <linux/regmap.h>
19 #include <linux/soc/qcom/smem.h>
20 #include <linux/soc/qcom/smem_state.h>
21 #include <linux/spinlock.h>
24 * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
25 * of a single 32-bit value between two processors. Each value has a single
26 * writer (the local side) and a single reader (the remote side). Values are
27 * uniquely identified in the system by the directed edge (local processor ID
28 * to remote processor ID) and a string identifier.
30 * Each processor is responsible for creating the outgoing SMEM items and each
31 * item is writable by the local processor and readable by the remote
32 * processor. By using two separate SMEM items that are single-reader and
33 * single-writer, SMP2P does not require any remote locking mechanisms.
35 * The driver uses the Linux GPIO and interrupt framework to expose a virtual
36 * GPIO for each outbound entry and a virtual interrupt controller for each
40 #define SMP2P_MAX_ENTRY 16
41 #define SMP2P_MAX_ENTRY_NAME 16
43 #define SMP2P_FEATURE_SSR_ACK 0x1
44 #define SMP2P_FLAGS_RESTART_DONE_BIT 0
45 #define SMP2P_FLAGS_RESTART_ACK_BIT 1
47 #define SMP2P_MAGIC 0x504d5324
48 #define SMP2P_ALL_FEATURES SMP2P_FEATURE_SSR_ACK
51 * struct smp2p_smem_item - in memory communication structure
52 * @magic: magic number
53 * @version: version - must be 1
54 * @features: features flag - currently unused
55 * @local_pid: processor id of sending end
56 * @remote_pid: processor id of receiving end
57 * @total_entries: number of entries - always SMP2P_MAX_ENTRY
58 * @valid_entries: number of allocated entries
60 * @entries: individual communication entries
61 * @name: name of the entry
62 * @value: content of the entry
64 struct smp2p_smem_item {
75 u8 name[SMP2P_MAX_ENTRY_NAME];
77 } entries[SMP2P_MAX_ENTRY];
81 * struct smp2p_entry - driver context matching one entry
82 * @node: list entry to keep track of allocated entries
83 * @smp2p: reference to the device driver context
84 * @name: name of the entry, to match against smp2p_smem_item
85 * @value: pointer to smp2p_smem_item entry value
86 * @last_value: last handled value
87 * @domain: irq_domain for inbound entries
88 * @irq_enabled:bitmap to track enabled irq bits
89 * @irq_rising: bitmap to mark irq bits for rising detection
90 * @irq_falling:bitmap to mark irq bits for falling detection
91 * @state: smem state handle
92 * @lock: spinlock to protect read-modify-write of the value
95 struct list_head node;
96 struct qcom_smp2p *smp2p;
102 struct irq_domain *domain;
103 DECLARE_BITMAP(irq_enabled, 32);
104 DECLARE_BITMAP(irq_rising, 32);
105 DECLARE_BITMAP(irq_falling, 32);
107 struct qcom_smem_state *state;
112 #define SMP2P_INBOUND 0
113 #define SMP2P_OUTBOUND 1
116 * struct qcom_smp2p - device driver context
117 * @dev: device driver handle
118 * @in: pointer to the inbound smem item
119 * @out: pointer to the outbound smem item
120 * @smem_items: ids of the two smem items
121 * @valid_entries: already scanned inbound entries
122 * @local_pid: processor id of the inbound edge
123 * @remote_pid: processor id of the outbound edge
124 * @ipc_regmap: regmap for the outbound ipc
125 * @ipc_offset: offset within the regmap
126 * @ipc_bit: bit in regmap@offset to kick to signal remote processor
127 * @mbox_client: mailbox client handle
128 * @mbox_chan: apcs ipc mailbox channel handle
129 * @inbound: list of inbound entries
130 * @outbound: list of outbound entries
135 struct smp2p_smem_item *in;
136 struct smp2p_smem_item *out;
138 unsigned smem_items[SMP2P_OUTBOUND + 1];
140 unsigned valid_entries;
142 bool ssr_ack_enabled;
144 bool negotiation_done;
149 struct regmap *ipc_regmap;
153 struct mbox_client mbox_client;
154 struct mbox_chan *mbox_chan;
156 struct list_head inbound;
157 struct list_head outbound;
160 static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
162 /* Make sure any updated data is written before the kick */
165 if (smp2p->mbox_chan) {
166 mbox_send_message(smp2p->mbox_chan, NULL);
167 mbox_client_txdone(smp2p->mbox_chan, 0);
169 regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
173 static bool qcom_smp2p_check_ssr(struct qcom_smp2p *smp2p)
175 struct smp2p_smem_item *in = smp2p->in;
178 if (!smp2p->ssr_ack_enabled)
181 restart = in->flags & BIT(SMP2P_FLAGS_RESTART_DONE_BIT);
183 return restart != smp2p->ssr_ack;
186 static void qcom_smp2p_do_ssr_ack(struct qcom_smp2p *smp2p)
188 struct smp2p_smem_item *out = smp2p->out;
191 smp2p->ssr_ack = !smp2p->ssr_ack;
193 val = out->flags & ~BIT(SMP2P_FLAGS_RESTART_ACK_BIT);
195 val |= BIT(SMP2P_FLAGS_RESTART_ACK_BIT);
198 qcom_smp2p_kick(smp2p);
201 static void qcom_smp2p_negotiate(struct qcom_smp2p *smp2p)
203 struct smp2p_smem_item *out = smp2p->out;
204 struct smp2p_smem_item *in = smp2p->in;
206 if (in->version == out->version) {
207 out->features &= in->features;
209 if (out->features & SMP2P_FEATURE_SSR_ACK)
210 smp2p->ssr_ack_enabled = true;
212 smp2p->negotiation_done = true;
216 static void qcom_smp2p_notify_in(struct qcom_smp2p *smp2p)
218 struct smp2p_smem_item *in;
219 struct smp2p_entry *entry;
222 char buf[SMP2P_MAX_ENTRY_NAME];
228 /* Match newly created entries */
229 for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
230 list_for_each_entry(entry, &smp2p->inbound, node) {
231 memcpy(buf, in->entries[i].name, sizeof(buf));
232 if (!strcmp(buf, entry->name)) {
233 entry->value = &in->entries[i].value;
238 smp2p->valid_entries = i;
240 /* Fire interrupts based on any value changes */
241 list_for_each_entry(entry, &smp2p->inbound, node) {
242 /* Ignore entries not yet allocated by the remote side */
246 val = readl(entry->value);
248 status = val ^ entry->last_value;
249 entry->last_value = val;
251 /* No changes of this entry? */
255 for_each_set_bit(i, entry->irq_enabled, 32) {
256 if (!(status & BIT(i)))
259 if ((val & BIT(i) && test_bit(i, entry->irq_rising)) ||
260 (!(val & BIT(i)) && test_bit(i, entry->irq_falling))) {
261 irq_pin = irq_find_mapping(entry->domain, i);
262 handle_nested_irq(irq_pin);
269 * qcom_smp2p_intr() - interrupt handler for incoming notifications
271 * @data: smp2p driver context
273 * Handle notifications from the remote side to handle newly allocated entries
274 * or any changes to the state bits of existing entries.
276 static irqreturn_t qcom_smp2p_intr(int irq, void *data)
278 struct smp2p_smem_item *in;
279 struct qcom_smp2p *smp2p = data;
280 unsigned int smem_id = smp2p->smem_items[SMP2P_INBOUND];
281 unsigned int pid = smp2p->remote_pid;
287 /* Acquire smem item, if not already found */
289 in = qcom_smem_get(pid, smem_id, &size);
292 "Unable to acquire remote smp2p item\n");
299 if (!smp2p->negotiation_done)
300 qcom_smp2p_negotiate(smp2p);
302 if (smp2p->negotiation_done) {
303 ack_restart = qcom_smp2p_check_ssr(smp2p);
304 qcom_smp2p_notify_in(smp2p);
307 qcom_smp2p_do_ssr_ack(smp2p);
314 static void smp2p_mask_irq(struct irq_data *irqd)
316 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
317 irq_hw_number_t irq = irqd_to_hwirq(irqd);
319 clear_bit(irq, entry->irq_enabled);
322 static void smp2p_unmask_irq(struct irq_data *irqd)
324 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
325 irq_hw_number_t irq = irqd_to_hwirq(irqd);
327 set_bit(irq, entry->irq_enabled);
330 static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
332 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
333 irq_hw_number_t irq = irqd_to_hwirq(irqd);
335 if (!(type & IRQ_TYPE_EDGE_BOTH))
338 if (type & IRQ_TYPE_EDGE_RISING)
339 set_bit(irq, entry->irq_rising);
341 clear_bit(irq, entry->irq_rising);
343 if (type & IRQ_TYPE_EDGE_FALLING)
344 set_bit(irq, entry->irq_falling);
346 clear_bit(irq, entry->irq_falling);
351 static struct irq_chip smp2p_irq_chip = {
353 .irq_mask = smp2p_mask_irq,
354 .irq_unmask = smp2p_unmask_irq,
355 .irq_set_type = smp2p_set_irq_type,
358 static int smp2p_irq_map(struct irq_domain *d,
362 struct smp2p_entry *entry = d->host_data;
364 irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
365 irq_set_chip_data(irq, entry);
366 irq_set_nested_thread(irq, 1);
367 irq_set_noprobe(irq);
372 static const struct irq_domain_ops smp2p_irq_ops = {
373 .map = smp2p_irq_map,
374 .xlate = irq_domain_xlate_twocell,
377 static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
378 struct smp2p_entry *entry,
379 struct device_node *node)
381 entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
382 if (!entry->domain) {
383 dev_err(smp2p->dev, "failed to add irq_domain\n");
390 static int smp2p_update_bits(void *data, u32 mask, u32 value)
392 struct smp2p_entry *entry = data;
397 spin_lock_irqsave(&entry->lock, flags);
398 val = orig = readl(entry->value);
401 writel(val, entry->value);
402 spin_unlock_irqrestore(&entry->lock, flags);
405 qcom_smp2p_kick(entry->smp2p);
410 static const struct qcom_smem_state_ops smp2p_state_ops = {
411 .update_bits = smp2p_update_bits,
414 static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
415 struct smp2p_entry *entry,
416 struct device_node *node)
418 struct smp2p_smem_item *out = smp2p->out;
419 char buf[SMP2P_MAX_ENTRY_NAME] = {};
421 /* Allocate an entry from the smem item */
422 strlcpy(buf, entry->name, SMP2P_MAX_ENTRY_NAME);
423 memcpy(out->entries[out->valid_entries].name, buf, SMP2P_MAX_ENTRY_NAME);
425 /* Make the logical entry reference the physical value */
426 entry->value = &out->entries[out->valid_entries].value;
428 out->valid_entries++;
430 entry->state = qcom_smem_state_register(node, &smp2p_state_ops, entry);
431 if (IS_ERR(entry->state)) {
432 dev_err(smp2p->dev, "failed to register qcom_smem_state\n");
433 return PTR_ERR(entry->state);
439 static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
441 struct smp2p_smem_item *out;
442 unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
443 unsigned pid = smp2p->remote_pid;
446 ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
447 if (ret < 0 && ret != -EEXIST) {
448 if (ret != -EPROBE_DEFER)
450 "unable to allocate local smp2p item\n");
454 out = qcom_smem_get(pid, smem_id, NULL);
456 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
460 memset(out, 0, sizeof(*out));
461 out->magic = SMP2P_MAGIC;
462 out->local_pid = smp2p->local_pid;
463 out->remote_pid = smp2p->remote_pid;
464 out->total_entries = SMP2P_MAX_ENTRY;
465 out->valid_entries = 0;
466 out->features = SMP2P_ALL_FEATURES;
469 * Make sure the rest of the header is written before we validate the
470 * item by writing a valid version number.
475 qcom_smp2p_kick(smp2p);
482 static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
484 struct device_node *syscon;
485 struct device *dev = smp2p->dev;
489 syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
491 dev_err(dev, "no qcom,ipc node\n");
495 smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
497 if (IS_ERR(smp2p->ipc_regmap))
498 return PTR_ERR(smp2p->ipc_regmap);
501 ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
503 dev_err(dev, "no offset in %s\n", key);
507 ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
509 dev_err(dev, "no bit in %s\n", key);
516 static int qcom_smp2p_probe(struct platform_device *pdev)
518 struct smp2p_entry *entry;
519 struct device_node *node;
520 struct qcom_smp2p *smp2p;
525 smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
529 smp2p->dev = &pdev->dev;
530 INIT_LIST_HEAD(&smp2p->inbound);
531 INIT_LIST_HEAD(&smp2p->outbound);
533 platform_set_drvdata(pdev, smp2p);
536 ret = of_property_read_u32_array(pdev->dev.of_node, key,
537 smp2p->smem_items, 2);
541 key = "qcom,local-pid";
542 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
544 goto report_read_failure;
546 key = "qcom,remote-pid";
547 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
549 goto report_read_failure;
551 irq = platform_get_irq(pdev, 0);
555 smp2p->mbox_client.dev = &pdev->dev;
556 smp2p->mbox_client.knows_txdone = true;
557 smp2p->mbox_chan = mbox_request_channel(&smp2p->mbox_client, 0);
558 if (IS_ERR(smp2p->mbox_chan)) {
559 if (PTR_ERR(smp2p->mbox_chan) != -ENODEV)
560 return PTR_ERR(smp2p->mbox_chan);
562 smp2p->mbox_chan = NULL;
564 ret = smp2p_parse_ipc(smp2p);
569 ret = qcom_smp2p_alloc_outbound_item(smp2p);
573 for_each_available_child_of_node(pdev->dev.of_node, node) {
574 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
578 goto unwind_interfaces;
581 entry->smp2p = smp2p;
582 spin_lock_init(&entry->lock);
584 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
587 goto unwind_interfaces;
590 if (of_property_read_bool(node, "interrupt-controller")) {
591 ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
594 goto unwind_interfaces;
597 list_add(&entry->node, &smp2p->inbound);
599 ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
602 goto unwind_interfaces;
605 list_add(&entry->node, &smp2p->outbound);
609 /* Kick the outgoing edge after allocating entries */
610 qcom_smp2p_kick(smp2p);
612 ret = devm_request_threaded_irq(&pdev->dev, irq,
613 NULL, qcom_smp2p_intr,
615 "smp2p", (void *)smp2p);
617 dev_err(&pdev->dev, "failed to request interrupt\n");
618 goto unwind_interfaces;
622 * Treat smp2p interrupt as wakeup source, but keep it disabled
623 * by default. User space can decide enabling it depending on its
624 * use cases. For example if remoteproc crashes and device wants
625 * to handle it immediatedly (e.g. to not miss phone calls) it can
626 * enable wakeup source from user space, while other devices which
627 * do not have proper autosleep feature may want to handle it with
628 * other wakeup events (e.g. Power button) instead waking up immediately.
630 device_set_wakeup_capable(&pdev->dev, true);
632 ret = dev_pm_set_wake_irq(&pdev->dev, irq);
634 goto set_wake_irq_fail;
639 dev_pm_clear_wake_irq(&pdev->dev);
642 list_for_each_entry(entry, &smp2p->inbound, node)
643 irq_domain_remove(entry->domain);
645 list_for_each_entry(entry, &smp2p->outbound, node)
646 qcom_smem_state_unregister(entry->state);
648 smp2p->out->valid_entries = 0;
651 mbox_free_channel(smp2p->mbox_chan);
656 dev_err(&pdev->dev, "failed to read %s\n", key);
660 static int qcom_smp2p_remove(struct platform_device *pdev)
662 struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
663 struct smp2p_entry *entry;
665 dev_pm_clear_wake_irq(&pdev->dev);
667 list_for_each_entry(entry, &smp2p->inbound, node)
668 irq_domain_remove(entry->domain);
670 list_for_each_entry(entry, &smp2p->outbound, node)
671 qcom_smem_state_unregister(entry->state);
673 mbox_free_channel(smp2p->mbox_chan);
675 smp2p->out->valid_entries = 0;
680 static const struct of_device_id qcom_smp2p_of_match[] = {
681 { .compatible = "qcom,smp2p" },
684 MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
686 static struct platform_driver qcom_smp2p_driver = {
687 .probe = qcom_smp2p_probe,
688 .remove = qcom_smp2p_remove,
690 .name = "qcom_smp2p",
691 .of_match_table = qcom_smp2p_of_match,
694 module_platform_driver(qcom_smp2p_driver);
696 MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
697 MODULE_LICENSE("GPL v2");