xref: /onnv-gate/usr/src/uts/sun4u/starfire/io/memctrl.c (revision 931:92fdf4004904)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23789Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Starfire Memory Controller specific routines.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <sys/dditypes.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/sunndi.h>
410Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
420Sstevel@tonic-gate #include <sys/promif.h>
430Sstevel@tonic-gate #include <sys/machsystm.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/starfire.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate struct mc_dimm_table {
480Sstevel@tonic-gate 	int	mc_type;
490Sstevel@tonic-gate 	int	mc_module_size;		/* module size in MB */
500Sstevel@tonic-gate };
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static struct mc_dimm_table dimmsize_table[] = {
530Sstevel@tonic-gate 	{ 4,	8 },
540Sstevel@tonic-gate 	{ 6,	8 },
550Sstevel@tonic-gate 	{ 11,	32 },
560Sstevel@tonic-gate 	{ 15,	128 },
570Sstevel@tonic-gate 	{ 0,	0 }
580Sstevel@tonic-gate };
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	MC_MB(mb) ((mb) * 1048576ull)
610Sstevel@tonic-gate 
620Sstevel@tonic-gate struct mc_seg_size {
630Sstevel@tonic-gate 	uint_t		seg_mask;
640Sstevel@tonic-gate 	uint64_t	seg_size;
650Sstevel@tonic-gate };
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct mc_seg_size mc_seg_table[] = {
680Sstevel@tonic-gate 	{ 0x7f,	MC_MB(64)	},
690Sstevel@tonic-gate 	{ 0x7e,	MC_MB(128)	},
700Sstevel@tonic-gate 	{ 0x7c,	MC_MB(256)	},
710Sstevel@tonic-gate 	{ 0x78,	MC_MB(512)	},
720Sstevel@tonic-gate 	{ 0x70,	MC_MB(1024)	},
730Sstevel@tonic-gate 	{ 0x60,	MC_MB(2048)	},
740Sstevel@tonic-gate 	{ 0x40,	MC_MB(4096)	},
750Sstevel@tonic-gate 	{ 0,	0		}
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Alignment of memory between MC's.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate uint64_t
mc_get_mem_alignment()820Sstevel@tonic-gate mc_get_mem_alignment()
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	return (STARFIRE_MC_MEMBOARD_ALIGNMENT);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate uint64_t
mc_get_asr_addr(pnode_t nodeid)88789Sahrens mc_get_asr_addr(pnode_t nodeid)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	int		rlen;
910Sstevel@tonic-gate 	uint64_t	psi_addr;
920Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
950Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
960Sstevel@tonic-gate 		return ((uint64_t)-1);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
990Sstevel@tonic-gate 		return ((uint64_t)-1);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
1020Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	return (STARFIRE_MC_ASR_ADDR(psi_addr));
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate uint64_t
mc_get_idle_addr(pnode_t nodeid)108789Sahrens mc_get_idle_addr(pnode_t nodeid)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	int		rlen;
1110Sstevel@tonic-gate 	uint64_t	psi_addr;
1120Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
1150Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
1160Sstevel@tonic-gate 		return ((uint64_t)-1);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
1190Sstevel@tonic-gate 		return ((uint64_t)-1);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
1220Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	return (STARFIRE_MC_IDLE_ADDR(psi_addr));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate int
mc_get_dimm_size(pnode_t nodeid)128789Sahrens mc_get_dimm_size(pnode_t nodeid)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	uint64_t	psi_addr;
1310Sstevel@tonic-gate 	uint_t		dimmtype;
1320Sstevel@tonic-gate 	int		i, rlen;
1330Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
1360Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
1370Sstevel@tonic-gate 		return (-1);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
1400Sstevel@tonic-gate 		return (-1);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
1430Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
1440Sstevel@tonic-gate 	psi_addr = STARFIRE_MC_DIMMTYPE_ADDR(psi_addr);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
1470Sstevel@tonic-gate 		return (-1);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	dimmtype = ldphysio(psi_addr);
1500Sstevel@tonic-gate 	dimmtype &= STARFIRE_MC_DIMMSIZE_MASK;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	for (i = 0; dimmsize_table[i].mc_type != 0; i++)
1530Sstevel@tonic-gate 		if (dimmsize_table[i].mc_type == dimmtype)
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (dimmsize_table[i].mc_module_size);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate uint64_t
mc_get_alignment_mask(pnode_t nodeid)160789Sahrens mc_get_alignment_mask(pnode_t nodeid)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	uint64_t	psi_addr, seg_sz;
1630Sstevel@tonic-gate 	uint_t		mcreg, seg_sz_mask;
1640Sstevel@tonic-gate 	int		i, rlen;
1650Sstevel@tonic-gate 	struct sf_memunit_regspec	reg;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	rlen = prom_getproplen(nodeid, "reg");
1680Sstevel@tonic-gate 	if (rlen != sizeof (struct sf_memunit_regspec))
1690Sstevel@tonic-gate 		return (-1);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)&reg) < 0)
1720Sstevel@tonic-gate 		return (-1);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	psi_addr = ((uint64_t)reg.regspec_addr_hi) << 32;
1750Sstevel@tonic-gate 	psi_addr |= (uint64_t)reg.regspec_addr_lo;
1760Sstevel@tonic-gate 	psi_addr = STARFIRE_MC_ASR_ADDR(psi_addr);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
1790Sstevel@tonic-gate 		return (-1);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	mcreg = ldphysio(psi_addr);
1820Sstevel@tonic-gate 	seg_sz_mask = (mcreg & STARFIRE_MC_MASK_MASK) >> 8;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	for (i = 0; mc_seg_table[i].seg_size != 0; i++)
1850Sstevel@tonic-gate 		if (mc_seg_table[i].seg_mask == seg_sz_mask)
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (mc_seg_table[i].seg_size == 0)
1890Sstevel@tonic-gate 		seg_sz = mc_get_mem_alignment();
1900Sstevel@tonic-gate 	else
1910Sstevel@tonic-gate 		seg_sz = mc_seg_table[i].seg_size;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate #ifdef DEBUG
194*931Smathue 	printf("nodeid %x, mc asr addr %lx, val %x, seg_sz_mask %x, "
195*931Smathue 	    "seg_sz %lx\n", nodeid, psi_addr, mcreg, seg_sz_mask, seg_sz);
1960Sstevel@tonic-gate #endif /* DEBUG */
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	return (seg_sz - 1);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate int
mc_read_asr(pnode_t nodeid,uint_t * mcregp)202789Sahrens mc_read_asr(pnode_t nodeid, uint_t *mcregp)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	uint64_t	psi_addr;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	*mcregp = 0;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	psi_addr = mc_get_asr_addr(nodeid);
2090Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
2100Sstevel@tonic-gate 		return (-1);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	*mcregp = ldphysio(psi_addr);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	return (0);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate int
mc_write_asr(pnode_t nodeid,uint_t mcreg)218789Sahrens mc_write_asr(pnode_t nodeid, uint_t mcreg)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	uint_t		mcreg_rd;
2210Sstevel@tonic-gate 	uint64_t	psi_addr;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	psi_addr = mc_get_asr_addr(nodeid);
2240Sstevel@tonic-gate 	if (psi_addr == (uint64_t)-1)
2250Sstevel@tonic-gate 		return (-1);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	stphysio(psi_addr, mcreg);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	mcreg_rd = ldphysio(psi_addr);
2300Sstevel@tonic-gate 	ASSERT(mcreg_rd == mcreg);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	return ((mcreg_rd != mcreg) ? -1 : 0);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate uint64_t
mc_asr_to_pa(uint_t mcreg)2360Sstevel@tonic-gate mc_asr_to_pa(uint_t mcreg)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	uint64_t	pa, masr, addrmask, lowbitmask;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * Remove memory present bit.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	masr = (uint64_t)(mcreg & ~STARFIRE_MC_MEM_PRESENT_MASK);
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * Get mask for bits 32-26.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	lowbitmask = masr & (uint64_t)STARFIRE_MC_MASK_MASK;
2480Sstevel@tonic-gate 	lowbitmask <<= STARFIRE_MC_MASK_SHIFT;
2490Sstevel@tonic-gate 	addrmask = STARFIRE_MC_ADDR_HIBITS | lowbitmask;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	pa = (masr << STARFIRE_MC_BASE_SHIFT) & addrmask;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	return (pa);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate uint_t
mc_pa_to_asr(uint_t masr,uint64_t pa)2570Sstevel@tonic-gate mc_pa_to_asr(uint_t masr, uint64_t pa)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	uint64_t	addrmask, lowbitmask;
2600Sstevel@tonic-gate 	uint_t		base;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Get mask for bits 32-26.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	lowbitmask = masr & (uint64_t)STARFIRE_MC_MASK_MASK;
2660Sstevel@tonic-gate 	lowbitmask <<= STARFIRE_MC_MASK_SHIFT;
2670Sstevel@tonic-gate 	addrmask = STARFIRE_MC_ADDR_HIBITS | lowbitmask;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	base  = (pa & addrmask) >> STARFIRE_MC_BASE_SHIFT;
2700Sstevel@tonic-gate 	masr &= ~ STARFIRE_MC_MEM_BASEADDR_MASK;
2710Sstevel@tonic-gate 	masr |= base & STARFIRE_MC_MEM_BASEADDR_MASK;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	ASSERT(mc_asr_to_pa(masr) == pa);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	return (masr);
2760Sstevel@tonic-gate }
277