summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/opl/io/pcicmu/pcmu_cb.c
blob: c87237f61a1d91a69a5ffe2f526d8e1b1a02d100 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * CMU-CH Control Block object
 */
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/async.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/pcicmu/pcicmu.h>
#include <sys/machsystm.h>

extern uint64_t	xc_tick_jump_limit;

void
pcmu_cb_create(pcmu_t *pcmu_p)
{
	pcmu_cb_t *pcb_p = (pcmu_cb_t *)
	    kmem_zalloc(sizeof (pcmu_cb_t), KM_SLEEP);
	mutex_init(&pcb_p->pcb_intr_lock, NULL, MUTEX_DRIVER, NULL);
	pcmu_p->pcmu_cb_p = pcb_p;
	pcb_p->pcb_pcmu_p = pcmu_p;
	pcmu_cb_setup(pcmu_p);
}

void
pcmu_cb_destroy(pcmu_t *pcmu_p)
{
	pcmu_cb_t *pcb_p = pcmu_p->pcmu_cb_p;

	intr_dist_rem(pcmu_cb_intr_dist, pcb_p);
	pcmu_cb_teardown(pcmu_p);
	pcmu_p->pcmu_cb_p = NULL;
	mutex_destroy(&pcb_p->pcb_intr_lock);
	kmem_free(pcb_p, sizeof (pcmu_cb_t));
}

uint64_t
pcmu_cb_ino_to_map_pa(pcmu_cb_t *pcb_p, pcmu_ib_ino_t ino)
{
	return (pcb_p->pcb_map_pa + ((ino & 0x1f) << 3));
}

uint64_t
pcmu_cb_ino_to_clr_pa(pcmu_cb_t *pcb_p, pcmu_ib_ino_t ino)
{
	return (pcb_p->pcb_clr_pa + ((ino & 0x1f) << 3));
}

static void
pcmu_cb_set_nintr_reg(pcmu_cb_t *pcb_p, pcmu_ib_ino_t ino, uint64_t value)
{
	uint64_t pa = pcmu_cb_ino_to_clr_pa(pcb_p, ino);

	PCMU_DBG3(PCMU_DBG_CB|PCMU_DBG_CONT, NULL,
		"pci-%x pcmu_cb_set_nintr_reg: ino=%x PA=%016llx\n",
		pcb_p->pcb_pcmu_p->pcmu_id, ino, pa);

	stdphysio(pa, value);
	(void) lddphysio(pa);	/* flush the previous write */
}

/*
 * enable an internal interrupt source:
 * if an interrupt is shared by both sides, record it in pcb_inos[] and
 * cb will own its distribution.
 */
void
pcmu_cb_enable_nintr(pcmu_t *pcmu_p, pcmu_cb_nintr_index_t idx)
{
	pcmu_cb_t *pcb_p = pcmu_p->pcmu_cb_p;
	pcmu_ib_ino_t ino = PCMU_IB_MONDO_TO_INO(pcmu_p->pcmu_inos[idx]);
	pcmu_ib_mondo_t mondo = PCMU_CB_INO_TO_MONDO(pcb_p, ino);
	uint32_t cpu_id;
	uint64_t reg, pa;
	pcmu_ib_t *pib_p = pcb_p->pcb_pcmu_p->pcmu_ib_p;
	volatile uint64_t *imr_p = ib_intr_map_reg_addr(pib_p, ino);

	ASSERT(idx < CBNINTR_MAX);
	pa = pcmu_cb_ino_to_map_pa(pcb_p, ino);

	mutex_enter(&pcb_p->pcb_intr_lock);
	cpu_id = intr_dist_cpuid();

	cpu_id = u2u_translate_tgtid(pib_p->pib_pcmu_p, cpu_id, imr_p);

	reg = ib_get_map_reg(mondo, cpu_id);
	stdphysio(pa, reg);

	ASSERT(pcb_p->pcb_inos[idx] == 0);
	pcb_p->pcb_inos[idx] = ino;

	pcmu_cb_set_nintr_reg(pcb_p, ino, PCMU_CLEAR_INTR_REG_IDLE);
	mutex_exit(&pcb_p->pcb_intr_lock);

	PCMU_DBG3(PCMU_DBG_CB|PCMU_DBG_CONT, NULL,
	    "pci-%x pcmu_cb_enable_nintr: ino=%x cpu_id=%x\n",
	    pcmu_p->pcmu_id, ino, cpu_id);
	PCMU_DBG2(PCMU_DBG_CB|PCMU_DBG_CONT, NULL,
	    "\tPA=%016llx data=%016llx\n", pa, reg);
}

