xref: /openbmc/linux/drivers/misc/sgi-xp/xp_main.c (revision 64d032ba)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (c) 2004-2008 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8 
9 /*
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 
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <asm/sn/intr.h>
22 #include <asm/sn/sn_sal.h>
23 #include "xp.h"
24 
25 /*
26  * 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.
29  */
30 EXPORT_SYMBOL_GPL(xp_nofault_PIOR);
31 
32 u64 xp_nofault_PIOR_target;
33 EXPORT_SYMBOL_GPL(xp_nofault_PIOR_target);
34 
35 /*
36  * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
37  * users of XPC.
38  */
39 struct xpc_registration xpc_registrations[XPC_NCHANNELS];
40 EXPORT_SYMBOL_GPL(xpc_registrations);
41 
42 /*
43  * Initialize the XPC interface to indicate that XPC isn't loaded.
44  */
45 static enum xp_retval
46 xpc_notloaded(void)
47 {
48 	return xpNotLoaded;
49 }
50 
51 struct xpc_interface xpc_interface = {
52 	(void (*)(int))xpc_notloaded,
53 	(void (*)(int))xpc_notloaded,
54 	(enum xp_retval(*)(short, int, u32, void **))xpc_notloaded,
55 	(enum xp_retval(*)(short, int, void *))xpc_notloaded,
56 	(enum xp_retval(*)(short, int, void *, xpc_notify_func, void *))
57 	    xpc_notloaded,
58 	(void (*)(short, int, void *))xpc_notloaded,
59 	(enum xp_retval(*)(short, void *))xpc_notloaded
60 };
61 EXPORT_SYMBOL_GPL(xpc_interface);
62 
63 /*
64  * XPC calls this when it (the XPC module) has been loaded.
65  */
66 void
67 xpc_set_interface(void (*connect) (int),
68 		  void (*disconnect) (int),
69 		  enum xp_retval (*allocate) (short, int, u32, void **),
70 		  enum xp_retval (*send) (short, int, void *),
71 		  enum xp_retval (*send_notify) (short, int, void *,
72 						  xpc_notify_func, void *),
73 		  void (*received) (short, int, void *),
74 		  enum xp_retval (*partid_to_nasids) (short, void *))
75 {
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 }
84 EXPORT_SYMBOL_GPL(xpc_set_interface);
85 
86 /*
87  * XPC calls this when it (the XPC module) is being unloaded.
88  */
89 void
90 xpc_clear_interface(void)
91 {
92 	xpc_interface.connect = (void (*)(int))xpc_notloaded;
93 	xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
94 	xpc_interface.allocate = (enum xp_retval(*)(short, int, u32,
95 						     void **))xpc_notloaded;
96 	xpc_interface.send = (enum xp_retval(*)(short, int, void *))
97 	    xpc_notloaded;
98 	xpc_interface.send_notify = (enum xp_retval(*)(short, int, void *,
99 							xpc_notify_func,
100 							void *))xpc_notloaded;
101 	xpc_interface.received = (void (*)(short, int, void *))
102 	    xpc_notloaded;
103 	xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
104 	    xpc_notloaded;
105 }
106 EXPORT_SYMBOL_GPL(xpc_clear_interface);
107 
108 /*
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 xpConnected 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  */
132 enum xp_retval
133 xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
134 	    u16 nentries, u32 assigned_limit, u32 idle_limit)
135 {
136 	struct xpc_registration *registration;
137 
138 	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 
145 	if (mutex_lock_interruptible(&registration->mutex) != 0)
146 		return xpInterrupted;
147 
148 	/* if XPC_CHANNEL_REGISTERED(ch_number) */
149 	if (registration->func != NULL) {
150 		mutex_unlock(&registration->mutex);
151 		return xpAlreadyRegistered;
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 
162 	mutex_unlock(&registration->mutex);
163 
164 	xpc_interface.connect(ch_number);
165 
166 	return xpSuccess;
167 }
168 EXPORT_SYMBOL_GPL(xpc_connect);
169 
170 /*
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  */
183 void
184 xpc_disconnect(int ch_number)
185 {
186 	struct xpc_registration *registration;
187 
188 	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 	 */
197 	mutex_lock(&registration->mutex);
198 
199 	/* if !XPC_CHANNEL_REGISTERED(ch_number) */
200 	if (registration->func == NULL) {
201 		mutex_unlock(&registration->mutex);
202 		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 
215 	mutex_unlock(&registration->mutex);
216 
217 	return;
218 }
219 EXPORT_SYMBOL_GPL(xpc_disconnect);
220 
221 int __init
222 xp_init(void)
223 {
224 	int ret, ch_number;
225 	u64 func_addr = *(u64 *)xp_nofault_PIOR;
226 	u64 err_func_addr = *(u64 *)xp_error_PIOR;
227 
228 	if (!ia64_platform_is("sn2"))
229 		return -ENODEV;
230 
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 	 */
240 	ret = sn_register_nofault_code(func_addr, err_func_addr, err_func_addr,
241 				       1, 1);
242 	if (ret != 0) {
243 		printk(KERN_ERR "XP: can't register nofault code, error=%d\n",
244 		       ret);
245 	}
246 	/*
247 	 * Setup the nofault PIO read target. (There is no special reason why
248 	 * SH_IPI_ACCESS was selected.)
249 	 */
250 	if (is_shub2())
251 		xp_nofault_PIOR_target = SH2_IPI_ACCESS0;
252 	else
253 		xp_nofault_PIOR_target = SH1_IPI_ACCESS;
254 
255 	/* initialize the connection registration mutex */
256 	for (ch_number = 0; ch_number < XPC_NCHANNELS; ch_number++)
257 		mutex_init(&xpc_registrations[ch_number].mutex);
258 
259 	return 0;
260 }
261 
262 module_init(xp_init);
263 
264 void __exit
265 xp_exit(void)
266 {
267 	u64 func_addr = *(u64 *)xp_nofault_PIOR;
268 	u64 err_func_addr = *(u64 *)xp_error_PIOR;
269 
270 	/* unregister the PIO read nofault code region */
271 	(void)sn_register_nofault_code(func_addr, err_func_addr,
272 				       err_func_addr, 1, 0);
273 }
274 
275 module_exit(xp_exit);
276 
277 MODULE_AUTHOR("Silicon Graphics, Inc.");
278 MODULE_DESCRIPTION("Cross Partition (XP) base");
279 MODULE_LICENSE("GPL");
280