init.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. *
  10. */
  11. #include <linux/config.h>
  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. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  28. extern char _text; /* start of kernel code, defined by linker */
  29. extern int data_start;
  30. extern char _end; /* end of BSS, defined by linker */
  31. extern char __init_begin, __init_end;
  32. #ifdef CONFIG_DISCONTIGMEM
  33. struct node_map_data node_data[MAX_NUMNODES] __read_mostly;
  34. bootmem_data_t bmem_data[MAX_NUMNODES] __read_mostly;
  35. unsigned char pfnnid_map[PFNNID_MAP_MAX] __read_mostly;
  36. #endif
  37. static struct resource data_resource = {
  38. .name = "Kernel data",
  39. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  40. };
  41. static struct resource code_resource = {
  42. .name = "Kernel code",
  43. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  44. };
  45. static struct resource pdcdata_resource = {
  46. .name = "PDC data (Page Zero)",
  47. .start = 0,
  48. .end = 0x9ff,
  49. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  50. };
  51. static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __read_mostly;
  52. /* The following array is initialized from the firmware specific
  53. * information retrieved in kernel/inventory.c.
  54. */
  55. physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __read_mostly;
  56. int npmem_ranges __read_mostly;
  57. #ifdef __LP64__
  58. #define MAX_MEM (~0UL)
  59. #else /* !__LP64__ */
  60. #define MAX_MEM (3584U*1024U*1024U)
  61. #endif /* !__LP64__ */
  62. static unsigned long mem_limit __read_mostly = MAX_MEM;
  63. static void __init mem_limit_func(void)
  64. {
  65. char *cp, *end;
  66. unsigned long limit;
  67. extern char saved_command_line[];
  68. /* We need this before __setup() functions are called */
  69. limit = MAX_MEM;
  70. for (cp = saved_command_line; *cp; ) {
  71. if (memcmp(cp, "mem=", 4) == 0) {
  72. cp += 4;
  73. limit = memparse(cp, &end);
  74. if (end != cp)
  75. break;
  76. cp = end;
  77. } else {
  78. while (*cp != ' ' && *cp)
  79. ++cp;
  80. while (*cp == ' ')
  81. ++cp;
  82. }
  83. }
  84. if (limit < mem_limit)
  85. mem_limit = limit;
  86. }
  87. #define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
  88. static void __init setup_bootmem(void)
  89. {
  90. unsigned long bootmap_size;
  91. unsigned long mem_max;
  92. unsigned long bootmap_pages;
  93. unsigned long bootmap_start_pfn;
  94. unsigned long bootmap_pfn;
  95. #ifndef CONFIG_DISCONTIGMEM
  96. physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
  97. int npmem_holes;
  98. #endif
  99. int i, sysram_resource_count;
  100. disable_sr_hashing(); /* Turn off space register hashing */
  101. /*
  102. * Sort the ranges. Since the number of ranges is typically
  103. * small, and performance is not an issue here, just do
  104. * a simple insertion sort.
  105. */
  106. for (i = 1; i < npmem_ranges; i++) {
  107. int j;
  108. for (j = i; j > 0; j--) {
  109. unsigned long tmp;
  110. if (pmem_ranges[j-1].start_pfn <
  111. pmem_ranges[j].start_pfn) {
  112. break;
  113. }
  114. tmp = pmem_ranges[j-1].start_pfn;
  115. pmem_ranges[j-1].start_pfn = pmem_ranges[j].start_pfn;
  116. pmem_ranges[j].start_pfn = tmp;
  117. tmp = pmem_ranges[j-1].pages;
  118. pmem_ranges[j-1].pages = pmem_ranges[j].pages;
  119. pmem_ranges[j].pages = tmp;
  120. }
  121. }
  122. #ifndef CONFIG_DISCONTIGMEM
  123. /*
  124. * Throw out ranges that are too far apart (controlled by
  125. * MAX_GAP).
  126. */
  127. for (i = 1; i < npmem_ranges; i++) {
  128. if (pmem_ranges[i].start_pfn -
  129. (pmem_ranges[i-1].start_pfn +
  130. pmem_ranges[i-1].pages) > MAX_GAP) {
  131. npmem_ranges = i;
  132. printk("Large gap in memory detected (%ld pages). "
  133. "Consider turning on CONFIG_DISCONTIGMEM\n",
  134. pmem_ranges[i].start_pfn -
  135. (pmem_ranges[i-1].start_pfn +
  136. pmem_ranges[i-1].pages));
  137. break;
  138. }
  139. }
  140. #endif
  141. if (npmem_ranges > 1) {
  142. /* Print the memory ranges */
  143. printk(KERN_INFO "Memory Ranges:\n");
  144. for (i = 0; i < npmem_ranges; i++) {
  145. unsigned long start;
  146. unsigned long size;
  147. size = (pmem_ranges[i].pages << PAGE_SHIFT);
  148. start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
  149. printk(KERN_INFO "%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
  150. i,start, start + (size - 1), size >> 20);
  151. }
  152. }
  153. sysram_resource_count = npmem_ranges;
  154. for (i = 0; i < sysram_resource_count; i++) {
  155. struct resource *res = &sysram_resources[i];
  156. res->name = "System RAM";
  157. res->start = pmem_ranges[i].start_pfn << PAGE_SHIFT;
  158. res->end = res->start + (pmem_ranges[i].pages << PAGE_SHIFT)-1;
  159. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  160. request_resource(&iomem_resource, res);
  161. }
  162. /*
  163. * For 32 bit kernels we limit the amount of memory we can
  164. * support, in order to preserve enough kernel address space
  165. * for other purposes. For 64 bit kernels we don't normally
  166. * limit the memory, but this mechanism can be used to
  167. * artificially limit the amount of memory (and it is written
  168. * to work with multiple memory ranges).
  169. */
  170. mem_limit_func(); /* check for "mem=" argument */
  171. mem_max = 0;
  172. num_physpages = 0;
  173. for (i = 0; i < npmem_ranges; i++) {
  174. unsigned long rsize;
  175. rsize = pmem_ranges[i].pages << PAGE_SHIFT;
  176. if ((mem_max + rsize) > mem_limit) {
  177. printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
  178. if (mem_max == mem_limit)
  179. npmem_ranges = i;
  180. else {
  181. pmem_ranges[i].pages = (mem_limit >> PAGE_SHIFT)
  182. - (mem_max >> PAGE_SHIFT);
  183. npmem_ranges = i + 1;
  184. mem_max = mem_limit;
  185. }
  186. num_physpages += pmem_ranges[i].pages;
  187. break;
  188. }
  189. num_physpages += pmem_ranges[i].pages;
  190. mem_max += rsize;
  191. }
  192. printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20);
  193. #ifndef CONFIG_DISCONTIGMEM
  194. /* Merge the ranges, keeping track of the holes */
  195. {
  196. unsigned long end_pfn;
  197. unsigned long hole_pages;
  198. npmem_holes = 0;
  199. end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
  200. for (i = 1; i < npmem_ranges; i++) {
  201. hole_pages = pmem_ranges[i].start_pfn - end_pfn;
  202. if (hole_pages) {
  203. pmem_holes[npmem_holes].start_pfn = end_pfn;
  204. pmem_holes[npmem_holes++].pages = hole_pages;
  205. end_pfn += hole_pages;
  206. }
  207. end_pfn += pmem_ranges[i].pages;
  208. }
  209. pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
  210. npmem_ranges = 1;
  211. }
  212. #endif
  213. bootmap_pages = 0;
  214. for (i = 0; i < npmem_ranges; i++)
  215. bootmap_pages += bootmem_bootmap_pages(pmem_ranges[i].pages);
  216. bootmap_start_pfn = PAGE_ALIGN(__pa((unsigned long) &_end)) >> PAGE_SHIFT;
  217. #ifdef CONFIG_DISCONTIGMEM
  218. for (i = 0; i < MAX_PHYSMEM_RANGES; i++) {
  219. memset(NODE_DATA(i), 0, sizeof(pg_data_t));
  220. NODE_DATA(i)->bdata = &bmem_data[i];
  221. }
  222. memset(pfnnid_map, 0xff, sizeof(pfnnid_map));
  223. for (i = 0; i < npmem_ranges; i++)
  224. node_set_online(i);
  225. #endif
  226. /*
  227. * Initialize and free the full range of memory in each range.
  228. * Note that the only writing these routines do are to the bootmap,
  229. * and we've made sure to locate the bootmap properly so that they
  230. * won't be writing over anything important.
  231. */
  232. bootmap_pfn = bootmap_start_pfn;
  233. max_pfn = 0;
  234. for (i = 0; i < npmem_ranges; i++) {
  235. unsigned long start_pfn;
  236. unsigned long npages;
  237. start_pfn = pmem_ranges[i].start_pfn;
  238. npages = pmem_ranges[i].pages;
  239. bootmap_size = init_bootmem_node(NODE_DATA(i),
  240. bootmap_pfn,
  241. start_pfn,
  242. (start_pfn + npages) );
  243. free_bootmem_node(NODE_DATA(i),
  244. (start_pfn << PAGE_SHIFT),
  245. (npages << PAGE_SHIFT) );
  246. bootmap_pfn += (bootmap_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  247. if ((start_pfn + npages) > max_pfn)
  248. max_pfn = start_pfn + npages;
  249. }
  250. /* IOMMU is always used to access "high mem" on those boxes
  251. * that can support enough mem that a PCI device couldn't
  252. * directly DMA to any physical addresses.
  253. * ISA DMA support will need to revisit this.
  254. */
  255. max_low_pfn = max_pfn;
  256. if ((bootmap_pfn - bootmap_start_pfn) != bootmap_pages) {
  257. printk(KERN_WARNING "WARNING! bootmap sizing is messed up!\n");
  258. BUG();
  259. }
  260. /* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
  261. #define PDC_CONSOLE_IO_IODC_SIZE 32768
  262. reserve_bootmem_node(NODE_DATA(0), 0UL,
  263. (unsigned long)(PAGE0->mem_free + PDC_CONSOLE_IO_IODC_SIZE));
  264. reserve_bootmem_node(NODE_DATA(0),__pa((unsigned long)&_text),
  265. (unsigned long)(&_end - &_text));
  266. reserve_bootmem_node(NODE_DATA(0), (bootmap_start_pfn << PAGE_SHIFT),
  267. ((bootmap_pfn - bootmap_start_pfn) << PAGE_SHIFT));
  268. #ifndef CONFIG_DISCONTIGMEM
  269. /* reserve the holes */
  270. for (i = 0; i < npmem_holes; i++) {
  271. reserve_bootmem_node(NODE_DATA(0),
  272. (pmem_holes[i].start_pfn << PAGE_SHIFT),
  273. (pmem_holes[i].pages << PAGE_SHIFT));
  274. }
  275. #endif
  276. #ifdef CONFIG_BLK_DEV_INITRD
  277. if (initrd_start) {
  278. printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
  279. if (__pa(initrd_start) < mem_max) {
  280. unsigned long initrd_reserve;
  281. if (__pa(initrd_end) > mem_max) {
  282. initrd_reserve = mem_max - __pa(initrd_start);
  283. } else {
  284. initrd_reserve = initrd_end - initrd_start;
  285. }
  286. initrd_below_start_ok = 1;
  287. printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
  288. reserve_bootmem_node(NODE_DATA(0),__pa(initrd_start), initrd_reserve);
  289. }
  290. }
  291. #endif
  292. data_resource.start = virt_to_phys(&data_start);
  293. data_resource.end = virt_to_phys(&_end)-1;
  294. code_resource.start = virt_to_phys(&_text);
  295. code_resource.end = virt_to_phys(&data_start)-1;
  296. /* We don't know which region the kernel will be in, so try
  297. * all of them.
  298. */
  299. for (i = 0; i < sysram_resource_count; i++) {
  300. struct resource *res = &sysram_resources[i];
  301. request_resource(res, &code_resource);
  302. request_resource(res, &data_resource);
  303. }
  304. request_resource(&sysram_resources[0], &pdcdata_resource);
  305. }
  306. void free_initmem(void)
  307. {
  308. unsigned long addr;
  309. printk(KERN_INFO "Freeing unused kernel memory: ");
  310. #ifdef CONFIG_DEBUG_KERNEL
  311. /* Attempt to catch anyone trying to execute code here
  312. * by filling the page with BRK insns.
  313. *
  314. * If we disable interrupts for all CPUs, then IPI stops working.
  315. * Kinda breaks the global cache flushing.
  316. */
  317. local_irq_disable();
  318. memset(&__init_begin, 0x00,
  319. (unsigned long)&__init_end - (unsigned long)&__init_begin);
  320. flush_data_cache();
  321. asm volatile("sync" : : );
  322. flush_icache_range((unsigned long)&__init_begin, (unsigned long)&__init_end);
  323. asm volatile("sync" : : );
  324. local_irq_enable();
  325. #endif
  326. addr = (unsigned long)(&__init_begin);
  327. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  328. ClearPageReserved(virt_to_page(addr));
  329. set_page_count(virt_to_page(addr), 1);
  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", (unsigned long)(&__init_end - &__init_begin) >> 10);
  337. }
  338. #ifdef CONFIG_DEBUG_RODATA
  339. void mark_rodata_ro(void)
  340. {
  341. extern char __start_rodata, __end_rodata;
  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] ", zl->zones[k]->zone_pgdat->node_id, 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 (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. pte = __mk_pte(address, pgprot);
  532. if (address >= end_paddr)
  533. pte_val(pte) = 0;
  534. set_pte(pg_table, pte);
  535. address += PAGE_SIZE;
  536. }
  537. start_pte = 0;
  538. if (address >= end_paddr)
  539. break;
  540. }
  541. start_pmd = 0;
  542. }
  543. }
  544. /*
  545. * pagetable_init() sets up the page tables
  546. *
  547. * Note that gateway_init() places the Linux gateway page at page 0.
  548. * Since gateway pages cannot be dereferenced this has the desirable
  549. * side effect of trapping those pesky NULL-reference errors in the
  550. * kernel.
  551. */
  552. static void __init pagetable_init(void)
  553. {
  554. int range;
  555. /* Map each physical memory range to its kernel vaddr */
  556. for (range = 0; range < npmem_ranges; range++) {
  557. unsigned long start_paddr;
  558. unsigned long end_paddr;
  559. unsigned long size;
  560. start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
  561. end_paddr = start_paddr + (pmem_ranges[range].pages << PAGE_SHIFT);
  562. size = pmem_ranges[range].pages << PAGE_SHIFT;
  563. map_pages((unsigned long)__va(start_paddr), start_paddr,
  564. size, PAGE_KERNEL);
  565. }
  566. #ifdef CONFIG_BLK_DEV_INITRD
  567. if (initrd_end && initrd_end > mem_limit) {
  568. printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
  569. map_pages(initrd_start, __pa(initrd_start),
  570. initrd_end - initrd_start, PAGE_KERNEL);
  571. }
  572. #endif
  573. empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
  574. memset(empty_zero_page, 0, PAGE_SIZE);
  575. }
  576. static void __init gateway_init(void)
  577. {
  578. unsigned long linux_gateway_page_addr;
  579. /* FIXME: This is 'const' in order to trick the compiler
  580. into not treating it as DP-relative data. */
  581. extern void * const linux_gateway_page;
  582. linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
  583. /*
  584. * Setup Linux Gateway page.
  585. *
  586. * The Linux gateway page will reside in kernel space (on virtual
  587. * page 0), so it doesn't need to be aliased into user space.
  588. */
  589. map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
  590. PAGE_SIZE, PAGE_GATEWAY);
  591. }
  592. #ifdef CONFIG_HPUX
  593. void
  594. map_hpux_gateway_page(struct task_struct *tsk, struct mm_struct *mm)
  595. {
  596. pgd_t *pg_dir;
  597. pmd_t *pmd;
  598. pte_t *pg_table;
  599. unsigned long start_pmd;
  600. unsigned long start_pte;
  601. unsigned long address;
  602. unsigned long hpux_gw_page_addr;
  603. /* FIXME: This is 'const' in order to trick the compiler
  604. into not treating it as DP-relative data. */
  605. extern void * const hpux_gateway_page;
  606. hpux_gw_page_addr = HPUX_GATEWAY_ADDR & PAGE_MASK;
  607. /*
  608. * Setup HP-UX Gateway page.
  609. *
  610. * The HP-UX gateway page resides in the user address space,
  611. * so it needs to be aliased into each process.
  612. */
  613. pg_dir = pgd_offset(mm,hpux_gw_page_addr);
  614. #if PTRS_PER_PMD == 1
  615. start_pmd = 0;
  616. #else
  617. start_pmd = ((hpux_gw_page_addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  618. #endif
  619. start_pte = ((hpux_gw_page_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  620. address = __pa(&hpux_gateway_page);
  621. #if PTRS_PER_PMD == 1
  622. pmd = (pmd_t *)__pa(pg_dir);
  623. #else
  624. pmd = (pmd_t *) pgd_address(*pg_dir);
  625. /*
  626. * pmd is physical at this point
  627. */
  628. if (!pmd) {
  629. pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL);
  630. pmd = (pmd_t *) __pa(pmd);
  631. }
  632. __pgd_val_set(*pg_dir, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pmd);
  633. #endif
  634. /* now change pmd to kernel virtual addresses */
  635. pmd = (pmd_t *)__va(pmd) + start_pmd;
  636. /*
  637. * pg_table is physical at this point
  638. */
  639. pg_table = (pte_t *) pmd_address(*pmd);
  640. if (!pg_table)
  641. pg_table = (pte_t *) __pa(get_zeroed_page(GFP_KERNEL));
  642. __pmd_val_set(*pmd, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pg_table);
  643. /* now change pg_table to kernel virtual addresses */
  644. pg_table = (pte_t *) __va(pg_table) + start_pte;
  645. set_pte(pg_table, __mk_pte(address, PAGE_GATEWAY));
  646. }
  647. EXPORT_SYMBOL(map_hpux_gateway_page);
  648. #endif
  649. void __init paging_init(void)
  650. {
  651. int i;
  652. setup_bootmem();
  653. pagetable_init();
  654. gateway_init();
  655. flush_cache_all_local(); /* start with known state */
  656. flush_tlb_all_local(NULL);
  657. for (i = 0; i < npmem_ranges; i++) {
  658. unsigned long zones_size[MAX_NR_ZONES] = { 0, 0, 0 };
  659. /* We have an IOMMU, so all memory can go into a single
  660. ZONE_DMA zone. */
  661. zones_size[ZONE_DMA] = 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. if (free_space_ids == 0)
  713. BUG();
  714. }
  715. free_space_ids--;
  716. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  717. space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
  718. space_id_index = index;
  719. spin_unlock(&sid_lock);
  720. return index << SPACEID_SHIFT;
  721. }
  722. void free_sid(unsigned long spaceid)
  723. {
  724. unsigned long index = spaceid >> SPACEID_SHIFT;
  725. unsigned long *dirty_space_offset;
  726. dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
  727. index &= (BITS_PER_LONG - 1);
  728. spin_lock(&sid_lock);
  729. if (*dirty_space_offset & (1L << index))
  730. BUG(); /* attempt to free space id twice */
  731. *dirty_space_offset |= (1L << index);
  732. dirty_space_ids++;
  733. spin_unlock(&sid_lock);
  734. }
  735. #ifdef CONFIG_SMP
  736. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  737. {
  738. int i;
  739. /* NOTE: sid_lock must be held upon entry */
  740. *ndirtyptr = dirty_space_ids;
  741. if (dirty_space_ids != 0) {
  742. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  743. dirty_array[i] = dirty_space_id[i];
  744. dirty_space_id[i] = 0;
  745. }
  746. dirty_space_ids = 0;
  747. }
  748. return;
  749. }
  750. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  751. {
  752. int i;
  753. /* NOTE: sid_lock must be held upon entry */
  754. if (ndirty != 0) {
  755. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  756. space_id[i] ^= dirty_array[i];
  757. }
  758. free_space_ids += ndirty;
  759. space_id_index = 0;
  760. }
  761. }
  762. #else /* CONFIG_SMP */
  763. static void recycle_sids(void)
  764. {
  765. int i;
  766. /* NOTE: sid_lock must be held upon entry */
  767. if (dirty_space_ids != 0) {
  768. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  769. space_id[i] ^= dirty_space_id[i];
  770. dirty_space_id[i] = 0;
  771. }
  772. free_space_ids += dirty_space_ids;
  773. dirty_space_ids = 0;
  774. space_id_index = 0;
  775. }
  776. }
  777. #endif
  778. /*
  779. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  780. * purged, we can safely reuse the space ids that were released but
  781. * not flushed from the tlb.
  782. */
  783. #ifdef CONFIG_SMP
  784. static unsigned long recycle_ndirty;
  785. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  786. static unsigned int recycle_inuse = 0;
  787. void flush_tlb_all(void)
  788. {
  789. int do_recycle;
  790. do_recycle = 0;
  791. spin_lock(&sid_lock);
  792. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  793. if (recycle_inuse) {
  794. BUG(); /* FIXME: Use a semaphore/wait queue here */
  795. }
  796. get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
  797. recycle_inuse++;
  798. do_recycle++;
  799. }
  800. spin_unlock(&sid_lock);
  801. on_each_cpu(flush_tlb_all_local, NULL, 1, 1);
  802. if (do_recycle) {
  803. spin_lock(&sid_lock);
  804. recycle_sids(recycle_ndirty,recycle_dirty_array);
  805. recycle_inuse = 0;
  806. spin_unlock(&sid_lock);
  807. }
  808. }
  809. #else
  810. void flush_tlb_all(void)
  811. {
  812. spin_lock(&sid_lock);
  813. flush_tlb_all_local(NULL);
  814. recycle_sids();
  815. spin_unlock(&sid_lock);
  816. }
  817. #endif
  818. #ifdef CONFIG_BLK_DEV_INITRD
  819. void free_initrd_mem(unsigned long start, unsigned long end)
  820. {
  821. #if 0
  822. if (start < end)
  823. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  824. for (; start < end; start += PAGE_SIZE) {
  825. ClearPageReserved(virt_to_page(start));
  826. set_page_count(virt_to_page(start), 1);
  827. free_page(start);
  828. num_physpages++;
  829. totalram_pages++;
  830. }
  831. #endif
  832. }
  833. #endif