xref: /linux/arch/powerpc/platforms/ps3/htab.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  PS3 pagetable management routines.
4  *
5  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
6  *  Copyright 2006, 2007 Sony Corporation
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/memblock.h>
11 
12 #include <asm/machdep.h>
13 #include <asm/udbg.h>
14 #include <asm/lv1call.h>
15 #include <asm/ps3fb.h>
16 
17 #define PS3_VERBOSE_RESULT
18 #include "platform.h"
19 
20 /**
21  * enum lpar_vas_id - id of LPAR virtual address space.
22  * @lpar_vas_id_current: Current selected virtual address space
23  *
24  * Identify the target LPAR address space.
25  */
26 
27 enum ps3_lpar_vas_id {
28 	PS3_LPAR_VAS_ID_CURRENT = 0,
29 };
30 
31 
32 static DEFINE_SPINLOCK(ps3_htab_lock);
33 
ps3_hpte_insert(unsigned long hpte_group,unsigned long vpn,unsigned long pa,unsigned long rflags,unsigned long vflags,int psize,int apsize,int ssize)34 static long ps3_hpte_insert(unsigned long hpte_group, unsigned long vpn,
35 	unsigned long pa, unsigned long rflags, unsigned long vflags,
36 	int psize, int apsize, int ssize)
37 {
38 	int result;
39 	u64 hpte_v, hpte_r;
40 	u64 inserted_index;
41 	u64 evicted_v, evicted_r;
42 	u64 hpte_v_array[4], hpte_rs;
43 	unsigned long flags;
44 	long ret = -1;
45 
46 	/*
47 	 * lv1_insert_htab_entry() will search for victim
48 	 * entry in both primary and secondary pte group
49 	 */
50 	vflags &= ~HPTE_V_SECONDARY;
51 
52 	hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
53 	hpte_r = hpte_encode_r(ps3_mm_phys_to_lpar(pa), psize, apsize) | rflags;
54 
55 	spin_lock_irqsave(&ps3_htab_lock, flags);
56 
57 	/* talk hvc to replace entries BOLTED == 0 */
58 	result = lv1_insert_htab_entry(PS3_LPAR_VAS_ID_CURRENT, hpte_group,
59 				       hpte_v, hpte_r,
60 				       HPTE_V_BOLTED, 0,
61 				       &inserted_index,
62 				       &evicted_v, &evicted_r);
63 
64 	if (result) {
65 		/* all entries bolted !*/
66 		pr_info("%s:result=%s vpn=%lx pa=%lx ix=%lx v=%llx r=%llx\n",
67 			__func__, ps3_result(result), vpn, pa, hpte_group,
68 			hpte_v, hpte_r);
69 		BUG();
70 	}
71 
72 	/*
73 	 * see if the entry is inserted into secondary pteg
74 	 */
75 	result = lv1_read_htab_entries(PS3_LPAR_VAS_ID_CURRENT,
76 				       inserted_index & ~0x3UL,
77 				       &hpte_v_array[0], &hpte_v_array[1],
78 				       &hpte_v_array[2], &hpte_v_array[3],
79 				       &hpte_rs);
80 	BUG_ON(result);
81 
82 	if (hpte_v_array[inserted_index % 4] & HPTE_V_SECONDARY)
83 		ret = (inserted_index & 7) | (1 << 3);
84 	else
85 		ret = inserted_index & 7;
86 
87 	spin_unlock_irqrestore(&ps3_htab_lock, flags);
88 
89 	return ret;
90 }
91 
ps3_hpte_remove(unsigned long hpte_group)92 static long ps3_hpte_remove(unsigned long hpte_group)
93 {
94 	panic("ps3_hpte_remove() not implemented");
95 	return 0;
96 }
97 
ps3_hpte_updatepp(unsigned long slot,unsigned long newpp,unsigned long vpn,int psize,int apsize,int ssize,unsigned long inv_flags)98 static long ps3_hpte_updatepp(unsigned long slot, unsigned long newpp,
99 			      unsigned long vpn, int psize, int apsize,
100 			      int ssize, unsigned long inv_flags)
101 {
102 	int result;
103 	u64 hpte_v, want_v, hpte_rs;
104 	u64 hpte_v_array[4];
105 	unsigned long flags;
106 	long ret;
107 
108 	want_v = hpte_encode_avpn(vpn, psize, ssize);
109 
110 	spin_lock_irqsave(&ps3_htab_lock, flags);
111 
112 	result = lv1_read_htab_entries(PS3_LPAR_VAS_ID_CURRENT, slot & ~0x3UL,
113 				       &hpte_v_array[0], &hpte_v_array[1],
114 				       &hpte_v_array[2], &hpte_v_array[3],
115 				       &hpte_rs);
116 
117 	if (result) {
118 		pr_info("%s: result=%s read vpn=%lx slot=%lx psize=%d\n",
119 			__func__, ps3_result(result), vpn, slot, psize);
120 		BUG();
121 	}
122 
123 	hpte_v = hpte_v_array[slot % 4];
124 
125 	/*
126 	 * As lv1_read_htab_entries() does not give us the RPN, we can
127 	 * not synthesize the new hpte_r value here, and therefore can
128 	 * not update the hpte with lv1_insert_htab_entry(), so we
129 	 * instead invalidate it and ask the caller to update it via
130 	 * ps3_hpte_insert() by returning a -1 value.
131 	 */
132 	if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
133 		/* not found */
134 		ret = -1;
135 	} else {
136 		/* entry found, just invalidate it */
137 		result = lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT,
138 					      slot, 0, 0);
139 		ret = -1;
140 	}
141 
142 	spin_unlock_irqrestore(&ps3_htab_lock, flags);
143 	return ret;
144 }
145 
ps3_hpte_updateboltedpp(unsigned long newpp,unsigned long ea,int psize,int ssize)146 static void ps3_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
147 	int psize, int ssize)
148 {
149 	pr_info("ps3_hpte_updateboltedpp() not implemented");
150 }
151 
ps3_hpte_invalidate(unsigned long slot,unsigned long vpn,int psize,int apsize,int ssize,int local)152 static void ps3_hpte_invalidate(unsigned long slot, unsigned long vpn,
153 				int psize, int apsize, int ssize, int local)
154 {
155 	unsigned long flags;
156 	int result;
157 
158 	spin_lock_irqsave(&ps3_htab_lock, flags);
159 
160 	result = lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT, slot, 0, 0);
161 
162 	if (result) {
163 		pr_info("%s: result=%s vpn=%lx slot=%lx psize=%d\n",
164 			__func__, ps3_result(result), vpn, slot, psize);
165 		BUG();
166 	}
167 
168 	spin_unlock_irqrestore(&ps3_htab_lock, flags);
169 }
170 
171 /* Called during kexec sequence with MMU off */
ps3_hpte_clear(void)172 static notrace void ps3_hpte_clear(void)
173 {
174 	unsigned long hpte_count = (1UL << ppc64_pft_size) >> 4;
175 	u64 i;
176 
177 	for (i = 0; i < hpte_count; i++)
178 		lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT, i, 0, 0);
179 
180 	ps3_mm_shutdown();
181 	ps3_mm_vas_destroy();
182 }
183 
ps3_hpte_init(unsigned long htab_size)184 void __init ps3_hpte_init(unsigned long htab_size)
185 {
186 	mmu_hash_ops.hpte_invalidate = ps3_hpte_invalidate;
187 	mmu_hash_ops.hpte_updatepp = ps3_hpte_updatepp;
188 	mmu_hash_ops.hpte_updateboltedpp = ps3_hpte_updateboltedpp;
189 	mmu_hash_ops.hpte_insert = ps3_hpte_insert;
190 	mmu_hash_ops.hpte_remove = ps3_hpte_remove;
191 	mmu_hash_ops.hpte_clear_all = ps3_hpte_clear;
192 
193 	ppc64_pft_size = __ilog2(htab_size);
194 }
195 
196