2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
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.
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.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/of_platform.h>
19 #include <linux/interrupt.h>
20 #include <linux/memblock.h>
21 #include <linux/slab.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
24 #include <linux/regmap.h>
25 #include <linux/list.h>
26 #include <linux/gpio.h>
27 #include <linux/mfd/syscon.h>
29 #include <linux/delay.h>
31 #include <linux/soc/qcom/smem.h>
34 * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
35 * of a single 32-bit value between two processors. Each value has a single
36 * writer (the local side) and a single reader (the remote side). Values are
37 * uniquely identified in the system by the directed edge (local processor ID
38 * to remote processor ID) and a string identifier.
40 * Each processor is responsible for creating the outgoing SMEM items and each
41 * item is writable by the local processor and readable by the remote
42 * processor. By using two separate SMEM items that are single-reader and
43 * single-writer, SMP2P does not require any remote locking mechanisms.
45 * The driver uses the Linux GPIO and interrupt framework to expose a virtual
46 * GPIO for each outbound entry and a virtual interrupt controller for each
50 #define SMP2P_MAX_ENTRY 16
51 #define SMP2P_MAX_ENTRY_NAME 16
53 #define SMP2P_FEATURE_SSR_ACK 0x1
55 #define SMP2P_MAGIC 0x504d5324
58 * struct smp2p_smem_item - in memory communication structure
59 * @magic: magic number
60 * @version: version - must be 1
61 * @features: features flag - currently unused
62 * @local_pid: processor id of sending end
63 * @remote_pid: processor id of receiving end
64 * @total_entries: number of entries - always SMP2P_MAX_ENTRY
65 * @valid_entries: number of allocated entries
67 * @entries: individual communication entries
68 * @name: name of the entry
69 * @value: content of the entry
71 struct smp2p_smem_item {
82 u8 name[SMP2P_MAX_ENTRY_NAME];
84 } entries[SMP2P_MAX_ENTRY];
88 * struct smp2p_entry - driver context matching one entry
89 * @node: list entry to keep track of allocated entries
90 * @smp2p: reference to the device driver context
91 * @name: name of the entry, to match against smp2p_smem_item
92 * @value: pointer to smp2p_smem_item entry value
93 * @last_value: last handled value
94 * @domain: irq_domain for inbound entries
95 * @irq_enabled:bitmap to track enabled irq bits
96 * @irq_rising: bitmap to mark irq bits for rising detection
97 * @irq_falling:bitmap to mark irq bits for falling detection
98 * @chip: gpio_chip for outbound entries
101 struct list_head node;
102 struct qcom_smp2p *smp2p;
108 struct irq_domain *domain;
109 DECLARE_BITMAP(irq_enabled, 32);
110 DECLARE_BITMAP(irq_rising, 32);
111 DECLARE_BITMAP(irq_falling, 32);
113 struct gpio_chip chip;
116 #define SMP2P_INBOUND 0
117 #define SMP2P_OUTBOUND 1
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
130 * @inbound: list of inbound entries
131 * @outbound: list of outbound entries
136 struct smp2p_smem_item *in;
137 struct smp2p_smem_item *out;
139 unsigned smem_items[SMP2P_OUTBOUND + 1];
141 unsigned valid_entries;
146 struct regmap *ipc_regmap;
150 struct list_head inbound;
151 struct list_head outbound;
154 static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
156 /* Make sure any updated data is written before the kick */
158 regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
161 static irqreturn_t qcom_smp2p_intr(int irq, void *data)
163 struct smp2p_smem_item *in;
164 struct smp2p_entry *entry;
165 struct qcom_smp2p *smp2p = data;
166 unsigned smem_id = smp2p->smem_items[SMP2P_INBOUND];
167 unsigned pid = smp2p->remote_pid;
174 u8 tmp_name[SMP2P_MAX_ENTRY_NAME];
178 /* Acquire smem item, if not already found */
180 ret = qcom_smem_get(pid, smem_id, (void **)&in, &size);
183 "Unable to acquire remote smp2p item\n");
190 /* Match newly created entries */
191 for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
192 list_for_each_entry(entry, &smp2p->inbound, node) {
193 memcpy_fromio(tmp_name, in->entries[i].name,
194 SMP2P_MAX_ENTRY_NAME);
195 if (!strcmp(tmp_name, entry->name)) {
196 entry->value = &in->entries[i].value;
201 smp2p->valid_entries = i;
203 /* Fire interrupts based on any value changes */
204 list_for_each_entry(entry, &smp2p->inbound, node) {
205 /* Ignore entries not yet allocated by the remote side */
211 status = val ^ entry->last_value;
212 entry->last_value = val;
214 /* No changes of this entry? */
218 for_each_set_bit(i, entry->irq_enabled, 32) {
219 if (!(status & BIT(i)))
223 if (test_bit(i, entry->irq_rising)) {
224 irq_pin = irq_find_mapping(entry->domain, i);
225 handle_nested_irq(irq_pin);
228 if (test_bit(i, entry->irq_falling)) {
229 irq_pin = irq_find_mapping(entry->domain, i);
230 handle_nested_irq(irq_pin);
240 static void smp2p_mask_irq(struct irq_data *irqd)
242 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
243 irq_hw_number_t irq = irqd_to_hwirq(irqd);
245 clear_bit(irq, entry->irq_enabled);
248 static void smp2p_unmask_irq(struct irq_data *irqd)
250 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
251 irq_hw_number_t irq = irqd_to_hwirq(irqd);
253 set_bit(irq, entry->irq_enabled);
256 static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
258 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
259 irq_hw_number_t irq = irqd_to_hwirq(irqd);
261 if (!(type & IRQ_TYPE_EDGE_BOTH))
264 if (type & IRQ_TYPE_EDGE_RISING)
265 set_bit(irq, entry->irq_rising);
267 clear_bit(irq, entry->irq_rising);
269 if (type & IRQ_TYPE_EDGE_FALLING)
270 set_bit(irq, entry->irq_falling);
272 clear_bit(irq, entry->irq_falling);
277 static struct irq_chip smp2p_irq_chip = {
279 .irq_mask = smp2p_mask_irq,
280 .irq_unmask = smp2p_unmask_irq,
281 .irq_set_type = smp2p_set_irq_type,
284 static int smp2p_irq_map(struct irq_domain *d,
288 struct smp2p_entry *entry = d->host_data;
290 irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
291 irq_set_chip_data(irq, entry);
292 irq_set_nested_thread(irq, 1);
294 #if defined(CONFIG_ARM64) || defined(CONFIG_ARM)
295 set_irq_flags(irq, IRQF_VALID);
297 irq_set_noprobe(virq);
303 static const struct irq_domain_ops smp2p_irq_ops = {
304 .map = smp2p_irq_map,
305 .xlate = irq_domain_xlate_twocell,
308 static int smp2p_gpio_direction_output(struct gpio_chip *chip,
312 struct smp2p_entry *entry = container_of(chip, struct smp2p_entry, chip);
315 *entry->value |= BIT(offset);
317 *entry->value &= ~BIT(offset);
319 qcom_smp2p_kick(entry->smp2p);
324 static void smp2p_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
326 smp2p_gpio_direction_output(chip, offset, value);
329 static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
330 struct smp2p_entry *entry,
331 struct device_node *node)
333 entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
334 if (!entry->domain) {
335 dev_err(smp2p->dev, "failed to add irq_domain\n");
342 static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
343 struct smp2p_entry *entry,
344 struct device_node *node)
346 struct smp2p_smem_item *out = smp2p->out;
347 struct gpio_chip *chip;
350 /* Allocate an entry from the smem item */
351 memcpy_toio(out->entries[out->valid_entries].name, entry->name, SMP2P_MAX_ENTRY_NAME);
352 out->valid_entries++;
354 /* Make the logical entry reference the physical value */
355 entry->value = &out->entries[out->valid_entries].value;
360 chip->label = entry->name;
361 chip->dev = smp2p->dev;
362 chip->owner = THIS_MODULE;
363 chip->of_node = node;
365 chip->set = smp2p_gpio_set;
366 chip->direction_output = smp2p_gpio_direction_output;
368 ret = gpiochip_add(chip);
370 dev_err(smp2p->dev, "failed register gpiochip\n");
375 static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
377 struct smp2p_smem_item *out;
378 unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
379 unsigned pid = smp2p->remote_pid;
382 ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
383 if (ret < 0 && ret != -EEXIST) {
384 if (ret != -EPROBE_DEFER)
386 "unable to allocate local smp2p item\n");
390 ret = qcom_smem_get(pid, smem_id, (void **)&out, NULL);
392 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
396 memset_io(out, 0, sizeof(*out));
397 out->magic = SMP2P_MAGIC;
398 out->local_pid = smp2p->local_pid;
399 out->remote_pid = smp2p->remote_pid;
400 out->total_entries = SMP2P_MAX_ENTRY;
401 out->valid_entries = 0;
404 * Make sure the rest of the header is written before we validate the
405 * item by writing a valid version number.
410 qcom_smp2p_kick(smp2p);
417 static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
419 struct device_node *syscon;
420 struct device *dev = smp2p->dev;
424 syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
426 dev_err(dev, "no qcom,ipc node\n");
430 smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
431 if (IS_ERR(smp2p->ipc_regmap))
432 return PTR_ERR(smp2p->ipc_regmap);
435 ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
437 dev_err(dev, "no offset in %s\n", key);
441 ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
443 dev_err(dev, "no bit in %s\n", key);
450 static int qcom_smp2p_probe(struct platform_device *pdev)
452 struct smp2p_entry *entry;
453 struct device_node *node;
454 struct qcom_smp2p *smp2p;
459 smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
463 smp2p->dev = &pdev->dev;
464 INIT_LIST_HEAD(&smp2p->inbound);
465 INIT_LIST_HEAD(&smp2p->outbound);
467 platform_set_drvdata(pdev, smp2p);
469 ret = smp2p_parse_ipc(smp2p);
474 ret = of_property_read_u32_array(pdev->dev.of_node, key,
475 smp2p->smem_items, 2);
479 key = "qcom,local-pid";
480 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
482 dev_err(&pdev->dev, "failed to read %s\n", key);
486 key = "qcom,remote-pid";
487 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
489 dev_err(&pdev->dev, "failed to read %s\n", key);
493 irq = platform_get_irq(pdev, 0);
495 dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
499 ret = qcom_smp2p_alloc_outbound_item(smp2p);
503 for_each_available_child_of_node(pdev->dev.of_node, node) {
504 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
507 goto unwind_interfaces;
510 entry->smp2p = smp2p;
512 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
514 goto unwind_interfaces;
516 if (of_property_read_bool(node, "qcom,inbound")) {
517 ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
519 goto unwind_interfaces;
521 list_add(&entry->node, &smp2p->inbound);
522 } else if (of_property_read_bool(node, "qcom,outbound")) {
523 ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
525 goto unwind_interfaces;
527 list_add(&entry->node, &smp2p->outbound);
529 dev_err(&pdev->dev, "neither inbound nor outbound\n");
531 goto unwind_interfaces;
535 /* Kick the outgoing edge after allocating entries */
536 qcom_smp2p_kick(smp2p);
538 ret = devm_request_threaded_irq(&pdev->dev, irq,
539 NULL, qcom_smp2p_intr,
541 "smp2p", (void *)smp2p);
543 dev_err(&pdev->dev, "failed to request interrupt\n");
544 goto unwind_interfaces;
551 list_for_each_entry(entry, &smp2p->inbound, node)
552 irq_domain_remove(entry->domain);
554 list_for_each_entry(entry, &smp2p->outbound, node)
555 gpiochip_remove(&entry->chip);
557 smp2p->out->valid_entries = 0;
562 static int qcom_smp2p_remove(struct platform_device *pdev)
564 struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
565 struct smp2p_entry *entry;
567 list_for_each_entry(entry, &smp2p->inbound, node)
568 irq_domain_remove(entry->domain);
570 list_for_each_entry(entry, &smp2p->outbound, node)
571 gpiochip_remove(&entry->chip);
573 smp2p->out->valid_entries = 0;
578 static const struct of_device_id qcom_smp2p_of_match[] = {
579 { .compatible = "qcom,smp2p" },
582 MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
584 static struct platform_driver qcom_smp2p_driver = {
585 .probe = qcom_smp2p_probe,
586 .remove = qcom_smp2p_remove,
588 .name = "qcom_smp2p",
589 .of_match_table = qcom_smp2p_of_match,
592 module_platform_driver(qcom_smp2p_driver);
594 MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
595 MODULE_LICENSE("GPL v2");