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/chpid.h>
24 #include <asm/setup.h>
25 #include <asm/ctl_reg.h>
29 #define SCLP_CMDW_READ_SCP_INFO 0x00020001
30 #define SCLP_CMDW_READ_SCP_INFO_FORCED 0x00120001
32 struct read_info_sccb {
33 struct sccb_header header; /* 0-7 */
36 u8 _reserved0[24 - 11]; /* 11-15 */
37 u8 loadparm[8]; /* 24-31 */
38 u8 _reserved1[48 - 32]; /* 32-47 */
39 u64 facilities; /* 48-55 */
40 u8 _reserved2[84 - 56]; /* 56-83 */
43 u8 _reserved3[91 - 86]; /* 86-90 */
45 u8 _reserved4[100 - 92]; /* 92-99 */
46 u32 rnsize2; /* 100-103 */
47 u64 rnmax2; /* 104-111 */
48 u8 _reserved5[4096 - 112]; /* 112-4095 */
49 } __attribute__((packed, aligned(PAGE_SIZE)));
51 static struct init_sccb __initdata early_event_mask_sccb __aligned(PAGE_SIZE);
52 static struct read_info_sccb __initdata early_read_info_sccb;
53 static int __initdata early_read_info_sccb_valid;
58 static unsigned long long rzm;
59 static unsigned long long rnmax;
61 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
66 rc = sclp_service_call(cmd, sccb);
69 __load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA |
70 PSW_MASK_BA | PSW_MASK_EXT | PSW_MASK_WAIT);
73 /* Contents of the sccb might have changed. */
75 __ctl_clear_bit(0, 9);
79 static void __init sclp_read_info_early(void)
83 struct read_info_sccb *sccb;
84 sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
85 SCLP_CMDW_READ_SCP_INFO};
87 sccb = &early_read_info_sccb;
88 for (i = 0; i < ARRAY_SIZE(commands); i++) {
90 memset(sccb, 0, sizeof(*sccb));
91 sccb->header.length = sizeof(*sccb);
92 sccb->header.function_code = 0x80;
93 sccb->header.control_mask[2] = 0x80;
94 rc = sclp_cmd_sync_early(commands[i], sccb);
95 } while (rc == -EBUSY);
99 if (sccb->header.response_code == 0x10) {
100 early_read_info_sccb_valid = 1;
103 if (sccb->header.response_code != 0x1f0)
108 static void __init sclp_event_mask_early(void)
110 struct init_sccb *sccb = &early_event_mask_sccb;
114 memset(sccb, 0, sizeof(*sccb));
115 sccb->header.length = sizeof(*sccb);
116 sccb->mask_length = sizeof(sccb_mask_t);
117 rc = sclp_cmd_sync_early(SCLP_CMDW_WRITE_EVENT_MASK, sccb);
118 } while (rc == -EBUSY);
121 void __init sclp_facilities_detect(void)
123 struct read_info_sccb *sccb;
125 sclp_read_info_early();
126 if (!early_read_info_sccb_valid)
129 sccb = &early_read_info_sccb;
130 sclp_facilities = sccb->facilities;
131 sclp_fac84 = sccb->fac84;
132 sclp_fac85 = sccb->fac85;
133 rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
134 rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
137 sclp_event_mask_early();
140 bool __init sclp_has_linemode(void)
142 struct init_sccb *sccb = &early_event_mask_sccb;
144 if (sccb->header.response_code != 0x20)
146 if (sccb->sclp_send_mask & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK))
151 bool __init sclp_has_vt220(void)
153 struct init_sccb *sccb = &early_event_mask_sccb;
155 if (sccb->header.response_code != 0x20)
157 if (sccb->sclp_send_mask & EVTYP_VT220MSG_MASK)
162 unsigned long long sclp_get_rnmax(void)
167 unsigned long long sclp_get_rzm(void)
172 u8 sclp_get_fac85(void)
176 EXPORT_SYMBOL_GPL(sclp_get_fac85);
179 * This function will be called after sclp_facilities_detect(), which gets
180 * called from early.c code. Therefore the sccb should have valid contents.
182 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
184 struct read_info_sccb *sccb;
186 if (!early_read_info_sccb_valid)
188 sccb = &early_read_info_sccb;
190 if (sccb->flags & 0x2)
192 memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
195 static void sclp_sync_callback(struct sclp_req *req, void *data)
197 struct completion *completion = data;
199 complete(completion);
202 static int do_sync_request(sclp_cmdw_t cmd, void *sccb)
204 struct completion completion;
205 struct sclp_req *request;
208 request = kzalloc(sizeof(*request), GFP_KERNEL);
211 request->command = cmd;
212 request->sccb = sccb;
213 request->status = SCLP_REQ_FILLED;
214 request->callback = sclp_sync_callback;
215 request->callback_data = &completion;
216 init_completion(&completion);
218 /* Perform sclp request. */
219 rc = sclp_add_request(request);
222 wait_for_completion(&completion);
224 /* Check response. */
225 if (request->status != SCLP_REQ_DONE) {
226 pr_warning("sync request failed (cmd=0x%08x, "
227 "status=0x%02x)\n", cmd, request->status);
236 * CPU configuration related functions.
239 #define SCLP_CMDW_READ_CPU_INFO 0x00010001
240 #define SCLP_CMDW_CONFIGURE_CPU 0x00110001
241 #define SCLP_CMDW_DECONFIGURE_CPU 0x00100001
243 struct read_cpu_info_sccb {
244 struct sccb_header header;
246 u16 offset_configured;
249 u8 reserved[4096 - 16];
250 } __attribute__((packed, aligned(PAGE_SIZE)));
252 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
253 struct read_cpu_info_sccb *sccb)
255 char *page = (char *) sccb;
257 memset(info, 0, sizeof(*info));
258 info->configured = sccb->nr_configured;
259 info->standby = sccb->nr_standby;
260 info->combined = sccb->nr_configured + sccb->nr_standby;
261 info->has_cpu_type = sclp_fac84 & 0x1;
262 memcpy(&info->cpu, page + sccb->offset_configured,
263 info->combined * sizeof(struct sclp_cpu_entry));
266 int sclp_get_cpu_info(struct sclp_cpu_info *info)
269 struct read_cpu_info_sccb *sccb;
271 if (!SCLP_HAS_CPU_INFO)
273 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
276 sccb->header.length = sizeof(*sccb);
277 rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
280 if (sccb->header.response_code != 0x0010) {
281 pr_warning("readcpuinfo failed (response=0x%04x)\n",
282 sccb->header.response_code);
286 sclp_fill_cpu_info(info, sccb);
288 free_page((unsigned long) sccb);
292 struct cpu_configure_sccb {
293 struct sccb_header header;
294 } __attribute__((packed, aligned(8)));
296 static int do_cpu_configure(sclp_cmdw_t cmd)
298 struct cpu_configure_sccb *sccb;
301 if (!SCLP_HAS_CPU_RECONFIG)
304 * This is not going to cross a page boundary since we force
305 * kmalloc to have a minimum alignment of 8 bytes on s390.
307 sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
310 sccb->header.length = sizeof(*sccb);
311 rc = do_sync_request(cmd, sccb);
314 switch (sccb->header.response_code) {
319 pr_warning("configure cpu failed (cmd=0x%08x, "
320 "response=0x%04x)\n", cmd,
321 sccb->header.response_code);
330 int sclp_cpu_configure(u8 cpu)
332 return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
335 int sclp_cpu_deconfigure(u8 cpu)
337 return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
340 #ifdef CONFIG_MEMORY_HOTPLUG
342 static DEFINE_MUTEX(sclp_mem_mutex);
343 static LIST_HEAD(sclp_mem_list);
344 static u8 sclp_max_storage_id;
345 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
346 static int sclp_mem_state_changed;
348 struct memory_increment {
349 struct list_head list;
355 struct assign_storage_sccb {
356 struct sccb_header header;
360 int arch_get_memory_phys_device(unsigned long start_pfn)
364 return PFN_PHYS(start_pfn) >> ilog2(rzm);
367 static unsigned long long rn2addr(u16 rn)
369 return (unsigned long long) (rn - 1) * rzm;
372 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
374 struct assign_storage_sccb *sccb;
377 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
380 sccb->header.length = PAGE_SIZE;
382 rc = do_sync_request(cmd, sccb);
385 switch (sccb->header.response_code) {
390 pr_warning("assign storage failed (cmd=0x%08x, "
391 "response=0x%04x, rn=0x%04x)\n", cmd,
392 sccb->header.response_code, rn);
397 free_page((unsigned long) sccb);
401 static int sclp_assign_storage(u16 rn)
403 unsigned long long start, address;
406 rc = do_assign_storage(0x000d0001, rn);
409 start = address = rn2addr(rn);
410 for (; address < start + rzm; address += PAGE_SIZE)
411 page_set_storage_key(address, PAGE_DEFAULT_KEY, 0);
416 static int sclp_unassign_storage(u16 rn)
418 return do_assign_storage(0x000c0001, rn);
421 struct attach_storage_sccb {
422 struct sccb_header header;
429 static int sclp_attach_storage(u8 id)
431 struct attach_storage_sccb *sccb;
435 sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
438 sccb->header.length = PAGE_SIZE;
439 rc = do_sync_request(0x00080001 | id << 8, sccb);
442 switch (sccb->header.response_code) {
444 set_bit(id, sclp_storage_ids);
445 for (i = 0; i < sccb->assigned; i++) {
446 if (sccb->entries[i])
447 sclp_unassign_storage(sccb->entries[i] >> 16);
455 free_page((unsigned long) sccb);
459 static int sclp_mem_change_state(unsigned long start, unsigned long size,
462 struct memory_increment *incr;
463 unsigned long long istart;
466 list_for_each_entry(incr, &sclp_mem_list, list) {
467 istart = rn2addr(incr->rn);
468 if (start + size - 1 < istart)
470 if (start > istart + rzm - 1)
473 if (incr->usecount++)
476 * Don't break the loop if one assign fails. Loop may
477 * be walked again on CANCEL and we can't save
478 * information if state changed before or not.
479 * So continue and increase usecount for all increments.
481 rc |= sclp_assign_storage(incr->rn);
483 if (--incr->usecount)
485 sclp_unassign_storage(incr->rn);
488 return rc ? -EIO : 0;
491 static int sclp_mem_notifier(struct notifier_block *nb,
492 unsigned long action, void *data)
494 unsigned long start, size;
495 struct memory_notify *arg;
500 start = arg->start_pfn << PAGE_SHIFT;
501 size = arg->nr_pages << PAGE_SHIFT;
502 mutex_lock(&sclp_mem_mutex);
503 for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
504 sclp_attach_storage(id);
507 case MEM_GOING_OFFLINE:
508 case MEM_CANCEL_OFFLINE:
510 case MEM_GOING_ONLINE:
511 rc = sclp_mem_change_state(start, size, 1);
513 case MEM_CANCEL_ONLINE:
514 sclp_mem_change_state(start, size, 0);
517 sclp_mem_change_state(start, size, 0);
524 sclp_mem_state_changed = 1;
525 mutex_unlock(&sclp_mem_mutex);
526 return rc ? NOTIFY_BAD : NOTIFY_OK;
529 static struct notifier_block sclp_mem_nb = {
530 .notifier_call = sclp_mem_notifier,
533 static void __init add_memory_merged(u16 rn)
535 static u16 first_rn, num;
536 unsigned long long start, size;
538 if (rn && first_rn && (first_rn + num == rn)) {
544 start = rn2addr(first_rn);
545 size = (unsigned long long ) num * rzm;
546 if (start >= VMEM_MAX_PHYS)
548 if (start + size > VMEM_MAX_PHYS)
549 size = VMEM_MAX_PHYS - start;
550 if (memory_end_set && (start >= memory_end))
552 if (memory_end_set && (start + size > memory_end))
553 size = memory_end - start;
554 add_memory(0, start, size);
560 static void __init sclp_add_standby_memory(void)
562 struct memory_increment *incr;
564 list_for_each_entry(incr, &sclp_mem_list, list)
566 add_memory_merged(incr->rn);
567 add_memory_merged(0);
570 static void __init insert_increment(u16 rn, int standby, int assigned)
572 struct memory_increment *incr, *new_incr;
573 struct list_head *prev;
576 new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
580 new_incr->standby = standby;
582 new_incr->usecount = 1;
584 prev = &sclp_mem_list;
585 list_for_each_entry(incr, &sclp_mem_list, list) {
586 if (assigned && incr->rn > rn)
588 if (!assigned && incr->rn - last_rn > 1)
594 new_incr->rn = last_rn + 1;
595 if (new_incr->rn > rnmax) {
599 list_add(&new_incr->list, prev);
602 static int sclp_mem_freeze(struct device *dev)
604 if (!sclp_mem_state_changed)
606 pr_err("Memory hotplug state changed, suspend refused.\n");
610 struct read_storage_sccb {
611 struct sccb_header header;
619 static const struct dev_pm_ops sclp_mem_pm_ops = {
620 .freeze = sclp_mem_freeze,
623 static struct platform_driver sclp_mem_pdrv = {
626 .pm = &sclp_mem_pm_ops,
630 static int __init sclp_detect_standby_memory(void)
632 struct platform_device *sclp_pdev;
633 struct read_storage_sccb *sccb;
634 int i, id, assigned, rc;
636 if (!early_read_info_sccb_valid)
638 if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
641 sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
645 for (id = 0; id <= sclp_max_storage_id; id++) {
646 memset(sccb, 0, PAGE_SIZE);
647 sccb->header.length = PAGE_SIZE;
648 rc = do_sync_request(0x00040001 | id << 8, sccb);
651 switch (sccb->header.response_code) {
653 set_bit(id, sclp_storage_ids);
654 for (i = 0; i < sccb->assigned; i++) {
655 if (!sccb->entries[i])
658 insert_increment(sccb->entries[i] >> 16, 0, 1);
664 for (i = 0; i < sccb->assigned; i++) {
665 if (!sccb->entries[i])
668 insert_increment(sccb->entries[i] >> 16, 1, 1);
676 sclp_max_storage_id = sccb->max_id;
678 if (rc || list_empty(&sclp_mem_list))
680 for (i = 1; i <= rnmax - assigned; i++)
681 insert_increment(0, 1, 0);
682 rc = register_memory_notifier(&sclp_mem_nb);
685 rc = platform_driver_register(&sclp_mem_pdrv);
688 sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
689 rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
692 sclp_add_standby_memory();
695 platform_driver_unregister(&sclp_mem_pdrv);
697 free_page((unsigned long) sccb);
700 __initcall(sclp_detect_standby_memory);
702 #endif /* CONFIG_MEMORY_HOTPLUG */
705 * Channel path configuration related functions.
708 #define SCLP_CMDW_CONFIGURE_CHPATH 0x000f0001
709 #define SCLP_CMDW_DECONFIGURE_CHPATH 0x000e0001
710 #define SCLP_CMDW_READ_CHPATH_INFORMATION 0x00030001
712 struct chp_cfg_sccb {
713 struct sccb_header header;
717 } __attribute__((packed));
719 static int do_chp_configure(sclp_cmdw_t cmd)
721 struct chp_cfg_sccb *sccb;
724 if (!SCLP_HAS_CHP_RECONFIG)
727 sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
730 sccb->header.length = sizeof(*sccb);
731 rc = do_sync_request(cmd, sccb);
734 switch (sccb->header.response_code) {
741 pr_warning("configure channel-path failed "
742 "(cmd=0x%08x, response=0x%04x)\n", cmd,
743 sccb->header.response_code);
748 free_page((unsigned long) sccb);
753 * sclp_chp_configure - perform configure channel-path sclp command
754 * @chpid: channel-path ID
756 * Perform configure channel-path command sclp command for specified chpid.
757 * Return 0 after command successfully finished, non-zero otherwise.
759 int sclp_chp_configure(struct chp_id chpid)
761 return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
765 * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
766 * @chpid: channel-path ID
768 * Perform deconfigure channel-path command sclp command for specified chpid
769 * and wait for completion. On success return 0. Return non-zero otherwise.
771 int sclp_chp_deconfigure(struct chp_id chpid)
773 return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
776 struct chp_info_sccb {
777 struct sccb_header header;
778 u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
779 u8 standby[SCLP_CHP_INFO_MASK_SIZE];
780 u8 configured[SCLP_CHP_INFO_MASK_SIZE];
784 } __attribute__((packed));
787 * sclp_chp_read_info - perform read channel-path information sclp command
788 * @info: resulting channel-path information data
790 * Perform read channel-path information sclp command and wait for completion.
791 * On success, store channel-path information in @info and return 0. Return
792 * non-zero otherwise.
794 int sclp_chp_read_info(struct sclp_chp_info *info)
796 struct chp_info_sccb *sccb;
799 if (!SCLP_HAS_CHP_INFO)
802 sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
805 sccb->header.length = sizeof(*sccb);
806 rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
809 if (sccb->header.response_code != 0x0010) {
810 pr_warning("read channel-path info failed "
811 "(response=0x%04x)\n", sccb->header.response_code);
815 memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
816 memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
817 memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
819 free_page((unsigned long) sccb);