David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 1 | /* sstate.c: System soft state support. |
| 2 | * |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/notifier.h> |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 8 | #include <linux/reboot.h> |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 9 | #include <linux/init.h> |
| 10 | |
| 11 | #include <asm/hypervisor.h> |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 12 | #include <asm/spitfire.h> |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 13 | #include <asm/oplib.h> |
| 14 | #include <asm/head.h> |
| 15 | #include <asm/io.h> |
| 16 | |
David S. Miller | ea5e744 | 2011-08-01 23:27:17 -0700 | [diff] [blame] | 17 | #include "kernel.h" |
| 18 | |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 19 | static int hv_supports_soft_state; |
| 20 | |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 21 | static void do_set_sstate(unsigned long state, const char *msg) |
| 22 | { |
| 23 | unsigned long err; |
| 24 | |
| 25 | if (!hv_supports_soft_state) |
| 26 | return; |
| 27 | |
| 28 | err = sun4v_mach_set_soft_state(state, kimage_addr_to_ra(msg)); |
| 29 | if (err) { |
| 30 | printk(KERN_WARNING "SSTATE: Failed to set soft-state to " |
| 31 | "state[%lx] msg[%s], err=%lu\n", |
| 32 | state, msg, err); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static const char booting_msg[32] __attribute__((aligned(32))) = |
| 37 | "Linux booting"; |
| 38 | static const char running_msg[32] __attribute__((aligned(32))) = |
| 39 | "Linux running"; |
| 40 | static const char halting_msg[32] __attribute__((aligned(32))) = |
| 41 | "Linux halting"; |
| 42 | static const char poweroff_msg[32] __attribute__((aligned(32))) = |
| 43 | "Linux powering off"; |
| 44 | static const char rebooting_msg[32] __attribute__((aligned(32))) = |
| 45 | "Linux rebooting"; |
Tom Hromatka | 5d0e770 | 2017-01-10 10:57:56 -0700 | [diff] [blame] | 46 | static const char panicking_msg[32] __attribute__((aligned(32))) = |
| 47 | "Linux panicking"; |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 48 | |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 49 | static int sstate_reboot_call(struct notifier_block *np, unsigned long type, void *_unused) |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 50 | { |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 51 | const char *msg; |
| 52 | |
| 53 | switch (type) { |
| 54 | case SYS_DOWN: |
| 55 | default: |
| 56 | msg = rebooting_msg; |
| 57 | break; |
| 58 | |
| 59 | case SYS_HALT: |
| 60 | msg = halting_msg; |
| 61 | break; |
| 62 | |
| 63 | case SYS_POWER_OFF: |
| 64 | msg = poweroff_msg; |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | do_set_sstate(HV_SOFT_STATE_TRANSITION, msg); |
| 69 | |
| 70 | return NOTIFY_OK; |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 71 | } |
| 72 | |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 73 | static struct notifier_block sstate_reboot_notifier = { |
| 74 | .notifier_call = sstate_reboot_call, |
| 75 | }; |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 76 | |
| 77 | static int sstate_panic_event(struct notifier_block *n, unsigned long event, void *ptr) |
| 78 | { |
Tom Hromatka | 5d0e770 | 2017-01-10 10:57:56 -0700 | [diff] [blame] | 79 | do_set_sstate(HV_SOFT_STATE_TRANSITION, panicking_msg); |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 80 | |
| 81 | return NOTIFY_DONE; |
| 82 | } |
| 83 | |
| 84 | static struct notifier_block sstate_panic_block = { |
| 85 | .notifier_call = sstate_panic_event, |
| 86 | .priority = INT_MAX, |
| 87 | }; |
| 88 | |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 89 | static int __init sstate_init(void) |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 90 | { |
| 91 | unsigned long major, minor; |
| 92 | |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 93 | if (tlb_type != hypervisor) |
| 94 | return 0; |
| 95 | |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 96 | major = 1; |
| 97 | minor = 0; |
| 98 | if (sun4v_hvapi_register(HV_GRP_SOFT_STATE, major, &minor)) |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 99 | return 0; |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 100 | |
| 101 | hv_supports_soft_state = 1; |
| 102 | |
| 103 | prom_sun4v_guest_soft_state(); |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 104 | |
| 105 | do_set_sstate(HV_SOFT_STATE_TRANSITION, booting_msg); |
| 106 | |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 107 | atomic_notifier_chain_register(&panic_notifier_list, |
| 108 | &sstate_panic_block); |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 109 | register_reboot_notifier(&sstate_reboot_notifier); |
| 110 | |
| 111 | return 0; |
David S. Miller | 22d6a1c | 2007-05-25 00:37:12 -0700 | [diff] [blame] | 112 | } |
David S. Miller | 446139a | 2008-09-02 00:49:38 -0700 | [diff] [blame] | 113 | |
| 114 | core_initcall(sstate_init); |
| 115 | |
| 116 | static int __init sstate_running(void) |
| 117 | { |
| 118 | do_set_sstate(HV_SOFT_STATE_NORMAL, running_msg); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | late_initcall(sstate_running); |