xref: /linux/drivers/misc/sgi-xp/xp_main.c (revision 7a6d94f0ed957fb667d4d74c5c6c640a26e87c8f)
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  *
6*7a6d94f0SMike Travis  * (C) Copyright 2020 Hewlett Packard Enterprise Development LP
745d9ca49SDean Nelson  * Copyright (c) 2004-2008 Silicon Graphics, Inc.  All Rights Reserved.
845d9ca49SDean Nelson  */
945d9ca49SDean Nelson 
1045d9ca49SDean Nelson /*
1145d9ca49SDean Nelson  * Cross Partition (XP) base.
1245d9ca49SDean Nelson  *
1345d9ca49SDean Nelson  *	XP provides a base from which its users can interact
1445d9ca49SDean Nelson  *	with XPC, yet not be dependent on XPC.
1545d9ca49SDean Nelson  *
1645d9ca49SDean Nelson  */
1745d9ca49SDean Nelson 
1845d9ca49SDean Nelson #include <linux/module.h>
19bc63d387SDean Nelson #include <linux/device.h>
2045d9ca49SDean Nelson #include "xp.h"
2145d9ca49SDean Nelson 
22bc63d387SDean Nelson /* define the XP debug device structures to be used with dev_dbg() et al */
232c2b94f9SDean Nelson 
24bc63d387SDean Nelson struct device_driver xp_dbg_name = {
25bc63d387SDean Nelson 	.name = "xp"
26bc63d387SDean Nelson };
27bc63d387SDean Nelson 
28bc63d387SDean Nelson struct device xp_dbg_subname = {
29bb0dc43eSKay Sievers 	.init_name = "",		/* set to "" */
30bc63d387SDean Nelson 	.driver = &xp_dbg_name
31bc63d387SDean Nelson };
32bc63d387SDean Nelson 
33bc63d387SDean Nelson struct device *xp = &xp_dbg_subname;
34bc63d387SDean Nelson 
35bc63d387SDean Nelson /* max #of partitions possible */
36bc63d387SDean Nelson short xp_max_npartitions;
37bc63d387SDean Nelson EXPORT_SYMBOL_GPL(xp_max_npartitions);
3845d9ca49SDean Nelson 
39261f3b49SDean Nelson short xp_partition_id;
40261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_partition_id);
41261f3b49SDean Nelson 
42261f3b49SDean Nelson u8 xp_region_size;
43261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_region_size);
44261f3b49SDean Nelson 
45a812dcc3SDean Nelson unsigned long (*xp_pa) (void *addr);
46a812dcc3SDean Nelson EXPORT_SYMBOL_GPL(xp_pa);
47a812dcc3SDean Nelson 
4868212893SRobin Holt unsigned long (*xp_socket_pa) (unsigned long gpa);
4968212893SRobin Holt EXPORT_SYMBOL_GPL(xp_socket_pa);
5068212893SRobin Holt 
51a812dcc3SDean Nelson enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
52a812dcc3SDean Nelson 				    const unsigned long src_gpa, size_t len);
53908787dbSDean Nelson EXPORT_SYMBOL_GPL(xp_remote_memcpy);
54908787dbSDean Nelson 
55261f3b49SDean Nelson int (*xp_cpu_to_nasid) (int cpuid);
56261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
57261f3b49SDean Nelson 
586c1c325dSDean Nelson enum xp_retval (*xp_expand_memprotect) (unsigned long phys_addr,
596c1c325dSDean Nelson 					unsigned long size);
606c1c325dSDean Nelson EXPORT_SYMBOL_GPL(xp_expand_memprotect);
616c1c325dSDean Nelson enum xp_retval (*xp_restrict_memprotect) (unsigned long phys_addr,
626c1c325dSDean Nelson 					  unsigned long size);
636c1c325dSDean Nelson EXPORT_SYMBOL_GPL(xp_restrict_memprotect);
646c1c325dSDean Nelson 
6545d9ca49SDean Nelson /*
6645d9ca49SDean Nelson  * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
6745d9ca49SDean Nelson  * users of XPC.
6845d9ca49SDean Nelson  */
69bc63d387SDean Nelson struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
702c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_registrations);
7145d9ca49SDean Nelson 
7245d9ca49SDean Nelson /*
73234041dfSKees Cook  * Initialize the XPC interface to NULL to indicate that XPC isn't loaded.
7445d9ca49SDean Nelson  */
75234041dfSKees Cook struct xpc_interface xpc_interface = { };
762c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_interface);
7745d9ca49SDean Nelson 
7845d9ca49SDean Nelson /*
7945d9ca49SDean Nelson  * XPC calls this when it (the XPC module) has been loaded.
8045d9ca49SDean Nelson  */
8145d9ca49SDean Nelson void
8245d9ca49SDean Nelson xpc_set_interface(void (*connect) (int),
8345d9ca49SDean Nelson 		  void (*disconnect) (int),
8497bf1aa1SDean Nelson 		  enum xp_retval (*send) (short, int, u32, void *, u16),
8597bf1aa1SDean Nelson 		  enum xp_retval (*send_notify) (short, int, u32, void *, u16,
8645d9ca49SDean Nelson 						  xpc_notify_func, void *),
8764d032baSDean Nelson 		  void (*received) (short, int, void *),
8864d032baSDean Nelson 		  enum xp_retval (*partid_to_nasids) (short, void *))
8945d9ca49SDean Nelson {
9045d9ca49SDean Nelson 	xpc_interface.connect = connect;
9145d9ca49SDean Nelson 	xpc_interface.disconnect = disconnect;
9245d9ca49SDean Nelson 	xpc_interface.send = send;
9345d9ca49SDean Nelson 	xpc_interface.send_notify = send_notify;
9445d9ca49SDean Nelson 	xpc_interface.received = received;
9545d9ca49SDean Nelson 	xpc_interface.partid_to_nasids = partid_to_nasids;
9645d9ca49SDean Nelson }
972c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_set_interface);
9845d9ca49SDean Nelson 
9945d9ca49SDean Nelson /*
10045d9ca49SDean Nelson  * XPC calls this when it (the XPC module) is being unloaded.
10145d9ca49SDean Nelson  */
10245d9ca49SDean Nelson void
10345d9ca49SDean Nelson xpc_clear_interface(void)
10445d9ca49SDean Nelson {
105234041dfSKees Cook 	memset(&xpc_interface, 0, sizeof(xpc_interface));
10645d9ca49SDean Nelson }
1072c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_clear_interface);
10845d9ca49SDean Nelson 
10945d9ca49SDean Nelson /*
11045d9ca49SDean Nelson  * Register for automatic establishment of a channel connection whenever
11145d9ca49SDean Nelson  * a partition comes up.
11245d9ca49SDean Nelson  *
11345d9ca49SDean Nelson  * Arguments:
11445d9ca49SDean Nelson  *
11545d9ca49SDean Nelson  *	ch_number - channel # to register for connection.
11645d9ca49SDean Nelson  *	func - function to call for asynchronous notification of channel
11745d9ca49SDean Nelson  *	       state changes (i.e., connection, disconnection, error) and
11845d9ca49SDean Nelson  *	       the arrival of incoming messages.
11945d9ca49SDean Nelson  *      key - pointer to optional user-defined value that gets passed back
12045d9ca49SDean Nelson  *	      to the user on any callouts made to func.
12145d9ca49SDean Nelson  *	payload_size - size in bytes of the XPC message's payload area which
12245d9ca49SDean Nelson  *		       contains a user-defined message. The user should make
12345d9ca49SDean Nelson  *		       this large enough to hold their largest message.
12445d9ca49SDean Nelson  *	nentries - max #of XPC message entries a message queue can contain.
12545d9ca49SDean Nelson  *		   The actual number, which is determined when a connection
12645d9ca49SDean Nelson  * 		   is established and may be less then requested, will be
12765c17b80SDean Nelson  *		   passed to the user via the xpConnected callout.
12845d9ca49SDean Nelson  *	assigned_limit - max number of kthreads allowed to be processing
12945d9ca49SDean Nelson  * 			 messages (per connection) at any given instant.
13045d9ca49SDean Nelson  *	idle_limit - max number of kthreads allowed to be idle at any given
13145d9ca49SDean Nelson  * 		     instant.
13245d9ca49SDean Nelson  */
13365c17b80SDean Nelson enum xp_retval
13445d9ca49SDean Nelson xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
13545d9ca49SDean Nelson 	    u16 nentries, u32 assigned_limit, u32 idle_limit)
13645d9ca49SDean Nelson {
13745d9ca49SDean Nelson 	struct xpc_registration *registration;
13845d9ca49SDean Nelson 
139bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
14045d9ca49SDean Nelson 	DBUG_ON(payload_size == 0 || nentries == 0);
14145d9ca49SDean Nelson 	DBUG_ON(func == NULL);
14245d9ca49SDean Nelson 	DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
14345d9ca49SDean Nelson 
144bd3e64c1SDean Nelson 	if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE)
145bd3e64c1SDean Nelson 		return xpPayloadTooBig;
146bd3e64c1SDean Nelson 
14745d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
14845d9ca49SDean Nelson 
1492c2b94f9SDean Nelson 	if (mutex_lock_interruptible(&registration->mutex) != 0)
15065c17b80SDean Nelson 		return xpInterrupted;
15145d9ca49SDean Nelson 
15245d9ca49SDean Nelson 	/* if XPC_CHANNEL_REGISTERED(ch_number) */
15345d9ca49SDean Nelson 	if (registration->func != NULL) {
15445d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
15565c17b80SDean Nelson 		return xpAlreadyRegistered;
15645d9ca49SDean Nelson 	}
15745d9ca49SDean Nelson 
15845d9ca49SDean Nelson 	/* register the channel for connection */
159bd3e64c1SDean Nelson 	registration->entry_size = XPC_MSG_SIZE(payload_size);
16045d9ca49SDean Nelson 	registration->nentries = nentries;
16145d9ca49SDean Nelson 	registration->assigned_limit = assigned_limit;
16245d9ca49SDean Nelson 	registration->idle_limit = idle_limit;
16345d9ca49SDean Nelson 	registration->key = key;
16445d9ca49SDean Nelson 	registration->func = func;
16545d9ca49SDean Nelson 
16645d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
16745d9ca49SDean Nelson 
168234041dfSKees Cook 	if (xpc_interface.connect)
16945d9ca49SDean Nelson 		xpc_interface.connect(ch_number);
17045d9ca49SDean Nelson 
17165c17b80SDean Nelson 	return xpSuccess;
17245d9ca49SDean Nelson }
1732c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_connect);
17445d9ca49SDean Nelson 
17545d9ca49SDean Nelson /*
17645d9ca49SDean Nelson  * Remove the registration for automatic connection of the specified channel
17745d9ca49SDean Nelson  * when a partition comes up.
17845d9ca49SDean Nelson  *
17945d9ca49SDean Nelson  * Before returning this xpc_disconnect() will wait for all connections on the
18045d9ca49SDean Nelson  * specified channel have been closed/torndown. So the caller can be assured
18145d9ca49SDean Nelson  * that they will not be receiving any more callouts from XPC to their
18245d9ca49SDean Nelson  * function registered via xpc_connect().
18345d9ca49SDean Nelson  *
18445d9ca49SDean Nelson  * Arguments:
18545d9ca49SDean Nelson  *
18645d9ca49SDean Nelson  *	ch_number - channel # to unregister.
18745d9ca49SDean Nelson  */
18845d9ca49SDean Nelson void
18945d9ca49SDean Nelson xpc_disconnect(int ch_number)
19045d9ca49SDean Nelson {
19145d9ca49SDean Nelson 	struct xpc_registration *registration;
19245d9ca49SDean Nelson 
193bc63d387SDean Nelson 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
19445d9ca49SDean Nelson 
19545d9ca49SDean Nelson 	registration = &xpc_registrations[ch_number];
19645d9ca49SDean Nelson 
19745d9ca49SDean Nelson 	/*
19845d9ca49SDean Nelson 	 * We've decided not to make this a down_interruptible(), since we
19945d9ca49SDean Nelson 	 * figured XPC's users will just turn around and call xpc_disconnect()
20045d9ca49SDean Nelson 	 * again anyways, so we might as well wait, if need be.
20145d9ca49SDean Nelson 	 */
20245d9ca49SDean Nelson 	mutex_lock(&registration->mutex);
20345d9ca49SDean Nelson 
20445d9ca49SDean Nelson 	/* if !XPC_CHANNEL_REGISTERED(ch_number) */
20545d9ca49SDean Nelson 	if (registration->func == NULL) {
20645d9ca49SDean Nelson 		mutex_unlock(&registration->mutex);
20745d9ca49SDean Nelson 		return;
20845d9ca49SDean Nelson 	}
20945d9ca49SDean Nelson 
21045d9ca49SDean Nelson 	/* remove the connection registration for the specified channel */
21145d9ca49SDean Nelson 	registration->func = NULL;
21245d9ca49SDean Nelson 	registration->key = NULL;
21345d9ca49SDean Nelson 	registration->nentries = 0;
214bd3e64c1SDean Nelson 	registration->entry_size = 0;
21545d9ca49SDean Nelson 	registration->assigned_limit = 0;
21645d9ca49SDean Nelson 	registration->idle_limit = 0;
21745d9ca49SDean Nelson 
218234041dfSKees Cook 	if (xpc_interface.disconnect)
21945d9ca49SDean Nelson 		xpc_interface.disconnect(ch_number);
22045d9ca49SDean Nelson 
22145d9ca49SDean Nelson 	mutex_unlock(&registration->mutex);
22245d9ca49SDean Nelson 
22345d9ca49SDean Nelson 	return;
22445d9ca49SDean Nelson }
2252c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_disconnect);
22645d9ca49SDean Nelson 
22795ef32cdSLee Jones static int __init
22845d9ca49SDean Nelson xp_init(void)
22945d9ca49SDean Nelson {
230bc63d387SDean Nelson 	enum xp_retval ret;
231bc63d387SDean Nelson 	int ch_number;
23245d9ca49SDean Nelson 
233e873cff0SRobin Holt 	/* initialize the connection registration mutex */
234e873cff0SRobin Holt 	for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
235e873cff0SRobin Holt 		mutex_init(&xpc_registrations[ch_number].mutex);
236e873cff0SRobin Holt 
237788b66e3SMike Travis 	if (is_uv_system())
238bc63d387SDean Nelson 		ret = xp_init_uv();
239bc63d387SDean Nelson 	else
240e873cff0SRobin Holt 		ret = 0;
241bc63d387SDean Nelson 
242bc63d387SDean Nelson 	if (ret != xpSuccess)
243e873cff0SRobin Holt 		return ret;
24445d9ca49SDean Nelson 
24545d9ca49SDean Nelson 	return 0;
24645d9ca49SDean Nelson }
24745d9ca49SDean Nelson 
2484a3ad2ddSDean Nelson module_init(xp_init);
24945d9ca49SDean Nelson 
25095ef32cdSLee Jones static void __exit
25145d9ca49SDean Nelson xp_exit(void)
25245d9ca49SDean Nelson {
253788b66e3SMike Travis 	if (is_uv_system())
254bc63d387SDean Nelson 		xp_exit_uv();
25545d9ca49SDean Nelson }
25645d9ca49SDean Nelson 
2574a3ad2ddSDean Nelson module_exit(xp_exit);
25845d9ca49SDean Nelson 
25945d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc.");
26045d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition (XP) base");
26145d9ca49SDean Nelson MODULE_LICENSE("GPL");
262