init.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/parisc/mm/init.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. * Copyright 1999 SuSE GmbH
  7. * changed by Philipp Rumpf
  8. * Copyright 1999 Philipp Rumpf ([email protected])
  9. * Copyright 2004 Randolph Chung ([email protected])
  10. * Copyright 2006-2007 Helge Deller ([email protected])
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/memblock.h>
  16. #include <linux/gfp.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  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/tlb.h>
  27. #include <asm/pdc_chassis.h>
  28. #include <asm/mmzone.h>
  29. #include <asm/sections.h>
  30. #include <asm/msgbuf.h>
  31. #include <asm/sparsemem.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. pmd_t pmd0[PTRS_PER_PMD] __section(".data..vm0.pmd") __attribute__ ((aligned(PAGE_SIZE)));
  36. #endif
  37. pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".data..vm0.pgd") __attribute__ ((aligned(PAGE_SIZE)));
  38. pte_t pg0[PT_INITIAL * PTRS_PER_PTE] __section(".data..vm0.pte") __attribute__ ((aligned(PAGE_SIZE)));
  39. static struct resource data_resource = {
  40. .name = "Kernel data",
  41. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  42. };
  43. static struct resource code_resource = {
  44. .name = "Kernel code",
  45. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  46. };
  47. static struct resource pdcdata_resource = {
  48. .name = "PDC data (Page Zero)",
  49. .start = 0,
  50. .end = 0x9ff,
  51. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  52. };
  53. static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __ro_after_init;
  54. /* The following array is initialized from the firmware specific
  55. * information retrieved in kernel/inventory.c.
  56. */
  57. physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __initdata;
  58. int npmem_ranges __initdata;
  59. #ifdef CONFIG_64BIT
  60. #define MAX_MEM (1UL << MAX_PHYSMEM_BITS)
  61. #else /* !CONFIG_64BIT */
  62. #define MAX_MEM (3584U*1024U*1024U)
  63. #endif /* !CONFIG_64BIT */
  64. static unsigned long mem_limit __read_mostly = MAX_MEM;
  65. static void __init mem_limit_func(void)
  66. {
  67. char *cp, *end;
  68. unsigned long limit;
  69. /* We need this before __setup() functions are called */
  70. limit = MAX_MEM;
  71. for (cp = boot_command_line; *cp; ) {
  72. if (memcmp(cp, "mem=", 4) == 0) {
  73. cp += 4;
  74. limit = memparse(cp, &end);
  75. if (end != cp)
  76. break;
  77. cp = end;
  78. } else {
  79. while (*cp != ' ' && *cp)
  80. ++cp;
  81. while (*cp == ' ')
  82. ++cp;
  83. }
  84. }
  85. if (limit < mem_limit)
  86. mem_limit = limit;
  87. }
  88. #define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
  89. static void __init setup_bootmem(void)
  90. {
  91. unsigned long mem_max;
  92. #ifndef CONFIG_SPARSEMEM
  93. physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
  94. int npmem_holes;
  95. #endif
  96. int i, sysram_resource_count;
  97. disable_sr_hashing(); /* Turn off space register hashing */
  98. /*
  99. * Sort the ranges. Since the number of ranges is typically
  100. * small, and performance is not an issue here, just do
  101. * a simple insertion sort.
  102. */
  103. for (i = 1; i < npmem_ranges; i++) {
  104. int j;
  105. for (j = i; j > 0; j--) {
  106. if (pmem_ranges[j-1].start_pfn <
  107. pmem_ranges[j].start_pfn) {
  108. break;
  109. }
  110. swap(pmem_ranges[j-1], pmem_ranges[j]);
  111. }
  112. }
  113. #ifndef CONFIG_SPARSEMEM
  114. /*
  115. * Throw out ranges that are too far apart (controlled by
  116. * MAX_GAP).
  117. */
  118. for (i = 1; i < npmem_ranges; i++) {
  119. if (pmem_ranges[i].start_pfn -
  120. (pmem_ranges[i-1].start_pfn +
  121. pmem_ranges[i-1].pages) > MAX_GAP) {
  122. npmem_ranges = i;
  123. printk("Large gap in memory detected (%ld pages). "
  124. "Consider turning on CONFIG_SPARSEMEM\n",
  125. pmem_ranges[i].start_pfn -
  126. (pmem_ranges[i-1].start_pfn +
  127. pmem_ranges[i-1].pages));
  128. break;
  129. }
  130. }
  131. #endif
  132. /* Print the memory ranges */
  133. pr_info("Memory Ranges:\n");
  134. for (i = 0; i < npmem_ranges; i++) {
  135. struct resource *res = &sysram_resources[i];
  136. unsigned long start;
  137. unsigned long size;
  138. size = (pmem_ranges[i].pages << PAGE_SHIFT);
  139. start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
  140. pr_info("%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
  141. i, start, start + (size - 1), size >> 20);
  142. /* request memory resource */
  143. res->name = "System RAM";
  144. res->start = start;
  145. res->end = start + size - 1;
  146. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  147. request_resource(&iomem_resource, res);
  148. }
  149. sysram_resource_count = npmem_ranges;
  150. /*
  151. * For 32 bit kernels we limit the amount of memory we can
  152. * support, in order to preserve enough kernel address space
  153. * for other purposes. For 64 bit kernels we don't normally
  154. * limit the memory, but this mechanism can be used to
  155. * artificially limit the amount of memory (and it is written
  156. * to work with multiple memory ranges).
  157. */
  158. mem_limit_func(); /* check for "mem=" argument */
  159. mem_max = 0;
  160. for (i = 0; i < npmem_ranges; i++) {
  161. unsigned long rsize;
  162. rsize = pmem_ranges[i].pages << PAGE_SHIFT;
  163. if ((mem_max + rsize) > mem_limit) {
  164. printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
  165. if (mem_max == mem_limit)
  166. npmem_ranges = i;
  167. else {
  168. pmem_ranges[i].pages = (mem_limit >> PAGE_SHIFT)
  169. - (mem_max >> PAGE_SHIFT);
  170. npmem_ranges = i + 1;
  171. mem_max = mem_limit;
  172. }
  173. break;
  174. }
  175. mem_max += rsize;
  176. }
  177. printk(KERN_INFO "Total Memory: %ld MB\n",mem_max >> 20);
  178. #ifndef CONFIG_SPARSEMEM
  179. /* Merge the ranges, keeping track of the holes */
  180. {
  181. unsigned long end_pfn;
  182. unsigned long hole_pages;
  183. npmem_holes = 0;
  184. end_pfn = pmem_ranges[0].start_pfn + pmem_ranges[0].pages;
  185. for (i = 1; i < npmem_ranges; i++) {
  186. hole_pages = pmem_ranges[i].start_pfn - end_pfn;
  187. if (hole_pages) {
  188. pmem_holes[npmem_holes].start_pfn = end_pfn;
  189. pmem_holes[npmem_holes++].pages = hole_pages;
  190. end_pfn += hole_pages;
  191. }
  192. end_pfn += pmem_ranges[i].pages;
  193. }
  194. pmem_ranges[0].pages = end_pfn - pmem_ranges[0].start_pfn;
  195. npmem_ranges = 1;
  196. }
  197. #endif
  198. /*
  199. * Initialize and free the full range of memory in each range.
  200. */
  201. max_pfn = 0;
  202. for (i = 0; i < npmem_ranges; i++) {
  203. unsigned long start_pfn;
  204. unsigned long npages;
  205. unsigned long start;
  206. unsigned long size;
  207. start_pfn = pmem_ranges[i].start_pfn;
  208. npages = pmem_ranges[i].pages;
  209. start = start_pfn << PAGE_SHIFT;
  210. size = npages << PAGE_SHIFT;
  211. /* add system RAM memblock */
  212. memblock_add(start, size);
  213. if ((start_pfn + npages) > max_pfn)
  214. max_pfn = start_pfn + npages;
  215. }
  216. /*
  217. * We can't use memblock top-down allocations because we only
  218. * created the initial mapping up to KERNEL_INITIAL_SIZE in
  219. * the assembly bootup code.
  220. */
  221. memblock_set_bottom_up(true);
  222. /* IOMMU is always used to access "high mem" on those boxes
  223. * that can support enough mem that a PCI device couldn't
  224. * directly DMA to any physical addresses.
  225. * ISA DMA support will need to revisit this.
  226. */
  227. max_low_pfn = max_pfn;
  228. /* reserve PAGE0 pdc memory, kernel text/data/bss & bootmap */
  229. #define PDC_CONSOLE_IO_IODC_SIZE 32768
  230. memblock_reserve(0UL, (unsigned long)(PAGE0->mem_free +
  231. PDC_CONSOLE_IO_IODC_SIZE));
  232. memblock_reserve(__pa(KERNEL_BINARY_TEXT_START),
  233. (unsigned long)(_end - KERNEL_BINARY_TEXT_START));
  234. #ifndef CONFIG_SPARSEMEM
  235. /* reserve the holes */
  236. for (i = 0; i < npmem_holes; i++) {
  237. memblock_reserve((pmem_holes[i].start_pfn << PAGE_SHIFT),
  238. (pmem_holes[i].pages << PAGE_SHIFT));
  239. }
  240. #endif
  241. #ifdef CONFIG_BLK_DEV_INITRD
  242. if (initrd_start) {
  243. printk(KERN_INFO "initrd: %08lx-%08lx\n", initrd_start, initrd_end);
  244. if (__pa(initrd_start) < mem_max) {
  245. unsigned long initrd_reserve;
  246. if (__pa(initrd_end) > mem_max) {
  247. initrd_reserve = mem_max - __pa(initrd_start);
  248. } else {
  249. initrd_reserve = initrd_end - initrd_start;
  250. }
  251. initrd_below_start_ok = 1;
  252. printk(KERN_INFO "initrd: reserving %08lx-%08lx (mem_max %08lx)\n", __pa(initrd_start), __pa(initrd_start) + initrd_reserve, mem_max);
  253. memblock_reserve(__pa(initrd_start), initrd_reserve);
  254. }
  255. }
  256. #endif
  257. data_resource.start = virt_to_phys(&data_start);
  258. data_resource.end = virt_to_phys(_end) - 1;
  259. code_resource.start = virt_to_phys(_text);
  260. code_resource.end = virt_to_phys(&data_start)-1;
  261. /* We don't know which region the kernel will be in, so try
  262. * all of them.
  263. */
  264. for (i = 0; i < sysram_resource_count; i++) {
  265. struct resource *res = &sysram_resources[i];
  266. request_resource(res, &code_resource);
  267. request_resource(res, &data_resource);
  268. }
  269. request_resource(&sysram_resources[0], &pdcdata_resource);
  270. /* Initialize Page Deallocation Table (PDT) and check for bad memory. */
  271. pdc_pdt_init();
  272. memblock_allow_resize();
  273. memblock_dump_all();
  274. }
  275. static bool kernel_set_to_readonly;
  276. static void __ref map_pages(unsigned long start_vaddr,
  277. unsigned long start_paddr, unsigned long size,
  278. pgprot_t pgprot, int force)
  279. {
  280. pmd_t *pmd;
  281. pte_t *pg_table;
  282. unsigned long end_paddr;
  283. unsigned long start_pmd;
  284. unsigned long start_pte;
  285. unsigned long tmp1;
  286. unsigned long tmp2;
  287. unsigned long address;
  288. unsigned long vaddr;
  289. unsigned long ro_start;
  290. unsigned long ro_end;
  291. unsigned long kernel_start, kernel_end;
  292. ro_start = __pa((unsigned long)_text);
  293. ro_end = __pa((unsigned long)&data_start);
  294. kernel_start = __pa((unsigned long)&__init_begin);
  295. kernel_end = __pa((unsigned long)&_end);
  296. end_paddr = start_paddr + size;
  297. /* for 2-level configuration PTRS_PER_PMD is 0 so start_pmd will be 0 */
  298. start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  299. start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  300. address = start_paddr;
  301. vaddr = start_vaddr;
  302. while (address < end_paddr) {
  303. pgd_t *pgd = pgd_offset_k(vaddr);
  304. p4d_t *p4d = p4d_offset(pgd, vaddr);
  305. pud_t *pud = pud_offset(p4d, vaddr);
  306. #if CONFIG_PGTABLE_LEVELS == 3
  307. if (pud_none(*pud)) {
  308. pmd = memblock_alloc(PAGE_SIZE << PMD_TABLE_ORDER,
  309. PAGE_SIZE << PMD_TABLE_ORDER);
  310. if (!pmd)
  311. panic("pmd allocation failed.\n");
  312. pud_populate(NULL, pud, pmd);
  313. }
  314. #endif
  315. pmd = pmd_offset(pud, vaddr);
  316. for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {
  317. if (pmd_none(*pmd)) {
  318. pg_table = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  319. if (!pg_table)
  320. panic("page table allocation failed\n");
  321. pmd_populate_kernel(NULL, pmd, pg_table);
  322. }
  323. pg_table = pte_offset_kernel(pmd, vaddr);
  324. for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++, pg_table++) {
  325. pte_t pte;
  326. pgprot_t prot;
  327. bool huge = false;
  328. if (force) {
  329. prot = pgprot;
  330. } else if (address < kernel_start || address >= kernel_end) {
  331. /* outside kernel memory */
  332. prot = PAGE_KERNEL;
  333. } else if (!kernel_set_to_readonly) {
  334. /* still initializing, allow writing to RO memory */
  335. prot = PAGE_KERNEL_RWX;
  336. huge = true;
  337. } else if (address >= ro_start) {
  338. /* Code (ro) and Data areas */
  339. prot = (address < ro_end) ?
  340. PAGE_KERNEL_EXEC : PAGE_KERNEL;
  341. huge = true;
  342. } else {
  343. prot = PAGE_KERNEL;
  344. }
  345. pte = __mk_pte(address, prot);
  346. if (huge)
  347. pte = pte_mkhuge(pte);
  348. if (address >= end_paddr)
  349. break;
  350. set_pte(pg_table, pte);
  351. address += PAGE_SIZE;
  352. vaddr += PAGE_SIZE;
  353. }
  354. start_pte = 0;
  355. if (address >= end_paddr)
  356. break;
  357. }
  358. start_pmd = 0;
  359. }
  360. }
  361. void __init set_kernel_text_rw(int enable_read_write)
  362. {
  363. unsigned long start = (unsigned long) __init_begin;
  364. unsigned long end = (unsigned long) &data_start;
  365. map_pages(start, __pa(start), end-start,
  366. PAGE_KERNEL_RWX, enable_read_write ? 1:0);
  367. /* force the kernel to see the new page table entries */
  368. flush_cache_all();
  369. flush_tlb_all();
  370. }
  371. void free_initmem(void)
  372. {
  373. unsigned long init_begin = (unsigned long)__init_begin;
  374. unsigned long init_end = (unsigned long)__init_end;
  375. unsigned long kernel_end = (unsigned long)&_end;
  376. /* Remap kernel text and data, but do not touch init section yet. */
  377. kernel_set_to_readonly = true;
  378. map_pages(init_end, __pa(init_end), kernel_end - init_end,
  379. PAGE_KERNEL, 0);
  380. /* The init text pages are marked R-X. We have to
  381. * flush the icache and mark them RW-
  382. *
  383. * Do a dummy remap of the data section first (the data
  384. * section is already PAGE_KERNEL) to pull in the TLB entries
  385. * for map_kernel */
  386. map_pages(init_begin, __pa(init_begin), init_end - init_begin,
  387. PAGE_KERNEL_RWX, 1);
  388. /* now remap at PAGE_KERNEL since the TLB is pre-primed to execute
  389. * map_pages */
  390. map_pages(init_begin, __pa(init_begin), init_end - init_begin,
  391. PAGE_KERNEL, 1);
  392. /* force the kernel to see the new TLB entries */
  393. __flush_tlb_range(0, init_begin, kernel_end);
  394. /* finally dump all the instructions which were cached, since the
  395. * pages are no-longer executable */
  396. flush_icache_range(init_begin, init_end);
  397. free_initmem_default(POISON_FREE_INITMEM);
  398. /* set up a new led state on systems shipped LED State panel */
  399. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
  400. }
  401. #ifdef CONFIG_STRICT_KERNEL_RWX
  402. void mark_rodata_ro(void)
  403. {
  404. /* rodata memory was already mapped with KERNEL_RO access rights by
  405. pagetable_init() and map_pages(). No need to do additional stuff here */
  406. unsigned long roai_size = __end_ro_after_init - __start_ro_after_init;
  407. pr_info("Write protected read-only-after-init data: %luk\n", roai_size >> 10);
  408. }
  409. #endif
  410. /*
  411. * Just an arbitrary offset to serve as a "hole" between mapping areas
  412. * (between top of physical memory and a potential pcxl dma mapping
  413. * area, and below the vmalloc mapping area).
  414. *
  415. * The current 32K value just means that there will be a 32K "hole"
  416. * between mapping areas. That means that any out-of-bounds memory
  417. * accesses will hopefully be caught. The vmalloc() routines leaves
  418. * a hole of 4kB between each vmalloced area for the same reason.
  419. */
  420. /* Leave room for gateway page expansion */
  421. #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
  422. #error KERNEL_MAP_START is in gateway reserved region
  423. #endif
  424. #define MAP_START (KERNEL_MAP_START)
  425. #define VM_MAP_OFFSET (32*1024)
  426. #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
  427. & ~(VM_MAP_OFFSET-1)))
  428. void *parisc_vmalloc_start __ro_after_init;
  429. EXPORT_SYMBOL(parisc_vmalloc_start);
  430. #ifdef CONFIG_PA11
  431. unsigned long pcxl_dma_start __ro_after_init;
  432. #endif
  433. void __init mem_init(void)
  434. {
  435. /* Do sanity checks on IPC (compat) structures */
  436. BUILD_BUG_ON(sizeof(struct ipc64_perm) != 48);
  437. #ifndef CONFIG_64BIT
  438. BUILD_BUG_ON(sizeof(struct semid64_ds) != 80);
  439. BUILD_BUG_ON(sizeof(struct msqid64_ds) != 104);
  440. BUILD_BUG_ON(sizeof(struct shmid64_ds) != 104);
  441. #endif
  442. #ifdef CONFIG_COMPAT
  443. BUILD_BUG_ON(sizeof(struct compat_ipc64_perm) != sizeof(struct ipc64_perm));
  444. BUILD_BUG_ON(sizeof(struct compat_semid64_ds) != 80);
  445. BUILD_BUG_ON(sizeof(struct compat_msqid64_ds) != 104);
  446. BUILD_BUG_ON(sizeof(struct compat_shmid64_ds) != 104);
  447. #endif
  448. /* Do sanity checks on page table constants */
  449. BUILD_BUG_ON(PTE_ENTRY_SIZE != sizeof(pte_t));
  450. BUILD_BUG_ON(PMD_ENTRY_SIZE != sizeof(pmd_t));
  451. BUILD_BUG_ON(PGD_ENTRY_SIZE != sizeof(pgd_t));
  452. BUILD_BUG_ON(PAGE_SHIFT + BITS_PER_PTE + BITS_PER_PMD + BITS_PER_PGD
  453. > BITS_PER_LONG);
  454. #if CONFIG_PGTABLE_LEVELS == 3
  455. BUILD_BUG_ON(PT_INITIAL > PTRS_PER_PMD);
  456. #else
  457. BUILD_BUG_ON(PT_INITIAL > PTRS_PER_PGD);
  458. #endif
  459. #ifdef CONFIG_64BIT
  460. /* avoid ldil_%L() asm statements to sign-extend into upper 32-bits */
  461. BUILD_BUG_ON(__PAGE_OFFSET >= 0x80000000);
  462. BUILD_BUG_ON(TMPALIAS_MAP_START >= 0x80000000);
  463. #endif
  464. high_memory = __va((max_pfn << PAGE_SHIFT));
  465. set_max_mapnr(max_low_pfn);
  466. memblock_free_all();
  467. #ifdef CONFIG_PA11
  468. if (boot_cpu_data.cpu_type == pcxl2 || boot_cpu_data.cpu_type == pcxl) {
  469. pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
  470. parisc_vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start
  471. + PCXL_DMA_MAP_SIZE);
  472. } else
  473. #endif
  474. parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
  475. #if 0
  476. /*
  477. * Do not expose the virtual kernel memory layout to userspace.
  478. * But keep code for debugging purposes.
  479. */
  480. printk("virtual kernel memory layout:\n"
  481. " vmalloc : 0x%px - 0x%px (%4ld MB)\n"
  482. " fixmap : 0x%px - 0x%px (%4ld kB)\n"
  483. " memory : 0x%px - 0x%px (%4ld MB)\n"
  484. " .init : 0x%px - 0x%px (%4ld kB)\n"
  485. " .data : 0x%px - 0x%px (%4ld kB)\n"
  486. " .text : 0x%px - 0x%px (%4ld kB)\n",
  487. (void*)VMALLOC_START, (void*)VMALLOC_END,
  488. (VMALLOC_END - VMALLOC_START) >> 20,
  489. (void *)FIXMAP_START, (void *)(FIXMAP_START + FIXMAP_SIZE),
  490. (unsigned long)(FIXMAP_SIZE / 1024),
  491. __va(0), high_memory,
  492. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  493. __init_begin, __init_end,
  494. ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10,
  495. _etext, _edata,
  496. ((unsigned long)_edata - (unsigned long)_etext) >> 10,
  497. _text, _etext,
  498. ((unsigned long)_etext - (unsigned long)_text) >> 10);
  499. #endif
  500. }
  501. unsigned long *empty_zero_page __ro_after_init;
  502. EXPORT_SYMBOL(empty_zero_page);
  503. /*
  504. * pagetable_init() sets up the page tables
  505. *
  506. * Note that gateway_init() places the Linux gateway page at page 0.
  507. * Since gateway pages cannot be dereferenced this has the desirable
  508. * side effect of trapping those pesky NULL-reference errors in the
  509. * kernel.
  510. */
  511. static void __init pagetable_init(void)
  512. {
  513. int range;
  514. /* Map each physical memory range to its kernel vaddr */
  515. for (range = 0; range < npmem_ranges; range++) {
  516. unsigned long start_paddr;
  517. unsigned long end_paddr;
  518. unsigned long size;
  519. start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
  520. size = pmem_ranges[range].pages << PAGE_SHIFT;
  521. end_paddr = start_paddr + size;
  522. map_pages((unsigned long)__va(start_paddr), start_paddr,
  523. size, PAGE_KERNEL, 0);
  524. }
  525. #ifdef CONFIG_BLK_DEV_INITRD
  526. if (initrd_end && initrd_end > mem_limit) {
  527. printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
  528. map_pages(initrd_start, __pa(initrd_start),
  529. initrd_end - initrd_start, PAGE_KERNEL, 0);
  530. }
  531. #endif
  532. empty_zero_page = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  533. if (!empty_zero_page)
  534. panic("zero page allocation failed.\n");
  535. }
  536. static void __init gateway_init(void)
  537. {
  538. unsigned long linux_gateway_page_addr;
  539. /* FIXME: This is 'const' in order to trick the compiler
  540. into not treating it as DP-relative data. */
  541. extern void * const linux_gateway_page;
  542. linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
  543. /*
  544. * Setup Linux Gateway page.
  545. *
  546. * The Linux gateway page will reside in kernel space (on virtual
  547. * page 0), so it doesn't need to be aliased into user space.
  548. */
  549. map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
  550. PAGE_SIZE, PAGE_GATEWAY, 1);
  551. }
  552. static void __init parisc_bootmem_free(void)
  553. {
  554. unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0, };
  555. max_zone_pfn[0] = memblock_end_of_DRAM();
  556. free_area_init(max_zone_pfn);
  557. }
  558. void __init paging_init(void)
  559. {
  560. setup_bootmem();
  561. pagetable_init();
  562. gateway_init();
  563. flush_cache_all_local(); /* start with known state */
  564. flush_tlb_all_local(NULL);
  565. sparse_init();
  566. parisc_bootmem_free();
  567. }
  568. #ifdef CONFIG_PA20
  569. /*
  570. * Currently, all PA20 chips have 18 bit protection IDs, which is the
  571. * limiting factor (space ids are 32 bits).
  572. */
  573. #define NR_SPACE_IDS 262144
  574. #else
  575. /*
  576. * Currently we have a one-to-one relationship between space IDs and
  577. * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
  578. * support 15 bit protection IDs, so that is the limiting factor.
  579. * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's
  580. * probably not worth the effort for a special case here.
  581. */
  582. #define NR_SPACE_IDS 32768
  583. #endif /* !CONFIG_PA20 */
  584. #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
  585. #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long)))
  586. static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
  587. static unsigned long dirty_space_id[SID_ARRAY_SIZE];
  588. static unsigned long space_id_index;
  589. static unsigned long free_space_ids = NR_SPACE_IDS - 1;
  590. static unsigned long dirty_space_ids;
  591. static DEFINE_SPINLOCK(sid_lock);
  592. unsigned long alloc_sid(void)
  593. {
  594. unsigned long index;
  595. spin_lock(&sid_lock);
  596. if (free_space_ids == 0) {
  597. if (dirty_space_ids != 0) {
  598. spin_unlock(&sid_lock);
  599. flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
  600. spin_lock(&sid_lock);
  601. }
  602. BUG_ON(free_space_ids == 0);
  603. }
  604. free_space_ids--;
  605. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  606. space_id[BIT_WORD(index)] |= BIT_MASK(index);
  607. space_id_index = index;
  608. spin_unlock(&sid_lock);
  609. return index << SPACEID_SHIFT;
  610. }
  611. void free_sid(unsigned long spaceid)
  612. {
  613. unsigned long index = spaceid >> SPACEID_SHIFT;
  614. unsigned long *dirty_space_offset, mask;
  615. dirty_space_offset = &dirty_space_id[BIT_WORD(index)];
  616. mask = BIT_MASK(index);
  617. spin_lock(&sid_lock);
  618. BUG_ON(*dirty_space_offset & mask); /* attempt to free space id twice */
  619. *dirty_space_offset |= mask;
  620. dirty_space_ids++;
  621. spin_unlock(&sid_lock);
  622. }
  623. #ifdef CONFIG_SMP
  624. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  625. {
  626. int i;
  627. /* NOTE: sid_lock must be held upon entry */
  628. *ndirtyptr = dirty_space_ids;
  629. if (dirty_space_ids != 0) {
  630. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  631. dirty_array[i] = dirty_space_id[i];
  632. dirty_space_id[i] = 0;
  633. }
  634. dirty_space_ids = 0;
  635. }
  636. return;
  637. }
  638. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  639. {
  640. int i;
  641. /* NOTE: sid_lock must be held upon entry */
  642. if (ndirty != 0) {
  643. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  644. space_id[i] ^= dirty_array[i];
  645. }
  646. free_space_ids += ndirty;
  647. space_id_index = 0;
  648. }
  649. }
  650. #else /* CONFIG_SMP */
  651. static void recycle_sids(void)
  652. {
  653. int i;
  654. /* NOTE: sid_lock must be held upon entry */
  655. if (dirty_space_ids != 0) {
  656. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  657. space_id[i] ^= dirty_space_id[i];
  658. dirty_space_id[i] = 0;
  659. }
  660. free_space_ids += dirty_space_ids;
  661. dirty_space_ids = 0;
  662. space_id_index = 0;
  663. }
  664. }
  665. #endif
  666. /*
  667. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  668. * purged, we can safely reuse the space ids that were released but
  669. * not flushed from the tlb.
  670. */
  671. #ifdef CONFIG_SMP
  672. static unsigned long recycle_ndirty;
  673. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  674. static unsigned int recycle_inuse;
  675. void flush_tlb_all(void)
  676. {
  677. int do_recycle;
  678. do_recycle = 0;
  679. spin_lock(&sid_lock);
  680. __inc_irq_stat(irq_tlb_count);
  681. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  682. BUG_ON(recycle_inuse); /* FIXME: Use a semaphore/wait queue here */
  683. get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
  684. recycle_inuse++;
  685. do_recycle++;
  686. }
  687. spin_unlock(&sid_lock);
  688. on_each_cpu(flush_tlb_all_local, NULL, 1);
  689. if (do_recycle) {
  690. spin_lock(&sid_lock);
  691. recycle_sids(recycle_ndirty,recycle_dirty_array);
  692. recycle_inuse = 0;
  693. spin_unlock(&sid_lock);
  694. }
  695. }
  696. #else
  697. void flush_tlb_all(void)
  698. {
  699. spin_lock(&sid_lock);
  700. __inc_irq_stat(irq_tlb_count);
  701. flush_tlb_all_local(NULL);
  702. recycle_sids();
  703. spin_unlock(&sid_lock);
  704. }
  705. #endif
  706. static const pgprot_t protection_map[16] = {
  707. [VM_NONE] = PAGE_NONE,
  708. [VM_READ] = PAGE_READONLY,
  709. [VM_WRITE] = PAGE_NONE,
  710. [VM_WRITE | VM_READ] = PAGE_READONLY,
  711. [VM_EXEC] = PAGE_EXECREAD,
  712. [VM_EXEC | VM_READ] = PAGE_EXECREAD,
  713. [VM_EXEC | VM_WRITE] = PAGE_EXECREAD,
  714. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_EXECREAD,
  715. [VM_SHARED] = PAGE_NONE,
  716. [VM_SHARED | VM_READ] = PAGE_READONLY,
  717. [VM_SHARED | VM_WRITE] = PAGE_WRITEONLY,
  718. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
  719. [VM_SHARED | VM_EXEC] = PAGE_EXECREAD,
  720. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_EXECREAD,
  721. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_RWX,
  722. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_RWX
  723. };
  724. DECLARE_VM_GET_PAGE_PROT