dlpar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 "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. static 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 = kzalloc(prop->length, GFP_KERNEL);
  50. if (!prop->value) {
  51. dlpar_free_cc_property(prop);
  52. return NULL;
  53. }
  54. memcpy(prop->value, value, prop->length);
  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. * preceeding '/', 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 = kmalloc(strlen(name) + 2, GFP_KERNEL);
  70. if (!dn->full_name) {
  71. kfree(dn);
  72. return NULL;
  73. }
  74. sprintf(dn->full_name, "/%s", name);
  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. static 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 NEXT_SIBLING 1
  97. #define NEXT_CHILD 2
  98. #define NEXT_PROPERTY 3
  99. #define PREV_PARENT 4
  100. #define MORE_MEMORY 5
  101. #define CALL_AGAIN -2
  102. #define ERR_CFG_USE -9003
  103. struct device_node *dlpar_configure_connector(u32 drc_index)
  104. {
  105. struct device_node *dn;
  106. struct device_node *first_dn = NULL;
  107. struct device_node *last_dn = NULL;
  108. struct property *property;
  109. struct property *last_property = NULL;
  110. struct cc_workarea *ccwa;
  111. int cc_token;
  112. int rc;
  113. cc_token = rtas_token("ibm,configure-connector");
  114. if (cc_token == RTAS_UNKNOWN_SERVICE)
  115. return NULL;
  116. spin_lock(&rtas_data_buf_lock);
  117. ccwa = (struct cc_workarea *)&rtas_data_buf[0];
  118. ccwa->drc_index = drc_index;
  119. ccwa->zero = 0;
  120. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  121. while (rc) {
  122. switch (rc) {
  123. case NEXT_SIBLING:
  124. dn = dlpar_parse_cc_node(ccwa);
  125. if (!dn)
  126. goto cc_error;
  127. dn->parent = last_dn->parent;
  128. last_dn->sibling = dn;
  129. last_dn = dn;
  130. break;
  131. case NEXT_CHILD:
  132. dn = dlpar_parse_cc_node(ccwa);
  133. if (!dn)
  134. goto cc_error;
  135. if (!first_dn)
  136. first_dn = dn;
  137. else {
  138. dn->parent = last_dn;
  139. if (last_dn)
  140. last_dn->child = dn;
  141. }
  142. last_dn = dn;
  143. break;
  144. case NEXT_PROPERTY:
  145. property = dlpar_parse_cc_property(ccwa);
  146. if (!property)
  147. goto cc_error;
  148. if (!last_dn->properties)
  149. last_dn->properties = property;
  150. else
  151. last_property->next = property;
  152. last_property = property;
  153. break;
  154. case PREV_PARENT:
  155. last_dn = last_dn->parent;
  156. break;
  157. case CALL_AGAIN:
  158. break;
  159. case MORE_MEMORY:
  160. case ERR_CFG_USE:
  161. default:
  162. printk(KERN_ERR "Unexpected Error (%d) "
  163. "returned from configure-connector\n", rc);
  164. goto cc_error;
  165. }
  166. rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
  167. }
  168. spin_unlock(&rtas_data_buf_lock);
  169. return first_dn;
  170. cc_error:
  171. if (first_dn)
  172. dlpar_free_cc_nodes(first_dn);
  173. spin_unlock(&rtas_data_buf_lock);
  174. return NULL;
  175. }
  176. static struct device_node *derive_parent(const char *path)
  177. {
  178. struct device_node *parent;
  179. char *last_slash;
  180. last_slash = strrchr(path, '/');
  181. if (last_slash == path) {
  182. parent = of_find_node_by_path("/");
  183. } else {
  184. char *parent_path;
  185. int parent_path_len = last_slash - path + 1;
  186. parent_path = kmalloc(parent_path_len, GFP_KERNEL);
  187. if (!parent_path)
  188. return NULL;
  189. strlcpy(parent_path, path, parent_path_len);
  190. parent = of_find_node_by_path(parent_path);
  191. kfree(parent_path);
  192. }
  193. return parent;
  194. }
  195. int dlpar_attach_node(struct device_node *dn)
  196. {
  197. struct proc_dir_entry *ent;
  198. int rc;
  199. of_node_set_flag(dn, OF_DYNAMIC);
  200. kref_init(&dn->kref);
  201. dn->parent = derive_parent(dn->full_name);
  202. if (!dn->parent)
  203. return -ENOMEM;
  204. rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,
  205. PSERIES_RECONFIG_ADD, dn);
  206. if (rc == NOTIFY_BAD) {
  207. printk(KERN_ERR "Failed to add device node %s\n",
  208. dn->full_name);
  209. return -ENOMEM; /* For now, safe to assume kmalloc failure */
  210. }
  211. of_attach_node(dn);
  212. #ifdef CONFIG_PROC_DEVICETREE
  213. ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
  214. if (ent)
  215. proc_device_tree_add_node(dn, ent);
  216. #endif
  217. of_node_put(dn->parent);
  218. return 0;
  219. }
  220. int dlpar_detach_node(struct device_node *dn)
  221. {
  222. struct device_node *parent = dn->parent;
  223. struct property *prop = dn->properties;
  224. #ifdef CONFIG_PROC_DEVICETREE
  225. while (prop) {
  226. remove_proc_entry(prop->name, dn->pde);
  227. prop = prop->next;
  228. }
  229. if (dn->pde)
  230. remove_proc_entry(dn->pde->name, parent->pde);
  231. #endif
  232. blocking_notifier_call_chain(&pSeries_reconfig_chain,
  233. PSERIES_RECONFIG_REMOVE, dn);
  234. of_detach_node(dn);
  235. of_node_put(dn); /* Must decrement the refcount */
  236. return 0;
  237. }
  238. #define DR_ENTITY_SENSE 9003
  239. #define DR_ENTITY_PRESENT 1
  240. #define DR_ENTITY_UNUSABLE 2
  241. #define ALLOCATION_STATE 9003
  242. #define ALLOC_UNUSABLE 0
  243. #define ALLOC_USABLE 1
  244. #define ISOLATION_STATE 9001
  245. #define ISOLATE 0
  246. #define UNISOLATE 1
  247. int dlpar_acquire_drc(u32 drc_index)
  248. {
  249. int dr_status, rc;
  250. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  251. DR_ENTITY_SENSE, drc_index);
  252. if (rc || dr_status != DR_ENTITY_UNUSABLE)
  253. return -1;
  254. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
  255. if (rc)
  256. return rc;
  257. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  258. if (rc) {
  259. rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  260. return rc;
  261. }
  262. return 0;
  263. }
  264. int dlpar_release_drc(u32 drc_index)
  265. {
  266. int dr_status, rc;
  267. rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
  268. DR_ENTITY_SENSE, drc_index);
  269. if (rc || dr_status != DR_ENTITY_PRESENT)
  270. return -1;
  271. rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
  272. if (rc)
  273. return rc;
  274. rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
  275. if (rc) {
  276. rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
  277. return rc;
  278. }
  279. return 0;
  280. }
  281. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  282. static DEFINE_MUTEX(pseries_cpu_hotplug_mutex);
  283. void cpu_hotplug_driver_lock(void)
  284. __acquires(pseries_cpu_hotplug_mutex)
  285. {
  286. mutex_lock(&pseries_cpu_hotplug_mutex);
  287. }
  288. void cpu_hotplug_driver_unlock(void)
  289. __releases(pseries_cpu_hotplug_mutex)
  290. {
  291. mutex_unlock(&pseries_cpu_hotplug_mutex);
  292. }
  293. static int dlpar_online_cpu(struct device_node *dn)
  294. {
  295. int rc = 0;
  296. unsigned int cpu;
  297. int len, nthreads, i;
  298. const u32 *intserv;
  299. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  300. if (!intserv)
  301. return -EINVAL;
  302. nthreads = len / sizeof(u32);
  303. cpu_maps_update_begin();
  304. for (i = 0; i < nthreads; i++) {
  305. for_each_present_cpu(cpu) {
  306. if (get_hard_smp_processor_id(cpu) != intserv[i])
  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", intserv[i]);
  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;
  328. unsigned long drc_index;
  329. char *cpu_name;
  330. int rc;
  331. cpu_hotplug_driver_lock();
  332. rc = strict_strtoul(buf, 0, &drc_index);
  333. if (rc) {
  334. rc = -EINVAL;
  335. goto out;
  336. }
  337. dn = dlpar_configure_connector(drc_index);
  338. if (!dn) {
  339. rc = -EINVAL;
  340. goto out;
  341. }
  342. /* configure-connector reports cpus as living in the base
  343. * directory of the device tree. CPUs actually live in the
  344. * cpus directory so we need to fixup the full_name.
  345. */
  346. cpu_name = kzalloc(strlen(dn->full_name) + strlen("/cpus") + 1,
  347. GFP_KERNEL);
  348. if (!cpu_name) {
  349. dlpar_free_cc_nodes(dn);
  350. rc = -ENOMEM;
  351. goto out;
  352. }
  353. sprintf(cpu_name, "/cpus%s", dn->full_name);
  354. kfree(dn->full_name);
  355. dn->full_name = cpu_name;
  356. rc = dlpar_acquire_drc(drc_index);
  357. if (rc) {
  358. dlpar_free_cc_nodes(dn);
  359. rc = -EINVAL;
  360. goto out;
  361. }
  362. rc = dlpar_attach_node(dn);
  363. if (rc) {
  364. dlpar_release_drc(drc_index);
  365. dlpar_free_cc_nodes(dn);
  366. }
  367. rc = dlpar_online_cpu(dn);
  368. out:
  369. cpu_hotplug_driver_unlock();
  370. return rc ? rc : count;
  371. }
  372. static int dlpar_offline_cpu(struct device_node *dn)
  373. {
  374. int rc = 0;
  375. unsigned int cpu;
  376. int len, nthreads, i;
  377. const u32 *intserv;
  378. intserv = of_get_property(dn, "ibm,ppc-interrupt-server#s", &len);
  379. if (!intserv)
  380. return -EINVAL;
  381. nthreads = len / sizeof(u32);
  382. cpu_maps_update_begin();
  383. for (i = 0; i < nthreads; i++) {
  384. for_each_present_cpu(cpu) {
  385. if (get_hard_smp_processor_id(cpu) != intserv[i])
  386. continue;
  387. if (get_cpu_current_state(cpu) == CPU_STATE_OFFLINE)
  388. break;
  389. if (get_cpu_current_state(cpu) == CPU_STATE_ONLINE) {
  390. cpu_maps_update_done();
  391. rc = cpu_down(cpu);
  392. if (rc)
  393. goto out;
  394. cpu_maps_update_begin();
  395. break;
  396. }
  397. /*
  398. * The cpu is in CPU_STATE_INACTIVE.
  399. * Upgrade it's state to CPU_STATE_OFFLINE.
  400. */
  401. set_preferred_offline_state(cpu, CPU_STATE_OFFLINE);
  402. BUG_ON(plpar_hcall_norets(H_PROD, intserv[i])
  403. != H_SUCCESS);
  404. __cpu_die(cpu);
  405. break;
  406. }
  407. if (cpu == num_possible_cpus())
  408. printk(KERN_WARNING "Could not find cpu to offline "
  409. "with physical id 0x%x\n", intserv[i]);
  410. }
  411. cpu_maps_update_done();
  412. out:
  413. return rc;
  414. }
  415. static ssize_t dlpar_cpu_release(const char *buf, size_t count)
  416. {
  417. struct device_node *dn;
  418. const u32 *drc_index;
  419. int rc;
  420. dn = of_find_node_by_path(buf);
  421. if (!dn)
  422. return -EINVAL;
  423. drc_index = of_get_property(dn, "ibm,my-drc-index", NULL);
  424. if (!drc_index) {
  425. of_node_put(dn);
  426. return -EINVAL;
  427. }
  428. cpu_hotplug_driver_lock();
  429. rc = dlpar_offline_cpu(dn);
  430. if (rc) {
  431. of_node_put(dn);
  432. rc = -EINVAL;
  433. goto out;
  434. }
  435. rc = dlpar_release_drc(*drc_index);
  436. if (rc) {
  437. of_node_put(dn);
  438. goto out;
  439. }
  440. rc = dlpar_detach_node(dn);
  441. if (rc) {
  442. dlpar_acquire_drc(*drc_index);
  443. goto out;
  444. }
  445. of_node_put(dn);
  446. out:
  447. cpu_hotplug_driver_unlock();
  448. return rc ? rc : count;
  449. }
  450. static int __init pseries_dlpar_init(void)
  451. {
  452. ppc_md.cpu_probe = dlpar_cpu_probe;
  453. ppc_md.cpu_release = dlpar_cpu_release;
  454. return 0;
  455. }
  456. machine_device_initcall(pseries, pseries_dlpar_init);
  457. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */