init_64.c 78 KB

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