1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 pagetable management routines.
5 * Copyright (C) 2006 Sony Computer Entertainment Inc.
6 * Copyright 2006, 2007 Sony Corporation
9 #include <linux/kernel.h>
10 #include <linux/memblock.h>
12 #include <asm/machdep.h>
14 #include <asm/lv1call.h>
15 #include <asm/ps3fb.h>
17 #define PS3_VERBOSE_RESULT
21 * enum lpar_vas_id - id of LPAR virtual address space.
22 * @lpar_vas_id_current: Current selected virtual address space
24 * Identify the target LPAR address space.
27 enum ps3_lpar_vas_id
{
28 PS3_LPAR_VAS_ID_CURRENT
= 0,
32 static DEFINE_SPINLOCK(ps3_htab_lock
);
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
)
41 u64 evicted_v
, evicted_r
;
42 u64 hpte_v_array
[4], hpte_rs
;
47 * lv1_insert_htab_entry() will search for victim
48 * entry in both primary and secondary pte group
50 vflags
&= ~HPTE_V_SECONDARY
;
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
;
55 spin_lock_irqsave(&ps3_htab_lock
, flags
);
57 /* talk hvc to replace entries BOLTED == 0 */
58 result
= lv1_insert_htab_entry(PS3_LPAR_VAS_ID_CURRENT
, hpte_group
,
62 &evicted_v
, &evicted_r
);
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
,
73 * see if the entry is inserted into secondary pteg
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],
82 if (hpte_v_array
[inserted_index
% 4] & HPTE_V_SECONDARY
)
83 ret
= (inserted_index
& 7) | (1 << 3);
85 ret
= inserted_index
& 7;
87 spin_unlock_irqrestore(&ps3_htab_lock
, flags
);
92 static long ps3_hpte_remove(unsigned long hpte_group
)
94 panic("ps3_hpte_remove() not implemented");
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
)
103 u64 hpte_v
, want_v
, hpte_rs
;
108 want_v
= hpte_encode_avpn(vpn
, psize
, ssize
);
110 spin_lock_irqsave(&ps3_htab_lock
, flags
);
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],
118 pr_info("%s: result=%s read vpn=%lx slot=%lx psize=%d\n",
119 __func__
, ps3_result(result
), vpn
, slot
, psize
);
123 hpte_v
= hpte_v_array
[slot
% 4];
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.
132 if (!HPTE_V_COMPARE(hpte_v
, want_v
) || !(hpte_v
& HPTE_V_VALID
)) {
136 /* entry found, just invalidate it */
137 result
= lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT
,
142 spin_unlock_irqrestore(&ps3_htab_lock
, flags
);
146 static void ps3_hpte_updateboltedpp(unsigned long newpp
, unsigned long ea
,
147 int psize
, int ssize
)
149 pr_info("ps3_hpte_updateboltedpp() not implemented");
152 static void ps3_hpte_invalidate(unsigned long slot
, unsigned long vpn
,
153 int psize
, int apsize
, int ssize
, int local
)
158 spin_lock_irqsave(&ps3_htab_lock
, flags
);
160 result
= lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT
, slot
, 0, 0);
163 pr_info("%s: result=%s vpn=%lx slot=%lx psize=%d\n",
164 __func__
, ps3_result(result
), vpn
, slot
, psize
);
168 spin_unlock_irqrestore(&ps3_htab_lock
, flags
);
171 /* Called during kexec sequence with MMU off */
172 static notrace
void ps3_hpte_clear(void)
174 unsigned long hpte_count
= (1UL << ppc64_pft_size
) >> 4;
177 for (i
= 0; i
< hpte_count
; i
++)
178 lv1_write_htab_entry(PS3_LPAR_VAS_ID_CURRENT
, i
, 0, 0);
181 ps3_mm_vas_destroy();
184 void __init
ps3_hpte_init(unsigned long htab_size
)
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
;
193 ppc64_pft_size
= __ilog2(htab_size
);