init.c 25 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. *
  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. /* FIXME: */
  309. #if 0
  310. printk(KERN_INFO "NOT FREEING INITMEM (%dk)\n",
  311. (&__init_end - &__init_begin) >> 10);
  312. return;
  313. #else
  314. unsigned long addr;
  315. printk(KERN_INFO "Freeing unused kernel memory: ");
  316. #if 1
  317. /* Attempt to catch anyone trying to execute code here
  318. * by filling the page with BRK insns.
  319. *
  320. * If we disable interrupts for all CPUs, then IPI stops working.
  321. * Kinda breaks the global cache flushing.
  322. */
  323. local_irq_disable();
  324. memset(&__init_begin, 0x00,
  325. (unsigned long)&__init_end - (unsigned long)&__init_begin);
  326. flush_data_cache();
  327. asm volatile("sync" : : );
  328. flush_icache_range((unsigned long)&__init_begin, (unsigned long)&__init_end);
  329. asm volatile("sync" : : );
  330. local_irq_enable();
  331. #endif
  332. addr = (unsigned long)(&__init_begin);
  333. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  334. ClearPageReserved(virt_to_page(addr));
  335. set_page_count(virt_to_page(addr), 1);
  336. free_page(addr);
  337. num_physpages++;
  338. totalram_pages++;
  339. }
  340. /* set up a new led state on systems shipped LED State panel */
  341. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
  342. printk("%luk freed\n", (unsigned long)(&__init_end - &__init_begin) >> 10);
  343. #endif
  344. }
  345. /*
  346. * Just an arbitrary offset to serve as a "hole" between mapping areas
  347. * (between top of physical memory and a potential pcxl dma mapping
  348. * area, and below the vmalloc mapping area).
  349. *
  350. * The current 32K value just means that there will be a 32K "hole"
  351. * between mapping areas. That means that any out-of-bounds memory
  352. * accesses will hopefully be caught. The vmalloc() routines leaves
  353. * a hole of 4kB between each vmalloced area for the same reason.
  354. */
  355. /* Leave room for gateway page expansion */
  356. #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
  357. #error KERNEL_MAP_START is in gateway reserved region
  358. #endif
  359. #define MAP_START (KERNEL_MAP_START)
  360. #define VM_MAP_OFFSET (32*1024)
  361. #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
  362. & ~(VM_MAP_OFFSET-1)))
  363. void *vmalloc_start __read_mostly;
  364. EXPORT_SYMBOL(vmalloc_start);
  365. #ifdef CONFIG_PA11
  366. unsigned long pcxl_dma_start __read_mostly;
  367. #endif
  368. void __init mem_init(void)
  369. {
  370. high_memory = __va((max_pfn << PAGE_SHIFT));
  371. #ifndef CONFIG_DISCONTIGMEM
  372. max_mapnr = page_to_pfn(virt_to_page(high_memory - 1)) + 1;
  373. totalram_pages += free_all_bootmem();
  374. #else
  375. {
  376. int i;
  377. for (i = 0; i < npmem_ranges; i++)
  378. totalram_pages += free_all_bootmem_node(NODE_DATA(i));
  379. }
  380. #endif
  381. printk(KERN_INFO "Memory: %luk available\n", num_physpages << (PAGE_SHIFT-10));
  382. #ifdef CONFIG_PA11
  383. if (hppa_dma_ops == &pcxl_dma_ops) {
  384. pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
  385. vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start + PCXL_DMA_MAP_SIZE);
  386. } else {
  387. pcxl_dma_start = 0;
  388. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  389. }
  390. #else
  391. vmalloc_start = SET_MAP_OFFSET(MAP_START);
  392. #endif
  393. }
  394. int do_check_pgt_cache(int low, int high)
  395. {
  396. return 0;
  397. }
  398. unsigned long *empty_zero_page __read_mostly;
  399. void show_mem(void)
  400. {
  401. int i,free = 0,total = 0,reserved = 0;
  402. int shared = 0, cached = 0;
  403. printk(KERN_INFO "Mem-info:\n");
  404. show_free_areas();
  405. printk(KERN_INFO "Free swap: %6ldkB\n",
  406. nr_swap_pages<<(PAGE_SHIFT-10));
  407. #ifndef CONFIG_DISCONTIGMEM
  408. i = max_mapnr;
  409. while (i-- > 0) {
  410. total++;
  411. if (PageReserved(mem_map+i))
  412. reserved++;
  413. else if (PageSwapCache(mem_map+i))
  414. cached++;
  415. else if (!page_count(&mem_map[i]))
  416. free++;
  417. else
  418. shared += page_count(&mem_map[i]) - 1;
  419. }
  420. #else
  421. for (i = 0; i < npmem_ranges; i++) {
  422. int j;
  423. for (j = node_start_pfn(i); j < node_end_pfn(i); j++) {
  424. struct page *p;
  425. unsigned long flags;
  426. pgdat_resize_lock(NODE_DATA(i), &flags);
  427. p = nid_page_nr(i, j) - node_start_pfn(i);
  428. total++;
  429. if (PageReserved(p))
  430. reserved++;
  431. else if (PageSwapCache(p))
  432. cached++;
  433. else if (!page_count(p))
  434. free++;
  435. else
  436. shared += page_count(p) - 1;
  437. pgdat_resize_unlock(NODE_DATA(i), &flags);
  438. }
  439. }
  440. #endif
  441. printk(KERN_INFO "%d pages of RAM\n", total);
  442. printk(KERN_INFO "%d reserved pages\n", reserved);
  443. printk(KERN_INFO "%d pages shared\n", shared);
  444. printk(KERN_INFO "%d pages swap cached\n", cached);
  445. #ifdef CONFIG_DISCONTIGMEM
  446. {
  447. struct zonelist *zl;
  448. int i, j, k;
  449. for (i = 0; i < npmem_ranges; i++) {
  450. for (j = 0; j < MAX_NR_ZONES; j++) {
  451. zl = NODE_DATA(i)->node_zonelists + j;
  452. printk("Zone list for zone %d on node %d: ", j, i);
  453. for (k = 0; zl->zones[k] != NULL; k++)
  454. printk("[%d/%s] ", zl->zones[k]->zone_pgdat->node_id, zl->zones[k]->name);
  455. printk("\n");
  456. }
  457. }
  458. }
  459. #endif
  460. }
  461. static void __init map_pages(unsigned long start_vaddr, unsigned long start_paddr, unsigned long size, pgprot_t pgprot)
  462. {
  463. pgd_t *pg_dir;
  464. pmd_t *pmd;
  465. pte_t *pg_table;
  466. unsigned long end_paddr;
  467. unsigned long start_pmd;
  468. unsigned long start_pte;
  469. unsigned long tmp1;
  470. unsigned long tmp2;
  471. unsigned long address;
  472. unsigned long ro_start;
  473. unsigned long ro_end;
  474. unsigned long fv_addr;
  475. unsigned long gw_addr;
  476. extern const unsigned long fault_vector_20;
  477. extern void * const linux_gateway_page;
  478. ro_start = __pa((unsigned long)&_text);
  479. ro_end = __pa((unsigned long)&data_start);
  480. fv_addr = __pa((unsigned long)&fault_vector_20) & PAGE_MASK;
  481. gw_addr = __pa((unsigned long)&linux_gateway_page) & PAGE_MASK;
  482. end_paddr = start_paddr + size;
  483. pg_dir = pgd_offset_k(start_vaddr);
  484. #if PTRS_PER_PMD == 1
  485. start_pmd = 0;
  486. #else
  487. start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  488. #endif
  489. start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  490. address = start_paddr;
  491. while (address < end_paddr) {
  492. #if PTRS_PER_PMD == 1
  493. pmd = (pmd_t *)__pa(pg_dir);
  494. #else
  495. pmd = (pmd_t *)pgd_address(*pg_dir);
  496. /*
  497. * pmd is physical at this point
  498. */
  499. if (!pmd) {
  500. pmd = (pmd_t *) alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE << PMD_ORDER);
  501. pmd = (pmd_t *) __pa(pmd);
  502. }
  503. pgd_populate(NULL, pg_dir, __va(pmd));
  504. #endif
  505. pg_dir++;
  506. /* now change pmd to kernel virtual addresses */
  507. pmd = (pmd_t *)__va(pmd) + start_pmd;
  508. for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++,pmd++) {
  509. /*
  510. * pg_table is physical at this point
  511. */
  512. pg_table = (pte_t *)pmd_address(*pmd);
  513. if (!pg_table) {
  514. pg_table = (pte_t *)
  515. alloc_bootmem_low_pages_node(NODE_DATA(0),PAGE_SIZE);
  516. pg_table = (pte_t *) __pa(pg_table);
  517. }
  518. pmd_populate_kernel(NULL, pmd, __va(pg_table));
  519. /* now change pg_table to kernel virtual addresses */
  520. pg_table = (pte_t *) __va(pg_table) + start_pte;
  521. for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++,pg_table++) {
  522. pte_t pte;
  523. /*
  524. * Map the fault vector writable so we can
  525. * write the HPMC checksum.
  526. */
  527. if (address >= ro_start && address < ro_end
  528. && address != fv_addr
  529. && address != gw_addr)
  530. pte = __mk_pte(address, PAGE_KERNEL_RO);
  531. else
  532. 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("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. extern void flush_tlb_all_local(void);
  651. void __init paging_init(void)
  652. {
  653. int i;
  654. setup_bootmem();
  655. pagetable_init();
  656. gateway_init();
  657. flush_cache_all_local(); /* start with known state */
  658. flush_tlb_all_local();
  659. for (i = 0; i < npmem_ranges; i++) {
  660. unsigned long zones_size[MAX_NR_ZONES] = { 0, 0, 0 };
  661. /* We have an IOMMU, so all memory can go into a single
  662. ZONE_DMA zone. */
  663. zones_size[ZONE_DMA] = pmem_ranges[i].pages;
  664. #ifdef CONFIG_DISCONTIGMEM
  665. /* Need to initialize the pfnnid_map before we can initialize
  666. the zone */
  667. {
  668. int j;
  669. for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
  670. j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
  671. j++) {
  672. pfnnid_map[j] = i;
  673. }
  674. }
  675. #endif
  676. free_area_init_node(i, NODE_DATA(i), zones_size,
  677. pmem_ranges[i].start_pfn, NULL);
  678. }
  679. }
  680. #ifdef CONFIG_PA20
  681. /*
  682. * Currently, all PA20 chips have 18 bit protection id's, which is the
  683. * limiting factor (space ids are 32 bits).
  684. */
  685. #define NR_SPACE_IDS 262144
  686. #else
  687. /*
  688. * Currently we have a one-to-one relationship between space id's and
  689. * protection id's. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
  690. * support 15 bit protection id's, so that is the limiting factor.
  691. * PCXT' has 18 bit protection id's, but only 16 bit spaceids, so it's
  692. * probably not worth the effort for a special case here.
  693. */
  694. #define NR_SPACE_IDS 32768
  695. #endif /* !CONFIG_PA20 */
  696. #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
  697. #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long)))
  698. static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
  699. static unsigned long dirty_space_id[SID_ARRAY_SIZE];
  700. static unsigned long space_id_index;
  701. static unsigned long free_space_ids = NR_SPACE_IDS - 1;
  702. static unsigned long dirty_space_ids = 0;
  703. static DEFINE_SPINLOCK(sid_lock);
  704. unsigned long alloc_sid(void)
  705. {
  706. unsigned long index;
  707. spin_lock(&sid_lock);
  708. if (free_space_ids == 0) {
  709. if (dirty_space_ids != 0) {
  710. spin_unlock(&sid_lock);
  711. flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
  712. spin_lock(&sid_lock);
  713. }
  714. if (free_space_ids == 0)
  715. BUG();
  716. }
  717. free_space_ids--;
  718. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  719. space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
  720. space_id_index = index;
  721. spin_unlock(&sid_lock);
  722. return index << SPACEID_SHIFT;
  723. }
  724. void free_sid(unsigned long spaceid)
  725. {
  726. unsigned long index = spaceid >> SPACEID_SHIFT;
  727. unsigned long *dirty_space_offset;
  728. dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
  729. index &= (BITS_PER_LONG - 1);
  730. spin_lock(&sid_lock);
  731. if (*dirty_space_offset & (1L << index))
  732. BUG(); /* attempt to free space id twice */
  733. *dirty_space_offset |= (1L << index);
  734. dirty_space_ids++;
  735. spin_unlock(&sid_lock);
  736. }
  737. #ifdef CONFIG_SMP
  738. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  739. {
  740. int i;
  741. /* NOTE: sid_lock must be held upon entry */
  742. *ndirtyptr = dirty_space_ids;
  743. if (dirty_space_ids != 0) {
  744. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  745. dirty_array[i] = dirty_space_id[i];
  746. dirty_space_id[i] = 0;
  747. }
  748. dirty_space_ids = 0;
  749. }
  750. return;
  751. }
  752. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  753. {
  754. int i;
  755. /* NOTE: sid_lock must be held upon entry */
  756. if (ndirty != 0) {
  757. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  758. space_id[i] ^= dirty_array[i];
  759. }
  760. free_space_ids += ndirty;
  761. space_id_index = 0;
  762. }
  763. }
  764. #else /* CONFIG_SMP */
  765. static void recycle_sids(void)
  766. {
  767. int i;
  768. /* NOTE: sid_lock must be held upon entry */
  769. if (dirty_space_ids != 0) {
  770. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  771. space_id[i] ^= dirty_space_id[i];
  772. dirty_space_id[i] = 0;
  773. }
  774. free_space_ids += dirty_space_ids;
  775. dirty_space_ids = 0;
  776. space_id_index = 0;
  777. }
  778. }
  779. #endif
  780. /*
  781. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  782. * purged, we can safely reuse the space ids that were released but
  783. * not flushed from the tlb.
  784. */
  785. #ifdef CONFIG_SMP
  786. static unsigned long recycle_ndirty;
  787. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  788. static unsigned int recycle_inuse = 0;
  789. void flush_tlb_all(void)
  790. {
  791. int do_recycle;
  792. do_recycle = 0;
  793. spin_lock(&sid_lock);
  794. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  795. if (recycle_inuse) {
  796. BUG(); /* FIXME: Use a semaphore/wait queue here */
  797. }
  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((void (*)(void *))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 0
  824. if (start < end)
  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. set_page_count(virt_to_page(start), 1);
  829. free_page(start);
  830. num_physpages++;
  831. totalram_pages++;
  832. }
  833. #endif
  834. }
  835. #endif