init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  6. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  7. * Copyright (C) 1996 Paul Mackerras
  8. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  9. *
  10. * Derived from "arch/i386/mm/init.c"
  11. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  12. *
  13. * Dave Engebretsen <engebret@us.ibm.com>
  14. * Rework for PPC64 port.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. */
  22. #include <linux/config.h>
  23. #include <linux/signal.h>
  24. #include <linux/sched.h>
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include <linux/mman.h>
  30. #include <linux/mm.h>
  31. #include <linux/swap.h>
  32. #include <linux/stddef.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/init.h>
  35. #include <linux/delay.h>
  36. #include <linux/bootmem.h>
  37. #include <linux/highmem.h>
  38. #include <linux/idr.h>
  39. #include <linux/nodemask.h>
  40. #include <linux/module.h>
  41. #include <asm/pgalloc.h>
  42. #include <asm/page.h>
  43. #include <asm/prom.h>
  44. #include <asm/lmb.h>
  45. #include <asm/rtas.h>
  46. #include <asm/io.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/pgtable.h>
  49. #include <asm/mmu.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/smp.h>
  52. #include <asm/machdep.h>
  53. #include <asm/tlb.h>
  54. #include <asm/eeh.h>
  55. #include <asm/processor.h>
  56. #include <asm/mmzone.h>
  57. #include <asm/cputable.h>
  58. #include <asm/ppcdebug.h>
  59. #include <asm/sections.h>
  60. #include <asm/system.h>
  61. #include <asm/iommu.h>
  62. #include <asm/abs_addr.h>
  63. #include <asm/vdso.h>
  64. #include <asm/imalloc.h>
  65. #if PGTABLE_RANGE > USER_VSID_RANGE
  66. #warning Limited user VSID range means pagetable space is wasted
  67. #endif
  68. #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE)
  69. #warning TASK_SIZE is smaller than it needs to be.
  70. #endif
  71. int mem_init_done;
  72. unsigned long ioremap_bot = IMALLOC_BASE;
  73. static unsigned long phbs_io_bot = PHBS_IO_BASE;
  74. extern pgd_t swapper_pg_dir[];
  75. extern struct task_struct *current_set[NR_CPUS];
  76. unsigned long klimit = (unsigned long)_end;
  77. unsigned long _SDR1=0;
  78. unsigned long _ASR=0;
  79. /* max amount of RAM to use */
  80. unsigned long __max_memory;
  81. /* info on what we think the IO hole is */
  82. unsigned long io_hole_start;
  83. unsigned long io_hole_size;
  84. void show_mem(void)
  85. {
  86. unsigned long total = 0, reserved = 0;
  87. unsigned long shared = 0, cached = 0;
  88. struct page *page;
  89. pg_data_t *pgdat;
  90. unsigned long i;
  91. printk("Mem-info:\n");
  92. show_free_areas();
  93. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  94. for_each_pgdat(pgdat) {
  95. for (i = 0; i < pgdat->node_spanned_pages; i++) {
  96. page = pgdat_page_nr(pgdat, i);
  97. total++;
  98. if (PageReserved(page))
  99. reserved++;
  100. else if (PageSwapCache(page))
  101. cached++;
  102. else if (page_count(page))
  103. shared += page_count(page) - 1;
  104. }
  105. }
  106. printk("%ld pages of RAM\n", total);
  107. printk("%ld reserved pages\n", reserved);
  108. printk("%ld pages shared\n", shared);
  109. printk("%ld pages swap cached\n", cached);
  110. }
  111. #ifdef CONFIG_PPC_ISERIES
  112. void __iomem *ioremap(unsigned long addr, unsigned long size)
  113. {
  114. return (void __iomem *)addr;
  115. }
  116. extern void __iomem *__ioremap(unsigned long addr, unsigned long size,
  117. unsigned long flags)
  118. {
  119. return (void __iomem *)addr;
  120. }
  121. void iounmap(volatile void __iomem *addr)
  122. {
  123. return;
  124. }
  125. #else
  126. /*
  127. * map_io_page currently only called by __ioremap
  128. * map_io_page adds an entry to the ioremap page table
  129. * and adds an entry to the HPT, possibly bolting it
  130. */
  131. static int map_io_page(unsigned long ea, unsigned long pa, int flags)
  132. {
  133. pgd_t *pgdp;
  134. pud_t *pudp;
  135. pmd_t *pmdp;
  136. pte_t *ptep;
  137. unsigned long vsid;
  138. if (mem_init_done) {
  139. pgdp = pgd_offset_k(ea);
  140. pudp = pud_alloc(&init_mm, pgdp, ea);
  141. if (!pudp)
  142. return -ENOMEM;
  143. pmdp = pmd_alloc(&init_mm, pudp, ea);
  144. if (!pmdp)
  145. return -ENOMEM;
  146. ptep = pte_alloc_kernel(pmdp, ea);
  147. if (!ptep)
  148. return -ENOMEM;
  149. set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
  150. __pgprot(flags)));
  151. } else {
  152. unsigned long va, vpn, hash, hpteg;
  153. /*
  154. * If the mm subsystem is not fully up, we cannot create a
  155. * linux page table entry for this mapping. Simply bolt an
  156. * entry in the hardware page table.
  157. */
  158. vsid = get_kernel_vsid(ea);
  159. va = (vsid << 28) | (ea & 0xFFFFFFF);
  160. vpn = va >> PAGE_SHIFT;
  161. hash = hpt_hash(vpn, 0);
  162. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  163. /* Panic if a pte grpup is full */
  164. if (ppc_md.hpte_insert(hpteg, va, pa >> PAGE_SHIFT,
  165. HPTE_V_BOLTED,
  166. _PAGE_NO_CACHE|_PAGE_GUARDED|PP_RWXX)
  167. == -1) {
  168. panic("map_io_page: could not insert mapping");
  169. }
  170. }
  171. return 0;
  172. }
  173. static void __iomem * __ioremap_com(unsigned long addr, unsigned long pa,
  174. unsigned long ea, unsigned long size,
  175. unsigned long flags)
  176. {
  177. unsigned long i;
  178. if ((flags & _PAGE_PRESENT) == 0)
  179. flags |= pgprot_val(PAGE_KERNEL);
  180. for (i = 0; i < size; i += PAGE_SIZE)
  181. if (map_io_page(ea+i, pa+i, flags))
  182. return NULL;
  183. return (void __iomem *) (ea + (addr & ~PAGE_MASK));
  184. }
  185. void __iomem *
  186. ioremap(unsigned long addr, unsigned long size)
  187. {
  188. return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  189. }
  190. void __iomem * __ioremap(unsigned long addr, unsigned long size,
  191. unsigned long flags)
  192. {
  193. unsigned long pa, ea;
  194. void __iomem *ret;
  195. /*
  196. * Choose an address to map it to.
  197. * Once the imalloc system is running, we use it.
  198. * Before that, we map using addresses going
  199. * up from ioremap_bot. imalloc will use
  200. * the addresses from ioremap_bot through
  201. * IMALLOC_END
  202. *
  203. */
  204. pa = addr & PAGE_MASK;
  205. size = PAGE_ALIGN(addr + size) - pa;
  206. if (size == 0)
  207. return NULL;
  208. if (mem_init_done) {
  209. struct vm_struct *area;
  210. area = im_get_free_area(size);
  211. if (area == NULL)
  212. return NULL;
  213. ea = (unsigned long)(area->addr);
  214. ret = __ioremap_com(addr, pa, ea, size, flags);
  215. if (!ret)
  216. im_free(area->addr);
  217. } else {
  218. ea = ioremap_bot;
  219. ret = __ioremap_com(addr, pa, ea, size, flags);
  220. if (ret)
  221. ioremap_bot += size;
  222. }
  223. return ret;
  224. }
  225. #define IS_PAGE_ALIGNED(_val) ((_val) == ((_val) & PAGE_MASK))
  226. int __ioremap_explicit(unsigned long pa, unsigned long ea,
  227. unsigned long size, unsigned long flags)
  228. {
  229. struct vm_struct *area;
  230. void __iomem *ret;
  231. /* For now, require page-aligned values for pa, ea, and size */
  232. if (!IS_PAGE_ALIGNED(pa) || !IS_PAGE_ALIGNED(ea) ||
  233. !IS_PAGE_ALIGNED(size)) {
  234. printk(KERN_ERR "unaligned value in %s\n", __FUNCTION__);
  235. return 1;
  236. }
  237. if (!mem_init_done) {
  238. /* Two things to consider in this case:
  239. * 1) No records will be kept (imalloc, etc) that the region
  240. * has been remapped
  241. * 2) It won't be easy to iounmap() the region later (because
  242. * of 1)
  243. */
  244. ;
  245. } else {
  246. area = im_get_area(ea, size,
  247. IM_REGION_UNUSED|IM_REGION_SUBSET|IM_REGION_EXISTS);
  248. if (area == NULL) {
  249. /* Expected when PHB-dlpar is in play */
  250. return 1;
  251. }
  252. if (ea != (unsigned long) area->addr) {
  253. printk(KERN_ERR "unexpected addr return from "
  254. "im_get_area\n");
  255. return 1;
  256. }
  257. }
  258. ret = __ioremap_com(pa, pa, ea, size, flags);
  259. if (ret == NULL) {
  260. printk(KERN_ERR "ioremap_explicit() allocation failure !\n");
  261. return 1;
  262. }
  263. if (ret != (void *) ea) {
  264. printk(KERN_ERR "__ioremap_com() returned unexpected addr\n");
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * Unmap an IO region and remove it from imalloc'd list.
  271. * Access to IO memory should be serialized by driver.
  272. * This code is modeled after vmalloc code - unmap_vm_area()
  273. *
  274. * XXX what about calls before mem_init_done (ie python_countermeasures())
  275. */
  276. void iounmap(volatile void __iomem *token)
  277. {
  278. void *addr;
  279. if (!mem_init_done)
  280. return;
  281. addr = (void *) ((unsigned long __force) token & PAGE_MASK);
  282. im_free(addr);
  283. }
  284. static int iounmap_subset_regions(unsigned long addr, unsigned long size)
  285. {
  286. struct vm_struct *area;
  287. /* Check whether subsets of this region exist */
  288. area = im_get_area(addr, size, IM_REGION_SUPERSET);
  289. if (area == NULL)
  290. return 1;
  291. while (area) {
  292. iounmap((void __iomem *) area->addr);
  293. area = im_get_area(addr, size,
  294. IM_REGION_SUPERSET);
  295. }
  296. return 0;
  297. }
  298. int iounmap_explicit(volatile void __iomem *start, unsigned long size)
  299. {
  300. struct vm_struct *area;
  301. unsigned long addr;
  302. int rc;
  303. addr = (unsigned long __force) start & PAGE_MASK;
  304. /* Verify that the region either exists or is a subset of an existing
  305. * region. In the latter case, split the parent region to create
  306. * the exact region
  307. */
  308. area = im_get_area(addr, size,
  309. IM_REGION_EXISTS | IM_REGION_SUBSET);
  310. if (area == NULL) {
  311. /* Determine whether subset regions exist. If so, unmap */
  312. rc = iounmap_subset_regions(addr, size);
  313. if (rc) {
  314. printk(KERN_ERR
  315. "%s() cannot unmap nonexistent range 0x%lx\n",
  316. __FUNCTION__, addr);
  317. return 1;
  318. }
  319. } else {
  320. iounmap((void __iomem *) area->addr);
  321. }
  322. /*
  323. * FIXME! This can't be right:
  324. iounmap(area->addr);
  325. * Maybe it should be "iounmap(area);"
  326. */
  327. return 0;
  328. }
  329. #endif
  330. EXPORT_SYMBOL(ioremap);
  331. EXPORT_SYMBOL(__ioremap);
  332. EXPORT_SYMBOL(iounmap);
  333. void free_initmem(void)
  334. {
  335. unsigned long addr;
  336. addr = (unsigned long)__init_begin;
  337. for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) {
  338. memset((void *)addr, 0xcc, PAGE_SIZE);
  339. ClearPageReserved(virt_to_page(addr));
  340. set_page_count(virt_to_page(addr), 1);
  341. free_page(addr);
  342. totalram_pages++;
  343. }
  344. printk ("Freeing unused kernel memory: %luk freed\n",
  345. ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10);
  346. }
  347. #ifdef CONFIG_BLK_DEV_INITRD
  348. void free_initrd_mem(unsigned long start, unsigned long end)
  349. {
  350. if (start < end)
  351. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  352. for (; start < end; start += PAGE_SIZE) {
  353. ClearPageReserved(virt_to_page(start));
  354. set_page_count(virt_to_page(start), 1);
  355. free_page(start);
  356. totalram_pages++;
  357. }
  358. }
  359. #endif
  360. static DEFINE_SPINLOCK(mmu_context_lock);
  361. static DEFINE_IDR(mmu_context_idr);
  362. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  363. {
  364. int index;
  365. int err;
  366. again:
  367. if (!idr_pre_get(&mmu_context_idr, GFP_KERNEL))
  368. return -ENOMEM;
  369. spin_lock(&mmu_context_lock);
  370. err = idr_get_new_above(&mmu_context_idr, NULL, 1, &index);
  371. spin_unlock(&mmu_context_lock);
  372. if (err == -EAGAIN)
  373. goto again;
  374. else if (err)
  375. return err;
  376. if (index > MAX_CONTEXT) {
  377. idr_remove(&mmu_context_idr, index);
  378. return -ENOMEM;
  379. }
  380. mm->context.id = index;
  381. return 0;
  382. }
  383. void destroy_context(struct mm_struct *mm)
  384. {
  385. spin_lock(&mmu_context_lock);
  386. idr_remove(&mmu_context_idr, mm->context.id);
  387. spin_unlock(&mmu_context_lock);
  388. mm->context.id = NO_CONTEXT;
  389. }
  390. /*
  391. * Do very early mm setup.
  392. */
  393. void __init mm_init_ppc64(void)
  394. {
  395. #ifndef CONFIG_PPC_ISERIES
  396. unsigned long i;
  397. #endif
  398. ppc64_boot_msg(0x100, "MM Init");
  399. /* This is the story of the IO hole... please, keep seated,
  400. * unfortunately, we are out of oxygen masks at the moment.
  401. * So we need some rough way to tell where your big IO hole
  402. * is. On pmac, it's between 2G and 4G, on POWER3, it's around
  403. * that area as well, on POWER4 we don't have one, etc...
  404. * We need that as a "hint" when sizing the TCE table on POWER3
  405. * So far, the simplest way that seem work well enough for us it
  406. * to just assume that the first discontinuity in our physical
  407. * RAM layout is the IO hole. That may not be correct in the future
  408. * (and isn't on iSeries but then we don't care ;)
  409. */
  410. #ifndef CONFIG_PPC_ISERIES
  411. for (i = 1; i < lmb.memory.cnt; i++) {
  412. unsigned long base, prevbase, prevsize;
  413. prevbase = lmb.memory.region[i-1].base;
  414. prevsize = lmb.memory.region[i-1].size;
  415. base = lmb.memory.region[i].base;
  416. if (base > (prevbase + prevsize)) {
  417. io_hole_start = prevbase + prevsize;
  418. io_hole_size = base - (prevbase + prevsize);
  419. break;
  420. }
  421. }
  422. #endif /* CONFIG_PPC_ISERIES */
  423. if (io_hole_start)
  424. printk("IO Hole assumed to be %lx -> %lx\n",
  425. io_hole_start, io_hole_start + io_hole_size - 1);
  426. ppc64_boot_msg(0x100, "MM Init Done");
  427. }
  428. /*
  429. * This is called by /dev/mem to know if a given address has to
  430. * be mapped non-cacheable or not
  431. */
  432. int page_is_ram(unsigned long pfn)
  433. {
  434. int i;
  435. unsigned long paddr = (pfn << PAGE_SHIFT);
  436. for (i=0; i < lmb.memory.cnt; i++) {
  437. unsigned long base;
  438. base = lmb.memory.region[i].base;
  439. if ((paddr >= base) &&
  440. (paddr < (base + lmb.memory.region[i].size))) {
  441. return 1;
  442. }
  443. }
  444. return 0;
  445. }
  446. EXPORT_SYMBOL(page_is_ram);
  447. /*
  448. * Initialize the bootmem system and give it all the memory we
  449. * have available.
  450. */
  451. #ifndef CONFIG_NEED_MULTIPLE_NODES
  452. void __init do_init_bootmem(void)
  453. {
  454. unsigned long i;
  455. unsigned long start, bootmap_pages;
  456. unsigned long total_pages = lmb_end_of_DRAM() >> PAGE_SHIFT;
  457. int boot_mapsize;
  458. /*
  459. * Find an area to use for the bootmem bitmap. Calculate the size of
  460. * bitmap required as (Total Memory) / PAGE_SIZE / BITS_PER_BYTE.
  461. * Add 1 additional page in case the address isn't page-aligned.
  462. */
  463. bootmap_pages = bootmem_bootmap_pages(total_pages);
  464. start = lmb_alloc(bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
  465. BUG_ON(!start);
  466. boot_mapsize = init_bootmem(start >> PAGE_SHIFT, total_pages);
  467. max_pfn = max_low_pfn;
  468. /* Add all physical memory to the bootmem map, mark each area
  469. * present.
  470. */
  471. for (i=0; i < lmb.memory.cnt; i++)
  472. free_bootmem(lmb.memory.region[i].base,
  473. lmb_size_bytes(&lmb.memory, i));
  474. /* reserve the sections we're already using */
  475. for (i=0; i < lmb.reserved.cnt; i++)
  476. reserve_bootmem(lmb.reserved.region[i].base,
  477. lmb_size_bytes(&lmb.reserved, i));
  478. for (i=0; i < lmb.memory.cnt; i++)
  479. memory_present(0, lmb_start_pfn(&lmb.memory, i),
  480. lmb_end_pfn(&lmb.memory, i));
  481. }
  482. /*
  483. * paging_init() sets up the page tables - in fact we've already done this.
  484. */
  485. void __init paging_init(void)
  486. {
  487. unsigned long zones_size[MAX_NR_ZONES];
  488. unsigned long zholes_size[MAX_NR_ZONES];
  489. unsigned long total_ram = lmb_phys_mem_size();
  490. unsigned long top_of_ram = lmb_end_of_DRAM();
  491. printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  492. top_of_ram, total_ram);
  493. printk(KERN_INFO "Memory hole size: %ldMB\n",
  494. (top_of_ram - total_ram) >> 20);
  495. /*
  496. * All pages are DMA-able so we put them all in the DMA zone.
  497. */
  498. memset(zones_size, 0, sizeof(zones_size));
  499. memset(zholes_size, 0, sizeof(zholes_size));
  500. zones_size[ZONE_DMA] = top_of_ram >> PAGE_SHIFT;
  501. zholes_size[ZONE_DMA] = (top_of_ram - total_ram) >> PAGE_SHIFT;
  502. free_area_init_node(0, NODE_DATA(0), zones_size,
  503. __pa(PAGE_OFFSET) >> PAGE_SHIFT, zholes_size);
  504. }
  505. #endif /* ! CONFIG_NEED_MULTIPLE_NODES */
  506. static struct kcore_list kcore_vmem;
  507. static int __init setup_kcore(void)
  508. {
  509. int i;
  510. for (i=0; i < lmb.memory.cnt; i++) {
  511. unsigned long base, size;
  512. struct kcore_list *kcore_mem;
  513. base = lmb.memory.region[i].base;
  514. size = lmb.memory.region[i].size;
  515. /* GFP_ATOMIC to avoid might_sleep warnings during boot */
  516. kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC);
  517. if (!kcore_mem)
  518. panic("mem_init: kmalloc failed\n");
  519. kclist_add(kcore_mem, __va(base), size);
  520. }
  521. kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START);
  522. return 0;
  523. }
  524. module_init(setup_kcore);
  525. void __init mem_init(void)
  526. {
  527. #ifdef CONFIG_NEED_MULTIPLE_NODES
  528. int nid;
  529. #endif
  530. pg_data_t *pgdat;
  531. unsigned long i;
  532. struct page *page;
  533. unsigned long reservedpages = 0, codesize, initsize, datasize, bsssize;
  534. num_physpages = max_low_pfn; /* RAM is assumed contiguous */
  535. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
  536. #ifdef CONFIG_NEED_MULTIPLE_NODES
  537. for_each_online_node(nid) {
  538. if (NODE_DATA(nid)->node_spanned_pages != 0) {
  539. printk("freeing bootmem node %x\n", nid);
  540. totalram_pages +=
  541. free_all_bootmem_node(NODE_DATA(nid));
  542. }
  543. }
  544. #else
  545. max_mapnr = num_physpages;
  546. totalram_pages += free_all_bootmem();
  547. #endif
  548. for_each_pgdat(pgdat) {
  549. for (i = 0; i < pgdat->node_spanned_pages; i++) {
  550. page = pgdat_page_nr(pgdat, i);
  551. if (PageReserved(page))
  552. reservedpages++;
  553. }
  554. }
  555. codesize = (unsigned long)&_etext - (unsigned long)&_stext;
  556. initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin;
  557. datasize = (unsigned long)&_edata - (unsigned long)&__init_end;
  558. bsssize = (unsigned long)&__bss_stop - (unsigned long)&__bss_start;
  559. printk(KERN_INFO "Memory: %luk/%luk available (%luk kernel code, "
  560. "%luk reserved, %luk data, %luk bss, %luk init)\n",
  561. (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
  562. num_physpages << (PAGE_SHIFT-10),
  563. codesize >> 10,
  564. reservedpages << (PAGE_SHIFT-10),
  565. datasize >> 10,
  566. bsssize >> 10,
  567. initsize >> 10);
  568. mem_init_done = 1;
  569. /* Initialize the vDSO */
  570. vdso_init();
  571. }
  572. /*
  573. * This is called when a page has been modified by the kernel.
  574. * It just marks the page as not i-cache clean. We do the i-cache
  575. * flush later when the page is given to a user process, if necessary.
  576. */
  577. void flush_dcache_page(struct page *page)
  578. {
  579. if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  580. return;
  581. /* avoid an atomic op if possible */
  582. if (test_bit(PG_arch_1, &page->flags))
  583. clear_bit(PG_arch_1, &page->flags);
  584. }
  585. EXPORT_SYMBOL(flush_dcache_page);
  586. void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
  587. {
  588. clear_page(page);
  589. if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  590. return;
  591. /*
  592. * We shouldnt have to do this, but some versions of glibc
  593. * require it (ld.so assumes zero filled pages are icache clean)
  594. * - Anton
  595. */
  596. /* avoid an atomic op if possible */
  597. if (test_bit(PG_arch_1, &pg->flags))
  598. clear_bit(PG_arch_1, &pg->flags);
  599. }
  600. EXPORT_SYMBOL(clear_user_page);
  601. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  602. struct page *pg)
  603. {
  604. copy_page(vto, vfrom);
  605. /*
  606. * We should be able to use the following optimisation, however
  607. * there are two problems.
  608. * Firstly a bug in some versions of binutils meant PLT sections
  609. * were not marked executable.
  610. * Secondly the first word in the GOT section is blrl, used
  611. * to establish the GOT address. Until recently the GOT was
  612. * not marked executable.
  613. * - Anton
  614. */
  615. #if 0
  616. if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
  617. return;
  618. #endif
  619. if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  620. return;
  621. /* avoid an atomic op if possible */
  622. if (test_bit(PG_arch_1, &pg->flags))
  623. clear_bit(PG_arch_1, &pg->flags);
  624. }
  625. void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  626. unsigned long addr, int len)
  627. {
  628. unsigned long maddr;
  629. maddr = (unsigned long)page_address(page) + (addr & ~PAGE_MASK);
  630. flush_icache_range(maddr, maddr + len);
  631. }
  632. EXPORT_SYMBOL(flush_icache_user_range);
  633. /*
  634. * This is called at the end of handling a user page fault, when the
  635. * fault has been handled by updating a PTE in the linux page tables.
  636. * We use it to preload an HPTE into the hash table corresponding to
  637. * the updated linux PTE.
  638. *
  639. * This must always be called with the mm->page_table_lock held
  640. */
  641. void update_mmu_cache(struct vm_area_struct *vma, unsigned long ea,
  642. pte_t pte)
  643. {
  644. unsigned long vsid;
  645. void *pgdir;
  646. pte_t *ptep;
  647. int local = 0;
  648. cpumask_t tmp;
  649. unsigned long flags;
  650. /* handle i-cache coherency */
  651. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE) &&
  652. !cpu_has_feature(CPU_FTR_NOEXECUTE)) {
  653. unsigned long pfn = pte_pfn(pte);
  654. if (pfn_valid(pfn)) {
  655. struct page *page = pfn_to_page(pfn);
  656. if (!PageReserved(page)
  657. && !test_bit(PG_arch_1, &page->flags)) {
  658. __flush_dcache_icache(page_address(page));
  659. set_bit(PG_arch_1, &page->flags);
  660. }
  661. }
  662. }
  663. /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
  664. if (!pte_young(pte))
  665. return;
  666. pgdir = vma->vm_mm->pgd;
  667. if (pgdir == NULL)
  668. return;
  669. ptep = find_linux_pte(pgdir, ea);
  670. if (!ptep)
  671. return;
  672. vsid = get_vsid(vma->vm_mm->context.id, ea);
  673. local_irq_save(flags);
  674. tmp = cpumask_of_cpu(smp_processor_id());
  675. if (cpus_equal(vma->vm_mm->cpu_vm_mask, tmp))
  676. local = 1;
  677. __hash_page(ea, 0, vsid, ptep, 0x300, local);
  678. local_irq_restore(flags);
  679. }
  680. void __iomem * reserve_phb_iospace(unsigned long size)
  681. {
  682. void __iomem *virt_addr;
  683. if (phbs_io_bot >= IMALLOC_BASE)
  684. panic("reserve_phb_iospace(): phb io space overflow\n");
  685. virt_addr = (void __iomem *) phbs_io_bot;
  686. phbs_io_bot += size;
  687. return virt_addr;
  688. }
  689. static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
  690. {
  691. memset(addr, 0, kmem_cache_size(cache));
  692. }
  693. static const int pgtable_cache_size[2] = {
  694. PTE_TABLE_SIZE, PMD_TABLE_SIZE
  695. };
  696. static const char *pgtable_cache_name[ARRAY_SIZE(pgtable_cache_size)] = {
  697. "pgd_pte_cache", "pud_pmd_cache",
  698. };
  699. kmem_cache_t *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)];
  700. void pgtable_cache_init(void)
  701. {
  702. int i;
  703. BUILD_BUG_ON(PTE_TABLE_SIZE != pgtable_cache_size[PTE_CACHE_NUM]);
  704. BUILD_BUG_ON(PMD_TABLE_SIZE != pgtable_cache_size[PMD_CACHE_NUM]);
  705. BUILD_BUG_ON(PUD_TABLE_SIZE != pgtable_cache_size[PUD_CACHE_NUM]);
  706. BUILD_BUG_ON(PGD_TABLE_SIZE != pgtable_cache_size[PGD_CACHE_NUM]);
  707. for (i = 0; i < ARRAY_SIZE(pgtable_cache_size); i++) {
  708. int size = pgtable_cache_size[i];
  709. const char *name = pgtable_cache_name[i];
  710. pgtable_cache[i] = kmem_cache_create(name,
  711. size, size,
  712. SLAB_HWCACHE_ALIGN
  713. | SLAB_MUST_HWCACHE_ALIGN,
  714. zero_ctor,
  715. NULL);
  716. if (! pgtable_cache[i])
  717. panic("pgtable_cache_init(): could not create %s!\n",
  718. name);
  719. }
  720. }
  721. pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr,
  722. unsigned long size, pgprot_t vma_prot)
  723. {
  724. if (ppc_md.phys_mem_access_prot)
  725. return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot);
  726. if (!page_is_ram(addr >> PAGE_SHIFT))
  727. vma_prot = __pgprot(pgprot_val(vma_prot)
  728. | _PAGE_GUARDED | _PAGE_NO_CACHE);
  729. return vma_prot;
  730. }
  731. EXPORT_SYMBOL(phys_mem_access_prot);