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