blob: 16f8dcab2da4ba9c63342ed7766a6c741da2b020 [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
47enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
48 const unsigned long src_gpa, size_t len);
Dean Nelson908787d2008-07-29 22:34:05 -070049EXPORT_SYMBOL_GPL(xp_remote_memcpy);
50
Dean Nelson261f3b42008-07-29 22:34:16 -070051int (*xp_cpu_to_nasid) (int cpuid);
52EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
53
Dean Nelson6c1c3252008-11-05 17:27:22 -060054enum xp_retval (*xp_expand_memprotect) (unsigned long phys_addr,
55 unsigned long size);
56EXPORT_SYMBOL_GPL(xp_expand_memprotect);
57enum xp_retval (*xp_restrict_memprotect) (unsigned long phys_addr,
58 unsigned long size);
59EXPORT_SYMBOL_GPL(xp_restrict_memprotect);
60
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070061/*
62 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
63 * users of XPC.
64 */
Dean Nelsonbc63d382008-07-29 22:34:04 -070065struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050066EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070067
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070068/*
69 * Initialize the XPC interface to indicate that XPC isn't loaded.
70 */
Dean Nelson65c17b82008-05-12 14:02:02 -070071static enum xp_retval
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050072xpc_notloaded(void)
73{
Dean Nelson65c17b82008-05-12 14:02:02 -070074 return xpNotLoaded;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050075}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070076
77struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050078 (void (*)(int))xpc_notloaded,
79 (void (*)(int))xpc_notloaded,
Dean Nelson97bf1aa2008-07-29 22:34:08 -070080 (enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded,
81 (enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func,
82 void *))xpc_notloaded,
Dean Nelson64d032b2008-05-12 14:02:03 -070083 (void (*)(short, int, void *))xpc_notloaded,
84 (enum xp_retval(*)(short, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070085};
Dean Nelson2c2b94f2008-04-22 14:50:17 -050086EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070087
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070088/*
89 * XPC calls this when it (the XPC module) has been loaded.
90 */
91void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050092xpc_set_interface(void (*connect) (int),
93 void (*disconnect) (int),
Dean Nelson97bf1aa2008-07-29 22:34:08 -070094 enum xp_retval (*send) (short, int, u32, void *, u16),
95 enum xp_retval (*send_notify) (short, int, u32, void *, u16,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050096 xpc_notify_func, void *),
Dean Nelson64d032b2008-05-12 14:02:03 -070097 void (*received) (short, int, void *),
98 enum xp_retval (*partid_to_nasids) (short, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070099{
100 xpc_interface.connect = connect;
101 xpc_interface.disconnect = disconnect;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700102 xpc_interface.send = send;
103 xpc_interface.send_notify = send_notify;
104 xpc_interface.received = received;
105 xpc_interface.partid_to_nasids = partid_to_nasids;
106}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500107EXPORT_SYMBOL_GPL(xpc_set_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700108
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700109/*
110 * XPC calls this when it (the XPC module) is being unloaded.
111 */
112void
113xpc_clear_interface(void)
114{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500115 xpc_interface.connect = (void (*)(int))xpc_notloaded;
116 xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700117 xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500118 xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700119 xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
120 u16, xpc_notify_func,
121 void *))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700122 xpc_interface.received = (void (*)(short, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500123 xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700124 xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500125 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700126}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500127EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700128
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700129/*
130 * Register for automatic establishment of a channel connection whenever
131 * a partition comes up.
132 *
133 * Arguments:
134 *
135 * ch_number - channel # to register for connection.
136 * func - function to call for asynchronous notification of channel
137 * state changes (i.e., connection, disconnection, error) and
138 * the arrival of incoming messages.
139 * key - pointer to optional user-defined value that gets passed back
140 * to the user on any callouts made to func.
141 * payload_size - size in bytes of the XPC message's payload area which
142 * contains a user-defined message. The user should make
143 * this large enough to hold their largest message.
144 * nentries - max #of XPC message entries a message queue can contain.
145 * The actual number, which is determined when a connection
146 * is established and may be less then requested, will be
Dean Nelson65c17b82008-05-12 14:02:02 -0700147 * passed to the user via the xpConnected callout.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700148 * assigned_limit - max number of kthreads allowed to be processing
149 * messages (per connection) at any given instant.
150 * idle_limit - max number of kthreads allowed to be idle at any given
151 * instant.
152 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700153enum xp_retval
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700154xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500155 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700156{
157 struct xpc_registration *registration;
158
Dean Nelsonbc63d382008-07-29 22:34:04 -0700159 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700160 DBUG_ON(payload_size == 0 || nentries == 0);
161 DBUG_ON(func == NULL);
162 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
163
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700164 if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE)
165 return xpPayloadTooBig;
166
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700167 registration = &xpc_registrations[ch_number];
168
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500169 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700170 return xpInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700171
172 /* if XPC_CHANNEL_REGISTERED(ch_number) */
173 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500174 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700175 return xpAlreadyRegistered;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700176 }
177
178 /* register the channel for connection */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700179 registration->entry_size = XPC_MSG_SIZE(payload_size);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700180 registration->nentries = nentries;
181 registration->assigned_limit = assigned_limit;
182 registration->idle_limit = idle_limit;
183 registration->key = key;
184 registration->func = func;
185
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500186 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700187
188 xpc_interface.connect(ch_number);
189
Dean Nelson65c17b82008-05-12 14:02:02 -0700190 return xpSuccess;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700191}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500192EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700193
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700194/*
195 * Remove the registration for automatic connection of the specified channel
196 * when a partition comes up.
197 *
198 * Before returning this xpc_disconnect() will wait for all connections on the
199 * specified channel have been closed/torndown. So the caller can be assured
200 * that they will not be receiving any more callouts from XPC to their
201 * function registered via xpc_connect().
202 *
203 * Arguments:
204 *
205 * ch_number - channel # to unregister.
206 */
207void
208xpc_disconnect(int ch_number)
209{
210 struct xpc_registration *registration;
211
Dean Nelsonbc63d382008-07-29 22:34:04 -0700212 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700213
214 registration = &xpc_registrations[ch_number];
215
216 /*
217 * We've decided not to make this a down_interruptible(), since we
218 * figured XPC's users will just turn around and call xpc_disconnect()
219 * again anyways, so we might as well wait, if need be.
220 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500221 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700222
223 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
224 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500225 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700226 return;
227 }
228
229 /* remove the connection registration for the specified channel */
230 registration->func = NULL;
231 registration->key = NULL;
232 registration->nentries = 0;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700233 registration->entry_size = 0;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700234 registration->assigned_limit = 0;
235 registration->idle_limit = 0;
236
237 xpc_interface.disconnect(ch_number);
238
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500239 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700240
241 return;
242}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500243EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700244
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700245int __init
246xp_init(void)
247{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700248 enum xp_retval ret;
249 int ch_number;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700250
Dean Nelsonbc63d382008-07-29 22:34:04 -0700251 if (is_shub())
252 ret = xp_init_sn2();
253 else if (is_uv())
254 ret = xp_init_uv();
255 else
256 ret = xpUnsupported;
257
258 if (ret != xpSuccess)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700259 return -ENODEV;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700260
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500261 /* initialize the connection registration mutex */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700262 for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500263 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700264
265 return 0;
266}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700267
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500268module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700269
270void __exit
271xp_exit(void)
272{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700273 if (is_shub())
274 xp_exit_sn2();
275 else if (is_uv())
276 xp_exit_uv();
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700277}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700278
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500279module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700280
281MODULE_AUTHOR("Silicon Graphics, Inc.");
282MODULE_DESCRIPTION("Cross Partition (XP) base");
283MODULE_LICENSE("GPL");