init.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008,2009,2010 Imagination Technologies
  3. *
  4. */
  5. #include <linux/export.h>
  6. #include <linux/mm.h>
  7. #include <linux/swap.h>
  8. #include <linux/init.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/percpu.h>
  12. #include <linux/memblock.h>
  13. #include <linux/initrd.h>
  14. #include <asm/setup.h>
  15. #include <asm/page.h>
  16. #include <asm/pgalloc.h>
  17. #include <asm/mmu.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/sections.h>
  20. #include <asm/tlb.h>
  21. #include <asm/user_gateway.h>
  22. #include <asm/mmzone.h>
  23. #include <asm/fixmap.h>
  24. unsigned long pfn_base;
  25. EXPORT_SYMBOL(pfn_base);
  26. pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_data;
  27. unsigned long empty_zero_page;
  28. EXPORT_SYMBOL(empty_zero_page);
  29. extern char __user_gateway_start;
  30. extern char __user_gateway_end;
  31. void *gateway_page;
  32. /*
  33. * Insert the gateway page into a set of page tables, creating the
  34. * page tables if necessary.
  35. */
  36. static void insert_gateway_page(pgd_t *pgd, unsigned long address)
  37. {
  38. pud_t *pud;
  39. pmd_t *pmd;
  40. pte_t *pte;
  41. BUG_ON(!pgd_present(*pgd));
  42. pud = pud_offset(pgd, address);
  43. BUG_ON(!pud_present(*pud));
  44. pmd = pmd_offset(pud, address);
  45. if (!pmd_present(*pmd)) {
  46. pte = alloc_bootmem_pages(PAGE_SIZE);
  47. set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte)));
  48. }
  49. pte = pte_offset_kernel(pmd, address);
  50. set_pte(pte, pfn_pte(__pa(gateway_page) >> PAGE_SHIFT, PAGE_READONLY));
  51. }
  52. /* Alloc and map a page in a known location accessible to userspace. */
  53. static void __init user_gateway_init(void)
  54. {
  55. unsigned long address = USER_GATEWAY_PAGE;
  56. int offset = pgd_index(address);
  57. pgd_t *pgd;
  58. gateway_page = alloc_bootmem_pages(PAGE_SIZE);
  59. pgd = swapper_pg_dir + offset;
  60. insert_gateway_page(pgd, address);
  61. #ifdef CONFIG_METAG_META12
  62. /*
  63. * Insert the gateway page into our current page tables even
  64. * though we've already inserted it into our reference page
  65. * table (swapper_pg_dir). This is because with a META1 mmu we
  66. * copy just the user address range and not the gateway page
  67. * entry on context switch, see switch_mmu().
  68. */
  69. pgd = (pgd_t *)mmu_get_base() + offset;
  70. insert_gateway_page(pgd, address);
  71. #endif /* CONFIG_METAG_META12 */
  72. BUG_ON((&__user_gateway_end - &__user_gateway_start) > PAGE_SIZE);
  73. gateway_page += (address & ~PAGE_MASK);
  74. memcpy(gateway_page, &__user_gateway_start,
  75. &__user_gateway_end - &__user_gateway_start);
  76. /*
  77. * We don't need to flush the TLB here, there should be no mapping
  78. * present at boot for this address and only valid mappings are in
  79. * the TLB (apart from on Meta 1.x, but those cached invalid
  80. * mappings should be impossible to hit here).
  81. *
  82. * We don't flush the code cache here even though we have written
  83. * code through the data cache and they may not be coherent. At
  84. * this point we assume there is no stale data in the code cache
  85. * for this address so there is no need to flush.
  86. */
  87. }
  88. static void __init allocate_pgdat(unsigned int nid)
  89. {
  90. unsigned long start_pfn, end_pfn;
  91. #ifdef CONFIG_NEED_MULTIPLE_NODES
  92. unsigned long phys;
  93. #endif
  94. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  95. #ifdef CONFIG_NEED_MULTIPLE_NODES
  96. phys = __memblock_alloc_base(sizeof(struct pglist_data),
  97. SMP_CACHE_BYTES, end_pfn << PAGE_SHIFT);
  98. /* Retry with all of system memory */
  99. if (!phys)
  100. phys = __memblock_alloc_base(sizeof(struct pglist_data),
  101. SMP_CACHE_BYTES,
  102. memblock_end_of_DRAM());
  103. if (!phys)
  104. panic("Can't allocate pgdat for node %d\n", nid);
  105. NODE_DATA(nid) = __va(phys);
  106. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  107. NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
  108. #endif
  109. NODE_DATA(nid)->node_start_pfn = start_pfn;
  110. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  111. }
  112. static void __init bootmem_init_one_node(unsigned int nid)
  113. {
  114. unsigned long total_pages, paddr;
  115. unsigned long end_pfn;
  116. struct pglist_data *p;
  117. p = NODE_DATA(nid);
  118. /* Nothing to do.. */
  119. if (!p->node_spanned_pages)
  120. return;
  121. end_pfn = pgdat_end_pfn(p);
  122. #ifdef CONFIG_HIGHMEM
  123. if (end_pfn > max_low_pfn)
  124. end_pfn = max_low_pfn;
  125. #endif
  126. total_pages = bootmem_bootmap_pages(end_pfn - p->node_start_pfn);
  127. paddr = memblock_alloc(total_pages << PAGE_SHIFT, PAGE_SIZE);
  128. if (!paddr)
  129. panic("Can't allocate bootmap for nid[%d]\n", nid);
  130. init_bootmem_node(p, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);
  131. free_bootmem_with_active_regions(nid, end_pfn);
  132. /*
  133. * XXX Handle initial reservations for the system memory node
  134. * only for the moment, we'll refactor this later for handling
  135. * reservations in other nodes.
  136. */
  137. if (nid == 0) {
  138. struct memblock_region *reg;
  139. /* Reserve the sections we're already using. */
  140. for_each_memblock(reserved, reg) {
  141. unsigned long size = reg->size;
  142. #ifdef CONFIG_HIGHMEM
  143. /* ...but not highmem */
  144. if (PFN_DOWN(reg->base) >= highstart_pfn)
  145. continue;
  146. if (PFN_UP(reg->base + size) > highstart_pfn)
  147. size = (highstart_pfn - PFN_DOWN(reg->base))
  148. << PAGE_SHIFT;
  149. #endif
  150. reserve_bootmem(reg->base, size, BOOTMEM_DEFAULT);
  151. }
  152. }
  153. sparse_memory_present_with_active_regions(nid);
  154. }
  155. static void __init do_init_bootmem(void)
  156. {
  157. struct memblock_region *reg;
  158. int i;
  159. /* Add active regions with valid PFNs. */
  160. for_each_memblock(memory, reg) {
  161. unsigned long start_pfn, end_pfn;
  162. start_pfn = memblock_region_memory_base_pfn(reg);
  163. end_pfn = memblock_region_memory_end_pfn(reg);
  164. memblock_set_node(PFN_PHYS(start_pfn),
  165. PFN_PHYS(end_pfn - start_pfn),
  166. &memblock.memory, 0);
  167. }
  168. /* All of system RAM sits in node 0 for the non-NUMA case */
  169. allocate_pgdat(0);
  170. node_set_online(0);
  171. soc_mem_setup();
  172. for_each_online_node(i)
  173. bootmem_init_one_node(i);
  174. sparse_init();
  175. }
  176. extern char _heap_start[];
  177. static void __init init_and_reserve_mem(void)
  178. {
  179. unsigned long start_pfn, heap_start;
  180. u64 base = min_low_pfn << PAGE_SHIFT;
  181. u64 size = (max_low_pfn << PAGE_SHIFT) - base;
  182. heap_start = (unsigned long) &_heap_start;
  183. memblock_add(base, size);
  184. /*
  185. * Partially used pages are not usable - thus
  186. * we are rounding upwards:
  187. */
  188. start_pfn = PFN_UP(__pa(heap_start));
  189. /*
  190. * Reserve the kernel text.
  191. */
  192. memblock_reserve(base, (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) - base);
  193. #ifdef CONFIG_HIGHMEM
  194. /*
  195. * Add & reserve highmem, so page structures are initialised.
  196. */
  197. base = highstart_pfn << PAGE_SHIFT;
  198. size = (highend_pfn << PAGE_SHIFT) - base;
  199. if (size) {
  200. memblock_add(base, size);
  201. memblock_reserve(base, size);
  202. }
  203. #endif
  204. }
  205. #ifdef CONFIG_HIGHMEM
  206. /*
  207. * Ensure we have allocated page tables in swapper_pg_dir for the
  208. * fixed mappings range from 'start' to 'end'.
  209. */
  210. static void __init allocate_pgtables(unsigned long start, unsigned long end)
  211. {
  212. pgd_t *pgd;
  213. pmd_t *pmd;
  214. pte_t *pte;
  215. int i, j;
  216. unsigned long vaddr;
  217. vaddr = start;
  218. i = pgd_index(vaddr);
  219. j = pmd_index(vaddr);
  220. pgd = swapper_pg_dir + i;
  221. for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
  222. pmd = (pmd_t *)pgd;
  223. for (; (j < PTRS_PER_PMD) && (vaddr != end); pmd++, j++) {
  224. vaddr += PMD_SIZE;
  225. if (!pmd_none(*pmd))
  226. continue;
  227. pte = (pte_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  228. pmd_populate_kernel(&init_mm, pmd, pte);
  229. }
  230. j = 0;
  231. }
  232. }
  233. static void __init fixedrange_init(void)
  234. {
  235. unsigned long vaddr, end;
  236. pgd_t *pgd;
  237. pud_t *pud;
  238. pmd_t *pmd;
  239. pte_t *pte;
  240. /*
  241. * Fixed mappings:
  242. */
  243. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  244. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  245. allocate_pgtables(vaddr, end);
  246. /*
  247. * Permanent kmaps:
  248. */
  249. vaddr = PKMAP_BASE;
  250. allocate_pgtables(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP);
  251. pgd = swapper_pg_dir + pgd_index(vaddr);
  252. pud = pud_offset(pgd, vaddr);
  253. pmd = pmd_offset(pud, vaddr);
  254. pte = pte_offset_kernel(pmd, vaddr);
  255. pkmap_page_table = pte;
  256. }
  257. #endif /* CONFIG_HIGHMEM */
  258. /*
  259. * paging_init() continues the virtual memory environment setup which
  260. * was begun by the code in arch/metag/kernel/setup.c.
  261. */
  262. void __init paging_init(unsigned long mem_end)
  263. {
  264. unsigned long max_zone_pfns[MAX_NR_ZONES];
  265. int nid;
  266. init_and_reserve_mem();
  267. memblock_allow_resize();
  268. memblock_dump_all();
  269. nodes_clear(node_online_map);
  270. init_new_context(&init_task, &init_mm);
  271. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  272. do_init_bootmem();
  273. mmu_init(mem_end);
  274. #ifdef CONFIG_HIGHMEM
  275. fixedrange_init();
  276. kmap_init();
  277. #endif
  278. /* Initialize the zero page to a bootmem page, already zeroed. */
  279. empty_zero_page = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
  280. user_gateway_init();
  281. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  282. for_each_online_node(nid) {
  283. pg_data_t *pgdat = NODE_DATA(nid);
  284. unsigned long low, start_pfn;
  285. start_pfn = pgdat->bdata->node_min_pfn;
  286. low = pgdat->bdata->node_low_pfn;
  287. if (max_zone_pfns[ZONE_NORMAL] < low)
  288. max_zone_pfns[ZONE_NORMAL] = low;
  289. #ifdef CONFIG_HIGHMEM
  290. max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
  291. #endif
  292. pr_info("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
  293. nid, start_pfn, low);
  294. }
  295. free_area_init_nodes(max_zone_pfns);
  296. }
  297. void __init mem_init(void)
  298. {
  299. #ifdef CONFIG_HIGHMEM
  300. unsigned long tmp;
  301. /*
  302. * Explicitly reset zone->managed_pages because highmem pages are
  303. * freed before calling free_all_bootmem();
  304. */
  305. reset_all_zones_managed_pages();
  306. for (tmp = highstart_pfn; tmp < highend_pfn; tmp++)
  307. free_highmem_page(pfn_to_page(tmp));
  308. #endif /* CONFIG_HIGHMEM */
  309. free_all_bootmem();
  310. mem_init_print_info(NULL);
  311. show_mem(0);
  312. }
  313. void free_initmem(void)
  314. {
  315. free_initmem_default(POISON_FREE_INITMEM);
  316. }
  317. #ifdef CONFIG_BLK_DEV_INITRD
  318. void free_initrd_mem(unsigned long start, unsigned long end)
  319. {
  320. free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
  321. "initrd");
  322. }
  323. #endif