blob: 6ec563a8b7e153755b0043cbe8748ab271f916f9 [file] [log] [blame]
Dima Zavin9caac252009-01-26 12:37:15 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi6133e8d2012-10-07 22:15:02 -07005 * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
Shashank Mittal8e49dec2010-03-01 15:19:04 -08006 *
Dima Zavin9caac252009-01-26 12:37:15 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
Amol Jadi6133e8d2012-10-07 22:15:02 -070014 * the documentation and/or other materials provided with the
Dima Zavin9caac252009-01-26 12:37:15 -080015 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Amol Jadi6133e8d2012-10-07 22:15:02 -070024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Dima Zavin9caac252009-01-26 12:37:15 -080025 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <debug.h>
32#include <reg.h>
33#include <string.h>
34#include <sys/types.h>
35#include <platform/iomap.h>
Shashank Mittal8e49dec2010-03-01 15:19:04 -080036#include <lib/ptable.h>
Dima Zavin9caac252009-01-26 12:37:15 -080037
38#include "smem.h"
39
Dima Zavin9caac252009-01-26 12:37:15 -080040
41/* partition table from SMEM */
42static struct smem_ptable smem_ptable;
Amol Jadi6133e8d2012-10-07 22:15:02 -070043static unsigned smem_apps_flash_start = 0xFFFFFFFF;
Dima Zavin9caac252009-01-26 12:37:15 -080044
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -070045static ram_partition_table ptable;
46
Dima Zavin9caac252009-01-26 12:37:15 -080047static void dump_smem_ptable(void)
48{
Amol Jadi6133e8d2012-10-07 22:15:02 -070049 unsigned i;
Dima Zavin9caac252009-01-26 12:37:15 -080050
Amol Jadi6133e8d2012-10-07 22:15:02 -070051 for (i = 0; i < smem_ptable.len; i++) {
Dima Zavin9caac252009-01-26 12:37:15 -080052 struct smem_ptn *p = &smem_ptable.parts[i];
53 if (p->name[0] == '\0')
54 continue;
55 dprintf(SPEW, "%d: %s offs=0x%08x size=0x%08x attr: 0x%08x\n",
56 i, p->name, p->start, p->size, p->attr);
57 }
58}
59
60void smem_ptable_init(void)
61{
62 unsigned i;
Amol Jadi6133e8d2012-10-07 22:15:02 -070063 unsigned ret;
64 unsigned len;
Dima Zavin9caac252009-01-26 12:37:15 -080065
Amol Jadi6133e8d2012-10-07 22:15:02 -070066 /* Read only the header portion of ptable */
67 ret = smem_read_alloc_entry_offset(SMEM_AARM_PARTITION_TABLE,
68 &smem_ptable,
69 SMEM_PTABLE_HDR_LEN,
70 0);
71 if (ret)
72 {
73 dprintf(CRITICAL, "Failed to read ptable hdr (%d)", ret);
74 ASSERT(0);
75 }
Dima Zavin9caac252009-01-26 12:37:15 -080076
Amol Jadi6133e8d2012-10-07 22:15:02 -070077 /* Verify ptable magic */
Dima Zavin9caac252009-01-26 12:37:15 -080078 if (smem_ptable.magic[0] != _SMEM_PTABLE_MAGIC_1 ||
79 smem_ptable.magic[1] != _SMEM_PTABLE_MAGIC_2)
80 return;
81
Amol Jadi6133e8d2012-10-07 22:15:02 -070082 /* Ensure that # of partitions is less than the max we have allocated. */
83 ASSERT(smem_ptable.len <= SMEM_PTABLE_MAX_PARTS);
84
85 /* Find out length of partition data based on table version. */
86 if (smem_ptable.version <= 3)
87 {
88 len = SMEM_PTABLE_HDR_LEN + SMEM_PTABLE_MAX_PARTS_V3*sizeof(struct smem_ptn);
89 }
90 else if (smem_ptable.version == 4)
91 {
92 len = SMEM_PTABLE_HDR_LEN + SMEM_PTABLE_MAX_PARTS_V4*sizeof(struct smem_ptn);
93 }
94 else
95 {
96 dprintf(CRITICAL, "Unknown ptable version (%d)", smem_ptable.version);
97 ASSERT(0);
98 }
99
100 ret = smem_read_alloc_entry(SMEM_AARM_PARTITION_TABLE,
101 &smem_ptable,
102 len);
103 if (ret)
104 {
105 dprintf(CRITICAL, "Failed to read ptable (%d)", ret);
106 ASSERT(0);
107 }
108
Dima Zavin9caac252009-01-26 12:37:15 -0800109 dump_smem_ptable();
110 dprintf(INFO, "smem ptable found: ver: %d len: %d\n",
Amol Jadi6133e8d2012-10-07 22:15:02 -0700111 smem_ptable.version, smem_ptable.len);
Dima Zavin9caac252009-01-26 12:37:15 -0800112
113 for (i = 0; i < smem_ptable.len; i++) {
114 if (!strcmp(smem_ptable.parts[i].name, "0:APPS"))
Ajay Dudanib01e5062011-12-03 23:23:42 -0800115 break;
Dima Zavin9caac252009-01-26 12:37:15 -0800116 }
117 if (i == smem_ptable.len)
118 return;
119
120 smem_apps_flash_start = smem_ptable.parts[i].start;
121}
122
123unsigned smem_get_apps_flash_start(void)
124{
125 return smem_apps_flash_start;
126}
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800127
128void smem_add_modem_partitions(struct ptable *flash_ptable)
129{
Amol Jadi6133e8d2012-10-07 22:15:02 -0700130 unsigned i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800131
132 if (smem_ptable.magic[0] != _SMEM_PTABLE_MAGIC_1 ||
133 smem_ptable.magic[1] != _SMEM_PTABLE_MAGIC_2)
134 return;
135
Amol Jadi6133e8d2012-10-07 22:15:02 -0700136 for (i = 0; i < smem_ptable.len; i++) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800137 char *token;
138 char *pname = NULL;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800139 struct smem_ptn *p = &smem_ptable.parts[i];
Ajay Dudanib01e5062011-12-03 23:23:42 -0800140 if (p->name[0] == '\0')
141 continue;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800142 token = strtok(p->name, ":");
Ajay Dudanib01e5062011-12-03 23:23:42 -0800143 while (token) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800144 pname = token;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800145 token = strtok(NULL, ":");
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800146 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800147 if (pname) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800148 ptable_add(flash_ptable, pname, p->start,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800149 p->size, 0, TYPE_MODEM_PARTITION,
150 PERM_WRITEABLE);
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800151 }
152 }
153}
154
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700155static void smem_copy_ram_ptable(void *buf)
156{
157 struct smem_ram_ptable *table_v0;
158 struct smem_ram_ptable_v1 *table_v1;
159 uint32_t pentry = 0;
160
161 ptable.hdr = *(struct smem_ram_ptable_hdr*)buf;
162
163 /* Perform member to member copy from smem_ram_ptable to wrapper struct ram_ptable */
164 if(ptable.hdr.version == SMEM_RAM_PTABLE_VERSION_1)
165 {
166 table_v1 = (struct smem_ram_ptable_v1*)buf;
167
168 memcpy(&ptable, table_v1, sizeof(ram_partition_table));
169 }
170 else if(ptable.hdr.version == SMEM_RAM_PTABLE_VERSION_0)
171 {
172 table_v0 = (struct smem_ram_ptable*)buf;
173
174 for(pentry = 0; pentry < ((struct smem_ram_ptable_hdr*)buf)->len; pentry++)
175 {
176 ptable.parts[pentry].start = table_v0->parts[pentry].start;
177 ptable.parts[pentry].size = table_v0->parts[pentry].size;
178 ptable.parts[pentry].attr = table_v0->parts[pentry].attr;
179 ptable.parts[pentry].category = table_v0->parts[pentry].category;
180 ptable.parts[pentry].domain = table_v0->parts[pentry].domain;
181 ptable.parts[pentry].type = table_v0->parts[pentry].type;
182 ptable.parts[pentry].num_partitions = table_v0->parts[pentry].num_partitions;
183 }
184
185 }
186 else
187 {
188 dprintf(CRITICAL,"ERROR: Unknown smem ram ptable version: %u", ptable.hdr.version);
189 ASSERT(0);
190 }
191}
192
Ajay Dudania1eafc42010-04-21 19:48:11 -0700193/* RAM Partition table from SMEM */
194int smem_ram_ptable_init(struct smem_ram_ptable *smem_ram_ptable)
195{
196 unsigned i;
197
198 i = smem_read_alloc_entry(SMEM_USABLE_RAM_PARTITION_TABLE,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800199 smem_ram_ptable,
200 sizeof(struct smem_ram_ptable));
Ajay Dudania1eafc42010-04-21 19:48:11 -0700201 if (i != 0)
202 return 0;
203
204 if (smem_ram_ptable->magic[0] != _SMEM_RAM_PTABLE_MAGIC_1 ||
205 smem_ram_ptable->magic[1] != _SMEM_RAM_PTABLE_MAGIC_2)
206 return 0;
207
Ajay Dudaniebb0b5b2011-08-02 14:35:55 -0700208 dprintf(SPEW, "smem ram ptable found: ver: %d len: %d\n",
Ajay Dudania1eafc42010-04-21 19:48:11 -0700209 smem_ram_ptable->version, smem_ram_ptable->len);
210
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700211 smem_copy_ram_ptable((void*)smem_ram_ptable);
212
Ajay Dudania1eafc42010-04-21 19:48:11 -0700213 return 1;
214}
Sundarajan Srinivasanbd8a5712013-07-09 17:09:09 -0700215
216/* RAM Partition table from SMEM */
217static uint32_t buffer[sizeof(struct smem_ram_ptable_v1)];
218int smem_ram_ptable_init_v1()
219{
220 uint32_t i;
221 uint32_t ret;
222 uint32_t version;
223 uint32_t smem_ram_ptable_size;
224 struct smem_ram_ptable_hdr *ram_ptable_hdr;
225
226 /* Check smem ram partition table version and decide on length of ram_ptable */
227 ret = smem_read_alloc_entry_offset(SMEM_USABLE_RAM_PARTITION_TABLE,
228 &version,
229 sizeof(version),
230 SMEM_RAM_PTABLE_VERSION_OFFSET);
231
232 if(ret)
233 return 0;
234
235 if(version == SMEM_RAM_PTABLE_VERSION_1)
236 smem_ram_ptable_size = sizeof(struct smem_ram_ptable_v1);
237 else if(version == SMEM_RAM_PTABLE_VERSION_0)
238 smem_ram_ptable_size = sizeof(struct smem_ram_ptable);
239 else
240 {
241 dprintf(CRITICAL,"ERROR: Wrong smem_ram_ptable version: %u", version);
242 ASSERT(0);
243 }
244
245 i = smem_read_alloc_entry(SMEM_USABLE_RAM_PARTITION_TABLE,
246 (void*)buffer,
247 smem_ram_ptable_size);
248 if (i != 0)
249 return 0;
250
251 ram_ptable_hdr = (struct smem_ram_ptable_hdr *)buffer;
252
253 if (ram_ptable_hdr->magic[0] != _SMEM_RAM_PTABLE_MAGIC_1 ||
254 ram_ptable_hdr->magic[1] != _SMEM_RAM_PTABLE_MAGIC_2)
255 return 0;
256
257 smem_copy_ram_ptable((void*)buffer);
258
259 dprintf(SPEW, "smem ram ptable found: ver: %u len: %u\n",
260 ram_ptable_hdr->version, ram_ptable_hdr->len);
261
262 return 1;
263}
264
265void smem_get_ram_ptable_entry(ram_partition *ptn, uint32_t entry)
266{
267 memcpy(ptn, &(ptable.parts[entry]), sizeof(ram_partition));
268}
269
270uint32_t smem_get_ram_ptable_len(void)
271{
272 return ptable.hdr.len;
273}
274
275uint32_t smem_get_ram_ptable_version(void)
276{
277 return ptable.hdr.version;
278}