init.c 28 KB

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