Merge tag 'rproc-v4.14' of git://github.com/andersson/remoteproc
[sfrench/cifs-2.6.git] / drivers / remoteproc / qcom_common.c
1 /*
2  * Qualcomm Peripheral Image Loader helpers
3  *
4  * Copyright (C) 2016 Linaro Ltd
5  * Copyright (C) 2015 Sony Mobile Communications Inc
6  * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/firmware.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/notifier.h>
22 #include <linux/remoteproc.h>
23 #include <linux/rpmsg/qcom_smd.h>
24
25 #include "remoteproc_internal.h"
26 #include "qcom_common.h"
27
28 #define to_smd_subdev(d) container_of(d, struct qcom_rproc_subdev, subdev)
29 #define to_ssr_subdev(d) container_of(d, struct qcom_rproc_ssr, subdev)
30
31 static BLOCKING_NOTIFIER_HEAD(ssr_notifiers);
32
33 /**
34  * qcom_mdt_find_rsc_table() - provide dummy resource table for remoteproc
35  * @rproc:      remoteproc handle
36  * @fw:         firmware header
37  * @tablesz:    outgoing size of the table
38  *
39  * Returns a dummy table.
40  */
41 struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc,
42                                                const struct firmware *fw,
43                                                int *tablesz)
44 {
45         static struct resource_table table = { .ver = 1, };
46
47         *tablesz = sizeof(table);
48         return &table;
49 }
50 EXPORT_SYMBOL_GPL(qcom_mdt_find_rsc_table);
51
52 static int smd_subdev_probe(struct rproc_subdev *subdev)
53 {
54         struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
55
56         smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
57
58         return PTR_ERR_OR_ZERO(smd->edge);
59 }
60
61 static void smd_subdev_remove(struct rproc_subdev *subdev)
62 {
63         struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
64
65         qcom_smd_unregister_edge(smd->edge);
66         smd->edge = NULL;
67 }
68
69 /**
70  * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
71  * @rproc:      rproc handle to parent the subdevice
72  * @smd:        reference to a Qualcomm subdev context
73  */
74 void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
75 {
76         struct device *dev = &rproc->dev;
77
78         smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
79         if (!smd->node)
80                 return;
81
82         smd->dev = dev;
83         rproc_add_subdev(rproc, &smd->subdev, smd_subdev_probe, smd_subdev_remove);
84 }
85 EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
86
87 /**
88  * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
89  * @rproc:      rproc handle
90  * @smd:        the SMD subdevice to remove
91  */
92 void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
93 {
94         rproc_remove_subdev(rproc, &smd->subdev);
95         of_node_put(smd->node);
96 }
97 EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
98
99 /**
100  * qcom_register_ssr_notifier() - register SSR notification handler
101  * @nb:         notifier_block to notify for restart notifications
102  *
103  * Returns 0 on success, negative errno on failure.
104  *
105  * This register the @notify function as handler for restart notifications. As
106  * remote processors are stopped this function will be called, with the SSR
107  * name passed as a parameter.
108  */
109 int qcom_register_ssr_notifier(struct notifier_block *nb)
110 {
111         return blocking_notifier_chain_register(&ssr_notifiers, nb);
112 }
113 EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
114
115 /**
116  * qcom_unregister_ssr_notifier() - unregister SSR notification handler
117  * @nb:         notifier_block to unregister
118  */
119 void qcom_unregister_ssr_notifier(struct notifier_block *nb)
120 {
121         blocking_notifier_chain_unregister(&ssr_notifiers, nb);
122 }
123 EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
124
125 static int ssr_notify_start(struct rproc_subdev *subdev)
126 {
127         return  0;
128 }
129
130 static void ssr_notify_stop(struct rproc_subdev *subdev)
131 {
132         struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
133
134         blocking_notifier_call_chain(&ssr_notifiers, 0, (void *)ssr->name);
135 }
136
137 /**
138  * qcom_add_ssr_subdev() - register subdevice as restart notification source
139  * @rproc:      rproc handle
140  * @ssr:        SSR subdevice handle
141  * @ssr_name:   identifier to use for notifications originating from @rproc
142  *
143  * As the @ssr is registered with the @rproc SSR events will be sent to all
144  * registered listeners in the system as the remoteproc is shut down.
145  */
146 void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
147                          const char *ssr_name)
148 {
149         ssr->name = ssr_name;
150
151         rproc_add_subdev(rproc, &ssr->subdev, ssr_notify_start, ssr_notify_stop);
152 }
153 EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
154
155 /**
156  * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
157  * @rproc:      rproc handle
158  * @ssr:        SSR subdevice handle
159  */
160 void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
161 {
162         rproc_remove_subdev(rproc, &ssr->subdev);
163 }
164 EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
165
166 MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
167 MODULE_LICENSE("GPL v2");