]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/remoteproc/qcom_mdt_loader.c
remoteproc: qcom: Merge cleanup and fixup from the future
[karo-tx-linux.git] / drivers / remoteproc / qcom_mdt_loader.c
1 /*
2  * Qualcomm Peripheral Image Loader
3  *
4  * Copyright (C) 2016 Linaro Ltd
5  * Copyright (C) 2015 Sony Mobile Communications Inc
6  * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/elf.h>
19 #include <linux/firmware.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/qcom_scm.h>
23 #include <linux/remoteproc.h>
24 #include <linux/slab.h>
25
26 #include "remoteproc_internal.h"
27 #include "qcom_mdt_loader.h"
28
29 /**
30  * qcom_mdt_find_rsc_table() - provide dummy resource table for remoteproc
31  * @rproc:      remoteproc handle
32  * @fw:         firmware header
33  * @tablesz:    outgoing size of the table
34  *
35  * Returns a dummy table.
36  */
37 struct resource_table *qcom_mdt_find_rsc_table(struct rproc *rproc,
38                                                const struct firmware *fw,
39                                                int *tablesz)
40 {
41         static struct resource_table table = { .ver = 1, };
42
43         *tablesz = sizeof(table);
44         return &table;
45 }
46 EXPORT_SYMBOL_GPL(qcom_mdt_find_rsc_table);
47
48 int qcom_mdt_parse(const struct firmware *fw, phys_addr_t *fw_addr, size_t *fw_size, bool *fw_relocate)
49 {
50         const struct elf32_phdr *phdrs;
51         const struct elf32_phdr *phdr;
52         const struct elf32_hdr *ehdr;
53         phys_addr_t min_addr = (phys_addr_t)ULLONG_MAX;
54         phys_addr_t max_addr = 0;
55         bool relocate = false;
56         int i;
57
58         ehdr = (struct elf32_hdr *)fw->data;
59         phdrs = (struct elf32_phdr *)(ehdr + 1);
60
61         for (i = 0; i < ehdr->e_phnum; i++) {
62                 phdr = &phdrs[i];
63
64                 if (phdr->p_type != PT_LOAD)
65                         continue;
66
67                 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
68                         continue;
69
70                 if (!phdr->p_memsz)
71                         continue;
72
73                 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
74                         relocate = true;
75
76                 if (phdr->p_paddr < min_addr)
77                         min_addr = phdr->p_paddr;
78
79                 if (phdr->p_paddr + phdr->p_memsz > max_addr)
80                         max_addr = round_up(phdr->p_paddr + phdr->p_memsz, SZ_4K);
81         }
82
83         if (fw_addr)
84                 *fw_addr = min_addr;
85         if (fw_size)
86                 *fw_size = max_addr - min_addr;
87         if (fw_relocate)
88                 *fw_relocate = relocate;
89
90         return 0;
91 }
92 EXPORT_SYMBOL_GPL(qcom_mdt_parse);
93
94 /**
95  * qcom_mdt_load() - load the firmware which header is defined in fw
96  * @rproc:      rproc handle
97  * @pas_id:     PAS identifier to load this firmware into
98  * @fw:         frimware object for the header
99  *
100  * Returns 0 on success, negative errno otherwise.
101  */
102 int qcom_mdt_load(struct rproc *rproc,
103                   const struct firmware *fw,
104                   const char *firmware)
105 {
106         const struct elf32_phdr *phdrs;
107         const struct elf32_phdr *phdr;
108         const struct elf32_hdr *ehdr;
109         unsigned fw_name_len;
110         char *fw_name;
111         void *ptr;
112         int ret;
113         int i;
114
115         ehdr = (struct elf32_hdr *)fw->data;
116         phdrs = (struct elf32_phdr *)(ehdr + 1);
117
118         fw_name_len = strlen(firmware);
119         if (fw_name_len <= 4)
120                 return -EINVAL;
121
122         fw_name = kstrdup(firmware, GFP_KERNEL);
123         if (!fw_name)
124                 return -ENOMEM;
125
126         for (i = 0; i < ehdr->e_phnum; i++) {
127                 phdr = &phdrs[i];
128
129                 if (phdr->p_type != PT_LOAD)
130                         continue;
131
132                 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
133                         continue;
134
135                 if (!phdr->p_memsz)
136                         continue;
137
138                 ptr = rproc_da_to_va(rproc, phdr->p_paddr, phdr->p_memsz);
139                 if (!ptr) {
140                         dev_err(&rproc->dev, "segment outside memory range\n");
141                         ret = -EINVAL;
142                         break;
143                 }
144
145                 if (phdr->p_filesz) {
146                         sprintf(fw_name + fw_name_len - 3, "b%02d", i);
147                         ret = request_firmware(&fw, fw_name, &rproc->dev);
148                         if (ret) {
149                                 dev_err(&rproc->dev, "failed to load %s\n", fw_name);
150                                 break;
151                         }
152
153                         memcpy(ptr, fw->data, fw->size);
154
155                         release_firmware(fw);
156                 }
157
158                 if (phdr->p_memsz > phdr->p_filesz)
159                         memset(ptr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz);
160         }
161
162         kfree(fw_name);
163
164         return ret;
165 }
166 EXPORT_SYMBOL_GPL(qcom_mdt_load);