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