blob: 6d7f557fd1c1a1e885eb3dad64886b3e0afe32fe [file] [log] [blame]
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07001/*
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 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07007 */
8
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07009/*
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
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070017#include <linux/module.h>
Dean Nelsonbc63d382008-07-29 22:34:04 -070018#include <linux/device.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050019#include "xp.h"
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070020
Dean Nelsonbc63d382008-07-29 22:34:04 -070021/* define the XP debug device structures to be used with dev_dbg() et al */
Dean Nelson2c2b94f2008-04-22 14:50:17 -050022
Dean Nelsonbc63d382008-07-29 22:34:04 -070023struct device_driver xp_dbg_name = {
24 .name = "xp"
25};
26
27struct device xp_dbg_subname = {
Kay Sieversbb0dc432009-01-06 10:44:37 -080028 .init_name = "", /* set to "" */
Dean Nelsonbc63d382008-07-29 22:34:04 -070029 .driver = &xp_dbg_name
30};
31
32struct device *xp = &xp_dbg_subname;
33
34/* max #of partitions possible */
35short xp_max_npartitions;
36EXPORT_SYMBOL_GPL(xp_max_npartitions);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070037
Dean Nelson261f3b42008-07-29 22:34:16 -070038short xp_partition_id;
39EXPORT_SYMBOL_GPL(xp_partition_id);
40
41u8 xp_region_size;
42EXPORT_SYMBOL_GPL(xp_region_size);
43
Dean Nelsona812dcc2008-07-29 22:34:16 -070044unsigned long (*xp_pa) (void *addr);
45EXPORT_SYMBOL_GPL(xp_pa);
46
Robin Holt68212892009-12-15 16:47:53 -080047unsigned long (*xp_socket_pa) (unsigned long gpa);
48EXPORT_SYMBOL_GPL(xp_socket_pa);
49
Dean Nelsona812dcc2008-07-29 22:34:16 -070050enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
51 const unsigned long src_gpa, size_t len);
Dean Nelson908787d2008-07-29 22:34:05 -070052EXPORT_SYMBOL_GPL(xp_remote_memcpy);
53
Dean Nelson261f3b42008-07-29 22:34:16 -070054int (*xp_cpu_to_nasid) (int cpuid);
55EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
56
Dean Nelson6c1c3252008-11-05 17:27:22 -060057enum xp_retval (*xp_expand_memprotect) (unsigned long phys_addr,
58 unsigned long size);
59EXPORT_SYMBOL_GPL(xp_expand_memprotect);
60enum xp_retval (*xp_restrict_memprotect) (unsigned long phys_addr,
61 unsigned long size);
62EXPORT_SYMBOL_GPL(xp_restrict_memprotect);
63
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070064/*
65 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
66 * users of XPC.
67 */
Dean Nelsonbc63d382008-07-29 22:34:04 -070068struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050069EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070070
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070071/*
Kees Cook234041d2017-04-04 22:07:10 -070072 * Initialize the XPC interface to NULL to indicate that XPC isn't loaded.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070073 */
Kees Cook234041d2017-04-04 22:07:10 -070074struct xpc_interface xpc_interface = { };
Dean Nelson2c2b94f2008-04-22 14:50:17 -050075EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070076
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070077/*
78 * XPC calls this when it (the XPC module) has been loaded.
79 */
80void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050081xpc_set_interface(void (*connect) (int),
82 void (*disconnect) (int),
Dean Nelson97bf1aa2008-07-29 22:34:08 -070083 enum xp_retval (*send) (short, int, u32, void *, u16),
84 enum xp_retval (*send_notify) (short, int, u32, void *, u16,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050085 xpc_notify_func, void *),
Dean Nelson64d032b2008-05-12 14:02:03 -070086 void (*received) (short, int, void *),
87 enum xp_retval (*partid_to_nasids) (short, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070088{
89 xpc_interface.connect = connect;
90 xpc_interface.disconnect = disconnect;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070091 xpc_interface.send = send;
92 xpc_interface.send_notify = send_notify;
93 xpc_interface.received = received;
94 xpc_interface.partid_to_nasids = partid_to_nasids;
95}
Dean Nelson2c2b94f2008-04-22 14:50:17 -050096EXPORT_SYMBOL_GPL(xpc_set_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070097
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070098/*
99 * XPC calls this when it (the XPC module) is being unloaded.
100 */
101void
102xpc_clear_interface(void)
103{
Kees Cook234041d2017-04-04 22:07:10 -0700104 memset(&xpc_interface, 0, sizeof(xpc_interface));
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700105}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500106EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700107
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700108/*
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
Dean Nelson65c17b82008-05-12 14:02:02 -0700126 * passed to the user via the xpConnected callout.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700127 * 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 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700132enum xp_retval
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700133xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500134 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700135{
136 struct xpc_registration *registration;
137
Dean Nelsonbc63d382008-07-29 22:34:04 -0700138 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700139 DBUG_ON(payload_size == 0 || nentries == 0);
140 DBUG_ON(func == NULL);
141 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
142
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700143 if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE)
144 return xpPayloadTooBig;
145
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700146 registration = &xpc_registrations[ch_number];
147
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500148 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700149 return xpInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700150
151 /* if XPC_CHANNEL_REGISTERED(ch_number) */
152 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500153 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700154 return xpAlreadyRegistered;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700155 }
156
157 /* register the channel for connection */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700158 registration->entry_size = XPC_MSG_SIZE(payload_size);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700159 registration->nentries = nentries;
160 registration->assigned_limit = assigned_limit;
161 registration->idle_limit = idle_limit;
162 registration->key = key;
163 registration->func = func;
164
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500165 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700166
Kees Cook234041d2017-04-04 22:07:10 -0700167 if (xpc_interface.connect)
168 xpc_interface.connect(ch_number);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700169
Dean Nelson65c17b82008-05-12 14:02:02 -0700170 return xpSuccess;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700171}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500172EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700173
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700174/*
175 * Remove the registration for automatic connection of the specified channel
176 * when a partition comes up.
177 *
178 * Before returning this xpc_disconnect() will wait for all connections on the
179 * specified channel have been closed/torndown. So the caller can be assured
180 * that they will not be receiving any more callouts from XPC to their
181 * function registered via xpc_connect().
182 *
183 * Arguments:
184 *
185 * ch_number - channel # to unregister.
186 */
187void
188xpc_disconnect(int ch_number)
189{
190 struct xpc_registration *registration;
191
Dean Nelsonbc63d382008-07-29 22:34:04 -0700192 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700193
194 registration = &xpc_registrations[ch_number];
195
196 /*
197 * We've decided not to make this a down_interruptible(), since we
198 * figured XPC's users will just turn around and call xpc_disconnect()
199 * again anyways, so we might as well wait, if need be.
200 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500201 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700202
203 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
204 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500205 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700206 return;
207 }
208
209 /* remove the connection registration for the specified channel */
210 registration->func = NULL;
211 registration->key = NULL;
212 registration->nentries = 0;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700213 registration->entry_size = 0;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700214 registration->assigned_limit = 0;
215 registration->idle_limit = 0;
216
Kees Cook234041d2017-04-04 22:07:10 -0700217 if (xpc_interface.disconnect)
218 xpc_interface.disconnect(ch_number);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700219
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500220 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700221
222 return;
223}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500224EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700225
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700226int __init
227xp_init(void)
228{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700229 enum xp_retval ret;
230 int ch_number;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700231
Robin Holte873cff2009-04-21 12:24:53 -0700232 /* initialize the connection registration mutex */
233 for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
234 mutex_init(&xpc_registrations[ch_number].mutex);
235
Dean Nelsonbc63d382008-07-29 22:34:04 -0700236 if (is_shub())
237 ret = xp_init_sn2();
238 else if (is_uv())
239 ret = xp_init_uv();
240 else
Robin Holte873cff2009-04-21 12:24:53 -0700241 ret = 0;
Dean Nelsonbc63d382008-07-29 22:34:04 -0700242
243 if (ret != xpSuccess)
Robin Holte873cff2009-04-21 12:24:53 -0700244 return ret;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700245
246 return 0;
247}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700248
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500249module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700250
251void __exit
252xp_exit(void)
253{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700254 if (is_shub())
255 xp_exit_sn2();
256 else if (is_uv())
257 xp_exit_uv();
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700258}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700259
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500260module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700261
262MODULE_AUTHOR("Silicon Graphics, Inc.");
263MODULE_DESCRIPTION("Cross Partition (XP) base");
264MODULE_LICENSE("GPL");