1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/kernel.h>
31#include <linux/export.h>
32#include <linux/mm.h>
33#include <linux/init.h>
34#include <linux/highmem.h>
35#include <linux/pagemap.h>
36#include <linux/preempt.h>
37#include <linux/spinlock.h>
38#include <linux/memblock.h>
39#include <linux/of_fdt.h>
40#include <linux/hugetlb.h>
41
42#include <asm/tlbflush.h>
43#include <asm/tlb.h>
44#include <asm/code-patching.h>
45#include <asm/cputhreads.h>
46#include <asm/hugetlb.h>
47#include <asm/paca.h>
48
49#include "mmu_decl.h"
50
51
52
53
54
55
56#ifdef CONFIG_PPC_BOOK3E_MMU
57#ifdef CONFIG_PPC_FSL_BOOK3E
58struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
59 [MMU_PAGE_4K] = {
60 .shift = 12,
61 .enc = BOOK3E_PAGESZ_4K,
62 },
63 [MMU_PAGE_2M] = {
64 .shift = 21,
65 .enc = BOOK3E_PAGESZ_2M,
66 },
67 [MMU_PAGE_4M] = {
68 .shift = 22,
69 .enc = BOOK3E_PAGESZ_4M,
70 },
71 [MMU_PAGE_16M] = {
72 .shift = 24,
73 .enc = BOOK3E_PAGESZ_16M,
74 },
75 [MMU_PAGE_64M] = {
76 .shift = 26,
77 .enc = BOOK3E_PAGESZ_64M,
78 },
79 [MMU_PAGE_256M] = {
80 .shift = 28,
81 .enc = BOOK3E_PAGESZ_256M,
82 },
83 [MMU_PAGE_1G] = {
84 .shift = 30,
85 .enc = BOOK3E_PAGESZ_1GB,
86 },
87};
88#else
89struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
90 [MMU_PAGE_4K] = {
91 .shift = 12,
92 .ind = 20,
93 .enc = BOOK3E_PAGESZ_4K,
94 },
95 [MMU_PAGE_16K] = {
96 .shift = 14,
97 .enc = BOOK3E_PAGESZ_16K,
98 },
99 [MMU_PAGE_64K] = {
100 .shift = 16,
101 .ind = 28,
102 .enc = BOOK3E_PAGESZ_64K,
103 },
104 [MMU_PAGE_1M] = {
105 .shift = 20,
106 .enc = BOOK3E_PAGESZ_1M,
107 },
108 [MMU_PAGE_16M] = {
109 .shift = 24,
110 .ind = 36,
111 .enc = BOOK3E_PAGESZ_16M,
112 },
113 [MMU_PAGE_256M] = {
114 .shift = 28,
115 .enc = BOOK3E_PAGESZ_256M,
116 },
117 [MMU_PAGE_1G] = {
118 .shift = 30,
119 .enc = BOOK3E_PAGESZ_1GB,
120 },
121};
122#endif
123
124static inline int mmu_get_tsize(int psize)
125{
126 return mmu_psize_defs[psize].enc;
127}
128#else
129static inline int mmu_get_tsize(int psize)
130{
131
132 return 0;
133}
134#endif
135
136
137
138
139
140#ifdef CONFIG_PPC64
141
142int mmu_linear_psize;
143int mmu_pte_psize;
144int mmu_vmemmap_psize;
145int book3e_htw_mode;
146unsigned long linear_map_top;
147
148
149
150
151
152
153
154
155int extlb_level_exc;
156
157#endif
158
159#ifdef CONFIG_PPC_FSL_BOOK3E
160
161DEFINE_PER_CPU(int, next_tlbcam_idx);
162EXPORT_PER_CPU_SYMBOL(next_tlbcam_idx);
163#endif
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180void local_flush_tlb_mm(struct mm_struct *mm)
181{
182 unsigned int pid;
183
184 preempt_disable();
185 pid = mm->context.id;
186 if (pid != MMU_NO_CONTEXT)
187 _tlbil_pid(pid);
188 preempt_enable();
189}
190EXPORT_SYMBOL(local_flush_tlb_mm);
191
192void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
193 int tsize, int ind)
194{
195 unsigned int pid;
196
197 preempt_disable();
198 pid = mm ? mm->context.id : 0;
199 if (pid != MMU_NO_CONTEXT)
200 _tlbil_va(vmaddr, pid, tsize, ind);
201 preempt_enable();
202}
203
204void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
205{
206 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
207 mmu_get_tsize(mmu_virtual_psize), 0);
208}
209EXPORT_SYMBOL(local_flush_tlb_page);
210
211
212
213
214#ifdef CONFIG_SMP
215
216static DEFINE_RAW_SPINLOCK(tlbivax_lock);
217
218static int mm_is_core_local(struct mm_struct *mm)
219{
220 return cpumask_subset(mm_cpumask(mm),
221 topology_sibling_cpumask(smp_processor_id()));
222}
223
224struct tlb_flush_param {
225 unsigned long addr;
226 unsigned int pid;
227 unsigned int tsize;
228 unsigned int ind;
229};
230
231static void do_flush_tlb_mm_ipi(void *param)
232{
233 struct tlb_flush_param *p = param;
234
235 _tlbil_pid(p ? p->pid : 0);
236}
237
238static void do_flush_tlb_page_ipi(void *param)
239{
240 struct tlb_flush_param *p = param;
241
242 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
243}
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262void flush_tlb_mm(struct mm_struct *mm)
263{
264 unsigned int pid;
265
266 preempt_disable();
267 pid = mm->context.id;
268 if (unlikely(pid == MMU_NO_CONTEXT))
269 goto no_context;
270 if (!mm_is_core_local(mm)) {
271 struct tlb_flush_param p = { .pid = pid };
272
273 smp_call_function_many(mm_cpumask(mm),
274 do_flush_tlb_mm_ipi, &p, 1);
275 }
276 _tlbil_pid(pid);
277 no_context:
278 preempt_enable();
279}
280EXPORT_SYMBOL(flush_tlb_mm);
281
282void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
283 int tsize, int ind)
284{
285 struct cpumask *cpu_mask;
286 unsigned int pid;
287
288
289
290
291
292 if (unlikely(WARN_ON(!mm)))
293 return;
294
295 preempt_disable();
296 pid = mm->context.id;
297 if (unlikely(pid == MMU_NO_CONTEXT))
298 goto bail;
299 cpu_mask = mm_cpumask(mm);
300 if (!mm_is_core_local(mm)) {
301
302 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
303 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
304 if (lock)
305 raw_spin_lock(&tlbivax_lock);
306 _tlbivax_bcast(vmaddr, pid, tsize, ind);
307 if (lock)
308 raw_spin_unlock(&tlbivax_lock);
309 goto bail;
310 } else {
311 struct tlb_flush_param p = {
312 .pid = pid,
313 .addr = vmaddr,
314 .tsize = tsize,
315 .ind = ind,
316 };
317
318 smp_call_function_many(cpu_mask,
319 do_flush_tlb_page_ipi, &p, 1);
320 }
321 }
322 _tlbil_va(vmaddr, pid, tsize, ind);
323 bail:
324 preempt_enable();
325}
326
327void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
328{
329#ifdef CONFIG_HUGETLB_PAGE
330 if (vma && is_vm_hugetlb_page(vma))
331 flush_hugetlb_page(vma, vmaddr);
332#endif
333
334 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
335 mmu_get_tsize(mmu_virtual_psize), 0);
336}
337EXPORT_SYMBOL(flush_tlb_page);
338
339#endif
340
341#ifdef CONFIG_PPC_47x
342void __init early_init_mmu_47x(void)
343{
344#ifdef CONFIG_SMP
345 unsigned long root = of_get_flat_dt_root();
346 if (of_get_flat_dt_prop(root, "cooperative-partition", NULL))
347 mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
348#endif
349}
350#endif
351
352
353
354
355void flush_tlb_kernel_range(unsigned long start, unsigned long end)
356{
357#ifdef CONFIG_SMP
358 preempt_disable();
359 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
360 _tlbil_pid(0);
361 preempt_enable();
362#else
363 _tlbil_pid(0);
364#endif
365}
366EXPORT_SYMBOL(flush_tlb_kernel_range);
367
368
369
370
371
372
373
374void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
375 unsigned long end)
376
377{
378 flush_tlb_mm(vma->vm_mm);
379}
380EXPORT_SYMBOL(flush_tlb_range);
381
382void tlb_flush(struct mmu_gather *tlb)
383{
384 flush_tlb_mm(tlb->mm);
385}
386
387
388
389
390
391
392#ifdef CONFIG_PPC64
393
394
395
396
397
398void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
399{
400 int tsize = mmu_psize_defs[mmu_pte_psize].enc;
401
402 if (book3e_htw_mode != PPC_HTW_NONE) {
403 unsigned long start = address & PMD_MASK;
404 unsigned long end = address + PMD_SIZE;
405 unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
406
407
408
409
410
411 while (start < end) {
412 __flush_tlb_page(tlb->mm, start, tsize, 1);
413 start += size;
414 }
415 } else {
416 unsigned long rmask = 0xf000000000000000ul;
417 unsigned long rid = (address & rmask) | 0x1000000000000000ul;
418 unsigned long vpte = address & ~rmask;
419
420#ifdef CONFIG_PPC_64K_PAGES
421 vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
422#else
423 vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
424#endif
425 vpte |= rid;
426 __flush_tlb_page(tlb->mm, vpte, tsize, 0);
427 }
428}
429
430static void setup_page_sizes(void)
431{
432 unsigned int tlb0cfg;
433 unsigned int tlb0ps;
434 unsigned int eptcfg;
435 int i, psize;
436
437#ifdef CONFIG_PPC_FSL_BOOK3E
438 unsigned int mmucfg = mfspr(SPRN_MMUCFG);
439 int fsl_mmu = mmu_has_feature(MMU_FTR_TYPE_FSL_E);
440
441 if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
442 unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
443 unsigned int min_pg, max_pg;
444
445 min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
446 max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
447
448 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
449 struct mmu_psize_def *def;
450 unsigned int shift;
451
452 def = &mmu_psize_defs[psize];
453 shift = def->shift;
454
455 if (shift == 0 || shift & 1)
456 continue;
457
458
459 shift = (shift - 10) >> 1;
460
461 if ((shift >= min_pg) && (shift <= max_pg))
462 def->flags |= MMU_PAGE_SIZE_DIRECT;
463 }
464
465 goto out;
466 }
467
468 if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V2) {
469 u32 tlb1cfg, tlb1ps;
470
471 tlb0cfg = mfspr(SPRN_TLB0CFG);
472 tlb1cfg = mfspr(SPRN_TLB1CFG);
473 tlb1ps = mfspr(SPRN_TLB1PS);
474 eptcfg = mfspr(SPRN_EPTCFG);
475
476 if ((tlb1cfg & TLBnCFG_IND) && (tlb0cfg & TLBnCFG_PT))
477 book3e_htw_mode = PPC_HTW_E6500;
478
479
480
481
482
483
484 if (eptcfg != 2)
485 book3e_htw_mode = PPC_HTW_NONE;
486
487 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
488 struct mmu_psize_def *def = &mmu_psize_defs[psize];
489
490 if (tlb1ps & (1U << (def->shift - 10))) {
491 def->flags |= MMU_PAGE_SIZE_DIRECT;
492
493 if (book3e_htw_mode && psize == MMU_PAGE_2M)
494 def->flags |= MMU_PAGE_SIZE_INDIRECT;
495 }
496 }
497
498 goto out;
499 }
500#endif
501
502 tlb0cfg = mfspr(SPRN_TLB0CFG);
503 tlb0ps = mfspr(SPRN_TLB0PS);
504 eptcfg = mfspr(SPRN_EPTCFG);
505
506
507 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
508 struct mmu_psize_def *def = &mmu_psize_defs[psize];
509
510 if (tlb0ps & (1U << (def->shift - 10)))
511 def->flags |= MMU_PAGE_SIZE_DIRECT;
512 }
513
514
515 if ((tlb0cfg & TLBnCFG_IND) == 0 ||
516 (tlb0cfg & TLBnCFG_PT) == 0)
517 goto out;
518
519 book3e_htw_mode = PPC_HTW_IBM;
520
521
522
523
524
525
526 for (i = 0; i < 3; i++) {
527 unsigned int ps, sps;
528
529 sps = eptcfg & 0x1f;
530 eptcfg >>= 5;
531 ps = eptcfg & 0x1f;
532 eptcfg >>= 5;
533 if (!ps || !sps)
534 continue;
535 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
536 struct mmu_psize_def *def = &mmu_psize_defs[psize];
537
538 if (ps == (def->shift - 10))
539 def->flags |= MMU_PAGE_SIZE_INDIRECT;
540 if (sps == (def->shift - 10))
541 def->ind = ps + 10;
542 }
543 }
544
545out:
546
547 pr_info("MMU: Supported page sizes\n");
548 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
549 struct mmu_psize_def *def = &mmu_psize_defs[psize];
550 const char *__page_type_names[] = {
551 "unsupported",
552 "direct",
553 "indirect",
554 "direct & indirect"
555 };
556 if (def->flags == 0) {
557 def->shift = 0;
558 continue;
559 }
560 pr_info(" %8ld KB as %s\n", 1ul << (def->shift - 10),
561 __page_type_names[def->flags & 0x3]);
562 }
563}
564
565static void setup_mmu_htw(void)
566{
567
568
569
570
571
572 switch (book3e_htw_mode) {
573 case PPC_HTW_IBM:
574 patch_exception(0x1c0, exc_data_tlb_miss_htw_book3e);
575 patch_exception(0x1e0, exc_instruction_tlb_miss_htw_book3e);
576 break;
577#ifdef CONFIG_PPC_FSL_BOOK3E
578 case PPC_HTW_E6500:
579 extlb_level_exc = EX_TLB_SIZE;
580 patch_exception(0x1c0, exc_data_tlb_miss_e6500_book3e);
581 patch_exception(0x1e0, exc_instruction_tlb_miss_e6500_book3e);
582 break;
583#endif
584 }
585 pr_info("MMU: Book3E HW tablewalk %s\n",
586 book3e_htw_mode != PPC_HTW_NONE ? "enabled" : "not supported");
587}
588
589
590
591
592static void early_init_this_mmu(void)
593{
594 unsigned int mas4;
595
596
597
598 mas4 = 0x4 << MAS4_WIMGED_SHIFT;
599 switch (book3e_htw_mode) {
600 case PPC_HTW_E6500:
601 mas4 |= MAS4_INDD;
602 mas4 |= BOOK3E_PAGESZ_2M << MAS4_TSIZED_SHIFT;
603 mas4 |= MAS4_TLBSELD(1);
604 mmu_pte_psize = MMU_PAGE_2M;
605 break;
606
607 case PPC_HTW_IBM:
608 mas4 |= MAS4_INDD;
609#ifdef CONFIG_PPC_64K_PAGES
610 mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
611 mmu_pte_psize = MMU_PAGE_256M;
612#else
613 mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
614 mmu_pte_psize = MMU_PAGE_1M;
615#endif
616 break;
617
618 case PPC_HTW_NONE:
619#ifdef CONFIG_PPC_64K_PAGES
620 mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
621#else
622 mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
623#endif
624 mmu_pte_psize = mmu_virtual_psize;
625 break;
626 }
627 mtspr(SPRN_MAS4, mas4);
628
629#ifdef CONFIG_PPC_FSL_BOOK3E
630 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
631 unsigned int num_cams;
632 int __maybe_unused cpu = smp_processor_id();
633 bool map = true;
634
635
636 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
637
638
639
640
641
642#ifdef CONFIG_SMP
643 if (hweight32(get_tensr()) > 1)
644 map = false;
645#endif
646
647 if (map)
648 linear_map_top = map_mem_in_cams(linear_map_top,
649 num_cams, false);
650 }
651#endif
652
653
654
655
656 mb();
657}
658
659static void __init early_init_mmu_global(void)
660{
661
662
663
664
665
666
667 mmu_linear_psize = MMU_PAGE_1G;
668
669
670
671
672
673
674
675 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E))
676 mmu_vmemmap_psize = MMU_PAGE_4K;
677 else
678 mmu_vmemmap_psize = MMU_PAGE_16M;
679
680
681
682
683
684
685
686 setup_page_sizes();
687
688
689 setup_mmu_htw();
690
691#ifdef CONFIG_PPC_FSL_BOOK3E
692 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
693 if (book3e_htw_mode == PPC_HTW_NONE) {
694 extlb_level_exc = EX_TLB_SIZE;
695 patch_exception(0x1c0, exc_data_tlb_miss_bolted_book3e);
696 patch_exception(0x1e0,
697 exc_instruction_tlb_miss_bolted_book3e);
698 }
699 }
700#endif
701
702
703
704
705 linear_map_top = memblock_end_of_DRAM();
706}
707
708static void __init early_mmu_set_memory_limit(void)
709{
710#ifdef CONFIG_PPC_FSL_BOOK3E
711 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
712
713
714
715
716
717
718
719 memblock_enforce_memory_limit(linear_map_top);
720 }
721#endif
722
723 memblock_set_current_limit(linear_map_top);
724}
725
726
727void __init early_init_mmu(void)
728{
729 early_init_mmu_global();
730 early_init_this_mmu();
731 early_mmu_set_memory_limit();
732}
733
734void early_init_mmu_secondary(void)
735{
736 early_init_this_mmu();
737}
738
739void setup_initial_memory_limit(phys_addr_t first_memblock_base,
740 phys_addr_t first_memblock_size)
741{
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759#ifdef CONFIG_PPC_FSL_BOOK3E
760 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
761 unsigned long linear_sz;
762 unsigned int num_cams;
763
764
765 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
766
767 linear_sz = map_mem_in_cams(first_memblock_size, num_cams,
768 true);
769
770 ppc64_rma_size = min_t(u64, linear_sz, 0x40000000);
771 } else
772#endif
773 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
774
775
776 memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
777}
778#else
779void __init early_init_mmu(void)
780{
781#ifdef CONFIG_PPC_47x
782 early_init_mmu_47x();
783#endif
784}
785#endif
786