xref: /titanic_41/usr/src/uts/sun4u/starfire/io/portctrl.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1990-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/starfire.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  * Interrupt target translation data for
38*7c478bd9Sstevel@tonic-gate  * Starfire's Port controller asics
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate struct pc_ittrans_data {
41*7c478bd9Sstevel@tonic-gate 	kmutex_t ittrans_lock;			 /* lock for ITTR array */
42*7c478bd9Sstevel@tonic-gate 	volatile uint64_t *ittrans_mondovec[32]; /* mondovecreg addr array */
43*7c478bd9Sstevel@tonic-gate 	uint64_t ittransreg_physaddr[32];	 /* ITTREG physaddr array */
44*7c478bd9Sstevel@tonic-gate };
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Setup and initialize the soft table that
48*7c478bd9Sstevel@tonic-gate  * represent the Starfire interrupt target translation
49*7c478bd9Sstevel@tonic-gate  * registers in the Port controller asics. There is one
50*7c478bd9Sstevel@tonic-gate  * for each sysio/pci instance.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate void
pc_ittrans_init(int upa_id,caddr_t * ittptr_cookie)53*7c478bd9Sstevel@tonic-gate pc_ittrans_init(int upa_id, caddr_t *ittptr_cookie)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	int i;
56*7c478bd9Sstevel@tonic-gate 	uint64_t physaddr;
57*7c478bd9Sstevel@tonic-gate 	struct pc_ittrans_data *tmpptr;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	ASSERT(ittptr_cookie != NULL);
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	/*
62*7c478bd9Sstevel@tonic-gate 	 * Allocate the data structure to support starfire's
63*7c478bd9Sstevel@tonic-gate 	 * interrupt target translations
64*7c478bd9Sstevel@tonic-gate 	 */
65*7c478bd9Sstevel@tonic-gate 	tmpptr = (struct pc_ittrans_data *)
66*7c478bd9Sstevel@tonic-gate 			kmem_zalloc(sizeof (struct pc_ittrans_data),
67*7c478bd9Sstevel@tonic-gate 				KM_SLEEP);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/* Initialize the ittrans lock */
70*7c478bd9Sstevel@tonic-gate 	mutex_init(&tmpptr->ittrans_lock, NULL, MUTEX_DEFAULT, NULL);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	 * Construct the base physical address of the
74*7c478bd9Sstevel@tonic-gate 	 * ITTR registers associated with this PC asics
75*7c478bd9Sstevel@tonic-gate 	 */
76*7c478bd9Sstevel@tonic-gate 	physaddr = STARFIRE_UPAID2UPS(upa_id);
77*7c478bd9Sstevel@tonic-gate 	physaddr |= (STARFIRE_PSI_BASE | STARFIRE_PSI_PCREG_OFF |
78*7c478bd9Sstevel@tonic-gate 			STARFIRE_PC_INT_MAP);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/*
81*7c478bd9Sstevel@tonic-gate 	 * Initialize the ITTR table
82*7c478bd9Sstevel@tonic-gate 	 * This soft table is used to represent the interrupt
83*7c478bd9Sstevel@tonic-gate 	 * target translation hardware registers in the Starfire's
84*7c478bd9Sstevel@tonic-gate 	 * PC asics. There are 32 slots and each slot consists of
85*7c478bd9Sstevel@tonic-gate 	 * a mondovec regaddr entry and the physical address of
86*7c478bd9Sstevel@tonic-gate 	 * the that ITT register. A empty slot is one whose
87*7c478bd9Sstevel@tonic-gate 	 * mondovec entry is null. To reserve/use a slot for a
88*7c478bd9Sstevel@tonic-gate 	 * particular intr mapping reg, we simply find
89*7c478bd9Sstevel@tonic-gate 	 * a empty slot and write in the mondovec mapping
90*7c478bd9Sstevel@tonic-gate 	 * regaddr into the mondovec field.
91*7c478bd9Sstevel@tonic-gate 	 */
92*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++) {
93*7c478bd9Sstevel@tonic-gate 		tmpptr->ittrans_mondovec[i] = NULL;
94*7c478bd9Sstevel@tonic-gate 		tmpptr->ittransreg_physaddr[i] = physaddr + i*16;
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	*ittptr_cookie = (caddr_t)tmpptr;
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate void
pc_ittrans_uninit(caddr_t ittr_cookie)101*7c478bd9Sstevel@tonic-gate pc_ittrans_uninit(caddr_t ittr_cookie)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	struct pc_ittrans_data *tmpptr;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	ASSERT(ittr_cookie != NULL);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	tmpptr = (struct pc_ittrans_data *)ittr_cookie;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&tmpptr->ittrans_lock);
110*7c478bd9Sstevel@tonic-gate 	kmem_free((int *)ittr_cookie, sizeof (struct pc_ittrans_data));
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /*
114*7c478bd9Sstevel@tonic-gate  * This routine searches for a slot in the soft ITTR table
115*7c478bd9Sstevel@tonic-gate  * that was reserved earlier by matching the mondovec
116*7c478bd9Sstevel@tonic-gate  * mapping regaddr argument with the corresponding field in
117*7c478bd9Sstevel@tonic-gate  * the table. Note that the soft ITTR table mirrors the
118*7c478bd9Sstevel@tonic-gate  * corresponding hw table in the starfire port controller(PC)
119*7c478bd9Sstevel@tonic-gate  * asics. A new slot will be obtained if the slot cannot
120*7c478bd9Sstevel@tonic-gate  * be found. (not reserved previously). The routine then programs
121*7c478bd9Sstevel@tonic-gate  * in the target cpu id into the PC ITTR hardware, updates the
122*7c478bd9Sstevel@tonic-gate  * soft table  and return the index to this slot as the target
123*7c478bd9Sstevel@tonic-gate  * id cookie.
124*7c478bd9Sstevel@tonic-gate  */
125*7c478bd9Sstevel@tonic-gate int
pc_translate_tgtid(caddr_t ittr_cookie,int cpu_id,volatile uint64_t * mondovec_addr)126*7c478bd9Sstevel@tonic-gate pc_translate_tgtid(caddr_t ittr_cookie, int cpu_id,
127*7c478bd9Sstevel@tonic-gate 		volatile uint64_t *mondovec_addr)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	struct pc_ittrans_data *ittptr;
130*7c478bd9Sstevel@tonic-gate 	int i;
131*7c478bd9Sstevel@tonic-gate 	int foundslot = -1;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	ASSERT(ittr_cookie != NULL);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	ittptr = (struct pc_ittrans_data *)ittr_cookie;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ittptr->ittrans_lock);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/*
140*7c478bd9Sstevel@tonic-gate 	 * Search the mondovec addrlist to see if we
141*7c478bd9Sstevel@tonic-gate 	 * already reserved/used a slot for this particular
142*7c478bd9Sstevel@tonic-gate 	 * mondovec mapping regaddr.
143*7c478bd9Sstevel@tonic-gate 	 */
144*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++) {
145*7c478bd9Sstevel@tonic-gate 		if (mondovec_addr == ittptr->ittrans_mondovec[i]) {
146*7c478bd9Sstevel@tonic-gate 			/*
147*7c478bd9Sstevel@tonic-gate 			 * found the slot that matches the
148*7c478bd9Sstevel@tonic-gate 			 * mondo vec in question
149*7c478bd9Sstevel@tonic-gate 			 */
150*7c478bd9Sstevel@tonic-gate 			foundslot = i;
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		if (foundslot == -1 && ittptr->ittrans_mondovec[i] == NULL)
154*7c478bd9Sstevel@tonic-gate 			/* keep track of a empty slot */
155*7c478bd9Sstevel@tonic-gate 			foundslot = i;
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if (foundslot != -1) {
159*7c478bd9Sstevel@tonic-gate 		/* We found a slot for this mondo vec, let's use it */
160*7c478bd9Sstevel@tonic-gate 		stphysio(ittptr->ittransreg_physaddr[foundslot],
161*7c478bd9Sstevel@tonic-gate 			STARFIRE_UPAID2HWMID(cpu_id));
162*7c478bd9Sstevel@tonic-gate 		ittptr->ittrans_mondovec[foundslot] = mondovec_addr;
163*7c478bd9Sstevel@tonic-gate 	} else {
164*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "No more ITTR slots!!");
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ittptr->ittrans_lock);
168*7c478bd9Sstevel@tonic-gate 	return (foundslot);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /*
172*7c478bd9Sstevel@tonic-gate  * This routine searches the interrupt target translation table
173*7c478bd9Sstevel@tonic-gate  * (if exists) for a slot that was reserved/used earlier by
174*7c478bd9Sstevel@tonic-gate  * matching the mondovec_addr input argument with the mondovec
175*7c478bd9Sstevel@tonic-gate  * field in the table. The routine then free the found slot by
176*7c478bd9Sstevel@tonic-gate  * resetting it to zero.
177*7c478bd9Sstevel@tonic-gate  */
178*7c478bd9Sstevel@tonic-gate void
pc_ittrans_cleanup(caddr_t ittr_cookie,volatile uint64_t * mondovec_addr)179*7c478bd9Sstevel@tonic-gate pc_ittrans_cleanup(caddr_t ittr_cookie,
180*7c478bd9Sstevel@tonic-gate 		volatile uint64_t *mondovec_addr)
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	struct pc_ittrans_data *ittptr;
184*7c478bd9Sstevel@tonic-gate 	int i;
185*7c478bd9Sstevel@tonic-gate 	int foundslot = -1;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	ASSERT(ittr_cookie != NULL);
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	ittptr = (struct pc_ittrans_data *)ittr_cookie;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ittptr->ittrans_lock);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	/*
194*7c478bd9Sstevel@tonic-gate 	 * Search the mondovec addrlist for the reserved/used
195*7c478bd9Sstevel@tonic-gate 	 * slot associated with this particular mondo vector.
196*7c478bd9Sstevel@tonic-gate 	 */
197*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++) {
198*7c478bd9Sstevel@tonic-gate 		if (mondovec_addr == ittptr->ittrans_mondovec[i]) {
199*7c478bd9Sstevel@tonic-gate 			/*
200*7c478bd9Sstevel@tonic-gate 			 * found the slot that matches the
201*7c478bd9Sstevel@tonic-gate 			 * mondo vec in question
202*7c478bd9Sstevel@tonic-gate 			 */
203*7c478bd9Sstevel@tonic-gate 			foundslot = i;
204*7c478bd9Sstevel@tonic-gate 			break;
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if (foundslot != -1) {
209*7c478bd9Sstevel@tonic-gate 		/* We found a slot for this mondo vec, clear it */
210*7c478bd9Sstevel@tonic-gate 		ittptr->ittrans_mondovec[foundslot] = 0;
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ittptr->ittrans_lock);
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate int
pc_madr_add(int lboard,int rboard,int proc,uint_t madr)217*7c478bd9Sstevel@tonic-gate pc_madr_add(int lboard, int rboard, int proc, uint_t madr)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate 	register int	i;
220*7c478bd9Sstevel@tonic-gate 	uint_t		madr_rd, madr_off;
221*7c478bd9Sstevel@tonic-gate 	uint64_t	pc_madr_addr;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	pc_madr_addr = STARFIRE_PC_MADR_ADDR(lboard, rboard, proc);
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	/*
226*7c478bd9Sstevel@tonic-gate 	 * First write with Presence bit disabled
227*7c478bd9Sstevel@tonic-gate 	 * and then with it enabled.
228*7c478bd9Sstevel@tonic-gate 	 */
229*7c478bd9Sstevel@tonic-gate 	madr_off = madr & ~STARFIRE_MC_MEM_PRESENT_MASK;
230*7c478bd9Sstevel@tonic-gate 	stphysio(pc_madr_addr, madr_off);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 20; i++) {
233*7c478bd9Sstevel@tonic-gate 		madr_rd = ldphysio(pc_madr_addr);
234*7c478bd9Sstevel@tonic-gate 		if (madr_off == madr_rd)
235*7c478bd9Sstevel@tonic-gate 			break;
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 	if (madr_off != madr_rd) {
238*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
239*7c478bd9Sstevel@tonic-gate 			"pc_madr_add: (1) failed to update "
240*7c478bd9Sstevel@tonic-gate 			"PC MADR (%d, %d, %d, 0x%x)\n",
241*7c478bd9Sstevel@tonic-gate 			lboard, rboard, proc, madr);
242*7c478bd9Sstevel@tonic-gate 		return (-1);
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 	if (madr == madr_off) {
245*7c478bd9Sstevel@tonic-gate 		/*
246*7c478bd9Sstevel@tonic-gate 		 * Caller wanted to write value out there
247*7c478bd9Sstevel@tonic-gate 		 * with presence bit turned off, which is
248*7c478bd9Sstevel@tonic-gate 		 * what we just completed.  So, we're finished.
249*7c478bd9Sstevel@tonic-gate 		 */
250*7c478bd9Sstevel@tonic-gate 		return (0);
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 	/*
253*7c478bd9Sstevel@tonic-gate 	 * Now write with Presence bit enabled.
254*7c478bd9Sstevel@tonic-gate 	 */
255*7c478bd9Sstevel@tonic-gate 	stphysio(pc_madr_addr, madr);
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 20; i++) {
258*7c478bd9Sstevel@tonic-gate 		madr_rd = ldphysio(pc_madr_addr);
259*7c478bd9Sstevel@tonic-gate 		if (madr == madr_rd)
260*7c478bd9Sstevel@tonic-gate 			break;
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 	if (madr != madr_rd) {
263*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
264*7c478bd9Sstevel@tonic-gate 			"pc_madr_add: (2) failed to update "
265*7c478bd9Sstevel@tonic-gate 			"PC MADR (%d, %d, %d, 0x%x)\n",
266*7c478bd9Sstevel@tonic-gate 			lboard, rboard, proc, madr);
267*7c478bd9Sstevel@tonic-gate 		return (-1);
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	return (0);
271*7c478bd9Sstevel@tonic-gate }
272