1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2007,2012
5 * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
8 #define KMSG_COMPONENT "sclp_cmd"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/completion.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
19 #include <linux/mmzone.h>
20 #include <linux/memory.h>
21 #include <linux/module.h>
22 #include <asm/ctl_reg.h>
23 #include <asm/chpid.h>
24 #include <asm/setup.h>
28 #include <asm/facility.h>
32 static void sclp_sync_callback(struct sclp_req *req, void *data)
34 struct completion *completion = data;
39 int sclp_sync_request(sclp_cmdw_t cmd, void *sccb)
41 return sclp_sync_request_timeout(cmd, sccb, 0);
44 int sclp_sync_request_timeout(sclp_cmdw_t cmd, void *sccb, int timeout)
46 struct completion completion;
47 struct sclp_req *request;
50 request = kzalloc(sizeof(*request), GFP_KERNEL);
54 request->queue_timeout = timeout;
55 request->command = cmd;
57 request->status = SCLP_REQ_FILLED;
58 request->callback = sclp_sync_callback;
59 request->callback_data = &completion;
60 init_completion(&completion);
62 /* Perform sclp request. */
63 rc = sclp_add_request(request);
66 wait_for_completion(&completion);
69 if (request->status != SCLP_REQ_DONE) {
70 pr_warn("sync request failed (cmd=0x%08x, status=0x%02x)\n",
71 cmd, request->status);
80 * CPU configuration related functions.
83 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
84 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
86 int _sclp_get_core_info(struct sclp_core_info *info)
89 int length = test_facility(140) ? EXT_SCCB_READ_CPU : PAGE_SIZE;
90 struct read_cpu_info_sccb *sccb;
92 if (!SCLP_HAS_CPU_INFO)
95 sccb = (void *)__get_free_pages(GFP_KERNEL | GFP_DMA | __GFP_ZERO, get_order(length));
98 sccb->header.length = length;
99 sccb->header.control_mask[2] = 0x80;
100 rc = sclp_sync_request_timeout(SCLP_CMDW_READ_CPU_INFO, sccb,
101 SCLP_QUEUE_INTERVAL);
104 if (sccb->header.response_code != 0x0010) {
105 pr_warn("readcpuinfo failed (response=0x%04x)\n",
106 sccb->header.response_code);
110 sclp_fill_core_info(info, sccb);
112 free_pages((unsigned long) sccb, get_order(length));
116 struct cpu_configure_sccb {
117 struct sccb_header header;
118 } __attribute__((packed, aligned(8)));
120 static int do_core_configure(sclp_cmdw_t cmd)
122 struct cpu_configure_sccb *sccb;
125 if (!SCLP_HAS_CPU_RECONFIG)
128 * This is not going to cross a page boundary since we force
129 * kmalloc to have a minimum alignment of 8 bytes on s390.
131 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
134 sccb->header.length = sizeof(*sccb);
135 rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
138 switch (sccb->header.response_code) {
143 pr_warn("configure cpu failed (cmd=0x%08x, response=0x%04x)\n",
144 cmd, sccb->header.response_code);
153 int sclp_core_configure(u8 core)
155 return do_core_configure(SCLP_CMDW_CONFIGURE_CPU | core << 8);
158 int sclp_core_deconfigure(u8 core)
160 return do_core_configure(SCLP_CMDW_DECONFIGURE_CPU | core << 8);
163 #ifdef CONFIG_MEMORY_HOTPLUG
165 static DEFINE_MUTEX(sclp_mem_mutex);
166 static LIST_HEAD(sclp_mem_list);
167 static u8 sclp_max_storage_id;
168 static DECLARE_BITMAP(sclp_storage_ids, 256);
170 struct memory_increment {
171 struct list_head list;
176 struct assign_storage_sccb {
177 struct sccb_header header;
181 int arch_get_memory_phys_device(unsigned long start_pfn)
185 return PFN_PHYS(start_pfn) >> ilog2(sclp.rzm);
188 static unsigned long long rn2addr(u16 rn)
190 return (unsigned long long) (rn - 1) * sclp.rzm;
193 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
195 struct assign_storage_sccb *sccb;
198 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
201 sccb->header.length = PAGE_SIZE;
203 rc = sclp_sync_request_timeout(cmd, sccb, SCLP_QUEUE_INTERVAL);
206 switch (sccb->header.response_code) {
211 pr_warn("assign storage failed (cmd=0x%08x, response=0x%04x, rn=0x%04x)\n",
212 cmd, sccb->header.response_code, rn);
217 free_page((unsigned long) sccb);
221 static int sclp_assign_storage(u16 rn)
223 unsigned long long start;
226 rc = do_assign_storage(0x000d0001, rn);
230 storage_key_init_range(start, start + sclp.rzm);
234 static int sclp_unassign_storage(u16 rn)
236 return do_assign_storage(0x000c0001, rn);
239 struct attach_storage_sccb {
240 struct sccb_header header;
247 static int sclp_attach_storage(u8 id)
249 struct attach_storage_sccb *sccb;
253 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
256 sccb->header.length = PAGE_SIZE;
257 sccb->header.function_code = 0x40;
258 rc = sclp_sync_request_timeout(0x00080001 | id << 8, sccb,
259 SCLP_QUEUE_INTERVAL);
262 switch (sccb->header.response_code) {
264 set_bit(id, sclp_storage_ids);
265 for (i = 0; i < sccb->assigned; i++) {
266 if (sccb->entries[i])
267 sclp_unassign_storage(sccb->entries[i] >> 16);
275 free_page((unsigned long) sccb);
279 static int sclp_mem_change_state(unsigned long start, unsigned long size,
282 struct memory_increment *incr;
283 unsigned long long istart;
286 list_for_each_entry(incr, &sclp_mem_list, list) {
287 istart = rn2addr(incr->rn);
288 if (start + size - 1 < istart)
290 if (start > istart + sclp.rzm - 1)
293 rc |= sclp_assign_storage(incr->rn);
295 sclp_unassign_storage(incr->rn);
297 incr->standby = online ? 0 : 1;
299 return rc ? -EIO : 0;
302 static bool contains_standby_increment(unsigned long start, unsigned long end)
304 struct memory_increment *incr;
305 unsigned long istart;
307 list_for_each_entry(incr, &sclp_mem_list, list) {
308 istart = rn2addr(incr->rn);
309 if (end - 1 < istart)
311 if (start > istart + sclp.rzm - 1)
319 static int sclp_mem_notifier(struct notifier_block *nb,
320 unsigned long action, void *data)
322 unsigned long start, size;
323 struct memory_notify *arg;
328 start = arg->start_pfn << PAGE_SHIFT;
329 size = arg->nr_pages << PAGE_SHIFT;
330 mutex_lock(&sclp_mem_mutex);
331 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
332 sclp_attach_storage(id);
334 case MEM_GOING_OFFLINE:
336 * We do not allow to set memory blocks offline that contain
337 * standby memory. This is done to simplify the "memory online"
340 if (contains_standby_increment(start, start + size))
344 case MEM_CANCEL_OFFLINE:
346 case MEM_GOING_ONLINE:
347 rc = sclp_mem_change_state(start, size, 1);
349 case MEM_CANCEL_ONLINE:
350 sclp_mem_change_state(start, size, 0);
353 sclp_mem_change_state(start, size, 0);
359 mutex_unlock(&sclp_mem_mutex);
360 return rc ? NOTIFY_BAD : NOTIFY_OK;
363 static struct notifier_block sclp_mem_nb = {
364 .notifier_call = sclp_mem_notifier,
367 static void __init align_to_block_size(unsigned long long *start,
368 unsigned long long *size,
369 unsigned long long alignment)
371 unsigned long long start_align, size_align;
373 start_align = roundup(*start, alignment);
374 size_align = rounddown(*start + *size, alignment) - start_align;
376 pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n",
377 *start, size_align >> 20, *size >> 20);
378 *start = start_align;
382 static void __init add_memory_merged(u16 rn)
384 unsigned long long start, size, addr, block_size;
385 static u16 first_rn, num;
387 if (rn && first_rn && (first_rn + num == rn)) {
393 start = rn2addr(first_rn);
394 size = (unsigned long long) num * sclp.rzm;
395 if (start >= VMEM_MAX_PHYS)
397 if (start + size > VMEM_MAX_PHYS)
398 size = VMEM_MAX_PHYS - start;
399 if (start >= ident_map_size)
401 if (start + size > ident_map_size)
402 size = ident_map_size - start;
403 block_size = memory_block_size_bytes();
404 align_to_block_size(&start, &size, block_size);
407 for (addr = start; addr < start + size; addr += block_size)
408 add_memory(0, addr, block_size, MHP_NONE);
414 static void __init sclp_add_standby_memory(void)
416 struct memory_increment *incr;
418 list_for_each_entry(incr, &sclp_mem_list, list)
420 add_memory_merged(incr->rn);
421 add_memory_merged(0);
424 static void __init insert_increment(u16 rn, int standby, int assigned)
426 struct memory_increment *incr, *new_incr;
427 struct list_head *prev;
430 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
434 new_incr->standby = standby;
436 prev = &sclp_mem_list;
437 list_for_each_entry(incr, &sclp_mem_list, list) {
438 if (assigned && incr->rn > rn)
440 if (!assigned && incr->rn - last_rn > 1)
446 new_incr->rn = last_rn + 1;
447 if (new_incr->rn > sclp.rnmax) {
451 list_add(&new_incr->list, prev);
454 static int __init sclp_detect_standby_memory(void)
456 struct read_storage_sccb *sccb;
457 int i, id, assigned, rc;
459 if (oldmem_data.start) /* No standby memory in kdump mode */
461 if ((sclp.facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
464 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
468 for (id = 0; id <= sclp_max_storage_id; id++) {
469 memset(sccb, 0, PAGE_SIZE);
470 sccb->header.length = PAGE_SIZE;
471 rc = sclp_sync_request(SCLP_CMDW_READ_STORAGE_INFO | id << 8, sccb);
474 switch (sccb->header.response_code) {
476 set_bit(id, sclp_storage_ids);
477 for (i = 0; i < sccb->assigned; i++) {
478 if (!sccb->entries[i])
481 insert_increment(sccb->entries[i] >> 16, 0, 1);
487 for (i = 0; i < sccb->assigned; i++) {
488 if (!sccb->entries[i])
491 insert_increment(sccb->entries[i] >> 16, 1, 1);
499 sclp_max_storage_id = sccb->max_id;
501 if (rc || list_empty(&sclp_mem_list))
503 for (i = 1; i <= sclp.rnmax - assigned; i++)
504 insert_increment(0, 1, 0);
505 rc = register_memory_notifier(&sclp_mem_nb);
508 sclp_add_standby_memory();
510 free_page((unsigned long) sccb);
513 __initcall(sclp_detect_standby_memory);
515 #endif /* CONFIG_MEMORY_HOTPLUG */
518 * Channel path configuration related functions.
521 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
522 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
523 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
525 struct chp_cfg_sccb {
526 struct sccb_header header;
530 } __attribute__((packed));
532 static int do_chp_configure(sclp_cmdw_t cmd)
534 struct chp_cfg_sccb *sccb;
537 if (!SCLP_HAS_CHP_RECONFIG)
540 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
543 sccb->header.length = sizeof(*sccb);
544 rc = sclp_sync_request(cmd, sccb);
547 switch (sccb->header.response_code) {
554 pr_warn("configure channel-path failed (cmd=0x%08x, response=0x%04x)\n",
555 cmd, sccb->header.response_code);
560 free_page((unsigned long) sccb);
565 * sclp_chp_configure - perform configure channel-path sclp command
566 * @chpid: channel-path ID
568 * Perform configure channel-path command sclp command for specified chpid.
569 * Return 0 after command successfully finished, non-zero otherwise.
571 int sclp_chp_configure(struct chp_id chpid)
573 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
577 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
578 * @chpid: channel-path ID
580 * Perform deconfigure channel-path command sclp command for specified chpid
581 * and wait for completion. On success return 0. Return non-zero otherwise.
583 int sclp_chp_deconfigure(struct chp_id chpid)
585 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
588 struct chp_info_sccb {
589 struct sccb_header header;
590 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
591 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
592 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
596 } __attribute__((packed));
599 * sclp_chp_read_info - perform read channel-path information sclp command
600 * @info: resulting channel-path information data
602 * Perform read channel-path information sclp command and wait for completion.
603 * On success, store channel-path information in @info and return 0. Return
604 * non-zero otherwise.
606 int sclp_chp_read_info(struct sclp_chp_info *info)
608 struct chp_info_sccb *sccb;
611 if (!SCLP_HAS_CHP_INFO)
614 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
617 sccb->header.length = sizeof(*sccb);
618 rc = sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
621 if (sccb->header.response_code != 0x0010) {
622 pr_warn("read channel-path info failed (response=0x%04x)\n",
623 sccb->header.response_code);
627 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
628 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
629 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
631 free_page((unsigned long) sccb);