dlpar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Support for dynamic reconfiguration for PCI, Memory, and CPU
  3. * Hotplug and Dynamic Logical Partitioning on RPA platforms.
  4. *
  5. * Copyright (C) 2009 Nathan Fontenot
  6. * Copyright (C) 2009 IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/kref.h>
  14. #include <linux/notifier.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/cpu.h>
  18. #include <linux/slab.h>
  19. #include "offline_states.h"
  20. #include <asm/prom.h>
  21. #include <asm/machdep.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/rtas.h>
  24. #include <asm/pSeries_reconfig.h>
  25. struct cc_workarea {
  26. u32 drc_index;
  27. u32 zero;
  28. u32 name_offset;
  29. u32 prop_length;
  30. u32 prop_offset;
  31. };
  32. static void dlpar_free_cc_property(struct property *prop)
  33. {
  34. kfree(prop->name);
  35. kfree(prop->value);
  36. kfree(prop);
  37. }
  38. static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
  39. {
  40. struct property *prop;
  41. char *name;
  42. char *value;
  43. prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  44. if (!prop)
  45. return NULL;
  46. name = (char *)ccwa + ccwa->name_offset;
  47. prop->name = kstrdup(name, GFP_KERNEL);
  48. prop->length = ccwa->prop_length;
  49. value = (char *)ccwa + ccwa->prop_offset;
  50. prop->value = kzalloc(prop->length, GFP_KERNEL);
  51. if (!prop->value) {
  52. dlpar_free_cc_property(prop);
  53. return NULL;
  54. }
  55. memcpy(prop->value, value, prop->length);
  56. return prop;
  57. }
  58. static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
  59. {
  60. struct device_node *dn;
  61. char *name;
  62. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  63. if (!dn)
  64. return NULL;
  65. /* The configure connector reported name does not contain a
  66. * preceeding '/', so we allocate a buffer large enough to
  67. * prepend this to the full_name.
  68. */
  69. name = (char *)ccwa + ccwa->name_offset;
  70. dn->full_name = kmalloc(strlen(name) + 2, GFP_KERNEL);
  71. if (!dn->full_name) {
  72. kfree(dn);
  73. return NULL;
  74. }
  75. sprintf(dn->full_name, "/%s", name);
  76. return dn;
  77. }
  78. static void dlpar_free_one_cc_node(struct device_node *dn)
  79. {
  80. struct property *prop;
  81. while (dn->properties) {
  82. prop = dn->properties;
  83. dn->properties = prop->next;
  84. dlpar_free_cc_property(prop);
  85. }
  86. kfree(dn->full_name);
  87. kfree(dn);
  88. }
  89. static void dlpar_free_cc_nodes(struct device_node *dn)
  90. {
  91. if (dn->child)
  92. dlpar_free_cc_nodes(dn->child);
  93. if (dn->sibling)
  94. dlpar_free_cc_nodes(dn->sibling);
  95. dlpar_free_one_cc_node(dn);
  96. }
  97. #define NEXT_SIBLING 1
  98. #define NEXT_CHILD 2
  99. #define NEXT_PROPERTY 3
  100. #define PREV_PARENT 4
  101. #define MORE_MEMORY 5
  102. #define CALL_AGAIN -2
  103. #define ERR_CFG_USE -9003
  104. struct device_node *dlpar_configure_connector(u32 drc_index)
  105. {
  106. struct device_node *dn;
  107. struct device_node *first_dn = NULL;
  108. struct device_node *last_dn = NULL;
  109. struct property *property;
  110. struct property *last_property = NULL;
  111. struct cc_workarea *ccwa;
  112. int cc_token;
  113. int rc;
  114. cc_token = rtas_token("ibm,configure-connector");
  115. if (cc_token == RTAS_UNKNOWN_SERVICE)
  116. return NULL;
  117. spin_lock(&rtas_data_buf_lock);
  118. ccwa = (struct cc_workarea *)&rtas_data_buf[0];
  119. ccwa->drc_index = drc_index;
  120. ccwa->zero = 0;
  121. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  122. while (rc) {
  123. switch (rc) {
  124. case NEXT_SIBLING:
  125. dn = dlpar_parse_cc_node(ccwa);
  126. if (!dn)
  127. goto cc_error;
  128. dn->parent = last_dn->parent;
  129. last_dn->sibling = dn;
  130. last_dn = dn;
  131. break;
  132. case NEXT_CHILD:
  133. dn = dlpar_parse_cc_node(ccwa);
  134. if (!dn)
  135. goto cc_error;
  136. if (!first_dn)
  137. first_dn = dn;
  138. else {
  139. dn->parent = last_dn;
  140. if (last_dn)
  141. last_dn->child = dn;
  142. }
  143. last_dn = dn;
  144. break;
  145. case NEXT_PROPERTY:
  146. property = dlpar_parse_cc_property(ccwa);
  147. if (!property)
  148. goto cc_error;
  149. if (!last_dn->properties)
  150. last_dn->properties = property;
  151. else
  152. last_property->next = property;
  153. last_property = property;
  154. break;
  155. case PREV_PARENT:
  156. last_dn = last_dn->parent;
  157. break;
  158. case CALL_AGAIN:
  159. break;
  160. case MORE_MEMORY:
  161. case ERR_CFG_USE:
  162. default:
  163. printk(KERN_ERR "Unexpected Error (%d) "
  164. "returned from configure-connector\n", rc);
  165. goto cc_error;
  166. }
  167. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  168. }
  169. spin_unlock(&rtas_data_buf_lock);
  170. return first_dn;
  171. cc_error:
  172. if (first_dn)
  173. dlpar_free_cc_nodes(first_dn);
  174. spin_unlock(&rtas_data_buf_lock);
  175. return NULL;
  176. }
  177. static struct device_node *derive_parent(const char *path)
  178. {
  179. struct device_node *parent;
  180. char *last_slash;
  181. last_slash = strrchr(path, '/');
  182. if (last_slash == path) {
  183. parent = of_find_node_by_path("/");
  184. } else {
  185. char *parent_path;
  186. int parent_path_len = last_slash - path + 1;
  187. parent_path = kmalloc(parent_path_len, GFP_KERNEL);
  188. if (!parent_path)
  189. return NULL;
  190. strlcpy(parent_path, path, parent_path_len);
  191. parent = of_find_node_by_path(parent_path);
  192. kfree(parent_path);
  193. }
  194. return parent;
  195. }
  196. int dlpar_attach_node(struct device_node *dn)
  197. {
  198. #ifdef CONFIG_PROC_DEVICETREE
  199. struct proc_dir_entry *ent;
  200. #endif
  201. int rc;
  202. of_node_set_flag(dn, OF_DYNAMIC);
  203. kref_init(&dn->kref);
  204. dn->parent = derive_parent(dn->full_name);
  205. if (!dn->parent)
  206. return -ENOMEM;
  207. rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,
  208. PSERIES_RECONFIG_ADD, dn);
  209. if (rc == NOTIFY_BAD) {
  210. printk(KERN_ERR "Failed to add device node %s\n",
  211. dn->full_name);
  212. return -ENOMEM; /* For now, safe to assume kmalloc failure */
  213. }
  214. of_attach_node(dn);
  215. #ifdef CONFIG_PROC_DEVICETREE
  216. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  217. if (ent)
  218. proc_device_tree_add_node(dn, ent);
  219. #endif
  220. of_node_put(dn->parent);
  221. return 0;
  222. }
  223. int dlpar_detach_node(struct device_node *dn)
  224. {
  225. #ifdef CONFIG_PROC_DEVICETREE
  226. struct device_node *parent = dn->parent;
  227. struct property *prop = dn->properties;
  228. while (prop) {
  229. remove_proc_entry(prop->name, dn->pde);
  230. prop = prop->next;
  231. }
  232. if (dn->pde)
  233. remove_proc_entry(dn->pde->name, parent->pde);
  234. #endif
  235. blocking_notifier_call_chain(&pSeries_reconfig_chain,
  236. PSERIES_RECONFIG_REMOVE, dn);
  237. of_detach_node(dn);
  238. of_node_put(dn); /* Must decrement the refcount */
  239. return 0;
  240. }
  241. #define DR_ENTITY_SENSE 9003
  242. #define DR_ENTITY_PRESENT 1
  243. #define DR_ENTITY_UNUSABLE 2
  244. #define ALLOCATION_STATE 9003
  245. #define ALLOC_UNUSABLE 0
  246. #define ALLOC_USABLE 1
  247. #define ISOLATION_STATE 9001
  248. #define ISOLATE 0
  249. #define UNISOLATE 1
  250. int dlpar_acquire_drc(u32 drc_index)
  251. {
  252. int dr_status, rc;
  253. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  254. DR_ENTITY_SENSE, drc_index);
  255. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  256. return -1;
  257. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  258. if (rc)
  259. return rc;
  260. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  261. if (rc) {
  262. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  263. return rc;
  264. }
  265. return 0;
  266. }
  267. int dlpar_release_drc(u32 drc_index)
  268. {
  269. int dr_status, rc;
  270. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  271. DR_ENTITY_SENSE, drc_index);
  272. if (rc || dr_status != DR_ENTITY_PRESENT)
  273. return -1;
  274. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  275. if (rc)
  276. return rc;
  277. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  278. if (rc) {
  279. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  280. return rc;
  281. }
  282. return 0;
  283. }
  284. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  285. static int dlpar_online_cpu(struct device_node *dn)
  286. {
  287. int rc = 0;
  288. unsigned int cpu;
  289. int len, nthreads, i;
  290. const u32 *intserv;
  291. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  292. if (!intserv)
  293. return -EINVAL;
  294. nthreads = len / sizeof(u32);
  295. cpu_maps_update_begin();
  296. for (i = 0; i < nthreads; i++) {
  297. for_each_present_cpu(cpu) {
  298. if (get_hard_smp_processor_id(cpu) != intserv[i])
  299. continue;
  300. BUG_ON(get_cpu_current_state(cpu)
  301. != CPU_STATE_OFFLINE);
  302. cpu_maps_update_done();
  303. rc = cpu_up(cpu);
  304. if (rc)
  305. goto out;
  306. cpu_maps_update_begin();
  307. break;
  308. }
  309. if (cpu == num_possible_cpus())
  310. printk(KERN_WARNING "Could not find cpu to online "
  311. "with physical id 0x%x\n", intserv[i]);
  312. }
  313. cpu_maps_update_done();
  314. out:
  315. return rc;
  316. }
  317. static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
  318. {
  319. struct device_node *dn;
  320. unsigned long drc_index;
  321. char *cpu_name;
  322. int rc;
  323. cpu_hotplug_driver_lock();
  324. rc = strict_strtoul(buf, 0, &drc_index);
  325. if (rc) {
  326. rc = -EINVAL;
  327. goto out;
  328. }
  329. dn = dlpar_configure_connector(drc_index);
  330. if (!dn) {
  331. rc = -EINVAL;
  332. goto out;
  333. }
  334. /* configure-connector reports cpus as living in the base
  335. * directory of the device tree. CPUs actually live in the
  336. * cpus directory so we need to fixup the full_name.
  337. */
  338. cpu_name = kzalloc(strlen(dn->full_name) + strlen("/cpus") + 1,
  339. GFP_KERNEL);
  340. if (!cpu_name) {
  341. dlpar_free_cc_nodes(dn);
  342. rc = -ENOMEM;
  343. goto out;
  344. }
  345. sprintf(cpu_name, "/cpus%s", dn->full_name);
  346. kfree(dn->full_name);
  347. dn->full_name = cpu_name;
  348. rc = dlpar_acquire_drc(drc_index);
  349. if (rc) {
  350. dlpar_free_cc_nodes(dn);
  351. rc = -EINVAL;
  352. goto out;
  353. }
  354. rc = dlpar_attach_node(dn);
  355. if (rc) {
  356. dlpar_release_drc(drc_index);
  357. dlpar_free_cc_nodes(dn);
  358. }
  359. rc = dlpar_online_cpu(dn);
  360. out:
  361. cpu_hotplug_driver_unlock();
  362. return rc ? rc : count;
  363. }
  364. static int dlpar_offline_cpu(struct device_node *dn)
  365. {
  366. int rc = 0;
  367. unsigned int cpu;
  368. int len, nthreads, i;
  369. const u32 *intserv;
  370. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  371. if (!intserv)
  372. return -EINVAL;
  373. nthreads = len / sizeof(u32);
  374. cpu_maps_update_begin();
  375. for (i = 0; i < nthreads; i++) {
  376. for_each_present_cpu(cpu) {
  377. if (get_hard_smp_processor_id(cpu) != intserv[i])
  378. continue;
  379. if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
  380. break;
  381. if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
  382. cpu_maps_update_done();
  383. rc = cpu_down(cpu);
  384. if (rc)
  385. goto out;
  386. cpu_maps_update_begin();
  387. break;
  388. }
  389. /*
  390. * The cpu is in CPU_STATE_INACTIVE.
  391. * Upgrade it's state to CPU_STATE_OFFLINE.
  392. */
  393. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  394. BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
  395. != H_SUCCESS);
  396. __cpu_die(cpu);
  397. break;
  398. }
  399. if (cpu == num_possible_cpus())
  400. printk(KERN_WARNING "Could not find cpu to offline "
  401. "with physical id 0x%x\n", intserv[i]);
  402. }
  403. cpu_maps_update_done();
  404. out:
  405. return rc;
  406. }
  407. static ssize_t dlpar_cpu_release(const char *buf, size_t count)
  408. {
  409. struct device_node *dn;
  410. const u32 *drc_index;
  411. int rc;
  412. dn = of_find_node_by_path(buf);
  413. if (!dn)
  414. return -EINVAL;
  415. drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
  416. if (!drc_index) {
  417. of_node_put(dn);
  418. return -EINVAL;
  419. }
  420. cpu_hotplug_driver_lock();
  421. rc = dlpar_offline_cpu(dn);
  422. if (rc) {
  423. of_node_put(dn);
  424. rc = -EINVAL;
  425. goto out;
  426. }
  427. rc = dlpar_release_drc(*drc_index);
  428. if (rc) {
  429. of_node_put(dn);
  430. goto out;
  431. }
  432. rc = dlpar_detach_node(dn);
  433. if (rc) {
  434. dlpar_acquire_drc(*drc_index);
  435. goto out;
  436. }
  437. of_node_put(dn);
  438. out:
  439. cpu_hotplug_driver_unlock();
  440. return rc ? rc : count;
  441. }
  442. static int __init pseries_dlpar_init(void)
  443. {
  444. ppc_md.cpu_probe = dlpar_cpu_probe;
  445. ppc_md.cpu_release = dlpar_cpu_release;
  446. return 0;
  447. }
  448. machine_device_initcall(pseries, pseries_dlpar_init);
  449. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */