suspend.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
  * Copyright (C) 2010 Brian King IBM Corporation
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */

19
#include <linux/cpu.h>
20 21
#include <linux/delay.h>
#include <linux/suspend.h>
22
#include <linux/stat.h>
23 24 25 26 27
#include <asm/firmware.h>
#include <asm/hvcall.h>
#include <asm/machdep.h>
#include <asm/mmu.h>
#include <asm/rtas.h>
28
#include <asm/topology.h>
29
#include "../../kernel/cacheinfo.h"
30 31

static u64 stream_id;
32
static struct device suspend_dev;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
static DECLARE_COMPLETION(suspend_work);
static struct rtas_suspend_me_data suspend_data;
static atomic_t suspending;

/**
 * pseries_suspend_begin - First phase of hibernation
 *
 * Check to ensure we are in a valid state to hibernate
 *
 * Return value:
 * 	0 on success / other on failure
 **/
static int pseries_suspend_begin(suspend_state_t state)
{
	long vasi_state, rc;
	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];

	/* Make sure the state is valid */
	rc = plpar_hcall(H_VASI_STATE, retbuf, stream_id);

	vasi_state = retbuf[0];

	if (rc) {
		pr_err("pseries_suspend_begin: vasi_state returned %ld\n",rc);
		return rc;
	} else if (vasi_state == H_VASI_ENABLED) {
		return -EAGAIN;
	} else if (vasi_state != H_VASI_SUSPENDING) {
		pr_err("pseries_suspend_begin: vasi_state returned state %ld\n",
		       vasi_state);
		return -EIO;
	}

	return 0;
}

/**
 * pseries_suspend_cpu - Suspend a single CPU
 *
 * Makes the H_JOIN call to suspend the CPU
 *
 **/
