1e2186023SMichael Ellerman /*
2e2186023SMichael Ellerman  * Copyright 2013, Michael (Ellerman|Neuling), IBM Corporation.
3e2186023SMichael Ellerman  *
4e2186023SMichael Ellerman  * This program is free software; you can redistribute it and/or
5e2186023SMichael Ellerman  * modify it under the terms of the GNU General Public License
6e2186023SMichael Ellerman  * as published by the Free Software Foundation; either version
7e2186023SMichael Ellerman  * 2 of the License, or (at your option) any later version.
8e2186023SMichael Ellerman  */
9e2186023SMichael Ellerman 
10e2186023SMichael Ellerman #define pr_fmt(fmt)	"powernv: " fmt
11e2186023SMichael Ellerman 
12e2186023SMichael Ellerman #include <linux/kernel.h>
13e2186023SMichael Ellerman #include <linux/cpu.h>
14e2186023SMichael Ellerman #include <linux/cpumask.h>
15e2186023SMichael Ellerman #include <linux/device.h>
16e2186023SMichael Ellerman #include <linux/gfp.h>
17e2186023SMichael Ellerman #include <linux/smp.h>
18e2186023SMichael Ellerman #include <linux/stop_machine.h>
19e2186023SMichael Ellerman 
20e2186023SMichael Ellerman #include <asm/cputhreads.h>
212201f994SNicholas Piggin #include <asm/cpuidle.h>
22e2186023SMichael Ellerman #include <asm/kvm_ppc.h>
23e2186023SMichael Ellerman #include <asm/machdep.h>
24e2186023SMichael Ellerman #include <asm/opal.h>
25e2186023SMichael Ellerman #include <asm/smp.h>
26e2186023SMichael Ellerman 
27e2186023SMichael Ellerman #include "subcore.h"
281217d34bSAnton Blanchard #include "powernv.h"
29e2186023SMichael Ellerman 
30e2186023SMichael Ellerman 
31e2186023SMichael Ellerman /*
32e2186023SMichael Ellerman  * Split/unsplit procedure:
33e2186023SMichael Ellerman  *
34e2186023SMichael Ellerman  * A core can be in one of three states, unsplit, 2-way split, and 4-way split.
35e2186023SMichael Ellerman  *
36e2186023SMichael Ellerman  * The mapping to subcores_per_core is simple:
37e2186023SMichael Ellerman  *
38e2186023SMichael Ellerman  *  State       | subcores_per_core
39e2186023SMichael Ellerman  *  ------------|------------------
40e2186023SMichael Ellerman  *  Unsplit     |        1
41e2186023SMichael Ellerman  *  2-way split |        2
42e2186023SMichael Ellerman  *  4-way split |        4
43e2186023SMichael Ellerman  *
44e2186023SMichael Ellerman  * The core is split along thread boundaries, the mapping between subcores and
45e2186023SMichael Ellerman  * threads is as follows:
46e2186023SMichael Ellerman  *
47e2186023SMichael Ellerman  *  Unsplit:
48e2186023SMichael Ellerman  *          ----------------------------
49e2186023SMichael Ellerman  *  Subcore |            0             |
50e2186023SMichael Ellerman  *          ----------------------------
51e2186023SMichael Ellerman  *  Thread  |  0  1  2  3  4  5  6  7  |
52e2186023SMichael Ellerman  *          ----------------------------
53e2186023SMichael Ellerman  *
54e2186023SMichael Ellerman  *  2-way split:
55e2186023SMichael Ellerman  *          -------------------------------------
56e2186023SMichael Ellerman  *  Subcore |        0        |        1        |
57e2186023SMichael Ellerman  *          -------------------------------------
58e2186023SMichael Ellerman  *  Thread  |  0   1   2   3  |  4   5   6   7  |
59e2186023SMichael Ellerman  *          -------------------------------------
60e2186023SMichael Ellerman  *
61e2186023SMichael Ellerman  *  4-way split:
62e2186023SMichael Ellerman  *          -----------------------------------------
63e2186023SMichael Ellerman  *  Subcore |    0    |    1    |    2    |    3    |
64e2186023SMichael Ellerman  *          -----------------------------------------
65e2186023SMichael Ellerman  *  Thread  |  0   1  |  2   3  |  4   5  |  6   7  |
66e2186023SMichael Ellerman  *          -----------------------------------------
67e2186023SMichael Ellerman  *
68e2186023SMichael Ellerman  *
69e2186023SMichael Ellerman  * Transitions
70e2186023SMichael Ellerman  * -----------
71e2186023SMichael Ellerman  *
72e2186023SMichael Ellerman  * It is not possible to transition between either of the split states, the
73e2186023SMichael Ellerman  * core must first be unsplit. The legal transitions are:
74e2186023SMichael Ellerman  *
75e2186023SMichael Ellerman  *  -----------          ---------------
76e2186023SMichael Ellerman  *  |         |  <---->  | 2-way split |
77e2186023SMichael Ellerman  *  |         |          ---------------
78e2186023SMichael Ellerman  *  | Unsplit |
79e2186023SMichael Ellerman  *  |         |          ---------------
80e2186023SMichael Ellerman  *  |         |  <---->  | 4-way split |
81e2186023SMichael Ellerman  *  -----------          ---------------
82e2186023SMichael Ellerman  *
83e2186023SMichael Ellerman  * Unsplitting
84e2186023SMichael Ellerman  * -----------
85e2186023SMichael Ellerman  *
86e2186023SMichael Ellerman  * Unsplitting is the simpler procedure. It requires thread 0 to request the
87e2186023SMichael Ellerman  * unsplit while all other threads NAP.
88e2186023SMichael Ellerman  *
89e2186023SMichael Ellerman  * Thread 0 clears HID0_POWER8_DYNLPARDIS (Dynamic LPAR Disable). This tells
90e2186023SMichael Ellerman  * the hardware that if all threads except 0 are napping, the hardware should
91e2186023SMichael Ellerman  * unsplit the core.
92e2186023SMichael Ellerman  *
93e2186023SMichael Ellerman  * Non-zero threads are sent to a NAP loop, they don't exit the loop until they
94e2186023SMichael Ellerman  * see the core unsplit.
95e2186023SMichael Ellerman  *
96e2186023SMichael Ellerman  * Core 0 spins waiting for the hardware to see all the other threads napping
97e2186023SMichael Ellerman  * and perform the unsplit.
98e2186023SMichael Ellerman  *
99e2186023SMichael Ellerman  * Once thread 0 sees the unsplit, it IPIs the secondary threads to wake them
100e2186023SMichael Ellerman  * out of NAP. They will then see the core unsplit and exit the NAP loop.
101e2186023SMichael Ellerman  *
102e2186023SMichael Ellerman  * Splitting
103e2186023SMichael Ellerman  * ---------
104e2186023SMichael Ellerman  *
105e2186023SMichael Ellerman  * The basic splitting procedure is fairly straight forward. However it is
106e2186023SMichael Ellerman  * complicated by the fact that after the split occurs, the newly created
107e2186023SMichael Ellerman  * subcores are not in a fully initialised state.
108e2186023SMichael Ellerman  *
109e2186023SMichael Ellerman  * Most notably the subcores do not have the correct value for SDR1, which
110e2186023SMichael Ellerman  * means they must not be running in virtual mode when the split occurs. The
111e2186023SMichael Ellerman  * subcores have separate timebases SPRs but these are pre-synchronised by
112e2186023SMichael Ellerman  * opal.
113e2186023SMichael Ellerman  *
114e2186023SMichael Ellerman  * To begin with secondary threads are sent to an assembly routine. There they
115e2186023SMichael Ellerman  * switch to real mode, so they are immune to the uninitialised SDR1 value.
116e2186023SMichael Ellerman  * Once in real mode they indicate that they are in real mode, and spin waiting
117e2186023SMichael Ellerman  * to see the core split.
118e2186023SMichael Ellerman  *
119e2186023SMichael Ellerman  * Thread 0 waits to see that all secondaries are in real mode, and then begins
120e2186023SMichael Ellerman  * the splitting procedure. It firstly sets HID0_POWER8_DYNLPARDIS, which
121e2186023SMichael Ellerman  * prevents the hardware from unsplitting. Then it sets the appropriate HID bit
122e2186023SMichael Ellerman  * to request the split, and spins waiting to see that the split has happened.
123e2186023SMichael Ellerman  *
124e2186023SMichael Ellerman  * Concurrently the secondaries will notice the split. When they do they set up
125e2186023SMichael Ellerman  * their SPRs, notably SDR1, and then they can return to virtual mode and exit
126e2186023SMichael Ellerman  * the procedure.
127e2186023SMichael Ellerman  */
128e2186023SMichael Ellerman 
129e2186023SMichael Ellerman /* Initialised at boot by subcore_init() */
130e2186023SMichael Ellerman static int subcores_per_core;
131e2186023SMichael Ellerman 
132e2186023SMichael Ellerman /*
133e2186023SMichael Ellerman  * Used to communicate to offline cpus that we want them to pop out of the
134e2186023SMichael Ellerman  * offline loop and do a split or unsplit.
135e2186023SMichael Ellerman  *
136e2186023SMichael Ellerman  * 0 - no split happening
137e2186023SMichael Ellerman  * 1 - unsplit in progress
138e2186023SMichael Ellerman  * 2 - split to 2 in progress
139e2186023SMichael Ellerman  * 4 - split to 4 in progress
140e2186023SMichael Ellerman  */
141e2186023SMichael Ellerman static int new_split_mode;
142e2186023SMichael Ellerman 
143e2186023SMichael Ellerman static cpumask_var_t cpu_offline_mask;
144e2186023SMichael Ellerman 
145e2186023SMichael Ellerman struct split_state {
146e2186023SMichael Ellerman 	u8 step;
147e2186023SMichael Ellerman 	u8 master;
148e2186023SMichael Ellerman };
149e2186023SMichael Ellerman 
150e2186023SMichael Ellerman static DEFINE_PER_CPU(struct split_state, split_state);
151e2186023SMichael Ellerman 
152e2186023SMichael Ellerman static void wait_for_sync_step(int step)
153e2186023SMichael Ellerman {
154e2186023SMichael Ellerman 	int i, cpu = smp_processor_id();
155e2186023SMichael Ellerman 
156e2186023SMichael Ellerman 	for (i = cpu + 1; i < cpu + threads_per_core; i++)
157e2186023SMichael Ellerman 		while(per_cpu(split_state, i).step < step)
158e2186023SMichael Ellerman 			barrier();
159e2186023SMichael Ellerman 
160e2186023SMichael Ellerman 	/* Order the wait loop vs any subsequent loads/stores. */
161e2186023SMichael Ellerman 	mb();
162e2186023SMichael Ellerman }
163e2186023SMichael Ellerman 
16477b54e9fSShreyas B. Prabhu static void update_hid_in_slw(u64 hid0)
16577b54e9fSShreyas B. Prabhu {
16677b54e9fSShreyas B. Prabhu 	u64 idle_states = pnv_get_supported_cpuidle_states();
16777b54e9fSShreyas B. Prabhu 
16877b54e9fSShreyas B. Prabhu 	if (idle_states & OPAL_PM_WINKLE_ENABLED) {
16977b54e9fSShreyas B. Prabhu 		/* OPAL call to patch slw with the new HID0 value */
17077b54e9fSShreyas B. Prabhu 		u64 cpu_pir = hard_smp_processor_id();
17177b54e9fSShreyas B. Prabhu 
17277b54e9fSShreyas B. Prabhu 		opal_slw_set_reg(cpu_pir, SPRN_HID0, hid0);
17377b54e9fSShreyas B. Prabhu 	}
17477b54e9fSShreyas B. Prabhu }
17577b54e9fSShreyas B. Prabhu 
176e2186023SMichael Ellerman static void unsplit_core(void)
177e2186023SMichael Ellerman {
178e2186023SMichael Ellerman 	u64 hid0, mask;
179e2186023SMichael Ellerman 	int i, cpu;
180e2186023SMichael Ellerman 
181e2186023SMichael Ellerman 	mask = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
182e2186023SMichael Ellerman 
183e2186023SMichael Ellerman 	cpu = smp_processor_id();
184e2186023SMichael Ellerman 	if (cpu_thread_in_core(cpu) != 0) {
185e2186023SMichael Ellerman 		while (mfspr(SPRN_HID0) & mask)
1862201f994SNicholas Piggin 			power7_idle_insn(PNV_THREAD_NAP);
187e2186023SMichael Ellerman 
188e2186023SMichael Ellerman 		per_cpu(split_state, cpu).step = SYNC_STEP_UNSPLIT;
189e2186023SMichael Ellerman 		return;
190e2186023SMichael Ellerman 	}
191e2186023SMichael Ellerman 
192e2186023SMichael Ellerman 	hid0 = mfspr(SPRN_HID0);
193e2186023SMichael Ellerman 	hid0 &= ~HID0_POWER8_DYNLPARDIS;
194e63dbd16SGautham R. Shenoy 	update_power8_hid0(hid0);
19577b54e9fSShreyas B. Prabhu 	update_hid_in_slw(hid0);
196e2186023SMichael Ellerman 
197e2186023SMichael Ellerman 	while (mfspr(SPRN_HID0) & mask)
198e2186023SMichael Ellerman 		cpu_relax();
199e2186023SMichael Ellerman 
200e2186023SMichael Ellerman 	/* Wake secondaries out of NAP */
201e2186023SMichael Ellerman 	for (i = cpu + 1; i < cpu + threads_per_core; i++)
202e2186023SMichael Ellerman 		smp_send_reschedule(i);
203e2186023SMichael Ellerman 
204e2186023SMichael Ellerman 	wait_for_sync_step(SYNC_STEP_UNSPLIT);
205e2186023SMichael Ellerman }
206e2186023SMichael Ellerman 
207e2186023SMichael Ellerman static void split_core(int new_mode)
208e2186023SMichael Ellerman {
209e2186023SMichael Ellerman 	struct {  u64 value; u64 mask; } split_parms[2] = {
210e2186023SMichael Ellerman 		{ HID0_POWER8_1TO2LPAR, HID0_POWER8_2LPARMODE },
211e2186023SMichael Ellerman 		{ HID0_POWER8_1TO4LPAR, HID0_POWER8_4LPARMODE }
212e2186023SMichael Ellerman 	};
213e2186023SMichael Ellerman 	int i, cpu;
214e2186023SMichael Ellerman 	u64 hid0;
215e2186023SMichael Ellerman 
216e2186023SMichael Ellerman 	/* Convert new_mode (2 or 4) into an index into our parms array */
217e2186023SMichael Ellerman 	i = (new_mode >> 1) - 1;
218e2186023SMichael Ellerman 	BUG_ON(i < 0 || i > 1);
219e2186023SMichael Ellerman 
220e2186023SMichael Ellerman 	cpu = smp_processor_id();
221e2186023SMichael Ellerman 	if (cpu_thread_in_core(cpu) != 0) {
222e2186023SMichael Ellerman 		split_core_secondary_loop(&per_cpu(split_state, cpu).step);
223e2186023SMichael Ellerman 		return;
224e2186023SMichael Ellerman 	}
225e2186023SMichael Ellerman 
226e2186023SMichael Ellerman 	wait_for_sync_step(SYNC_STEP_REAL_MODE);
227e2186023SMichael Ellerman 
228e2186023SMichael Ellerman 	/* Write new mode */
229e2186023SMichael Ellerman 	hid0  = mfspr(SPRN_HID0);
230e2186023SMichael Ellerman 	hid0 |= HID0_POWER8_DYNLPARDIS | split_parms[i].value;
231e63dbd16SGautham R. Shenoy 	update_power8_hid0(hid0);
23277b54e9fSShreyas B. Prabhu 	update_hid_in_slw(hid0);
233e2186023SMichael Ellerman 
234e2186023SMichael Ellerman 	/* Wait for it to happen */
235e2186023SMichael Ellerman 	while (!(mfspr(SPRN_HID0) & split_parms[i].mask))
236e2186023SMichael Ellerman 		cpu_relax();
237e2186023SMichael Ellerman }
238e2186023SMichael Ellerman 
239e2186023SMichael Ellerman static void cpu_do_split(int new_mode)
240e2186023SMichael Ellerman {
241e2186023SMichael Ellerman 	/*
242e2186023SMichael Ellerman 	 * At boot subcores_per_core will be 0, so we will always unsplit at
243e2186023SMichael Ellerman 	 * boot. In the usual case where the core is already unsplit it's a
244e2186023SMichael Ellerman 	 * nop, and this just ensures the kernel's notion of the mode is
245e2186023SMichael Ellerman 	 * consistent with the hardware.
246e2186023SMichael Ellerman 	 */
247e2186023SMichael Ellerman 	if (subcores_per_core != 1)
248e2186023SMichael Ellerman 		unsplit_core();
249e2186023SMichael Ellerman 
250e2186023SMichael Ellerman 	if (new_mode != 1)
251e2186023SMichael Ellerman 		split_core(new_mode);
252e2186023SMichael Ellerman 
253e2186023SMichael Ellerman 	mb();
254e2186023SMichael Ellerman 	per_cpu(split_state, smp_processor_id()).step = SYNC_STEP_FINISHED;
255e2186023SMichael Ellerman }
256e2186023SMichael Ellerman 
257e2186023SMichael Ellerman bool cpu_core_split_required(void)
258e2186023SMichael Ellerman {
259e2186023SMichael Ellerman 	smp_rmb();
260e2186023SMichael Ellerman 
261e2186023SMichael Ellerman 	if (!new_split_mode)
262e2186023SMichael Ellerman 		return false;
263e2186023SMichael Ellerman 
264e2186023SMichael Ellerman 	cpu_do_split(new_split_mode);
265e2186023SMichael Ellerman 
266e2186023SMichael Ellerman 	return true;
267e2186023SMichael Ellerman }
268e2186023SMichael Ellerman 
26977b54e9fSShreyas B. Prabhu void update_subcore_sibling_mask(void)
27077b54e9fSShreyas B. Prabhu {
27177b54e9fSShreyas B. Prabhu 	int cpu;
27277b54e9fSShreyas B. Prabhu 	/*
27377b54e9fSShreyas B. Prabhu 	 * sibling mask for the first cpu. Left shift this by required bits
27477b54e9fSShreyas B. Prabhu 	 * to get sibling mask for the rest of the cpus.
27577b54e9fSShreyas B. Prabhu 	 */
27677b54e9fSShreyas B. Prabhu 	int sibling_mask_first_cpu =  (1 << threads_per_subcore) - 1;
27777b54e9fSShreyas B. Prabhu 
27877b54e9fSShreyas B. Prabhu 	for_each_possible_cpu(cpu) {
27977b54e9fSShreyas B. Prabhu 		int tid = cpu_thread_in_core(cpu);
28077b54e9fSShreyas B. Prabhu 		int offset = (tid / threads_per_subcore) * threads_per_subcore;
28177b54e9fSShreyas B. Prabhu 		int mask = sibling_mask_first_cpu << offset;
28277b54e9fSShreyas B. Prabhu 
283d2e60075SNicholas Piggin 		paca_ptrs[cpu]->subcore_sibling_mask = mask;
28477b54e9fSShreyas B. Prabhu 
28577b54e9fSShreyas B. Prabhu 	}
28677b54e9fSShreyas B. Prabhu }
28777b54e9fSShreyas B. Prabhu 
288e2186023SMichael Ellerman static int cpu_update_split_mode(void *data)
289e2186023SMichael Ellerman {
290e2186023SMichael Ellerman 	int cpu, new_mode = *(int *)data;
291e2186023SMichael Ellerman 
292e2186023SMichael Ellerman 	if (this_cpu_ptr(&split_state)->master) {
293e2186023SMichael Ellerman 		new_split_mode = new_mode;
294e2186023SMichael Ellerman 		smp_wmb();
295e2186023SMichael Ellerman 
296e2186023SMichael Ellerman 		cpumask_andnot(cpu_offline_mask, cpu_present_mask,
297e2186023SMichael Ellerman 			       cpu_online_mask);
298e2186023SMichael Ellerman 
299e2186023SMichael Ellerman 		/* This should work even though the cpu is offline */
300e2186023SMichael Ellerman 		for_each_cpu(cpu, cpu_offline_mask)
301e2186023SMichael Ellerman 			smp_send_reschedule(cpu);
302e2186023SMichael Ellerman 	}
303e2186023SMichael Ellerman 
304e2186023SMichael Ellerman 	cpu_do_split(new_mode);
305e2186023SMichael Ellerman 
306e2186023SMichael Ellerman 	if (this_cpu_ptr(&split_state)->master) {
307e2186023SMichael Ellerman 		/* Wait for all cpus to finish before we touch subcores_per_core */
308e2186023SMichael Ellerman 		for_each_present_cpu(cpu) {
309e2186023SMichael Ellerman 			if (cpu >= setup_max_cpus)
310e2186023SMichael Ellerman 				break;
311e2186023SMichael Ellerman 
312e2186023SMichael Ellerman 			while(per_cpu(split_state, cpu).step < SYNC_STEP_FINISHED)
313e2186023SMichael Ellerman 				barrier();
314e2186023SMichael Ellerman 		}
315e2186023SMichael Ellerman 
316e2186023SMichael Ellerman 		new_split_mode = 0;
317e2186023SMichael Ellerman 
318e2186023SMichael Ellerman 		/* Make the new mode public */
319e2186023SMichael Ellerman 		subcores_per_core = new_mode;
320e2186023SMichael Ellerman 		threads_per_subcore = threads_per_core / subcores_per_core;
32177b54e9fSShreyas B. Prabhu 		update_subcore_sibling_mask();
322e2186023SMichael Ellerman 
323e2186023SMichael Ellerman 		/* Make sure the new mode is written before we exit */
324e2186023SMichael Ellerman 		mb();
325e2186023SMichael Ellerman 	}
326e2186023SMichael Ellerman 
327e2186023SMichael Ellerman 	return 0;
328e2186023SMichael Ellerman }
329e2186023SMichael Ellerman 
330e2186023SMichael Ellerman static int set_subcores_per_core(int new_mode)
331e2186023SMichael Ellerman {
332e2186023SMichael Ellerman 	struct split_state *state;
333e2186023SMichael Ellerman 	int cpu;
334e2186023SMichael Ellerman 
335e2186023SMichael Ellerman 	if (kvm_hv_mode_active()) {
336e2186023SMichael Ellerman 		pr_err("Unable to change split core mode while KVM active.\n");
337e2186023SMichael Ellerman 		return -EBUSY;
338e2186023SMichael Ellerman 	}
339e2186023SMichael Ellerman 
340e2186023SMichael Ellerman 	/*
341e2186023SMichael Ellerman 	 * We are only called at boot, or from the sysfs write. If that ever
342e2186023SMichael Ellerman 	 * changes we'll need a lock here.
343e2186023SMichael Ellerman 	 */
344e2186023SMichael Ellerman 	BUG_ON(new_mode < 1 || new_mode > 4 || new_mode == 3);
345e2186023SMichael Ellerman 
346e2186023SMichael Ellerman 	for_each_present_cpu(cpu) {
347e2186023SMichael Ellerman 		state = &per_cpu(split_state, cpu);
348e2186023SMichael Ellerman 		state->step = SYNC_STEP_INITIAL;
349e2186023SMichael Ellerman 		state->master = 0;
350e2186023SMichael Ellerman 	}
351e2186023SMichael Ellerman 
352f9a69931SSebastian Andrzej Siewior 	cpus_read_lock();
353e2186023SMichael Ellerman 
354e2186023SMichael Ellerman 	/* This cpu will update the globals before exiting stop machine */
355e2186023SMichael Ellerman 	this_cpu_ptr(&split_state)->master = 1;
356e2186023SMichael Ellerman 
357e2186023SMichael Ellerman 	/* Ensure state is consistent before we call the other cpus */
358e2186023SMichael Ellerman 	mb();
359e2186023SMichael Ellerman 
360f9a69931SSebastian Andrzej Siewior 	stop_machine_cpuslocked(cpu_update_split_mode, &new_mode,
361f9a69931SSebastian Andrzej Siewior 				cpu_online_mask);
362e2186023SMichael Ellerman 
363f9a69931SSebastian Andrzej Siewior 	cpus_read_unlock();
364e2186023SMichael Ellerman 
365e2186023SMichael Ellerman 	return 0;
366e2186023SMichael Ellerman }
367e2186023SMichael Ellerman 
368e2186023SMichael Ellerman static ssize_t __used store_subcores_per_core(struct device *dev,
369e2186023SMichael Ellerman 		struct device_attribute *attr, const char *buf,
370e2186023SMichael Ellerman 		size_t count)
371e2186023SMichael Ellerman {
372e2186023SMichael Ellerman 	unsigned long val;
373e2186023SMichael Ellerman 	int rc;
374e2186023SMichael Ellerman 
375e2186023SMichael Ellerman 	/* We are serialised by the attribute lock */
376e2186023SMichael Ellerman 
377e2186023SMichael Ellerman 	rc = sscanf(buf, "%lx", &val);
378e2186023SMichael Ellerman 	if (rc != 1)
379e2186023SMichael Ellerman 		return -EINVAL;
380e2186023SMichael Ellerman 
381e2186023SMichael Ellerman 	switch (val) {
382e2186023SMichael Ellerman 	case 1:
383e2186023SMichael Ellerman 	case 2:
384e2186023SMichael Ellerman 	case 4:
385e2186023SMichael Ellerman 		if (subcores_per_core == val)
386e2186023SMichael Ellerman 			/* Nothing to do */
387e2186023SMichael Ellerman 			goto out;
388e2186023SMichael Ellerman 		break;
389e2186023SMichael Ellerman 	default:
390e2186023SMichael Ellerman 		return -EINVAL;
391e2186023SMichael Ellerman 	}
392e2186023SMichael Ellerman 
393e2186023SMichael Ellerman 	rc = set_subcores_per_core(val);
394e2186023SMichael Ellerman 	if (rc)
395e2186023SMichael Ellerman 		return rc;
396e2186023SMichael Ellerman 
397e2186023SMichael Ellerman out:
398e2186023SMichael Ellerman 	return count;
399e2186023SMichael Ellerman }
400e2186023SMichael Ellerman 
401e2186023SMichael Ellerman static ssize_t show_subcores_per_core(struct device *dev,
402e2186023SMichael Ellerman 		struct device_attribute *attr, char *buf)
403e2186023SMichael Ellerman {
404e2186023SMichael Ellerman 	return sprintf(buf, "%x\n", subcores_per_core);
405e2186023SMichael Ellerman }
406e2186023SMichael Ellerman 
407e2186023SMichael Ellerman static DEVICE_ATTR(subcores_per_core, 0644,
408e2186023SMichael Ellerman 		show_subcores_per_core, store_subcores_per_core);
409e2186023SMichael Ellerman 
410e2186023SMichael Ellerman static int subcore_init(void)
411e2186023SMichael Ellerman {
4120e5e7f5eSMichael Ellerman 	unsigned pvr_ver;
4130e5e7f5eSMichael Ellerman 
4140e5e7f5eSMichael Ellerman 	pvr_ver = PVR_VER(mfspr(SPRN_PVR));
4150e5e7f5eSMichael Ellerman 
4160e5e7f5eSMichael Ellerman 	if (pvr_ver != PVR_POWER8 &&
4170e5e7f5eSMichael Ellerman 	    pvr_ver != PVR_POWER8E &&
4180e5e7f5eSMichael Ellerman 	    pvr_ver != PVR_POWER8NVL)
419e2186023SMichael Ellerman 		return 0;
420e2186023SMichael Ellerman 
421e2186023SMichael Ellerman 	/*
422e2186023SMichael Ellerman 	 * We need all threads in a core to be present to split/unsplit so
423e2186023SMichael Ellerman          * continue only if max_cpus are aligned to threads_per_core.
424e2186023SMichael Ellerman 	 */
425e2186023SMichael Ellerman 	if (setup_max_cpus % threads_per_core)
426e2186023SMichael Ellerman 		return 0;
427e2186023SMichael Ellerman 
428e2186023SMichael Ellerman 	BUG_ON(!alloc_cpumask_var(&cpu_offline_mask, GFP_KERNEL));
429e2186023SMichael Ellerman 
430e2186023SMichael Ellerman 	set_subcores_per_core(1);
431e2186023SMichael Ellerman 
432e2186023SMichael Ellerman 	return device_create_file(cpu_subsys.dev_root,
433e2186023SMichael Ellerman 				  &dev_attr_subcores_per_core);
434e2186023SMichael Ellerman }
435e2186023SMichael Ellerman machine_device_initcall(powernv, subcore_init);
436