GNU Linux-libre 4.9.294-gnu1
[releases.git] / drivers / soc / qcom / smsm.c
1 /*
2  * Copyright (c) 2015, Sony Mobile Communications Inc.
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/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
20 #include <linux/spinlock.h>
21 #include <linux/regmap.h>
22 #include <linux/soc/qcom/smem.h>
23 #include <linux/soc/qcom/smem_state.h>
24
25 /*
26  * This driver implements the Qualcomm Shared Memory State Machine, a mechanism
27  * for communicating single bit state information to remote processors.
28  *
29  * The implementation is based on two sections of shared memory; the first
30  * holding the state bits and the second holding a matrix of subscription bits.
31  *
32  * The state bits are structured in entries of 32 bits, each belonging to one
33  * system in the SoC. The entry belonging to the local system is considered
34  * read-write, while the rest should be considered read-only.
35  *
36  * The subscription matrix consists of N bitmaps per entry, denoting interest
37  * in updates of the entry for each of the N hosts. Upon updating a state bit
38  * each host's subscription bitmap should be queried and the remote system
39  * should be interrupted if they request so.
40  *
41  * The subscription matrix is laid out in entry-major order:
42  * entry0: [host0 ... hostN]
43  *      .
44  *      .
45  * entryM: [host0 ... hostN]
46  *
47  * A third, optional, shared memory region might contain information regarding
48  * the number of entries in the state bitmap as well as number of columns in
49  * the subscription matrix.
50  */
51
52 /*
53  * Shared memory identifiers, used to acquire handles to respective memory
54  * region.
55  */
56 #define SMEM_SMSM_SHARED_STATE          85
57 #define SMEM_SMSM_CPU_INTR_MASK         333
58 #define SMEM_SMSM_SIZE_INFO             419
59
60 /*
61  * Default sizes, in case SMEM_SMSM_SIZE_INFO is not found.
62  */
63 #define SMSM_DEFAULT_NUM_ENTRIES        8
64 #define SMSM_DEFAULT_NUM_HOSTS          3
65
66 struct smsm_entry;
67 struct smsm_host;
68
69 /**
70  * struct qcom_smsm - smsm driver context
71  * @dev:        smsm device pointer
72  * @local_host: column in the subscription matrix representing this system
73  * @num_hosts:  number of columns in the subscription matrix
74  * @num_entries: number of entries in the state map and rows in the subscription
75  *              matrix
76  * @local_state: pointer to the local processor's state bits
77  * @subscription: pointer to local processor's row in subscription matrix
78  * @state:      smem state handle
79  * @lock:       spinlock for read-modify-write of the outgoing state
80  * @entries:    context for each of the entries
81  * @hosts:      context for each of the hosts
82  */
83 struct qcom_smsm {
84         struct device *dev;
85
86         u32 local_host;
87
88         u32 num_hosts;
89         u32 num_entries;
90
91         u32 *local_state;
92         u32 *subscription;
93         struct qcom_smem_state *state;
94
95         spinlock_t lock;
96
97         struct smsm_entry *entries;
98         struct smsm_host *hosts;
99 };
100
101 /**
102  * struct smsm_entry - per remote processor entry context
103  * @smsm:       back-reference to driver context
104  * @domain:     IRQ domain for this entry, if representing a remote system
105  * @irq_enabled: bitmap of which state bits IRQs are enabled
106  * @irq_rising: bitmap tracking if rising bits should be propagated
107  * @irq_falling: bitmap tracking if falling bits should be propagated
108  * @last_value: snapshot of state bits last time the interrupts where propagated
109  * @remote_state: pointer to this entry's state bits
110  * @subscription: pointer to a row in the subscription matrix representing this
111  *              entry
112  */
113 struct smsm_entry {
114         struct qcom_smsm *smsm;
115
116         struct irq_domain *domain;
117         DECLARE_BITMAP(irq_enabled, 32);
118         DECLARE_BITMAP(irq_rising, 32);
119         DECLARE_BITMAP(irq_falling, 32);
120         unsigned long last_value;
121
122         u32 *remote_state;
123         u32 *subscription;
124 };
125
126 /**
127  * struct smsm_host - representation of a remote host
128  * @ipc_regmap: regmap for outgoing interrupt
129  * @ipc_offset: offset in @ipc_regmap for outgoing interrupt
130  * @ipc_bit:    bit in @ipc_regmap + @ipc_offset for outgoing interrupt
131  */
132 struct smsm_host {
133         struct regmap *ipc_regmap;
134         int ipc_offset;
135         int ipc_bit;
136 };
137
138 /**
139  * smsm_update_bits() - change bit in outgoing entry and inform subscribers
140  * @data:       smsm context pointer
141  * @offset:     bit in the entry
142  * @value:      new value
143  *
144  * Used to set and clear the bits in the outgoing/local entry and inform
145  * subscribers about the change.
146  */
147 static int smsm_update_bits(void *data, u32 mask, u32 value)
148 {
149         struct qcom_smsm *smsm = data;
150         struct smsm_host *hostp;
151         unsigned long flags;
152         u32 changes;
153         u32 host;
154         u32 orig;
155         u32 val;
156
157         spin_lock_irqsave(&smsm->lock, flags);
158
159         /* Update the entry */
160         val = orig = readl(smsm->local_state);
161         val &= ~mask;
162         val |= value;
163
164         /* Don't signal if we didn't change the value */
165         changes = val ^ orig;
166         if (!changes) {
167                 spin_unlock_irqrestore(&smsm->lock, flags);
168                 goto done;
169         }
170
171         /* Write out the new value */
172         writel(val, smsm->local_state);
173         spin_unlock_irqrestore(&smsm->lock, flags);
174
175         /* Make sure the value update is ordered before any kicks */
176         wmb();
177
178         /* Iterate over all hosts to check whom wants a kick */
179         for (host = 0; host < smsm->num_hosts; host++) {
180                 hostp = &smsm->hosts[host];
181
182                 val = readl(smsm->subscription + host);
183                 if (val & changes && hostp->ipc_regmap) {
184                         regmap_write(hostp->ipc_regmap,
185                                      hostp->ipc_offset,
186                                      BIT(hostp->ipc_bit));
187                 }
188         }
189
190 done:
191         return 0;
192 }
193
194 static const struct qcom_smem_state_ops smsm_state_ops = {
195         .update_bits = smsm_update_bits,
196 };
197
198 /**
199  * smsm_intr() - cascading IRQ handler for SMSM
200  * @irq:        unused
201  * @data:       entry related to this IRQ
202  *
203  * This function cascades an incoming interrupt from a remote system, based on
204  * the state bits and configuration.
205  */
206 static irqreturn_t smsm_intr(int irq, void *data)
207 {
208         struct smsm_entry *entry = data;
209         unsigned i;
210         int irq_pin;
211         u32 changed;
212         u32 val;
213
214         val = readl(entry->remote_state);
215         changed = val ^ xchg(&entry->last_value, val);
216
217         for_each_set_bit(i, entry->irq_enabled, 32) {
218                 if (!(changed & BIT(i)))
219                         continue;
220
221                 if (val & BIT(i)) {
222                         if (test_bit(i, entry->irq_rising)) {
223                                 irq_pin = irq_find_mapping(entry->domain, i);
224                                 handle_nested_irq(irq_pin);
225                         }
226                 } else {
227                         if (test_bit(i, entry->irq_falling)) {
228                                 irq_pin = irq_find_mapping(entry->domain, i);
229                                 handle_nested_irq(irq_pin);
230                         }
231                 }
232         }
233
234         return IRQ_HANDLED;
235 }
236
237 /**
238  * smsm_mask_irq() - un-subscribe from cascades of IRQs of a certain staus bit
239  * @irqd:       IRQ handle to be masked
240  *
241  * This un-subscribes the local CPU from interrupts upon changes to the defines
242  * status bit. The bit is also cleared from cascading.
243  */
244 static void smsm_mask_irq(struct irq_data *irqd)
245 {
246         struct smsm_entry *entry = irq_data_get_irq_chip_data(irqd);
247         irq_hw_number_t irq = irqd_to_hwirq(irqd);
248         struct qcom_smsm *smsm = entry->smsm;
249         u32 val;
250
251         if (entry->subscription) {
252                 val = readl(entry->subscription + smsm->local_host);
253                 val &= ~BIT(irq);
254                 writel(val, entry->subscription + smsm->local_host);
255         }
256
257         clear_bit(irq, entry->irq_enabled);
258 }
259
260 /**
261  * smsm_unmask_irq() - subscribe to cascades of IRQs of a certain status bit
262  * @irqd:       IRQ handle to be unmasked
263  *
264
265  * This subscribes the local CPU to interrupts upon changes to the defined
266  * status bit. The bit is also marked for cascading.
267
268  */
269 static void smsm_unmask_irq(struct irq_data *irqd)
270 {
271         struct smsm_entry *entry = irq_data_get_irq_chip_data(irqd);
272         irq_hw_number_t irq = irqd_to_hwirq(irqd);
273         struct qcom_smsm *smsm = entry->smsm;
274         u32 val;
275
276         /* Make sure our last cached state is up-to-date */
277         if (readl(entry->remote_state) & BIT(irq))
278                 set_bit(irq, &entry->last_value);
279         else
280                 clear_bit(irq, &entry->last_value);
281
282         set_bit(irq, entry->irq_enabled);
283
284         if (entry->subscription) {
285                 val = readl(entry->subscription + smsm->local_host);
286                 val |= BIT(irq);
287                 writel(val, entry->subscription + smsm->local_host);
288         }
289 }
290
291 /**
292  * smsm_set_irq_type() - updates the requested IRQ type for the cascading
293  * @irqd:       consumer interrupt handle
294  * @type:       requested flags
295  */
296 static int smsm_set_irq_type(struct irq_data *irqd, unsigned int type)
297 {
298         struct smsm_entry *entry = irq_data_get_irq_chip_data(irqd);
299         irq_hw_number_t irq = irqd_to_hwirq(irqd);
300
301         if (!(type & IRQ_TYPE_EDGE_BOTH))
302                 return -EINVAL;
303
304         if (type & IRQ_TYPE_EDGE_RISING)
305                 set_bit(irq, entry->irq_rising);
306         else
307                 clear_bit(irq, entry->irq_rising);
308
309         if (type & IRQ_TYPE_EDGE_FALLING)
310                 set_bit(irq, entry->irq_falling);
311         else
312                 clear_bit(irq, entry->irq_falling);
313
314         return 0;
315 }
316
317 static struct irq_chip smsm_irq_chip = {
318         .name           = "smsm",
319         .irq_mask       = smsm_mask_irq,
320         .irq_unmask     = smsm_unmask_irq,
321         .irq_set_type   = smsm_set_irq_type,
322 };
323
324 /**
325  * smsm_irq_map() - sets up a mapping for a cascaded IRQ
326  * @d:          IRQ domain representing an entry
327  * @irq:        IRQ to set up
328  * @hw:         unused
329  */
330 static int smsm_irq_map(struct irq_domain *d,
331                         unsigned int irq,
332                         irq_hw_number_t hw)
333 {
334         struct smsm_entry *entry = d->host_data;
335
336         irq_set_chip_and_handler(irq, &smsm_irq_chip, handle_level_irq);
337         irq_set_chip_data(irq, entry);
338         irq_set_nested_thread(irq, 1);
339
340         return 0;
341 }
342
343 static const struct irq_domain_ops smsm_irq_ops = {
344         .map = smsm_irq_map,
345         .xlate = irq_domain_xlate_twocell,
346 };
347
348 /**
349  * smsm_parse_ipc() - parses a qcom,ipc-%d device tree property
350  * @smsm:       smsm driver context
351  * @host_id:    index of the remote host to be resolved
352  *
353  * Parses device tree to acquire the information needed for sending the
354  * outgoing interrupts to a remote host - identified by @host_id.
355  */
356 static int smsm_parse_ipc(struct qcom_smsm *smsm, unsigned host_id)
357 {
358         struct device_node *syscon;
359         struct device_node *node = smsm->dev->of_node;
360         struct smsm_host *host = &smsm->hosts[host_id];
361         char key[16];
362         int ret;
363
364         snprintf(key, sizeof(key), "qcom,ipc-%d", host_id);
365         syscon = of_parse_phandle(node, key, 0);
366         if (!syscon)
367                 return 0;
368
369         host->ipc_regmap = syscon_node_to_regmap(syscon);
370         if (IS_ERR(host->ipc_regmap))
371                 return PTR_ERR(host->ipc_regmap);
372
373         ret = of_property_read_u32_index(node, key, 1, &host->ipc_offset);
374         if (ret < 0) {
375                 dev_err(smsm->dev, "no offset in %s\n", key);
376                 return -EINVAL;
377         }
378
379         ret = of_property_read_u32_index(node, key, 2, &host->ipc_bit);
380         if (ret < 0) {
381                 dev_err(smsm->dev, "no bit in %s\n", key);
382                 return -EINVAL;
383         }
384
385         return 0;
386 }
387
388 /**
389  * smsm_inbound_entry() - parse DT and set up an entry representing a remote system
390  * @smsm:       smsm driver context
391  * @entry:      entry context to be set up
392  * @node:       dt node containing the entry's properties
393  */
394 static int smsm_inbound_entry(struct qcom_smsm *smsm,
395                               struct smsm_entry *entry,
396                               struct device_node *node)
397 {
398         int ret;
399         int irq;
400
401         irq = irq_of_parse_and_map(node, 0);
402         if (!irq) {
403                 dev_err(smsm->dev, "failed to parse smsm interrupt\n");
404                 return -EINVAL;
405         }
406
407         ret = devm_request_threaded_irq(smsm->dev, irq,
408                                         NULL, smsm_intr,
409                                         IRQF_ONESHOT,
410                                         "smsm", (void *)entry);
411         if (ret) {
412                 dev_err(smsm->dev, "failed to request interrupt\n");
413                 return ret;
414         }
415
416         entry->domain = irq_domain_add_linear(node, 32, &smsm_irq_ops, entry);
417         if (!entry->domain) {
418                 dev_err(smsm->dev, "failed to add irq_domain\n");
419                 return -ENOMEM;
420         }
421
422         return 0;
423 }
424
425 /**
426  * smsm_get_size_info() - parse the optional memory segment for sizes
427  * @smsm:       smsm driver context
428  *
429  * Attempt to acquire the number of hosts and entries from the optional shared
430  * memory location. Not being able to find this segment should indicate that
431  * we're on a older system where these values was hard coded to
432  * SMSM_DEFAULT_NUM_ENTRIES and SMSM_DEFAULT_NUM_HOSTS.
433  *
434  * Returns 0 on success, negative errno on failure.
435  */
436 static int smsm_get_size_info(struct qcom_smsm *smsm)
437 {
438         size_t size;
439         struct {
440                 u32 num_hosts;
441                 u32 num_entries;
442                 u32 reserved0;
443                 u32 reserved1;
444         } *info;
445
446         info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
447         if (PTR_ERR(info) == -ENOENT || size != sizeof(*info)) {
448                 dev_warn(smsm->dev, "no smsm size info, using defaults\n");
449                 smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
450                 smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
451                 return 0;
452         } else if (IS_ERR(info)) {
453                 dev_err(smsm->dev, "unable to retrieve smsm size info\n");
454                 return PTR_ERR(info);
455         }
456
457         smsm->num_entries = info->num_entries;
458         smsm->num_hosts = info->num_hosts;
459
460         dev_dbg(smsm->dev,
461                 "found custom size of smsm: %d entries %d hosts\n",
462                 smsm->num_entries, smsm->num_hosts);
463
464         return 0;
465 }
466
467 static int qcom_smsm_probe(struct platform_device *pdev)
468 {
469         struct device_node *local_node;
470         struct device_node *node;
471         struct smsm_entry *entry;
472         struct qcom_smsm *smsm;
473         u32 *intr_mask;
474         size_t size;
475         u32 *states;
476         u32 id;
477         int ret;
478
479         smsm = devm_kzalloc(&pdev->dev, sizeof(*smsm), GFP_KERNEL);
480         if (!smsm)
481                 return -ENOMEM;
482         smsm->dev = &pdev->dev;
483         spin_lock_init(&smsm->lock);
484
485         ret = smsm_get_size_info(smsm);
486         if (ret)
487                 return ret;
488
489         smsm->entries = devm_kcalloc(&pdev->dev,
490                                      smsm->num_entries,
491                                      sizeof(struct smsm_entry),
492                                      GFP_KERNEL);
493         if (!smsm->entries)
494                 return -ENOMEM;
495
496         smsm->hosts = devm_kcalloc(&pdev->dev,
497                                    smsm->num_hosts,
498                                    sizeof(struct smsm_host),
499                                    GFP_KERNEL);
500         if (!smsm->hosts)
501                 return -ENOMEM;
502
503         local_node = of_find_node_with_property(pdev->dev.of_node, "#qcom,smem-state-cells");
504         if (!local_node) {
505                 dev_err(&pdev->dev, "no state entry\n");
506                 return -EINVAL;
507         }
508
509         of_property_read_u32(pdev->dev.of_node,
510                              "qcom,local-host",
511                              &smsm->local_host);
512
513         /* Parse the host properties */
514         for (id = 0; id < smsm->num_hosts; id++) {
515                 ret = smsm_parse_ipc(smsm, id);
516                 if (ret < 0)
517                         return ret;
518         }
519
520         /* Acquire the main SMSM state vector */
521         ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE,
522                               smsm->num_entries * sizeof(u32));
523         if (ret < 0 && ret != -EEXIST) {
524                 dev_err(&pdev->dev, "unable to allocate shared state entry\n");
525                 return ret;
526         }
527
528         states = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE, NULL);
529         if (IS_ERR(states)) {
530                 dev_err(&pdev->dev, "Unable to acquire shared state entry\n");
531                 return PTR_ERR(states);
532         }
533
534         /* Acquire the list of interrupt mask vectors */
535         size = smsm->num_entries * smsm->num_hosts * sizeof(u32);
536         ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_CPU_INTR_MASK, size);
537         if (ret < 0 && ret != -EEXIST) {
538                 dev_err(&pdev->dev, "unable to allocate smsm interrupt mask\n");
539                 return ret;
540         }
541
542         intr_mask = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_CPU_INTR_MASK, NULL);
543         if (IS_ERR(intr_mask)) {
544                 dev_err(&pdev->dev, "unable to acquire shared memory interrupt mask\n");
545                 return PTR_ERR(intr_mask);
546         }
547
548         /* Setup the reference to the local state bits */
549         smsm->local_state = states + smsm->local_host;
550         smsm->subscription = intr_mask + smsm->local_host * smsm->num_hosts;
551
552         /* Register the outgoing state */
553         smsm->state = qcom_smem_state_register(local_node, &smsm_state_ops, smsm);
554         if (IS_ERR(smsm->state)) {
555                 dev_err(smsm->dev, "failed to register qcom_smem_state\n");
556                 return PTR_ERR(smsm->state);
557         }
558
559         /* Register handlers for remote processor entries of interest. */
560         for_each_available_child_of_node(pdev->dev.of_node, node) {
561                 if (!of_property_read_bool(node, "interrupt-controller"))
562                         continue;
563
564                 ret = of_property_read_u32(node, "reg", &id);
565                 if (ret || id >= smsm->num_entries) {
566                         dev_err(&pdev->dev, "invalid reg of entry\n");
567                         if (!ret)
568                                 ret = -EINVAL;
569                         goto unwind_interfaces;
570                 }
571                 entry = &smsm->entries[id];
572
573                 entry->smsm = smsm;
574                 entry->remote_state = states + id;
575
576                 /* Setup subscription pointers and unsubscribe to any kicks */
577                 entry->subscription = intr_mask + id * smsm->num_hosts;
578                 writel(0, entry->subscription + smsm->local_host);
579
580                 ret = smsm_inbound_entry(smsm, entry, node);
581                 if (ret < 0)
582                         goto unwind_interfaces;
583         }
584
585         platform_set_drvdata(pdev, smsm);
586
587         return 0;
588
589 unwind_interfaces:
590         for (id = 0; id < smsm->num_entries; id++)
591                 if (smsm->entries[id].domain)
592                         irq_domain_remove(smsm->entries[id].domain);
593
594         qcom_smem_state_unregister(smsm->state);
595
596         return ret;
597 }
598
599 static int qcom_smsm_remove(struct platform_device *pdev)
600 {
601         struct qcom_smsm *smsm = platform_get_drvdata(pdev);
602         unsigned id;
603
604         for (id = 0; id < smsm->num_entries; id++)
605                 if (smsm->entries[id].domain)
606                         irq_domain_remove(smsm->entries[id].domain);
607
608         qcom_smem_state_unregister(smsm->state);
609
610         return 0;
611 }
612
613 static const struct of_device_id qcom_smsm_of_match[] = {
614         { .compatible = "qcom,smsm" },
615         {}
616 };
617 MODULE_DEVICE_TABLE(of, qcom_smsm_of_match);
618
619 static struct platform_driver qcom_smsm_driver = {
620         .probe = qcom_smsm_probe,
621         .remove = qcom_smsm_remove,
622         .driver  = {
623                 .name  = "qcom-smsm",
624                 .of_match_table = qcom_smsm_of_match,
625         },
626 };
627 module_platform_driver(qcom_smsm_driver);
628
629 MODULE_DESCRIPTION("Qualcomm Shared Memory State Machine driver");
630 MODULE_LICENSE("GPL v2");