2 * Copyright (c) 2009 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/mutex.h>
37 #include <machine/frame.h>
38 #include <machine/md_var.h>
39 #include <machine/smp.h>
41 static struct mtx ia64_highfp_mtx;
44 ia64_highfp_init(void *_)
46 mtx_init(&ia64_highfp_mtx, "High FP lock", NULL, MTX_SPIN);
48 SYSINIT(ia64_highfp_init, SI_SUB_LOCK, SI_ORDER_ANY, ia64_highfp_init, NULL);
52 ia64_highfp_ipi(struct pcpu *cpu)
56 ipi_send(cpu, ia64_ipi_highfp);
57 error = msleep_spin(&cpu->pc_fpcurthread, &ia64_highfp_mtx,
64 ia64_highfp_drop(struct thread *td)
71 mtx_lock_spin(&ia64_highfp_mtx);
74 KASSERT(cpu->pc_fpcurthread == td,
75 ("cpu->pc_fpcurthread != td"));
76 td->td_frame->tf_special.psr |= IA64_PSR_DFH;
77 pcb->pcb_fpcpu = NULL;
78 cpu->pc_fpcurthread = NULL;
80 mtx_unlock_spin(&ia64_highfp_mtx);
82 return ((cpu != NULL) ? 1 : 0);
86 ia64_highfp_enable(struct thread *td, struct trapframe *tf)
94 mtx_lock_spin(&ia64_highfp_mtx);
97 if (cpu != NULL && cpu != pcpup) {
98 KASSERT(cpu->pc_fpcurthread == td,
99 ("cpu->pc_fpcurthread != td"));
100 ia64_highfp_ipi(cpu);
103 td1 = PCPU_GET(fpcurthread);
104 if (td1 != NULL && td1 != td) {
105 KASSERT(td1->td_pcb->pcb_fpcpu == pcpup,
106 ("td1->td_pcb->pcb_fpcpu != pcpup"));
107 save_high_fp(&td1->td_pcb->pcb_high_fp);
108 td1->td_frame->tf_special.psr |= IA64_PSR_DFH;
109 td1->td_pcb->pcb_fpcpu = NULL;
110 PCPU_SET(fpcurthread, NULL);
114 KASSERT(pcb->pcb_fpcpu == NULL, ("pcb->pcb_fpcpu != NULL"));
115 KASSERT(PCPU_GET(fpcurthread) == NULL,
116 ("PCPU_GET(fpcurthread) != NULL"));
117 restore_high_fp(&pcb->pcb_high_fp);
118 PCPU_SET(fpcurthread, td);
119 pcb->pcb_fpcpu = pcpup;
120 tf->tf_special.psr &= ~IA64_PSR_MFH;
122 tf->tf_special.psr &= ~IA64_PSR_DFH;
123 mtx_unlock_spin(&ia64_highfp_mtx);
125 return ((td1 != NULL) ? 1 : 0);
129 ia64_highfp_save(struct thread *td)
136 mtx_lock_spin(&ia64_highfp_mtx);
137 cpu = pcb->pcb_fpcpu;
139 if (cpu != NULL && cpu != pcpup) {
140 KASSERT(cpu->pc_fpcurthread == td,
141 ("cpu->pc_fpcurthread != td"));
142 ia64_highfp_ipi(cpu);
146 KASSERT(cpu->pc_fpcurthread == td,
147 ("cpu->pc_fpcurthread != td"));
148 save_high_fp(&pcb->pcb_high_fp);
149 td->td_frame->tf_special.psr |= IA64_PSR_DFH;
150 pcb->pcb_fpcpu = NULL;
151 cpu->pc_fpcurthread = NULL;
153 mtx_unlock_spin(&ia64_highfp_mtx);
155 return ((cpu != NULL) ? 1 : 0);
160 ia64_highfp_save_ipi(void)
164 mtx_lock_spin(&ia64_highfp_mtx);
165 td = PCPU_GET(fpcurthread);
167 KASSERT(td->td_pcb->pcb_fpcpu == pcpup,
168 ("td->td_pcb->pcb_fpcpu != pcpup"));
169 save_high_fp(&td->td_pcb->pcb_high_fp);
170 td->td_frame->tf_special.psr |= IA64_PSR_DFH;
171 td->td_pcb->pcb_fpcpu = NULL;
172 PCPU_SET(fpcurthread, NULL);
174 mtx_unlock_spin(&ia64_highfp_mtx);
175 wakeup(&PCPU_GET(fpcurthread));
177 return ((td != NULL) ? 1 : 0);