This source file includes following definitions.
- smsm_update_bits
- smsm_intr
- smsm_mask_irq
- smsm_unmask_irq
- smsm_set_irq_type
- smsm_irq_map
- smsm_parse_ipc
- smsm_inbound_entry
- smsm_get_size_info
- qcom_smsm_probe
- qcom_smsm_remove
1
2
3
4
5
6
7 #include <linux/interrupt.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/of_irq.h>
11 #include <linux/platform_device.h>
12 #include <linux/spinlock.h>
13 #include <linux/regmap.h>
14 #include <linux/soc/qcom/smem.h>
15 #include <linux/soc/qcom/smem_state.h>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 #define SMEM_SMSM_SHARED_STATE 85
49 #define SMEM_SMSM_CPU_INTR_MASK 333
50 #define SMEM_SMSM_SIZE_INFO 419
51
52
53
54
55 #define SMSM_DEFAULT_NUM_ENTRIES 8
56 #define SMSM_DEFAULT_NUM_HOSTS 3
57
58 struct smsm_entry;
59 struct smsm_host;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 struct qcom_smsm {
76 struct device *dev;
77
78 u32 local_host;
79
80 u32 num_hosts;
81 u32 num_entries;
82
83 u32 *local_state;
84 u32 *subscription;
85 struct qcom_smem_state *state;
86
87 spinlock_t lock;
88
89 struct smsm_entry *entries;
90 struct smsm_host *hosts;
91 };
92
93
94
95
96
97
98
99
100
101
102
103
104
105 struct smsm_entry {
106 struct qcom_smsm *smsm;
107
108 struct irq_domain *domain;
109 DECLARE_BITMAP(irq_enabled, 32);
110 DECLARE_BITMAP(irq_rising, 32);
111 DECLARE_BITMAP(irq_falling, 32);
112 u32 last_value;
113
114 u32 *remote_state;
115 u32 *subscription;
116 };
117
118
119
120
121
122
123
124 struct smsm_host {
125 struct regmap *ipc_regmap;
126 int ipc_offset;
127 int ipc_bit;
128 };
129
130
131
132
133
134
135
136
137
138
139 static int smsm_update_bits(void *data, u32 mask, u32 value)
140 {
141 struct qcom_smsm *smsm = data;
142 struct smsm_host *hostp;
143 unsigned long flags;
144 u32 changes;
145 u32 host;
146 u32 orig;
147 u32 val;
148
149 spin_lock_irqsave(&smsm->lock, flags);
150
151
152 val = orig = readl(smsm->local_state);
153 val &= ~mask;
154 val |= value;
155
156
157 changes = val ^ orig;
158 if (!changes) {
159 spin_unlock_irqrestore(&smsm->lock, flags);
160 goto done;
161 }
162
163
164 writel(val, smsm->local_state);
165 spin_unlock_irqrestore(&smsm->lock, flags);
166
167
168 wmb();
169
170
171 for (host = 0; host < smsm->num_hosts; host++) {
172 hostp = &smsm->hosts[host];
173
174 val = readl(smsm->subscription + host);
175 if (val & changes && hostp->ipc_regmap) {
176 regmap_write(hostp->ipc_regmap,
177 hostp->ipc_offset,
178 BIT(hostp->ipc_bit));
179 }
180 }
181
182 done:
183 return 0;
184 }
185
186 static const struct qcom_smem_state_ops smsm_state_ops = {
187 .update_bits = smsm_update_bits,
188 };
189
190
191
192
193
194
195
196
197
198 static irqreturn_t smsm_intr(int irq, void *data)
199 {
200 struct smsm_entry *entry = data;
201 unsigned i;
202 int irq_pin;
203 u32 changed;
204 u32 val;
205
206 val = readl(entry->remote_state);
207 changed = val ^ entry->last_value;
208 entry->last_value = val;
209
210 for_each_set_bit(i, entry->irq_enabled, 32) {
211 if (!(changed & BIT(i)))
212 continue;
213
214 if (val & BIT(i)) {
215 if (test_bit(i, entry->irq_rising)) {
216 irq_pin = irq_find_mapping(entry->domain, i);
217 handle_nested_irq(irq_pin);
218 }
219 } else {
220 if (test_bit(i, entry->irq_falling)) {
221 irq_pin = irq_find_mapping(entry->domain, i);
222 handle_nested_irq(irq_pin);
223 }
224 }
225 }
226
227 return IRQ_HANDLED;
228 }
229
230
231
232
233
234
235
236
237 static void smsm_mask_irq(struct irq_data *irqd)
238 {
239 struct smsm_entry *entry = irq_data_get_irq_chip_data(irqd);
240 irq_hw_number_t irq = irqd_to_hwirq(irqd);
241 struct qcom_smsm *smsm = entry->smsm;
242 u32 val;
243
244 if (entry->subscription) {
245 val = readl(entry->subscription + smsm->local_host);
246 val &= ~BIT(irq);
247 writel(val, entry->subscription + smsm->local_host);
248 }
249
250 clear_bit(irq, entry->irq_enabled);
251 }
252
253
254
255
256
257
258
259
260
261
262 static void smsm_unmask_irq(struct irq_data *irqd)
263 {
264 struct smsm_entry *entry = irq_data_get_irq_chip_data(irqd);
265 irq_hw_number_t irq = irqd_to_hwirq(irqd);
266 struct qcom_smsm *smsm = entry->smsm;
267 u32 val;
268
269 set_bit(irq, entry->irq_enabled);
270
271 if (entry->subscription) {
272 val = readl(entry->subscription + smsm->local_host);
273 val |= BIT(irq);
274 writel(val, entry->subscription + smsm->local_host);
275 }
276 }
277
278
279
280
281
282
283 static int smsm_set_irq_type(struct irq_data *irqd, unsigned int type)
284 {
285 struct smsm_entry *entry = irq_data_get_irq_chip_data(irqd);
286 irq_hw_number_t irq = irqd_to_hwirq(irqd);
287
288 if (!(type & IRQ_TYPE_EDGE_BOTH))
289 return -EINVAL;
290
291 if (type & IRQ_TYPE_EDGE_RISING)
292 set_bit(irq, entry->irq_rising);
293 else
294 clear_bit(irq, entry->irq_rising);
295
296 if (type & IRQ_TYPE_EDGE_FALLING)
297 set_bit(irq, entry->irq_falling);
298 else
299 clear_bit(irq, entry->irq_falling);
300
301 return 0;
302 }
303
304 static struct irq_chip smsm_irq_chip = {
305 .name = "smsm",
306 .irq_mask = smsm_mask_irq,
307 .irq_unmask = smsm_unmask_irq,
308 .irq_set_type = smsm_set_irq_type,
309 };
310
311
312
313
314
315
316
317 static int smsm_irq_map(struct irq_domain *d,
318 unsigned int irq,
319 irq_hw_number_t hw)
320 {
321 struct smsm_entry *entry = d->host_data;
322
323 irq_set_chip_and_handler(irq, &smsm_irq_chip, handle_level_irq);
324 irq_set_chip_data(irq, entry);
325 irq_set_nested_thread(irq, 1);
326
327 return 0;
328 }
329
330 static const struct irq_domain_ops smsm_irq_ops = {
331 .map = smsm_irq_map,
332 .xlate = irq_domain_xlate_twocell,
333 };
334
335
336
337
338
339
340
341
342
343 static int smsm_parse_ipc(struct qcom_smsm *smsm, unsigned host_id)
344 {
345 struct device_node *syscon;
346 struct device_node *node = smsm->dev->of_node;
347 struct smsm_host *host = &smsm->hosts[host_id];
348 char key[16];
349 int ret;
350
351 snprintf(key, sizeof(key), "qcom,ipc-%d", host_id);
352 syscon = of_parse_phandle(node, key, 0);
353 if (!syscon)
354 return 0;
355
356 host->ipc_regmap = syscon_node_to_regmap(syscon);
357 if (IS_ERR(host->ipc_regmap))
358 return PTR_ERR(host->ipc_regmap);
359
360 ret = of_property_read_u32_index(node, key, 1, &host->ipc_offset);
361 if (ret < 0) {
362 dev_err(smsm->dev, "no offset in %s\n", key);
363 return -EINVAL;
364 }
365
366 ret = of_property_read_u32_index(node, key, 2, &host->ipc_bit);
367 if (ret < 0) {
368 dev_err(smsm->dev, "no bit in %s\n", key);
369 return -EINVAL;
370 }
371
372 return 0;
373 }
374
375
376
377
378
379
380
381 static int smsm_inbound_entry(struct qcom_smsm *smsm,
382 struct smsm_entry *entry,
383 struct device_node *node)
384 {
385 int ret;
386 int irq;
387
388 irq = irq_of_parse_and_map(node, 0);
389 if (!irq) {
390 dev_err(smsm->dev, "failed to parse smsm interrupt\n");
391 return -EINVAL;
392 }
393
394 ret = devm_request_threaded_irq(smsm->dev, irq,
395 NULL, smsm_intr,
396 IRQF_ONESHOT,
397 "smsm", (void *)entry);
398 if (ret) {
399 dev_err(smsm->dev, "failed to request interrupt\n");
400 return ret;
401 }
402
403 entry->domain = irq_domain_add_linear(node, 32, &smsm_irq_ops, entry);
404 if (!entry->domain) {
405 dev_err(smsm->dev, "failed to add irq_domain\n");
406 return -ENOMEM;
407 }
408
409 return 0;
410 }
411
412
413
414
415
416
417
418
419
420
421
422
423 static int smsm_get_size_info(struct qcom_smsm *smsm)
424 {
425 size_t size;
426 struct {
427 u32 num_hosts;
428 u32 num_entries;
429 u32 reserved0;
430 u32 reserved1;
431 } *info;
432
433 info = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SIZE_INFO, &size);
434 if (IS_ERR(info) && PTR_ERR(info) != -ENOENT) {
435 if (PTR_ERR(info) != -EPROBE_DEFER)
436 dev_err(smsm->dev, "unable to retrieve smsm size info\n");
437 return PTR_ERR(info);
438 } else if (IS_ERR(info) || size != sizeof(*info)) {
439 dev_warn(smsm->dev, "no smsm size info, using defaults\n");
440 smsm->num_entries = SMSM_DEFAULT_NUM_ENTRIES;
441 smsm->num_hosts = SMSM_DEFAULT_NUM_HOSTS;
442 return 0;
443 }
444
445 smsm->num_entries = info->num_entries;
446 smsm->num_hosts = info->num_hosts;
447
448 dev_dbg(smsm->dev,
449 "found custom size of smsm: %d entries %d hosts\n",
450 smsm->num_entries, smsm->num_hosts);
451
452 return 0;
453 }
454
455 static int qcom_smsm_probe(struct platform_device *pdev)
456 {
457 struct device_node *local_node;
458 struct device_node *node;
459 struct smsm_entry *entry;
460 struct qcom_smsm *smsm;
461 u32 *intr_mask;
462 size_t size;
463 u32 *states;
464 u32 id;
465 int ret;
466
467 smsm = devm_kzalloc(&pdev->dev, sizeof(*smsm), GFP_KERNEL);
468 if (!smsm)
469 return -ENOMEM;
470 smsm->dev = &pdev->dev;
471 spin_lock_init(&smsm->lock);
472
473 ret = smsm_get_size_info(smsm);
474 if (ret)
475 return ret;
476
477 smsm->entries = devm_kcalloc(&pdev->dev,
478 smsm->num_entries,
479 sizeof(struct smsm_entry),
480 GFP_KERNEL);
481 if (!smsm->entries)
482 return -ENOMEM;
483
484 smsm->hosts = devm_kcalloc(&pdev->dev,
485 smsm->num_hosts,
486 sizeof(struct smsm_host),
487 GFP_KERNEL);
488 if (!smsm->hosts)
489 return -ENOMEM;
490
491 for_each_child_of_node(pdev->dev.of_node, local_node) {
492 if (of_find_property(local_node, "#qcom,smem-state-cells", NULL))
493 break;
494 }
495 if (!local_node) {
496 dev_err(&pdev->dev, "no state entry\n");
497 return -EINVAL;
498 }
499
500 of_property_read_u32(pdev->dev.of_node,
501 "qcom,local-host",
502 &smsm->local_host);
503
504
505 for (id = 0; id < smsm->num_hosts; id++) {
506 ret = smsm_parse_ipc(smsm, id);
507 if (ret < 0)
508 return ret;
509 }
510
511
512 ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE,
513 smsm->num_entries * sizeof(u32));
514 if (ret < 0 && ret != -EEXIST) {
515 dev_err(&pdev->dev, "unable to allocate shared state entry\n");
516 return ret;
517 }
518
519 states = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_SHARED_STATE, NULL);
520 if (IS_ERR(states)) {
521 dev_err(&pdev->dev, "Unable to acquire shared state entry\n");
522 return PTR_ERR(states);
523 }
524
525
526 size = smsm->num_entries * smsm->num_hosts * sizeof(u32);
527 ret = qcom_smem_alloc(QCOM_SMEM_HOST_ANY, SMEM_SMSM_CPU_INTR_MASK, size);
528 if (ret < 0 && ret != -EEXIST) {
529 dev_err(&pdev->dev, "unable to allocate smsm interrupt mask\n");
530 return ret;
531 }
532
533 intr_mask = qcom_smem_get(QCOM_SMEM_HOST_ANY, SMEM_SMSM_CPU_INTR_MASK, NULL);
534 if (IS_ERR(intr_mask)) {
535 dev_err(&pdev->dev, "unable to acquire shared memory interrupt mask\n");
536 return PTR_ERR(intr_mask);
537 }
538
539
540 smsm->local_state = states + smsm->local_host;
541 smsm->subscription = intr_mask + smsm->local_host * smsm->num_hosts;
542
543
544 smsm->state = qcom_smem_state_register(local_node, &smsm_state_ops, smsm);
545 if (IS_ERR(smsm->state)) {
546 dev_err(smsm->dev, "failed to register qcom_smem_state\n");
547 return PTR_ERR(smsm->state);
548 }
549
550
551 for_each_available_child_of_node(pdev->dev.of_node, node) {
552 if (!of_property_read_bool(node, "interrupt-controller"))
553 continue;
554
555 ret = of_property_read_u32(node, "reg", &id);
556 if (ret || id >= smsm->num_entries) {
557 dev_err(&pdev->dev, "invalid reg of entry\n");
558 if (!ret)
559 ret = -EINVAL;
560 goto unwind_interfaces;
561 }
562 entry = &smsm->entries[id];
563
564 entry->smsm = smsm;
565 entry->remote_state = states + id;
566
567
568 entry->subscription = intr_mask + id * smsm->num_hosts;
569 writel(0, entry->subscription + smsm->local_host);
570
571 ret = smsm_inbound_entry(smsm, entry, node);
572 if (ret < 0)
573 goto unwind_interfaces;
574 }
575
576 platform_set_drvdata(pdev, smsm);
577
578 return 0;
579
580 unwind_interfaces:
581 for (id = 0; id < smsm->num_entries; id++)
582 if (smsm->entries[id].domain)
583 irq_domain_remove(smsm->entries[id].domain);
584
585 qcom_smem_state_unregister(smsm->state);
586
587 return ret;
588 }
589
590 static int qcom_smsm_remove(struct platform_device *pdev)
591 {
592 struct qcom_smsm *smsm = platform_get_drvdata(pdev);
593 unsigned id;
594
595 for (id = 0; id < smsm->num_entries; id++)
596 if (smsm->entries[id].domain)
597 irq_domain_remove(smsm->entries[id].domain);
598
599 qcom_smem_state_unregister(smsm->state);
600
601 return 0;
602 }
603
604 static const struct of_device_id qcom_smsm_of_match[] = {
605 { .compatible = "qcom,smsm" },
606 {}
607 };
608 MODULE_DEVICE_TABLE(of, qcom_smsm_of_match);
609
610 static struct platform_driver qcom_smsm_driver = {
611 .probe = qcom_smsm_probe,
612 .remove = qcom_smsm_remove,
613 .driver = {
614 .name = "qcom-smsm",
615 .of_match_table = qcom_smsm_of_match,
616 },
617 };
618 module_platform_driver(qcom_smsm_driver);
619
620 MODULE_DESCRIPTION("Qualcomm Shared Memory State Machine driver");
621 MODULE_LICENSE("GPL v2");