17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5e12a8a13Ssusans  * Common Development and Distribution License (the "License").
6e12a8a13Ssusans  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2107d06da5SSurya Prakki 
227c478bd9Sstevel@tonic-gate /*
2307d06da5SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
327c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/note.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/i2c_client.h>
407c478bd9Sstevel@tonic-gate #include <sys/cherrystone.h>
417c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
427c478bd9Sstevel@tonic-gate #include <sys/promif.h>
437c478bd9Sstevel@tonic-gate #include <vm/page.h>
447c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
457c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Cherrystone Keyswitch Information */
487c478bd9Sstevel@tonic-gate #define	CHERRY_KEY_POLL_PORT	3
497c478bd9Sstevel@tonic-gate #define	CHERRY_KEY_POLL_BIT	2
507c478bd9Sstevel@tonic-gate #define	CHERRY_KEY_POLL_INTVL	10
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	SHARED_PCF8584_PATH "/pci@9,700000/ebus@1/i2c@1,2e/nvram@4,a4"
537c478bd9Sstevel@tonic-gate static dev_info_t *shared_pcf8584_dip;
547c478bd9Sstevel@tonic-gate static kmutex_t cherry_pcf8584_mutex;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static	boolean_t	key_locked_bit;
577c478bd9Sstevel@tonic-gate static	clock_t		keypoll_timeout_hz;
587c478bd9Sstevel@tonic-gate 
5913bb8906Slm /*
6013bb8906Slm  * Table that maps memory slices to a specific memnode.
6113bb8906Slm  */
6213bb8906Slm int slice_to_memnode[CHERRYSTONE_MAX_SLICE];
6313bb8906Slm 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * For software memory interleaving support.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static void update_mem_bounds(int, int, int, uint64_t, uint64_t);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static uint64_t
707c478bd9Sstevel@tonic-gate slice_table[CHERRYSTONE_SBD_SLOTS][CHERRYSTONE_CPUS_PER_BOARD]
717c478bd9Sstevel@tonic-gate 		[CHERRYSTONE_BANKS_PER_MC][2];
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	SLICE_PA	0
747c478bd9Sstevel@tonic-gate #define	SLICE_SPAN	1
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* Function prototypes */
777c478bd9Sstevel@tonic-gate int (*p2get_mem_unum)(int, uint64_t, char *, int, int *);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int (*cherry_ssc050_get_port_bit) (dev_info_t *, int, int, uint8_t *, int);
807c478bd9Sstevel@tonic-gate extern	void (*abort_seq_handler)();
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static	int cherry_dev_search(dev_info_t *, void *);
837c478bd9Sstevel@tonic-gate static	void keyswitch_poll(void *);
847c478bd9Sstevel@tonic-gate static	void cherry_abort_seq_handler(char *msg);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* Function definitions from this point forward. */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int
set_platform_tsb_spares()897c478bd9Sstevel@tonic-gate set_platform_tsb_spares()
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate void
startup_platform(void)957c478bd9Sstevel@tonic-gate startup_platform(void)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * Disable an active h/w watchdog timer
997c478bd9Sstevel@tonic-gate 	 * upon exit to OBP.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	extern int disable_watchdog_on_exit;
1027c478bd9Sstevel@tonic-gate 	disable_watchdog_on_exit = 1;
1037c478bd9Sstevel@tonic-gate 
104*7d94095bSToomas Soome 	mutex_init(&cherry_pcf8584_mutex, NULL, MUTEX_ADAPTIVE, NULL);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #pragma weak mmu_init_large_pages
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate void
set_platform_defaults(void)1107c478bd9Sstevel@tonic-gate set_platform_defaults(void)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	extern void mmu_init_large_pages(size_t);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((mmu_page_sizes == max_mmu_page_sizes) &&
115e12a8a13Ssusans 	    (mmu_ism_pagesize != DEFAULT_ISM_PAGESIZE)) {
1167c478bd9Sstevel@tonic-gate 		if (&mmu_init_large_pages)
1177c478bd9Sstevel@tonic-gate 			mmu_init_large_pages(mmu_ism_pagesize);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
load_platform_modules(void)1227c478bd9Sstevel@tonic-gate load_platform_modules(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	if (modload("drv", "pmc") < 0) {
1257c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pmc driver failed to load");
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate void
load_platform_drivers(void)1307c478bd9Sstevel@tonic-gate load_platform_drivers(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	char		**drv;
1337c478bd9Sstevel@tonic-gate 	dev_info_t	*i2cnexus_dip;
1347c478bd9Sstevel@tonic-gate 	dev_info_t	*keysw_dip = NULL;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	static char	*boot_time_drivers[] = {
1377c478bd9Sstevel@tonic-gate 		"todds1287",
1387c478bd9Sstevel@tonic-gate 		"mc-us3",
1397c478bd9Sstevel@tonic-gate 		"ssc050",
1407c478bd9Sstevel@tonic-gate 		NULL
1417c478bd9Sstevel@tonic-gate 	};
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	for (drv = boot_time_drivers; *drv; drv++) {
1447c478bd9Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(*drv) != DDI_SUCCESS)
1457c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Failed to install \"%s\" driver.",
146986fd29aSsetje 			    *drv);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * mc-us3 and ssc050 must stay loaded for plat_get_mem_unum()
1517c478bd9Sstevel@tonic-gate 	 * and keyswitch_poll()
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	(void) ddi_hold_driver(ddi_name_to_major("mc-us3"));
1547c478bd9Sstevel@tonic-gate 	(void) ddi_hold_driver(ddi_name_to_major("ssc050"));
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/* Gain access into the ssc050_get_port function */
1577c478bd9Sstevel@tonic-gate 	cherry_ssc050_get_port_bit = (int (*) (dev_info_t *, int, int,
158986fd29aSsetje 	    uint8_t *, int)) modgetsymvalue("ssc050_get_port_bit", 0);
1597c478bd9Sstevel@tonic-gate 	if (cherry_ssc050_get_port_bit == NULL) {
1607c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "cannot find ssc050_get_port_bit");
1617c478bd9Sstevel@tonic-gate 		return;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	e_ddi_walk_driver("i2c-ssc050", cherry_dev_search, (void *)&keysw_dip);
1657c478bd9Sstevel@tonic-gate 	ASSERT(keysw_dip != NULL);
1667c478bd9Sstevel@tonic-gate 
1673611c136Smb 	/*
1683611c136Smb 	 * prevent detach of i2c-ssc050
1693611c136Smb 	 */
1703611c136Smb 	e_ddi_hold_devi(keysw_dip);
1713611c136Smb 
1727c478bd9Sstevel@tonic-gate 	keypoll_timeout_hz = drv_usectohz(10 * MICROSEC);
1737c478bd9Sstevel@tonic-gate 	keyswitch_poll(keysw_dip);
1747c478bd9Sstevel@tonic-gate 	abort_seq_handler = cherry_abort_seq_handler;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Figure out which pcf8584_dip is shared with OBP for the nvram
1787c478bd9Sstevel@tonic-gate 	 * device, so the lock can be acquired.
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	i2cnexus_dip = e_ddi_hold_devi_by_path(SHARED_PCF8584_PATH, 0);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	ASSERT(i2cnexus_dip != NULL);
1847c478bd9Sstevel@tonic-gate 	shared_pcf8584_dip = ddi_get_parent(i2cnexus_dip);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	ndi_hold_devi(shared_pcf8584_dip);
1877c478bd9Sstevel@tonic-gate 	ndi_rele_devi(i2cnexus_dip);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static int
cherry_dev_search(dev_info_t * dip,void * arg)1917c478bd9Sstevel@tonic-gate cherry_dev_search(dev_info_t *dip, void *arg)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	int		*dev_regs; /* Info about where the device is. */
1947c478bd9Sstevel@tonic-gate 	uint_t		len;
1957c478bd9Sstevel@tonic-gate 	int		err;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (strcmp(ddi_binding_name(dip), "i2c-ssc050") != 0)
1987c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
201986fd29aSsetje 	    DDI_PROP_DONTPASS, "reg", &dev_regs, &len);
2027c478bd9Sstevel@tonic-gate 	if (err != DDI_PROP_SUCCESS) {
2037c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * regs[0] contains the bus number and regs[1]
2077c478bd9Sstevel@tonic-gate 	 * contains the device address of the i2c device.
2087c478bd9Sstevel@tonic-gate 	 * 0x82 is the device address of the i2c device
2097c478bd9Sstevel@tonic-gate 	 * from which  the key switch position is read.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if (dev_regs[0] == 0 && dev_regs[1] == 0x82) {
2127c478bd9Sstevel@tonic-gate 		*((dev_info_t **)arg) = dip;
2137c478bd9Sstevel@tonic-gate 		ddi_prop_free(dev_regs);
2147c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	ddi_prop_free(dev_regs);
2177c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static void
keyswitch_poll(void * arg)2217c478bd9Sstevel@tonic-gate keyswitch_poll(void *arg)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = arg;
2247c478bd9Sstevel@tonic-gate 	uchar_t	port_byte;
2257c478bd9Sstevel@tonic-gate 	int	port = CHERRY_KEY_POLL_PORT;
2267c478bd9Sstevel@tonic-gate 	int	bit = CHERRY_KEY_POLL_BIT;
2277c478bd9Sstevel@tonic-gate 	int	err;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	err = cherry_ssc050_get_port_bit(dip, port, bit,
230986fd29aSsetje 	    &port_byte, I2C_NOSLEEP);
2317c478bd9Sstevel@tonic-gate 	if (err != 0) {
2323611c136Smb 		cmn_err(CE_WARN, "keyswitch polling disabled: "
233986fd29aSsetje 		    "errno=%d while reading ssc050", err);
2347c478bd9Sstevel@tonic-gate 		return;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	key_locked_bit = (boolean_t)((port_byte & 0x1));
23807d06da5SSurya Prakki 	(void) timeout(keyswitch_poll, (caddr_t)dip, keypoll_timeout_hz);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static void
cherry_abort_seq_handler(char * msg)2427c478bd9Sstevel@tonic-gate cherry_abort_seq_handler(char *msg)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	if (key_locked_bit == 0)
2457c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "KEY in LOCKED position, "
246986fd29aSsetje 		    "ignoring debug enter sequence");
2477c478bd9Sstevel@tonic-gate 	else  {
2487c478bd9Sstevel@tonic-gate 		debug_enter(msg);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2547c478bd9Sstevel@tonic-gate int
plat_cpu_poweron(struct cpu * cp)2557c478bd9Sstevel@tonic-gate plat_cpu_poweron(struct cpu *cp)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	return (ENOTSUP);	/* not supported on this platform */
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2617c478bd9Sstevel@tonic-gate int
plat_cpu_poweroff(struct cpu * cp)2627c478bd9Sstevel@tonic-gate plat_cpu_poweroff(struct cpu *cp)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	return (ENOTSUP);	/* not supported on this platform */
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * Given a pfn, return the board and beginning/end of the page's
2697c478bd9Sstevel@tonic-gate  * memory controller's address range.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate static int
plat_discover_slice(pfn_t pfn,pfn_t * first,pfn_t * last)2727c478bd9Sstevel@tonic-gate plat_discover_slice(pfn_t pfn, pfn_t *first, pfn_t *last)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	int bd, cpu, bank;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	for (bd = 0; bd < CHERRYSTONE_SBD_SLOTS; bd++) {
2777c478bd9Sstevel@tonic-gate 		for (cpu = 0; cpu < CHERRYSTONE_CPUS_PER_BOARD; cpu++) {
2787c478bd9Sstevel@tonic-gate 			for (bank = 0; bank < CHERRYSTONE_BANKS_PER_MC;
279986fd29aSsetje 			    bank++) {
2807c478bd9Sstevel@tonic-gate 				uint64_t *slice = slice_table[bd][cpu][bank];
2817c478bd9Sstevel@tonic-gate 				uint64_t base = btop(slice[SLICE_PA]);
2827c478bd9Sstevel@tonic-gate 				uint64_t len = btop(slice[SLICE_SPAN]);
2837c478bd9Sstevel@tonic-gate 				if (len && pfn >= base && pfn < (base + len)) {
2847c478bd9Sstevel@tonic-gate 					*first = base;
2857c478bd9Sstevel@tonic-gate 					*last = base + len - 1;
2867c478bd9Sstevel@tonic-gate 					return (bd);
2877c478bd9Sstevel@tonic-gate 				}
2887c478bd9Sstevel@tonic-gate 			}
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	panic("plat_discover_slice: no slice for pfn 0x%lx\n", pfn);
2927c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
29513bb8906Slm /*ARGSUSED*/
2967c478bd9Sstevel@tonic-gate void
plat_freelist_process(int mnode)2977c478bd9Sstevel@tonic-gate plat_freelist_process(int mnode)
298*7d94095bSToomas Soome {
299*7d94095bSToomas Soome }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * Called for each board/cpu/PA range detected in plat_fill_mc().
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static void
update_mem_bounds(int boardid,int cpuid,int bankid,uint64_t base,uint64_t size)3057c478bd9Sstevel@tonic-gate update_mem_bounds(int boardid, int cpuid, int bankid,
306*7d94095bSToomas Soome     uint64_t base, uint64_t size)
3077c478bd9Sstevel@tonic-gate {
30813bb8906Slm 	uint64_t	end;
30913bb8906Slm 	int		mnode;
31013bb8906Slm 
3117c478bd9Sstevel@tonic-gate 	slice_table[boardid][cpuid][bankid][SLICE_PA] = base;
3127c478bd9Sstevel@tonic-gate 	slice_table[boardid][cpuid][bankid][SLICE_SPAN] = size;
31313bb8906Slm 
31413bb8906Slm 	end = base + size - 1;
31513bb8906Slm 
31613bb8906Slm 	/*
31713bb8906Slm 	 * First see if this board already has a memnode associated
31813bb8906Slm 	 * with it.  If not, see if this slice has a memnode.  This
31913bb8906Slm 	 * covers the cases where a single slice covers multiple
32013bb8906Slm 	 * boards (cross-board interleaving) and where a single
32113bb8906Slm 	 * board has multiple slices (1+GB DIMMs).
32213bb8906Slm 	 */
32313bb8906Slm 	if ((mnode = plat_lgrphand_to_mem_node(boardid)) == -1) {
32413bb8906Slm 		if ((mnode = slice_to_memnode[PA_2_SLICE(base)]) == -1)
32513bb8906Slm 			mnode = mem_node_alloc();
32613bb8906Slm 
32713bb8906Slm 		ASSERT(mnode >= 0);
32813bb8906Slm 		ASSERT(mnode < MAX_MEM_NODES);
32913bb8906Slm 		plat_assign_lgrphand_to_mem_node(boardid, mnode);
33013bb8906Slm 	}
33113bb8906Slm 
33213bb8906Slm 	base = P2ALIGN(base, (1ul << PA_SLICE_SHIFT));
33313bb8906Slm 
33413bb8906Slm 	while (base < end) {
33513bb8906Slm 		slice_to_memnode[PA_2_SLICE(base)] = mnode;
33613bb8906Slm 		base += (1ul << PA_SLICE_SHIFT);
33713bb8906Slm 	}
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * Dynamically detect memory slices in the system by decoding
3427c478bd9Sstevel@tonic-gate  * the cpu memory decoder registers at boot time.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate void
plat_fill_mc(pnode_t nodeid)345fa9e4066Sahrens plat_fill_mc(pnode_t nodeid)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	uint64_t	mc_addr, saf_addr;
3487c478bd9Sstevel@tonic-gate 	uint64_t	mc_decode[CHERRYSTONE_BANKS_PER_MC];
3497c478bd9Sstevel@tonic-gate 	uint64_t	base, size;
3507c478bd9Sstevel@tonic-gate 	uint64_t	saf_mask;
3517c478bd9Sstevel@tonic-gate 	uint64_t	offset;
3527c478bd9Sstevel@tonic-gate 	uint32_t	regs[4];
3537c478bd9Sstevel@tonic-gate 	int		len;
3547c478bd9Sstevel@tonic-gate 	int		local_mc;
3557c478bd9Sstevel@tonic-gate 	int		portid;
3567c478bd9Sstevel@tonic-gate 	int		boardid;
3577c478bd9Sstevel@tonic-gate 	int		cpuid;
3587c478bd9Sstevel@tonic-gate 	int		i;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if ((prom_getprop(nodeid, "portid", (caddr_t)&portid) < 0) ||
3617c478bd9Sstevel@tonic-gate 	    (portid == -1))
3627c478bd9Sstevel@tonic-gate 		return;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * Decode the board number from the MC portid.  Assumes
3667c478bd9Sstevel@tonic-gate 	 * portid == safari agentid.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	boardid = CHERRYSTONE_GETSLOT(portid);
3697c478bd9Sstevel@tonic-gate 	cpuid = CHERRYSTONE_GETSID(portid);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * The "reg" property returns 4 32-bit values. The first two are
3737c478bd9Sstevel@tonic-gate 	 * combined to form a 64-bit address.  The second two are for a
3747c478bd9Sstevel@tonic-gate 	 * 64-bit size, but we don't actually need to look at that value.
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	len = prom_getproplen(nodeid, "reg");
3777c478bd9Sstevel@tonic-gate 	if (len != (sizeof (uint32_t) * 4)) {
3787c478bd9Sstevel@tonic-gate 		prom_printf("Warning: malformed 'reg' property\n");
3797c478bd9Sstevel@tonic-gate 		return;
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	if (prom_getprop(nodeid, "reg", (caddr_t)regs) < 0)
3827c478bd9Sstevel@tonic-gate 		return;
3837c478bd9Sstevel@tonic-gate 	mc_addr = ((uint64_t)regs[0]) << 32;
3847c478bd9Sstevel@tonic-gate 	mc_addr |= (uint64_t)regs[1];
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * Figure out whether the memory controller we are examining
3887c478bd9Sstevel@tonic-gate 	 * belongs to this CPU or a different one.
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	saf_addr = lddsafaddr(8);
3917c478bd9Sstevel@tonic-gate 	saf_mask = (uint64_t)SAF_MASK;
3927c478bd9Sstevel@tonic-gate 	if ((mc_addr & saf_mask) == saf_addr)
3937c478bd9Sstevel@tonic-gate 		local_mc = 1;
3947c478bd9Sstevel@tonic-gate 	else
3957c478bd9Sstevel@tonic-gate 		local_mc = 0;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	for (i = 0; i < CHERRYSTONE_BANKS_PER_MC; i++) {
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * Memory decode masks are at offsets 0x10 - 0x28.
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		offset = 0x10 + (i << 3);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		/*
4047c478bd9Sstevel@tonic-gate 		 * If the memory controller is local to this CPU, we use
4057c478bd9Sstevel@tonic-gate 		 * the special ASI to read the decode registers.
4067c478bd9Sstevel@tonic-gate 		 * Otherwise, we load the values from a magic address in
4077c478bd9Sstevel@tonic-gate 		 * I/O space.
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		if (local_mc)
4107c478bd9Sstevel@tonic-gate 			mc_decode[i] = lddmcdecode(offset);
4117c478bd9Sstevel@tonic-gate 		else
4127c478bd9Sstevel@tonic-gate 			mc_decode[i] = lddphysio(mc_addr | offset);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		/*
4157c478bd9Sstevel@tonic-gate 		 * If the upper bit is set, we have a valid mask
4167c478bd9Sstevel@tonic-gate 		 */
4177c478bd9Sstevel@tonic-gate 		if ((int64_t)mc_decode[i] < 0) {
4187c478bd9Sstevel@tonic-gate 			/*
4197c478bd9Sstevel@tonic-gate 			 * The memory decode register is a bitmask field,
4207c478bd9Sstevel@tonic-gate 			 * so we can decode that into both a base and
4217c478bd9Sstevel@tonic-gate 			 * a span.
4227c478bd9Sstevel@tonic-gate 			 */
4237c478bd9Sstevel@tonic-gate 			base = MC_BASE(mc_decode[i]) << PHYS2UM_SHIFT;
4247c478bd9Sstevel@tonic-gate 			size = MC_UK2SPAN(mc_decode[i]);
4257c478bd9Sstevel@tonic-gate 			update_mem_bounds(boardid, cpuid, i, base, size);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
43013bb8906Slm /*
43113bb8906Slm  * This routine is run midway through the boot process.  By the time we get
43213bb8906Slm  * here, we know about all the active CPU boards in the system, and we have
43313bb8906Slm  * extracted information about each board's memory from the memory
43413bb8906Slm  * controllers.  We have also figured out which ranges of memory will be
43513bb8906Slm  * assigned to which memnodes, so we walk the slice table to build the table
43613bb8906Slm  * of memnodes.
43713bb8906Slm  */
43813bb8906Slm /* ARGSUSED */
43913bb8906Slm void
plat_build_mem_nodes(prom_memlist_t * list,size_t nelems)440986fd29aSsetje plat_build_mem_nodes(prom_memlist_t *list, size_t  nelems)
44113bb8906Slm {
44213bb8906Slm 	int	slice;
44313bb8906Slm 	pfn_t	basepfn;
44413bb8906Slm 	pgcnt_t npgs;
44513bb8906Slm 
44613bb8906Slm 	mem_node_pfn_shift = PFN_SLICE_SHIFT;
44713bb8906Slm 	mem_node_physalign = (1ull << PA_SLICE_SHIFT);
44813bb8906Slm 	npgs = 1ull << PFN_SLICE_SHIFT;
44913bb8906Slm 
45013bb8906Slm 	for (slice = 0; slice < CHERRYSTONE_MAX_SLICE; slice++) {
45113bb8906Slm 		if (slice_to_memnode[slice] == -1)
45213bb8906Slm 			continue;
45313bb8906Slm 		basepfn = (uint64_t)slice << PFN_SLICE_SHIFT;
45413bb8906Slm 		mem_node_add_slice(basepfn, basepfn + npgs - 1);
45513bb8906Slm 	}
45613bb8906Slm }
45713bb8906Slm 
45813bb8906Slm 
45913bb8906Slm 
46013bb8906Slm /*
46113bb8906Slm  * Cherrystone support for lgroups.
46213bb8906Slm  *
46313bb8906Slm  * On Cherrystone, an lgroup platform handle == slot number.
46413bb8906Slm  *
46513bb8906Slm  * Mappings between lgroup handles and memnodes are managed
46613bb8906Slm  * in addition to mappings between memory slices and memnodes
46713bb8906Slm  * to support cross-board interleaving as well as multiple
46813bb8906Slm  * slices per board (e.g. >1GB DIMMs). The initial mapping
46913bb8906Slm  * of memnodes to lgroup handles is determined at boot time.
47013bb8906Slm  */
47113bb8906Slm 
47213bb8906Slm int
plat_pfn_to_mem_node(pfn_t pfn)47313bb8906Slm plat_pfn_to_mem_node(pfn_t pfn)
47413bb8906Slm {
47513bb8906Slm 	return (slice_to_memnode[PFN_2_SLICE(pfn)]);
47613bb8906Slm }
47713bb8906Slm 
47813bb8906Slm /*
47913bb8906Slm  * Return the platform handle for the lgroup containing the given CPU
48013bb8906Slm  *
48113bb8906Slm  * For Cherrystone, lgroup platform handle == slot/board number
48213bb8906Slm  */
48313bb8906Slm lgrp_handle_t
plat_lgrp_cpu_to_hand(processorid_t id)48413bb8906Slm plat_lgrp_cpu_to_hand(processorid_t id)
48513bb8906Slm {
48613bb8906Slm 	return (CHERRYSTONE_GETSLOT(id));
48713bb8906Slm }
48813bb8906Slm 
48913bb8906Slm /*
49013bb8906Slm  * Platform specific lgroup initialization
49113bb8906Slm  */
49213bb8906Slm void
plat_lgrp_init(void)49313bb8906Slm plat_lgrp_init(void)
49413bb8906Slm {
49513bb8906Slm 	int i;
49613bb8906Slm 
49713bb8906Slm 	/*
49813bb8906Slm 	 * Initialize lookup tables to invalid values so we catch
49913bb8906Slm 	 * any illegal use of them.
50013bb8906Slm 	 */
50113bb8906Slm 	for (i = 0; i < CHERRYSTONE_MAX_SLICE; i++) {
50213bb8906Slm 		slice_to_memnode[i] = -1;
50313bb8906Slm 	}
50413bb8906Slm }
50513bb8906Slm 
50613bb8906Slm /*
50713bb8906Slm  * Return latency between "from" and "to" lgroups
50813bb8906Slm  *
50913bb8906Slm  * This latency number can only be used for relative comparison
51013bb8906Slm  * between lgroups on the running system, cannot be used across platforms,
51113bb8906Slm  * and may not reflect the actual latency.  It is platform and implementation
51213bb8906Slm  * specific, so platform gets to decide its value.  It would be nice if the
51313bb8906Slm  * number was at least proportional to make comparisons more meaningful though.
51413bb8906Slm  * NOTE: The numbers below are supposed to be load latencies for uncached
51513bb8906Slm  * memory divided by 10.
51613bb8906Slm  */
51713bb8906Slm int
plat_lgrp_latency(lgrp_handle_t from,lgrp_handle_t to)51813bb8906Slm plat_lgrp_latency(lgrp_handle_t from, lgrp_handle_t to)
51913bb8906Slm {
52013bb8906Slm 	/*
52113bb8906Slm 	 * Return min remote latency when there are more than two lgroups
52213bb8906Slm 	 * (root and child) and getting latency between two different lgroups
52313bb8906Slm 	 * or root is involved
52413bb8906Slm 	 */
52513bb8906Slm 	if (lgrp_optimizations() && (from != to ||
52613bb8906Slm 	    from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE))
52713bb8906Slm 		return (21);
52813bb8906Slm 	else
52913bb8906Slm 		return (19);
53013bb8906Slm }
53113bb8906Slm 
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate  * No platform drivers on this platform
5347c478bd9Sstevel@tonic-gate  */
5357c478bd9Sstevel@tonic-gate char *platform_module_list[] = {
5367c478bd9Sstevel@tonic-gate 	(char *)0
5377c478bd9Sstevel@tonic-gate };
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5407c478bd9Sstevel@tonic-gate void
plat_tod_fault(enum tod_fault_type tod_bad)5417c478bd9Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5467c478bd9Sstevel@tonic-gate int
plat_get_mem_unum(int synd_code,uint64_t flt_addr,int flt_bus_id,int flt_in_memory,ushort_t flt_status,char * buf,int buflen,int * lenp)5477c478bd9Sstevel@tonic-gate plat_get_mem_unum(int synd_code, uint64_t flt_addr, int flt_bus_id,
5487c478bd9Sstevel@tonic-gate     int flt_in_memory, ushort_t flt_status, char *buf, int buflen, int *lenp)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	if (flt_in_memory && (p2get_mem_unum != NULL))
5517c478bd9Sstevel@tonic-gate 		return (p2get_mem_unum(synd_code, P2ALIGN(flt_addr, 8),
552986fd29aSsetje 		    buf, buflen, lenp));
5537c478bd9Sstevel@tonic-gate 	else
5547c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * This platform hook gets called from mc_add_mem_unum_label() in the mc-us3
5597c478bd9Sstevel@tonic-gate  * driver giving each platform the opportunity to add platform
5607c478bd9Sstevel@tonic-gate  * specific label information to the unum for ECC error logging purposes.
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate void
plat_add_mem_unum_label(char * unum,int mcid,int bank,int dimm)5637c478bd9Sstevel@tonic-gate plat_add_mem_unum_label(char *unum, int mcid, int bank, int dimm)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(bank, dimm))
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	char board = CHERRYSTONE_GETSLOT_LABEL(mcid);
5687c478bd9Sstevel@tonic-gate 	char old_unum[UNUM_NAMLEN];
5697c478bd9Sstevel@tonic-gate 
57007d06da5SSurya Prakki 	(void) strcpy(old_unum, unum);
57107d06da5SSurya Prakki 	(void) snprintf(unum, UNUM_NAMLEN, "Slot %c: %s", board, old_unum);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate int
plat_get_cpu_unum(int cpuid,char * buf,int buflen,int * lenp)5757c478bd9Sstevel@tonic-gate plat_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	char board = CHERRYSTONE_GETSLOT_LABEL(cpuid);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (snprintf(buf, buflen, "Slot %c", board) >= buflen) {
5807c478bd9Sstevel@tonic-gate 		return (ENOSPC);
5817c478bd9Sstevel@tonic-gate 	} else {
5827c478bd9Sstevel@tonic-gate 		*lenp = strlen(buf);
5837c478bd9Sstevel@tonic-gate 		return (0);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * Cherrystone's BBC pcf8584 controller is used by both OBP and the OS's i2c
5897c478bd9Sstevel@tonic-gate  * drivers.  The 'eeprom' command executes OBP code to handle property requests.
5907c478bd9Sstevel@tonic-gate  * If eeprom didn't do this, or if the controllers were partitioned so that all
5917c478bd9Sstevel@tonic-gate  * devices on a given controller were driven by either OBP or the OS, this
5927c478bd9Sstevel@tonic-gate  * wouldn't be necessary.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * Note that getprop doesn't have the same issue as it reads from cached
5957c478bd9Sstevel@tonic-gate  * memory in OBP.
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * Common locking enter code
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate void
plat_setprop_enter(void)6027c478bd9Sstevel@tonic-gate plat_setprop_enter(void)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	mutex_enter(&cherry_pcf8584_mutex);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Common locking exit code
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate void
plat_setprop_exit(void)6117c478bd9Sstevel@tonic-gate plat_setprop_exit(void)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	mutex_exit(&cherry_pcf8584_mutex);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * Called by pcf8584 driver
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate void
plat_shared_i2c_enter(dev_info_t * i2cnexus_dip)6207c478bd9Sstevel@tonic-gate plat_shared_i2c_enter(dev_info_t *i2cnexus_dip)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	if (i2cnexus_dip == shared_pcf8584_dip) {
6237c478bd9Sstevel@tonic-gate 		plat_setprop_enter();
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * Called by pcf8584 driver
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate void
plat_shared_i2c_exit(dev_info_t * i2cnexus_dip)6317c478bd9Sstevel@tonic-gate plat_shared_i2c_exit(dev_info_t *i2cnexus_dip)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	if (i2cnexus_dip == shared_pcf8584_dip) {
6347c478bd9Sstevel@tonic-gate 		plat_setprop_exit();
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate }
637