init.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * linux/arch/parisc/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright 1999 SuSE GmbH
  6. * changed by Philipp Rumpf
  7. * Copyright 1999 Philipp Rumpf (prumpf@tux.org)
  8. * Copyright 2004 Randolph Chung (tausq@debian.org)
  9. * Copyright 2006 Helge Deller (deller@gmx.de)
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/pci.h> /* for hppa_dma_ops and pcxl_dma_ops */
  18. #include <linux/initrd.h>
  19. #include <linux/swap.h>
  20. #include <linux/unistd.h>
  21. #include <linux/nodemask.h> /* for node_online_map */
  22. #include <linux/pagemap.h> /* for release_pages and page_cache_release */
  23. #include <asm/pgalloc.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/tlb.h>
  26. #include <asm/pdc_chassis.h>
  27. #include <asm/mmzone.h>
  28. #include <asm/sections.h>
  29. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  30. extern int data_start;
  31. #ifdef CONFIG_DISCONTIGMEM
  32. struct node_map_data node_data[MAX_NUMNODES] __read_mostly;
  33. bootmem_data_t bmem_data[MAX_NUMNODES] __read_mostly;
  34. unsigned char pfnnid_map[PFNNID_MAP_MAX] __read_mostly;
  35. #endif
  36. static struct resource data_resource = {
  37. .name = "Kernel data",
  38. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  39. };
  40. static struct resource code_resource = {
  41. .name = "Kernel code",
  42. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  43. };
  44. static struct resource pdcdata_resource = {
  45. .name = "PDC data (Page Zero)",
  46. .start = 0,
  47. .end = 0x9ff,
  48. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  49. };
  50. static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __read_mostly;
  51. /* The following array is initialized from the firmware specific
  52. * information retrieved in kernel/inventory.c.
  53. */
  54. physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __read_mostly;
  55. int npmem_ranges __read_mostly;
  56. #ifdef __LP64__
  57. #define MAX_MEM (~0UL)
  58. #else /* !__LP64__ */
  59. #define MAX_MEM (3584U*1024U*1024U)
  60. #endif /* !__LP64__ */
  61. static unsigned long mem_limit __read_mostly = MAX_MEM;
  62. static void __init mem_limit_func(void)
  63. {
  64. char *cp, *end;
  65. unsigned long limit;
  66. extern char saved_command_line[];
  67. /* We need this before __setup() functions are called */
  68. limit = MAX_MEM;
  69. for (cp = saved_command_line; *cp; ) {
  70. if (memcmp(cp, "mem=", 4) == 0) {
  71. cp += 4;
  72. limit = memparse(cp, &end);
  73. if (end != cp)
  74. break;
  75. cp = end;
  76. } else {
  77. while (*cp != ' ' && *cp)
  78. ++cp;
  79. while (*cp == ' ')
  80. ++cp;
  81. }
  82. }
  83. if (limit < mem_limit)
  84. mem_limit = limit;
  85. }
  86. #define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
  87. static void __init setup_bootmem(void)
  88. {
  89. unsigned long bootmap_size;
  90. unsigned long mem_max;
  91. unsigned long bootmap_pages;
  92. unsigned long bootmap_start_pfn;
  93. unsigned long bootmap_pfn;
  94. #ifndef CONFIG_DISCONTIGMEM
  95. physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
  96. int npmem_holes;
  97. #endif
  98. int i, sysram_resource_count;
  99. disable_sr_hashing(); /* Turn off space register hashing */
  100. /*
  101. * Sort the ranges. Since the number of ranges is typically
  102. * small, and performance is not an issue here, just do
  103. * a simple insertion sort.
  104. */
  105. for (i = 1; i < npmem_ranges; i++) {
  106. int j;
  107. for (j = i; j > 0; j--) {
  108. unsigned long tmp;
  109. if (pmem_ranges[j-1].start_pfn <
  110. pmem_ranges[j].start_pfn) {
  111. break;
  112. }
  113. tmp = pmem_ranges[j-1].start_pfn;
  114. pmem_ranges[j-1].start_pfn = pmem_ranges[j].start_pfn;
  115. pmem_ranges[j].start_pfn = tmp;
  116. tmp = pmem_ranges[j-1].pages;
  117. pmem_ranges[j-1].pages = pmem_ranges[j].pages;
  118. pmem_ranges[j].pages = tmp;
  119. }
  120. }
  121. #ifndef CONFIG_DISCONTIGMEM
  122. /*
  123. * Throw out ranges that are too far apart (controlled by
  124. * MAX_GAP).
  125. */
  126. for (i = 1; i < npmem_ranges; i++) {
  127. if (pmem_ranges[i].start_pfn -
  128. (pmem_ranges[i-1].start_pfn +
  129. pmem_ranges[i-1].pages) > MAX_GAP) {
  130. npmem_ranges = i;
  131. printk("Large gap in memory detected (%ld pages). "
  132. "Consider turning on CONFIG_DISCONTIGMEM\n",
  133. pmem_ranges[i].start_pfn -
  134. (pmem_ranges[i-1].start_pfn +
  135. pmem_ranges[i-1].pages));
  136. break;
  137. }
  138. }
  139. #endif
  140. if (npmem_ranges > 1) {
  141. /* Print the memory ranges */
  142. printk(KERN_INFO "Memory Ranges:\n");
  143. for (i = 0; i < npmem_ranges; i++) {
  144. unsigned long start;
  145. unsigned long size;
  146. size = (pmem_ranges[i].pages << PAGE_SHIFT);
  147. start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
  148. printk(KERN_INFO "%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
  149. i,start, start + (size - 1), size >> 20);
  150. }
  151. }
  152. sysram_resource_count = npmem_ranges;
  153. for (i = 0; i < sysram_resource_count; i++) {
  154. struct resource *res = &sysram_resources[i];
  155. res->name = "System RAM";
  156. res->start = pmem_ranges[i].start_pfn << PAGE_SHIFT;
  157. res->end = res->start + (pmem_ranges[i].pages << PAGE_SHIFT)-1;
  158. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  159. request_resource(&iomem_resource, res);
  160. }
  161. /*
  162. * For 32 bit kernels we limit the amount of memory we can
  163. * support, in order to preserve enough kernel address space
  164. * for other purposes. For 64 bit kernels we don't normally
  165. * limit the memory, but this mechanism can be used to
  166. * artificially limit the amount of memory (and it is written
  167. * to work with multiple memory ranges).
  168. */
  169. mem_limit_func(); /* check for "mem=" argument */
  170. mem_max = 0;
  171. num_physpages = 0;
  172. for (i = 0; i < npmem_ranges; i++) {
  173. unsigned long rsize;
  174. rsize = pmem_ranges[i].pages << PAGE_SHIFT;
  175. if ((mem_max + rsize) > mem_limit) {
  176. printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
  177. if (mem_max == mem_limit)
  178. npmem_ranges = i;
  179. else {
  180. pmem_ranges[i].pages = (mem_limit >> PAGE_SHIFT)
  181. - (mem_max >> PAGE_SHIFT);
  182. npmem_ranges = i + 1;
  183. mem_max = mem_limit;
  184. }
  185. num_physpages += pmem_ranges[i].pages;
  186. break;
  187. }
  188. num_physpages += pmem_ranges[i].pages;
  189. mem_max += rsize;
  190. }
  191. printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20);
  192. #ifndef CONFIG_DISCONTIGMEM
  193. /* Merge the ranges, keeping track of the holes */
  194. {
  195. unsigned long end_pfn;
  196. unsigned long hole_pages;
  197. npmem_holes = 0;
  198. end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
  199. for (i = 1; i < npmem_ranges; i++) {
  200. hole_pages = pmem_ranges[i].start_pfn - end_pfn;
  201. if (hole_pages) {
  202. pmem_holes[npmem_holes].start_pfn = end_pfn;
  203. pmem_holes[npmem_holes++].pages = hole_pages;
  204. end_pfn += hole_pages;
  205. }
  206. end_pfn += pmem_ranges[i].pages;
  207. }
  208. pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
  209. npmem_ranges = 1;
  210. }
  211. #endif
  212. bootmap_pages = 0;
  213. for (i = 0; i < npmem_ranges; i++)
  214. bootmap_pages += bootmem_bootmap_pages(pmem_ranges[i].pages);
  215. bootmap_start_pfn = PAGE_ALIGN(__pa((unsigned long) &_end)) >> PAGE_SHIFT;
  216. #ifdef CONFIG_DISCONTIGMEM
  217. for (i = 0; i < MAX_PHYSMEM_RANGES; i++) {
  218. memset(NODE_DATA(i), 0, sizeof(pg_data_t));
  219. NODE_DATA(i)->bdata = &bmem_data[i];
  220. }
  221. memset(pfnnid_map, 0xff, sizeof(pfnnid_map));
  222. for (i = 0; i < npmem_ranges; i++)
  223. node_set_online(i);
  224. #endif
  225. /*
  226. * Initialize and free the full range of memory in each range.
  227. * Note that the only writing these routines do are to the bootmap,
  228. * and we've made sure to locate the bootmap properly so that they
  229. * won't be writing over anything important.
  230. */
  231. bootmap_pfn = bootmap_start_pfn;
  232. max_pfn = 0;
  233. for (i = 0; i < npmem_ranges; i++) {
  234. unsigned long start_pfn;
  235. unsigned long npages;
  236. start_pfn = pmem_ranges[i].start_pfn;
  237. npages = pmem_ranges[i].pages;
  238. bootmap_size = init_bootmem_node(NODE_DATA(i),
  239. bootmap_pfn,
  240. start_pfn,
  241. (start_pfn + npages) );
  242. free_bootmem_node(NODE_DATA(i),
  243. (start_pfn << PAGE_SHIFT),
  244. (npages << PAGE_SHIFT) );
  245. bootmap_pfn += (bootmap_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  246. if ((start_pfn + npages) > max_pfn)
  247. max_pfn = start_pfn + npages;
  248. }
  249. /* IOMMU is always used to access "high mem" on those boxes
  250. * that can support enough mem that a PCI device couldn't
  251. * directly DMA to any physical addresses.
  252. * ISA DMA support will need to revisit this.
  253. */
  254. max_low_pfn = max_pfn;
  255. if ((bootmap_pfn - bootmap_start_pfn) != bootmap_pages) {
  256. printk(KERN_WARNING "WARNING! bootmap sizing is messed up!\n");
  257. BUG();
  258. }
  259. /* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
  260. #define PDC_CONSOLE_IO_IODC_SIZE 32768
  261. reserve_bootmem_node(NODE_DATA(0), 0UL,
  262. (unsigned long)(PAGE0->mem_free + PDC_CONSOLE_IO_IODC_SIZE));
  263. reserve_bootmem_node(NODE_DATA(0), __pa((unsigned long)_text),
  264. (unsigned long)(_end - _text));
  265. reserve_bootmem_node(NODE_DATA(0), (bootmap_start_pfn << PAGE_SHIFT),
  266. ((bootmap_pfn - bootmap_start_pfn) << PAGE_SHIFT));
  267. #ifndef CONFIG_DISCONTIGMEM
  268. /* reserve the holes */
  269. for (i = 0; i < npmem_holes; i++) {
  270. reserve_bootmem_node(NODE_DATA(0),
  271. (pmem_holes[i].start_pfn << PAGE_SHIFT),
  272. (pmem_holes[i].pages << PAGE_SHIFT));
  273. }
  274. #endif
  275. #ifdef CONFIG_BLK_DEV_INITRD
  276. if (initrd_start) {
  277. printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
  278. if (__pa(initrd_start) < mem_max) {
  279. unsigned long initrd_reserve;
  280. if (__pa(initrd_end) > mem_max) {
  281. initrd_reserve = mem_max - __pa(initrd_start);
  282. } else {
  283. initrd_reserve = initrd_end - initrd_start;
  284. }
  285. initrd_below_start_ok = 1;
  286. printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
  287. reserve_bootmem_node(NODE_DATA(0),__pa(initrd_start), initrd_reserve);
  288. }
  289. }
  290. #endif
  291. data_resource.start = virt_to_phys(&data_start);
  292. data_resource.end = virt_to_phys(_end) - 1;
  293. code_resource.start = virt_to_phys(_text);
  294. code_resource.end = virt_to_phys(&data_start)-1;
  295. /* We don't know which region the kernel will be in, so try
  296. * all of them.
  297. */
  298. for (i = 0; i < sysram_resource_count; i++) {
  299. struct resource *res = &sysram_resources[i];
  300. request_resource(res, &code_resource);
  301. request_resource(res, &data_resource);
  302. }
  303. request_resource(&sysram_resources[0], &pdcdata_resource);
  304. }
  305. void free_initmem(void)
  306. {
  307. unsigned long addr, init_begin, init_end;
  308. printk(KERN_INFO "Freeing unused kernel memory: ");
  309. #ifdef CONFIG_DEBUG_KERNEL
  310. /* Attempt to catch anyone trying to execute code here
  311. * by filling the page with BRK insns.
  312. *
  313. * If we disable interrupts for all CPUs, then IPI stops working.
  314. * Kinda breaks the global cache flushing.
  315. */
  316. local_irq_disable();
  317. memset(__init_begin, 0x00,
  318. (unsigned long)__init_end - (unsigned long)__init_begin);
  319. flush_data_cache();
  320. asm volatile("sync" : : );
  321. flush_icache_range((unsigned long)__init_begin, (unsigned long)__init_end);
  322. asm volatile("sync" : : );
  323. local_irq_enable();
  324. #endif
  325. /* align __init_begin and __init_end to page size,
  326. ignoring linker script where we might have tried to save RAM */
  327. init_begin = PAGE_ALIGN((unsigned long)(__init_begin));
  328. init_end = PAGE_ALIGN((unsigned long)(__init_end));
  329. for (addr = init_begin; addr < init_end; addr += PAGE_SIZE) {
  330. ClearPageReserved(virt_to_page(addr));
  331. init_page_count(virt_to_page(addr));
  332. free_page(addr);
  333. num_physpages++;
  334. totalram_pages++;
  335. }
  336. /* set up a new led state on systems shipped LED State panel */
  337. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
  338. printk("%luk freed\n", (init_end - init_begin) >> 10);
  339. }
  340. #ifdef CONFIG_DEBUG_RODATA
  341. void mark_rodata_ro(void)
  342. {
  343. /* rodata memory was already mapped with KERNEL_RO access rights by
  344. pagetable_init() and map_pages(). No need to do additional stuff here */
  345. printk (KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  346. (unsigned long)(__end_rodata - __start_rodata) >> 10);
  347. }
  348. #endif
  349. /*
  350. * Just an arbitrary offset to serve as a "hole" between mapping areas
  351. * (between top of physical memory and a potential pcxl dma mapping
  352. * area, and below the vmalloc mapping area).
  353. *
  354. * The current 32K value just means that there will be a 32K "hole"
  355. * between mapping areas. That means that any out-of-bounds memory
  356. * accesses will hopefully be caught. The vmalloc() routines leaves
  357. * a hole of 4kB between each vmalloced area for the same reason.
  358. */
  359. /* Leave room for gateway page expansion */
  360. #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
  361. #error KERNEL_MAP_START is in gateway reserved region
  362. #endif
  363. #define MAP_START (KERNEL_MAP_START)
  364. #define VM_MAP_OFFSET (32*1024)
  365. #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
  366. & ~(VM_MAP_OFFSET-1)))
  367. void *vmalloc_start __read_mostly;
  368. EXPORT_SYMBOL(vmalloc_start);
  369. #ifdef CONFIG_PA11
  370. unsigned long pcxl_dma_start __read_mostly;
  371. #endif
  372. void __init mem_init(void)
  373. {
  374. int codesize, reservedpages, datasize, initsize;
  375. int tmp;
  376. high_memory = __va((max_pfn << PAGE_SHIFT));
  377. #ifndef CONFIG_DISCONTIGMEM
  378. max_mapnr = page_to_pfn(virt_to_page(high_memory - 1)) + 1;
  379. totalram_pages += free_all_bootmem();
  380. #else
  381. {
  382. int i;
  383. for (i = 0; i < npmem_ranges; i++)
  384. totalram_pages += free_all_bootmem_node(NODE_DATA(i));
  385. }
  386. #endif
  387. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  388. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  389. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  390. reservedpages = 0;
  391. for (tmp = 0; tmp < max_low_pfn; tmp++)
  392. /*
  393. * Only count reserved RAM pages
  394. */
  395. if (PageReserved(pfn_to_page(tmp)))
  396. reservedpages++;
  397. #ifdef CONFIG_PA11
  398. if (hppa_dma_ops == &pcxl_dma_ops) {
  399. pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
  400. vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start + PCXL_DMA_MAP_SIZE);
  401. } else {
  402. pcxl_dma_start = 0;
  403. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  404. }
  405. #else
  406. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  407. #endif
  408. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
  409. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  410. num_physpages << (PAGE_SHIFT-10),
  411. codesize >> 10,
  412. reservedpages << (PAGE_SHIFT-10),
  413. datasize >> 10,
  414. initsize >> 10,
  415. (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
  416. );
  417. #ifdef CONFIG_DEBUG_KERNEL /* double-sanity-check paranoia */
  418. printk("virtual kernel memory layout:\n"
  419. " vmalloc : 0x%p - 0x%p (%4ld MB)\n"
  420. " lowmem : 0x%p - 0x%p (%4ld MB)\n"
  421. " .init : 0x%p - 0x%p (%4ld kB)\n"
  422. " .data : 0x%p - 0x%p (%4ld kB)\n"
  423. " .text : 0x%p - 0x%p (%4ld kB)\n",
  424. (void*)VMALLOC_START, (void*)VMALLOC_END,
  425. (VMALLOC_END - VMALLOC_START) >> 20,
  426. __va(0), high_memory,
  427. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  428. &__init_begin, &__init_end,
  429. ((unsigned long)&__init_end - (unsigned long)&__init_begin) >> 10,
  430. &_etext, &_edata,
  431. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  432. &_text, &_etext,
  433. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  434. #endif
  435. }
  436. unsigned long *empty_zero_page __read_mostly;
  437. void show_mem(void)
  438. {
  439. int i,free = 0,total = 0,reserved = 0;
  440. int shared = 0, cached = 0;
  441. printk(KERN_INFO "Mem-info:\n");
  442. show_free_areas();
  443. printk(KERN_INFO "Free swap: %6ldkB\n",
  444. nr_swap_pages<<(PAGE_SHIFT-10));
  445. #ifndef CONFIG_DISCONTIGMEM
  446. i = max_mapnr;
  447. while (i-- > 0) {
  448. total++;
  449. if (PageReserved(mem_map+i))
  450. reserved++;
  451. else if (PageSwapCache(mem_map+i))
  452. cached++;
  453. else if (!page_count(&mem_map[i]))
  454. free++;
  455. else
  456. shared += page_count(&mem_map[i]) - 1;
  457. }
  458. #else
  459. for (i = 0; i < npmem_ranges; i++) {
  460. int j;
  461. for (j = node_start_pfn(i); j < node_end_pfn(i); j++) {
  462. struct page *p;
  463. unsigned long flags;
  464. pgdat_resize_lock(NODE_DATA(i), &flags);
  465. p = nid_page_nr(i, j) - node_start_pfn(i);
  466. total++;
  467. if (PageReserved(p))
  468. reserved++;
  469. else if (PageSwapCache(p))
  470. cached++;
  471. else if (!page_count(p))
  472. free++;
  473. else
  474. shared += page_count(p) - 1;
  475. pgdat_resize_unlock(NODE_DATA(i), &flags);
  476. }
  477. }
  478. #endif
  479. printk(KERN_INFO "%d pages of RAM\n", total);
  480. printk(KERN_INFO "%d reserved pages\n", reserved);
  481. printk(KERN_INFO "%d pages shared\n", shared);
  482. printk(KERN_INFO "%d pages swap cached\n", cached);
  483. #ifdef CONFIG_DISCONTIGMEM
  484. {
  485. struct zonelist *zl;
  486. int i, j, k;
  487. for (i = 0; i < npmem_ranges; i++) {
  488. for (j = 0; j < MAX_NR_ZONES; j++) {
  489. zl = NODE_DATA(i)->node_zonelists + j;
  490. printk("Zone list for zone %d on node %d: ", j, i);
  491. for (k = 0; zl->zones[k] != NULL; k++)
  492. printk("[%d/%s] ", zone_to_nid(zl->zones[k]), zl->zones[k]->name);
  493. printk("\n");
  494. }
  495. }
  496. }
  497. #endif
  498. }
  499. static void __init map_pages(unsigned long start_vaddr, unsigned long start_paddr, unsigned long size, pgprot_t pgprot)
  500. {
  501. pgd_t *pg_dir;
  502. pmd_t *pmd;
  503. pte_t *pg_table;
  504. unsigned long end_paddr;
  505. unsigned long start_pmd;
  506. unsigned long start_pte;
  507. unsigned long tmp1;
  508. unsigned long tmp2;
  509. unsigned long address;
  510. unsigned long ro_start;
  511. unsigned long ro_end;
  512. unsigned long fv_addr;
  513. unsigned long gw_addr;
  514. extern const unsigned long fault_vector_20;
  515. extern void * const linux_gateway_page;
  516. ro_start = __pa((unsigned long)_text);
  517. ro_end = __pa((unsigned long)&data_start);
  518. fv_addr = __pa((unsigned long)&fault_vector_20) & PAGE_MASK;
  519. gw_addr = __pa((unsigned long)&linux_gateway_page) & PAGE_MASK;
  520. end_paddr = start_paddr + size;
  521. pg_dir = pgd_offset_k(start_vaddr);
  522. #if PTRS_PER_PMD == 1
  523. start_pmd = 0;
  524. #else
  525. start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  526. #endif
  527. start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  528. address = start_paddr;
  529. while (address < end_paddr) {
  530. #if PTRS_PER_PMD == 1
  531. pmd = (pmd_t *)__pa(pg_dir);
  532. #else
  533. pmd = (pmd_t *)pgd_address(*pg_dir);
  534. /*
  535. * pmd is physical at this point
  536. */
  537. if (!pmd) {
  538. pmd = (pmd_t *) alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE << PMD_ORDER);
  539. pmd = (pmd_t *) __pa(pmd);
  540. }
  541. pgd_populate(NULL, pg_dir, __va(pmd));
  542. #endif
  543. pg_dir++;
  544. /* now change pmd to kernel virtual addresses */
  545. pmd = (pmd_t *)__va(pmd) + start_pmd;
  546. for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++,pmd++) {
  547. /*
  548. * pg_table is physical at this point
  549. */
  550. pg_table = (pte_t *)pmd_address(*pmd);
  551. if (!pg_table) {
  552. pg_table = (pte_t *)
  553. alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE);
  554. pg_table = (pte_t *) __pa(pg_table);
  555. }
  556. pmd_populate_kernel(NULL, pmd, __va(pg_table));
  557. /* now change pg_table to kernel virtual addresses */
  558. pg_table = (pte_t *) __va(pg_table) + start_pte;
  559. for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++,pg_table++) {
  560. pte_t pte;
  561. /*
  562. * Map the fault vector writable so we can
  563. * write the HPMC checksum.
  564. */
  565. #if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
  566. if (address >= ro_start && address < ro_end
  567. && address != fv_addr
  568. && address != gw_addr)
  569. pte = __mk_pte(address, PAGE_KERNEL_RO);
  570. else
  571. #endif
  572. pte = __mk_pte(address, pgprot);
  573. if (address >= end_paddr)
  574. pte_val(pte) = 0;
  575. set_pte(pg_table, pte);
  576. address += PAGE_SIZE;
  577. }
  578. start_pte = 0;
  579. if (address >= end_paddr)
  580. break;
  581. }
  582. start_pmd = 0;
  583. }
  584. }
  585. /*
  586. * pagetable_init() sets up the page tables
  587. *
  588. * Note that gateway_init() places the Linux gateway page at page 0.
  589. * Since gateway pages cannot be dereferenced this has the desirable
  590. * side effect of trapping those pesky NULL-reference errors in the
  591. * kernel.
  592. */
  593. static void __init pagetable_init(void)
  594. {
  595. int range;
  596. /* Map each physical memory range to its kernel vaddr */
  597. for (range = 0; range < npmem_ranges; range++) {
  598. unsigned long start_paddr;
  599. unsigned long end_paddr;
  600. unsigned long size;
  601. start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
  602. end_paddr = start_paddr + (pmem_ranges[range].pages << PAGE_SHIFT);
  603. size = pmem_ranges[range].pages << PAGE_SHIFT;
  604. map_pages((unsigned long)__va(start_paddr), start_paddr,
  605. size, PAGE_KERNEL);
  606. }
  607. #ifdef CONFIG_BLK_DEV_INITRD
  608. if (initrd_end && initrd_end > mem_limit) {
  609. printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
  610. map_pages(initrd_start, __pa(initrd_start),
  611. initrd_end - initrd_start, PAGE_KERNEL);
  612. }
  613. #endif
  614. empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
  615. memset(empty_zero_page, 0, PAGE_SIZE);
  616. }
  617. static void __init gateway_init(void)
  618. {
  619. unsigned long linux_gateway_page_addr;
  620. /* FIXME: This is 'const' in order to trick the compiler
  621. into not treating it as DP-relative data. */
  622. extern void * const linux_gateway_page;
  623. linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
  624. /*
  625. * Setup Linux Gateway page.
  626. *
  627. * The Linux gateway page will reside in kernel space (on virtual
  628. * page 0), so it doesn't need to be aliased into user space.
  629. */
  630. map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
  631. PAGE_SIZE, PAGE_GATEWAY);
  632. }
  633. #ifdef CONFIG_HPUX
  634. void
  635. map_hpux_gateway_page(struct task_struct *tsk, struct mm_struct *mm)
  636. {
  637. pgd_t *pg_dir;
  638. pmd_t *pmd;
  639. pte_t *pg_table;
  640. unsigned long start_pmd;
  641. unsigned long start_pte;
  642. unsigned long address;
  643. unsigned long hpux_gw_page_addr;
  644. /* FIXME: This is 'const' in order to trick the compiler
  645. into not treating it as DP-relative data. */
  646. extern void * const hpux_gateway_page;
  647. hpux_gw_page_addr = HPUX_GATEWAY_ADDR & PAGE_MASK;
  648. /*
  649. * Setup HP-UX Gateway page.
  650. *
  651. * The HP-UX gateway page resides in the user address space,
  652. * so it needs to be aliased into each process.
  653. */
  654. pg_dir = pgd_offset(mm,hpux_gw_page_addr);
  655. #if PTRS_PER_PMD == 1
  656. start_pmd = 0;
  657. #else
  658. start_pmd = ((hpux_gw_page_addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  659. #endif
  660. start_pte = ((hpux_gw_page_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  661. address = __pa(&hpux_gateway_page);
  662. #if PTRS_PER_PMD == 1
  663. pmd = (pmd_t *)__pa(pg_dir);
  664. #else
  665. pmd = (pmd_t *) pgd_address(*pg_dir);
  666. /*
  667. * pmd is physical at this point
  668. */
  669. if (!pmd) {
  670. pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL);
  671. pmd = (pmd_t *) __pa(pmd);
  672. }
  673. __pgd_val_set(*pg_dir, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pmd);
  674. #endif
  675. /* now change pmd to kernel virtual addresses */
  676. pmd = (pmd_t *)__va(pmd) + start_pmd;
  677. /*
  678. * pg_table is physical at this point
  679. */
  680. pg_table = (pte_t *) pmd_address(*pmd);
  681. if (!pg_table)
  682. pg_table = (pte_t *) __pa(get_zeroed_page(GFP_KERNEL));
  683. __pmd_val_set(*pmd, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pg_table);
  684. /* now change pg_table to kernel virtual addresses */
  685. pg_table = (pte_t *) __va(pg_table) + start_pte;
  686. set_pte(pg_table, __mk_pte(address, PAGE_GATEWAY));
  687. }
  688. EXPORT_SYMBOL(map_hpux_gateway_page);
  689. #endif
  690. void __init paging_init(void)
  691. {
  692. int i;
  693. setup_bootmem();
  694. pagetable_init();
  695. gateway_init();
  696. flush_cache_all_local(); /* start with known state */
  697. flush_tlb_all_local(NULL);
  698. for (i = 0; i < npmem_ranges; i++) {
  699. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  700. /* We have an IOMMU, so all memory can go into a single
  701. ZONE_DMA zone. */
  702. zones_size[ZONE_DMA] = pmem_ranges[i].pages;
  703. #ifdef CONFIG_DISCONTIGMEM
  704. /* Need to initialize the pfnnid_map before we can initialize
  705. the zone */
  706. {
  707. int j;
  708. for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
  709. j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
  710. j++) {
  711. pfnnid_map[j] = i;
  712. }
  713. }
  714. #endif
  715. free_area_init_node(i, NODE_DATA(i), zones_size,
  716. pmem_ranges[i].start_pfn, NULL);
  717. }
  718. }
  719. #ifdef CONFIG_PA20
  720. /*
  721. * Currently, all PA20 chips have 18 bit protection id's, which is the
  722. * limiting factor (space ids are 32 bits).
  723. */
  724. #define NR_SPACE_IDS 262144
  725. #else
  726. /*
  727. * Currently we have a one-to-one relationship between space id's and
  728. * protection id's. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
  729. * support 15 bit protection id's, so that is the limiting factor.
  730. * PCXT' has 18 bit protection id's, but only 16 bit spaceids, so it's
  731. * probably not worth the effort for a special case here.
  732. */
  733. #define NR_SPACE_IDS 32768
  734. #endif /* !CONFIG_PA20 */
  735. #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
  736. #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long)))
  737. static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
  738. static unsigned long dirty_space_id[SID_ARRAY_SIZE];
  739. static unsigned long space_id_index;
  740. static unsigned long free_space_ids = NR_SPACE_IDS - 1;
  741. static unsigned long dirty_space_ids = 0;
  742. static DEFINE_SPINLOCK(sid_lock);
  743. unsigned long alloc_sid(void)
  744. {
  745. unsigned long index;
  746. spin_lock(&sid_lock);
  747. if (free_space_ids == 0) {
  748. if (dirty_space_ids != 0) {
  749. spin_unlock(&sid_lock);
  750. flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
  751. spin_lock(&sid_lock);
  752. }
  753. BUG_ON(free_space_ids == 0);
  754. }
  755. free_space_ids--;
  756. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  757. space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
  758. space_id_index = index;
  759. spin_unlock(&sid_lock);
  760. return index << SPACEID_SHIFT;
  761. }
  762. void free_sid(unsigned long spaceid)
  763. {
  764. unsigned long index = spaceid >> SPACEID_SHIFT;
  765. unsigned long *dirty_space_offset;
  766. dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
  767. index &= (BITS_PER_LONG - 1);
  768. spin_lock(&sid_lock);
  769. BUG_ON(*dirty_space_offset & (1L << index)); /* attempt to free space id twice */
  770. *dirty_space_offset |= (1L << index);
  771. dirty_space_ids++;
  772. spin_unlock(&sid_lock);
  773. }
  774. #ifdef CONFIG_SMP
  775. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  776. {
  777. int i;
  778. /* NOTE: sid_lock must be held upon entry */
  779. *ndirtyptr = dirty_space_ids;
  780. if (dirty_space_ids != 0) {
  781. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  782. dirty_array[i] = dirty_space_id[i];
  783. dirty_space_id[i] = 0;
  784. }
  785. dirty_space_ids = 0;
  786. }
  787. return;
  788. }
  789. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  790. {
  791. int i;
  792. /* NOTE: sid_lock must be held upon entry */
  793. if (ndirty != 0) {
  794. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  795. space_id[i] ^= dirty_array[i];
  796. }
  797. free_space_ids += ndirty;
  798. space_id_index = 0;
  799. }
  800. }
  801. #else /* CONFIG_SMP */
  802. static void recycle_sids(void)
  803. {
  804. int i;
  805. /* NOTE: sid_lock must be held upon entry */
  806. if (dirty_space_ids != 0) {
  807. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  808. space_id[i] ^= dirty_space_id[i];
  809. dirty_space_id[i] = 0;
  810. }
  811. free_space_ids += dirty_space_ids;
  812. dirty_space_ids = 0;
  813. space_id_index = 0;
  814. }
  815. }
  816. #endif
  817. /*
  818. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  819. * purged, we can safely reuse the space ids that were released but
  820. * not flushed from the tlb.
  821. */
  822. #ifdef CONFIG_SMP
  823. static unsigned long recycle_ndirty;
  824. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  825. static unsigned int recycle_inuse;
  826. void flush_tlb_all(void)
  827. {
  828. int do_recycle;
  829. do_recycle = 0;
  830. spin_lock(&sid_lock);
  831. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  832. BUG_ON(recycle_inuse); /* FIXME: Use a semaphore/wait queue here */
  833. get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
  834. recycle_inuse++;
  835. do_recycle++;
  836. }
  837. spin_unlock(&sid_lock);
  838. on_each_cpu(flush_tlb_all_local, NULL, 1, 1);
  839. if (do_recycle) {
  840. spin_lock(&sid_lock);
  841. recycle_sids(recycle_ndirty,recycle_dirty_array);
  842. recycle_inuse = 0;
  843. spin_unlock(&sid_lock);
  844. }
  845. }
  846. #else
  847. void flush_tlb_all(void)
  848. {
  849. spin_lock(&sid_lock);
  850. flush_tlb_all_local(NULL);
  851. recycle_sids();
  852. spin_unlock(&sid_lock);
  853. }
  854. #endif
  855. #ifdef CONFIG_BLK_DEV_INITRD
  856. void free_initrd_mem(unsigned long start, unsigned long end)
  857. {
  858. if (start >= end)
  859. return;
  860. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  861. for (; start < end; start += PAGE_SIZE) {
  862. ClearPageReserved(virt_to_page(start));
  863. init_page_count(virt_to_page(start));
  864. free_page(start);
  865. num_physpages++;
  866. totalram_pages++;
  867. }
  868. }
  869. #endif