init.c 24 KB

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