Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015, Sony Mobile Communications AB.
4 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
5 */
6
7#include <linux/interrupt.h>
8#include <linux/list.h>
9#include <linux/io.h>
10#include <linux/of.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/seq_file.h>
20#include <linux/soc/qcom/smem.h>
21#include <linux/soc/qcom/smem_state.h>
22#include <linux/spinlock.h>
23
24/*
25 * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
26 * of a single 32-bit value between two processors. Each value has a single
27 * writer (the local side) and a single reader (the remote side). Values are
28 * uniquely identified in the system by the directed edge (local processor ID
29 * to remote processor ID) and a string identifier.
30 *
31 * Each processor is responsible for creating the outgoing SMEM items and each
32 * item is writable by the local processor and readable by the remote
33 * processor. By using two separate SMEM items that are single-reader and
34 * single-writer, SMP2P does not require any remote locking mechanisms.
35 *
36 * The driver uses the Linux GPIO and interrupt framework to expose a virtual
37 * GPIO for each outbound entry and a virtual interrupt controller for each
38 * inbound entry.
39 */
40
41#define SMP2P_MAX_ENTRY 16
42#define SMP2P_MAX_ENTRY_NAME 16
43
44#define SMP2P_FEATURE_SSR_ACK 0x1
45#define SMP2P_FLAGS_RESTART_DONE_BIT 0
46#define SMP2P_FLAGS_RESTART_ACK_BIT 1
47
48#define SMP2P_MAGIC 0x504d5324
49#define SMP2P_ALL_FEATURES SMP2P_FEATURE_SSR_ACK
50
51/**
52 * struct smp2p_smem_item - in memory communication structure
53 * @magic: magic number
54 * @version: version - must be 1
55 * @features: features flag - currently unused
56 * @local_pid: processor id of sending end
57 * @remote_pid: processor id of receiving end
58 * @total_entries: number of entries - always SMP2P_MAX_ENTRY
59 * @valid_entries: number of allocated entries
60 * @flags:
61 * @entries: individual communication entries
62 * @entries.name: name of the entry
63 * @entries.value: content of the entry
64 */
65struct smp2p_smem_item {
66 u32 magic;
67 u8 version;
68 unsigned features:24;
69 u16 local_pid;
70 u16 remote_pid;
71 u16 total_entries;
72 u16 valid_entries;
73 u32 flags;
74
75 struct {
76 u8 name[SMP2P_MAX_ENTRY_NAME];
77 u32 value;
78 } entries[SMP2P_MAX_ENTRY];
79} __packed;
80
81/**
82 * struct smp2p_entry - driver context matching one entry
83 * @node: list entry to keep track of allocated entries
84 * @smp2p: reference to the device driver context
85 * @name: name of the entry, to match against smp2p_smem_item
86 * @value: pointer to smp2p_smem_item entry value
87 * @last_value: last handled value
88 * @domain: irq_domain for inbound entries
89 * @irq_enabled:bitmap to track enabled irq bits
90 * @irq_rising: bitmap to mark irq bits for rising detection
91 * @irq_falling:bitmap to mark irq bits for falling detection
92 * @state: smem state handle
93 * @lock: spinlock to protect read-modify-write of the value
94 */
95struct smp2p_entry {
96 struct list_head node;
97 struct qcom_smp2p *smp2p;
98
99 const char *name;
100 u32 *value;
101 u32 last_value;
102
103 struct irq_domain *domain;
104 DECLARE_BITMAP(irq_enabled, 32);
105 DECLARE_BITMAP(irq_rising, 32);
106 DECLARE_BITMAP(irq_falling, 32);
107
108 struct qcom_smem_state *state;
109
110 spinlock_t lock;
111};
112
113#define SMP2P_INBOUND 0
114#define SMP2P_OUTBOUND 1
115
116/**
117 * struct qcom_smp2p - device driver context
118 * @dev: device driver handle
119 * @in: pointer to the inbound smem item
120 * @out: pointer to the outbound smem item
121 * @smem_items: ids of the two smem items
122 * @valid_entries: already scanned inbound entries
123 * @ssr_ack_enabled: SMP2P_FEATURE_SSR_ACK feature is supported and was enabled
124 * @ssr_ack: current cached state of the local ack bit
125 * @negotiation_done: whether negotiating finished
126 * @local_pid: processor id of the inbound edge
127 * @remote_pid: processor id of the outbound edge
128 * @ipc_regmap: regmap for the outbound ipc
129 * @ipc_offset: offset within the regmap
130 * @ipc_bit: bit in regmap@offset to kick to signal remote processor
131 * @mbox_client: mailbox client handle
132 * @mbox_chan: apcs ipc mailbox channel handle
133 * @inbound: list of inbound entries
134 * @outbound: list of outbound entries
135 */
136struct qcom_smp2p {
137 struct device *dev;
138
139 struct smp2p_smem_item *in;
140 struct smp2p_smem_item *out;
141
142 unsigned smem_items[SMP2P_OUTBOUND + 1];
143
144 unsigned valid_entries;
145
146 bool ssr_ack_enabled;
147 bool ssr_ack;
148 bool negotiation_done;
149
150 unsigned local_pid;
151 unsigned remote_pid;
152
153 struct regmap *ipc_regmap;
154 int ipc_offset;
155 int ipc_bit;
156
157 struct mbox_client mbox_client;
158 struct mbox_chan *mbox_chan;
159
160 struct list_head inbound;
161 struct list_head outbound;
162};
163
164static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
165{
166 /* Make sure any updated data is written before the kick */
167 wmb();
168
169 if (smp2p->mbox_chan) {
170 mbox_send_message(smp2p->mbox_chan, NULL);
171 mbox_client_txdone(smp2p->mbox_chan, 0);
172 } else {
173 regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
174 }
175}
176
177static bool qcom_smp2p_check_ssr(struct qcom_smp2p *smp2p)
178{
179 struct smp2p_smem_item *in = smp2p->in;
180 bool restart;
181
182 if (!smp2p->ssr_ack_enabled)
183 return false;
184
185 restart = in->flags & BIT(SMP2P_FLAGS_RESTART_DONE_BIT);
186
187 return restart != smp2p->ssr_ack;
188}
189
190static void qcom_smp2p_do_ssr_ack(struct qcom_smp2p *smp2p)
191{
192 struct smp2p_smem_item *out = smp2p->out;
193 u32 val;
194
195 smp2p->ssr_ack = !smp2p->ssr_ack;
196
197 val = out->flags & ~BIT(SMP2P_FLAGS_RESTART_ACK_BIT);
198 if (smp2p->ssr_ack)
199 val |= BIT(SMP2P_FLAGS_RESTART_ACK_BIT);
200 out->flags = val;
201
202 qcom_smp2p_kick(smp2p);
203}
204
205static void qcom_smp2p_negotiate(struct qcom_smp2p *smp2p)
206{
207 struct smp2p_smem_item *out = smp2p->out;
208 struct smp2p_smem_item *in = smp2p->in;
209
210 if (in->version == out->version) {
211 out->features &= in->features;
212
213 if (out->features & SMP2P_FEATURE_SSR_ACK)
214 smp2p->ssr_ack_enabled = true;
215
216 smp2p->negotiation_done = true;
217 }
218}
219
220static void qcom_smp2p_notify_in(struct qcom_smp2p *smp2p)
221{
222 struct smp2p_smem_item *in;
223 struct smp2p_entry *entry;
224 int irq_pin;
225 u32 status;
226 char buf[SMP2P_MAX_ENTRY_NAME];
227 u32 val;
228 int i;
229
230 in = smp2p->in;
231
232 /* Match newly created entries */
233 for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
234 list_for_each_entry(entry, &smp2p->inbound, node) {
235 memcpy(buf, in->entries[i].name, sizeof(buf));
236 if (!strcmp(buf, entry->name)) {
237 entry->value = &in->entries[i].value;
238 break;
239 }
240 }
241 }
242 smp2p->valid_entries = i;
243
244 /* Fire interrupts based on any value changes */
245 list_for_each_entry(entry, &smp2p->inbound, node) {
246 /* Ignore entries not yet allocated by the remote side */
247 if (!entry->value)
248 continue;
249
250 val = readl(entry->value);
251
252 status = val ^ entry->last_value;
253 entry->last_value = val;
254
255 /* No changes of this entry? */
256 if (!status)
257 continue;
258
259 for_each_set_bit(i, entry->irq_enabled, 32) {
260 if (!(status & BIT(i)))
261 continue;
262
263 if ((val & BIT(i) && test_bit(i, entry->irq_rising)) ||
264 (!(val & BIT(i)) && test_bit(i, entry->irq_falling))) {
265 irq_pin = irq_find_mapping(entry->domain, i);
266 handle_nested_irq(irq_pin);
267 }
268 }
269 }
270}
271
272/**
273 * qcom_smp2p_intr() - interrupt handler for incoming notifications
274 * @irq: unused
275 * @data: smp2p driver context
276 *
277 * Handle notifications from the remote side to handle newly allocated entries
278 * or any changes to the state bits of existing entries.
279 *
280 * Return: %IRQ_HANDLED
281 */
282static irqreturn_t qcom_smp2p_intr(int irq, void *data)
283{
284 struct smp2p_smem_item *in;
285 struct qcom_smp2p *smp2p = data;
286 unsigned int smem_id = smp2p->smem_items[SMP2P_INBOUND];
287 unsigned int pid = smp2p->remote_pid;
288 bool ack_restart;
289 size_t size;
290
291 in = smp2p->in;
292
293 /* Acquire smem item, if not already found */
294 if (!in) {
295 in = qcom_smem_get(pid, smem_id, &size);
296 if (IS_ERR(in)) {
297 dev_err(smp2p->dev,
298 "Unable to acquire remote smp2p item\n");
299 goto out;
300 }
301
302 smp2p->in = in;
303 }
304
305 if (!smp2p->negotiation_done)
306 qcom_smp2p_negotiate(smp2p);
307
308 if (smp2p->negotiation_done) {
309 ack_restart = qcom_smp2p_check_ssr(smp2p);
310 qcom_smp2p_notify_in(smp2p);
311
312 if (ack_restart)
313 qcom_smp2p_do_ssr_ack(smp2p);
314 }
315
316out:
317 return IRQ_HANDLED;
318}
319
320static void smp2p_mask_irq(struct irq_data *irqd)
321{
322 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
323 irq_hw_number_t irq = irqd_to_hwirq(irqd);
324
325 clear_bit(irq, entry->irq_enabled);
326}
327
328static void smp2p_unmask_irq(struct irq_data *irqd)
329{
330 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
331 irq_hw_number_t irq = irqd_to_hwirq(irqd);
332
333 set_bit(irq, entry->irq_enabled);
334}
335
336static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
337{
338 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
339 irq_hw_number_t irq = irqd_to_hwirq(irqd);
340
341 if (!(type & IRQ_TYPE_EDGE_BOTH))
342 return -EINVAL;
343
344 if (type & IRQ_TYPE_EDGE_RISING)
345 set_bit(irq, entry->irq_rising);
346 else
347 clear_bit(irq, entry->irq_rising);
348
349 if (type & IRQ_TYPE_EDGE_FALLING)
350 set_bit(irq, entry->irq_falling);
351 else
352 clear_bit(irq, entry->irq_falling);
353
354 return 0;
355}
356
357static void smp2p_irq_print_chip(struct irq_data *irqd, struct seq_file *p)
358{
359 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
360
361 seq_printf(p, " %8s", dev_name(entry->smp2p->dev));
362}
363
364static struct irq_chip smp2p_irq_chip = {
365 .name = "smp2p",
366 .irq_mask = smp2p_mask_irq,
367 .irq_unmask = smp2p_unmask_irq,
368 .irq_set_type = smp2p_set_irq_type,
369 .irq_print_chip = smp2p_irq_print_chip,
370};
371
372static int smp2p_irq_map(struct irq_domain *d,
373 unsigned int irq,
374 irq_hw_number_t hw)
375{
376 struct smp2p_entry *entry = d->host_data;
377
378 irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
379 irq_set_chip_data(irq, entry);
380 irq_set_nested_thread(irq, 1);
381 irq_set_noprobe(irq);
382
383 return 0;
384}
385
386static const struct irq_domain_ops smp2p_irq_ops = {
387 .map = smp2p_irq_map,
388 .xlate = irq_domain_xlate_twocell,
389};
390
391static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
392 struct smp2p_entry *entry,
393 struct device_node *node)
394{
395 entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
396 if (!entry->domain) {
397 dev_err(smp2p->dev, "failed to add irq_domain\n");
398 return -ENOMEM;
399 }
400
401 return 0;
402}
403
404static int smp2p_update_bits(void *data, u32 mask, u32 value)
405{
406 struct smp2p_entry *entry = data;
407 unsigned long flags;
408 u32 orig;
409 u32 val;
410
411 spin_lock_irqsave(&entry->lock, flags);
412 val = orig = readl(entry->value);
413 val &= ~mask;
414 val |= value;
415 writel(val, entry->value);
416 spin_unlock_irqrestore(&entry->lock, flags);
417
418 if (val != orig)
419 qcom_smp2p_kick(entry->smp2p);
420
421 return 0;
422}
423
424static const struct qcom_smem_state_ops smp2p_state_ops = {
425 .update_bits = smp2p_update_bits,
426};
427
428static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
429 struct smp2p_entry *entry,
430 struct device_node *node)
431{
432 struct smp2p_smem_item *out = smp2p->out;
433 char buf[SMP2P_MAX_ENTRY_NAME] = {};
434
435 /* Allocate an entry from the smem item */
436 strscpy(buf, entry->name, SMP2P_MAX_ENTRY_NAME);
437 memcpy(out->entries[out->valid_entries].name, buf, SMP2P_MAX_ENTRY_NAME);
438
439 /* Make the logical entry reference the physical value */
440 entry->value = &out->entries[out->valid_entries].value;
441
442 out->valid_entries++;
443
444 entry->state = qcom_smem_state_register(node, &smp2p_state_ops, entry);
445 if (IS_ERR(entry->state)) {
446 dev_err(smp2p->dev, "failed to register qcom_smem_state\n");
447 return PTR_ERR(entry->state);
448 }
449
450 return 0;
451}
452
453static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
454{
455 struct smp2p_smem_item *out;
456 unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
457 unsigned pid = smp2p->remote_pid;
458 int ret;
459
460 ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
461 if (ret < 0 && ret != -EEXIST) {
462 if (ret != -EPROBE_DEFER)
463 dev_err(smp2p->dev,
464 "unable to allocate local smp2p item\n");
465 return ret;
466 }
467
468 out = qcom_smem_get(pid, smem_id, NULL);
469 if (IS_ERR(out)) {
470 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
471 return PTR_ERR(out);
472 }
473
474 memset(out, 0, sizeof(*out));
475 out->magic = SMP2P_MAGIC;
476 out->local_pid = smp2p->local_pid;
477 out->remote_pid = smp2p->remote_pid;
478 out->total_entries = SMP2P_MAX_ENTRY;
479 out->valid_entries = 0;
480 out->features = SMP2P_ALL_FEATURES;
481
482 /*
483 * Make sure the rest of the header is written before we validate the
484 * item by writing a valid version number.
485 */
486 wmb();
487 out->version = 1;
488
489 qcom_smp2p_kick(smp2p);
490
491 smp2p->out = out;
492
493 return 0;
494}
495
496static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
497{
498 struct device_node *syscon;
499 struct device *dev = smp2p->dev;
500 const char *key;
501 int ret;
502
503 syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
504 if (!syscon) {
505 dev_err(dev, "no qcom,ipc node\n");
506 return -ENODEV;
507 }
508
509 smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
510 of_node_put(syscon);
511 if (IS_ERR(smp2p->ipc_regmap))
512 return PTR_ERR(smp2p->ipc_regmap);
513
514 key = "qcom,ipc";
515 ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
516 if (ret < 0) {
517 dev_err(dev, "no offset in %s\n", key);
518 return -EINVAL;
519 }
520
521 ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
522 if (ret < 0) {
523 dev_err(dev, "no bit in %s\n", key);
524 return -EINVAL;
525 }
526
527 return 0;
528}
529
530static int qcom_smp2p_probe(struct platform_device *pdev)
531{
532 struct smp2p_entry *entry;
533 struct device_node *node;
534 struct qcom_smp2p *smp2p;
535 const char *key;
536 int irq;
537 int ret;
538
539 smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
540 if (!smp2p)
541 return -ENOMEM;
542
543 smp2p->dev = &pdev->dev;
544 INIT_LIST_HEAD(&smp2p->inbound);
545 INIT_LIST_HEAD(&smp2p->outbound);
546
547 platform_set_drvdata(pdev, smp2p);
548
549 key = "qcom,smem";
550 ret = of_property_read_u32_array(pdev->dev.of_node, key,
551 smp2p->smem_items, 2);
552 if (ret)
553 return ret;
554
555 key = "qcom,local-pid";
556 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
557 if (ret)
558 goto report_read_failure;
559
560 key = "qcom,remote-pid";
561 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
562 if (ret)
563 goto report_read_failure;
564
565 irq = platform_get_irq(pdev, 0);
566 if (irq < 0)
567 return irq;
568
569 smp2p->mbox_client.dev = &pdev->dev;
570 smp2p->mbox_client.knows_txdone = true;
571 smp2p->mbox_chan = mbox_request_channel(&smp2p->mbox_client, 0);
572 if (IS_ERR(smp2p->mbox_chan)) {
573 if (PTR_ERR(smp2p->mbox_chan) != -ENODEV)
574 return PTR_ERR(smp2p->mbox_chan);
575
576 smp2p->mbox_chan = NULL;
577
578 ret = smp2p_parse_ipc(smp2p);
579 if (ret)
580 return ret;
581 }
582
583 ret = qcom_smp2p_alloc_outbound_item(smp2p);
584 if (ret < 0)
585 goto release_mbox;
586
587 for_each_available_child_of_node(pdev->dev.of_node, node) {
588 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
589 if (!entry) {
590 ret = -ENOMEM;
591 of_node_put(node);
592 goto unwind_interfaces;
593 }
594
595 entry->smp2p = smp2p;
596 spin_lock_init(&entry->lock);
597
598 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
599 if (ret < 0) {
600 of_node_put(node);
601 goto unwind_interfaces;
602 }
603
604 if (of_property_read_bool(node, "interrupt-controller")) {
605 ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
606 if (ret < 0) {
607 of_node_put(node);
608 goto unwind_interfaces;
609 }
610
611 list_add(&entry->node, &smp2p->inbound);
612 } else {
613 ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
614 if (ret < 0) {
615 of_node_put(node);
616 goto unwind_interfaces;
617 }
618
619 list_add(&entry->node, &smp2p->outbound);
620 }
621 }
622
623 /* Kick the outgoing edge after allocating entries */
624 qcom_smp2p_kick(smp2p);
625
626 ret = devm_request_threaded_irq(&pdev->dev, irq,
627 NULL, qcom_smp2p_intr,
628 IRQF_ONESHOT,
629 NULL, (void *)smp2p);
630 if (ret) {
631 dev_err(&pdev->dev, "failed to request interrupt\n");
632 goto unwind_interfaces;
633 }
634
635 /*
636 * Treat smp2p interrupt as wakeup source, but keep it disabled
637 * by default. User space can decide enabling it depending on its
638 * use cases. For example if remoteproc crashes and device wants
639 * to handle it immediatedly (e.g. to not miss phone calls) it can
640 * enable wakeup source from user space, while other devices which
641 * do not have proper autosleep feature may want to handle it with
642 * other wakeup events (e.g. Power button) instead waking up immediately.
643 */
644 device_set_wakeup_capable(&pdev->dev, true);
645
646 ret = dev_pm_set_wake_irq(&pdev->dev, irq);
647 if (ret)
648 goto set_wake_irq_fail;
649
650 return 0;
651
652set_wake_irq_fail:
653 dev_pm_clear_wake_irq(&pdev->dev);
654
655unwind_interfaces:
656 list_for_each_entry(entry, &smp2p->inbound, node)
657 irq_domain_remove(entry->domain);
658
659 list_for_each_entry(entry, &smp2p->outbound, node)
660 qcom_smem_state_unregister(entry->state);
661
662 smp2p->out->valid_entries = 0;
663
664release_mbox:
665 mbox_free_channel(smp2p->mbox_chan);
666
667 return ret;
668
669report_read_failure:
670 dev_err(&pdev->dev, "failed to read %s\n", key);
671 return -EINVAL;
672}
673
674static void qcom_smp2p_remove(struct platform_device *pdev)
675{
676 struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
677 struct smp2p_entry *entry;
678
679 dev_pm_clear_wake_irq(&pdev->dev);
680
681 list_for_each_entry(entry, &smp2p->inbound, node)
682 irq_domain_remove(entry->domain);
683
684 list_for_each_entry(entry, &smp2p->outbound, node)
685 qcom_smem_state_unregister(entry->state);
686
687 mbox_free_channel(smp2p->mbox_chan);
688
689 smp2p->out->valid_entries = 0;
690}
691
692static const struct of_device_id qcom_smp2p_of_match[] = {
693 { .compatible = "qcom,smp2p" },
694 {}
695};
696MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
697
698static struct platform_driver qcom_smp2p_driver = {
699 .probe = qcom_smp2p_probe,
700 .remove_new = qcom_smp2p_remove,
701 .driver = {
702 .name = "qcom_smp2p",
703 .of_match_table = qcom_smp2p_of_match,
704 },
705};
706module_platform_driver(qcom_smp2p_driver);
707
708MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
709MODULE_LICENSE("GPL v2");