init.c 26 KB

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