init_64.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  1. /*
  2. * arch/sparc64/mm/init.c
  3. *
  4. * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/init.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/mm.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/initrd.h>
  16. #include <linux/swap.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/poison.h>
  19. #include <linux/fs.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/cache.h>
  23. #include <linux/sort.h>
  24. #include <linux/percpu.h>
  25. #include <linux/memblock.h>
  26. #include <linux/mmzone.h>
  27. #include <linux/gfp.h>
  28. #include <asm/head.h>
  29. #include <asm/system.h>
  30. #include <asm/page.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/oplib.h>
  34. #include <asm/iommu.h>
  35. #include <asm/io.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/dma.h>
  40. #include <asm/starfire.h>
  41. #include <asm/tlb.h>
  42. #include <asm/spitfire.h>
  43. #include <asm/sections.h>
  44. #include <asm/tsb.h>
  45. #include <asm/hypervisor.h>
  46. #include <asm/prom.h>
  47. #include <asm/mdesc.h>
  48. #include <asm/cpudata.h>
  49. #include <asm/irq.h>
  50. #include "init_64.h"
  51. unsigned long kern_linear_pte_xor[2] __read_mostly;
  52. /* A bitmap, one bit for every 256MB of physical memory. If the bit
  53. * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
  54. * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
  55. */
  56. unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
  57. #ifndef CONFIG_DEBUG_PAGEALLOC
  58. /* A special kernel TSB for 4MB and 256MB linear mappings.
  59. * Space is allocated for this right after the trap table
  60. * in arch/sparc64/kernel/head.S
  61. */
  62. extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
  63. #endif
  64. #define MAX_BANKS 32
  65. static struct linux_prom64_registers pavail[MAX_BANKS] __devinitdata;
  66. static int pavail_ents __devinitdata;
  67. static int cmp_p64(const void *a, const void *b)
  68. {
  69. const struct linux_prom64_registers *x = a, *y = b;
  70. if (x->phys_addr > y->phys_addr)
  71. return 1;
  72. if (x->phys_addr < y->phys_addr)
  73. return -1;
  74. return 0;
  75. }
  76. static void __init read_obp_memory(const char *property,
  77. struct linux_prom64_registers *regs,
  78. int *num_ents)
  79. {
  80. phandle node = prom_finddevice("/memory");
  81. int prop_size = prom_getproplen(node, property);
  82. int ents, ret, i;
  83. ents = prop_size / sizeof(struct linux_prom64_registers);
  84. if (ents > MAX_BANKS) {
  85. prom_printf("The machine has more %s property entries than "
  86. "this kernel can support (%d).\n",
  87. property, MAX_BANKS);
  88. prom_halt();
  89. }
  90. ret = prom_getproperty(node, property, (char *) regs, prop_size);
  91. if (ret == -1) {
  92. prom_printf("Couldn't get %s property from /memory.\n");
  93. prom_halt();
  94. }
  95. /* Sanitize what we got from the firmware, by page aligning
  96. * everything.
  97. */
  98. for (i = 0; i < ents; i++) {
  99. unsigned long base, size;
  100. base = regs[i].phys_addr;
  101. size = regs[i].reg_size;
  102. size &= PAGE_MASK;
  103. if (base & ~PAGE_MASK) {
  104. unsigned long new_base = PAGE_ALIGN(base);
  105. size -= new_base - base;
  106. if ((long) size < 0L)
  107. size = 0UL;
  108. base = new_base;
  109. }
  110. if (size == 0UL) {
  111. /* If it is empty, simply get rid of it.
  112. * This simplifies the logic of the other
  113. * functions that process these arrays.
  114. */
  115. memmove(&regs[i], &regs[i + 1],
  116. (ents - i - 1) * sizeof(regs[0]));
  117. i--;
  118. ents--;
  119. continue;
  120. }
  121. regs[i].phys_addr = base;
  122. regs[i].reg_size = size;
  123. }
  124. *num_ents = ents;
  125. sort(regs, ents, sizeof(struct linux_prom64_registers),
  126. cmp_p64, NULL);
  127. }
  128. unsigned long sparc64_valid_addr_bitmap[VALID_ADDR_BITMAP_BYTES /
  129. sizeof(unsigned long)];
  130. EXPORT_SYMBOL(sparc64_valid_addr_bitmap);
  131. /* Kernel physical address base and size in bytes. */
  132. unsigned long kern_base __read_mostly;
  133. unsigned long kern_size __read_mostly;
  134. /* Initial ramdisk setup */
  135. extern unsigned long sparc_ramdisk_image64;
  136. extern unsigned int sparc_ramdisk_image;
  137. extern unsigned int sparc_ramdisk_size;
  138. struct page *mem_map_zero __read_mostly;
  139. EXPORT_SYMBOL(mem_map_zero);
  140. unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
  141. unsigned long sparc64_kern_pri_context __read_mostly;
  142. unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
  143. unsigned long sparc64_kern_sec_context __read_mostly;
  144. int num_kernel_image_mappings;
  145. #ifdef CONFIG_DEBUG_DCFLUSH
  146. atomic_t dcpage_flushes = ATOMIC_INIT(0);
  147. #ifdef CONFIG_SMP
  148. atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
  149. #endif
  150. #endif
  151. inline void flush_dcache_page_impl(struct page *page)
  152. {
  153. BUG_ON(tlb_type == hypervisor);
  154. #ifdef CONFIG_DEBUG_DCFLUSH
  155. atomic_inc(&dcpage_flushes);
  156. #endif
  157. #ifdef DCACHE_ALIASING_POSSIBLE
  158. __flush_dcache_page(page_address(page),
  159. ((tlb_type == spitfire) &&
  160. page_mapping(page) != NULL));
  161. #else
  162. if (page_mapping(page) != NULL &&
  163. tlb_type == spitfire)
  164. __flush_icache_page(__pa(page_address(page)));
  165. #endif
  166. }
  167. #define PG_dcache_dirty PG_arch_1
  168. #define PG_dcache_cpu_shift 32UL
  169. #define PG_dcache_cpu_mask \
  170. ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
  171. #define dcache_dirty_cpu(page) \
  172. (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
  173. static inline void set_dcache_dirty(struct page *page, int this_cpu)
  174. {
  175. unsigned long mask = this_cpu;
  176. unsigned long non_cpu_bits;
  177. non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
  178. mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
  179. __asm__ __volatile__("1:\n\t"
  180. "ldx [%2], %%g7\n\t"
  181. "and %%g7, %1, %%g1\n\t"
  182. "or %%g1, %0, %%g1\n\t"
  183. "casx [%2], %%g7, %%g1\n\t"
  184. "cmp %%g7, %%g1\n\t"
  185. "bne,pn %%xcc, 1b\n\t"
  186. " nop"
  187. : /* no outputs */
  188. : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
  189. : "g1", "g7");
  190. }
  191. static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
  192. {
  193. unsigned long mask = (1UL << PG_dcache_dirty);
  194. __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
  195. "1:\n\t"
  196. "ldx [%2], %%g7\n\t"
  197. "srlx %%g7, %4, %%g1\n\t"
  198. "and %%g1, %3, %%g1\n\t"
  199. "cmp %%g1, %0\n\t"
  200. "bne,pn %%icc, 2f\n\t"
  201. " andn %%g7, %1, %%g1\n\t"
  202. "casx [%2], %%g7, %%g1\n\t"
  203. "cmp %%g7, %%g1\n\t"
  204. "bne,pn %%xcc, 1b\n\t"
  205. " nop\n"
  206. "2:"
  207. : /* no outputs */
  208. : "r" (cpu), "r" (mask), "r" (&page->flags),
  209. "i" (PG_dcache_cpu_mask),
  210. "i" (PG_dcache_cpu_shift)
  211. : "g1", "g7");
  212. }
  213. static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
  214. {
  215. unsigned long tsb_addr = (unsigned long) ent;
  216. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  217. tsb_addr = __pa(tsb_addr);
  218. __tsb_insert(tsb_addr, tag, pte);
  219. }
  220. unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
  221. unsigned long _PAGE_SZBITS __read_mostly;
  222. static void flush_dcache(unsigned long pfn)
  223. {
  224. struct page *page;
  225. page = pfn_to_page(pfn);
  226. if (page) {
  227. unsigned long pg_flags;
  228. pg_flags = page->flags;
  229. if (pg_flags & (1UL << PG_dcache_dirty)) {
  230. int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
  231. PG_dcache_cpu_mask);
  232. int this_cpu = get_cpu();
  233. /* This is just to optimize away some function calls
  234. * in the SMP case.
  235. */
  236. if (cpu == this_cpu)
  237. flush_dcache_page_impl(page);
  238. else
  239. smp_flush_dcache_page_impl(page, cpu);
  240. clear_dcache_dirty_cpu(page, cpu);
  241. put_cpu();
  242. }
  243. }
  244. }
  245. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
  246. {
  247. struct mm_struct *mm;
  248. struct tsb *tsb;
  249. unsigned long tag, flags;
  250. unsigned long tsb_index, tsb_hash_shift;
  251. pte_t pte = *ptep;
  252. if (tlb_type != hypervisor) {
  253. unsigned long pfn = pte_pfn(pte);
  254. if (pfn_valid(pfn))
  255. flush_dcache(pfn);
  256. }
  257. mm = vma->vm_mm;
  258. tsb_index = MM_TSB_BASE;
  259. tsb_hash_shift = PAGE_SHIFT;
  260. spin_lock_irqsave(&mm->context.lock, flags);
  261. #ifdef CONFIG_HUGETLB_PAGE
  262. if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
  263. if ((tlb_type == hypervisor &&
  264. (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
  265. (tlb_type != hypervisor &&
  266. (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
  267. tsb_index = MM_TSB_HUGE;
  268. tsb_hash_shift = HPAGE_SHIFT;
  269. }
  270. }
  271. #endif
  272. tsb = mm->context.tsb_block[tsb_index].tsb;
  273. tsb += ((address >> tsb_hash_shift) &
  274. (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
  275. tag = (address >> 22UL);
  276. tsb_insert(tsb, tag, pte_val(pte));
  277. spin_unlock_irqrestore(&mm->context.lock, flags);
  278. }
  279. void flush_dcache_page(struct page *page)
  280. {
  281. struct address_space *mapping;
  282. int this_cpu;
  283. if (tlb_type == hypervisor)
  284. return;
  285. /* Do not bother with the expensive D-cache flush if it
  286. * is merely the zero page. The 'bigcore' testcase in GDB
  287. * causes this case to run millions of times.
  288. */
  289. if (page == ZERO_PAGE(0))
  290. return;
  291. this_cpu = get_cpu();
  292. mapping = page_mapping(page);
  293. if (mapping && !mapping_mapped(mapping)) {
  294. int dirty = test_bit(PG_dcache_dirty, &page->flags);
  295. if (dirty) {
  296. int dirty_cpu = dcache_dirty_cpu(page);
  297. if (dirty_cpu == this_cpu)
  298. goto out;
  299. smp_flush_dcache_page_impl(page, dirty_cpu);
  300. }
  301. set_dcache_dirty(page, this_cpu);
  302. } else {
  303. /* We could delay the flush for the !page_mapping
  304. * case too. But that case is for exec env/arg
  305. * pages and those are %99 certainly going to get
  306. * faulted into the tlb (and thus flushed) anyways.
  307. */
  308. flush_dcache_page_impl(page);
  309. }
  310. out:
  311. put_cpu();
  312. }
  313. EXPORT_SYMBOL(flush_dcache_page);
  314. void __kprobes flush_icache_range(unsigned long start, unsigned long end)
  315. {
  316. /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
  317. if (tlb_type == spitfire) {
  318. unsigned long kaddr;
  319. /* This code only runs on Spitfire cpus so this is
  320. * why we can assume _PAGE_PADDR_4U.
  321. */
  322. for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
  323. unsigned long paddr, mask = _PAGE_PADDR_4U;
  324. if (kaddr >= PAGE_OFFSET)
  325. paddr = kaddr & mask;
  326. else {
  327. pgd_t *pgdp = pgd_offset_k(kaddr);
  328. pud_t *pudp = pud_offset(pgdp, kaddr);
  329. pmd_t *pmdp = pmd_offset(pudp, kaddr);
  330. pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
  331. paddr = pte_val(*ptep) & mask;
  332. }
  333. __flush_icache_page(paddr);
  334. }
  335. }
  336. }
  337. EXPORT_SYMBOL(flush_icache_range);
  338. void mmu_info(struct seq_file *m)
  339. {
  340. if (tlb_type == cheetah)
  341. seq_printf(m, "MMU Type\t: Cheetah\n");
  342. else if (tlb_type == cheetah_plus)
  343. seq_printf(m, "MMU Type\t: Cheetah+\n");
  344. else if (tlb_type == spitfire)
  345. seq_printf(m, "MMU Type\t: Spitfire\n");
  346. else if (tlb_type == hypervisor)
  347. seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
  348. else
  349. seq_printf(m, "MMU Type\t: ???\n");
  350. #ifdef CONFIG_DEBUG_DCFLUSH
  351. seq_printf(m, "DCPageFlushes\t: %d\n",
  352. atomic_read(&dcpage_flushes));
  353. #ifdef CONFIG_SMP
  354. seq_printf(m, "DCPageFlushesXC\t: %d\n",
  355. atomic_read(&dcpage_flushes_xcall));
  356. #endif /* CONFIG_SMP */
  357. #endif /* CONFIG_DEBUG_DCFLUSH */
  358. }
  359. struct linux_prom_translation prom_trans[512] __read_mostly;
  360. unsigned int prom_trans_ents __read_mostly;
  361. unsigned long kern_locked_tte_data;
  362. /* The obp translations are saved based on 8k pagesize, since obp can
  363. * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
  364. * HI_OBP_ADDRESS range are handled in ktlb.S.
  365. */
  366. static inline int in_obp_range(unsigned long vaddr)
  367. {
  368. return (vaddr >= LOW_OBP_ADDRESS &&
  369. vaddr < HI_OBP_ADDRESS);
  370. }
  371. static int cmp_ptrans(const void *a, const void *b)
  372. {
  373. const struct linux_prom_translation *x = a, *y = b;
  374. if (x->virt > y->virt)
  375. return 1;
  376. if (x->virt < y->virt)
  377. return -1;
  378. return 0;
  379. }
  380. /* Read OBP translations property into 'prom_trans[]'. */
  381. static void __init read_obp_translations(void)
  382. {
  383. int n, node, ents, first, last, i;
  384. node = prom_finddevice("/virtual-memory");
  385. n = prom_getproplen(node, "translations");
  386. if (unlikely(n == 0 || n == -1)) {
  387. prom_printf("prom_mappings: Couldn't get size.\n");
  388. prom_halt();
  389. }
  390. if (unlikely(n > sizeof(prom_trans))) {
  391. prom_printf("prom_mappings: Size %Zd is too big.\n", n);
  392. prom_halt();
  393. }
  394. if ((n = prom_getproperty(node, "translations",
  395. (char *)&prom_trans[0],
  396. sizeof(prom_trans))) == -1) {
  397. prom_printf("prom_mappings: Couldn't get property.\n");
  398. prom_halt();
  399. }
  400. n = n / sizeof(struct linux_prom_translation);
  401. ents = n;
  402. sort(prom_trans, ents, sizeof(struct linux_prom_translation),
  403. cmp_ptrans, NULL);
  404. /* Now kick out all the non-OBP entries. */
  405. for (i = 0; i < ents; i++) {
  406. if (in_obp_range(prom_trans[i].virt))
  407. break;
  408. }
  409. first = i;
  410. for (; i < ents; i++) {
  411. if (!in_obp_range(prom_trans[i].virt))
  412. break;
  413. }
  414. last = i;
  415. for (i = 0; i < (last - first); i++) {
  416. struct linux_prom_translation *src = &prom_trans[i + first];
  417. struct linux_prom_translation *dest = &prom_trans[i];
  418. *dest = *src;
  419. }
  420. for (; i < ents; i++) {
  421. struct linux_prom_translation *dest = &prom_trans[i];
  422. dest->virt = dest->size = dest->data = 0x0UL;
  423. }
  424. prom_trans_ents = last - first;
  425. if (tlb_type == spitfire) {
  426. /* Clear diag TTE bits. */
  427. for (i = 0; i < prom_trans_ents; i++)
  428. prom_trans[i].data &= ~0x0003fe0000000000UL;
  429. }
  430. }
  431. static void __init hypervisor_tlb_lock(unsigned long vaddr,
  432. unsigned long pte,
  433. unsigned long mmu)
  434. {
  435. unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
  436. if (ret != 0) {
  437. prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
  438. "errors with %lx\n", vaddr, 0, pte, mmu, ret);
  439. prom_halt();
  440. }
  441. }
  442. static unsigned long kern_large_tte(unsigned long paddr);
  443. static void __init remap_kernel(void)
  444. {
  445. unsigned long phys_page, tte_vaddr, tte_data;
  446. int i, tlb_ent = sparc64_highest_locked_tlbent();
  447. tte_vaddr = (unsigned long) KERNBASE;
  448. phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  449. tte_data = kern_large_tte(phys_page);
  450. kern_locked_tte_data = tte_data;
  451. /* Now lock us into the TLBs via Hypervisor or OBP. */
  452. if (tlb_type == hypervisor) {
  453. for (i = 0; i < num_kernel_image_mappings; i++) {
  454. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
  455. hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
  456. tte_vaddr += 0x400000;
  457. tte_data += 0x400000;
  458. }
  459. } else {
  460. for (i = 0; i < num_kernel_image_mappings; i++) {
  461. prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
  462. prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
  463. tte_vaddr += 0x400000;
  464. tte_data += 0x400000;
  465. }
  466. sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
  467. }
  468. if (tlb_type == cheetah_plus) {
  469. sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
  470. CTX_CHEETAH_PLUS_NUC);
  471. sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
  472. sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
  473. }
  474. }
  475. static void __init inherit_prom_mappings(void)
  476. {
  477. /* Now fixup OBP's idea about where we really are mapped. */
  478. printk("Remapping the kernel... ");
  479. remap_kernel();
  480. printk("done.\n");
  481. }
  482. void prom_world(int enter)
  483. {
  484. if (!enter)
  485. set_fs((mm_segment_t) { get_thread_current_ds() });
  486. __asm__ __volatile__("flushw");
  487. }
  488. void __flush_dcache_range(unsigned long start, unsigned long end)
  489. {
  490. unsigned long va;
  491. if (tlb_type == spitfire) {
  492. int n = 0;
  493. for (va = start; va < end; va += 32) {
  494. spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
  495. if (++n >= 512)
  496. break;
  497. }
  498. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  499. start = __pa(start);
  500. end = __pa(end);
  501. for (va = start; va < end; va += 32)
  502. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  503. "membar #Sync"
  504. : /* no outputs */
  505. : "r" (va),
  506. "i" (ASI_DCACHE_INVALIDATE));
  507. }
  508. }
  509. EXPORT_SYMBOL(__flush_dcache_range);
  510. /* get_new_mmu_context() uses "cache + 1". */
  511. DEFINE_SPINLOCK(ctx_alloc_lock);
  512. unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
  513. #define MAX_CTX_NR (1UL << CTX_NR_BITS)
  514. #define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
  515. DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
  516. /* Caller does TLB context flushing on local CPU if necessary.
  517. * The caller also ensures that CTX_VALID(mm->context) is false.
  518. *
  519. * We must be careful about boundary cases so that we never
  520. * let the user have CTX 0 (nucleus) or we ever use a CTX
  521. * version of zero (and thus NO_CONTEXT would not be caught
  522. * by version mis-match tests in mmu_context.h).
  523. *
  524. * Always invoked with interrupts disabled.
  525. */
  526. void get_new_mmu_context(struct mm_struct *mm)
  527. {
  528. unsigned long ctx, new_ctx;
  529. unsigned long orig_pgsz_bits;
  530. unsigned long flags;
  531. int new_version;
  532. spin_lock_irqsave(&ctx_alloc_lock, flags);
  533. orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
  534. ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
  535. new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
  536. new_version = 0;
  537. if (new_ctx >= (1 << CTX_NR_BITS)) {
  538. new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
  539. if (new_ctx >= ctx) {
  540. int i;
  541. new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
  542. CTX_FIRST_VERSION;
  543. if (new_ctx == 1)
  544. new_ctx = CTX_FIRST_VERSION;
  545. /* Don't call memset, for 16 entries that's just
  546. * plain silly...
  547. */
  548. mmu_context_bmap[0] = 3;
  549. mmu_context_bmap[1] = 0;
  550. mmu_context_bmap[2] = 0;
  551. mmu_context_bmap[3] = 0;
  552. for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
  553. mmu_context_bmap[i + 0] = 0;
  554. mmu_context_bmap[i + 1] = 0;
  555. mmu_context_bmap[i + 2] = 0;
  556. mmu_context_bmap[i + 3] = 0;
  557. }
  558. new_version = 1;
  559. goto out;
  560. }
  561. }
  562. mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
  563. new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
  564. out:
  565. tlb_context_cache = new_ctx;
  566. mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
  567. spin_unlock_irqrestore(&ctx_alloc_lock, flags);
  568. if (unlikely(new_version))
  569. smp_new_mmu_context_version();
  570. }
  571. static int numa_enabled = 1;
  572. static int numa_debug;
  573. static int __init early_numa(char *p)
  574. {
  575. if (!p)
  576. return 0;
  577. if (strstr(p, "off"))
  578. numa_enabled = 0;
  579. if (strstr(p, "debug"))
  580. numa_debug = 1;
  581. return 0;
  582. }
  583. early_param("numa", early_numa);
  584. #define numadbg(f, a...) \
  585. do { if (numa_debug) \
  586. printk(KERN_INFO f, ## a); \
  587. } while (0)
  588. static void __init find_ramdisk(unsigned long phys_base)
  589. {
  590. #ifdef CONFIG_BLK_DEV_INITRD
  591. if (sparc_ramdisk_image || sparc_ramdisk_image64) {
  592. unsigned long ramdisk_image;
  593. /* Older versions of the bootloader only supported a
  594. * 32-bit physical address for the ramdisk image
  595. * location, stored at sparc_ramdisk_image. Newer
  596. * SILO versions set sparc_ramdisk_image to zero and
  597. * provide a full 64-bit physical address at
  598. * sparc_ramdisk_image64.
  599. */
  600. ramdisk_image = sparc_ramdisk_image;
  601. if (!ramdisk_image)
  602. ramdisk_image = sparc_ramdisk_image64;
  603. /* Another bootloader quirk. The bootloader normalizes
  604. * the physical address to KERNBASE, so we have to
  605. * factor that back out and add in the lowest valid
  606. * physical page address to get the true physical address.
  607. */
  608. ramdisk_image -= KERNBASE;
  609. ramdisk_image += phys_base;
  610. numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
  611. ramdisk_image, sparc_ramdisk_size);
  612. initrd_start = ramdisk_image;
  613. initrd_end = ramdisk_image + sparc_ramdisk_size;
  614. memblock_reserve(initrd_start, sparc_ramdisk_size);
  615. initrd_start += PAGE_OFFSET;
  616. initrd_end += PAGE_OFFSET;
  617. }
  618. #endif
  619. }
  620. struct node_mem_mask {
  621. unsigned long mask;
  622. unsigned long val;
  623. unsigned long bootmem_paddr;
  624. };
  625. static struct node_mem_mask node_masks[MAX_NUMNODES];
  626. static int num_node_masks;
  627. int numa_cpu_lookup_table[NR_CPUS];
  628. cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
  629. #ifdef CONFIG_NEED_MULTIPLE_NODES
  630. struct mdesc_mblock {
  631. u64 base;
  632. u64 size;
  633. u64 offset; /* RA-to-PA */
  634. };
  635. static struct mdesc_mblock *mblocks;
  636. static int num_mblocks;
  637. static unsigned long ra_to_pa(unsigned long addr)
  638. {
  639. int i;
  640. for (i = 0; i < num_mblocks; i++) {
  641. struct mdesc_mblock *m = &mblocks[i];
  642. if (addr >= m->base &&
  643. addr < (m->base + m->size)) {
  644. addr += m->offset;
  645. break;
  646. }
  647. }
  648. return addr;
  649. }
  650. static int find_node(unsigned long addr)
  651. {
  652. int i;
  653. addr = ra_to_pa(addr);
  654. for (i = 0; i < num_node_masks; i++) {
  655. struct node_mem_mask *p = &node_masks[i];
  656. if ((addr & p->mask) == p->val)
  657. return i;
  658. }
  659. return -1;
  660. }
  661. u64 memblock_nid_range(u64 start, u64 end, int *nid)
  662. {
  663. *nid = find_node(start);
  664. start += PAGE_SIZE;
  665. while (start < end) {
  666. int n = find_node(start);
  667. if (n != *nid)
  668. break;
  669. start += PAGE_SIZE;
  670. }
  671. if (start > end)
  672. start = end;
  673. return start;
  674. }
  675. #else
  676. u64 memblock_nid_range(u64 start, u64 end, int *nid)
  677. {
  678. *nid = 0;
  679. return end;
  680. }
  681. #endif
  682. /* This must be invoked after performing all of the necessary
  683. * add_active_range() calls for 'nid'. We need to be able to get
  684. * correct data from get_pfn_range_for_nid().
  685. */
  686. static void __init allocate_node_data(int nid)
  687. {
  688. unsigned long paddr, num_pages, start_pfn, end_pfn;
  689. struct pglist_data *p;
  690. #ifdef CONFIG_NEED_MULTIPLE_NODES
  691. paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
  692. if (!paddr) {
  693. prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
  694. prom_halt();
  695. }
  696. NODE_DATA(nid) = __va(paddr);
  697. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  698. NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
  699. #endif
  700. p = NODE_DATA(nid);
  701. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  702. p->node_start_pfn = start_pfn;
  703. p->node_spanned_pages = end_pfn - start_pfn;
  704. if (p->node_spanned_pages) {
  705. num_pages = bootmem_bootmap_pages(p->node_spanned_pages);
  706. paddr = memblock_alloc_try_nid(num_pages << PAGE_SHIFT, PAGE_SIZE, nid);
  707. if (!paddr) {
  708. prom_printf("Cannot allocate bootmap for nid[%d]\n",
  709. nid);
  710. prom_halt();
  711. }
  712. node_masks[nid].bootmem_paddr = paddr;
  713. }
  714. }
  715. static void init_node_masks_nonnuma(void)
  716. {
  717. int i;
  718. numadbg("Initializing tables for non-numa.\n");
  719. node_masks[0].mask = node_masks[0].val = 0;
  720. num_node_masks = 1;
  721. for (i = 0; i < NR_CPUS; i++)
  722. numa_cpu_lookup_table[i] = 0;
  723. numa_cpumask_lookup_table[0] = CPU_MASK_ALL;
  724. }
  725. #ifdef CONFIG_NEED_MULTIPLE_NODES
  726. struct pglist_data *node_data[MAX_NUMNODES];
  727. EXPORT_SYMBOL(numa_cpu_lookup_table);
  728. EXPORT_SYMBOL(numa_cpumask_lookup_table);
  729. EXPORT_SYMBOL(node_data);
  730. struct mdesc_mlgroup {
  731. u64 node;
  732. u64 latency;
  733. u64 match;
  734. u64 mask;
  735. };
  736. static struct mdesc_mlgroup *mlgroups;
  737. static int num_mlgroups;
  738. static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
  739. u32 cfg_handle)
  740. {
  741. u64 arc;
  742. mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
  743. u64 target = mdesc_arc_target(md, arc);
  744. const u64 *val;
  745. val = mdesc_get_property(md, target,
  746. "cfg-handle", NULL);
  747. if (val && *val == cfg_handle)
  748. return 0;
  749. }
  750. return -ENODEV;
  751. }
  752. static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
  753. u32 cfg_handle)
  754. {
  755. u64 arc, candidate, best_latency = ~(u64)0;
  756. candidate = MDESC_NODE_NULL;
  757. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
  758. u64 target = mdesc_arc_target(md, arc);
  759. const char *name = mdesc_node_name(md, target);
  760. const u64 *val;
  761. if (strcmp(name, "pio-latency-group"))
  762. continue;
  763. val = mdesc_get_property(md, target, "latency", NULL);
  764. if (!val)
  765. continue;
  766. if (*val < best_latency) {
  767. candidate = target;
  768. best_latency = *val;
  769. }
  770. }
  771. if (candidate == MDESC_NODE_NULL)
  772. return -ENODEV;
  773. return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
  774. }
  775. int of_node_to_nid(struct device_node *dp)
  776. {
  777. const struct linux_prom64_registers *regs;
  778. struct mdesc_handle *md;
  779. u32 cfg_handle;
  780. int count, nid;
  781. u64 grp;
  782. /* This is the right thing to do on currently supported
  783. * SUN4U NUMA platforms as well, as the PCI controller does
  784. * not sit behind any particular memory controller.
  785. */
  786. if (!mlgroups)
  787. return -1;
  788. regs = of_get_property(dp, "reg", NULL);
  789. if (!regs)
  790. return -1;
  791. cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
  792. md = mdesc_grab();
  793. count = 0;
  794. nid = -1;
  795. mdesc_for_each_node_by_name(md, grp, "group") {
  796. if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
  797. nid = count;
  798. break;
  799. }
  800. count++;
  801. }
  802. mdesc_release(md);
  803. return nid;
  804. }
  805. static void __init add_node_ranges(void)
  806. {
  807. struct memblock_region *reg;
  808. for_each_memblock(memory, reg) {
  809. unsigned long size = reg->size;
  810. unsigned long start, end;
  811. start = reg->base;
  812. end = start + size;
  813. while (start < end) {
  814. unsigned long this_end;
  815. int nid;
  816. this_end = memblock_nid_range(start, end, &nid);
  817. numadbg("Adding active range nid[%d] "
  818. "start[%lx] end[%lx]\n",
  819. nid, start, this_end);
  820. add_active_range(nid,
  821. start >> PAGE_SHIFT,
  822. this_end >> PAGE_SHIFT);
  823. start = this_end;
  824. }
  825. }
  826. }
  827. static int __init grab_mlgroups(struct mdesc_handle *md)
  828. {
  829. unsigned long paddr;
  830. int count = 0;
  831. u64 node;
  832. mdesc_for_each_node_by_name(md, node, "memory-latency-group")
  833. count++;
  834. if (!count)
  835. return -ENOENT;
  836. paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
  837. SMP_CACHE_BYTES);
  838. if (!paddr)
  839. return -ENOMEM;
  840. mlgroups = __va(paddr);
  841. num_mlgroups = count;
  842. count = 0;
  843. mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
  844. struct mdesc_mlgroup *m = &mlgroups[count++];
  845. const u64 *val;
  846. m->node = node;
  847. val = mdesc_get_property(md, node, "latency", NULL);
  848. m->latency = *val;
  849. val = mdesc_get_property(md, node, "address-match", NULL);
  850. m->match = *val;
  851. val = mdesc_get_property(md, node, "address-mask", NULL);
  852. m->mask = *val;
  853. numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
  854. "match[%llx] mask[%llx]\n",
  855. count - 1, m->node, m->latency, m->match, m->mask);
  856. }
  857. return 0;
  858. }
  859. static int __init grab_mblocks(struct mdesc_handle *md)
  860. {
  861. unsigned long paddr;
  862. int count = 0;
  863. u64 node;
  864. mdesc_for_each_node_by_name(md, node, "mblock")
  865. count++;
  866. if (!count)
  867. return -ENOENT;
  868. paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
  869. SMP_CACHE_BYTES);
  870. if (!paddr)
  871. return -ENOMEM;
  872. mblocks = __va(paddr);
  873. num_mblocks = count;
  874. count = 0;
  875. mdesc_for_each_node_by_name(md, node, "mblock") {
  876. struct mdesc_mblock *m = &mblocks[count++];
  877. const u64 *val;
  878. val = mdesc_get_property(md, node, "base", NULL);
  879. m->base = *val;
  880. val = mdesc_get_property(md, node, "size", NULL);
  881. m->size = *val;
  882. val = mdesc_get_property(md, node,
  883. "address-congruence-offset", NULL);
  884. m->offset = *val;
  885. numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
  886. count - 1, m->base, m->size, m->offset);
  887. }
  888. return 0;
  889. }
  890. static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
  891. u64 grp, cpumask_t *mask)
  892. {
  893. u64 arc;
  894. cpus_clear(*mask);
  895. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
  896. u64 target = mdesc_arc_target(md, arc);
  897. const char *name = mdesc_node_name(md, target);
  898. const u64 *id;
  899. if (strcmp(name, "cpu"))
  900. continue;
  901. id = mdesc_get_property(md, target, "id", NULL);
  902. if (*id < nr_cpu_ids)
  903. cpu_set(*id, *mask);
  904. }
  905. }
  906. static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
  907. {
  908. int i;
  909. for (i = 0; i < num_mlgroups; i++) {
  910. struct mdesc_mlgroup *m = &mlgroups[i];
  911. if (m->node == node)
  912. return m;
  913. }
  914. return NULL;
  915. }
  916. static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
  917. int index)
  918. {
  919. struct mdesc_mlgroup *candidate = NULL;
  920. u64 arc, best_latency = ~(u64)0;
  921. struct node_mem_mask *n;
  922. mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
  923. u64 target = mdesc_arc_target(md, arc);
  924. struct mdesc_mlgroup *m = find_mlgroup(target);
  925. if (!m)
  926. continue;
  927. if (m->latency < best_latency) {
  928. candidate = m;
  929. best_latency = m->latency;
  930. }
  931. }
  932. if (!candidate)
  933. return -ENOENT;
  934. if (num_node_masks != index) {
  935. printk(KERN_ERR "Inconsistent NUMA state, "
  936. "index[%d] != num_node_masks[%d]\n",
  937. index, num_node_masks);
  938. return -EINVAL;
  939. }
  940. n = &node_masks[num_node_masks++];
  941. n->mask = candidate->mask;
  942. n->val = candidate->match;
  943. numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%llx])\n",
  944. index, n->mask, n->val, candidate->latency);
  945. return 0;
  946. }
  947. static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
  948. int index)
  949. {
  950. cpumask_t mask;
  951. int cpu;
  952. numa_parse_mdesc_group_cpus(md, grp, &mask);
  953. for_each_cpu_mask(cpu, mask)
  954. numa_cpu_lookup_table[cpu] = index;
  955. numa_cpumask_lookup_table[index] = mask;
  956. if (numa_debug) {
  957. printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
  958. for_each_cpu_mask(cpu, mask)
  959. printk("%d ", cpu);
  960. printk("]\n");
  961. }
  962. return numa_attach_mlgroup(md, grp, index);
  963. }
  964. static int __init numa_parse_mdesc(void)
  965. {
  966. struct mdesc_handle *md = mdesc_grab();
  967. int i, err, count;
  968. u64 node;
  969. node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
  970. if (node == MDESC_NODE_NULL) {
  971. mdesc_release(md);
  972. return -ENOENT;
  973. }
  974. err = grab_mblocks(md);
  975. if (err < 0)
  976. goto out;
  977. err = grab_mlgroups(md);
  978. if (err < 0)
  979. goto out;
  980. count = 0;
  981. mdesc_for_each_node_by_name(md, node, "group") {
  982. err = numa_parse_mdesc_group(md, node, count);
  983. if (err < 0)
  984. break;
  985. count++;
  986. }
  987. add_node_ranges();
  988. for (i = 0; i < num_node_masks; i++) {
  989. allocate_node_data(i);
  990. node_set_online(i);
  991. }
  992. err = 0;
  993. out:
  994. mdesc_release(md);
  995. return err;
  996. }
  997. static int __init numa_parse_jbus(void)
  998. {
  999. unsigned long cpu, index;
  1000. /* NUMA node id is encoded in bits 36 and higher, and there is
  1001. * a 1-to-1 mapping from CPU ID to NUMA node ID.
  1002. */
  1003. index = 0;
  1004. for_each_present_cpu(cpu) {
  1005. numa_cpu_lookup_table[cpu] = index;
  1006. numa_cpumask_lookup_table[index] = cpumask_of_cpu(cpu);
  1007. node_masks[index].mask = ~((1UL << 36UL) - 1UL);
  1008. node_masks[index].val = cpu << 36UL;
  1009. index++;
  1010. }
  1011. num_node_masks = index;
  1012. add_node_ranges();
  1013. for (index = 0; index < num_node_masks; index++) {
  1014. allocate_node_data(index);
  1015. node_set_online(index);
  1016. }
  1017. return 0;
  1018. }
  1019. static int __init numa_parse_sun4u(void)
  1020. {
  1021. if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1022. unsigned long ver;
  1023. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  1024. if ((ver >> 32UL) == __JALAPENO_ID ||
  1025. (ver >> 32UL) == __SERRANO_ID)
  1026. return numa_parse_jbus();
  1027. }
  1028. return -1;
  1029. }
  1030. static int __init bootmem_init_numa(void)
  1031. {
  1032. int err = -1;
  1033. numadbg("bootmem_init_numa()\n");
  1034. if (numa_enabled) {
  1035. if (tlb_type == hypervisor)
  1036. err = numa_parse_mdesc();
  1037. else
  1038. err = numa_parse_sun4u();
  1039. }
  1040. return err;
  1041. }
  1042. #else
  1043. static int bootmem_init_numa(void)
  1044. {
  1045. return -1;
  1046. }
  1047. #endif
  1048. static void __init bootmem_init_nonnuma(void)
  1049. {
  1050. unsigned long top_of_ram = memblock_end_of_DRAM();
  1051. unsigned long total_ram = memblock_phys_mem_size();
  1052. struct memblock_region *reg;
  1053. numadbg("bootmem_init_nonnuma()\n");
  1054. printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  1055. top_of_ram, total_ram);
  1056. printk(KERN_INFO "Memory hole size: %ldMB\n",
  1057. (top_of_ram - total_ram) >> 20);
  1058. init_node_masks_nonnuma();
  1059. for_each_memblock(memory, reg) {
  1060. unsigned long start_pfn, end_pfn;
  1061. if (!reg->size)
  1062. continue;
  1063. start_pfn = memblock_region_memory_base_pfn(reg);
  1064. end_pfn = memblock_region_memory_end_pfn(reg);
  1065. add_active_range(0, start_pfn, end_pfn);
  1066. }
  1067. allocate_node_data(0);
  1068. node_set_online(0);
  1069. }
  1070. static void __init reserve_range_in_node(int nid, unsigned long start,
  1071. unsigned long end)
  1072. {
  1073. numadbg(" reserve_range_in_node(nid[%d],start[%lx],end[%lx]\n",
  1074. nid, start, end);
  1075. while (start < end) {
  1076. unsigned long this_end;
  1077. int n;
  1078. this_end = memblock_nid_range(start, end, &n);
  1079. if (n == nid) {
  1080. numadbg(" MATCH reserving range [%lx:%lx]\n",
  1081. start, this_end);
  1082. reserve_bootmem_node(NODE_DATA(nid), start,
  1083. (this_end - start), BOOTMEM_DEFAULT);
  1084. } else
  1085. numadbg(" NO MATCH, advancing start to %lx\n",
  1086. this_end);
  1087. start = this_end;
  1088. }
  1089. }
  1090. static void __init trim_reserved_in_node(int nid)
  1091. {
  1092. struct memblock_region *reg;
  1093. numadbg(" trim_reserved_in_node(%d)\n", nid);
  1094. for_each_memblock(reserved, reg)
  1095. reserve_range_in_node(nid, reg->base, reg->base + reg->size);
  1096. }
  1097. static void __init bootmem_init_one_node(int nid)
  1098. {
  1099. struct pglist_data *p;
  1100. numadbg("bootmem_init_one_node(%d)\n", nid);
  1101. p = NODE_DATA(nid);
  1102. if (p->node_spanned_pages) {
  1103. unsigned long paddr = node_masks[nid].bootmem_paddr;
  1104. unsigned long end_pfn;
  1105. end_pfn = p->node_start_pfn + p->node_spanned_pages;
  1106. numadbg(" init_bootmem_node(%d, %lx, %lx, %lx)\n",
  1107. nid, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);
  1108. init_bootmem_node(p, paddr >> PAGE_SHIFT,
  1109. p->node_start_pfn, end_pfn);
  1110. numadbg(" free_bootmem_with_active_regions(%d, %lx)\n",
  1111. nid, end_pfn);
  1112. free_bootmem_with_active_regions(nid, end_pfn);
  1113. trim_reserved_in_node(nid);
  1114. numadbg(" sparse_memory_present_with_active_regions(%d)\n",
  1115. nid);
  1116. sparse_memory_present_with_active_regions(nid);
  1117. }
  1118. }
  1119. static unsigned long __init bootmem_init(unsigned long phys_base)
  1120. {
  1121. unsigned long end_pfn;
  1122. int nid;
  1123. end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  1124. max_pfn = max_low_pfn = end_pfn;
  1125. min_low_pfn = (phys_base >> PAGE_SHIFT);
  1126. if (bootmem_init_numa() < 0)
  1127. bootmem_init_nonnuma();
  1128. /* XXX cpu notifier XXX */
  1129. for_each_online_node(nid)
  1130. bootmem_init_one_node(nid);
  1131. sparse_init();
  1132. return end_pfn;
  1133. }
  1134. static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
  1135. static int pall_ents __initdata;
  1136. #ifdef CONFIG_DEBUG_PAGEALLOC
  1137. static unsigned long __ref kernel_map_range(unsigned long pstart,
  1138. unsigned long pend, pgprot_t prot)
  1139. {
  1140. unsigned long vstart = PAGE_OFFSET + pstart;
  1141. unsigned long vend = PAGE_OFFSET + pend;
  1142. unsigned long alloc_bytes = 0UL;
  1143. if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
  1144. prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
  1145. vstart, vend);
  1146. prom_halt();
  1147. }
  1148. while (vstart < vend) {
  1149. unsigned long this_end, paddr = __pa(vstart);
  1150. pgd_t *pgd = pgd_offset_k(vstart);
  1151. pud_t *pud;
  1152. pmd_t *pmd;
  1153. pte_t *pte;
  1154. pud = pud_offset(pgd, vstart);
  1155. if (pud_none(*pud)) {
  1156. pmd_t *new;
  1157. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1158. alloc_bytes += PAGE_SIZE;
  1159. pud_populate(&init_mm, pud, new);
  1160. }
  1161. pmd = pmd_offset(pud, vstart);
  1162. if (!pmd_present(*pmd)) {
  1163. pte_t *new;
  1164. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1165. alloc_bytes += PAGE_SIZE;
  1166. pmd_populate_kernel(&init_mm, pmd, new);
  1167. }
  1168. pte = pte_offset_kernel(pmd, vstart);
  1169. this_end = (vstart + PMD_SIZE) & PMD_MASK;
  1170. if (this_end > vend)
  1171. this_end = vend;
  1172. while (vstart < this_end) {
  1173. pte_val(*pte) = (paddr | pgprot_val(prot));
  1174. vstart += PAGE_SIZE;
  1175. paddr += PAGE_SIZE;
  1176. pte++;
  1177. }
  1178. }
  1179. return alloc_bytes;
  1180. }
  1181. extern unsigned int kvmap_linear_patch[1];
  1182. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1183. static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
  1184. {
  1185. const unsigned long shift_256MB = 28;
  1186. const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
  1187. const unsigned long size_256MB = (1UL << shift_256MB);
  1188. while (start < end) {
  1189. long remains;
  1190. remains = end - start;
  1191. if (remains < size_256MB)
  1192. break;
  1193. if (start & mask_256MB) {
  1194. start = (start + size_256MB) & ~mask_256MB;
  1195. continue;
  1196. }
  1197. while (remains >= size_256MB) {
  1198. unsigned long index = start >> shift_256MB;
  1199. __set_bit(index, kpte_linear_bitmap);
  1200. start += size_256MB;
  1201. remains -= size_256MB;
  1202. }
  1203. }
  1204. }
  1205. static void __init init_kpte_bitmap(void)
  1206. {
  1207. unsigned long i;
  1208. for (i = 0; i < pall_ents; i++) {
  1209. unsigned long phys_start, phys_end;
  1210. phys_start = pall[i].phys_addr;
  1211. phys_end = phys_start + pall[i].reg_size;
  1212. mark_kpte_bitmap(phys_start, phys_end);
  1213. }
  1214. }
  1215. static void __init kernel_physical_mapping_init(void)
  1216. {
  1217. #ifdef CONFIG_DEBUG_PAGEALLOC
  1218. unsigned long i, mem_alloced = 0UL;
  1219. for (i = 0; i < pall_ents; i++) {
  1220. unsigned long phys_start, phys_end;
  1221. phys_start = pall[i].phys_addr;
  1222. phys_end = phys_start + pall[i].reg_size;
  1223. mem_alloced += kernel_map_range(phys_start, phys_end,
  1224. PAGE_KERNEL);
  1225. }
  1226. printk("Allocated %ld bytes for kernel page tables.\n",
  1227. mem_alloced);
  1228. kvmap_linear_patch[0] = 0x01000000; /* nop */
  1229. flushi(&kvmap_linear_patch[0]);
  1230. __flush_tlb_all();
  1231. #endif
  1232. }
  1233. #ifdef CONFIG_DEBUG_PAGEALLOC
  1234. void kernel_map_pages(struct page *page, int numpages, int enable)
  1235. {
  1236. unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
  1237. unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
  1238. kernel_map_range(phys_start, phys_end,
  1239. (enable ? PAGE_KERNEL : __pgprot(0)));
  1240. flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
  1241. PAGE_OFFSET + phys_end);
  1242. /* we should perform an IPI and flush all tlbs,
  1243. * but that can deadlock->flush only current cpu.
  1244. */
  1245. __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
  1246. PAGE_OFFSET + phys_end);
  1247. }
  1248. #endif
  1249. unsigned long __init find_ecache_flush_span(unsigned long size)
  1250. {
  1251. int i;
  1252. for (i = 0; i < pavail_ents; i++) {
  1253. if (pavail[i].reg_size >= size)
  1254. return pavail[i].phys_addr;
  1255. }
  1256. return ~0UL;
  1257. }
  1258. static void __init tsb_phys_patch(void)
  1259. {
  1260. struct tsb_ldquad_phys_patch_entry *pquad;
  1261. struct tsb_phys_patch_entry *p;
  1262. pquad = &__tsb_ldquad_phys_patch;
  1263. while (pquad < &__tsb_ldquad_phys_patch_end) {
  1264. unsigned long addr = pquad->addr;
  1265. if (tlb_type == hypervisor)
  1266. *(unsigned int *) addr = pquad->sun4v_insn;
  1267. else
  1268. *(unsigned int *) addr = pquad->sun4u_insn;
  1269. wmb();
  1270. __asm__ __volatile__("flush %0"
  1271. : /* no outputs */
  1272. : "r" (addr));
  1273. pquad++;
  1274. }
  1275. p = &__tsb_phys_patch;
  1276. while (p < &__tsb_phys_patch_end) {
  1277. unsigned long addr = p->addr;
  1278. *(unsigned int *) addr = p->insn;
  1279. wmb();
  1280. __asm__ __volatile__("flush %0"
  1281. : /* no outputs */
  1282. : "r" (addr));
  1283. p++;
  1284. }
  1285. }
  1286. /* Don't mark as init, we give this to the Hypervisor. */
  1287. #ifndef CONFIG_DEBUG_PAGEALLOC
  1288. #define NUM_KTSB_DESCR 2
  1289. #else
  1290. #define NUM_KTSB_DESCR 1
  1291. #endif
  1292. static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
  1293. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  1294. static void __init sun4v_ktsb_init(void)
  1295. {
  1296. unsigned long ktsb_pa;
  1297. /* First KTSB for PAGE_SIZE mappings. */
  1298. ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
  1299. switch (PAGE_SIZE) {
  1300. case 8 * 1024:
  1301. default:
  1302. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
  1303. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
  1304. break;
  1305. case 64 * 1024:
  1306. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
  1307. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
  1308. break;
  1309. case 512 * 1024:
  1310. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
  1311. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
  1312. break;
  1313. case 4 * 1024 * 1024:
  1314. ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
  1315. ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
  1316. break;
  1317. };
  1318. ktsb_descr[0].assoc = 1;
  1319. ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
  1320. ktsb_descr[0].ctx_idx = 0;
  1321. ktsb_descr[0].tsb_base = ktsb_pa;
  1322. ktsb_descr[0].resv = 0;
  1323. #ifndef CONFIG_DEBUG_PAGEALLOC
  1324. /* Second KTSB for 4MB/256MB mappings. */
  1325. ktsb_pa = (kern_base +
  1326. ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
  1327. ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
  1328. ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
  1329. HV_PGSZ_MASK_256MB);
  1330. ktsb_descr[1].assoc = 1;
  1331. ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
  1332. ktsb_descr[1].ctx_idx = 0;
  1333. ktsb_descr[1].tsb_base = ktsb_pa;
  1334. ktsb_descr[1].resv = 0;
  1335. #endif
  1336. }
  1337. void __cpuinit sun4v_ktsb_register(void)
  1338. {
  1339. unsigned long pa, ret;
  1340. pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
  1341. ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
  1342. if (ret != 0) {
  1343. prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
  1344. "errors with %lx\n", pa, ret);
  1345. prom_halt();
  1346. }
  1347. }
  1348. /* paging_init() sets up the page tables */
  1349. static unsigned long last_valid_pfn;
  1350. pgd_t swapper_pg_dir[2048];
  1351. static void sun4u_pgprot_init(void);
  1352. static void sun4v_pgprot_init(void);
  1353. void __init paging_init(void)
  1354. {
  1355. unsigned long end_pfn, shift, phys_base;
  1356. unsigned long real_end, i;
  1357. /* These build time checkes make sure that the dcache_dirty_cpu()
  1358. * page->flags usage will work.
  1359. *
  1360. * When a page gets marked as dcache-dirty, we store the
  1361. * cpu number starting at bit 32 in the page->flags. Also,
  1362. * functions like clear_dcache_dirty_cpu use the cpu mask
  1363. * in 13-bit signed-immediate instruction fields.
  1364. */
  1365. /*
  1366. * Page flags must not reach into upper 32 bits that are used
  1367. * for the cpu number
  1368. */
  1369. BUILD_BUG_ON(NR_PAGEFLAGS > 32);
  1370. /*
  1371. * The bit fields placed in the high range must not reach below
  1372. * the 32 bit boundary. Otherwise we cannot place the cpu field
  1373. * at the 32 bit boundary.
  1374. */
  1375. BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
  1376. ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
  1377. BUILD_BUG_ON(NR_CPUS > 4096);
  1378. kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
  1379. kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
  1380. /* Invalidate both kernel TSBs. */
  1381. memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
  1382. #ifndef CONFIG_DEBUG_PAGEALLOC
  1383. memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
  1384. #endif
  1385. if (tlb_type == hypervisor)
  1386. sun4v_pgprot_init();
  1387. else
  1388. sun4u_pgprot_init();
  1389. if (tlb_type == cheetah_plus ||
  1390. tlb_type == hypervisor)
  1391. tsb_phys_patch();
  1392. if (tlb_type == hypervisor) {
  1393. sun4v_patch_tlb_handlers();
  1394. sun4v_ktsb_init();
  1395. }
  1396. memblock_init();
  1397. /* Find available physical memory...
  1398. *
  1399. * Read it twice in order to work around a bug in openfirmware.
  1400. * The call to grab this table itself can cause openfirmware to
  1401. * allocate memory, which in turn can take away some space from
  1402. * the list of available memory. Reading it twice makes sure
  1403. * we really do get the final value.
  1404. */
  1405. read_obp_translations();
  1406. read_obp_memory("reg", &pall[0], &pall_ents);
  1407. read_obp_memory("available", &pavail[0], &pavail_ents);
  1408. read_obp_memory("available", &pavail[0], &pavail_ents);
  1409. phys_base = 0xffffffffffffffffUL;
  1410. for (i = 0; i < pavail_ents; i++) {
  1411. phys_base = min(phys_base, pavail[i].phys_addr);
  1412. memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
  1413. }
  1414. memblock_reserve(kern_base, kern_size);
  1415. find_ramdisk(phys_base);
  1416. memblock_enforce_memory_limit(cmdline_memory_size);
  1417. memblock_analyze();
  1418. memblock_dump_all();
  1419. set_bit(0, mmu_context_bmap);
  1420. shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
  1421. real_end = (unsigned long)_end;
  1422. num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
  1423. printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
  1424. num_kernel_image_mappings);
  1425. /* Set kernel pgd to upper alias so physical page computations
  1426. * work.
  1427. */
  1428. init_mm.pgd += ((shift) / (sizeof(pgd_t)));
  1429. memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
  1430. /* Now can init the kernel/bad page tables. */
  1431. pud_set(pud_offset(&swapper_pg_dir[0], 0),
  1432. swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
  1433. inherit_prom_mappings();
  1434. init_kpte_bitmap();
  1435. /* Ok, we can use our TLB miss and window trap handlers safely. */
  1436. setup_tba();
  1437. __flush_tlb_all();
  1438. if (tlb_type == hypervisor)
  1439. sun4v_ktsb_register();
  1440. prom_build_devicetree();
  1441. of_populate_present_mask();
  1442. #ifndef CONFIG_SMP
  1443. of_fill_in_cpu_data();
  1444. #endif
  1445. if (tlb_type == hypervisor) {
  1446. sun4v_mdesc_init();
  1447. mdesc_populate_present_mask(cpu_all_mask);
  1448. #ifndef CONFIG_SMP
  1449. mdesc_fill_in_cpu_data(cpu_all_mask);
  1450. #endif
  1451. }
  1452. /* Once the OF device tree and MDESC have been setup, we know
  1453. * the list of possible cpus. Therefore we can allocate the
  1454. * IRQ stacks.
  1455. */
  1456. for_each_possible_cpu(i) {
  1457. /* XXX Use node local allocations... XXX */
  1458. softirq_stack[i] = __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
  1459. hardirq_stack[i] = __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
  1460. }
  1461. /* Setup bootmem... */
  1462. last_valid_pfn = end_pfn = bootmem_init(phys_base);
  1463. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1464. max_mapnr = last_valid_pfn;
  1465. #endif
  1466. kernel_physical_mapping_init();
  1467. {
  1468. unsigned long max_zone_pfns[MAX_NR_ZONES];
  1469. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  1470. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  1471. free_area_init_nodes(max_zone_pfns);
  1472. }
  1473. printk("Booting Linux...\n");
  1474. }
  1475. int __devinit page_in_phys_avail(unsigned long paddr)
  1476. {
  1477. int i;
  1478. paddr &= PAGE_MASK;
  1479. for (i = 0; i < pavail_ents; i++) {
  1480. unsigned long start, end;
  1481. start = pavail[i].phys_addr;
  1482. end = start + pavail[i].reg_size;
  1483. if (paddr >= start && paddr < end)
  1484. return 1;
  1485. }
  1486. if (paddr >= kern_base && paddr < (kern_base + kern_size))
  1487. return 1;
  1488. #ifdef CONFIG_BLK_DEV_INITRD
  1489. if (paddr >= __pa(initrd_start) &&
  1490. paddr < __pa(PAGE_ALIGN(initrd_end)))
  1491. return 1;
  1492. #endif
  1493. return 0;
  1494. }
  1495. static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
  1496. static int pavail_rescan_ents __initdata;
  1497. /* Certain OBP calls, such as fetching "available" properties, can
  1498. * claim physical memory. So, along with initializing the valid
  1499. * address bitmap, what we do here is refetch the physical available
  1500. * memory list again, and make sure it provides at least as much
  1501. * memory as 'pavail' does.
  1502. */
  1503. static void __init setup_valid_addr_bitmap_from_pavail(unsigned long *bitmap)
  1504. {
  1505. int i;
  1506. read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
  1507. for (i = 0; i < pavail_ents; i++) {
  1508. unsigned long old_start, old_end;
  1509. old_start = pavail[i].phys_addr;
  1510. old_end = old_start + pavail[i].reg_size;
  1511. while (old_start < old_end) {
  1512. int n;
  1513. for (n = 0; n < pavail_rescan_ents; n++) {
  1514. unsigned long new_start, new_end;
  1515. new_start = pavail_rescan[n].phys_addr;
  1516. new_end = new_start +
  1517. pavail_rescan[n].reg_size;
  1518. if (new_start <= old_start &&
  1519. new_end >= (old_start + PAGE_SIZE)) {
  1520. set_bit(old_start >> 22, bitmap);
  1521. goto do_next_page;
  1522. }
  1523. }
  1524. prom_printf("mem_init: Lost memory in pavail\n");
  1525. prom_printf("mem_init: OLD start[%lx] size[%lx]\n",
  1526. pavail[i].phys_addr,
  1527. pavail[i].reg_size);
  1528. prom_printf("mem_init: NEW start[%lx] size[%lx]\n",
  1529. pavail_rescan[i].phys_addr,
  1530. pavail_rescan[i].reg_size);
  1531. prom_printf("mem_init: Cannot continue, aborting.\n");
  1532. prom_halt();
  1533. do_next_page:
  1534. old_start += PAGE_SIZE;
  1535. }
  1536. }
  1537. }
  1538. static void __init patch_tlb_miss_handler_bitmap(void)
  1539. {
  1540. extern unsigned int valid_addr_bitmap_insn[];
  1541. extern unsigned int valid_addr_bitmap_patch[];
  1542. valid_addr_bitmap_insn[1] = valid_addr_bitmap_patch[1];
  1543. mb();
  1544. valid_addr_bitmap_insn[0] = valid_addr_bitmap_patch[0];
  1545. flushi(&valid_addr_bitmap_insn[0]);
  1546. }
  1547. void __init mem_init(void)
  1548. {
  1549. unsigned long codepages, datapages, initpages;
  1550. unsigned long addr, last;
  1551. addr = PAGE_OFFSET + kern_base;
  1552. last = PAGE_ALIGN(kern_size) + addr;
  1553. while (addr < last) {
  1554. set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
  1555. addr += PAGE_SIZE;
  1556. }
  1557. setup_valid_addr_bitmap_from_pavail(sparc64_valid_addr_bitmap);
  1558. patch_tlb_miss_handler_bitmap();
  1559. high_memory = __va(last_valid_pfn << PAGE_SHIFT);
  1560. #ifdef CONFIG_NEED_MULTIPLE_NODES
  1561. {
  1562. int i;
  1563. for_each_online_node(i) {
  1564. if (NODE_DATA(i)->node_spanned_pages != 0) {
  1565. totalram_pages +=
  1566. free_all_bootmem_node(NODE_DATA(i));
  1567. }
  1568. }
  1569. }
  1570. #else
  1571. totalram_pages = free_all_bootmem();
  1572. #endif
  1573. /* We subtract one to account for the mem_map_zero page
  1574. * allocated below.
  1575. */
  1576. totalram_pages -= 1;
  1577. num_physpages = totalram_pages;
  1578. /*
  1579. * Set up the zero page, mark it reserved, so that page count
  1580. * is not manipulated when freeing the page from user ptes.
  1581. */
  1582. mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
  1583. if (mem_map_zero == NULL) {
  1584. prom_printf("paging_init: Cannot alloc zero page.\n");
  1585. prom_halt();
  1586. }
  1587. SetPageReserved(mem_map_zero);
  1588. codepages = (((unsigned long) _etext) - ((unsigned long) _start));
  1589. codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
  1590. datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
  1591. datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
  1592. initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
  1593. initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
  1594. printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
  1595. nr_free_pages() << (PAGE_SHIFT-10),
  1596. codepages << (PAGE_SHIFT-10),
  1597. datapages << (PAGE_SHIFT-10),
  1598. initpages << (PAGE_SHIFT-10),
  1599. PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
  1600. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1601. cheetah_ecache_flush_init();
  1602. }
  1603. void free_initmem(void)
  1604. {
  1605. unsigned long addr, initend;
  1606. int do_free = 1;
  1607. /* If the physical memory maps were trimmed by kernel command
  1608. * line options, don't even try freeing this initmem stuff up.
  1609. * The kernel image could have been in the trimmed out region
  1610. * and if so the freeing below will free invalid page structs.
  1611. */
  1612. if (cmdline_memory_size)
  1613. do_free = 0;
  1614. /*
  1615. * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
  1616. */
  1617. addr = PAGE_ALIGN((unsigned long)(__init_begin));
  1618. initend = (unsigned long)(__init_end) & PAGE_MASK;
  1619. for (; addr < initend; addr += PAGE_SIZE) {
  1620. unsigned long page;
  1621. struct page *p;
  1622. page = (addr +
  1623. ((unsigned long) __va(kern_base)) -
  1624. ((unsigned long) KERNBASE));
  1625. memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
  1626. if (do_free) {
  1627. p = virt_to_page(page);
  1628. ClearPageReserved(p);
  1629. init_page_count(p);
  1630. __free_page(p);
  1631. num_physpages++;
  1632. totalram_pages++;
  1633. }
  1634. }
  1635. }
  1636. #ifdef CONFIG_BLK_DEV_INITRD
  1637. void free_initrd_mem(unsigned long start, unsigned long end)
  1638. {
  1639. if (start < end)
  1640. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  1641. for (; start < end; start += PAGE_SIZE) {
  1642. struct page *p = virt_to_page(start);
  1643. ClearPageReserved(p);
  1644. init_page_count(p);
  1645. __free_page(p);
  1646. num_physpages++;
  1647. totalram_pages++;
  1648. }
  1649. }
  1650. #endif
  1651. #define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
  1652. #define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
  1653. #define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
  1654. #define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
  1655. #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
  1656. #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
  1657. pgprot_t PAGE_KERNEL __read_mostly;
  1658. EXPORT_SYMBOL(PAGE_KERNEL);
  1659. pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
  1660. pgprot_t PAGE_COPY __read_mostly;
  1661. pgprot_t PAGE_SHARED __read_mostly;
  1662. EXPORT_SYMBOL(PAGE_SHARED);
  1663. unsigned long pg_iobits __read_mostly;
  1664. unsigned long _PAGE_IE __read_mostly;
  1665. EXPORT_SYMBOL(_PAGE_IE);
  1666. unsigned long _PAGE_E __read_mostly;
  1667. EXPORT_SYMBOL(_PAGE_E);
  1668. unsigned long _PAGE_CACHE __read_mostly;
  1669. EXPORT_SYMBOL(_PAGE_CACHE);
  1670. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  1671. unsigned long vmemmap_table[VMEMMAP_SIZE];
  1672. int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
  1673. {
  1674. unsigned long vstart = (unsigned long) start;
  1675. unsigned long vend = (unsigned long) (start + nr);
  1676. unsigned long phys_start = (vstart - VMEMMAP_BASE);
  1677. unsigned long phys_end = (vend - VMEMMAP_BASE);
  1678. unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
  1679. unsigned long end = VMEMMAP_ALIGN(phys_end);
  1680. unsigned long pte_base;
  1681. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1682. _PAGE_CP_4U | _PAGE_CV_4U |
  1683. _PAGE_P_4U | _PAGE_W_4U);
  1684. if (tlb_type == hypervisor)
  1685. pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1686. _PAGE_CP_4V | _PAGE_CV_4V |
  1687. _PAGE_P_4V | _PAGE_W_4V);
  1688. for (; addr < end; addr += VMEMMAP_CHUNK) {
  1689. unsigned long *vmem_pp =
  1690. vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
  1691. void *block;
  1692. if (!(*vmem_pp & _PAGE_VALID)) {
  1693. block = vmemmap_alloc_block(1UL << 22, node);
  1694. if (!block)
  1695. return -ENOMEM;
  1696. *vmem_pp = pte_base | __pa(block);
  1697. printk(KERN_INFO "[%p-%p] page_structs=%lu "
  1698. "node=%d entry=%lu/%lu\n", start, block, nr,
  1699. node,
  1700. addr >> VMEMMAP_CHUNK_SHIFT,
  1701. VMEMMAP_SIZE);
  1702. }
  1703. }
  1704. return 0;
  1705. }
  1706. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  1707. static void prot_init_common(unsigned long page_none,
  1708. unsigned long page_shared,
  1709. unsigned long page_copy,
  1710. unsigned long page_readonly,
  1711. unsigned long page_exec_bit)
  1712. {
  1713. PAGE_COPY = __pgprot(page_copy);
  1714. PAGE_SHARED = __pgprot(page_shared);
  1715. protection_map[0x0] = __pgprot(page_none);
  1716. protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
  1717. protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
  1718. protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
  1719. protection_map[0x4] = __pgprot(page_readonly);
  1720. protection_map[0x5] = __pgprot(page_readonly);
  1721. protection_map[0x6] = __pgprot(page_copy);
  1722. protection_map[0x7] = __pgprot(page_copy);
  1723. protection_map[0x8] = __pgprot(page_none);
  1724. protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
  1725. protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
  1726. protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
  1727. protection_map[0xc] = __pgprot(page_readonly);
  1728. protection_map[0xd] = __pgprot(page_readonly);
  1729. protection_map[0xe] = __pgprot(page_shared);
  1730. protection_map[0xf] = __pgprot(page_shared);
  1731. }
  1732. static void __init sun4u_pgprot_init(void)
  1733. {
  1734. unsigned long page_none, page_shared, page_copy, page_readonly;
  1735. unsigned long page_exec_bit;
  1736. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1737. _PAGE_CACHE_4U | _PAGE_P_4U |
  1738. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1739. _PAGE_EXEC_4U);
  1740. PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
  1741. _PAGE_CACHE_4U | _PAGE_P_4U |
  1742. __ACCESS_BITS_4U | __DIRTY_BITS_4U |
  1743. _PAGE_EXEC_4U | _PAGE_L_4U);
  1744. _PAGE_IE = _PAGE_IE_4U;
  1745. _PAGE_E = _PAGE_E_4U;
  1746. _PAGE_CACHE = _PAGE_CACHE_4U;
  1747. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
  1748. __ACCESS_BITS_4U | _PAGE_E_4U);
  1749. #ifdef CONFIG_DEBUG_PAGEALLOC
  1750. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
  1751. 0xfffff80000000000UL;
  1752. #else
  1753. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
  1754. 0xfffff80000000000UL;
  1755. #endif
  1756. kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
  1757. _PAGE_P_4U | _PAGE_W_4U);
  1758. /* XXX Should use 256MB on Panther. XXX */
  1759. kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
  1760. _PAGE_SZBITS = _PAGE_SZBITS_4U;
  1761. _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
  1762. _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
  1763. _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
  1764. page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
  1765. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1766. __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
  1767. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1768. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1769. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
  1770. __ACCESS_BITS_4U | _PAGE_EXEC_4U);
  1771. page_exec_bit = _PAGE_EXEC_4U;
  1772. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1773. page_exec_bit);
  1774. }
  1775. static void __init sun4v_pgprot_init(void)
  1776. {
  1777. unsigned long page_none, page_shared, page_copy, page_readonly;
  1778. unsigned long page_exec_bit;
  1779. PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
  1780. _PAGE_CACHE_4V | _PAGE_P_4V |
  1781. __ACCESS_BITS_4V | __DIRTY_BITS_4V |
  1782. _PAGE_EXEC_4V);
  1783. PAGE_KERNEL_LOCKED = PAGE_KERNEL;
  1784. _PAGE_IE = _PAGE_IE_4V;
  1785. _PAGE_E = _PAGE_E_4V;
  1786. _PAGE_CACHE = _PAGE_CACHE_4V;
  1787. #ifdef CONFIG_DEBUG_PAGEALLOC
  1788. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1789. 0xfffff80000000000UL;
  1790. #else
  1791. kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
  1792. 0xfffff80000000000UL;
  1793. #endif
  1794. kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1795. _PAGE_P_4V | _PAGE_W_4V);
  1796. #ifdef CONFIG_DEBUG_PAGEALLOC
  1797. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
  1798. 0xfffff80000000000UL;
  1799. #else
  1800. kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
  1801. 0xfffff80000000000UL;
  1802. #endif
  1803. kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
  1804. _PAGE_P_4V | _PAGE_W_4V);
  1805. pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
  1806. __ACCESS_BITS_4V | _PAGE_E_4V);
  1807. _PAGE_SZBITS = _PAGE_SZBITS_4V;
  1808. _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
  1809. _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
  1810. _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
  1811. _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
  1812. page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
  1813. page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1814. __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
  1815. page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1816. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1817. page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
  1818. __ACCESS_BITS_4V | _PAGE_EXEC_4V);
  1819. page_exec_bit = _PAGE_EXEC_4V;
  1820. prot_init_common(page_none, page_shared, page_copy, page_readonly,
  1821. page_exec_bit);
  1822. }
  1823. unsigned long pte_sz_bits(unsigned long sz)
  1824. {
  1825. if (tlb_type == hypervisor) {
  1826. switch (sz) {
  1827. case 8 * 1024:
  1828. default:
  1829. return _PAGE_SZ8K_4V;
  1830. case 64 * 1024:
  1831. return _PAGE_SZ64K_4V;
  1832. case 512 * 1024:
  1833. return _PAGE_SZ512K_4V;
  1834. case 4 * 1024 * 1024:
  1835. return _PAGE_SZ4MB_4V;
  1836. };
  1837. } else {
  1838. switch (sz) {
  1839. case 8 * 1024:
  1840. default:
  1841. return _PAGE_SZ8K_4U;
  1842. case 64 * 1024:
  1843. return _PAGE_SZ64K_4U;
  1844. case 512 * 1024:
  1845. return _PAGE_SZ512K_4U;
  1846. case 4 * 1024 * 1024:
  1847. return _PAGE_SZ4MB_4U;
  1848. };
  1849. }
  1850. }
  1851. pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
  1852. {
  1853. pte_t pte;
  1854. pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
  1855. pte_val(pte) |= (((unsigned long)space) << 32);
  1856. pte_val(pte) |= pte_sz_bits(page_size);
  1857. return pte;
  1858. }
  1859. static unsigned long kern_large_tte(unsigned long paddr)
  1860. {
  1861. unsigned long val;
  1862. val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
  1863. _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
  1864. _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
  1865. if (tlb_type == hypervisor)
  1866. val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
  1867. _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
  1868. _PAGE_EXEC_4V | _PAGE_W_4V);
  1869. return val | paddr;
  1870. }
  1871. /* If not locked, zap it. */
  1872. void __flush_tlb_all(void)
  1873. {
  1874. unsigned long pstate;
  1875. int i;
  1876. __asm__ __volatile__("flushw\n\t"
  1877. "rdpr %%pstate, %0\n\t"
  1878. "wrpr %0, %1, %%pstate"
  1879. : "=r" (pstate)
  1880. : "i" (PSTATE_IE));
  1881. if (tlb_type == hypervisor) {
  1882. sun4v_mmu_demap_all();
  1883. } else if (tlb_type == spitfire) {
  1884. for (i = 0; i < 64; i++) {
  1885. /* Spitfire Errata #32 workaround */
  1886. /* NOTE: Always runs on spitfire, so no
  1887. * cheetah+ page size encodings.
  1888. */
  1889. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1890. "flush %%g6"
  1891. : /* No outputs */
  1892. : "r" (0),
  1893. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1894. if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
  1895. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1896. "membar #Sync"
  1897. : /* no outputs */
  1898. : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
  1899. spitfire_put_dtlb_data(i, 0x0UL);
  1900. }
  1901. /* Spitfire Errata #32 workaround */
  1902. /* NOTE: Always runs on spitfire, so no
  1903. * cheetah+ page size encodings.
  1904. */
  1905. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  1906. "flush %%g6"
  1907. : /* No outputs */
  1908. : "r" (0),
  1909. "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
  1910. if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
  1911. __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
  1912. "membar #Sync"
  1913. : /* no outputs */
  1914. : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
  1915. spitfire_put_itlb_data(i, 0x0UL);
  1916. }
  1917. }
  1918. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  1919. cheetah_flush_dtlb_all();
  1920. cheetah_flush_itlb_all();
  1921. }
  1922. __asm__ __volatile__("wrpr %0, 0, %%pstate"
  1923. : : "r" (pstate));
  1924. }