1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define pr_fmt(fmt) "%s: " fmt, __func__
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/slab.h>
31#include <linux/mutex.h>
32#include <linux/dma-mapping.h>
33#include <linux/firmware.h>
34#include <linux/string.h>
35#include <linux/debugfs.h>
36#include <linux/remoteproc.h>
37#include <linux/iommu.h>
38#include <linux/idr.h>
39#include <linux/elf.h>
40#include <linux/virtio_ids.h>
41#include <linux/virtio_ring.h>
42#include <asm/byteorder.h>
43
44#include "remoteproc_internal.h"
45
46typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
47 struct resource_table *table, int len);
48typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
49
50
51static DEFINE_IDA(rproc_dev_index);
52
53
54
55
56
57
58
59
60
61
62
63
64static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
65 unsigned long iova, int flags, void *token)
66{
67 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
68
69
70
71
72
73 return -ENOSYS;
74}
75
76static int rproc_enable_iommu(struct rproc *rproc)
77{
78 struct iommu_domain *domain;
79 struct device *dev = rproc->dev.parent;
80 int ret;
81
82
83
84
85
86
87
88
89
90
91
92
93 if (!iommu_present(dev->bus)) {
94 dev_dbg(dev, "iommu not found\n");
95 return 0;
96 }
97
98 domain = iommu_domain_alloc(dev->bus);
99 if (!domain) {
100 dev_err(dev, "can't alloc iommu domain\n");
101 return -ENOMEM;
102 }
103
104 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
105
106 ret = iommu_attach_device(domain, dev);
107 if (ret) {
108 dev_err(dev, "can't attach iommu device: %d\n", ret);
109 goto free_domain;
110 }
111
112 rproc->domain = domain;
113
114 return 0;
115
116free_domain:
117 iommu_domain_free(domain);
118 return ret;
119}
120
121static void rproc_disable_iommu(struct rproc *rproc)
122{
123 struct iommu_domain *domain = rproc->domain;
124 struct device *dev = rproc->dev.parent;
125
126 if (!domain)
127 return;
128
129 iommu_detach_device(domain, dev);
130 iommu_domain_free(domain);
131
132 return;
133}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
153{
154 struct rproc_mem_entry *carveout;
155 void *ptr = NULL;
156
157 list_for_each_entry(carveout, &rproc->carveouts, node) {
158 int offset = da - carveout->da;
159
160
161 if (offset < 0)
162 continue;
163
164
165 if (offset + len > carveout->len)
166 continue;
167
168 ptr = carveout->va + offset;
169
170 break;
171 }
172
173 return ptr;
174}
175EXPORT_SYMBOL(rproc_da_to_va);
176
177int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
178{
179 struct rproc *rproc = rvdev->rproc;
180 struct device *dev = &rproc->dev;
181 struct rproc_vring *rvring = &rvdev->vring[i];
182 dma_addr_t dma;
183 void *va;
184 int ret, size, notifyid;
185
186
187 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
188
189 if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
190 dev_err(dev, "idr_pre_get failed\n");
191 return -ENOMEM;
192 }
193
194
195
196
197
198
199 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
200 if (!va) {
201 dev_err(dev->parent, "dma_alloc_coherent failed\n");
202 return -EINVAL;
203 }
204
205
206
207
208
209
210
211 ret = idr_get_new(&rproc->notifyids, rvring, ¬ifyid);
212 if (ret) {
213 dev_err(dev, "idr_get_new failed: %d\n", ret);
214 dma_free_coherent(dev->parent, size, va, dma);
215 return ret;
216 }
217
218 dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
219 dma, size, notifyid);
220
221 rvring->va = va;
222 rvring->dma = dma;
223 rvring->notifyid = notifyid;
224
225 return 0;
226}
227
228static int
229rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
230{
231 struct rproc *rproc = rvdev->rproc;
232 struct device *dev = &rproc->dev;
233 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
234 struct rproc_vring *rvring = &rvdev->vring[i];
235
236 dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
237 i, vring->da, vring->num, vring->align);
238
239
240 if (vring->reserved) {
241 dev_err(dev, "vring rsc has non zero reserved bytes\n");
242 return -EINVAL;
243 }
244
245
246 if (!vring->num || !vring->align) {
247 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
248 vring->num, vring->align);
249 return -EINVAL;
250 }
251
252 rvring->len = vring->num;
253 rvring->align = vring->align;
254 rvring->rvdev = rvdev;
255
256 return 0;
257}
258
259void rproc_free_vring(struct rproc_vring *rvring)
260{
261 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
262 struct rproc *rproc = rvring->rvdev->rproc;
263
264 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
265 idr_remove(&rproc->notifyids, rvring->notifyid);
266}
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
296 int avail)
297{
298 struct device *dev = &rproc->dev;
299 struct rproc_vdev *rvdev;
300 int i, ret;
301
302
303 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
304 + rsc->config_len > avail) {
305 dev_err(dev, "vdev rsc is truncated\n");
306 return -EINVAL;
307 }
308
309
310 if (rsc->reserved[0] || rsc->reserved[1]) {
311 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
312 return -EINVAL;
313 }
314
315 dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
316 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
317
318
319 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
320 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
321 return -EINVAL;
322 }
323
324 rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
325 if (!rvdev)
326 return -ENOMEM;
327
328 rvdev->rproc = rproc;
329
330
331 for (i = 0; i < rsc->num_of_vrings; i++) {
332 ret = rproc_parse_vring(rvdev, rsc, i);
333 if (ret)
334 goto free_rvdev;
335 }
336
337
338 rvdev->dfeatures = rsc->dfeatures;
339
340 list_add_tail(&rvdev->node, &rproc->rvdevs);
341
342
343 ret = rproc_add_virtio_dev(rvdev, rsc->id);
344 if (ret)
345 goto free_rvdev;
346
347 return 0;
348
349free_rvdev:
350 kfree(rvdev);
351 return ret;
352}
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
371 int avail)
372{
373 struct rproc_mem_entry *trace;
374 struct device *dev = &rproc->dev;
375 void *ptr;
376 char name[15];
377
378 if (sizeof(*rsc) > avail) {
379 dev_err(dev, "trace rsc is truncated\n");
380 return -EINVAL;
381 }
382
383
384 if (rsc->reserved) {
385 dev_err(dev, "trace rsc has non zero reserved bytes\n");
386 return -EINVAL;
387 }
388
389
390 ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
391 if (!ptr) {
392 dev_err(dev, "erroneous trace resource entry\n");
393 return -EINVAL;
394 }
395
396 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
397 if (!trace) {
398 dev_err(dev, "kzalloc trace failed\n");
399 return -ENOMEM;
400 }
401
402
403 trace->len = rsc->len;
404 trace->va = ptr;
405
406
407 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
408
409
410 trace->priv = rproc_create_trace_file(name, rproc, trace);
411 if (!trace->priv) {
412 trace->va = NULL;
413 kfree(trace);
414 return -EINVAL;
415 }
416
417 list_add_tail(&trace->node, &rproc->traces);
418
419 rproc->num_traces++;
420
421 dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
422 rsc->da, rsc->len);
423
424 return 0;
425}
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
453 int avail)
454{
455 struct rproc_mem_entry *mapping;
456 struct device *dev = &rproc->dev;
457 int ret;
458
459
460 if (!rproc->domain)
461 return -EINVAL;
462
463 if (sizeof(*rsc) > avail) {
464 dev_err(dev, "devmem rsc is truncated\n");
465 return -EINVAL;
466 }
467
468
469 if (rsc->reserved) {
470 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
471 return -EINVAL;
472 }
473
474 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
475 if (!mapping) {
476 dev_err(dev, "kzalloc mapping failed\n");
477 return -ENOMEM;
478 }
479
480 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
481 if (ret) {
482 dev_err(dev, "failed to map devmem: %d\n", ret);
483 goto out;
484 }
485
486
487
488
489
490
491
492
493 mapping->da = rsc->da;
494 mapping->len = rsc->len;
495 list_add_tail(&mapping->node, &rproc->mappings);
496
497 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
498 rsc->pa, rsc->da, rsc->len);
499
500 return 0;
501
502out:
503 kfree(mapping);
504 return ret;
505}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525static int rproc_handle_carveout(struct rproc *rproc,
526 struct fw_rsc_carveout *rsc, int avail)
527{
528 struct rproc_mem_entry *carveout, *mapping;
529 struct device *dev = &rproc->dev;
530 dma_addr_t dma;
531 void *va;
532 int ret;
533
534 if (sizeof(*rsc) > avail) {
535 dev_err(dev, "carveout rsc is truncated\n");
536 return -EINVAL;
537 }
538
539
540 if (rsc->reserved) {
541 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
542 return -EINVAL;
543 }
544
545 dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
546 rsc->da, rsc->pa, rsc->len, rsc->flags);
547
548 carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
549 if (!carveout) {
550 dev_err(dev, "kzalloc carveout failed\n");
551 return -ENOMEM;
552 }
553
554 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
555 if (!va) {
556 dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
557 ret = -ENOMEM;
558 goto free_carv;
559 }
560
561 dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580 if (rproc->domain) {
581 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
582 if (!mapping) {
583 dev_err(dev, "kzalloc mapping failed\n");
584 ret = -ENOMEM;
585 goto dma_free;
586 }
587
588 ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
589 rsc->flags);
590 if (ret) {
591 dev_err(dev, "iommu_map failed: %d\n", ret);
592 goto free_mapping;
593 }
594
595
596
597
598
599
600
601
602 mapping->da = rsc->da;
603 mapping->len = rsc->len;
604 list_add_tail(&mapping->node, &rproc->mappings);
605
606 dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
607 }
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 rsc->pa = dma;
627
628 carveout->va = va;
629 carveout->len = rsc->len;
630 carveout->dma = dma;
631 carveout->da = rsc->da;
632
633 list_add_tail(&carveout->node, &rproc->carveouts);
634
635 return 0;
636
637free_mapping:
638 kfree(mapping);
639dma_free:
640 dma_free_coherent(dev->parent, rsc->len, va, dma);
641free_carv:
642 kfree(carveout);
643 return ret;
644}
645
646
647
648
649
650static rproc_handle_resource_t rproc_handle_rsc[] = {
651 [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
652 [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
653 [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
654 [RSC_VDEV] = NULL,
655};
656
657
658static int
659rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
660{
661 struct device *dev = &rproc->dev;
662 rproc_handle_resource_t handler;
663 int ret = 0, i;
664
665 for (i = 0; i < table->num; i++) {
666 int offset = table->offset[i];
667 struct fw_rsc_hdr *hdr = (void *)table + offset;
668 int avail = len - offset - sizeof(*hdr);
669 void *rsc = (void *)hdr + sizeof(*hdr);
670
671
672 if (avail < 0) {
673 dev_err(dev, "rsc table is truncated\n");
674 return -EINVAL;
675 }
676
677 dev_dbg(dev, "rsc: type %d\n", hdr->type);
678
679 if (hdr->type >= RSC_LAST) {
680 dev_warn(dev, "unsupported resource %d\n", hdr->type);
681 continue;
682 }
683
684 handler = rproc_handle_rsc[hdr->type];
685 if (!handler)
686 continue;
687
688 ret = handler(rproc, rsc, avail);
689 if (ret)
690 break;
691 }
692
693 return ret;
694}
695
696
697static int
698rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
699{
700 struct device *dev = &rproc->dev;
701 int ret = 0, i;
702
703 for (i = 0; i < table->num; i++) {
704 int offset = table->offset[i];
705 struct fw_rsc_hdr *hdr = (void *)table + offset;
706 int avail = len - offset - sizeof(*hdr);
707 struct fw_rsc_vdev *vrsc;
708
709
710 if (avail < 0) {
711 dev_err(dev, "rsc table is truncated\n");
712 return -EINVAL;
713 }
714
715 dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
716
717 if (hdr->type != RSC_VDEV)
718 continue;
719
720 vrsc = (struct fw_rsc_vdev *)hdr->data;
721
722 ret = rproc_handle_vdev(rproc, vrsc, avail);
723 if (ret)
724 break;
725 }
726
727 return ret;
728}
729
730
731
732
733
734
735
736
737static void rproc_resource_cleanup(struct rproc *rproc)
738{
739 struct rproc_mem_entry *entry, *tmp;
740 struct device *dev = &rproc->dev;
741
742
743 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
744 rproc_remove_trace_file(entry->priv);
745 rproc->num_traces--;
746 list_del(&entry->node);
747 kfree(entry);
748 }
749
750
751 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
752 dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
753 list_del(&entry->node);
754 kfree(entry);
755 }
756
757
758 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
759 size_t unmapped;
760
761 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
762 if (unmapped != entry->len) {
763
764 dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
765 unmapped);
766 }
767
768 list_del(&entry->node);
769 kfree(entry);
770 }
771}
772
773
774
775
776static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
777{
778 struct device *dev = &rproc->dev;
779 const char *name = rproc->firmware;
780 struct resource_table *table;
781 int ret, tablesz;
782
783 ret = rproc_fw_sanity_check(rproc, fw);
784 if (ret)
785 return ret;
786
787 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
788
789
790
791
792
793 ret = rproc_enable_iommu(rproc);
794 if (ret) {
795 dev_err(dev, "can't enable iommu: %d\n", ret);
796 return ret;
797 }
798
799 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
800
801
802 table = rproc_find_rsc_table(rproc, fw, &tablesz);
803 if (!table) {
804 ret = -EINVAL;
805 goto clean_up;
806 }
807
808
809 ret = rproc_handle_boot_rsc(rproc, table, tablesz);
810 if (ret) {
811 dev_err(dev, "Failed to process resources: %d\n", ret);
812 goto clean_up;
813 }
814
815
816 ret = rproc_load_segments(rproc, fw);
817 if (ret) {
818 dev_err(dev, "Failed to load program segments: %d\n", ret);
819 goto clean_up;
820 }
821
822
823 ret = rproc->ops->start(rproc);
824 if (ret) {
825 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
826 goto clean_up;
827 }
828
829 rproc->state = RPROC_RUNNING;
830
831 dev_info(dev, "remote processor %s is now up\n", rproc->name);
832
833 return 0;
834
835clean_up:
836 rproc_resource_cleanup(rproc);
837 rproc_disable_iommu(rproc);
838 return ret;
839}
840
841
842
843
844
845
846
847
848
849static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
850{
851 struct rproc *rproc = context;
852 struct resource_table *table;
853 int ret, tablesz;
854
855 if (rproc_fw_sanity_check(rproc, fw) < 0)
856 goto out;
857
858
859 table = rproc_find_rsc_table(rproc, fw, &tablesz);
860 if (!table)
861 goto out;
862
863
864 ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
865 if (ret)
866 goto out;
867
868out:
869 release_firmware(fw);
870
871 complete_all(&rproc->firmware_loading_complete);
872}
873
874
875
876
877
878
879
880
881
882
883
884
885int rproc_boot(struct rproc *rproc)
886{
887 const struct firmware *firmware_p;
888 struct device *dev;
889 int ret;
890
891 if (!rproc) {
892 pr_err("invalid rproc handle\n");
893 return -EINVAL;
894 }
895
896 dev = &rproc->dev;
897
898 ret = mutex_lock_interruptible(&rproc->lock);
899 if (ret) {
900 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
901 return ret;
902 }
903
904
905 if (!rproc->firmware) {
906 dev_err(dev, "%s: no firmware to load\n", __func__);
907 ret = -EINVAL;
908 goto unlock_mutex;
909 }
910
911
912 if (!try_module_get(dev->parent->driver->owner)) {
913 dev_err(dev, "%s: can't get owner\n", __func__);
914 ret = -EINVAL;
915 goto unlock_mutex;
916 }
917
918
919 if (atomic_inc_return(&rproc->power) > 1) {
920 ret = 0;
921 goto unlock_mutex;
922 }
923
924 dev_info(dev, "powering up %s\n", rproc->name);
925
926
927 ret = request_firmware(&firmware_p, rproc->firmware, dev);
928 if (ret < 0) {
929 dev_err(dev, "request_firmware failed: %d\n", ret);
930 goto downref_rproc;
931 }
932
933 ret = rproc_fw_boot(rproc, firmware_p);
934
935 release_firmware(firmware_p);
936
937downref_rproc:
938 if (ret) {
939 module_put(dev->parent->driver->owner);
940 atomic_dec(&rproc->power);
941 }
942unlock_mutex:
943 mutex_unlock(&rproc->lock);
944 return ret;
945}
946EXPORT_SYMBOL(rproc_boot);
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967void rproc_shutdown(struct rproc *rproc)
968{
969 struct device *dev = &rproc->dev;
970 int ret;
971
972 ret = mutex_lock_interruptible(&rproc->lock);
973 if (ret) {
974 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
975 return;
976 }
977
978
979 if (!atomic_dec_and_test(&rproc->power))
980 goto out;
981
982
983 ret = rproc->ops->stop(rproc);
984 if (ret) {
985 atomic_inc(&rproc->power);
986 dev_err(dev, "can't stop rproc: %d\n", ret);
987 goto out;
988 }
989
990
991 rproc_resource_cleanup(rproc);
992
993 rproc_disable_iommu(rproc);
994
995 rproc->state = RPROC_OFFLINE;
996
997 dev_info(dev, "stopped remote processor %s\n", rproc->name);
998
999out:
1000 mutex_unlock(&rproc->lock);
1001 if (!ret)
1002 module_put(dev->parent->driver->owner);
1003}
1004EXPORT_SYMBOL(rproc_shutdown);
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026int rproc_add(struct rproc *rproc)
1027{
1028 struct device *dev = &rproc->dev;
1029 int ret = 0;
1030
1031 ret = device_add(dev);
1032 if (ret < 0)
1033 return ret;
1034
1035 dev_info(dev, "%s is available\n", rproc->name);
1036
1037 dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1038 dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1039
1040
1041 rproc_create_debug_dir(rproc);
1042
1043
1044 init_completion(&rproc->firmware_loading_complete);
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
1055 rproc->firmware, dev, GFP_KERNEL,
1056 rproc, rproc_fw_config_virtio);
1057 if (ret < 0) {
1058 dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
1059 complete_all(&rproc->firmware_loading_complete);
1060 }
1061
1062 return ret;
1063}
1064EXPORT_SYMBOL(rproc_add);
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075static void rproc_type_release(struct device *dev)
1076{
1077 struct rproc *rproc = container_of(dev, struct rproc, dev);
1078
1079 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1080
1081 rproc_delete_debug_dir(rproc);
1082
1083 idr_remove_all(&rproc->notifyids);
1084 idr_destroy(&rproc->notifyids);
1085
1086 if (rproc->index >= 0)
1087 ida_simple_remove(&rproc_dev_index, rproc->index);
1088
1089 kfree(rproc);
1090}
1091
1092static struct device_type rproc_type = {
1093 .name = "remoteproc",
1094 .release = rproc_type_release,
1095};
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120struct rproc *rproc_alloc(struct device *dev, const char *name,
1121 const struct rproc_ops *ops,
1122 const char *firmware, int len)
1123{
1124 struct rproc *rproc;
1125
1126 if (!dev || !name || !ops)
1127 return NULL;
1128
1129 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1130 if (!rproc) {
1131 dev_err(dev, "%s: kzalloc failed\n", __func__);
1132 return NULL;
1133 }
1134
1135 rproc->name = name;
1136 rproc->ops = ops;
1137 rproc->firmware = firmware;
1138 rproc->priv = &rproc[1];
1139
1140 device_initialize(&rproc->dev);
1141 rproc->dev.parent = dev;
1142 rproc->dev.type = &rproc_type;
1143
1144
1145 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1146 if (rproc->index < 0) {
1147 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1148 put_device(&rproc->dev);
1149 return NULL;
1150 }
1151
1152 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1153
1154 atomic_set(&rproc->power, 0);
1155
1156
1157 rproc->fw_ops = &rproc_elf_fw_ops;
1158
1159 mutex_init(&rproc->lock);
1160
1161 idr_init(&rproc->notifyids);
1162
1163 INIT_LIST_HEAD(&rproc->carveouts);
1164 INIT_LIST_HEAD(&rproc->mappings);
1165 INIT_LIST_HEAD(&rproc->traces);
1166 INIT_LIST_HEAD(&rproc->rvdevs);
1167
1168 rproc->state = RPROC_OFFLINE;
1169
1170 return rproc;
1171}
1172EXPORT_SYMBOL(rproc_alloc);
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183void rproc_put(struct rproc *rproc)
1184{
1185 put_device(&rproc->dev);
1186}
1187EXPORT_SYMBOL(rproc_put);
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204int rproc_del(struct rproc *rproc)
1205{
1206 struct rproc_vdev *rvdev, *tmp;
1207
1208 if (!rproc)
1209 return -EINVAL;
1210
1211
1212 wait_for_completion(&rproc->firmware_loading_complete);
1213
1214
1215 list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
1216 rproc_remove_virtio_dev(rvdev);
1217
1218 device_del(&rproc->dev);
1219
1220 return 0;
1221}
1222EXPORT_SYMBOL(rproc_del);
1223
1224static int __init remoteproc_init(void)
1225{
1226 rproc_init_debugfs();
1227
1228 return 0;
1229}
1230module_init(remoteproc_init);
1231
1232static void __exit remoteproc_exit(void)
1233{
1234 rproc_exit_debugfs();
1235}
1236module_exit(remoteproc_exit);
1237
1238MODULE_LICENSE("GPL v2");
1239MODULE_DESCRIPTION("Generic Remote Processor Framework");
1240