blob: 168651acdd83e003dae22f9bdee6147e89c1b1f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms).
4 *
5 * Copyright (C) 2005 Nathan Lynch
6 * Copyright (C) 2005 IBM Corporation
7 *
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/kref.h>
16#include <linux/notifier.h>
17#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110021#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110023#include <asm/pSeries_reconfig.h>
Brian King46db2f82009-08-28 12:06:29 +000024#include <asm/mmu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26
27
28/*
29 * Routines for "runtime" addition and removal of device tree nodes.
30 */
31#ifdef CONFIG_PROC_DEVICETREE
32/*
33 * Add a node to /proc/device-tree.
34 */
35static void add_node_proc_entries(struct device_node *np)
36{
37 struct proc_dir_entry *ent;
38
39 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
40 if (ent)
41 proc_device_tree_add_node(np, ent);
42}
43
44static void remove_node_proc_entries(struct device_node *np)
45{
46 struct property *pp = np->properties;
47 struct device_node *parent = np->parent;
48
49 while (pp) {
50 remove_proc_entry(pp->name, np->pde);
51 pp = pp->next;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (np->pde)
54 remove_proc_entry(np->pde->name, parent->pde);
55}
56#else /* !CONFIG_PROC_DEVICETREE */
57static void add_node_proc_entries(struct device_node *np)
58{
59 return;
60}
61
62static void remove_node_proc_entries(struct device_node *np)
63{
64 return;
65}
66#endif /* CONFIG_PROC_DEVICETREE */
67
68/**
69 * derive_parent - basically like dirname(1)
70 * @path: the full_name of a node to be added to the tree
71 *
72 * Returns the node which should be the parent of the node
73 * described by path. E.g., for path = "/foo/bar", returns
74 * the node with full_name = "/foo".
75 */
76static struct device_node *derive_parent(const char *path)
77{
78 struct device_node *parent = NULL;
79 char *parent_path = "/";
80 size_t parent_path_len = strrchr(path, '/') - path + 1;
81
82 /* reject if path is "/" */
83 if (!strcmp(path, "/"))
84 return ERR_PTR(-EINVAL);
85
86 if (strrchr(path, '/') != path) {
87 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
88 if (!parent_path)
89 return ERR_PTR(-ENOMEM);
90 strlcpy(parent_path, path, parent_path_len);
91 }
92 parent = of_find_node_by_path(parent_path);
93 if (!parent)
94 return ERR_PTR(-EINVAL);
95 if (strcmp(parent_path, "/"))
96 kfree(parent_path);
97 return parent;
98}
99
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000100static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102int pSeries_reconfig_notifier_register(struct notifier_block *nb)
103{
Alan Sterne041c682006-03-27 01:16:30 -0800104 return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
108{
Alan Sterne041c682006-03-27 01:16:30 -0800109 blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000112int pSeries_reconfig_notify(unsigned long action, void *p)
113{
114 int err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
115 action, p);
116
Akinobu Mitade2780a2011-06-21 03:35:56 +0000117 return notifier_to_errno(err);
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
121{
122 struct device_node *np;
123 int err = -ENOMEM;
124
Pekka Enberg874ca6c2005-09-06 15:18:32 -0700125 np = kzalloc(sizeof(*np), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!np)
127 goto out_err;
128
Julia Lawall74052172010-05-14 09:30:13 +0000129 np->full_name = kstrdup(path, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (!np->full_name)
131 goto out_err;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 np->properties = proplist;
Michael Ellermand3b814b2007-06-19 16:07:58 +1000134 of_node_set_flag(np, OF_DYNAMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 kref_init(&np->kref);
136
137 np->parent = derive_parent(path);
138 if (IS_ERR(np->parent)) {
139 err = PTR_ERR(np->parent);
140 goto out_err;
141 }
142
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000143 err = pSeries_reconfig_notify(PSERIES_RECONFIG_ADD, np);
144 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 printk(KERN_ERR "Failed to add device node %s\n", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto out_err;
147 }
148
149 of_attach_node(np);
150
151 add_node_proc_entries(np);
152
153 of_node_put(np->parent);
154
155 return 0;
156
157out_err:
158 if (np) {
159 of_node_put(np->parent);
160 kfree(np->full_name);
161 kfree(np);
162 }
163 return err;
164}
165
166static int pSeries_reconfig_remove_node(struct device_node *np)
167{
168 struct device_node *parent, *child;
169
170 parent = of_get_parent(np);
171 if (!parent)
172 return -EINVAL;
173
174 if ((child = of_get_next_child(np, NULL))) {
175 of_node_put(child);
Julia Lawall842decb2008-02-04 23:34:58 -0800176 of_node_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -EBUSY;
178 }
179
180 remove_node_proc_entries(np);
181
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000182 pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 of_detach_node(np);
184
185 of_node_put(parent);
186 of_node_put(np); /* Must decrement the refcount */
187 return 0;
188}
189
190/*
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000191 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * OF device nodes. Should be deprecated as soon as we get an
193 * in-kernel wrapper for the RTAS ibm,configure-connector call.
194 */
195
196static void release_prop_list(const struct property *prop)
197{
198 struct property *next;
199 for (; prop; prop = next) {
200 next = prop->next;
201 kfree(prop->name);
202 kfree(prop->value);
203 kfree(prop);
204 }
205
206}
207
208/**
209 * parse_next_property - process the next property from raw input buffer
210 * @buf: input buffer, must be nul-terminated
211 * @end: end of the input buffer + 1, for validation
212 * @name: return value; set to property name in buf
213 * @length: return value; set to length of value
214 * @value: return value; set to the property value in buf
215 *
216 * Note that the caller must make copies of the name and value returned,
217 * this function does no allocation or copying of the data. Return value
218 * is set to the next name in buf, or NULL on error.
219 */
220static char * parse_next_property(char *buf, char *end, char **name, int *length,
221 unsigned char **value)
222{
223 char *tmp;
224
225 *name = buf;
226
227 tmp = strchr(buf, ' ');
228 if (!tmp) {
229 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100230 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return NULL;
232 }
233 *tmp = '\0';
234
235 if (++tmp >= end) {
236 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100237 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return NULL;
239 }
240
241 /* now we're on the length */
242 *length = -1;
243 *length = simple_strtoul(tmp, &tmp, 10);
244 if (*length == -1) {
245 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100246 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return NULL;
248 }
249 if (*tmp != ' ' || ++tmp >= end) {
250 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100251 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return NULL;
253 }
254
255 /* now we're on the value */
256 *value = tmp;
257 tmp += *length;
258 if (tmp > end) {
259 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100260 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return NULL;
262 }
263 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
264 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100265 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return NULL;
267 }
268 tmp++;
269
270 /* and now we should be on the next name, or the end */
271 return tmp;
272}
273
274static struct property *new_property(const char *name, const int length,
275 const unsigned char *value, struct property *last)
276{
Yan Burmanf8485352006-12-02 13:26:57 +0200277 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!new)
280 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
283 goto cleanup;
284 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
285 goto cleanup;
286
287 strcpy(new->name, name);
288 memcpy(new->value, value, length);
289 *(((char *)new->value) + length) = 0;
290 new->length = length;
291 new->next = last;
292 return new;
293
294cleanup:
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800295 kfree(new->name);
296 kfree(new->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 kfree(new);
298 return NULL;
299}
300
301static int do_add_node(char *buf, size_t bufsize)
302{
303 char *path, *end, *name;
304 struct device_node *np;
305 struct property *prop = NULL;
306 unsigned char* value;
307 int length, rv = 0;
308
309 end = buf + bufsize;
310 path = buf;
311 buf = strchr(buf, ' ');
312 if (!buf)
313 return -EINVAL;
314 *buf = '\0';
315 buf++;
316
317 if ((np = of_find_node_by_path(path))) {
318 of_node_put(np);
319 return -EINVAL;
320 }
321
322 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
323 while (buf < end &&
324 (buf = parse_next_property(buf, end, &name, &length, &value))) {
325 struct property *last = prop;
326
327 prop = new_property(name, length, value, last);
328 if (!prop) {
329 rv = -ENOMEM;
330 prop = last;
331 goto out;
332 }
333 }
334 if (!buf) {
335 rv = -EINVAL;
336 goto out;
337 }
338
339 rv = pSeries_reconfig_add_node(path, prop);
340
341out:
342 if (rv)
343 release_prop_list(prop);
344 return rv;
345}
346
347static int do_remove_node(char *buf)
348{
349 struct device_node *node;
350 int rv = -ENODEV;
351
352 if ((node = of_find_node_by_path(buf)))
353 rv = pSeries_reconfig_remove_node(node);
354
355 of_node_put(node);
356 return rv;
357}
358
Dave C Boutcher610d9152006-01-12 16:10:22 -0600359static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
360{
361 char *handle_str;
362 phandle handle;
363 *npp = NULL;
364
365 handle_str = buf;
366
367 buf = strchr(buf, ' ');
368 if (!buf)
369 return NULL;
370 *buf = '\0';
371 buf++;
372
Nathan Fontenot4b6e8052008-07-03 13:19:24 +1000373 handle = simple_strtoul(handle_str, NULL, 0);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600374
375 *npp = of_find_node_by_phandle(handle);
376 return buf;
377}
378
379static int do_add_property(char *buf, size_t bufsize)
380{
381 struct property *prop = NULL;
382 struct device_node *np;
383 unsigned char *value;
384 char *name, *end;
385 int length;
386 end = buf + bufsize;
387 buf = parse_node(buf, bufsize, &np);
388
389 if (!np)
390 return -ENODEV;
391
392 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
393 return -EINVAL;
394
395 prop = new_property(name, length, value, NULL);
396 if (!prop)
397 return -ENOMEM;
398
399 prom_add_property(np, prop);
400
401 return 0;
402}
403
404static int do_remove_property(char *buf, size_t bufsize)
405{
406 struct device_node *np;
407 char *tmp;
408 struct property *prop;
409 buf = parse_node(buf, bufsize, &np);
410
411 if (!np)
412 return -ENODEV;
413
414 tmp = strchr(buf,' ');
415 if (tmp)
416 *tmp = '\0';
417
418 if (strlen(buf) == 0)
419 return -EINVAL;
420
421 prop = of_find_property(np, buf, NULL);
422
423 return prom_remove_property(np, prop);
424}
425
426static int do_update_property(char *buf, size_t bufsize)
427{
428 struct device_node *np;
429 unsigned char *value;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000430 char *name, *end, *next_prop;
431 int rc, length;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600432 struct property *newprop, *oldprop;
433 buf = parse_node(buf, bufsize, &np);
434 end = buf + bufsize;
435
436 if (!np)
437 return -ENODEV;
438
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000439 next_prop = parse_next_property(buf, end, &name, &length, &value);
440 if (!next_prop)
Dave C Boutcher610d9152006-01-12 16:10:22 -0600441 return -EINVAL;
442
443 newprop = new_property(name, length, value, NULL);
444 if (!newprop)
445 return -ENOMEM;
446
Brian King46db2f82009-08-28 12:06:29 +0000447 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
448 slb_set_size(*(int *)value);
449
Dave C Boutcher610d9152006-01-12 16:10:22 -0600450 oldprop = of_find_property(np, name,NULL);
Brian King46db2f82009-08-28 12:06:29 +0000451 if (!oldprop) {
452 if (strlen(name))
453 return prom_add_property(np, newprop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600454 return -ENODEV;
Brian King46db2f82009-08-28 12:06:29 +0000455 }
Dave C Boutcher610d9152006-01-12 16:10:22 -0600456
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000457 rc = prom_update_property(np, newprop, oldprop);
458 if (rc)
459 return rc;
460
461 /* For memory under the ibm,dynamic-reconfiguration-memory node
462 * of the device tree, adding and removing memory is just an update
463 * to the ibm,dynamic-memory property instead of adding/removing a
464 * memory node in the device tree. For these cases we still need to
465 * involve the notifier chain.
466 */
467 if (!strcmp(name, "ibm,dynamic-memory")) {
468 int action;
469
470 next_prop = parse_next_property(next_prop, end, &name,
471 &length, &value);
472 if (!next_prop)
473 return -EINVAL;
474
475 if (!strcmp(name, "add"))
476 action = PSERIES_DRCONF_MEM_ADD;
477 else
478 action = PSERIES_DRCONF_MEM_REMOVE;
479
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000480 rc = pSeries_reconfig_notify(action, value);
481 if (rc) {
482 prom_update_property(np, oldprop, newprop);
483 return rc;
Nathan Fontenotc5785f92009-03-09 00:00:00 +0000484 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000485 }
486
Nathan Fontenotc5785f92009-03-09 00:00:00 +0000487 return 0;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/**
491 * ofdt_write - perform operations on the Open Firmware device tree
492 *
493 * @file: not used
494 * @buf: command and arguments
495 * @count: size of the command buffer
496 * @off: not used
497 *
498 * Operations supported at this time are addition and removal of
499 * whole nodes along with their properties. Operations on individual
500 * properties are not implemented (yet).
501 */
502static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
503 loff_t *off)
504{
505 int rv = 0;
506 char *kbuf;
507 char *tmp;
508
509 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
510 rv = -ENOMEM;
511 goto out;
512 }
513 if (copy_from_user(kbuf, buf, count)) {
514 rv = -EFAULT;
515 goto out;
516 }
517
518 kbuf[count] = '\0';
519
520 tmp = strchr(kbuf, ' ');
521 if (!tmp) {
522 rv = -EINVAL;
523 goto out;
524 }
525 *tmp = '\0';
526 tmp++;
527
528 if (!strcmp(kbuf, "add_node"))
529 rv = do_add_node(tmp, count - (tmp - kbuf));
530 else if (!strcmp(kbuf, "remove_node"))
531 rv = do_remove_node(tmp);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600532 else if (!strcmp(kbuf, "add_property"))
533 rv = do_add_property(tmp, count - (tmp - kbuf));
534 else if (!strcmp(kbuf, "remove_property"))
535 rv = do_remove_property(tmp, count - (tmp - kbuf));
536 else if (!strcmp(kbuf, "update_property"))
537 rv = do_update_property(tmp, count - (tmp - kbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 else
539 rv = -EINVAL;
540out:
541 kfree(kbuf);
542 return rv ? rv : count;
543}
544
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800545static const struct file_operations ofdt_fops = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200546 .write = ofdt_write,
547 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000550/* create /proc/powerpc/ofdt write-only by root */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static int proc_ppc64_create_ofdt(void)
552{
553 struct proc_dir_entry *ent;
554
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100555 if (!machine_is(pseries))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
557
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000558 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
Denis V. Lunev66747132008-04-29 01:02:26 -0700559 if (ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ent->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 return 0;
563}
564__initcall(proc_ppc64_create_ofdt);