1843e1988Sjohnlev /*
2843e1988Sjohnlev  * CDDL HEADER START
3843e1988Sjohnlev  *
4843e1988Sjohnlev  * The contents of this file are subject to the terms of the
5843e1988Sjohnlev  * Common Development and Distribution License (the "License").
6843e1988Sjohnlev  * You may not use this file except in compliance with the License.
7843e1988Sjohnlev  *
8843e1988Sjohnlev  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9843e1988Sjohnlev  * or http://www.opensolaris.org/os/licensing.
10843e1988Sjohnlev  * See the License for the specific language governing permissions
11843e1988Sjohnlev  * and limitations under the License.
12843e1988Sjohnlev  *
13843e1988Sjohnlev  * When distributing Covered Code, include this CDDL HEADER in each
14843e1988Sjohnlev  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15843e1988Sjohnlev  * If applicable, add the following below this CDDL HEADER, with the
16843e1988Sjohnlev  * fields enclosed by brackets "[]" replaced with your own identifying
17843e1988Sjohnlev  * information: Portions Copyright [yyyy] [name of copyright owner]
18843e1988Sjohnlev  *
19843e1988Sjohnlev  * CDDL HEADER END
20843e1988Sjohnlev  */
21843e1988Sjohnlev 
22843e1988Sjohnlev /*
23ea8190a2Ssmaybe  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24843e1988Sjohnlev  * Use is subject to license terms.
25843e1988Sjohnlev  */
26843e1988Sjohnlev 
27843e1988Sjohnlev /*
28843e1988Sjohnlev  *
29843e1988Sjohnlev  * xenbus_comms.c
30843e1988Sjohnlev  *
31843e1988Sjohnlev  * Low level code to talks to Xen Store: ringbuffer and event channel.
32843e1988Sjohnlev  *
33843e1988Sjohnlev  * Copyright (C) 2005 Rusty Russell, IBM Corporation
34843e1988Sjohnlev  *
35843e1988Sjohnlev  * This file may be distributed separately from the Linux kernel, or
36843e1988Sjohnlev  * incorporated into other software packages, subject to the following license:
37843e1988Sjohnlev  *
38843e1988Sjohnlev  * Permission is hereby granted, free of charge, to any person obtaining a copy
39843e1988Sjohnlev  * of this source file (the "Software"), to deal in the Software without
40843e1988Sjohnlev  * restriction, including without limitation the rights to use, copy, modify,
41843e1988Sjohnlev  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
42843e1988Sjohnlev  * and to permit persons to whom the Software is furnished to do so, subject to
43843e1988Sjohnlev  * the following conditions:
44843e1988Sjohnlev  *
45843e1988Sjohnlev  * The above copyright notice and this permission notice shall be included in
46843e1988Sjohnlev  * all copies or substantial portions of the Software.
47843e1988Sjohnlev  *
48843e1988Sjohnlev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49843e1988Sjohnlev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50843e1988Sjohnlev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51843e1988Sjohnlev  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52843e1988Sjohnlev  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53843e1988Sjohnlev  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
54843e1988Sjohnlev  * IN THE SOFTWARE.
55843e1988Sjohnlev  */
56843e1988Sjohnlev 
57843e1988Sjohnlev #include <sys/types.h>
58843e1988Sjohnlev #include <vm/hat.h>
59843e1988Sjohnlev #include <vm/as.h>
60843e1988Sjohnlev #include <sys/bootconf.h>
61843e1988Sjohnlev #include <vm/seg_kmem.h>
62551bc2a6Smrj #ifdef XPV_HVM_DRIVER
63551bc2a6Smrj #include <sys/pc_mmu.h>
64551bc2a6Smrj #include <sys/xpv_support.h>
65551bc2a6Smrj #include <sys/hypervisor.h>
66551bc2a6Smrj #else
67551bc2a6Smrj #include <vm/kboot_mmu.h>
68551bc2a6Smrj #include <sys/bootinfo.h>
69843e1988Sjohnlev #include <sys/hypervisor.h>
70843e1988Sjohnlev #include <sys/evtchn_impl.h>
71551bc2a6Smrj #endif
72843e1988Sjohnlev #include <sys/condvar.h>
73843e1988Sjohnlev #include <sys/mutex.h>
74843e1988Sjohnlev #include <sys/atomic.h>
75843e1988Sjohnlev #include <sys/mman.h>
76843e1988Sjohnlev #include <sys/errno.h>
77843e1988Sjohnlev #include <sys/cmn_err.h>
78843e1988Sjohnlev #include <sys/avintr.h>
79843e1988Sjohnlev #include <xen/sys/xenbus_comms.h>
80843e1988Sjohnlev #include <xen/public/io/xs_wire.h>
81843e1988Sjohnlev 
82ea8190a2Ssmaybe #ifndef XPV_HVM_DRIVER
83843e1988Sjohnlev static int xenbus_irq;
84ea8190a2Ssmaybe #endif
85843e1988Sjohnlev static ddi_umem_cookie_t xb_cookie; /* cookie for xenbus comm page */
86843e1988Sjohnlev extern caddr_t xb_addr;	/* va of xenbus comm page */
87843e1988Sjohnlev 
88843e1988Sjohnlev static kcondvar_t xb_wait_cv;
89843e1988Sjohnlev static kmutex_t xb_wait_lock;
90843e1988Sjohnlev 
91843e1988Sjohnlev #define	xs_domain_interface(ra) ((struct xenstore_domain_interface *)(ra))
92843e1988Sjohnlev 
93843e1988Sjohnlev static uint_t
xenbus_intr(caddr_t arg __unused,caddr_t arg1 __unused)94*db830ba5SToomas Soome xenbus_intr(caddr_t arg __unused, caddr_t arg1 __unused)
95843e1988Sjohnlev {
96843e1988Sjohnlev 	mutex_enter(&xb_wait_lock);
97843e1988Sjohnlev 	cv_broadcast(&xb_wait_cv);
98843e1988Sjohnlev 	mutex_exit(&xb_wait_lock);
99843e1988Sjohnlev 	return (DDI_INTR_CLAIMED);
100843e1988Sjohnlev }
101843e1988Sjohnlev 
102843e1988Sjohnlev static int
check_indexes(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod)103843e1988Sjohnlev check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
104843e1988Sjohnlev {
105843e1988Sjohnlev 	return ((prod - cons) <= XENSTORE_RING_SIZE);
106843e1988Sjohnlev }
107843e1988Sjohnlev 
108843e1988Sjohnlev static void *
get_output_chunk(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod,char * buf,uint32_t * len)109843e1988Sjohnlev get_output_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
110843e1988Sjohnlev     char *buf, uint32_t *len)
111843e1988Sjohnlev {
112843e1988Sjohnlev 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
113843e1988Sjohnlev 	if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
114843e1988Sjohnlev 		*len = XENSTORE_RING_SIZE - (prod - cons);
115843e1988Sjohnlev 	return ((void *)(buf + MASK_XENSTORE_IDX(prod)));
116843e1988Sjohnlev }
117843e1988Sjohnlev 
118843e1988Sjohnlev static const void *
get_input_chunk(XENSTORE_RING_IDX cons,XENSTORE_RING_IDX prod,const char * buf,uint32_t * len)119843e1988Sjohnlev get_input_chunk(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod,
120843e1988Sjohnlev     const char *buf, uint32_t *len)
121843e1988Sjohnlev {
122843e1988Sjohnlev 	*len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
123843e1988Sjohnlev 	if ((prod - cons) < *len)
124843e1988Sjohnlev 		*len = prod - cons;
125843e1988Sjohnlev 	return ((void *)(buf + MASK_XENSTORE_IDX(cons)));
126843e1988Sjohnlev }
127843e1988Sjohnlev 
128843e1988Sjohnlev 
129843e1988Sjohnlev int
xb_write(const void * data,unsigned len)130843e1988Sjohnlev xb_write(const void *data, unsigned len)
131843e1988Sjohnlev {
132843e1988Sjohnlev 	volatile struct xenstore_domain_interface *intf =
133843e1988Sjohnlev 	    xs_domain_interface(xb_addr);
134843e1988Sjohnlev 	XENSTORE_RING_IDX cons, prod;
135843e1988Sjohnlev 	extern int do_polled_io;
136843e1988Sjohnlev 
137843e1988Sjohnlev 	while (len != 0) {
138843e1988Sjohnlev 		void *dst;
139843e1988Sjohnlev 		unsigned int avail;
140843e1988Sjohnlev 
141843e1988Sjohnlev 		mutex_enter(&xb_wait_lock);
142843e1988Sjohnlev 		while ((intf->req_prod - intf->req_cons) ==
143843e1988Sjohnlev 		    XENSTORE_RING_SIZE) {
144843e1988Sjohnlev 			if (interrupts_unleashed && !do_polled_io) {
145843e1988Sjohnlev 				if (cv_wait_sig(&xb_wait_cv,
146843e1988Sjohnlev 				    &xb_wait_lock) == 0) {
147843e1988Sjohnlev 					mutex_exit(&xb_wait_lock);
148843e1988Sjohnlev 					return (EINTR);
149843e1988Sjohnlev 				}
150843e1988Sjohnlev 			} else { /* polled mode needed for early probes */
151843e1988Sjohnlev 				(void) HYPERVISOR_yield();
152843e1988Sjohnlev 			}
153843e1988Sjohnlev 		}
154843e1988Sjohnlev 		mutex_exit(&xb_wait_lock);
155843e1988Sjohnlev 		/* Read indexes, then verify. */
156843e1988Sjohnlev 		cons = intf->req_cons;
157843e1988Sjohnlev 		prod = intf->req_prod;
158843e1988Sjohnlev 		membar_enter();
159843e1988Sjohnlev 		if (!check_indexes(cons, prod))
160843e1988Sjohnlev 			return (EIO);
161843e1988Sjohnlev 
162843e1988Sjohnlev 		dst = get_output_chunk(cons, prod, (char *)intf->req, &avail);
163843e1988Sjohnlev 		if (avail == 0)
164843e1988Sjohnlev 			continue;
165843e1988Sjohnlev 		if (avail > len)
166843e1988Sjohnlev 			avail = len;
167843e1988Sjohnlev 
168843e1988Sjohnlev 		(void) memcpy(dst, data, avail);
169843e1988Sjohnlev 		data = (void *)((uintptr_t)data + avail);
170843e1988Sjohnlev 		len -= avail;
171843e1988Sjohnlev 
172843e1988Sjohnlev 		/* Other side must not see new header until data is there. */
173843e1988Sjohnlev 		membar_producer();
174843e1988Sjohnlev 		intf->req_prod += avail;
175843e1988Sjohnlev 
176843e1988Sjohnlev 		/* This implies mb() before other side sees interrupt. */
177843e1988Sjohnlev 		ec_notify_via_evtchn(xen_info->store_evtchn);
178843e1988Sjohnlev 	}
179843e1988Sjohnlev 
180843e1988Sjohnlev 	return (0);
181843e1988Sjohnlev }
182843e1988Sjohnlev 
183843e1988Sjohnlev int
xb_read(void * data,unsigned len)184843e1988Sjohnlev xb_read(void *data, unsigned len)
185843e1988Sjohnlev {
186843e1988Sjohnlev 	volatile struct xenstore_domain_interface *intf =
187843e1988Sjohnlev 	    xs_domain_interface(xb_addr);
188843e1988Sjohnlev 	XENSTORE_RING_IDX cons, prod;
189843e1988Sjohnlev 	extern int do_polled_io;
190843e1988Sjohnlev 
191843e1988Sjohnlev 	while (len != 0) {
192843e1988Sjohnlev 		unsigned int avail;
193843e1988Sjohnlev 		const char *src;
194843e1988Sjohnlev 
195843e1988Sjohnlev 		mutex_enter(&xb_wait_lock);
196843e1988Sjohnlev 		while (intf->rsp_cons == intf->rsp_prod) {
197843e1988Sjohnlev 			if (interrupts_unleashed && !do_polled_io) {
198843e1988Sjohnlev 				if (cv_wait_sig(&xb_wait_cv,
199843e1988Sjohnlev 				    &xb_wait_lock) == 0) {
200843e1988Sjohnlev 					mutex_exit(&xb_wait_lock);
201843e1988Sjohnlev 					return (EINTR);
202843e1988Sjohnlev 				}
203843e1988Sjohnlev 			} else { /* polled mode needed for early probes */
204843e1988Sjohnlev 				(void) HYPERVISOR_yield();
205843e1988Sjohnlev 			}
206843e1988Sjohnlev 		}
207843e1988Sjohnlev 		mutex_exit(&xb_wait_lock);
208843e1988Sjohnlev 		/* Read indexes, then verify. */
209843e1988Sjohnlev 		cons = intf->rsp_cons;
210843e1988Sjohnlev 		prod = intf->rsp_prod;
211843e1988Sjohnlev 		membar_enter();
212843e1988Sjohnlev 		if (!check_indexes(cons, prod))
213843e1988Sjohnlev 			return (EIO);
214843e1988Sjohnlev 
215843e1988Sjohnlev 		src = get_input_chunk(cons, prod, (char *)intf->rsp, &avail);
216843e1988Sjohnlev 		if (avail == 0)
217843e1988Sjohnlev 			continue;
218843e1988Sjohnlev 		if (avail > len)
219843e1988Sjohnlev 			avail = len;
220843e1988Sjohnlev 
221843e1988Sjohnlev 		/* We must read header before we read data. */
222843e1988Sjohnlev 		membar_consumer();
223843e1988Sjohnlev 
224843e1988Sjohnlev 		(void) memcpy(data, src, avail);
225843e1988Sjohnlev 		data = (void *)((uintptr_t)data + avail);
226843e1988Sjohnlev 		len -= avail;
227843e1988Sjohnlev 
228843e1988Sjohnlev 		/* Other side must not see free space until we've copied out */
229843e1988Sjohnlev 		membar_enter();
230843e1988Sjohnlev 		intf->rsp_cons += avail;
231843e1988Sjohnlev 
232843e1988Sjohnlev 		/* Implies mb(): they will see new header. */
233843e1988Sjohnlev 		ec_notify_via_evtchn(xen_info->store_evtchn);
234843e1988Sjohnlev 	}
235843e1988Sjohnlev 
236843e1988Sjohnlev 	return (0);
237843e1988Sjohnlev }
238843e1988Sjohnlev 
239843e1988Sjohnlev void
xb_suspend(void)240843e1988Sjohnlev xb_suspend(void)
241843e1988Sjohnlev {
242ea8190a2Ssmaybe #ifdef XPV_HVM_DRIVER
243ea8190a2Ssmaybe 	ec_unbind_evtchn(xen_info->store_evtchn);
244ea8190a2Ssmaybe #else
245*db830ba5SToomas Soome 	rem_avintr(NULL, IPL_XENBUS, xenbus_intr, xenbus_irq);
246ea8190a2Ssmaybe #endif
247843e1988Sjohnlev }
248843e1988Sjohnlev 
249843e1988Sjohnlev void
xb_setup_intr(void)250843e1988Sjohnlev xb_setup_intr(void)
251843e1988Sjohnlev {
252551bc2a6Smrj #ifdef XPV_HVM_DRIVER
253551bc2a6Smrj 	ec_bind_evtchn_to_handler(xen_info->store_evtchn, IPL_XENBUS,
254551bc2a6Smrj 	    xenbus_intr, NULL);
255551bc2a6Smrj #else
256843e1988Sjohnlev 	xenbus_irq = ec_bind_evtchn_to_irq(xen_info->store_evtchn);
257551bc2a6Smrj 	if (xenbus_irq < 0) {
258551bc2a6Smrj 		cmn_err(CE_WARN, "Couldn't bind xenbus event channel");
259551bc2a6Smrj 		return;
260551bc2a6Smrj 	}
261*db830ba5SToomas Soome 	if (!add_avintr(NULL, IPL_XENBUS, xenbus_intr, "xenbus",
262843e1988Sjohnlev 	    xenbus_irq, NULL, NULL, NULL, NULL))
263843e1988Sjohnlev 		cmn_err(CE_WARN, "XENBUS add intr failed\n");
264551bc2a6Smrj #endif
265843e1988Sjohnlev }
266843e1988Sjohnlev 
267843e1988Sjohnlev /*
268843e1988Sjohnlev  * Set up our xenstore page and event channel. Domain 0 needs to allocate a
269843e1988Sjohnlev  * page and event channel; other domains use what we are told.
270843e1988Sjohnlev  */
271843e1988Sjohnlev void
xb_init(void)272843e1988Sjohnlev xb_init(void)
273843e1988Sjohnlev {
274843e1988Sjohnlev 	int err;
275843e1988Sjohnlev 
276843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
277843e1988Sjohnlev 
278843e1988Sjohnlev 		if (xb_addr != NULL)
279843e1988Sjohnlev 			return;
280843e1988Sjohnlev 
281843e1988Sjohnlev 		xb_addr = ddi_umem_alloc(PAGESIZE, DDI_UMEM_SLEEP,
282843e1988Sjohnlev 		    &xb_cookie);
283843e1988Sjohnlev 		xen_info->store_mfn = pfn_to_mfn(hat_getpfnum(kas.a_hat,
284843e1988Sjohnlev 		    xb_addr));
285843e1988Sjohnlev 
286843e1988Sjohnlev 		err = xen_alloc_unbound_evtchn(0,
287843e1988Sjohnlev 		    (int *)&xen_info->store_evtchn);
288843e1988Sjohnlev 		ASSERT(err == 0);
289843e1988Sjohnlev 	} else {
290843e1988Sjohnlev 		/*
291843e1988Sjohnlev 		 * This is harmless on first boot, but needed for resume and
292843e1988Sjohnlev 		 * migrate. We use kbm_map_ma() as a shortcut instead of
293843e1988Sjohnlev 		 * directly using HYPERVISOR_update_va_mapping().
294843e1988Sjohnlev 		 */
295843e1988Sjohnlev 		ASSERT(xb_addr != NULL);
296843e1988Sjohnlev 		kbm_map_ma(mfn_to_ma(xen_info->store_mfn),
297843e1988Sjohnlev 		    (uintptr_t)xb_addr, 0);
298843e1988Sjohnlev 	}
299843e1988Sjohnlev 
300843e1988Sjohnlev 	ASSERT(xen_info->store_evtchn);
301843e1988Sjohnlev }
302843e1988Sjohnlev 
303843e1988Sjohnlev void *
xb_xenstore_cookie(void)304843e1988Sjohnlev xb_xenstore_cookie(void)
305843e1988Sjohnlev {
306843e1988Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
307843e1988Sjohnlev 	return (xb_cookie);
308843e1988Sjohnlev }
309