xref: /openbmc/linux/drivers/misc/sgi-xp/xp_main.c (revision 95ef32cd)
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 
4768212893SRobin Holt unsigned long (*xp_socket_pa) (unsigned long gpa);
4868212893SRobin Holt EXPORT_SYMBOL_GPL(xp_socket_pa);
4968212893SRobin 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 /*
72234041dfSKees Cook  * Initialize the XPC interface to NULL to indicate that XPC isn't loaded.
7345d9ca49SDean Nelson  */
74234041dfSKees Cook struct xpc_interface xpc_interface = { };
752c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_interface);
7645d9ca49SDean Nelson 
7745d9ca49SDean Nelson /*
7845d9ca49SDean Nelson  * XPC calls this when it (the XPC module) has been loaded.
7945d9ca49SDean Nelson  */
8045d9ca49SDean Nelson void
8145d9ca49SDean Nelson xpc_set_interface(void (*connect) (int),
8245d9ca49SDean Nelson 		  void (*disconnect) (int),
8397bf1aa1SDean Nelson 		  enum xp_retval (*send) (short, int, u32, void *, u16),
8497bf1aa1SDean Nelson 		  enum xp_retval (*send_notify) (short, int, u32, void *, u16,
8545d9ca49SDean Nelson 						  xpc_notify_func, void *),
8664d032baSDean Nelson 		  void (*received) (short, int, void *),
8764d032baSDean Nelson 		  enum xp_retval (*partid_to_nasids) (short, void *))
8845d9ca49SDean Nelson {
8945d9ca49SDean Nelson 	xpc_interface.connect = connect;
9045d9ca49SDean Nelson 	xpc_interface.disconnect = disconnect;
9145d9ca49SDean Nelson 	xpc_interface.send = send;
9245d9ca49SDean Nelson 	xpc_interface.send_notify = send_notify;
9345d9ca49SDean Nelson 	xpc_interface.received = received;
9445d9ca49SDean Nelson 	xpc_interface.partid_to_nasids = partid_to_nasids;
9545d9ca49SDean Nelson }
962c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_set_interface);
9745d9ca49SDean Nelson 
9845d9ca49SDean Nelson /*
9945d9ca49SDean Nelson  * XPC calls this when it (the XPC module) is being unloaded.
10045d9ca49SDean Nelson  */
10145d9ca49SDean Nelson void
10245d9ca49SDean Nelson xpc_clear_interface(void)
10345d9ca49SDean Nelson {
104234041dfSKees Cook 	memset(&xpc_interface, 0, sizeof(xpc_interface));
10545d9ca49SDean Nelson }
1062c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_clear_interface);
10745d9ca49SDean Nelson 
10845d9ca49SDean Nelson /*
10945d9ca49SDean Nelson  * Register for automatic establishment of a channel connection whenever
11045d9ca49SDean Nelson  * a partition comes up.
11145d9ca49SDean Nelson  *
11245d9ca49SDean Nelson  * Arguments:
11345d9ca49SDean Nelson  *
11445d9ca49SDean Nelson  *	ch_number - channel # to register for connection.
11545d9ca49SDean Nelson  *	func - function to call for asynchronous notification of channel
11645d9ca49SDean Nelson  *	       state changes (i.e., connection, disconnection, error) and
11745d9ca49SDean Nelson  *	       the arrival of incoming messages.
11845d9ca49SDean Nelson  *      key - pointer to optional user-defined value that gets passed back
11945d9ca49SDean Nelson  *	      to the user on any callouts made to func.
12045d9ca49SDean Nelson  *	payload_size - size in bytes of the XPC message's payload area which
12145d9ca49SDean Nelson  *		       contains a user-defined message. The user should make
12245d9ca49SDean Nelson  *		       this large enough to hold their largest message.
12345d9ca49SDean Nelson  *	nentries - max #of XPC message entries a message queue can contain.
12445d9ca49SDean Nelson  *		   The actual number, which is determined when a connection
12545d9ca49SDean Nelson  * 		   is established and may be less then requested, will be
12665c17b80SDean Nelson  *		   passed to the user via the xpConnected callout.
12745d9ca49SDean Nelson  *	assigned_limit - max number of kthreads allowed to be processing
12845d9ca49SDean Nelson  * 			 messages (per connection) at any given instant.
12945d9ca49SDean Nelson  *	idle_limit - max number of kthreads allowed to be idle at any given
13045d9ca49SDean Nelson  * 		     instant.
13145d9ca49SDean Nelson  */
13265c17b80SDean Nelson enum xp_retval
13345d9ca49SDean Nelson xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
13445d9ca49SDean Nelson 	    u16 nentries, u32 assigned_limit, u32 idle_limit)
13545d9ca49SDean Nelson {
13645d9ca49SDean Nelson 	struct xpc_registration *registration;
13745d9ca49SDean Nelson 
138bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
13945d9ca49SDean Nelson 	DBUG_ON(payload_size == 0 || nentries == 0);
14045d9ca49SDean Nelson 	DBUG_ON(func == NULL);
14145d9ca49SDean Nelson 	DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
14245d9ca49SDean Nelson 
143bd3e64c1SDean Nelson 	if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE)
144bd3e64c1SDean Nelson 		return xpPayloadTooBig;
145bd3e64c1SDean Nelson 
14645d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
14745d9ca49SDean Nelson 
1482c2b94f9SDean Nelson 	if (mutex_lock_interruptible(&registration->mutex) != 0)
14965c17b80SDean Nelson 		return xpInterrupted;
15045d9ca49SDean Nelson 
15145d9ca49SDean Nelson 	/* if XPC_CHANNEL_REGISTERED(ch_number) */
15245d9ca49SDean Nelson 	if (registration->func != NULL) {
15345d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
15465c17b80SDean Nelson 		return xpAlreadyRegistered;
15545d9ca49SDean Nelson 	}
15645d9ca49SDean Nelson 
15745d9ca49SDean Nelson 	/* register the channel for connection */
158bd3e64c1SDean Nelson 	registration->entry_size = XPC_MSG_SIZE(payload_size);
15945d9ca49SDean Nelson 	registration->nentries = nentries;
16045d9ca49SDean Nelson 	registration->assigned_limit = assigned_limit;
16145d9ca49SDean Nelson 	registration->idle_limit = idle_limit;
16245d9ca49SDean Nelson 	registration->key = key;
16345d9ca49SDean Nelson 	registration->func = func;
16445d9ca49SDean Nelson 
16545d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
16645d9ca49SDean Nelson 
167234041dfSKees Cook 	if (xpc_interface.connect)
16845d9ca49SDean Nelson 		xpc_interface.connect(ch_number);
16945d9ca49SDean Nelson 
17065c17b80SDean Nelson 	return xpSuccess;
17145d9ca49SDean Nelson }
1722c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_connect);
17345d9ca49SDean Nelson 
17445d9ca49SDean Nelson /*
17545d9ca49SDean Nelson  * Remove the registration for automatic connection of the specified channel
17645d9ca49SDean Nelson  * when a partition comes up.
17745d9ca49SDean Nelson  *
17845d9ca49SDean Nelson  * Before returning this xpc_disconnect() will wait for all connections on the
17945d9ca49SDean Nelson  * specified channel have been closed/torndown. So the caller can be assured
18045d9ca49SDean Nelson  * that they will not be receiving any more callouts from XPC to their
18145d9ca49SDean Nelson  * function registered via xpc_connect().
18245d9ca49SDean Nelson  *
18345d9ca49SDean Nelson  * Arguments:
18445d9ca49SDean Nelson  *
18545d9ca49SDean Nelson  *	ch_number - channel # to unregister.
18645d9ca49SDean Nelson  */
18745d9ca49SDean Nelson void
18845d9ca49SDean Nelson xpc_disconnect(int ch_number)
18945d9ca49SDean Nelson {
19045d9ca49SDean Nelson 	struct xpc_registration *registration;
19145d9ca49SDean Nelson 
192bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
19345d9ca49SDean Nelson 
19445d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
19545d9ca49SDean Nelson 
19645d9ca49SDean Nelson 	/*
19745d9ca49SDean Nelson 	 * We've decided not to make this a down_interruptible(), since we
19845d9ca49SDean Nelson 	 * figured XPC's users will just turn around and call xpc_disconnect()
19945d9ca49SDean Nelson 	 * again anyways, so we might as well wait, if need be.
20045d9ca49SDean Nelson 	 */
20145d9ca49SDean Nelson 	mutex_lock(&registration->mutex);
20245d9ca49SDean Nelson 
20345d9ca49SDean Nelson 	/* if !XPC_CHANNEL_REGISTERED(ch_number) */
20445d9ca49SDean Nelson 	if (registration->func == NULL) {
20545d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
20645d9ca49SDean Nelson 		return;
20745d9ca49SDean Nelson 	}
20845d9ca49SDean Nelson 
20945d9ca49SDean Nelson 	/* remove the connection registration for the specified channel */
21045d9ca49SDean Nelson 	registration->func = NULL;
21145d9ca49SDean Nelson 	registration->key = NULL;
21245d9ca49SDean Nelson 	registration->nentries = 0;
213bd3e64c1SDean Nelson 	registration->entry_size = 0;
21445d9ca49SDean Nelson 	registration->assigned_limit = 0;
21545d9ca49SDean Nelson 	registration->idle_limit = 0;
21645d9ca49SDean Nelson 
217234041dfSKees Cook 	if (xpc_interface.disconnect)
21845d9ca49SDean Nelson 		xpc_interface.disconnect(ch_number);
21945d9ca49SDean Nelson 
22045d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
22145d9ca49SDean Nelson 
22245d9ca49SDean Nelson 	return;
22345d9ca49SDean Nelson }
2242c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_disconnect);
22545d9ca49SDean Nelson 
22695ef32cdSLee Jones static int __init
22745d9ca49SDean Nelson xp_init(void)
22845d9ca49SDean Nelson {
229bc63d387SDean Nelson 	enum xp_retval ret;
230bc63d387SDean Nelson 	int ch_number;
23145d9ca49SDean Nelson 
232e873cff0SRobin Holt 	/* initialize the connection registration mutex */
233e873cff0SRobin Holt 	for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
234e873cff0SRobin Holt 		mutex_init(&xpc_registrations[ch_number].mutex);
235e873cff0SRobin Holt 
2369726bfcdSChristoph Hellwig 	if (is_uv())
237bc63d387SDean Nelson 		ret = xp_init_uv();
238bc63d387SDean Nelson 	else
239e873cff0SRobin Holt 		ret = 0;
240bc63d387SDean Nelson 
241bc63d387SDean Nelson 	if (ret != xpSuccess)
242e873cff0SRobin Holt 		return ret;
24345d9ca49SDean Nelson 
24445d9ca49SDean Nelson 	return 0;
24545d9ca49SDean Nelson }
24645d9ca49SDean Nelson 
2474a3ad2ddSDean Nelson module_init(xp_init);
24845d9ca49SDean Nelson 
24995ef32cdSLee Jones static void __exit
25045d9ca49SDean Nelson xp_exit(void)
25145d9ca49SDean Nelson {
2529726bfcdSChristoph Hellwig 	if (is_uv())
253bc63d387SDean Nelson 		xp_exit_uv();
25445d9ca49SDean Nelson }
25545d9ca49SDean Nelson 
2564a3ad2ddSDean Nelson module_exit(xp_exit);
25745d9ca49SDean Nelson 
25845d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc.");
25945d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition (XP) base");
26045d9ca49SDean Nelson MODULE_LICENSE("GPL");
261