xpc.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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) structures and macros.
  10. */
  11. #ifndef _IA64_SN_KERNEL_XPC_H
  12. #define _IA64_SN_KERNEL_XPC_H
  13. #include <linux/config.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/sysctl.h>
  16. #include <linux/device.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/processor.h>
  19. #include <asm/sn/bte.h>
  20. #include <asm/sn/clksupport.h>
  21. #include <asm/sn/addrs.h>
  22. #include <asm/sn/mspec.h>
  23. #include <asm/sn/shub_mmr.h>
  24. #include <asm/sn/xp.h>
  25. /*
  26. * XPC Version numbers consist of a major and minor number. XPC can always
  27. * talk to versions with same major #, and never talk to versions with a
  28. * different major #.
  29. */
  30. #define _XPC_VERSION(_maj, _min) (((_maj) << 4) | ((_min) & 0xf))
  31. #define XPC_VERSION_MAJOR(_v) ((_v) >> 4)
  32. #define XPC_VERSION_MINOR(_v) ((_v) & 0xf)
  33. /*
  34. * The next macros define word or bit representations for given
  35. * C-brick nasid in either the SAL provided bit array representing
  36. * nasids in the partition/machine or the AMO_t array used for
  37. * inter-partition initiation communications.
  38. *
  39. * For SN2 machines, C-Bricks are alway even numbered NASIDs. As
  40. * such, some space will be saved by insisting that nasid information
  41. * passed from SAL always be packed for C-Bricks and the
  42. * cross-partition interrupts use the same packing scheme.
  43. */
  44. #define XPC_NASID_W_INDEX(_n) (((_n) / 64) / 2)
  45. #define XPC_NASID_B_INDEX(_n) (((_n) / 2) & (64 - 1))
  46. #define XPC_NASID_IN_ARRAY(_n, _p) ((_p)[XPC_NASID_W_INDEX(_n)] & \
  47. (1UL << XPC_NASID_B_INDEX(_n)))
  48. #define XPC_NASID_FROM_W_B(_w, _b) (((_w) * 64 + (_b)) * 2)
  49. #define XPC_HB_DEFAULT_INTERVAL 5 /* incr HB every x secs */
  50. #define XPC_HB_CHECK_DEFAULT_TIMEOUT 20 /* check HB every x secs */
  51. /* define the process name of HB checker and the CPU it is pinned to */
  52. #define XPC_HB_CHECK_THREAD_NAME "xpc_hb"
  53. #define XPC_HB_CHECK_CPU 0
  54. /* define the process name of the discovery thread */
  55. #define XPC_DISCOVERY_THREAD_NAME "xpc_discovery"
  56. #define XPC_HB_ALLOWED(_p, _v) ((_v)->heartbeating_to_mask & (1UL << (_p)))
  57. #define XPC_ALLOW_HB(_p, _v) (_v)->heartbeating_to_mask |= (1UL << (_p))
  58. #define XPC_DISALLOW_HB(_p, _v) (_v)->heartbeating_to_mask &= (~(1UL << (_p)))
  59. /*
  60. * Reserved Page provided by SAL.
  61. *
  62. * SAL provides one page per partition of reserved memory. When SAL
  63. * initialization is complete, SAL_signature, SAL_version, partid,
  64. * part_nasids, and mach_nasids are set.
  65. *
  66. * Note: Until vars_pa is set, the partition XPC code has not been initialized.
  67. */
  68. struct xpc_rsvd_page {
  69. u64 SAL_signature; /* SAL unique signature */
  70. u64 SAL_version; /* SAL specified version */
  71. u8 partid; /* partition ID from SAL */
  72. u8 version;
  73. u8 pad[6]; /* pad to u64 align */
  74. volatile u64 vars_pa;
  75. u64 part_nasids[XP_NASID_MASK_WORDS] ____cacheline_aligned;
  76. u64 mach_nasids[XP_NASID_MASK_WORDS] ____cacheline_aligned;
  77. };
  78. #define XPC_RP_VERSION _XPC_VERSION(1,0) /* version 1.0 of the reserved page */
  79. #define XPC_RSVD_PAGE_ALIGNED_SIZE \
  80. (L1_CACHE_ALIGN(sizeof(struct xpc_rsvd_page)))
  81. /*
  82. * Define the structures by which XPC variables can be exported to other
  83. * partitions. (There are two: struct xpc_vars and struct xpc_vars_part)
  84. */
  85. /*
  86. * The following structure describes the partition generic variables
  87. * needed by other partitions in order to properly initialize.
  88. *
  89. * struct xpc_vars version number also applies to struct xpc_vars_part.
  90. * Changes to either structure and/or related functionality should be
  91. * reflected by incrementing either the major or minor version numbers
  92. * of struct xpc_vars.
  93. */
  94. struct xpc_vars {
  95. u8 version;
  96. u64 heartbeat;
  97. u64 heartbeating_to_mask;
  98. u64 kdb_status; /* 0 = machine running */
  99. int act_nasid;
  100. int act_phys_cpuid;
  101. u64 vars_part_pa;
  102. u64 amos_page_pa; /* paddr of page of AMOs from MSPEC driver */
  103. AMO_t *amos_page; /* vaddr of page of AMOs from MSPEC driver */
  104. AMO_t *act_amos; /* pointer to the first activation AMO */
  105. };
  106. #define XPC_V_VERSION _XPC_VERSION(3,0) /* version 3.0 of the cross vars */
  107. #define XPC_VARS_ALIGNED_SIZE (L1_CACHE_ALIGN(sizeof(struct xpc_vars)))
  108. /*
  109. * The following structure describes the per partition specific variables.
  110. *
  111. * An array of these structures, one per partition, will be defined. As a
  112. * partition becomes active XPC will copy the array entry corresponding to
  113. * itself from that partition. It is desirable that the size of this
  114. * structure evenly divide into a cacheline, such that none of the entries
  115. * in this array crosses a cacheline boundary. As it is now, each entry
  116. * occupies half a cacheline.
  117. */
  118. struct xpc_vars_part {
  119. volatile u64 magic;
  120. u64 openclose_args_pa; /* physical address of open and close args */
  121. u64 GPs_pa; /* physical address of Get/Put values */
  122. u64 IPI_amo_pa; /* physical address of IPI AMO_t structure */
  123. int IPI_nasid; /* nasid of where to send IPIs */
  124. int IPI_phys_cpuid; /* physical CPU ID of where to send IPIs */
  125. u8 nchannels; /* #of defined channels supported */
  126. u8 reserved[23]; /* pad to a full 64 bytes */
  127. };
  128. /*
  129. * The vars_part MAGIC numbers play a part in the first contact protocol.
  130. *
  131. * MAGIC1 indicates that the per partition specific variables for a remote
  132. * partition have been initialized by this partition.
  133. *
  134. * MAGIC2 indicates that this partition has pulled the remote partititions
  135. * per partition variables that pertain to this partition.
  136. */
  137. #define XPC_VP_MAGIC1 0x0053524156435058L /* 'XPCVARS\0'L (little endian) */
  138. #define XPC_VP_MAGIC2 0x0073726176435058L /* 'XPCvars\0'L (little endian) */
  139. /*
  140. * Functions registered by add_timer() or called by kernel_thread() only
  141. * allow for a single 64-bit argument. The following macros can be used to
  142. * pack and unpack two (32-bit, 16-bit or 8-bit) arguments into or out from
  143. * the passed argument.
  144. */
  145. #define XPC_PACK_ARGS(_arg1, _arg2) \
  146. ((((u64) _arg1) & 0xffffffff) | \
  147. ((((u64) _arg2) & 0xffffffff) << 32))
  148. #define XPC_UNPACK_ARG1(_args) (((u64) _args) & 0xffffffff)
  149. #define XPC_UNPACK_ARG2(_args) ((((u64) _args) >> 32) & 0xffffffff)
  150. /*
  151. * Define a Get/Put value pair (pointers) used with a message queue.
  152. */
  153. struct xpc_gp {
  154. volatile s64 get; /* Get value */
  155. volatile s64 put; /* Put value */
  156. };
  157. #define XPC_GP_SIZE \
  158. L1_CACHE_ALIGN(sizeof(struct xpc_gp) * XPC_NCHANNELS)
  159. /*
  160. * Define a structure that contains arguments associated with opening and
  161. * closing a channel.
  162. */
  163. struct xpc_openclose_args {
  164. u16 reason; /* reason why channel is closing */
  165. u16 msg_size; /* sizeof each message entry */
  166. u16 remote_nentries; /* #of message entries in remote msg queue */
  167. u16 local_nentries; /* #of message entries in local msg queue */
  168. u64 local_msgqueue_pa; /* physical address of local message queue */
  169. };
  170. #define XPC_OPENCLOSE_ARGS_SIZE \
  171. L1_CACHE_ALIGN(sizeof(struct xpc_openclose_args) * XPC_NCHANNELS)
  172. /* struct xpc_msg flags */
  173. #define XPC_M_DONE 0x01 /* msg has been received/consumed */
  174. #define XPC_M_READY 0x02 /* msg is ready to be sent */
  175. #define XPC_M_INTERRUPT 0x04 /* send interrupt when msg consumed */
  176. #define XPC_MSG_ADDRESS(_payload) \
  177. ((struct xpc_msg *)((u8 *)(_payload) - XPC_MSG_PAYLOAD_OFFSET))
  178. /*
  179. * Defines notify entry.
  180. *
  181. * This is used to notify a message's sender that their message was received
  182. * and consumed by the intended recipient.
  183. */
  184. struct xpc_notify {
  185. struct semaphore sema; /* notify semaphore */
  186. volatile u8 type; /* type of notification */
  187. /* the following two fields are only used if type == XPC_N_CALL */
  188. xpc_notify_func func; /* user's notify function */
  189. void *key; /* pointer to user's key */
  190. };
  191. /* struct xpc_notify type of notification */
  192. #define XPC_N_CALL 0x01 /* notify function provided by user */
  193. /*
  194. * Define the structure that manages all the stuff required by a channel. In
  195. * particular, they are used to manage the messages sent across the channel.
  196. *
  197. * This structure is private to a partition, and is NOT shared across the
  198. * partition boundary.
  199. *
  200. * There is an array of these structures for each remote partition. It is
  201. * allocated at the time a partition becomes active. The array contains one
  202. * of these structures for each potential channel connection to that partition.
  203. *
  204. * Each of these structures manages two message queues (circular buffers).
  205. * They are allocated at the time a channel connection is made. One of
  206. * these message queues (local_msgqueue) holds the locally created messages
  207. * that are destined for the remote partition. The other of these message
  208. * queues (remote_msgqueue) is a locally cached copy of the remote partition's
  209. * own local_msgqueue.
  210. *
  211. * The following is a description of the Get/Put pointers used to manage these
  212. * two message queues. Consider the local_msgqueue to be on one partition
  213. * and the remote_msgqueue to be its cached copy on another partition. A
  214. * description of what each of the lettered areas contains is included.
  215. *
  216. *
  217. * local_msgqueue remote_msgqueue
  218. *
  219. * |/////////| |/////////|
  220. * w_remote_GP.get --> +---------+ |/////////|
  221. * | F | |/////////|
  222. * remote_GP.get --> +---------+ +---------+ <-- local_GP->get
  223. * | | | |
  224. * | | | E |
  225. * | | | |
  226. * | | +---------+ <-- w_local_GP.get
  227. * | B | |/////////|
  228. * | | |////D////|
  229. * | | |/////////|
  230. * | | +---------+ <-- w_remote_GP.put
  231. * | | |////C////|
  232. * local_GP->put --> +---------+ +---------+ <-- remote_GP.put
  233. * | | |/////////|
  234. * | A | |/////////|
  235. * | | |/////////|
  236. * w_local_GP.put --> +---------+ |/////////|
  237. * |/////////| |/////////|
  238. *
  239. *
  240. * ( remote_GP.[get|put] are cached copies of the remote
  241. * partition's local_GP->[get|put], and thus their values can
  242. * lag behind their counterparts on the remote partition. )
  243. *
  244. *
  245. * A - Messages that have been allocated, but have not yet been sent to the
  246. * remote partition.
  247. *
  248. * B - Messages that have been sent, but have not yet been acknowledged by the
  249. * remote partition as having been received.
  250. *
  251. * C - Area that needs to be prepared for the copying of sent messages, by
  252. * the clearing of the message flags of any previously received messages.
  253. *
  254. * D - Area into which sent messages are to be copied from the remote
  255. * partition's local_msgqueue and then delivered to their intended
  256. * recipients. [ To allow for a multi-message copy, another pointer
  257. * (next_msg_to_pull) has been added to keep track of the next message
  258. * number needing to be copied (pulled). It chases after w_remote_GP.put.
  259. * Any messages lying between w_local_GP.get and next_msg_to_pull have
  260. * been copied and are ready to be delivered. ]
  261. *
  262. * E - Messages that have been copied and delivered, but have not yet been
  263. * acknowledged by the recipient as having been received.
  264. *
  265. * F - Messages that have been acknowledged, but XPC has not yet notified the
  266. * sender that the message was received by its intended recipient.
  267. * This is also an area that needs to be prepared for the allocating of
  268. * new messages, by the clearing of the message flags of the acknowledged
  269. * messages.
  270. */
  271. struct xpc_channel {
  272. partid_t partid; /* ID of remote partition connected */
  273. spinlock_t lock; /* lock for updating this structure */
  274. u32 flags; /* general flags */
  275. enum xpc_retval reason; /* reason why channel is disconnect'g */
  276. int reason_line; /* line# disconnect initiated from */
  277. u16 number; /* channel # */
  278. u16 msg_size; /* sizeof each msg entry */
  279. u16 local_nentries; /* #of msg entries in local msg queue */
  280. u16 remote_nentries; /* #of msg entries in remote msg queue*/
  281. void *local_msgqueue_base; /* base address of kmalloc'd space */
  282. struct xpc_msg *local_msgqueue; /* local message queue */
  283. void *remote_msgqueue_base; /* base address of kmalloc'd space */
  284. struct xpc_msg *remote_msgqueue;/* cached copy of remote partition's */
  285. /* local message queue */
  286. u64 remote_msgqueue_pa; /* phys addr of remote partition's */
  287. /* local message queue */
  288. atomic_t references; /* #of external references to queues */
  289. atomic_t n_on_msg_allocate_wq; /* #on msg allocation wait queue */
  290. wait_queue_head_t msg_allocate_wq; /* msg allocation wait queue */
  291. /* queue of msg senders who want to be notified when msg received */
  292. atomic_t n_to_notify; /* #of msg senders to notify */
  293. struct xpc_notify *notify_queue;/* notify queue for messages sent */
  294. xpc_channel_func func; /* user's channel function */
  295. void *key; /* pointer to user's key */
  296. struct semaphore msg_to_pull_sema; /* next msg to pull serialization */
  297. struct semaphore teardown_sema; /* wait for teardown completion */
  298. struct xpc_openclose_args *local_openclose_args; /* args passed on */
  299. /* opening or closing of channel */
  300. /* various flavors of local and remote Get/Put values */
  301. struct xpc_gp *local_GP; /* local Get/Put values */
  302. struct xpc_gp remote_GP; /* remote Get/Put values */
  303. struct xpc_gp w_local_GP; /* working local Get/Put values */
  304. struct xpc_gp w_remote_GP; /* working remote Get/Put values */
  305. s64 next_msg_to_pull; /* Put value of next msg to pull */
  306. /* kthread management related fields */
  307. // >>> rethink having kthreads_assigned_limit and kthreads_idle_limit; perhaps
  308. // >>> allow the assigned limit be unbounded and let the idle limit be dynamic
  309. // >>> dependent on activity over the last interval of time
  310. atomic_t kthreads_assigned; /* #of kthreads assigned to channel */
  311. u32 kthreads_assigned_limit; /* limit on #of kthreads assigned */
  312. atomic_t kthreads_idle; /* #of kthreads idle waiting for work */
  313. u32 kthreads_idle_limit; /* limit on #of kthreads idle */
  314. atomic_t kthreads_active; /* #of kthreads actively working */
  315. // >>> following field is temporary
  316. u32 kthreads_created; /* total #of kthreads created */
  317. wait_queue_head_t idle_wq; /* idle kthread wait queue */
  318. } ____cacheline_aligned;
  319. /* struct xpc_channel flags */
  320. #define XPC_C_WASCONNECTED 0x00000001 /* channel was connected */
  321. #define XPC_C_ROPENREPLY 0x00000002 /* remote open channel reply */
  322. #define XPC_C_OPENREPLY 0x00000004 /* local open channel reply */
  323. #define XPC_C_ROPENREQUEST 0x00000008 /* remote open channel request */
  324. #define XPC_C_OPENREQUEST 0x00000010 /* local open channel request */
  325. #define XPC_C_SETUP 0x00000020 /* channel's msgqueues are alloc'd */
  326. #define XPC_C_CONNECTCALLOUT 0x00000040 /* channel connected callout made */
  327. #define XPC_C_CONNECTED 0x00000080 /* local channel is connected */
  328. #define XPC_C_CONNECTING 0x00000100 /* channel is being connected */
  329. #define XPC_C_RCLOSEREPLY 0x00000200 /* remote close channel reply */
  330. #define XPC_C_CLOSEREPLY 0x00000400 /* local close channel reply */
  331. #define XPC_C_RCLOSEREQUEST 0x00000800 /* remote close channel request */
  332. #define XPC_C_CLOSEREQUEST 0x00001000 /* local close channel request */
  333. #define XPC_C_DISCONNECTED 0x00002000 /* channel is disconnected */
  334. #define XPC_C_DISCONNECTING 0x00004000 /* channel is being disconnected */
  335. /*
  336. * Manages channels on a partition basis. There is one of these structures
  337. * for each partition (a partition will never utilize the structure that
  338. * represents itself).
  339. */
  340. struct xpc_partition {
  341. /* XPC HB infrastructure */
  342. u64 remote_rp_pa; /* phys addr of partition's rsvd pg */
  343. u64 remote_vars_pa; /* phys addr of partition's vars */
  344. u64 remote_vars_part_pa; /* phys addr of partition's vars part */
  345. u64 last_heartbeat; /* HB at last read */
  346. u64 remote_amos_page_pa; /* phys addr of partition's amos page */
  347. int remote_act_nasid; /* active part's act/deact nasid */
  348. int remote_act_phys_cpuid; /* active part's act/deact phys cpuid */
  349. u32 act_IRQ_rcvd; /* IRQs since activation */
  350. spinlock_t act_lock; /* protect updating of act_state */
  351. u8 act_state; /* from XPC HB viewpoint */
  352. enum xpc_retval reason; /* reason partition is deactivating */
  353. int reason_line; /* line# deactivation initiated from */
  354. int reactivate_nasid; /* nasid in partition to reactivate */
  355. /* XPC infrastructure referencing and teardown control */
  356. volatile u8 setup_state; /* infrastructure setup state */
  357. wait_queue_head_t teardown_wq; /* kthread waiting to teardown infra */
  358. atomic_t references; /* #of references to infrastructure */
  359. /*
  360. * NONE OF THE PRECEDING FIELDS OF THIS STRUCTURE WILL BE CLEARED WHEN
  361. * XPC SETS UP THE NECESSARY INFRASTRUCTURE TO SUPPORT CROSS PARTITION
  362. * COMMUNICATION. ALL OF THE FOLLOWING FIELDS WILL BE CLEARED. (THE
  363. * 'nchannels' FIELD MUST BE THE FIRST OF THE FIELDS TO BE CLEARED.)
  364. */
  365. u8 nchannels; /* #of defined channels supported */
  366. atomic_t nchannels_active; /* #of channels that are not DISCONNECTED */
  367. struct xpc_channel *channels;/* array of channel structures */
  368. void *local_GPs_base; /* base address of kmalloc'd space */
  369. struct xpc_gp *local_GPs; /* local Get/Put values */
  370. void *remote_GPs_base; /* base address of kmalloc'd space */
  371. struct xpc_gp *remote_GPs;/* copy of remote partition's local Get/Put */
  372. /* values */
  373. u64 remote_GPs_pa; /* phys address of remote partition's local */
  374. /* Get/Put values */
  375. /* fields used to pass args when opening or closing a channel */
  376. void *local_openclose_args_base; /* base address of kmalloc'd space */
  377. struct xpc_openclose_args *local_openclose_args; /* local's args */
  378. void *remote_openclose_args_base; /* base address of kmalloc'd space */
  379. struct xpc_openclose_args *remote_openclose_args; /* copy of remote's */
  380. /* args */
  381. u64 remote_openclose_args_pa; /* phys addr of remote's args */
  382. /* IPI sending, receiving and handling related fields */
  383. int remote_IPI_nasid; /* nasid of where to send IPIs */
  384. int remote_IPI_phys_cpuid; /* phys CPU ID of where to send IPIs */
  385. AMO_t *remote_IPI_amo_va; /* address of remote IPI AMO_t structure */
  386. AMO_t *local_IPI_amo_va; /* address of IPI AMO_t structure */
  387. u64 local_IPI_amo; /* IPI amo flags yet to be handled */
  388. char IPI_owner[8]; /* IPI owner's name */
  389. struct timer_list dropped_IPI_timer; /* dropped IPI timer */
  390. spinlock_t IPI_lock; /* IPI handler lock */
  391. /* channel manager related fields */
  392. atomic_t channel_mgr_requests; /* #of requests to activate chan mgr */
  393. wait_queue_head_t channel_mgr_wq; /* channel mgr's wait queue */
  394. } ____cacheline_aligned;
  395. /* struct xpc_partition act_state values (for XPC HB) */
  396. #define XPC_P_INACTIVE 0x00 /* partition is not active */
  397. #define XPC_P_ACTIVATION_REQ 0x01 /* created thread to activate */
  398. #define XPC_P_ACTIVATING 0x02 /* activation thread started */
  399. #define XPC_P_ACTIVE 0x03 /* xpc_partition_up() was called */
  400. #define XPC_P_DEACTIVATING 0x04 /* partition deactivation initiated */
  401. #define XPC_DEACTIVATE_PARTITION(_p, _reason) \
  402. xpc_deactivate_partition(__LINE__, (_p), (_reason))
  403. /* struct xpc_partition setup_state values */
  404. #define XPC_P_UNSET 0x00 /* infrastructure was never setup */
  405. #define XPC_P_SETUP 0x01 /* infrastructure is setup */
  406. #define XPC_P_WTEARDOWN 0x02 /* waiting to teardown infrastructure */
  407. #define XPC_P_TORNDOWN 0x03 /* infrastructure is torndown */
  408. /*
  409. * struct xpc_partition IPI_timer #of seconds to wait before checking for
  410. * dropped IPIs. These occur whenever an IPI amo write doesn't complete until
  411. * after the IPI was received.
  412. */
  413. #define XPC_P_DROPPED_IPI_WAIT (0.25 * HZ)
  414. #define XPC_PARTID(_p) ((partid_t) ((_p) - &xpc_partitions[0]))
  415. /* found in xp_main.c */
  416. extern struct xpc_registration xpc_registrations[];
  417. /* >>> found in xpc_main.c only */
  418. extern struct device *xpc_part;
  419. extern struct device *xpc_chan;
  420. extern irqreturn_t xpc_notify_IRQ_handler(int, void *, struct pt_regs *);
  421. extern void xpc_dropped_IPI_check(struct xpc_partition *);
  422. extern void xpc_activate_kthreads(struct xpc_channel *, int);
  423. extern void xpc_create_kthreads(struct xpc_channel *, int);
  424. extern void xpc_disconnect_wait(int);
  425. /* found in xpc_main.c and efi-xpc.c */
  426. extern void xpc_activate_partition(struct xpc_partition *);
  427. /* found in xpc_partition.c */
  428. extern int xpc_exiting;
  429. extern int xpc_hb_interval;
  430. extern int xpc_hb_check_interval;
  431. extern struct xpc_vars *xpc_vars;
  432. extern struct xpc_rsvd_page *xpc_rsvd_page;
  433. extern struct xpc_vars_part *xpc_vars_part;
  434. extern struct xpc_partition xpc_partitions[XP_MAX_PARTITIONS + 1];
  435. extern char xpc_remote_copy_buffer[];
  436. extern struct xpc_rsvd_page *xpc_rsvd_page_init(void);
  437. extern void xpc_allow_IPI_ops(void);
  438. extern void xpc_restrict_IPI_ops(void);
  439. extern int xpc_identify_act_IRQ_sender(void);
  440. extern enum xpc_retval xpc_mark_partition_active(struct xpc_partition *);
  441. extern void xpc_mark_partition_inactive(struct xpc_partition *);
  442. extern void xpc_discovery(void);
  443. extern void xpc_check_remote_hb(void);
  444. extern void xpc_deactivate_partition(const int, struct xpc_partition *,
  445. enum xpc_retval);
  446. extern enum xpc_retval xpc_initiate_partid_to_nasids(partid_t, void *);
  447. /* found in xpc_channel.c */
  448. extern void xpc_initiate_connect(int);
  449. extern void xpc_initiate_disconnect(int);
  450. extern enum xpc_retval xpc_initiate_allocate(partid_t, int, u32, void **);
  451. extern enum xpc_retval xpc_initiate_send(partid_t, int, void *);
  452. extern enum xpc_retval xpc_initiate_send_notify(partid_t, int, void *,
  453. xpc_notify_func, void *);
  454. extern void xpc_initiate_received(partid_t, int, void *);
  455. extern enum xpc_retval xpc_setup_infrastructure(struct xpc_partition *);
  456. extern enum xpc_retval xpc_pull_remote_vars_part(struct xpc_partition *);
  457. extern void xpc_process_channel_activity(struct xpc_partition *);
  458. extern void xpc_connected_callout(struct xpc_channel *);
  459. extern void xpc_deliver_msg(struct xpc_channel *);
  460. extern void xpc_disconnect_channel(const int, struct xpc_channel *,
  461. enum xpc_retval, unsigned long *);
  462. extern void xpc_disconnected_callout(struct xpc_channel *);
  463. extern void xpc_partition_down(struct xpc_partition *, enum xpc_retval);
  464. extern void xpc_teardown_infrastructure(struct xpc_partition *);
  465. static inline void
  466. xpc_wakeup_channel_mgr(struct xpc_partition *part)
  467. {
  468. if (atomic_inc_return(&part->channel_mgr_requests) == 1) {
  469. wake_up(&part->channel_mgr_wq);
  470. }
  471. }
  472. /*
  473. * These next two inlines are used to keep us from tearing down a channel's
  474. * msg queues while a thread may be referencing them.
  475. */
  476. static inline void
  477. xpc_msgqueue_ref(struct xpc_channel *ch)
  478. {
  479. atomic_inc(&ch->references);
  480. }
  481. static inline void
  482. xpc_msgqueue_deref(struct xpc_channel *ch)
  483. {
  484. s32 refs = atomic_dec_return(&ch->references);
  485. DBUG_ON(refs < 0);
  486. if (refs == 0) {
  487. xpc_wakeup_channel_mgr(&xpc_partitions[ch->partid]);
  488. }
  489. }
  490. #define XPC_DISCONNECT_CHANNEL(_ch, _reason, _irqflgs) \
  491. xpc_disconnect_channel(__LINE__, _ch, _reason, _irqflgs)
  492. /*
  493. * These two inlines are used to keep us from tearing down a partition's
  494. * setup infrastructure while a thread may be referencing it.
  495. */
  496. static inline void
  497. xpc_part_deref(struct xpc_partition *part)
  498. {
  499. s32 refs = atomic_dec_return(&part->references);
  500. DBUG_ON(refs < 0);
  501. if (refs == 0 && part->setup_state == XPC_P_WTEARDOWN) {
  502. wake_up(&part->teardown_wq);
  503. }
  504. }
  505. static inline int
  506. xpc_part_ref(struct xpc_partition *part)
  507. {
  508. int setup;
  509. atomic_inc(&part->references);
  510. setup = (part->setup_state == XPC_P_SETUP);
  511. if (!setup) {
  512. xpc_part_deref(part);
  513. }
  514. return setup;
  515. }
  516. /*
  517. * The following macro is to be used for the setting of the reason and
  518. * reason_line fields in both the struct xpc_channel and struct xpc_partition
  519. * structures.
  520. */
  521. #define XPC_SET_REASON(_p, _reason, _line) \
  522. { \
  523. (_p)->reason = _reason; \
  524. (_p)->reason_line = _line; \
  525. }
  526. /*
  527. * The following set of macros and inlines are used for the sending and
  528. * receiving of IPIs (also known as IRQs). There are two flavors of IPIs,
  529. * one that is associated with partition activity (SGI_XPC_ACTIVATE) and
  530. * the other that is associated with channel activity (SGI_XPC_NOTIFY).
  531. */
  532. static inline u64
  533. xpc_IPI_receive(AMO_t *amo)
  534. {
  535. return FETCHOP_LOAD_OP(TO_AMO((u64) &amo->variable), FETCHOP_CLEAR);
  536. }
  537. static inline enum xpc_retval
  538. xpc_IPI_send(AMO_t *amo, u64 flag, int nasid, int phys_cpuid, int vector)
  539. {
  540. int ret = 0;
  541. unsigned long irq_flags;
  542. local_irq_save(irq_flags);
  543. FETCHOP_STORE_OP(TO_AMO((u64) &amo->variable), FETCHOP_OR, flag);
  544. sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
  545. /*
  546. * We must always use the nofault function regardless of whether we
  547. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  548. * didn't, we'd never know that the other partition is down and would
  549. * keep sending IPIs and AMOs to it until the heartbeat times out.
  550. */
  551. ret = xp_nofault_PIOR((u64 *) GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
  552. xp_nofault_PIOR_target));
  553. local_irq_restore(irq_flags);
  554. return ((ret == 0) ? xpcSuccess : xpcPioReadError);
  555. }
  556. /*
  557. * IPIs associated with SGI_XPC_ACTIVATE IRQ.
  558. */
  559. /*
  560. * Flag the appropriate AMO variable and send an IPI to the specified node.
  561. */
  562. static inline void
  563. xpc_activate_IRQ_send(u64 amos_page, int from_nasid, int to_nasid,
  564. int to_phys_cpuid)
  565. {
  566. int w_index = XPC_NASID_W_INDEX(from_nasid);
  567. int b_index = XPC_NASID_B_INDEX(from_nasid);
  568. AMO_t *amos = (AMO_t *) __va(amos_page +
  569. (XP_MAX_PARTITIONS * sizeof(AMO_t)));
  570. (void) xpc_IPI_send(&amos[w_index], (1UL << b_index), to_nasid,
  571. to_phys_cpuid, SGI_XPC_ACTIVATE);
  572. }
  573. static inline void
  574. xpc_IPI_send_activate(struct xpc_vars *vars)
  575. {
  576. xpc_activate_IRQ_send(vars->amos_page_pa, cnodeid_to_nasid(0),
  577. vars->act_nasid, vars->act_phys_cpuid);
  578. }
  579. static inline void
  580. xpc_IPI_send_activated(struct xpc_partition *part)
  581. {
  582. xpc_activate_IRQ_send(part->remote_amos_page_pa, cnodeid_to_nasid(0),
  583. part->remote_act_nasid, part->remote_act_phys_cpuid);
  584. }
  585. static inline void
  586. xpc_IPI_send_reactivate(struct xpc_partition *part)
  587. {
  588. xpc_activate_IRQ_send(xpc_vars->amos_page_pa, part->reactivate_nasid,
  589. xpc_vars->act_nasid, xpc_vars->act_phys_cpuid);
  590. }
  591. /*
  592. * IPIs associated with SGI_XPC_NOTIFY IRQ.
  593. */
  594. /*
  595. * Send an IPI to the remote partition that is associated with the
  596. * specified channel.
  597. */
  598. #define XPC_NOTIFY_IRQ_SEND(_ch, _ipi_f, _irq_f) \
  599. xpc_notify_IRQ_send(_ch, _ipi_f, #_ipi_f, _irq_f)
  600. static inline void
  601. xpc_notify_IRQ_send(struct xpc_channel *ch, u8 ipi_flag, char *ipi_flag_string,
  602. unsigned long *irq_flags)
  603. {
  604. struct xpc_partition *part = &xpc_partitions[ch->partid];
  605. enum xpc_retval ret;
  606. if (likely(part->act_state != XPC_P_DEACTIVATING)) {
  607. ret = xpc_IPI_send(part->remote_IPI_amo_va,
  608. (u64) ipi_flag << (ch->number * 8),
  609. part->remote_IPI_nasid,
  610. part->remote_IPI_phys_cpuid,
  611. SGI_XPC_NOTIFY);
  612. dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
  613. ipi_flag_string, ch->partid, ch->number, ret);
  614. if (unlikely(ret != xpcSuccess)) {
  615. if (irq_flags != NULL) {
  616. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  617. }
  618. XPC_DEACTIVATE_PARTITION(part, ret);
  619. if (irq_flags != NULL) {
  620. spin_lock_irqsave(&ch->lock, *irq_flags);
  621. }
  622. }
  623. }
  624. }
  625. /*
  626. * Make it look like the remote partition, which is associated with the
  627. * specified channel, sent us an IPI. This faked IPI will be handled
  628. * by xpc_dropped_IPI_check().
  629. */
  630. #define XPC_NOTIFY_IRQ_SEND_LOCAL(_ch, _ipi_f) \
  631. xpc_notify_IRQ_send_local(_ch, _ipi_f, #_ipi_f)
  632. static inline void
  633. xpc_notify_IRQ_send_local(struct xpc_channel *ch, u8 ipi_flag,
  634. char *ipi_flag_string)
  635. {
  636. struct xpc_partition *part = &xpc_partitions[ch->partid];
  637. FETCHOP_STORE_OP(TO_AMO((u64) &part->local_IPI_amo_va->variable),
  638. FETCHOP_OR, ((u64) ipi_flag << (ch->number * 8)));
  639. dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
  640. ipi_flag_string, ch->partid, ch->number);
  641. }
  642. /*
  643. * The sending and receiving of IPIs includes the setting of an AMO variable
  644. * to indicate the reason the IPI was sent. The 64-bit variable is divided
  645. * up into eight bytes, ordered from right to left. Byte zero pertains to
  646. * channel 0, byte one to channel 1, and so on. Each byte is described by
  647. * the following IPI flags.
  648. */
  649. #define XPC_IPI_CLOSEREQUEST 0x01
  650. #define XPC_IPI_CLOSEREPLY 0x02
  651. #define XPC_IPI_OPENREQUEST 0x04
  652. #define XPC_IPI_OPENREPLY 0x08
  653. #define XPC_IPI_MSGREQUEST 0x10
  654. /* given an AMO variable and a channel#, get its associated IPI flags */
  655. #define XPC_GET_IPI_FLAGS(_amo, _c) ((u8) (((_amo) >> ((_c) * 8)) & 0xff))
  656. #define XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(_amo) ((_amo) & 0x0f0f0f0f0f0f0f0f)
  657. #define XPC_ANY_MSG_IPI_FLAGS_SET(_amo) ((_amo) & 0x1010101010101010)
  658. static inline void
  659. xpc_IPI_send_closerequest(struct xpc_channel *ch, unsigned long *irq_flags)
  660. {
  661. struct xpc_openclose_args *args = ch->local_openclose_args;
  662. args->reason = ch->reason;
  663. XPC_NOTIFY_IRQ_SEND(ch, XPC_IPI_CLOSEREQUEST, irq_flags);
  664. }
  665. static inline void
  666. xpc_IPI_send_closereply(struct xpc_channel *ch, unsigned long *irq_flags)
  667. {
  668. XPC_NOTIFY_IRQ_SEND(ch, XPC_IPI_CLOSEREPLY, irq_flags);
  669. }
  670. static inline void
  671. xpc_IPI_send_openrequest(struct xpc_channel *ch, unsigned long *irq_flags)
  672. {
  673. struct xpc_openclose_args *args = ch->local_openclose_args;
  674. args->msg_size = ch->msg_size;
  675. args->local_nentries = ch->local_nentries;
  676. XPC_NOTIFY_IRQ_SEND(ch, XPC_IPI_OPENREQUEST, irq_flags);
  677. }
  678. static inline void
  679. xpc_IPI_send_openreply(struct xpc_channel *ch, unsigned long *irq_flags)
  680. {
  681. struct xpc_openclose_args *args = ch->local_openclose_args;
  682. args->remote_nentries = ch->remote_nentries;
  683. args->local_nentries = ch->local_nentries;
  684. args->local_msgqueue_pa = __pa(ch->local_msgqueue);
  685. XPC_NOTIFY_IRQ_SEND(ch, XPC_IPI_OPENREPLY, irq_flags);
  686. }
  687. static inline void
  688. xpc_IPI_send_msgrequest(struct xpc_channel *ch)
  689. {
  690. XPC_NOTIFY_IRQ_SEND(ch, XPC_IPI_MSGREQUEST, NULL);
  691. }
  692. static inline void
  693. xpc_IPI_send_local_msgrequest(struct xpc_channel *ch)
  694. {
  695. XPC_NOTIFY_IRQ_SEND_LOCAL(ch, XPC_IPI_MSGREQUEST);
  696. }
  697. /*
  698. * Memory for XPC's AMO variables is allocated by the MSPEC driver. These
  699. * pages are located in the lowest granule. The lowest granule uses 4k pages
  700. * for cached references and an alternate TLB handler to never provide a
  701. * cacheable mapping for the entire region. This will prevent speculative
  702. * reading of cached copies of our lines from being issued which will cause
  703. * a PI FSB Protocol error to be generated by the SHUB. For XPC, we need 64
  704. * (XP_MAX_PARTITIONS) AMO variables for message notification (xpc_main.c)
  705. * and an additional 16 AMO variables for partition activation (xpc_hb.c).
  706. */
  707. static inline AMO_t *
  708. xpc_IPI_init(partid_t partid)
  709. {
  710. AMO_t *part_amo = xpc_vars->amos_page + partid;
  711. xpc_IPI_receive(part_amo);
  712. return part_amo;
  713. }
  714. static inline enum xpc_retval
  715. xpc_map_bte_errors(bte_result_t error)
  716. {
  717. switch (error) {
  718. case BTE_SUCCESS: return xpcSuccess;
  719. case BTEFAIL_DIR: return xpcBteDirectoryError;
  720. case BTEFAIL_POISON: return xpcBtePoisonError;
  721. case BTEFAIL_WERR: return xpcBteWriteError;
  722. case BTEFAIL_ACCESS: return xpcBteAccessError;
  723. case BTEFAIL_PWERR: return xpcBtePWriteError;
  724. case BTEFAIL_PRERR: return xpcBtePReadError;
  725. case BTEFAIL_TOUT: return xpcBteTimeOutError;
  726. case BTEFAIL_XTERR: return xpcBteXtalkError;
  727. case BTEFAIL_NOTAVAIL: return xpcBteNotAvailable;
  728. default: return xpcBteUnmappedError;
  729. }
  730. }
  731. static inline void *
  732. xpc_kmalloc_cacheline_aligned(size_t size, int flags, void **base)
  733. {
  734. /* see if kmalloc will give us cachline aligned memory by default */
  735. *base = kmalloc(size, flags);
  736. if (*base == NULL) {
  737. return NULL;
  738. }
  739. if ((u64) *base == L1_CACHE_ALIGN((u64) *base)) {
  740. return *base;
  741. }
  742. kfree(*base);
  743. /* nope, we'll have to do it ourselves */
  744. *base = kmalloc(size + L1_CACHE_BYTES, flags);
  745. if (*base == NULL) {
  746. return NULL;
  747. }
  748. return (void *) L1_CACHE_ALIGN((u64) *base);
  749. }
  750. /*
  751. * Check to see if there is any channel activity to/from the specified
  752. * partition.
  753. */
  754. static inline void
  755. xpc_check_for_channel_activity(struct xpc_partition *part)
  756. {
  757. u64 IPI_amo;
  758. unsigned long irq_flags;
  759. IPI_amo = xpc_IPI_receive(part->local_IPI_amo_va);
  760. if (IPI_amo == 0) {
  761. return;
  762. }
  763. spin_lock_irqsave(&part->IPI_lock, irq_flags);
  764. part->local_IPI_amo |= IPI_amo;
  765. spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
  766. dev_dbg(xpc_chan, "received IPI from partid=%d, IPI_amo=0x%lx\n",
  767. XPC_PARTID(part), IPI_amo);
  768. xpc_wakeup_channel_mgr(part);
  769. }
  770. #endif /* _IA64_SN_KERNEL_XPC_H */