1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/* Copyright (c) 2018, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/err.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/notifier.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/io.h>
#include <linux/of_platform.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <soc/qcom/subsystem_notif.h>
#define CLIENT_STATE_OFFSET 4
#define SUBSYS_STATE_OFFSET 8
static void __iomem *base_reg;
enum subsystem_type {
VIRTUAL,
NATIVE,
};
struct subsystem_descriptor {
const char *name;
u32 offset;
enum subsystem_type type;
struct notifier_block nb;
void *handle;
unsigned int ssr_irq;
struct list_head subsystem_list;
struct work_struct work;
};
static LIST_HEAD(subsystem_descriptor_list);
static struct workqueue_struct *ssr_wq;
static void subsystem_notif_wq_func(struct work_struct *work)
{
struct subsystem_descriptor *subsystem =
container_of(work, struct subsystem_descriptor, work);
void *subsystem_handle;
int state, ret;
state = readl_relaxed(base_reg + subsystem->offset);
subsystem_handle = subsys_notif_add_subsys(subsystem->name);
ret = subsys_notif_queue_notification(subsystem_handle, state, NULL);
writel_relaxed(ret, base_reg + subsystem->offset + CLIENT_STATE_OFFSET);
}
static int subsystem_state_callback(struct notifier_block *this,
unsigned long value, void *priv)
{
struct subsystem_descriptor *subsystem =
container_of(this, struct subsystem_descriptor, nb);
writel_relaxed(value, base_reg + subsystem->offset +
SUBSYS_STATE_OFFSET);
return NOTIFY_OK;
}
static irqreturn_t subsystem_restart_irq_handler(int irq, void *dev_id)
{
struct subsystem_descriptor *subsystem = dev_id;
queue_work(ssr_wq, &subsystem->work);
return IRQ_HANDLED;
}
static int subsys_notif_virt_probe(struct platform_device *pdev)
{
struct device_node *node;
struct device_node *child = NULL;
const char *ss_type;
struct resource *res;
struct subsystem_descriptor *subsystem;
int ret = 0;
if (!pdev) {
dev_err(&pdev->dev, "pdev is NULL\n");
return -EINVAL;
}
node = pdev->dev.of_node;
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vdev_base");
base_reg = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR_OR_NULL(base_reg)) {
dev_err(&pdev->dev, "Memory mapping failed\n");
return -ENOMEM;
}
ssr_wq = create_singlethread_workqueue("ssr_wq");
if (!ssr_wq) {
dev_err(&pdev->dev, "Workqueue creation failed\n");
return -ENOMEM;
}
for_each_child_of_node(node, child) {
subsystem = devm_kmalloc(&pdev->dev,
sizeof(struct subsystem_descriptor),
GFP_KERNEL);
if (!subsystem) {
ret = -ENOMEM;
goto err;
}
subsystem->name =
of_get_property(child, "subsys-name", NULL);
if (IS_ERR_OR_NULL(subsystem->name)) {
dev_err(&pdev->dev, "Could not find subsystem name\n");
ret = -EINVAL;
goto err;
}
ret = of_property_read_u32(child, "offset",
&subsystem->offset);
if (ret) {
dev_err(&pdev->dev, "offset reading for %s failed\n",
subsystem->name);
ret = -EINVAL;
goto err;
}
ret = of_property_read_string(child, "type",
&ss_type);
if (ret) {
dev_err(&pdev->dev, "type reading for %s failed\n",
subsystem->name);
ret = -EINVAL;
goto err;
}
if (!strcmp(ss_type, "virtual"))
subsystem->type = VIRTUAL;
if (!strcmp(ss_type, "native"))
subsystem->type = NATIVE;
switch (subsystem->type) {
case NATIVE:
subsystem->nb.notifier_call =
subsystem_state_callback;
subsystem->handle =
subsys_notif_register_notifier(
subsystem->name, &subsystem->nb);
if (IS_ERR_OR_NULL(subsystem->handle)) {
dev_err(&pdev->dev,
"Could not register SSR notifier cb\n");
ret = -EINVAL;
goto err;
}
list_add_tail(&subsystem->subsystem_list,
&subsystem_descriptor_list);
break;
case VIRTUAL:
subsystem->ssr_irq =
of_irq_get_byname(child, "state-irq");
if (subsystem->ssr_irq < 0) {
dev_err(&pdev->dev, "Could not find IRQ\n");
ret = -EINVAL;
goto err;
}
ret = devm_request_threaded_irq(&pdev->dev,
subsystem->ssr_irq, NULL,
subsystem_restart_irq_handler,
IRQF_ONESHOT | IRQF_TRIGGER_RISING,
subsystem->name, subsystem);
break;
default:
dev_err(&pdev->dev, "Unsupported type %d\n",
subsystem->type);
}
}
INIT_WORK(&subsystem->work, subsystem_notif_wq_func);
return 0;
err:
destroy_workqueue(ssr_wq);
return ret;
}
static int subsys_notif_virt_remove(struct platform_device *pdev)
{
struct subsystem_descriptor *subsystem, *node;
destroy_workqueue(ssr_wq);
list_for_each_entry_safe(subsystem, node, &subsystem_descriptor_list,
subsystem_list) {
subsys_notif_unregister_notifier(subsystem->handle,
&subsystem->nb);
list_del(&subsystem->subsystem_list);
}
return 0;
}
static const struct of_device_id match_table[] = {
{ .compatible = "qcom,subsys-notif-virt" },
{},
};
static struct platform_driver subsys_notif_virt_driver = {
.probe = subsys_notif_virt_probe,
.remove = subsys_notif_virt_remove,
.driver = {
.name = "subsys_notif_virt",
.owner = THIS_MODULE,
.of_match_table = match_table,
},
};
static int __init subsys_notif_virt_init(void)
{
return platform_driver_register(&subsys_notif_virt_driver);
}
module_init(subsys_notif_virt_init);
static void __exit subsys_notif_virt_exit(void)
{
platform_driver_unregister(&subsys_notif_virt_driver);
}
module_exit(subsys_notif_virt_exit);
MODULE_DESCRIPTION("Subsystem Notification Virtual Driver");
MODULE_LICENSE("GPL v2");
|