static void
pcmu_cb_disable_nintr_reg(pcmu_cb_t *pcb_p, pcmu_ib_ino_t ino, int wait)
{
	uint64_t tmp, map_reg_pa = pcmu_cb_ino_to_map_pa(pcb_p, ino);
	ASSERT(MUTEX_HELD(&pcb_p->pcb_intr_lock));

	/* mark interrupt invalid in mapping register */
	tmp = lddphysio(map_reg_pa) & ~PCMU_INTR_MAP_REG_VALID;
	stdphysio(map_reg_pa, tmp);
	(void) lddphysio(map_reg_pa);   /* flush previous write */

	if (wait) {
		hrtime_t start_time;
		hrtime_t prev, curr, interval, jump;
		hrtime_t intr_timeout;
		uint64_t state_reg_pa = pcb_p->pcb_obsta_pa;
		uint_t shift = (ino & 0x1f) << 1;

		/* busy wait if there is interrupt being processed */
		/* unless panic or timeout for interrupt pending is reached */

		intr_timeout = pcmu_intrpend_timeout;
		jump = TICK_TO_NSEC(xc_tick_jump_limit);
		start_time = curr = gethrtime();
		while ((((lddphysio(state_reg_pa) >> shift) &
			PCMU_CLEAR_INTR_REG_MASK) ==
			PCMU_CLEAR_INTR_REG_PENDING) && !panicstr) {
			/*
			 * If we have a really large jump in hrtime, it is most
			 * probably because we entered the debugger (or OBP,
			 * in general). So, we adjust the timeout accordingly
			 * to prevent declaring an interrupt timeout. The
			 * master-interrupt mechanism in OBP should deliver
			 * the interrupts properly.
			 */
			prev = curr;
			curr = gethrtime();
			interval = curr - prev;
			if (interval > jump)
				intr_timeout += interval;
			if (curr - start_time > intr_timeout) {
				cmn_err(CE_WARN, "pcmu@%x "
				    "pcmu_cb_disable_nintr_reg(%lx,%x) timeout",
				    pcb_p->pcb_pcmu_p->pcmu_id, map_reg_pa,
				    PCMU_CB_INO_TO_MONDO(pcb_p, ino));
				break;
			}
		}
	}
}

void
pcmu_cb_disable_nintr(pcmu_cb_t *pcb_p, pcmu_cb_nintr_index_t idx, int wait)
{
	pcmu_ib_t *pib_p = pcb_p->pcb_pcmu_p->pcmu_ib_p;
	volatile uint64_t *imr_p;
	pcmu_ib_ino_t ino = pcb_p->pcb_inos[idx];
	ASSERT(idx < CBNINTR_MAX);
	ASSERT(ino);

	imr_p = ib_intr_map_reg_addr(pib_p, ino);
	mutex_enter(&pcb_p->pcb_intr_lock);
	pcmu_cb_disable_nintr_reg(pcb_p, ino, wait);
	pcmu_cb_set_nintr_reg(pcb_p, ino, PCMU_CLEAR_INTR_REG_PENDING);
	pcb_p->pcb_inos[idx] = 0;
	mutex_exit(&pcb_p->pcb_intr_lock);
	u2u_ittrans_cleanup((u2u_ittrans_data_t *)(pcb_p->pcb_ittrans_cookie),
			imr_p);
}

