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