blob: 3b064ffab17c25ff13029cf31f88697ff9483f29 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/memory.c
2 *
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/mm.h>
18#include <linux/mm_types.h>
19#include <linux/bootmem.h>
20#include <linux/module.h>
21#include <linux/memory_alloc.h>
22#include <linux/memblock.h>
23#include <asm/pgtable.h>
24#include <asm/io.h>
25#include <asm/mach/map.h>
26#include <asm/cacheflush.h>
27#include <asm/setup.h>
28#include <asm/mach-types.h>
29#include <mach/msm_memtypes.h>
30#include <linux/hardirq.h>
31#if defined(CONFIG_MSM_NPA_REMOTE)
32#include "npa_remote.h"
33#include <linux/completion.h>
34#include <linux/err.h>
35#endif
36#include <linux/android_pmem.h>
37#include <mach/msm_iomap.h>
38#include <mach/socinfo.h>
39#include <../../mm/mm.h>
Laura Abbottf637aff2011-12-14 14:16:17 -080040#include <linux/fmem.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042void *strongly_ordered_page;
43char strongly_ordered_mem[PAGE_SIZE*2-4];
44
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045void map_page_strongly_ordered(void)
46{
47#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
48 long unsigned int phys;
49 struct map_desc map;
50
51 if (strongly_ordered_page)
52 return;
53
54 strongly_ordered_page = (void*)PFN_ALIGN((int)&strongly_ordered_mem);
55 phys = __pa(strongly_ordered_page);
56
57 map.pfn = __phys_to_pfn(phys);
58 map.virtual = MSM_STRONGLY_ORDERED_PAGE;
59 map.length = PAGE_SIZE;
60 map.type = MT_DEVICE_STRONGLY_ORDERED;
61 create_mapping(&map);
62
63 printk(KERN_ALERT "Initialized strongly ordered page successfully\n");
64#endif
65}
66EXPORT_SYMBOL(map_page_strongly_ordered);
67
68void write_to_strongly_ordered_memory(void)
69{
70#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
71 if (!strongly_ordered_page) {
72 if (!in_interrupt())
73 map_page_strongly_ordered();
74 else {
75 printk(KERN_ALERT "Cannot map strongly ordered page in "
76 "Interrupt Context\n");
77 /* capture it here before the allocation fails later */
78 BUG();
79 }
80 }
81 *(int *)MSM_STRONGLY_ORDERED_PAGE = 0;
82#endif
83}
84EXPORT_SYMBOL(write_to_strongly_ordered_memory);
85
86void flush_axi_bus_buffer(void)
87{
88#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
89 __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" \
90 : : "r" (0) : "memory");
91 write_to_strongly_ordered_memory();
92#endif
93}
94
95#define CACHE_LINE_SIZE 32
96
97/* These cache related routines make the assumption that the associated
98 * physical memory is contiguous. They will operate on all (L1
99 * and L2 if present) caches.
100 */
101void clean_and_invalidate_caches(unsigned long vstart,
102 unsigned long length, unsigned long pstart)
103{
104 unsigned long vaddr;
105
106 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
107 asm ("mcr p15, 0, %0, c7, c14, 1" : : "r" (vaddr));
108#ifdef CONFIG_OUTER_CACHE
109 outer_flush_range(pstart, pstart + length);
110#endif
111 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
112 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
113
114 flush_axi_bus_buffer();
115}
116
117void clean_caches(unsigned long vstart,
118 unsigned long length, unsigned long pstart)
119{
120 unsigned long vaddr;
121
122 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
123 asm ("mcr p15, 0, %0, c7, c10, 1" : : "r" (vaddr));
124#ifdef CONFIG_OUTER_CACHE
125 outer_clean_range(pstart, pstart + length);
126#endif
127 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
128 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
129
130 flush_axi_bus_buffer();
131}
132
133void invalidate_caches(unsigned long vstart,
134 unsigned long length, unsigned long pstart)
135{
136 unsigned long vaddr;
137
138 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
139 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (vaddr));
140#ifdef CONFIG_OUTER_CACHE
141 outer_inv_range(pstart, pstart + length);
142#endif
143 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
144 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
145
146 flush_axi_bus_buffer();
147}
148
149void *alloc_bootmem_aligned(unsigned long size, unsigned long alignment)
150{
151 void *unused_addr = NULL;
152 unsigned long addr, tmp_size, unused_size;
153
154 /* Allocate maximum size needed, see where it ends up.
155 * Then free it -- in this path there are no other allocators
156 * so we can depend on getting the same address back
157 * when we allocate a smaller piece that is aligned
158 * at the end (if necessary) and the piece we really want,
159 * then free the unused first piece.
160 */
161
162 tmp_size = size + alignment - PAGE_SIZE;
163 addr = (unsigned long)alloc_bootmem(tmp_size);
164 free_bootmem(__pa(addr), tmp_size);
165
166 unused_size = alignment - (addr % alignment);
167 if (unused_size)
168 unused_addr = alloc_bootmem(unused_size);
169
170 addr = (unsigned long)alloc_bootmem(size);
171 if (unused_size)
172 free_bootmem(__pa(unused_addr), unused_size);
173
174 return (void *)addr;
175}
176
Larry Bassela4414b12011-08-04 11:11:02 -0700177int (*change_memory_power)(u64, u64, int);
Larry Bassela7eadea2011-07-14 10:46:00 -0700178
Larry Bassela4414b12011-08-04 11:11:02 -0700179int platform_physical_remove_pages(u64 start, u64 size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180{
Larry Bassela7eadea2011-07-14 10:46:00 -0700181 if (!change_memory_power)
182 return 0;
Larry Bassela4414b12011-08-04 11:11:02 -0700183 return change_memory_power(start, size, MEMORY_DEEP_POWERDOWN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184}
185
Larry Bassela4414b12011-08-04 11:11:02 -0700186int platform_physical_active_pages(u64 start, u64 size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187{
Larry Bassela7eadea2011-07-14 10:46:00 -0700188 if (!change_memory_power)
189 return 0;
Larry Bassela4414b12011-08-04 11:11:02 -0700190 return change_memory_power(start, size, MEMORY_ACTIVE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191}
192
Larry Bassela4414b12011-08-04 11:11:02 -0700193int platform_physical_low_power_pages(u64 start, u64 size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194{
Larry Bassela7eadea2011-07-14 10:46:00 -0700195 if (!change_memory_power)
196 return 0;
Larry Bassela4414b12011-08-04 11:11:02 -0700197 return change_memory_power(start, size, MEMORY_SELF_REFRESH);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198}
199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200char *memtype_name[] = {
201 "SMI_KERNEL",
202 "SMI",
203 "EBI0",
204 "EBI1"
205};
206
207struct reserve_info *reserve_info;
208
Larry Bassel7fb0b252011-07-22 14:18:50 -0700209static unsigned long stable_size(struct membank *mb,
210 unsigned long unstable_limit)
211{
Olav Haugan5b633022011-10-28 13:26:31 -0700212 unsigned long upper_limit = mb->start + mb->size;
213
214 if (!unstable_limit)
Larry Bassel7fb0b252011-07-22 14:18:50 -0700215 return mb->size;
Olav Haugan5b633022011-10-28 13:26:31 -0700216
217 /* Check for 32 bit roll-over */
218 if (upper_limit >= mb->start) {
219 /* If we didn't roll over we can safely make the check below */
220 if (upper_limit <= unstable_limit)
221 return mb->size;
222 }
223
Larry Bassel7fb0b252011-07-22 14:18:50 -0700224 if (mb->start >= unstable_limit)
225 return 0;
226 return unstable_limit - mb->start;
227}
228
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229static void __init calculate_reserve_limits(void)
230{
231 int i;
232 struct membank *mb;
233 int memtype;
234 struct memtype_reserve *mt;
Larry Bassel7fb0b252011-07-22 14:18:50 -0700235 unsigned long size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236
237 for (i = 0, mb = &meminfo.bank[0]; i < meminfo.nr_banks; i++, mb++) {
238 memtype = reserve_info->paddr_to_memtype(mb->start);
239 if (memtype == MEMTYPE_NONE) {
240 pr_warning("unknown memory type for bank at %lx\n",
241 (long unsigned int)mb->start);
242 continue;
243 }
244 mt = &reserve_info->memtype_reserve_table[memtype];
Larry Bassel7fb0b252011-07-22 14:18:50 -0700245 size = stable_size(mb, reserve_info->low_unstable_address);
246 mt->limit = max(mt->limit, size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 }
248}
249
250static void __init adjust_reserve_sizes(void)
251{
252 int i;
253 struct memtype_reserve *mt;
254
255 mt = &reserve_info->memtype_reserve_table[0];
256 for (i = 0; i < MEMTYPE_MAX; i++, mt++) {
257 if (mt->flags & MEMTYPE_FLAGS_1M_ALIGN)
258 mt->size = (mt->size + SECTION_SIZE - 1) & SECTION_MASK;
259 if (mt->size > mt->limit) {
260 pr_warning("%lx size for %s too large, setting to %lx\n",
261 mt->size, memtype_name[i], mt->limit);
262 mt->size = mt->limit;
263 }
264 }
265}
266
267static void __init reserve_memory_for_mempools(void)
268{
269 int i, memtype, membank_type;
270 struct memtype_reserve *mt;
271 struct membank *mb;
272 int ret;
Larry Bassel7fb0b252011-07-22 14:18:50 -0700273 unsigned long size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275 mt = &reserve_info->memtype_reserve_table[0];
276 for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
277 if (mt->flags & MEMTYPE_FLAGS_FIXED || !mt->size)
278 continue;
279
280 /* We know we will find a memory bank of the proper size
281 * as we have limited the size of the memory pool for
282 * each memory type to the size of the largest memory
283 * bank. Choose the memory bank with the highest physical
284 * address which is large enough, so that we will not
285 * take memory from the lowest memory bank which the kernel
286 * is in (and cause boot problems) and so that we might
287 * be able to steal memory that would otherwise become
Larry Bassel7fb0b252011-07-22 14:18:50 -0700288 * highmem. However, do not use unstable memory.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 */
290 for (i = meminfo.nr_banks - 1; i >= 0; i--) {
291 mb = &meminfo.bank[i];
292 membank_type =
293 reserve_info->paddr_to_memtype(mb->start);
294 if (memtype != membank_type)
295 continue;
Larry Bassel7fb0b252011-07-22 14:18:50 -0700296 size = stable_size(mb,
297 reserve_info->low_unstable_address);
298 if (size >= mt->size) {
Olav Haugan5b633022011-10-28 13:26:31 -0700299 mt->start = mb->start + (size - mt->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 ret = memblock_remove(mt->start, mt->size);
301 BUG_ON(ret);
302 break;
303 }
304 }
305 }
306}
307
Larry Bassel1eb58f02011-12-06 16:01:59 -0800308unsigned long __init reserve_memory_for_fmem(unsigned long fmem_size)
309{
310 struct membank *mb;
311 int ret;
312 unsigned long fmem_phys;
313
314 if (!fmem_size)
315 return 0;
316
317 mb = &meminfo.bank[meminfo.nr_banks - 1];
Olav Haugan069fa452012-02-10 15:45:23 -0800318 /*
319 * Placing fmem at the top of memory causes multimedia issues.
320 * Instead, place it 1 page below the top of memory to prevent
321 * the issues from occurring.
322 */
323 fmem_phys = mb->start + (mb->size - fmem_size) - PAGE_SIZE;
Larry Bassel1eb58f02011-12-06 16:01:59 -0800324 ret = memblock_remove(fmem_phys, fmem_size);
325 BUG_ON(ret);
326
327 pr_info("fmem start %lx size %lx\n", fmem_phys, fmem_size);
328 return fmem_phys;
329}
330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331static void __init initialize_mempools(void)
332{
333 struct mem_pool *mpool;
334 int memtype;
335 struct memtype_reserve *mt;
336
337 mt = &reserve_info->memtype_reserve_table[0];
338 for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
339 if (!mt->size)
340 continue;
341 mpool = initialize_memory_pool(mt->start, mt->size, memtype);
342 if (!mpool)
343 pr_warning("failed to create %s mempool\n",
344 memtype_name[memtype]);
345 }
346}
347
348void __init msm_reserve(void)
349{
350 memory_pool_init();
351 reserve_info->calculate_reserve_sizes();
352 calculate_reserve_limits();
353 adjust_reserve_sizes();
354 reserve_memory_for_mempools();
355 initialize_mempools();
356}
357
358static int get_ebi_memtype(void)
359{
360 /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
361 if (cpu_is_msm7x30() || cpu_is_msm8x55())
362 return MEMTYPE_EBI0;
363 return MEMTYPE_EBI1;
364}
365
366void *allocate_contiguous_ebi(unsigned long size,
367 unsigned long align, int cached)
368{
369 return allocate_contiguous_memory(size, get_ebi_memtype(),
370 align, cached);
371}
372EXPORT_SYMBOL(allocate_contiguous_ebi);
373
374unsigned long allocate_contiguous_ebi_nomap(unsigned long size,
375 unsigned long align)
376{
Jordan Crouse8c78b132011-05-26 10:27:47 -0600377 return _allocate_contiguous_memory_nomap(size, get_ebi_memtype(),
378 align, __builtin_return_address(0));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379}
380EXPORT_SYMBOL(allocate_contiguous_ebi_nomap);
381
382/* emulation of the deprecated pmem_kalloc and pmem_kfree */
383int32_t pmem_kalloc(const size_t size, const uint32_t flags)
384{
385 int pmem_memtype;
386 int memtype = MEMTYPE_NONE;
387 int ebi1_memtype = MEMTYPE_EBI1;
388 unsigned int align;
389 int32_t paddr;
390
391 switch (flags & PMEM_ALIGNMENT_MASK) {
392 case PMEM_ALIGNMENT_4K:
393 align = SZ_4K;
394 break;
395 case PMEM_ALIGNMENT_1M:
396 align = SZ_1M;
397 break;
398 default:
399 pr_alert("Invalid alignment %x\n",
400 (flags & PMEM_ALIGNMENT_MASK));
401 return -EINVAL;
402 }
403
404 /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
405 if (cpu_is_msm7x30() || cpu_is_msm8x55())
406 ebi1_memtype = MEMTYPE_EBI0;
407
408 pmem_memtype = flags & PMEM_MEMTYPE_MASK;
409 if (pmem_memtype == PMEM_MEMTYPE_EBI1)
410 memtype = ebi1_memtype;
411 else if (pmem_memtype == PMEM_MEMTYPE_SMI)
412 memtype = MEMTYPE_SMI_KERNEL;
413 else {
414 pr_alert("Invalid memory type %x\n",
415 flags & PMEM_MEMTYPE_MASK);
416 return -EINVAL;
417 }
418
Jordan Crouse8c78b132011-05-26 10:27:47 -0600419 paddr = _allocate_contiguous_memory_nomap(size, memtype, align,
420 __builtin_return_address(0));
421
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 if (!paddr && pmem_memtype == PMEM_MEMTYPE_SMI)
Jordan Crouse8c78b132011-05-26 10:27:47 -0600423 paddr = _allocate_contiguous_memory_nomap(size,
424 ebi1_memtype, align, __builtin_return_address(0));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425
426 if (!paddr)
427 return -ENOMEM;
428 return paddr;
429}
430EXPORT_SYMBOL(pmem_kalloc);
431
432int pmem_kfree(const int32_t physaddr)
433{
434 free_contiguous_memory_by_paddr(physaddr);
435
436 return 0;
437}
438EXPORT_SYMBOL(pmem_kfree);
Vikram Mulukutlac39c6092011-07-29 18:36:35 -0700439
440unsigned int msm_ttbr0;
441
442void store_ttbr0(void)
443{
444 /* Store TTBR0 for post-mortem debugging purposes. */
445 asm("mrc p15, 0, %0, c2, c0, 0\n"
446 : "=r" (msm_ttbr0));
447}
Laura Abbottf637aff2011-12-14 14:16:17 -0800448
449int request_fmem_c_region(void *unused)
450{
451 return fmem_set_state(FMEM_C_STATE);
452}
453
454int release_fmem_c_region(void *unused)
455{
456 return fmem_set_state(FMEM_T_STATE);
457}