init.c 23 KB

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