1
2
3
4
5
6
7#include <linux/module.h>
8#include <linux/dma-mapping.h>
9#include <linux/remoteproc.h>
10#include <linux/ste_modem_shm.h>
11#include "remoteproc_internal.h"
12
13#define SPROC_FW_SIZE (50 * 4096)
14#define SPROC_MAX_TOC_ENTRIES 32
15#define SPROC_MAX_NOTIFY_ID 14
16#define SPROC_RESOURCE_NAME "rsc-table"
17#define SPROC_MODEM_NAME "ste-modem"
18#define SPROC_MODEM_FIRMWARE SPROC_MODEM_NAME "-fw.bin"
19
20#define sproc_dbg(sproc, fmt, ...) \
21 dev_dbg(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
22#define sproc_err(sproc, fmt, ...) \
23 dev_err(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
24
25
26struct sproc {
27 struct rproc *rproc;
28 struct ste_modem_device *mdev;
29 int error;
30 void *fw_addr;
31 size_t fw_size;
32 dma_addr_t fw_dma_addr;
33};
34
35
36struct ste_toc_entry {
37 __le32 start;
38 __le32 size;
39 __le32 flags;
40 __le32 entry_point;
41 __le32 load_addr;
42 char name[12];
43};
44
45
46
47
48
49
50
51
52struct ste_toc {
53 struct ste_toc_entry table[SPROC_MAX_TOC_ENTRIES];
54};
55
56
57static int sproc_load_segments(struct rproc *rproc, const struct firmware *fw)
58{
59 struct sproc *sproc = rproc->priv;
60
61 memcpy(sproc->fw_addr, fw->data, fw->size);
62
63 return 0;
64}
65
66
67static const struct ste_toc_entry *sproc_find_rsc_entry(const void *data)
68{
69 int i;
70 const struct ste_toc *toc = data;
71
72
73 for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
74 toc->table[i].start != 0xffffffff; i++) {
75 if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
76 sizeof(toc->table[i].name)))
77 return &toc->table[i];
78 }
79
80 return NULL;
81}
82
83
84static struct resource_table *
85sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
86 int *tablesz)
87{
88 struct sproc *sproc = rproc->priv;
89 struct resource_table *table;
90 const struct ste_toc_entry *entry;
91
92 if (!fw)
93 return NULL;
94
95 entry = sproc_find_rsc_entry(fw->data);
96 if (!entry) {
97 sproc_err(sproc, "resource table not found in fw\n");
98 return NULL;
99 }
100
101 table = (void *)(fw->data + entry->start);
102
103
104 if (entry->start > SPROC_FW_SIZE ||
105 entry->size > SPROC_FW_SIZE ||
106 fw->size > SPROC_FW_SIZE ||
107 entry->start + entry->size > fw->size ||
108 sizeof(struct resource_table) > entry->size) {
109 sproc_err(sproc, "bad size of fw or resource table\n");
110 return NULL;
111 }
112
113
114 if (table->ver != 1) {
115 sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
116 return NULL;
117 }
118
119
120 if (table->reserved[0] || table->reserved[1]) {
121 sproc_err(sproc, "non zero reserved bytes\n");
122 return NULL;
123 }
124
125
126 if (table->num > SPROC_MAX_TOC_ENTRIES ||
127 table->num * sizeof(table->offset[0]) +
128 sizeof(struct resource_table) > entry->size) {
129 sproc_err(sproc, "resource table incomplete\n");
130 return NULL;
131 }
132
133
134 if (SPROC_FW_SIZE < fw->size) {
135 sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
136 SPROC_FW_SIZE, fw->size);
137 return NULL;
138 }
139
140 sproc->fw_size = fw->size;
141 *tablesz = entry->size;
142
143 return table;
144}
145
146
147static struct resource_table *
148sproc_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
149{
150 struct sproc *sproc = rproc->priv;
151 const struct ste_toc_entry *entry;
152
153 if (!fw || !sproc->fw_addr)
154 return NULL;
155
156 entry = sproc_find_rsc_entry(sproc->fw_addr);
157 if (!entry) {
158 sproc_err(sproc, "resource table not found in fw\n");
159 return NULL;
160 }
161
162 return sproc->fw_addr + entry->start;
163}
164
165
166static const struct rproc_fw_ops sproc_fw_ops = {
167 .load = sproc_load_segments,
168 .find_rsc_table = sproc_find_rsc_table,
169 .find_loaded_rsc_table = sproc_find_loaded_rsc_table,
170};
171
172
173static void sproc_kick(struct rproc *rproc, int vqid)
174{
175 struct sproc *sproc = rproc->priv;
176
177 sproc_dbg(sproc, "kick vqid:%d\n", vqid);
178
179
180
181
182
183 sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
184}
185
186
187static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
188{
189 struct sproc *sproc = mdev->drv_data;
190
191 if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
192 sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
193}
194
195static struct ste_modem_dev_cb sproc_dev_cb = {
196 .kick = sproc_kick_callback,
197};
198
199
200static int sproc_start(struct rproc *rproc)
201{
202 struct sproc *sproc = rproc->priv;
203 int i, err;
204
205 sproc_dbg(sproc, "start ste-modem\n");
206
207
208 if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
209 sproc_err(sproc, "Notification IDs too high:%d\n",
210 rproc->max_notifyid);
211 return -EINVAL;
212 }
213
214
215 for (i = 0; i <= rproc->max_notifyid; i++) {
216 err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
217 if (err) {
218 sproc_err(sproc,
219 "subscription of kicks failed:%d\n", err);
220 return err;
221 }
222 }
223
224
225 return sproc->mdev->ops.power(sproc->mdev, true);
226}
227
228
229static int sproc_stop(struct rproc *rproc)
230{
231 struct sproc *sproc = rproc->priv;
232
233 sproc_dbg(sproc, "stop ste-modem\n");
234
235 return sproc->mdev->ops.power(sproc->mdev, false);
236}
237
238static struct rproc_ops sproc_ops = {
239 .start = sproc_start,
240 .stop = sproc_stop,
241 .kick = sproc_kick,
242};
243
244
245static int sproc_drv_remove(struct platform_device *pdev)
246{
247 struct ste_modem_device *mdev =
248 container_of(pdev, struct ste_modem_device, pdev);
249 struct sproc *sproc = mdev->drv_data;
250
251 sproc_dbg(sproc, "remove ste-modem\n");
252
253
254 sproc->mdev->ops.setup(sproc->mdev, NULL);
255
256
257 rproc_del(sproc->rproc);
258 dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE,
259 sproc->fw_addr, sproc->fw_dma_addr);
260 rproc_put(sproc->rproc);
261
262 mdev->drv_data = NULL;
263
264 return 0;
265}
266
267
268static int sproc_probe(struct platform_device *pdev)
269{
270 struct ste_modem_device *mdev =
271 container_of(pdev, struct ste_modem_device, pdev);
272 struct sproc *sproc;
273 struct rproc *rproc;
274 int err;
275
276 dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
277
278 if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
279 !mdev->ops.power) {
280 dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
281 return -EINVAL;
282 }
283
284 rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
285 SPROC_MODEM_FIRMWARE, sizeof(*sproc));
286 if (!rproc)
287 return -ENOMEM;
288
289 sproc = rproc->priv;
290 sproc->mdev = mdev;
291 sproc->rproc = rproc;
292 rproc->has_iommu = false;
293 mdev->drv_data = sproc;
294
295
296 sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
297
298
299 rproc->fw_ops = &sproc_fw_ops;
300
301
302
303
304
305
306 sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
307 &sproc->fw_dma_addr,
308 GFP_KERNEL);
309 if (!sproc->fw_addr) {
310 sproc_err(sproc, "Cannot allocate memory for fw\n");
311 err = -ENOMEM;
312 goto free_rproc;
313 }
314
315
316 err = rproc_add(rproc);
317 if (err)
318 goto free_mem;
319
320 return 0;
321
322free_mem:
323 dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE,
324 sproc->fw_addr, sproc->fw_dma_addr);
325free_rproc:
326
327 mdev->drv_data = NULL;
328 rproc_put(rproc);
329 return err;
330}
331
332static struct platform_driver sproc_driver = {
333 .driver = {
334 .name = SPROC_MODEM_NAME,
335 },
336 .probe = sproc_probe,
337 .remove = sproc_drv_remove,
338};
339
340module_platform_driver(sproc_driver);
341MODULE_LICENSE("GPL v2");
342MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");
343