hotplug-memory.c 23 KB

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