init.c 28 KB

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