init.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Memory subsystem initialization for Hexagon
  4. *
  5. * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/init.h>
  8. #include <linux/mm.h>
  9. #include <linux/memblock.h>
  10. #include <asm/atomic.h>
  11. #include <linux/highmem.h>
  12. #include <asm/tlb.h>
  13. #include <asm/sections.h>
  14. #include <asm/setup.h>
  15. #include <asm/vm_mmu.h>
  16. /*
  17. * Define a startpg just past the end of the kernel image and a lastpg
  18. * that corresponds to the end of real or simulated platform memory.
  19. */
  20. #define bootmem_startpg (PFN_UP(((unsigned long) _end) - PAGE_OFFSET + PHYS_OFFSET))
  21. unsigned long bootmem_lastpg; /* Should be set by platform code */
  22. unsigned long __phys_offset; /* physical kernel offset >> 12 */
  23. /* Set as variable to limit PMD copies */
  24. int max_kernel_seg = 0x303;
  25. /* indicate pfn's of high memory */
  26. unsigned long highstart_pfn, highend_pfn;
  27. /* Default cache attribute for newly created page tables */
  28. unsigned long _dflt_cache_att = CACHEDEF;
  29. /*
  30. * The current "generation" of kernel map, which should not roll
  31. * over until Hell freezes over. Actual bound in years needs to be
  32. * calculated to confirm.
  33. */
  34. DEFINE_SPINLOCK(kmap_gen_lock);
  35. /* checkpatch says don't init this to 0. */
  36. unsigned long long kmap_generation;
  37. /*
  38. * mem_init - initializes memory
  39. *
  40. * Frees up bootmem
  41. * Fixes up more stuff for HIGHMEM
  42. * Calculates and displays memory available/used
  43. */
  44. void __init mem_init(void)
  45. {
  46. /* No idea where this is actually declared. Seems to evade LXR. */
  47. memblock_free_all();
  48. /*
  49. * To-Do: someone somewhere should wipe out the bootmem map
  50. * after we're done?
  51. */
  52. /*
  53. * This can be moved to some more virtual-memory-specific
  54. * initialization hook at some point. Set the init_mm
  55. * descriptors "context" value to point to the initial
  56. * kernel segment table's physical address.
  57. */
  58. init_mm.context.ptbase = __pa(init_mm.pgd);
  59. }
  60. void sync_icache_dcache(pte_t pte)
  61. {
  62. unsigned long addr;
  63. struct page *page;
  64. page = pte_page(pte);
  65. addr = (unsigned long) page_address(page);
  66. __vmcache_idsync(addr, PAGE_SIZE);
  67. }
  68. /*
  69. * In order to set up page allocator "nodes",
  70. * somebody has to call free_area_init() for UMA.
  71. *
  72. * In this mode, we only have one pg_data_t
  73. * structure: contig_mem_data.
  74. */
  75. static void __init paging_init(void)
  76. {
  77. unsigned long max_zone_pfn[MAX_NR_ZONES] = {0, };
  78. /*
  79. * This is not particularly well documented anywhere, but
  80. * give ZONE_NORMAL all the memory, including the big holes
  81. * left by the kernel+bootmem_map which are already left as reserved
  82. * in the bootmem_map; free_area_init should see those bits and
  83. * adjust accordingly.
  84. */
  85. max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
  86. free_area_init(max_zone_pfn); /* sets up the zonelists and mem_map */
  87. /*
  88. * Start of high memory area. Will probably need something more
  89. * fancy if we... get more fancy.
  90. */
  91. high_memory = (void *)((bootmem_lastpg + 1) << PAGE_SHIFT);
  92. }
  93. #ifndef DMA_RESERVE
  94. #define DMA_RESERVE (4)
  95. #endif
  96. #define DMA_CHUNKSIZE (1<<22)
  97. #define DMA_RESERVED_BYTES (DMA_RESERVE * DMA_CHUNKSIZE)
  98. /*
  99. * Pick out the memory size. We look for mem=size,
  100. * where size is "size[KkMm]"
  101. */
  102. static int __init early_mem(char *p)
  103. {
  104. unsigned long size;
  105. char *endp;
  106. size = memparse(p, &endp);
  107. bootmem_lastpg = PFN_DOWN(size);
  108. return 0;
  109. }
  110. early_param("mem", early_mem);
  111. size_t hexagon_coherent_pool_size = (size_t) (DMA_RESERVE << 22);
  112. void __init setup_arch_memory(void)
  113. {
  114. /* XXX Todo: this probably should be cleaned up */
  115. u32 *segtable = (u32 *) &swapper_pg_dir[0];
  116. u32 *segtable_end;
  117. /*
  118. * Set up boot memory allocator
  119. *
  120. * The Gorman book also talks about these functions.
  121. * This needs to change for highmem setups.
  122. */
  123. /* Prior to this, bootmem_lastpg is actually mem size */
  124. bootmem_lastpg += ARCH_PFN_OFFSET;
  125. /* Memory size needs to be a multiple of 16M */
  126. bootmem_lastpg = PFN_DOWN((bootmem_lastpg << PAGE_SHIFT) &
  127. ~((BIG_KERNEL_PAGE_SIZE) - 1));
  128. memblock_add(PHYS_OFFSET,
  129. (bootmem_lastpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
  130. /* Reserve kernel text/data/bss */
  131. memblock_reserve(PHYS_OFFSET,
  132. (bootmem_startpg - ARCH_PFN_OFFSET) << PAGE_SHIFT);
  133. /*
  134. * Reserve the top DMA_RESERVE bytes of RAM for DMA (uncached)
  135. * memory allocation
  136. */
  137. max_low_pfn = bootmem_lastpg - PFN_DOWN(DMA_RESERVED_BYTES);
  138. min_low_pfn = ARCH_PFN_OFFSET;
  139. memblock_reserve(PFN_PHYS(max_low_pfn), DMA_RESERVED_BYTES);
  140. printk(KERN_INFO "bootmem_startpg: 0x%08lx\n", bootmem_startpg);
  141. printk(KERN_INFO "bootmem_lastpg: 0x%08lx\n", bootmem_lastpg);
  142. printk(KERN_INFO "min_low_pfn: 0x%08lx\n", min_low_pfn);
  143. printk(KERN_INFO "max_low_pfn: 0x%08lx\n", max_low_pfn);
  144. /*
  145. * The default VM page tables (will be) populated with
  146. * VA=PA+PAGE_OFFSET mapping. We go in and invalidate entries
  147. * higher than what we have memory for.
  148. */
  149. /* this is pointer arithmetic; each entry covers 4MB */
  150. segtable = segtable + (PAGE_OFFSET >> 22);
  151. /* this actually only goes to the end of the first gig */
  152. segtable_end = segtable + (1<<(30-22));
  153. /*
  154. * Move forward to the start of empty pages; take into account
  155. * phys_offset shift.
  156. */
  157. segtable += (bootmem_lastpg-ARCH_PFN_OFFSET)>>(22-PAGE_SHIFT);
  158. {
  159. int i;
  160. for (i = 1 ; i <= DMA_RESERVE ; i++)
  161. segtable[-i] = ((segtable[-i] & __HVM_PTE_PGMASK_4MB)
  162. | __HVM_PTE_R | __HVM_PTE_W | __HVM_PTE_X
  163. | __HEXAGON_C_UNC << 6
  164. | __HVM_PDE_S_4MB);
  165. }
  166. printk(KERN_INFO "clearing segtable from %p to %p\n", segtable,
  167. segtable_end);
  168. while (segtable < (segtable_end-8))
  169. *(segtable++) = __HVM_PDE_S_INVALID;
  170. /* stop the pointer at the device I/O 4MB page */
  171. printk(KERN_INFO "segtable = %p (should be equal to _K_io_map)\n",
  172. segtable);
  173. #if 0
  174. /* Other half of the early device table from vm_init_segtable. */
  175. printk(KERN_INFO "&_K_init_devicetable = 0x%08x\n",
  176. (unsigned long) _K_init_devicetable-PAGE_OFFSET);
  177. *segtable = ((u32) (unsigned long) _K_init_devicetable-PAGE_OFFSET) |
  178. __HVM_PDE_S_4KB;
  179. printk(KERN_INFO "*segtable = 0x%08x\n", *segtable);
  180. #endif
  181. /*
  182. * The bootmem allocator seemingly just lives to feed memory
  183. * to the paging system
  184. */
  185. printk(KERN_INFO "PAGE_SIZE=%lu\n", PAGE_SIZE);
  186. paging_init(); /* See Gorman Book, 2.3 */
  187. /*
  188. * At this point, the page allocator is kind of initialized, but
  189. * apparently no pages are available (just like with the bootmem
  190. * allocator), and need to be freed themselves via mem_init(),
  191. * which is called by start_kernel() later on in the process
  192. */
  193. }
  194. static const pgprot_t protection_map[16] = {
  195. [VM_NONE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  196. CACHEDEF),
  197. [VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  198. _PAGE_READ | CACHEDEF),
  199. [VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  200. CACHEDEF),
  201. [VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  202. _PAGE_READ | CACHEDEF),
  203. [VM_EXEC] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  204. _PAGE_EXECUTE | CACHEDEF),
  205. [VM_EXEC | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  206. _PAGE_EXECUTE | _PAGE_READ |
  207. CACHEDEF),
  208. [VM_EXEC | VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  209. _PAGE_EXECUTE | CACHEDEF),
  210. [VM_EXEC | VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  211. _PAGE_EXECUTE | _PAGE_READ |
  212. CACHEDEF),
  213. [VM_SHARED] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  214. CACHEDEF),
  215. [VM_SHARED | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  216. _PAGE_READ | CACHEDEF),
  217. [VM_SHARED | VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  218. _PAGE_WRITE | CACHEDEF),
  219. [VM_SHARED | VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  220. _PAGE_READ | _PAGE_WRITE |
  221. CACHEDEF),
  222. [VM_SHARED | VM_EXEC] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  223. _PAGE_EXECUTE | CACHEDEF),
  224. [VM_SHARED | VM_EXEC | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  225. _PAGE_EXECUTE | _PAGE_READ |
  226. CACHEDEF),
  227. [VM_SHARED | VM_EXEC | VM_WRITE] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  228. _PAGE_EXECUTE | _PAGE_WRITE |
  229. CACHEDEF),
  230. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = __pgprot(_PAGE_PRESENT | _PAGE_USER |
  231. _PAGE_READ | _PAGE_EXECUTE |
  232. _PAGE_WRITE | CACHEDEF)
  233. };
  234. DECLARE_VM_GET_PAGE_PROT