xref: /openbmc/linux/drivers/misc/sgi-xp/xp_main.c (revision a812dcc3)
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 = {
28bc63d387SDean Nelson 	.bus_id = {0},		/* 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 
47a812dcc3SDean Nelson enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
48a812dcc3SDean Nelson 				    const unsigned long src_gpa, size_t len);
49908787dbSDean Nelson EXPORT_SYMBOL_GPL(xp_remote_memcpy);
50908787dbSDean Nelson 
51261f3b49SDean Nelson int (*xp_cpu_to_nasid) (int cpuid);
52261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
53261f3b49SDean Nelson 
5445d9ca49SDean Nelson /*
5545d9ca49SDean Nelson  * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
5645d9ca49SDean Nelson  * users of XPC.
5745d9ca49SDean Nelson  */
58bc63d387SDean Nelson struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
592c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_registrations);
6045d9ca49SDean Nelson 
6145d9ca49SDean Nelson /*
6245d9ca49SDean Nelson  * Initialize the XPC interface to indicate that XPC isn't loaded.
6345d9ca49SDean Nelson  */
6465c17b80SDean Nelson static enum xp_retval
654a3ad2ddSDean Nelson xpc_notloaded(void)
664a3ad2ddSDean Nelson {
6765c17b80SDean Nelson 	return xpNotLoaded;
684a3ad2ddSDean Nelson }
6945d9ca49SDean Nelson 
7045d9ca49SDean Nelson struct xpc_interface xpc_interface = {
7145d9ca49SDean Nelson 	(void (*)(int))xpc_notloaded,
7245d9ca49SDean Nelson 	(void (*)(int))xpc_notloaded,
7397bf1aa1SDean Nelson 	(enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded,
7497bf1aa1SDean Nelson 	(enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func,
7597bf1aa1SDean Nelson 			   void *))xpc_notloaded,
7664d032baSDean Nelson 	(void (*)(short, int, void *))xpc_notloaded,
7764d032baSDean Nelson 	(enum xp_retval(*)(short, void *))xpc_notloaded
7845d9ca49SDean Nelson };
792c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_interface);
8045d9ca49SDean Nelson 
8145d9ca49SDean Nelson /*
8245d9ca49SDean Nelson  * XPC calls this when it (the XPC module) has been loaded.
8345d9ca49SDean Nelson  */
8445d9ca49SDean Nelson void
8545d9ca49SDean Nelson xpc_set_interface(void (*connect) (int),
8645d9ca49SDean Nelson 		  void (*disconnect) (int),
8797bf1aa1SDean Nelson 		  enum xp_retval (*send) (short, int, u32, void *, u16),
8897bf1aa1SDean Nelson 		  enum xp_retval (*send_notify) (short, int, u32, void *, u16,
8945d9ca49SDean Nelson 						  xpc_notify_func, void *),
9064d032baSDean Nelson 		  void (*received) (short, int, void *),
9164d032baSDean Nelson 		  enum xp_retval (*partid_to_nasids) (short, void *))
9245d9ca49SDean Nelson {
9345d9ca49SDean Nelson 	xpc_interface.connect = connect;
9445d9ca49SDean Nelson 	xpc_interface.disconnect = disconnect;
9545d9ca49SDean Nelson 	xpc_interface.send = send;
9645d9ca49SDean Nelson 	xpc_interface.send_notify = send_notify;
9745d9ca49SDean Nelson 	xpc_interface.received = received;
9845d9ca49SDean Nelson 	xpc_interface.partid_to_nasids = partid_to_nasids;
9945d9ca49SDean Nelson }
1002c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_set_interface);
10145d9ca49SDean Nelson 
10245d9ca49SDean Nelson /*
10345d9ca49SDean Nelson  * XPC calls this when it (the XPC module) is being unloaded.
10445d9ca49SDean Nelson  */
10545d9ca49SDean Nelson void
10645d9ca49SDean Nelson xpc_clear_interface(void)
10745d9ca49SDean Nelson {
10845d9ca49SDean Nelson 	xpc_interface.connect = (void (*)(int))xpc_notloaded;
10945d9ca49SDean Nelson 	xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
11097bf1aa1SDean Nelson 	xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
11145d9ca49SDean Nelson 	    xpc_notloaded;
11297bf1aa1SDean Nelson 	xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
11397bf1aa1SDean Nelson 						       u16, xpc_notify_func,
1144a3ad2ddSDean Nelson 						       void *))xpc_notloaded;
11564d032baSDean Nelson 	xpc_interface.received = (void (*)(short, int, void *))
11645d9ca49SDean Nelson 	    xpc_notloaded;
11764d032baSDean Nelson 	xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
11845d9ca49SDean Nelson 	    xpc_notloaded;
11945d9ca49SDean Nelson }
1202c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_clear_interface);
12145d9ca49SDean Nelson 
12245d9ca49SDean Nelson /*
12345d9ca49SDean Nelson  * Register for automatic establishment of a channel connection whenever
12445d9ca49SDean Nelson  * a partition comes up.
12545d9ca49SDean Nelson  *
12645d9ca49SDean Nelson  * Arguments:
12745d9ca49SDean Nelson  *
12845d9ca49SDean Nelson  *	ch_number - channel # to register for connection.
12945d9ca49SDean Nelson  *	func - function to call for asynchronous notification of channel
13045d9ca49SDean Nelson  *	       state changes (i.e., connection, disconnection, error) and
13145d9ca49SDean Nelson  *	       the arrival of incoming messages.
13245d9ca49SDean Nelson  *      key - pointer to optional user-defined value that gets passed back
13345d9ca49SDean Nelson  *	      to the user on any callouts made to func.
13445d9ca49SDean Nelson  *	payload_size - size in bytes of the XPC message's payload area which
13545d9ca49SDean Nelson  *		       contains a user-defined message. The user should make
13645d9ca49SDean Nelson  *		       this large enough to hold their largest message.
13745d9ca49SDean Nelson  *	nentries - max #of XPC message entries a message queue can contain.
13845d9ca49SDean Nelson  *		   The actual number, which is determined when a connection
13945d9ca49SDean Nelson  * 		   is established and may be less then requested, will be
14065c17b80SDean Nelson  *		   passed to the user via the xpConnected callout.
14145d9ca49SDean Nelson  *	assigned_limit - max number of kthreads allowed to be processing
14245d9ca49SDean Nelson  * 			 messages (per connection) at any given instant.
14345d9ca49SDean Nelson  *	idle_limit - max number of kthreads allowed to be idle at any given
14445d9ca49SDean Nelson  * 		     instant.
14545d9ca49SDean Nelson  */
14665c17b80SDean Nelson enum xp_retval
14745d9ca49SDean Nelson xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
14845d9ca49SDean Nelson 	    u16 nentries, u32 assigned_limit, u32 idle_limit)
14945d9ca49SDean Nelson {
15045d9ca49SDean Nelson 	struct xpc_registration *registration;
15145d9ca49SDean Nelson 
152bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
15345d9ca49SDean Nelson 	DBUG_ON(payload_size == 0 || nentries == 0);
15445d9ca49SDean Nelson 	DBUG_ON(func == NULL);
15545d9ca49SDean Nelson 	DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
15645d9ca49SDean Nelson 
15745d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
15845d9ca49SDean Nelson 
1592c2b94f9SDean Nelson 	if (mutex_lock_interruptible(&registration->mutex) != 0)
16065c17b80SDean Nelson 		return xpInterrupted;
16145d9ca49SDean Nelson 
16245d9ca49SDean Nelson 	/* if XPC_CHANNEL_REGISTERED(ch_number) */
16345d9ca49SDean Nelson 	if (registration->func != NULL) {
16445d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
16565c17b80SDean Nelson 		return xpAlreadyRegistered;
16645d9ca49SDean Nelson 	}
16745d9ca49SDean Nelson 
16845d9ca49SDean Nelson 	/* register the channel for connection */
16945d9ca49SDean Nelson 	registration->msg_size = XPC_MSG_SIZE(payload_size);
17045d9ca49SDean Nelson 	registration->nentries = nentries;
17145d9ca49SDean Nelson 	registration->assigned_limit = assigned_limit;
17245d9ca49SDean Nelson 	registration->idle_limit = idle_limit;
17345d9ca49SDean Nelson 	registration->key = key;
17445d9ca49SDean Nelson 	registration->func = func;
17545d9ca49SDean Nelson 
17645d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
17745d9ca49SDean Nelson 
17845d9ca49SDean Nelson 	xpc_interface.connect(ch_number);
17945d9ca49SDean Nelson 
18065c17b80SDean Nelson 	return xpSuccess;
18145d9ca49SDean Nelson }
1822c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_connect);
18345d9ca49SDean Nelson 
18445d9ca49SDean Nelson /*
18545d9ca49SDean Nelson  * Remove the registration for automatic connection of the specified channel
18645d9ca49SDean Nelson  * when a partition comes up.
18745d9ca49SDean Nelson  *
18845d9ca49SDean Nelson  * Before returning this xpc_disconnect() will wait for all connections on the
18945d9ca49SDean Nelson  * specified channel have been closed/torndown. So the caller can be assured
19045d9ca49SDean Nelson  * that they will not be receiving any more callouts from XPC to their
19145d9ca49SDean Nelson  * function registered via xpc_connect().
19245d9ca49SDean Nelson  *
19345d9ca49SDean Nelson  * Arguments:
19445d9ca49SDean Nelson  *
19545d9ca49SDean Nelson  *	ch_number - channel # to unregister.
19645d9ca49SDean Nelson  */
19745d9ca49SDean Nelson void
19845d9ca49SDean Nelson xpc_disconnect(int ch_number)
19945d9ca49SDean Nelson {
20045d9ca49SDean Nelson 	struct xpc_registration *registration;
20145d9ca49SDean Nelson 
202bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
20345d9ca49SDean Nelson 
20445d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
20545d9ca49SDean Nelson 
20645d9ca49SDean Nelson 	/*
20745d9ca49SDean Nelson 	 * We've decided not to make this a down_interruptible(), since we
20845d9ca49SDean Nelson 	 * figured XPC's users will just turn around and call xpc_disconnect()
20945d9ca49SDean Nelson 	 * again anyways, so we might as well wait, if need be.
21045d9ca49SDean Nelson 	 */
21145d9ca49SDean Nelson 	mutex_lock(&registration->mutex);
21245d9ca49SDean Nelson 
21345d9ca49SDean Nelson 	/* if !XPC_CHANNEL_REGISTERED(ch_number) */
21445d9ca49SDean Nelson 	if (registration->func == NULL) {
21545d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
21645d9ca49SDean Nelson 		return;
21745d9ca49SDean Nelson 	}
21845d9ca49SDean Nelson 
21945d9ca49SDean Nelson 	/* remove the connection registration for the specified channel */
22045d9ca49SDean Nelson 	registration->func = NULL;
22145d9ca49SDean Nelson 	registration->key = NULL;
22245d9ca49SDean Nelson 	registration->nentries = 0;
22345d9ca49SDean Nelson 	registration->msg_size = 0;
22445d9ca49SDean Nelson 	registration->assigned_limit = 0;
22545d9ca49SDean Nelson 	registration->idle_limit = 0;
22645d9ca49SDean Nelson 
22745d9ca49SDean Nelson 	xpc_interface.disconnect(ch_number);
22845d9ca49SDean Nelson 
22945d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
23045d9ca49SDean Nelson 
23145d9ca49SDean Nelson 	return;
23245d9ca49SDean Nelson }
2332c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_disconnect);
23445d9ca49SDean Nelson 
23545d9ca49SDean Nelson int __init
23645d9ca49SDean Nelson xp_init(void)
23745d9ca49SDean Nelson {
238bc63d387SDean Nelson 	enum xp_retval ret;
239bc63d387SDean Nelson 	int ch_number;
24045d9ca49SDean Nelson 
241bc63d387SDean Nelson 	if (is_shub())
242bc63d387SDean Nelson 		ret = xp_init_sn2();
243bc63d387SDean Nelson 	else if (is_uv())
244bc63d387SDean Nelson 		ret = xp_init_uv();
245bc63d387SDean Nelson 	else
246bc63d387SDean Nelson 		ret = xpUnsupported;
247bc63d387SDean Nelson 
248bc63d387SDean Nelson 	if (ret != xpSuccess)
24945d9ca49SDean Nelson 		return -ENODEV;
25045d9ca49SDean Nelson 
25145d9ca49SDean Nelson 	/* initialize the connection registration mutex */
252bc63d387SDean Nelson 	for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
25345d9ca49SDean Nelson 		mutex_init(&xpc_registrations[ch_number].mutex);
25445d9ca49SDean Nelson 
25545d9ca49SDean Nelson 	return 0;
25645d9ca49SDean Nelson }
25745d9ca49SDean Nelson 
2584a3ad2ddSDean Nelson module_init(xp_init);
25945d9ca49SDean Nelson 
26045d9ca49SDean Nelson void __exit
26145d9ca49SDean Nelson xp_exit(void)
26245d9ca49SDean Nelson {
263bc63d387SDean Nelson 	if (is_shub())
264bc63d387SDean Nelson 		xp_exit_sn2();
265bc63d387SDean Nelson 	else if (is_uv())
266bc63d387SDean Nelson 		xp_exit_uv();
26745d9ca49SDean Nelson }
26845d9ca49SDean Nelson 
2694a3ad2ddSDean Nelson module_exit(xp_exit);
27045d9ca49SDean Nelson 
27145d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc.");
27245d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition (XP) base");
27345d9ca49SDean Nelson MODULE_LICENSE("GPL");
274