]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - arch/powerpc/platforms/pseries/reconfig.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-focal-kernel.git] / arch / powerpc / platforms / pseries / reconfig.c
1 /*
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>
18 #include <linux/slab.h>
19
20 #include <asm/prom.h>
21 #include <asm/machdep.h>
22 #include <asm/uaccess.h>
23 #include <asm/pSeries_reconfig.h>
24 #include <asm/mmu.h>
25
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 */
35 static 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
44 static 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 }
53 if (np->pde)
54 remove_proc_entry(np->pde->name, parent->pde);
55 }
56 #else /* !CONFIG_PROC_DEVICETREE */
57 static void add_node_proc_entries(struct device_node *np)
58 {
59 return;
60 }
61
62 static 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 */
76 static 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
100 BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);
101
102 int pSeries_reconfig_notifier_register(struct notifier_block *nb)
103 {
104 return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);
105 }
106
107 void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
108 {
109 blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
110 }
111
112 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
113 {
114 struct device_node *np;
115 int err = -ENOMEM;
116
117 np = kzalloc(sizeof(*np), GFP_KERNEL);
118 if (!np)
119 goto out_err;
120
121 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
122 if (!np->full_name)
123 goto out_err;
124
125 strcpy(np->full_name, path);
126
127 np->properties = proplist;
128 of_node_set_flag(np, OF_DYNAMIC);
129 kref_init(&np->kref);
130
131 np->parent = derive_parent(path);
132 if (IS_ERR(np->parent)) {
133 err = PTR_ERR(np->parent);
134 goto out_err;
135 }
136
137 err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
138 PSERIES_RECONFIG_ADD, np);
139 if (err == NOTIFY_BAD) {
140 printk(KERN_ERR "Failed to add device node %s\n", path);
141 err = -ENOMEM; /* For now, safe to assume kmalloc failure */
142 goto out_err;
143 }
144
145 of_attach_node(np);
146
147 add_node_proc_entries(np);
148
149 of_node_put(np->parent);
150
151 return 0;
152
153 out_err:
154 if (np) {
155 of_node_put(np->parent);
156 kfree(np->full_name);
157 kfree(np);
158 }
159 return err;
160 }
161
162 static int pSeries_reconfig_remove_node(struct device_node *np)
163 {
164 struct device_node *parent, *child;
165
166 parent = of_get_parent(np);
167 if (!parent)
168 return -EINVAL;
169
170 if ((child = of_get_next_child(np, NULL))) {
171 of_node_put(child);
172 of_node_put(parent);
173 return -EBUSY;
174 }
175
176 remove_node_proc_entries(np);
177
178 blocking_notifier_call_chain(&pSeries_reconfig_chain,
179 PSERIES_RECONFIG_REMOVE, np);
180 of_detach_node(np);
181
182 of_node_put(parent);
183 of_node_put(np); /* Must decrement the refcount */
184 return 0;
185 }
186
187 /*
188 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
189 * OF device nodes. Should be deprecated as soon as we get an
190 * in-kernel wrapper for the RTAS ibm,configure-connector call.
191 */
192
193 static void release_prop_list(const struct property *prop)
194 {
195 struct property *next;
196 for (; prop; prop = next) {
197 next = prop->next;
198 kfree(prop->name);
199 kfree(prop->value);
200 kfree(prop);
201 }
202
203 }
204
205 /**
206 * parse_next_property - process the next property from raw input buffer
207 * @buf: input buffer, must be nul-terminated
208 * @end: end of the input buffer + 1, for validation
209 * @name: return value; set to property name in buf
210 * @length: return value; set to length of value
211 * @value: return value; set to the property value in buf
212 *
213 * Note that the caller must make copies of the name and value returned,
214 * this function does no allocation or copying of the data. Return value
215 * is set to the next name in buf, or NULL on error.
216 */
217 static char * parse_next_property(char *buf, char *end, char **name, int *length,
218 unsigned char **value)
219 {
220 char *tmp;
221
222 *name = buf;
223
224 tmp = strchr(buf, ' ');
225 if (!tmp) {
226 printk(KERN_ERR "property parse failed in %s at line %d\n",
227 __func__, __LINE__);
228 return NULL;
229 }
230 *tmp = '\0';
231
232 if (++tmp >= end) {
233 printk(KERN_ERR "property parse failed in %s at line %d\n",
234 __func__, __LINE__);
235 return NULL;
236 }
237
238 /* now we're on the length */
239 *length = -1;
240 *length = simple_strtoul(tmp, &tmp, 10);
241 if (*length == -1) {
242 printk(KERN_ERR "property parse failed in %s at line %d\n",
243 __func__, __LINE__);
244 return NULL;
245 }
246 if (*tmp != ' ' || ++tmp >= end) {
247 printk(KERN_ERR "property parse failed in %s at line %d\n",
248 __func__, __LINE__);
249 return NULL;
250 }
251
252 /* now we're on the value */
253 *value = tmp;
254 tmp += *length;
255 if (tmp > end) {
256 printk(KERN_ERR "property parse failed in %s at line %d\n",
257 __func__, __LINE__);
258 return NULL;
259 }
260 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
261 printk(KERN_ERR "property parse failed in %s at line %d\n",
262 __func__, __LINE__);
263 return NULL;
264 }
265 tmp++;
266
267 /* and now we should be on the next name, or the end */
268 return tmp;
269 }
270
271 static struct property *new_property(const char *name, const int length,
272 const unsigned char *value, struct property *last)
273 {
274 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
275
276 if (!new)
277 return NULL;
278
279 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
280 goto cleanup;
281 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
282 goto cleanup;
283
284 strcpy(new->name, name);
285 memcpy(new->value, value, length);
286 *(((char *)new->value) + length) = 0;
287 new->length = length;
288 new->next = last;
289 return new;
290
291 cleanup:
292 kfree(new->name);
293 kfree(new->value);
294 kfree(new);
295 return NULL;
296 }
297
298 static int do_add_node(char *buf, size_t bufsize)
299 {
300 char *path, *end, *name;
301 struct device_node *np;
302 struct property *prop = NULL;
303 unsigned char* value;
304 int length, rv = 0;
305
306 end = buf + bufsize;
307 path = buf;
308 buf = strchr(buf, ' ');
309 if (!buf)
310 return -EINVAL;
311 *buf = '\0';
312 buf++;
313
314 if ((np = of_find_node_by_path(path))) {
315 of_node_put(np);
316 return -EINVAL;
317 }
318
319 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
320 while (buf < end &&
321 (buf = parse_next_property(buf, end, &name, &length, &value))) {
322 struct property *last = prop;
323
324 prop = new_property(name, length, value, last);
325 if (!prop) {
326 rv = -ENOMEM;
327 prop = last;
328 goto out;
329 }
330 }
331 if (!buf) {
332 rv = -EINVAL;
333 goto out;
334 }
335
336 rv = pSeries_reconfig_add_node(path, prop);
337
338 out:
339 if (rv)
340 release_prop_list(prop);
341 return rv;
342 }
343
344 static int do_remove_node(char *buf)
345 {
346 struct device_node *node;
347 int rv = -ENODEV;
348
349 if ((node = of_find_node_by_path(buf)))
350 rv = pSeries_reconfig_remove_node(node);
351
352 of_node_put(node);
353 return rv;
354 }
355
356 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
357 {
358 char *handle_str;
359 phandle handle;
360 *npp = NULL;
361
362 handle_str = buf;
363
364 buf = strchr(buf, ' ');
365 if (!buf)
366 return NULL;
367 *buf = '\0';
368 buf++;
369
370 handle = simple_strtoul(handle_str, NULL, 0);
371
372 *npp = of_find_node_by_phandle(handle);
373 return buf;
374 }
375
376 static int do_add_property(char *buf, size_t bufsize)
377 {
378 struct property *prop = NULL;
379 struct device_node *np;
380 unsigned char *value;
381 char *name, *end;
382 int length;
383 end = buf + bufsize;
384 buf = parse_node(buf, bufsize, &np);
385
386 if (!np)
387 return -ENODEV;
388
389 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
390 return -EINVAL;
391
392 prop = new_property(name, length, value, NULL);
393 if (!prop)
394 return -ENOMEM;
395
396 prom_add_property(np, prop);
397
398 return 0;
399 }
400
401 static int do_remove_property(char *buf, size_t bufsize)
402 {
403 struct device_node *np;
404 char *tmp;
405 struct property *prop;
406 buf = parse_node(buf, bufsize, &np);
407
408 if (!np)
409 return -ENODEV;
410
411 tmp = strchr(buf,' ');
412 if (tmp)
413 *tmp = '\0';
414
415 if (strlen(buf) == 0)
416 return -EINVAL;
417
418 prop = of_find_property(np, buf, NULL);
419
420 return prom_remove_property(np, prop);
421 }
422
423 static int do_update_property(char *buf, size_t bufsize)
424 {
425 struct device_node *np;
426 unsigned char *value;
427 char *name, *end, *next_prop;
428 int rc, length;
429 struct property *newprop, *oldprop;
430 buf = parse_node(buf, bufsize, &np);
431 end = buf + bufsize;
432
433 if (!np)
434 return -ENODEV;
435
436 next_prop = parse_next_property(buf, end, &name, &length, &value);
437 if (!next_prop)
438 return -EINVAL;
439
440 newprop = new_property(name, length, value, NULL);
441 if (!newprop)
442 return -ENOMEM;
443
444 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
445 slb_set_size(*(int *)value);
446
447 oldprop = of_find_property(np, name,NULL);
448 if (!oldprop) {
449 if (strlen(name))
450 return prom_add_property(np, newprop);
451 return -ENODEV;
452 }
453
454 rc = prom_update_property(np, newprop, oldprop);
455 if (rc)
456 return rc;
457
458 /* For memory under the ibm,dynamic-reconfiguration-memory node
459 * of the device tree, adding and removing memory is just an update
460 * to the ibm,dynamic-memory property instead of adding/removing a
461 * memory node in the device tree. For these cases we still need to
462 * involve the notifier chain.
463 */
464 if (!strcmp(name, "ibm,dynamic-memory")) {
465 int action;
466
467 next_prop = parse_next_property(next_prop, end, &name,
468 &length, &value);
469 if (!next_prop)
470 return -EINVAL;
471
472 if (!strcmp(name, "add"))
473 action = PSERIES_DRCONF_MEM_ADD;
474 else
475 action = PSERIES_DRCONF_MEM_REMOVE;
476
477 rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,
478 action, value);
479 if (rc == NOTIFY_BAD) {
480 rc = prom_update_property(np, oldprop, newprop);
481 return -ENOMEM;
482 }
483 }
484
485 return 0;
486 }
487
488 /**
489 * ofdt_write - perform operations on the Open Firmware device tree
490 *
491 * @file: not used
492 * @buf: command and arguments
493 * @count: size of the command buffer
494 * @off: not used
495 *
496 * Operations supported at this time are addition and removal of
497 * whole nodes along with their properties. Operations on individual
498 * properties are not implemented (yet).
499 */
500 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
501 loff_t *off)
502 {
503 int rv = 0;
504 char *kbuf;
505 char *tmp;
506
507 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
508 rv = -ENOMEM;
509 goto out;
510 }
511 if (copy_from_user(kbuf, buf, count)) {
512 rv = -EFAULT;
513 goto out;
514 }
515
516 kbuf[count] = '\0';
517
518 tmp = strchr(kbuf, ' ');
519 if (!tmp) {
520 rv = -EINVAL;
521 goto out;
522 }
523 *tmp = '\0';
524 tmp++;
525
526 if (!strcmp(kbuf, "add_node"))
527 rv = do_add_node(tmp, count - (tmp - kbuf));
528 else if (!strcmp(kbuf, "remove_node"))
529 rv = do_remove_node(tmp);
530 else if (!strcmp(kbuf, "add_property"))
531 rv = do_add_property(tmp, count - (tmp - kbuf));
532 else if (!strcmp(kbuf, "remove_property"))
533 rv = do_remove_property(tmp, count - (tmp - kbuf));
534 else if (!strcmp(kbuf, "update_property"))
535 rv = do_update_property(tmp, count - (tmp - kbuf));
536 else
537 rv = -EINVAL;
538 out:
539 kfree(kbuf);
540 return rv ? rv : count;
541 }
542
543 static const struct file_operations ofdt_fops = {
544 .write = ofdt_write
545 };
546
547 /* create /proc/powerpc/ofdt write-only by root */
548 static int proc_ppc64_create_ofdt(void)
549 {
550 struct proc_dir_entry *ent;
551
552 if (!machine_is(pseries))
553 return 0;
554
555 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
556 if (ent)
557 ent->size = 0;
558
559 return 0;
560 }
561 __initcall(proc_ppc64_create_ofdt);