init_64.c 74 KB

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