void
pcmu_cb_clear_nintr(pcmu_cb_t *pcb_p, pcmu_cb_nintr_index_t idx)
{
	pcmu_ib_ino_t ino = pcb_p->pcb_inos[idx];
	ASSERT(idx < CBNINTR_MAX);
	ASSERT(ino);
	pcmu_cb_set_nintr_reg(pcb_p, ino, PCMU_CLEAR_INTR_REG_IDLE);
}

void
pcmu_cb_intr_dist(void *arg)
{
	int i;
	pcmu_cb_t *pcb_p = (pcmu_cb_t *)arg;

	mutex_enter(&pcb_p->pcb_intr_lock);
	for (i = 0; i < pcb_p->pcb_no_of_inos; i++) {
		uint64_t mr_pa;
		volatile uint64_t imr;
		pcmu_ib_mondo_t mondo;
		uint32_t cpu_id;
		pcmu_ib_t *pib_p = pcb_p->pcb_pcmu_p->pcmu_ib_p;
		volatile uint64_t *imr_p;

		pcmu_ib_ino_t ino = pcb_p->pcb_inos[i];
		if (!ino)	/* skip non-shared interrupts */
			continue;

		mr_pa = pcmu_cb_ino_to_map_pa(pcb_p, ino);
		imr = lddphysio(mr_pa);
		if (!PCMU_IB_INO_INTR_ISON(imr))
			continue;

		mondo = PCMU_CB_INO_TO_MONDO(pcb_p, ino);
		cpu_id = intr_dist_cpuid();
		imr_p = ib_intr_map_reg_addr(pib_p, ino);

		cpu_id = u2u_translate_tgtid(pib_p->pib_pcmu_p, cpu_id, imr_p);

		pcmu_cb_disable_nintr_reg(pcb_p, ino, PCMU_IB_INTR_WAIT);
		stdphysio(mr_pa, ib_get_map_reg(mondo, cpu_id));
		(void) lddphysio(mr_pa);	/* flush previous write */
	}
	mutex_exit(&pcb_p->pcb_intr_lock);
}

void
pcmu_cb_suspend(pcmu_cb_t *pcb_p)
{
	int i, inos = pcb_p->pcb_no_of_inos;
	ASSERT(!pcb_p->pcb_imr_save);
	pcb_p->pcb_imr_save = kmem_alloc(inos * sizeof (uint64_t), KM_SLEEP);

	/*
	 * save the internal interrupts' mapping registers content
	 *
	 * The PBM IMR really doesn't need to be saved, as it is
	 * different per side and is handled by pcmu_pbm_suspend/resume.
	 * But it complicates the logic.
	 */
	for (i = 0; i < inos; i++) {
		uint64_t pa;
		pcmu_ib_ino_t ino = pcb_p->pcb_inos[i];
		if (!ino)
			continue;
		pa = pcmu_cb_ino_to_map_pa(pcb_p, ino);
		pcb_p->pcb_imr_save[i] = lddphysio(pa);
	}
}

void
pcmu_cb_resume(pcmu_cb_t *pcb_p)
{
	int i;
	for (i = 0; i < pcb_p->pcb_no_of_inos; i++) {
		uint64_t pa;
		pcmu_ib_ino_t ino = pcb_p->pcb_inos[i];
		if (!ino)
			continue;
		pa = pcmu_cb_ino_to_map_pa(pcb_p, ino);
		pcmu_cb_set_nintr_reg(pcb_p, ino, PCMU_CLEAR_INTR_REG_IDLE);
		stdphysio(pa, pcb_p->pcb_imr_save[i]);	/* restore IMR */
	}
	kmem_free(pcb_p->pcb_imr_save,
	    pcb_p->pcb_no_of_inos * sizeof (uint64_t));
	pcb_p->pcb_imr_save = NULL;
}