dlpar.c 11 KB

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