xref: /openbmc/linux/arch/powerpc/platforms/pseries/dlpar.c (revision 2222ce0fbbcc4ebfa9995c8d23d72c8239ad712c)
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 
13999e2dadSNathan Fontenot #define pr_fmt(fmt)	"dlpar: " fmt
14999e2dadSNathan Fontenot 
15ab519a01SNathan Fontenot #include <linux/kernel.h>
16ab519a01SNathan Fontenot #include <linux/notifier.h>
17ab519a01SNathan Fontenot #include <linux/spinlock.h>
18ab519a01SNathan Fontenot #include <linux/cpu.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201cf3d8b3SNathan Fontenot #include <linux/of.h>
21b6db63d1SGautham R Shenoy #include "offline_states.h"
221217d34bSAnton Blanchard #include "pseries.h"
23ab519a01SNathan Fontenot 
24ab519a01SNathan Fontenot #include <asm/prom.h>
25ab519a01SNathan Fontenot #include <asm/machdep.h>
26ab519a01SNathan Fontenot #include <asm/uaccess.h>
27ab519a01SNathan Fontenot #include <asm/rtas.h>
28ab519a01SNathan Fontenot 
29ab519a01SNathan Fontenot struct cc_workarea {
30d6f1e7abSBharata B Rao 	__be32	drc_index;
31d6f1e7abSBharata B Rao 	__be32	zero;
32d6f1e7abSBharata B Rao 	__be32	name_offset;
33d6f1e7abSBharata B Rao 	__be32	prop_length;
34d6f1e7abSBharata B Rao 	__be32	prop_offset;
35ab519a01SNathan Fontenot };
36ab519a01SNathan Fontenot 
3720648974SNathan Fontenot void dlpar_free_cc_property(struct property *prop)
38ab519a01SNathan Fontenot {
39ab519a01SNathan Fontenot 	kfree(prop->name);
40ab519a01SNathan Fontenot 	kfree(prop->value);
41ab519a01SNathan Fontenot 	kfree(prop);
42ab519a01SNathan Fontenot }
43ab519a01SNathan Fontenot 
44ab519a01SNathan Fontenot static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
45ab519a01SNathan Fontenot {
46ab519a01SNathan Fontenot 	struct property *prop;
47ab519a01SNathan Fontenot 	char *name;
48ab519a01SNathan Fontenot 	char *value;
49ab519a01SNathan Fontenot 
50ab519a01SNathan Fontenot 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
51ab519a01SNathan Fontenot 	if (!prop)
52ab519a01SNathan Fontenot 		return NULL;
53ab519a01SNathan Fontenot 
54d6f1e7abSBharata B Rao 	name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
55ab519a01SNathan Fontenot 	prop->name = kstrdup(name, GFP_KERNEL);
56ab519a01SNathan Fontenot 
57d6f1e7abSBharata B Rao 	prop->length = be32_to_cpu(ccwa->prop_length);
58d6f1e7abSBharata B Rao 	value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
59e72ed6b5SNishanth Aravamudan 	prop->value = kmemdup(value, prop->length, GFP_KERNEL);
60ab519a01SNathan Fontenot 	if (!prop->value) {
61ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
62ab519a01SNathan Fontenot 		return NULL;
63ab519a01SNathan Fontenot 	}
64ab519a01SNathan Fontenot 
65ab519a01SNathan Fontenot 	return prop;
66ab519a01SNathan Fontenot }
67ab519a01SNathan Fontenot 
688d5ff320STyrel Datwyler static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa,
698d5ff320STyrel Datwyler 					       const char *path)
70ab519a01SNathan Fontenot {
71ab519a01SNathan Fontenot 	struct device_node *dn;
72ab519a01SNathan Fontenot 	char *name;
73ab519a01SNathan Fontenot 
748d5ff320STyrel Datwyler 	/* If parent node path is "/" advance path to NULL terminator to
758d5ff320STyrel Datwyler 	 * prevent double leading slashs in full_name.
768d5ff320STyrel Datwyler 	 */
778d5ff320STyrel Datwyler 	if (!path[1])
788d5ff320STyrel Datwyler 		path++;
798d5ff320STyrel Datwyler 
80ab519a01SNathan Fontenot 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
81ab519a01SNathan Fontenot 	if (!dn)
82ab519a01SNathan Fontenot 		return NULL;
83ab519a01SNathan Fontenot 
84d6f1e7abSBharata B Rao 	name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
858d5ff320STyrel Datwyler 	dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
86ab519a01SNathan Fontenot 	if (!dn->full_name) {
87ab519a01SNathan Fontenot 		kfree(dn);
88ab519a01SNathan Fontenot 		return NULL;
89ab519a01SNathan Fontenot 	}
90ab519a01SNathan Fontenot 
911578cb76STyrel Datwyler 	of_node_set_flag(dn, OF_DYNAMIC);
9297a9a717STyrel Datwyler 	of_node_init(dn);
931578cb76STyrel Datwyler 
94ab519a01SNathan Fontenot 	return dn;
95ab519a01SNathan Fontenot }
96ab519a01SNathan Fontenot 
97ab519a01SNathan Fontenot static void dlpar_free_one_cc_node(struct device_node *dn)
98ab519a01SNathan Fontenot {
99ab519a01SNathan Fontenot 	struct property *prop;
100ab519a01SNathan Fontenot 
101ab519a01SNathan Fontenot 	while (dn->properties) {
102ab519a01SNathan Fontenot 		prop = dn->properties;
103ab519a01SNathan Fontenot 		dn->properties = prop->next;
104ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
105ab519a01SNathan Fontenot 	}
106ab519a01SNathan Fontenot 
107ab519a01SNathan Fontenot 	kfree(dn->full_name);
108ab519a01SNathan Fontenot 	kfree(dn);
109ab519a01SNathan Fontenot }
110ab519a01SNathan Fontenot 
11120648974SNathan Fontenot void dlpar_free_cc_nodes(struct device_node *dn)
112ab519a01SNathan Fontenot {
113ab519a01SNathan Fontenot 	if (dn->child)
114ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->child);
115ab519a01SNathan Fontenot 
116ab519a01SNathan Fontenot 	if (dn->sibling)
117ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->sibling);
118ab519a01SNathan Fontenot 
119ab519a01SNathan Fontenot 	dlpar_free_one_cc_node(dn);
120ab519a01SNathan Fontenot }
121ab519a01SNathan Fontenot 
1229c740025SAnton Blanchard #define COMPLETE	0
123ab519a01SNathan Fontenot #define NEXT_SIBLING    1
124ab519a01SNathan Fontenot #define NEXT_CHILD      2
125ab519a01SNathan Fontenot #define NEXT_PROPERTY   3
126ab519a01SNathan Fontenot #define PREV_PARENT     4
127ab519a01SNathan Fontenot #define MORE_MEMORY     5
128ab519a01SNathan Fontenot #define CALL_AGAIN	-2
129ab519a01SNathan Fontenot #define ERR_CFG_USE     -9003
130ab519a01SNathan Fontenot 
131d6f1e7abSBharata B Rao struct device_node *dlpar_configure_connector(__be32 drc_index,
1328d5ff320STyrel Datwyler 					      struct device_node *parent)
133ab519a01SNathan Fontenot {
134ab519a01SNathan Fontenot 	struct device_node *dn;
135ab519a01SNathan Fontenot 	struct device_node *first_dn = NULL;
136ab519a01SNathan Fontenot 	struct device_node *last_dn = NULL;
137ab519a01SNathan Fontenot 	struct property *property;
138ab519a01SNathan Fontenot 	struct property *last_property = NULL;
139ab519a01SNathan Fontenot 	struct cc_workarea *ccwa;
14093f68f1eSNathan Fontenot 	char *data_buf;
1418d5ff320STyrel Datwyler 	const char *parent_path = parent->full_name;
142ab519a01SNathan Fontenot 	int cc_token;
14393f68f1eSNathan Fontenot 	int rc = -1;
144ab519a01SNathan Fontenot 
145ab519a01SNathan Fontenot 	cc_token = rtas_token("ibm,configure-connector");
146ab519a01SNathan Fontenot 	if (cc_token == RTAS_UNKNOWN_SERVICE)
147ab519a01SNathan Fontenot 		return NULL;
148ab519a01SNathan Fontenot 
14993f68f1eSNathan Fontenot 	data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
15093f68f1eSNathan Fontenot 	if (!data_buf)
15193f68f1eSNathan Fontenot 		return NULL;
15293f68f1eSNathan Fontenot 
15393f68f1eSNathan Fontenot 	ccwa = (struct cc_workarea *)&data_buf[0];
154ab519a01SNathan Fontenot 	ccwa->drc_index = drc_index;
155ab519a01SNathan Fontenot 	ccwa->zero = 0;
156ab519a01SNathan Fontenot 
15793f68f1eSNathan Fontenot 	do {
15893f68f1eSNathan Fontenot 		/* Since we release the rtas_data_buf lock between configure
15993f68f1eSNathan Fontenot 		 * connector calls we want to re-populate the rtas_data_buffer
16093f68f1eSNathan Fontenot 		 * with the contents of the previous call.
16193f68f1eSNathan Fontenot 		 */
16293f68f1eSNathan Fontenot 		spin_lock(&rtas_data_buf_lock);
16393f68f1eSNathan Fontenot 
16493f68f1eSNathan Fontenot 		memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
165ab519a01SNathan Fontenot 		rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
16693f68f1eSNathan Fontenot 		memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
16793f68f1eSNathan Fontenot 
16893f68f1eSNathan Fontenot 		spin_unlock(&rtas_data_buf_lock);
16993f68f1eSNathan Fontenot 
170ab519a01SNathan Fontenot 		switch (rc) {
1719c740025SAnton Blanchard 		case COMPLETE:
1729c740025SAnton Blanchard 			break;
1739c740025SAnton Blanchard 
174ab519a01SNathan Fontenot 		case NEXT_SIBLING:
1758d5ff320STyrel Datwyler 			dn = dlpar_parse_cc_node(ccwa, parent_path);
176ab519a01SNathan Fontenot 			if (!dn)
177ab519a01SNathan Fontenot 				goto cc_error;
178ab519a01SNathan Fontenot 
179ab519a01SNathan Fontenot 			dn->parent = last_dn->parent;
180ab519a01SNathan Fontenot 			last_dn->sibling = dn;
181ab519a01SNathan Fontenot 			last_dn = dn;
182ab519a01SNathan Fontenot 			break;
183ab519a01SNathan Fontenot 
184ab519a01SNathan Fontenot 		case NEXT_CHILD:
1858d5ff320STyrel Datwyler 			if (first_dn)
1868d5ff320STyrel Datwyler 				parent_path = last_dn->full_name;
1878d5ff320STyrel Datwyler 
1888d5ff320STyrel Datwyler 			dn = dlpar_parse_cc_node(ccwa, parent_path);
189ab519a01SNathan Fontenot 			if (!dn)
190ab519a01SNathan Fontenot 				goto cc_error;
191ab519a01SNathan Fontenot 
1928d5ff320STyrel Datwyler 			if (!first_dn) {
1938d5ff320STyrel Datwyler 				dn->parent = parent;
194ab519a01SNathan Fontenot 				first_dn = dn;
1958d5ff320STyrel Datwyler 			} else {
196ab519a01SNathan Fontenot 				dn->parent = last_dn;
197ab519a01SNathan Fontenot 				if (last_dn)
198ab519a01SNathan Fontenot 					last_dn->child = dn;
199ab519a01SNathan Fontenot 			}
200ab519a01SNathan Fontenot 
201ab519a01SNathan Fontenot 			last_dn = dn;
202ab519a01SNathan Fontenot 			break;
203ab519a01SNathan Fontenot 
204ab519a01SNathan Fontenot 		case NEXT_PROPERTY:
205ab519a01SNathan Fontenot 			property = dlpar_parse_cc_property(ccwa);
206ab519a01SNathan Fontenot 			if (!property)
207ab519a01SNathan Fontenot 				goto cc_error;
208ab519a01SNathan Fontenot 
209ab519a01SNathan Fontenot 			if (!last_dn->properties)
210ab519a01SNathan Fontenot 				last_dn->properties = property;
211ab519a01SNathan Fontenot 			else
212ab519a01SNathan Fontenot 				last_property->next = property;
213ab519a01SNathan Fontenot 
214ab519a01SNathan Fontenot 			last_property = property;
215ab519a01SNathan Fontenot 			break;
216ab519a01SNathan Fontenot 
217ab519a01SNathan Fontenot 		case PREV_PARENT:
218ab519a01SNathan Fontenot 			last_dn = last_dn->parent;
2198d5ff320STyrel Datwyler 			parent_path = last_dn->parent->full_name;
220ab519a01SNathan Fontenot 			break;
221ab519a01SNathan Fontenot 
222ab519a01SNathan Fontenot 		case CALL_AGAIN:
223ab519a01SNathan Fontenot 			break;
224ab519a01SNathan Fontenot 
225ab519a01SNathan Fontenot 		case MORE_MEMORY:
226ab519a01SNathan Fontenot 		case ERR_CFG_USE:
227ab519a01SNathan Fontenot 		default:
228ab519a01SNathan Fontenot 			printk(KERN_ERR "Unexpected Error (%d) "
229ab519a01SNathan Fontenot 			       "returned from configure-connector\n", rc);
230ab519a01SNathan Fontenot 			goto cc_error;
231ab519a01SNathan Fontenot 		}
23293f68f1eSNathan Fontenot 	} while (rc);
233ab519a01SNathan Fontenot 
234ab519a01SNathan Fontenot cc_error:
23593f68f1eSNathan Fontenot 	kfree(data_buf);
23693f68f1eSNathan Fontenot 
23793f68f1eSNathan Fontenot 	if (rc) {
238ab519a01SNathan Fontenot 		if (first_dn)
239ab519a01SNathan Fontenot 			dlpar_free_cc_nodes(first_dn);
24093f68f1eSNathan Fontenot 
241ab519a01SNathan Fontenot 		return NULL;
242ab519a01SNathan Fontenot 	}
243ab519a01SNathan Fontenot 
24493f68f1eSNathan Fontenot 	return first_dn;
24593f68f1eSNathan Fontenot }
24693f68f1eSNathan Fontenot 
247ab519a01SNathan Fontenot static struct device_node *derive_parent(const char *path)
248ab519a01SNathan Fontenot {
249ab519a01SNathan Fontenot 	struct device_node *parent;
250ab519a01SNathan Fontenot 	char *last_slash;
251ab519a01SNathan Fontenot 
252ab519a01SNathan Fontenot 	last_slash = strrchr(path, '/');
253ab519a01SNathan Fontenot 	if (last_slash == path) {
254ab519a01SNathan Fontenot 		parent = of_find_node_by_path("/");
255ab519a01SNathan Fontenot 	} else {
256ab519a01SNathan Fontenot 		char *parent_path;
257ab519a01SNathan Fontenot 		int parent_path_len = last_slash - path + 1;
258ab519a01SNathan Fontenot 		parent_path = kmalloc(parent_path_len, GFP_KERNEL);
259ab519a01SNathan Fontenot 		if (!parent_path)
260ab519a01SNathan Fontenot 			return NULL;
261ab519a01SNathan Fontenot 
262ab519a01SNathan Fontenot 		strlcpy(parent_path, path, parent_path_len);
263ab519a01SNathan Fontenot 		parent = of_find_node_by_path(parent_path);
264ab519a01SNathan Fontenot 		kfree(parent_path);
265ab519a01SNathan Fontenot 	}
266ab519a01SNathan Fontenot 
267ab519a01SNathan Fontenot 	return parent;
268ab519a01SNathan Fontenot }
269ab519a01SNathan Fontenot 
270ab519a01SNathan Fontenot int dlpar_attach_node(struct device_node *dn)
271ab519a01SNathan Fontenot {
272ab519a01SNathan Fontenot 	int rc;
273ab519a01SNathan Fontenot 
274ab519a01SNathan Fontenot 	dn->parent = derive_parent(dn->full_name);
275ab519a01SNathan Fontenot 	if (!dn->parent)
276ab519a01SNathan Fontenot 		return -ENOMEM;
277ab519a01SNathan Fontenot 
2781cf3d8b3SNathan Fontenot 	rc = of_attach_node(dn);
2793aef19f0SAkinobu Mita 	if (rc) {
280ab519a01SNathan Fontenot 		printk(KERN_ERR "Failed to add device node %s\n",
281ab519a01SNathan Fontenot 		       dn->full_name);
2823aef19f0SAkinobu Mita 		return rc;
283ab519a01SNathan Fontenot 	}
284ab519a01SNathan Fontenot 
285ab519a01SNathan Fontenot 	of_node_put(dn->parent);
286ab519a01SNathan Fontenot 	return 0;
287ab519a01SNathan Fontenot }
288ab519a01SNathan Fontenot 
289ab519a01SNathan Fontenot int dlpar_detach_node(struct device_node *dn)
290ab519a01SNathan Fontenot {
2915935ff43STyrel Datwyler 	struct device_node *child;
2921cf3d8b3SNathan Fontenot 	int rc;
293ab519a01SNathan Fontenot 
2945935ff43STyrel Datwyler 	child = of_get_next_child(dn, NULL);
2955935ff43STyrel Datwyler 	while (child) {
2965935ff43STyrel Datwyler 		dlpar_detach_node(child);
2975935ff43STyrel Datwyler 		child = of_get_next_child(dn, child);
2985935ff43STyrel Datwyler 	}
2995935ff43STyrel Datwyler 
3001cf3d8b3SNathan Fontenot 	rc = of_detach_node(dn);
3011cf3d8b3SNathan Fontenot 	if (rc)
3021cf3d8b3SNathan Fontenot 		return rc;
3031cf3d8b3SNathan Fontenot 
3041cf3d8b3SNathan Fontenot 	of_node_put(dn); /* Must decrement the refcount */
305ab519a01SNathan Fontenot 	return 0;
306ab519a01SNathan Fontenot }
307ab519a01SNathan Fontenot 
308ab519a01SNathan Fontenot #define DR_ENTITY_SENSE		9003
309ab519a01SNathan Fontenot #define DR_ENTITY_PRESENT	1
310ab519a01SNathan Fontenot #define DR_ENTITY_UNUSABLE	2
311ab519a01SNathan Fontenot #define ALLOCATION_STATE	9003
312ab519a01SNathan Fontenot #define ALLOC_UNUSABLE		0
313ab519a01SNathan Fontenot #define ALLOC_USABLE		1
314ab519a01SNathan Fontenot #define ISOLATION_STATE		9001
315ab519a01SNathan Fontenot #define ISOLATE			0
316ab519a01SNathan Fontenot #define UNISOLATE		1
317ab519a01SNathan Fontenot 
318ab519a01SNathan Fontenot int dlpar_acquire_drc(u32 drc_index)
319ab519a01SNathan Fontenot {
320ab519a01SNathan Fontenot 	int dr_status, rc;
321ab519a01SNathan Fontenot 
322ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
323ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
324ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_UNUSABLE)
325ab519a01SNathan Fontenot 		return -1;
326ab519a01SNathan Fontenot 
327ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
328ab519a01SNathan Fontenot 	if (rc)
329ab519a01SNathan Fontenot 		return rc;
330ab519a01SNathan Fontenot 
331ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
332ab519a01SNathan Fontenot 	if (rc) {
333ab519a01SNathan Fontenot 		rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
334ab519a01SNathan Fontenot 		return rc;
335ab519a01SNathan Fontenot 	}
336ab519a01SNathan Fontenot 
337ab519a01SNathan Fontenot 	return 0;
338ab519a01SNathan Fontenot }
339ab519a01SNathan Fontenot 
340ab519a01SNathan Fontenot int dlpar_release_drc(u32 drc_index)
341ab519a01SNathan Fontenot {
342ab519a01SNathan Fontenot 	int dr_status, rc;
343ab519a01SNathan Fontenot 
344ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
345ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
346ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_PRESENT)
347ab519a01SNathan Fontenot 		return -1;
348ab519a01SNathan Fontenot 
349ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
350ab519a01SNathan Fontenot 	if (rc)
351ab519a01SNathan Fontenot 		return rc;
352ab519a01SNathan Fontenot 
353ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
354ab519a01SNathan Fontenot 	if (rc) {
355ab519a01SNathan Fontenot 		rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
356ab519a01SNathan Fontenot 		return rc;
357ab519a01SNathan Fontenot 	}
358ab519a01SNathan Fontenot 
359ab519a01SNathan Fontenot 	return 0;
360ab519a01SNathan Fontenot }
361ab519a01SNathan Fontenot 
3621a8061c4SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
363ab519a01SNathan Fontenot 
364275a64f6SNathan Fontenot static int dlpar_online_cpu(struct device_node *dn)
365275a64f6SNathan Fontenot {
366275a64f6SNathan Fontenot 	int rc = 0;
367275a64f6SNathan Fontenot 	unsigned int cpu;
368275a64f6SNathan Fontenot 	int len, nthreads, i;
369822e7122SThomas Falcon 	const __be32 *intserv;
370822e7122SThomas Falcon 	u32 thread;
371275a64f6SNathan Fontenot 
372275a64f6SNathan Fontenot 	intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
373275a64f6SNathan Fontenot 	if (!intserv)
374275a64f6SNathan Fontenot 		return -EINVAL;
375275a64f6SNathan Fontenot 
376275a64f6SNathan Fontenot 	nthreads = len / sizeof(u32);
377275a64f6SNathan Fontenot 
378275a64f6SNathan Fontenot 	cpu_maps_update_begin();
379275a64f6SNathan Fontenot 	for (i = 0; i < nthreads; i++) {
380822e7122SThomas Falcon 		thread = be32_to_cpu(intserv[i]);
381275a64f6SNathan Fontenot 		for_each_present_cpu(cpu) {
382822e7122SThomas Falcon 			if (get_hard_smp_processor_id(cpu) != thread)
383275a64f6SNathan Fontenot 				continue;
384275a64f6SNathan Fontenot 			BUG_ON(get_cpu_current_state(cpu)
385275a64f6SNathan Fontenot 					!= CPU_STATE_OFFLINE);
386275a64f6SNathan Fontenot 			cpu_maps_update_done();
38710ccaf17SDan Streetman 			rc = device_online(get_cpu_device(cpu));
388275a64f6SNathan Fontenot 			if (rc)
389275a64f6SNathan Fontenot 				goto out;
390275a64f6SNathan Fontenot 			cpu_maps_update_begin();
391275a64f6SNathan Fontenot 
392275a64f6SNathan Fontenot 			break;
393275a64f6SNathan Fontenot 		}
394275a64f6SNathan Fontenot 		if (cpu == num_possible_cpus())
395275a64f6SNathan Fontenot 			printk(KERN_WARNING "Could not find cpu to online "
396822e7122SThomas Falcon 			       "with physical id 0x%x\n", thread);
397275a64f6SNathan Fontenot 	}
398275a64f6SNathan Fontenot 	cpu_maps_update_done();
399275a64f6SNathan Fontenot 
400275a64f6SNathan Fontenot out:
401275a64f6SNathan Fontenot 	return rc;
402275a64f6SNathan Fontenot 
403275a64f6SNathan Fontenot }
404275a64f6SNathan Fontenot 
4051a8061c4SNathan Fontenot static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
4061a8061c4SNathan Fontenot {
4078d5ff320STyrel Datwyler 	struct device_node *dn, *parent;
4081618bd53SDaniel Walter 	u32 drc_index;
4091a8061c4SNathan Fontenot 	int rc;
4101a8061c4SNathan Fontenot 
4111618bd53SDaniel Walter 	rc = kstrtou32(buf, 0, &drc_index);
4126dedcca6SToshi Kani 	if (rc)
4136dedcca6SToshi Kani 		return -EINVAL;
4141a8061c4SNathan Fontenot 
415f32393c9SNathan Fontenot 	rc = dlpar_acquire_drc(drc_index);
416f32393c9SNathan Fontenot 	if (rc)
417f32393c9SNathan Fontenot 		return -EINVAL;
418f32393c9SNathan Fontenot 
4198d5ff320STyrel Datwyler 	parent = of_find_node_by_path("/cpus");
4206dedcca6SToshi Kani 	if (!parent)
4216dedcca6SToshi Kani 		return -ENODEV;
4228d5ff320STyrel Datwyler 
423d6f1e7abSBharata B Rao 	dn = dlpar_configure_connector(cpu_to_be32(drc_index), parent);
424*2222ce0fSNathan Fontenot 	of_node_put(parent);
4256dedcca6SToshi Kani 	if (!dn)
4266dedcca6SToshi Kani 		return -EINVAL;
4271a8061c4SNathan Fontenot 
4281a8061c4SNathan Fontenot 	rc = dlpar_attach_node(dn);
4291a8061c4SNathan Fontenot 	if (rc) {
4301a8061c4SNathan Fontenot 		dlpar_release_drc(drc_index);
4311a8061c4SNathan Fontenot 		dlpar_free_cc_nodes(dn);
4326dedcca6SToshi Kani 		return rc;
4331a8061c4SNathan Fontenot 	}
4341a8061c4SNathan Fontenot 
435275a64f6SNathan Fontenot 	rc = dlpar_online_cpu(dn);
4366dedcca6SToshi Kani 	if (rc)
4376dedcca6SToshi Kani 		return rc;
438b6db63d1SGautham R Shenoy 
4396dedcca6SToshi Kani 	return count;
4401a8061c4SNathan Fontenot }
4411a8061c4SNathan Fontenot 
442275a64f6SNathan Fontenot static int dlpar_offline_cpu(struct device_node *dn)
443275a64f6SNathan Fontenot {
444275a64f6SNathan Fontenot 	int rc = 0;
445275a64f6SNathan Fontenot 	unsigned int cpu;
446275a64f6SNathan Fontenot 	int len, nthreads, i;
447e36d1227SThomas Falcon 	const __be32 *intserv;
448e36d1227SThomas Falcon 	u32 thread;
449275a64f6SNathan Fontenot 
450275a64f6SNathan Fontenot 	intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
451275a64f6SNathan Fontenot 	if (!intserv)
452275a64f6SNathan Fontenot 		return -EINVAL;
453275a64f6SNathan Fontenot 
454275a64f6SNathan Fontenot 	nthreads = len / sizeof(u32);
455275a64f6SNathan Fontenot 
456275a64f6SNathan Fontenot 	cpu_maps_update_begin();
457275a64f6SNathan Fontenot 	for (i = 0; i < nthreads; i++) {
458e36d1227SThomas Falcon 		thread = be32_to_cpu(intserv[i]);
459275a64f6SNathan Fontenot 		for_each_present_cpu(cpu) {
460e36d1227SThomas Falcon 			if (get_hard_smp_processor_id(cpu) != thread)
461275a64f6SNathan Fontenot 				continue;
462275a64f6SNathan Fontenot 
463275a64f6SNathan Fontenot 			if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
464275a64f6SNathan Fontenot 				break;
465275a64f6SNathan Fontenot 
466275a64f6SNathan Fontenot 			if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
467ceddee23SRobert Jennings 				set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
468275a64f6SNathan Fontenot 				cpu_maps_update_done();
46910ccaf17SDan Streetman 				rc = device_offline(get_cpu_device(cpu));
470275a64f6SNathan Fontenot 				if (rc)
471275a64f6SNathan Fontenot 					goto out;
472275a64f6SNathan Fontenot 				cpu_maps_update_begin();
473275a64f6SNathan Fontenot 				break;
474275a64f6SNathan Fontenot 
475275a64f6SNathan Fontenot 			}
476275a64f6SNathan Fontenot 
477275a64f6SNathan Fontenot 			/*
478275a64f6SNathan Fontenot 			 * The cpu is in CPU_STATE_INACTIVE.
479275a64f6SNathan Fontenot 			 * Upgrade it's state to CPU_STATE_OFFLINE.
480275a64f6SNathan Fontenot 			 */
481275a64f6SNathan Fontenot 			set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
482e36d1227SThomas Falcon 			BUG_ON(plpar_hcall_norets(H_PROD, thread)
483275a64f6SNathan Fontenot 								!= H_SUCCESS);
484275a64f6SNathan Fontenot 			__cpu_die(cpu);
485275a64f6SNathan Fontenot 			break;
486275a64f6SNathan Fontenot 		}
487275a64f6SNathan Fontenot 		if (cpu == num_possible_cpus())
488275a64f6SNathan Fontenot 			printk(KERN_WARNING "Could not find cpu to offline "
489e36d1227SThomas Falcon 			       "with physical id 0x%x\n", thread);
490275a64f6SNathan Fontenot 	}
491275a64f6SNathan Fontenot 	cpu_maps_update_done();
492275a64f6SNathan Fontenot 
493275a64f6SNathan Fontenot out:
494275a64f6SNathan Fontenot 	return rc;
495275a64f6SNathan Fontenot 
496275a64f6SNathan Fontenot }
497275a64f6SNathan Fontenot 
4981a8061c4SNathan Fontenot static ssize_t dlpar_cpu_release(const char *buf, size_t count)
4991a8061c4SNathan Fontenot {
5001a8061c4SNathan Fontenot 	struct device_node *dn;
501e36d1227SThomas Falcon 	u32 drc_index;
5021a8061c4SNathan Fontenot 	int rc;
5031a8061c4SNathan Fontenot 
5041a8061c4SNathan Fontenot 	dn = of_find_node_by_path(buf);
5051a8061c4SNathan Fontenot 	if (!dn)
5061a8061c4SNathan Fontenot 		return -EINVAL;
5071a8061c4SNathan Fontenot 
508e36d1227SThomas Falcon 	rc = of_property_read_u32(dn, "ibm,my-drc-index", &drc_index);
509e36d1227SThomas Falcon 	if (rc) {
5101a8061c4SNathan Fontenot 		of_node_put(dn);
5111a8061c4SNathan Fontenot 		return -EINVAL;
5121a8061c4SNathan Fontenot 	}
5131a8061c4SNathan Fontenot 
514275a64f6SNathan Fontenot 	rc = dlpar_offline_cpu(dn);
515b6db63d1SGautham R Shenoy 	if (rc) {
516b6db63d1SGautham R Shenoy 		of_node_put(dn);
5176dedcca6SToshi Kani 		return -EINVAL;
518b6db63d1SGautham R Shenoy 	}
519b6db63d1SGautham R Shenoy 
520e36d1227SThomas Falcon 	rc = dlpar_release_drc(drc_index);
5211a8061c4SNathan Fontenot 	if (rc) {
5221a8061c4SNathan Fontenot 		of_node_put(dn);
5236dedcca6SToshi Kani 		return rc;
5241a8061c4SNathan Fontenot 	}
5251a8061c4SNathan Fontenot 
5261a8061c4SNathan Fontenot 	rc = dlpar_detach_node(dn);
5271a8061c4SNathan Fontenot 	if (rc) {
528e36d1227SThomas Falcon 		dlpar_acquire_drc(drc_index);
5296dedcca6SToshi Kani 		return rc;
5301a8061c4SNathan Fontenot 	}
5311a8061c4SNathan Fontenot 
5321a8061c4SNathan Fontenot 	of_node_put(dn);
5336dedcca6SToshi Kani 
5346dedcca6SToshi Kani 	return count;
5351a8061c4SNathan Fontenot }
5361a8061c4SNathan Fontenot 
537999e2dadSNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
538999e2dadSNathan Fontenot 
539999e2dadSNathan Fontenot static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
540999e2dadSNathan Fontenot {
541999e2dadSNathan Fontenot 	int rc;
542999e2dadSNathan Fontenot 
543999e2dadSNathan Fontenot 	/* pseries error logs are in BE format, convert to cpu type */
544999e2dadSNathan Fontenot 	switch (hp_elog->id_type) {
545999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_COUNT:
546999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_count =
547999e2dadSNathan Fontenot 					be32_to_cpu(hp_elog->_drc_u.drc_count);
548999e2dadSNathan Fontenot 		break;
549999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_INDEX:
550999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_index =
551999e2dadSNathan Fontenot 					be32_to_cpu(hp_elog->_drc_u.drc_index);
552999e2dadSNathan Fontenot 	}
553999e2dadSNathan Fontenot 
554999e2dadSNathan Fontenot 	switch (hp_elog->resource) {
555999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_MEM:
556999e2dadSNathan Fontenot 		rc = dlpar_memory(hp_elog);
557999e2dadSNathan Fontenot 		break;
558999e2dadSNathan Fontenot 	default:
559999e2dadSNathan Fontenot 		pr_warn_ratelimited("Invalid resource (%d) specified\n",
560999e2dadSNathan Fontenot 				    hp_elog->resource);
561999e2dadSNathan Fontenot 		rc = -EINVAL;
562999e2dadSNathan Fontenot 	}
563999e2dadSNathan Fontenot 
564999e2dadSNathan Fontenot 	return rc;
565999e2dadSNathan Fontenot }
566999e2dadSNathan Fontenot 
567999e2dadSNathan Fontenot static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
568999e2dadSNathan Fontenot 			   const char *buf, size_t count)
569999e2dadSNathan Fontenot {
570999e2dadSNathan Fontenot 	struct pseries_hp_errorlog *hp_elog;
571999e2dadSNathan Fontenot 	const char *arg;
572999e2dadSNathan Fontenot 	int rc;
573999e2dadSNathan Fontenot 
574999e2dadSNathan Fontenot 	hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
575999e2dadSNathan Fontenot 	if (!hp_elog) {
576999e2dadSNathan Fontenot 		rc = -ENOMEM;
577999e2dadSNathan Fontenot 		goto dlpar_store_out;
578999e2dadSNathan Fontenot 	}
579999e2dadSNathan Fontenot 
580999e2dadSNathan Fontenot 	/* Parse out the request from the user, this will be in the form
581999e2dadSNathan Fontenot 	 * <resource> <action> <id_type> <id>
582999e2dadSNathan Fontenot 	 */
583999e2dadSNathan Fontenot 	arg = buf;
584999e2dadSNathan Fontenot 	if (!strncmp(arg, "memory", 6)) {
585999e2dadSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
586999e2dadSNathan Fontenot 		arg += strlen("memory ");
587999e2dadSNathan Fontenot 	} else {
588999e2dadSNathan Fontenot 		pr_err("Invalid resource specified: \"%s\"\n", buf);
589999e2dadSNathan Fontenot 		rc = -EINVAL;
590999e2dadSNathan Fontenot 		goto dlpar_store_out;
591999e2dadSNathan Fontenot 	}
592999e2dadSNathan Fontenot 
593999e2dadSNathan Fontenot 	if (!strncmp(arg, "add", 3)) {
594999e2dadSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
595999e2dadSNathan Fontenot 		arg += strlen("add ");
596999e2dadSNathan Fontenot 	} else if (!strncmp(arg, "remove", 6)) {
597999e2dadSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
598999e2dadSNathan Fontenot 		arg += strlen("remove ");
599999e2dadSNathan Fontenot 	} else {
600999e2dadSNathan Fontenot 		pr_err("Invalid action specified: \"%s\"\n", buf);
601999e2dadSNathan Fontenot 		rc = -EINVAL;
602999e2dadSNathan Fontenot 		goto dlpar_store_out;
603999e2dadSNathan Fontenot 	}
604999e2dadSNathan Fontenot 
605999e2dadSNathan Fontenot 	if (!strncmp(arg, "index", 5)) {
606999e2dadSNathan Fontenot 		u32 index;
607999e2dadSNathan Fontenot 
608999e2dadSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
609999e2dadSNathan Fontenot 		arg += strlen("index ");
610999e2dadSNathan Fontenot 		if (kstrtou32(arg, 0, &index)) {
611999e2dadSNathan Fontenot 			rc = -EINVAL;
612999e2dadSNathan Fontenot 			pr_err("Invalid drc_index specified: \"%s\"\n", buf);
613999e2dadSNathan Fontenot 			goto dlpar_store_out;
614999e2dadSNathan Fontenot 		}
615999e2dadSNathan Fontenot 
616999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_index = cpu_to_be32(index);
617999e2dadSNathan Fontenot 	} else if (!strncmp(arg, "count", 5)) {
618999e2dadSNathan Fontenot 		u32 count;
619999e2dadSNathan Fontenot 
620999e2dadSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
621999e2dadSNathan Fontenot 		arg += strlen("count ");
622999e2dadSNathan Fontenot 		if (kstrtou32(arg, 0, &count)) {
623999e2dadSNathan Fontenot 			rc = -EINVAL;
624999e2dadSNathan Fontenot 			pr_err("Invalid count specified: \"%s\"\n", buf);
625999e2dadSNathan Fontenot 			goto dlpar_store_out;
626999e2dadSNathan Fontenot 		}
627999e2dadSNathan Fontenot 
628999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_count = cpu_to_be32(count);
629999e2dadSNathan Fontenot 	} else {
630999e2dadSNathan Fontenot 		pr_err("Invalid id_type specified: \"%s\"\n", buf);
631999e2dadSNathan Fontenot 		rc = -EINVAL;
632999e2dadSNathan Fontenot 		goto dlpar_store_out;
633999e2dadSNathan Fontenot 	}
634999e2dadSNathan Fontenot 
635999e2dadSNathan Fontenot 	rc = handle_dlpar_errorlog(hp_elog);
636999e2dadSNathan Fontenot 
637999e2dadSNathan Fontenot dlpar_store_out:
638999e2dadSNathan Fontenot 	kfree(hp_elog);
639999e2dadSNathan Fontenot 	return rc ? rc : count;
640999e2dadSNathan Fontenot }
641999e2dadSNathan Fontenot 
642999e2dadSNathan Fontenot static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
643999e2dadSNathan Fontenot 
6441a8061c4SNathan Fontenot static int __init pseries_dlpar_init(void)
6451a8061c4SNathan Fontenot {
646999e2dadSNathan Fontenot 	int rc;
647999e2dadSNathan Fontenot 
648999e2dadSNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
6491a8061c4SNathan Fontenot 	ppc_md.cpu_probe = dlpar_cpu_probe;
6501a8061c4SNathan Fontenot 	ppc_md.cpu_release = dlpar_cpu_release;
651999e2dadSNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
6521a8061c4SNathan Fontenot 
653999e2dadSNathan Fontenot 	rc = sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
654999e2dadSNathan Fontenot 
655999e2dadSNathan Fontenot 	return rc;
6561a8061c4SNathan Fontenot }
6571a8061c4SNathan Fontenot machine_device_initcall(pseries, pseries_dlpar_init);
6581a8061c4SNathan Fontenot 
659