static int pseries_suspend_cpu(void)
{
	if (atomic_read(&suspending))
		return rtas_suspend_cpu(&suspend_data);
	return 0;
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/**
 * pseries_suspend_enable_irqs
 *
 * Post suspend configuration updates
 *
 **/
static void pseries_suspend_enable_irqs(void)
{
	/*
	 * Update configuration which can be modified based on device tree
	 * changes during resume.
	 */
	cacheinfo_cpu_offline(smp_processor_id());
	post_mobility_fixup();
	cacheinfo_cpu_online(smp_processor_id());
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/**
 * pseries_suspend_enter - Final phase of hibernation
 *
 * Return value:
 * 	0 on success / other on failure
 **/
static int pseries_suspend_enter(suspend_state_t state)
{
	int rc = rtas_suspend_last_cpu(&suspend_data);

	atomic_set(&suspending, 0);
	atomic_set(&suspend_data.done, 1);
	return rc;
}

/**
 * pseries_prepare_late - Prepare to suspend all other CPUs
 *
 * Return value:
 * 	0 on success / other on failure
 **/
static int pseries_prepare_late(void)
{
	atomic_set(&suspending, 1);
	atomic_set(&suspend_data.working, 0);
	atomic_set(&suspend_data.done, 0);
	atomic_set(&suspend_data.error, 0);
	suspend_data.complete = &suspend_work;
127
	reinit_completion(&suspend_work);
128 129 130 131 132
	return 0;
}

/**
 * store_hibernate - Initiate partition hibernation
133 134
 * @dev:		subsys root device
 * @attr:		device attribute struct
135 136 137 138 139 140 141 142 143
 * @buf:		buffer
 * @count:		buffer size
 *
 * Write the stream ID received from the HMC to this file
 * to trigger hibernating the partition
 *
 * Return value:
 * 	number of bytes printed to buffer / other on failure
 **/
144 145
static ssize_t store_hibernate(struct device *dev,
			       struct device_attribute *attr,
146 147
			       const char *buf, size_t count)
{
148
	cpumask_var_t offline_mask;
149 150 151 152 153
	int rc;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

154
	if (!alloc_cpumask_var(&offline_mask, GFP_KERNEL))
155 156
		return -ENOMEM;

157 158 159 160 161 162 163 164
	stream_id = simple_strtoul(buf, NULL, 16);

	do {
		rc = pseries_suspend_begin(PM_SUSPEND_MEM);
		if (rc == -EAGAIN)
			ssleep(1);
	} while (rc == -EAGAIN);

165
	if (!rc) {
166 167 168 169 170 171 172 173 174 175
		/* All present CPUs must be online */
		cpumask_andnot(offline_mask, cpu_present_mask,
				cpu_online_mask);
		rc = rtas_online_cpus_mask(offline_mask);
		if (rc) {
			pr_err("%s: Could not bring present CPUs online.\n",
					__func__);
			goto out;
		}

176
		stop_topology_update();
177
		rc = pm_suspend(PM_SUSPEND_MEM);
178
		start_topology_update();
179 180 181 182 183

		/* Take down CPUs not online prior to suspend */
		if (!rtas_offline_cpus_mask(offline_mask))
			pr_warn("%s: Could not restore CPUs to offline "
					"state.\n", __func__);
184
	}
185 186 187 188 189

	stream_id = 0;

	if (!rc)
		rc = count;
190 191
out:
	free_cpumask_var(offline_mask);
192 193 194
	return rc;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#define USER_DT_UPDATE	0
#define KERN_DT_UPDATE	1

/**
 * show_hibernate - Report device tree update responsibilty
 * @dev:		subsys root device
 * @attr:		device attribute struct
 * @buf:		buffer
 *
 * Report whether a device tree update is performed by the kernel after a
 * resume, or if drmgr must coordinate the update from user space.
 *
 * Return value:
 *	0 if drmgr is to initiate update, and 1 otherwise
 **/
static ssize_t show_hibernate(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
	return sprintf(buf, "%d\n", KERN_DT_UPDATE);
}

217
static DEVICE_ATTR(hibernate, 0644, show_hibernate, store_hibernate);
218

219
static struct bus_type suspend_subsys = {
220
	.name = "power",
221
	.dev_name = "power",
222 223
};

224
static const struct platform_suspend_ops pseries_suspend_ops = {
225 226 227 228 229 230 231 232 233 234 235 236
	.valid		= suspend_valid_only_mem,
	.begin		= pseries_suspend_begin,
	.prepare_late	= pseries_prepare_late,
	.enter		= pseries_suspend_enter,
};

/**
 * pseries_suspend_sysfs_register - Register with sysfs
 *
 * Return value:
 * 	0 on success / other on failure
 **/
237
static int pseries_suspend_sysfs_register(struct device *dev)
238 239 240
{
	int rc;

241
	if ((rc = subsys_system_register(&suspend_subsys, NULL)))
242 243
		return rc;

244 245
	dev->id = 0;
	dev->bus = &suspend_subsys;
246

247 248
	if ((rc = device_create_file(suspend_subsys.dev_root, &dev_attr_hibernate)))
		goto subsys_unregister;
249 250 251

	return 0;

252 253
subsys_unregister:
	bus_unregister(&suspend_subsys);
254 255 256 257 258 259 260 261 262 263 264 265 266
	return rc;
}

/**
 * pseries_suspend_init - initcall for pSeries suspend
 *
 * Return value:
 * 	0 on success / other on failure
 **/
static int __init pseries_suspend_init(void)
{
	int rc;

267
	if (!firmware_has_feature(FW_FEATURE_LPAR))
268 269 270 271 272 273
		return 0;

	suspend_data.token = rtas_token("ibm,suspend-me");
	if (suspend_data.token == RTAS_UNKNOWN_SERVICE)
		return 0;

274
	if ((rc = pseries_suspend_sysfs_register(&suspend_dev)))
275 276 277
		return rc;

	ppc_md.suspend_disable_cpu = pseries_suspend_cpu;
278
	ppc_md.suspend_enable_irqs = pseries_suspend_enable_irqs;
279 280 281
	suspend_set_ops(&pseries_suspend_ops);
	return 0;
}
282
machine_device_initcall(pseries, pseries_suspend_init);
新手
引导
客服 返回
顶部