init.c 27 KB

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