xpc_partition.c 31 KB

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