init.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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 and page_cache_release */
  24. #include <asm/pgalloc.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/tlb.h>
  27. #include <asm/pdc_chassis.h>
  28. #include <asm/mmzone.h>
  29. #include <asm/sections.h>
  30. extern int data_start;
  31. #if PT_NLEVELS == 3
  32. /* NOTE: This layout exactly conforms to the hybrid L2/L3 page table layout
  33. * with the first pmd adjacent to the pgd and below it. gcc doesn't actually
  34. * guarantee that global objects will be laid out in memory in the same order
  35. * as the order of declaration, so put these in different sections and use
  36. * the linker script to order them. */
  37. pmd_t pmd0[PTRS_PER_PMD] __attribute__ ((__section__ (".data..vm0.pmd"), aligned(PAGE_SIZE)));
  38. #endif
  39. pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__ ((__section__ (".data..vm0.pgd"), aligned(PAGE_SIZE)));
  40. pte_t pg0[PT_INITIAL * PTRS_PER_PTE] __attribute__ ((__section__ (".data..vm0.pte"), aligned(PAGE_SIZE)));
  41. #ifdef CONFIG_DISCONTIGMEM
  42. struct node_map_data node_data[MAX_NUMNODES] __read_mostly;
  43. unsigned char pfnnid_map[PFNNID_MAP_MAX] __read_mostly;
  44. #endif
  45. static struct resource data_resource = {
  46. .name = "Kernel data",
  47. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  48. };
  49. static struct resource code_resource = {
  50. .name = "Kernel code",
  51. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  52. };
  53. static struct resource pdcdata_resource = {
  54. .name = "PDC data (Page Zero)",
  55. .start = 0,
  56. .end = 0x9ff,
  57. .flags = IORESOURCE_BUSY | IORESOURCE_MEM,
  58. };
  59. static struct resource sysram_resources[MAX_PHYSMEM_RANGES] __read_mostly;
  60. /* The following array is initialized from the firmware specific
  61. * information retrieved in kernel/inventory.c.
  62. */
  63. physmem_range_t pmem_ranges[MAX_PHYSMEM_RANGES] __read_mostly;
  64. int npmem_ranges __read_mostly;
  65. #ifdef CONFIG_64BIT
  66. #define MAX_MEM (~0UL)
  67. #else /* !CONFIG_64BIT */
  68. #define MAX_MEM (3584U*1024U*1024U)
  69. #endif /* !CONFIG_64BIT */
  70. static unsigned long mem_limit __read_mostly = MAX_MEM;
  71. static void __init mem_limit_func(void)
  72. {
  73. char *cp, *end;
  74. unsigned long limit;
  75. /* We need this before __setup() functions are called */
  76. limit = MAX_MEM;
  77. for (cp = boot_command_line; *cp; ) {
  78. if (memcmp(cp, "mem=", 4) == 0) {
  79. cp += 4;
  80. limit = memparse(cp, &end);
  81. if (end != cp)
  82. break;
  83. cp = end;
  84. } else {
  85. while (*cp != ' ' && *cp)
  86. ++cp;
  87. while (*cp == ' ')
  88. ++cp;
  89. }
  90. }
  91. if (limit < mem_limit)
  92. mem_limit = limit;
  93. }
  94. #define MAX_GAP (0x40000000UL >> PAGE_SHIFT)
  95. static void __init setup_bootmem(void)
  96. {
  97. unsigned long bootmap_size;
  98. unsigned long mem_max;
  99. unsigned long bootmap_pages;
  100. unsigned long bootmap_start_pfn;
  101. unsigned long bootmap_pfn;
  102. #ifndef CONFIG_DISCONTIGMEM
  103. physmem_range_t pmem_holes[MAX_PHYSMEM_RANGES - 1];
  104. int npmem_holes;
  105. #endif
  106. int i, sysram_resource_count;
  107. disable_sr_hashing(); /* Turn off space register hashing */
  108. /*
  109. * Sort the ranges. Since the number of ranges is typically
  110. * small, and performance is not an issue here, just do
  111. * a simple insertion sort.
  112. */
  113. for (i = 1; i < npmem_ranges; i++) {
  114. int j;
  115. for (j = i; j > 0; j--) {
  116. unsigned long tmp;
  117. if (pmem_ranges[j-1].start_pfn <
  118. pmem_ranges[j].start_pfn) {
  119. break;
  120. }
  121. tmp = pmem_ranges[j-1].start_pfn;
  122. pmem_ranges[j-1].start_pfn = pmem_ranges[j].start_pfn;
  123. pmem_ranges[j].start_pfn = tmp;
  124. tmp = pmem_ranges[j-1].pages;
  125. pmem_ranges[j-1].pages = pmem_ranges[j].pages;
  126. pmem_ranges[j].pages = tmp;
  127. }
  128. }
  129. #ifndef CONFIG_DISCONTIGMEM
  130. /*
  131. * Throw out ranges that are too far apart (controlled by
  132. * MAX_GAP).
  133. */
  134. for (i = 1; i < npmem_ranges; i++) {
  135. if (pmem_ranges[i].start_pfn -
  136. (pmem_ranges[i-1].start_pfn +
  137. pmem_ranges[i-1].pages) > MAX_GAP) {
  138. npmem_ranges = i;
  139. printk("Large gap in memory detected (%ld pages). "
  140. "Consider turning on CONFIG_DISCONTIGMEM\n",
  141. pmem_ranges[i].start_pfn -
  142. (pmem_ranges[i-1].start_pfn +
  143. pmem_ranges[i-1].pages));
  144. break;
  145. }
  146. }
  147. #endif
  148. if (npmem_ranges > 1) {
  149. /* Print the memory ranges */
  150. printk(KERN_INFO "Memory Ranges:\n");
  151. for (i = 0; i < npmem_ranges; i++) {
  152. unsigned long start;
  153. unsigned long size;
  154. size = (pmem_ranges[i].pages << PAGE_SHIFT);
  155. start = (pmem_ranges[i].start_pfn << PAGE_SHIFT);
  156. printk(KERN_INFO "%2d) Start 0x%016lx End 0x%016lx Size %6ld MB\n",
  157. i,start, start + (size - 1), size >> 20);
  158. }
  159. }
  160. sysram_resource_count = npmem_ranges;
  161. for (i = 0; i < sysram_resource_count; i++) {
  162. struct resource *res = &sysram_resources[i];
  163. res->name = "System RAM";
  164. res->start = pmem_ranges[i].start_pfn << PAGE_SHIFT;
  165. res->end = res->start + (pmem_ranges[i].pages << PAGE_SHIFT)-1;
  166. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  167. request_resource(&iomem_resource, res);
  168. }
  169. /*
  170. * For 32 bit kernels we limit the amount of memory we can
  171. * support, in order to preserve enough kernel address space
  172. * for other purposes. For 64 bit kernels we don't normally
  173. * limit the memory, but this mechanism can be used to
  174. * artificially limit the amount of memory (and it is written
  175. * to work with multiple memory ranges).
  176. */
  177. mem_limit_func(); /* check for "mem=" argument */
  178. mem_max = 0;
  179. num_physpages = 0;
  180. for (i = 0; i < npmem_ranges; i++) {
  181. unsigned long rsize;
  182. rsize = pmem_ranges[i].pages << PAGE_SHIFT;
  183. if ((mem_max + rsize) > mem_limit) {
  184. printk(KERN_WARNING "Memory truncated to %ld MB\n", mem_limit >> 20);
  185. if (mem_max == mem_limit)
  186. npmem_ranges = i;
  187. else {
  188. pmem_ranges[i].pages = (mem_limit >> PAGE_SHIFT)
  189. - (mem_max >> PAGE_SHIFT);
  190. npmem_ranges = i + 1;
  191. mem_max = mem_limit;
  192. }
  193. num_physpages += pmem_ranges[i].pages;
  194. break;
  195. }
  196. num_physpages += pmem_ranges[i].pages;
  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((unsigned long)_text),
  273. (unsigned long)(_end - _text), BOOTMEM_DEFAULT);
  274. reserve_bootmem_node(NODE_DATA(0), (bootmap_start_pfn << PAGE_SHIFT),
  275. ((bootmap_pfn - bootmap_start_pfn) << PAGE_SHIFT),
  276. BOOTMEM_DEFAULT);
  277. #ifndef CONFIG_DISCONTIGMEM
  278. /* reserve the holes */
  279. for (i = 0; i < npmem_holes; i++) {
  280. reserve_bootmem_node(NODE_DATA(0),
  281. (pmem_holes[i].start_pfn << PAGE_SHIFT),
  282. (pmem_holes[i].pages << PAGE_SHIFT),
  283. BOOTMEM_DEFAULT);
  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. reserve_bootmem_node(NODE_DATA(0), __pa(initrd_start),
  299. initrd_reserve, BOOTMEM_DEFAULT);
  300. }
  301. }
  302. #endif
  303. data_resource.start = virt_to_phys(&data_start);
  304. data_resource.end = virt_to_phys(_end) - 1;
  305. code_resource.start = virt_to_phys(_text);
  306. code_resource.end = virt_to_phys(&data_start)-1;
  307. /* We don't know which region the kernel will be in, so try
  308. * all of them.
  309. */
  310. for (i = 0; i < sysram_resource_count; i++) {
  311. struct resource *res = &sysram_resources[i];
  312. request_resource(res, &code_resource);
  313. request_resource(res, &data_resource);
  314. }
  315. request_resource(&sysram_resources[0], &pdcdata_resource);
  316. }
  317. static void __init map_pages(unsigned long start_vaddr,
  318. unsigned long start_paddr, unsigned long size,
  319. pgprot_t pgprot, int force)
  320. {
  321. pgd_t *pg_dir;
  322. pmd_t *pmd;
  323. pte_t *pg_table;
  324. unsigned long end_paddr;
  325. unsigned long start_pmd;
  326. unsigned long start_pte;
  327. unsigned long tmp1;
  328. unsigned long tmp2;
  329. unsigned long address;
  330. unsigned long vaddr;
  331. unsigned long ro_start;
  332. unsigned long ro_end;
  333. unsigned long fv_addr;
  334. unsigned long gw_addr;
  335. extern const unsigned long fault_vector_20;
  336. extern void * const linux_gateway_page;
  337. ro_start = __pa((unsigned long)_text);
  338. ro_end = __pa((unsigned long)&data_start);
  339. fv_addr = __pa((unsigned long)&fault_vector_20) & PAGE_MASK;
  340. gw_addr = __pa((unsigned long)&linux_gateway_page) & PAGE_MASK;
  341. end_paddr = start_paddr + size;
  342. pg_dir = pgd_offset_k(start_vaddr);
  343. #if PTRS_PER_PMD == 1
  344. start_pmd = 0;
  345. #else
  346. start_pmd = ((start_vaddr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  347. #endif
  348. start_pte = ((start_vaddr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  349. address = start_paddr;
  350. vaddr = start_vaddr;
  351. while (address < end_paddr) {
  352. #if PTRS_PER_PMD == 1
  353. pmd = (pmd_t *)__pa(pg_dir);
  354. #else
  355. pmd = (pmd_t *)pgd_address(*pg_dir);
  356. /*
  357. * pmd is physical at this point
  358. */
  359. if (!pmd) {
  360. pmd = (pmd_t *) alloc_bootmem_low_pages_node(NODE_DATA(0), PAGE_SIZE << PMD_ORDER);
  361. pmd = (pmd_t *) __pa(pmd);
  362. }
  363. pgd_populate(NULL, pg_dir, __va(pmd));
  364. #endif
  365. pg_dir++;
  366. /* now change pmd to kernel virtual addresses */
  367. pmd = (pmd_t *)__va(pmd) + start_pmd;
  368. for (tmp1 = start_pmd; tmp1 < PTRS_PER_PMD; tmp1++, pmd++) {
  369. /*
  370. * pg_table is physical at this point
  371. */
  372. pg_table = (pte_t *)pmd_address(*pmd);
  373. if (!pg_table) {
  374. pg_table = (pte_t *)
  375. alloc_bootmem_low_pages_node(NODE_DATA(0), PAGE_SIZE);
  376. pg_table = (pte_t *) __pa(pg_table);
  377. }
  378. pmd_populate_kernel(NULL, pmd, __va(pg_table));
  379. /* now change pg_table to kernel virtual addresses */
  380. pg_table = (pte_t *) __va(pg_table) + start_pte;
  381. for (tmp2 = start_pte; tmp2 < PTRS_PER_PTE; tmp2++, pg_table++) {
  382. pte_t pte;
  383. /*
  384. * Map the fault vector writable so we can
  385. * write the HPMC checksum.
  386. */
  387. if (force)
  388. pte = __mk_pte(address, pgprot);
  389. else if (core_kernel_text(vaddr) &&
  390. address != fv_addr)
  391. pte = __mk_pte(address, PAGE_KERNEL_EXEC);
  392. else
  393. #if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
  394. if (address >= ro_start && address < ro_end
  395. && address != fv_addr
  396. && address != gw_addr)
  397. pte = __mk_pte(address, PAGE_KERNEL_RO);
  398. else
  399. #endif
  400. pte = __mk_pte(address, pgprot);
  401. if (address >= end_paddr) {
  402. if (force)
  403. break;
  404. else
  405. pte_val(pte) = 0;
  406. }
  407. set_pte(pg_table, pte);
  408. address += PAGE_SIZE;
  409. vaddr += PAGE_SIZE;
  410. }
  411. start_pte = 0;
  412. if (address >= end_paddr)
  413. break;
  414. }
  415. start_pmd = 0;
  416. }
  417. }
  418. void free_initmem(void)
  419. {
  420. unsigned long addr;
  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. /* Attempt to catch anyone trying to execute code here
  439. * by filling the page with BRK insns.
  440. */
  441. memset((void *)init_begin, 0x00, init_end - init_begin);
  442. /* finally dump all the instructions which were cached, since the
  443. * pages are no-longer executable */
  444. flush_icache_range(init_begin, init_end);
  445. for (addr = init_begin; addr < init_end; addr += PAGE_SIZE) {
  446. ClearPageReserved(virt_to_page(addr));
  447. init_page_count(virt_to_page(addr));
  448. free_page(addr);
  449. num_physpages++;
  450. totalram_pages++;
  451. }
  452. /* set up a new led state on systems shipped LED State panel */
  453. pdc_chassis_send_status(PDC_CHASSIS_DIRECT_BCOMPLETE);
  454. printk(KERN_INFO "Freeing unused kernel memory: %luk freed\n",
  455. (init_end - init_begin) >> 10);
  456. }
  457. #ifdef CONFIG_DEBUG_RODATA
  458. void mark_rodata_ro(void)
  459. {
  460. /* rodata memory was already mapped with KERNEL_RO access rights by
  461. pagetable_init() and map_pages(). No need to do additional stuff here */
  462. printk (KERN_INFO "Write protecting the kernel read-only data: %luk\n",
  463. (unsigned long)(__end_rodata - __start_rodata) >> 10);
  464. }
  465. #endif
  466. /*
  467. * Just an arbitrary offset to serve as a "hole" between mapping areas
  468. * (between top of physical memory and a potential pcxl dma mapping
  469. * area, and below the vmalloc mapping area).
  470. *
  471. * The current 32K value just means that there will be a 32K "hole"
  472. * between mapping areas. That means that any out-of-bounds memory
  473. * accesses will hopefully be caught. The vmalloc() routines leaves
  474. * a hole of 4kB between each vmalloced area for the same reason.
  475. */
  476. /* Leave room for gateway page expansion */
  477. #if KERNEL_MAP_START < GATEWAY_PAGE_SIZE
  478. #error KERNEL_MAP_START is in gateway reserved region
  479. #endif
  480. #define MAP_START (KERNEL_MAP_START)
  481. #define VM_MAP_OFFSET (32*1024)
  482. #define SET_MAP_OFFSET(x) ((void *)(((unsigned long)(x) + VM_MAP_OFFSET) \
  483. & ~(VM_MAP_OFFSET-1)))
  484. void *parisc_vmalloc_start __read_mostly;
  485. EXPORT_SYMBOL(parisc_vmalloc_start);
  486. #ifdef CONFIG_PA11
  487. unsigned long pcxl_dma_start __read_mostly;
  488. #endif
  489. void __init mem_init(void)
  490. {
  491. int codesize, reservedpages, datasize, initsize;
  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. #ifndef CONFIG_DISCONTIGMEM
  500. max_mapnr = page_to_pfn(virt_to_page(high_memory - 1)) + 1;
  501. totalram_pages += free_all_bootmem();
  502. #else
  503. {
  504. int i;
  505. for (i = 0; i < npmem_ranges; i++)
  506. totalram_pages += free_all_bootmem_node(NODE_DATA(i));
  507. }
  508. #endif
  509. codesize = (unsigned long)_etext - (unsigned long)_text;
  510. datasize = (unsigned long)_edata - (unsigned long)_etext;
  511. initsize = (unsigned long)__init_end - (unsigned long)__init_begin;
  512. reservedpages = 0;
  513. {
  514. unsigned long pfn;
  515. #ifdef CONFIG_DISCONTIGMEM
  516. int i;
  517. for (i = 0; i < npmem_ranges; i++) {
  518. for (pfn = node_start_pfn(i); pfn < node_end_pfn(i); pfn++) {
  519. if (PageReserved(pfn_to_page(pfn)))
  520. reservedpages++;
  521. }
  522. }
  523. #else /* !CONFIG_DISCONTIGMEM */
  524. for (pfn = 0; pfn < max_pfn; pfn++) {
  525. /*
  526. * Only count reserved RAM pages
  527. */
  528. if (PageReserved(pfn_to_page(pfn)))
  529. reservedpages++;
  530. }
  531. #endif
  532. }
  533. #ifdef CONFIG_PA11
  534. if (hppa_dma_ops == &pcxl_dma_ops) {
  535. pcxl_dma_start = (unsigned long)SET_MAP_OFFSET(MAP_START);
  536. parisc_vmalloc_start = SET_MAP_OFFSET(pcxl_dma_start
  537. + PCXL_DMA_MAP_SIZE);
  538. } else {
  539. pcxl_dma_start = 0;
  540. parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
  541. }
  542. #else
  543. parisc_vmalloc_start = SET_MAP_OFFSET(MAP_START);
  544. #endif
  545. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init)\n",
  546. nr_free_pages() << (PAGE_SHIFT-10),
  547. num_physpages << (PAGE_SHIFT-10),
  548. codesize >> 10,
  549. reservedpages << (PAGE_SHIFT-10),
  550. datasize >> 10,
  551. initsize >> 10
  552. );
  553. #ifdef CONFIG_DEBUG_KERNEL /* double-sanity-check paranoia */
  554. printk("virtual kernel memory layout:\n"
  555. " vmalloc : 0x%p - 0x%p (%4ld MB)\n"
  556. " memory : 0x%p - 0x%p (%4ld MB)\n"
  557. " .init : 0x%p - 0x%p (%4ld kB)\n"
  558. " .data : 0x%p - 0x%p (%4ld kB)\n"
  559. " .text : 0x%p - 0x%p (%4ld kB)\n",
  560. (void*)VMALLOC_START, (void*)VMALLOC_END,
  561. (VMALLOC_END - VMALLOC_START) >> 20,
  562. __va(0), high_memory,
  563. ((unsigned long)high_memory - (unsigned long)__va(0)) >> 20,
  564. __init_begin, __init_end,
  565. ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10,
  566. _etext, _edata,
  567. ((unsigned long)_edata - (unsigned long)_etext) >> 10,
  568. _text, _etext,
  569. ((unsigned long)_etext - (unsigned long)_text) >> 10);
  570. #endif
  571. }
  572. unsigned long *empty_zero_page __read_mostly;
  573. EXPORT_SYMBOL(empty_zero_page);
  574. void show_mem(unsigned int filter)
  575. {
  576. int i,free = 0,total = 0,reserved = 0;
  577. int shared = 0, cached = 0;
  578. printk(KERN_INFO "Mem-info:\n");
  579. show_free_areas(filter);
  580. #ifndef CONFIG_DISCONTIGMEM
  581. i = max_mapnr;
  582. while (i-- > 0) {
  583. total++;
  584. if (PageReserved(mem_map+i))
  585. reserved++;
  586. else if (PageSwapCache(mem_map+i))
  587. cached++;
  588. else if (!page_count(&mem_map[i]))
  589. free++;
  590. else
  591. shared += page_count(&mem_map[i]) - 1;
  592. }
  593. #else
  594. for (i = 0; i < npmem_ranges; i++) {
  595. int j;
  596. for (j = node_start_pfn(i); j < node_end_pfn(i); j++) {
  597. struct page *p;
  598. unsigned long flags;
  599. pgdat_resize_lock(NODE_DATA(i), &flags);
  600. p = nid_page_nr(i, j) - node_start_pfn(i);
  601. total++;
  602. if (PageReserved(p))
  603. reserved++;
  604. else if (PageSwapCache(p))
  605. cached++;
  606. else if (!page_count(p))
  607. free++;
  608. else
  609. shared += page_count(p) - 1;
  610. pgdat_resize_unlock(NODE_DATA(i), &flags);
  611. }
  612. }
  613. #endif
  614. printk(KERN_INFO "%d pages of RAM\n", total);
  615. printk(KERN_INFO "%d reserved pages\n", reserved);
  616. printk(KERN_INFO "%d pages shared\n", shared);
  617. printk(KERN_INFO "%d pages swap cached\n", cached);
  618. #ifdef CONFIG_DISCONTIGMEM
  619. {
  620. struct zonelist *zl;
  621. int i, j;
  622. for (i = 0; i < npmem_ranges; i++) {
  623. zl = node_zonelist(i, 0);
  624. for (j = 0; j < MAX_NR_ZONES; j++) {
  625. struct zoneref *z;
  626. struct zone *zone;
  627. printk("Zone list for zone %d on node %d: ", j, i);
  628. for_each_zone_zonelist(zone, z, zl, j)
  629. printk("[%d/%s] ", zone_to_nid(zone),
  630. zone->name);
  631. printk("\n");
  632. }
  633. }
  634. }
  635. #endif
  636. }
  637. /*
  638. * pagetable_init() sets up the page tables
  639. *
  640. * Note that gateway_init() places the Linux gateway page at page 0.
  641. * Since gateway pages cannot be dereferenced this has the desirable
  642. * side effect of trapping those pesky NULL-reference errors in the
  643. * kernel.
  644. */
  645. static void __init pagetable_init(void)
  646. {
  647. int range;
  648. /* Map each physical memory range to its kernel vaddr */
  649. for (range = 0; range < npmem_ranges; range++) {
  650. unsigned long start_paddr;
  651. unsigned long end_paddr;
  652. unsigned long size;
  653. start_paddr = pmem_ranges[range].start_pfn << PAGE_SHIFT;
  654. end_paddr = start_paddr + (pmem_ranges[range].pages << PAGE_SHIFT);
  655. size = pmem_ranges[range].pages << PAGE_SHIFT;
  656. map_pages((unsigned long)__va(start_paddr), start_paddr,
  657. size, PAGE_KERNEL, 0);
  658. }
  659. #ifdef CONFIG_BLK_DEV_INITRD
  660. if (initrd_end && initrd_end > mem_limit) {
  661. printk(KERN_INFO "initrd: mapping %08lx-%08lx\n", initrd_start, initrd_end);
  662. map_pages(initrd_start, __pa(initrd_start),
  663. initrd_end - initrd_start, PAGE_KERNEL, 0);
  664. }
  665. #endif
  666. empty_zero_page = alloc_bootmem_pages(PAGE_SIZE);
  667. memset(empty_zero_page, 0, PAGE_SIZE);
  668. }
  669. static void __init gateway_init(void)
  670. {
  671. unsigned long linux_gateway_page_addr;
  672. /* FIXME: This is 'const' in order to trick the compiler
  673. into not treating it as DP-relative data. */
  674. extern void * const linux_gateway_page;
  675. linux_gateway_page_addr = LINUX_GATEWAY_ADDR & PAGE_MASK;
  676. /*
  677. * Setup Linux Gateway page.
  678. *
  679. * The Linux gateway page will reside in kernel space (on virtual
  680. * page 0), so it doesn't need to be aliased into user space.
  681. */
  682. map_pages(linux_gateway_page_addr, __pa(&linux_gateway_page),
  683. PAGE_SIZE, PAGE_GATEWAY, 1);
  684. }
  685. #ifdef CONFIG_HPUX
  686. void
  687. map_hpux_gateway_page(struct task_struct *tsk, struct mm_struct *mm)
  688. {
  689. pgd_t *pg_dir;
  690. pmd_t *pmd;
  691. pte_t *pg_table;
  692. unsigned long start_pmd;
  693. unsigned long start_pte;
  694. unsigned long address;
  695. unsigned long hpux_gw_page_addr;
  696. /* FIXME: This is 'const' in order to trick the compiler
  697. into not treating it as DP-relative data. */
  698. extern void * const hpux_gateway_page;
  699. hpux_gw_page_addr = HPUX_GATEWAY_ADDR & PAGE_MASK;
  700. /*
  701. * Setup HP-UX Gateway page.
  702. *
  703. * The HP-UX gateway page resides in the user address space,
  704. * so it needs to be aliased into each process.
  705. */
  706. pg_dir = pgd_offset(mm,hpux_gw_page_addr);
  707. #if PTRS_PER_PMD == 1
  708. start_pmd = 0;
  709. #else
  710. start_pmd = ((hpux_gw_page_addr >> PMD_SHIFT) & (PTRS_PER_PMD - 1));
  711. #endif
  712. start_pte = ((hpux_gw_page_addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1));
  713. address = __pa(&hpux_gateway_page);
  714. #if PTRS_PER_PMD == 1
  715. pmd = (pmd_t *)__pa(pg_dir);
  716. #else
  717. pmd = (pmd_t *) pgd_address(*pg_dir);
  718. /*
  719. * pmd is physical at this point
  720. */
  721. if (!pmd) {
  722. pmd = (pmd_t *) get_zeroed_page(GFP_KERNEL);
  723. pmd = (pmd_t *) __pa(pmd);
  724. }
  725. __pgd_val_set(*pg_dir, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pmd);
  726. #endif
  727. /* now change pmd to kernel virtual addresses */
  728. pmd = (pmd_t *)__va(pmd) + start_pmd;
  729. /*
  730. * pg_table is physical at this point
  731. */
  732. pg_table = (pte_t *) pmd_address(*pmd);
  733. if (!pg_table)
  734. pg_table = (pte_t *) __pa(get_zeroed_page(GFP_KERNEL));
  735. __pmd_val_set(*pmd, PxD_FLAG_PRESENT | PxD_FLAG_VALID | (unsigned long) pg_table);
  736. /* now change pg_table to kernel virtual addresses */
  737. pg_table = (pte_t *) __va(pg_table) + start_pte;
  738. set_pte(pg_table, __mk_pte(address, PAGE_GATEWAY));
  739. }
  740. EXPORT_SYMBOL(map_hpux_gateway_page);
  741. #endif
  742. void __init paging_init(void)
  743. {
  744. int i;
  745. setup_bootmem();
  746. pagetable_init();
  747. gateway_init();
  748. flush_cache_all_local(); /* start with known state */
  749. flush_tlb_all_local(NULL);
  750. for (i = 0; i < npmem_ranges; i++) {
  751. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  752. zones_size[ZONE_NORMAL] = pmem_ranges[i].pages;
  753. #ifdef CONFIG_DISCONTIGMEM
  754. /* Need to initialize the pfnnid_map before we can initialize
  755. the zone */
  756. {
  757. int j;
  758. for (j = (pmem_ranges[i].start_pfn >> PFNNID_SHIFT);
  759. j <= ((pmem_ranges[i].start_pfn + pmem_ranges[i].pages) >> PFNNID_SHIFT);
  760. j++) {
  761. pfnnid_map[j] = i;
  762. }
  763. }
  764. #endif
  765. free_area_init_node(i, zones_size,
  766. pmem_ranges[i].start_pfn, NULL);
  767. }
  768. }
  769. #ifdef CONFIG_PA20
  770. /*
  771. * Currently, all PA20 chips have 18 bit protection IDs, which is the
  772. * limiting factor (space ids are 32 bits).
  773. */
  774. #define NR_SPACE_IDS 262144
  775. #else
  776. /*
  777. * Currently we have a one-to-one relationship between space IDs and
  778. * protection IDs. Older parisc chips (PCXS, PCXT, PCXL, PCXL2) only
  779. * support 15 bit protection IDs, so that is the limiting factor.
  780. * PCXT' has 18 bit protection IDs, but only 16 bit spaceids, so it's
  781. * probably not worth the effort for a special case here.
  782. */
  783. #define NR_SPACE_IDS 32768
  784. #endif /* !CONFIG_PA20 */
  785. #define RECYCLE_THRESHOLD (NR_SPACE_IDS / 2)
  786. #define SID_ARRAY_SIZE (NR_SPACE_IDS / (8 * sizeof(long)))
  787. static unsigned long space_id[SID_ARRAY_SIZE] = { 1 }; /* disallow space 0 */
  788. static unsigned long dirty_space_id[SID_ARRAY_SIZE];
  789. static unsigned long space_id_index;
  790. static unsigned long free_space_ids = NR_SPACE_IDS - 1;
  791. static unsigned long dirty_space_ids = 0;
  792. static DEFINE_SPINLOCK(sid_lock);
  793. unsigned long alloc_sid(void)
  794. {
  795. unsigned long index;
  796. spin_lock(&sid_lock);
  797. if (free_space_ids == 0) {
  798. if (dirty_space_ids != 0) {
  799. spin_unlock(&sid_lock);
  800. flush_tlb_all(); /* flush_tlb_all() calls recycle_sids() */
  801. spin_lock(&sid_lock);
  802. }
  803. BUG_ON(free_space_ids == 0);
  804. }
  805. free_space_ids--;
  806. index = find_next_zero_bit(space_id, NR_SPACE_IDS, space_id_index);
  807. space_id[index >> SHIFT_PER_LONG] |= (1L << (index & (BITS_PER_LONG - 1)));
  808. space_id_index = index;
  809. spin_unlock(&sid_lock);
  810. return index << SPACEID_SHIFT;
  811. }
  812. void free_sid(unsigned long spaceid)
  813. {
  814. unsigned long index = spaceid >> SPACEID_SHIFT;
  815. unsigned long *dirty_space_offset;
  816. dirty_space_offset = dirty_space_id + (index >> SHIFT_PER_LONG);
  817. index &= (BITS_PER_LONG - 1);
  818. spin_lock(&sid_lock);
  819. BUG_ON(*dirty_space_offset & (1L << index)); /* attempt to free space id twice */
  820. *dirty_space_offset |= (1L << index);
  821. dirty_space_ids++;
  822. spin_unlock(&sid_lock);
  823. }
  824. #ifdef CONFIG_SMP
  825. static void get_dirty_sids(unsigned long *ndirtyptr,unsigned long *dirty_array)
  826. {
  827. int i;
  828. /* NOTE: sid_lock must be held upon entry */
  829. *ndirtyptr = dirty_space_ids;
  830. if (dirty_space_ids != 0) {
  831. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  832. dirty_array[i] = dirty_space_id[i];
  833. dirty_space_id[i] = 0;
  834. }
  835. dirty_space_ids = 0;
  836. }
  837. return;
  838. }
  839. static void recycle_sids(unsigned long ndirty,unsigned long *dirty_array)
  840. {
  841. int i;
  842. /* NOTE: sid_lock must be held upon entry */
  843. if (ndirty != 0) {
  844. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  845. space_id[i] ^= dirty_array[i];
  846. }
  847. free_space_ids += ndirty;
  848. space_id_index = 0;
  849. }
  850. }
  851. #else /* CONFIG_SMP */
  852. static void recycle_sids(void)
  853. {
  854. int i;
  855. /* NOTE: sid_lock must be held upon entry */
  856. if (dirty_space_ids != 0) {
  857. for (i = 0; i < SID_ARRAY_SIZE; i++) {
  858. space_id[i] ^= dirty_space_id[i];
  859. dirty_space_id[i] = 0;
  860. }
  861. free_space_ids += dirty_space_ids;
  862. dirty_space_ids = 0;
  863. space_id_index = 0;
  864. }
  865. }
  866. #endif
  867. /*
  868. * flush_tlb_all() calls recycle_sids(), since whenever the entire tlb is
  869. * purged, we can safely reuse the space ids that were released but
  870. * not flushed from the tlb.
  871. */
  872. #ifdef CONFIG_SMP
  873. static unsigned long recycle_ndirty;
  874. static unsigned long recycle_dirty_array[SID_ARRAY_SIZE];
  875. static unsigned int recycle_inuse;
  876. void flush_tlb_all(void)
  877. {
  878. int do_recycle;
  879. do_recycle = 0;
  880. spin_lock(&sid_lock);
  881. if (dirty_space_ids > RECYCLE_THRESHOLD) {
  882. BUG_ON(recycle_inuse); /* FIXME: Use a semaphore/wait queue here */
  883. get_dirty_sids(&recycle_ndirty,recycle_dirty_array);
  884. recycle_inuse++;
  885. do_recycle++;
  886. }
  887. spin_unlock(&sid_lock);
  888. on_each_cpu(flush_tlb_all_local, NULL, 1);
  889. if (do_recycle) {
  890. spin_lock(&sid_lock);
  891. recycle_sids(recycle_ndirty,recycle_dirty_array);
  892. recycle_inuse = 0;
  893. spin_unlock(&sid_lock);
  894. }
  895. }
  896. #else
  897. void flush_tlb_all(void)
  898. {
  899. spin_lock(&sid_lock);
  900. flush_tlb_all_local(NULL);
  901. recycle_sids();
  902. spin_unlock(&sid_lock);
  903. }
  904. #endif
  905. #ifdef CONFIG_BLK_DEV_INITRD
  906. void free_initrd_mem(unsigned long start, unsigned long end)
  907. {
  908. if (start >= end)
  909. return;
  910. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  911. for (; start < end; start += PAGE_SIZE) {
  912. ClearPageReserved(virt_to_page(start));
  913. init_page_count(virt_to_page(start));
  914. free_page(start);
  915. num_physpages++;
  916. totalram_pages++;
  917. }
  918. }
  919. #endif