xref: /onnv-gate/usr/src/uts/sun4u/starfire/io/memctrl.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1998, 2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Starfire Memory Controller specific routines.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/debug.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/errno.h>
36*0Sstevel@tonic-gate #include <sys/dditypes.h>
37*0Sstevel@tonic-gate #include <sys/conf.h>
38*0Sstevel@tonic-gate #include <sys/ddi.h>
39*0Sstevel@tonic-gate #include <sys/sunddi.h>
40*0Sstevel@tonic-gate #include <sys/sunndi.h>
41*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
42*0Sstevel@tonic-gate #include <sys/promif.h>
43*0Sstevel@tonic-gate #include <sys/machsystm.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <sys/starfire.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate struct mc_dimm_table {
48*0Sstevel@tonic-gate 	int	mc_type;
49*0Sstevel@tonic-gate 	int	mc_module_size;		/* module size in MB */
50*0Sstevel@tonic-gate };
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static struct mc_dimm_table dimmsize_table[] = {
53*0Sstevel@tonic-gate 	{ 4,	8 },
54*0Sstevel@tonic-gate 	{ 6,	8 },
55*0Sstevel@tonic-gate 	{ 11,	32 },
56*0Sstevel@tonic-gate 	{ 15,	128 },
57*0Sstevel@tonic-gate 	{ 0,	0 }
58*0Sstevel@tonic-gate };
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define	MC_MB(mb) ((mb) * 1048576ull)
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate struct mc_seg_size {
63*0Sstevel@tonic-gate 	uint_t		seg_mask;
64*0Sstevel@tonic-gate 	uint64_t	seg_size;
65*0Sstevel@tonic-gate };
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate struct mc_seg_size mc_seg_table[] = {
68*0Sstevel@tonic-gate 	{ 0x7f,	MC_MB(64)	},
69*0Sstevel@tonic-gate 	{ 0x7e,	MC_MB(128)	},
70*0Sstevel@tonic-gate 	{ 0x7c,	MC_MB(256)	},
71*0Sstevel@tonic-gate 	{ 0x78,	MC_MB(512)	},
72*0Sstevel@tonic-gate 	{ 0x70,	MC_MB(1024)	},
73*0Sstevel@tonic-gate 	{ 0x60,	MC_MB(2048)	},
74*0Sstevel@tonic-gate 	{ 0x40,	MC_MB(4096)	},
75*0Sstevel@tonic-gate 	{ 0,	0		}
76*0Sstevel@tonic-gate };
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * Alignment of memory between MC's.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate uint64_t
82*0Sstevel@tonic-gate mc_get_mem_alignment()
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	return (STARFIRE_MC_MEMBOARD_ALIGNMENT);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate uint64_t
88*0Sstevel@tonic-gate mc_get_asr_addr(dnode_t nodeid)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	int		rlen;
91*0Sstevel@tonic-gate 	uint64_t	psi_addr;
92*0Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
95*0Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
96*0Sstevel@tonic-gate 		return ((uint64_t)-1);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
99*0Sstevel@tonic-gate 		return ((uint64_t)-1);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
102*0Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	return (STARFIRE_MC_ASR_ADDR(psi_addr));
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate uint64_t
108*0Sstevel@tonic-gate mc_get_idle_addr(dnode_t nodeid)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	int		rlen;
111*0Sstevel@tonic-gate 	uint64_t	psi_addr;
112*0Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
115*0Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
116*0Sstevel@tonic-gate 		return ((uint64_t)-1);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
119*0Sstevel@tonic-gate 		return ((uint64_t)-1);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
122*0Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	return (STARFIRE_MC_IDLE_ADDR(psi_addr));
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate int
128*0Sstevel@tonic-gate mc_get_dimm_size(dnode_t nodeid)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	uint64_t	psi_addr;
131*0Sstevel@tonic-gate 	uint_t		dimmtype;
132*0Sstevel@tonic-gate 	int		i, rlen;
133*0Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
136*0Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
137*0Sstevel@tonic-gate 		return (-1);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
140*0Sstevel@tonic-gate 		return (-1);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
143*0Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
144*0Sstevel@tonic-gate 	psi_addr = STARFIRE_MC_DIMMTYPE_ADDR(psi_addr);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
147*0Sstevel@tonic-gate 		return (-1);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	dimmtype = ldphysio(psi_addr);
150*0Sstevel@tonic-gate 	dimmtype &= STARFIRE_MC_DIMMSIZE_MASK;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	for (i = 0; dimmsize_table[i].mc_type != 0; i++)
153*0Sstevel@tonic-gate 		if (dimmsize_table[i].mc_type == dimmtype)
154*0Sstevel@tonic-gate 			break;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	return (dimmsize_table[i].mc_module_size);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate uint64_t
160*0Sstevel@tonic-gate mc_get_alignment_mask(dnode_t nodeid)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	uint64_t	psi_addr, seg_sz;
163*0Sstevel@tonic-gate 	uint_t		mcreg, seg_sz_mask;
164*0Sstevel@tonic-gate 	int		i, rlen;
165*0Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
168*0Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
169*0Sstevel@tonic-gate 		return (-1);
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
172*0Sstevel@tonic-gate 		return (-1);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
175*0Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
176*0Sstevel@tonic-gate 	psi_addr = STARFIRE_MC_ASR_ADDR(psi_addr);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
179*0Sstevel@tonic-gate 		return (-1);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	mcreg = ldphysio(psi_addr);
182*0Sstevel@tonic-gate 	seg_sz_mask = (mcreg & STARFIRE_MC_MASK_MASK) >> 8;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	for (i = 0; mc_seg_table[i].seg_size != 0; i++)
185*0Sstevel@tonic-gate 		if (mc_seg_table[i].seg_mask == seg_sz_mask)
186*0Sstevel@tonic-gate 			break;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	if (mc_seg_table[i].seg_size == 0)
189*0Sstevel@tonic-gate 		seg_sz = mc_get_mem_alignment();
190*0Sstevel@tonic-gate 	else
191*0Sstevel@tonic-gate 		seg_sz = mc_seg_table[i].seg_size;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate #ifdef DEBUG
194*0Sstevel@tonic-gate 	printf("nodeid %x, mc asr addr %llx, val %x, seg_sz_mask %x, "
195*0Sstevel@tonic-gate 	    "seg_sz %llx\n", nodeid, psi_addr, mcreg, seg_sz_mask, seg_sz);
196*0Sstevel@tonic-gate #endif /* DEBUG */
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	return (seg_sz - 1);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate int
202*0Sstevel@tonic-gate mc_read_asr(dnode_t nodeid, uint_t *mcregp)
203*0Sstevel@tonic-gate {
204*0Sstevel@tonic-gate 	uint64_t	psi_addr;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	*mcregp = 0;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	psi_addr = mc_get_asr_addr(nodeid);
209*0Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
210*0Sstevel@tonic-gate 		return (-1);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	*mcregp = ldphysio(psi_addr);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	return (0);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate int
218*0Sstevel@tonic-gate mc_write_asr(dnode_t nodeid, uint_t mcreg)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	uint_t		mcreg_rd;
221*0Sstevel@tonic-gate 	uint64_t	psi_addr;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	psi_addr = mc_get_asr_addr(nodeid);
224*0Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
225*0Sstevel@tonic-gate 		return (-1);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	stphysio(psi_addr, mcreg);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	mcreg_rd = ldphysio(psi_addr);
230*0Sstevel@tonic-gate 	ASSERT(mcreg_rd == mcreg);
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	return ((mcreg_rd != mcreg) ? -1 : 0);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate uint64_t
236*0Sstevel@tonic-gate mc_asr_to_pa(uint_t mcreg)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	uint64_t	pa, masr, addrmask, lowbitmask;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	/*
241*0Sstevel@tonic-gate 	 * Remove memory present bit.
242*0Sstevel@tonic-gate 	 */
243*0Sstevel@tonic-gate 	masr = (uint64_t)(mcreg & ~STARFIRE_MC_MEM_PRESENT_MASK);
244*0Sstevel@tonic-gate 	/*
245*0Sstevel@tonic-gate 	 * Get mask for bits 32-26.
246*0Sstevel@tonic-gate 	 */
247*0Sstevel@tonic-gate 	lowbitmask = masr & (uint64_t)STARFIRE_MC_MASK_MASK;
248*0Sstevel@tonic-gate 	lowbitmask <<= STARFIRE_MC_MASK_SHIFT;
249*0Sstevel@tonic-gate 	addrmask = STARFIRE_MC_ADDR_HIBITS | lowbitmask;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	pa = (masr << STARFIRE_MC_BASE_SHIFT) & addrmask;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	return (pa);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate uint_t
257*0Sstevel@tonic-gate mc_pa_to_asr(uint_t masr, uint64_t pa)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate 	uint64_t	addrmask, lowbitmask;
260*0Sstevel@tonic-gate 	uint_t		base;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	/*
263*0Sstevel@tonic-gate 	 * Get mask for bits 32-26.
264*0Sstevel@tonic-gate 	 */
265*0Sstevel@tonic-gate 	lowbitmask = masr & (uint64_t)STARFIRE_MC_MASK_MASK;
266*0Sstevel@tonic-gate 	lowbitmask <<= STARFIRE_MC_MASK_SHIFT;
267*0Sstevel@tonic-gate 	addrmask = STARFIRE_MC_ADDR_HIBITS | lowbitmask;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	base  = (pa & addrmask) >> STARFIRE_MC_BASE_SHIFT;
270*0Sstevel@tonic-gate 	masr &= ~ STARFIRE_MC_MEM_BASEADDR_MASK;
271*0Sstevel@tonic-gate 	masr |= base & STARFIRE_MC_MEM_BASEADDR_MASK;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	ASSERT(mc_asr_to_pa(masr) == pa);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	return (masr);
276*0Sstevel@tonic-gate }
277