2 * Copyright IBM Corp. 2007, 2009
4 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
5 * 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/slab.h>
16 #include <linux/string.h>
18 #include <linux/mmzone.h>
19 #include <linux/memory.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <asm/ctl_reg.h>
23 #include <asm/chpid.h>
24 #include <asm/setup.h>
30 #define SCLP_CMDW_READ_SCP_INFO 0x00020001
31 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
33 struct read_info_sccb {
34 struct sccb_header header; /* 0-7 */
37 u8 _reserved0[24 - 11]; /* 11-15 */
38 u8 loadparm[8]; /* 24-31 */
39 u8 _reserved1[48 - 32]; /* 32-47 */
40 u64 facilities; /* 48-55 */
41 u8 _reserved2[84 - 56]; /* 56-83 */
44 u8 _reserved3[91 - 86]; /* 86-90 */
46 u8 _reserved4[100 - 92]; /* 92-99 */
47 u32 rnsize2; /* 100-103 */
48 u64 rnmax2; /* 104-111 */
49 u8 _reserved5[4096 - 112]; /* 112-4095 */
50 } __attribute__((packed, aligned(PAGE_SIZE)));
52 static struct init_sccb __initdata early_event_mask_sccb __aligned(PAGE_SIZE);
53 static struct read_info_sccb __initdata early_read_info_sccb;
54 static int __initdata early_read_info_sccb_valid;
59 static unsigned long long rzm;
60 static unsigned long long rnmax;
62 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
67 rc = sclp_service_call(cmd, sccb);
70 __load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA |
71 PSW_MASK_BA | PSW_MASK_EXT | PSW_MASK_WAIT);
74 /* Contents of the sccb might have changed. */
76 __ctl_clear_bit(0, 9);
80 static void __init sclp_read_info_early(void)
84 struct read_info_sccb *sccb;
85 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
86 SCLP_CMDW_READ_SCP_INFO};
88 sccb = &early_read_info_sccb;
89 for (i = 0; i < ARRAY_SIZE(commands); i++) {
91 memset(sccb, 0, sizeof(*sccb));
92 sccb->header.length = sizeof(*sccb);
93 sccb->header.function_code = 0x80;
94 sccb->header.control_mask[2] = 0x80;
95 rc = sclp_cmd_sync_early(commands[i], sccb);
96 } while (rc == -EBUSY);
100 if (sccb->header.response_code == 0x10) {
101 early_read_info_sccb_valid = 1;
104 if (sccb->header.response_code != 0x1f0)
109 static void __init sclp_event_mask_early(void)
111 struct init_sccb *sccb = &early_event_mask_sccb;
115 memset(sccb, 0, sizeof(*sccb));
116 sccb->header.length = sizeof(*sccb);
117 sccb->mask_length = sizeof(sccb_mask_t);
118 rc = sclp_cmd_sync_early(SCLP_CMDW_WRITE_EVENT_MASK, sccb);
119 } while (rc == -EBUSY);
122 void __init sclp_facilities_detect(void)
124 struct read_info_sccb *sccb;
126 sclp_read_info_early();
127 if (!early_read_info_sccb_valid)
130 sccb = &early_read_info_sccb;
131 sclp_facilities = sccb->facilities;
132 sclp_fac84 = sccb->fac84;
133 sclp_fac85 = sccb->fac85;
134 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
135 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
138 sclp_event_mask_early();
141 bool __init sclp_has_linemode(void)
143 struct init_sccb *sccb = &early_event_mask_sccb;
145 if (sccb->header.response_code != 0x20)
147 if (sccb->sclp_send_mask & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK))
152 bool __init sclp_has_vt220(void)
154 struct init_sccb *sccb = &early_event_mask_sccb;
156 if (sccb->header.response_code != 0x20)
158 if (sccb->sclp_send_mask & EVTYP_VT220MSG_MASK)
163 unsigned long long sclp_get_rnmax(void)
168 unsigned long long sclp_get_rzm(void)
173 u8 sclp_get_fac85(void)
177 EXPORT_SYMBOL_GPL(sclp_get_fac85);
180 * This function will be called after sclp_facilities_detect(), which gets
181 * called from early.c code. Therefore the sccb should have valid contents.
183 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
185 struct read_info_sccb *sccb;
187 if (!early_read_info_sccb_valid)
189 sccb = &early_read_info_sccb;
191 if (sccb->flags & 0x2)
193 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
196 static void sclp_sync_callback(struct sclp_req *req, void *data)
198 struct completion *completion = data;
200 complete(completion);
203 static int do_sync_request(sclp_cmdw_t cmd, void *sccb)
205 struct completion completion;
206 struct sclp_req *request;
209 request = kzalloc(sizeof(*request), GFP_KERNEL);
212 request->command = cmd;
213 request->sccb = sccb;
214 request->status = SCLP_REQ_FILLED;
215 request->callback = sclp_sync_callback;
216 request->callback_data = &completion;
217 init_completion(&completion);
219 /* Perform sclp request. */
220 rc = sclp_add_request(request);
223 wait_for_completion(&completion);
225 /* Check response. */
226 if (request->status != SCLP_REQ_DONE) {
227 pr_warning("sync request failed (cmd=0x%08x, "
228 "status=0x%02x)\n", cmd, request->status);
237 * CPU configuration related functions.
240 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
241 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
242 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
244 struct read_cpu_info_sccb {
245 struct sccb_header header;
247 u16 offset_configured;
250 u8 reserved[4096 - 16];
251 } __attribute__((packed, aligned(PAGE_SIZE)));
253 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
254 struct read_cpu_info_sccb *sccb)
256 char *page = (char *) sccb;
258 memset(info, 0, sizeof(*info));
259 info->configured = sccb->nr_configured;
260 info->standby = sccb->nr_standby;
261 info->combined = sccb->nr_configured + sccb->nr_standby;
262 info->has_cpu_type = sclp_fac84 & 0x1;
263 memcpy(&info->cpu, page + sccb->offset_configured,
264 info->combined * sizeof(struct sclp_cpu_entry));
267 int sclp_get_cpu_info(struct sclp_cpu_info *info)
270 struct read_cpu_info_sccb *sccb;
272 if (!SCLP_HAS_CPU_INFO)
274 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
277 sccb->header.length = sizeof(*sccb);
278 rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
281 if (sccb->header.response_code != 0x0010) {
282 pr_warning("readcpuinfo failed (response=0x%04x)\n",
283 sccb->header.response_code);
287 sclp_fill_cpu_info(info, sccb);
289 free_page((unsigned long) sccb);
293 struct cpu_configure_sccb {
294 struct sccb_header header;
295 } __attribute__((packed, aligned(8)));
297 static int do_cpu_configure(sclp_cmdw_t cmd)
299 struct cpu_configure_sccb *sccb;
302 if (!SCLP_HAS_CPU_RECONFIG)
305 * This is not going to cross a page boundary since we force
306 * kmalloc to have a minimum alignment of 8 bytes on s390.
308 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
311 sccb->header.length = sizeof(*sccb);
312 rc = do_sync_request(cmd, sccb);
315 switch (sccb->header.response_code) {
320 pr_warning("configure cpu failed (cmd=0x%08x, "
321 "response=0x%04x)\n", cmd,
322 sccb->header.response_code);
331 int sclp_cpu_configure(u8 cpu)
333 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
336 int sclp_cpu_deconfigure(u8 cpu)
338 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
341 #ifdef CONFIG_MEMORY_HOTPLUG
343 static DEFINE_MUTEX(sclp_mem_mutex);
344 static LIST_HEAD(sclp_mem_list);
345 static u8 sclp_max_storage_id;
346 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
347 static int sclp_mem_state_changed;
349 struct memory_increment {
350 struct list_head list;
356 struct assign_storage_sccb {
357 struct sccb_header header;
361 int arch_get_memory_phys_device(unsigned long start_pfn)
365 return PFN_PHYS(start_pfn) >> ilog2(rzm);
368 static unsigned long long rn2addr(u16 rn)
370 return (unsigned long long) (rn - 1) * rzm;
373 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
375 struct assign_storage_sccb *sccb;
378 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
381 sccb->header.length = PAGE_SIZE;
383 rc = do_sync_request(cmd, sccb);
386 switch (sccb->header.response_code) {
391 pr_warning("assign storage failed (cmd=0x%08x, "
392 "response=0x%04x, rn=0x%04x)\n", cmd,
393 sccb->header.response_code, rn);
398 free_page((unsigned long) sccb);
402 static int sclp_assign_storage(u16 rn)
404 unsigned long long start;
407 rc = do_assign_storage(0x000d0001, rn);
411 storage_key_init_range(start, start + rzm);
415 static int sclp_unassign_storage(u16 rn)
417 return do_assign_storage(0x000c0001, rn);
420 struct attach_storage_sccb {
421 struct sccb_header header;
428 static int sclp_attach_storage(u8 id)
430 struct attach_storage_sccb *sccb;
434 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
437 sccb->header.length = PAGE_SIZE;
438 rc = do_sync_request(0x00080001 | id << 8, sccb);
441 switch (sccb->header.response_code) {
443 set_bit(id, sclp_storage_ids);
444 for (i = 0; i < sccb->assigned; i++) {
445 if (sccb->entries[i])
446 sclp_unassign_storage(sccb->entries[i] >> 16);
454 free_page((unsigned long) sccb);
458 static int sclp_mem_change_state(unsigned long start, unsigned long size,
461 struct memory_increment *incr;
462 unsigned long long istart;
465 list_for_each_entry(incr, &sclp_mem_list, list) {
466 istart = rn2addr(incr->rn);
467 if (start + size - 1 < istart)
469 if (start > istart + rzm - 1)
472 if (incr->usecount++)
475 * Don't break the loop if one assign fails. Loop may
476 * be walked again on CANCEL and we can't save
477 * information if state changed before or not.
478 * So continue and increase usecount for all increments.
480 rc |= sclp_assign_storage(incr->rn);
482 if (--incr->usecount)
484 sclp_unassign_storage(incr->rn);
487 return rc ? -EIO : 0;
490 static int sclp_mem_notifier(struct notifier_block *nb,
491 unsigned long action, void *data)
493 unsigned long start, size;
494 struct memory_notify *arg;
499 start = arg->start_pfn << PAGE_SHIFT;
500 size = arg->nr_pages << PAGE_SHIFT;
501 mutex_lock(&sclp_mem_mutex);
502 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
503 sclp_attach_storage(id);
506 case MEM_GOING_OFFLINE:
507 case MEM_CANCEL_OFFLINE:
509 case MEM_GOING_ONLINE:
510 rc = sclp_mem_change_state(start, size, 1);
512 case MEM_CANCEL_ONLINE:
513 sclp_mem_change_state(start, size, 0);
516 sclp_mem_change_state(start, size, 0);
523 sclp_mem_state_changed = 1;
524 mutex_unlock(&sclp_mem_mutex);
525 return rc ? NOTIFY_BAD : NOTIFY_OK;
528 static struct notifier_block sclp_mem_nb = {
529 .notifier_call = sclp_mem_notifier,
532 static void __init add_memory_merged(u16 rn)
534 static u16 first_rn, num;
535 unsigned long long start, size;
537 if (rn && first_rn && (first_rn + num == rn)) {
543 start = rn2addr(first_rn);
544 size = (unsigned long long ) num * rzm;
545 if (start >= VMEM_MAX_PHYS)
547 if (start + size > VMEM_MAX_PHYS)
548 size = VMEM_MAX_PHYS - start;
549 if (memory_end_set && (start >= memory_end))
551 if (memory_end_set && (start + size > memory_end))
552 size = memory_end - start;
553 add_memory(0, start, size);
559 static void __init sclp_add_standby_memory(void)
561 struct memory_increment *incr;
563 list_for_each_entry(incr, &sclp_mem_list, list)
565 add_memory_merged(incr->rn);
566 add_memory_merged(0);
569 static void __init insert_increment(u16 rn, int standby, int assigned)
571 struct memory_increment *incr, *new_incr;
572 struct list_head *prev;
575 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
579 new_incr->standby = standby;
581 new_incr->usecount = 1;
583 prev = &sclp_mem_list;
584 list_for_each_entry(incr, &sclp_mem_list, list) {
585 if (assigned && incr->rn > rn)
587 if (!assigned && incr->rn - last_rn > 1)
593 new_incr->rn = last_rn + 1;
594 if (new_incr->rn > rnmax) {
598 list_add(&new_incr->list, prev);
601 static int sclp_mem_freeze(struct device *dev)
603 if (!sclp_mem_state_changed)
605 pr_err("Memory hotplug state changed, suspend refused.\n");
609 struct read_storage_sccb {
610 struct sccb_header header;
618 static const struct dev_pm_ops sclp_mem_pm_ops = {
619 .freeze = sclp_mem_freeze,
622 static struct platform_driver sclp_mem_pdrv = {
625 .pm = &sclp_mem_pm_ops,
629 static int __init sclp_detect_standby_memory(void)
631 struct platform_device *sclp_pdev;
632 struct read_storage_sccb *sccb;
633 int i, id, assigned, rc;
635 if (!early_read_info_sccb_valid)
637 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
640 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
644 for (id = 0; id <= sclp_max_storage_id; id++) {
645 memset(sccb, 0, PAGE_SIZE);
646 sccb->header.length = PAGE_SIZE;
647 rc = do_sync_request(0x00040001 | id << 8, sccb);
650 switch (sccb->header.response_code) {
652 set_bit(id, sclp_storage_ids);
653 for (i = 0; i < sccb->assigned; i++) {
654 if (!sccb->entries[i])
657 insert_increment(sccb->entries[i] >> 16, 0, 1);
663 for (i = 0; i < sccb->assigned; i++) {
664 if (!sccb->entries[i])
667 insert_increment(sccb->entries[i] >> 16, 1, 1);
675 sclp_max_storage_id = sccb->max_id;
677 if (rc || list_empty(&sclp_mem_list))
679 for (i = 1; i <= rnmax - assigned; i++)
680 insert_increment(0, 1, 0);
681 rc = register_memory_notifier(&sclp_mem_nb);
684 rc = platform_driver_register(&sclp_mem_pdrv);
687 sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
688 rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
691 sclp_add_standby_memory();
694 platform_driver_unregister(&sclp_mem_pdrv);
696 free_page((unsigned long) sccb);
699 __initcall(sclp_detect_standby_memory);
701 #endif /* CONFIG_MEMORY_HOTPLUG */
704 * Channel path configuration related functions.
707 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
708 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
709 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
711 struct chp_cfg_sccb {
712 struct sccb_header header;
716 } __attribute__((packed));
718 static int do_chp_configure(sclp_cmdw_t cmd)
720 struct chp_cfg_sccb *sccb;
723 if (!SCLP_HAS_CHP_RECONFIG)
726 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
729 sccb->header.length = sizeof(*sccb);
730 rc = do_sync_request(cmd, sccb);
733 switch (sccb->header.response_code) {
740 pr_warning("configure channel-path failed "
741 "(cmd=0x%08x, response=0x%04x)\n", cmd,
742 sccb->header.response_code);
747 free_page((unsigned long) sccb);
752 * sclp_chp_configure - perform configure channel-path sclp command
753 * @chpid: channel-path ID
755 * Perform configure channel-path command sclp command for specified chpid.
756 * Return 0 after command successfully finished, non-zero otherwise.
758 int sclp_chp_configure(struct chp_id chpid)
760 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
764 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
765 * @chpid: channel-path ID
767 * Perform deconfigure channel-path command sclp command for specified chpid
768 * and wait for completion. On success return 0. Return non-zero otherwise.
770 int sclp_chp_deconfigure(struct chp_id chpid)
772 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
775 struct chp_info_sccb {
776 struct sccb_header header;
777 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
778 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
779 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
783 } __attribute__((packed));
786 * sclp_chp_read_info - perform read channel-path information sclp command
787 * @info: resulting channel-path information data
789 * Perform read channel-path information sclp command and wait for completion.
790 * On success, store channel-path information in @info and return 0. Return
791 * non-zero otherwise.
793 int sclp_chp_read_info(struct sclp_chp_info *info)
795 struct chp_info_sccb *sccb;
798 if (!SCLP_HAS_CHP_INFO)
801 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
804 sccb->header.length = sizeof(*sccb);
805 rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
808 if (sccb->header.response_code != 0x0010) {
809 pr_warning("read channel-path info failed "
810 "(response=0x%04x)\n", sccb->header.response_code);
814 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
815 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
816 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
818 free_page((unsigned long) sccb);