1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2007,2012
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
6 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
9 #define KMSG_COMPONENT "sclp_cmd"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/completion.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
20 #include <linux/mmzone.h>
21 #include <linux/memory.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <asm/ctl_reg.h>
25 #include <asm/chpid.h>
26 #include <asm/setup.h>
33 static void sclp_sync_callback(struct sclp_req
*req
, void *data
)
35 struct completion
*completion
= data
;
40 int sclp_sync_request(sclp_cmdw_t cmd
, void *sccb
)
42 return sclp_sync_request_timeout(cmd
, sccb
, 0);
45 int sclp_sync_request_timeout(sclp_cmdw_t cmd
, void *sccb
, int timeout
)
47 struct completion completion
;
48 struct sclp_req
*request
;
51 request
= kzalloc(sizeof(*request
), GFP_KERNEL
);
55 request
->queue_timeout
= timeout
;
56 request
->command
= cmd
;
58 request
->status
= SCLP_REQ_FILLED
;
59 request
->callback
= sclp_sync_callback
;
60 request
->callback_data
= &completion
;
61 init_completion(&completion
);
63 /* Perform sclp request. */
64 rc
= sclp_add_request(request
);
67 wait_for_completion(&completion
);
70 if (request
->status
!= SCLP_REQ_DONE
) {
71 pr_warn("sync request failed (cmd=0x%08x, status=0x%02x)\n",
72 cmd
, request
->status
);
81 * CPU configuration related functions.
84 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
85 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
87 int _sclp_get_core_info(struct sclp_core_info
*info
)
90 struct read_cpu_info_sccb
*sccb
;
92 if (!SCLP_HAS_CPU_INFO
)
94 sccb
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
97 sccb
->header
.length
= sizeof(*sccb
);
98 rc
= sclp_sync_request_timeout(SCLP_CMDW_READ_CPU_INFO
, sccb
,
102 if (sccb
->header
.response_code
!= 0x0010) {
103 pr_warn("readcpuinfo failed (response=0x%04x)\n",
104 sccb
->header
.response_code
);
108 sclp_fill_core_info(info
, sccb
);
110 free_page((unsigned long) sccb
);
114 struct cpu_configure_sccb
{
115 struct sccb_header header
;
116 } __attribute__((packed
, aligned(8)));
118 static int do_core_configure(sclp_cmdw_t cmd
)
120 struct cpu_configure_sccb
*sccb
;
123 if (!SCLP_HAS_CPU_RECONFIG
)
126 * This is not going to cross a page boundary since we force
127 * kmalloc to have a minimum alignment of 8 bytes on s390.
129 sccb
= kzalloc(sizeof(*sccb
), GFP_KERNEL
| GFP_DMA
);
132 sccb
->header
.length
= sizeof(*sccb
);
133 rc
= sclp_sync_request_timeout(cmd
, sccb
, SCLP_QUEUE_INTERVAL
);
136 switch (sccb
->header
.response_code
) {
141 pr_warn("configure cpu failed (cmd=0x%08x, response=0x%04x)\n",
142 cmd
, sccb
->header
.response_code
);
151 int sclp_core_configure(u8 core
)
153 return do_core_configure(SCLP_CMDW_CONFIGURE_CPU
| core
<< 8);
156 int sclp_core_deconfigure(u8 core
)
158 return do_core_configure(SCLP_CMDW_DECONFIGURE_CPU
| core
<< 8);
161 #ifdef CONFIG_MEMORY_HOTPLUG
163 static DEFINE_MUTEX(sclp_mem_mutex
);
164 static LIST_HEAD(sclp_mem_list
);
165 static u8 sclp_max_storage_id
;
166 static DECLARE_BITMAP(sclp_storage_ids
, 256);
167 static int sclp_mem_state_changed
;
169 struct memory_increment
{
170 struct list_head list
;
175 struct assign_storage_sccb
{
176 struct sccb_header header
;
180 int arch_get_memory_phys_device(unsigned long start_pfn
)
184 return PFN_PHYS(start_pfn
) >> ilog2(sclp
.rzm
);
187 static unsigned long long rn2addr(u16 rn
)
189 return (unsigned long long) (rn
- 1) * sclp
.rzm
;
192 static int do_assign_storage(sclp_cmdw_t cmd
, u16 rn
)
194 struct assign_storage_sccb
*sccb
;
197 sccb
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
200 sccb
->header
.length
= PAGE_SIZE
;
202 rc
= sclp_sync_request_timeout(cmd
, sccb
, SCLP_QUEUE_INTERVAL
);
205 switch (sccb
->header
.response_code
) {
210 pr_warn("assign storage failed (cmd=0x%08x, response=0x%04x, rn=0x%04x)\n",
211 cmd
, sccb
->header
.response_code
, rn
);
216 free_page((unsigned long) sccb
);
220 static int sclp_assign_storage(u16 rn
)
222 unsigned long long start
;
225 rc
= do_assign_storage(0x000d0001, rn
);
229 storage_key_init_range(start
, start
+ sclp
.rzm
);
233 static int sclp_unassign_storage(u16 rn
)
235 return do_assign_storage(0x000c0001, rn
);
238 struct attach_storage_sccb
{
239 struct sccb_header header
;
246 static int sclp_attach_storage(u8 id
)
248 struct attach_storage_sccb
*sccb
;
252 sccb
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
255 sccb
->header
.length
= PAGE_SIZE
;
256 sccb
->header
.function_code
= 0x40;
257 rc
= sclp_sync_request_timeout(0x00080001 | id
<< 8, sccb
,
258 SCLP_QUEUE_INTERVAL
);
261 switch (sccb
->header
.response_code
) {
263 set_bit(id
, sclp_storage_ids
);
264 for (i
= 0; i
< sccb
->assigned
; i
++) {
265 if (sccb
->entries
[i
])
266 sclp_unassign_storage(sccb
->entries
[i
] >> 16);
274 free_page((unsigned long) sccb
);
278 static int sclp_mem_change_state(unsigned long start
, unsigned long size
,
281 struct memory_increment
*incr
;
282 unsigned long long istart
;
285 list_for_each_entry(incr
, &sclp_mem_list
, list
) {
286 istart
= rn2addr(incr
->rn
);
287 if (start
+ size
- 1 < istart
)
289 if (start
> istart
+ sclp
.rzm
- 1)
292 rc
|= sclp_assign_storage(incr
->rn
);
294 sclp_unassign_storage(incr
->rn
);
296 incr
->standby
= online
? 0 : 1;
298 return rc
? -EIO
: 0;
301 static bool contains_standby_increment(unsigned long start
, unsigned long end
)
303 struct memory_increment
*incr
;
304 unsigned long istart
;
306 list_for_each_entry(incr
, &sclp_mem_list
, list
) {
307 istart
= rn2addr(incr
->rn
);
308 if (end
- 1 < istart
)
310 if (start
> istart
+ sclp
.rzm
- 1)
318 static int sclp_mem_notifier(struct notifier_block
*nb
,
319 unsigned long action
, void *data
)
321 unsigned long start
, size
;
322 struct memory_notify
*arg
;
327 start
= arg
->start_pfn
<< PAGE_SHIFT
;
328 size
= arg
->nr_pages
<< PAGE_SHIFT
;
329 mutex_lock(&sclp_mem_mutex
);
330 for_each_clear_bit(id
, sclp_storage_ids
, sclp_max_storage_id
+ 1)
331 sclp_attach_storage(id
);
333 case MEM_GOING_OFFLINE
:
335 * We do not allow to set memory blocks offline that contain
336 * standby memory. This is done to simplify the "memory online"
339 if (contains_standby_increment(start
, start
+ size
))
343 case MEM_CANCEL_OFFLINE
:
345 case MEM_GOING_ONLINE
:
346 rc
= sclp_mem_change_state(start
, size
, 1);
348 case MEM_CANCEL_ONLINE
:
349 sclp_mem_change_state(start
, size
, 0);
352 sclp_mem_change_state(start
, size
, 0);
359 sclp_mem_state_changed
= 1;
360 mutex_unlock(&sclp_mem_mutex
);
361 return rc
? NOTIFY_BAD
: NOTIFY_OK
;
364 static struct notifier_block sclp_mem_nb
= {
365 .notifier_call
= sclp_mem_notifier
,
368 static void __init
align_to_block_size(unsigned long long *start
,
369 unsigned long long *size
,
370 unsigned long long alignment
)
372 unsigned long long start_align
, size_align
;
374 start_align
= roundup(*start
, alignment
);
375 size_align
= rounddown(*start
+ *size
, alignment
) - start_align
;
377 pr_info("Standby memory at 0x%llx (%lluM of %lluM usable)\n",
378 *start
, size_align
>> 20, *size
>> 20);
379 *start
= start_align
;
383 static void __init
add_memory_merged(u16 rn
)
385 unsigned long long start
, size
, addr
, block_size
;
386 static u16 first_rn
, num
;
388 if (rn
&& first_rn
&& (first_rn
+ num
== rn
)) {
394 start
= rn2addr(first_rn
);
395 size
= (unsigned long long) num
* sclp
.rzm
;
396 if (start
>= VMEM_MAX_PHYS
)
398 if (start
+ size
> VMEM_MAX_PHYS
)
399 size
= VMEM_MAX_PHYS
- start
;
400 if (memory_end_set
&& (start
>= memory_end
))
402 if (memory_end_set
&& (start
+ size
> memory_end
))
403 size
= memory_end
- start
;
404 block_size
= memory_block_size_bytes();
405 align_to_block_size(&start
, &size
, block_size
);
408 for (addr
= start
; addr
< start
+ size
; addr
+= block_size
)
409 add_memory(numa_pfn_to_nid(PFN_DOWN(addr
)), addr
, block_size
);
415 static void __init
sclp_add_standby_memory(void)
417 struct memory_increment
*incr
;
419 list_for_each_entry(incr
, &sclp_mem_list
, list
)
421 add_memory_merged(incr
->rn
);
422 add_memory_merged(0);
425 static void __init
insert_increment(u16 rn
, int standby
, int assigned
)
427 struct memory_increment
*incr
, *new_incr
;
428 struct list_head
*prev
;
431 new_incr
= kzalloc(sizeof(*new_incr
), GFP_KERNEL
);
435 new_incr
->standby
= standby
;
437 prev
= &sclp_mem_list
;
438 list_for_each_entry(incr
, &sclp_mem_list
, list
) {
439 if (assigned
&& incr
->rn
> rn
)
441 if (!assigned
&& incr
->rn
- last_rn
> 1)
447 new_incr
->rn
= last_rn
+ 1;
448 if (new_incr
->rn
> sclp
.rnmax
) {
452 list_add(&new_incr
->list
, prev
);
455 static int sclp_mem_freeze(struct device
*dev
)
457 if (!sclp_mem_state_changed
)
459 pr_err("Memory hotplug state changed, suspend refused.\n");
463 struct read_storage_sccb
{
464 struct sccb_header header
;
472 static const struct dev_pm_ops sclp_mem_pm_ops
= {
473 .freeze
= sclp_mem_freeze
,
476 static struct platform_driver sclp_mem_pdrv
= {
479 .pm
= &sclp_mem_pm_ops
,
483 static int __init
sclp_detect_standby_memory(void)
485 struct platform_device
*sclp_pdev
;
486 struct read_storage_sccb
*sccb
;
487 int i
, id
, assigned
, rc
;
489 if (OLDMEM_BASE
) /* No standby memory in kdump mode */
491 if ((sclp
.facilities
& 0xe00000000000ULL
) != 0xe00000000000ULL
)
494 sccb
= (void *) __get_free_page(GFP_KERNEL
| GFP_DMA
);
498 for (id
= 0; id
<= sclp_max_storage_id
; id
++) {
499 memset(sccb
, 0, PAGE_SIZE
);
500 sccb
->header
.length
= PAGE_SIZE
;
501 rc
= sclp_sync_request(0x00040001 | id
<< 8, sccb
);
504 switch (sccb
->header
.response_code
) {
506 set_bit(id
, sclp_storage_ids
);
507 for (i
= 0; i
< sccb
->assigned
; i
++) {
508 if (!sccb
->entries
[i
])
511 insert_increment(sccb
->entries
[i
] >> 16, 0, 1);
517 for (i
= 0; i
< sccb
->assigned
; i
++) {
518 if (!sccb
->entries
[i
])
521 insert_increment(sccb
->entries
[i
] >> 16, 1, 1);
529 sclp_max_storage_id
= sccb
->max_id
;
531 if (rc
|| list_empty(&sclp_mem_list
))
533 for (i
= 1; i
<= sclp
.rnmax
- assigned
; i
++)
534 insert_increment(0, 1, 0);
535 rc
= register_memory_notifier(&sclp_mem_nb
);
538 rc
= platform_driver_register(&sclp_mem_pdrv
);
541 sclp_pdev
= platform_device_register_simple("sclp_mem", -1, NULL
, 0);
542 rc
= PTR_ERR_OR_ZERO(sclp_pdev
);
545 sclp_add_standby_memory();
548 platform_driver_unregister(&sclp_mem_pdrv
);
550 free_page((unsigned long) sccb
);
553 __initcall(sclp_detect_standby_memory
);
555 #endif /* CONFIG_MEMORY_HOTPLUG */
558 * Channel path configuration related functions.
561 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
562 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
563 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
565 struct chp_cfg_sccb
{
566 struct sccb_header header
;
570 } __attribute__((packed
));
572 static int do_chp_configure(sclp_cmdw_t cmd
)
574 struct chp_cfg_sccb
*sccb
;
577 if (!SCLP_HAS_CHP_RECONFIG
)
580 sccb
= (struct chp_cfg_sccb
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
583 sccb
->header
.length
= sizeof(*sccb
);
584 rc
= sclp_sync_request(cmd
, sccb
);
587 switch (sccb
->header
.response_code
) {
594 pr_warn("configure channel-path failed (cmd=0x%08x, response=0x%04x)\n",
595 cmd
, sccb
->header
.response_code
);
600 free_page((unsigned long) sccb
);
605 * sclp_chp_configure - perform configure channel-path sclp command
606 * @chpid: channel-path ID
608 * Perform configure channel-path command sclp command for specified chpid.
609 * Return 0 after command successfully finished, non-zero otherwise.
611 int sclp_chp_configure(struct chp_id chpid
)
613 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH
| chpid
.id
<< 8);
617 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
618 * @chpid: channel-path ID
620 * Perform deconfigure channel-path command sclp command for specified chpid
621 * and wait for completion. On success return 0. Return non-zero otherwise.
623 int sclp_chp_deconfigure(struct chp_id chpid
)
625 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH
| chpid
.id
<< 8);
628 struct chp_info_sccb
{
629 struct sccb_header header
;
630 u8 recognized
[SCLP_CHP_INFO_MASK_SIZE
];
631 u8 standby
[SCLP_CHP_INFO_MASK_SIZE
];
632 u8 configured
[SCLP_CHP_INFO_MASK_SIZE
];
636 } __attribute__((packed
));
639 * sclp_chp_read_info - perform read channel-path information sclp command
640 * @info: resulting channel-path information data
642 * Perform read channel-path information sclp command and wait for completion.
643 * On success, store channel-path information in @info and return 0. Return
644 * non-zero otherwise.
646 int sclp_chp_read_info(struct sclp_chp_info
*info
)
648 struct chp_info_sccb
*sccb
;
651 if (!SCLP_HAS_CHP_INFO
)
654 sccb
= (struct chp_info_sccb
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
657 sccb
->header
.length
= sizeof(*sccb
);
658 rc
= sclp_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION
, sccb
);
661 if (sccb
->header
.response_code
!= 0x0010) {
662 pr_warn("read channel-path info failed (response=0x%04x)\n",
663 sccb
->header
.response_code
);
667 memcpy(info
->recognized
, sccb
->recognized
, SCLP_CHP_INFO_MASK_SIZE
);
668 memcpy(info
->standby
, sccb
->standby
, SCLP_CHP_INFO_MASK_SIZE
);
669 memcpy(info
->configured
, sccb
->configured
, SCLP_CHP_INFO_MASK_SIZE
);
671 free_page((unsigned long) sccb
);