18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright IBM Corporation, 2013 38c2ecf20Sopenharmony_ci * Author Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com> 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 68c2ecf20Sopenharmony_ci * under the terms of version 2.1 of the GNU Lesser General Public License 78c2ecf20Sopenharmony_ci * as published by the Free Software Foundation. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This program is distributed in the hope that it would be useful, but 108c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 118c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* 168c2ecf20Sopenharmony_ci * PPC64 THP Support for hash based MMUs 178c2ecf20Sopenharmony_ci */ 188c2ecf20Sopenharmony_ci#include <linux/mm.h> 198c2ecf20Sopenharmony_ci#include <asm/machdep.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciint __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid, 228c2ecf20Sopenharmony_ci pmd_t *pmdp, unsigned long trap, unsigned long flags, 238c2ecf20Sopenharmony_ci int ssize, unsigned int psize) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci unsigned int index, valid; 268c2ecf20Sopenharmony_ci unsigned char *hpte_slot_array; 278c2ecf20Sopenharmony_ci unsigned long rflags, pa, hidx; 288c2ecf20Sopenharmony_ci unsigned long old_pmd, new_pmd; 298c2ecf20Sopenharmony_ci int ret, lpsize = MMU_PAGE_16M; 308c2ecf20Sopenharmony_ci unsigned long vpn, hash, shift, slot; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci /* 338c2ecf20Sopenharmony_ci * atomically mark the linux large page PMD busy and dirty 348c2ecf20Sopenharmony_ci */ 358c2ecf20Sopenharmony_ci do { 368c2ecf20Sopenharmony_ci pmd_t pmd = READ_ONCE(*pmdp); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci old_pmd = pmd_val(pmd); 398c2ecf20Sopenharmony_ci /* If PMD busy, retry the access */ 408c2ecf20Sopenharmony_ci if (unlikely(old_pmd & H_PAGE_BUSY)) 418c2ecf20Sopenharmony_ci return 0; 428c2ecf20Sopenharmony_ci /* If PMD permissions don't match, take page fault */ 438c2ecf20Sopenharmony_ci if (unlikely(!check_pte_access(access, old_pmd))) 448c2ecf20Sopenharmony_ci return 1; 458c2ecf20Sopenharmony_ci /* 468c2ecf20Sopenharmony_ci * Try to lock the PTE, add ACCESSED and DIRTY if it was 478c2ecf20Sopenharmony_ci * a write access 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ci new_pmd = old_pmd | H_PAGE_BUSY | _PAGE_ACCESSED; 508c2ecf20Sopenharmony_ci if (access & _PAGE_WRITE) 518c2ecf20Sopenharmony_ci new_pmd |= _PAGE_DIRTY; 528c2ecf20Sopenharmony_ci } while (!pmd_xchg(pmdp, __pmd(old_pmd), __pmd(new_pmd))); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* 558c2ecf20Sopenharmony_ci * Make sure this is thp or devmap entry 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ci if (!(old_pmd & (H_PAGE_THP_HUGE | _PAGE_DEVMAP))) 588c2ecf20Sopenharmony_ci return 0; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci rflags = htab_convert_pte_flags(new_pmd); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#if 0 638c2ecf20Sopenharmony_ci if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* 668c2ecf20Sopenharmony_ci * No CPU has hugepages but lacks no execute, so we 678c2ecf20Sopenharmony_ci * don't need to worry about that case 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_ci rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci#endif 728c2ecf20Sopenharmony_ci /* 738c2ecf20Sopenharmony_ci * Find the slot index details for this ea, using base page size. 748c2ecf20Sopenharmony_ci */ 758c2ecf20Sopenharmony_ci shift = mmu_psize_defs[psize].shift; 768c2ecf20Sopenharmony_ci index = (ea & ~HPAGE_PMD_MASK) >> shift; 778c2ecf20Sopenharmony_ci BUG_ON(index >= PTE_FRAG_SIZE); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci vpn = hpt_vpn(ea, vsid, ssize); 808c2ecf20Sopenharmony_ci hpte_slot_array = get_hpte_slot_array(pmdp); 818c2ecf20Sopenharmony_ci if (psize == MMU_PAGE_4K) { 828c2ecf20Sopenharmony_ci /* 838c2ecf20Sopenharmony_ci * invalidate the old hpte entry if we have that mapped via 64K 848c2ecf20Sopenharmony_ci * base page size. This is because demote_segment won't flush 858c2ecf20Sopenharmony_ci * hash page table entries. 868c2ecf20Sopenharmony_ci */ 878c2ecf20Sopenharmony_ci if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) { 888c2ecf20Sopenharmony_ci flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K, 898c2ecf20Sopenharmony_ci ssize, flags); 908c2ecf20Sopenharmony_ci /* 918c2ecf20Sopenharmony_ci * With THP, we also clear the slot information with 928c2ecf20Sopenharmony_ci * respect to all the 64K hash pte mapping the 16MB 938c2ecf20Sopenharmony_ci * page. They are all invalid now. This make sure we 948c2ecf20Sopenharmony_ci * don't find the slot valid when we fault with 4k 958c2ecf20Sopenharmony_ci * base page size. 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_ci memset(hpte_slot_array, 0, PTE_FRAG_SIZE); 998c2ecf20Sopenharmony_ci } 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci valid = hpte_valid(hpte_slot_array, index); 1038c2ecf20Sopenharmony_ci if (valid) { 1048c2ecf20Sopenharmony_ci /* update the hpte bits */ 1058c2ecf20Sopenharmony_ci hash = hpt_hash(vpn, shift, ssize); 1068c2ecf20Sopenharmony_ci hidx = hpte_hash_index(hpte_slot_array, index); 1078c2ecf20Sopenharmony_ci if (hidx & _PTEIDX_SECONDARY) 1088c2ecf20Sopenharmony_ci hash = ~hash; 1098c2ecf20Sopenharmony_ci slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; 1108c2ecf20Sopenharmony_ci slot += hidx & _PTEIDX_GROUP_IX; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn, 1138c2ecf20Sopenharmony_ci psize, lpsize, ssize, flags); 1148c2ecf20Sopenharmony_ci /* 1158c2ecf20Sopenharmony_ci * We failed to update, try to insert a new entry. 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_ci if (ret == -1) { 1188c2ecf20Sopenharmony_ci /* 1198c2ecf20Sopenharmony_ci * large pte is marked busy, so we can be sure 1208c2ecf20Sopenharmony_ci * nobody is looking at hpte_slot_array. hence we can 1218c2ecf20Sopenharmony_ci * safely update this here. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_ci valid = 0; 1248c2ecf20Sopenharmony_ci hpte_slot_array[index] = 0; 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci if (!valid) { 1298c2ecf20Sopenharmony_ci unsigned long hpte_group; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci hash = hpt_hash(vpn, shift, ssize); 1328c2ecf20Sopenharmony_ci /* insert new entry */ 1338c2ecf20Sopenharmony_ci pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT; 1348c2ecf20Sopenharmony_ci new_pmd |= H_PAGE_HASHPTE; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_cirepeat: 1378c2ecf20Sopenharmony_ci hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* Insert into the hash table, primary slot */ 1408c2ecf20Sopenharmony_ci slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0, 1418c2ecf20Sopenharmony_ci psize, lpsize, ssize); 1428c2ecf20Sopenharmony_ci /* 1438c2ecf20Sopenharmony_ci * Primary is full, try the secondary 1448c2ecf20Sopenharmony_ci */ 1458c2ecf20Sopenharmony_ci if (unlikely(slot == -1)) { 1468c2ecf20Sopenharmony_ci hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP; 1478c2ecf20Sopenharmony_ci slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, 1488c2ecf20Sopenharmony_ci rflags, 1498c2ecf20Sopenharmony_ci HPTE_V_SECONDARY, 1508c2ecf20Sopenharmony_ci psize, lpsize, ssize); 1518c2ecf20Sopenharmony_ci if (slot == -1) { 1528c2ecf20Sopenharmony_ci if (mftb() & 0x1) 1538c2ecf20Sopenharmony_ci hpte_group = (hash & htab_hash_mask) * 1548c2ecf20Sopenharmony_ci HPTES_PER_GROUP; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci mmu_hash_ops.hpte_remove(hpte_group); 1578c2ecf20Sopenharmony_ci goto repeat; 1588c2ecf20Sopenharmony_ci } 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci /* 1618c2ecf20Sopenharmony_ci * Hypervisor failure. Restore old pmd and return -1 1628c2ecf20Sopenharmony_ci * similar to __hash_page_* 1638c2ecf20Sopenharmony_ci */ 1648c2ecf20Sopenharmony_ci if (unlikely(slot == -2)) { 1658c2ecf20Sopenharmony_ci *pmdp = __pmd(old_pmd); 1668c2ecf20Sopenharmony_ci hash_failure_debug(ea, access, vsid, trap, ssize, 1678c2ecf20Sopenharmony_ci psize, lpsize, old_pmd); 1688c2ecf20Sopenharmony_ci return -1; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci /* 1718c2ecf20Sopenharmony_ci * large pte is marked busy, so we can be sure 1728c2ecf20Sopenharmony_ci * nobody is looking at hpte_slot_array. hence we can 1738c2ecf20Sopenharmony_ci * safely update this here. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci mark_hpte_slot_valid(hpte_slot_array, index, slot); 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci /* 1788c2ecf20Sopenharmony_ci * Mark the pte with H_PAGE_COMBO, if we are trying to hash it with 1798c2ecf20Sopenharmony_ci * base page size 4k. 1808c2ecf20Sopenharmony_ci */ 1818c2ecf20Sopenharmony_ci if (psize == MMU_PAGE_4K) 1828c2ecf20Sopenharmony_ci new_pmd |= H_PAGE_COMBO; 1838c2ecf20Sopenharmony_ci /* 1848c2ecf20Sopenharmony_ci * The hpte valid is stored in the pgtable whose address is in the 1858c2ecf20Sopenharmony_ci * second half of the PMD. Order this against clearing of the busy bit in 1868c2ecf20Sopenharmony_ci * huge pmd. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci smp_wmb(); 1898c2ecf20Sopenharmony_ci *pmdp = __pmd(new_pmd & ~H_PAGE_BUSY); 1908c2ecf20Sopenharmony_ci return 0; 1918c2ecf20Sopenharmony_ci} 192