dlpar.c 11 KB

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