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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/* arch/arm/mach-msm/memory.c
*
* Copyright (C) 2007 Google, Inc.
* Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/bootmem.h>
#include <linux/module.h>
#include <linux/memory_alloc.h>
#include <linux/memblock.h>
#include <asm/pgtable.h>
#include <asm/io.h>
#include <asm/mach/map.h>
#include <asm/cacheflush.h>
#include <asm/setup.h>
#include <asm/mach-types.h>
#include <mach/msm_memtypes.h>
#include <linux/hardirq.h>
#if defined(CONFIG_MSM_NPA_REMOTE)
#include "npa_remote.h"
#include <linux/completion.h>
#include <linux/err.h>
#endif
#include <linux/android_pmem.h>
#include <mach/msm_iomap.h>
#include <mach/socinfo.h>
#include <linux/sched.h>
#include <linux/of_fdt.h>
/* fixme */
#include <asm/tlbflush.h>
#include <../../mm/mm.h>
#include <linux/fmem.h>
void *strongly_ordered_page;
char strongly_ordered_mem[PAGE_SIZE*2-4];
void map_page_strongly_ordered(void)
{
#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
long unsigned int phys;
struct map_desc map;
if (strongly_ordered_page)
return;
strongly_ordered_page = (void*)PFN_ALIGN((int)&strongly_ordered_mem);
phys = __pa(strongly_ordered_page);
map.pfn = __phys_to_pfn(phys);
map.virtual = MSM_STRONGLY_ORDERED_PAGE;
map.length = PAGE_SIZE;
map.type = MT_DEVICE_STRONGLY_ORDERED;
create_mapping(&map);
printk(KERN_ALERT "Initialized strongly ordered page successfully\n");
#endif
}
EXPORT_SYMBOL(map_page_strongly_ordered);
void write_to_strongly_ordered_memory(void)
{
#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
if (!strongly_ordered_page) {
if (!in_interrupt())
map_page_strongly_ordered();
else {
printk(KERN_ALERT "Cannot map strongly ordered page in "
"Interrupt Context\n");
/* capture it here before the allocation fails later */
BUG();
}
}
*(int *)MSM_STRONGLY_ORDERED_PAGE = 0;
#endif
}
EXPORT_SYMBOL(write_to_strongly_ordered_memory);
/* These cache related routines make the assumption (if outer cache is
* available) that the associated physical memory is contiguous.
* They will operate on all (L1 and L2 if present) caches.
*/
void clean_and_invalidate_caches(unsigned long vstart,
unsigned long length, unsigned long pstart)
{
dmac_flush_range((void *)vstart, (void *) (vstart + length));
outer_flush_range(pstart, pstart + length);
}
void clean_caches(unsigned long vstart,
unsigned long length, unsigned long pstart)
{
dmac_clean_range((void *)vstart, (void *) (vstart + length));
outer_clean_range(pstart, pstart + length);
}
void invalidate_caches(unsigned long vstart,
unsigned long length, unsigned long pstart)
{
dmac_inv_range((void *)vstart, (void *) (vstart + length));
outer_inv_range(pstart, pstart + length);
}
void * __init alloc_bootmem_aligned(unsigned long size, unsigned long alignment)
{
void *unused_addr = NULL;
unsigned long addr, tmp_size, unused_size;
/* Allocate maximum size needed, see where it ends up.
* Then free it -- in this path there are no other allocators
* so we can depend on getting the same address back
* when we allocate a smaller piece that is aligned
* at the end (if necessary) and the piece we really want,
* then free the unused first piece.
*/
tmp_size = size + alignment - PAGE_SIZE;
addr = (unsigned long)alloc_bootmem(tmp_size);
free_bootmem(__pa(addr), tmp_size);
unused_size = alignment - (addr % alignment);
if (unused_size)
unused_addr = alloc_bootmem(unused_size);
addr = (unsigned long)alloc_bootmem(size);
if (unused_size)
free_bootmem(__pa(unused_addr), unused_size);
return (void *)addr;
}
int (*change_memory_power)(u64, u64, int);
int platform_physical_remove_pages(u64 start, u64 size)
{
if (!change_memory_power)
return 0;
return change_memory_power(start, size, MEMORY_DEEP_POWERDOWN);
}
int platform_physical_active_pages(u64 start, u64 size)
{
if (!change_memory_power)
return 0;
return change_memory_power(start, size, MEMORY_ACTIVE);
}
int platform_physical_low_power_pages(u64 start, u64 size)
{
if (!change_memory_power)
return 0;
return change_memory_power(start, size, MEMORY_SELF_REFRESH);
}
char *memtype_name[] = {
"SMI_KERNEL",
"SMI",
"EBI0",
"EBI1"
};
struct reserve_info *reserve_info;
static unsigned long stable_size(struct membank *mb,
unsigned long unstable_limit)
{
unsigned long upper_limit = mb->start + mb->size;
if (!unstable_limit)
return mb->size;
/* Check for 32 bit roll-over */
if (upper_limit >= mb->start) {
/* If we didn't roll over we can safely make the check below */
if (upper_limit <= unstable_limit)
return mb->size;
}
if (mb->start >= unstable_limit)
return 0;
return unstable_limit - mb->start;
}
/* stable size of all memory banks contiguous to and below this one */
static unsigned long total_stable_size(unsigned long bank)
{
int i;
struct membank *mb = &meminfo.bank[bank];
int memtype = reserve_info->paddr_to_memtype(mb->start);
unsigned long size;
size = stable_size(mb, reserve_info->low_unstable_address);
for (i = bank - 1, mb = &meminfo.bank[bank - 1]; i >= 0; i--, mb--) {
if (mb->start + mb->size != (mb + 1)->start)
break;
if (reserve_info->paddr_to_memtype(mb->start) != memtype)
break;
size += stable_size(mb, reserve_info->low_unstable_address);
}
return size;
}
static void __init calculate_reserve_limits(void)
{
int i;
struct membank *mb;
int memtype;
struct memtype_reserve *mt;
unsigned long size;
for (i = 0, mb = &meminfo.bank[0]; i < meminfo.nr_banks; i++, mb++) {
memtype = reserve_info->paddr_to_memtype(mb->start);
if (memtype == MEMTYPE_NONE) {
pr_warning("unknown memory type for bank at %lx\n",
(long unsigned int)mb->start);
continue;
}
mt = &reserve_info->memtype_reserve_table[memtype];
size = total_stable_size(i);
mt->limit = max(mt->limit, size);
}
}
static void __init adjust_reserve_sizes(void)
{
int i;
struct memtype_reserve *mt;
mt = &reserve_info->memtype_reserve_table[0];
for (i = 0; i < MEMTYPE_MAX; i++, mt++) {
if (mt->flags & MEMTYPE_FLAGS_1M_ALIGN)
mt->size = (mt->size + SECTION_SIZE - 1) & SECTION_MASK;
if (mt->size > mt->limit) {
pr_warning("%lx size for %s too large, setting to %lx\n",
mt->size, memtype_name[i], mt->limit);
mt->size = mt->limit;
}
}
}
static void __init reserve_memory_for_mempools(void)
{
int i, memtype, membank_type;
struct memtype_reserve *mt;
struct membank *mb;
int ret;
unsigned long size;
mt = &reserve_info->memtype_reserve_table[0];
for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
if (mt->flags & MEMTYPE_FLAGS_FIXED || !mt->size)
continue;
/* We know we will find memory bank(s) of the proper size
* as we have limited the size of the memory pool for
* each memory type to the largest total size of the memory
* banks which are contiguous and of the correct memory type.
* Choose the memory bank with the highest physical
* address which is large enough, so that we will not
* take memory from the lowest memory bank which the kernel
* is in (and cause boot problems) and so that we might
* be able to steal memory that would otherwise become
* highmem. However, do not use unstable memory.
*/
for (i = meminfo.nr_banks - 1; i >= 0; i--) {
mb = &meminfo.bank[i];
membank_type =
reserve_info->paddr_to_memtype(mb->start);
if (memtype != membank_type)
continue;
size = total_stable_size(i);
if (size >= mt->size) {
size = stable_size(mb,
reserve_info->low_unstable_address);
if (!size)
continue;
/* mt->size may be larger than size, all this
* means is that we are carving the memory pool
* out of multiple contiguous memory banks.
*/
mt->start = mb->start + (size - mt->size);
ret = memblock_remove(mt->start, mt->size);
BUG_ON(ret);
break;
}
}
}
}
static void __init initialize_mempools(void)
{
struct mem_pool *mpool;
int memtype;
struct memtype_reserve *mt;
mt = &reserve_info->memtype_reserve_table[0];
for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
if (!mt->size)
continue;
mpool = initialize_memory_pool(mt->start, mt->size, memtype);
if (!mpool)
pr_warning("failed to create %s mempool\n",
memtype_name[memtype]);
}
}
#define MAX_FIXED_AREA_SIZE 0x11000000
void __init msm_reserve(void)
{
unsigned long msm_fixed_area_size;
unsigned long msm_fixed_area_start;
memory_pool_init();
reserve_info->calculate_reserve_sizes();
msm_fixed_area_size = reserve_info->fixed_area_size;
msm_fixed_area_start = reserve_info->fixed_area_start;
if (msm_fixed_area_size)
if (msm_fixed_area_start > reserve_info->low_unstable_address
- MAX_FIXED_AREA_SIZE)
reserve_info->low_unstable_address =
msm_fixed_area_start;
calculate_reserve_limits();
adjust_reserve_sizes();
reserve_memory_for_mempools();
initialize_mempools();
}
static int get_ebi_memtype(void)
{
/* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
if (cpu_is_msm7x30() || cpu_is_msm8x55())
return MEMTYPE_EBI0;
return MEMTYPE_EBI1;
}
void *allocate_contiguous_ebi(unsigned long size,
unsigned long align, int cached)
{
return allocate_contiguous_memory(size, get_ebi_memtype(),
align, cached);
}
EXPORT_SYMBOL(allocate_contiguous_ebi);
unsigned long allocate_contiguous_ebi_nomap(unsigned long size,
unsigned long align)
{
return _allocate_contiguous_memory_nomap(size, get_ebi_memtype(),
align, __builtin_return_address(0));
}
EXPORT_SYMBOL(allocate_contiguous_ebi_nomap);
unsigned int msm_ttbr0;
void store_ttbr0(void)
{
/* Store TTBR0 for post-mortem debugging purposes. */
asm("mrc p15, 0, %0, c2, c0, 0\n"
: "=r" (msm_ttbr0));
}
int request_fmem_c_region(void *unused)
{
return fmem_set_state(FMEM_C_STATE);
}
int release_fmem_c_region(void *unused)
{
return fmem_set_state(FMEM_T_STATE);
}
static char * const memtype_names[] = {
[MEMTYPE_SMI_KERNEL] = "SMI_KERNEL",
[MEMTYPE_SMI] = "SMI",
[MEMTYPE_EBI0] = "EBI0",
[MEMTYPE_EBI1] = "EBI1",
};
int msm_get_memory_type_from_name(const char *memtype_name)
{
int i;
for (i = 0; i < ARRAY_SIZE(memtype_names); i++) {
if (memtype_names[i] &&
strcmp(memtype_name, memtype_names[i]) == 0)
return i;
}
pr_err("Could not find memory type %s\n", memtype_name);
return -EINVAL;
}
static int reserve_memory_type(const char *mem_name,
struct memtype_reserve *reserve_table,
int size)
{
int ret = msm_get_memory_type_from_name(mem_name);
if (ret >= 0) {
reserve_table[ret].size += size;
ret = 0;
}
return ret;
}
static int check_for_compat(unsigned long node)
{
char **start = __compat_exports_start;
for ( ; start < __compat_exports_end; start++)
if (of_flat_dt_is_compatible(node, *start))
return 1;
return 0;
}
int __init dt_scan_for_memory_reserve(unsigned long node, const char *uname,
int depth, void *data)
{
char *memory_name_prop;
unsigned int *memory_remove_prop;
unsigned long memory_name_prop_length;
unsigned long memory_remove_prop_length;
unsigned long memory_size_prop_length;
unsigned int *memory_size_prop;
unsigned int memory_size;
unsigned int memory_start;
int ret;
memory_name_prop = of_get_flat_dt_prop(node,
"qcom,memory-reservation-type",
&memory_name_prop_length);
memory_remove_prop = of_get_flat_dt_prop(node,
"qcom,memblock-remove",
&memory_remove_prop_length);
if (memory_name_prop || memory_remove_prop) {
if (!check_for_compat(node))
goto out;
} else {
goto out;
}
if (memory_name_prop) {
if (strnlen(memory_name_prop, memory_name_prop_length) == 0) {
WARN(1, "Memory name was malformed\n");
goto mem_remove;
}
memory_size_prop = of_get_flat_dt_prop(node,
"qcom,memory-reservation-size",
&memory_size_prop_length);
if (memory_size_prop &&
(memory_size_prop_length == sizeof(unsigned int))) {
memory_size = be32_to_cpu(*memory_size_prop);
if (reserve_memory_type(memory_name_prop,
data, memory_size) == 0)
pr_info("%s reserved %s size %x\n",
uname, memory_name_prop, memory_size);
else
WARN(1, "Node %s reserve failed\n",
uname);
} else {
WARN(1, "Node %s specified bad/nonexistent size\n",
uname);
}
}
mem_remove:
if (memory_remove_prop) {
if (memory_remove_prop_length != (2*sizeof(unsigned int))) {
WARN(1, "Memory remove malformed\n");
goto out;
}
memory_start = be32_to_cpu(memory_remove_prop[0]);
memory_size = be32_to_cpu(memory_remove_prop[1]);
ret = memblock_remove(memory_start, memory_size);
if (ret)
WARN(1, "Failed to remove memory %x-%x\n",
memory_start, memory_start+memory_size);
else
pr_info("Node %s removed memory %x-%x\n", uname,
memory_start, memory_start+memory_size);
}
out:
return 0;
}
unsigned long get_ddr_size(void)
{
unsigned int i;
unsigned long ret = 0;
for (i = 0; i < meminfo.nr_banks; i++)
ret += meminfo.bank[i].size;
return ret;
}
|