xpc_partition.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) partition support.
  10. *
  11. * This is the part of XPC that detects the presence/absence of
  12. * other partitions. It provides a heartbeat and monitors the
  13. * heartbeats of other partitions.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/cache.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/nodemask.h>
  21. #include <asm/uncached.h>
  22. #include <asm/sn/bte.h>
  23. #include <asm/sn/intr.h>
  24. #include <asm/sn/sn_sal.h>
  25. #include <asm/sn/nodepda.h>
  26. #include <asm/sn/addrs.h>
  27. #include "xpc.h"
  28. /* XPC is exiting flag */
  29. int xpc_exiting;
  30. /* SH_IPI_ACCESS shub register value on startup */
  31. static u64 xpc_sh1_IPI_access;
  32. static u64 xpc_sh2_IPI_access0;
  33. static u64 xpc_sh2_IPI_access1;
  34. static u64 xpc_sh2_IPI_access2;
  35. static u64 xpc_sh2_IPI_access3;
  36. /* original protection values for each node */
  37. u64 xpc_prot_vec[MAX_NUMNODES];
  38. /* this partition's reserved page pointers */
  39. struct xpc_rsvd_page *xpc_rsvd_page;
  40. static u64 *xpc_part_nasids;
  41. static u64 *xpc_mach_nasids;
  42. struct xpc_vars *xpc_vars;
  43. struct xpc_vars_part *xpc_vars_part;
  44. static int xp_nasid_mask_bytes; /* actual size in bytes of nasid mask */
  45. static int xp_nasid_mask_words; /* actual size in words of nasid mask */
  46. struct xpc_partition *xpc_partitions;
  47. /*
  48. * Generic buffer used to store a local copy of portions of a remote
  49. * partition's reserved page (either its header and part_nasids mask,
  50. * or its vars).
  51. */
  52. char *xpc_remote_copy_buffer;
  53. void *xpc_remote_copy_buffer_base;
  54. /*
  55. * Guarantee that the kmalloc'd memory is cacheline aligned.
  56. */
  57. void *
  58. xpc_kmalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
  59. {
  60. /* see if kmalloc will give us cachline aligned memory by default */
  61. *base = kmalloc(size, flags);
  62. if (*base == NULL)
  63. return NULL;
  64. if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
  65. return *base;
  66. kfree(*base);
  67. /* nope, we'll have to do it ourselves */
  68. *base = kmalloc(size + L1_CACHE_BYTES, flags);
  69. if (*base == NULL)
  70. return NULL;
  71. return (void *)L1_CACHE_ALIGN((u64)*base);
  72. }
  73. /*
  74. * Given a nasid, get the physical address of the partition's reserved page
  75. * for that nasid. This function returns 0 on any error.
  76. */
  77. static u64
  78. xpc_get_rsvd_page_pa(int nasid)
  79. {
  80. bte_result_t bte_res;
  81. s64 status;
  82. u64 cookie = 0;
  83. u64 rp_pa = nasid; /* seed with nasid */
  84. u64 len = 0;
  85. u64 buf = buf;
  86. u64 buf_len = 0;
  87. void *buf_base = NULL;
  88. while (1) {
  89. status = sn_partition_reserved_page_pa(buf, &cookie, &rp_pa,
  90. &len);
  91. dev_dbg(xpc_part, "SAL returned with status=%li, cookie="
  92. "0x%016lx, address=0x%016lx, len=0x%016lx\n",
  93. status, cookie, rp_pa, len);
  94. if (status != SALRET_MORE_PASSES)
  95. break;
  96. if (L1_CACHE_ALIGN(len) > buf_len) {
  97. kfree(buf_base);
  98. buf_len = L1_CACHE_ALIGN(len);
  99. buf = (u64)xpc_kmalloc_cacheline_aligned(buf_len,
  100. GFP_KERNEL,
  101. &buf_base);
  102. if (buf_base == NULL) {
  103. dev_err(xpc_part, "unable to kmalloc "
  104. "len=0x%016lx\n", buf_len);
  105. status = SALRET_ERROR;
  106. break;
  107. }
  108. }
  109. bte_res = xp_bte_copy(rp_pa, buf, buf_len,
  110. (BTE_NOTIFY | BTE_WACQUIRE), NULL);
  111. if (bte_res != BTE_SUCCESS) {
  112. dev_dbg(xpc_part, "xp_bte_copy failed %i\n", bte_res);
  113. status = SALRET_ERROR;
  114. break;
  115. }
  116. }
  117. kfree(buf_base);
  118. if (status != SALRET_OK)
  119. rp_pa = 0;
  120. dev_dbg(xpc_part, "reserved page at phys address 0x%016lx\n", rp_pa);
  121. return rp_pa;
  122. }
  123. /*
  124. * Fill the partition reserved page with the information needed by
  125. * other partitions to discover we are alive and establish initial
  126. * communications.
  127. */
  128. struct xpc_rsvd_page *
  129. xpc_rsvd_page_init(void)
  130. {
  131. struct xpc_rsvd_page *rp;
  132. AMO_t *amos_page;
  133. u64 rp_pa, nasid_array = 0;
  134. int i, ret;
  135. /* get the local reserved page's address */
  136. preempt_disable();
  137. rp_pa = xpc_get_rsvd_page_pa(cpuid_to_nasid(smp_processor_id()));
  138. preempt_enable();
  139. if (rp_pa == 0) {
  140. dev_err(xpc_part, "SAL failed to locate the reserved page\n");
  141. return NULL;
  142. }
  143. rp = (struct xpc_rsvd_page *)__va(rp_pa);
  144. if (rp->partid != sn_partition_id) {
  145. dev_err(xpc_part, "the reserved page's partid of %d should be "
  146. "%d\n", rp->partid, sn_partition_id);
  147. return NULL;
  148. }
  149. rp->version = XPC_RP_VERSION;
  150. /* establish the actual sizes of the nasid masks */
  151. if (rp->SAL_version == 1) {
  152. /* SAL_version 1 didn't set the nasids_size field */
  153. rp->nasids_size = 128;
  154. }
  155. xp_nasid_mask_bytes = rp->nasids_size;
  156. xp_nasid_mask_words = xp_nasid_mask_bytes / 8;
  157. /* setup the pointers to the various items in the reserved page */
  158. xpc_part_nasids = XPC_RP_PART_NASIDS(rp);
  159. xpc_mach_nasids = XPC_RP_MACH_NASIDS(rp);
  160. xpc_vars = XPC_RP_VARS(rp);
  161. xpc_vars_part = XPC_RP_VARS_PART(rp);
  162. /*
  163. * Before clearing xpc_vars, see if a page of AMOs had been previously
  164. * allocated. If not we'll need to allocate one and set permissions
  165. * so that cross-partition AMOs are allowed.
  166. *
  167. * The allocated AMO page needs MCA reporting to remain disabled after
  168. * XPC has unloaded. To make this work, we keep a copy of the pointer
  169. * to this page (i.e., amos_page) in the struct xpc_vars structure,
  170. * which is pointed to by the reserved page, and re-use that saved copy
  171. * on subsequent loads of XPC. This AMO page is never freed, and its
  172. * memory protections are never restricted.
  173. */
  174. amos_page = xpc_vars->amos_page;
  175. if (amos_page == NULL) {
  176. amos_page = (AMO_t *)TO_AMO(uncached_alloc_page(0, 1));
  177. if (amos_page == NULL) {
  178. dev_err(xpc_part, "can't allocate page of AMOs\n");
  179. return NULL;
  180. }
  181. /*
  182. * Open up AMO-R/W to cpu. This is done for Shub 1.1 systems
  183. * when xpc_allow_IPI_ops() is called via xpc_hb_init().
  184. */
  185. if (!enable_shub_wars_1_1()) {
  186. ret = sn_change_memprotect(ia64_tpa((u64)amos_page),
  187. PAGE_SIZE,
  188. SN_MEMPROT_ACCESS_CLASS_1,
  189. &nasid_array);
  190. if (ret != 0) {
  191. dev_err(xpc_part, "can't change memory "
  192. "protections\n");
  193. uncached_free_page(__IA64_UNCACHED_OFFSET |
  194. TO_PHYS((u64)amos_page), 1);
  195. return NULL;
  196. }
  197. }
  198. } else if (!IS_AMO_ADDRESS((u64)amos_page)) {
  199. /*
  200. * EFI's XPBOOT can also set amos_page in the reserved page,
  201. * but it happens to leave it as an uncached physical address
  202. * and we need it to be an uncached virtual, so we'll have to
  203. * convert it.
  204. */
  205. if (!IS_AMO_PHYS_ADDRESS((u64)amos_page)) {
  206. dev_err(xpc_part, "previously used amos_page address "
  207. "is bad = 0x%p\n", (void *)amos_page);
  208. return NULL;
  209. }
  210. amos_page = (AMO_t *)TO_AMO((u64)amos_page);
  211. }
  212. /* clear xpc_vars */
  213. memset(xpc_vars, 0, sizeof(struct xpc_vars));
  214. xpc_vars->version = XPC_V_VERSION;
  215. xpc_vars->act_nasid = cpuid_to_nasid(0);
  216. xpc_vars->act_phys_cpuid = cpu_physical_id(0);
  217. xpc_vars->vars_part_pa = __pa(xpc_vars_part);
  218. xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
  219. xpc_vars->amos_page = amos_page; /* save for next load of XPC */
  220. /* clear xpc_vars_part */
  221. memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part) *
  222. xp_max_npartitions);
  223. /* initialize the activate IRQ related AMO variables */
  224. for (i = 0; i < xp_nasid_mask_words; i++)
  225. (void)xpc_IPI_init(XPC_ACTIVATE_IRQ_AMOS + i);
  226. /* initialize the engaged remote partitions related AMO variables */
  227. (void)xpc_IPI_init(XPC_ENGAGED_PARTITIONS_AMO);
  228. (void)xpc_IPI_init(XPC_DISENGAGE_REQUEST_AMO);
  229. /* timestamp of when reserved page was setup by XPC */
  230. rp->stamp = CURRENT_TIME;
  231. /*
  232. * This signifies to the remote partition that our reserved
  233. * page is initialized.
  234. */
  235. rp->vars_pa = __pa(xpc_vars);
  236. return rp;
  237. }
  238. /*
  239. * Change protections to allow IPI operations (and AMO operations on
  240. * Shub 1.1 systems).
  241. */
  242. void
  243. xpc_allow_IPI_ops(void)
  244. {
  245. int node;
  246. int nasid;
  247. /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
  248. if (is_shub2()) {
  249. xpc_sh2_IPI_access0 =
  250. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
  251. xpc_sh2_IPI_access1 =
  252. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
  253. xpc_sh2_IPI_access2 =
  254. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
  255. xpc_sh2_IPI_access3 =
  256. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
  257. for_each_online_node(node) {
  258. nasid = cnodeid_to_nasid(node);
  259. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  260. -1UL);
  261. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  262. -1UL);
  263. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  264. -1UL);
  265. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  266. -1UL);
  267. }
  268. } else {
  269. xpc_sh1_IPI_access =
  270. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
  271. for_each_online_node(node) {
  272. nasid = cnodeid_to_nasid(node);
  273. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  274. -1UL);
  275. /*
  276. * Since the BIST collides with memory operations on
  277. * SHUB 1.1 sn_change_memprotect() cannot be used.
  278. */
  279. if (enable_shub_wars_1_1()) {
  280. /* open up everything */
  281. xpc_prot_vec[node] = (u64)HUB_L((u64 *)
  282. GLOBAL_MMR_ADDR
  283. (nasid,
  284. SH1_MD_DQLP_MMR_DIR_PRIVEC0));
  285. HUB_S((u64 *)
  286. GLOBAL_MMR_ADDR(nasid,
  287. SH1_MD_DQLP_MMR_DIR_PRIVEC0),
  288. -1UL);
  289. HUB_S((u64 *)
  290. GLOBAL_MMR_ADDR(nasid,
  291. SH1_MD_DQRP_MMR_DIR_PRIVEC0),
  292. -1UL);
  293. }
  294. }
  295. }
  296. }
  297. /*
  298. * Restrict protections to disallow IPI operations (and AMO operations on
  299. * Shub 1.1 systems).
  300. */
  301. void
  302. xpc_restrict_IPI_ops(void)
  303. {
  304. int node;
  305. int nasid;
  306. /* >>> Change SH_IPI_ACCESS code to use SAL call once it is available */
  307. if (is_shub2()) {
  308. for_each_online_node(node) {
  309. nasid = cnodeid_to_nasid(node);
  310. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  311. xpc_sh2_IPI_access0);
  312. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  313. xpc_sh2_IPI_access1);
  314. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  315. xpc_sh2_IPI_access2);
  316. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  317. xpc_sh2_IPI_access3);
  318. }
  319. } else {
  320. for_each_online_node(node) {
  321. nasid = cnodeid_to_nasid(node);
  322. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  323. xpc_sh1_IPI_access);
  324. if (enable_shub_wars_1_1()) {
  325. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  326. SH1_MD_DQLP_MMR_DIR_PRIVEC0),
  327. xpc_prot_vec[node]);
  328. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  329. SH1_MD_DQRP_MMR_DIR_PRIVEC0),
  330. xpc_prot_vec[node]);
  331. }
  332. }
  333. }
  334. }
  335. /*
  336. * At periodic intervals, scan through all active partitions and ensure
  337. * their heartbeat is still active. If not, the partition is deactivated.
  338. */
  339. void
  340. xpc_check_remote_hb(void)
  341. {
  342. struct xpc_vars *remote_vars;
  343. struct xpc_partition *part;
  344. short partid;
  345. bte_result_t bres;
  346. remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
  347. for (partid = 0; partid < xp_max_npartitions; partid++) {
  348. if (xpc_exiting)
  349. break;
  350. if (partid == sn_partition_id)
  351. continue;
  352. part = &xpc_partitions[partid];
  353. if (part->act_state == XPC_P_INACTIVE ||
  354. part->act_state == XPC_P_DEACTIVATING) {
  355. continue;
  356. }
  357. /* pull the remote_hb cache line */
  358. bres = xp_bte_copy(part->remote_vars_pa,
  359. (u64)remote_vars,
  360. XPC_RP_VARS_SIZE,
  361. (BTE_NOTIFY | BTE_WACQUIRE), NULL);
  362. if (bres != BTE_SUCCESS) {
  363. XPC_DEACTIVATE_PARTITION(part,
  364. xpc_map_bte_errors(bres));
  365. continue;
  366. }
  367. dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
  368. " = %ld, heartbeat_offline = %ld, HB_mask = 0x%lx\n",
  369. partid, remote_vars->heartbeat, part->last_heartbeat,
  370. remote_vars->heartbeat_offline,
  371. remote_vars->heartbeating_to_mask);
  372. if (((remote_vars->heartbeat == part->last_heartbeat) &&
  373. (remote_vars->heartbeat_offline == 0)) ||
  374. !xpc_hb_allowed(sn_partition_id, remote_vars)) {
  375. XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
  376. continue;
  377. }
  378. part->last_heartbeat = remote_vars->heartbeat;
  379. }
  380. }
  381. /*
  382. * Get a copy of a portion of the remote partition's rsvd page.
  383. *
  384. * remote_rp points to a buffer that is cacheline aligned for BTE copies and
  385. * is large enough to contain a copy of their reserved page header and
  386. * part_nasids mask.
  387. */
  388. static enum xp_retval
  389. xpc_get_remote_rp(int nasid, u64 *discovered_nasids,
  390. struct xpc_rsvd_page *remote_rp, u64 *remote_rp_pa)
  391. {
  392. int bres, i;
  393. /* get the reserved page's physical address */
  394. *remote_rp_pa = xpc_get_rsvd_page_pa(nasid);
  395. if (*remote_rp_pa == 0)
  396. return xpNoRsvdPageAddr;
  397. /* pull over the reserved page header and part_nasids mask */
  398. bres = xp_bte_copy(*remote_rp_pa, (u64)remote_rp,
  399. XPC_RP_HEADER_SIZE + xp_nasid_mask_bytes,
  400. (BTE_NOTIFY | BTE_WACQUIRE), NULL);
  401. if (bres != BTE_SUCCESS)
  402. return xpc_map_bte_errors(bres);
  403. if (discovered_nasids != NULL) {
  404. u64 *remote_part_nasids = XPC_RP_PART_NASIDS(remote_rp);
  405. for (i = 0; i < xp_nasid_mask_words; i++)
  406. discovered_nasids[i] |= remote_part_nasids[i];
  407. }
  408. /* check that the partid is for another partition */
  409. if (remote_rp->partid < 0 || remote_rp->partid >= xp_max_npartitions)
  410. return xpInvalidPartid;
  411. if (remote_rp->partid == sn_partition_id)
  412. return xpLocalPartid;
  413. if (XPC_VERSION_MAJOR(remote_rp->version) !=
  414. XPC_VERSION_MAJOR(XPC_RP_VERSION)) {
  415. return xpBadVersion;
  416. }
  417. return xpSuccess;
  418. }
  419. /*
  420. * Get a copy of the remote partition's XPC variables from the reserved page.
  421. *
  422. * remote_vars points to a buffer that is cacheline aligned for BTE copies and
  423. * assumed to be of size XPC_RP_VARS_SIZE.
  424. */
  425. static enum xp_retval
  426. xpc_get_remote_vars(u64 remote_vars_pa, struct xpc_vars *remote_vars)
  427. {
  428. int bres;
  429. if (remote_vars_pa == 0)
  430. return xpVarsNotSet;
  431. /* pull over the cross partition variables */
  432. bres = xp_bte_copy(remote_vars_pa, (u64)remote_vars, XPC_RP_VARS_SIZE,
  433. (BTE_NOTIFY | BTE_WACQUIRE), NULL);
  434. if (bres != BTE_SUCCESS)
  435. return xpc_map_bte_errors(bres);
  436. if (XPC_VERSION_MAJOR(remote_vars->version) !=
  437. XPC_VERSION_MAJOR(XPC_V_VERSION)) {
  438. return xpBadVersion;
  439. }
  440. return xpSuccess;
  441. }
  442. /*
  443. * Update the remote partition's info.
  444. */
  445. static void
  446. xpc_update_partition_info(struct xpc_partition *part, u8 remote_rp_version,
  447. struct timespec *remote_rp_stamp, u64 remote_rp_pa,
  448. u64 remote_vars_pa, struct xpc_vars *remote_vars)
  449. {
  450. part->remote_rp_version = remote_rp_version;
  451. dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
  452. part->remote_rp_version);
  453. part->remote_rp_stamp = *remote_rp_stamp;
  454. dev_dbg(xpc_part, " remote_rp_stamp (tv_sec = 0x%lx tv_nsec = 0x%lx\n",
  455. part->remote_rp_stamp.tv_sec, part->remote_rp_stamp.tv_nsec);
  456. part->remote_rp_pa = remote_rp_pa;
  457. dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
  458. part->remote_vars_pa = remote_vars_pa;
  459. dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
  460. part->remote_vars_pa);
  461. part->last_heartbeat = remote_vars->heartbeat;
  462. dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
  463. part->last_heartbeat);
  464. part->remote_vars_part_pa = remote_vars->vars_part_pa;
  465. dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
  466. part->remote_vars_part_pa);
  467. part->remote_act_nasid = remote_vars->act_nasid;
  468. dev_dbg(xpc_part, " remote_act_nasid = 0x%x\n",
  469. part->remote_act_nasid);
  470. part->remote_act_phys_cpuid = remote_vars->act_phys_cpuid;
  471. dev_dbg(xpc_part, " remote_act_phys_cpuid = 0x%x\n",
  472. part->remote_act_phys_cpuid);
  473. part->remote_amos_page_pa = remote_vars->amos_page_pa;
  474. dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
  475. part->remote_amos_page_pa);
  476. part->remote_vars_version = remote_vars->version;
  477. dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
  478. part->remote_vars_version);
  479. }
  480. /*
  481. * Prior code has determined the nasid which generated an IPI. Inspect
  482. * that nasid to determine if its partition needs to be activated or
  483. * deactivated.
  484. *
  485. * A partition is consider "awaiting activation" if our partition
  486. * flags indicate it is not active and it has a heartbeat. A
  487. * partition is considered "awaiting deactivation" if our partition
  488. * flags indicate it is active but it has no heartbeat or it is not
  489. * sending its heartbeat to us.
  490. *
  491. * To determine the heartbeat, the remote nasid must have a properly
  492. * initialized reserved page.
  493. */
  494. static void
  495. xpc_identify_act_IRQ_req(int nasid)
  496. {
  497. struct xpc_rsvd_page *remote_rp;
  498. struct xpc_vars *remote_vars;
  499. u64 remote_rp_pa;
  500. u64 remote_vars_pa;
  501. int remote_rp_version;
  502. int reactivate = 0;
  503. int stamp_diff;
  504. struct timespec remote_rp_stamp = { 0, 0 };
  505. short partid;
  506. struct xpc_partition *part;
  507. enum xp_retval ret;
  508. /* pull over the reserved page structure */
  509. remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
  510. ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
  511. if (ret != xpSuccess) {
  512. dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
  513. "which sent interrupt, reason=%d\n", nasid, ret);
  514. return;
  515. }
  516. remote_vars_pa = remote_rp->vars_pa;
  517. remote_rp_version = remote_rp->version;
  518. if (XPC_SUPPORTS_RP_STAMP(remote_rp_version))
  519. remote_rp_stamp = remote_rp->stamp;
  520. partid = remote_rp->partid;
  521. part = &xpc_partitions[partid];
  522. /* pull over the cross partition variables */
  523. remote_vars = (struct xpc_vars *)xpc_remote_copy_buffer;
  524. ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
  525. if (ret != xpSuccess) {
  526. dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
  527. "which sent interrupt, reason=%d\n", nasid, ret);
  528. XPC_DEACTIVATE_PARTITION(part, ret);
  529. return;
  530. }
  531. part->act_IRQ_rcvd++;
  532. dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
  533. "%ld:0x%lx\n", (int)nasid, (int)partid, part->act_IRQ_rcvd,
  534. remote_vars->heartbeat, remote_vars->heartbeating_to_mask);
  535. if (xpc_partition_disengaged(part) &&
  536. part->act_state == XPC_P_INACTIVE) {
  537. xpc_update_partition_info(part, remote_rp_version,
  538. &remote_rp_stamp, remote_rp_pa,
  539. remote_vars_pa, remote_vars);
  540. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
  541. if (xpc_partition_disengage_requested(1UL << partid)) {
  542. /*
  543. * Other side is waiting on us to disengage,
  544. * even though we already have.
  545. */
  546. return;
  547. }
  548. } else {
  549. /* other side doesn't support disengage requests */
  550. xpc_clear_partition_disengage_request(1UL << partid);
  551. }
  552. xpc_activate_partition(part);
  553. return;
  554. }
  555. DBUG_ON(part->remote_rp_version == 0);
  556. DBUG_ON(part->remote_vars_version == 0);
  557. if (!XPC_SUPPORTS_RP_STAMP(part->remote_rp_version)) {
  558. DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(part->
  559. remote_vars_version));
  560. if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
  561. DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
  562. version));
  563. /* see if the other side rebooted */
  564. if (part->remote_amos_page_pa ==
  565. remote_vars->amos_page_pa &&
  566. xpc_hb_allowed(sn_partition_id, remote_vars)) {
  567. /* doesn't look that way, so ignore the IPI */
  568. return;
  569. }
  570. }
  571. /*
  572. * Other side rebooted and previous XPC didn't support the
  573. * disengage request, so we don't need to do anything special.
  574. */
  575. xpc_update_partition_info(part, remote_rp_version,
  576. &remote_rp_stamp, remote_rp_pa,
  577. remote_vars_pa, remote_vars);
  578. part->reactivate_nasid = nasid;
  579. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  580. return;
  581. }
  582. DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version));
  583. if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
  584. DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
  585. /*
  586. * Other side rebooted and previous XPC did support the
  587. * disengage request, but the new one doesn't.
  588. */
  589. xpc_clear_partition_engaged(1UL << partid);
  590. xpc_clear_partition_disengage_request(1UL << partid);
  591. xpc_update_partition_info(part, remote_rp_version,
  592. &remote_rp_stamp, remote_rp_pa,
  593. remote_vars_pa, remote_vars);
  594. reactivate = 1;
  595. } else {
  596. DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
  597. stamp_diff = xpc_compare_stamps(&part->remote_rp_stamp,
  598. &remote_rp_stamp);
  599. if (stamp_diff != 0) {
  600. DBUG_ON(stamp_diff >= 0);
  601. /*
  602. * Other side rebooted and the previous XPC did support
  603. * the disengage request, as does the new one.
  604. */
  605. DBUG_ON(xpc_partition_engaged(1UL << partid));
  606. DBUG_ON(xpc_partition_disengage_requested(1UL <<
  607. partid));
  608. xpc_update_partition_info(part, remote_rp_version,
  609. &remote_rp_stamp,
  610. remote_rp_pa, remote_vars_pa,
  611. remote_vars);
  612. reactivate = 1;
  613. }
  614. }
  615. if (part->disengage_request_timeout > 0 &&
  616. !xpc_partition_disengaged(part)) {
  617. /* still waiting on other side to disengage from us */
  618. return;
  619. }
  620. if (reactivate) {
  621. part->reactivate_nasid = nasid;
  622. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  623. } else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) &&
  624. xpc_partition_disengage_requested(1UL << partid)) {
  625. XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
  626. }
  627. }
  628. /*
  629. * Loop through the activation AMO variables and process any bits
  630. * which are set. Each bit indicates a nasid sending a partition
  631. * activation or deactivation request.
  632. *
  633. * Return #of IRQs detected.
  634. */
  635. int
  636. xpc_identify_act_IRQ_sender(void)
  637. {
  638. int word, bit;
  639. u64 nasid_mask;
  640. u64 nasid; /* remote nasid */
  641. int n_IRQs_detected = 0;
  642. AMO_t *act_amos;
  643. act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
  644. /* scan through act AMO variable looking for non-zero entries */
  645. for (word = 0; word < xp_nasid_mask_words; word++) {
  646. if (xpc_exiting)
  647. break;
  648. nasid_mask = xpc_IPI_receive(&act_amos[word]);
  649. if (nasid_mask == 0) {
  650. /* no IRQs from nasids in this variable */
  651. continue;
  652. }
  653. dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
  654. nasid_mask);
  655. /*
  656. * If this nasid has been added to the machine since
  657. * our partition was reset, this will retain the
  658. * remote nasid in our reserved pages machine mask.
  659. * This is used in the event of module reload.
  660. */
  661. xpc_mach_nasids[word] |= nasid_mask;
  662. /* locate the nasid(s) which sent interrupts */
  663. for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
  664. if (nasid_mask & (1UL << bit)) {
  665. n_IRQs_detected++;
  666. nasid = XPC_NASID_FROM_W_B(word, bit);
  667. dev_dbg(xpc_part, "interrupt from nasid %ld\n",
  668. nasid);
  669. xpc_identify_act_IRQ_req(nasid);
  670. }
  671. }
  672. }
  673. return n_IRQs_detected;
  674. }
  675. /*
  676. * See if the other side has responded to a partition disengage request
  677. * from us.
  678. */
  679. int
  680. xpc_partition_disengaged(struct xpc_partition *part)
  681. {
  682. short partid = XPC_PARTID(part);
  683. int disengaged;
  684. disengaged = (xpc_partition_engaged(1UL << partid) == 0);
  685. if (part->disengage_request_timeout) {
  686. if (!disengaged) {
  687. if (time_before(jiffies,
  688. part->disengage_request_timeout)) {
  689. /* timelimit hasn't been reached yet */
  690. return 0;
  691. }
  692. /*
  693. * Other side hasn't responded to our disengage
  694. * request in a timely fashion, so assume it's dead.
  695. */
  696. dev_info(xpc_part, "disengage from remote partition %d "
  697. "timed out\n", partid);
  698. xpc_disengage_request_timedout = 1;
  699. xpc_clear_partition_engaged(1UL << partid);
  700. disengaged = 1;
  701. }
  702. part->disengage_request_timeout = 0;
  703. /* cancel the timer function, provided it's not us */
  704. if (!in_interrupt()) {
  705. del_singleshot_timer_sync(&part->
  706. disengage_request_timer);
  707. }
  708. DBUG_ON(part->act_state != XPC_P_DEACTIVATING &&
  709. part->act_state != XPC_P_INACTIVE);
  710. if (part->act_state != XPC_P_INACTIVE)
  711. xpc_wakeup_channel_mgr(part);
  712. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version))
  713. xpc_cancel_partition_disengage_request(part);
  714. }
  715. return disengaged;
  716. }
  717. /*
  718. * Mark specified partition as active.
  719. */
  720. enum xp_retval
  721. xpc_mark_partition_active(struct xpc_partition *part)
  722. {
  723. unsigned long irq_flags;
  724. enum xp_retval ret;
  725. dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
  726. spin_lock_irqsave(&part->act_lock, irq_flags);
  727. if (part->act_state == XPC_P_ACTIVATING) {
  728. part->act_state = XPC_P_ACTIVE;
  729. ret = xpSuccess;
  730. } else {
  731. DBUG_ON(part->reason == xpSuccess);
  732. ret = part->reason;
  733. }
  734. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  735. return ret;
  736. }
  737. /*
  738. * Notify XPC that the partition is down.
  739. */
  740. void
  741. xpc_deactivate_partition(const int line, struct xpc_partition *part,
  742. enum xp_retval reason)
  743. {
  744. unsigned long irq_flags;
  745. spin_lock_irqsave(&part->act_lock, irq_flags);
  746. if (part->act_state == XPC_P_INACTIVE) {
  747. XPC_SET_REASON(part, reason, line);
  748. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  749. if (reason == xpReactivating) {
  750. /* we interrupt ourselves to reactivate partition */
  751. xpc_IPI_send_reactivate(part);
  752. }
  753. return;
  754. }
  755. if (part->act_state == XPC_P_DEACTIVATING) {
  756. if ((part->reason == xpUnloading && reason != xpUnloading) ||
  757. reason == xpReactivating) {
  758. XPC_SET_REASON(part, reason, line);
  759. }
  760. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  761. return;
  762. }
  763. part->act_state = XPC_P_DEACTIVATING;
  764. XPC_SET_REASON(part, reason, line);
  765. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  766. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
  767. xpc_request_partition_disengage(part);
  768. xpc_IPI_send_disengage(part);
  769. /* set a timelimit on the disengage request */
  770. part->disengage_request_timeout = jiffies +
  771. (xpc_disengage_request_timelimit * HZ);
  772. part->disengage_request_timer.expires =
  773. part->disengage_request_timeout;
  774. add_timer(&part->disengage_request_timer);
  775. }
  776. dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
  777. XPC_PARTID(part), reason);
  778. xpc_partition_going_down(part, reason);
  779. }
  780. /*
  781. * Mark specified partition as inactive.
  782. */
  783. void
  784. xpc_mark_partition_inactive(struct xpc_partition *part)
  785. {
  786. unsigned long irq_flags;
  787. dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
  788. XPC_PARTID(part));
  789. spin_lock_irqsave(&part->act_lock, irq_flags);
  790. part->act_state = XPC_P_INACTIVE;
  791. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  792. part->remote_rp_pa = 0;
  793. }
  794. /*
  795. * SAL has provided a partition and machine mask. The partition mask
  796. * contains a bit for each even nasid in our partition. The machine
  797. * mask contains a bit for each even nasid in the entire machine.
  798. *
  799. * Using those two bit arrays, we can determine which nasids are
  800. * known in the machine. Each should also have a reserved page
  801. * initialized if they are available for partitioning.
  802. */
  803. void
  804. xpc_discovery(void)
  805. {
  806. void *remote_rp_base;
  807. struct xpc_rsvd_page *remote_rp;
  808. struct xpc_vars *remote_vars;
  809. u64 remote_rp_pa;
  810. u64 remote_vars_pa;
  811. int region;
  812. int region_size;
  813. int max_regions;
  814. int nasid;
  815. struct xpc_rsvd_page *rp;
  816. short partid;
  817. struct xpc_partition *part;
  818. u64 *discovered_nasids;
  819. enum xp_retval ret;
  820. remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
  821. xp_nasid_mask_bytes,
  822. GFP_KERNEL, &remote_rp_base);
  823. if (remote_rp == NULL)
  824. return;
  825. remote_vars = (struct xpc_vars *)remote_rp;
  826. discovered_nasids = kzalloc(sizeof(u64) * xp_nasid_mask_words,
  827. GFP_KERNEL);
  828. if (discovered_nasids == NULL) {
  829. kfree(remote_rp_base);
  830. return;
  831. }
  832. rp = (struct xpc_rsvd_page *)xpc_rsvd_page;
  833. /*
  834. * The term 'region' in this context refers to the minimum number of
  835. * nodes that can comprise an access protection grouping. The access
  836. * protection is in regards to memory, IOI and IPI.
  837. */
  838. max_regions = 64;
  839. region_size = sn_region_size;
  840. switch (region_size) {
  841. case 128:
  842. max_regions *= 2;
  843. case 64:
  844. max_regions *= 2;
  845. case 32:
  846. max_regions *= 2;
  847. region_size = 16;
  848. DBUG_ON(!is_shub2());
  849. }
  850. for (region = 0; region < max_regions; region++) {
  851. if (xpc_exiting)
  852. break;
  853. dev_dbg(xpc_part, "searching region %d\n", region);
  854. for (nasid = (region * region_size * 2);
  855. nasid < ((region + 1) * region_size * 2); nasid += 2) {
  856. if (xpc_exiting)
  857. break;
  858. dev_dbg(xpc_part, "checking nasid %d\n", nasid);
  859. if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
  860. dev_dbg(xpc_part, "PROM indicates Nasid %d is "
  861. "part of the local partition; skipping "
  862. "region\n", nasid);
  863. break;
  864. }
  865. if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
  866. dev_dbg(xpc_part, "PROM indicates Nasid %d was "
  867. "not on Numa-Link network at reset\n",
  868. nasid);
  869. continue;
  870. }
  871. if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
  872. dev_dbg(xpc_part, "Nasid %d is part of a "
  873. "partition which was previously "
  874. "discovered\n", nasid);
  875. continue;
  876. }
  877. /* pull over the reserved page structure */
  878. ret = xpc_get_remote_rp(nasid, discovered_nasids,
  879. remote_rp, &remote_rp_pa);
  880. if (ret != xpSuccess) {
  881. dev_dbg(xpc_part, "unable to get reserved page "
  882. "from nasid %d, reason=%d\n", nasid,
  883. ret);
  884. if (ret == xpLocalPartid)
  885. break;
  886. continue;
  887. }
  888. remote_vars_pa = remote_rp->vars_pa;
  889. partid = remote_rp->partid;
  890. part = &xpc_partitions[partid];
  891. /* pull over the cross partition variables */
  892. ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
  893. if (ret != xpSuccess) {
  894. dev_dbg(xpc_part, "unable to get XPC variables "
  895. "from nasid %d, reason=%d\n", nasid,
  896. ret);
  897. XPC_DEACTIVATE_PARTITION(part, ret);
  898. continue;
  899. }
  900. if (part->act_state != XPC_P_INACTIVE) {
  901. dev_dbg(xpc_part, "partition %d on nasid %d is "
  902. "already activating\n", partid, nasid);
  903. break;
  904. }
  905. /*
  906. * Register the remote partition's AMOs with SAL so it
  907. * can handle and cleanup errors within that address
  908. * range should the remote partition go down. We don't
  909. * unregister this range because it is difficult to
  910. * tell when outstanding writes to the remote partition
  911. * are finished and thus when it is thus safe to
  912. * unregister. This should not result in wasted space
  913. * in the SAL xp_addr_region table because we should
  914. * get the same page for remote_act_amos_pa after
  915. * module reloads and system reboots.
  916. */
  917. if (sn_register_xp_addr_region
  918. (remote_vars->amos_page_pa, PAGE_SIZE, 1) < 0) {
  919. dev_dbg(xpc_part,
  920. "partition %d failed to "
  921. "register xp_addr region 0x%016lx\n",
  922. partid, remote_vars->amos_page_pa);
  923. XPC_SET_REASON(part, xpPhysAddrRegFailed,
  924. __LINE__);
  925. break;
  926. }
  927. /*
  928. * The remote nasid is valid and available.
  929. * Send an interrupt to that nasid to notify
  930. * it that we are ready to begin activation.
  931. */
  932. dev_dbg(xpc_part, "sending an interrupt to AMO 0x%lx, "
  933. "nasid %d, phys_cpuid 0x%x\n",
  934. remote_vars->amos_page_pa,
  935. remote_vars->act_nasid,
  936. remote_vars->act_phys_cpuid);
  937. if (XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
  938. version)) {
  939. part->remote_amos_page_pa =
  940. remote_vars->amos_page_pa;
  941. xpc_mark_partition_disengaged(part);
  942. xpc_cancel_partition_disengage_request(part);
  943. }
  944. xpc_IPI_send_activate(remote_vars);
  945. }
  946. }
  947. kfree(discovered_nasids);
  948. kfree(remote_rp_base);
  949. }
  950. /*
  951. * Given a partid, get the nasids owned by that partition from the
  952. * remote partition's reserved page.
  953. */
  954. enum xp_retval
  955. xpc_initiate_partid_to_nasids(short partid, void *nasid_mask)
  956. {
  957. struct xpc_partition *part;
  958. u64 part_nasid_pa;
  959. int bte_res;
  960. part = &xpc_partitions[partid];
  961. if (part->remote_rp_pa == 0)
  962. return xpPartitionDown;
  963. memset(nasid_mask, 0, XP_NASID_MASK_BYTES);
  964. part_nasid_pa = (u64)XPC_RP_PART_NASIDS(part->remote_rp_pa);
  965. bte_res = xp_bte_copy(part_nasid_pa, (u64)nasid_mask,
  966. xp_nasid_mask_bytes, (BTE_NOTIFY | BTE_WACQUIRE),
  967. NULL);
  968. return xpc_map_bte_errors(bte_res);
  969. }