xref: /kvm-unit-tests/lib/s390x/sclp.c (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
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, CTL0_SERVICE_SIGNAL);
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, CTL0_SERVICE_SIGNAL);
63 	sclp_clear_busy();
64 }
65 
66 void sclp_wait_busy(void)
67 {
68 	while (sclp_busy)
69 		mb();
70 }
71 
72 void sclp_mark_busy(void)
73 {
74 	/*
75 	 * With multiple CPUs we might need to wait for another CPU's
76 	 * request before grabbing the busy indication.
77 	 */
78 	while (true) {
79 		sclp_wait_busy();
80 		spin_lock(&sclp_lock);
81 		if (!sclp_busy) {
82 			sclp_busy = true;
83 			spin_unlock(&sclp_lock);
84 			return;
85 		}
86 		spin_unlock(&sclp_lock);
87 	}
88 }
89 
90 void sclp_clear_busy(void)
91 {
92 	spin_lock(&sclp_lock);
93 	sclp_busy = false;
94 	spin_unlock(&sclp_lock);
95 }
96 
97 static void sclp_read_scp_info(ReadInfo *ri, int length)
98 {
99 	unsigned int commands[] = { SCLP_CMDW_READ_SCP_INFO_FORCED,
100 				    SCLP_CMDW_READ_SCP_INFO };
101 	int i, cc;
102 
103 	for (i = 0; i < ARRAY_SIZE(commands); i++) {
104 		sclp_mark_busy();
105 		memset(&ri->h, 0, sizeof(ri->h));
106 		ri->h.length = length;
107 
108 		cc = sclp_service_call(commands[i], ri);
109 		if (cc)
110 			break;
111 		if (ri->h.response_code == SCLP_RC_NORMAL_READ_COMPLETION)
112 			return;
113 		if (ri->h.response_code != SCLP_RC_INVALID_SCLP_COMMAND)
114 			break;
115 	}
116 	report_abort("READ_SCP_INFO failed");
117 }
118 
119 void sclp_read_info(void)
120 {
121 	sclp_read_scp_info((void *)_read_info, SCCB_SIZE);
122 	read_info = (ReadInfo *)_read_info;
123 }
124 
125 int sclp_get_cpu_num(void)
126 {
127 	assert(read_info);
128 	return read_info->entries_cpu;
129 }
130 
131 CPUEntry *sclp_get_cpu_entries(void)
132 {
133 	assert(read_info);
134 	return (CPUEntry *)(_read_info + read_info->offset_cpu);
135 }
136 
137 static bool sclp_feat_check(int byte, int bit)
138 {
139 	uint8_t *rib = (uint8_t *)read_info;
140 
141 	return !!(rib[byte] & (0x80 >> bit));
142 }
143 
144 void sclp_facilities_setup(void)
145 {
146 	unsigned short cpu0_addr = stap();
147 	CPUEntry *cpu;
148 	int i;
149 
150 	assert(read_info);
151 
152 	cpu = sclp_get_cpu_entries();
153 	if (read_info->offset_cpu > 134)
154 		sclp_facilities.has_diag318 = read_info->byte_134_diag318;
155 	sclp_facilities.has_gsls = sclp_feat_check(85, SCLP_FEAT_85_BIT_GSLS);
156 	sclp_facilities.has_kss = sclp_feat_check(98, SCLP_FEAT_98_BIT_KSS);
157 	sclp_facilities.has_cmma = sclp_feat_check(116, SCLP_FEAT_116_BIT_CMMA);
158 	sclp_facilities.has_64bscao = sclp_feat_check(116, SCLP_FEAT_116_BIT_64BSCAO);
159 	sclp_facilities.has_esca = sclp_feat_check(116, SCLP_FEAT_116_BIT_ESCA);
160 	sclp_facilities.has_ibs = sclp_feat_check(117, SCLP_FEAT_117_BIT_IBS);
161 	sclp_facilities.has_pfmfi = sclp_feat_check(117, SCLP_FEAT_117_BIT_PFMFI);
162 
163 	for (i = 0; i < read_info->entries_cpu; i++, cpu++) {
164 		/*
165 		 * The logic for only reading the facilities from the
166 		 * boot cpu comes from the kernel. I haven't yet found
167 		 * documentation that explains why this is necessary
168 		 * but I figure there's a reason behind doing it this
169 		 * way.
170 		 */
171 		if (cpu->address == cpu0_addr) {
172 			sclp_facilities.has_sief2 = cpu->feat_sief2;
173 			sclp_facilities.has_skeyi = cpu->feat_skeyi;
174 			sclp_facilities.has_siif = cpu->feat_siif;
175 			sclp_facilities.has_sigpif = cpu->feat_sigpif;
176 			sclp_facilities.has_ib = cpu->feat_ib;
177 			sclp_facilities.has_cei = cpu->feat_cei;
178 			break;
179 		}
180 	}
181 }
182 
183 /* Perform service call. Return 0 on success, non-zero otherwise. */
184 int sclp_service_call(unsigned int command, void *sccb)
185 {
186 	int cc;
187 
188 	sclp_setup_int();
189 	cc = servc(command, __pa(sccb));
190 	sclp_wait_busy();
191 	if (cc == 3)
192 		return -1;
193 	if (cc == 2)
194 		return -1;
195 	return 0;
196 }
197 
198 void sclp_memory_setup(void)
199 {
200 	uint64_t rnmax, rnsize;
201 	int cc;
202 
203 	assert(read_info);
204 
205 	/* calculate the storage increment size */
206 	rnsize = read_info->rnsize;
207 	if (!rnsize) {
208 		rnsize = read_info->rnsize2;
209 	}
210 	storage_increment_size = rnsize << 20;
211 
212 	/* calculate the maximum memory size */
213 	rnmax = read_info->rnmax;
214 	if (!rnmax) {
215 		rnmax = read_info->rnmax2;
216 	}
217 	max_ram_size = rnmax * storage_increment_size;
218 
219 	/* lowcore is always accessible, so the first increment is accessible */
220 	ram_size = storage_increment_size;
221 
222 	/* probe for r/w memory up to max memory size */
223 	while (ram_size < max_ram_size) {
224 		expect_pgm_int();
225 		cc = tprot(ram_size + storage_increment_size - 1, 0);
226 		/* stop once we receive an exception or have protected memory */
227 		if (clear_pgm_int() || cc != 0)
228 			break;
229 		ram_size += storage_increment_size;
230 	}
231 
232 	mem_init(ram_size);
233 }
234 
235 uint64_t get_ram_size(void)
236 {
237 	return ram_size;
238 }
239 
240 uint64_t get_max_ram_size(void)
241 {
242 	return max_ram_size;
243 }
244