xpc_partition.c 31 KB

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