summaryrefslogtreecommitdiffstats
path: root/sys/ia64/ia64/sapic.c
blob: 208fd5814fb5839fbdcdb6d794be0e6e0ed8689c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*-
 * Copyright (c) 2001 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include "opt_ddb.h"

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <machine/sapicvar.h>
#include <machine/sapicreg.h>
#include <sys/bus.h>
#include <machine/intr.h>

static MALLOC_DEFINE(M_SAPIC, "sapic", "I/O SAPIC devices");

struct sapic_rte {
	u_int64_t	rte_vector		:8;
	u_int64_t	rte_delivery_mode	:3;
	u_int64_t	rte_destination_mode	:1;
	u_int64_t	rte_delivery_status	:1;
	u_int64_t	rte_polarity		:1;
	u_int64_t	rte_rirr		:1;
	u_int64_t	rte_trigger_mode	:1;
	u_int64_t	rte_mask		:1;
	u_int64_t	rte_flushen		:1;
	u_int64_t	rte_reserved		:30;
	u_int64_t	rte_destination_eid	:8;
	u_int64_t	rte_destination_id	:8;
};

static u_int32_t
sapic_read(struct sapic *sa, int which)
{
	vm_offset_t reg = sa->sa_registers;

	*(volatile u_int32_t *) (reg + SAPIC_IO_SELECT) = which;
	ia64_mf();
	ia64_mf_a();
	return *(volatile u_int32_t *) (reg + SAPIC_IO_WINDOW);
}

static void
sapic_write(struct sapic *sa, int which, u_int32_t value)
{
	vm_offset_t reg = sa->sa_registers;

	*(volatile u_int32_t *) (reg + SAPIC_IO_SELECT) = which;
	ia64_mf();
	ia64_mf_a();
	*(volatile u_int32_t *) (reg + SAPIC_IO_WINDOW) = value;
	ia64_mf();
	ia64_mf_a();
}

#ifdef DDB

static void
sapic_read_rte(struct sapic *sa, int which,
	       struct sapic_rte *rte)
{
	u_int32_t *p = (u_int32_t *) rte;
	register_t c;

	c = intr_disable();
	p[0] = sapic_read(sa, SAPIC_RTE_BASE + 2*which);
	p[1] = sapic_read(sa, SAPIC_RTE_BASE + 2*which + 1);
	intr_restore(c);
}

#endif

static void
sapic_write_rte(struct sapic *sa, int which,
		struct sapic_rte *rte)
{
	u_int32_t *p = (u_int32_t *) rte;
	register_t c;

	c = intr_disable();
	sapic_write(sa, SAPIC_RTE_BASE + 2*which, p[0]);
	sapic_write(sa, SAPIC_RTE_BASE + 2*which + 1, p[1]);
	intr_restore(c);
}

struct sapic *
sapic_create(int id, int base, u_int64_t address)
{
	struct sapic *sa;
	int max;

	sa = malloc(sizeof(struct sapic), M_SAPIC, M_NOWAIT);
	if (!sa)
		return 0;

	sa->sa_id = id;
	sa->sa_base = base;
	sa->sa_registers = IA64_PHYS_TO_RR6(address);

	max = (sapic_read(sa, SAPIC_VERSION) >> 16) & 0xff;
	sa->sa_limit = base + max;

	ia64_add_sapic(sa);

	return sa;
}

void
sapic_enable(struct sapic *sa, int input, int vector,
	     int trigger_mode, int polarity)
{
	struct sapic_rte rte;
	u_int64_t lid = ia64_get_lid();

	bzero(&rte, sizeof(rte));
	rte.rte_destination_id = (lid >> 24) & 15;
	rte.rte_destination_eid = (lid >> 16) & 15;
	rte.rte_trigger_mode = trigger_mode;
	rte.rte_polarity = polarity;
	rte.rte_delivery_mode = 0; /* fixed */
	rte.rte_vector = vector;
	sapic_write_rte(sa, input, &rte);
}

void
sapic_eoi(struct sapic *sa, int vector)
{
	vm_offset_t reg = sa->sa_registers;

	*(volatile u_int32_t *) (reg + SAPIC_APIC_EOI) = vector;
	ia64_mf();
}

#ifdef DDB

#include <ddb/ddb.h>

void
sapic_print(struct sapic *sa, int input)
{
	struct sapic_rte rte;

	sapic_read_rte(sa, input, &rte);
	if (rte.rte_mask == 0) {
		db_printf("%3d %d %d %s %s %s %s %s ID=%x EID=%x\n",
			  rte.rte_vector,
			  rte.rte_delivery_mode,
			  rte.rte_destination_mode,
			  rte.rte_delivery_status ? "DS" : "  ",
			  rte.rte_polarity ? "low-active " : "high-active",
			  rte.rte_rirr ? "RIRR" : "    ",
			  rte.rte_trigger_mode ? "level" : "edge ",
			  rte.rte_flushen ? "F" : " ",
			  rte.rte_destination_id,
			  rte.rte_destination_eid);
	}
}

#endif
OpenPOWER on IntegriCloud