1*e66c3209SChristophe Leroy // SPDX-License-Identifier: GPL-2.0+ 2*e66c3209SChristophe Leroy /* 3*e66c3209SChristophe Leroy * Copyright 2018, Christophe Leroy CS S.I. 4*e66c3209SChristophe Leroy * <christophe.leroy@c-s.fr> 5*e66c3209SChristophe Leroy * 6*e66c3209SChristophe Leroy * This dumps the content of BATS 7*e66c3209SChristophe Leroy */ 8*e66c3209SChristophe Leroy 9*e66c3209SChristophe Leroy #include <asm/debugfs.h> 10*e66c3209SChristophe Leroy #include <asm/pgtable.h> 11*e66c3209SChristophe Leroy #include <asm/cpu_has_feature.h> 12*e66c3209SChristophe Leroy 13*e66c3209SChristophe Leroy static char *pp_601(int k, int pp) 14*e66c3209SChristophe Leroy { 15*e66c3209SChristophe Leroy if (pp == 0) 16*e66c3209SChristophe Leroy return k ? "NA" : "RWX"; 17*e66c3209SChristophe Leroy if (pp == 1) 18*e66c3209SChristophe Leroy return k ? "ROX" : "RWX"; 19*e66c3209SChristophe Leroy if (pp == 2) 20*e66c3209SChristophe Leroy return k ? "RWX" : "RWX"; 21*e66c3209SChristophe Leroy return k ? "ROX" : "ROX"; 22*e66c3209SChristophe Leroy } 23*e66c3209SChristophe Leroy 24*e66c3209SChristophe Leroy static void bat_show_601(struct seq_file *m, int idx, u32 lower, u32 upper) 25*e66c3209SChristophe Leroy { 26*e66c3209SChristophe Leroy u32 blpi = upper & 0xfffe0000; 27*e66c3209SChristophe Leroy u32 k = (upper >> 2) & 3; 28*e66c3209SChristophe Leroy u32 pp = upper & 3; 29*e66c3209SChristophe Leroy phys_addr_t pbn = PHYS_BAT_ADDR(lower); 30*e66c3209SChristophe Leroy u32 bsm = lower & 0x3ff; 31*e66c3209SChristophe Leroy u32 size = (bsm + 1) << 17; 32*e66c3209SChristophe Leroy 33*e66c3209SChristophe Leroy seq_printf(m, "%d: ", idx); 34*e66c3209SChristophe Leroy if (!(lower & 0x40)) { 35*e66c3209SChristophe Leroy seq_puts(m, " -\n"); 36*e66c3209SChristophe Leroy return; 37*e66c3209SChristophe Leroy } 38*e66c3209SChristophe Leroy 39*e66c3209SChristophe Leroy seq_printf(m, "0x%08x-0x%08x ", blpi, blpi + size - 1); 40*e66c3209SChristophe Leroy #ifdef CONFIG_PHYS_64BIT 41*e66c3209SChristophe Leroy seq_printf(m, "0x%016llx ", pbn); 42*e66c3209SChristophe Leroy #else 43*e66c3209SChristophe Leroy seq_printf(m, "0x%08x ", pbn); 44*e66c3209SChristophe Leroy #endif 45*e66c3209SChristophe Leroy 46*e66c3209SChristophe Leroy seq_printf(m, "Kernel %s User %s", pp_601(k & 2, pp), pp_601(k & 1, pp)); 47*e66c3209SChristophe Leroy 48*e66c3209SChristophe Leroy if (lower & _PAGE_WRITETHRU) 49*e66c3209SChristophe Leroy seq_puts(m, "write through "); 50*e66c3209SChristophe Leroy if (lower & _PAGE_NO_CACHE) 51*e66c3209SChristophe Leroy seq_puts(m, "no cache "); 52*e66c3209SChristophe Leroy if (lower & _PAGE_COHERENT) 53*e66c3209SChristophe Leroy seq_puts(m, "coherent "); 54*e66c3209SChristophe Leroy seq_puts(m, "\n"); 55*e66c3209SChristophe Leroy } 56*e66c3209SChristophe Leroy 57*e66c3209SChristophe Leroy #define BAT_SHOW_601(_m, _n, _l, _u) bat_show_601(_m, _n, mfspr(_l), mfspr(_u)) 58*e66c3209SChristophe Leroy 59*e66c3209SChristophe Leroy static int bats_show_601(struct seq_file *m, void *v) 60*e66c3209SChristophe Leroy { 61*e66c3209SChristophe Leroy seq_puts(m, "---[ Block Address Translation ]---\n"); 62*e66c3209SChristophe Leroy 63*e66c3209SChristophe Leroy BAT_SHOW_601(m, 0, SPRN_IBAT0L, SPRN_IBAT0U); 64*e66c3209SChristophe Leroy BAT_SHOW_601(m, 1, SPRN_IBAT1L, SPRN_IBAT1U); 65*e66c3209SChristophe Leroy BAT_SHOW_601(m, 2, SPRN_IBAT2L, SPRN_IBAT2U); 66*e66c3209SChristophe Leroy BAT_SHOW_601(m, 3, SPRN_IBAT3L, SPRN_IBAT3U); 67*e66c3209SChristophe Leroy 68*e66c3209SChristophe Leroy return 0; 69*e66c3209SChristophe Leroy } 70*e66c3209SChristophe Leroy 71*e66c3209SChristophe Leroy static void bat_show_603(struct seq_file *m, int idx, u32 lower, u32 upper, bool is_d) 72*e66c3209SChristophe Leroy { 73*e66c3209SChristophe Leroy u32 bepi = upper & 0xfffe0000; 74*e66c3209SChristophe Leroy u32 bl = (upper >> 2) & 0x7ff; 75*e66c3209SChristophe Leroy u32 k = upper & 3; 76*e66c3209SChristophe Leroy phys_addr_t brpn = PHYS_BAT_ADDR(lower); 77*e66c3209SChristophe Leroy u32 size = (bl + 1) << 17; 78*e66c3209SChristophe Leroy 79*e66c3209SChristophe Leroy seq_printf(m, "%d: ", idx); 80*e66c3209SChristophe Leroy if (k == 0) { 81*e66c3209SChristophe Leroy seq_puts(m, " -\n"); 82*e66c3209SChristophe Leroy return; 83*e66c3209SChristophe Leroy } 84*e66c3209SChristophe Leroy 85*e66c3209SChristophe Leroy seq_printf(m, "0x%08x-0x%08x ", bepi, bepi + size - 1); 86*e66c3209SChristophe Leroy #ifdef CONFIG_PHYS_64BIT 87*e66c3209SChristophe Leroy seq_printf(m, "0x%016llx ", brpn); 88*e66c3209SChristophe Leroy #else 89*e66c3209SChristophe Leroy seq_printf(m, "0x%08x ", brpn); 90*e66c3209SChristophe Leroy #endif 91*e66c3209SChristophe Leroy 92*e66c3209SChristophe Leroy if (k == 1) 93*e66c3209SChristophe Leroy seq_puts(m, "User "); 94*e66c3209SChristophe Leroy else if (k == 2) 95*e66c3209SChristophe Leroy seq_puts(m, "Kernel "); 96*e66c3209SChristophe Leroy else 97*e66c3209SChristophe Leroy seq_puts(m, "Kernel/User "); 98*e66c3209SChristophe Leroy 99*e66c3209SChristophe Leroy if (lower & BPP_RX) 100*e66c3209SChristophe Leroy seq_puts(m, is_d ? "RO " : "EXEC "); 101*e66c3209SChristophe Leroy else if (lower & BPP_RW) 102*e66c3209SChristophe Leroy seq_puts(m, is_d ? "RW " : "EXEC "); 103*e66c3209SChristophe Leroy else 104*e66c3209SChristophe Leroy seq_puts(m, is_d ? "NA " : "NX "); 105*e66c3209SChristophe Leroy 106*e66c3209SChristophe Leroy if (lower & _PAGE_WRITETHRU) 107*e66c3209SChristophe Leroy seq_puts(m, "write through "); 108*e66c3209SChristophe Leroy if (lower & _PAGE_NO_CACHE) 109*e66c3209SChristophe Leroy seq_puts(m, "no cache "); 110*e66c3209SChristophe Leroy if (lower & _PAGE_COHERENT) 111*e66c3209SChristophe Leroy seq_puts(m, "coherent "); 112*e66c3209SChristophe Leroy if (lower & _PAGE_GUARDED) 113*e66c3209SChristophe Leroy seq_puts(m, "guarded "); 114*e66c3209SChristophe Leroy seq_puts(m, "\n"); 115*e66c3209SChristophe Leroy } 116*e66c3209SChristophe Leroy 117*e66c3209SChristophe Leroy #define BAT_SHOW_603(_m, _n, _l, _u, _d) bat_show_603(_m, _n, mfspr(_l), mfspr(_u), _d) 118*e66c3209SChristophe Leroy 119*e66c3209SChristophe Leroy static int bats_show_603(struct seq_file *m, void *v) 120*e66c3209SChristophe Leroy { 121*e66c3209SChristophe Leroy seq_puts(m, "---[ Instruction Block Address Translation ]---\n"); 122*e66c3209SChristophe Leroy 123*e66c3209SChristophe Leroy BAT_SHOW_603(m, 0, SPRN_IBAT0L, SPRN_IBAT0U, false); 124*e66c3209SChristophe Leroy BAT_SHOW_603(m, 1, SPRN_IBAT1L, SPRN_IBAT1U, false); 125*e66c3209SChristophe Leroy BAT_SHOW_603(m, 2, SPRN_IBAT2L, SPRN_IBAT2U, false); 126*e66c3209SChristophe Leroy BAT_SHOW_603(m, 3, SPRN_IBAT3L, SPRN_IBAT3U, false); 127*e66c3209SChristophe Leroy if (mmu_has_feature(MMU_FTR_USE_HIGH_BATS)) { 128*e66c3209SChristophe Leroy BAT_SHOW_603(m, 4, SPRN_IBAT4L, SPRN_IBAT4U, false); 129*e66c3209SChristophe Leroy BAT_SHOW_603(m, 5, SPRN_IBAT5L, SPRN_IBAT5U, false); 130*e66c3209SChristophe Leroy BAT_SHOW_603(m, 6, SPRN_IBAT6L, SPRN_IBAT6U, false); 131*e66c3209SChristophe Leroy BAT_SHOW_603(m, 7, SPRN_IBAT7L, SPRN_IBAT7U, false); 132*e66c3209SChristophe Leroy } 133*e66c3209SChristophe Leroy 134*e66c3209SChristophe Leroy seq_puts(m, "\n---[ Data Block Address Translation ]---\n"); 135*e66c3209SChristophe Leroy 136*e66c3209SChristophe Leroy BAT_SHOW_603(m, 0, SPRN_DBAT0L, SPRN_DBAT0U, true); 137*e66c3209SChristophe Leroy BAT_SHOW_603(m, 1, SPRN_DBAT1L, SPRN_DBAT1U, true); 138*e66c3209SChristophe Leroy BAT_SHOW_603(m, 2, SPRN_DBAT2L, SPRN_DBAT2U, true); 139*e66c3209SChristophe Leroy BAT_SHOW_603(m, 3, SPRN_DBAT3L, SPRN_DBAT3U, true); 140*e66c3209SChristophe Leroy if (mmu_has_feature(MMU_FTR_USE_HIGH_BATS)) { 141*e66c3209SChristophe Leroy BAT_SHOW_603(m, 4, SPRN_DBAT4L, SPRN_DBAT4U, true); 142*e66c3209SChristophe Leroy BAT_SHOW_603(m, 5, SPRN_DBAT5L, SPRN_DBAT5U, true); 143*e66c3209SChristophe Leroy BAT_SHOW_603(m, 6, SPRN_DBAT6L, SPRN_DBAT6U, true); 144*e66c3209SChristophe Leroy BAT_SHOW_603(m, 7, SPRN_DBAT7L, SPRN_DBAT7U, true); 145*e66c3209SChristophe Leroy } 146*e66c3209SChristophe Leroy 147*e66c3209SChristophe Leroy return 0; 148*e66c3209SChristophe Leroy } 149*e66c3209SChristophe Leroy 150*e66c3209SChristophe Leroy static int bats_open(struct inode *inode, struct file *file) 151*e66c3209SChristophe Leroy { 152*e66c3209SChristophe Leroy if (cpu_has_feature(CPU_FTR_601)) 153*e66c3209SChristophe Leroy return single_open(file, bats_show_601, NULL); 154*e66c3209SChristophe Leroy 155*e66c3209SChristophe Leroy return single_open(file, bats_show_603, NULL); 156*e66c3209SChristophe Leroy } 157*e66c3209SChristophe Leroy 158*e66c3209SChristophe Leroy static const struct file_operations bats_fops = { 159*e66c3209SChristophe Leroy .open = bats_open, 160*e66c3209SChristophe Leroy .read = seq_read, 161*e66c3209SChristophe Leroy .llseek = seq_lseek, 162*e66c3209SChristophe Leroy .release = single_release, 163*e66c3209SChristophe Leroy }; 164*e66c3209SChristophe Leroy 165*e66c3209SChristophe Leroy static int __init bats_init(void) 166*e66c3209SChristophe Leroy { 167*e66c3209SChristophe Leroy struct dentry *debugfs_file; 168*e66c3209SChristophe Leroy 169*e66c3209SChristophe Leroy debugfs_file = debugfs_create_file("block_address_translation", 0400, 170*e66c3209SChristophe Leroy powerpc_debugfs_root, NULL, &bats_fops); 171*e66c3209SChristophe Leroy return debugfs_file ? 0 : -ENOMEM; 172*e66c3209SChristophe Leroy } 173*e66c3209SChristophe Leroy device_initcall(bats_init); 174