hotplug-memory.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 <asm/fadump.h>
  23. #include <asm/drmem.h>
  24. #include "pseries.h"
  25. static bool rtas_hp_event;
  26. unsigned long pseries_memory_block_size(void)
  27. {
  28. struct device_node *np;
  29. u64 memblock_size = MIN_MEMORY_BLOCK_SIZE;
  30. struct resource r;
  31. np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  32. if (np) {
  33. const __be64 *size;
  34. size = of_get_property(np, "ibm,lmb-size", NULL);
  35. if (size)
  36. memblock_size = be64_to_cpup(size);
  37. of_node_put(np);
  38. } else if (machine_is(pseries)) {
  39. /* This fallback really only applies to pseries */
  40. unsigned int memzero_size = 0;
  41. np = of_find_node_by_path("/memory@0");
  42. if (np) {
  43. if (!of_address_to_resource(np, 0, &r))
  44. memzero_size = resource_size(&r);
  45. of_node_put(np);
  46. }
  47. if (memzero_size) {
  48. /* We now know the size of memory@0, use this to find
  49. * the first memoryblock and get its size.
  50. */
  51. char buf[64];
  52. sprintf(buf, "/memory@%x", memzero_size);
  53. np = of_find_node_by_path(buf);
  54. if (np) {
  55. if (!of_address_to_resource(np, 0, &r))
  56. memblock_size = resource_size(&r);
  57. of_node_put(np);
  58. }
  59. }
  60. }
  61. return memblock_size;
  62. }
  63. static void dlpar_free_property(struct property *prop)
  64. {
  65. kfree(prop->name);
  66. kfree(prop->value);
  67. kfree(prop);
  68. }
  69. static struct property *dlpar_clone_property(struct property *prop,
  70. u32 prop_size)
  71. {
  72. struct property *new_prop;
  73. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  74. if (!new_prop)
  75. return NULL;
  76. new_prop->name = kstrdup(prop->name, GFP_KERNEL);
  77. new_prop->value = kzalloc(prop_size, GFP_KERNEL);
  78. if (!new_prop->name || !new_prop->value) {
  79. dlpar_free_property(new_prop);
  80. return NULL;
  81. }
  82. memcpy(new_prop->value, prop->value, prop->length);
  83. new_prop->length = prop_size;
  84. of_property_set_flag(new_prop, OF_DYNAMIC);
  85. return new_prop;
  86. }
  87. static bool find_aa_index(struct device_node *dr_node,
  88. struct property *ala_prop,
  89. const u32 *lmb_assoc, u32 *aa_index)
  90. {
  91. u32 *assoc_arrays, new_prop_size;
  92. struct property *new_prop;
  93. int aa_arrays, aa_array_entries, aa_array_sz;
  94. int i, index;
  95. /*
  96. * The ibm,associativity-lookup-arrays property is defined to be
  97. * a 32-bit value specifying the number of associativity arrays
  98. * followed by a 32-bitvalue specifying the number of entries per
  99. * array, followed by the associativity arrays.
  100. */
  101. assoc_arrays = ala_prop->value;
  102. aa_arrays = be32_to_cpu(assoc_arrays[0]);
  103. aa_array_entries = be32_to_cpu(assoc_arrays[1]);
  104. aa_array_sz = aa_array_entries * sizeof(u32);
  105. for (i = 0; i < aa_arrays; i++) {
  106. index = (i * aa_array_entries) + 2;
  107. if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
  108. continue;
  109. *aa_index = i;
  110. return true;
  111. }
  112. new_prop_size = ala_prop->length + aa_array_sz;
  113. new_prop = dlpar_clone_property(ala_prop, new_prop_size);
  114. if (!new_prop)
  115. return false;
  116. assoc_arrays = new_prop->value;
  117. /* increment the number of entries in the lookup array */
  118. assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
  119. /* copy the new associativity into the lookup array */
  120. index = aa_arrays * aa_array_entries + 2;
  121. memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
  122. of_update_property(dr_node, new_prop);
  123. /*
  124. * The associativity lookup array index for this lmb is
  125. * number of entries - 1 since we added its associativity
  126. * to the end of the lookup array.
  127. */
  128. *aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
  129. return true;
  130. }
  131. static int update_lmb_associativity_index(struct drmem_lmb *lmb)
  132. {
  133. struct device_node *parent, *lmb_node, *dr_node;
  134. struct property *ala_prop;
  135. const u32 *lmb_assoc;
  136. u32 aa_index;
  137. bool found;
  138. parent = of_find_node_by_path("/");
  139. if (!parent)
  140. return -ENODEV;
  141. lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
  142. parent);
  143. of_node_put(parent);
  144. if (!lmb_node)
  145. return -EINVAL;
  146. lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
  147. if (!lmb_assoc) {
  148. dlpar_free_cc_nodes(lmb_node);
  149. return -ENODEV;
  150. }
  151. dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  152. if (!dr_node) {
  153. dlpar_free_cc_nodes(lmb_node);
  154. return -ENODEV;
  155. }
  156. ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
  157. NULL);
  158. if (!ala_prop) {
  159. of_node_put(dr_node);
  160. dlpar_free_cc_nodes(lmb_node);
  161. return -ENODEV;
  162. }
  163. found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
  164. of_node_put(dr_node);
  165. dlpar_free_cc_nodes(lmb_node);
  166. if (!found) {
  167. pr_err("Could not find LMB associativity\n");
  168. return -1;
  169. }
  170. lmb->aa_index = aa_index;
  171. return 0;
  172. }
  173. static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
  174. {
  175. unsigned long section_nr;
  176. struct mem_section *mem_sect;
  177. struct memory_block *mem_block;
  178. section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
  179. mem_sect = __nr_to_section(section_nr);
  180. mem_block = find_memory_block(mem_sect);
  181. return mem_block;
  182. }
  183. static int get_lmb_range(u32 drc_index, int n_lmbs,
  184. struct drmem_lmb **start_lmb,
  185. struct drmem_lmb **end_lmb)
  186. {
  187. struct drmem_lmb *lmb, *start, *end;
  188. struct drmem_lmb *limit;
  189. start = NULL;
  190. for_each_drmem_lmb(lmb) {
  191. if (lmb->drc_index == drc_index) {
  192. start = lmb;
  193. break;
  194. }
  195. }
  196. if (!start)
  197. return -EINVAL;
  198. end = &start[n_lmbs];
  199. limit = &drmem_info->lmbs[drmem_info->n_lmbs];
  200. if (end > limit)
  201. return -EINVAL;
  202. *start_lmb = start;
  203. *end_lmb = end;
  204. return 0;
  205. }
  206. static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
  207. {
  208. struct memory_block *mem_block;
  209. int rc;
  210. mem_block = lmb_to_memblock(lmb);
  211. if (!mem_block)
  212. return -EINVAL;
  213. if (online && mem_block->dev.offline)
  214. rc = device_online(&mem_block->dev);
  215. else if (!online && !mem_block->dev.offline)
  216. rc = device_offline(&mem_block->dev);
  217. else
  218. rc = 0;
  219. put_device(&mem_block->dev);
  220. return rc;
  221. }
  222. static int dlpar_online_lmb(struct drmem_lmb *lmb)
  223. {
  224. return dlpar_change_lmb_state(lmb, true);
  225. }
  226. #ifdef CONFIG_MEMORY_HOTREMOVE
  227. static int dlpar_offline_lmb(struct drmem_lmb *lmb)
  228. {
  229. return dlpar_change_lmb_state(lmb, false);
  230. }
  231. static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
  232. {
  233. unsigned long block_sz, start_pfn;
  234. int sections_per_block;
  235. int i, nid;
  236. start_pfn = base >> PAGE_SHIFT;
  237. lock_device_hotplug();
  238. if (!pfn_valid(start_pfn))
  239. goto out;
  240. block_sz = pseries_memory_block_size();
  241. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  242. nid = memory_add_physaddr_to_nid(base);
  243. for (i = 0; i < sections_per_block; i++) {
  244. __remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
  245. base += MIN_MEMORY_BLOCK_SIZE;
  246. }
  247. out:
  248. /* Update memory regions for memory remove */
  249. memblock_remove(base, memblock_size);
  250. unlock_device_hotplug();
  251. return 0;
  252. }
  253. static int pseries_remove_mem_node(struct device_node *np)
  254. {
  255. const char *type;
  256. const __be32 *regs;
  257. unsigned long base;
  258. unsigned int lmb_size;
  259. int ret = -EINVAL;
  260. /*
  261. * Check to see if we are actually removing memory
  262. */
  263. type = of_get_property(np, "device_type", NULL);
  264. if (type == NULL || strcmp(type, "memory") != 0)
  265. return 0;
  266. /*
  267. * Find the base address and size of the memblock
  268. */
  269. regs = of_get_property(np, "reg", NULL);
  270. if (!regs)
  271. return ret;
  272. base = be64_to_cpu(*(unsigned long *)regs);
  273. lmb_size = be32_to_cpu(regs[3]);
  274. pseries_remove_memblock(base, lmb_size);
  275. return 0;
  276. }
  277. static bool lmb_is_removable(struct drmem_lmb *lmb)
  278. {
  279. int i, scns_per_block;
  280. int rc = 1;
  281. unsigned long pfn, block_sz;
  282. u64 phys_addr;
  283. if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
  284. return false;
  285. block_sz = memory_block_size_bytes();
  286. scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  287. phys_addr = lmb->base_addr;
  288. #ifdef CONFIG_FA_DUMP
  289. /*
  290. * Don't hot-remove memory that falls in fadump boot memory area
  291. * and memory that is reserved for capturing old kernel memory.
  292. */
  293. if (is_fadump_memory_area(phys_addr, block_sz))
  294. return false;
  295. #endif
  296. for (i = 0; i < scns_per_block; i++) {
  297. pfn = PFN_DOWN(phys_addr);
  298. if (!pfn_present(pfn)) {
  299. phys_addr += MIN_MEMORY_BLOCK_SIZE;
  300. continue;
  301. }
  302. rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
  303. phys_addr += MIN_MEMORY_BLOCK_SIZE;
  304. }
  305. return rc ? true : false;
  306. }
  307. static int dlpar_add_lmb(struct drmem_lmb *);
  308. static int dlpar_remove_lmb(struct drmem_lmb *lmb)
  309. {
  310. unsigned long block_sz;
  311. int nid, rc;
  312. if (!lmb_is_removable(lmb))
  313. return -EINVAL;
  314. rc = dlpar_offline_lmb(lmb);
  315. if (rc)
  316. return rc;
  317. block_sz = pseries_memory_block_size();
  318. nid = memory_add_physaddr_to_nid(lmb->base_addr);
  319. __remove_memory(nid, lmb->base_addr, block_sz);
  320. /* Update memory regions for memory remove */
  321. memblock_remove(lmb->base_addr, block_sz);
  322. invalidate_lmb_associativity_index(lmb);
  323. lmb->flags &= ~DRCONF_MEM_ASSIGNED;
  324. return 0;
  325. }
  326. static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
  327. {
  328. struct drmem_lmb *lmb;
  329. int lmbs_removed = 0;
  330. int lmbs_available = 0;
  331. int rc;
  332. pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
  333. if (lmbs_to_remove == 0)
  334. return -EINVAL;
  335. /* Validate that there are enough LMBs to satisfy the request */
  336. for_each_drmem_lmb(lmb) {
  337. if (lmb_is_removable(lmb))
  338. lmbs_available++;
  339. if (lmbs_available == lmbs_to_remove)
  340. break;
  341. }
  342. if (lmbs_available < lmbs_to_remove) {
  343. pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
  344. lmbs_available, lmbs_to_remove);
  345. return -EINVAL;
  346. }
  347. for_each_drmem_lmb(lmb) {
  348. rc = dlpar_remove_lmb(lmb);
  349. if (rc)
  350. continue;
  351. /* Mark this lmb so we can add it later if all of the
  352. * requested LMBs cannot be removed.
  353. */
  354. drmem_mark_lmb_reserved(lmb);
  355. lmbs_removed++;
  356. if (lmbs_removed == lmbs_to_remove)
  357. break;
  358. }
  359. if (lmbs_removed != lmbs_to_remove) {
  360. pr_err("Memory hot-remove failed, adding LMB's back\n");
  361. for_each_drmem_lmb(lmb) {
  362. if (!drmem_lmb_reserved(lmb))
  363. continue;
  364. rc = dlpar_add_lmb(lmb);
  365. if (rc)
  366. pr_err("Failed to add LMB back, drc index %x\n",
  367. lmb->drc_index);
  368. drmem_remove_lmb_reservation(lmb);
  369. }
  370. rc = -EINVAL;
  371. } else {
  372. for_each_drmem_lmb(lmb) {
  373. if (!drmem_lmb_reserved(lmb))
  374. continue;
  375. dlpar_release_drc(lmb->drc_index);
  376. pr_info("Memory at %llx was hot-removed\n",
  377. lmb->base_addr);
  378. drmem_remove_lmb_reservation(lmb);
  379. }
  380. rc = 0;
  381. }
  382. return rc;
  383. }
  384. static int dlpar_memory_remove_by_index(u32 drc_index)
  385. {
  386. struct drmem_lmb *lmb;
  387. int lmb_found;
  388. int rc;
  389. pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
  390. lmb_found = 0;
  391. for_each_drmem_lmb(lmb) {
  392. if (lmb->drc_index == drc_index) {
  393. lmb_found = 1;
  394. rc = dlpar_remove_lmb(lmb);
  395. if (!rc)
  396. dlpar_release_drc(lmb->drc_index);
  397. break;
  398. }
  399. }
  400. if (!lmb_found)
  401. rc = -EINVAL;
  402. if (rc)
  403. pr_info("Failed to hot-remove memory at %llx\n",
  404. lmb->base_addr);
  405. else
  406. pr_info("Memory at %llx was hot-removed\n", lmb->base_addr);
  407. return rc;
  408. }
  409. static int dlpar_memory_readd_by_index(u32 drc_index)
  410. {
  411. struct drmem_lmb *lmb;
  412. int lmb_found;
  413. int rc;
  414. pr_info("Attempting to update LMB, drc index %x\n", drc_index);
  415. lmb_found = 0;
  416. for_each_drmem_lmb(lmb) {
  417. if (lmb->drc_index == drc_index) {
  418. lmb_found = 1;
  419. rc = dlpar_remove_lmb(lmb);
  420. if (!rc) {
  421. rc = dlpar_add_lmb(lmb);
  422. if (rc)
  423. dlpar_release_drc(lmb->drc_index);
  424. }
  425. break;
  426. }
  427. }
  428. if (!lmb_found)
  429. rc = -EINVAL;
  430. if (rc)
  431. pr_info("Failed to update memory at %llx\n",
  432. lmb->base_addr);
  433. else
  434. pr_info("Memory at %llx was updated\n", lmb->base_addr);
  435. return rc;
  436. }
  437. static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
  438. {
  439. struct drmem_lmb *lmb, *start_lmb, *end_lmb;
  440. int lmbs_available = 0;
  441. int rc;
  442. pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
  443. lmbs_to_remove, drc_index);
  444. if (lmbs_to_remove == 0)
  445. return -EINVAL;
  446. rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
  447. if (rc)
  448. return -EINVAL;
  449. /* Validate that there are enough LMBs to satisfy the request */
  450. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  451. if (lmb->flags & DRCONF_MEM_RESERVED)
  452. break;
  453. lmbs_available++;
  454. }
  455. if (lmbs_available < lmbs_to_remove)
  456. return -EINVAL;
  457. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  458. if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
  459. continue;
  460. rc = dlpar_remove_lmb(lmb);
  461. if (rc)
  462. break;
  463. drmem_mark_lmb_reserved(lmb);
  464. }
  465. if (rc) {
  466. pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
  467. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  468. if (!drmem_lmb_reserved(lmb))
  469. continue;
  470. rc = dlpar_add_lmb(lmb);
  471. if (rc)
  472. pr_err("Failed to add LMB, drc index %x\n",
  473. lmb->drc_index);
  474. drmem_remove_lmb_reservation(lmb);
  475. }
  476. rc = -EINVAL;
  477. } else {
  478. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  479. if (!drmem_lmb_reserved(lmb))
  480. continue;
  481. dlpar_release_drc(lmb->drc_index);
  482. pr_info("Memory at %llx (drc index %x) was hot-removed\n",
  483. lmb->base_addr, lmb->drc_index);
  484. drmem_remove_lmb_reservation(lmb);
  485. }
  486. }
  487. return rc;
  488. }
  489. #else
  490. static inline int pseries_remove_memblock(unsigned long base,
  491. unsigned int memblock_size)
  492. {
  493. return -EOPNOTSUPP;
  494. }
  495. static inline int pseries_remove_mem_node(struct device_node *np)
  496. {
  497. return 0;
  498. }
  499. static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
  500. {
  501. return -EOPNOTSUPP;
  502. }
  503. static int dlpar_remove_lmb(struct drmem_lmb *lmb)
  504. {
  505. return -EOPNOTSUPP;
  506. }
  507. static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
  508. {
  509. return -EOPNOTSUPP;
  510. }
  511. static int dlpar_memory_remove_by_index(u32 drc_index)
  512. {
  513. return -EOPNOTSUPP;
  514. }
  515. static int dlpar_memory_readd_by_index(u32 drc_index)
  516. {
  517. return -EOPNOTSUPP;
  518. }
  519. static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
  520. {
  521. return -EOPNOTSUPP;
  522. }
  523. #endif /* CONFIG_MEMORY_HOTREMOVE */
  524. static int dlpar_add_lmb(struct drmem_lmb *lmb)
  525. {
  526. unsigned long block_sz;
  527. int nid, rc;
  528. if (lmb->flags & DRCONF_MEM_ASSIGNED)
  529. return -EINVAL;
  530. rc = update_lmb_associativity_index(lmb);
  531. if (rc) {
  532. dlpar_release_drc(lmb->drc_index);
  533. return rc;
  534. }
  535. block_sz = memory_block_size_bytes();
  536. /* Find the node id for this address */
  537. nid = memory_add_physaddr_to_nid(lmb->base_addr);
  538. /* Add the memory */
  539. rc = __add_memory(nid, lmb->base_addr, block_sz);
  540. if (rc) {
  541. invalidate_lmb_associativity_index(lmb);
  542. return rc;
  543. }
  544. rc = dlpar_online_lmb(lmb);
  545. if (rc) {
  546. __remove_memory(nid, lmb->base_addr, block_sz);
  547. invalidate_lmb_associativity_index(lmb);
  548. } else {
  549. lmb->flags |= DRCONF_MEM_ASSIGNED;
  550. }
  551. return rc;
  552. }
  553. static int dlpar_memory_add_by_count(u32 lmbs_to_add)
  554. {
  555. struct drmem_lmb *lmb;
  556. int lmbs_available = 0;
  557. int lmbs_added = 0;
  558. int rc;
  559. pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
  560. if (lmbs_to_add == 0)
  561. return -EINVAL;
  562. /* Validate that there are enough LMBs to satisfy the request */
  563. for_each_drmem_lmb(lmb) {
  564. if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
  565. lmbs_available++;
  566. if (lmbs_available == lmbs_to_add)
  567. break;
  568. }
  569. if (lmbs_available < lmbs_to_add)
  570. return -EINVAL;
  571. for_each_drmem_lmb(lmb) {
  572. if (lmb->flags & DRCONF_MEM_ASSIGNED)
  573. continue;
  574. rc = dlpar_acquire_drc(lmb->drc_index);
  575. if (rc)
  576. continue;
  577. rc = dlpar_add_lmb(lmb);
  578. if (rc) {
  579. dlpar_release_drc(lmb->drc_index);
  580. continue;
  581. }
  582. /* Mark this lmb so we can remove it later if all of the
  583. * requested LMBs cannot be added.
  584. */
  585. drmem_mark_lmb_reserved(lmb);
  586. lmbs_added++;
  587. if (lmbs_added == lmbs_to_add)
  588. break;
  589. }
  590. if (lmbs_added != lmbs_to_add) {
  591. pr_err("Memory hot-add failed, removing any added LMBs\n");
  592. for_each_drmem_lmb(lmb) {
  593. if (!drmem_lmb_reserved(lmb))
  594. continue;
  595. rc = dlpar_remove_lmb(lmb);
  596. if (rc)
  597. pr_err("Failed to remove LMB, drc index %x\n",
  598. lmb->drc_index);
  599. else
  600. dlpar_release_drc(lmb->drc_index);
  601. drmem_remove_lmb_reservation(lmb);
  602. }
  603. rc = -EINVAL;
  604. } else {
  605. for_each_drmem_lmb(lmb) {
  606. if (!drmem_lmb_reserved(lmb))
  607. continue;
  608. pr_info("Memory at %llx (drc index %x) was hot-added\n",
  609. lmb->base_addr, lmb->drc_index);
  610. drmem_remove_lmb_reservation(lmb);
  611. }
  612. rc = 0;
  613. }
  614. return rc;
  615. }
  616. static int dlpar_memory_add_by_index(u32 drc_index)
  617. {
  618. struct drmem_lmb *lmb;
  619. int rc, lmb_found;
  620. pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
  621. lmb_found = 0;
  622. for_each_drmem_lmb(lmb) {
  623. if (lmb->drc_index == drc_index) {
  624. lmb_found = 1;
  625. rc = dlpar_acquire_drc(lmb->drc_index);
  626. if (!rc) {
  627. rc = dlpar_add_lmb(lmb);
  628. if (rc)
  629. dlpar_release_drc(lmb->drc_index);
  630. }
  631. break;
  632. }
  633. }
  634. if (!lmb_found)
  635. rc = -EINVAL;
  636. if (rc)
  637. pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
  638. else
  639. pr_info("Memory at %llx (drc index %x) was hot-added\n",
  640. lmb->base_addr, drc_index);
  641. return rc;
  642. }
  643. static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
  644. {
  645. struct drmem_lmb *lmb, *start_lmb, *end_lmb;
  646. int lmbs_available = 0;
  647. int rc;
  648. pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
  649. lmbs_to_add, drc_index);
  650. if (lmbs_to_add == 0)
  651. return -EINVAL;
  652. rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
  653. if (rc)
  654. return -EINVAL;
  655. /* Validate that the LMBs in this range are not reserved */
  656. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  657. if (lmb->flags & DRCONF_MEM_RESERVED)
  658. break;
  659. lmbs_available++;
  660. }
  661. if (lmbs_available < lmbs_to_add)
  662. return -EINVAL;
  663. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  664. if (lmb->flags & DRCONF_MEM_ASSIGNED)
  665. continue;
  666. rc = dlpar_acquire_drc(lmb->drc_index);
  667. if (rc)
  668. break;
  669. rc = dlpar_add_lmb(lmb);
  670. if (rc) {
  671. dlpar_release_drc(lmb->drc_index);
  672. break;
  673. }
  674. drmem_mark_lmb_reserved(lmb);
  675. }
  676. if (rc) {
  677. pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
  678. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  679. if (!drmem_lmb_reserved(lmb))
  680. continue;
  681. rc = dlpar_remove_lmb(lmb);
  682. if (rc)
  683. pr_err("Failed to remove LMB, drc index %x\n",
  684. lmb->drc_index);
  685. else
  686. dlpar_release_drc(lmb->drc_index);
  687. drmem_remove_lmb_reservation(lmb);
  688. }
  689. rc = -EINVAL;
  690. } else {
  691. for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
  692. if (!drmem_lmb_reserved(lmb))
  693. continue;
  694. pr_info("Memory at %llx (drc index %x) was hot-added\n",
  695. lmb->base_addr, lmb->drc_index);
  696. drmem_remove_lmb_reservation(lmb);
  697. }
  698. }
  699. return rc;
  700. }
  701. int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
  702. {
  703. u32 count, drc_index;
  704. int rc;
  705. lock_device_hotplug();
  706. switch (hp_elog->action) {
  707. case PSERIES_HP_ELOG_ACTION_ADD:
  708. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) {
  709. count = hp_elog->_drc_u.drc_count;
  710. rc = dlpar_memory_add_by_count(count);
  711. } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
  712. drc_index = hp_elog->_drc_u.drc_index;
  713. rc = dlpar_memory_add_by_index(drc_index);
  714. } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) {
  715. count = hp_elog->_drc_u.ic.count;
  716. drc_index = hp_elog->_drc_u.ic.index;
  717. rc = dlpar_memory_add_by_ic(count, drc_index);
  718. } else {
  719. rc = -EINVAL;
  720. }
  721. break;
  722. case PSERIES_HP_ELOG_ACTION_REMOVE:
  723. if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) {
  724. count = hp_elog->_drc_u.drc_count;
  725. rc = dlpar_memory_remove_by_count(count);
  726. } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
  727. drc_index = hp_elog->_drc_u.drc_index;
  728. rc = dlpar_memory_remove_by_index(drc_index);
  729. } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) {
  730. count = hp_elog->_drc_u.ic.count;
  731. drc_index = hp_elog->_drc_u.ic.index;
  732. rc = dlpar_memory_remove_by_ic(count, drc_index);
  733. } else {
  734. rc = -EINVAL;
  735. }
  736. break;
  737. case PSERIES_HP_ELOG_ACTION_READD:
  738. drc_index = hp_elog->_drc_u.drc_index;
  739. rc = dlpar_memory_readd_by_index(drc_index);
  740. break;
  741. default:
  742. pr_err("Invalid action (%d) specified\n", hp_elog->action);
  743. rc = -EINVAL;
  744. break;
  745. }
  746. if (!rc) {
  747. rtas_hp_event = true;
  748. rc = drmem_update_dt();
  749. rtas_hp_event = false;
  750. }
  751. unlock_device_hotplug();
  752. return rc;
  753. }
  754. static int pseries_add_mem_node(struct device_node *np)
  755. {
  756. const char *type;
  757. const __be32 *regs;
  758. unsigned long base;
  759. unsigned int lmb_size;
  760. int ret = -EINVAL;
  761. /*
  762. * Check to see if we are actually adding memory
  763. */
  764. type = of_get_property(np, "device_type", NULL);
  765. if (type == NULL || strcmp(type, "memory") != 0)
  766. return 0;
  767. /*
  768. * Find the base and size of the memblock
  769. */
  770. regs = of_get_property(np, "reg", NULL);
  771. if (!regs)
  772. return ret;
  773. base = be64_to_cpu(*(unsigned long *)regs);
  774. lmb_size = be32_to_cpu(regs[3]);
  775. /*
  776. * Update memory region to represent the memory add
  777. */
  778. ret = memblock_add(base, lmb_size);
  779. return (ret < 0) ? -EINVAL : 0;
  780. }
  781. static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
  782. {
  783. struct of_drconf_cell_v1 *new_drmem, *old_drmem;
  784. unsigned long memblock_size;
  785. u32 entries;
  786. __be32 *p;
  787. int i, rc = -EINVAL;
  788. if (rtas_hp_event)
  789. return 0;
  790. memblock_size = pseries_memory_block_size();
  791. if (!memblock_size)
  792. return -EINVAL;
  793. if (!pr->old_prop)
  794. return 0;
  795. p = (__be32 *) pr->old_prop->value;
  796. if (!p)
  797. return -EINVAL;
  798. /* The first int of the property is the number of lmb's described
  799. * by the property. This is followed by an array of of_drconf_cell
  800. * entries. Get the number of entries and skip to the array of
  801. * of_drconf_cell's.
  802. */
  803. entries = be32_to_cpu(*p++);
  804. old_drmem = (struct of_drconf_cell_v1 *)p;
  805. p = (__be32 *)pr->prop->value;
  806. p++;
  807. new_drmem = (struct of_drconf_cell_v1 *)p;
  808. for (i = 0; i < entries; i++) {
  809. if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
  810. (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
  811. rc = pseries_remove_memblock(
  812. be64_to_cpu(old_drmem[i].base_addr),
  813. memblock_size);
  814. break;
  815. } else if ((!(be32_to_cpu(old_drmem[i].flags) &
  816. DRCONF_MEM_ASSIGNED)) &&
  817. (be32_to_cpu(new_drmem[i].flags) &
  818. DRCONF_MEM_ASSIGNED)) {
  819. rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
  820. memblock_size);
  821. rc = (rc < 0) ? -EINVAL : 0;
  822. break;
  823. }
  824. }
  825. return rc;
  826. }
  827. static int pseries_memory_notifier(struct notifier_block *nb,
  828. unsigned long action, void *data)
  829. {
  830. struct of_reconfig_data *rd = data;
  831. int err = 0;
  832. switch (action) {
  833. case OF_RECONFIG_ATTACH_NODE:
  834. err = pseries_add_mem_node(rd->dn);
  835. break;
  836. case OF_RECONFIG_DETACH_NODE:
  837. err = pseries_remove_mem_node(rd->dn);
  838. break;
  839. case OF_RECONFIG_UPDATE_PROPERTY:
  840. if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
  841. err = pseries_update_drconf_memory(rd);
  842. break;
  843. }
  844. return notifier_from_errno(err);
  845. }
  846. static struct notifier_block pseries_mem_nb = {
  847. .notifier_call = pseries_memory_notifier,
  848. };
  849. static int __init pseries_memory_hotplug_init(void)
  850. {
  851. if (firmware_has_feature(FW_FEATURE_LPAR))
  852. of_reconfig_notifier_register(&pseries_mem_nb);
  853. return 0;
  854. }
  855. machine_device_initcall(pseries, pseries_memory_hotplug_init);