init_64.c 72 KB

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