init_64.c 74 KB

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