init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * linux/arch/parisc/mm/init.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright 1999 SuSE GmbH
  6. * changed by Philipp Rumpf
  7. * Copyright 1999 Philipp Rumpf (prumpf@tux.org)
  8. * Copyright 2004 Randolph Chung (tausq@debian.org)
  9. * Copyright 2006 Helge Deller (deller@gmx.de)
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/pci.h> /* for hppa_dma_ops and pcxl_dma_ops */
  18. #include <linux/initrd.h>
  19. #include <linux/swap.h>
  20. #include <linux/unistd.h>
  21. #include <linux/nodemask.h> /* for node_online_map */
  22. #include <linux/pagemap.h> /* for release_pages and page_cache_release */
  23. #include <asm/pgalloc.h>
  24. #include <asm/tlb.h>
  25. #include <asm/pdc_chassis.h>
  26. #include <asm/mmzone.h>
  27. #include <asm/sections.h>
  28. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  29. extern int data_start;
  30. #ifdef CONFIG_DISCONTIGMEM
  31. struct node_map_data node_data[MAX_NUMNODES] __read_mostly;
  32. bootmem_data_t bmem_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 __LP64__
  56. #define MAX_MEM (~0UL)
  57. #else /* !__LP64__ */
  58. #define MAX_MEM (3584U*1024U*1024U)
  59. #endif /* !__LP64__ */
  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. extern char saved_command_line[];
  66. /* We need this before __setup() functions are called */
  67. limit = MAX_MEM;
  68. for (cp = saved_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 = &bmem_data[i];
  219. }
  220. memset(pfnnid_map, 0xff, sizeof(pfnnid_map));
  221. for (i = 0; i < npmem_ranges; i++)
  222. node_set_online(i);
  223. #endif
  224. /*
  225. * Initialize and free the full range of memory in each range.
  226. * Note that the only writing these routines do are to the bootmap,
  227. * and we've made sure to locate the bootmap properly so that they
  228. * won't be writing over anything important.
  229. */
  230. bootmap_pfn = bootmap_start_pfn;
  231. max_pfn = 0;
  232. for (i = 0; i < npmem_ranges; i++) {
  233. unsigned long start_pfn;
  234. unsigned long npages;
  235. start_pfn = pmem_ranges[i].start_pfn;
  236. npages = pmem_ranges[i].pages;
  237. bootmap_size = init_bootmem_node(NODE_DATA(i),
  238. bootmap_pfn,
  239. start_pfn,
  240. (start_pfn + npages) );
  241. free_bootmem_node(NODE_DATA(i),
  242. (start_pfn << PAGE_SHIFT),
  243. (npages << PAGE_SHIFT) );
  244. bootmap_pfn += (bootmap_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  245. if ((start_pfn + npages) > max_pfn)
  246. max_pfn = start_pfn + npages;
  247. }
  248. /* IOMMU is always used to access "high mem" on those boxes
  249. * that can support enough mem that a PCI device couldn't
  250. * directly DMA to any physical addresses.
  251. * ISA DMA support will need to revisit this.
  252. */
  253. max_low_pfn = max_pfn;
  254. if ((bootmap_pfn - bootmap_start_pfn) != bootmap_pages) {
  255. printk(KERN_WARNING "WARNING! bootmap sizing is messed up!\n");
  256. BUG();
  257. }
  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 + PDC_CONSOLE_IO_IODC_SIZE));
  262. reserve_bootmem_node(NODE_DATA(0), __pa((unsigned long)_text),
  263. (unsigned long)(_end - _text));
  264. reserve_bootmem_node(NODE_DATA(0), (bootmap_start_pfn << PAGE_SHIFT),
  265. ((bootmap_pfn - bootmap_start_pfn) << PAGE_SHIFT));
  266. #ifndef CONFIG_DISCONTIGMEM
  267. /* reserve the holes */
  268. for (i = 0; i < npmem_holes; i++) {
  269. reserve_bootmem_node(NODE_DATA(0),
  270. (pmem_holes[i].start_pfn << PAGE_SHIFT),
  271. (pmem_holes[i].pages << PAGE_SHIFT));
  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), initrd_reserve);
  287. }
  288. }
  289. #endif
  290. data_resource.start = virt_to_phys(&data_start);
  291. data_resource.end = virt_to_phys(_end) - 1;
  292. code_resource.start = virt_to_phys(_text);
  293. code_resource.end = virt_to_phys(&data_start)-1;
  294. /* We don't know which region the kernel will be in, so try
  295. * all of them.
  296. */
  297. for (i = 0; i < sysram_resource_count; i++) {
  298. struct resource *res = &sysram_resources[i];
  299. request_resource(res, &code_resource);
  300. request_resource(res, &data_resource);
  301. }
  302. request_resource(&sysram_resources[0], &pdcdata_resource);
  303. }
  304. void free_initmem(void)
  305. {
  306. unsigned long addr, init_begin, init_end;
  307. printk(KERN_INFO "Freeing unused kernel memory: ");
  308. #ifdef CONFIG_DEBUG_KERNEL
  309. /* Attempt to catch anyone trying to execute code here
  310. * by filling the page with BRK insns.
  311. *
  312. * If we disable interrupts for all CPUs, then IPI stops working.
  313. * Kinda breaks the global cache flushing.
  314. */
  315. local_irq_disable();
  316. memset(__init_begin, 0x00,
  317. (unsigned long)__init_end - (unsigned long)__init_begin);
  318. flush_data_cache();
  319. asm volatile("sync" : : );
  320. flush_icache_range((unsigned long)__init_begin, (unsigned long)__init_end);
  321. asm volatile("sync" : : );
  322. local_irq_enable();
  323. #endif
  324. /* align __init_begin and __init_end to page size,
  325. ignoring linker script where we might have tried to save RAM */
  326. init_begin = PAGE_ALIGN((unsigned long)(__init_begin));
  327. init_end = PAGE_ALIGN((unsigned long)(__init_end));
  328. for (addr = init_begin; addr < init_end; addr += PAGE_SIZE) {
  329. ClearPageReserved(virt_to_page(addr));
  330. init_page_count(virt_to_page(addr));
  331. free_page(addr);
  332. num_physpages++;
  333. totalram_pages++;
  334. }
  335. /* set up a new led state on systems shipped LED State panel */
  336. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
  337. printk("%luk freed\n", (init_end - init_begin) >> 10);
  338. }
  339. #ifdef CONFIG_DEBUG_RODATA
  340. void mark_rodata_ro(void)
  341. {
  342. /* rodata memory was already mapped with KERNEL_RO access rights by
  343. pagetable_init() and map_pages(). No need to do additional stuff here */
  344. printk (KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  345. (unsigned long)(__end_rodata - __start_rodata) >> 10);
  346. }
  347. #endif
  348. /*
  349. * Just an arbitrary offset to serve as a "hole" between mapping areas
  350. * (between top of physical memory and a potential pcxl dma mapping
  351. * area, and below the vmalloc mapping area).
  352. *
  353. * The current 32K value just means that there will be a 32K "hole"
  354. * between mapping areas. That means that any out-of-bounds memory
  355. * accesses will hopefully be caught. The vmalloc() routines leaves
  356. * a hole of 4kB between each vmalloced area for the same reason.
  357. */
  358. /* Leave room for gateway page expansion */
  359. #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
  360. #error KERNEL_MAP_START is in gateway reserved region
  361. #endif
  362. #define MAP_START (KERNEL_MAP_START)
  363. #define VM_MAP_OFFSET (32*1024)
  364. #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
  365. & ~(VM_MAP_OFFSET-1)))
  366. void *vmalloc_start __read_mostly;
  367. EXPORT_SYMBOL(vmalloc_start);
  368. #ifdef CONFIG_PA11
  369. unsigned long pcxl_dma_start __read_mostly;
  370. #endif
  371. void __init mem_init(void)
  372. {
  373. high_memory = __va((max_pfn << PAGE_SHIFT));
  374. #ifndef CONFIG_DISCONTIGMEM
  375. max_mapnr = page_to_pfn(virt_to_page(high_memory - 1)) + 1;
  376. totalram_pages += free_all_bootmem();
  377. #else
  378. {
  379. int i;
  380. for (i = 0; i < npmem_ranges; i++)
  381. totalram_pages += free_all_bootmem_node(NODE_DATA(i));
  382. }
  383. #endif
  384. printk(KERN_INFO "Memory: %luk available\n", num_physpages << (PAGE_SHIFT-10));
  385. #ifdef CONFIG_PA11
  386. if (hppa_dma_ops == &pcxl_dma_ops) {
  387. pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
  388. vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start + PCXL_DMA_MAP_SIZE);
  389. } else {
  390. pcxl_dma_start = 0;
  391. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  392. }
  393. #else
  394. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  395. #endif
  396. }
  397. unsigned long *empty_zero_page __read_mostly;
  398. void show_mem(void)
  399. {
  400. int i,free = 0,total = 0,reserved = 0;
  401. int shared = 0, cached = 0;
  402. printk(KERN_INFO "Mem-info:\n");
  403. show_free_areas();
  404. printk(KERN_INFO "Free swap: %6ldkB\n",
  405. nr_swap_pages<<(PAGE_SHIFT-10));
  406. #ifndef CONFIG_DISCONTIGMEM
  407. i = max_mapnr;
  408. while (i-- > 0) {
  409. total++;
  410. if (PageReserved(mem_map+i))
  411. reserved++;
  412. else if (PageSwapCache(mem_map+i))
  413. cached++;
  414. else if (!page_count(&mem_map[i]))
  415. free++;
  416. else
  417. shared += page_count(&mem_map[i]) - 1;
  418. }
  419. #else
  420. for (i = 0; i < npmem_ranges; i++) {
  421. int j;
  422. for (j = node_start_pfn(i); j < node_end_pfn(i); j++) {
  423. struct page *p;
  424. unsigned long flags;
  425. pgdat_resize_lock(NODE_DATA(i), &flags);
  426. p = nid_page_nr(i, j) - node_start_pfn(i);
  427. total++;
  428. if (PageReserved(p))
  429. reserved++;
  430. else if (PageSwapCache(p))
  431. cached++;
  432. else if (!page_count(p))
  433. free++;
  434. else
  435. shared += page_count(p) - 1;
  436. pgdat_resize_unlock(NODE_DATA(i), &flags);
  437. }
  438. }
  439. #endif
  440. printk(KERN_INFO "%d pages of RAM\n", total);
  441. printk(KERN_INFO "%d reserved pages\n", reserved);
  442. printk(KERN_INFO "%d pages shared\n", shared);
  443. printk(KERN_INFO "%d pages swap cached\n", cached);
  444. #ifdef CONFIG_DISCONTIGMEM
  445. {
  446. struct zonelist *zl;
  447. int i, j, k;
  448. for (i = 0; i < npmem_ranges; i++) {
  449. for (j = 0; j < MAX_NR_ZONES; j++) {
  450. zl = NODE_DATA(i)->node_zonelists + j;
  451. printk("Zone list for zone %d on node %d: ", j, i);
  452. for (k = 0; zl->zones[k] != NULL; k++)
  453. printk("[%d/%s] ", zone_to_nid(zl->zones[k]), zl->zones[k]->name);
  454. printk("\n");
  455. }
  456. }
  457. }
  458. #endif
  459. }
  460. static void __init map_pages(unsigned long start_vaddr, unsigned long start_paddr, unsigned long size, pgprot_t pgprot)
  461. {
  462. pgd_t *pg_dir;
  463. pmd_t *pmd;
  464. pte_t *pg_table;
  465. unsigned long end_paddr;
  466. unsigned long start_pmd;
  467. unsigned long start_pte;
  468. unsigned long tmp1;
  469. unsigned long tmp2;
  470. unsigned long address;
  471. unsigned long ro_start;
  472. unsigned long ro_end;
  473. unsigned long fv_addr;
  474. unsigned long gw_addr;
  475. extern const unsigned long fault_vector_20;
  476. extern void * const linux_gateway_page;
  477. ro_start = __pa((unsigned long)_text);
  478. ro_end = __pa((unsigned long)&data_start);
  479. fv_addr = __pa((unsigned long)&fault_vector_20) & PAGE_MASK;
  480. gw_addr = __pa((unsigned long)&linux_gateway_page) & PAGE_MASK;
  481. end_paddr = start_paddr + size;
  482. pg_dir = pgd_offset_k(start_vaddr);
  483. #if PTRS_PER_PMD == 1
  484. start_pmd = 0;
  485. #else
  486. start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  487. #endif
  488. start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  489. address = start_paddr;
  490. while (address < end_paddr) {
  491. #if PTRS_PER_PMD == 1
  492. pmd = (pmd_t *)__pa(pg_dir);
  493. #else
  494. pmd = (pmd_t *)pgd_address(*pg_dir);
  495. /*
  496. * pmd is physical at this point
  497. */
  498. if (!pmd) {
  499. pmd = (pmd_t *) alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE << PMD_ORDER);
  500. pmd = (pmd_t *) __pa(pmd);
  501. }
  502. pgd_populate(NULL, pg_dir, __va(pmd));
  503. #endif
  504. pg_dir++;
  505. /* now change pmd to kernel virtual addresses */
  506. pmd = (pmd_t *)__va(pmd) + start_pmd;
  507. for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++,pmd++) {
  508. /*
  509. * pg_table is physical at this point
  510. */
  511. pg_table = (pte_t *)pmd_address(*pmd);
  512. if (!pg_table) {
  513. pg_table = (pte_t *)
  514. alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE);
  515. pg_table = (pte_t *) __pa(pg_table);
  516. }
  517. pmd_populate_kernel(NULL, pmd, __va(pg_table));
  518. /* now change pg_table to kernel virtual addresses */
  519. pg_table = (pte_t *) __va(pg_table) + start_pte;
  520. for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++,pg_table++) {
  521. pte_t pte;
  522. /*
  523. * Map the fault vector writable so we can
  524. * write the HPMC checksum.
  525. */
  526. #if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
  527. if (address >= ro_start && address < ro_end
  528. && address != fv_addr
  529. && address != gw_addr)
  530. pte = __mk_pte(address, PAGE_KERNEL_RO);
  531. else
  532. #endif
  533. pte = __mk_pte(address, pgprot);
  534. if (address >= end_paddr)
  535. pte_val(pte) = 0;
  536. set_pte(pg_table, pte);
  537. address += PAGE_SIZE;
  538. }
  539. start_pte = 0;
  540. if (address >= end_paddr)
  541. break;
  542. }
  543. start_pmd = 0;
  544. }
  545. }
  546. /*
  547. * pagetable_init() sets up the page tables
  548. *
  549. * Note that gateway_init() places the Linux gateway page at page 0.
  550. * Since gateway pages cannot be dereferenced this has the desirable
  551. * side effect of trapping those pesky NULL-reference errors in the
  552. * kernel.
  553. */
  554. static void __init pagetable_init(void)
  555. {
  556. int range;
  557. /* Map each physical memory range to its kernel vaddr */
  558. for (range = 0; range < npmem_ranges; range++) {
  559. unsigned long start_paddr;
  560. unsigned long end_paddr;
  561. unsigned long size;
  562. start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
  563. end_paddr = start_paddr + (pmem_ranges[range].pages << PAGE_SHIFT);
  564. size = pmem_ranges[range].pages << PAGE_SHIFT;
  565. map_pages((unsigned long)__va(start_paddr), start_paddr,
  566. size, PAGE_KERNEL);
  567. }
  568. #ifdef CONFIG_BLK_DEV_INITRD
  569. if (initrd_end && initrd_end > mem_limit) {
  570. printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
  571. map_pages(initrd_start, __pa(initrd_start),
  572. initrd_end - initrd_start, PAGE_KERNEL);
  573. }
  574. #endif
  575. empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
  576. memset(empty_zero_page, 0, PAGE_SIZE);
  577. }
  578. static void __init gateway_init(void)
  579. {
  580. unsigned long linux_gateway_page_addr;
  581. /* FIXME: This is 'const' in order to trick the compiler
  582. into not treating it as DP-relative data. */
  583. extern void * const linux_gateway_page;
  584. linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
  585. /*
  586. * Setup Linux Gateway page.
  587. *
  588. * The Linux gateway page will reside in kernel space (on virtual
  589. * page 0), so it doesn't need to be aliased into user space.
  590. */
  591. map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
  592. PAGE_SIZE, PAGE_GATEWAY);
  593. }
  594. #ifdef CONFIG_HPUX
  595. void
  596. map_hpux_gateway_page(struct task_struct *tsk, struct mm_struct *mm)
  597. {
  598. pgd_t *pg_dir;
  599. pmd_t *pmd;
  600. pte_t *pg_table;
  601. unsigned long start_pmd;
  602. unsigned long start_pte;
  603. unsigned long address;
  604. unsigned long hpux_gw_page_addr;
  605. /* FIXME: This is 'const' in order to trick the compiler
  606. into not treating it as DP-relative data. */
  607. extern void * const hpux_gateway_page;
  608. hpux_gw_page_addr = HPUX_GATEWAY_ADDR & PAGE_MASK;
  609. /*
  610. * Setup HP-UX Gateway page.
  611. *
  612. * The HP-UX gateway page resides in the user address space,
  613. * so it needs to be aliased into each process.
  614. */
  615. pg_dir = pgd_offset(mm,hpux_gw_page_addr);
  616. #if PTRS_PER_PMD == 1
  617. start_pmd = 0;
  618. #else
  619. start_pmd = ((hpux_gw_page_addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  620. #endif
  621. start_pte = ((hpux_gw_page_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  622. address = __pa(&hpux_gateway_page);
  623. #if PTRS_PER_PMD == 1
  624. pmd = (pmd_t *)__pa(pg_dir);
  625. #else
  626. pmd = (pmd_t *) pgd_address(*pg_dir);
  627. /*
  628. * pmd is physical at this point
  629. */
  630. if (!pmd) {
  631. pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL);
  632. pmd = (pmd_t *) __pa(pmd);
  633. }
  634. __pgd_val_set(*pg_dir, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pmd);
  635. #endif
  636. /* now change pmd to kernel virtual addresses */
  637. pmd = (pmd_t *)__va(pmd) + start_pmd;
  638. /*
  639. * pg_table is physical at this point
  640. */
  641. pg_table = (pte_t *) pmd_address(*pmd);
  642. if (!pg_table)
  643. pg_table = (pte_t *) __pa(get_zeroed_page(GFP_KERNEL));
  644. __pmd_val_set(*pmd, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pg_table);
  645. /* now change pg_table to kernel virtual addresses */
  646. pg_table = (pte_t *) __va(pg_table) + start_pte;
  647. set_pte(pg_table, __mk_pte(address, PAGE_GATEWAY));
  648. }
  649. EXPORT_SYMBOL(map_hpux_gateway_page);
  650. #endif
  651. void __init paging_init(void)
  652. {
  653. int i;
  654. setup_bootmem();
  655. pagetable_init();
  656. gateway_init();
  657. flush_cache_all_local(); /* start with known state */
  658. flush_tlb_all_local(NULL);
  659. for (i = 0; i < npmem_ranges; i++) {
  660. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  661. zones_size[ZONE_NORMAL] = pmem_ranges[i].pages;
  662. #ifdef CONFIG_DISCONTIGMEM
  663. /* Need to initialize the pfnnid_map before we can initialize
  664. the zone */
  665. {
  666. int j;
  667. for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
  668. j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
  669. j++) {
  670. pfnnid_map[j] = i;
  671. }
  672. }
  673. #endif
  674. free_area_init_node(i, NODE_DATA(i), zones_size,
  675. pmem_ranges[i].start_pfn, NULL);
  676. }
  677. }
  678. #ifdef CONFIG_PA20
  679. /*
  680. * Currently, all PA20 chips have 18 bit protection id's, which is the
  681. * limiting factor (space ids are 32 bits).
  682. */
  683. #define NR_SPACE_IDS 262144
  684. #else
  685. /*
  686. * Currently we have a one-to-one relationship between space id's and
  687. * protection id's. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
  688. * support 15 bit protection id's, so that is the limiting factor.
  689. * PCXT' has 18 bit protection id's, but only 16 bit spaceids, so it's
  690. * probably not worth the effort for a special case here.
  691. */
  692. #define NR_SPACE_IDS 32768
  693. #endif /* !CONFIG_PA20 */
  694. #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
  695. #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long)))
  696. static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
  697. static unsigned long dirty_space_id[SID_ARRAY_SIZE];
  698. static unsigned long space_id_index;
  699. static unsigned long free_space_ids = NR_SPACE_IDS - 1;
  700. static unsigned long dirty_space_ids = 0;
  701. static DEFINE_SPINLOCK(sid_lock);
  702. unsigned long alloc_sid(void)
  703. {
  704. unsigned long index;
  705. spin_lock(&sid_lock);
  706. if (free_space_ids == 0) {
  707. if (dirty_space_ids != 0) {
  708. spin_unlock(&sid_lock);
  709. flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
  710. spin_lock(&sid_lock);
  711. }
  712. BUG_ON(free_space_ids == 0);
  713. }
  714. free_space_ids--;
  715. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  716. space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
  717. space_id_index = index;
  718. spin_unlock(&sid_lock);
  719. return index << SPACEID_SHIFT;
  720. }
  721. void free_sid(unsigned long spaceid)
  722. {
  723. unsigned long index = spaceid >> SPACEID_SHIFT;
  724. unsigned long *dirty_space_offset;
  725. dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
  726. index &= (BITS_PER_LONG - 1);
  727. spin_lock(&sid_lock);
  728. BUG_ON(*dirty_space_offset & (1L << index)); /* attempt to free space id twice */
  729. *dirty_space_offset |= (1L << index);
  730. dirty_space_ids++;
  731. spin_unlock(&sid_lock);
  732. }
  733. #ifdef CONFIG_SMP
  734. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  735. {
  736. int i;
  737. /* NOTE: sid_lock must be held upon entry */
  738. *ndirtyptr = dirty_space_ids;
  739. if (dirty_space_ids != 0) {
  740. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  741. dirty_array[i] = dirty_space_id[i];
  742. dirty_space_id[i] = 0;
  743. }
  744. dirty_space_ids = 0;
  745. }
  746. return;
  747. }
  748. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  749. {
  750. int i;
  751. /* NOTE: sid_lock must be held upon entry */
  752. if (ndirty != 0) {
  753. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  754. space_id[i] ^= dirty_array[i];
  755. }
  756. free_space_ids += ndirty;
  757. space_id_index = 0;
  758. }
  759. }
  760. #else /* CONFIG_SMP */
  761. static void recycle_sids(void)
  762. {
  763. int i;
  764. /* NOTE: sid_lock must be held upon entry */
  765. if (dirty_space_ids != 0) {
  766. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  767. space_id[i] ^= dirty_space_id[i];
  768. dirty_space_id[i] = 0;
  769. }
  770. free_space_ids += dirty_space_ids;
  771. dirty_space_ids = 0;
  772. space_id_index = 0;
  773. }
  774. }
  775. #endif
  776. /*
  777. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  778. * purged, we can safely reuse the space ids that were released but
  779. * not flushed from the tlb.
  780. */
  781. #ifdef CONFIG_SMP
  782. static unsigned long recycle_ndirty;
  783. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  784. static unsigned int recycle_inuse;
  785. void flush_tlb_all(void)
  786. {
  787. int do_recycle;
  788. do_recycle = 0;
  789. spin_lock(&sid_lock);
  790. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  791. BUG_ON(recycle_inuse); /* FIXME: Use a semaphore/wait queue here */
  792. get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
  793. recycle_inuse++;
  794. do_recycle++;
  795. }
  796. spin_unlock(&sid_lock);
  797. on_each_cpu(flush_tlb_all_local, NULL, 1, 1);
  798. if (do_recycle) {
  799. spin_lock(&sid_lock);
  800. recycle_sids(recycle_ndirty,recycle_dirty_array);
  801. recycle_inuse = 0;
  802. spin_unlock(&sid_lock);
  803. }
  804. }
  805. #else
  806. void flush_tlb_all(void)
  807. {
  808. spin_lock(&sid_lock);
  809. flush_tlb_all_local(NULL);
  810. recycle_sids();
  811. spin_unlock(&sid_lock);
  812. }
  813. #endif
  814. #ifdef CONFIG_BLK_DEV_INITRD
  815. void free_initrd_mem(unsigned long start, unsigned long end)
  816. {
  817. if (start >= end)
  818. return;
  819. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  820. for (; start < end; start += PAGE_SIZE) {
  821. ClearPageReserved(virt_to_page(start));
  822. init_page_count(virt_to_page(start));
  823. free_page(start);
  824. num_physpages++;
  825. totalram_pages++;
  826. }
  827. }
  828. #endif