xref: /onnv-gate/usr/src/uts/sun4u/starfire/os/pda.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 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
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 Post Descriptor Array (post2obp) management.
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/cpuvar.h>
37*0Sstevel@tonic-gate #include <sys/dditypes.h>
38*0Sstevel@tonic-gate #include <sys/conf.h>
39*0Sstevel@tonic-gate #include <sys/ddi.h>
40*0Sstevel@tonic-gate #include <sys/kmem.h>
41*0Sstevel@tonic-gate #include <sys/sunddi.h>
42*0Sstevel@tonic-gate #include <sys/sunndi.h>
43*0Sstevel@tonic-gate #include <sys/vm.h>
44*0Sstevel@tonic-gate #include <vm/seg.h>
45*0Sstevel@tonic-gate #include <vm/seg_kmem.h>
46*0Sstevel@tonic-gate #include <vm/seg_kp.h>
47*0Sstevel@tonic-gate #include <sys/machsystm.h>
48*0Sstevel@tonic-gate #include <sys/starfire.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
51*0Sstevel@tonic-gate #include <sys/pda.h>
52*0Sstevel@tonic-gate #include <sys/cpu_sgn.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate extern struct cpu	*SIGBCPU;
55*0Sstevel@tonic-gate extern cpu_sgnblk_t	*cpu_sgnblkp[];
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate extern uint64_t		mc_get_mem_alignment();
58*0Sstevel@tonic-gate extern uint64_t		mc_asr_to_pa(uint_t mcreg);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static post2obp_info_t	*cpu_p2o_mapin(int cpuid);
61*0Sstevel@tonic-gate static void		cpu_p2o_mapout(int cpuid, post2obp_info_t *p2o);
62*0Sstevel@tonic-gate static void		p2o_update_checksum(post2obp_info_t *p2o);
63*0Sstevel@tonic-gate static uint_t		p2o_calc_checksum(post2obp_info_t *p2o);
64*0Sstevel@tonic-gate static void		p2o_mem_sort(post2obp_info_t *p2o);
65*0Sstevel@tonic-gate static void		p2o_mem_coalesce(post2obp_info_t *p2o);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate typedef struct {
68*0Sstevel@tonic-gate 	post2obp_info_t	*p2o_ptr;
69*0Sstevel@tonic-gate 	int		p2o_cpuid;
70*0Sstevel@tonic-gate } p2o_info_t;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * PDA management routines.  Should ultimately be made
74*0Sstevel@tonic-gate  * accessible to other Starfire subsystems, but for
75*0Sstevel@tonic-gate  * now we'll leave it here.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate pda_handle_t
pda_open()78*0Sstevel@tonic-gate pda_open()
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	p2o_info_t	*pip;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	if (SIGBCPU == NULL) {
83*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "pda_open: SIGBCPU is NULL");
84*0Sstevel@tonic-gate 		return (NULL);
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	pip = (p2o_info_t *)kmem_alloc(sizeof (p2o_info_t), KM_SLEEP);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	pip->p2o_cpuid = (int)SIGBCPU->cpu_id;
90*0Sstevel@tonic-gate 	pip->p2o_ptr = cpu_p2o_mapin(pip->p2o_cpuid);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	if (pip->p2o_ptr == NULL) {
93*0Sstevel@tonic-gate 		kmem_free((caddr_t)pip, sizeof (p2o_info_t));
94*0Sstevel@tonic-gate 		return ((pda_handle_t)NULL);
95*0Sstevel@tonic-gate 	} else {
96*0Sstevel@tonic-gate 		return ((pda_handle_t)pip);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate void
pda_close(pda_handle_t ph)101*0Sstevel@tonic-gate pda_close(pda_handle_t ph)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	p2o_info_t	*pip;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	if ((pip = (p2o_info_t *)ph) == NULL)
106*0Sstevel@tonic-gate 		return;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	cpu_p2o_mapout(pip->p2o_cpuid, pip->p2o_ptr);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	kmem_free((caddr_t)pip, sizeof (p2o_info_t));
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate int
pda_board_present(pda_handle_t ph,int boardnum)114*0Sstevel@tonic-gate pda_board_present(pda_handle_t ph, int boardnum)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	ushort_t	bda_board;
117*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	bda_board = p2o->p2o_bdinfo[boardnum].bda_board;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if ((bda_board & BDAN_MASK) != BDAN_GOOD)
122*0Sstevel@tonic-gate 		return (0);
123*0Sstevel@tonic-gate 	else
124*0Sstevel@tonic-gate 		return (1);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate void *
pda_get_board_info(pda_handle_t ph,int boardnum)128*0Sstevel@tonic-gate pda_get_board_info(pda_handle_t ph, int boardnum)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	return ((void *)&(p2o->p2o_bdinfo[boardnum]));
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate uint_t
pda_get_mem_size(pda_handle_t ph,int boardnum)136*0Sstevel@tonic-gate pda_get_mem_size(pda_handle_t ph, int boardnum)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	int		c;
139*0Sstevel@tonic-gate 	pgcnt_t		npages;
140*0Sstevel@tonic-gate 	uint_t		asr;
141*0Sstevel@tonic-gate 	pfn_t		basepfn, endpfn;
142*0Sstevel@tonic-gate 	uint64_t	basepa, endpa;
143*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if (boardnum == -1)
146*0Sstevel@tonic-gate 		return (p2o->p2o_memtotal.Memt_NumPages);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	asr = p2o->p2o_bdminfo[boardnum].bmda_adr;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	basepa = mc_asr_to_pa(asr);
151*0Sstevel@tonic-gate 	/*
152*0Sstevel@tonic-gate 	 * Put on MC alignment.
153*0Sstevel@tonic-gate 	 */
154*0Sstevel@tonic-gate 	endpa = mc_get_mem_alignment();
155*0Sstevel@tonic-gate 	basepa &= ~(endpa - 1);
156*0Sstevel@tonic-gate 	endpa += basepa;
157*0Sstevel@tonic-gate 	basepfn = (pfn_t)(basepa >> PAGESHIFT);
158*0Sstevel@tonic-gate 	endpfn = (pfn_t)(endpa >> PAGESHIFT);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	npages = 0;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	for (c = 0; c < p2o->p2o_memtotal.Memt_NumChunks; c++) {
163*0Sstevel@tonic-gate 		pfn_t	c_basepfn, c_endpfn;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 		c_basepfn = (pfn_t)p2o->p2o_mchunks[c].Memc_StartAddress
166*0Sstevel@tonic-gate 		    >> (PAGESHIFT - BDA_PAGESHIFT);
167*0Sstevel@tonic-gate 		c_endpfn = (pfn_t)p2o->p2o_mchunks[c].Memc_Size
168*0Sstevel@tonic-gate 		    >> (PAGESHIFT - BDA_PAGESHIFT);
169*0Sstevel@tonic-gate 		c_endpfn += c_basepfn;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 		if ((endpfn <= c_basepfn) || (basepfn >= c_endpfn))
172*0Sstevel@tonic-gate 			continue;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		c_basepfn = MAX(c_basepfn, basepfn);
175*0Sstevel@tonic-gate 		c_endpfn = MIN(c_endpfn, endpfn);
176*0Sstevel@tonic-gate 		ASSERT(c_basepfn <= c_endpfn);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 		npages += c_endpfn - c_basepfn;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	return (npages);
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate void
pda_mem_add_span(pda_handle_t ph,uint64_t basepa,uint64_t nbytes)185*0Sstevel@tonic-gate pda_mem_add_span(pda_handle_t ph, uint64_t basepa, uint64_t nbytes)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
188*0Sstevel@tonic-gate 	int		c, nchunks;
189*0Sstevel@tonic-gate 	pfn_t		a_pfn, a_npgs;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	ASSERT(p2o);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	nchunks = p2o->p2o_memtotal.Memt_NumChunks;
194*0Sstevel@tonic-gate 	a_pfn = (pfn_t)(basepa >> BDA_PAGESHIFT);
195*0Sstevel@tonic-gate 	a_npgs = (pfn_t)(nbytes >> BDA_PAGESHIFT);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	for (c = 0; c < nchunks; c++) {
198*0Sstevel@tonic-gate 		int	cend;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		if (a_pfn <= p2o->p2o_mchunks[c].Memc_StartAddress) {
201*0Sstevel@tonic-gate 			for (cend = nchunks; cend > c; cend--)
202*0Sstevel@tonic-gate 				p2o->p2o_mchunks[cend] =
203*0Sstevel@tonic-gate 						p2o->p2o_mchunks[cend - 1];
204*0Sstevel@tonic-gate 			break;
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 	p2o->p2o_mchunks[c].Memc_StartAddress = a_pfn;
208*0Sstevel@tonic-gate 	p2o->p2o_mchunks[c].Memc_Size = a_npgs;
209*0Sstevel@tonic-gate 	nchunks++;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	p2o->p2o_memtotal.Memt_NumChunks = nchunks;
212*0Sstevel@tonic-gate 	p2o->p2o_memtotal.Memt_NumPages += a_npgs;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	p2o_mem_sort(p2o);
215*0Sstevel@tonic-gate 	p2o_mem_coalesce(p2o);
216*0Sstevel@tonic-gate 	p2o_update_checksum(p2o);
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate void
pda_mem_del_span(pda_handle_t ph,uint64_t basepa,uint64_t nbytes)220*0Sstevel@tonic-gate pda_mem_del_span(pda_handle_t ph, uint64_t basepa, uint64_t nbytes)
221*0Sstevel@tonic-gate {
222*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
223*0Sstevel@tonic-gate 	int		c, o_nchunks, n_nchunks;
224*0Sstevel@tonic-gate 	pfn_t		d_pfn;
225*0Sstevel@tonic-gate 	pgcnt_t		d_npgs, npages;
226*0Sstevel@tonic-gate 	MemChunk_t	*mp, *endp;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	ASSERT(p2o);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	d_pfn = (pfn_t)(basepa >> BDA_PAGESHIFT);
231*0Sstevel@tonic-gate 	d_npgs = (pgcnt_t)(nbytes >> BDA_PAGESHIFT);
232*0Sstevel@tonic-gate 	n_nchunks = o_nchunks = p2o->p2o_memtotal.Memt_NumChunks;
233*0Sstevel@tonic-gate 	endp = &(p2o->p2o_mchunks[o_nchunks]);
234*0Sstevel@tonic-gate 	npages = 0;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	for (c = 0; c < o_nchunks; c++) {
237*0Sstevel@tonic-gate 		uint_t	p_pfn, p_npgs;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 		p_pfn = p2o->p2o_mchunks[c].Memc_StartAddress;
240*0Sstevel@tonic-gate 		p_npgs = p2o->p2o_mchunks[c].Memc_Size;
241*0Sstevel@tonic-gate 		if (p_npgs == 0)
242*0Sstevel@tonic-gate 			continue;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 		if (((d_pfn + d_npgs) <= p_pfn) ||
245*0Sstevel@tonic-gate 					(d_pfn >= (p_pfn + p_npgs))) {
246*0Sstevel@tonic-gate 			npages += p_npgs;
247*0Sstevel@tonic-gate 			continue;
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 		if (d_pfn < p_pfn) {
251*0Sstevel@tonic-gate 			if ((d_pfn + d_npgs) >= (p_pfn + p_npgs)) {
252*0Sstevel@tonic-gate 				/*
253*0Sstevel@tonic-gate 				 * Entire chunk goes away.
254*0Sstevel@tonic-gate 				 */
255*0Sstevel@tonic-gate 				p_pfn = p_npgs = 0;
256*0Sstevel@tonic-gate 			} else {
257*0Sstevel@tonic-gate 				p_npgs -= d_pfn + d_npgs - p_pfn;
258*0Sstevel@tonic-gate 				p_pfn = d_pfn + d_npgs;
259*0Sstevel@tonic-gate 			}
260*0Sstevel@tonic-gate 		} else if (d_pfn == p_pfn) {
261*0Sstevel@tonic-gate 			if ((d_pfn + d_npgs) >= (p_pfn + p_npgs)) {
262*0Sstevel@tonic-gate 				p_pfn = p_npgs = 0;
263*0Sstevel@tonic-gate 			} else {
264*0Sstevel@tonic-gate 				p_npgs -= d_npgs;
265*0Sstevel@tonic-gate 				p_pfn += d_npgs;
266*0Sstevel@tonic-gate 			}
267*0Sstevel@tonic-gate 		} else {
268*0Sstevel@tonic-gate 			if ((d_pfn + d_npgs) >= (p_pfn + p_npgs)) {
269*0Sstevel@tonic-gate 				p_npgs = d_pfn - p_pfn;
270*0Sstevel@tonic-gate 				npages += p_npgs;
271*0Sstevel@tonic-gate 			} else {
272*0Sstevel@tonic-gate 				/*
273*0Sstevel@tonic-gate 				 * Ugh, got to split a
274*0Sstevel@tonic-gate 				 * memchunk, we're going to
275*0Sstevel@tonic-gate 				 * need an extra one.  It's
276*0Sstevel@tonic-gate 				 * gotten from the end.
277*0Sstevel@tonic-gate 				 */
278*0Sstevel@tonic-gate 				endp->Memc_StartAddress = d_pfn + d_npgs;
279*0Sstevel@tonic-gate 				endp->Memc_Size = (p_pfn + p_npgs)
280*0Sstevel@tonic-gate 							- (d_pfn + d_npgs);
281*0Sstevel@tonic-gate 				npages += endp->Memc_Size;
282*0Sstevel@tonic-gate 				endp++;
283*0Sstevel@tonic-gate 				n_nchunks++;
284*0Sstevel@tonic-gate 				p_npgs = d_pfn - p_pfn;
285*0Sstevel@tonic-gate 			}
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 		p2o->p2o_mchunks[c].Memc_StartAddress = p_pfn;
289*0Sstevel@tonic-gate 		p2o->p2o_mchunks[c].Memc_Size = p_npgs;
290*0Sstevel@tonic-gate 		if (p_npgs == 0)
291*0Sstevel@tonic-gate 			n_nchunks--;
292*0Sstevel@tonic-gate 		npages += p_npgs;
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 	p2o->p2o_memtotal.Memt_NumChunks = n_nchunks;
295*0Sstevel@tonic-gate 	p2o->p2o_memtotal.Memt_NumPages = npages;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	/*
298*0Sstevel@tonic-gate 	 * There is a possibility we created holes in the memchunk list
299*0Sstevel@tonic-gate 	 * due to memchunks that went away.  Before we can sort and
300*0Sstevel@tonic-gate 	 * coalesce we need to "pull up" the end of the memchunk list
301*0Sstevel@tonic-gate 	 * and get rid of any holes.
302*0Sstevel@tonic-gate 	 * endp = points to the last empty memchunk entry.
303*0Sstevel@tonic-gate 	 */
304*0Sstevel@tonic-gate 	for (mp = &(p2o->p2o_mchunks[0]); mp < endp; mp++) {
305*0Sstevel@tonic-gate 		register MemChunk_t	*mmp;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 		if (mp->Memc_Size)
308*0Sstevel@tonic-gate 			continue;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 		for (mmp = mp; mmp < endp; mmp++)
311*0Sstevel@tonic-gate 			*mmp = *(mmp + 1);
312*0Sstevel@tonic-gate 		mp--;
313*0Sstevel@tonic-gate 		endp--;
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 	ASSERT(endp == &(p2o->p2o_mchunks[n_nchunks]));
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	p2o_mem_sort(p2o);
318*0Sstevel@tonic-gate 	p2o_mem_coalesce(p2o);
319*0Sstevel@tonic-gate 	p2o_update_checksum(p2o);
320*0Sstevel@tonic-gate }
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate /*
323*0Sstevel@tonic-gate  * Synchonize all memory attributes (currently just MC ADRs [aka ASR])
324*0Sstevel@tonic-gate  * with PDA representative values for the given board.  A board value
325*0Sstevel@tonic-gate  * of (-1) indicates all boards.
326*0Sstevel@tonic-gate  */
327*0Sstevel@tonic-gate /*ARGSUSED*/
328*0Sstevel@tonic-gate void
pda_mem_sync(pda_handle_t ph,int board,int unit)329*0Sstevel@tonic-gate pda_mem_sync(pda_handle_t ph, int board, int unit)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
332*0Sstevel@tonic-gate 	register int	b;
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	for (b = 0; b < MAX_SYSBDS; b++) {
335*0Sstevel@tonic-gate 		if ((board != -1) && (board != b))
336*0Sstevel@tonic-gate 			continue;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 		if (pda_board_present(ph, b)) {
339*0Sstevel@tonic-gate 			uint64_t	masr;
340*0Sstevel@tonic-gate 			uint_t		masr_value;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 			masr = STARFIRE_MC_ASR_ADDR_BOARD(b);
343*0Sstevel@tonic-gate 			masr_value = ldphysio(masr);
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 			p2o->p2o_bdminfo[b].bmda_adr = masr_value;
346*0Sstevel@tonic-gate 		}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 		if (board == b)
349*0Sstevel@tonic-gate 			break;
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	p2o_update_checksum(p2o);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate void
pda_get_busmask(pda_handle_t ph,short * amask,short * dmask)356*0Sstevel@tonic-gate pda_get_busmask(pda_handle_t ph, short *amask, short *dmask)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 	if (amask)
361*0Sstevel@tonic-gate 		*amask = p2o ? p2o->p2o_abus_mask : 0;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	if (dmask)
364*0Sstevel@tonic-gate 		*dmask = p2o ? p2o->p2o_dbus_mask : 0;
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate int
pda_is_valid(pda_handle_t ph)368*0Sstevel@tonic-gate pda_is_valid(pda_handle_t ph)
369*0Sstevel@tonic-gate {
370*0Sstevel@tonic-gate 	post2obp_info_t	*p2o = ((p2o_info_t *)ph)->p2o_ptr;
371*0Sstevel@tonic-gate 	uint_t		csum;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	if (p2o == NULL)
374*0Sstevel@tonic-gate 		return (0);
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	csum = p2o_calc_checksum(p2o);
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	return (csum == p2o->p2o_csum);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate /*
382*0Sstevel@tonic-gate  * Post2obp support functions below here.  Internal to PDA module.
383*0Sstevel@tonic-gate  *
384*0Sstevel@tonic-gate  * p2o_update_checksum
385*0Sstevel@tonic-gate  *
386*0Sstevel@tonic-gate  * Calculate checksum for post2obp structure and insert it so
387*0Sstevel@tonic-gate  * when POST reads it he'll be happy.
388*0Sstevel@tonic-gate  */
389*0Sstevel@tonic-gate static void
p2o_update_checksum(post2obp_info_t * p2o)390*0Sstevel@tonic-gate p2o_update_checksum(post2obp_info_t *p2o)
391*0Sstevel@tonic-gate {
392*0Sstevel@tonic-gate 	uint_t	new_csum;
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	ASSERT(p2o);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	new_csum = p2o_calc_checksum(p2o);
397*0Sstevel@tonic-gate 	p2o->p2o_csum = new_csum;
398*0Sstevel@tonic-gate }
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate static uint_t
p2o_calc_checksum(post2obp_info_t * p2o)401*0Sstevel@tonic-gate p2o_calc_checksum(post2obp_info_t *p2o)
402*0Sstevel@tonic-gate {
403*0Sstevel@tonic-gate 	int	i, nchunks;
404*0Sstevel@tonic-gate 	uint_t	*csumptr;
405*0Sstevel@tonic-gate 	uint_t	p2o_size;
406*0Sstevel@tonic-gate 	uint_t	csum, o_csum;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	ASSERT(p2o != NULL);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	nchunks = p2o->p2o_memtotal.Memt_NumChunks;
411*0Sstevel@tonic-gate 	p2o_size = sizeof (post2obp_info_t)
412*0Sstevel@tonic-gate 		    + ((nchunks - VAR_ARRAY_LEN) * sizeof (MemChunk_t));
413*0Sstevel@tonic-gate 	p2o_size /= sizeof (uint_t);
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	o_csum = p2o->p2o_csum;
416*0Sstevel@tonic-gate 	p2o->p2o_csum = 0;
417*0Sstevel@tonic-gate 	csum = 0;
418*0Sstevel@tonic-gate 	for (i = 0, csumptr = (uint_t *)p2o; i < p2o_size; i++)
419*0Sstevel@tonic-gate 		csum += *csumptr++;
420*0Sstevel@tonic-gate 	p2o->p2o_csum = o_csum;
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	return (-csum);
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate /*
426*0Sstevel@tonic-gate  * Sort the mchunk list in ascending order based on the
427*0Sstevel@tonic-gate  * Memc_StartAddress field.
428*0Sstevel@tonic-gate  *
429*0Sstevel@tonic-gate  * disclosure: This is based on the qsort() library routine.
430*0Sstevel@tonic-gate  */
431*0Sstevel@tonic-gate static void
p2o_mem_sort(post2obp_info_t * p2o)432*0Sstevel@tonic-gate p2o_mem_sort(post2obp_info_t *p2o)
433*0Sstevel@tonic-gate {
434*0Sstevel@tonic-gate 	MemChunk_t	*base;
435*0Sstevel@tonic-gate 	int		nchunks;
436*0Sstevel@tonic-gate 	uint_t		c1, c2;
437*0Sstevel@tonic-gate 	char		*min, *max;
438*0Sstevel@tonic-gate 	register char 	c, *i, *j, *lo, *hi;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	ASSERT(p2o != NULL);
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	nchunks = p2o->p2o_memtotal.Memt_NumChunks;
443*0Sstevel@tonic-gate 	base = &p2o->p2o_mchunks[0];
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	/* ala qsort() */
446*0Sstevel@tonic-gate 	max = (char *)base + nchunks * sizeof (MemChunk_t);
447*0Sstevel@tonic-gate 	hi  = max;
448*0Sstevel@tonic-gate 	for (j = lo = (char *)base; (lo += sizeof (MemChunk_t)) < hi; ) {
449*0Sstevel@tonic-gate 		c1 = ((MemChunk_t *)j)->Memc_StartAddress;
450*0Sstevel@tonic-gate 		c2 = ((MemChunk_t *)lo)->Memc_StartAddress;
451*0Sstevel@tonic-gate 		if (c1 > c2)
452*0Sstevel@tonic-gate 			j = lo;
453*0Sstevel@tonic-gate 	}
454*0Sstevel@tonic-gate 	if (j != (char *)base) {
455*0Sstevel@tonic-gate 		for (i = (char *)base,
456*0Sstevel@tonic-gate 		    hi = (char *)base + sizeof (MemChunk_t);
457*0Sstevel@tonic-gate 		    /* CSTYLED */
458*0Sstevel@tonic-gate 		    i < hi;) {
459*0Sstevel@tonic-gate 			c = *j;
460*0Sstevel@tonic-gate 			*j++ = *i;
461*0Sstevel@tonic-gate 			*i++ = c;
462*0Sstevel@tonic-gate 		}
463*0Sstevel@tonic-gate 	}
464*0Sstevel@tonic-gate 	for (min = (char *)base;
465*0Sstevel@tonic-gate 	    /* CSTYLED */
466*0Sstevel@tonic-gate 	    (hi = min += sizeof (MemChunk_t)) < max;) {
467*0Sstevel@tonic-gate 		do {
468*0Sstevel@tonic-gate 			hi -= sizeof (MemChunk_t);
469*0Sstevel@tonic-gate 			c1 = ((MemChunk_t *)hi)->Memc_StartAddress;
470*0Sstevel@tonic-gate 			c2 = ((MemChunk_t *)min)->Memc_StartAddress;
471*0Sstevel@tonic-gate 		} while (c1 > c2);
472*0Sstevel@tonic-gate 		if ((hi += sizeof (MemChunk_t)) != min) {
473*0Sstevel@tonic-gate 			for (lo = min + sizeof (MemChunk_t);
474*0Sstevel@tonic-gate 			    /* CSTYLED */
475*0Sstevel@tonic-gate 			    --lo >= min;) {
476*0Sstevel@tonic-gate 				c = *lo;
477*0Sstevel@tonic-gate 				for (i = j = lo;
478*0Sstevel@tonic-gate 				    (j -= sizeof (MemChunk_t)) >= hi;
479*0Sstevel@tonic-gate 				    i = j) {
480*0Sstevel@tonic-gate 					*i = *j;
481*0Sstevel@tonic-gate 				}
482*0Sstevel@tonic-gate 				*i = c;
483*0Sstevel@tonic-gate 			}
484*0Sstevel@tonic-gate 		}
485*0Sstevel@tonic-gate 	}
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate static void
p2o_mem_coalesce(post2obp_info_t * p2o)489*0Sstevel@tonic-gate p2o_mem_coalesce(post2obp_info_t *p2o)
490*0Sstevel@tonic-gate {
491*0Sstevel@tonic-gate 	MemChunk_t	*mc;
492*0Sstevel@tonic-gate 	int		nchunks, new_nchunks;
493*0Sstevel@tonic-gate 	uint_t		addr, size, naddr, nsize;
494*0Sstevel@tonic-gate 	uint_t		npages;
495*0Sstevel@tonic-gate 	register int	i, cp, ncp;
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	ASSERT(p2o != NULL);
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	nchunks = new_nchunks = p2o->p2o_memtotal.Memt_NumChunks;
500*0Sstevel@tonic-gate 	mc = &p2o->p2o_mchunks[0];
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	for (cp = i = 0; i < (nchunks-1); i++, cp = ncp) {
503*0Sstevel@tonic-gate 		ncp = cp + 1;
504*0Sstevel@tonic-gate 		addr = mc[cp].Memc_StartAddress;
505*0Sstevel@tonic-gate 		size = mc[cp].Memc_Size;
506*0Sstevel@tonic-gate 		naddr = mc[ncp].Memc_StartAddress;
507*0Sstevel@tonic-gate 		nsize = mc[ncp].Memc_Size;
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 		if ((addr + size) >= naddr) {
510*0Sstevel@tonic-gate 			uint_t	overlap;
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 			overlap = addr + size - naddr;
513*0Sstevel@tonic-gate 			/*
514*0Sstevel@tonic-gate 			 * if (nsize < overlap) then
515*0Sstevel@tonic-gate 			 * next entry fits within the current
516*0Sstevel@tonic-gate 			 * entry so no need to update size.
517*0Sstevel@tonic-gate 			 */
518*0Sstevel@tonic-gate 			if (nsize >= overlap) {
519*0Sstevel@tonic-gate 				size += nsize - overlap;
520*0Sstevel@tonic-gate 				mc[cp].Memc_Size = size;
521*0Sstevel@tonic-gate 			}
522*0Sstevel@tonic-gate 			bcopy((char *)&mc[ncp+1],
523*0Sstevel@tonic-gate 			    (char *)&mc[ncp],
524*0Sstevel@tonic-gate 			    (nchunks - ncp - 1) * sizeof (MemChunk_t));
525*0Sstevel@tonic-gate 			ncp = cp;
526*0Sstevel@tonic-gate 			new_nchunks--;
527*0Sstevel@tonic-gate 		}
528*0Sstevel@tonic-gate 	}
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	npages = 0;
531*0Sstevel@tonic-gate 	for (i = 0; i < new_nchunks; i++)
532*0Sstevel@tonic-gate 		npages += p2o->p2o_mchunks[i].Memc_Size;
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	p2o->p2o_memtotal.Memt_NumChunks = new_nchunks;
535*0Sstevel@tonic-gate 	p2o->p2o_memtotal.Memt_NumPages = npages;
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate /*
539*0Sstevel@tonic-gate  * Mapin the the cpu's post2obp structure.
540*0Sstevel@tonic-gate  */
541*0Sstevel@tonic-gate static post2obp_info_t *
cpu_p2o_mapin(int cpuid)542*0Sstevel@tonic-gate cpu_p2o_mapin(int cpuid)
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate 	uint64_t	cpu_p2o_physaddr;
545*0Sstevel@tonic-gate 	uint32_t	cpu_p2o_offset;
546*0Sstevel@tonic-gate 	caddr_t		cvaddr;
547*0Sstevel@tonic-gate 	uint_t		num_pages;
548*0Sstevel@tonic-gate 	pfn_t		pfn;
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	ASSERT(cpu_sgnblkp[cpuid] != NULL);
551*0Sstevel@tonic-gate 	/*
552*0Sstevel@tonic-gate 	 * Construct the physical base address of the bbsram
553*0Sstevel@tonic-gate 	 * in PSI space associated with this cpu in question.
554*0Sstevel@tonic-gate 	 */
555*0Sstevel@tonic-gate 	cpu_p2o_offset = (uint32_t)cpu_sgnblkp[cpuid]->sigb_postconfig;
556*0Sstevel@tonic-gate 	if (cpu_p2o_offset == 0) {
557*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
558*0Sstevel@tonic-gate 			"cpu_p2o_mapin:%d: sigb_postconfig == NULL\n",
559*0Sstevel@tonic-gate 			cpuid);
560*0Sstevel@tonic-gate 		return (NULL);
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 	cpu_p2o_physaddr = (STARFIRE_UPAID2UPS(cpuid) | STARFIRE_PSI_BASE) +
563*0Sstevel@tonic-gate 				(uint64_t)cpu_p2o_offset;
564*0Sstevel@tonic-gate 	cpu_p2o_offset = (uint32_t)(cpu_p2o_physaddr & MMU_PAGEOFFSET);
565*0Sstevel@tonic-gate 	cpu_p2o_physaddr -= (uint64_t)cpu_p2o_offset;
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	/*
568*0Sstevel@tonic-gate 	 * cpu_p2o_physaddr = Beginning of page containing p2o.
569*0Sstevel@tonic-gate 	 * cpu_p2o_offset   = Offset within page where p2o starts.
570*0Sstevel@tonic-gate 	 */
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	pfn = (pfn_t)(cpu_p2o_physaddr >> MMU_PAGESHIFT);
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	num_pages = mmu_btopr(cpu_p2o_offset + sizeof (post2obp_info_t));
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	/*
577*0Sstevel@tonic-gate 	 * Map in the post2obp structure.
578*0Sstevel@tonic-gate 	 */
579*0Sstevel@tonic-gate 	cvaddr = vmem_alloc(heap_arena, ptob(num_pages), VM_SLEEP);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	hat_devload(kas.a_hat, cvaddr, ptob(num_pages),
582*0Sstevel@tonic-gate 	    pfn, PROT_READ | PROT_WRITE, HAT_LOAD_LOCK);
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate 	return ((post2obp_info_t *)(cvaddr + (ulong_t)cpu_p2o_offset));
585*0Sstevel@tonic-gate }
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate static void
cpu_p2o_mapout(int cpuid,post2obp_info_t * p2o)588*0Sstevel@tonic-gate cpu_p2o_mapout(int cpuid, post2obp_info_t *p2o)
589*0Sstevel@tonic-gate {
590*0Sstevel@tonic-gate 	ulong_t		cvaddr, num_pages;
591*0Sstevel@tonic-gate 	uint32_t	cpu_p2o_offset;
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	ASSERT(cpu_sgnblkp[cpuid] != NULL);
594*0Sstevel@tonic-gate 
595*0Sstevel@tonic-gate 	cpu_p2o_offset = (uint32_t)cpu_sgnblkp[cpuid]->sigb_postconfig;
596*0Sstevel@tonic-gate 	if (cpu_p2o_offset == 0) {
597*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
598*0Sstevel@tonic-gate 			"cpu_p2o_mapout:%d: sigb_postconfig == NULL\n",
599*0Sstevel@tonic-gate 			cpuid);
600*0Sstevel@tonic-gate 		return;
601*0Sstevel@tonic-gate 	}
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	cpu_p2o_offset = (uint32_t)(((STARFIRE_UPAID2UPS(cpuid) |
604*0Sstevel@tonic-gate 					STARFIRE_PSI_BASE) +
605*0Sstevel@tonic-gate 					(uint64_t)cpu_p2o_offset) &
606*0Sstevel@tonic-gate 					MMU_PAGEOFFSET);
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	num_pages = mmu_btopr(cpu_p2o_offset + sizeof (post2obp_info_t));
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	cvaddr = (ulong_t)p2o - cpu_p2o_offset;
611*0Sstevel@tonic-gate 	if (cvaddr & MMU_PAGEOFFSET) {
612*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
613*0Sstevel@tonic-gate 			"cpu_p2o_mapout:%d: cvaddr (0x%x) not on page "
614*0Sstevel@tonic-gate 			"boundary\n",
615*0Sstevel@tonic-gate 			cpuid, (uint_t)cvaddr);
616*0Sstevel@tonic-gate 		return;
617*0Sstevel@tonic-gate 	}
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	hat_unload(kas.a_hat, (caddr_t)cvaddr, ptob(num_pages),
620*0Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
621*0Sstevel@tonic-gate 	vmem_free(heap_arena, (caddr_t)cvaddr, ptob(num_pages));
622*0Sstevel@tonic-gate }
623