27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5b885580Alexander Kolbasov * Common Development and Distribution License (the "License").
6b885580Alexander Kolbasov * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
22b885580Alexander Kolbasov * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate * Facilities for cross-processor subroutine calls using "mailbox" interrupts.
287c478bdstevel@tonic-gate */
307c478bdstevel@tonic-gate#include <sys/types.h>
317c478bdstevel@tonic-gate#include <sys/thread.h>
327c478bdstevel@tonic-gate#include <sys/cpuvar.h>
337c478bdstevel@tonic-gate#include <sys/x_call.h>
347c478bdstevel@tonic-gate#include <sys/systm.h>
357c478bdstevel@tonic-gate#include <sys/machsystm.h>
367c478bdstevel@tonic-gate#include <sys/intr.h>
37b885580Alexander Kolbasov#include <sys/xc_impl.h>
407c478bdstevel@tonic-gate * Interrupt another CPU.
417c478bdstevel@tonic-gate * 	This is useful to make the other CPU go through a trap so that
427c478bdstevel@tonic-gate *	it recognizes an address space trap (AST) for preempting a thread.
437c478bdstevel@tonic-gate *
447c478bdstevel@tonic-gate *	It is possible to be preempted here and be resumed on the CPU
457c478bdstevel@tonic-gate *	being poked, so it isn't an error to poke the current CPU.
467c478bdstevel@tonic-gate *	We could check this and still get preempted after the check, so
477c478bdstevel@tonic-gate *	we don't bother.
487c478bdstevel@tonic-gate */
507c478bdstevel@tonic-gatepoke_cpu(int cpun)
527c478bdstevel@tonic-gate	uint32_t *ptr = (uint32_t *)&cpu[cpun]->cpu_m.poke_cpu_outstanding;
547c478bdstevel@tonic-gate	/*
557c478bdstevel@tonic-gate	 * If panicstr is set or a poke_cpu is already pending,
567c478bdstevel@tonic-gate	 * no need to send another one. Use atomic swap to protect
577c478bdstevel@tonic-gate	 * against multiple CPUs sending redundant pokes.
587c478bdstevel@tonic-gate	 */
597c478bdstevel@tonic-gate	if (panicstr || *ptr == B_TRUE ||
607c478bdstevel@tonic-gate	    atomic_swap_32(ptr, B_TRUE) == B_TRUE)
617c478bdstevel@tonic-gate		return;
637c478bdstevel@tonic-gate	xt_one(cpun, setsoftint_tl1, poke_cpu_inum, 0);
65b885580Alexander Kolbasov
66b885580Alexander Kolbasovextern int xc_spl_enter[];
67b885580Alexander Kolbasov
68b885580Alexander Kolbasov/*
69b885580Alexander Kolbasov * Call a function on a target CPU
70b885580Alexander Kolbasov */
71b885580Alexander Kolbasovvoid
72b885580Alexander Kolbasovcpu_call(cpu_t *cp, cpu_call_func_t func, uintptr_t arg1, uintptr_t arg2)
73b885580Alexander Kolbasov{
74b885580Alexander Kolbasov	if (panicstr)
75b885580Alexander Kolbasov		return;
76b885580Alexander Kolbasov
77b885580Alexander Kolbasov	/*
78b885580Alexander Kolbasov	 * Prevent CPU from going offline
79b885580Alexander Kolbasov	 */
80b885580Alexander Kolbasov	kpreempt_disable();
81b885580Alexander Kolbasov
82b885580Alexander Kolbasov	/*
83b885580Alexander Kolbasov	 * If we are on the target CPU, call the function directly, but raise
84b885580Alexander Kolbasov	 * the PIL to XC_PIL.
85b885580Alexander Kolbasov	 * This guarantees that functions called via cpu_call() can not ever
86b885580Alexander Kolbasov	 * interrupt each other.
87b885580Alexander Kolbasov	 */
88b885580Alexander Kolbasov	if (CPU != cp) {
89b885580Alexander Kolbasov		xc_one(cp->cpu_id, (xcfunc_t *)func, (uint64_t)arg1,
90b885580Alexander Kolbasov		    (uint64_t)arg2);
91b885580Alexander Kolbasov	} else {
92b885580Alexander Kolbasov		int lcx;
93b885580Alexander Kolbasov		int opl;
94b885580Alexander Kolbasov
95b885580Alexander Kolbasov		XC_SPL_ENTER(lcx, opl);
96b885580Alexander Kolbasov		func(arg1, arg2);
97b885580Alexander Kolbasov		XC_SPL_EXIT(lcx, opl);
98b885580Alexander Kolbasov	}
99b885580Alexander Kolbasov
100b885580Alexander Kolbasov	kpreempt_enable();
101b885580Alexander Kolbasov}