init.c 8.2 KB

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