dlpar.c 11 KB

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