init_64.c 68 KB

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