dlpar.c 11 KB

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