blob: c22503cd1edf9388d32401f1c472ab1992324b7b [file] [log] [blame]
Bjorn Andersson50e99642015-09-24 18:25:03 -07001/*
2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/interrupt.h>
16#include <linux/list.h>
17#include <linux/io.h>
18#include <linux/of.h>
19#include <linux/irq.h>
20#include <linux/irqdomain.h>
Bjorn Anderssone7180262017-11-29 16:00:40 -080021#include <linux/mailbox_client.h>
Bjorn Andersson50e99642015-09-24 18:25:03 -070022#include <linux/mfd/syscon.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/regmap.h>
26#include <linux/soc/qcom/smem.h>
27#include <linux/soc/qcom/smem_state.h>
28#include <linux/spinlock.h>
29
30/*
31 * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
32 * of a single 32-bit value between two processors. Each value has a single
33 * writer (the local side) and a single reader (the remote side). Values are
34 * uniquely identified in the system by the directed edge (local processor ID
35 * to remote processor ID) and a string identifier.
36 *
37 * Each processor is responsible for creating the outgoing SMEM items and each
38 * item is writable by the local processor and readable by the remote
39 * processor. By using two separate SMEM items that are single-reader and
40 * single-writer, SMP2P does not require any remote locking mechanisms.
41 *
42 * The driver uses the Linux GPIO and interrupt framework to expose a virtual
43 * GPIO for each outbound entry and a virtual interrupt controller for each
44 * inbound entry.
45 */
46
47#define SMP2P_MAX_ENTRY 16
48#define SMP2P_MAX_ENTRY_NAME 16
49
50#define SMP2P_FEATURE_SSR_ACK 0x1
51
52#define SMP2P_MAGIC 0x504d5324
53
54/**
55 * struct smp2p_smem_item - in memory communication structure
56 * @magic: magic number
57 * @version: version - must be 1
58 * @features: features flag - currently unused
59 * @local_pid: processor id of sending end
60 * @remote_pid: processor id of receiving end
61 * @total_entries: number of entries - always SMP2P_MAX_ENTRY
62 * @valid_entries: number of allocated entries
63 * @flags:
64 * @entries: individual communication entries
65 * @name: name of the entry
66 * @value: content of the entry
67 */
68struct smp2p_smem_item {
69 u32 magic;
70 u8 version;
71 unsigned features:24;
72 u16 local_pid;
73 u16 remote_pid;
74 u16 total_entries;
75 u16 valid_entries;
76 u32 flags;
77
78 struct {
79 u8 name[SMP2P_MAX_ENTRY_NAME];
80 u32 value;
81 } entries[SMP2P_MAX_ENTRY];
82} __packed;
83
84/**
85 * struct smp2p_entry - driver context matching one entry
86 * @node: list entry to keep track of allocated entries
87 * @smp2p: reference to the device driver context
88 * @name: name of the entry, to match against smp2p_smem_item
89 * @value: pointer to smp2p_smem_item entry value
90 * @last_value: last handled value
91 * @domain: irq_domain for inbound entries
92 * @irq_enabled:bitmap to track enabled irq bits
93 * @irq_rising: bitmap to mark irq bits for rising detection
94 * @irq_falling:bitmap to mark irq bits for falling detection
95 * @state: smem state handle
96 * @lock: spinlock to protect read-modify-write of the value
97 */
98struct smp2p_entry {
99 struct list_head node;
100 struct qcom_smp2p *smp2p;
101
102 const char *name;
103 u32 *value;
104 u32 last_value;
105
106 struct irq_domain *domain;
107 DECLARE_BITMAP(irq_enabled, 32);
108 DECLARE_BITMAP(irq_rising, 32);
109 DECLARE_BITMAP(irq_falling, 32);
110
111 struct qcom_smem_state *state;
112
113 spinlock_t lock;
114};
115
116#define SMP2P_INBOUND 0
117#define SMP2P_OUTBOUND 1
118
119/**
120 * struct qcom_smp2p - device driver context
121 * @dev: device driver handle
122 * @in: pointer to the inbound smem item
123 * @smem_items: ids of the two smem items
124 * @valid_entries: already scanned inbound entries
125 * @local_pid: processor id of the inbound edge
126 * @remote_pid: processor id of the outbound edge
127 * @ipc_regmap: regmap for the outbound ipc
128 * @ipc_offset: offset within the regmap
129 * @ipc_bit: bit in regmap@offset to kick to signal remote processor
Bjorn Anderssone7180262017-11-29 16:00:40 -0800130 * @mbox_client: mailbox client handle
131 * @mbox_chan: apcs ipc mailbox channel handle
Bjorn Andersson50e99642015-09-24 18:25:03 -0700132 * @inbound: list of inbound entries
133 * @outbound: list of outbound entries
134 */
135struct qcom_smp2p {
136 struct device *dev;
137
138 struct smp2p_smem_item *in;
139 struct smp2p_smem_item *out;
140
141 unsigned smem_items[SMP2P_OUTBOUND + 1];
142
143 unsigned valid_entries;
144
145 unsigned local_pid;
146 unsigned remote_pid;
147
148 struct regmap *ipc_regmap;
149 int ipc_offset;
150 int ipc_bit;
151
Bjorn Anderssone7180262017-11-29 16:00:40 -0800152 struct mbox_client mbox_client;
153 struct mbox_chan *mbox_chan;
154
Bjorn Andersson50e99642015-09-24 18:25:03 -0700155 struct list_head inbound;
156 struct list_head outbound;
157};
158
159static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
160{
161 /* Make sure any updated data is written before the kick */
162 wmb();
Bjorn Anderssone7180262017-11-29 16:00:40 -0800163
164 if (smp2p->mbox_chan) {
165 mbox_send_message(smp2p->mbox_chan, NULL);
166 mbox_client_txdone(smp2p->mbox_chan, 0);
167 } else {
168 regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
169 }
Bjorn Andersson50e99642015-09-24 18:25:03 -0700170}
171
172/**
173 * qcom_smp2p_intr() - interrupt handler for incoming notifications
174 * @irq: unused
175 * @data: smp2p driver context
176 *
177 * Handle notifications from the remote side to handle newly allocated entries
178 * or any changes to the state bits of existing entries.
179 */
180static irqreturn_t qcom_smp2p_intr(int irq, void *data)
181{
182 struct smp2p_smem_item *in;
183 struct smp2p_entry *entry;
184 struct qcom_smp2p *smp2p = data;
185 unsigned smem_id = smp2p->smem_items[SMP2P_INBOUND];
186 unsigned pid = smp2p->remote_pid;
187 size_t size;
188 int irq_pin;
189 u32 status;
190 char buf[SMP2P_MAX_ENTRY_NAME];
191 u32 val;
192 int i;
193
194 in = smp2p->in;
195
196 /* Acquire smem item, if not already found */
197 if (!in) {
198 in = qcom_smem_get(pid, smem_id, &size);
199 if (IS_ERR(in)) {
200 dev_err(smp2p->dev,
201 "Unable to acquire remote smp2p item\n");
202 return IRQ_HANDLED;
203 }
204
205 smp2p->in = in;
206 }
207
208 /* Match newly created entries */
209 for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
210 list_for_each_entry(entry, &smp2p->inbound, node) {
Bjorn Anderssone7306dd2016-06-09 17:22:57 -0700211 memcpy(buf, in->entries[i].name, sizeof(buf));
Bjorn Andersson50e99642015-09-24 18:25:03 -0700212 if (!strcmp(buf, entry->name)) {
213 entry->value = &in->entries[i].value;
214 break;
215 }
216 }
217 }
218 smp2p->valid_entries = i;
219
220 /* Fire interrupts based on any value changes */
221 list_for_each_entry(entry, &smp2p->inbound, node) {
222 /* Ignore entries not yet allocated by the remote side */
223 if (!entry->value)
224 continue;
225
226 val = readl(entry->value);
227
228 status = val ^ entry->last_value;
229 entry->last_value = val;
230
231 /* No changes of this entry? */
232 if (!status)
233 continue;
234
235 for_each_set_bit(i, entry->irq_enabled, 32) {
236 if (!(status & BIT(i)))
237 continue;
238
239 if ((val & BIT(i) && test_bit(i, entry->irq_rising)) ||
240 (!(val & BIT(i)) && test_bit(i, entry->irq_falling))) {
241 irq_pin = irq_find_mapping(entry->domain, i);
242 handle_nested_irq(irq_pin);
243 }
244 }
245 }
246
247 return IRQ_HANDLED;
248}
249
250static void smp2p_mask_irq(struct irq_data *irqd)
251{
252 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
253 irq_hw_number_t irq = irqd_to_hwirq(irqd);
254
255 clear_bit(irq, entry->irq_enabled);
256}
257
258static void smp2p_unmask_irq(struct irq_data *irqd)
259{
260 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
261 irq_hw_number_t irq = irqd_to_hwirq(irqd);
262
263 set_bit(irq, entry->irq_enabled);
264}
265
266static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
267{
268 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
269 irq_hw_number_t irq = irqd_to_hwirq(irqd);
270
271 if (!(type & IRQ_TYPE_EDGE_BOTH))
272 return -EINVAL;
273
274 if (type & IRQ_TYPE_EDGE_RISING)
275 set_bit(irq, entry->irq_rising);
276 else
277 clear_bit(irq, entry->irq_rising);
278
279 if (type & IRQ_TYPE_EDGE_FALLING)
280 set_bit(irq, entry->irq_falling);
281 else
282 clear_bit(irq, entry->irq_falling);
283
284 return 0;
285}
286
287static struct irq_chip smp2p_irq_chip = {
288 .name = "smp2p",
289 .irq_mask = smp2p_mask_irq,
290 .irq_unmask = smp2p_unmask_irq,
291 .irq_set_type = smp2p_set_irq_type,
292};
293
294static int smp2p_irq_map(struct irq_domain *d,
295 unsigned int irq,
296 irq_hw_number_t hw)
297{
298 struct smp2p_entry *entry = d->host_data;
299
300 irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
301 irq_set_chip_data(irq, entry);
302 irq_set_nested_thread(irq, 1);
303 irq_set_noprobe(irq);
304
305 return 0;
306}
307
308static const struct irq_domain_ops smp2p_irq_ops = {
309 .map = smp2p_irq_map,
310 .xlate = irq_domain_xlate_twocell,
311};
312
313static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
314 struct smp2p_entry *entry,
315 struct device_node *node)
316{
317 entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
318 if (!entry->domain) {
319 dev_err(smp2p->dev, "failed to add irq_domain\n");
320 return -ENOMEM;
321 }
322
323 return 0;
324}
325
326static int smp2p_update_bits(void *data, u32 mask, u32 value)
327{
328 struct smp2p_entry *entry = data;
329 u32 orig;
330 u32 val;
331
332 spin_lock(&entry->lock);
333 val = orig = readl(entry->value);
334 val &= ~mask;
335 val |= value;
336 writel(val, entry->value);
337 spin_unlock(&entry->lock);
338
339 if (val != orig)
340 qcom_smp2p_kick(entry->smp2p);
341
342 return 0;
343}
344
345static const struct qcom_smem_state_ops smp2p_state_ops = {
346 .update_bits = smp2p_update_bits,
347};
348
349static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
350 struct smp2p_entry *entry,
351 struct device_node *node)
352{
353 struct smp2p_smem_item *out = smp2p->out;
354 char buf[SMP2P_MAX_ENTRY_NAME] = {};
355
356 /* Allocate an entry from the smem item */
357 strlcpy(buf, entry->name, SMP2P_MAX_ENTRY_NAME);
Bjorn Anderssone7306dd2016-06-09 17:22:57 -0700358 memcpy(out->entries[out->valid_entries].name, buf, SMP2P_MAX_ENTRY_NAME);
Bjorn Andersson50e99642015-09-24 18:25:03 -0700359
360 /* Make the logical entry reference the physical value */
361 entry->value = &out->entries[out->valid_entries].value;
362
Bjorn Andersson63af8e42016-06-09 17:22:56 -0700363 out->valid_entries++;
364
Bjorn Andersson50e99642015-09-24 18:25:03 -0700365 entry->state = qcom_smem_state_register(node, &smp2p_state_ops, entry);
366 if (IS_ERR(entry->state)) {
367 dev_err(smp2p->dev, "failed to register qcom_smem_state\n");
368 return PTR_ERR(entry->state);
369 }
370
371 return 0;
372}
373
374static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
375{
376 struct smp2p_smem_item *out;
377 unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
378 unsigned pid = smp2p->remote_pid;
379 int ret;
380
381 ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
382 if (ret < 0 && ret != -EEXIST) {
383 if (ret != -EPROBE_DEFER)
384 dev_err(smp2p->dev,
385 "unable to allocate local smp2p item\n");
386 return ret;
387 }
388
389 out = qcom_smem_get(pid, smem_id, NULL);
390 if (IS_ERR(out)) {
391 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
392 return PTR_ERR(out);
393 }
394
395 memset(out, 0, sizeof(*out));
396 out->magic = SMP2P_MAGIC;
397 out->local_pid = smp2p->local_pid;
398 out->remote_pid = smp2p->remote_pid;
399 out->total_entries = SMP2P_MAX_ENTRY;
400 out->valid_entries = 0;
401
402 /*
403 * Make sure the rest of the header is written before we validate the
404 * item by writing a valid version number.
405 */
406 wmb();
407 out->version = 1;
408
409 qcom_smp2p_kick(smp2p);
410
411 smp2p->out = out;
412
413 return 0;
414}
415
416static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
417{
418 struct device_node *syscon;
419 struct device *dev = smp2p->dev;
420 const char *key;
421 int ret;
422
423 syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
424 if (!syscon) {
425 dev_err(dev, "no qcom,ipc node\n");
426 return -ENODEV;
427 }
428
429 smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
430 if (IS_ERR(smp2p->ipc_regmap))
431 return PTR_ERR(smp2p->ipc_regmap);
432
433 key = "qcom,ipc";
434 ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
435 if (ret < 0) {
436 dev_err(dev, "no offset in %s\n", key);
437 return -EINVAL;
438 }
439
440 ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
441 if (ret < 0) {
442 dev_err(dev, "no bit in %s\n", key);
443 return -EINVAL;
444 }
445
446 return 0;
447}
448
449static int qcom_smp2p_probe(struct platform_device *pdev)
450{
451 struct smp2p_entry *entry;
452 struct device_node *node;
453 struct qcom_smp2p *smp2p;
454 const char *key;
455 int irq;
456 int ret;
457
458 smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
459 if (!smp2p)
460 return -ENOMEM;
461
462 smp2p->dev = &pdev->dev;
463 INIT_LIST_HEAD(&smp2p->inbound);
464 INIT_LIST_HEAD(&smp2p->outbound);
465
466 platform_set_drvdata(pdev, smp2p);
467
Bjorn Andersson50e99642015-09-24 18:25:03 -0700468 key = "qcom,smem";
469 ret = of_property_read_u32_array(pdev->dev.of_node, key,
470 smp2p->smem_items, 2);
471 if (ret)
472 return ret;
473
474 key = "qcom,local-pid";
475 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
Markus Elfringc01fc222017-11-02 19:27:33 +0100476 if (ret)
477 goto report_read_failure;
Bjorn Andersson50e99642015-09-24 18:25:03 -0700478
479 key = "qcom,remote-pid";
480 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
Markus Elfringc01fc222017-11-02 19:27:33 +0100481 if (ret)
482 goto report_read_failure;
Bjorn Andersson50e99642015-09-24 18:25:03 -0700483
484 irq = platform_get_irq(pdev, 0);
485 if (irq < 0) {
486 dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
487 return irq;
488 }
489
Bjorn Anderssone7180262017-11-29 16:00:40 -0800490 smp2p->mbox_client.dev = &pdev->dev;
491 smp2p->mbox_client.knows_txdone = true;
492 smp2p->mbox_chan = mbox_request_channel(&smp2p->mbox_client, 0);
493 if (IS_ERR(smp2p->mbox_chan)) {
494 if (PTR_ERR(smp2p->mbox_chan) != -ENODEV)
495 return PTR_ERR(smp2p->mbox_chan);
496
497 smp2p->mbox_chan = NULL;
498
499 ret = smp2p_parse_ipc(smp2p);
500 if (ret)
501 return ret;
502 }
503
Bjorn Andersson50e99642015-09-24 18:25:03 -0700504 ret = qcom_smp2p_alloc_outbound_item(smp2p);
505 if (ret < 0)
Bjorn Anderssone7180262017-11-29 16:00:40 -0800506 goto release_mbox;
Bjorn Andersson50e99642015-09-24 18:25:03 -0700507
508 for_each_available_child_of_node(pdev->dev.of_node, node) {
509 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
510 if (!entry) {
511 ret = -ENOMEM;
512 goto unwind_interfaces;
513 }
514
515 entry->smp2p = smp2p;
516 spin_lock_init(&entry->lock);
517
518 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
519 if (ret < 0)
520 goto unwind_interfaces;
521
522 if (of_property_read_bool(node, "interrupt-controller")) {
523 ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
524 if (ret < 0)
525 goto unwind_interfaces;
526
527 list_add(&entry->node, &smp2p->inbound);
528 } else {
529 ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
530 if (ret < 0)
531 goto unwind_interfaces;
532
533 list_add(&entry->node, &smp2p->outbound);
534 }
535 }
536
537 /* Kick the outgoing edge after allocating entries */
538 qcom_smp2p_kick(smp2p);
539
540 ret = devm_request_threaded_irq(&pdev->dev, irq,
541 NULL, qcom_smp2p_intr,
542 IRQF_ONESHOT,
543 "smp2p", (void *)smp2p);
544 if (ret) {
545 dev_err(&pdev->dev, "failed to request interrupt\n");
546 goto unwind_interfaces;
547 }
548
549
550 return 0;
551
552unwind_interfaces:
553 list_for_each_entry(entry, &smp2p->inbound, node)
554 irq_domain_remove(entry->domain);
555
556 list_for_each_entry(entry, &smp2p->outbound, node)
557 qcom_smem_state_unregister(entry->state);
558
559 smp2p->out->valid_entries = 0;
560
Bjorn Anderssone7180262017-11-29 16:00:40 -0800561release_mbox:
562 mbox_free_channel(smp2p->mbox_chan);
563
Bjorn Andersson50e99642015-09-24 18:25:03 -0700564 return ret;
Markus Elfringc01fc222017-11-02 19:27:33 +0100565
566report_read_failure:
567 dev_err(&pdev->dev, "failed to read %s\n", key);
568 return -EINVAL;
Bjorn Andersson50e99642015-09-24 18:25:03 -0700569}
570
571static int qcom_smp2p_remove(struct platform_device *pdev)
572{
573 struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
574 struct smp2p_entry *entry;
575
576 list_for_each_entry(entry, &smp2p->inbound, node)
577 irq_domain_remove(entry->domain);
578
579 list_for_each_entry(entry, &smp2p->outbound, node)
580 qcom_smem_state_unregister(entry->state);
581
Bjorn Anderssone7180262017-11-29 16:00:40 -0800582 mbox_free_channel(smp2p->mbox_chan);
583
Bjorn Andersson50e99642015-09-24 18:25:03 -0700584 smp2p->out->valid_entries = 0;
585
586 return 0;
587}
588
589static const struct of_device_id qcom_smp2p_of_match[] = {
590 { .compatible = "qcom,smp2p" },
591 {}
592};
593MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
594
595static struct platform_driver qcom_smp2p_driver = {
596 .probe = qcom_smp2p_probe,
597 .remove = qcom_smp2p_remove,
598 .driver = {
599 .name = "qcom_smp2p",
600 .of_match_table = qcom_smp2p_of_match,
601 },
602};
603module_platform_driver(qcom_smp2p_driver);
604
605MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
606MODULE_LICENSE("GPL v2");