xref: /linux/drivers/misc/sgi-xp/xp_main.c (revision 682128939f546e3a9cdd9fef392b932dd9c41a65)
145d9ca49SDean Nelson /*
245d9ca49SDean Nelson  * This file is subject to the terms and conditions of the GNU General Public
345d9ca49SDean Nelson  * License.  See the file "COPYING" in the main directory of this archive
445d9ca49SDean Nelson  * for more details.
545d9ca49SDean Nelson  *
645d9ca49SDean Nelson  * Copyright (c) 2004-2008 Silicon Graphics, Inc.  All Rights Reserved.
745d9ca49SDean Nelson  */
845d9ca49SDean Nelson 
945d9ca49SDean Nelson /*
1045d9ca49SDean Nelson  * Cross Partition (XP) base.
1145d9ca49SDean Nelson  *
1245d9ca49SDean Nelson  *	XP provides a base from which its users can interact
1345d9ca49SDean Nelson  *	with XPC, yet not be dependent on XPC.
1445d9ca49SDean Nelson  *
1545d9ca49SDean Nelson  */
1645d9ca49SDean Nelson 
1745d9ca49SDean Nelson #include <linux/module.h>
18bc63d387SDean Nelson #include <linux/device.h>
1945d9ca49SDean Nelson #include "xp.h"
2045d9ca49SDean Nelson 
21bc63d387SDean Nelson /* define the XP debug device structures to be used with dev_dbg() et al */
222c2b94f9SDean Nelson 
23bc63d387SDean Nelson struct device_driver xp_dbg_name = {
24bc63d387SDean Nelson 	.name = "xp"
25bc63d387SDean Nelson };
26bc63d387SDean Nelson 
27bc63d387SDean Nelson struct device xp_dbg_subname = {
28bb0dc43eSKay Sievers 	.init_name = "",		/* set to "" */
29bc63d387SDean Nelson 	.driver = &xp_dbg_name
30bc63d387SDean Nelson };
31bc63d387SDean Nelson 
32bc63d387SDean Nelson struct device *xp = &xp_dbg_subname;
33bc63d387SDean Nelson 
34bc63d387SDean Nelson /* max #of partitions possible */
35bc63d387SDean Nelson short xp_max_npartitions;
36bc63d387SDean Nelson EXPORT_SYMBOL_GPL(xp_max_npartitions);
3745d9ca49SDean Nelson 
38261f3b49SDean Nelson short xp_partition_id;
39261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_partition_id);
40261f3b49SDean Nelson 
41261f3b49SDean Nelson u8 xp_region_size;
42261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_region_size);
43261f3b49SDean Nelson 
44a812dcc3SDean Nelson unsigned long (*xp_pa) (void *addr);
45a812dcc3SDean Nelson EXPORT_SYMBOL_GPL(xp_pa);
46a812dcc3SDean Nelson 
47*68212893SRobin Holt unsigned long (*xp_socket_pa) (unsigned long gpa);
48*68212893SRobin Holt EXPORT_SYMBOL_GPL(xp_socket_pa);
49*68212893SRobin Holt 
50a812dcc3SDean Nelson enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
51a812dcc3SDean Nelson 				    const unsigned long src_gpa, size_t len);
52908787dbSDean Nelson EXPORT_SYMBOL_GPL(xp_remote_memcpy);
53908787dbSDean Nelson 
54261f3b49SDean Nelson int (*xp_cpu_to_nasid) (int cpuid);
55261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
56261f3b49SDean Nelson 
576c1c325dSDean Nelson enum xp_retval (*xp_expand_memprotect) (unsigned long phys_addr,
586c1c325dSDean Nelson 					unsigned long size);
596c1c325dSDean Nelson EXPORT_SYMBOL_GPL(xp_expand_memprotect);
606c1c325dSDean Nelson enum xp_retval (*xp_restrict_memprotect) (unsigned long phys_addr,
616c1c325dSDean Nelson 					  unsigned long size);
626c1c325dSDean Nelson EXPORT_SYMBOL_GPL(xp_restrict_memprotect);
636c1c325dSDean Nelson 
6445d9ca49SDean Nelson /*
6545d9ca49SDean Nelson  * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
6645d9ca49SDean Nelson  * users of XPC.
6745d9ca49SDean Nelson  */
68bc63d387SDean Nelson struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
692c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_registrations);
7045d9ca49SDean Nelson 
7145d9ca49SDean Nelson /*
7245d9ca49SDean Nelson  * Initialize the XPC interface to indicate that XPC isn't loaded.
7345d9ca49SDean Nelson  */
7465c17b80SDean Nelson static enum xp_retval
754a3ad2ddSDean Nelson xpc_notloaded(void)
764a3ad2ddSDean Nelson {
7765c17b80SDean Nelson 	return xpNotLoaded;
784a3ad2ddSDean Nelson }
7945d9ca49SDean Nelson 
8045d9ca49SDean Nelson struct xpc_interface xpc_interface = {
8145d9ca49SDean Nelson 	(void (*)(int))xpc_notloaded,
8245d9ca49SDean Nelson 	(void (*)(int))xpc_notloaded,
8397bf1aa1SDean Nelson 	(enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded,
8497bf1aa1SDean Nelson 	(enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func,
8597bf1aa1SDean Nelson 			   void *))xpc_notloaded,
8664d032baSDean Nelson 	(void (*)(short, int, void *))xpc_notloaded,
8764d032baSDean Nelson 	(enum xp_retval(*)(short, void *))xpc_notloaded
8845d9ca49SDean Nelson };
892c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_interface);
9045d9ca49SDean Nelson 
9145d9ca49SDean Nelson /*
9245d9ca49SDean Nelson  * XPC calls this when it (the XPC module) has been loaded.
9345d9ca49SDean Nelson  */
9445d9ca49SDean Nelson void
9545d9ca49SDean Nelson xpc_set_interface(void (*connect) (int),
9645d9ca49SDean Nelson 		  void (*disconnect) (int),
9797bf1aa1SDean Nelson 		  enum xp_retval (*send) (short, int, u32, void *, u16),
9897bf1aa1SDean Nelson 		  enum xp_retval (*send_notify) (short, int, u32, void *, u16,
9945d9ca49SDean Nelson 						  xpc_notify_func, void *),
10064d032baSDean Nelson 		  void (*received) (short, int, void *),
10164d032baSDean Nelson 		  enum xp_retval (*partid_to_nasids) (short, void *))
10245d9ca49SDean Nelson {
10345d9ca49SDean Nelson 	xpc_interface.connect = connect;
10445d9ca49SDean Nelson 	xpc_interface.disconnect = disconnect;
10545d9ca49SDean Nelson 	xpc_interface.send = send;
10645d9ca49SDean Nelson 	xpc_interface.send_notify = send_notify;
10745d9ca49SDean Nelson 	xpc_interface.received = received;
10845d9ca49SDean Nelson 	xpc_interface.partid_to_nasids = partid_to_nasids;
10945d9ca49SDean Nelson }
1102c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_set_interface);
11145d9ca49SDean Nelson 
11245d9ca49SDean Nelson /*
11345d9ca49SDean Nelson  * XPC calls this when it (the XPC module) is being unloaded.
11445d9ca49SDean Nelson  */
11545d9ca49SDean Nelson void
11645d9ca49SDean Nelson xpc_clear_interface(void)
11745d9ca49SDean Nelson {
11845d9ca49SDean Nelson 	xpc_interface.connect = (void (*)(int))xpc_notloaded;
11945d9ca49SDean Nelson 	xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
12097bf1aa1SDean Nelson 	xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
12145d9ca49SDean Nelson 	    xpc_notloaded;
12297bf1aa1SDean Nelson 	xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
12397bf1aa1SDean Nelson 						       u16, xpc_notify_func,
1244a3ad2ddSDean Nelson 						       void *))xpc_notloaded;
12564d032baSDean Nelson 	xpc_interface.received = (void (*)(short, int, void *))
12645d9ca49SDean Nelson 	    xpc_notloaded;
12764d032baSDean Nelson 	xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
12845d9ca49SDean Nelson 	    xpc_notloaded;
12945d9ca49SDean Nelson }
1302c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_clear_interface);
13145d9ca49SDean Nelson 
13245d9ca49SDean Nelson /*
13345d9ca49SDean Nelson  * Register for automatic establishment of a channel connection whenever
13445d9ca49SDean Nelson  * a partition comes up.
13545d9ca49SDean Nelson  *
13645d9ca49SDean Nelson  * Arguments:
13745d9ca49SDean Nelson  *
13845d9ca49SDean Nelson  *	ch_number - channel # to register for connection.
13945d9ca49SDean Nelson  *	func - function to call for asynchronous notification of channel
14045d9ca49SDean Nelson  *	       state changes (i.e., connection, disconnection, error) and
14145d9ca49SDean Nelson  *	       the arrival of incoming messages.
14245d9ca49SDean Nelson  *      key - pointer to optional user-defined value that gets passed back
14345d9ca49SDean Nelson  *	      to the user on any callouts made to func.
14445d9ca49SDean Nelson  *	payload_size - size in bytes of the XPC message's payload area which
14545d9ca49SDean Nelson  *		       contains a user-defined message. The user should make
14645d9ca49SDean Nelson  *		       this large enough to hold their largest message.
14745d9ca49SDean Nelson  *	nentries - max #of XPC message entries a message queue can contain.
14845d9ca49SDean Nelson  *		   The actual number, which is determined when a connection
14945d9ca49SDean Nelson  * 		   is established and may be less then requested, will be
15065c17b80SDean Nelson  *		   passed to the user via the xpConnected callout.
15145d9ca49SDean Nelson  *	assigned_limit - max number of kthreads allowed to be processing
15245d9ca49SDean Nelson  * 			 messages (per connection) at any given instant.
15345d9ca49SDean Nelson  *	idle_limit - max number of kthreads allowed to be idle at any given
15445d9ca49SDean Nelson  * 		     instant.
15545d9ca49SDean Nelson  */
15665c17b80SDean Nelson enum xp_retval
15745d9ca49SDean Nelson xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
15845d9ca49SDean Nelson 	    u16 nentries, u32 assigned_limit, u32 idle_limit)
15945d9ca49SDean Nelson {
16045d9ca49SDean Nelson 	struct xpc_registration *registration;
16145d9ca49SDean Nelson 
162bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
16345d9ca49SDean Nelson 	DBUG_ON(payload_size == 0 || nentries == 0);
16445d9ca49SDean Nelson 	DBUG_ON(func == NULL);
16545d9ca49SDean Nelson 	DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
16645d9ca49SDean Nelson 
167bd3e64c1SDean Nelson 	if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE)
168bd3e64c1SDean Nelson 		return xpPayloadTooBig;
169bd3e64c1SDean Nelson 
17045d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
17145d9ca49SDean Nelson 
1722c2b94f9SDean Nelson 	if (mutex_lock_interruptible(&registration->mutex) != 0)
17365c17b80SDean Nelson 		return xpInterrupted;
17445d9ca49SDean Nelson 
17545d9ca49SDean Nelson 	/* if XPC_CHANNEL_REGISTERED(ch_number) */
17645d9ca49SDean Nelson 	if (registration->func != NULL) {
17745d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
17865c17b80SDean Nelson 		return xpAlreadyRegistered;
17945d9ca49SDean Nelson 	}
18045d9ca49SDean Nelson 
18145d9ca49SDean Nelson 	/* register the channel for connection */
182bd3e64c1SDean Nelson 	registration->entry_size = XPC_MSG_SIZE(payload_size);
18345d9ca49SDean Nelson 	registration->nentries = nentries;
18445d9ca49SDean Nelson 	registration->assigned_limit = assigned_limit;
18545d9ca49SDean Nelson 	registration->idle_limit = idle_limit;
18645d9ca49SDean Nelson 	registration->key = key;
18745d9ca49SDean Nelson 	registration->func = func;
18845d9ca49SDean Nelson 
18945d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
19045d9ca49SDean Nelson 
19145d9ca49SDean Nelson 	xpc_interface.connect(ch_number);
19245d9ca49SDean Nelson 
19365c17b80SDean Nelson 	return xpSuccess;
19445d9ca49SDean Nelson }
1952c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_connect);
19645d9ca49SDean Nelson 
19745d9ca49SDean Nelson /*
19845d9ca49SDean Nelson  * Remove the registration for automatic connection of the specified channel
19945d9ca49SDean Nelson  * when a partition comes up.
20045d9ca49SDean Nelson  *
20145d9ca49SDean Nelson  * Before returning this xpc_disconnect() will wait for all connections on the
20245d9ca49SDean Nelson  * specified channel have been closed/torndown. So the caller can be assured
20345d9ca49SDean Nelson  * that they will not be receiving any more callouts from XPC to their
20445d9ca49SDean Nelson  * function registered via xpc_connect().
20545d9ca49SDean Nelson  *
20645d9ca49SDean Nelson  * Arguments:
20745d9ca49SDean Nelson  *
20845d9ca49SDean Nelson  *	ch_number - channel # to unregister.
20945d9ca49SDean Nelson  */
21045d9ca49SDean Nelson void
21145d9ca49SDean Nelson xpc_disconnect(int ch_number)
21245d9ca49SDean Nelson {
21345d9ca49SDean Nelson 	struct xpc_registration *registration;
21445d9ca49SDean Nelson 
215bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
21645d9ca49SDean Nelson 
21745d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
21845d9ca49SDean Nelson 
21945d9ca49SDean Nelson 	/*
22045d9ca49SDean Nelson 	 * We've decided not to make this a down_interruptible(), since we
22145d9ca49SDean Nelson 	 * figured XPC's users will just turn around and call xpc_disconnect()
22245d9ca49SDean Nelson 	 * again anyways, so we might as well wait, if need be.
22345d9ca49SDean Nelson 	 */
22445d9ca49SDean Nelson 	mutex_lock(&registration->mutex);
22545d9ca49SDean Nelson 
22645d9ca49SDean Nelson 	/* if !XPC_CHANNEL_REGISTERED(ch_number) */
22745d9ca49SDean Nelson 	if (registration->func == NULL) {
22845d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
22945d9ca49SDean Nelson 		return;
23045d9ca49SDean Nelson 	}
23145d9ca49SDean Nelson 
23245d9ca49SDean Nelson 	/* remove the connection registration for the specified channel */
23345d9ca49SDean Nelson 	registration->func = NULL;
23445d9ca49SDean Nelson 	registration->key = NULL;
23545d9ca49SDean Nelson 	registration->nentries = 0;
236bd3e64c1SDean Nelson 	registration->entry_size = 0;
23745d9ca49SDean Nelson 	registration->assigned_limit = 0;
23845d9ca49SDean Nelson 	registration->idle_limit = 0;
23945d9ca49SDean Nelson 
24045d9ca49SDean Nelson 	xpc_interface.disconnect(ch_number);
24145d9ca49SDean Nelson 
24245d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
24345d9ca49SDean Nelson 
24445d9ca49SDean Nelson 	return;
24545d9ca49SDean Nelson }
2462c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_disconnect);
24745d9ca49SDean Nelson 
24845d9ca49SDean Nelson int __init
24945d9ca49SDean Nelson xp_init(void)
25045d9ca49SDean Nelson {
251bc63d387SDean Nelson 	enum xp_retval ret;
252bc63d387SDean Nelson 	int ch_number;
25345d9ca49SDean Nelson 
254e873cff0SRobin Holt 	/* initialize the connection registration mutex */
255e873cff0SRobin Holt 	for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
256e873cff0SRobin Holt 		mutex_init(&xpc_registrations[ch_number].mutex);
257e873cff0SRobin Holt 
258bc63d387SDean Nelson 	if (is_shub())
259bc63d387SDean Nelson 		ret = xp_init_sn2();
260bc63d387SDean Nelson 	else if (is_uv())
261bc63d387SDean Nelson 		ret = xp_init_uv();
262bc63d387SDean Nelson 	else
263e873cff0SRobin Holt 		ret = 0;
264bc63d387SDean Nelson 
265bc63d387SDean Nelson 	if (ret != xpSuccess)
266e873cff0SRobin Holt 		return ret;
26745d9ca49SDean Nelson 
26845d9ca49SDean Nelson 	return 0;
26945d9ca49SDean Nelson }
27045d9ca49SDean Nelson 
2714a3ad2ddSDean Nelson module_init(xp_init);
27245d9ca49SDean Nelson 
27345d9ca49SDean Nelson void __exit
27445d9ca49SDean Nelson xp_exit(void)
27545d9ca49SDean Nelson {
276bc63d387SDean Nelson 	if (is_shub())
277bc63d387SDean Nelson 		xp_exit_sn2();
278bc63d387SDean Nelson 	else if (is_uv())
279bc63d387SDean Nelson 		xp_exit_uv();
28045d9ca49SDean Nelson }
28145d9ca49SDean Nelson 
2824a3ad2ddSDean Nelson module_exit(xp_exit);
28345d9ca49SDean Nelson 
28445d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc.");
28545d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition (XP) base");
28645d9ca49SDean Nelson MODULE_LICENSE("GPL");
287