18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * This file contains the routines for handling the MMU on those 48c2ecf20Sopenharmony_ci * PowerPC implementations where the MMU substantially follows the 58c2ecf20Sopenharmony_ci * architecture specification. This includes the 6xx, 7xx, 7xxx, 68c2ecf20Sopenharmony_ci * and 8260 implementations but excludes the 8xx and 4xx. 78c2ecf20Sopenharmony_ci * -- paulus 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Derived from arch/ppc/mm/init.c: 108c2ecf20Sopenharmony_ci * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au) 138c2ecf20Sopenharmony_ci * and Cort Dougan (PReP) (cort@cs.nmt.edu) 148c2ecf20Sopenharmony_ci * Copyright (C) 1996 Paul Mackerras 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * Derived from "arch/i386/mm/init.c" 178c2ecf20Sopenharmony_ci * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <linux/kernel.h> 218c2ecf20Sopenharmony_ci#include <linux/mm.h> 228c2ecf20Sopenharmony_ci#include <linux/init.h> 238c2ecf20Sopenharmony_ci#include <linux/highmem.h> 248c2ecf20Sopenharmony_ci#include <linux/memblock.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include <asm/prom.h> 278c2ecf20Sopenharmony_ci#include <asm/mmu.h> 288c2ecf20Sopenharmony_ci#include <asm/machdep.h> 298c2ecf20Sopenharmony_ci#include <asm/code-patching.h> 308c2ecf20Sopenharmony_ci#include <asm/sections.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#include <mm/mmu_decl.h> 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciu8 __initdata early_hash[SZ_256K] __aligned(SZ_256K) = {0}; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistruct hash_pte *Hash; 378c2ecf20Sopenharmony_cistatic unsigned long Hash_size, Hash_mask; 388c2ecf20Sopenharmony_ciunsigned long _SDR1; 398c2ecf20Sopenharmony_cistatic unsigned int hash_mb, hash_mb2; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */ 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistruct batrange { /* stores address ranges mapped by BATs */ 448c2ecf20Sopenharmony_ci unsigned long start; 458c2ecf20Sopenharmony_ci unsigned long limit; 468c2ecf20Sopenharmony_ci phys_addr_t phys; 478c2ecf20Sopenharmony_ci} bat_addrs[8]; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/* 508c2ecf20Sopenharmony_ci * Return PA for this VA if it is mapped by a BAT, or 0 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_ciphys_addr_t v_block_mapped(unsigned long va) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci int b; 558c2ecf20Sopenharmony_ci for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b) 568c2ecf20Sopenharmony_ci if (va >= bat_addrs[b].start && va < bat_addrs[b].limit) 578c2ecf20Sopenharmony_ci return bat_addrs[b].phys + (va - bat_addrs[b].start); 588c2ecf20Sopenharmony_ci return 0; 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* 628c2ecf20Sopenharmony_ci * Return VA for a given PA or 0 if not mapped 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ciunsigned long p_block_mapped(phys_addr_t pa) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci int b; 678c2ecf20Sopenharmony_ci for (b = 0; b < ARRAY_SIZE(bat_addrs); ++b) 688c2ecf20Sopenharmony_ci if (pa >= bat_addrs[b].phys 698c2ecf20Sopenharmony_ci && pa < (bat_addrs[b].limit-bat_addrs[b].start) 708c2ecf20Sopenharmony_ci +bat_addrs[b].phys) 718c2ecf20Sopenharmony_ci return bat_addrs[b].start+(pa-bat_addrs[b].phys); 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciint __init find_free_bat(void) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci int b; 788c2ecf20Sopenharmony_ci int n = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci for (b = 0; b < n; b++) { 818c2ecf20Sopenharmony_ci struct ppc_bat *bat = BATS[b]; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (!(bat[1].batu & 3)) 848c2ecf20Sopenharmony_ci return b; 858c2ecf20Sopenharmony_ci } 868c2ecf20Sopenharmony_ci return -1; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci/* 908c2ecf20Sopenharmony_ci * This function calculates the size of the larger block usable to map the 918c2ecf20Sopenharmony_ci * beginning of an area based on the start address and size of that area: 928c2ecf20Sopenharmony_ci * - max block size is 256 on 6xx. 938c2ecf20Sopenharmony_ci * - base address must be aligned to the block size. So the maximum block size 948c2ecf20Sopenharmony_ci * is identified by the lowest bit set to 1 in the base address (for instance 958c2ecf20Sopenharmony_ci * if base is 0x16000000, max size is 0x02000000). 968c2ecf20Sopenharmony_ci * - block size has to be a power of two. This is calculated by finding the 978c2ecf20Sopenharmony_ci * highest bit set to 1. 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_ciunsigned int bat_block_size(unsigned long base, unsigned long top) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci unsigned int max_size = SZ_256M; 1028c2ecf20Sopenharmony_ci unsigned int base_shift = (ffs(base) - 1) & 31; 1038c2ecf20Sopenharmony_ci unsigned int block_shift = (fls(top - base) - 1) & 31; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci return min3(max_size, 1U << base_shift, 1U << block_shift); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/* 1098c2ecf20Sopenharmony_ci * Set up one of the IBAT (block address translation) register pairs. 1108c2ecf20Sopenharmony_ci * The parameters are not checked; in particular size must be a power 1118c2ecf20Sopenharmony_ci * of 2 between 128k and 256M. 1128c2ecf20Sopenharmony_ci */ 1138c2ecf20Sopenharmony_cistatic void setibat(int index, unsigned long virt, phys_addr_t phys, 1148c2ecf20Sopenharmony_ci unsigned int size, pgprot_t prot) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci unsigned int bl = (size >> 17) - 1; 1178c2ecf20Sopenharmony_ci int wimgxpp; 1188c2ecf20Sopenharmony_ci struct ppc_bat *bat = BATS[index]; 1198c2ecf20Sopenharmony_ci unsigned long flags = pgprot_val(prot); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (!cpu_has_feature(CPU_FTR_NEED_COHERENT)) 1228c2ecf20Sopenharmony_ci flags &= ~_PAGE_COHERENT; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci wimgxpp = (flags & _PAGE_COHERENT) | (_PAGE_EXEC ? BPP_RX : BPP_XX); 1258c2ecf20Sopenharmony_ci bat[0].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */ 1268c2ecf20Sopenharmony_ci bat[0].batl = BAT_PHYS_ADDR(phys) | wimgxpp; 1278c2ecf20Sopenharmony_ci if (flags & _PAGE_USER) 1288c2ecf20Sopenharmony_ci bat[0].batu |= 1; /* Vp = 1 */ 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic void clearibat(int index) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci struct ppc_bat *bat = BATS[index]; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci bat[0].batu = 0; 1368c2ecf20Sopenharmony_ci bat[0].batl = 0; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic unsigned long __init __mmu_mapin_ram(unsigned long base, unsigned long top) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci int idx; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci while ((idx = find_free_bat()) != -1 && base != top) { 1448c2ecf20Sopenharmony_ci unsigned int size = bat_block_size(base, top); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (size < 128 << 10) 1478c2ecf20Sopenharmony_ci break; 1488c2ecf20Sopenharmony_ci setbat(idx, PAGE_OFFSET + base, base, size, PAGE_KERNEL_X); 1498c2ecf20Sopenharmony_ci base += size; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci return base; 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ciunsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci unsigned long done; 1588c2ecf20Sopenharmony_ci unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (__map_without_bats) { 1618c2ecf20Sopenharmony_ci pr_debug("RAM mapped without BATs\n"); 1628c2ecf20Sopenharmony_ci return base; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci if (debug_pagealloc_enabled()) { 1658c2ecf20Sopenharmony_ci if (base >= border) 1668c2ecf20Sopenharmony_ci return base; 1678c2ecf20Sopenharmony_ci if (top >= border) 1688c2ecf20Sopenharmony_ci top = border; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci if (!strict_kernel_rwx_enabled() || base >= border || top <= border) 1728c2ecf20Sopenharmony_ci return __mmu_mapin_ram(base, top); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci done = __mmu_mapin_ram(base, border); 1758c2ecf20Sopenharmony_ci if (done != border) 1768c2ecf20Sopenharmony_ci return done; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci return __mmu_mapin_ram(border, top); 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic bool is_module_segment(unsigned long addr) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_MODULES)) 1848c2ecf20Sopenharmony_ci return false; 1858c2ecf20Sopenharmony_ci#ifdef MODULES_VADDR 1868c2ecf20Sopenharmony_ci if (addr < ALIGN_DOWN(MODULES_VADDR, SZ_256M)) 1878c2ecf20Sopenharmony_ci return false; 1888c2ecf20Sopenharmony_ci if (addr > ALIGN(MODULES_END, SZ_256M) - 1) 1898c2ecf20Sopenharmony_ci return false; 1908c2ecf20Sopenharmony_ci#else 1918c2ecf20Sopenharmony_ci if (addr < ALIGN_DOWN(VMALLOC_START, SZ_256M)) 1928c2ecf20Sopenharmony_ci return false; 1938c2ecf20Sopenharmony_ci if (addr > ALIGN(VMALLOC_END, SZ_256M) - 1) 1948c2ecf20Sopenharmony_ci return false; 1958c2ecf20Sopenharmony_ci#endif 1968c2ecf20Sopenharmony_ci return true; 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_civoid mmu_mark_initmem_nx(void) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4; 2028c2ecf20Sopenharmony_ci int i; 2038c2ecf20Sopenharmony_ci unsigned long base = (unsigned long)_stext - PAGE_OFFSET; 2048c2ecf20Sopenharmony_ci unsigned long top = ALIGN((unsigned long)_etext - PAGE_OFFSET, SZ_128K); 2058c2ecf20Sopenharmony_ci unsigned long border = (unsigned long)__init_begin - PAGE_OFFSET; 2068c2ecf20Sopenharmony_ci unsigned long size; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci for (i = 0; i < nb - 1 && base < top;) { 2098c2ecf20Sopenharmony_ci size = bat_block_size(base, top); 2108c2ecf20Sopenharmony_ci setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT); 2118c2ecf20Sopenharmony_ci base += size; 2128c2ecf20Sopenharmony_ci } 2138c2ecf20Sopenharmony_ci if (base < top) { 2148c2ecf20Sopenharmony_ci size = bat_block_size(base, top); 2158c2ecf20Sopenharmony_ci if ((top - base) > size) { 2168c2ecf20Sopenharmony_ci size <<= 1; 2178c2ecf20Sopenharmony_ci if (strict_kernel_rwx_enabled() && base + size > border) 2188c2ecf20Sopenharmony_ci pr_warn("Some RW data is getting mapped X. " 2198c2ecf20Sopenharmony_ci "Adjust CONFIG_DATA_SHIFT to avoid that.\n"); 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci setibat(i++, PAGE_OFFSET + base, base, size, PAGE_KERNEL_TEXT); 2228c2ecf20Sopenharmony_ci base += size; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci for (; i < nb; i++) 2258c2ecf20Sopenharmony_ci clearibat(i); 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci update_bats(); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci for (i = TASK_SIZE >> 28; i < 16; i++) { 2308c2ecf20Sopenharmony_ci /* Do not set NX on VM space for modules */ 2318c2ecf20Sopenharmony_ci if (is_module_segment(i << 28)) 2328c2ecf20Sopenharmony_ci continue; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci mtsrin(mfsrin(i << 28) | 0x10000000, i << 28); 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_civoid mmu_mark_rodata_ro(void) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci int nb = mmu_has_feature(MMU_FTR_USE_HIGH_BATS) ? 8 : 4; 2418c2ecf20Sopenharmony_ci int i; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci for (i = 0; i < nb; i++) { 2448c2ecf20Sopenharmony_ci struct ppc_bat *bat = BATS[i]; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci if (bat_addrs[i].start < (unsigned long)__init_begin) 2478c2ecf20Sopenharmony_ci bat[1].batl = (bat[1].batl & ~BPP_RW) | BPP_RX; 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci update_bats(); 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/* 2548c2ecf20Sopenharmony_ci * Set up one of the I/D BAT (block address translation) register pairs. 2558c2ecf20Sopenharmony_ci * The parameters are not checked; in particular size must be a power 2568c2ecf20Sopenharmony_ci * of 2 between 128k and 256M. 2578c2ecf20Sopenharmony_ci * On 603+, only set IBAT when _PAGE_EXEC is set 2588c2ecf20Sopenharmony_ci */ 2598c2ecf20Sopenharmony_civoid __init setbat(int index, unsigned long virt, phys_addr_t phys, 2608c2ecf20Sopenharmony_ci unsigned int size, pgprot_t prot) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci unsigned int bl; 2638c2ecf20Sopenharmony_ci int wimgxpp; 2648c2ecf20Sopenharmony_ci struct ppc_bat *bat; 2658c2ecf20Sopenharmony_ci unsigned long flags = pgprot_val(prot); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (index == -1) 2688c2ecf20Sopenharmony_ci index = find_free_bat(); 2698c2ecf20Sopenharmony_ci if (index == -1) { 2708c2ecf20Sopenharmony_ci pr_err("%s: no BAT available for mapping 0x%llx\n", __func__, 2718c2ecf20Sopenharmony_ci (unsigned long long)phys); 2728c2ecf20Sopenharmony_ci return; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci bat = BATS[index]; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci if ((flags & _PAGE_NO_CACHE) || 2778c2ecf20Sopenharmony_ci (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0)) 2788c2ecf20Sopenharmony_ci flags &= ~_PAGE_COHERENT; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci bl = (size >> 17) - 1; 2818c2ecf20Sopenharmony_ci /* Do DBAT first */ 2828c2ecf20Sopenharmony_ci wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE 2838c2ecf20Sopenharmony_ci | _PAGE_COHERENT | _PAGE_GUARDED); 2848c2ecf20Sopenharmony_ci wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX; 2858c2ecf20Sopenharmony_ci bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */ 2868c2ecf20Sopenharmony_ci bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp; 2878c2ecf20Sopenharmony_ci if (flags & _PAGE_USER) 2888c2ecf20Sopenharmony_ci bat[1].batu |= 1; /* Vp = 1 */ 2898c2ecf20Sopenharmony_ci if (flags & _PAGE_GUARDED) { 2908c2ecf20Sopenharmony_ci /* G bit must be zero in IBATs */ 2918c2ecf20Sopenharmony_ci flags &= ~_PAGE_EXEC; 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci if (flags & _PAGE_EXEC) 2948c2ecf20Sopenharmony_ci bat[0] = bat[1]; 2958c2ecf20Sopenharmony_ci else 2968c2ecf20Sopenharmony_ci bat[0].batu = bat[0].batl = 0; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci bat_addrs[index].start = virt; 2998c2ecf20Sopenharmony_ci bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1; 3008c2ecf20Sopenharmony_ci bat_addrs[index].phys = phys; 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci/* 3048c2ecf20Sopenharmony_ci * Preload a translation in the hash table 3058c2ecf20Sopenharmony_ci */ 3068c2ecf20Sopenharmony_civoid hash_preload(struct mm_struct *mm, unsigned long ea) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci pmd_t *pmd; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci if (!Hash) 3118c2ecf20Sopenharmony_ci return; 3128c2ecf20Sopenharmony_ci pmd = pmd_off(mm, ea); 3138c2ecf20Sopenharmony_ci if (!pmd_none(*pmd)) 3148c2ecf20Sopenharmony_ci add_hash_page(mm->context.id, ea, pmd_val(*pmd)); 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci/* 3188c2ecf20Sopenharmony_ci * This is called at the end of handling a user page fault, when the 3198c2ecf20Sopenharmony_ci * fault has been handled by updating a PTE in the linux page tables. 3208c2ecf20Sopenharmony_ci * We use it to preload an HPTE into the hash table corresponding to 3218c2ecf20Sopenharmony_ci * the updated linux PTE. 3228c2ecf20Sopenharmony_ci * 3238c2ecf20Sopenharmony_ci * This must always be called with the pte lock held. 3248c2ecf20Sopenharmony_ci */ 3258c2ecf20Sopenharmony_civoid update_mmu_cache(struct vm_area_struct *vma, unsigned long address, 3268c2ecf20Sopenharmony_ci pte_t *ptep) 3278c2ecf20Sopenharmony_ci{ 3288c2ecf20Sopenharmony_ci if (!mmu_has_feature(MMU_FTR_HPTE_TABLE)) 3298c2ecf20Sopenharmony_ci return; 3308c2ecf20Sopenharmony_ci /* 3318c2ecf20Sopenharmony_ci * We don't need to worry about _PAGE_PRESENT here because we are 3328c2ecf20Sopenharmony_ci * called with either mm->page_table_lock held or ptl lock held 3338c2ecf20Sopenharmony_ci */ 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */ 3368c2ecf20Sopenharmony_ci if (!pte_young(*ptep) || address >= TASK_SIZE) 3378c2ecf20Sopenharmony_ci return; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci /* We have to test for regs NULL since init will get here first thing at boot */ 3408c2ecf20Sopenharmony_ci if (!current->thread.regs) 3418c2ecf20Sopenharmony_ci return; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci /* We also avoid filling the hash if not coming from a fault */ 3448c2ecf20Sopenharmony_ci if (TRAP(current->thread.regs) != 0x300 && TRAP(current->thread.regs) != 0x400) 3458c2ecf20Sopenharmony_ci return; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci hash_preload(vma->vm_mm, address); 3488c2ecf20Sopenharmony_ci} 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci/* 3518c2ecf20Sopenharmony_ci * Initialize the hash table and patch the instructions in hashtable.S. 3528c2ecf20Sopenharmony_ci */ 3538c2ecf20Sopenharmony_civoid __init MMU_init_hw(void) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci unsigned int n_hpteg, lg_n_hpteg; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci if (!mmu_has_feature(MMU_FTR_HPTE_TABLE)) 3588c2ecf20Sopenharmony_ci return; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci#define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */ 3638c2ecf20Sopenharmony_ci#define SDR1_LOW_BITS ((n_hpteg - 1) >> 10) 3648c2ecf20Sopenharmony_ci#define MIN_N_HPTEG 1024 /* min 64kB hash table */ 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci /* 3678c2ecf20Sopenharmony_ci * Allow 1 HPTE (1/8 HPTEG) for each page of memory. 3688c2ecf20Sopenharmony_ci * This is less than the recommended amount, but then 3698c2ecf20Sopenharmony_ci * Linux ain't AIX. 3708c2ecf20Sopenharmony_ci */ 3718c2ecf20Sopenharmony_ci n_hpteg = total_memory / (PAGE_SIZE * 8); 3728c2ecf20Sopenharmony_ci if (n_hpteg < MIN_N_HPTEG) 3738c2ecf20Sopenharmony_ci n_hpteg = MIN_N_HPTEG; 3748c2ecf20Sopenharmony_ci lg_n_hpteg = __ilog2(n_hpteg); 3758c2ecf20Sopenharmony_ci if (n_hpteg & (n_hpteg - 1)) { 3768c2ecf20Sopenharmony_ci ++lg_n_hpteg; /* round up if not power of 2 */ 3778c2ecf20Sopenharmony_ci n_hpteg = 1 << lg_n_hpteg; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci Hash_size = n_hpteg << LG_HPTEG_SIZE; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci /* 3828c2ecf20Sopenharmony_ci * Find some memory for the hash table. 3838c2ecf20Sopenharmony_ci */ 3848c2ecf20Sopenharmony_ci if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322); 3858c2ecf20Sopenharmony_ci Hash = memblock_alloc(Hash_size, Hash_size); 3868c2ecf20Sopenharmony_ci if (!Hash) 3878c2ecf20Sopenharmony_ci panic("%s: Failed to allocate %lu bytes align=0x%lx\n", 3888c2ecf20Sopenharmony_ci __func__, Hash_size, Hash_size); 3898c2ecf20Sopenharmony_ci _SDR1 = __pa(Hash) | SDR1_LOW_BITS; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci pr_info("Total memory = %lldMB; using %ldkB for hash table\n", 3928c2ecf20Sopenharmony_ci (unsigned long long)(total_memory >> 20), Hash_size >> 10); 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci Hash_mask = n_hpteg - 1; 3968c2ecf20Sopenharmony_ci hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg; 3978c2ecf20Sopenharmony_ci if (lg_n_hpteg > 16) 3988c2ecf20Sopenharmony_ci hash_mb2 = 16 - LG_HPTEG_SIZE; 3998c2ecf20Sopenharmony_ci} 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_civoid __init MMU_init_hw_patch(void) 4028c2ecf20Sopenharmony_ci{ 4038c2ecf20Sopenharmony_ci unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE); 4048c2ecf20Sopenharmony_ci unsigned int hash = (unsigned int)Hash - PAGE_OFFSET; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci if (!mmu_has_feature(MMU_FTR_HPTE_TABLE)) 4078c2ecf20Sopenharmony_ci return; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci if (ppc_md.progress) 4108c2ecf20Sopenharmony_ci ppc_md.progress("hash:patch", 0x345); 4118c2ecf20Sopenharmony_ci if (ppc_md.progress) 4128c2ecf20Sopenharmony_ci ppc_md.progress("hash:done", 0x205); 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* WARNING: Make sure nothing can trigger a KASAN check past this point */ 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci /* 4178c2ecf20Sopenharmony_ci * Patch up the instructions in hashtable.S:create_hpte 4188c2ecf20Sopenharmony_ci */ 4198c2ecf20Sopenharmony_ci modify_instruction_site(&patch__hash_page_A0, 0xffff, hash >> 16); 4208c2ecf20Sopenharmony_ci modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6); 4218c2ecf20Sopenharmony_ci modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6); 4228c2ecf20Sopenharmony_ci modify_instruction_site(&patch__hash_page_B, 0xffff, hmask); 4238c2ecf20Sopenharmony_ci modify_instruction_site(&patch__hash_page_C, 0xffff, hmask); 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci /* 4268c2ecf20Sopenharmony_ci * Patch up the instructions in hashtable.S:flush_hash_page 4278c2ecf20Sopenharmony_ci */ 4288c2ecf20Sopenharmony_ci modify_instruction_site(&patch__flush_hash_A0, 0xffff, hash >> 16); 4298c2ecf20Sopenharmony_ci modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6); 4308c2ecf20Sopenharmony_ci modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6); 4318c2ecf20Sopenharmony_ci modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask); 4328c2ecf20Sopenharmony_ci} 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_civoid setup_initial_memory_limit(phys_addr_t first_memblock_base, 4358c2ecf20Sopenharmony_ci phys_addr_t first_memblock_size) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci /* We don't currently support the first MEMBLOCK not mapping 0 4388c2ecf20Sopenharmony_ci * physical on those processors 4398c2ecf20Sopenharmony_ci */ 4408c2ecf20Sopenharmony_ci BUG_ON(first_memblock_base != 0); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci memblock_set_current_limit(min_t(u64, first_memblock_size, SZ_256M)); 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_civoid __init print_system_hash_info(void) 4468c2ecf20Sopenharmony_ci{ 4478c2ecf20Sopenharmony_ci pr_info("Hash_size = 0x%lx\n", Hash_size); 4488c2ecf20Sopenharmony_ci if (Hash_mask) 4498c2ecf20Sopenharmony_ci pr_info("Hash_mask = 0x%lx\n", Hash_mask); 4508c2ecf20Sopenharmony_ci} 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_KUEP 4538c2ecf20Sopenharmony_civoid __init setup_kuep(bool disabled) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci pr_info("Activating Kernel Userspace Execution Prevention\n"); 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci if (disabled) 4588c2ecf20Sopenharmony_ci pr_warn("KUEP cannot be disabled yet on 6xx when compiled in\n"); 4598c2ecf20Sopenharmony_ci} 4608c2ecf20Sopenharmony_ci#endif 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_KUAP 4638c2ecf20Sopenharmony_civoid __init setup_kuap(bool disabled) 4648c2ecf20Sopenharmony_ci{ 4658c2ecf20Sopenharmony_ci pr_info("Activating Kernel Userspace Access Protection\n"); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci if (disabled) 4688c2ecf20Sopenharmony_ci pr_warn("KUAP cannot be disabled yet on 6xx when compiled in\n"); 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci#endif 471