hotplug-memory.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * pseries Memory Hotplug infrastructure.
  3. *
  4. * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "pseries-hotplug-mem: " fmt
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/memblock.h>
  15. #include <linux/memory.h>
  16. #include <linux/memory_hotplug.h>
  17. #include <linux/slab.h>
  18. #include <asm/firmware.h>
  19. #include <asm/machdep.h>
  20. #include <asm/prom.h>
  21. #include <asm/sparsemem.h>
  22. #include "pseries.h"
  23. static bool rtas_hp_event;
  24. unsigned long pseries_memory_block_size(void)
  25. {
  26. struct device_node *np;
  27. unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
  28. struct resource r;
  29. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  30. if (np) {
  31. const __be64 *size;
  32. size = of_get_property(np, "ibm,lmb-size", NULL);
  33. if (size)
  34. memblock_size = be64_to_cpup(size);
  35. of_node_put(np);
  36. } else if (machine_is(pseries)) {
  37. /* This fallback really only applies to pseries */
  38. unsigned int memzero_size = 0;
  39. np = of_find_node_by_path("/memory@0");
  40. if (np) {
  41. if (!of_address_to_resource(np, 0, &r))
  42. memzero_size = resource_size(&r);
  43. of_node_put(np);
  44. }
  45. if (memzero_size) {
  46. /* We now know the size of memory@0, use this to find
  47. * the first memoryblock and get its size.
  48. */
  49. char buf[64];
  50. sprintf(buf, "/memory@%x", memzero_size);
  51. np = of_find_node_by_path(buf);
  52. if (np) {
  53. if (!of_address_to_resource(np, 0, &r))
  54. memblock_size = resource_size(&r);
  55. of_node_put(np);
  56. }
  57. }
  58. }
  59. return memblock_size;
  60. }
  61. static void dlpar_free_drconf_property(struct property *prop)
  62. {
  63. kfree(prop->name);
  64. kfree(prop->value);
  65. kfree(prop);
  66. }
  67. static struct property *dlpar_clone_drconf_property(struct device_node *dn)
  68. {
  69. struct property *prop, *new_prop;
  70. struct of_drconf_cell *lmbs;
  71. u32 num_lmbs, *p;
  72. int i;
  73. prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
  74. if (!prop)
  75. return NULL;
  76. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  77. if (!new_prop)
  78. return NULL;
  79. new_prop->name = kstrdup(prop->name, GFP_KERNEL);
  80. new_prop->value = kmemdup(prop->value, prop->length, GFP_KERNEL);
  81. if (!new_prop->name || !new_prop->value) {
  82. dlpar_free_drconf_property(new_prop);
  83. return NULL;
  84. }
  85. new_prop->length = prop->length;
  86. /* Convert the property to cpu endian-ness */
  87. p = new_prop->value;
  88. *p = be32_to_cpu(*p);
  89. num_lmbs = *p++;
  90. lmbs = (struct of_drconf_cell *)p;
  91. for (i = 0; i < num_lmbs; i++) {
  92. lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
  93. lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
  94. lmbs[i].aa_index = be32_to_cpu(lmbs[i].aa_index);
  95. lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
  96. }
  97. return new_prop;
  98. }
  99. static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
  100. {
  101. unsigned long section_nr;
  102. struct mem_section *mem_sect;
  103. struct memory_block *mem_block;
  104. section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
  105. mem_sect = __nr_to_section(section_nr);
  106. mem_block = find_memory_block(mem_sect);
  107. return mem_block;
  108. }
  109. #ifdef CONFIG_MEMORY_HOTREMOVE
  110. static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
  111. {
  112. unsigned long block_sz, start_pfn;
  113. int sections_per_block;
  114. int i, nid;
  115. start_pfn = base >> PAGE_SHIFT;
  116. lock_device_hotplug();
  117. if (!pfn_valid(start_pfn))
  118. goto out;
  119. block_sz = pseries_memory_block_size();
  120. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  121. nid = memory_add_physaddr_to_nid(base);
  122. for (i = 0; i < sections_per_block; i++) {
  123. remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
  124. base += MIN_MEMORY_BLOCK_SIZE;
  125. }
  126. out:
  127. /* Update memory regions for memory remove */
  128. memblock_remove(base, memblock_size);
  129. unlock_device_hotplug();
  130. return 0;
  131. }
  132. static int pseries_remove_mem_node(struct device_node *np)
  133. {
  134. const char *type;
  135. const __be32 *regs;
  136. unsigned long base;
  137. unsigned int lmb_size;
  138. int ret = -EINVAL;
  139. /*
  140. * Check to see if we are actually removing memory
  141. */
  142. type = of_get_property(np, "device_type", NULL);
  143. if (type == NULL || strcmp(type, "memory") != 0)
  144. return 0;
  145. /*
  146. * Find the base address and size of the memblock
  147. */
  148. regs = of_get_property(np, "reg", NULL);
  149. if (!regs)
  150. return ret;
  151. base = be64_to_cpu(*(unsigned long *)regs);
  152. lmb_size = be32_to_cpu(regs[3]);
  153. pseries_remove_memblock(base, lmb_size);
  154. return 0;
  155. }
  156. static bool lmb_is_removable(struct of_drconf_cell *lmb)
  157. {
  158. int i, scns_per_block;
  159. int rc = 1;
  160. unsigned long pfn, block_sz;
  161. u64 phys_addr;
  162. if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
  163. return false;
  164. block_sz = memory_block_size_bytes();
  165. scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  166. phys_addr = lmb->base_addr;
  167. for (i = 0; i < scns_per_block; i++) {
  168. pfn = PFN_DOWN(phys_addr);
  169. if (!pfn_present(pfn))
  170. continue;
  171. rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
  172. phys_addr += MIN_MEMORY_BLOCK_SIZE;
  173. }
  174. return rc ? true : false;
  175. }
  176. static int dlpar_add_lmb(struct of_drconf_cell *);
  177. static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
  178. {
  179. struct memory_block *mem_block;
  180. unsigned long block_sz;
  181. int nid, rc;
  182. if (!lmb_is_removable(lmb))
  183. return -EINVAL;
  184. mem_block = lmb_to_memblock(lmb);
  185. if (!mem_block)
  186. return -EINVAL;
  187. rc = device_offline(&mem_block->dev);
  188. put_device(&mem_block->dev);
  189. if (rc)
  190. return rc;
  191. block_sz = pseries_memory_block_size();
  192. nid = memory_add_physaddr_to_nid(lmb->base_addr);
  193. remove_memory(nid, lmb->base_addr, block_sz);
  194. /* Update memory regions for memory remove */
  195. memblock_remove(lmb->base_addr, block_sz);
  196. dlpar_release_drc(lmb->drc_index);
  197. lmb->flags &= ~DRCONF_MEM_ASSIGNED;
  198. return 0;
  199. }
  200. static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
  201. struct property *prop)
  202. {
  203. struct of_drconf_cell *lmbs;
  204. int lmbs_removed = 0;
  205. int lmbs_available = 0;
  206. u32 num_lmbs, *p;
  207. int i, rc;
  208. pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
  209. if (lmbs_to_remove == 0)
  210. return -EINVAL;
  211. p = prop->value;
  212. num_lmbs = *p++;
  213. lmbs = (struct of_drconf_cell *)p;
  214. /* Validate that there are enough LMBs to satisfy the request */
  215. for (i = 0; i < num_lmbs; i++) {
  216. if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
  217. lmbs_available++;
  218. }
  219. if (lmbs_available < lmbs_to_remove)
  220. return -EINVAL;
  221. for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
  222. rc = dlpar_remove_lmb(&lmbs[i]);
  223. if (rc)
  224. continue;
  225. lmbs_removed++;
  226. /* Mark this lmb so we can add it later if all of the
  227. * requested LMBs cannot be removed.
  228. */
  229. lmbs[i].reserved = 1;
  230. }
  231. if (lmbs_removed != lmbs_to_remove) {
  232. pr_err("Memory hot-remove failed, adding LMB's back\n");
  233. for (i = 0; i < num_lmbs; i++) {
  234. if (!lmbs[i].reserved)
  235. continue;
  236. rc = dlpar_add_lmb(&lmbs[i]);
  237. if (rc)
  238. pr_err("Failed to add LMB back, drc index %x\n",
  239. lmbs[i].drc_index);
  240. lmbs[i].reserved = 0;
  241. }
  242. rc = -EINVAL;
  243. } else {
  244. for (i = 0; i < num_lmbs; i++) {
  245. if (!lmbs[i].reserved)
  246. continue;
  247. pr_info("Memory at %llx was hot-removed\n",
  248. lmbs[i].base_addr);
  249. lmbs[i].reserved = 0;
  250. }
  251. rc = 0;
  252. }
  253. return rc;
  254. }
  255. static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
  256. {
  257. struct of_drconf_cell *lmbs;
  258. u32 num_lmbs, *p;
  259. int lmb_found;
  260. int i, rc;
  261. pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
  262. p = prop->value;
  263. num_lmbs = *p++;
  264. lmbs = (struct of_drconf_cell *)p;
  265. lmb_found = 0;
  266. for (i = 0; i < num_lmbs; i++) {
  267. if (lmbs[i].drc_index == drc_index) {
  268. lmb_found = 1;
  269. rc = dlpar_remove_lmb(&lmbs[i]);
  270. break;
  271. }
  272. }
  273. if (!lmb_found)
  274. rc = -EINVAL;
  275. if (rc)
  276. pr_info("Failed to hot-remove memory at %llx\n",
  277. lmbs[i].base_addr);
  278. else
  279. pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
  280. return rc;
  281. }
  282. #else
  283. static inline int pseries_remove_memblock(unsigned long base,
  284. unsigned int memblock_size)
  285. {
  286. return -EOPNOTSUPP;
  287. }
  288. static inline int pseries_remove_mem_node(struct device_node *np)
  289. {
  290. return 0;
  291. }
  292. static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
  293. {
  294. return -EOPNOTSUPP;
  295. }
  296. static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
  297. {
  298. return -EOPNOTSUPP;
  299. }
  300. static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
  301. struct property *prop)
  302. {
  303. return -EOPNOTSUPP;
  304. }
  305. static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
  306. {
  307. return -EOPNOTSUPP;
  308. }
  309. #endif /* CONFIG_MEMORY_HOTREMOVE */
  310. static int dlpar_add_lmb(struct of_drconf_cell *lmb)
  311. {
  312. struct memory_block *mem_block;
  313. unsigned long block_sz;
  314. int nid, rc;
  315. if (lmb->flags & DRCONF_MEM_ASSIGNED)
  316. return -EINVAL;
  317. block_sz = memory_block_size_bytes();
  318. rc = dlpar_acquire_drc(lmb->drc_index);
  319. if (rc)
  320. return rc;
  321. /* Find the node id for this address */
  322. nid = memory_add_physaddr_to_nid(lmb->base_addr);
  323. /* Add the memory */
  324. rc = add_memory(nid, lmb->base_addr, block_sz);
  325. if (rc) {
  326. dlpar_release_drc(lmb->drc_index);
  327. return rc;
  328. }
  329. /* Register this block of memory */
  330. rc = memblock_add(lmb->base_addr, block_sz);
  331. if (rc) {
  332. remove_memory(nid, lmb->base_addr, block_sz);
  333. dlpar_release_drc(lmb->drc_index);
  334. return rc;
  335. }
  336. mem_block = lmb_to_memblock(lmb);
  337. if (!mem_block) {
  338. remove_memory(nid, lmb->base_addr, block_sz);
  339. dlpar_release_drc(lmb->drc_index);
  340. return -EINVAL;
  341. }
  342. rc = device_online(&mem_block->dev);
  343. put_device(&mem_block->dev);
  344. if (rc) {
  345. remove_memory(nid, lmb->base_addr, block_sz);
  346. dlpar_release_drc(lmb->drc_index);
  347. return rc;
  348. }
  349. lmb->flags |= DRCONF_MEM_ASSIGNED;
  350. return 0;
  351. }
  352. static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
  353. {
  354. struct of_drconf_cell *lmbs;
  355. u32 num_lmbs, *p;
  356. int lmbs_available = 0;
  357. int lmbs_added = 0;
  358. int i, rc;
  359. pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
  360. if (lmbs_to_add == 0)
  361. return -EINVAL;
  362. p = prop->value;
  363. num_lmbs = *p++;
  364. lmbs = (struct of_drconf_cell *)p;
  365. /* Validate that there are enough LMBs to satisfy the request */
  366. for (i = 0; i < num_lmbs; i++) {
  367. if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
  368. lmbs_available++;
  369. }
  370. if (lmbs_available < lmbs_to_add)
  371. return -EINVAL;
  372. for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
  373. rc = dlpar_add_lmb(&lmbs[i]);
  374. if (rc)
  375. continue;
  376. lmbs_added++;
  377. /* Mark this lmb so we can remove it later if all of the
  378. * requested LMBs cannot be added.
  379. */
  380. lmbs[i].reserved = 1;
  381. }
  382. if (lmbs_added != lmbs_to_add) {
  383. pr_err("Memory hot-add failed, removing any added LMBs\n");
  384. for (i = 0; i < num_lmbs; i++) {
  385. if (!lmbs[i].reserved)
  386. continue;
  387. rc = dlpar_remove_lmb(&lmbs[i]);
  388. if (rc)
  389. pr_err("Failed to remove LMB, drc index %x\n",
  390. be32_to_cpu(lmbs[i].drc_index));
  391. }
  392. rc = -EINVAL;
  393. } else {
  394. for (i = 0; i < num_lmbs; i++) {
  395. if (!lmbs[i].reserved)
  396. continue;
  397. pr_info("Memory at %llx (drc index %x) was hot-added\n",
  398. lmbs[i].base_addr, lmbs[i].drc_index);
  399. lmbs[i].reserved = 0;
  400. }
  401. }
  402. return rc;
  403. }
  404. static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
  405. {
  406. struct of_drconf_cell *lmbs;
  407. u32 num_lmbs, *p;
  408. int i, lmb_found;
  409. int rc;
  410. pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
  411. p = prop->value;
  412. num_lmbs = *p++;
  413. lmbs = (struct of_drconf_cell *)p;
  414. lmb_found = 0;
  415. for (i = 0; i < num_lmbs; i++) {
  416. if (lmbs[i].drc_index == drc_index) {
  417. lmb_found = 1;
  418. rc = dlpar_add_lmb(&lmbs[i]);
  419. break;
  420. }
  421. }
  422. if (!lmb_found)
  423. rc = -EINVAL;
  424. if (rc)
  425. pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
  426. else
  427. pr_info("Memory at %llx (drc index %x) was hot-added\n",
  428. lmbs[i].base_addr, drc_index);
  429. return rc;
  430. }
  431. static void dlpar_update_drconf_property(struct device_node *dn,
  432. struct property *prop)
  433. {
  434. struct of_drconf_cell *lmbs;
  435. u32 num_lmbs, *p;
  436. int i;
  437. /* Convert the property back to BE */
  438. p = prop->value;
  439. num_lmbs = *p;
  440. *p = cpu_to_be32(*p);
  441. p++;
  442. lmbs = (struct of_drconf_cell *)p;
  443. for (i = 0; i < num_lmbs; i++) {
  444. lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
  445. lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
  446. lmbs[i].aa_index = cpu_to_be32(lmbs[i].aa_index);
  447. lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
  448. }
  449. rtas_hp_event = true;
  450. of_update_property(dn, prop);
  451. rtas_hp_event = false;
  452. }
  453. int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
  454. {
  455. struct device_node *dn;
  456. struct property *prop;
  457. u32 count, drc_index;
  458. int rc;
  459. count = hp_elog->_drc_u.drc_count;
  460. drc_index = hp_elog->_drc_u.drc_index;
  461. lock_device_hotplug();
  462. dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  463. if (!dn) {
  464. rc = -EINVAL;
  465. goto dlpar_memory_out;
  466. }
  467. prop = dlpar_clone_drconf_property(dn);
  468. if (!prop) {
  469. rc = -EINVAL;
  470. goto dlpar_memory_out;
  471. }
  472. switch (hp_elog->action) {
  473. case PSERIES_HP_ELOG_ACTION_ADD:
  474. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
  475. rc = dlpar_memory_add_by_count(count, prop);
  476. else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
  477. rc = dlpar_memory_add_by_index(drc_index, prop);
  478. else
  479. rc = -EINVAL;
  480. break;
  481. case PSERIES_HP_ELOG_ACTION_REMOVE:
  482. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
  483. rc = dlpar_memory_remove_by_count(count, prop);
  484. else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
  485. rc = dlpar_memory_remove_by_index(drc_index, prop);
  486. else
  487. rc = -EINVAL;
  488. break;
  489. default:
  490. pr_err("Invalid action (%d) specified\n", hp_elog->action);
  491. rc = -EINVAL;
  492. break;
  493. }
  494. if (rc)
  495. dlpar_free_drconf_property(prop);
  496. else
  497. dlpar_update_drconf_property(dn, prop);
  498. dlpar_memory_out:
  499. of_node_put(dn);
  500. unlock_device_hotplug();
  501. return rc;
  502. }
  503. static int pseries_add_mem_node(struct device_node *np)
  504. {
  505. const char *type;
  506. const __be32 *regs;
  507. unsigned long base;
  508. unsigned int lmb_size;
  509. int ret = -EINVAL;
  510. /*
  511. * Check to see if we are actually adding memory
  512. */
  513. type = of_get_property(np, "device_type", NULL);
  514. if (type == NULL || strcmp(type, "memory") != 0)
  515. return 0;
  516. /*
  517. * Find the base and size of the memblock
  518. */
  519. regs = of_get_property(np, "reg", NULL);
  520. if (!regs)
  521. return ret;
  522. base = be64_to_cpu(*(unsigned long *)regs);
  523. lmb_size = be32_to_cpu(regs[3]);
  524. /*
  525. * Update memory region to represent the memory add
  526. */
  527. ret = memblock_add(base, lmb_size);
  528. return (ret < 0) ? -EINVAL : 0;
  529. }
  530. static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
  531. {
  532. struct of_drconf_cell *new_drmem, *old_drmem;
  533. unsigned long memblock_size;
  534. u32 entries;
  535. __be32 *p;
  536. int i, rc = -EINVAL;
  537. if (rtas_hp_event)
  538. return 0;
  539. memblock_size = pseries_memory_block_size();
  540. if (!memblock_size)
  541. return -EINVAL;
  542. p = (__be32 *) pr->old_prop->value;
  543. if (!p)
  544. return -EINVAL;
  545. /* The first int of the property is the number of lmb's described
  546. * by the property. This is followed by an array of of_drconf_cell
  547. * entries. Get the number of entries and skip to the array of
  548. * of_drconf_cell's.
  549. */
  550. entries = be32_to_cpu(*p++);
  551. old_drmem = (struct of_drconf_cell *)p;
  552. p = (__be32 *)pr->prop->value;
  553. p++;
  554. new_drmem = (struct of_drconf_cell *)p;
  555. for (i = 0; i < entries; i++) {
  556. if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
  557. (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
  558. rc = pseries_remove_memblock(
  559. be64_to_cpu(old_drmem[i].base_addr),
  560. memblock_size);
  561. break;
  562. } else if ((!(be32_to_cpu(old_drmem[i].flags) &
  563. DRCONF_MEM_ASSIGNED)) &&
  564. (be32_to_cpu(new_drmem[i].flags) &
  565. DRCONF_MEM_ASSIGNED)) {
  566. rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
  567. memblock_size);
  568. rc = (rc < 0) ? -EINVAL : 0;
  569. break;
  570. }
  571. }
  572. return rc;
  573. }
  574. static int pseries_memory_notifier(struct notifier_block *nb,
  575. unsigned long action, void *data)
  576. {
  577. struct of_reconfig_data *rd = data;
  578. int err = 0;
  579. switch (action) {
  580. case OF_RECONFIG_ATTACH_NODE:
  581. err = pseries_add_mem_node(rd->dn);
  582. break;
  583. case OF_RECONFIG_DETACH_NODE:
  584. err = pseries_remove_mem_node(rd->dn);
  585. break;
  586. case OF_RECONFIG_UPDATE_PROPERTY:
  587. if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
  588. err = pseries_update_drconf_memory(rd);
  589. break;
  590. }
  591. return notifier_from_errno(err);
  592. }
  593. static struct notifier_block pseries_mem_nb = {
  594. .notifier_call = pseries_memory_notifier,
  595. };
  596. static int __init pseries_memory_hotplug_init(void)
  597. {
  598. if (firmware_has_feature(FW_FEATURE_LPAR))
  599. of_reconfig_notifier_register(&pseries_mem_nb);
  600. return 0;
  601. }
  602. machine_device_initcall(pseries, pseries_memory_hotplug_init);