init_64.c 65 KB

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