Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
400e64df OBC |
2 | /* |
3 | * Remote Processor Framework | |
4 | * | |
5 | * Copyright (C) 2011 Texas Instruments, Inc. | |
6 | * Copyright (C) 2011 Google, Inc. | |
7 | * | |
8 | * Ohad Ben-Cohen <ohad@wizery.com> | |
9 | * Brian Swetland <swetland@google.com> | |
10 | * Mark Grosen <mgrosen@ti.com> | |
11 | * Fernando Guzman Lugo <fernando.lugo@ti.com> | |
12 | * Suman Anna <s-anna@ti.com> | |
13 | * Robert Tivy <rtivy@ti.com> | |
14 | * Armando Uribe De Leon <x0095078@ti.com> | |
400e64df OBC |
15 | */ |
16 | ||
17 | #define pr_fmt(fmt) "%s: " fmt, __func__ | |
18 | ||
dc5192c4 | 19 | #include <linux/delay.h> |
400e64df OBC |
20 | #include <linux/kernel.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/device.h> | |
f39650de | 23 | #include <linux/panic_notifier.h> |
400e64df OBC |
24 | #include <linux/slab.h> |
25 | #include <linux/mutex.h> | |
0a0f0d8b | 26 | #include <linux/dma-map-ops.h> |
400e64df | 27 | #include <linux/dma-mapping.h> |
e0d07278 | 28 | #include <linux/dma-direct.h> /* XXX: pokes into bus_dma_range */ |
400e64df OBC |
29 | #include <linux/firmware.h> |
30 | #include <linux/string.h> | |
31 | #include <linux/debugfs.h> | |
c0abe2ca | 32 | #include <linux/rculist.h> |
400e64df OBC |
33 | #include <linux/remoteproc.h> |
34 | #include <linux/iommu.h> | |
b5ab5e24 | 35 | #include <linux/idr.h> |
400e64df | 36 | #include <linux/elf.h> |
a2b950ac | 37 | #include <linux/crc32.h> |
086d0872 | 38 | #include <linux/of_reserved_mem.h> |
400e64df OBC |
39 | #include <linux/virtio_ids.h> |
40 | #include <linux/virtio_ring.h> | |
cf59d3e9 | 41 | #include <asm/byteorder.h> |
086d0872 | 42 | #include <linux/platform_device.h> |
400e64df OBC |
43 | |
44 | #include "remoteproc_internal.h" | |
45 | ||
b36de8cf LP |
46 | #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL |
47 | ||
fec47d86 DG |
48 | static DEFINE_MUTEX(rproc_list_mutex); |
49 | static LIST_HEAD(rproc_list); | |
dc5192c4 | 50 | static struct notifier_block rproc_panic_nb; |
fec47d86 | 51 | |
a2b950ac OBC |
52 | typedef int (*rproc_handle_resource_t)(struct rproc *rproc, |
53 | void *, int offset, int avail); | |
400e64df | 54 | |
c6aed238 LP |
55 | static int rproc_alloc_carveout(struct rproc *rproc, |
56 | struct rproc_mem_entry *mem); | |
57 | static int rproc_release_carveout(struct rproc *rproc, | |
58 | struct rproc_mem_entry *mem); | |
59 | ||
b5ab5e24 OBC |
60 | /* Unique indices for remoteproc devices */ |
61 | static DEFINE_IDA(rproc_dev_index); | |
62 | ||
8afd519c FGL |
63 | static const char * const rproc_crash_names[] = { |
64 | [RPROC_MMUFAULT] = "mmufault", | |
b3d39032 BA |
65 | [RPROC_WATCHDOG] = "watchdog", |
66 | [RPROC_FATAL_ERROR] = "fatal error", | |
8afd519c FGL |
67 | }; |
68 | ||
69 | /* translate rproc_crash_type to string */ | |
70 | static const char *rproc_crash_to_string(enum rproc_crash_type type) | |
71 | { | |
72 | if (type < ARRAY_SIZE(rproc_crash_names)) | |
73 | return rproc_crash_names[type]; | |
b23f7a09 | 74 | return "unknown"; |
8afd519c FGL |
75 | } |
76 | ||
400e64df OBC |
77 | /* |
78 | * This is the IOMMU fault handler we register with the IOMMU API | |
79 | * (when relevant; not all remote processors access memory through | |
80 | * an IOMMU). | |
81 | * | |
82 | * IOMMU core will invoke this handler whenever the remote processor | |
83 | * will try to access an unmapped device address. | |
400e64df OBC |
84 | */ |
85 | static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, | |
730f84ce | 86 | unsigned long iova, int flags, void *token) |
400e64df | 87 | { |
8afd519c FGL |
88 | struct rproc *rproc = token; |
89 | ||
400e64df OBC |
90 | dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); |
91 | ||
8afd519c FGL |
92 | rproc_report_crash(rproc, RPROC_MMUFAULT); |
93 | ||
400e64df OBC |
94 | /* |
95 | * Let the iommu core know we're not really handling this fault; | |
8afd519c | 96 | * we just used it as a recovery trigger. |
400e64df OBC |
97 | */ |
98 | return -ENOSYS; | |
99 | } | |
100 | ||
101 | static int rproc_enable_iommu(struct rproc *rproc) | |
102 | { | |
103 | struct iommu_domain *domain; | |
b5ab5e24 | 104 | struct device *dev = rproc->dev.parent; |
400e64df OBC |
105 | int ret; |
106 | ||
315491e5 SA |
107 | if (!rproc->has_iommu) { |
108 | dev_dbg(dev, "iommu not present\n"); | |
0798e1da | 109 | return 0; |
400e64df OBC |
110 | } |
111 | ||
112 | domain = iommu_domain_alloc(dev->bus); | |
113 | if (!domain) { | |
114 | dev_err(dev, "can't alloc iommu domain\n"); | |
115 | return -ENOMEM; | |
116 | } | |
117 | ||
77ca2332 | 118 | iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); |
400e64df OBC |
119 | |
120 | ret = iommu_attach_device(domain, dev); | |
121 | if (ret) { | |
122 | dev_err(dev, "can't attach iommu device: %d\n", ret); | |
123 | goto free_domain; | |
124 | } | |
125 | ||
126 | rproc->domain = domain; | |
127 | ||
128 | return 0; | |
129 | ||
130 | free_domain: | |
131 | iommu_domain_free(domain); | |
132 | return ret; | |
133 | } | |
134 | ||
135 | static void rproc_disable_iommu(struct rproc *rproc) | |
136 | { | |
137 | struct iommu_domain *domain = rproc->domain; | |
b5ab5e24 | 138 | struct device *dev = rproc->dev.parent; |
400e64df OBC |
139 | |
140 | if (!domain) | |
141 | return; | |
142 | ||
143 | iommu_detach_device(domain, dev); | |
144 | iommu_domain_free(domain); | |
400e64df OBC |
145 | } |
146 | ||
086d0872 | 147 | phys_addr_t rproc_va_to_pa(void *cpu_addr) |
eb30596e LP |
148 | { |
149 | /* | |
150 | * Return physical address according to virtual address location | |
151 | * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent | |
152 | * - in kernel: if region allocated in generic dma memory pool | |
153 | */ | |
154 | if (is_vmalloc_addr(cpu_addr)) { | |
155 | return page_to_phys(vmalloc_to_page(cpu_addr)) + | |
156 | offset_in_page(cpu_addr); | |
157 | } | |
158 | ||
159 | WARN_ON(!virt_addr_valid(cpu_addr)); | |
160 | return virt_to_phys(cpu_addr); | |
161 | } | |
086d0872 | 162 | EXPORT_SYMBOL(rproc_va_to_pa); |
eb30596e | 163 | |
a01f7cd6 SA |
164 | /** |
165 | * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address | |
166 | * @rproc: handle of a remote processor | |
167 | * @da: remoteproc device address to translate | |
168 | * @len: length of the memory region @da is pointing to | |
95347e73 | 169 | * @is_iomem: optional pointer filled in to indicate if @da is iomapped memory |
a01f7cd6 | 170 | * |
400e64df OBC |
171 | * Some remote processors will ask us to allocate them physically contiguous |
172 | * memory regions (which we call "carveouts"), and map them to specific | |
a01f7cd6 SA |
173 | * device addresses (which are hardcoded in the firmware). They may also have |
174 | * dedicated memory regions internal to the processors, and use them either | |
175 | * exclusively or alongside carveouts. | |
400e64df OBC |
176 | * |
177 | * They may then ask us to copy objects into specific device addresses (e.g. | |
178 | * code/data sections) or expose us certain symbols in other device address | |
179 | * (e.g. their trace buffer). | |
180 | * | |
a01f7cd6 SA |
181 | * This function is a helper function with which we can go over the allocated |
182 | * carveouts and translate specific device addresses to kernel virtual addresses | |
183 | * so we can access the referenced memory. This function also allows to perform | |
184 | * translations on the internal remoteproc memory regions through a platform | |
185 | * implementation specific da_to_va ops, if present. | |
186 | * | |
400e64df OBC |
187 | * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, |
188 | * but only on kernel direct mapped RAM memory. Instead, we're just using | |
a01f7cd6 SA |
189 | * here the output of the DMA API for the carveouts, which should be more |
190 | * correct. | |
f2867434 SA |
191 | * |
192 | * Return: a valid kernel address on success or NULL on failure | |
400e64df | 193 | */ |
40df0a91 | 194 | void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) |
400e64df OBC |
195 | { |
196 | struct rproc_mem_entry *carveout; | |
197 | void *ptr = NULL; | |
198 | ||
a01f7cd6 | 199 | if (rproc->ops->da_to_va) { |
40df0a91 | 200 | ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem); |
a01f7cd6 SA |
201 | if (ptr) |
202 | goto out; | |
203 | } | |
204 | ||
400e64df OBC |
205 | list_for_each_entry(carveout, &rproc->carveouts, node) { |
206 | int offset = da - carveout->da; | |
207 | ||
74457c40 LP |
208 | /* Verify that carveout is allocated */ |
209 | if (!carveout->va) | |
210 | continue; | |
211 | ||
400e64df OBC |
212 | /* try next carveout if da is too small */ |
213 | if (offset < 0) | |
214 | continue; | |
215 | ||
216 | /* try next carveout if da is too large */ | |
217 | if (offset + len > carveout->len) | |
218 | continue; | |
219 | ||
220 | ptr = carveout->va + offset; | |
221 | ||
40df0a91 PF |
222 | if (is_iomem) |
223 | *is_iomem = carveout->is_iomem; | |
224 | ||
400e64df OBC |
225 | break; |
226 | } | |
227 | ||
a01f7cd6 | 228 | out: |
400e64df OBC |
229 | return ptr; |
230 | } | |
4afc89d6 | 231 | EXPORT_SYMBOL(rproc_da_to_va); |
400e64df | 232 | |
b0019ccd LP |
233 | /** |
234 | * rproc_find_carveout_by_name() - lookup the carveout region by a name | |
235 | * @rproc: handle of a remote processor | |
2e7d4c2c AP |
236 | * @name: carveout name to find (format string) |
237 | * @...: optional parameters matching @name string | |
b0019ccd LP |
238 | * |
239 | * Platform driver has the capability to register some pre-allacoted carveout | |
240 | * (physically contiguous memory regions) before rproc firmware loading and | |
241 | * associated resource table analysis. These regions may be dedicated memory | |
242 | * regions internal to the coprocessor or specified DDR region with specific | |
243 | * attributes | |
244 | * | |
245 | * This function is a helper function with which we can go over the | |
246 | * allocated carveouts and return associated region characteristics like | |
247 | * coprocessor address, length or processor virtual address. | |
248 | * | |
249 | * Return: a valid pointer on carveout entry on success or NULL on failure. | |
250 | */ | |
7e05c8de | 251 | __printf(2, 3) |
b0019ccd LP |
252 | struct rproc_mem_entry * |
253 | rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) | |
254 | { | |
255 | va_list args; | |
256 | char _name[32]; | |
257 | struct rproc_mem_entry *carveout, *mem = NULL; | |
258 | ||
259 | if (!name) | |
260 | return NULL; | |
261 | ||
262 | va_start(args, name); | |
263 | vsnprintf(_name, sizeof(_name), name, args); | |
264 | va_end(args); | |
265 | ||
266 | list_for_each_entry(carveout, &rproc->carveouts, node) { | |
267 | /* Compare carveout and requested names */ | |
268 | if (!strcmp(carveout->name, _name)) { | |
269 | mem = carveout; | |
270 | break; | |
271 | } | |
272 | } | |
273 | ||
274 | return mem; | |
275 | } | |
276 | ||
c874bf59 LP |
277 | /** |
278 | * rproc_check_carveout_da() - Check specified carveout da configuration | |
279 | * @rproc: handle of a remote processor | |
280 | * @mem: pointer on carveout to check | |
281 | * @da: area device address | |
282 | * @len: associated area size | |
283 | * | |
284 | * This function is a helper function to verify requested device area (couple | |
28d7d5c6 LP |
285 | * da, len) is part of specified carveout. |
286 | * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is | |
287 | * checked. | |
c874bf59 | 288 | * |
28d7d5c6 | 289 | * Return: 0 if carveout matches request else error |
c874bf59 | 290 | */ |
28d7d5c6 LP |
291 | static int rproc_check_carveout_da(struct rproc *rproc, |
292 | struct rproc_mem_entry *mem, u32 da, u32 len) | |
c874bf59 LP |
293 | { |
294 | struct device *dev = &rproc->dev; | |
28d7d5c6 | 295 | int delta; |
c874bf59 LP |
296 | |
297 | /* Check requested resource length */ | |
298 | if (len > mem->len) { | |
299 | dev_err(dev, "Registered carveout doesn't fit len request\n"); | |
28d7d5c6 | 300 | return -EINVAL; |
c874bf59 LP |
301 | } |
302 | ||
303 | if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { | |
28d7d5c6 LP |
304 | /* Address doesn't match registered carveout configuration */ |
305 | return -EINVAL; | |
c874bf59 LP |
306 | } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { |
307 | delta = da - mem->da; | |
308 | ||
309 | /* Check requested resource belongs to registered carveout */ | |
310 | if (delta < 0) { | |
311 | dev_err(dev, | |
312 | "Registered carveout doesn't fit da request\n"); | |
28d7d5c6 | 313 | return -EINVAL; |
c874bf59 LP |
314 | } |
315 | ||
316 | if (delta + len > mem->len) { | |
317 | dev_err(dev, | |
318 | "Registered carveout doesn't fit len request\n"); | |
28d7d5c6 | 319 | return -EINVAL; |
c874bf59 LP |
320 | } |
321 | } | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
6db20ea8 | 326 | int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) |
400e64df | 327 | { |
7a186941 | 328 | struct rproc *rproc = rvdev->rproc; |
b5ab5e24 | 329 | struct device *dev = &rproc->dev; |
6db20ea8 | 330 | struct rproc_vring *rvring = &rvdev->vring[i]; |
c0d63157 | 331 | struct fw_rsc_vdev *rsc; |
096ee786 | 332 | int ret, notifyid; |
c6aed238 | 333 | struct rproc_mem_entry *mem; |
096ee786 | 334 | size_t size; |
400e64df | 335 | |
7a186941 | 336 | /* actual size of vring (in bytes) */ |
6db20ea8 | 337 | size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); |
7a186941 | 338 | |
c6aed238 LP |
339 | rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; |
340 | ||
341 | /* Search for pre-registered carveout */ | |
342 | mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, | |
343 | i); | |
344 | if (mem) { | |
345 | if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) | |
346 | return -ENOMEM; | |
347 | } else { | |
348 | /* Register carveout in in list */ | |
99cf0361 BDC |
349 | mem = rproc_mem_entry_init(dev, NULL, 0, |
350 | size, rsc->vring[i].da, | |
c6aed238 LP |
351 | rproc_alloc_carveout, |
352 | rproc_release_carveout, | |
353 | "vdev%dvring%d", | |
354 | rvdev->index, i); | |
355 | if (!mem) { | |
356 | dev_err(dev, "Can't allocate memory entry structure\n"); | |
357 | return -ENOMEM; | |
358 | } | |
359 | ||
360 | rproc_add_carveout(rproc, mem); | |
400e64df OBC |
361 | } |
362 | ||
6db20ea8 OBC |
363 | /* |
364 | * Assign an rproc-wide unique index for this vring | |
365 | * TODO: assign a notifyid for rvdev updates as well | |
6db20ea8 OBC |
366 | * TODO: support predefined notifyids (via resource table) |
367 | */ | |
15fc6110 | 368 | ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); |
b39599b7 | 369 | if (ret < 0) { |
15fc6110 | 370 | dev_err(dev, "idr_alloc failed: %d\n", ret); |
7a186941 OBC |
371 | return ret; |
372 | } | |
15fc6110 | 373 | notifyid = ret; |
400e64df | 374 | |
48f18f89 BA |
375 | /* Potentially bump max_notifyid */ |
376 | if (notifyid > rproc->max_notifyid) | |
377 | rproc->max_notifyid = notifyid; | |
378 | ||
6db20ea8 | 379 | rvring->notifyid = notifyid; |
400e64df | 380 | |
c6aed238 | 381 | /* Let the rproc know the notifyid of this vring.*/ |
c0d63157 | 382 | rsc->vring[i].notifyid = notifyid; |
400e64df OBC |
383 | return 0; |
384 | } | |
385 | ||
6db20ea8 OBC |
386 | static int |
387 | rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) | |
7a186941 OBC |
388 | { |
389 | struct rproc *rproc = rvdev->rproc; | |
b5ab5e24 | 390 | struct device *dev = &rproc->dev; |
6db20ea8 OBC |
391 | struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; |
392 | struct rproc_vring *rvring = &rvdev->vring[i]; | |
7a186941 | 393 | |
9d7814a9 | 394 | dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", |
730f84ce | 395 | i, vring->da, vring->num, vring->align); |
7a186941 | 396 | |
6db20ea8 OBC |
397 | /* verify queue size and vring alignment are sane */ |
398 | if (!vring->num || !vring->align) { | |
399 | dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", | |
730f84ce | 400 | vring->num, vring->align); |
6db20ea8 | 401 | return -EINVAL; |
7a186941 | 402 | } |
6db20ea8 OBC |
403 | |
404 | rvring->len = vring->num; | |
405 | rvring->align = vring->align; | |
406 | rvring->rvdev = rvdev; | |
407 | ||
408 | return 0; | |
409 | } | |
410 | ||
411 | void rproc_free_vring(struct rproc_vring *rvring) | |
412 | { | |
6db20ea8 | 413 | struct rproc *rproc = rvring->rvdev->rproc; |
00a0eec5 | 414 | int idx = rvring - rvring->rvdev->vring; |
c0d63157 | 415 | struct fw_rsc_vdev *rsc; |
6db20ea8 | 416 | |
6db20ea8 | 417 | idr_remove(&rproc->notifyids, rvring->notifyid); |
099a3f33 | 418 | |
4d3ebb3b MP |
419 | /* |
420 | * At this point rproc_stop() has been called and the installed resource | |
421 | * table in the remote processor memory may no longer be accessible. As | |
422 | * such and as per rproc_stop(), rproc->table_ptr points to the cached | |
423 | * resource table (rproc->cached_table). The cached resource table is | |
424 | * only available when a remote processor has been booted by the | |
425 | * remoteproc core, otherwise it is NULL. | |
426 | * | |
427 | * Based on the above, reset the virtio device section in the cached | |
428 | * resource table only if there is one to work with. | |
429 | */ | |
430 | if (rproc->table_ptr) { | |
431 | rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; | |
432 | rsc->vring[idx].da = 0; | |
433 | rsc->vring[idx].notifyid = -1; | |
434 | } | |
7a186941 OBC |
435 | } |
436 | ||
6f8b0373 | 437 | static int rproc_vdev_do_start(struct rproc_subdev *subdev) |
f5bcb353 BA |
438 | { |
439 | struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); | |
440 | ||
441 | return rproc_add_virtio_dev(rvdev, rvdev->id); | |
442 | } | |
443 | ||
6f8b0373 | 444 | static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed) |
f5bcb353 BA |
445 | { |
446 | struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); | |
d4c036fe | 447 | int ret; |
f5bcb353 | 448 | |
d4c036fe LP |
449 | ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev); |
450 | if (ret) | |
451 | dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret); | |
f5bcb353 BA |
452 | } |
453 | ||
086d0872 LP |
454 | /** |
455 | * rproc_rvdev_release() - release the existence of a rvdev | |
456 | * | |
457 | * @dev: the subdevice's dev | |
458 | */ | |
459 | static void rproc_rvdev_release(struct device *dev) | |
460 | { | |
461 | struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev); | |
462 | ||
463 | of_reserved_mem_device_release(dev); | |
464 | ||
465 | kfree(rvdev); | |
466 | } | |
467 | ||
e0d07278 JQ |
468 | static int copy_dma_range_map(struct device *to, struct device *from) |
469 | { | |
470 | const struct bus_dma_region *map = from->dma_range_map, *new_map, *r; | |
471 | int num_ranges = 0; | |
472 | ||
473 | if (!map) | |
474 | return 0; | |
475 | ||
476 | for (r = map; r->size; r++) | |
477 | num_ranges++; | |
478 | ||
479 | new_map = kmemdup(map, array_size(num_ranges + 1, sizeof(*map)), | |
480 | GFP_KERNEL); | |
481 | if (!new_map) | |
482 | return -ENOMEM; | |
483 | to->dma_range_map = new_map; | |
484 | return 0; | |
485 | } | |
486 | ||
400e64df | 487 | /** |
fd2c15ec | 488 | * rproc_handle_vdev() - handle a vdev fw resource |
400e64df | 489 | * @rproc: the remote processor |
2bf23461 | 490 | * @ptr: the vring resource descriptor |
2e7d4c2c | 491 | * @offset: offset of the resource entry |
fd2c15ec | 492 | * @avail: size of available data (for sanity checking the image) |
400e64df | 493 | * |
7a186941 OBC |
494 | * This resource entry requests the host to statically register a virtio |
495 | * device (vdev), and setup everything needed to support it. It contains | |
496 | * everything needed to make it possible: the virtio device id, virtio | |
497 | * device features, vrings information, virtio config space, etc... | |
498 | * | |
499 | * Before registering the vdev, the vrings are allocated from non-cacheable | |
500 | * physically contiguous memory. Currently we only support two vrings per | |
501 | * remote processor (temporary limitation). We might also want to consider | |
502 | * doing the vring allocation only later when ->find_vqs() is invoked, and | |
503 | * then release them upon ->del_vqs(). | |
504 | * | |
505 | * Note: @da is currently not really handled correctly: we dynamically | |
506 | * allocate it using the DMA API, ignoring requested hard coded addresses, | |
507 | * and we don't take care of any required IOMMU programming. This is all | |
508 | * going to be taken care of when the generic iommu-based DMA API will be | |
509 | * merged. Meanwhile, statically-addressed iommu-based firmware images should | |
510 | * use RSC_DEVMEM resource entries to map their required @da to the physical | |
511 | * address of their base CMA region (ouch, hacky!). | |
400e64df | 512 | * |
f2867434 | 513 | * Return: 0 on success, or an appropriate error code otherwise |
400e64df | 514 | */ |
2bf23461 | 515 | static int rproc_handle_vdev(struct rproc *rproc, void *ptr, |
730f84ce | 516 | int offset, int avail) |
400e64df | 517 | { |
2bf23461 | 518 | struct fw_rsc_vdev *rsc = ptr; |
b5ab5e24 | 519 | struct device *dev = &rproc->dev; |
7a186941 OBC |
520 | struct rproc_vdev *rvdev; |
521 | int i, ret; | |
086d0872 | 522 | char name[16]; |
400e64df | 523 | |
fd2c15ec | 524 | /* make sure resource isn't truncated */ |
c8784657 GS |
525 | if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len > |
526 | avail) { | |
b5ab5e24 | 527 | dev_err(dev, "vdev rsc is truncated\n"); |
400e64df OBC |
528 | return -EINVAL; |
529 | } | |
530 | ||
fd2c15ec OBC |
531 | /* make sure reserved bytes are zeroes */ |
532 | if (rsc->reserved[0] || rsc->reserved[1]) { | |
533 | dev_err(dev, "vdev rsc has non zero reserved bytes\n"); | |
400e64df OBC |
534 | return -EINVAL; |
535 | } | |
536 | ||
9d7814a9 | 537 | dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", |
fd2c15ec OBC |
538 | rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); |
539 | ||
7a186941 OBC |
540 | /* we currently support only two vrings per rvdev */ |
541 | if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { | |
fd2c15ec | 542 | dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); |
400e64df OBC |
543 | return -EINVAL; |
544 | } | |
545 | ||
899585ad | 546 | rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); |
7a186941 OBC |
547 | if (!rvdev) |
548 | return -ENOMEM; | |
400e64df | 549 | |
aab8d802 BA |
550 | kref_init(&rvdev->refcount); |
551 | ||
f5bcb353 | 552 | rvdev->id = rsc->id; |
7a186941 | 553 | rvdev->rproc = rproc; |
c6aed238 | 554 | rvdev->index = rproc->nb_vdev++; |
400e64df | 555 | |
086d0872 LP |
556 | /* Initialise vdev subdevice */ |
557 | snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index); | |
c774ad01 | 558 | rvdev->dev.parent = &rproc->dev; |
086d0872 LP |
559 | rvdev->dev.release = rproc_rvdev_release; |
560 | dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name); | |
561 | dev_set_drvdata(&rvdev->dev, rvdev); | |
562 | ||
563 | ret = device_register(&rvdev->dev); | |
564 | if (ret) { | |
565 | put_device(&rvdev->dev); | |
566 | return ret; | |
567 | } | |
0374a4ea CJ |
568 | |
569 | ret = copy_dma_range_map(&rvdev->dev, rproc->dev.parent); | |
570 | if (ret) | |
571 | goto free_rvdev; | |
572 | ||
086d0872 LP |
573 | /* Make device dma capable by inheriting from parent's capabilities */ |
574 | set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent)); | |
575 | ||
576 | ret = dma_coerce_mask_and_coherent(&rvdev->dev, | |
577 | dma_get_mask(rproc->dev.parent)); | |
578 | if (ret) { | |
579 | dev_warn(dev, | |
7efb1425 MPT |
580 | "Failed to set DMA mask %llx. Trying to continue... (%pe)\n", |
581 | dma_get_mask(rproc->dev.parent), ERR_PTR(ret)); | |
086d0872 LP |
582 | } |
583 | ||
6db20ea8 | 584 | /* parse the vrings */ |
7a186941 | 585 | for (i = 0; i < rsc->num_of_vrings; i++) { |
6db20ea8 | 586 | ret = rproc_parse_vring(rvdev, rsc, i); |
7a186941 | 587 | if (ret) |
6db20ea8 | 588 | goto free_rvdev; |
7a186941 | 589 | } |
400e64df | 590 | |
a2b950ac OBC |
591 | /* remember the resource offset*/ |
592 | rvdev->rsc_offset = offset; | |
fd2c15ec | 593 | |
a863af5d BA |
594 | /* allocate the vring resources */ |
595 | for (i = 0; i < rsc->num_of_vrings; i++) { | |
596 | ret = rproc_alloc_vring(rvdev, i); | |
597 | if (ret) | |
598 | goto unwind_vring_allocations; | |
599 | } | |
600 | ||
7a186941 | 601 | list_add_tail(&rvdev->node, &rproc->rvdevs); |
fd2c15ec | 602 | |
6f8b0373 AE |
603 | rvdev->subdev.start = rproc_vdev_do_start; |
604 | rvdev->subdev.stop = rproc_vdev_do_stop; | |
4902676f BA |
605 | |
606 | rproc_add_subdev(rproc, &rvdev->subdev); | |
400e64df OBC |
607 | |
608 | return 0; | |
7a186941 | 609 | |
a863af5d BA |
610 | unwind_vring_allocations: |
611 | for (i--; i >= 0; i--) | |
612 | rproc_free_vring(&rvdev->vring[i]); | |
6db20ea8 | 613 | free_rvdev: |
086d0872 | 614 | device_unregister(&rvdev->dev); |
7a186941 | 615 | return ret; |
400e64df OBC |
616 | } |
617 | ||
aab8d802 BA |
618 | void rproc_vdev_release(struct kref *ref) |
619 | { | |
620 | struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount); | |
a863af5d | 621 | struct rproc_vring *rvring; |
f5bcb353 | 622 | struct rproc *rproc = rvdev->rproc; |
a863af5d BA |
623 | int id; |
624 | ||
625 | for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) { | |
626 | rvring = &rvdev->vring[id]; | |
a863af5d BA |
627 | rproc_free_vring(rvring); |
628 | } | |
aab8d802 | 629 | |
f5bcb353 | 630 | rproc_remove_subdev(rproc, &rvdev->subdev); |
aab8d802 | 631 | list_del(&rvdev->node); |
086d0872 | 632 | device_unregister(&rvdev->dev); |
aab8d802 BA |
633 | } |
634 | ||
400e64df OBC |
635 | /** |
636 | * rproc_handle_trace() - handle a shared trace buffer resource | |
637 | * @rproc: the remote processor | |
2bf23461 | 638 | * @ptr: the trace resource descriptor |
2e7d4c2c | 639 | * @offset: offset of the resource entry |
fd2c15ec | 640 | * @avail: size of available data (for sanity checking the image) |
400e64df OBC |
641 | * |
642 | * In case the remote processor dumps trace logs into memory, | |
643 | * export it via debugfs. | |
644 | * | |
645 | * Currently, the 'da' member of @rsc should contain the device address | |
646 | * where the remote processor is dumping the traces. Later we could also | |
647 | * support dynamically allocating this address using the generic | |
648 | * DMA API (but currently there isn't a use case for that). | |
649 | * | |
f2867434 | 650 | * Return: 0 on success, or an appropriate error code otherwise |
400e64df | 651 | */ |
2bf23461 | 652 | static int rproc_handle_trace(struct rproc *rproc, void *ptr, |
730f84ce | 653 | int offset, int avail) |
400e64df | 654 | { |
2bf23461 | 655 | struct fw_rsc_trace *rsc = ptr; |
a987e6b9 | 656 | struct rproc_debug_trace *trace; |
b5ab5e24 | 657 | struct device *dev = &rproc->dev; |
400e64df OBC |
658 | char name[15]; |
659 | ||
fd2c15ec | 660 | if (sizeof(*rsc) > avail) { |
b5ab5e24 | 661 | dev_err(dev, "trace rsc is truncated\n"); |
fd2c15ec OBC |
662 | return -EINVAL; |
663 | } | |
664 | ||
665 | /* make sure reserved bytes are zeroes */ | |
666 | if (rsc->reserved) { | |
667 | dev_err(dev, "trace rsc has non zero reserved bytes\n"); | |
668 | return -EINVAL; | |
669 | } | |
670 | ||
400e64df | 671 | trace = kzalloc(sizeof(*trace), GFP_KERNEL); |
172e6ab1 | 672 | if (!trace) |
400e64df | 673 | return -ENOMEM; |
400e64df OBC |
674 | |
675 | /* set the trace buffer dma properties */ | |
a987e6b9 LP |
676 | trace->trace_mem.len = rsc->len; |
677 | trace->trace_mem.da = rsc->da; | |
678 | ||
679 | /* set pointer on rproc device */ | |
680 | trace->rproc = rproc; | |
400e64df OBC |
681 | |
682 | /* make sure snprintf always null terminates, even if truncating */ | |
683 | snprintf(name, sizeof(name), "trace%d", rproc->num_traces); | |
684 | ||
685 | /* create the debugfs entry */ | |
a987e6b9 LP |
686 | trace->tfile = rproc_create_trace_file(name, rproc, trace); |
687 | if (!trace->tfile) { | |
400e64df OBC |
688 | kfree(trace); |
689 | return -EINVAL; | |
690 | } | |
691 | ||
692 | list_add_tail(&trace->node, &rproc->traces); | |
693 | ||
694 | rproc->num_traces++; | |
695 | ||
a987e6b9 LP |
696 | dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n", |
697 | name, rsc->da, rsc->len); | |
400e64df OBC |
698 | |
699 | return 0; | |
700 | } | |
701 | ||
702 | /** | |
703 | * rproc_handle_devmem() - handle devmem resource entry | |
704 | * @rproc: remote processor handle | |
2bf23461 | 705 | * @ptr: the devmem resource entry |
2e7d4c2c | 706 | * @offset: offset of the resource entry |
fd2c15ec | 707 | * @avail: size of available data (for sanity checking the image) |
400e64df OBC |
708 | * |
709 | * Remote processors commonly need to access certain on-chip peripherals. | |
710 | * | |
711 | * Some of these remote processors access memory via an iommu device, | |
712 | * and might require us to configure their iommu before they can access | |
713 | * the on-chip peripherals they need. | |
714 | * | |
715 | * This resource entry is a request to map such a peripheral device. | |
716 | * | |
717 | * These devmem entries will contain the physical address of the device in | |
718 | * the 'pa' member. If a specific device address is expected, then 'da' will | |
719 | * contain it (currently this is the only use case supported). 'len' will | |
720 | * contain the size of the physical region we need to map. | |
721 | * | |
722 | * Currently we just "trust" those devmem entries to contain valid physical | |
723 | * addresses, but this is going to change: we want the implementations to | |
724 | * tell us ranges of physical addresses the firmware is allowed to request, | |
725 | * and not allow firmwares to request access to physical addresses that | |
726 | * are outside those ranges. | |
f2867434 SA |
727 | * |
728 | * Return: 0 on success, or an appropriate error code otherwise | |
400e64df | 729 | */ |
2bf23461 | 730 | static int rproc_handle_devmem(struct rproc *rproc, void *ptr, |
730f84ce | 731 | int offset, int avail) |
400e64df | 732 | { |
2bf23461 | 733 | struct fw_rsc_devmem *rsc = ptr; |
400e64df | 734 | struct rproc_mem_entry *mapping; |
b5ab5e24 | 735 | struct device *dev = &rproc->dev; |
400e64df OBC |
736 | int ret; |
737 | ||
738 | /* no point in handling this resource without a valid iommu domain */ | |
739 | if (!rproc->domain) | |
740 | return -EINVAL; | |
741 | ||
fd2c15ec | 742 | if (sizeof(*rsc) > avail) { |
b5ab5e24 | 743 | dev_err(dev, "devmem rsc is truncated\n"); |
fd2c15ec OBC |
744 | return -EINVAL; |
745 | } | |
746 | ||
747 | /* make sure reserved bytes are zeroes */ | |
748 | if (rsc->reserved) { | |
b5ab5e24 | 749 | dev_err(dev, "devmem rsc has non zero reserved bytes\n"); |
fd2c15ec OBC |
750 | return -EINVAL; |
751 | } | |
752 | ||
400e64df | 753 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
172e6ab1 | 754 | if (!mapping) |
400e64df | 755 | return -ENOMEM; |
400e64df OBC |
756 | |
757 | ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); | |
758 | if (ret) { | |
b5ab5e24 | 759 | dev_err(dev, "failed to map devmem: %d\n", ret); |
400e64df OBC |
760 | goto out; |
761 | } | |
762 | ||
763 | /* | |
764 | * We'll need this info later when we'll want to unmap everything | |
765 | * (e.g. on shutdown). | |
766 | * | |
767 | * We can't trust the remote processor not to change the resource | |
768 | * table, so we must maintain this info independently. | |
769 | */ | |
770 | mapping->da = rsc->da; | |
771 | mapping->len = rsc->len; | |
772 | list_add_tail(&mapping->node, &rproc->mappings); | |
773 | ||
b5ab5e24 | 774 | dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", |
730f84ce | 775 | rsc->pa, rsc->da, rsc->len); |
400e64df OBC |
776 | |
777 | return 0; | |
778 | ||
779 | out: | |
780 | kfree(mapping); | |
781 | return ret; | |
782 | } | |
783 | ||
f2e74abf | 784 | /** |
d7c51706 | 785 | * rproc_alloc_carveout() - allocated specified carveout |
f2e74abf | 786 | * @rproc: rproc handle |
d7c51706 | 787 | * @mem: the memory entry to allocate |
400e64df | 788 | * |
d7c51706 LP |
789 | * This function allocate specified memory entry @mem using |
790 | * dma_alloc_coherent() as default allocator | |
f2867434 SA |
791 | * |
792 | * Return: 0 on success, or an appropriate error code otherwise | |
400e64df | 793 | */ |
d7c51706 LP |
794 | static int rproc_alloc_carveout(struct rproc *rproc, |
795 | struct rproc_mem_entry *mem) | |
400e64df | 796 | { |
d7c51706 | 797 | struct rproc_mem_entry *mapping = NULL; |
b5ab5e24 | 798 | struct device *dev = &rproc->dev; |
400e64df OBC |
799 | dma_addr_t dma; |
800 | void *va; | |
801 | int ret; | |
802 | ||
d7c51706 | 803 | va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); |
400e64df | 804 | if (!va) { |
9c219b23 | 805 | dev_err(dev->parent, |
096ee786 CL |
806 | "failed to allocate dma memory: len 0x%zx\n", |
807 | mem->len); | |
72029c90 | 808 | return -ENOMEM; |
400e64df OBC |
809 | } |
810 | ||
096ee786 | 811 | dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n", |
d7c51706 | 812 | va, &dma, mem->len); |
400e64df | 813 | |
60f849a5 LP |
814 | if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { |
815 | /* | |
816 | * Check requested da is equal to dma address | |
817 | * and print a warn message in case of missalignment. | |
818 | * Don't stop rproc_start sequence as coprocessor may | |
819 | * build pa to da translation on its side. | |
820 | */ | |
821 | if (mem->da != (u32)dma) | |
822 | dev_warn(dev->parent, | |
823 | "Allocated carveout doesn't fit device address request\n"); | |
824 | } | |
825 | ||
400e64df OBC |
826 | /* |
827 | * Ok, this is non-standard. | |
828 | * | |
829 | * Sometimes we can't rely on the generic iommu-based DMA API | |
830 | * to dynamically allocate the device address and then set the IOMMU | |
831 | * tables accordingly, because some remote processors might | |
832 | * _require_ us to use hard coded device addresses that their | |
833 | * firmware was compiled with. | |
834 | * | |
835 | * In this case, we must use the IOMMU API directly and map | |
836 | * the memory to the device address as expected by the remote | |
837 | * processor. | |
838 | * | |
839 | * Obviously such remote processor devices should not be configured | |
840 | * to use the iommu-based DMA API: we expect 'dma' to contain the | |
841 | * physical address in this case. | |
842 | */ | |
60f849a5 | 843 | if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { |
7168d914 DC |
844 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
845 | if (!mapping) { | |
7168d914 DC |
846 | ret = -ENOMEM; |
847 | goto dma_free; | |
848 | } | |
849 | ||
d7c51706 LP |
850 | ret = iommu_map(rproc->domain, mem->da, dma, mem->len, |
851 | mem->flags); | |
400e64df OBC |
852 | if (ret) { |
853 | dev_err(dev, "iommu_map failed: %d\n", ret); | |
7168d914 | 854 | goto free_mapping; |
400e64df OBC |
855 | } |
856 | ||
857 | /* | |
858 | * We'll need this info later when we'll want to unmap | |
859 | * everything (e.g. on shutdown). | |
860 | * | |
861 | * We can't trust the remote processor not to change the | |
862 | * resource table, so we must maintain this info independently. | |
863 | */ | |
d7c51706 LP |
864 | mapping->da = mem->da; |
865 | mapping->len = mem->len; | |
400e64df OBC |
866 | list_add_tail(&mapping->node, &rproc->mappings); |
867 | ||
b605ed8b | 868 | dev_dbg(dev, "carveout mapped 0x%x to %pad\n", |
d7c51706 | 869 | mem->da, &dma); |
60f849a5 LP |
870 | } |
871 | ||
872 | if (mem->da == FW_RSC_ADDR_ANY) { | |
b36de8cf LP |
873 | /* Update device address as undefined by requester */ |
874 | if ((u64)dma & HIGH_BITS_MASK) | |
875 | dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); | |
876 | ||
d7c51706 | 877 | mem->da = (u32)dma; |
400e64df OBC |
878 | } |
879 | ||
80137b40 | 880 | mem->dma = dma; |
d7c51706 | 881 | mem->va = va; |
400e64df OBC |
882 | |
883 | return 0; | |
884 | ||
7168d914 DC |
885 | free_mapping: |
886 | kfree(mapping); | |
400e64df | 887 | dma_free: |
d7c51706 | 888 | dma_free_coherent(dev->parent, mem->len, va, dma); |
400e64df OBC |
889 | return ret; |
890 | } | |
891 | ||
d7c51706 LP |
892 | /** |
893 | * rproc_release_carveout() - release acquired carveout | |
894 | * @rproc: rproc handle | |
895 | * @mem: the memory entry to release | |
896 | * | |
897 | * This function releases specified memory entry @mem allocated via | |
898 | * rproc_alloc_carveout() function by @rproc. | |
f2867434 SA |
899 | * |
900 | * Return: 0 on success, or an appropriate error code otherwise | |
d7c51706 LP |
901 | */ |
902 | static int rproc_release_carveout(struct rproc *rproc, | |
903 | struct rproc_mem_entry *mem) | |
904 | { | |
905 | struct device *dev = &rproc->dev; | |
906 | ||
907 | /* clean up carveout allocations */ | |
908 | dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); | |
909 | return 0; | |
910 | } | |
911 | ||
912 | /** | |
913 | * rproc_handle_carveout() - handle phys contig memory allocation requests | |
914 | * @rproc: rproc handle | |
2bf23461 | 915 | * @ptr: the resource entry |
2e7d4c2c | 916 | * @offset: offset of the resource entry |
d7c51706 LP |
917 | * @avail: size of available data (for image validation) |
918 | * | |
919 | * This function will handle firmware requests for allocation of physically | |
920 | * contiguous memory regions. | |
921 | * | |
922 | * These request entries should come first in the firmware's resource table, | |
923 | * as other firmware entries might request placing other data objects inside | |
924 | * these memory regions (e.g. data/code segments, trace resource entries, ...). | |
925 | * | |
926 | * Allocating memory this way helps utilizing the reserved physical memory | |
927 | * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries | |
928 | * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB | |
929 | * pressure is important; it may have a substantial impact on performance. | |
f2867434 SA |
930 | * |
931 | * Return: 0 on success, or an appropriate error code otherwise | |
d7c51706 LP |
932 | */ |
933 | static int rproc_handle_carveout(struct rproc *rproc, | |
2bf23461 | 934 | void *ptr, int offset, int avail) |
d7c51706 | 935 | { |
2bf23461 | 936 | struct fw_rsc_carveout *rsc = ptr; |
d7c51706 LP |
937 | struct rproc_mem_entry *carveout; |
938 | struct device *dev = &rproc->dev; | |
939 | ||
940 | if (sizeof(*rsc) > avail) { | |
941 | dev_err(dev, "carveout rsc is truncated\n"); | |
942 | return -EINVAL; | |
943 | } | |
944 | ||
945 | /* make sure reserved bytes are zeroes */ | |
946 | if (rsc->reserved) { | |
947 | dev_err(dev, "carveout rsc has non zero reserved bytes\n"); | |
948 | return -EINVAL; | |
949 | } | |
950 | ||
951 | dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", | |
952 | rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); | |
953 | ||
ffa5f9c8 LP |
954 | /* |
955 | * Check carveout rsc already part of a registered carveout, | |
956 | * Search by name, then check the da and length | |
957 | */ | |
958 | carveout = rproc_find_carveout_by_name(rproc, rsc->name); | |
959 | ||
960 | if (carveout) { | |
961 | if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { | |
962 | dev_err(dev, | |
963 | "Carveout already associated to resource table\n"); | |
964 | return -ENOMEM; | |
965 | } | |
966 | ||
967 | if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) | |
968 | return -ENOMEM; | |
969 | ||
970 | /* Update memory carveout with resource table info */ | |
971 | carveout->rsc_offset = offset; | |
972 | carveout->flags = rsc->flags; | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
d7c51706 | 977 | /* Register carveout in in list */ |
99cf0361 | 978 | carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, |
d7c51706 LP |
979 | rproc_alloc_carveout, |
980 | rproc_release_carveout, rsc->name); | |
981 | if (!carveout) { | |
982 | dev_err(dev, "Can't allocate memory entry structure\n"); | |
983 | return -ENOMEM; | |
984 | } | |
985 | ||
986 | carveout->flags = rsc->flags; | |
987 | carveout->rsc_offset = offset; | |
988 | rproc_add_carveout(rproc, carveout); | |
989 | ||
990 | return 0; | |
991 | } | |
992 | ||
15c0b025 LP |
993 | /** |
994 | * rproc_add_carveout() - register an allocated carveout region | |
995 | * @rproc: rproc handle | |
996 | * @mem: memory entry to register | |
997 | * | |
998 | * This function registers specified memory entry in @rproc carveouts list. | |
999 | * Specified carveout should have been allocated before registering. | |
1000 | */ | |
1001 | void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) | |
1002 | { | |
1003 | list_add_tail(&mem->node, &rproc->carveouts); | |
1004 | } | |
1005 | EXPORT_SYMBOL(rproc_add_carveout); | |
1006 | ||
72029c90 LP |
1007 | /** |
1008 | * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct | |
1009 | * @dev: pointer on device struct | |
1010 | * @va: virtual address | |
1011 | * @dma: dma address | |
1012 | * @len: memory carveout length | |
1013 | * @da: device address | |
a9f6fe0d LP |
1014 | * @alloc: memory carveout allocation function |
1015 | * @release: memory carveout release function | |
72029c90 LP |
1016 | * @name: carveout name |
1017 | * | |
1018 | * This function allocates a rproc_mem_entry struct and fill it with parameters | |
1019 | * provided by client. | |
f2867434 SA |
1020 | * |
1021 | * Return: a valid pointer on success, or NULL on failure | |
72029c90 | 1022 | */ |
7e05c8de | 1023 | __printf(8, 9) |
72029c90 LP |
1024 | struct rproc_mem_entry * |
1025 | rproc_mem_entry_init(struct device *dev, | |
096ee786 | 1026 | void *va, dma_addr_t dma, size_t len, u32 da, |
d7c51706 | 1027 | int (*alloc)(struct rproc *, struct rproc_mem_entry *), |
72029c90 LP |
1028 | int (*release)(struct rproc *, struct rproc_mem_entry *), |
1029 | const char *name, ...) | |
1030 | { | |
1031 | struct rproc_mem_entry *mem; | |
1032 | va_list args; | |
1033 | ||
1034 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); | |
1035 | if (!mem) | |
1036 | return mem; | |
1037 | ||
1038 | mem->va = va; | |
1039 | mem->dma = dma; | |
1040 | mem->da = da; | |
1041 | mem->len = len; | |
d7c51706 | 1042 | mem->alloc = alloc; |
72029c90 | 1043 | mem->release = release; |
d7c51706 | 1044 | mem->rsc_offset = FW_RSC_ADDR_ANY; |
1429cca1 | 1045 | mem->of_resm_idx = -1; |
72029c90 LP |
1046 | |
1047 | va_start(args, name); | |
1048 | vsnprintf(mem->name, sizeof(mem->name), name, args); | |
1049 | va_end(args); | |
1050 | ||
1051 | return mem; | |
1052 | } | |
1053 | EXPORT_SYMBOL(rproc_mem_entry_init); | |
1054 | ||
1429cca1 LP |
1055 | /** |
1056 | * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct | |
1057 | * from a reserved memory phandle | |
1058 | * @dev: pointer on device struct | |
1059 | * @of_resm_idx: reserved memory phandle index in "memory-region" | |
1060 | * @len: memory carveout length | |
1061 | * @da: device address | |
1062 | * @name: carveout name | |
1063 | * | |
1064 | * This function allocates a rproc_mem_entry struct and fill it with parameters | |
1065 | * provided by client. | |
f2867434 SA |
1066 | * |
1067 | * Return: a valid pointer on success, or NULL on failure | |
1429cca1 | 1068 | */ |
7e05c8de | 1069 | __printf(5, 6) |
1429cca1 | 1070 | struct rproc_mem_entry * |
096ee786 | 1071 | rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len, |
1429cca1 LP |
1072 | u32 da, const char *name, ...) |
1073 | { | |
1074 | struct rproc_mem_entry *mem; | |
1075 | va_list args; | |
1076 | ||
1077 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); | |
1078 | if (!mem) | |
1079 | return mem; | |
1080 | ||
1081 | mem->da = da; | |
1082 | mem->len = len; | |
1083 | mem->rsc_offset = FW_RSC_ADDR_ANY; | |
1084 | mem->of_resm_idx = of_resm_idx; | |
1085 | ||
1086 | va_start(args, name); | |
1087 | vsnprintf(mem->name, sizeof(mem->name), name, args); | |
1088 | va_end(args); | |
1089 | ||
1090 | return mem; | |
1091 | } | |
1092 | EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); | |
1093 | ||
a8aa5ee1 SA |
1094 | /** |
1095 | * rproc_of_parse_firmware() - parse and return the firmware-name | |
1096 | * @dev: pointer on device struct representing a rproc | |
1097 | * @index: index to use for the firmware-name retrieval | |
1098 | * @fw_name: pointer to a character string, in which the firmware | |
1099 | * name is returned on success and unmodified otherwise. | |
1100 | * | |
1101 | * This is an OF helper function that parses a device's DT node for | |
1102 | * the "firmware-name" property and returns the firmware name pointer | |
1103 | * in @fw_name on success. | |
1104 | * | |
1105 | * Return: 0 on success, or an appropriate failure. | |
1106 | */ | |
1107 | int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name) | |
1108 | { | |
1109 | int ret; | |
1110 | ||
1111 | ret = of_property_read_string_index(dev->of_node, "firmware-name", | |
1112 | index, fw_name); | |
1113 | return ret ? ret : 0; | |
1114 | } | |
1115 | EXPORT_SYMBOL(rproc_of_parse_firmware); | |
1116 | ||
2e7d4c2c | 1117 | /* |
e12bc14b OBC |
1118 | * A lookup table for resource handlers. The indices are defined in |
1119 | * enum fw_resource_type. | |
1120 | */ | |
232fcdbb | 1121 | static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { |
2bf23461 JY |
1122 | [RSC_CARVEOUT] = rproc_handle_carveout, |
1123 | [RSC_DEVMEM] = rproc_handle_devmem, | |
1124 | [RSC_TRACE] = rproc_handle_trace, | |
1125 | [RSC_VDEV] = rproc_handle_vdev, | |
232fcdbb SB |
1126 | }; |
1127 | ||
400e64df | 1128 | /* handle firmware resource entries before booting the remote processor */ |
a4b24c75 | 1129 | static int rproc_handle_resources(struct rproc *rproc, |
232fcdbb | 1130 | rproc_handle_resource_t handlers[RSC_LAST]) |
400e64df | 1131 | { |
b5ab5e24 | 1132 | struct device *dev = &rproc->dev; |
e12bc14b | 1133 | rproc_handle_resource_t handler; |
fd2c15ec OBC |
1134 | int ret = 0, i; |
1135 | ||
d4bb86f2 BA |
1136 | if (!rproc->table_ptr) |
1137 | return 0; | |
1138 | ||
a2b950ac OBC |
1139 | for (i = 0; i < rproc->table_ptr->num; i++) { |
1140 | int offset = rproc->table_ptr->offset[i]; | |
1141 | struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; | |
a4b24c75 | 1142 | int avail = rproc->table_sz - offset - sizeof(*hdr); |
fd2c15ec OBC |
1143 | void *rsc = (void *)hdr + sizeof(*hdr); |
1144 | ||
1145 | /* make sure table isn't truncated */ | |
1146 | if (avail < 0) { | |
1147 | dev_err(dev, "rsc table is truncated\n"); | |
1148 | return -EINVAL; | |
1149 | } | |
400e64df | 1150 | |
fd2c15ec | 1151 | dev_dbg(dev, "rsc: type %d\n", hdr->type); |
400e64df | 1152 | |
b1a17513 CL |
1153 | if (hdr->type >= RSC_VENDOR_START && |
1154 | hdr->type <= RSC_VENDOR_END) { | |
1155 | ret = rproc_handle_rsc(rproc, hdr->type, rsc, | |
1156 | offset + sizeof(*hdr), avail); | |
1157 | if (ret == RSC_HANDLED) | |
1158 | continue; | |
1159 | else if (ret < 0) | |
1160 | break; | |
1161 | ||
1162 | dev_warn(dev, "unsupported vendor resource %d\n", | |
1163 | hdr->type); | |
1164 | continue; | |
1165 | } | |
1166 | ||
fd2c15ec OBC |
1167 | if (hdr->type >= RSC_LAST) { |
1168 | dev_warn(dev, "unsupported resource %d\n", hdr->type); | |
e12bc14b | 1169 | continue; |
400e64df OBC |
1170 | } |
1171 | ||
232fcdbb | 1172 | handler = handlers[hdr->type]; |
e12bc14b OBC |
1173 | if (!handler) |
1174 | continue; | |
1175 | ||
a2b950ac | 1176 | ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); |
7a186941 | 1177 | if (ret) |
400e64df | 1178 | break; |
fd2c15ec | 1179 | } |
400e64df OBC |
1180 | |
1181 | return ret; | |
1182 | } | |
1183 | ||
c455daa4 BA |
1184 | static int rproc_prepare_subdevices(struct rproc *rproc) |
1185 | { | |
1186 | struct rproc_subdev *subdev; | |
1187 | int ret; | |
1188 | ||
1189 | list_for_each_entry(subdev, &rproc->subdevs, node) { | |
1190 | if (subdev->prepare) { | |
1191 | ret = subdev->prepare(subdev); | |
1192 | if (ret) | |
1193 | goto unroll_preparation; | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | return 0; | |
1198 | ||
1199 | unroll_preparation: | |
1200 | list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { | |
1201 | if (subdev->unprepare) | |
1202 | subdev->unprepare(subdev); | |
1203 | } | |
1204 | ||
1205 | return ret; | |
1206 | } | |
1207 | ||
618fcff3 | 1208 | static int rproc_start_subdevices(struct rproc *rproc) |
7bdc9650 BA |
1209 | { |
1210 | struct rproc_subdev *subdev; | |
1211 | int ret; | |
1212 | ||
1213 | list_for_each_entry(subdev, &rproc->subdevs, node) { | |
be37b1e0 BA |
1214 | if (subdev->start) { |
1215 | ret = subdev->start(subdev); | |
1216 | if (ret) | |
1217 | goto unroll_registration; | |
1218 | } | |
7bdc9650 BA |
1219 | } |
1220 | ||
1221 | return 0; | |
1222 | ||
1223 | unroll_registration: | |
be37b1e0 BA |
1224 | list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { |
1225 | if (subdev->stop) | |
1226 | subdev->stop(subdev, true); | |
1227 | } | |
7bdc9650 BA |
1228 | |
1229 | return ret; | |
1230 | } | |
1231 | ||
618fcff3 | 1232 | static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) |
7bdc9650 BA |
1233 | { |
1234 | struct rproc_subdev *subdev; | |
1235 | ||
be37b1e0 BA |
1236 | list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { |
1237 | if (subdev->stop) | |
1238 | subdev->stop(subdev, crashed); | |
1239 | } | |
7bdc9650 BA |
1240 | } |
1241 | ||
c455daa4 BA |
1242 | static void rproc_unprepare_subdevices(struct rproc *rproc) |
1243 | { | |
1244 | struct rproc_subdev *subdev; | |
1245 | ||
1246 | list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { | |
1247 | if (subdev->unprepare) | |
1248 | subdev->unprepare(subdev); | |
1249 | } | |
1250 | } | |
1251 | ||
d7c51706 LP |
1252 | /** |
1253 | * rproc_alloc_registered_carveouts() - allocate all carveouts registered | |
1254 | * in the list | |
1255 | * @rproc: the remote processor handle | |
1256 | * | |
1257 | * This function parses registered carveout list, performs allocation | |
1258 | * if alloc() ops registered and updates resource table information | |
1259 | * if rsc_offset set. | |
1260 | * | |
1261 | * Return: 0 on success | |
1262 | */ | |
1263 | static int rproc_alloc_registered_carveouts(struct rproc *rproc) | |
1264 | { | |
1265 | struct rproc_mem_entry *entry, *tmp; | |
1266 | struct fw_rsc_carveout *rsc; | |
1267 | struct device *dev = &rproc->dev; | |
b36de8cf | 1268 | u64 pa; |
d7c51706 LP |
1269 | int ret; |
1270 | ||
1271 | list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { | |
1272 | if (entry->alloc) { | |
1273 | ret = entry->alloc(rproc, entry); | |
1274 | if (ret) { | |
1275 | dev_err(dev, "Unable to allocate carveout %s: %d\n", | |
1276 | entry->name, ret); | |
1277 | return -ENOMEM; | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | if (entry->rsc_offset != FW_RSC_ADDR_ANY) { | |
1282 | /* update resource table */ | |
1283 | rsc = (void *)rproc->table_ptr + entry->rsc_offset; | |
1284 | ||
1285 | /* | |
1286 | * Some remote processors might need to know the pa | |
1287 | * even though they are behind an IOMMU. E.g., OMAP4's | |
1288 | * remote M3 processor needs this so it can control | |
1289 | * on-chip hardware accelerators that are not behind | |
1290 | * the IOMMU, and therefor must know the pa. | |
1291 | * | |
1292 | * Generally we don't want to expose physical addresses | |
1293 | * if we don't have to (remote processors are generally | |
1294 | * _not_ trusted), so we might want to do this only for | |
1295 | * remote processor that _must_ have this (e.g. OMAP4's | |
1296 | * dual M3 subsystem). | |
1297 | * | |
1298 | * Non-IOMMU processors might also want to have this info. | |
1299 | * In this case, the device address and the physical address | |
1300 | * are the same. | |
1301 | */ | |
ffa5f9c8 LP |
1302 | |
1303 | /* Use va if defined else dma to generate pa */ | |
d7c51706 | 1304 | if (entry->va) |
b36de8cf | 1305 | pa = (u64)rproc_va_to_pa(entry->va); |
ffa5f9c8 | 1306 | else |
b36de8cf LP |
1307 | pa = (u64)entry->dma; |
1308 | ||
1309 | if (((u64)pa) & HIGH_BITS_MASK) | |
1310 | dev_warn(dev, | |
1311 | "Physical address cast in 32bit to fit resource table format\n"); | |
ffa5f9c8 | 1312 | |
b36de8cf | 1313 | rsc->pa = (u32)pa; |
ffa5f9c8 LP |
1314 | rsc->da = entry->da; |
1315 | rsc->len = entry->len; | |
d7c51706 LP |
1316 | } |
1317 | } | |
1318 | ||
1319 | return 0; | |
1320 | } | |
1321 | ||
2666ca91 | 1322 | |
400e64df OBC |
1323 | /** |
1324 | * rproc_resource_cleanup() - clean up and free all acquired resources | |
1325 | * @rproc: rproc handle | |
1326 | * | |
1327 | * This function will free all resources acquired for @rproc, and it | |
7a186941 | 1328 | * is called whenever @rproc either shuts down or fails to boot. |
400e64df | 1329 | */ |
d9473cbf | 1330 | void rproc_resource_cleanup(struct rproc *rproc) |
400e64df OBC |
1331 | { |
1332 | struct rproc_mem_entry *entry, *tmp; | |
a987e6b9 | 1333 | struct rproc_debug_trace *trace, *ttmp; |
d81fb32f | 1334 | struct rproc_vdev *rvdev, *rvtmp; |
b5ab5e24 | 1335 | struct device *dev = &rproc->dev; |
400e64df OBC |
1336 | |
1337 | /* clean up debugfs trace entries */ | |
a987e6b9 LP |
1338 | list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { |
1339 | rproc_remove_trace_file(trace->tfile); | |
400e64df | 1340 | rproc->num_traces--; |
a987e6b9 LP |
1341 | list_del(&trace->node); |
1342 | kfree(trace); | |
400e64df OBC |
1343 | } |
1344 | ||
400e64df OBC |
1345 | /* clean up iommu mapping entries */ |
1346 | list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { | |
1347 | size_t unmapped; | |
1348 | ||
1349 | unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); | |
1350 | if (unmapped != entry->len) { | |
1351 | /* nothing much to do besides complaining */ | |
096ee786 | 1352 | dev_err(dev, "failed to unmap %zx/%zu\n", entry->len, |
730f84ce | 1353 | unmapped); |
400e64df OBC |
1354 | } |
1355 | ||
1356 | list_del(&entry->node); | |
1357 | kfree(entry); | |
1358 | } | |
b6356a01 SA |
1359 | |
1360 | /* clean up carveout allocations */ | |
1361 | list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { | |
f2e74abf LP |
1362 | if (entry->release) |
1363 | entry->release(rproc, entry); | |
b6356a01 SA |
1364 | list_del(&entry->node); |
1365 | kfree(entry); | |
1366 | } | |
d81fb32f BA |
1367 | |
1368 | /* clean up remote vdev entries */ | |
f5bcb353 | 1369 | list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) |
2b45cef5 | 1370 | kref_put(&rvdev->refcount, rproc_vdev_release); |
2666ca91 SJ |
1371 | |
1372 | rproc_coredump_cleanup(rproc); | |
400e64df | 1373 | } |
d9473cbf | 1374 | EXPORT_SYMBOL(rproc_resource_cleanup); |
400e64df | 1375 | |
1efa30d0 SJ |
1376 | static int rproc_start(struct rproc *rproc, const struct firmware *fw) |
1377 | { | |
a4b24c75 | 1378 | struct resource_table *loaded_table; |
1efa30d0 | 1379 | struct device *dev = &rproc->dev; |
a4b24c75 | 1380 | int ret; |
1efa30d0 SJ |
1381 | |
1382 | /* load the ELF segments to memory */ | |
1383 | ret = rproc_load_segments(rproc, fw); | |
1384 | if (ret) { | |
1385 | dev_err(dev, "Failed to load program segments: %d\n", ret); | |
1386 | return ret; | |
1387 | } | |
1388 | ||
1389 | /* | |
1390 | * The starting device has been given the rproc->cached_table as the | |
1391 | * resource table. The address of the vring along with the other | |
1392 | * allocated resources (carveouts etc) is stored in cached_table. | |
1393 | * In order to pass this information to the remote device we must copy | |
1394 | * this information to device memory. We also update the table_ptr so | |
1395 | * that any subsequent changes will be applied to the loaded version. | |
1396 | */ | |
1397 | loaded_table = rproc_find_loaded_rsc_table(rproc, fw); | |
1398 | if (loaded_table) { | |
a4b24c75 | 1399 | memcpy(loaded_table, rproc->cached_table, rproc->table_sz); |
1efa30d0 SJ |
1400 | rproc->table_ptr = loaded_table; |
1401 | } | |
1402 | ||
c455daa4 BA |
1403 | ret = rproc_prepare_subdevices(rproc); |
1404 | if (ret) { | |
1405 | dev_err(dev, "failed to prepare subdevices for %s: %d\n", | |
1406 | rproc->name, ret); | |
f68d51bd | 1407 | goto reset_table_ptr; |
c455daa4 BA |
1408 | } |
1409 | ||
1efa30d0 SJ |
1410 | /* power up the remote processor */ |
1411 | ret = rproc->ops->start(rproc); | |
1412 | if (ret) { | |
1413 | dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); | |
c455daa4 | 1414 | goto unprepare_subdevices; |
1efa30d0 SJ |
1415 | } |
1416 | ||
618fcff3 BA |
1417 | /* Start any subdevices for the remote processor */ |
1418 | ret = rproc_start_subdevices(rproc); | |
1efa30d0 SJ |
1419 | if (ret) { |
1420 | dev_err(dev, "failed to probe subdevices for %s: %d\n", | |
1421 | rproc->name, ret); | |
c455daa4 | 1422 | goto stop_rproc; |
1efa30d0 SJ |
1423 | } |
1424 | ||
1425 | rproc->state = RPROC_RUNNING; | |
1426 | ||
1427 | dev_info(dev, "remote processor %s is now up\n", rproc->name); | |
1428 | ||
1429 | return 0; | |
c455daa4 BA |
1430 | |
1431 | stop_rproc: | |
1432 | rproc->ops->stop(rproc); | |
c455daa4 BA |
1433 | unprepare_subdevices: |
1434 | rproc_unprepare_subdevices(rproc); | |
f68d51bd SA |
1435 | reset_table_ptr: |
1436 | rproc->table_ptr = rproc->cached_table; | |
c455daa4 BA |
1437 | |
1438 | return ret; | |
1efa30d0 SJ |
1439 | } |
1440 | ||
6a6c4dc0 | 1441 | static int __rproc_attach(struct rproc *rproc) |
d848a481 MP |
1442 | { |
1443 | struct device *dev = &rproc->dev; | |
1444 | int ret; | |
1445 | ||
1446 | ret = rproc_prepare_subdevices(rproc); | |
1447 | if (ret) { | |
1448 | dev_err(dev, "failed to prepare subdevices for %s: %d\n", | |
1449 | rproc->name, ret); | |
1450 | goto out; | |
1451 | } | |
1452 | ||
1453 | /* Attach to the remote processor */ | |
1454 | ret = rproc_attach_device(rproc); | |
1455 | if (ret) { | |
1456 | dev_err(dev, "can't attach to rproc %s: %d\n", | |
1457 | rproc->name, ret); | |
1458 | goto unprepare_subdevices; | |
1459 | } | |
1460 | ||
1461 | /* Start any subdevices for the remote processor */ | |
1462 | ret = rproc_start_subdevices(rproc); | |
1463 | if (ret) { | |
1464 | dev_err(dev, "failed to probe subdevices for %s: %d\n", | |
1465 | rproc->name, ret); | |
1466 | goto stop_rproc; | |
1467 | } | |
1468 | ||
76f4c875 | 1469 | rproc->state = RPROC_ATTACHED; |
d848a481 MP |
1470 | |
1471 | dev_info(dev, "remote processor %s is now attached\n", rproc->name); | |
1472 | ||
1473 | return 0; | |
1474 | ||
1475 | stop_rproc: | |
1476 | rproc->ops->stop(rproc); | |
1477 | unprepare_subdevices: | |
1478 | rproc_unprepare_subdevices(rproc); | |
1479 | out: | |
1480 | return ret; | |
1481 | } | |
1482 | ||
400e64df OBC |
1483 | /* |
1484 | * take a firmware and boot a remote processor with it. | |
1485 | */ | |
1486 | static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) | |
1487 | { | |
b5ab5e24 | 1488 | struct device *dev = &rproc->dev; |
400e64df | 1489 | const char *name = rproc->firmware; |
58b64090 | 1490 | int ret; |
400e64df OBC |
1491 | |
1492 | ret = rproc_fw_sanity_check(rproc, fw); | |
1493 | if (ret) | |
1494 | return ret; | |
1495 | ||
e981f6d4 | 1496 | dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); |
400e64df OBC |
1497 | |
1498 | /* | |
1499 | * if enabling an IOMMU isn't relevant for this rproc, this is | |
1500 | * just a nop | |
1501 | */ | |
1502 | ret = rproc_enable_iommu(rproc); | |
1503 | if (ret) { | |
1504 | dev_err(dev, "can't enable iommu: %d\n", ret); | |
49cff125 | 1505 | return ret; |
400e64df OBC |
1506 | } |
1507 | ||
33467ac3 LP |
1508 | /* Prepare rproc for firmware loading if needed */ |
1509 | ret = rproc_prepare_device(rproc); | |
1510 | if (ret) { | |
1511 | dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); | |
1512 | goto disable_iommu; | |
1513 | } | |
1514 | ||
3e5f9eb5 | 1515 | rproc->bootaddr = rproc_get_boot_addr(rproc, fw); |
988d204c | 1516 | |
c1d35c1a BA |
1517 | /* Load resource table, core dump segment list etc from the firmware */ |
1518 | ret = rproc_parse_fw(rproc, fw); | |
58b64090 | 1519 | if (ret) |
33467ac3 | 1520 | goto unprepare_rproc; |
a0c10687 | 1521 | |
b35d7afc BA |
1522 | /* reset max_notifyid */ |
1523 | rproc->max_notifyid = -1; | |
1524 | ||
c6aed238 LP |
1525 | /* reset handled vdev */ |
1526 | rproc->nb_vdev = 0; | |
1527 | ||
400e64df | 1528 | /* handle fw resources which are required to boot rproc */ |
a4b24c75 | 1529 | ret = rproc_handle_resources(rproc, rproc_loading_handlers); |
400e64df OBC |
1530 | if (ret) { |
1531 | dev_err(dev, "Failed to process resources: %d\n", ret); | |
229b85a6 | 1532 | goto clean_up_resources; |
400e64df OBC |
1533 | } |
1534 | ||
d7c51706 LP |
1535 | /* Allocate carveout resources associated to rproc */ |
1536 | ret = rproc_alloc_registered_carveouts(rproc); | |
1537 | if (ret) { | |
1538 | dev_err(dev, "Failed to allocate associated carveouts: %d\n", | |
1539 | ret); | |
1540 | goto clean_up_resources; | |
1541 | } | |
1542 | ||
1efa30d0 SJ |
1543 | ret = rproc_start(rproc, fw); |
1544 | if (ret) | |
229b85a6 | 1545 | goto clean_up_resources; |
400e64df OBC |
1546 | |
1547 | return 0; | |
1548 | ||
229b85a6 BA |
1549 | clean_up_resources: |
1550 | rproc_resource_cleanup(rproc); | |
a0c10687 BA |
1551 | kfree(rproc->cached_table); |
1552 | rproc->cached_table = NULL; | |
988d204c | 1553 | rproc->table_ptr = NULL; |
33467ac3 LP |
1554 | unprepare_rproc: |
1555 | /* release HW resources if needed */ | |
1556 | rproc_unprepare_device(rproc); | |
58b64090 | 1557 | disable_iommu: |
400e64df OBC |
1558 | rproc_disable_iommu(rproc); |
1559 | return ret; | |
1560 | } | |
1561 | ||
1a631382 MP |
1562 | static int rproc_set_rsc_table(struct rproc *rproc) |
1563 | { | |
1564 | struct resource_table *table_ptr; | |
1565 | struct device *dev = &rproc->dev; | |
1566 | size_t table_sz; | |
1567 | int ret; | |
1568 | ||
1569 | table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz); | |
1570 | if (!table_ptr) { | |
1571 | /* Not having a resource table is acceptable */ | |
1572 | return 0; | |
1573 | } | |
1574 | ||
1575 | if (IS_ERR(table_ptr)) { | |
1576 | ret = PTR_ERR(table_ptr); | |
1577 | dev_err(dev, "can't load resource table: %d\n", ret); | |
1578 | return ret; | |
1579 | } | |
1580 | ||
9dc9507f MP |
1581 | /* |
1582 | * If it is possible to detach the remote processor, keep an untouched | |
1583 | * copy of the resource table. That way we can start fresh again when | |
1584 | * the remote processor is re-attached, that is: | |
1585 | * | |
1586 | * DETACHED -> ATTACHED -> DETACHED -> ATTACHED | |
1587 | * | |
1588 | * Free'd in rproc_reset_rsc_table_on_detach() and | |
1589 | * rproc_reset_rsc_table_on_stop(). | |
1590 | */ | |
1591 | if (rproc->ops->detach) { | |
1592 | rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL); | |
1593 | if (!rproc->clean_table) | |
1594 | return -ENOMEM; | |
1595 | } else { | |
1596 | rproc->clean_table = NULL; | |
1597 | } | |
1598 | ||
1a631382 MP |
1599 | rproc->cached_table = NULL; |
1600 | rproc->table_ptr = table_ptr; | |
1601 | rproc->table_sz = table_sz; | |
1602 | ||
1603 | return 0; | |
1604 | } | |
1605 | ||
9dc9507f MP |
1606 | static int rproc_reset_rsc_table_on_detach(struct rproc *rproc) |
1607 | { | |
1608 | struct resource_table *table_ptr; | |
1609 | ||
1610 | /* A resource table was never retrieved, nothing to do here */ | |
1611 | if (!rproc->table_ptr) | |
1612 | return 0; | |
1613 | ||
1614 | /* | |
1615 | * If we made it to this point a clean_table _must_ have been | |
1616 | * allocated in rproc_set_rsc_table(). If one isn't present | |
1617 | * something went really wrong and we must complain. | |
1618 | */ | |
1619 | if (WARN_ON(!rproc->clean_table)) | |
1620 | return -EINVAL; | |
1621 | ||
1622 | /* Remember where the external entity installed the resource table */ | |
1623 | table_ptr = rproc->table_ptr; | |
1624 | ||
1625 | /* | |
1626 | * If we made it here the remote processor was started by another | |
1627 | * entity and a cache table doesn't exist. As such make a copy of | |
1628 | * the resource table currently used by the remote processor and | |
1629 | * use that for the rest of the shutdown process. The memory | |
1630 | * allocated here is free'd in rproc_detach(). | |
1631 | */ | |
1632 | rproc->cached_table = kmemdup(rproc->table_ptr, | |
1633 | rproc->table_sz, GFP_KERNEL); | |
1634 | if (!rproc->cached_table) | |
1635 | return -ENOMEM; | |
1636 | ||
1637 | /* | |
1638 | * Use a copy of the resource table for the remainder of the | |
1639 | * shutdown process. | |
1640 | */ | |
1641 | rproc->table_ptr = rproc->cached_table; | |
1642 | ||
1643 | /* | |
1644 | * Reset the memory area where the firmware loaded the resource table | |
1645 | * to its original value. That way when we re-attach the remote | |
1646 | * processor the resource table is clean and ready to be used again. | |
1647 | */ | |
1648 | memcpy(table_ptr, rproc->clean_table, rproc->table_sz); | |
1649 | ||
1650 | /* | |
1651 | * The clean resource table is no longer needed. Allocated in | |
1652 | * rproc_set_rsc_table(). | |
1653 | */ | |
1654 | kfree(rproc->clean_table); | |
1655 | ||
1656 | return 0; | |
1657 | } | |
1658 | ||
8088dd4d MP |
1659 | static int rproc_reset_rsc_table_on_stop(struct rproc *rproc) |
1660 | { | |
1661 | /* A resource table was never retrieved, nothing to do here */ | |
1662 | if (!rproc->table_ptr) | |
1663 | return 0; | |
1664 | ||
1665 | /* | |
1666 | * If a cache table exists the remote processor was started by | |
1667 | * the remoteproc core. That cache table should be used for | |
1668 | * the rest of the shutdown process. | |
1669 | */ | |
1670 | if (rproc->cached_table) | |
1671 | goto out; | |
1672 | ||
1673 | /* | |
1674 | * If we made it here the remote processor was started by another | |
1675 | * entity and a cache table doesn't exist. As such make a copy of | |
1676 | * the resource table currently used by the remote processor and | |
1677 | * use that for the rest of the shutdown process. The memory | |
1678 | * allocated here is free'd in rproc_shutdown(). | |
1679 | */ | |
1680 | rproc->cached_table = kmemdup(rproc->table_ptr, | |
1681 | rproc->table_sz, GFP_KERNEL); | |
1682 | if (!rproc->cached_table) | |
1683 | return -ENOMEM; | |
1684 | ||
1685 | /* | |
1686 | * Since the remote processor is being switched off the clean table | |
1687 | * won't be needed. Allocated in rproc_set_rsc_table(). | |
1688 | */ | |
1689 | kfree(rproc->clean_table); | |
1690 | ||
1691 | out: | |
1692 | /* | |
1693 | * Use a copy of the resource table for the remainder of the | |
1694 | * shutdown process. | |
1695 | */ | |
1696 | rproc->table_ptr = rproc->cached_table; | |
1697 | return 0; | |
1698 | } | |
1699 | ||
fdf0e00e MP |
1700 | /* |
1701 | * Attach to remote processor - similar to rproc_fw_boot() but without | |
1702 | * the steps that deal with the firmware image. | |
1703 | */ | |
6a6c4dc0 | 1704 | static int rproc_attach(struct rproc *rproc) |
fdf0e00e MP |
1705 | { |
1706 | struct device *dev = &rproc->dev; | |
1707 | int ret; | |
1708 | ||
1709 | /* | |
1710 | * if enabling an IOMMU isn't relevant for this rproc, this is | |
1711 | * just a nop | |
1712 | */ | |
1713 | ret = rproc_enable_iommu(rproc); | |
1714 | if (ret) { | |
1715 | dev_err(dev, "can't enable iommu: %d\n", ret); | |
1716 | return ret; | |
1717 | } | |
1718 | ||
6e20a051 AP |
1719 | /* Do anything that is needed to boot the remote processor */ |
1720 | ret = rproc_prepare_device(rproc); | |
1721 | if (ret) { | |
1722 | dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); | |
1723 | goto disable_iommu; | |
1724 | } | |
1725 | ||
1a631382 MP |
1726 | ret = rproc_set_rsc_table(rproc); |
1727 | if (ret) { | |
1728 | dev_err(dev, "can't load resource table: %d\n", ret); | |
6e20a051 | 1729 | goto unprepare_device; |
1a631382 MP |
1730 | } |
1731 | ||
fdf0e00e MP |
1732 | /* reset max_notifyid */ |
1733 | rproc->max_notifyid = -1; | |
1734 | ||
1735 | /* reset handled vdev */ | |
1736 | rproc->nb_vdev = 0; | |
1737 | ||
1738 | /* | |
1739 | * Handle firmware resources required to attach to a remote processor. | |
1740 | * Because we are attaching rather than booting the remote processor, | |
1741 | * we expect the platform driver to properly set rproc->table_ptr. | |
1742 | */ | |
1743 | ret = rproc_handle_resources(rproc, rproc_loading_handlers); | |
1744 | if (ret) { | |
1745 | dev_err(dev, "Failed to process resources: %d\n", ret); | |
6e20a051 | 1746 | goto unprepare_device; |
fdf0e00e MP |
1747 | } |
1748 | ||
1749 | /* Allocate carveout resources associated to rproc */ | |
1750 | ret = rproc_alloc_registered_carveouts(rproc); | |
1751 | if (ret) { | |
1752 | dev_err(dev, "Failed to allocate associated carveouts: %d\n", | |
1753 | ret); | |
1754 | goto clean_up_resources; | |
1755 | } | |
1756 | ||
6a6c4dc0 | 1757 | ret = __rproc_attach(rproc); |
fdf0e00e MP |
1758 | if (ret) |
1759 | goto clean_up_resources; | |
1760 | ||
1761 | return 0; | |
1762 | ||
1763 | clean_up_resources: | |
1764 | rproc_resource_cleanup(rproc); | |
6e20a051 AP |
1765 | unprepare_device: |
1766 | /* release HW resources if needed */ | |
1767 | rproc_unprepare_device(rproc); | |
fdf0e00e MP |
1768 | disable_iommu: |
1769 | rproc_disable_iommu(rproc); | |
1770 | return ret; | |
1771 | } | |
1772 | ||
400e64df | 1773 | /* |
5e6533f7 | 1774 | * take a firmware and boot it up. |
400e64df OBC |
1775 | * |
1776 | * Note: this function is called asynchronously upon registration of the | |
1777 | * remote processor (so we must wait until it completes before we try | |
1778 | * to unregister the device. one other option is just to use kref here, | |
1779 | * that might be cleaner). | |
1780 | */ | |
5e6533f7 | 1781 | static void rproc_auto_boot_callback(const struct firmware *fw, void *context) |
400e64df OBC |
1782 | { |
1783 | struct rproc *rproc = context; | |
a2b950ac | 1784 | |
7a20c64d | 1785 | rproc_boot(rproc); |
ddf71187 | 1786 | |
3cc6e787 | 1787 | release_firmware(fw); |
400e64df OBC |
1788 | } |
1789 | ||
5e6533f7 | 1790 | static int rproc_trigger_auto_boot(struct rproc *rproc) |
70b85ef8 FGL |
1791 | { |
1792 | int ret; | |
1793 | ||
e3d21939 MP |
1794 | /* |
1795 | * Since the remote processor is in a detached state, it has already | |
1796 | * been booted by another entity. As such there is no point in waiting | |
1797 | * for a firmware image to be loaded, we can simply initiate the process | |
1798 | * of attaching to it immediately. | |
1799 | */ | |
1800 | if (rproc->state == RPROC_DETACHED) | |
1801 | return rproc_boot(rproc); | |
1802 | ||
70b85ef8 | 1803 | /* |
70b85ef8 FGL |
1804 | * We're initiating an asynchronous firmware loading, so we can |
1805 | * be built-in kernel code, without hanging the boot process. | |
1806 | */ | |
0733d839 | 1807 | ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT, |
70b85ef8 | 1808 | rproc->firmware, &rproc->dev, GFP_KERNEL, |
5e6533f7 | 1809 | rproc, rproc_auto_boot_callback); |
2099c77d | 1810 | if (ret < 0) |
70b85ef8 | 1811 | dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); |
70b85ef8 FGL |
1812 | |
1813 | return ret; | |
1814 | } | |
1815 | ||
880f5b38 | 1816 | static int rproc_stop(struct rproc *rproc, bool crashed) |
1efa30d0 SJ |
1817 | { |
1818 | struct device *dev = &rproc->dev; | |
1819 | int ret; | |
1820 | ||
d2008a96 MP |
1821 | /* No need to continue if a stop() operation has not been provided */ |
1822 | if (!rproc->ops->stop) | |
1823 | return -EINVAL; | |
1824 | ||
618fcff3 BA |
1825 | /* Stop any subdevices for the remote processor */ |
1826 | rproc_stop_subdevices(rproc, crashed); | |
1efa30d0 | 1827 | |
0a8b81cb | 1828 | /* the installed resource table is no longer accessible */ |
8088dd4d MP |
1829 | ret = rproc_reset_rsc_table_on_stop(rproc); |
1830 | if (ret) { | |
1831 | dev_err(dev, "can't reset resource table: %d\n", ret); | |
1832 | return ret; | |
1833 | } | |
1834 | ||
0a8b81cb | 1835 | |
1efa30d0 SJ |
1836 | /* power off the remote processor */ |
1837 | ret = rproc->ops->stop(rproc); | |
1838 | if (ret) { | |
1839 | dev_err(dev, "can't stop rproc: %d\n", ret); | |
1840 | return ret; | |
1841 | } | |
1842 | ||
c455daa4 BA |
1843 | rproc_unprepare_subdevices(rproc); |
1844 | ||
1efa30d0 SJ |
1845 | rproc->state = RPROC_OFFLINE; |
1846 | ||
1847 | dev_info(dev, "stopped remote processor %s\n", rproc->name); | |
1848 | ||
1849 | return 0; | |
1850 | } | |
1851 | ||
6070203f MP |
1852 | /* |
1853 | * __rproc_detach(): Does the opposite of __rproc_attach() | |
1854 | */ | |
d3962a39 | 1855 | static int __rproc_detach(struct rproc *rproc) |
6070203f MP |
1856 | { |
1857 | struct device *dev = &rproc->dev; | |
1858 | int ret; | |
1859 | ||
1860 | /* No need to continue if a detach() operation has not been provided */ | |
1861 | if (!rproc->ops->detach) | |
1862 | return -EINVAL; | |
1863 | ||
1864 | /* Stop any subdevices for the remote processor */ | |
1865 | rproc_stop_subdevices(rproc, false); | |
1866 | ||
9dc9507f MP |
1867 | /* the installed resource table is no longer accessible */ |
1868 | ret = rproc_reset_rsc_table_on_detach(rproc); | |
1869 | if (ret) { | |
1870 | dev_err(dev, "can't reset resource table: %d\n", ret); | |
1871 | return ret; | |
1872 | } | |
1873 | ||
6070203f MP |
1874 | /* Tell the remote processor the core isn't available anymore */ |
1875 | ret = rproc->ops->detach(rproc); | |
1876 | if (ret) { | |
1877 | dev_err(dev, "can't detach from rproc: %d\n", ret); | |
1878 | return ret; | |
1879 | } | |
1880 | ||
1881 | rproc_unprepare_subdevices(rproc); | |
1882 | ||
1883 | rproc->state = RPROC_DETACHED; | |
1884 | ||
1885 | dev_info(dev, "detached remote processor %s\n", rproc->name); | |
1886 | ||
1887 | return 0; | |
1888 | } | |
2666ca91 | 1889 | |
70b85ef8 FGL |
1890 | /** |
1891 | * rproc_trigger_recovery() - recover a remoteproc | |
1892 | * @rproc: the remote processor | |
1893 | * | |
56324d7a | 1894 | * The recovery is done by resetting all the virtio devices, that way all the |
70b85ef8 FGL |
1895 | * rpmsg drivers will be reseted along with the remote processor making the |
1896 | * remoteproc functional again. | |
1897 | * | |
1898 | * This function can sleep, so it cannot be called from atomic context. | |
f2867434 SA |
1899 | * |
1900 | * Return: 0 on success or a negative value upon failure | |
70b85ef8 FGL |
1901 | */ |
1902 | int rproc_trigger_recovery(struct rproc *rproc) | |
1903 | { | |
7e83cab8 SJ |
1904 | const struct firmware *firmware_p; |
1905 | struct device *dev = &rproc->dev; | |
1906 | int ret; | |
1907 | ||
7e83cab8 SJ |
1908 | ret = mutex_lock_interruptible(&rproc->lock); |
1909 | if (ret) | |
1910 | return ret; | |
1911 | ||
0b145574 AE |
1912 | /* State could have changed before we got the mutex */ |
1913 | if (rproc->state != RPROC_CRASHED) | |
1914 | goto unlock_mutex; | |
1915 | ||
1916 | dev_err(dev, "recovering %s\n", rproc->name); | |
1917 | ||
fcd58037 | 1918 | ret = rproc_stop(rproc, true); |
7e83cab8 SJ |
1919 | if (ret) |
1920 | goto unlock_mutex; | |
ddf71187 | 1921 | |
2666ca91 | 1922 | /* generate coredump */ |
adf60a87 | 1923 | rproc->ops->coredump(rproc); |
2666ca91 | 1924 | |
7e83cab8 SJ |
1925 | /* load firmware */ |
1926 | ret = request_firmware(&firmware_p, rproc->firmware, dev); | |
1927 | if (ret < 0) { | |
1928 | dev_err(dev, "request_firmware failed: %d\n", ret); | |
1929 | goto unlock_mutex; | |
1930 | } | |
ddf71187 | 1931 | |
7e83cab8 SJ |
1932 | /* boot the remote processor up again */ |
1933 | ret = rproc_start(rproc, firmware_p); | |
1934 | ||
1935 | release_firmware(firmware_p); | |
1936 | ||
1937 | unlock_mutex: | |
1938 | mutex_unlock(&rproc->lock); | |
1939 | return ret; | |
70b85ef8 FGL |
1940 | } |
1941 | ||
8afd519c FGL |
1942 | /** |
1943 | * rproc_crash_handler_work() - handle a crash | |
2e7d4c2c | 1944 | * @work: work treating the crash |
8afd519c FGL |
1945 | * |
1946 | * This function needs to handle everything related to a crash, like cpu | |
1947 | * registers and stack dump, information to help to debug the fatal error, etc. | |
1948 | */ | |
1949 | static void rproc_crash_handler_work(struct work_struct *work) | |
1950 | { | |
1951 | struct rproc *rproc = container_of(work, struct rproc, crash_handler); | |
1952 | struct device *dev = &rproc->dev; | |
1953 | ||
1954 | dev_dbg(dev, "enter %s\n", __func__); | |
1955 | ||
1956 | mutex_lock(&rproc->lock); | |
1957 | ||
1958 | if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { | |
1959 | /* handle only the first crash detected */ | |
1960 | mutex_unlock(&rproc->lock); | |
1961 | return; | |
1962 | } | |
1963 | ||
1964 | rproc->state = RPROC_CRASHED; | |
1965 | dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, | |
1966 | rproc->name); | |
1967 | ||
1968 | mutex_unlock(&rproc->lock); | |
1969 | ||
2e37abb8 FGL |
1970 | if (!rproc->recovery_disabled) |
1971 | rproc_trigger_recovery(rproc); | |
a781e5aa RB |
1972 | |
1973 | pm_relax(rproc->dev.parent); | |
8afd519c FGL |
1974 | } |
1975 | ||
400e64df | 1976 | /** |
1b0ef906 | 1977 | * rproc_boot() - boot a remote processor |
400e64df OBC |
1978 | * @rproc: handle of a remote processor |
1979 | * | |
1980 | * Boot a remote processor (i.e. load its firmware, power it on, ...). | |
1981 | * | |
1982 | * If the remote processor is already powered on, this function immediately | |
1983 | * returns (successfully). | |
1984 | * | |
f2867434 | 1985 | * Return: 0 on success, and an appropriate error value otherwise |
400e64df | 1986 | */ |
1b0ef906 | 1987 | int rproc_boot(struct rproc *rproc) |
400e64df OBC |
1988 | { |
1989 | const struct firmware *firmware_p; | |
1990 | struct device *dev; | |
1991 | int ret; | |
1992 | ||
1993 | if (!rproc) { | |
1994 | pr_err("invalid rproc handle\n"); | |
1995 | return -EINVAL; | |
1996 | } | |
1997 | ||
b5ab5e24 | 1998 | dev = &rproc->dev; |
400e64df OBC |
1999 | |
2000 | ret = mutex_lock_interruptible(&rproc->lock); | |
2001 | if (ret) { | |
2002 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
2003 | return ret; | |
2004 | } | |
2005 | ||
2099c77d SJ |
2006 | if (rproc->state == RPROC_DELETED) { |
2007 | ret = -ENODEV; | |
2008 | dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); | |
2009 | goto unlock_mutex; | |
2010 | } | |
2011 | ||
0f9dc562 | 2012 | /* skip the boot or attach process if rproc is already powered up */ |
400e64df OBC |
2013 | if (atomic_inc_return(&rproc->power) > 1) { |
2014 | ret = 0; | |
2015 | goto unlock_mutex; | |
2016 | } | |
2017 | ||
0f9dc562 MP |
2018 | if (rproc->state == RPROC_DETACHED) { |
2019 | dev_info(dev, "attaching to %s\n", rproc->name); | |
400e64df | 2020 | |
6a6c4dc0 | 2021 | ret = rproc_attach(rproc); |
0f9dc562 MP |
2022 | } else { |
2023 | dev_info(dev, "powering up %s\n", rproc->name); | |
400e64df | 2024 | |
0f9dc562 MP |
2025 | /* load firmware */ |
2026 | ret = request_firmware(&firmware_p, rproc->firmware, dev); | |
2027 | if (ret < 0) { | |
2028 | dev_err(dev, "request_firmware failed: %d\n", ret); | |
2029 | goto downref_rproc; | |
2030 | } | |
400e64df | 2031 | |
0f9dc562 MP |
2032 | ret = rproc_fw_boot(rproc, firmware_p); |
2033 | ||
2034 | release_firmware(firmware_p); | |
2035 | } | |
400e64df OBC |
2036 | |
2037 | downref_rproc: | |
fbb6aacb | 2038 | if (ret) |
400e64df | 2039 | atomic_dec(&rproc->power); |
400e64df OBC |
2040 | unlock_mutex: |
2041 | mutex_unlock(&rproc->lock); | |
2042 | return ret; | |
2043 | } | |
2044 | EXPORT_SYMBOL(rproc_boot); | |
2045 | ||
2046 | /** | |
2047 | * rproc_shutdown() - power off the remote processor | |
2048 | * @rproc: the remote processor | |
2049 | * | |
2050 | * Power off a remote processor (previously booted with rproc_boot()). | |
2051 | * | |
2052 | * In case @rproc is still being used by an additional user(s), then | |
2053 | * this function will just decrement the power refcount and exit, | |
2054 | * without really powering off the device. | |
2055 | * | |
2056 | * Every call to rproc_boot() must (eventually) be accompanied by a call | |
2057 | * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. | |
2058 | * | |
2059 | * Notes: | |
2060 | * - we're not decrementing the rproc's refcount, only the power refcount. | |
2061 | * which means that the @rproc handle stays valid even after rproc_shutdown() | |
2062 | * returns, and users can still use it with a subsequent rproc_boot(), if | |
2063 | * needed. | |
400e64df OBC |
2064 | */ |
2065 | void rproc_shutdown(struct rproc *rproc) | |
2066 | { | |
b5ab5e24 | 2067 | struct device *dev = &rproc->dev; |
400e64df OBC |
2068 | int ret; |
2069 | ||
2070 | ret = mutex_lock_interruptible(&rproc->lock); | |
2071 | if (ret) { | |
2072 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
2073 | return; | |
2074 | } | |
2075 | ||
2076 | /* if the remote proc is still needed, bail out */ | |
2077 | if (!atomic_dec_and_test(&rproc->power)) | |
2078 | goto out; | |
2079 | ||
fcd58037 | 2080 | ret = rproc_stop(rproc, false); |
400e64df OBC |
2081 | if (ret) { |
2082 | atomic_inc(&rproc->power); | |
400e64df OBC |
2083 | goto out; |
2084 | } | |
2085 | ||
2086 | /* clean up all acquired resources */ | |
2087 | rproc_resource_cleanup(rproc); | |
2088 | ||
33467ac3 LP |
2089 | /* release HW resources if needed */ |
2090 | rproc_unprepare_device(rproc); | |
2091 | ||
400e64df OBC |
2092 | rproc_disable_iommu(rproc); |
2093 | ||
988d204c | 2094 | /* Free the copy of the resource table */ |
a0c10687 BA |
2095 | kfree(rproc->cached_table); |
2096 | rproc->cached_table = NULL; | |
988d204c | 2097 | rproc->table_ptr = NULL; |
400e64df OBC |
2098 | out: |
2099 | mutex_unlock(&rproc->lock); | |
400e64df OBC |
2100 | } |
2101 | EXPORT_SYMBOL(rproc_shutdown); | |
2102 | ||
d3962a39 MP |
2103 | /** |
2104 | * rproc_detach() - Detach the remote processor from the | |
2105 | * remoteproc core | |
2106 | * | |
2107 | * @rproc: the remote processor | |
2108 | * | |
2109 | * Detach a remote processor (previously attached to with rproc_attach()). | |
2110 | * | |
2111 | * In case @rproc is still being used by an additional user(s), then | |
2112 | * this function will just decrement the power refcount and exit, | |
2113 | * without disconnecting the device. | |
2114 | * | |
2115 | * Function rproc_detach() calls __rproc_detach() in order to let a remote | |
2116 | * processor know that services provided by the application processor are | |
2117 | * no longer available. From there it should be possible to remove the | |
2118 | * platform driver and even power cycle the application processor (if the HW | |
2119 | * supports it) without needing to switch off the remote processor. | |
f2867434 SA |
2120 | * |
2121 | * Return: 0 on success, and an appropriate error value otherwise | |
d3962a39 MP |
2122 | */ |
2123 | int rproc_detach(struct rproc *rproc) | |
2124 | { | |
2125 | struct device *dev = &rproc->dev; | |
2126 | int ret; | |
2127 | ||
2128 | ret = mutex_lock_interruptible(&rproc->lock); | |
2129 | if (ret) { | |
2130 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
2131 | return ret; | |
2132 | } | |
2133 | ||
2134 | /* if the remote proc is still needed, bail out */ | |
2135 | if (!atomic_dec_and_test(&rproc->power)) { | |
2136 | ret = 0; | |
2137 | goto out; | |
2138 | } | |
2139 | ||
2140 | ret = __rproc_detach(rproc); | |
2141 | if (ret) { | |
2142 | atomic_inc(&rproc->power); | |
2143 | goto out; | |
2144 | } | |
2145 | ||
2146 | /* clean up all acquired resources */ | |
2147 | rproc_resource_cleanup(rproc); | |
2148 | ||
2149 | /* release HW resources if needed */ | |
2150 | rproc_unprepare_device(rproc); | |
2151 | ||
2152 | rproc_disable_iommu(rproc); | |
2153 | ||
9dc9507f MP |
2154 | /* Free the copy of the resource table */ |
2155 | kfree(rproc->cached_table); | |
2156 | rproc->cached_table = NULL; | |
d3962a39 MP |
2157 | rproc->table_ptr = NULL; |
2158 | out: | |
2159 | mutex_unlock(&rproc->lock); | |
2160 | return ret; | |
2161 | } | |
2162 | EXPORT_SYMBOL(rproc_detach); | |
2163 | ||
fec47d86 DG |
2164 | /** |
2165 | * rproc_get_by_phandle() - find a remote processor by phandle | |
2166 | * @phandle: phandle to the rproc | |
2167 | * | |
2168 | * Finds an rproc handle using the remote processor's phandle, and then | |
2169 | * return a handle to the rproc. | |
2170 | * | |
2171 | * This function increments the remote processor's refcount, so always | |
2172 | * use rproc_put() to decrement it back once rproc isn't needed anymore. | |
2173 | * | |
f2867434 | 2174 | * Return: rproc handle on success, and NULL on failure |
fec47d86 | 2175 | */ |
8de3dbd0 | 2176 | #ifdef CONFIG_OF |
fec47d86 DG |
2177 | struct rproc *rproc_get_by_phandle(phandle phandle) |
2178 | { | |
2179 | struct rproc *rproc = NULL, *r; | |
2180 | struct device_node *np; | |
2181 | ||
2182 | np = of_find_node_by_phandle(phandle); | |
2183 | if (!np) | |
2184 | return NULL; | |
2185 | ||
c0abe2ca BA |
2186 | rcu_read_lock(); |
2187 | list_for_each_entry_rcu(r, &rproc_list, node) { | |
fec47d86 | 2188 | if (r->dev.parent && r->dev.parent->of_node == np) { |
fbb6aacb BA |
2189 | /* prevent underlying implementation from being removed */ |
2190 | if (!try_module_get(r->dev.parent->driver->owner)) { | |
2191 | dev_err(&r->dev, "can't get owner\n"); | |
2192 | break; | |
2193 | } | |
2194 | ||
fec47d86 DG |
2195 | rproc = r; |
2196 | get_device(&rproc->dev); | |
2197 | break; | |
2198 | } | |
2199 | } | |
c0abe2ca | 2200 | rcu_read_unlock(); |
fec47d86 DG |
2201 | |
2202 | of_node_put(np); | |
2203 | ||
2204 | return rproc; | |
2205 | } | |
8de3dbd0 OBC |
2206 | #else |
2207 | struct rproc *rproc_get_by_phandle(phandle phandle) | |
2208 | { | |
2209 | return NULL; | |
2210 | } | |
2211 | #endif | |
fec47d86 DG |
2212 | EXPORT_SYMBOL(rproc_get_by_phandle); |
2213 | ||
4c1ad562 SA |
2214 | /** |
2215 | * rproc_set_firmware() - assign a new firmware | |
2216 | * @rproc: rproc handle to which the new firmware is being assigned | |
2217 | * @fw_name: new firmware name to be assigned | |
2218 | * | |
2219 | * This function allows remoteproc drivers or clients to configure a custom | |
2220 | * firmware name that is different from the default name used during remoteproc | |
2221 | * registration. The function does not trigger a remote processor boot, | |
2222 | * only sets the firmware name used for a subsequent boot. This function | |
2223 | * should also be called only when the remote processor is offline. | |
2224 | * | |
2225 | * This allows either the userspace to configure a different name through | |
2226 | * sysfs or a kernel-level remoteproc or a remoteproc client driver to set | |
2227 | * a specific firmware when it is controlling the boot and shutdown of the | |
2228 | * remote processor. | |
2229 | * | |
2230 | * Return: 0 on success or a negative value upon failure | |
2231 | */ | |
2232 | int rproc_set_firmware(struct rproc *rproc, const char *fw_name) | |
2233 | { | |
2234 | struct device *dev; | |
2235 | int ret, len; | |
2236 | char *p; | |
2237 | ||
2238 | if (!rproc || !fw_name) | |
2239 | return -EINVAL; | |
2240 | ||
2241 | dev = rproc->dev.parent; | |
2242 | ||
2243 | ret = mutex_lock_interruptible(&rproc->lock); | |
2244 | if (ret) { | |
2245 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
2246 | return -EINVAL; | |
2247 | } | |
2248 | ||
2249 | if (rproc->state != RPROC_OFFLINE) { | |
2250 | dev_err(dev, "can't change firmware while running\n"); | |
2251 | ret = -EBUSY; | |
2252 | goto out; | |
2253 | } | |
2254 | ||
2255 | len = strcspn(fw_name, "\n"); | |
2256 | if (!len) { | |
2257 | dev_err(dev, "can't provide empty string for firmware name\n"); | |
2258 | ret = -EINVAL; | |
2259 | goto out; | |
2260 | } | |
2261 | ||
2262 | p = kstrndup(fw_name, len, GFP_KERNEL); | |
2263 | if (!p) { | |
2264 | ret = -ENOMEM; | |
2265 | goto out; | |
2266 | } | |
2267 | ||
43d3f2c7 | 2268 | kfree_const(rproc->firmware); |
4c1ad562 SA |
2269 | rproc->firmware = p; |
2270 | ||
2271 | out: | |
2272 | mutex_unlock(&rproc->lock); | |
2273 | return ret; | |
2274 | } | |
2275 | EXPORT_SYMBOL(rproc_set_firmware); | |
2276 | ||
88d3a136 MP |
2277 | static int rproc_validate(struct rproc *rproc) |
2278 | { | |
2279 | switch (rproc->state) { | |
2280 | case RPROC_OFFLINE: | |
2281 | /* | |
2282 | * An offline processor without a start() | |
2283 | * function makes no sense. | |
2284 | */ | |
2285 | if (!rproc->ops->start) | |
2286 | return -EINVAL; | |
2287 | break; | |
2288 | case RPROC_DETACHED: | |
2289 | /* | |
2290 | * A remote processor in a detached state without an | |
2291 | * attach() function makes not sense. | |
2292 | */ | |
2293 | if (!rproc->ops->attach) | |
2294 | return -EINVAL; | |
2295 | /* | |
2296 | * When attaching to a remote processor the device memory | |
2297 | * is already available and as such there is no need to have a | |
2298 | * cached table. | |
2299 | */ | |
2300 | if (rproc->cached_table) | |
2301 | return -EINVAL; | |
2302 | break; | |
2303 | default: | |
2304 | /* | |
2305 | * When adding a remote processor, the state of the device | |
2306 | * can be offline or detached, nothing else. | |
2307 | */ | |
2308 | return -EINVAL; | |
2309 | } | |
2310 | ||
2311 | return 0; | |
2312 | } | |
2313 | ||
400e64df | 2314 | /** |
160e7c84 | 2315 | * rproc_add() - register a remote processor |
400e64df OBC |
2316 | * @rproc: the remote processor handle to register |
2317 | * | |
2318 | * Registers @rproc with the remoteproc framework, after it has been | |
2319 | * allocated with rproc_alloc(). | |
2320 | * | |
2321 | * This is called by the platform-specific rproc implementation, whenever | |
2322 | * a new remote processor device is probed. | |
2323 | * | |
400e64df OBC |
2324 | * Note: this function initiates an asynchronous firmware loading |
2325 | * context, which will look for virtio devices supported by the rproc's | |
2326 | * firmware. | |
2327 | * | |
2328 | * If found, those virtio devices will be created and added, so as a result | |
7a186941 | 2329 | * of registering this remote processor, additional virtio drivers might be |
400e64df | 2330 | * probed. |
f2867434 SA |
2331 | * |
2332 | * Return: 0 on success and an appropriate error code otherwise | |
400e64df | 2333 | */ |
160e7c84 | 2334 | int rproc_add(struct rproc *rproc) |
400e64df | 2335 | { |
b5ab5e24 | 2336 | struct device *dev = &rproc->dev; |
70b85ef8 | 2337 | int ret; |
400e64df | 2338 | |
519346ec | 2339 | ret = rproc_validate(rproc); |
b5ab5e24 OBC |
2340 | if (ret < 0) |
2341 | return ret; | |
400e64df | 2342 | |
519346ec SG |
2343 | /* add char device for this remoteproc */ |
2344 | ret = rproc_char_device_add(rproc); | |
88d3a136 MP |
2345 | if (ret < 0) |
2346 | return ret; | |
2347 | ||
519346ec | 2348 | ret = device_add(dev); |
7dbdb8bd SG |
2349 | if (ret < 0) { |
2350 | put_device(dev); | |
2351 | goto rproc_remove_cdev; | |
2352 | } | |
88d3a136 | 2353 | |
b5ab5e24 | 2354 | dev_info(dev, "%s is available\n", rproc->name); |
400e64df OBC |
2355 | |
2356 | /* create debugfs entries */ | |
2357 | rproc_create_debug_dir(rproc); | |
7a20c64d SJ |
2358 | |
2359 | /* if rproc is marked always-on, request it to boot */ | |
2360 | if (rproc->auto_boot) { | |
5e6533f7 | 2361 | ret = rproc_trigger_auto_boot(rproc); |
7a20c64d | 2362 | if (ret < 0) |
7dbdb8bd | 2363 | goto rproc_remove_dev; |
7a20c64d | 2364 | } |
400e64df | 2365 | |
d2e12e66 DG |
2366 | /* expose to rproc_get_by_phandle users */ |
2367 | mutex_lock(&rproc_list_mutex); | |
c0abe2ca | 2368 | list_add_rcu(&rproc->node, &rproc_list); |
d2e12e66 DG |
2369 | mutex_unlock(&rproc_list_mutex); |
2370 | ||
2371 | return 0; | |
7dbdb8bd SG |
2372 | |
2373 | rproc_remove_dev: | |
2374 | rproc_delete_debug_dir(rproc); | |
2375 | device_del(dev); | |
2376 | rproc_remove_cdev: | |
2377 | rproc_char_device_remove(rproc); | |
2378 | return ret; | |
400e64df | 2379 | } |
160e7c84 | 2380 | EXPORT_SYMBOL(rproc_add); |
400e64df | 2381 | |
305ac5a7 PC |
2382 | static void devm_rproc_remove(void *rproc) |
2383 | { | |
2384 | rproc_del(rproc); | |
2385 | } | |
2386 | ||
2387 | /** | |
2388 | * devm_rproc_add() - resource managed rproc_add() | |
2389 | * @dev: the underlying device | |
2390 | * @rproc: the remote processor handle to register | |
2391 | * | |
2392 | * This function performs like rproc_add() but the registered rproc device will | |
2393 | * automatically be removed on driver detach. | |
2394 | * | |
f2867434 | 2395 | * Return: 0 on success, negative errno on failure |
305ac5a7 PC |
2396 | */ |
2397 | int devm_rproc_add(struct device *dev, struct rproc *rproc) | |
2398 | { | |
2399 | int err; | |
2400 | ||
2401 | err = rproc_add(rproc); | |
2402 | if (err) | |
2403 | return err; | |
2404 | ||
2405 | return devm_add_action_or_reset(dev, devm_rproc_remove, rproc); | |
2406 | } | |
2407 | EXPORT_SYMBOL(devm_rproc_add); | |
2408 | ||
b5ab5e24 OBC |
2409 | /** |
2410 | * rproc_type_release() - release a remote processor instance | |
2411 | * @dev: the rproc's device | |
2412 | * | |
2413 | * This function should _never_ be called directly. | |
2414 | * | |
2415 | * It will be called by the driver core when no one holds a valid pointer | |
2416 | * to @dev anymore. | |
2417 | */ | |
2418 | static void rproc_type_release(struct device *dev) | |
2419 | { | |
2420 | struct rproc *rproc = container_of(dev, struct rproc, dev); | |
2421 | ||
7183a2a7 OBC |
2422 | dev_info(&rproc->dev, "releasing %s\n", rproc->name); |
2423 | ||
b5ab5e24 OBC |
2424 | idr_destroy(&rproc->notifyids); |
2425 | ||
2426 | if (rproc->index >= 0) | |
2427 | ida_simple_remove(&rproc_dev_index, rproc->index); | |
2428 | ||
1487deda | 2429 | kfree_const(rproc->firmware); |
db655278 | 2430 | kfree_const(rproc->name); |
fb98e2bd | 2431 | kfree(rproc->ops); |
b5ab5e24 OBC |
2432 | kfree(rproc); |
2433 | } | |
2434 | ||
c42ca04d | 2435 | static const struct device_type rproc_type = { |
b5ab5e24 OBC |
2436 | .name = "remoteproc", |
2437 | .release = rproc_type_release, | |
2438 | }; | |
400e64df | 2439 | |
0c2ae2b1 MP |
2440 | static int rproc_alloc_firmware(struct rproc *rproc, |
2441 | const char *name, const char *firmware) | |
2442 | { | |
1487deda | 2443 | const char *p; |
0c2ae2b1 | 2444 | |
9d5f82c8 MP |
2445 | /* |
2446 | * Allocate a firmware name if the caller gave us one to work | |
2447 | * with. Otherwise construct a new one using a default pattern. | |
2448 | */ | |
2449 | if (firmware) | |
1487deda | 2450 | p = kstrdup_const(firmware, GFP_KERNEL); |
9d5f82c8 MP |
2451 | else |
2452 | p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name); | |
4df4f8be MP |
2453 | |
2454 | if (!p) | |
2455 | return -ENOMEM; | |
0c2ae2b1 MP |
2456 | |
2457 | rproc->firmware = p; | |
2458 | ||
2459 | return 0; | |
2460 | } | |
2461 | ||
bf860aa1 MP |
2462 | static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops) |
2463 | { | |
2464 | rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); | |
2465 | if (!rproc->ops) | |
2466 | return -ENOMEM; | |
2467 | ||
adf60a87 SG |
2468 | /* Default to rproc_coredump if no coredump function is specified */ |
2469 | if (!rproc->ops->coredump) | |
2470 | rproc->ops->coredump = rproc_coredump; | |
2471 | ||
bf860aa1 MP |
2472 | if (rproc->ops->load) |
2473 | return 0; | |
2474 | ||
2475 | /* Default to ELF loader if no load function is specified */ | |
2476 | rproc->ops->load = rproc_elf_load_segments; | |
2477 | rproc->ops->parse_fw = rproc_elf_load_rsc_table; | |
2478 | rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; | |
e29ff72b | 2479 | rproc->ops->sanity_check = rproc_elf_sanity_check; |
bf860aa1 MP |
2480 | rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; |
2481 | ||
2482 | return 0; | |
2483 | } | |
2484 | ||
400e64df OBC |
2485 | /** |
2486 | * rproc_alloc() - allocate a remote processor handle | |
2487 | * @dev: the underlying device | |
2488 | * @name: name of this remote processor | |
2489 | * @ops: platform-specific handlers (mainly start/stop) | |
8b4aec9a | 2490 | * @firmware: name of firmware file to load, can be NULL |
400e64df OBC |
2491 | * @len: length of private data needed by the rproc driver (in bytes) |
2492 | * | |
2493 | * Allocates a new remote processor handle, but does not register | |
8b4aec9a | 2494 | * it yet. if @firmware is NULL, a default name is used. |
400e64df OBC |
2495 | * |
2496 | * This function should be used by rproc implementations during initialization | |
2497 | * of the remote processor. | |
2498 | * | |
2499 | * After creating an rproc handle using this function, and when ready, | |
160e7c84 | 2500 | * implementations should then call rproc_add() to complete |
400e64df OBC |
2501 | * the registration of the remote processor. |
2502 | * | |
400e64df | 2503 | * Note: _never_ directly deallocate @rproc, even if it was not registered |
433c0e04 | 2504 | * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). |
f2867434 SA |
2505 | * |
2506 | * Return: new rproc pointer on success, and NULL on failure | |
400e64df OBC |
2507 | */ |
2508 | struct rproc *rproc_alloc(struct device *dev, const char *name, | |
730f84ce AS |
2509 | const struct rproc_ops *ops, |
2510 | const char *firmware, int len) | |
400e64df OBC |
2511 | { |
2512 | struct rproc *rproc; | |
2513 | ||
2514 | if (!dev || !name || !ops) | |
2515 | return NULL; | |
2516 | ||
0f57dc6a | 2517 | rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); |
0c2ae2b1 | 2518 | if (!rproc) |
0f57dc6a | 2519 | return NULL; |
0c2ae2b1 | 2520 | |
400e64df | 2521 | rproc->priv = &rproc[1]; |
ddf71187 | 2522 | rproc->auto_boot = true; |
418fd787 CL |
2523 | rproc->elf_class = ELFCLASSNONE; |
2524 | rproc->elf_machine = EM_NONE; | |
400e64df | 2525 | |
b5ab5e24 OBC |
2526 | device_initialize(&rproc->dev); |
2527 | rproc->dev.parent = dev; | |
2528 | rproc->dev.type = &rproc_type; | |
2aefbef0 | 2529 | rproc->dev.class = &rproc_class; |
7c89717f | 2530 | rproc->dev.driver_data = rproc; |
6442df49 | 2531 | idr_init(&rproc->notifyids); |
b5ab5e24 | 2532 | |
db655278 SA |
2533 | rproc->name = kstrdup_const(name, GFP_KERNEL); |
2534 | if (!rproc->name) | |
2535 | goto put_device; | |
2536 | ||
226f5db4 MP |
2537 | if (rproc_alloc_firmware(rproc, name, firmware)) |
2538 | goto put_device; | |
2539 | ||
2540 | if (rproc_alloc_ops(rproc, ops)) | |
2541 | goto put_device; | |
2542 | ||
b5ab5e24 OBC |
2543 | /* Assign a unique device index and name */ |
2544 | rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); | |
2545 | if (rproc->index < 0) { | |
2546 | dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); | |
226f5db4 | 2547 | goto put_device; |
b5ab5e24 OBC |
2548 | } |
2549 | ||
2550 | dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); | |
2551 | ||
400e64df OBC |
2552 | atomic_set(&rproc->power, 0); |
2553 | ||
400e64df OBC |
2554 | mutex_init(&rproc->lock); |
2555 | ||
2556 | INIT_LIST_HEAD(&rproc->carveouts); | |
2557 | INIT_LIST_HEAD(&rproc->mappings); | |
2558 | INIT_LIST_HEAD(&rproc->traces); | |
7a186941 | 2559 | INIT_LIST_HEAD(&rproc->rvdevs); |
7bdc9650 | 2560 | INIT_LIST_HEAD(&rproc->subdevs); |
2666ca91 | 2561 | INIT_LIST_HEAD(&rproc->dump_segments); |
400e64df | 2562 | |
8afd519c FGL |
2563 | INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); |
2564 | ||
400e64df OBC |
2565 | rproc->state = RPROC_OFFLINE; |
2566 | ||
2567 | return rproc; | |
0c2ae2b1 | 2568 | |
226f5db4 MP |
2569 | put_device: |
2570 | put_device(&rproc->dev); | |
0c2ae2b1 | 2571 | return NULL; |
400e64df OBC |
2572 | } |
2573 | EXPORT_SYMBOL(rproc_alloc); | |
2574 | ||
2575 | /** | |
433c0e04 BA |
2576 | * rproc_free() - unroll rproc_alloc() |
2577 | * @rproc: the remote processor handle | |
2578 | * | |
2579 | * This function decrements the rproc dev refcount. | |
2580 | * | |
2581 | * If no one holds any reference to rproc anymore, then its refcount would | |
2582 | * now drop to zero, and it would be freed. | |
2583 | */ | |
2584 | void rproc_free(struct rproc *rproc) | |
2585 | { | |
2586 | put_device(&rproc->dev); | |
2587 | } | |
2588 | EXPORT_SYMBOL(rproc_free); | |
2589 | ||
2590 | /** | |
2591 | * rproc_put() - release rproc reference | |
400e64df OBC |
2592 | * @rproc: the remote processor handle |
2593 | * | |
c6b5a276 | 2594 | * This function decrements the rproc dev refcount. |
400e64df | 2595 | * |
c6b5a276 OBC |
2596 | * If no one holds any reference to rproc anymore, then its refcount would |
2597 | * now drop to zero, and it would be freed. | |
400e64df | 2598 | */ |
160e7c84 | 2599 | void rproc_put(struct rproc *rproc) |
400e64df | 2600 | { |
fbb6aacb | 2601 | module_put(rproc->dev.parent->driver->owner); |
b5ab5e24 | 2602 | put_device(&rproc->dev); |
400e64df | 2603 | } |
160e7c84 | 2604 | EXPORT_SYMBOL(rproc_put); |
400e64df OBC |
2605 | |
2606 | /** | |
160e7c84 | 2607 | * rproc_del() - unregister a remote processor |
400e64df OBC |
2608 | * @rproc: rproc handle to unregister |
2609 | * | |
400e64df OBC |
2610 | * This function should be called when the platform specific rproc |
2611 | * implementation decides to remove the rproc device. it should | |
160e7c84 | 2612 | * _only_ be called if a previous invocation of rproc_add() |
400e64df OBC |
2613 | * has completed successfully. |
2614 | * | |
160e7c84 | 2615 | * After rproc_del() returns, @rproc isn't freed yet, because |
c6b5a276 | 2616 | * of the outstanding reference created by rproc_alloc. To decrement that |
433c0e04 | 2617 | * one last refcount, one still needs to call rproc_free(). |
400e64df | 2618 | * |
f2867434 | 2619 | * Return: 0 on success and -EINVAL if @rproc isn't valid |
400e64df | 2620 | */ |
160e7c84 | 2621 | int rproc_del(struct rproc *rproc) |
400e64df OBC |
2622 | { |
2623 | if (!rproc) | |
2624 | return -EINVAL; | |
2625 | ||
ddf71187 | 2626 | /* TODO: make sure this works with rproc->power > 1 */ |
16324fc8 | 2627 | rproc_shutdown(rproc); |
ddf71187 | 2628 | |
2099c77d SJ |
2629 | mutex_lock(&rproc->lock); |
2630 | rproc->state = RPROC_DELETED; | |
2631 | mutex_unlock(&rproc->lock); | |
2632 | ||
b003d45b SJ |
2633 | rproc_delete_debug_dir(rproc); |
2634 | ||
fec47d86 DG |
2635 | /* the rproc is downref'ed as soon as it's removed from the klist */ |
2636 | mutex_lock(&rproc_list_mutex); | |
c0abe2ca | 2637 | list_del_rcu(&rproc->node); |
fec47d86 DG |
2638 | mutex_unlock(&rproc_list_mutex); |
2639 | ||
c0abe2ca BA |
2640 | /* Ensure that no readers of rproc_list are still active */ |
2641 | synchronize_rcu(); | |
2642 | ||
b5ab5e24 | 2643 | device_del(&rproc->dev); |
930eec0b | 2644 | rproc_char_device_remove(rproc); |
400e64df OBC |
2645 | |
2646 | return 0; | |
2647 | } | |
160e7c84 | 2648 | EXPORT_SYMBOL(rproc_del); |
400e64df | 2649 | |
305ac5a7 PC |
2650 | static void devm_rproc_free(struct device *dev, void *res) |
2651 | { | |
2652 | rproc_free(*(struct rproc **)res); | |
2653 | } | |
2654 | ||
2655 | /** | |
2656 | * devm_rproc_alloc() - resource managed rproc_alloc() | |
2657 | * @dev: the underlying device | |
2658 | * @name: name of this remote processor | |
2659 | * @ops: platform-specific handlers (mainly start/stop) | |
2660 | * @firmware: name of firmware file to load, can be NULL | |
2661 | * @len: length of private data needed by the rproc driver (in bytes) | |
2662 | * | |
2663 | * This function performs like rproc_alloc() but the acquired rproc device will | |
2664 | * automatically be released on driver detach. | |
2665 | * | |
f2867434 | 2666 | * Return: new rproc instance, or NULL on failure |
305ac5a7 PC |
2667 | */ |
2668 | struct rproc *devm_rproc_alloc(struct device *dev, const char *name, | |
2669 | const struct rproc_ops *ops, | |
2670 | const char *firmware, int len) | |
2671 | { | |
2672 | struct rproc **ptr, *rproc; | |
2673 | ||
2674 | ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL); | |
2675 | if (!ptr) | |
7dcef398 | 2676 | return NULL; |
305ac5a7 PC |
2677 | |
2678 | rproc = rproc_alloc(dev, name, ops, firmware, len); | |
2679 | if (rproc) { | |
2680 | *ptr = rproc; | |
2681 | devres_add(dev, ptr); | |
2682 | } else { | |
2683 | devres_free(ptr); | |
2684 | } | |
2685 | ||
2686 | return rproc; | |
2687 | } | |
2688 | EXPORT_SYMBOL(devm_rproc_alloc); | |
2689 | ||
7bdc9650 BA |
2690 | /** |
2691 | * rproc_add_subdev() - add a subdevice to a remoteproc | |
2692 | * @rproc: rproc handle to add the subdevice to | |
2693 | * @subdev: subdev handle to register | |
4902676f BA |
2694 | * |
2695 | * Caller is responsible for populating optional subdevice function pointers. | |
7bdc9650 | 2696 | */ |
4902676f | 2697 | void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) |
7bdc9650 | 2698 | { |
7bdc9650 BA |
2699 | list_add_tail(&subdev->node, &rproc->subdevs); |
2700 | } | |
2701 | EXPORT_SYMBOL(rproc_add_subdev); | |
2702 | ||
2703 | /** | |
2704 | * rproc_remove_subdev() - remove a subdevice from a remoteproc | |
2705 | * @rproc: rproc handle to remove the subdevice from | |
2706 | * @subdev: subdev handle, previously registered with rproc_add_subdev() | |
2707 | */ | |
2708 | void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) | |
2709 | { | |
2710 | list_del(&subdev->node); | |
2711 | } | |
2712 | EXPORT_SYMBOL(rproc_remove_subdev); | |
2713 | ||
7c89717f BA |
2714 | /** |
2715 | * rproc_get_by_child() - acquire rproc handle of @dev's ancestor | |
2716 | * @dev: child device to find ancestor of | |
2717 | * | |
f2867434 | 2718 | * Return: the ancestor rproc instance, or NULL if not found |
7c89717f BA |
2719 | */ |
2720 | struct rproc *rproc_get_by_child(struct device *dev) | |
2721 | { | |
2722 | for (dev = dev->parent; dev; dev = dev->parent) { | |
2723 | if (dev->type == &rproc_type) | |
2724 | return dev->driver_data; | |
2725 | } | |
2726 | ||
2727 | return NULL; | |
2728 | } | |
2729 | EXPORT_SYMBOL(rproc_get_by_child); | |
2730 | ||
8afd519c FGL |
2731 | /** |
2732 | * rproc_report_crash() - rproc crash reporter function | |
2733 | * @rproc: remote processor | |
2734 | * @type: crash type | |
2735 | * | |
2736 | * This function must be called every time a crash is detected by the low-level | |
2737 | * drivers implementing a specific remoteproc. This should not be called from a | |
2738 | * non-remoteproc driver. | |
2739 | * | |
2740 | * This function can be called from atomic/interrupt context. | |
2741 | */ | |
2742 | void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) | |
2743 | { | |
2744 | if (!rproc) { | |
2745 | pr_err("NULL rproc pointer\n"); | |
2746 | return; | |
2747 | } | |
2748 | ||
a781e5aa RB |
2749 | /* Prevent suspend while the remoteproc is being recovered */ |
2750 | pm_stay_awake(rproc->dev.parent); | |
2751 | ||
8afd519c FGL |
2752 | dev_err(&rproc->dev, "crash detected in %s: type %s\n", |
2753 | rproc->name, rproc_crash_to_string(type)); | |
2754 | ||
3ad51c17 AE |
2755 | /* Have a worker handle the error; ensure system is not suspended */ |
2756 | queue_work(system_freezable_wq, &rproc->crash_handler); | |
8afd519c FGL |
2757 | } |
2758 | EXPORT_SYMBOL(rproc_report_crash); | |
2759 | ||
dc5192c4 BA |
2760 | static int rproc_panic_handler(struct notifier_block *nb, unsigned long event, |
2761 | void *ptr) | |
2762 | { | |
2763 | unsigned int longest = 0; | |
2764 | struct rproc *rproc; | |
2765 | unsigned int d; | |
2766 | ||
2767 | rcu_read_lock(); | |
2768 | list_for_each_entry_rcu(rproc, &rproc_list, node) { | |
800dad00 MP |
2769 | if (!rproc->ops->panic) |
2770 | continue; | |
2771 | ||
2772 | if (rproc->state != RPROC_RUNNING && | |
2773 | rproc->state != RPROC_ATTACHED) | |
dc5192c4 BA |
2774 | continue; |
2775 | ||
2776 | d = rproc->ops->panic(rproc); | |
2777 | longest = max(longest, d); | |
2778 | } | |
2779 | rcu_read_unlock(); | |
2780 | ||
2781 | /* | |
2782 | * Delay for the longest requested duration before returning. This can | |
2783 | * be used by the remoteproc drivers to give the remote processor time | |
2784 | * to perform any requested operations (such as flush caches), when | |
2785 | * it's not possible to signal the Linux side due to the panic. | |
2786 | */ | |
2787 | mdelay(longest); | |
2788 | ||
2789 | return NOTIFY_DONE; | |
2790 | } | |
2791 | ||
2792 | static void __init rproc_init_panic(void) | |
2793 | { | |
2794 | rproc_panic_nb.notifier_call = rproc_panic_handler; | |
2795 | atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb); | |
2796 | } | |
2797 | ||
2798 | static void __exit rproc_exit_panic(void) | |
2799 | { | |
2800 | atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb); | |
2801 | } | |
2802 | ||
400e64df OBC |
2803 | static int __init remoteproc_init(void) |
2804 | { | |
2aefbef0 | 2805 | rproc_init_sysfs(); |
400e64df | 2806 | rproc_init_debugfs(); |
62b8f9e9 | 2807 | rproc_init_cdev(); |
dc5192c4 | 2808 | rproc_init_panic(); |
b5ab5e24 | 2809 | |
400e64df OBC |
2810 | return 0; |
2811 | } | |
a8f40111 | 2812 | subsys_initcall(remoteproc_init); |
400e64df OBC |
2813 | |
2814 | static void __exit remoteproc_exit(void) | |
2815 | { | |
f42f79af SA |
2816 | ida_destroy(&rproc_dev_index); |
2817 | ||
dc5192c4 | 2818 | rproc_exit_panic(); |
400e64df | 2819 | rproc_exit_debugfs(); |
2aefbef0 | 2820 | rproc_exit_sysfs(); |
400e64df OBC |
2821 | } |
2822 | module_exit(remoteproc_exit); | |
2823 | ||
2824 | MODULE_LICENSE("GPL v2"); | |
2825 | MODULE_DESCRIPTION("Generic Remote Processor Framework"); |