init.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. /* bootmap sizing messed up? */
  254. BUG_ON((bootmap_pfn - bootmap_start_pfn) != bootmap_pages);
  255. /* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
  256. #define PDC_CONSOLE_IO_IODC_SIZE 32768
  257. reserve_bootmem_node(NODE_DATA(0), 0UL,
  258. (unsigned long)(PAGE0->mem_free +
  259. PDC_CONSOLE_IO_IODC_SIZE), BOOTMEM_DEFAULT);
  260. reserve_bootmem_node(NODE_DATA(0), __pa((unsigned long)_text),
  261. (unsigned long)(_end - _text), BOOTMEM_DEFAULT);
  262. reserve_bootmem_node(NODE_DATA(0), (bootmap_start_pfn << PAGE_SHIFT),
  263. ((bootmap_pfn - bootmap_start_pfn) << PAGE_SHIFT),
  264. BOOTMEM_DEFAULT);
  265. #ifndef CONFIG_DISCONTIGMEM
  266. /* reserve the holes */
  267. for (i = 0; i < npmem_holes; i++) {
  268. reserve_bootmem_node(NODE_DATA(0),
  269. (pmem_holes[i].start_pfn << PAGE_SHIFT),
  270. (pmem_holes[i].pages << PAGE_SHIFT),
  271. BOOTMEM_DEFAULT);
  272. }
  273. #endif
  274. #ifdef CONFIG_BLK_DEV_INITRD
  275. if (initrd_start) {
  276. printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
  277. if (__pa(initrd_start) < mem_max) {
  278. unsigned long initrd_reserve;
  279. if (__pa(initrd_end) > mem_max) {
  280. initrd_reserve = mem_max - __pa(initrd_start);
  281. } else {
  282. initrd_reserve = initrd_end - initrd_start;
  283. }
  284. initrd_below_start_ok = 1;
  285. printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
  286. reserve_bootmem_node(NODE_DATA(0), __pa(initrd_start),
  287. initrd_reserve, BOOTMEM_DEFAULT);
  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;
  308. unsigned long init_begin = (unsigned long)__init_begin;
  309. unsigned long init_end = (unsigned long)__init_end;
  310. #ifdef CONFIG_DEBUG_KERNEL
  311. /* Attempt to catch anyone trying to execute code here
  312. * by filling the page with BRK insns.
  313. */
  314. memset((void *)init_begin, 0x00, init_end - init_begin);
  315. flush_icache_range(init_begin, init_end);
  316. #endif
  317. /* align __init_begin and __init_end to page size,
  318. ignoring linker script where we might have tried to save RAM */
  319. init_begin = PAGE_ALIGN(init_begin);
  320. init_end = PAGE_ALIGN(init_end);
  321. for (addr = init_begin; addr < init_end; addr += PAGE_SIZE) {
  322. ClearPageReserved(virt_to_page(addr));
  323. init_page_count(virt_to_page(addr));
  324. free_page(addr);
  325. num_physpages++;
  326. totalram_pages++;
  327. }
  328. /* set up a new led state on systems shipped LED State panel */
  329. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
  330. printk(KERN_INFO "Freeing unused kernel memory: %luk freed\n",
  331. (init_end - init_begin) >> 10);
  332. }
  333. #ifdef CONFIG_DEBUG_RODATA
  334. void mark_rodata_ro(void)
  335. {
  336. /* rodata memory was already mapped with KERNEL_RO access rights by
  337. pagetable_init() and map_pages(). No need to do additional stuff here */
  338. printk (KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  339. (unsigned long)(__end_rodata - __start_rodata) >> 10);
  340. }
  341. #endif
  342. /*
  343. * Just an arbitrary offset to serve as a "hole" between mapping areas
  344. * (between top of physical memory and a potential pcxl dma mapping
  345. * area, and below the vmalloc mapping area).
  346. *
  347. * The current 32K value just means that there will be a 32K "hole"
  348. * between mapping areas. That means that any out-of-bounds memory
  349. * accesses will hopefully be caught. The vmalloc() routines leaves
  350. * a hole of 4kB between each vmalloced area for the same reason.
  351. */
  352. /* Leave room for gateway page expansion */
  353. #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
  354. #error KERNEL_MAP_START is in gateway reserved region
  355. #endif
  356. #define MAP_START (KERNEL_MAP_START)
  357. #define VM_MAP_OFFSET (32*1024)
  358. #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
  359. & ~(VM_MAP_OFFSET-1)))
  360. void *vmalloc_start __read_mostly;
  361. EXPORT_SYMBOL(vmalloc_start);
  362. #ifdef CONFIG_PA11
  363. unsigned long pcxl_dma_start __read_mostly;
  364. #endif
  365. void __init mem_init(void)
  366. {
  367. int codesize, reservedpages, datasize, initsize;
  368. /* Do sanity checks on page table constants */
  369. BUILD_BUG_ON(PTE_ENTRY_SIZE != sizeof(pte_t));
  370. BUILD_BUG_ON(PMD_ENTRY_SIZE != sizeof(pmd_t));
  371. BUILD_BUG_ON(PGD_ENTRY_SIZE != sizeof(pgd_t));
  372. BUILD_BUG_ON(PAGE_SHIFT + BITS_PER_PTE + BITS_PER_PMD + BITS_PER_PGD
  373. > BITS_PER_LONG);
  374. high_memory = __va((max_pfn << PAGE_SHIFT));
  375. #ifndef CONFIG_DISCONTIGMEM
  376. max_mapnr = page_to_pfn(virt_to_page(high_memory - 1)) + 1;
  377. totalram_pages += free_all_bootmem();
  378. #else
  379. {
  380. int i;
  381. for (i = 0; i < npmem_ranges; i++)
  382. totalram_pages += free_all_bootmem_node(NODE_DATA(i));
  383. }
  384. #endif
  385. codesize = (unsigned long)_etext - (unsigned long)_text;
  386. datasize = (unsigned long)_edata - (unsigned long)_etext;
  387. initsize = (unsigned long)__init_end - (unsigned long)__init_begin;
  388. reservedpages = 0;
  389. {
  390. unsigned long pfn;
  391. #ifdef CONFIG_DISCONTIGMEM
  392. int i;
  393. for (i = 0; i < npmem_ranges; i++) {
  394. for (pfn = node_start_pfn(i); pfn < node_end_pfn(i); pfn++) {
  395. if (PageReserved(pfn_to_page(pfn)))
  396. reservedpages++;
  397. }
  398. }
  399. #else /* !CONFIG_DISCONTIGMEM */
  400. for (pfn = 0; pfn < max_pfn; pfn++) {
  401. /*
  402. * Only count reserved RAM pages
  403. */
  404. if (PageReserved(pfn_to_page(pfn)))
  405. reservedpages++;
  406. }
  407. #endif
  408. }
  409. #ifdef CONFIG_PA11
  410. if (hppa_dma_ops == &pcxl_dma_ops) {
  411. pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
  412. vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start + PCXL_DMA_MAP_SIZE);
  413. } else {
  414. pcxl_dma_start = 0;
  415. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  416. }
  417. #else
  418. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  419. #endif
  420. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
  421. nr_free_pages() << (PAGE_SHIFT-10),
  422. num_physpages << (PAGE_SHIFT-10),
  423. codesize >> 10,
  424. reservedpages << (PAGE_SHIFT-10),
  425. datasize >> 10,
  426. initsize >> 10
  427. );
  428. #ifdef CONFIG_DEBUG_KERNEL /* double-sanity-check paranoia */
  429. printk("virtual kernel memory layout:\n"
  430. " vmalloc : 0x%p - 0x%p (%4ld MB)\n"
  431. " memory : 0x%p - 0x%p (%4ld MB)\n"
  432. " .init : 0x%p - 0x%p (%4ld kB)\n"
  433. " .data : 0x%p - 0x%p (%4ld kB)\n"
  434. " .text : 0x%p - 0x%p (%4ld kB)\n",
  435. (void*)VMALLOC_START, (void*)VMALLOC_END,
  436. (VMALLOC_END - VMALLOC_START) >> 20,
  437. __va(0), high_memory,
  438. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  439. __init_begin, __init_end,
  440. ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10,
  441. _etext, _edata,
  442. ((unsigned long)_edata - (unsigned long)_etext) >> 10,
  443. _text, _etext,
  444. ((unsigned long)_etext - (unsigned long)_text) >> 10);
  445. #endif
  446. }
  447. unsigned long *empty_zero_page __read_mostly;
  448. EXPORT_SYMBOL(empty_zero_page);
  449. void show_mem(void)
  450. {
  451. int i,free = 0,total = 0,reserved = 0;
  452. int shared = 0, cached = 0;
  453. printk(KERN_INFO "Mem-info:\n");
  454. show_free_areas();
  455. #ifndef CONFIG_DISCONTIGMEM
  456. i = max_mapnr;
  457. while (i-- > 0) {
  458. total++;
  459. if (PageReserved(mem_map+i))
  460. reserved++;
  461. else if (PageSwapCache(mem_map+i))
  462. cached++;
  463. else if (!page_count(&mem_map[i]))
  464. free++;
  465. else
  466. shared += page_count(&mem_map[i]) - 1;
  467. }
  468. #else
  469. for (i = 0; i < npmem_ranges; i++) {
  470. int j;
  471. for (j = node_start_pfn(i); j < node_end_pfn(i); j++) {
  472. struct page *p;
  473. unsigned long flags;
  474. pgdat_resize_lock(NODE_DATA(i), &flags);
  475. p = nid_page_nr(i, j) - node_start_pfn(i);
  476. total++;
  477. if (PageReserved(p))
  478. reserved++;
  479. else if (PageSwapCache(p))
  480. cached++;
  481. else if (!page_count(p))
  482. free++;
  483. else
  484. shared += page_count(p) - 1;
  485. pgdat_resize_unlock(NODE_DATA(i), &flags);
  486. }
  487. }
  488. #endif
  489. printk(KERN_INFO "%d pages of RAM\n", total);
  490. printk(KERN_INFO "%d reserved pages\n", reserved);
  491. printk(KERN_INFO "%d pages shared\n", shared);
  492. printk(KERN_INFO "%d pages swap cached\n", cached);
  493. #ifdef CONFIG_DISCONTIGMEM
  494. {
  495. struct zonelist *zl;
  496. int i, j;
  497. for (i = 0; i < npmem_ranges; i++) {
  498. zl = node_zonelist(i, 0);
  499. for (j = 0; j < MAX_NR_ZONES; j++) {
  500. struct zoneref *z;
  501. struct zone *zone;
  502. printk("Zone list for zone %d on node %d: ", j, i);
  503. for_each_zone_zonelist(zone, z, zl, j)
  504. printk("[%d/%s] ", zone_to_nid(zone),
  505. zone->name);
  506. printk("\n");
  507. }
  508. }
  509. }
  510. #endif
  511. }
  512. static void __init map_pages(unsigned long start_vaddr, unsigned long start_paddr, unsigned long size, pgprot_t pgprot)
  513. {
  514. pgd_t *pg_dir;
  515. pmd_t *pmd;
  516. pte_t *pg_table;
  517. unsigned long end_paddr;
  518. unsigned long start_pmd;
  519. unsigned long start_pte;
  520. unsigned long tmp1;
  521. unsigned long tmp2;
  522. unsigned long address;
  523. unsigned long ro_start;
  524. unsigned long ro_end;
  525. unsigned long fv_addr;
  526. unsigned long gw_addr;
  527. extern const unsigned long fault_vector_20;
  528. extern void * const linux_gateway_page;
  529. ro_start = __pa((unsigned long)_text);
  530. ro_end = __pa((unsigned long)&data_start);
  531. fv_addr = __pa((unsigned long)&fault_vector_20) & PAGE_MASK;
  532. gw_addr = __pa((unsigned long)&linux_gateway_page) & PAGE_MASK;
  533. end_paddr = start_paddr + size;
  534. pg_dir = pgd_offset_k(start_vaddr);
  535. #if PTRS_PER_PMD == 1
  536. start_pmd = 0;
  537. #else
  538. start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  539. #endif
  540. start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  541. address = start_paddr;
  542. while (address < end_paddr) {
  543. #if PTRS_PER_PMD == 1
  544. pmd = (pmd_t *)__pa(pg_dir);
  545. #else
  546. pmd = (pmd_t *)pgd_address(*pg_dir);
  547. /*
  548. * pmd is physical at this point
  549. */
  550. if (!pmd) {
  551. pmd = (pmd_t *) alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE << PMD_ORDER);
  552. pmd = (pmd_t *) __pa(pmd);
  553. }
  554. pgd_populate(NULL, pg_dir, __va(pmd));
  555. #endif
  556. pg_dir++;
  557. /* now change pmd to kernel virtual addresses */
  558. pmd = (pmd_t *)__va(pmd) + start_pmd;
  559. for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++,pmd++) {
  560. /*
  561. * pg_table is physical at this point
  562. */
  563. pg_table = (pte_t *)pmd_address(*pmd);
  564. if (!pg_table) {
  565. pg_table = (pte_t *)
  566. alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE);
  567. pg_table = (pte_t *) __pa(pg_table);
  568. }
  569. pmd_populate_kernel(NULL, pmd, __va(pg_table));
  570. /* now change pg_table to kernel virtual addresses */
  571. pg_table = (pte_t *) __va(pg_table) + start_pte;
  572. for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++,pg_table++) {
  573. pte_t pte;
  574. /*
  575. * Map the fault vector writable so we can
  576. * write the HPMC checksum.
  577. */
  578. #if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
  579. if (address >= ro_start && address < ro_end
  580. && address != fv_addr
  581. && address != gw_addr)
  582. pte = __mk_pte(address, PAGE_KERNEL_RO);
  583. else
  584. #endif
  585. pte = __mk_pte(address, pgprot);
  586. if (address >= end_paddr)
  587. pte_val(pte) = 0;
  588. set_pte(pg_table, pte);
  589. address += PAGE_SIZE;
  590. }
  591. start_pte = 0;
  592. if (address >= end_paddr)
  593. break;
  594. }
  595. start_pmd = 0;
  596. }
  597. }
  598. /*
  599. * pagetable_init() sets up the page tables
  600. *
  601. * Note that gateway_init() places the Linux gateway page at page 0.
  602. * Since gateway pages cannot be dereferenced this has the desirable
  603. * side effect of trapping those pesky NULL-reference errors in the
  604. * kernel.
  605. */
  606. static void __init pagetable_init(void)
  607. {
  608. int range;
  609. /* Map each physical memory range to its kernel vaddr */
  610. for (range = 0; range < npmem_ranges; range++) {
  611. unsigned long start_paddr;
  612. unsigned long end_paddr;
  613. unsigned long size;
  614. start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
  615. end_paddr = start_paddr + (pmem_ranges[range].pages << PAGE_SHIFT);
  616. size = pmem_ranges[range].pages << PAGE_SHIFT;
  617. map_pages((unsigned long)__va(start_paddr), start_paddr,
  618. size, PAGE_KERNEL);
  619. }
  620. #ifdef CONFIG_BLK_DEV_INITRD
  621. if (initrd_end && initrd_end > mem_limit) {
  622. printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
  623. map_pages(initrd_start, __pa(initrd_start),
  624. initrd_end - initrd_start, PAGE_KERNEL);
  625. }
  626. #endif
  627. empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
  628. memset(empty_zero_page, 0, PAGE_SIZE);
  629. }
  630. static void __init gateway_init(void)
  631. {
  632. unsigned long linux_gateway_page_addr;
  633. /* FIXME: This is 'const' in order to trick the compiler
  634. into not treating it as DP-relative data. */
  635. extern void * const linux_gateway_page;
  636. linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
  637. /*
  638. * Setup Linux Gateway page.
  639. *
  640. * The Linux gateway page will reside in kernel space (on virtual
  641. * page 0), so it doesn't need to be aliased into user space.
  642. */
  643. map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
  644. PAGE_SIZE, PAGE_GATEWAY);
  645. }
  646. #ifdef CONFIG_HPUX
  647. void
  648. map_hpux_gateway_page(struct task_struct *tsk, struct mm_struct *mm)
  649. {
  650. pgd_t *pg_dir;
  651. pmd_t *pmd;
  652. pte_t *pg_table;
  653. unsigned long start_pmd;
  654. unsigned long start_pte;
  655. unsigned long address;
  656. unsigned long hpux_gw_page_addr;
  657. /* FIXME: This is 'const' in order to trick the compiler
  658. into not treating it as DP-relative data. */
  659. extern void * const hpux_gateway_page;
  660. hpux_gw_page_addr = HPUX_GATEWAY_ADDR & PAGE_MASK;
  661. /*
  662. * Setup HP-UX Gateway page.
  663. *
  664. * The HP-UX gateway page resides in the user address space,
  665. * so it needs to be aliased into each process.
  666. */
  667. pg_dir = pgd_offset(mm,hpux_gw_page_addr);
  668. #if PTRS_PER_PMD == 1
  669. start_pmd = 0;
  670. #else
  671. start_pmd = ((hpux_gw_page_addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  672. #endif
  673. start_pte = ((hpux_gw_page_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  674. address = __pa(&hpux_gateway_page);
  675. #if PTRS_PER_PMD == 1
  676. pmd = (pmd_t *)__pa(pg_dir);
  677. #else
  678. pmd = (pmd_t *) pgd_address(*pg_dir);
  679. /*
  680. * pmd is physical at this point
  681. */
  682. if (!pmd) {
  683. pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL);
  684. pmd = (pmd_t *) __pa(pmd);
  685. }
  686. __pgd_val_set(*pg_dir, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pmd);
  687. #endif
  688. /* now change pmd to kernel virtual addresses */
  689. pmd = (pmd_t *)__va(pmd) + start_pmd;
  690. /*
  691. * pg_table is physical at this point
  692. */
  693. pg_table = (pte_t *) pmd_address(*pmd);
  694. if (!pg_table)
  695. pg_table = (pte_t *) __pa(get_zeroed_page(GFP_KERNEL));
  696. __pmd_val_set(*pmd, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pg_table);
  697. /* now change pg_table to kernel virtual addresses */
  698. pg_table = (pte_t *) __va(pg_table) + start_pte;
  699. set_pte(pg_table, __mk_pte(address, PAGE_GATEWAY));
  700. }
  701. EXPORT_SYMBOL(map_hpux_gateway_page);
  702. #endif
  703. void __init paging_init(void)
  704. {
  705. int i;
  706. setup_bootmem();
  707. pagetable_init();
  708. gateway_init();
  709. flush_cache_all_local(); /* start with known state */
  710. flush_tlb_all_local(NULL);
  711. for (i = 0; i < npmem_ranges; i++) {
  712. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  713. zones_size[ZONE_NORMAL] = pmem_ranges[i].pages;
  714. #ifdef CONFIG_DISCONTIGMEM
  715. /* Need to initialize the pfnnid_map before we can initialize
  716. the zone */
  717. {
  718. int j;
  719. for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
  720. j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
  721. j++) {
  722. pfnnid_map[j] = i;
  723. }
  724. }
  725. #endif
  726. free_area_init_node(i, zones_size,
  727. pmem_ranges[i].start_pfn, NULL);
  728. }
  729. }
  730. #ifdef CONFIG_PA20
  731. /*
  732. * Currently, all PA20 chips have 18 bit protection IDs, which is the
  733. * limiting factor (space ids are 32 bits).
  734. */
  735. #define NR_SPACE_IDS 262144
  736. #else
  737. /*
  738. * Currently we have a one-to-one relationship between space IDs and
  739. * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
  740. * support 15 bit protection IDs, so that is the limiting factor.
  741. * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's
  742. * probably not worth the effort for a special case here.
  743. */
  744. #define NR_SPACE_IDS 32768
  745. #endif /* !CONFIG_PA20 */
  746. #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
  747. #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long)))
  748. static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
  749. static unsigned long dirty_space_id[SID_ARRAY_SIZE];
  750. static unsigned long space_id_index;
  751. static unsigned long free_space_ids = NR_SPACE_IDS - 1;
  752. static unsigned long dirty_space_ids = 0;
  753. static DEFINE_SPINLOCK(sid_lock);
  754. unsigned long alloc_sid(void)
  755. {
  756. unsigned long index;
  757. spin_lock(&sid_lock);
  758. if (free_space_ids == 0) {
  759. if (dirty_space_ids != 0) {
  760. spin_unlock(&sid_lock);
  761. flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
  762. spin_lock(&sid_lock);
  763. }
  764. BUG_ON(free_space_ids == 0);
  765. }
  766. free_space_ids--;
  767. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  768. space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
  769. space_id_index = index;
  770. spin_unlock(&sid_lock);
  771. return index << SPACEID_SHIFT;
  772. }
  773. void free_sid(unsigned long spaceid)
  774. {
  775. unsigned long index = spaceid >> SPACEID_SHIFT;
  776. unsigned long *dirty_space_offset;
  777. dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
  778. index &= (BITS_PER_LONG - 1);
  779. spin_lock(&sid_lock);
  780. BUG_ON(*dirty_space_offset & (1L << index)); /* attempt to free space id twice */
  781. *dirty_space_offset |= (1L << index);
  782. dirty_space_ids++;
  783. spin_unlock(&sid_lock);
  784. }
  785. #ifdef CONFIG_SMP
  786. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  787. {
  788. int i;
  789. /* NOTE: sid_lock must be held upon entry */
  790. *ndirtyptr = dirty_space_ids;
  791. if (dirty_space_ids != 0) {
  792. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  793. dirty_array[i] = dirty_space_id[i];
  794. dirty_space_id[i] = 0;
  795. }
  796. dirty_space_ids = 0;
  797. }
  798. return;
  799. }
  800. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  801. {
  802. int i;
  803. /* NOTE: sid_lock must be held upon entry */
  804. if (ndirty != 0) {
  805. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  806. space_id[i] ^= dirty_array[i];
  807. }
  808. free_space_ids += ndirty;
  809. space_id_index = 0;
  810. }
  811. }
  812. #else /* CONFIG_SMP */
  813. static void recycle_sids(void)
  814. {
  815. int i;
  816. /* NOTE: sid_lock must be held upon entry */
  817. if (dirty_space_ids != 0) {
  818. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  819. space_id[i] ^= dirty_space_id[i];
  820. dirty_space_id[i] = 0;
  821. }
  822. free_space_ids += dirty_space_ids;
  823. dirty_space_ids = 0;
  824. space_id_index = 0;
  825. }
  826. }
  827. #endif
  828. /*
  829. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  830. * purged, we can safely reuse the space ids that were released but
  831. * not flushed from the tlb.
  832. */
  833. #ifdef CONFIG_SMP
  834. static unsigned long recycle_ndirty;
  835. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  836. static unsigned int recycle_inuse;
  837. void flush_tlb_all(void)
  838. {
  839. int do_recycle;
  840. do_recycle = 0;
  841. spin_lock(&sid_lock);
  842. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  843. BUG_ON(recycle_inuse); /* FIXME: Use a semaphore/wait queue here */
  844. get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
  845. recycle_inuse++;
  846. do_recycle++;
  847. }
  848. spin_unlock(&sid_lock);
  849. on_each_cpu(flush_tlb_all_local, NULL, 1);
  850. if (do_recycle) {
  851. spin_lock(&sid_lock);
  852. recycle_sids(recycle_ndirty,recycle_dirty_array);
  853. recycle_inuse = 0;
  854. spin_unlock(&sid_lock);
  855. }
  856. }
  857. #else
  858. void flush_tlb_all(void)
  859. {
  860. spin_lock(&sid_lock);
  861. flush_tlb_all_local(NULL);
  862. recycle_sids();
  863. spin_unlock(&sid_lock);
  864. }
  865. #endif
  866. #ifdef CONFIG_BLK_DEV_INITRD
  867. void free_initrd_mem(unsigned long start, unsigned long end)
  868. {
  869. if (start >= end)
  870. return;
  871. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  872. for (; start < end; start += PAGE_SIZE) {
  873. ClearPageReserved(virt_to_page(start));
  874. init_page_count(virt_to_page(start));
  875. free_page(start);
  876. num_physpages++;
  877. totalram_pages++;
  878. }
  879. }
  880. #endif