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/kref.h>
  14. #include <linux/notifier.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/cpu.h>
  17. #include <linux/slab.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. #include <asm/pSeries_reconfig.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 = pSeries_reconfig_notify(PSERIES_RECONFIG_ADD, 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_attach_node(dn);
  222. of_node_put(dn->parent);
  223. return 0;
  224. }
  225. int dlpar_detach_node(struct device_node *dn)
  226. {
  227. pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, dn);
  228. of_detach_node(dn);
  229. of_node_put(dn); /* Must decrement the refcount */
  230. return 0;
  231. }
  232. #define DR_ENTITY_SENSE 9003
  233. #define DR_ENTITY_PRESENT 1
  234. #define DR_ENTITY_UNUSABLE 2
  235. #define ALLOCATION_STATE 9003
  236. #define ALLOC_UNUSABLE 0
  237. #define ALLOC_USABLE 1
  238. #define ISOLATION_STATE 9001
  239. #define ISOLATE 0
  240. #define UNISOLATE 1
  241. int dlpar_acquire_drc(u32 drc_index)
  242. {
  243. int dr_status, rc;
  244. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  245. DR_ENTITY_SENSE, drc_index);
  246. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  247. return -1;
  248. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  249. if (rc)
  250. return rc;
  251. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  252. if (rc) {
  253. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  254. return rc;
  255. }
  256. return 0;
  257. }
  258. int dlpar_release_drc(u32 drc_index)
  259. {
  260. int dr_status, rc;
  261. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  262. DR_ENTITY_SENSE, drc_index);
  263. if (rc || dr_status != DR_ENTITY_PRESENT)
  264. return -1;
  265. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  266. if (rc)
  267. return rc;
  268. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  269. if (rc) {
  270. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  271. return rc;
  272. }
  273. return 0;
  274. }
  275. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  276. static int dlpar_online_cpu(struct device_node *dn)
  277. {
  278. int rc = 0;
  279. unsigned int cpu;
  280. int len, nthreads, i;
  281. const u32 *intserv;
  282. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  283. if (!intserv)
  284. return -EINVAL;
  285. nthreads = len / sizeof(u32);
  286. cpu_maps_update_begin();
  287. for (i = 0; i < nthreads; i++) {
  288. for_each_present_cpu(cpu) {
  289. if (get_hard_smp_processor_id(cpu) != intserv[i])
  290. continue;
  291. BUG_ON(get_cpu_current_state(cpu)
  292. != CPU_STATE_OFFLINE);
  293. cpu_maps_update_done();
  294. rc = cpu_up(cpu);
  295. if (rc)
  296. goto out;
  297. cpu_maps_update_begin();
  298. break;
  299. }
  300. if (cpu == num_possible_cpus())
  301. printk(KERN_WARNING "Could not find cpu to online "
  302. "with physical id 0x%x\n", intserv[i]);
  303. }
  304. cpu_maps_update_done();
  305. out:
  306. return rc;
  307. }
  308. static ssize_t dlpar_cpu_probe(const char *buf, size_t count)
  309. {
  310. struct device_node *dn;
  311. unsigned long drc_index;
  312. char *cpu_name;
  313. int rc;
  314. cpu_hotplug_driver_lock();
  315. rc = strict_strtoul(buf, 0, &drc_index);
  316. if (rc) {
  317. rc = -EINVAL;
  318. goto out;
  319. }
  320. dn = dlpar_configure_connector(drc_index);
  321. if (!dn) {
  322. rc = -EINVAL;
  323. goto out;
  324. }
  325. /* configure-connector reports cpus as living in the base
  326. * directory of the device tree. CPUs actually live in the
  327. * cpus directory so we need to fixup the full_name.
  328. */
  329. cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
  330. if (!cpu_name) {
  331. dlpar_free_cc_nodes(dn);
  332. rc = -ENOMEM;
  333. goto out;
  334. }
  335. kfree(dn->full_name);
  336. dn->full_name = cpu_name;
  337. rc = dlpar_acquire_drc(drc_index);
  338. if (rc) {
  339. dlpar_free_cc_nodes(dn);
  340. rc = -EINVAL;
  341. goto out;
  342. }
  343. rc = dlpar_attach_node(dn);
  344. if (rc) {
  345. dlpar_release_drc(drc_index);
  346. dlpar_free_cc_nodes(dn);
  347. goto out;
  348. }
  349. rc = dlpar_online_cpu(dn);
  350. out:
  351. cpu_hotplug_driver_unlock();
  352. return rc ? rc : count;
  353. }
  354. static int dlpar_offline_cpu(struct device_node *dn)
  355. {
  356. int rc = 0;
  357. unsigned int cpu;
  358. int len, nthreads, i;
  359. const u32 *intserv;
  360. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  361. if (!intserv)
  362. return -EINVAL;
  363. nthreads = len / sizeof(u32);
  364. cpu_maps_update_begin();
  365. for (i = 0; i < nthreads; i++) {
  366. for_each_present_cpu(cpu) {
  367. if (get_hard_smp_processor_id(cpu) != intserv[i])
  368. continue;
  369. if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
  370. break;
  371. if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
  372. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  373. cpu_maps_update_done();
  374. rc = cpu_down(cpu);
  375. if (rc)
  376. goto out;
  377. cpu_maps_update_begin();
  378. break;
  379. }
  380. /*
  381. * The cpu is in CPU_STATE_INACTIVE.
  382. * Upgrade it's state to CPU_STATE_OFFLINE.
  383. */
  384. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  385. BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
  386. != H_SUCCESS);
  387. __cpu_die(cpu);
  388. break;
  389. }
  390. if (cpu == num_possible_cpus())
  391. printk(KERN_WARNING "Could not find cpu to offline "
  392. "with physical id 0x%x\n", intserv[i]);
  393. }
  394. cpu_maps_update_done();
  395. out:
  396. return rc;
  397. }
  398. static ssize_t dlpar_cpu_release(const char *buf, size_t count)
  399. {
  400. struct device_node *dn;
  401. const u32 *drc_index;
  402. int rc;
  403. dn = of_find_node_by_path(buf);
  404. if (!dn)
  405. return -EINVAL;
  406. drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
  407. if (!drc_index) {
  408. of_node_put(dn);
  409. return -EINVAL;
  410. }
  411. cpu_hotplug_driver_lock();
  412. rc = dlpar_offline_cpu(dn);
  413. if (rc) {
  414. of_node_put(dn);
  415. rc = -EINVAL;
  416. goto out;
  417. }
  418. rc = dlpar_release_drc(*drc_index);
  419. if (rc) {
  420. of_node_put(dn);
  421. goto out;
  422. }
  423. rc = dlpar_detach_node(dn);
  424. if (rc) {
  425. dlpar_acquire_drc(*drc_index);
  426. goto out;
  427. }
  428. of_node_put(dn);
  429. out:
  430. cpu_hotplug_driver_unlock();
  431. return rc ? rc : 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 */