init.c 25 KB

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