init.c 25 KB

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