xref: /openbmc/linux/arch/powerpc/platforms/pseries/dlpar.c (revision 1578cb76d46c9735a740c919cb9b7e5d1ba92420)
1ab519a01SNathan Fontenot /*
2ab519a01SNathan Fontenot  * Support for dynamic reconfiguration for PCI, Memory, and CPU
3ab519a01SNathan Fontenot  * Hotplug and Dynamic Logical Partitioning on RPA platforms.
4ab519a01SNathan Fontenot  *
5ab519a01SNathan Fontenot  * Copyright (C) 2009 Nathan Fontenot
6ab519a01SNathan Fontenot  * Copyright (C) 2009 IBM Corporation
7ab519a01SNathan Fontenot  *
8ab519a01SNathan Fontenot  * This program is free software; you can redistribute it and/or
9ab519a01SNathan Fontenot  * modify it under the terms of the GNU General Public License version
10ab519a01SNathan Fontenot  * 2 as published by the Free Software Foundation.
11ab519a01SNathan Fontenot  */
12ab519a01SNathan Fontenot 
13ab519a01SNathan Fontenot #include <linux/kernel.h>
14ab519a01SNathan Fontenot #include <linux/kref.h>
15ab519a01SNathan Fontenot #include <linux/notifier.h>
16ab519a01SNathan Fontenot #include <linux/spinlock.h>
17ab519a01SNathan Fontenot #include <linux/cpu.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
191cf3d8b3SNathan Fontenot #include <linux/of.h>
20b6db63d1SGautham R Shenoy #include "offline_states.h"
21ab519a01SNathan Fontenot 
22ab519a01SNathan Fontenot #include <asm/prom.h>
23ab519a01SNathan Fontenot #include <asm/machdep.h>
24ab519a01SNathan Fontenot #include <asm/uaccess.h>
25ab519a01SNathan Fontenot #include <asm/rtas.h>
26ab519a01SNathan Fontenot 
27ab519a01SNathan Fontenot struct cc_workarea {
28ab519a01SNathan Fontenot 	u32	drc_index;
29ab519a01SNathan Fontenot 	u32	zero;
30ab519a01SNathan Fontenot 	u32	name_offset;
31ab519a01SNathan Fontenot 	u32	prop_length;
32ab519a01SNathan Fontenot 	u32	prop_offset;
33ab519a01SNathan Fontenot };
34ab519a01SNathan Fontenot 
3520648974SNathan Fontenot void dlpar_free_cc_property(struct property *prop)
36ab519a01SNathan Fontenot {
37ab519a01SNathan Fontenot 	kfree(prop->name);
38ab519a01SNathan Fontenot 	kfree(prop->value);
39ab519a01SNathan Fontenot 	kfree(prop);
40ab519a01SNathan Fontenot }
41ab519a01SNathan Fontenot 
42ab519a01SNathan Fontenot static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
43ab519a01SNathan Fontenot {
44ab519a01SNathan Fontenot 	struct property *prop;
45ab519a01SNathan Fontenot 	char *name;
46ab519a01SNathan Fontenot 	char *value;
47ab519a01SNathan Fontenot 
48ab519a01SNathan Fontenot 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
49ab519a01SNathan Fontenot 	if (!prop)
50ab519a01SNathan Fontenot 		return NULL;
51ab519a01SNathan Fontenot 
52ab519a01SNathan Fontenot 	name = (char *)ccwa + ccwa->name_offset;
53ab519a01SNathan Fontenot 	prop->name = kstrdup(name, GFP_KERNEL);
54ab519a01SNathan Fontenot 
55ab519a01SNathan Fontenot 	prop->length = ccwa->prop_length;
56ab519a01SNathan Fontenot 	value = (char *)ccwa + ccwa->prop_offset;
57e72ed6b5SNishanth Aravamudan 	prop->value = kmemdup(value, prop->length, GFP_KERNEL);
58ab519a01SNathan Fontenot 	if (!prop->value) {
59ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
60ab519a01SNathan Fontenot 		return NULL;
61ab519a01SNathan Fontenot 	}
62ab519a01SNathan Fontenot 
63ab519a01SNathan Fontenot 	return prop;
64ab519a01SNathan Fontenot }
65ab519a01SNathan Fontenot 
66ab519a01SNathan Fontenot static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
67ab519a01SNathan Fontenot {
68ab519a01SNathan Fontenot 	struct device_node *dn;
69ab519a01SNathan Fontenot 	char *name;
70ab519a01SNathan Fontenot 
71ab519a01SNathan Fontenot 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
72ab519a01SNathan Fontenot 	if (!dn)
73ab519a01SNathan Fontenot 		return NULL;
74ab519a01SNathan Fontenot 
75ab519a01SNathan Fontenot 	/* The configure connector reported name does not contain a
7625985edcSLucas De Marchi 	 * preceding '/', so we allocate a buffer large enough to
77ab519a01SNathan Fontenot 	 * prepend this to the full_name.
78ab519a01SNathan Fontenot 	 */
79ab519a01SNathan Fontenot 	name = (char *)ccwa + ccwa->name_offset;
8043caa61fSJulia Lawall 	dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);
81ab519a01SNathan Fontenot 	if (!dn->full_name) {
82ab519a01SNathan Fontenot 		kfree(dn);
83ab519a01SNathan Fontenot 		return NULL;
84ab519a01SNathan Fontenot 	}
85ab519a01SNathan Fontenot 
86*1578cb76STyrel Datwyler 	of_node_set_flag(dn, OF_DYNAMIC);
87*1578cb76STyrel Datwyler 	kref_init(&dn->kref);
88*1578cb76STyrel Datwyler 
89ab519a01SNathan Fontenot 	return dn;
90ab519a01SNathan Fontenot }
91ab519a01SNathan Fontenot 
92ab519a01SNathan Fontenot static void dlpar_free_one_cc_node(struct device_node *dn)
93ab519a01SNathan Fontenot {
94ab519a01SNathan Fontenot 	struct property *prop;
95ab519a01SNathan Fontenot 
96ab519a01SNathan Fontenot 	while (dn->properties) {
97ab519a01SNathan Fontenot 		prop = dn->properties;
98ab519a01SNathan Fontenot 		dn->properties = prop->next;
99ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
100ab519a01SNathan Fontenot 	}
101ab519a01SNathan Fontenot 
102ab519a01SNathan Fontenot 	kfree(dn->full_name);
103ab519a01SNathan Fontenot 	kfree(dn);
104ab519a01SNathan Fontenot }
105ab519a01SNathan Fontenot 
10620648974SNathan Fontenot void dlpar_free_cc_nodes(struct device_node *dn)
107ab519a01SNathan Fontenot {
108ab519a01SNathan Fontenot 	if (dn->child)
109ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->child);
110ab519a01SNathan Fontenot 
111ab519a01SNathan Fontenot 	if (dn->sibling)
112ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->sibling);
113ab519a01SNathan Fontenot 
114ab519a01SNathan Fontenot 	dlpar_free_one_cc_node(dn);
115ab519a01SNathan Fontenot }
116ab519a01SNathan Fontenot 
1179c740025SAnton Blanchard #define COMPLETE	0
118ab519a01SNathan Fontenot #define NEXT_SIBLING    1
119ab519a01SNathan Fontenot #define NEXT_CHILD      2
120ab519a01SNathan Fontenot #define NEXT_PROPERTY   3
121ab519a01SNathan Fontenot #define PREV_PARENT     4
122ab519a01SNathan Fontenot #define MORE_MEMORY     5
123ab519a01SNathan Fontenot #define CALL_AGAIN	-2
124ab519a01SNathan Fontenot #define ERR_CFG_USE     -9003
125ab519a01SNathan Fontenot 
126ab519a01SNathan Fontenot struct device_node *dlpar_configure_connector(u32 drc_index)
127ab519a01SNathan Fontenot {
128ab519a01SNathan Fontenot 	struct device_node *dn;
129ab519a01SNathan Fontenot 	struct device_node *first_dn = NULL;
130ab519a01SNathan Fontenot 	struct device_node *last_dn = NULL;
131ab519a01SNathan Fontenot 	struct property *property;
132ab519a01SNathan Fontenot 	struct property *last_property = NULL;
133ab519a01SNathan Fontenot 	struct cc_workarea *ccwa;
13493f68f1eSNathan Fontenot 	char *data_buf;
135ab519a01SNathan Fontenot 	int cc_token;
13693f68f1eSNathan Fontenot 	int rc = -1;
137ab519a01SNathan Fontenot 
138ab519a01SNathan Fontenot 	cc_token = rtas_token("ibm,configure-connector");
139ab519a01SNathan Fontenot 	if (cc_token == RTAS_UNKNOWN_SERVICE)
140ab519a01SNathan Fontenot 		return NULL;
141ab519a01SNathan Fontenot 
14293f68f1eSNathan Fontenot 	data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
14393f68f1eSNathan Fontenot 	if (!data_buf)
14493f68f1eSNathan Fontenot 		return NULL;
14593f68f1eSNathan Fontenot 
14693f68f1eSNathan Fontenot 	ccwa = (struct cc_workarea *)&data_buf[0];
147ab519a01SNathan Fontenot 	ccwa->drc_index = drc_index;
148ab519a01SNathan Fontenot 	ccwa->zero = 0;
149ab519a01SNathan Fontenot 
15093f68f1eSNathan Fontenot 	do {
15193f68f1eSNathan Fontenot 		/* Since we release the rtas_data_buf lock between configure
15293f68f1eSNathan Fontenot 		 * connector calls we want to re-populate the rtas_data_buffer
15393f68f1eSNathan Fontenot 		 * with the contents of the previous call.
15493f68f1eSNathan Fontenot 		 */
15593f68f1eSNathan Fontenot 		spin_lock(&rtas_data_buf_lock);
15693f68f1eSNathan Fontenot 
15793f68f1eSNathan Fontenot 		memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
158ab519a01SNathan Fontenot 		rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
15993f68f1eSNathan Fontenot 		memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
16093f68f1eSNathan Fontenot 
16193f68f1eSNathan Fontenot 		spin_unlock(&rtas_data_buf_lock);
16293f68f1eSNathan Fontenot 
163ab519a01SNathan Fontenot 		switch (rc) {
1649c740025SAnton Blanchard 		case COMPLETE:
1659c740025SAnton Blanchard 			break;
1669c740025SAnton Blanchard 
167ab519a01SNathan Fontenot 		case NEXT_SIBLING:
168ab519a01SNathan Fontenot 			dn = dlpar_parse_cc_node(ccwa);
169ab519a01SNathan Fontenot 			if (!dn)
170ab519a01SNathan Fontenot 				goto cc_error;
171ab519a01SNathan Fontenot 
172ab519a01SNathan Fontenot 			dn->parent = last_dn->parent;
173ab519a01SNathan Fontenot 			last_dn->sibling = dn;
174ab519a01SNathan Fontenot 			last_dn = dn;
175ab519a01SNathan Fontenot 			break;
176ab519a01SNathan Fontenot 
177ab519a01SNathan Fontenot 		case NEXT_CHILD:
178ab519a01SNathan Fontenot 			dn = dlpar_parse_cc_node(ccwa);
179ab519a01SNathan Fontenot 			if (!dn)
180ab519a01SNathan Fontenot 				goto cc_error;
181ab519a01SNathan Fontenot 
182ab519a01SNathan Fontenot 			if (!first_dn)
183ab519a01SNathan Fontenot 				first_dn = dn;
184ab519a01SNathan Fontenot 			else {
185ab519a01SNathan Fontenot 				dn->parent = last_dn;
186ab519a01SNathan Fontenot 				if (last_dn)
187ab519a01SNathan Fontenot 					last_dn->child = dn;
188ab519a01SNathan Fontenot 			}
189ab519a01SNathan Fontenot 
190ab519a01SNathan Fontenot 			last_dn = dn;
191ab519a01SNathan Fontenot 			break;
192ab519a01SNathan Fontenot 
193ab519a01SNathan Fontenot 		case NEXT_PROPERTY:
194ab519a01SNathan Fontenot 			property = dlpar_parse_cc_property(ccwa);
195ab519a01SNathan Fontenot 			if (!property)
196ab519a01SNathan Fontenot 				goto cc_error;
197ab519a01SNathan Fontenot 
198ab519a01SNathan Fontenot 			if (!last_dn->properties)
199ab519a01SNathan Fontenot 				last_dn->properties = property;
200ab519a01SNathan Fontenot 			else
201ab519a01SNathan Fontenot 				last_property->next = property;
202ab519a01SNathan Fontenot 
203ab519a01SNathan Fontenot 			last_property = property;
204ab519a01SNathan Fontenot 			break;
205ab519a01SNathan Fontenot 
206ab519a01SNathan Fontenot 		case PREV_PARENT:
207ab519a01SNathan Fontenot 			last_dn = last_dn->parent;
208ab519a01SNathan Fontenot 			break;
209ab519a01SNathan Fontenot 
210ab519a01SNathan Fontenot 		case CALL_AGAIN:
211ab519a01SNathan Fontenot 			break;
212ab519a01SNathan Fontenot 
213ab519a01SNathan Fontenot 		case MORE_MEMORY:
214ab519a01SNathan Fontenot 		case ERR_CFG_USE:
215ab519a01SNathan Fontenot 		default:
216ab519a01SNathan Fontenot 			printk(KERN_ERR "Unexpected Error (%d) "
217ab519a01SNathan Fontenot 			       "returned from configure-connector\n", rc);
218ab519a01SNathan Fontenot 			goto cc_error;
219ab519a01SNathan Fontenot 		}
22093f68f1eSNathan Fontenot 	} while (rc);
221ab519a01SNathan Fontenot 
222ab519a01SNathan Fontenot cc_error:
22393f68f1eSNathan Fontenot 	kfree(data_buf);
22493f68f1eSNathan Fontenot 
22593f68f1eSNathan Fontenot 	if (rc) {
226ab519a01SNathan Fontenot 		if (first_dn)
227ab519a01SNathan Fontenot 			dlpar_free_cc_nodes(first_dn);
22893f68f1eSNathan Fontenot 
229ab519a01SNathan Fontenot 		return NULL;
230ab519a01SNathan Fontenot 	}
231ab519a01SNathan Fontenot 
23293f68f1eSNathan Fontenot 	return first_dn;
23393f68f1eSNathan Fontenot }
23493f68f1eSNathan Fontenot 
235ab519a01SNathan Fontenot static struct device_node *derive_parent(const char *path)
236ab519a01SNathan Fontenot {
237ab519a01SNathan Fontenot 	struct device_node *parent;
238ab519a01SNathan Fontenot 	char *last_slash;
239ab519a01SNathan Fontenot 
240ab519a01SNathan Fontenot 	last_slash = strrchr(path, '/');
241ab519a01SNathan Fontenot 	if (last_slash == path) {
242ab519a01SNathan Fontenot 		parent = of_find_node_by_path("/");
243ab519a01SNathan Fontenot 	} else {
244ab519a01SNathan Fontenot 		char *parent_path;
245ab519a01SNathan Fontenot 		int parent_path_len = last_slash - path + 1;
246ab519a01SNathan Fontenot 		parent_path = kmalloc(parent_path_len, GFP_KERNEL);
247ab519a01SNathan Fontenot 		if (!parent_path)
248ab519a01SNathan Fontenot 			return NULL;
249ab519a01SNathan Fontenot 
250ab519a01SNathan Fontenot 		strlcpy(parent_path, path, parent_path_len);
251ab519a01SNathan Fontenot 		parent = of_find_node_by_path(parent_path);
252ab519a01SNathan Fontenot 		kfree(parent_path);
253ab519a01SNathan Fontenot 	}
254ab519a01SNathan Fontenot 
255ab519a01SNathan Fontenot 	return parent;
256ab519a01SNathan Fontenot }
257ab519a01SNathan Fontenot 
258ab519a01SNathan Fontenot int dlpar_attach_node(struct device_node *dn)
259ab519a01SNathan Fontenot {
260ab519a01SNathan Fontenot 	int rc;
261ab519a01SNathan Fontenot 
262ab519a01SNathan Fontenot 	dn->parent = derive_parent(dn->full_name);
263ab519a01SNathan Fontenot 	if (!dn->parent)
264ab519a01SNathan Fontenot 		return -ENOMEM;
265ab519a01SNathan Fontenot 
2661cf3d8b3SNathan Fontenot 	rc = of_attach_node(dn);
2673aef19f0SAkinobu Mita 	if (rc) {
268ab519a01SNathan Fontenot 		printk(KERN_ERR "Failed to add device node %s\n",
269ab519a01SNathan Fontenot 		       dn->full_name);
2703aef19f0SAkinobu Mita 		return rc;
271ab519a01SNathan Fontenot 	}
272ab519a01SNathan Fontenot 
273ab519a01SNathan Fontenot 	of_node_put(dn->parent);
274ab519a01SNathan Fontenot 	return 0;
275ab519a01SNathan Fontenot }
276ab519a01SNathan Fontenot 
277ab519a01SNathan Fontenot int dlpar_detach_node(struct device_node *dn)
278ab519a01SNathan Fontenot {
2791cf3d8b3SNathan Fontenot 	int rc;
280ab519a01SNathan Fontenot 
2811cf3d8b3SNathan Fontenot 	rc = of_detach_node(dn);
2821cf3d8b3SNathan Fontenot 	if (rc)
2831cf3d8b3SNathan Fontenot 		return rc;
2841cf3d8b3SNathan Fontenot 
2851cf3d8b3SNathan Fontenot 	of_node_put(dn); /* Must decrement the refcount */
286ab519a01SNathan Fontenot 	return 0;
287ab519a01SNathan Fontenot }
288ab519a01SNathan Fontenot 
289ab519a01SNathan Fontenot #define DR_ENTITY_SENSE		9003
290ab519a01SNathan Fontenot #define DR_ENTITY_PRESENT	1
291ab519a01SNathan Fontenot #define DR_ENTITY_UNUSABLE	2
292ab519a01SNathan Fontenot #define ALLOCATION_STATE	9003
293ab519a01SNathan Fontenot #define ALLOC_UNUSABLE		0
294ab519a01SNathan Fontenot #define ALLOC_USABLE		1
295ab519a01SNathan Fontenot #define ISOLATION_STATE		9001
296ab519a01SNathan Fontenot #define ISOLATE			0
297ab519a01SNathan Fontenot #define UNISOLATE		1
298ab519a01SNathan Fontenot 
299ab519a01SNathan Fontenot int dlpar_acquire_drc(u32 drc_index)
300ab519a01SNathan Fontenot {
301ab519a01SNathan Fontenot 	int dr_status, rc;
302ab519a01SNathan Fontenot 
303ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
304ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
305ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_UNUSABLE)
306ab519a01SNathan Fontenot 		return -1;
307ab519a01SNathan Fontenot 
308ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
309ab519a01SNathan Fontenot 	if (rc)
310ab519a01SNathan Fontenot 		return rc;
311ab519a01SNathan Fontenot 
312ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
313ab519a01SNathan Fontenot 	if (rc) {
314ab519a01SNathan Fontenot 		rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
315ab519a01SNathan Fontenot 		return rc;
316ab519a01SNathan Fontenot 	}
317ab519a01SNathan Fontenot 
318ab519a01SNathan Fontenot 	return 0;
319ab519a01SNathan Fontenot }
320ab519a01SNathan Fontenot 
321ab519a01SNathan Fontenot int dlpar_release_drc(u32 drc_index)
322ab519a01SNathan Fontenot {
323ab519a01SNathan Fontenot 	int dr_status, rc;
324ab519a01SNathan Fontenot 
325ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
326ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
327ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_PRESENT)
328ab519a01SNathan Fontenot 		return -1;
329ab519a01SNathan Fontenot 
330ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
331ab519a01SNathan Fontenot 	if (rc)
332ab519a01SNathan Fontenot 		return rc;
333ab519a01SNathan Fontenot 
334ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
335ab519a01SNathan Fontenot 	if (rc) {
336ab519a01SNathan Fontenot 		rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
337ab519a01SNathan Fontenot 		return rc;
338ab519a01SNathan Fontenot 	}
339ab519a01SNathan Fontenot 
340ab519a01SNathan Fontenot 	return 0;
341ab519a01SNathan Fontenot }
342ab519a01SNathan Fontenot 
3431a8061c4SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
344ab519a01SNathan Fontenot 
345275a64f6SNathan Fontenot static int dlpar_online_cpu(struct device_node *dn)
346275a64f6SNathan Fontenot {
347275a64f6SNathan Fontenot 	int rc = 0;
348275a64f6SNathan Fontenot 	unsigned int cpu;
349275a64f6SNathan Fontenot 	int len, nthreads, i;
350275a64f6SNathan Fontenot 	const u32 *intserv;
351275a64f6SNathan Fontenot 
352275a64f6SNathan Fontenot 	intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
353275a64f6SNathan Fontenot 	if (!intserv)
354275a64f6SNathan Fontenot 		return -EINVAL;
355275a64f6SNathan Fontenot 
356275a64f6SNathan Fontenot 	nthreads = len / sizeof(u32);
357275a64f6SNathan Fontenot 
358275a64f6SNathan Fontenot 	cpu_maps_update_begin();
359275a64f6SNathan Fontenot 	for (i = 0; i < nthreads; i++) {
360275a64f6SNathan Fontenot 		for_each_present_cpu(cpu) {
361275a64f6SNathan Fontenot 			if (get_hard_smp_processor_id(cpu) != intserv[i])
362275a64f6SNathan Fontenot 				continue;
363275a64f6SNathan Fontenot 			BUG_ON(get_cpu_current_state(cpu)
364275a64f6SNathan Fontenot 					!= CPU_STATE_OFFLINE);
365275a64f6SNathan Fontenot 			cpu_maps_update_done();
366275a64f6SNathan Fontenot 			rc = cpu_up(cpu);
367275a64f6SNathan Fontenot 			if (rc)
368275a64f6SNathan Fontenot 				goto out;
369275a64f6SNathan Fontenot 			cpu_maps_update_begin();
370275a64f6SNathan Fontenot 
371275a64f6SNathan Fontenot 			break;
372275a64f6SNathan Fontenot 		}
373275a64f6SNathan Fontenot 		if (cpu == num_possible_cpus())
374275a64f6SNathan Fontenot 			printk(KERN_WARNING "Could not find cpu to online "
375275a64f6SNathan Fontenot 			       "with physical id 0x%x\n", intserv[i]);
376275a64f6SNathan Fontenot 	}
377275a64f6SNathan Fontenot 	cpu_maps_update_done();
378275a64f6SNathan Fontenot 
379275a64f6SNathan Fontenot out:
380275a64f6SNathan Fontenot 	return rc;
381275a64f6SNathan Fontenot 
382275a64f6SNathan Fontenot }
383275a64f6SNathan Fontenot 
3841a8061c4SNathan Fontenot static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
3851a8061c4SNathan Fontenot {
3861a8061c4SNathan Fontenot 	struct device_node *dn;
3871a8061c4SNathan Fontenot 	unsigned long drc_index;
3881a8061c4SNathan Fontenot 	char *cpu_name;
3891a8061c4SNathan Fontenot 	int rc;
3901a8061c4SNathan Fontenot 
39151badebdSGautham R Shenoy 	cpu_hotplug_driver_lock();
3921a8061c4SNathan Fontenot 	rc = strict_strtoul(buf, 0, &drc_index);
39351badebdSGautham R Shenoy 	if (rc) {
39451badebdSGautham R Shenoy 		rc = -EINVAL;
39551badebdSGautham R Shenoy 		goto out;
39651badebdSGautham R Shenoy 	}
3971a8061c4SNathan Fontenot 
3981a8061c4SNathan Fontenot 	dn = dlpar_configure_connector(drc_index);
39951badebdSGautham R Shenoy 	if (!dn) {
40051badebdSGautham R Shenoy 		rc = -EINVAL;
40151badebdSGautham R Shenoy 		goto out;
40251badebdSGautham R Shenoy 	}
4031a8061c4SNathan Fontenot 
4041a8061c4SNathan Fontenot 	/* configure-connector reports cpus as living in the base
4051a8061c4SNathan Fontenot 	 * directory of the device tree.  CPUs actually live in the
4061a8061c4SNathan Fontenot 	 * cpus directory so we need to fixup the full_name.
4071a8061c4SNathan Fontenot 	 */
40843caa61fSJulia Lawall 	cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
4091a8061c4SNathan Fontenot 	if (!cpu_name) {
4101a8061c4SNathan Fontenot 		dlpar_free_cc_nodes(dn);
41151badebdSGautham R Shenoy 		rc = -ENOMEM;
41251badebdSGautham R Shenoy 		goto out;
4131a8061c4SNathan Fontenot 	}
4141a8061c4SNathan Fontenot 
4151a8061c4SNathan Fontenot 	kfree(dn->full_name);
4161a8061c4SNathan Fontenot 	dn->full_name = cpu_name;
4171a8061c4SNathan Fontenot 
4181a8061c4SNathan Fontenot 	rc = dlpar_acquire_drc(drc_index);
4191a8061c4SNathan Fontenot 	if (rc) {
4201a8061c4SNathan Fontenot 		dlpar_free_cc_nodes(dn);
42151badebdSGautham R Shenoy 		rc = -EINVAL;
42251badebdSGautham R Shenoy 		goto out;
4231a8061c4SNathan Fontenot 	}
4241a8061c4SNathan Fontenot 
4251a8061c4SNathan Fontenot 	rc = dlpar_attach_node(dn);
4261a8061c4SNathan Fontenot 	if (rc) {
4271a8061c4SNathan Fontenot 		dlpar_release_drc(drc_index);
4281a8061c4SNathan Fontenot 		dlpar_free_cc_nodes(dn);
429a7df5c5eSJulia Lawall 		goto out;
4301a8061c4SNathan Fontenot 	}
4311a8061c4SNathan Fontenot 
432275a64f6SNathan Fontenot 	rc = dlpar_online_cpu(dn);
43351badebdSGautham R Shenoy out:
43451badebdSGautham R Shenoy 	cpu_hotplug_driver_unlock();
435b6db63d1SGautham R Shenoy 
4361a8061c4SNathan Fontenot 	return rc ? rc : count;
4371a8061c4SNathan Fontenot }
4381a8061c4SNathan Fontenot 
439275a64f6SNathan Fontenot static int dlpar_offline_cpu(struct device_node *dn)
440275a64f6SNathan Fontenot {
441275a64f6SNathan Fontenot 	int rc = 0;
442275a64f6SNathan Fontenot 	unsigned int cpu;
443275a64f6SNathan Fontenot 	int len, nthreads, i;
444275a64f6SNathan Fontenot 	const u32 *intserv;
445275a64f6SNathan Fontenot 
446275a64f6SNathan Fontenot 	intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
447275a64f6SNathan Fontenot 	if (!intserv)
448275a64f6SNathan Fontenot 		return -EINVAL;
449275a64f6SNathan Fontenot 
450275a64f6SNathan Fontenot 	nthreads = len / sizeof(u32);
451275a64f6SNathan Fontenot 
452275a64f6SNathan Fontenot 	cpu_maps_update_begin();
453275a64f6SNathan Fontenot 	for (i = 0; i < nthreads; i++) {
454275a64f6SNathan Fontenot 		for_each_present_cpu(cpu) {
455275a64f6SNathan Fontenot 			if (get_hard_smp_processor_id(cpu) != intserv[i])
456275a64f6SNathan Fontenot 				continue;
457275a64f6SNathan Fontenot 
458275a64f6SNathan Fontenot 			if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
459275a64f6SNathan Fontenot 				break;
460275a64f6SNathan Fontenot 
461275a64f6SNathan Fontenot 			if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
462ceddee23SRobert Jennings 				set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
463275a64f6SNathan Fontenot 				cpu_maps_update_done();
464275a64f6SNathan Fontenot 				rc = cpu_down(cpu);
465275a64f6SNathan Fontenot 				if (rc)
466275a64f6SNathan Fontenot 					goto out;
467275a64f6SNathan Fontenot 				cpu_maps_update_begin();
468275a64f6SNathan Fontenot 				break;
469275a64f6SNathan Fontenot 
470275a64f6SNathan Fontenot 			}
471275a64f6SNathan Fontenot 
472275a64f6SNathan Fontenot 			/*
473275a64f6SNathan Fontenot 			 * The cpu is in CPU_STATE_INACTIVE.
474275a64f6SNathan Fontenot 			 * Upgrade it's state to CPU_STATE_OFFLINE.
475275a64f6SNathan Fontenot 			 */
476275a64f6SNathan Fontenot 			set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
477275a64f6SNathan Fontenot 			BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
478275a64f6SNathan Fontenot 								!= H_SUCCESS);
479275a64f6SNathan Fontenot 			__cpu_die(cpu);
480275a64f6SNathan Fontenot 			break;
481275a64f6SNathan Fontenot 		}
482275a64f6SNathan Fontenot 		if (cpu == num_possible_cpus())
483275a64f6SNathan Fontenot 			printk(KERN_WARNING "Could not find cpu to offline "
484275a64f6SNathan Fontenot 			       "with physical id 0x%x\n", intserv[i]);
485275a64f6SNathan Fontenot 	}
486275a64f6SNathan Fontenot 	cpu_maps_update_done();
487275a64f6SNathan Fontenot 
488275a64f6SNathan Fontenot out:
489275a64f6SNathan Fontenot 	return rc;
490275a64f6SNathan Fontenot 
491275a64f6SNathan Fontenot }
492275a64f6SNathan Fontenot 
4931a8061c4SNathan Fontenot static ssize_t dlpar_cpu_release(const char *buf, size_t count)
4941a8061c4SNathan Fontenot {
4951a8061c4SNathan Fontenot 	struct device_node *dn;
4961a8061c4SNathan Fontenot 	const u32 *drc_index;
4971a8061c4SNathan Fontenot 	int rc;
4981a8061c4SNathan Fontenot 
4991a8061c4SNathan Fontenot 	dn = of_find_node_by_path(buf);
5001a8061c4SNathan Fontenot 	if (!dn)
5011a8061c4SNathan Fontenot 		return -EINVAL;
5021a8061c4SNathan Fontenot 
5031a8061c4SNathan Fontenot 	drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
5041a8061c4SNathan Fontenot 	if (!drc_index) {
5051a8061c4SNathan Fontenot 		of_node_put(dn);
5061a8061c4SNathan Fontenot 		return -EINVAL;
5071a8061c4SNathan Fontenot 	}
5081a8061c4SNathan Fontenot 
50951badebdSGautham R Shenoy 	cpu_hotplug_driver_lock();
510275a64f6SNathan Fontenot 	rc = dlpar_offline_cpu(dn);
511b6db63d1SGautham R Shenoy 	if (rc) {
512b6db63d1SGautham R Shenoy 		of_node_put(dn);
51351badebdSGautham R Shenoy 		rc = -EINVAL;
51451badebdSGautham R Shenoy 		goto out;
515b6db63d1SGautham R Shenoy 	}
516b6db63d1SGautham R Shenoy 
5171a8061c4SNathan Fontenot 	rc = dlpar_release_drc(*drc_index);
5181a8061c4SNathan Fontenot 	if (rc) {
5191a8061c4SNathan Fontenot 		of_node_put(dn);
52051badebdSGautham R Shenoy 		goto out;
5211a8061c4SNathan Fontenot 	}
5221a8061c4SNathan Fontenot 
5231a8061c4SNathan Fontenot 	rc = dlpar_detach_node(dn);
5241a8061c4SNathan Fontenot 	if (rc) {
5251a8061c4SNathan Fontenot 		dlpar_acquire_drc(*drc_index);
52651badebdSGautham R Shenoy 		goto out;
5271a8061c4SNathan Fontenot 	}
5281a8061c4SNathan Fontenot 
5291a8061c4SNathan Fontenot 	of_node_put(dn);
53051badebdSGautham R Shenoy out:
53151badebdSGautham R Shenoy 	cpu_hotplug_driver_unlock();
53251badebdSGautham R Shenoy 	return rc ? rc : count;
5331a8061c4SNathan Fontenot }
5341a8061c4SNathan Fontenot 
5351a8061c4SNathan Fontenot static int __init pseries_dlpar_init(void)
5361a8061c4SNathan Fontenot {
5371a8061c4SNathan Fontenot 	ppc_md.cpu_probe = dlpar_cpu_probe;
5381a8061c4SNathan Fontenot 	ppc_md.cpu_release = dlpar_cpu_release;
5391a8061c4SNathan Fontenot 
5401a8061c4SNathan Fontenot 	return 0;
5411a8061c4SNathan Fontenot }
5421a8061c4SNathan Fontenot machine_device_initcall(pseries, pseries_dlpar_init);
5431a8061c4SNathan Fontenot 
5441a8061c4SNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
545