18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <linux/errno.h> 38c2ecf20Sopenharmony_ci#include <linux/init.h> 48c2ecf20Sopenharmony_ci#include <asm/sclp.h> 58c2ecf20Sopenharmony_ci#include <asm/sections.h> 68c2ecf20Sopenharmony_ci#include <asm/mem_detect.h> 78c2ecf20Sopenharmony_ci#include <asm/sparsemem.h> 88c2ecf20Sopenharmony_ci#include "compressed/decompressor.h" 98c2ecf20Sopenharmony_ci#include "boot.h" 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ciunsigned long __bootdata(max_physmem_end); 128c2ecf20Sopenharmony_cistruct mem_detect_info __bootdata(mem_detect); 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/* up to 256 storage elements, 1020 subincrements each */ 158c2ecf20Sopenharmony_ci#define ENTRIES_EXTENDED_MAX \ 168c2ecf20Sopenharmony_ci (256 * (1020 / 2) * sizeof(struct mem_detect_block)) 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* 198c2ecf20Sopenharmony_ci * To avoid corrupting old kernel memory during dump, find lowest memory 208c2ecf20Sopenharmony_ci * chunk possible either right after the kernel end (decompressed kernel) or 218c2ecf20Sopenharmony_ci * after initrd (if it is present and there is no hole between the kernel end 228c2ecf20Sopenharmony_ci * and initrd) 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_cistatic void *mem_detect_alloc_extended(void) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci unsigned long offset = ALIGN(mem_safe_offset(), sizeof(u64)); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && INITRD_START && INITRD_SIZE && 298c2ecf20Sopenharmony_ci INITRD_START < offset + ENTRIES_EXTENDED_MAX) 308c2ecf20Sopenharmony_ci offset = ALIGN(INITRD_START + INITRD_SIZE, sizeof(u64)); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci return (void *)offset; 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic struct mem_detect_block *__get_mem_detect_block_ptr(u32 n) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci if (n < MEM_INLINED_ENTRIES) 388c2ecf20Sopenharmony_ci return &mem_detect.entries[n]; 398c2ecf20Sopenharmony_ci if (unlikely(!mem_detect.entries_extended)) 408c2ecf20Sopenharmony_ci mem_detect.entries_extended = mem_detect_alloc_extended(); 418c2ecf20Sopenharmony_ci return &mem_detect.entries_extended[n - MEM_INLINED_ENTRIES]; 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* 458c2ecf20Sopenharmony_ci * sequential calls to add_mem_detect_block with adjacent memory areas 468c2ecf20Sopenharmony_ci * are merged together into single memory block. 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_civoid add_mem_detect_block(u64 start, u64 end) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci struct mem_detect_block *block; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (mem_detect.count) { 538c2ecf20Sopenharmony_ci block = __get_mem_detect_block_ptr(mem_detect.count - 1); 548c2ecf20Sopenharmony_ci if (block->end == start) { 558c2ecf20Sopenharmony_ci block->end = end; 568c2ecf20Sopenharmony_ci return; 578c2ecf20Sopenharmony_ci } 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci block = __get_mem_detect_block_ptr(mem_detect.count); 618c2ecf20Sopenharmony_ci block->start = start; 628c2ecf20Sopenharmony_ci block->end = end; 638c2ecf20Sopenharmony_ci mem_detect.count++; 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic int __diag260(unsigned long rx1, unsigned long rx2) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci register unsigned long _rx1 asm("2") = rx1; 698c2ecf20Sopenharmony_ci register unsigned long _rx2 asm("3") = rx2; 708c2ecf20Sopenharmony_ci register unsigned long _ry asm("4") = 0x10; /* storage configuration */ 718c2ecf20Sopenharmony_ci int rc = -1; /* fail */ 728c2ecf20Sopenharmony_ci unsigned long reg1, reg2; 738c2ecf20Sopenharmony_ci psw_t old; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci asm volatile( 768c2ecf20Sopenharmony_ci " mvc 0(16,%[psw_old]),0(%[psw_pgm])\n" 778c2ecf20Sopenharmony_ci " epsw %0,%1\n" 788c2ecf20Sopenharmony_ci " st %0,0(%[psw_pgm])\n" 798c2ecf20Sopenharmony_ci " st %1,4(%[psw_pgm])\n" 808c2ecf20Sopenharmony_ci " larl %0,1f\n" 818c2ecf20Sopenharmony_ci " stg %0,8(%[psw_pgm])\n" 828c2ecf20Sopenharmony_ci " diag %[rx],%[ry],0x260\n" 838c2ecf20Sopenharmony_ci " ipm %[rc]\n" 848c2ecf20Sopenharmony_ci " srl %[rc],28\n" 858c2ecf20Sopenharmony_ci "1: mvc 0(16,%[psw_pgm]),0(%[psw_old])\n" 868c2ecf20Sopenharmony_ci : "=&d" (reg1), "=&a" (reg2), 878c2ecf20Sopenharmony_ci "+Q" (S390_lowcore.program_new_psw), 888c2ecf20Sopenharmony_ci "=Q" (old), 898c2ecf20Sopenharmony_ci [rc] "+&d" (rc), [ry] "+d" (_ry) 908c2ecf20Sopenharmony_ci : [rx] "d" (_rx1), "d" (_rx2), 918c2ecf20Sopenharmony_ci [psw_old] "a" (&old), 928c2ecf20Sopenharmony_ci [psw_pgm] "a" (&S390_lowcore.program_new_psw) 938c2ecf20Sopenharmony_ci : "cc", "memory"); 948c2ecf20Sopenharmony_ci return rc == 0 ? _ry : -1; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic int diag260(void) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci int rc, i; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci struct { 1028c2ecf20Sopenharmony_ci unsigned long start; 1038c2ecf20Sopenharmony_ci unsigned long end; 1048c2ecf20Sopenharmony_ci } storage_extents[8] __aligned(16); /* VM supports up to 8 extends */ 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci memset(storage_extents, 0, sizeof(storage_extents)); 1078c2ecf20Sopenharmony_ci rc = __diag260((unsigned long)storage_extents, sizeof(storage_extents)); 1088c2ecf20Sopenharmony_ci if (rc == -1) 1098c2ecf20Sopenharmony_ci return -1; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci for (i = 0; i < min_t(int, rc, ARRAY_SIZE(storage_extents)); i++) 1128c2ecf20Sopenharmony_ci add_mem_detect_block(storage_extents[i].start, storage_extents[i].end + 1); 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic int tprot(unsigned long addr) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci unsigned long reg1, reg2; 1198c2ecf20Sopenharmony_ci int rc = -EFAULT; 1208c2ecf20Sopenharmony_ci psw_t old; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci asm volatile( 1238c2ecf20Sopenharmony_ci " mvc 0(16,%[psw_old]),0(%[psw_pgm])\n" 1248c2ecf20Sopenharmony_ci " epsw %[reg1],%[reg2]\n" 1258c2ecf20Sopenharmony_ci " st %[reg1],0(%[psw_pgm])\n" 1268c2ecf20Sopenharmony_ci " st %[reg2],4(%[psw_pgm])\n" 1278c2ecf20Sopenharmony_ci " larl %[reg1],1f\n" 1288c2ecf20Sopenharmony_ci " stg %[reg1],8(%[psw_pgm])\n" 1298c2ecf20Sopenharmony_ci " tprot 0(%[addr]),0\n" 1308c2ecf20Sopenharmony_ci " ipm %[rc]\n" 1318c2ecf20Sopenharmony_ci " srl %[rc],28\n" 1328c2ecf20Sopenharmony_ci "1: mvc 0(16,%[psw_pgm]),0(%[psw_old])\n" 1338c2ecf20Sopenharmony_ci : [reg1] "=&d" (reg1), 1348c2ecf20Sopenharmony_ci [reg2] "=&a" (reg2), 1358c2ecf20Sopenharmony_ci [rc] "+&d" (rc), 1368c2ecf20Sopenharmony_ci "=Q" (S390_lowcore.program_new_psw.addr), 1378c2ecf20Sopenharmony_ci "=Q" (old) 1388c2ecf20Sopenharmony_ci : [psw_old] "a" (&old), 1398c2ecf20Sopenharmony_ci [psw_pgm] "a" (&S390_lowcore.program_new_psw), 1408c2ecf20Sopenharmony_ci [addr] "a" (addr) 1418c2ecf20Sopenharmony_ci : "cc", "memory"); 1428c2ecf20Sopenharmony_ci return rc; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic void search_mem_end(void) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci unsigned long range = 1 << (MAX_PHYSMEM_BITS - 20); /* in 1MB blocks */ 1488c2ecf20Sopenharmony_ci unsigned long offset = 0; 1498c2ecf20Sopenharmony_ci unsigned long pivot; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci while (range > 1) { 1528c2ecf20Sopenharmony_ci range >>= 1; 1538c2ecf20Sopenharmony_ci pivot = offset + range; 1548c2ecf20Sopenharmony_ci if (!tprot(pivot << 20)) 1558c2ecf20Sopenharmony_ci offset = pivot; 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci add_mem_detect_block(0, (offset + 1) << 20); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_civoid detect_memory(void) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci sclp_early_get_memsize(&max_physmem_end); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (!sclp_early_read_storage_info()) { 1668c2ecf20Sopenharmony_ci mem_detect.info_source = MEM_DETECT_SCLP_STOR_INFO; 1678c2ecf20Sopenharmony_ci return; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci if (!diag260()) { 1718c2ecf20Sopenharmony_ci mem_detect.info_source = MEM_DETECT_DIAG260; 1728c2ecf20Sopenharmony_ci return; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci if (max_physmem_end) { 1768c2ecf20Sopenharmony_ci add_mem_detect_block(0, max_physmem_end); 1778c2ecf20Sopenharmony_ci mem_detect.info_source = MEM_DETECT_SCLP_READ_INFO; 1788c2ecf20Sopenharmony_ci return; 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci search_mem_end(); 1828c2ecf20Sopenharmony_ci mem_detect.info_source = MEM_DETECT_BIN_SEARCH; 1838c2ecf20Sopenharmony_ci max_physmem_end = get_mem_detect_end(); 1848c2ecf20Sopenharmony_ci} 185