Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* 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 | |
| 41 | int 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 | |
| 52 | void *strongly_ordered_page; |
| 53 | char 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 | */ |
| 65 | void 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 | } |
| 86 | EXPORT_SYMBOL(map_page_strongly_ordered); |
| 87 | |
| 88 | void 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 | } |
| 104 | EXPORT_SYMBOL(write_to_strongly_ordered_memory); |
| 105 | |
| 106 | void 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 | */ |
| 121 | void 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 | |
| 137 | void 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 | |
| 153 | void 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 | |
| 169 | void *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 | |
Larry Bassel | a7eadea | 2011-07-14 10:46:00 -0700 | [diff] [blame^] | 197 | int (*change_memory_power)(unsigned long, unsigned long, int); |
| 198 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 199 | int platform_physical_remove_pages(unsigned long start_pfn, |
| 200 | unsigned long nr_pages) |
| 201 | { |
Larry Bassel | a7eadea | 2011-07-14 10:46:00 -0700 | [diff] [blame^] | 202 | if (!change_memory_power) |
| 203 | return 0; |
| 204 | return change_memory_power(start_pfn, nr_pages, MEMORY_DEEP_POWERDOWN); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | int platform_physical_active_pages(unsigned long start_pfn, |
| 208 | unsigned long nr_pages) |
| 209 | { |
Larry Bassel | a7eadea | 2011-07-14 10:46:00 -0700 | [diff] [blame^] | 210 | if (!change_memory_power) |
| 211 | return 0; |
| 212 | return change_memory_power(start_pfn, nr_pages, MEMORY_ACTIVE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int platform_physical_low_power_pages(unsigned long start_pfn, |
| 216 | unsigned long nr_pages) |
| 217 | { |
Larry Bassel | a7eadea | 2011-07-14 10:46:00 -0700 | [diff] [blame^] | 218 | if (!change_memory_power) |
| 219 | return 0; |
| 220 | return change_memory_power(start_pfn, nr_pages, MEMORY_SELF_REFRESH); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | char *memtype_name[] = { |
| 224 | "SMI_KERNEL", |
| 225 | "SMI", |
| 226 | "EBI0", |
| 227 | "EBI1" |
| 228 | }; |
| 229 | |
| 230 | struct reserve_info *reserve_info; |
| 231 | |
| 232 | static void __init calculate_reserve_limits(void) |
| 233 | { |
| 234 | int i; |
| 235 | struct membank *mb; |
| 236 | int memtype; |
| 237 | struct memtype_reserve *mt; |
| 238 | |
| 239 | for (i = 0, mb = &meminfo.bank[0]; i < meminfo.nr_banks; i++, mb++) { |
| 240 | memtype = reserve_info->paddr_to_memtype(mb->start); |
| 241 | if (memtype == MEMTYPE_NONE) { |
| 242 | pr_warning("unknown memory type for bank at %lx\n", |
| 243 | (long unsigned int)mb->start); |
| 244 | continue; |
| 245 | } |
| 246 | mt = &reserve_info->memtype_reserve_table[memtype]; |
| 247 | mt->limit = max(mt->limit, mb->size); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | static void __init adjust_reserve_sizes(void) |
| 252 | { |
| 253 | int i; |
| 254 | struct memtype_reserve *mt; |
| 255 | |
| 256 | mt = &reserve_info->memtype_reserve_table[0]; |
| 257 | for (i = 0; i < MEMTYPE_MAX; i++, mt++) { |
| 258 | if (mt->flags & MEMTYPE_FLAGS_1M_ALIGN) |
| 259 | mt->size = (mt->size + SECTION_SIZE - 1) & SECTION_MASK; |
| 260 | if (mt->size > mt->limit) { |
| 261 | pr_warning("%lx size for %s too large, setting to %lx\n", |
| 262 | mt->size, memtype_name[i], mt->limit); |
| 263 | mt->size = mt->limit; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static void __init reserve_memory_for_mempools(void) |
| 269 | { |
| 270 | int i, memtype, membank_type; |
| 271 | struct memtype_reserve *mt; |
| 272 | struct membank *mb; |
| 273 | int ret; |
| 274 | |
| 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 |
| 288 | * highmem. |
| 289 | */ |
| 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; |
| 296 | if (mb->size >= mt->size) { |
| 297 | mt->start = mb->start + mb->size - mt->size; |
| 298 | ret = memblock_remove(mt->start, mt->size); |
| 299 | BUG_ON(ret); |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void __init initialize_mempools(void) |
| 307 | { |
| 308 | struct mem_pool *mpool; |
| 309 | int memtype; |
| 310 | struct memtype_reserve *mt; |
| 311 | |
| 312 | mt = &reserve_info->memtype_reserve_table[0]; |
| 313 | for (memtype = 0; memtype < MEMTYPE_MAX; memtype++, mt++) { |
| 314 | if (!mt->size) |
| 315 | continue; |
| 316 | mpool = initialize_memory_pool(mt->start, mt->size, memtype); |
| 317 | if (!mpool) |
| 318 | pr_warning("failed to create %s mempool\n", |
| 319 | memtype_name[memtype]); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void __init msm_reserve(void) |
| 324 | { |
| 325 | memory_pool_init(); |
| 326 | reserve_info->calculate_reserve_sizes(); |
| 327 | calculate_reserve_limits(); |
| 328 | adjust_reserve_sizes(); |
| 329 | reserve_memory_for_mempools(); |
| 330 | initialize_mempools(); |
| 331 | } |
| 332 | |
| 333 | static int get_ebi_memtype(void) |
| 334 | { |
| 335 | /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */ |
| 336 | if (cpu_is_msm7x30() || cpu_is_msm8x55()) |
| 337 | return MEMTYPE_EBI0; |
| 338 | return MEMTYPE_EBI1; |
| 339 | } |
| 340 | |
| 341 | void *allocate_contiguous_ebi(unsigned long size, |
| 342 | unsigned long align, int cached) |
| 343 | { |
| 344 | return allocate_contiguous_memory(size, get_ebi_memtype(), |
| 345 | align, cached); |
| 346 | } |
| 347 | EXPORT_SYMBOL(allocate_contiguous_ebi); |
| 348 | |
| 349 | unsigned long allocate_contiguous_ebi_nomap(unsigned long size, |
| 350 | unsigned long align) |
| 351 | { |
| 352 | return allocate_contiguous_memory_nomap(size, get_ebi_memtype(), align); |
| 353 | } |
| 354 | EXPORT_SYMBOL(allocate_contiguous_ebi_nomap); |
| 355 | |
| 356 | /* emulation of the deprecated pmem_kalloc and pmem_kfree */ |
| 357 | int32_t pmem_kalloc(const size_t size, const uint32_t flags) |
| 358 | { |
| 359 | int pmem_memtype; |
| 360 | int memtype = MEMTYPE_NONE; |
| 361 | int ebi1_memtype = MEMTYPE_EBI1; |
| 362 | unsigned int align; |
| 363 | int32_t paddr; |
| 364 | |
| 365 | switch (flags & PMEM_ALIGNMENT_MASK) { |
| 366 | case PMEM_ALIGNMENT_4K: |
| 367 | align = SZ_4K; |
| 368 | break; |
| 369 | case PMEM_ALIGNMENT_1M: |
| 370 | align = SZ_1M; |
| 371 | break; |
| 372 | default: |
| 373 | pr_alert("Invalid alignment %x\n", |
| 374 | (flags & PMEM_ALIGNMENT_MASK)); |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | |
| 378 | /* on 7x30 and 8x55 "EBI1 kernel PMEM" is really on EBI0 */ |
| 379 | if (cpu_is_msm7x30() || cpu_is_msm8x55()) |
| 380 | ebi1_memtype = MEMTYPE_EBI0; |
| 381 | |
| 382 | pmem_memtype = flags & PMEM_MEMTYPE_MASK; |
| 383 | if (pmem_memtype == PMEM_MEMTYPE_EBI1) |
| 384 | memtype = ebi1_memtype; |
| 385 | else if (pmem_memtype == PMEM_MEMTYPE_SMI) |
| 386 | memtype = MEMTYPE_SMI_KERNEL; |
| 387 | else { |
| 388 | pr_alert("Invalid memory type %x\n", |
| 389 | flags & PMEM_MEMTYPE_MASK); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | |
| 393 | paddr = allocate_contiguous_memory_nomap(size, memtype, align); |
| 394 | if (!paddr && pmem_memtype == PMEM_MEMTYPE_SMI) |
| 395 | paddr = allocate_contiguous_memory_nomap(size, |
| 396 | ebi1_memtype, align); |
| 397 | |
| 398 | if (!paddr) |
| 399 | return -ENOMEM; |
| 400 | return paddr; |
| 401 | } |
| 402 | EXPORT_SYMBOL(pmem_kalloc); |
| 403 | |
| 404 | int pmem_kfree(const int32_t physaddr) |
| 405 | { |
| 406 | free_contiguous_memory_by_paddr(physaddr); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | EXPORT_SYMBOL(pmem_kfree); |