dlpar.c 11 KB

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