12b6cd37eSSepherosa Ziehau /*
22b6cd37eSSepherosa Ziehau * Copyright (c) 1996, by Steve Passe
32b6cd37eSSepherosa Ziehau * Copyright (c) 2008 The DragonFly Project.
42b6cd37eSSepherosa Ziehau * All rights reserved.
52b6cd37eSSepherosa Ziehau *
62b6cd37eSSepherosa Ziehau * Redistribution and use in source and binary forms, with or without
72b6cd37eSSepherosa Ziehau * modification, are permitted provided that the following conditions
82b6cd37eSSepherosa Ziehau * are met:
92b6cd37eSSepherosa Ziehau * 1. Redistributions of source code must retain the above copyright
102b6cd37eSSepherosa Ziehau * notice, this list of conditions and the following disclaimer.
112b6cd37eSSepherosa Ziehau * 2. The name of the developer may NOT be used to endorse or promote products
122b6cd37eSSepherosa Ziehau * derived from this software without specific prior written permission.
132b6cd37eSSepherosa Ziehau *
142b6cd37eSSepherosa Ziehau * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152b6cd37eSSepherosa Ziehau * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162b6cd37eSSepherosa Ziehau * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172b6cd37eSSepherosa Ziehau * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182b6cd37eSSepherosa Ziehau * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192b6cd37eSSepherosa Ziehau * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202b6cd37eSSepherosa Ziehau * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212b6cd37eSSepherosa Ziehau * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222b6cd37eSSepherosa Ziehau * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232b6cd37eSSepherosa Ziehau * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242b6cd37eSSepherosa Ziehau * SUCH DAMAGE.
252b6cd37eSSepherosa Ziehau *
262b6cd37eSSepherosa Ziehau * $FreeBSD: src/sys/i386/include/mpapic.h,v 1.14.2.2 2000/09/30 02:49:34 ps Exp $
272b6cd37eSSepherosa Ziehau */
282b6cd37eSSepherosa Ziehau
292b6cd37eSSepherosa Ziehau #ifndef _ARCH_APIC_LAPIC_H_
302b6cd37eSSepherosa Ziehau #define _ARCH_APIC_LAPIC_H_
312b6cd37eSSepherosa Ziehau
3283c8d73eSSepherosa Ziehau #include <machine_base/apic/apicreg.h>
332b6cd37eSSepherosa Ziehau
342b6cd37eSSepherosa Ziehau /*
35fbac0dc4SSepherosa Ziehau * APIC ID <-> CPU ID mapping macros
362b6cd37eSSepherosa Ziehau */
37fbac0dc4SSepherosa Ziehau #define CPUID_TO_APICID(cpu_id) (cpu_id_to_apic_id[(cpu_id)])
38fbac0dc4SSepherosa Ziehau #define APICID_TO_CPUID(apic_id) (apic_id_to_cpu_id[(apic_id)])
392b6cd37eSSepherosa Ziehau
4083c8d73eSSepherosa Ziehau #ifndef _SYS_QUEUE_H_
4183c8d73eSSepherosa Ziehau #include <sys/queue.h>
4283c8d73eSSepherosa Ziehau #endif
4383c8d73eSSepherosa Ziehau
4483c8d73eSSepherosa Ziehau struct lapic_enumerator {
4583c8d73eSSepherosa Ziehau int lapic_prio;
4683c8d73eSSepherosa Ziehau TAILQ_ENTRY(lapic_enumerator) lapic_link;
4783c8d73eSSepherosa Ziehau int (*lapic_probe)(struct lapic_enumerator *);
48f8ae0475SSepherosa Ziehau int (*lapic_enumerate)(struct lapic_enumerator *);
4983c8d73eSSepherosa Ziehau };
5083c8d73eSSepherosa Ziehau
5183c8d73eSSepherosa Ziehau #define LAPIC_ENUM_PRIO_MPTABLE 20
5283c8d73eSSepherosa Ziehau #define LAPIC_ENUM_PRIO_MADT 40
5383c8d73eSSepherosa Ziehau
548afc0c3dSSepherosa Ziehau extern volatile lapic_t *lapic_mem;
55fbac0dc4SSepherosa Ziehau extern int cpu_id_to_apic_id[];
56fbac0dc4SSepherosa Ziehau extern int apic_id_to_cpu_id[];
572e0ed166SSepherosa Ziehau extern int lapic_enable;
588afc0c3dSSepherosa Ziehau extern int lapic_usable;
59f89b4a45SSepherosa Ziehau extern int x2apic_enable;
603c38fc60SSepherosa Ziehau extern void (*lapic_eoi)(void);
618afc0c3dSSepherosa Ziehau extern int (*apic_ipi)(int, int, int);
628afc0c3dSSepherosa Ziehau extern void (*single_apic_ipi)(int, int, int);
6383c8d73eSSepherosa Ziehau
6483c8d73eSSepherosa Ziehau void apic_dump(char*);
6583c8d73eSSepherosa Ziehau void lapic_init(boolean_t);
660e44391aSSepherosa Ziehau void lapic_set_cpuid(int, int);
673566408bSSepherosa Ziehau int lapic_config(void);
6883c8d73eSSepherosa Ziehau void lapic_enumerator_register(struct lapic_enumerator *);
6983c8d73eSSepherosa Ziehau void set_apic_timer(int);
7083c8d73eSSepherosa Ziehau int read_apic_timer(void);
7183c8d73eSSepherosa Ziehau void u_sleep(int);
7283c8d73eSSepherosa Ziehau
7313481c87SSepherosa Ziehau void lapic_map(vm_paddr_t);
749a4bd8f3SSepherosa Ziehau int lapic_unused_apic_id(int);
759a4bd8f3SSepherosa Ziehau void lapic_fixup_noioapic(void);
768afc0c3dSSepherosa Ziehau void lapic_seticr_sync(uint32_t, uint32_t);
77f89b4a45SSepherosa Ziehau void lapic_x2apic_enter(boolean_t);
789a4bd8f3SSepherosa Ziehau
7949b0f72aSSepherosa Ziehau #ifndef _MACHINE_SMP_H_
8049b0f72aSSepherosa Ziehau #include <machine/smp.h>
8149b0f72aSSepherosa Ziehau #endif
8249b0f72aSSepherosa Ziehau
83*da82a65aSzrj #ifndef _SYS_CPUMASK_H_
84*da82a65aSzrj #include <sys/cpumask.h>
85*da82a65aSzrj #endif
86*da82a65aSzrj
879a4bd8f3SSepherosa Ziehau void selected_apic_ipi(cpumask_t, int, int);
889a4bd8f3SSepherosa Ziehau
892b6cd37eSSepherosa Ziehau /*
9083c8d73eSSepherosa Ziehau * Send an IPI INTerrupt containing 'vector' to all CPUs EXCEPT myself
912b6cd37eSSepherosa Ziehau */
922b6cd37eSSepherosa Ziehau static __inline int
all_but_self_ipi(int vector)932b6cd37eSSepherosa Ziehau all_but_self_ipi(int vector)
942b6cd37eSSepherosa Ziehau {
95c07315c4SMatthew Dillon if (CPUMASK_ISUP(smp_active_mask))
962b6cd37eSSepherosa Ziehau return 0;
972b6cd37eSSepherosa Ziehau return apic_ipi(APIC_DEST_ALLESELF, vector, APIC_DELMODE_FIXED);
982b6cd37eSSepherosa Ziehau }
992b6cd37eSSepherosa Ziehau
1008afc0c3dSSepherosa Ziehau #define LAPIC_MEM_READ(field) (lapic_mem->field)
1018afc0c3dSSepherosa Ziehau #define LAPIC_MEM_WRITE(field, val) \
1028afc0c3dSSepherosa Ziehau do { \
1038afc0c3dSSepherosa Ziehau lapic_mem->field = (val); \
1048afc0c3dSSepherosa Ziehau } while (0)
1058afc0c3dSSepherosa Ziehau
106f89b4a45SSepherosa Ziehau #define LAPIC_MSR_READ(msr) rdmsr((msr))
107f89b4a45SSepherosa Ziehau #define LAPIC_MSR_WRITE(msr, val) wrmsr((msr), (val))
1088afc0c3dSSepherosa Ziehau
109f89b4a45SSepherosa Ziehau #define LAPIC_READ(field) \
110f89b4a45SSepherosa Ziehau (x2apic_enable ? (uint32_t)LAPIC_MSR_READ(LAPIC2MSR(field)) \
111f89b4a45SSepherosa Ziehau : LAPIC_MEM_READ(field))
112f89b4a45SSepherosa Ziehau
113f89b4a45SSepherosa Ziehau #define LAPIC_WRITE(field, val) \
114f89b4a45SSepherosa Ziehau do { \
115f89b4a45SSepherosa Ziehau if (x2apic_enable) \
116f89b4a45SSepherosa Ziehau LAPIC_MSR_WRITE(LAPIC2MSR(field), (val)); \
117f89b4a45SSepherosa Ziehau else \
118f89b4a45SSepherosa Ziehau LAPIC_MEM_WRITE(field, (val)); \
119f89b4a45SSepherosa Ziehau } while (0)
120f89b4a45SSepherosa Ziehau
121f89b4a45SSepherosa Ziehau #define LAPIC_READID \
122f89b4a45SSepherosa Ziehau (x2apic_enable ? (uint32_t)LAPIC_MSR_READ(MSR_X2APIC_ID) \
123f89b4a45SSepherosa Ziehau : APIC_ID(LAPIC_MEM_READ(id)))
1248afc0c3dSSepherosa Ziehau
1252b6cd37eSSepherosa Ziehau #endif /* _ARCH_APIC_LAPIC_H_ */
126