0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/bug.h>
0011 #include <linux/mm_types.h>
0012
0013 #include <asm/arcregs.h>
0014 #include <asm/setup.h>
0015 #include <asm/mmu_context.h>
0016 #include <asm/mmu.h>
0017
0018
0019 DEFINE_PER_CPU(unsigned int, asid_cache) = MM_CTXT_FIRST_CYCLE;
0020
0021 static int __read_mostly pae_exists;
0022
0023
0024
0025
0026
0027 static inline void __tlb_entry_erase(void)
0028 {
0029 write_aux_reg(ARC_REG_TLBPD1, 0);
0030
0031 if (is_pae40_enabled())
0032 write_aux_reg(ARC_REG_TLBPD1HI, 0);
0033
0034 write_aux_reg(ARC_REG_TLBPD0, 0);
0035 write_aux_reg(ARC_REG_TLBCOMMAND, TLBWrite);
0036 }
0037
0038 static void utlb_invalidate(void)
0039 {
0040 write_aux_reg(ARC_REG_TLBCOMMAND, TLBIVUTLB);
0041 }
0042
0043 #ifdef CONFIG_ARC_MMU_V3
0044
0045 static inline unsigned int tlb_entry_lkup(unsigned long vaddr_n_asid)
0046 {
0047 unsigned int idx;
0048
0049 write_aux_reg(ARC_REG_TLBPD0, vaddr_n_asid);
0050
0051 write_aux_reg(ARC_REG_TLBCOMMAND, TLBProbe);
0052 idx = read_aux_reg(ARC_REG_TLBINDEX);
0053
0054 return idx;
0055 }
0056
0057 static void tlb_entry_erase(unsigned int vaddr_n_asid)
0058 {
0059 unsigned int idx;
0060
0061
0062 idx = tlb_entry_lkup(vaddr_n_asid);
0063
0064
0065 if (likely(!(idx & TLB_LKUP_ERR))) {
0066 __tlb_entry_erase();
0067 } else {
0068
0069 WARN(idx == TLB_DUP_ERR, "Probe returned Dup PD for %x\n",
0070 vaddr_n_asid);
0071 }
0072 }
0073
0074 static void tlb_entry_insert(unsigned int pd0, phys_addr_t pd1)
0075 {
0076 unsigned int idx;
0077
0078
0079
0080
0081
0082 idx = tlb_entry_lkup(pd0);
0083
0084
0085
0086
0087
0088
0089
0090 if (likely(idx & TLB_LKUP_ERR))
0091 write_aux_reg(ARC_REG_TLBCOMMAND, TLBGetIndex);
0092
0093
0094 write_aux_reg(ARC_REG_TLBPD1, pd1);
0095
0096
0097
0098
0099
0100
0101 write_aux_reg(ARC_REG_TLBCOMMAND, TLBWrite);
0102 }
0103
0104 #else
0105
0106 static void tlb_entry_erase(unsigned int vaddr_n_asid)
0107 {
0108 write_aux_reg(ARC_REG_TLBPD0, vaddr_n_asid | _PAGE_PRESENT);
0109 write_aux_reg(ARC_REG_TLBCOMMAND, TLBDeleteEntry);
0110 }
0111
0112 static void tlb_entry_insert(unsigned int pd0, phys_addr_t pd1)
0113 {
0114 write_aux_reg(ARC_REG_TLBPD0, pd0);
0115
0116 if (!is_pae40_enabled()) {
0117 write_aux_reg(ARC_REG_TLBPD1, pd1);
0118 } else {
0119 write_aux_reg(ARC_REG_TLBPD1, pd1 & 0xFFFFFFFF);
0120 write_aux_reg(ARC_REG_TLBPD1HI, (u64)pd1 >> 32);
0121 }
0122
0123 write_aux_reg(ARC_REG_TLBCOMMAND, TLBInsertEntry);
0124 }
0125
0126 #endif
0127
0128
0129
0130
0131
0132 noinline void local_flush_tlb_all(void)
0133 {
0134 struct cpuinfo_arc_mmu *mmu = &cpuinfo_arc700[smp_processor_id()].mmu;
0135 unsigned long flags;
0136 unsigned int entry;
0137 int num_tlb = mmu->sets * mmu->ways;
0138
0139 local_irq_save(flags);
0140
0141
0142 write_aux_reg(ARC_REG_TLBPD1, 0);
0143
0144 if (is_pae40_enabled())
0145 write_aux_reg(ARC_REG_TLBPD1HI, 0);
0146
0147 write_aux_reg(ARC_REG_TLBPD0, 0);
0148
0149 for (entry = 0; entry < num_tlb; entry++) {
0150
0151 write_aux_reg(ARC_REG_TLBINDEX, entry);
0152 write_aux_reg(ARC_REG_TLBCOMMAND, TLBWriteNI);
0153 }
0154
0155 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
0156 const int stlb_idx = 0x800;
0157
0158
0159 write_aux_reg(ARC_REG_TLBPD0, _PAGE_HW_SZ);
0160
0161 for (entry = stlb_idx; entry < stlb_idx + 16; entry++) {
0162 write_aux_reg(ARC_REG_TLBINDEX, entry);
0163 write_aux_reg(ARC_REG_TLBCOMMAND, TLBWriteNI);
0164 }
0165 }
0166
0167 utlb_invalidate();
0168
0169 local_irq_restore(flags);
0170 }
0171
0172
0173
0174
0175 noinline void local_flush_tlb_mm(struct mm_struct *mm)
0176 {
0177
0178
0179
0180
0181
0182
0183 if (atomic_read(&mm->mm_users) == 0)
0184 return;
0185
0186
0187
0188
0189
0190
0191
0192
0193 destroy_context(mm);
0194 if (current->mm == mm)
0195 get_new_mmu_context(mm);
0196 }
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
0207 unsigned long end)
0208 {
0209 const unsigned int cpu = smp_processor_id();
0210 unsigned long flags;
0211
0212
0213
0214
0215
0216
0217
0218
0219 if (unlikely((end - start) >= PAGE_SIZE * 32)) {
0220 local_flush_tlb_mm(vma->vm_mm);
0221 return;
0222 }
0223
0224
0225
0226
0227
0228
0229 start &= PAGE_MASK;
0230
0231 local_irq_save(flags);
0232
0233 if (asid_mm(vma->vm_mm, cpu) != MM_CTXT_NO_ASID) {
0234 while (start < end) {
0235 tlb_entry_erase(start | hw_pid(vma->vm_mm, cpu));
0236 start += PAGE_SIZE;
0237 }
0238 }
0239
0240 local_irq_restore(flags);
0241 }
0242
0243
0244
0245
0246
0247
0248
0249 void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
0250 {
0251 unsigned long flags;
0252
0253
0254
0255 if (unlikely((end - start) >= PAGE_SIZE * 32)) {
0256 local_flush_tlb_all();
0257 return;
0258 }
0259
0260 start &= PAGE_MASK;
0261
0262 local_irq_save(flags);
0263 while (start < end) {
0264 tlb_entry_erase(start);
0265 start += PAGE_SIZE;
0266 }
0267
0268 local_irq_restore(flags);
0269 }
0270
0271
0272
0273
0274
0275
0276 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
0277 {
0278 const unsigned int cpu = smp_processor_id();
0279 unsigned long flags;
0280
0281
0282
0283
0284 local_irq_save(flags);
0285
0286 if (asid_mm(vma->vm_mm, cpu) != MM_CTXT_NO_ASID) {
0287 tlb_entry_erase((page & PAGE_MASK) | hw_pid(vma->vm_mm, cpu));
0288 }
0289
0290 local_irq_restore(flags);
0291 }
0292
0293 #ifdef CONFIG_SMP
0294
0295 struct tlb_args {
0296 struct vm_area_struct *ta_vma;
0297 unsigned long ta_start;
0298 unsigned long ta_end;
0299 };
0300
0301 static inline void ipi_flush_tlb_page(void *arg)
0302 {
0303 struct tlb_args *ta = arg;
0304
0305 local_flush_tlb_page(ta->ta_vma, ta->ta_start);
0306 }
0307
0308 static inline void ipi_flush_tlb_range(void *arg)
0309 {
0310 struct tlb_args *ta = arg;
0311
0312 local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
0313 }
0314
0315 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
0316 static inline void ipi_flush_pmd_tlb_range(void *arg)
0317 {
0318 struct tlb_args *ta = arg;
0319
0320 local_flush_pmd_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
0321 }
0322 #endif
0323
0324 static inline void ipi_flush_tlb_kernel_range(void *arg)
0325 {
0326 struct tlb_args *ta = (struct tlb_args *)arg;
0327
0328 local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
0329 }
0330
0331 void flush_tlb_all(void)
0332 {
0333 on_each_cpu((smp_call_func_t)local_flush_tlb_all, NULL, 1);
0334 }
0335
0336 void flush_tlb_mm(struct mm_struct *mm)
0337 {
0338 on_each_cpu_mask(mm_cpumask(mm), (smp_call_func_t)local_flush_tlb_mm,
0339 mm, 1);
0340 }
0341
0342 void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
0343 {
0344 struct tlb_args ta = {
0345 .ta_vma = vma,
0346 .ta_start = uaddr
0347 };
0348
0349 on_each_cpu_mask(mm_cpumask(vma->vm_mm), ipi_flush_tlb_page, &ta, 1);
0350 }
0351
0352 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
0353 unsigned long end)
0354 {
0355 struct tlb_args ta = {
0356 .ta_vma = vma,
0357 .ta_start = start,
0358 .ta_end = end
0359 };
0360
0361 on_each_cpu_mask(mm_cpumask(vma->vm_mm), ipi_flush_tlb_range, &ta, 1);
0362 }
0363
0364 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
0365 void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
0366 unsigned long end)
0367 {
0368 struct tlb_args ta = {
0369 .ta_vma = vma,
0370 .ta_start = start,
0371 .ta_end = end
0372 };
0373
0374 on_each_cpu_mask(mm_cpumask(vma->vm_mm), ipi_flush_pmd_tlb_range, &ta, 1);
0375 }
0376 #endif
0377
0378 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
0379 {
0380 struct tlb_args ta = {
0381 .ta_start = start,
0382 .ta_end = end
0383 };
0384
0385 on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1);
0386 }
0387 #endif
0388
0389
0390
0391
0392 void create_tlb(struct vm_area_struct *vma, unsigned long vaddr, pte_t *ptep)
0393 {
0394 unsigned long flags;
0395 unsigned int asid_or_sasid, rwx;
0396 unsigned long pd0;
0397 phys_addr_t pd1;
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423 if (current->active_mm != vma->vm_mm)
0424 return;
0425
0426 local_irq_save(flags);
0427
0428 vaddr &= PAGE_MASK;
0429
0430
0431 pte_val(*ptep) |= (_PAGE_PRESENT | _PAGE_ACCESSED);
0432
0433
0434
0435
0436 asid_or_sasid = read_aux_reg(ARC_REG_PID) & 0xff;
0437
0438 pd0 = vaddr | asid_or_sasid | (pte_val(*ptep) & PTE_BITS_IN_PD0);
0439
0440
0441
0442
0443
0444
0445
0446
0447 rwx = pte_val(*ptep) & PTE_BITS_RWX;
0448
0449 if (pte_val(*ptep) & _PAGE_GLOBAL)
0450 rwx <<= 3;
0451 else
0452 rwx |= (rwx << 3);
0453
0454 pd1 = rwx | (pte_val(*ptep) & PTE_BITS_NON_RWX_IN_PD1);
0455
0456 tlb_entry_insert(pd0, pd1);
0457
0458 local_irq_restore(flags);
0459 }
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470 void update_mmu_cache(struct vm_area_struct *vma, unsigned long vaddr_unaligned,
0471 pte_t *ptep)
0472 {
0473 unsigned long vaddr = vaddr_unaligned & PAGE_MASK;
0474 phys_addr_t paddr = pte_val(*ptep) & PAGE_MASK_PHYS;
0475 struct page *page = pfn_to_page(pte_pfn(*ptep));
0476
0477 create_tlb(vma, vaddr, ptep);
0478
0479 if (page == ZERO_PAGE(0)) {
0480 return;
0481 }
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492 if ((vma->vm_flags & VM_EXEC) ||
0493 addr_not_cache_congruent(paddr, vaddr)) {
0494
0495 int dirty = !test_and_set_bit(PG_dc_clean, &page->flags);
0496 if (dirty) {
0497
0498 __flush_dcache_page(paddr, paddr);
0499
0500
0501 if (vma->vm_flags & VM_EXEC)
0502 __inv_icache_page(paddr, vaddr);
0503 }
0504 }
0505 }
0506
0507 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
0531 pmd_t *pmd)
0532 {
0533 pte_t pte = __pte(pmd_val(*pmd));
0534 update_mmu_cache(vma, addr, &pte);
0535 }
0536
0537 void local_flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
0538 unsigned long end)
0539 {
0540 unsigned int cpu;
0541 unsigned long flags;
0542
0543 local_irq_save(flags);
0544
0545 cpu = smp_processor_id();
0546
0547 if (likely(asid_mm(vma->vm_mm, cpu) != MM_CTXT_NO_ASID)) {
0548 unsigned int asid = hw_pid(vma->vm_mm, cpu);
0549
0550
0551 tlb_entry_erase(start | _PAGE_HW_SZ | asid);
0552 }
0553
0554 local_irq_restore(flags);
0555 }
0556
0557 #endif
0558
0559
0560
0561
0562
0563 void read_decode_mmu_bcr(void)
0564 {
0565 struct cpuinfo_arc_mmu *mmu = &cpuinfo_arc700[smp_processor_id()].mmu;
0566 unsigned int tmp;
0567 struct bcr_mmu_3 {
0568 #ifdef CONFIG_CPU_BIG_ENDIAN
0569 unsigned int ver:8, ways:4, sets:4, res:3, sasid:1, pg_sz:4,
0570 u_itlb:4, u_dtlb:4;
0571 #else
0572 unsigned int u_dtlb:4, u_itlb:4, pg_sz:4, sasid:1, res:3, sets:4,
0573 ways:4, ver:8;
0574 #endif
0575 } *mmu3;
0576
0577 struct bcr_mmu_4 {
0578 #ifdef CONFIG_CPU_BIG_ENDIAN
0579 unsigned int ver:8, sasid:1, sz1:4, sz0:4, res:2, pae:1,
0580 n_ways:2, n_entry:2, n_super:2, u_itlb:3, u_dtlb:3;
0581 #else
0582
0583 unsigned int u_dtlb:3, u_itlb:3, n_super:2, n_entry:2, n_ways:2,
0584 pae:1, res:2, sz0:4, sz1:4, sasid:1, ver:8;
0585 #endif
0586 } *mmu4;
0587
0588 tmp = read_aux_reg(ARC_REG_MMU_BCR);
0589 mmu->ver = (tmp >> 24);
0590
0591 if (is_isa_arcompact() && mmu->ver == 3) {
0592 mmu3 = (struct bcr_mmu_3 *)&tmp;
0593 mmu->pg_sz_k = 1 << (mmu3->pg_sz - 1);
0594 mmu->sets = 1 << mmu3->sets;
0595 mmu->ways = 1 << mmu3->ways;
0596 mmu->u_dtlb = mmu3->u_dtlb;
0597 mmu->u_itlb = mmu3->u_itlb;
0598 mmu->sasid = mmu3->sasid;
0599 } else {
0600 mmu4 = (struct bcr_mmu_4 *)&tmp;
0601 mmu->pg_sz_k = 1 << (mmu4->sz0 - 1);
0602 mmu->s_pg_sz_m = 1 << (mmu4->sz1 - 11);
0603 mmu->sets = 64 << mmu4->n_entry;
0604 mmu->ways = mmu4->n_ways * 2;
0605 mmu->u_dtlb = mmu4->u_dtlb * 4;
0606 mmu->u_itlb = mmu4->u_itlb * 4;
0607 mmu->sasid = mmu4->sasid;
0608 pae_exists = mmu->pae = mmu4->pae;
0609 }
0610 }
0611
0612 char *arc_mmu_mumbojumbo(int cpu_id, char *buf, int len)
0613 {
0614 int n = 0;
0615 struct cpuinfo_arc_mmu *p_mmu = &cpuinfo_arc700[cpu_id].mmu;
0616 char super_pg[64] = "";
0617
0618 if (p_mmu->s_pg_sz_m)
0619 scnprintf(super_pg, 64, "%dM Super Page %s",
0620 p_mmu->s_pg_sz_m,
0621 IS_USED_CFG(CONFIG_TRANSPARENT_HUGEPAGE));
0622
0623 n += scnprintf(buf + n, len - n,
0624 "MMU [v%x]\t: %dk PAGE, %s, swalk %d lvl, JTLB %d (%dx%d), uDTLB %d, uITLB %d%s%s\n",
0625 p_mmu->ver, p_mmu->pg_sz_k, super_pg, CONFIG_PGTABLE_LEVELS,
0626 p_mmu->sets * p_mmu->ways, p_mmu->sets, p_mmu->ways,
0627 p_mmu->u_dtlb, p_mmu->u_itlb,
0628 IS_AVAIL2(p_mmu->pae, ", PAE40 ", CONFIG_ARC_HAS_PAE40));
0629
0630 return buf;
0631 }
0632
0633 int pae40_exist_but_not_enab(void)
0634 {
0635 return pae_exists && !is_pae40_enabled();
0636 }
0637
0638 void arc_mmu_init(void)
0639 {
0640 struct cpuinfo_arc_mmu *mmu = &cpuinfo_arc700[smp_processor_id()].mmu;
0641 char str[256];
0642 int compat = 0;
0643
0644 pr_info("%s", arc_mmu_mumbojumbo(0, str, sizeof(str)));
0645
0646
0647
0648
0649 BUILD_BUG_ON(!IS_ALIGNED((CONFIG_ARC_KVADDR_SIZE << 20), PMD_SIZE));
0650
0651
0652
0653
0654
0655 BUILD_BUG_ON(!IS_ALIGNED(STACK_TOP, PMD_SIZE));
0656
0657
0658
0659
0660
0661
0662
0663 if (is_isa_arcompact() && mmu->ver == 3)
0664 compat = 1;
0665 else if (is_isa_arcv2() && mmu->ver >= 4)
0666 compat = 1;
0667
0668 if (!compat)
0669 panic("MMU ver %d doesn't match kernel built for\n", mmu->ver);
0670
0671 if (mmu->pg_sz_k != TO_KB(PAGE_SIZE))
0672 panic("MMU pg size != PAGE_SIZE (%luk)\n", TO_KB(PAGE_SIZE));
0673
0674 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
0675 mmu->s_pg_sz_m != TO_MB(HPAGE_PMD_SIZE))
0676 panic("MMU Super pg size != Linux HPAGE_PMD_SIZE (%luM)\n",
0677 (unsigned long)TO_MB(HPAGE_PMD_SIZE));
0678
0679 if (IS_ENABLED(CONFIG_ARC_HAS_PAE40) && !mmu->pae)
0680 panic("Hardware doesn't support PAE40\n");
0681
0682
0683 mmu_setup_asid(NULL, 0);
0684
0685
0686 mmu_setup_pgd(NULL, swapper_pg_dir);
0687
0688 if (pae40_exist_but_not_enab())
0689 write_aux_reg(ARC_REG_TLBPD1HI, 0);
0690 }
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707 #define SET_WAY_TO_IDX(mmu, set, way) ((set) * mmu->ways + (way))
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717 volatile int dup_pd_silent;
0718
0719 void do_tlb_overlap_fault(unsigned long cause, unsigned long address,
0720 struct pt_regs *regs)
0721 {
0722 struct cpuinfo_arc_mmu *mmu = &cpuinfo_arc700[smp_processor_id()].mmu;
0723 unsigned long flags;
0724 int set, n_ways = mmu->ways;
0725
0726 n_ways = min(n_ways, 4);
0727 BUG_ON(mmu->ways > 4);
0728
0729 local_irq_save(flags);
0730
0731
0732 for (set = 0; set < mmu->sets; set++) {
0733
0734 int is_valid, way;
0735 unsigned int pd0[4];
0736
0737
0738 for (way = 0, is_valid = 0; way < n_ways; way++) {
0739 write_aux_reg(ARC_REG_TLBINDEX,
0740 SET_WAY_TO_IDX(mmu, set, way));
0741 write_aux_reg(ARC_REG_TLBCOMMAND, TLBRead);
0742 pd0[way] = read_aux_reg(ARC_REG_TLBPD0);
0743 is_valid |= pd0[way] & _PAGE_PRESENT;
0744 pd0[way] &= PAGE_MASK;
0745 }
0746
0747
0748 if (!is_valid)
0749 continue;
0750
0751
0752 for (way = 0; way < n_ways - 1; way++) {
0753
0754 int n;
0755
0756 if (!pd0[way])
0757 continue;
0758
0759 for (n = way + 1; n < n_ways; n++) {
0760 if (pd0[way] != pd0[n])
0761 continue;
0762
0763 if (!dup_pd_silent)
0764 pr_info("Dup TLB PD0 %08x @ set %d ways %d,%d\n",
0765 pd0[way], set, way, n);
0766
0767
0768
0769
0770
0771 pd0[way] = 0;
0772 write_aux_reg(ARC_REG_TLBINDEX,
0773 SET_WAY_TO_IDX(mmu, set, way));
0774 __tlb_entry_erase();
0775 }
0776 }
0777 }
0778
0779 local_irq_restore(flags);
0780 }