blob: 2f4668136b200d9f22a25c7761749e7e512b315f [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}
Kent Yoder4726b7b2012-04-12 05:04:11 +0000106EXPORT_SYMBOL_GPL(pSeries_reconfig_notifier_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
109{
Alan Sterne041c682006-03-27 01:16:30 -0800110 blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Kent Yoder4726b7b2012-04-12 05:04:11 +0000112EXPORT_SYMBOL_GPL(pSeries_reconfig_notifier_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000114int pSeries_reconfig_notify(unsigned long action, void *p)
115{
116 int err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
117 action, p);
118
Akinobu Mitade2780a2011-06-21 03:35:56 +0000119 return notifier_to_errno(err);
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
123{
124 struct device_node *np;
125 int err = -ENOMEM;
126
Pekka Enberg874ca6c2005-09-06 15:18:32 -0700127 np = kzalloc(sizeof(*np), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!np)
129 goto out_err;
130
Julia Lawall74052172010-05-14 09:30:13 +0000131 np->full_name = kstrdup(path, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (!np->full_name)
133 goto out_err;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 np->properties = proplist;
Michael Ellermand3b814b2007-06-19 16:07:58 +1000136 of_node_set_flag(np, OF_DYNAMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 kref_init(&np->kref);
138
139 np->parent = derive_parent(path);
140 if (IS_ERR(np->parent)) {
141 err = PTR_ERR(np->parent);
142 goto out_err;
143 }
144
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000145 err = pSeries_reconfig_notify(PSERIES_RECONFIG_ADD, np);
146 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 printk(KERN_ERR "Failed to add device node %s\n", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto out_err;
149 }
150
151 of_attach_node(np);
152
153 add_node_proc_entries(np);
154
155 of_node_put(np->parent);
156
157 return 0;
158
159out_err:
160 if (np) {
161 of_node_put(np->parent);
162 kfree(np->full_name);
163 kfree(np);
164 }
165 return err;
166}
167
168static int pSeries_reconfig_remove_node(struct device_node *np)
169{
170 struct device_node *parent, *child;
171
172 parent = of_get_parent(np);
173 if (!parent)
174 return -EINVAL;
175
176 if ((child = of_get_next_child(np, NULL))) {
177 of_node_put(child);
Julia Lawall842decb2008-02-04 23:34:58 -0800178 of_node_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EBUSY;
180 }
181
182 remove_node_proc_entries(np);
183
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000184 pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 of_detach_node(np);
186
187 of_node_put(parent);
188 of_node_put(np); /* Must decrement the refcount */
189 return 0;
190}
191
192/*
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000193 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * OF device nodes. Should be deprecated as soon as we get an
195 * in-kernel wrapper for the RTAS ibm,configure-connector call.
196 */
197
198static void release_prop_list(const struct property *prop)
199{
200 struct property *next;
201 for (; prop; prop = next) {
202 next = prop->next;
203 kfree(prop->name);
204 kfree(prop->value);
205 kfree(prop);
206 }
207
208}
209
210/**
211 * parse_next_property - process the next property from raw input buffer
212 * @buf: input buffer, must be nul-terminated
213 * @end: end of the input buffer + 1, for validation
214 * @name: return value; set to property name in buf
215 * @length: return value; set to length of value
216 * @value: return value; set to the property value in buf
217 *
218 * Note that the caller must make copies of the name and value returned,
219 * this function does no allocation or copying of the data. Return value
220 * is set to the next name in buf, or NULL on error.
221 */
222static char * parse_next_property(char *buf, char *end, char **name, int *length,
223 unsigned char **value)
224{
225 char *tmp;
226
227 *name = buf;
228
229 tmp = strchr(buf, ' ');
230 if (!tmp) {
231 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100232 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return NULL;
234 }
235 *tmp = '\0';
236
237 if (++tmp >= end) {
238 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100239 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return NULL;
241 }
242
243 /* now we're on the length */
244 *length = -1;
245 *length = simple_strtoul(tmp, &tmp, 10);
246 if (*length == -1) {
247 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100248 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return NULL;
250 }
251 if (*tmp != ' ' || ++tmp >= end) {
252 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100253 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return NULL;
255 }
256
257 /* now we're on the value */
258 *value = tmp;
259 tmp += *length;
260 if (tmp > end) {
261 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100262 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return NULL;
264 }
265 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
266 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100267 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return NULL;
269 }
270 tmp++;
271
272 /* and now we should be on the next name, or the end */
273 return tmp;
274}
275
276static struct property *new_property(const char *name, const int length,
277 const unsigned char *value, struct property *last)
278{
Yan Burmanf8485352006-12-02 13:26:57 +0200279 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (!new)
282 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Grant Likelyc22618a2012-11-14 22:37:12 +0000284 if (!(new->name = kstrdup(name, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto cleanup;
286 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
287 goto cleanup;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 memcpy(new->value, value, length);
290 *(((char *)new->value) + length) = 0;
291 new->length = length;
292 new->next = last;
293 return new;
294
295cleanup:
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800296 kfree(new->name);
297 kfree(new->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 kfree(new);
299 return NULL;
300}
301
302static int do_add_node(char *buf, size_t bufsize)
303{
304 char *path, *end, *name;
305 struct device_node *np;
306 struct property *prop = NULL;
307 unsigned char* value;
308 int length, rv = 0;
309
310 end = buf + bufsize;
311 path = buf;
312 buf = strchr(buf, ' ');
313 if (!buf)
314 return -EINVAL;
315 *buf = '\0';
316 buf++;
317
318 if ((np = of_find_node_by_path(path))) {
319 of_node_put(np);
320 return -EINVAL;
321 }
322
323 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
324 while (buf < end &&
325 (buf = parse_next_property(buf, end, &name, &length, &value))) {
326 struct property *last = prop;
327
328 prop = new_property(name, length, value, last);
329 if (!prop) {
330 rv = -ENOMEM;
331 prop = last;
332 goto out;
333 }
334 }
335 if (!buf) {
336 rv = -EINVAL;
337 goto out;
338 }
339
340 rv = pSeries_reconfig_add_node(path, prop);
341
342out:
343 if (rv)
344 release_prop_list(prop);
345 return rv;
346}
347
348static int do_remove_node(char *buf)
349{
350 struct device_node *node;
351 int rv = -ENODEV;
352
353 if ((node = of_find_node_by_path(buf)))
354 rv = pSeries_reconfig_remove_node(node);
355
356 of_node_put(node);
357 return rv;
358}
359
Dave C Boutcher610d9152006-01-12 16:10:22 -0600360static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
361{
362 char *handle_str;
363 phandle handle;
364 *npp = NULL;
365
366 handle_str = buf;
367
368 buf = strchr(buf, ' ');
369 if (!buf)
370 return NULL;
371 *buf = '\0';
372 buf++;
373
Nathan Fontenot4b6e8052008-07-03 13:19:24 +1000374 handle = simple_strtoul(handle_str, NULL, 0);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600375
376 *npp = of_find_node_by_phandle(handle);
377 return buf;
378}
379
380static int do_add_property(char *buf, size_t bufsize)
381{
382 struct property *prop = NULL;
383 struct device_node *np;
384 unsigned char *value;
385 char *name, *end;
386 int length;
387 end = buf + bufsize;
388 buf = parse_node(buf, bufsize, &np);
389
390 if (!np)
391 return -ENODEV;
392
393 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
394 return -EINVAL;
395
396 prop = new_property(name, length, value, NULL);
397 if (!prop)
398 return -ENOMEM;
399
400 prom_add_property(np, prop);
401
402 return 0;
403}
404
405static int do_remove_property(char *buf, size_t bufsize)
406{
407 struct device_node *np;
408 char *tmp;
409 struct property *prop;
410 buf = parse_node(buf, bufsize, &np);
411
412 if (!np)
413 return -ENODEV;
414
415 tmp = strchr(buf,' ');
416 if (tmp)
417 *tmp = '\0';
418
419 if (strlen(buf) == 0)
420 return -EINVAL;
421
422 prop = of_find_property(np, buf, NULL);
423
424 return prom_remove_property(np, prop);
425}
426
427static int do_update_property(char *buf, size_t bufsize)
428{
429 struct device_node *np;
Kent Yoder4726b7b2012-04-12 05:04:11 +0000430 struct pSeries_reconfig_prop_update upd_value;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600431 unsigned char *value;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000432 char *name, *end, *next_prop;
433 int rc, length;
Dong Aisheng475d0092012-07-11 15:16:37 +1000434 struct property *newprop;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600435 buf = parse_node(buf, bufsize, &np);
436 end = buf + bufsize;
437
438 if (!np)
439 return -ENODEV;
440
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000441 next_prop = parse_next_property(buf, end, &name, &length, &value);
442 if (!next_prop)
Dave C Boutcher610d9152006-01-12 16:10:22 -0600443 return -EINVAL;
444
Dong Aisheng475d0092012-07-11 15:16:37 +1000445 if (!strlen(name))
446 return -ENODEV;
447
Dave C Boutcher610d9152006-01-12 16:10:22 -0600448 newprop = new_property(name, length, value, NULL);
449 if (!newprop)
450 return -ENOMEM;
451
Brian King46db2f82009-08-28 12:06:29 +0000452 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
453 slb_set_size(*(int *)value);
454
Kent Yoder4726b7b2012-04-12 05:04:11 +0000455 upd_value.node = np;
456 upd_value.property = newprop;
457 pSeries_reconfig_notify(PSERIES_UPDATE_PROPERTY, &upd_value);
458
Dong Aisheng475d0092012-07-11 15:16:37 +1000459 rc = prom_update_property(np, newprop);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000460 if (rc)
461 return rc;
462
463 /* For memory under the ibm,dynamic-reconfiguration-memory node
464 * of the device tree, adding and removing memory is just an update
465 * to the ibm,dynamic-memory property instead of adding/removing a
466 * memory node in the device tree. For these cases we still need to
467 * involve the notifier chain.
468 */
469 if (!strcmp(name, "ibm,dynamic-memory")) {
470 int action;
471
472 next_prop = parse_next_property(next_prop, end, &name,
473 &length, &value);
474 if (!next_prop)
475 return -EINVAL;
476
477 if (!strcmp(name, "add"))
478 action = PSERIES_DRCONF_MEM_ADD;
479 else
480 action = PSERIES_DRCONF_MEM_REMOVE;
481
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000482 rc = pSeries_reconfig_notify(action, value);
483 if (rc) {
Dong Aisheng475d0092012-07-11 15:16:37 +1000484 prom_update_property(np, newprop);
Akinobu Mita3aef19f2011-06-21 03:35:55 +0000485 return rc;
Nathan Fontenotc5785f92009-03-09 00:00:00 +0000486 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000487 }
488
Nathan Fontenotc5785f92009-03-09 00:00:00 +0000489 return 0;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/**
493 * ofdt_write - perform operations on the Open Firmware device tree
494 *
495 * @file: not used
496 * @buf: command and arguments
497 * @count: size of the command buffer
498 * @off: not used
499 *
500 * Operations supported at this time are addition and removal of
501 * whole nodes along with their properties. Operations on individual
502 * properties are not implemented (yet).
503 */
504static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
505 loff_t *off)
506{
507 int rv = 0;
508 char *kbuf;
509 char *tmp;
510
511 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
512 rv = -ENOMEM;
513 goto out;
514 }
515 if (copy_from_user(kbuf, buf, count)) {
516 rv = -EFAULT;
517 goto out;
518 }
519
520 kbuf[count] = '\0';
521
522 tmp = strchr(kbuf, ' ');
523 if (!tmp) {
524 rv = -EINVAL;
525 goto out;
526 }
527 *tmp = '\0';
528 tmp++;
529
530 if (!strcmp(kbuf, "add_node"))
531 rv = do_add_node(tmp, count - (tmp - kbuf));
532 else if (!strcmp(kbuf, "remove_node"))
533 rv = do_remove_node(tmp);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600534 else if (!strcmp(kbuf, "add_property"))
535 rv = do_add_property(tmp, count - (tmp - kbuf));
536 else if (!strcmp(kbuf, "remove_property"))
537 rv = do_remove_property(tmp, count - (tmp - kbuf));
538 else if (!strcmp(kbuf, "update_property"))
539 rv = do_update_property(tmp, count - (tmp - kbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 else
541 rv = -EINVAL;
542out:
543 kfree(kbuf);
544 return rv ? rv : count;
545}
546
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800547static const struct file_operations ofdt_fops = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200548 .write = ofdt_write,
549 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550};
551
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000552/* create /proc/powerpc/ofdt write-only by root */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553static int proc_ppc64_create_ofdt(void)
554{
555 struct proc_dir_entry *ent;
556
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100557 if (!machine_is(pseries))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000560 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
Denis V. Lunev66747132008-04-29 01:02:26 -0700561 if (ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ent->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 return 0;
565}
566__initcall(proc_ppc64_create_ofdt);