blob: 1fbf99bae963a8cd49008b26bbbb81c0790ee6d1 [file] [log] [blame]
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07001/*
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 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07007 */
8
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07009/*
10 * Cross Partition (XP) base.
11 *
12 * XP provides a base from which its users can interact
13 * with XPC, yet not be dependent on XPC.
14 *
15 */
16
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070017#include <linux/kernel.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050020#include <linux/mutex.h>
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070021#include <asm/sn/intr.h>
22#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050023#include "xp.h"
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070024
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070025/*
Dean Nelson2c2b94f2008-04-22 14:50:17 -050026 * The export of xp_nofault_PIOR needs to happen here since it is defined
27 * in drivers/misc/sgi-xp/xp_nofault.S. The target of the nofault read is
28 * defined here.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070029 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -050030EXPORT_SYMBOL_GPL(xp_nofault_PIOR);
31
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070032u64 xp_nofault_PIOR_target;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050033EXPORT_SYMBOL_GPL(xp_nofault_PIOR_target);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070034
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070035/*
36 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
37 * users of XPC.
38 */
39struct xpc_registration xpc_registrations[XPC_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050040EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070041
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070042/*
43 * Initialize the XPC interface to indicate that XPC isn't loaded.
44 */
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050045static enum xpc_retval
46xpc_notloaded(void)
47{
48 return xpcNotLoaded;
49}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070050
51struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050052 (void (*)(int))xpc_notloaded,
53 (void (*)(int))xpc_notloaded,
54 (enum xpc_retval(*)(partid_t, int, u32, void **))xpc_notloaded,
55 (enum xpc_retval(*)(partid_t, int, void *))xpc_notloaded,
56 (enum xpc_retval(*)(partid_t, int, void *, xpc_notify_func, void *))
57 xpc_notloaded,
58 (void (*)(partid_t, int, void *))xpc_notloaded,
59 (enum xpc_retval(*)(partid_t, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070060};
Dean Nelson2c2b94f2008-04-22 14:50:17 -050061EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070062
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070063/*
64 * XPC calls this when it (the XPC module) has been loaded.
65 */
66void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050067xpc_set_interface(void (*connect) (int),
68 void (*disconnect) (int),
69 enum xpc_retval (*allocate) (partid_t, int, u32, void **),
70 enum xpc_retval (*send) (partid_t, int, void *),
71 enum xpc_retval (*send_notify) (partid_t, int, void *,
72 xpc_notify_func, void *),
73 void (*received) (partid_t, int, void *),
74 enum xpc_retval (*partid_to_nasids) (partid_t, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070075{
76 xpc_interface.connect = connect;
77 xpc_interface.disconnect = disconnect;
78 xpc_interface.allocate = allocate;
79 xpc_interface.send = send;
80 xpc_interface.send_notify = send_notify;
81 xpc_interface.received = received;
82 xpc_interface.partid_to_nasids = partid_to_nasids;
83}
Dean Nelson2c2b94f2008-04-22 14:50:17 -050084EXPORT_SYMBOL_GPL(xpc_set_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070085
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070086/*
87 * XPC calls this when it (the XPC module) is being unloaded.
88 */
89void
90xpc_clear_interface(void)
91{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050092 xpc_interface.connect = (void (*)(int))xpc_notloaded;
93 xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
94 xpc_interface.allocate = (enum xpc_retval(*)(partid_t, int, u32,
95 void **))xpc_notloaded;
96 xpc_interface.send = (enum xpc_retval(*)(partid_t, int, void *))
97 xpc_notloaded;
98 xpc_interface.send_notify = (enum xpc_retval(*)(partid_t, int, void *,
99 xpc_notify_func,
100 void *))xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700101 xpc_interface.received = (void (*)(partid_t, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500102 xpc_notloaded;
103 xpc_interface.partid_to_nasids = (enum xpc_retval(*)(partid_t, void *))
104 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700105}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500106EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700107
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700108/*
109 * Register for automatic establishment of a channel connection whenever
110 * a partition comes up.
111 *
112 * Arguments:
113 *
114 * ch_number - channel # to register for connection.
115 * func - function to call for asynchronous notification of channel
116 * state changes (i.e., connection, disconnection, error) and
117 * the arrival of incoming messages.
118 * key - pointer to optional user-defined value that gets passed back
119 * to the user on any callouts made to func.
120 * payload_size - size in bytes of the XPC message's payload area which
121 * contains a user-defined message. The user should make
122 * this large enough to hold their largest message.
123 * nentries - max #of XPC message entries a message queue can contain.
124 * The actual number, which is determined when a connection
125 * is established and may be less then requested, will be
126 * passed to the user via the xpcConnected callout.
127 * assigned_limit - max number of kthreads allowed to be processing
128 * messages (per connection) at any given instant.
129 * idle_limit - max number of kthreads allowed to be idle at any given
130 * instant.
131 */
132enum xpc_retval
133xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500134 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700135{
136 struct xpc_registration *registration;
137
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700138 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
139 DBUG_ON(payload_size == 0 || nentries == 0);
140 DBUG_ON(func == NULL);
141 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
142
143 registration = &xpc_registrations[ch_number];
144
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500145 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700146 return xpcInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700147
148 /* if XPC_CHANNEL_REGISTERED(ch_number) */
149 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500150 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700151 return xpcAlreadyRegistered;
152 }
153
154 /* register the channel for connection */
155 registration->msg_size = XPC_MSG_SIZE(payload_size);
156 registration->nentries = nentries;
157 registration->assigned_limit = assigned_limit;
158 registration->idle_limit = idle_limit;
159 registration->key = key;
160 registration->func = func;
161
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500162 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700163
164 xpc_interface.connect(ch_number);
165
166 return xpcSuccess;
167}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500168EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700169
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700170/*
171 * Remove the registration for automatic connection of the specified channel
172 * when a partition comes up.
173 *
174 * Before returning this xpc_disconnect() will wait for all connections on the
175 * specified channel have been closed/torndown. So the caller can be assured
176 * that they will not be receiving any more callouts from XPC to their
177 * function registered via xpc_connect().
178 *
179 * Arguments:
180 *
181 * ch_number - channel # to unregister.
182 */
183void
184xpc_disconnect(int ch_number)
185{
186 struct xpc_registration *registration;
187
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700188 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
189
190 registration = &xpc_registrations[ch_number];
191
192 /*
193 * We've decided not to make this a down_interruptible(), since we
194 * figured XPC's users will just turn around and call xpc_disconnect()
195 * again anyways, so we might as well wait, if need be.
196 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500197 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700198
199 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
200 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500201 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700202 return;
203 }
204
205 /* remove the connection registration for the specified channel */
206 registration->func = NULL;
207 registration->key = NULL;
208 registration->nentries = 0;
209 registration->msg_size = 0;
210 registration->assigned_limit = 0;
211 registration->idle_limit = 0;
212
213 xpc_interface.disconnect(ch_number);
214
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500215 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700216
217 return;
218}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500219EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700220
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700221int __init
222xp_init(void)
223{
224 int ret, ch_number;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500225 u64 func_addr = *(u64 *)xp_nofault_PIOR;
226 u64 err_func_addr = *(u64 *)xp_error_PIOR;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700227
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500228 if (!ia64_platform_is("sn2"))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700229 return -ENODEV;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700230
231 /*
232 * Register a nofault code region which performs a cross-partition
233 * PIO read. If the PIO read times out, the MCA handler will consume
234 * the error and return to a kernel-provided instruction to indicate
235 * an error. This PIO read exists because it is guaranteed to timeout
236 * if the destination is down (AMO operations do not timeout on at
237 * least some CPUs on Shubs <= v1.2, which unfortunately we have to
238 * work around).
239 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500240 ret = sn_register_nofault_code(func_addr, err_func_addr, err_func_addr,
241 1, 1);
242 if (ret != 0) {
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700243 printk(KERN_ERR "XP: can't register nofault code, error=%d\n",
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500244 ret);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700245 }
246 /*
247 * Setup the nofault PIO read target. (There is no special reason why
248 * SH_IPI_ACCESS was selected.)
249 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500250 if (is_shub2())
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700251 xp_nofault_PIOR_target = SH2_IPI_ACCESS0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500252 else
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700253 xp_nofault_PIOR_target = SH1_IPI_ACCESS;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700254
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500255 /* initialize the connection registration mutex */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500256 for (ch_number = 0; ch_number < XPC_NCHANNELS; ch_number++)
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500257 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700258
259 return 0;
260}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700261
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500262module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700263
264void __exit
265xp_exit(void)
266{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500267 u64 func_addr = *(u64 *)xp_nofault_PIOR;
268 u64 err_func_addr = *(u64 *)xp_error_PIOR;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700269
270 /* unregister the PIO read nofault code region */
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500271 (void)sn_register_nofault_code(func_addr, err_func_addr,
272 err_func_addr, 1, 0);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700273}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700274
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500275module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700276
277MODULE_AUTHOR("Silicon Graphics, Inc.");
278MODULE_DESCRIPTION("Cross Partition (XP) base");
279MODULE_LICENSE("GPL");