blob: c38071ca3caaaceb9b22699064b7ce37f904268f [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
55/*
56 * The trick of making the zero page strongly ordered no longer
57 * works. We no longer want to make a second alias to the zero
58 * page that is strongly ordered. Manually changing the bits
59 * in the page table for the zero page would have side effects
60 * elsewhere that aren't necessary. The result is that we need
61 * to get a page from else where. Given when the first call
62 * to write_to_strongly_ordered_memory occurs, using bootmem
63 * to get a page makes the most sense.
64 */
65void map_page_strongly_ordered(void)
66{
67#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
68 long unsigned int phys;
69 struct map_desc map;
70
71 if (strongly_ordered_page)
72 return;
73
74 strongly_ordered_page = (void*)PFN_ALIGN((int)&strongly_ordered_mem);
75 phys = __pa(strongly_ordered_page);
76
77 map.pfn = __phys_to_pfn(phys);
78 map.virtual = MSM_STRONGLY_ORDERED_PAGE;
79 map.length = PAGE_SIZE;
80 map.type = MT_DEVICE_STRONGLY_ORDERED;
81 create_mapping(&map);
82
83 printk(KERN_ALERT "Initialized strongly ordered page successfully\n");
84#endif
85}
86EXPORT_SYMBOL(map_page_strongly_ordered);
87
88void write_to_strongly_ordered_memory(void)
89{
90#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
91 if (!strongly_ordered_page) {
92 if (!in_interrupt())
93 map_page_strongly_ordered();
94 else {
95 printk(KERN_ALERT "Cannot map strongly ordered page in "
96 "Interrupt Context\n");
97 /* capture it here before the allocation fails later */
98 BUG();
99 }
100 }
101 *(int *)MSM_STRONGLY_ORDERED_PAGE = 0;
102#endif
103}
104EXPORT_SYMBOL(write_to_strongly_ordered_memory);
105
106void flush_axi_bus_buffer(void)
107{
108#if defined(CONFIG_ARCH_MSM7X27) && !defined(CONFIG_ARCH_MSM7X27A)
109 __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" \
110 : : "r" (0) : "memory");
111 write_to_strongly_ordered_memory();
112#endif
113}
114
115#define CACHE_LINE_SIZE 32
116
117/* These cache related routines make the assumption that the associated
118 * physical memory is contiguous. They will operate on all (L1
119 * and L2 if present) caches.
120 */
121void clean_and_invalidate_caches(unsigned long vstart,
122 unsigned long length, unsigned long pstart)
123{
124 unsigned long vaddr;
125
126 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
127 asm ("mcr p15, 0, %0, c7, c14, 1" : : "r" (vaddr));
128#ifdef CONFIG_OUTER_CACHE
129 outer_flush_range(pstart, pstart + length);
130#endif
131 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
132 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
133
134 flush_axi_bus_buffer();
135}
136
137void clean_caches(unsigned long vstart,
138 unsigned long length, unsigned long pstart)
139{
140 unsigned long vaddr;
141
142 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
143 asm ("mcr p15, 0, %0, c7, c10, 1" : : "r" (vaddr));
144#ifdef CONFIG_OUTER_CACHE
145 outer_clean_range(pstart, pstart + length);
146#endif
147 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
148 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
149
150 flush_axi_bus_buffer();
151}
152
153void invalidate_caches(unsigned long vstart,
154 unsigned long length, unsigned long pstart)
155{
156 unsigned long vaddr;
157
158 for (vaddr = vstart; vaddr < vstart + length; vaddr += CACHE_LINE_SIZE)
159 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (vaddr));
160#ifdef CONFIG_OUTER_CACHE
161 outer_inv_range(pstart, pstart + length);
162#endif
163 asm ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
164 asm ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
165
166 flush_axi_bus_buffer();
167}
168
169void *alloc_bootmem_aligned(unsigned long size, unsigned long alignment)
170{
171 void *unused_addr = NULL;
172 unsigned long addr, tmp_size, unused_size;
173
174 /* Allocate maximum size needed, see where it ends up.
175 * Then free it -- in this path there are no other allocators
176 * so we can depend on getting the same address back
177 * when we allocate a smaller piece that is aligned
178 * at the end (if necessary) and the piece we really want,
179 * then free the unused first piece.
180 */
181
182 tmp_size = size + alignment - PAGE_SIZE;
183 addr = (unsigned long)alloc_bootmem(tmp_size);
184 free_bootmem(__pa(addr), tmp_size);
185
186 unused_size = alignment - (addr % alignment);
187 if (unused_size)
188 unused_addr = alloc_bootmem(unused_size);
189
190 addr = (unsigned long)alloc_bootmem(size);
191 if (unused_size)
192 free_bootmem(__pa(unused_addr), unused_size);
193
194 return (void *)addr;
195}
196
197int platform_physical_remove_pages(unsigned long start_pfn,
198 unsigned long nr_pages)
199{
200 return 1;
201}
202
203int platform_physical_active_pages(unsigned long start_pfn,
204 unsigned long nr_pages)
205{
206 return 1;
207}
208
209int platform_physical_low_power_pages(unsigned long start_pfn,
210 unsigned long nr_pages)
211{
212 return 1;
213}
214
215
216char *memtype_name[] = {
217 "SMI_KERNEL",
218 "SMI",
219 "EBI0",
220 "EBI1"
221};
222
223struct reserve_info *reserve_info;
224
225static void __init calculate_reserve_limits(void)
226{
227 int i;
228 struct membank *mb;
229 int memtype;
230 struct memtype_reserve *mt;
231
232 for (i = 0, mb = &meminfo.bank[0]; i < meminfo.nr_banks; i++, mb++) {
233 memtype = reserve_info->paddr_to_memtype(mb->start);
234 if (memtype == MEMTYPE_NONE) {
235 pr_warning("unknown memory type for bank at %lx\n",
236 (long unsigned int)mb->start);
237 continue;
238 }
239 mt = &reserve_info->memtype_reserve_table[memtype];
240 mt->limit = max(mt->limit, mb->size);
241 }
242}
243
244static void __init adjust_reserve_sizes(void)
245{
246 int i;
247 struct memtype_reserve *mt;
248
249 mt = &reserve_info->memtype_reserve_table[0];
250 for (i = 0; i < MEMTYPE_MAX; i++, mt++) {
251 if (mt->flags & MEMTYPE_FLAGS_1M_ALIGN)
252 mt->size = (mt->size + SECTION_SIZE - 1) & SECTION_MASK;
253 if (mt->size > mt->limit) {
254 pr_warning("%lx size for %s too large, setting to %lx\n",
255 mt->size, memtype_name[i], mt->limit);
256 mt->size = mt->limit;
257 }
258 }
259}
260
261static void __init reserve_memory_for_mempools(void)
262{
263 int i, memtype, membank_type;
264 struct memtype_reserve *mt;
265 struct membank *mb;
266 int ret;
267
268 mt = &reserve_info->memtype_reserve_table[0];
269 for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
270 if (mt->flags & MEMTYPE_FLAGS_FIXED || !mt->size)
271 continue;
272
273 /* We know we will find a memory bank of the proper size
274 * as we have limited the size of the memory pool for
275 * each memory type to the size of the largest memory
276 * bank. Choose the memory bank with the highest physical
277 * address which is large enough, so that we will not
278 * take memory from the lowest memory bank which the kernel
279 * is in (and cause boot problems) and so that we might
280 * be able to steal memory that would otherwise become
281 * highmem.
282 */
283 for (i = meminfo.nr_banks - 1; i >= 0; i--) {
284 mb = &meminfo.bank[i];
285 membank_type =
286 reserve_info->paddr_to_memtype(mb->start);
287 if (memtype != membank_type)
288 continue;
289 if (mb->size >= mt->size) {
290 mt->start = mb->start + mb->size - mt->size;
291 ret = memblock_remove(mt->start, mt->size);
292 BUG_ON(ret);
293 break;
294 }
295 }
296 }
297}
298
299static void __init initialize_mempools(void)
300{
301 struct mem_pool *mpool;
302 int memtype;
303 struct memtype_reserve *mt;
304
305 mt = &reserve_info->memtype_reserve_table[0];
306 for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) {
307 if (!mt->size)
308 continue;
309 mpool = initialize_memory_pool(mt->start, mt->size, memtype);
310 if (!mpool)
311 pr_warning("failed to create %s mempool\n",
312 memtype_name[memtype]);
313 }
314}
315
316void __init msm_reserve(void)
317{
318 memory_pool_init();
319 reserve_info->calculate_reserve_sizes();
320 calculate_reserve_limits();
321 adjust_reserve_sizes();
322 reserve_memory_for_mempools();
323 initialize_mempools();
324}
325
326static int get_ebi_memtype(void)
327{
328 /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
329 if (cpu_is_msm7x30() || cpu_is_msm8x55())
330 return MEMTYPE_EBI0;
331 return MEMTYPE_EBI1;
332}
333
334void *allocate_contiguous_ebi(unsigned long size,
335 unsigned long align, int cached)
336{
337 return allocate_contiguous_memory(size, get_ebi_memtype(),
338 align, cached);
339}
340EXPORT_SYMBOL(allocate_contiguous_ebi);
341
342unsigned long allocate_contiguous_ebi_nomap(unsigned long size,
343 unsigned long align)
344{
345 return allocate_contiguous_memory_nomap(size, get_ebi_memtype(), align);
346}
347EXPORT_SYMBOL(allocate_contiguous_ebi_nomap);
348
349/* emulation of the deprecated pmem_kalloc and pmem_kfree */
350int32_t pmem_kalloc(const size_t size, const uint32_t flags)
351{
352 int pmem_memtype;
353 int memtype = MEMTYPE_NONE;
354 int ebi1_memtype = MEMTYPE_EBI1;
355 unsigned int align;
356 int32_t paddr;
357
358 switch (flags & PMEM_ALIGNMENT_MASK) {
359 case PMEM_ALIGNMENT_4K:
360 align = SZ_4K;
361 break;
362 case PMEM_ALIGNMENT_1M:
363 align = SZ_1M;
364 break;
365 default:
366 pr_alert("Invalid alignment %x\n",
367 (flags & PMEM_ALIGNMENT_MASK));
368 return -EINVAL;
369 }
370
371 /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */
372 if (cpu_is_msm7x30() || cpu_is_msm8x55())
373 ebi1_memtype = MEMTYPE_EBI0;
374
375 pmem_memtype = flags & PMEM_MEMTYPE_MASK;
376 if (pmem_memtype == PMEM_MEMTYPE_EBI1)
377 memtype = ebi1_memtype;
378 else if (pmem_memtype == PMEM_MEMTYPE_SMI)
379 memtype = MEMTYPE_SMI_KERNEL;
380 else {
381 pr_alert("Invalid memory type %x\n",
382 flags & PMEM_MEMTYPE_MASK);
383 return -EINVAL;
384 }
385
386 paddr = allocate_contiguous_memory_nomap(size, memtype, align);
387 if (!paddr && pmem_memtype == PMEM_MEMTYPE_SMI)
388 paddr = allocate_contiguous_memory_nomap(size,
389 ebi1_memtype, align);
390
391 if (!paddr)
392 return -ENOMEM;
393 return paddr;
394}
395EXPORT_SYMBOL(pmem_kalloc);
396
397int pmem_kfree(const int32_t physaddr)
398{
399 free_contiguous_memory_by_paddr(physaddr);
400
401 return 0;
402}
403EXPORT_SYMBOL(pmem_kfree);