blob: bb9257642fcffcc70d5ea60f2f86ae2f57d2d65d [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/kernel.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050020#include <linux/mutex.h>
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070021#include <asm/sn/intr.h>
22#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050023#include "xp.h"
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070024
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070025/*
26 * Target of nofault PIO read.
27 */
28u64 xp_nofault_PIOR_target;
29
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070030/*
31 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
32 * users of XPC.
33 */
34struct xpc_registration xpc_registrations[XPC_NCHANNELS];
35
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070036/*
37 * Initialize the XPC interface to indicate that XPC isn't loaded.
38 */
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050039static enum xpc_retval
40xpc_notloaded(void)
41{
42 return xpcNotLoaded;
43}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070044
45struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050046 (void (*)(int))xpc_notloaded,
47 (void (*)(int))xpc_notloaded,
48 (enum xpc_retval(*)(partid_t, int, u32, void **))xpc_notloaded,
49 (enum xpc_retval(*)(partid_t, int, void *))xpc_notloaded,
50 (enum xpc_retval(*)(partid_t, int, void *, xpc_notify_func, void *))
51 xpc_notloaded,
52 (void (*)(partid_t, int, void *))xpc_notloaded,
53 (enum xpc_retval(*)(partid_t, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070054};
55
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070056/*
57 * XPC calls this when it (the XPC module) has been loaded.
58 */
59void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050060xpc_set_interface(void (*connect) (int),
61 void (*disconnect) (int),
62 enum xpc_retval (*allocate) (partid_t, int, u32, void **),
63 enum xpc_retval (*send) (partid_t, int, void *),
64 enum xpc_retval (*send_notify) (partid_t, int, void *,
65 xpc_notify_func, void *),
66 void (*received) (partid_t, int, void *),
67 enum xpc_retval (*partid_to_nasids) (partid_t, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070068{
69 xpc_interface.connect = connect;
70 xpc_interface.disconnect = disconnect;
71 xpc_interface.allocate = allocate;
72 xpc_interface.send = send;
73 xpc_interface.send_notify = send_notify;
74 xpc_interface.received = received;
75 xpc_interface.partid_to_nasids = partid_to_nasids;
76}
77
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070078/*
79 * XPC calls this when it (the XPC module) is being unloaded.
80 */
81void
82xpc_clear_interface(void)
83{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050084 xpc_interface.connect = (void (*)(int))xpc_notloaded;
85 xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
86 xpc_interface.allocate = (enum xpc_retval(*)(partid_t, int, u32,
87 void **))xpc_notloaded;
88 xpc_interface.send = (enum xpc_retval(*)(partid_t, int, void *))
89 xpc_notloaded;
90 xpc_interface.send_notify = (enum xpc_retval(*)(partid_t, int, void *,
91 xpc_notify_func,
92 void *))xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070093 xpc_interface.received = (void (*)(partid_t, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050094 xpc_notloaded;
95 xpc_interface.partid_to_nasids = (enum xpc_retval(*)(partid_t, void *))
96 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070097}
98
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070099/*
100 * Register for automatic establishment of a channel connection whenever
101 * a partition comes up.
102 *
103 * Arguments:
104 *
105 * ch_number - channel # to register for connection.
106 * func - function to call for asynchronous notification of channel
107 * state changes (i.e., connection, disconnection, error) and
108 * the arrival of incoming messages.
109 * key - pointer to optional user-defined value that gets passed back
110 * to the user on any callouts made to func.
111 * payload_size - size in bytes of the XPC message's payload area which
112 * contains a user-defined message. The user should make
113 * this large enough to hold their largest message.
114 * nentries - max #of XPC message entries a message queue can contain.
115 * The actual number, which is determined when a connection
116 * is established and may be less then requested, will be
117 * passed to the user via the xpcConnected callout.
118 * assigned_limit - max number of kthreads allowed to be processing
119 * messages (per connection) at any given instant.
120 * idle_limit - max number of kthreads allowed to be idle at any given
121 * instant.
122 */
123enum xpc_retval
124xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500125 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700126{
127 struct xpc_registration *registration;
128
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700129 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
130 DBUG_ON(payload_size == 0 || nentries == 0);
131 DBUG_ON(func == NULL);
132 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
133
134 registration = &xpc_registrations[ch_number];
135
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500136 if (mutex_lock_interruptible(&registration->mutex) != 0) {
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700137 return xpcInterrupted;
138 }
139
140 /* if XPC_CHANNEL_REGISTERED(ch_number) */
141 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500142 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700143 return xpcAlreadyRegistered;
144 }
145
146 /* register the channel for connection */
147 registration->msg_size = XPC_MSG_SIZE(payload_size);
148 registration->nentries = nentries;
149 registration->assigned_limit = assigned_limit;
150 registration->idle_limit = idle_limit;
151 registration->key = key;
152 registration->func = func;
153
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500154 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700155
156 xpc_interface.connect(ch_number);
157
158 return xpcSuccess;
159}
160
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700161/*
162 * Remove the registration for automatic connection of the specified channel
163 * when a partition comes up.
164 *
165 * Before returning this xpc_disconnect() will wait for all connections on the
166 * specified channel have been closed/torndown. So the caller can be assured
167 * that they will not be receiving any more callouts from XPC to their
168 * function registered via xpc_connect().
169 *
170 * Arguments:
171 *
172 * ch_number - channel # to unregister.
173 */
174void
175xpc_disconnect(int ch_number)
176{
177 struct xpc_registration *registration;
178
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700179 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
180
181 registration = &xpc_registrations[ch_number];
182
183 /*
184 * We've decided not to make this a down_interruptible(), since we
185 * figured XPC's users will just turn around and call xpc_disconnect()
186 * again anyways, so we might as well wait, if need be.
187 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500188 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700189
190 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
191 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500192 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700193 return;
194 }
195
196 /* remove the connection registration for the specified channel */
197 registration->func = NULL;
198 registration->key = NULL;
199 registration->nentries = 0;
200 registration->msg_size = 0;
201 registration->assigned_limit = 0;
202 registration->idle_limit = 0;
203
204 xpc_interface.disconnect(ch_number);
205
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500206 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700207
208 return;
209}
210
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700211int __init
212xp_init(void)
213{
214 int ret, ch_number;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500215 u64 func_addr = *(u64 *)xp_nofault_PIOR;
216 u64 err_func_addr = *(u64 *)xp_error_PIOR;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700217
218 if (!ia64_platform_is("sn2")) {
219 return -ENODEV;
220 }
221
222 /*
223 * Register a nofault code region which performs a cross-partition
224 * PIO read. If the PIO read times out, the MCA handler will consume
225 * the error and return to a kernel-provided instruction to indicate
226 * an error. This PIO read exists because it is guaranteed to timeout
227 * if the destination is down (AMO operations do not timeout on at
228 * least some CPUs on Shubs <= v1.2, which unfortunately we have to
229 * work around).
230 */
231 if ((ret = sn_register_nofault_code(func_addr, err_func_addr,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500232 err_func_addr, 1, 1)) != 0) {
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700233 printk(KERN_ERR "XP: can't register nofault code, error=%d\n",
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500234 ret);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700235 }
236 /*
237 * Setup the nofault PIO read target. (There is no special reason why
238 * SH_IPI_ACCESS was selected.)
239 */
240 if (is_shub2()) {
241 xp_nofault_PIOR_target = SH2_IPI_ACCESS0;
242 } else {
243 xp_nofault_PIOR_target = SH1_IPI_ACCESS;
244 }
245
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500246 /* initialize the connection registration mutex */
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700247 for (ch_number = 0; ch_number < XPC_NCHANNELS; ch_number++) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500248 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700249 }
250
251 return 0;
252}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700253
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500254module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700255
256void __exit
257xp_exit(void)
258{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500259 u64 func_addr = *(u64 *)xp_nofault_PIOR;
260 u64 err_func_addr = *(u64 *)xp_error_PIOR;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700261
262 /* unregister the PIO read nofault code region */
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500263 (void)sn_register_nofault_code(func_addr, err_func_addr,
264 err_func_addr, 1, 0);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700265}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700266
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500267module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700268
269MODULE_AUTHOR("Silicon Graphics, Inc.");
270MODULE_DESCRIPTION("Cross Partition (XP) base");
271MODULE_LICENSE("GPL");
272
273EXPORT_SYMBOL(xp_nofault_PIOR);
274EXPORT_SYMBOL(xp_nofault_PIOR_target);
275EXPORT_SYMBOL(xpc_registrations);
276EXPORT_SYMBOL(xpc_interface);
277EXPORT_SYMBOL(xpc_clear_interface);
278EXPORT_SYMBOL(xpc_set_interface);
279EXPORT_SYMBOL(xpc_connect);
280EXPORT_SYMBOL(xpc_disconnect);