blob: b9c963f820b353fc61407fba21b9f8ea94a292c1 [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>
40
41int arch_io_remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
42 unsigned long pfn, unsigned long size, pgprot_t prot)
43{
44 unsigned long pfn_addr = pfn << PAGE_SHIFT;
45 if ((pfn_addr >= 0x88000000) && (pfn_addr < 0xD0000000)) {
46 prot = pgprot_device(prot);
47 pr_debug("remapping device %lx\n", prot);
48 }
49 return remap_pfn_range(vma, addr, pfn, size, prot);
50}
51
52void *strongly_ordered_page;
53char strongly_ordered_mem[PAGE_SIZE*2-4];
54
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055void map_page_strongly_ordered(void)
56{
57#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
58 long unsigned int phys;
59 struct map_desc map;
60
61 if (strongly_ordered_page)
62 return;
63
64 strongly_ordered_page = (void*)PFN_ALIGN((int)&strongly_ordered_mem);
65 phys = __pa(strongly_ordered_page);
66
67 map.pfn = __phys_to_pfn(phys);
68 map.virtual = MSM_STRONGLY_ORDERED_PAGE;
69 map.length = PAGE_SIZE;
70 map.type = MT_DEVICE_STRONGLY_ORDERED;
71 create_mapping(&map);
72
73 printk(KERN_ALERT "Initialized strongly ordered page successfully\n");
74#endif
75}
76EXPORT_SYMBOL(map_page_strongly_ordered);
77
78void write_to_strongly_ordered_memory(void)
79{
80#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
81 if (!strongly_ordered_page) {
82 if (!in_interrupt())
83 map_page_strongly_ordered();
84 else {
85 printk(KERN_ALERT "Cannot map strongly ordered page in "
86 "Interrupt Context\n");
87 /* capture it here before the allocation fails later */
88 BUG();
89 }
90 }
91 *(int *)MSM_STRONGLY_ORDERED_PAGE = 0;
92#endif
93}
94EXPORT_SYMBOL(write_to_strongly_ordered_memory);
95
96void flush_axi_bus_buffer(void)
97{
98#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
99 __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" \
100 : : "r" (0) : "memory");
101 write_to_strongly_ordered_memory();
102#endif
103}
104
105#define CACHE_LINE_SIZE 32
106
107/* These cache related routines make the assumption that the associated
108 * physical memory is contiguous. They will operate on all (L1
109 * and L2 if present) caches.
110 */
111void clean_and_invalidate_caches(unsigned long vstart,
112 unsigned long length, unsigned long pstart)
113{
114 unsigned long vaddr;
115
116 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
117 asm ("mcr p15, 0, %0, c7, c14, 1" : : "r" (vaddr));
118#ifdef CONFIG_OUTER_CACHE
119 outer_flush_range(pstart, pstart + length);
120#endif
121 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
122 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
123
124 flush_axi_bus_buffer();
125}
126
127void clean_caches(unsigned long vstart,
128 unsigned long length, unsigned long pstart)
129{
130 unsigned long vaddr;
131
132 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
133 asm ("mcr p15, 0, %0, c7, c10, 1" : : "r" (vaddr));
134#ifdef CONFIG_OUTER_CACHE
135 outer_clean_range(pstart, pstart + length);
136#endif
137 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
138 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
139
140 flush_axi_bus_buffer();
141}
142
143void invalidate_caches(unsigned long vstart,
144 unsigned long length, unsigned long pstart)
145{
146 unsigned long vaddr;
147
148 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
149 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (vaddr));
150#ifdef CONFIG_OUTER_CACHE
151 outer_inv_range(pstart, pstart + length);
152#endif
153 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
154 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
155
156 flush_axi_bus_buffer();
157}
158
159void *alloc_bootmem_aligned(unsigned long size, unsigned long alignment)
160{
161 void *unused_addr = NULL;
162 unsigned long addr, tmp_size, unused_size;
163
164 /* Allocate maximum size needed, see where it ends up.
165 * Then free it -- in this path there are no other allocators
166 * so we can depend on getting the same address back
167 * when we allocate a smaller piece that is aligned
168 * at the end (if necessary) and the piece we really want,
169 * then free the unused first piece.
170 */
171
172 tmp_size = size + alignment - PAGE_SIZE;
173 addr = (unsigned long)alloc_bootmem(tmp_size);
174 free_bootmem(__pa(addr), tmp_size);
175
176 unused_size = alignment - (addr % alignment);
177 if (unused_size)
178 unused_addr = alloc_bootmem(unused_size);
179
180 addr = (unsigned long)alloc_bootmem(size);
181 if (unused_size)
182 free_bootmem(__pa(unused_addr), unused_size);
183
184 return (void *)addr;
185}
186
Larry Bassela4414b12011-08-04 11:11:02 -0700187int (*change_memory_power)(u64, u64, int);
Larry Bassela7eadea2011-07-14 10:46:00 -0700188
Larry Bassela4414b12011-08-04 11:11:02 -0700189int platform_physical_remove_pages(u64 start, u64 size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190{
Larry Bassela7eadea2011-07-14 10:46:00 -0700191 if (!change_memory_power)
192 return 0;
Larry Bassela4414b12011-08-04 11:11:02 -0700193 return change_memory_power(start, size, MEMORY_DEEP_POWERDOWN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194}
195
Larry Bassela4414b12011-08-04 11:11:02 -0700196int platform_physical_active_pages(u64 start, u64 size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197{
Larry Bassela7eadea2011-07-14 10:46:00 -0700198 if (!change_memory_power)
199 return 0;
Larry Bassela4414b12011-08-04 11:11:02 -0700200 return change_memory_power(start, size, MEMORY_ACTIVE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201}
202
Larry Bassela4414b12011-08-04 11:11:02 -0700203int platform_physical_low_power_pages(u64 start, u64 size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204{
Larry Bassela7eadea2011-07-14 10:46:00 -0700205 if (!change_memory_power)
206 return 0;
Larry Bassela4414b12011-08-04 11:11:02 -0700207 return change_memory_power(start, size, MEMORY_SELF_REFRESH);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208}
209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210char *memtype_name[] = {
211 "SMI_KERNEL",
212 "SMI",
213 "EBI0",
214 "EBI1"
215};
216
217struct reserve_info *reserve_info;
218
Larry Bassel7fb0b252011-07-22 14:18:50 -0700219static unsigned long stable_size(struct membank *mb,
220 unsigned long unstable_limit)
221{
222 if (!unstable_limit || mb->start + mb->size <= unstable_limit)
223 return mb->size;
224 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) {
299 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
308static void __init initialize_mempools(void)
309{
310 struct mem_pool *mpool;
311 int memtype;
312 struct memtype_reserve *mt;
313
314 mt = &reserve_info->memtype_reserve_table[0];
315 for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
316 if (!mt->size)
317 continue;
318 mpool = initialize_memory_pool(mt->start, mt->size, memtype);
319 if (!mpool)
320 pr_warning("failed to create %s mempool\n",
321 memtype_name[memtype]);
322 }
323}
324
325void __init msm_reserve(void)
326{
327 memory_pool_init();
328 reserve_info->calculate_reserve_sizes();
329 calculate_reserve_limits();
330 adjust_reserve_sizes();
331 reserve_memory_for_mempools();
332 initialize_mempools();
333}
334
335static int get_ebi_memtype(void)
336{
337 /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
338 if (cpu_is_msm7x30() || cpu_is_msm8x55())
339 return MEMTYPE_EBI0;
340 return MEMTYPE_EBI1;
341}
342
343void *allocate_contiguous_ebi(unsigned long size,
344 unsigned long align, int cached)
345{
346 return allocate_contiguous_memory(size, get_ebi_memtype(),
347 align, cached);
348}
349EXPORT_SYMBOL(allocate_contiguous_ebi);
350
351unsigned long allocate_contiguous_ebi_nomap(unsigned long size,
352 unsigned long align)
353{
Jordan Crouse8c78b132011-05-26 10:27:47 -0600354 return _allocate_contiguous_memory_nomap(size, get_ebi_memtype(),
355 align, __builtin_return_address(0));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356}
357EXPORT_SYMBOL(allocate_contiguous_ebi_nomap);
358
359/* emulation of the deprecated pmem_kalloc and pmem_kfree */
360int32_t pmem_kalloc(const size_t size, const uint32_t flags)
361{
362 int pmem_memtype;
363 int memtype = MEMTYPE_NONE;
364 int ebi1_memtype = MEMTYPE_EBI1;
365 unsigned int align;
366 int32_t paddr;
367
368 switch (flags & PMEM_ALIGNMENT_MASK) {
369 case PMEM_ALIGNMENT_4K:
370 align = SZ_4K;
371 break;
372 case PMEM_ALIGNMENT_1M:
373 align = SZ_1M;
374 break;
375 default:
376 pr_alert("Invalid alignment %x\n",
377 (flags & PMEM_ALIGNMENT_MASK));
378 return -EINVAL;
379 }
380
381 /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
382 if (cpu_is_msm7x30() || cpu_is_msm8x55())
383 ebi1_memtype = MEMTYPE_EBI0;
384
385 pmem_memtype = flags & PMEM_MEMTYPE_MASK;
386 if (pmem_memtype == PMEM_MEMTYPE_EBI1)
387 memtype = ebi1_memtype;
388 else if (pmem_memtype == PMEM_MEMTYPE_SMI)
389 memtype = MEMTYPE_SMI_KERNEL;
390 else {
391 pr_alert("Invalid memory type %x\n",
392 flags & PMEM_MEMTYPE_MASK);
393 return -EINVAL;
394 }
395
Jordan Crouse8c78b132011-05-26 10:27:47 -0600396 paddr = _allocate_contiguous_memory_nomap(size, memtype, align,
397 __builtin_return_address(0));
398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 if (!paddr && pmem_memtype == PMEM_MEMTYPE_SMI)
Jordan Crouse8c78b132011-05-26 10:27:47 -0600400 paddr = _allocate_contiguous_memory_nomap(size,
401 ebi1_memtype, align, __builtin_return_address(0));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402
403 if (!paddr)
404 return -ENOMEM;
405 return paddr;
406}
407EXPORT_SYMBOL(pmem_kalloc);
408
409int pmem_kfree(const int32_t physaddr)
410{
411 free_contiguous_memory_by_paddr(physaddr);
412
413 return 0;
414}
415EXPORT_SYMBOL(pmem_kfree);
Vikram Mulukutlac39c6092011-07-29 18:36:35 -0700416
417unsigned int msm_ttbr0;
418
419void store_ttbr0(void)
420{
421 /* Store TTBR0 for post-mortem debugging purposes. */
422 asm("mrc p15, 0, %0, c2, c0, 0\n"
423 : "=r" (msm_ttbr0));
424}