xpc_partition.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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-2005 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. * 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, kdb_status = %ld, HB_mask = 0x%lx\n", partid,
  363. remote_vars->heartbeat, part->last_heartbeat,
  364. remote_vars->kdb_status,
  365. remote_vars->heartbeating_to_mask);
  366. if (((remote_vars->heartbeat == part->last_heartbeat) &&
  367. (remote_vars->kdb_status == 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 (!xpc_partition_disengaged(part)) {
  620. /* still waiting on other side to disengage from us */
  621. return;
  622. }
  623. if (reactivate) {
  624. part->reactivate_nasid = nasid;
  625. XPC_DEACTIVATE_PARTITION(part, xpcReactivating);
  626. } else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) &&
  627. xpc_partition_disengage_requested(1UL << partid)) {
  628. XPC_DEACTIVATE_PARTITION(part, xpcOtherGoingDown);
  629. }
  630. }
  631. /*
  632. * Loop through the activation AMO variables and process any bits
  633. * which are set. Each bit indicates a nasid sending a partition
  634. * activation or deactivation request.
  635. *
  636. * Return #of IRQs detected.
  637. */
  638. int
  639. xpc_identify_act_IRQ_sender(void)
  640. {
  641. int word, bit;
  642. u64 nasid_mask;
  643. u64 nasid; /* remote nasid */
  644. int n_IRQs_detected = 0;
  645. AMO_t *act_amos;
  646. act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
  647. /* scan through act AMO variable looking for non-zero entries */
  648. for (word = 0; word < xp_nasid_mask_words; word++) {
  649. if (xpc_exiting) {
  650. break;
  651. }
  652. nasid_mask = xpc_IPI_receive(&act_amos[word]);
  653. if (nasid_mask == 0) {
  654. /* no IRQs from nasids in this variable */
  655. continue;
  656. }
  657. dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
  658. nasid_mask);
  659. /*
  660. * If this nasid has been added to the machine since
  661. * our partition was reset, this will retain the
  662. * remote nasid in our reserved pages machine mask.
  663. * This is used in the event of module reload.
  664. */
  665. xpc_mach_nasids[word] |= nasid_mask;
  666. /* locate the nasid(s) which sent interrupts */
  667. for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
  668. if (nasid_mask & (1UL << bit)) {
  669. n_IRQs_detected++;
  670. nasid = XPC_NASID_FROM_W_B(word, bit);
  671. dev_dbg(xpc_part, "interrupt from nasid %ld\n",
  672. nasid);
  673. xpc_identify_act_IRQ_req(nasid);
  674. }
  675. }
  676. }
  677. return n_IRQs_detected;
  678. }
  679. /*
  680. * See if the other side has responded to a partition disengage request
  681. * from us.
  682. */
  683. int
  684. xpc_partition_disengaged(struct xpc_partition *part)
  685. {
  686. partid_t partid = XPC_PARTID(part);
  687. int disengaged;
  688. disengaged = (xpc_partition_engaged(1UL << partid) == 0);
  689. if (part->disengage_request_timeout) {
  690. if (!disengaged) {
  691. if (jiffies < part->disengage_request_timeout) {
  692. /* timelimit hasn't been reached yet */
  693. return 0;
  694. }
  695. /*
  696. * Other side hasn't responded to our disengage
  697. * request in a timely fashion, so assume it's dead.
  698. */
  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. }
  713. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
  714. xpc_cancel_partition_disengage_request(part);
  715. }
  716. }
  717. return disengaged;
  718. }
  719. /*
  720. * Mark specified partition as active.
  721. */
  722. enum xpc_retval
  723. xpc_mark_partition_active(struct xpc_partition *part)
  724. {
  725. unsigned long irq_flags;
  726. enum xpc_retval ret;
  727. dev_dbg(xpc_part, "setting partition %d to ACTIVE\n", XPC_PARTID(part));
  728. spin_lock_irqsave(&part->act_lock, irq_flags);
  729. if (part->act_state == XPC_P_ACTIVATING) {
  730. part->act_state = XPC_P_ACTIVE;
  731. ret = xpcSuccess;
  732. } else {
  733. DBUG_ON(part->reason == xpcSuccess);
  734. ret = part->reason;
  735. }
  736. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  737. return ret;
  738. }
  739. /*
  740. * Notify XPC that the partition is down.
  741. */
  742. void
  743. xpc_deactivate_partition(const int line, struct xpc_partition *part,
  744. enum xpc_retval reason)
  745. {
  746. unsigned long irq_flags;
  747. spin_lock_irqsave(&part->act_lock, irq_flags);
  748. if (part->act_state == XPC_P_INACTIVE) {
  749. XPC_SET_REASON(part, reason, line);
  750. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  751. if (reason == xpcReactivating) {
  752. /* we interrupt ourselves to reactivate partition */
  753. xpc_IPI_send_reactivate(part);
  754. }
  755. return;
  756. }
  757. if (part->act_state == XPC_P_DEACTIVATING) {
  758. if ((part->reason == xpcUnloading && reason != xpcUnloading) ||
  759. reason == xpcReactivating) {
  760. XPC_SET_REASON(part, reason, line);
  761. }
  762. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  763. return;
  764. }
  765. part->act_state = XPC_P_DEACTIVATING;
  766. XPC_SET_REASON(part, reason, line);
  767. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  768. if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
  769. xpc_request_partition_disengage(part);
  770. xpc_IPI_send_disengage(part);
  771. /* set a timelimit on the disengage request */
  772. part->disengage_request_timeout = jiffies +
  773. (xpc_disengage_request_timelimit * HZ);
  774. part->disengage_request_timer.expires =
  775. part->disengage_request_timeout;
  776. add_timer(&part->disengage_request_timer);
  777. }
  778. dev_dbg(xpc_part, "bringing partition %d down, reason = %d\n",
  779. XPC_PARTID(part), reason);
  780. xpc_partition_going_down(part, reason);
  781. }
  782. /*
  783. * Mark specified partition as inactive.
  784. */
  785. void
  786. xpc_mark_partition_inactive(struct xpc_partition *part)
  787. {
  788. unsigned long irq_flags;
  789. dev_dbg(xpc_part, "setting partition %d to INACTIVE\n",
  790. XPC_PARTID(part));
  791. spin_lock_irqsave(&part->act_lock, irq_flags);
  792. part->act_state = XPC_P_INACTIVE;
  793. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  794. part->remote_rp_pa = 0;
  795. }
  796. /*
  797. * SAL has provided a partition and machine mask. The partition mask
  798. * contains a bit for each even nasid in our partition. The machine
  799. * mask contains a bit for each even nasid in the entire machine.
  800. *
  801. * Using those two bit arrays, we can determine which nasids are
  802. * known in the machine. Each should also have a reserved page
  803. * initialized if they are available for partitioning.
  804. */
  805. void
  806. xpc_discovery(void)
  807. {
  808. void *remote_rp_base;
  809. struct xpc_rsvd_page *remote_rp;
  810. struct xpc_vars *remote_vars;
  811. u64 remote_rp_pa;
  812. u64 remote_vars_pa;
  813. int region;
  814. int region_size;
  815. int max_regions;
  816. int nasid;
  817. struct xpc_rsvd_page *rp;
  818. partid_t partid;
  819. struct xpc_partition *part;
  820. u64 *discovered_nasids;
  821. enum xpc_retval ret;
  822. remote_rp = xpc_kmalloc_cacheline_aligned(XPC_RP_HEADER_SIZE +
  823. xp_nasid_mask_bytes,
  824. GFP_KERNEL, &remote_rp_base);
  825. if (remote_rp == NULL) {
  826. return;
  827. }
  828. remote_vars = (struct xpc_vars *) remote_rp;
  829. discovered_nasids = kmalloc(sizeof(u64) * xp_nasid_mask_words,
  830. GFP_KERNEL);
  831. if (discovered_nasids == NULL) {
  832. kfree(remote_rp_base);
  833. return;
  834. }
  835. memset(discovered_nasids, 0, sizeof(u64) * xp_nasid_mask_words);
  836. rp = (struct xpc_rsvd_page *) xpc_rsvd_page;
  837. /*
  838. * The term 'region' in this context refers to the minimum number of
  839. * nodes that can comprise an access protection grouping. The access
  840. * protection is in regards to memory, IOI and IPI.
  841. */
  842. max_regions = 64;
  843. region_size = sn_region_size;
  844. switch (region_size) {
  845. case 128:
  846. max_regions *= 2;
  847. case 64:
  848. max_regions *= 2;
  849. case 32:
  850. max_regions *= 2;
  851. region_size = 16;
  852. DBUG_ON(!is_shub2());
  853. }
  854. for (region = 0; region < max_regions; region++) {
  855. if ((volatile int) xpc_exiting) {
  856. break;
  857. }
  858. dev_dbg(xpc_part, "searching region %d\n", region);
  859. for (nasid = (region * region_size * 2);
  860. nasid < ((region + 1) * region_size * 2);
  861. nasid += 2) {
  862. if ((volatile int) xpc_exiting) {
  863. break;
  864. }
  865. dev_dbg(xpc_part, "checking nasid %d\n", nasid);
  866. if (XPC_NASID_IN_ARRAY(nasid, xpc_part_nasids)) {
  867. dev_dbg(xpc_part, "PROM indicates Nasid %d is "
  868. "part of the local partition; skipping "
  869. "region\n", nasid);
  870. break;
  871. }
  872. if (!(XPC_NASID_IN_ARRAY(nasid, xpc_mach_nasids))) {
  873. dev_dbg(xpc_part, "PROM indicates Nasid %d was "
  874. "not on Numa-Link network at reset\n",
  875. nasid);
  876. continue;
  877. }
  878. if (XPC_NASID_IN_ARRAY(nasid, discovered_nasids)) {
  879. dev_dbg(xpc_part, "Nasid %d is part of a "
  880. "partition which was previously "
  881. "discovered\n", nasid);
  882. continue;
  883. }
  884. /* pull over the reserved page structure */
  885. ret = xpc_get_remote_rp(nasid, discovered_nasids,
  886. remote_rp, &remote_rp_pa);
  887. if (ret != xpcSuccess) {
  888. dev_dbg(xpc_part, "unable to get reserved page "
  889. "from nasid %d, reason=%d\n", nasid,
  890. ret);
  891. if (ret == xpcLocalPartid) {
  892. break;
  893. }
  894. continue;
  895. }
  896. remote_vars_pa = remote_rp->vars_pa;
  897. partid = remote_rp->partid;
  898. part = &xpc_partitions[partid];
  899. /* pull over the cross partition variables */
  900. ret = xpc_get_remote_vars(remote_vars_pa, remote_vars);
  901. if (ret != xpcSuccess) {
  902. dev_dbg(xpc_part, "unable to get XPC variables "
  903. "from nasid %d, reason=%d\n", nasid,
  904. ret);
  905. XPC_DEACTIVATE_PARTITION(part, ret);
  906. continue;
  907. }
  908. if (part->act_state != XPC_P_INACTIVE) {
  909. dev_dbg(xpc_part, "partition %d on nasid %d is "
  910. "already activating\n", partid, nasid);
  911. break;
  912. }
  913. /*
  914. * Register the remote partition's AMOs with SAL so it
  915. * can handle and cleanup errors within that address
  916. * range should the remote partition go down. We don't
  917. * unregister this range because it is difficult to
  918. * tell when outstanding writes to the remote partition
  919. * are finished and thus when it is thus safe to
  920. * unregister. This should not result in wasted space
  921. * in the SAL xp_addr_region table because we should
  922. * get the same page for remote_act_amos_pa after
  923. * module reloads and system reboots.
  924. */
  925. if (sn_register_xp_addr_region(
  926. remote_vars->amos_page_pa,
  927. PAGE_SIZE, 1) < 0) {
  928. dev_dbg(xpc_part, "partition %d failed to "
  929. "register xp_addr region 0x%016lx\n",
  930. partid, remote_vars->amos_page_pa);
  931. XPC_SET_REASON(part, xpcPhysAddrRegFailed,
  932. __LINE__);
  933. break;
  934. }
  935. /*
  936. * The remote nasid is valid and available.
  937. * Send an interrupt to that nasid to notify
  938. * it that we are ready to begin activation.
  939. */
  940. dev_dbg(xpc_part, "sending an interrupt to AMO 0x%lx, "
  941. "nasid %d, phys_cpuid 0x%x\n",
  942. remote_vars->amos_page_pa,
  943. remote_vars->act_nasid,
  944. remote_vars->act_phys_cpuid);
  945. if (XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
  946. version)) {
  947. part->remote_amos_page_pa =
  948. remote_vars->amos_page_pa;
  949. xpc_mark_partition_disengaged(part);
  950. xpc_cancel_partition_disengage_request(part);
  951. }
  952. xpc_IPI_send_activate(remote_vars);
  953. }
  954. }
  955. kfree(discovered_nasids);
  956. kfree(remote_rp_base);
  957. }
  958. /*
  959. * Given a partid, get the nasids owned by that partition from the
  960. * remote partition's reserved page.
  961. */
  962. enum xpc_retval
  963. xpc_initiate_partid_to_nasids(partid_t partid, void *nasid_mask)
  964. {
  965. struct xpc_partition *part;
  966. u64 part_nasid_pa;
  967. int bte_res;
  968. part = &xpc_partitions[partid];
  969. if (part->remote_rp_pa == 0) {
  970. return xpcPartitionDown;
  971. }
  972. memset(nasid_mask, 0, XP_NASID_MASK_BYTES);
  973. part_nasid_pa = (u64) XPC_RP_PART_NASIDS(part->remote_rp_pa);
  974. bte_res = xp_bte_copy(part_nasid_pa, ia64_tpa((u64) nasid_mask),
  975. xp_nasid_mask_bytes, (BTE_NOTIFY | BTE_WACQUIRE), NULL);
  976. return xpc_map_bte_errors(bte_res);
  977. }