1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * s390x SCLP driver 4 * 5 * Copyright (c) 2017 Red Hat Inc 6 * 7 * Authors: 8 * David Hildenbrand <david@redhat.com> 9 */ 10 11 #include <libcflat.h> 12 #include <asm/page.h> 13 #include <asm/arch_def.h> 14 #include <asm/interrupt.h> 15 #include <asm/barrier.h> 16 #include <asm/spinlock.h> 17 #include "sclp.h" 18 #include <alloc_phys.h> 19 #include <alloc_page.h> 20 21 extern unsigned long stacktop; 22 23 static uint64_t storage_increment_size; 24 static uint64_t max_ram_size; 25 static uint64_t ram_size; 26 char _read_info[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE))); 27 static ReadInfo *read_info; 28 struct sclp_facilities sclp_facilities; 29 30 char _sccb[PAGE_SIZE] __attribute__((__aligned__(4096))); 31 static volatile bool sclp_busy; 32 static struct spinlock sclp_lock; 33 34 static void mem_init(phys_addr_t mem_end) 35 { 36 phys_addr_t freemem_start = (phys_addr_t)&stacktop; 37 phys_addr_t base, top; 38 39 phys_alloc_init(freemem_start, mem_end - freemem_start); 40 phys_alloc_get_unused(&base, &top); 41 base = PAGE_ALIGN(base) >> PAGE_SHIFT; 42 top = top >> PAGE_SHIFT; 43 44 /* Make the pages available to the physical allocator */ 45 page_alloc_init_area(AREA_ANY_NUMBER, base, top); 46 page_alloc_ops_enable(); 47 } 48 49 void sclp_setup_int(void) 50 { 51 uint64_t mask; 52 53 ctl_set_bit(0, 9); 54 55 mask = extract_psw_mask(); 56 mask |= PSW_MASK_EXT; 57 load_psw_mask(mask); 58 } 59 60 void sclp_handle_ext(void) 61 { 62 ctl_clear_bit(0, 9); 63 spin_lock(&sclp_lock); 64 sclp_busy = false; 65 spin_unlock(&sclp_lock); 66 } 67 68 void sclp_wait_busy(void) 69 { 70 while (sclp_busy) 71 mb(); 72 } 73 74 void sclp_mark_busy(void) 75 { 76 /* 77 * With multiple CPUs we might need to wait for another CPU's 78 * request before grabbing the busy indication. 79 */ 80 while (true) { 81 sclp_wait_busy(); 82 spin_lock(&sclp_lock); 83 if (!sclp_busy) { 84 sclp_busy = true; 85 spin_unlock(&sclp_lock); 86 return; 87 } 88 spin_unlock(&sclp_lock); 89 } 90 } 91 92 static void sclp_read_scp_info(ReadInfo *ri, int length) 93 { 94 unsigned int commands[] = { SCLP_CMDW_READ_SCP_INFO_FORCED, 95 SCLP_CMDW_READ_SCP_INFO }; 96 int i, cc; 97 98 for (i = 0; i < ARRAY_SIZE(commands); i++) { 99 sclp_mark_busy(); 100 memset(&ri->h, 0, sizeof(ri->h)); 101 ri->h.length = length; 102 103 cc = sclp_service_call(commands[i], ri); 104 if (cc) 105 break; 106 if (ri->h.response_code == SCLP_RC_NORMAL_READ_COMPLETION) 107 return; 108 if (ri->h.response_code != SCLP_RC_INVALID_SCLP_COMMAND) 109 break; 110 } 111 report_abort("READ_SCP_INFO failed"); 112 } 113 114 void sclp_read_info(void) 115 { 116 sclp_read_scp_info((void *)_read_info, SCCB_SIZE); 117 read_info = (ReadInfo *)_read_info; 118 } 119 120 int sclp_get_cpu_num(void) 121 { 122 assert(read_info); 123 return read_info->entries_cpu; 124 } 125 126 CPUEntry *sclp_get_cpu_entries(void) 127 { 128 assert(read_info); 129 return (CPUEntry *)(_read_info + read_info->offset_cpu); 130 } 131 132 void sclp_facilities_setup(void) 133 { 134 unsigned short cpu0_addr = stap(); 135 CPUEntry *cpu; 136 int i; 137 138 assert(read_info); 139 140 cpu = sclp_get_cpu_entries(); 141 sclp_facilities.has_diag318 = read_info->byte_134_diag318; 142 for (i = 0; i < read_info->entries_cpu; i++, cpu++) { 143 /* 144 * The logic for only reading the facilities from the 145 * boot cpu comes from the kernel. I haven't yet found 146 * documentation that explains why this is necessary 147 * but I figure there's a reason behind doing it this 148 * way. 149 */ 150 if (cpu->address == cpu0_addr) { 151 sclp_facilities.has_sief2 = cpu->feat_sief2; 152 break; 153 } 154 } 155 } 156 157 /* Perform service call. Return 0 on success, non-zero otherwise. */ 158 int sclp_service_call(unsigned int command, void *sccb) 159 { 160 int cc; 161 162 sclp_setup_int(); 163 cc = servc(command, __pa(sccb)); 164 sclp_wait_busy(); 165 if (cc == 3) 166 return -1; 167 if (cc == 2) 168 return -1; 169 return 0; 170 } 171 172 void sclp_memory_setup(void) 173 { 174 uint64_t rnmax, rnsize; 175 int cc; 176 177 assert(read_info); 178 179 /* calculate the storage increment size */ 180 rnsize = read_info->rnsize; 181 if (!rnsize) { 182 rnsize = read_info->rnsize2; 183 } 184 storage_increment_size = rnsize << 20; 185 186 /* calculate the maximum memory size */ 187 rnmax = read_info->rnmax; 188 if (!rnmax) { 189 rnmax = read_info->rnmax2; 190 } 191 max_ram_size = rnmax * storage_increment_size; 192 193 /* lowcore is always accessible, so the first increment is accessible */ 194 ram_size = storage_increment_size; 195 196 /* probe for r/w memory up to max memory size */ 197 while (ram_size < max_ram_size) { 198 expect_pgm_int(); 199 cc = tprot(ram_size + storage_increment_size - 1); 200 /* stop once we receive an exception or have protected memory */ 201 if (clear_pgm_int() || cc != 0) 202 break; 203 ram_size += storage_increment_size; 204 } 205 206 mem_init(ram_size); 207 } 208 209 uint64_t get_ram_size(void) 210 { 211 return ram_size; 212 } 213 214 uint64_t get_max_ram_size(void) 215 { 216 return max_ram_size; 217 } 218