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
51966Sarutz * Common Development and Distribution License (the "License").
61966Sarutz * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
226803Spothier * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/conf.h>
280Sstevel@tonic-gate #include <sys/ddi.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <sys/sunddi.h>
310Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
320Sstevel@tonic-gate #include <sys/obpdefs.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/open.h>
370Sstevel@tonic-gate #include <sys/thread.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/x_call.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/sysmacros.h>
420Sstevel@tonic-gate #include <sys/ivintr.h>
430Sstevel@tonic-gate #include <sys/intr.h>
440Sstevel@tonic-gate #include <sys/intreg.h>
450Sstevel@tonic-gate #include <sys/autoconf.h>
460Sstevel@tonic-gate #include <sys/modctl.h>
470Sstevel@tonic-gate #include <sys/spl.h>
480Sstevel@tonic-gate #include <sys/async.h>
490Sstevel@tonic-gate #include <sys/mc.h>
500Sstevel@tonic-gate #include <sys/mc-us3i.h>
510Sstevel@tonic-gate #include <sys/note.h>
520Sstevel@tonic-gate #include <sys/cpu_module.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * pm-hardware-state value
560Sstevel@tonic-gate */
570Sstevel@tonic-gate #define NO_SUSPEND_RESUME "no-suspend-resume"
580Sstevel@tonic-gate
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate * Function prototypes
610Sstevel@tonic-gate */
620Sstevel@tonic-gate
630Sstevel@tonic-gate static int mc_open(dev_t *, int, int, cred_t *);
640Sstevel@tonic-gate static int mc_close(dev_t, int, int, cred_t *);
650Sstevel@tonic-gate static int mc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
660Sstevel@tonic-gate static int mc_attach(dev_info_t *, ddi_attach_cmd_t);
670Sstevel@tonic-gate static int mc_detach(dev_info_t *, ddi_detach_cmd_t);
680Sstevel@tonic-gate
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * Configuration data structures
710Sstevel@tonic-gate */
720Sstevel@tonic-gate static struct cb_ops mc_cb_ops = {
730Sstevel@tonic-gate mc_open, /* open */
740Sstevel@tonic-gate mc_close, /* close */
750Sstevel@tonic-gate nulldev, /* strategy */
760Sstevel@tonic-gate nulldev, /* print */
770Sstevel@tonic-gate nodev, /* dump */
780Sstevel@tonic-gate nulldev, /* read */
790Sstevel@tonic-gate nulldev, /* write */
800Sstevel@tonic-gate mc_ioctl, /* ioctl */
810Sstevel@tonic-gate nodev, /* devmap */
820Sstevel@tonic-gate nodev, /* mmap */
830Sstevel@tonic-gate nodev, /* segmap */
840Sstevel@tonic-gate nochpoll, /* poll */
850Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
860Sstevel@tonic-gate 0, /* streamtab */
870Sstevel@tonic-gate D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flag */
880Sstevel@tonic-gate CB_REV, /* rev */
890Sstevel@tonic-gate nodev, /* cb_aread */
900Sstevel@tonic-gate nodev /* cb_awrite */
910Sstevel@tonic-gate };
920Sstevel@tonic-gate
930Sstevel@tonic-gate static struct dev_ops mc_ops = {
940Sstevel@tonic-gate DEVO_REV, /* rev */
950Sstevel@tonic-gate 0, /* refcnt */
960Sstevel@tonic-gate ddi_no_info, /* getinfo */
970Sstevel@tonic-gate nulldev, /* identify */
980Sstevel@tonic-gate nulldev, /* probe */
990Sstevel@tonic-gate mc_attach, /* attach */
1000Sstevel@tonic-gate mc_detach, /* detach */
1010Sstevel@tonic-gate nulldev, /* reset */
1020Sstevel@tonic-gate &mc_cb_ops, /* cb_ops */
1030Sstevel@tonic-gate (struct bus_ops *)0, /* bus_ops */
1047656SSherry.Moore@Sun.COM nulldev, /* power */
1057656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * Driver globals
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate static void *mcp;
1120Sstevel@tonic-gate static int nmcs = 0;
1130Sstevel@tonic-gate static int seg_id;
1140Sstevel@tonic-gate static int nsegments;
1150Sstevel@tonic-gate static uint64_t memsize;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static uint_t mc_debug = 0;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static int getreg;
1200Sstevel@tonic-gate static int nregs;
1210Sstevel@tonic-gate struct memory_reg_info *reg_info;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate static mc_dlist_t *seg_head, *seg_tail, *bank_head, *bank_tail;
1240Sstevel@tonic-gate static mc_dlist_t *mctrl_head, *mctrl_tail, *dgrp_head, *dgrp_tail;
1250Sstevel@tonic-gate static mc_dlist_t *device_head, *device_tail;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate static kmutex_t mcmutex;
1280Sstevel@tonic-gate static kmutex_t mcdatamutex;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate static struct modldrv modldrv = {
1330Sstevel@tonic-gate &mod_driverops, /* module type, this one is a driver */
134*7696SRichard.Bean@Sun.COM "Memory-controller", /* module name */
1350Sstevel@tonic-gate &mc_ops, /* driver ops */
1360Sstevel@tonic-gate };
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate static struct modlinkage modlinkage = {
1390Sstevel@tonic-gate MODREV_1, /* rev */
1400Sstevel@tonic-gate (void *)&modldrv,
1410Sstevel@tonic-gate NULL
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate static int mc_get_memory_reg_info(struct mc_soft_state *softsp);
1450Sstevel@tonic-gate static void mc_construct(struct mc_soft_state *softsp);
1460Sstevel@tonic-gate static void mc_delete(int mc_id);
1470Sstevel@tonic-gate static void mc_node_add(mc_dlist_t *node, mc_dlist_t **head, mc_dlist_t **tail);
1480Sstevel@tonic-gate static void mc_node_del(mc_dlist_t *node, mc_dlist_t **head, mc_dlist_t **tail);
1490Sstevel@tonic-gate static void *mc_node_get(int id, mc_dlist_t *head);
1500Sstevel@tonic-gate static void mc_add_mem_unum_label(char *unum, int mcid, int bank, int dimm);
1510Sstevel@tonic-gate static int mc_get_mem_unum(int synd_code, uint64_t paddr, char *buf,
1520Sstevel@tonic-gate int buflen, int *lenp);
1530Sstevel@tonic-gate static int mc_get_mem_info(int synd_code, uint64_t paddr,
1540Sstevel@tonic-gate uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
1550Sstevel@tonic-gate int *segsp, int *banksp, int *mcidp);
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate #pragma weak p2get_mem_unum
1580Sstevel@tonic-gate #pragma weak p2get_mem_info
1590Sstevel@tonic-gate #pragma weak plat_add_mem_unum_label
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /* For testing only */
1620Sstevel@tonic-gate struct test_unum {
1630Sstevel@tonic-gate int synd_code;
1640Sstevel@tonic-gate uint64_t paddr;
1650Sstevel@tonic-gate char unum[UNUM_NAMLEN];
1660Sstevel@tonic-gate int len;
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * These are the module initialization routines.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate int
_init(void)1740Sstevel@tonic-gate _init(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate int error;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if ((error = ddi_soft_state_init(&mcp,
1790Sstevel@tonic-gate sizeof (struct mc_soft_state), 1)) != 0)
1800Sstevel@tonic-gate return (error);
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate error = mod_install(&modlinkage);
1830Sstevel@tonic-gate if (error == 0) {
1840Sstevel@tonic-gate mutex_init(&mcmutex, NULL, MUTEX_DRIVER, NULL);
1850Sstevel@tonic-gate mutex_init(&mcdatamutex, NULL, MUTEX_DRIVER, NULL);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate return (error);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate int
_fini(void)1920Sstevel@tonic-gate _fini(void)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate int error;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate if ((error = mod_remove(&modlinkage)) != 0)
1970Sstevel@tonic-gate return (error);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate ddi_soft_state_fini(&mcp);
2000Sstevel@tonic-gate mutex_destroy(&mcmutex);
2010Sstevel@tonic-gate mutex_destroy(&mcdatamutex);
2020Sstevel@tonic-gate return (0);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2060Sstevel@tonic-gate _info(struct modinfo *modinfop)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate static int
mc_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2120Sstevel@tonic-gate mc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate struct mc_soft_state *softsp;
2150Sstevel@tonic-gate struct dimm_info *dimminfop;
2160Sstevel@tonic-gate int instance, len, err;
2170Sstevel@tonic-gate int mcreg1_len;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate switch (cmd) {
2200Sstevel@tonic-gate case DDI_ATTACH:
2210Sstevel@tonic-gate break;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate case DDI_RESUME:
2240Sstevel@tonic-gate return (DDI_SUCCESS);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate default:
2270Sstevel@tonic-gate return (DDI_FAILURE);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate instance = ddi_get_instance(devi);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (ddi_soft_state_zalloc(mcp, instance) != DDI_SUCCESS)
2330Sstevel@tonic-gate return (DDI_FAILURE);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate softsp = ddi_get_soft_state(mcp, instance);
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /* Set the dip in the soft state */
2380Sstevel@tonic-gate softsp->dip = devi;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if ((softsp->portid = (int)ddi_getprop(DDI_DEV_T_ANY, softsp->dip,
2410Sstevel@tonic-gate DDI_PROP_DONTPASS, "portid", -1)) == -1) {
2420Sstevel@tonic-gate DPRINTF(MC_ATTACH_DEBUG, ("mc%d: unable to get %s property\n",
2430Sstevel@tonic-gate instance, "portid"));
2440Sstevel@tonic-gate goto bad;
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate DPRINTF(MC_ATTACH_DEBUG, ("mc_attach: mc %d portid %d, cpuid %d\n",
2480Sstevel@tonic-gate instance, softsp->portid, CPU->cpu_id));
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /* Get the content of Memory Control Register I from obp */
2510Sstevel@tonic-gate mcreg1_len = sizeof (uint64_t);
2520Sstevel@tonic-gate if ((ddi_getlongprop_buf(DDI_DEV_T_ANY, softsp->dip, DDI_PROP_DONTPASS,
2530Sstevel@tonic-gate "memory-control-register-1", (caddr_t)&(softsp->mcreg1),
2540Sstevel@tonic-gate &mcreg1_len) == DDI_PROP_SUCCESS) &&
2550Sstevel@tonic-gate (mcreg1_len == sizeof (uint64_t))) {
2560Sstevel@tonic-gate softsp->mcr_read_ok = 1;
2570Sstevel@tonic-gate DPRINTF(MC_ATTACH_DEBUG, ("mc%d from obp: Reg1: 0x%lx\n",
2586803Spothier instance, softsp->mcreg1));
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* attach fails if mcreg1 cannot be accessed */
2620Sstevel@tonic-gate if (!softsp->mcr_read_ok) {
2630Sstevel@tonic-gate DPRINTF(MC_ATTACH_DEBUG, ("mc%d: unable to get mcreg1\n",
2640Sstevel@tonic-gate instance));
2650Sstevel@tonic-gate goto bad;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /* nothing to suspend/resume here */
2690Sstevel@tonic-gate (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
2700Sstevel@tonic-gate "pm-hardware-state", NO_SUSPEND_RESUME,
2710Sstevel@tonic-gate sizeof (NO_SUSPEND_RESUME));
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate * Get the label of dimms and pin routing information from the
2750Sstevel@tonic-gate * memory-layout property of the memory controller.
2760Sstevel@tonic-gate */
2770Sstevel@tonic-gate err = ddi_getlongprop(DDI_DEV_T_ANY, softsp->dip, DDI_PROP_DONTPASS,
2780Sstevel@tonic-gate "memory-layout", (caddr_t)&dimminfop, &len);
2790Sstevel@tonic-gate if (err == DDI_PROP_SUCCESS && dimminfop->table_width == 1) {
2800Sstevel@tonic-gate /* Set the pointer and size of property in the soft state */
2810Sstevel@tonic-gate softsp->memlayoutp = dimminfop;
2820Sstevel@tonic-gate softsp->memlayoutlen = len;
2830Sstevel@tonic-gate } else {
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate * memory-layout property was not found or some other
2860Sstevel@tonic-gate * error occured, plat_get_mem_unum() will not work
2870Sstevel@tonic-gate * for this mc.
2880Sstevel@tonic-gate */
2890Sstevel@tonic-gate softsp->memlayoutp = NULL;
2900Sstevel@tonic-gate softsp->memlayoutlen = 0;
2910Sstevel@tonic-gate DPRINTF(MC_ATTACH_DEBUG,
2920Sstevel@tonic-gate ("mc %d: missing or unsupported memory-layout property\n",
2930Sstevel@tonic-gate instance));
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate mutex_enter(&mcmutex);
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate /* Get the physical segments from memory/reg, just once for all MC */
2990Sstevel@tonic-gate if (!getreg) {
3000Sstevel@tonic-gate if (mc_get_memory_reg_info(softsp) != 0) {
3010Sstevel@tonic-gate goto bad1;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate getreg = 1;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate /* Construct the physical and logical layout of the MC */
3070Sstevel@tonic-gate mc_construct(softsp);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (nmcs == 1) {
3100Sstevel@tonic-gate if (&p2get_mem_unum)
3110Sstevel@tonic-gate p2get_mem_unum = mc_get_mem_unum;
3120Sstevel@tonic-gate if (&p2get_mem_info)
3130Sstevel@tonic-gate p2get_mem_info = mc_get_mem_info;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate if (ddi_create_minor_node(devi, "mc-us3i", S_IFCHR, instance,
3170Sstevel@tonic-gate "ddi_mem_ctrl", 0) != DDI_SUCCESS) {
3180Sstevel@tonic-gate DPRINTF(MC_ATTACH_DEBUG, ("mc_attach: create_minor_node"
3190Sstevel@tonic-gate " failed \n"));
3200Sstevel@tonic-gate goto bad1;
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate mutex_exit(&mcmutex);
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate ddi_report_dev(devi);
3250Sstevel@tonic-gate return (DDI_SUCCESS);
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate bad1:
3280Sstevel@tonic-gate /* release all allocated data struture for this MC */
3290Sstevel@tonic-gate mc_delete(softsp->portid);
3300Sstevel@tonic-gate mutex_exit(&mcmutex);
3310Sstevel@tonic-gate if (softsp->memlayoutp != NULL)
3320Sstevel@tonic-gate kmem_free(softsp->memlayoutp, softsp->memlayoutlen);
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate bad:
3350Sstevel@tonic-gate cmn_err(CE_WARN, "mc-us3i: attach failed for instance %d\n", instance);
3360Sstevel@tonic-gate ddi_soft_state_free(mcp, instance);
3370Sstevel@tonic-gate return (DDI_FAILURE);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /* ARGSUSED */
3410Sstevel@tonic-gate static int
mc_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)3420Sstevel@tonic-gate mc_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate int instance;
3450Sstevel@tonic-gate struct mc_soft_state *softsp;
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /* get the instance of this devi */
3480Sstevel@tonic-gate instance = ddi_get_instance(devi);
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /* get the soft state pointer for this device node */
3510Sstevel@tonic-gate softsp = ddi_get_soft_state(mcp, instance);
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate switch (cmd) {
3540Sstevel@tonic-gate case DDI_SUSPEND:
3550Sstevel@tonic-gate return (DDI_SUCCESS);
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate case DDI_DETACH:
3580Sstevel@tonic-gate break;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate default:
3610Sstevel@tonic-gate return (DDI_FAILURE);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate DPRINTF(MC_DETACH_DEBUG, ("mc %d DETACH: portid %d\n", instance,
3650Sstevel@tonic-gate softsp->portid));
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate mutex_enter(&mcmutex);
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate /* release all allocated data struture for this MC */
3700Sstevel@tonic-gate mc_delete(softsp->portid);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate if (softsp->memlayoutp != NULL)
3730Sstevel@tonic-gate kmem_free(softsp->memlayoutp, softsp->memlayoutlen);
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate if (nmcs == 0) {
3760Sstevel@tonic-gate if (&p2get_mem_unum)
3770Sstevel@tonic-gate p2get_mem_unum = NULL;
3780Sstevel@tonic-gate if (&p2get_mem_info)
3790Sstevel@tonic-gate p2get_mem_info = NULL;
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate mutex_exit(&mcmutex);
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
3850Sstevel@tonic-gate /* free up the soft state */
3860Sstevel@tonic-gate ddi_soft_state_free(mcp, instance);
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate return (DDI_SUCCESS);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate /* ARGSUSED */
3920Sstevel@tonic-gate static int
mc_open(dev_t * devp,int flag,int otyp,cred_t * credp)3930Sstevel@tonic-gate mc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate int status = 0;
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate /* verify that otyp is appropriate */
3980Sstevel@tonic-gate if (otyp != OTYP_CHR) {
3990Sstevel@tonic-gate return (EINVAL);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate mutex_enter(&mcmutex);
4030Sstevel@tonic-gate /* At least one attached? */
4040Sstevel@tonic-gate if (nmcs == 0) {
4050Sstevel@tonic-gate status = ENXIO;
4060Sstevel@tonic-gate }
4073685Smb158278 mutex_exit(&mcmutex);
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate return (status);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate /* ARGSUSED */
4130Sstevel@tonic-gate static int
mc_close(dev_t devp,int flag,int otyp,cred_t * credp)4140Sstevel@tonic-gate mc_close(dev_t devp, int flag, int otyp, cred_t *credp)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate return (0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * cmd includes MCIOC_MEMCONF, MCIOC_MEM, MCIOC_SEG, MCIOC_BANK, MCIOC_DEVGRP,
4210Sstevel@tonic-gate * MCIOC_CTRLCONF, MCIOC_CONTROL.
4220Sstevel@tonic-gate *
4230Sstevel@tonic-gate * MCIOC_MEM, MCIOC_SEG, MCIOC_CTRLCONF, and MCIOC_CONTROL are
4240Sstevel@tonic-gate * associated with various length struct. If given number is less than the
4250Sstevel@tonic-gate * number in kernel, update the number and return EINVAL so that user could
4260Sstevel@tonic-gate * allocate enough space for it.
4270Sstevel@tonic-gate *
4280Sstevel@tonic-gate */
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate /* ARGSUSED */
4310Sstevel@tonic-gate static int
mc_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cred_p,int * rval_p)4320Sstevel@tonic-gate mc_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p,
4330Sstevel@tonic-gate int *rval_p)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate size_t size;
4360Sstevel@tonic-gate struct mc_memconf mcmconf;
4370Sstevel@tonic-gate struct mc_memory *mcmem, mcmem_in;
4380Sstevel@tonic-gate struct mc_segment *mcseg, mcseg_in;
4390Sstevel@tonic-gate struct mc_bank mcbank;
4400Sstevel@tonic-gate struct mc_devgrp mcdevgrp;
4410Sstevel@tonic-gate struct mc_ctrlconf *mcctrlconf, mcctrlconf_in;
4420Sstevel@tonic-gate struct mc_control *mccontrol, mccontrol_in;
4430Sstevel@tonic-gate struct seg_info *seg = NULL;
4440Sstevel@tonic-gate struct bank_info *bank = NULL;
4450Sstevel@tonic-gate struct dgrp_info *dgrp = NULL;
4460Sstevel@tonic-gate struct mctrl_info *mcport;
4470Sstevel@tonic-gate mc_dlist_t *mctrl;
4480Sstevel@tonic-gate int i, status = 0;
4490Sstevel@tonic-gate cpu_t *cpu;
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate switch (cmd) {
4520Sstevel@tonic-gate case MCIOC_MEMCONF:
4530Sstevel@tonic-gate mutex_enter(&mcdatamutex);
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate mcmconf.nmcs = nmcs;
4560Sstevel@tonic-gate mcmconf.nsegments = nsegments;
4570Sstevel@tonic-gate mcmconf.nbanks = NLOGBANKS_PER_SEG;
4580Sstevel@tonic-gate mcmconf.ndevgrps = NDGRPS_PER_MC;
4590Sstevel@tonic-gate mcmconf.ndevs = NDIMMS_PER_DGRP;
4600Sstevel@tonic-gate mcmconf.len_dev = MAX_DEVLEN;
4610Sstevel@tonic-gate mcmconf.xfer_size = TRANSFER_SIZE;
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate mutex_exit(&mcdatamutex);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate if (copyout(&mcmconf, (void *)arg, sizeof (mcmconf)))
4660Sstevel@tonic-gate return (EFAULT);
4670Sstevel@tonic-gate return (0);
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate /*
4700Sstevel@tonic-gate * input: nsegments and allocate space for various length of segmentids
4710Sstevel@tonic-gate *
4720Sstevel@tonic-gate * return 0: size, number of segments, and all segment ids,
4730Sstevel@tonic-gate * where glocal and local ids are identical.
4740Sstevel@tonic-gate * EINVAL: if the given nsegments is less than that in kernel and
4750Sstevel@tonic-gate * nsegments of struct will be updated.
4760Sstevel@tonic-gate * EFAULT: if other errors in kernel.
4770Sstevel@tonic-gate */
4780Sstevel@tonic-gate case MCIOC_MEM:
4790Sstevel@tonic-gate if (copyin((void *)arg, &mcmem_in, sizeof (mcmem_in)) != 0)
4800Sstevel@tonic-gate return (EFAULT);
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate mutex_enter(&mcdatamutex);
4830Sstevel@tonic-gate if (mcmem_in.nsegments < nsegments) {
4840Sstevel@tonic-gate mcmem_in.nsegments = nsegments;
4850Sstevel@tonic-gate mutex_exit(&mcdatamutex);
4860Sstevel@tonic-gate if (copyout(&mcmem_in, (void *)arg, sizeof (mcmem_in)))
4870Sstevel@tonic-gate status = EFAULT;
4880Sstevel@tonic-gate else
4890Sstevel@tonic-gate status = EINVAL;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate return (status);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate size = sizeof (*mcmem) + (nsegments - 1) *
4950Sstevel@tonic-gate sizeof (mcmem->segmentids[0]);
4960Sstevel@tonic-gate mcmem = kmem_zalloc(size, KM_SLEEP);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate mcmem->size = memsize;
4990Sstevel@tonic-gate mcmem->nsegments = nsegments;
5000Sstevel@tonic-gate seg = (struct seg_info *)seg_head;
5010Sstevel@tonic-gate for (i = 0; i < nsegments; i++) {
5020Sstevel@tonic-gate ASSERT(seg != NULL);
5030Sstevel@tonic-gate mcmem->segmentids[i].globalid = seg->seg_node.id;
5040Sstevel@tonic-gate mcmem->segmentids[i].localid = seg->seg_node.id;
5050Sstevel@tonic-gate seg = (struct seg_info *)seg->seg_node.next;
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate mutex_exit(&mcdatamutex);
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate if (copyout(mcmem, (void *)arg, size))
5100Sstevel@tonic-gate status = EFAULT;
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate kmem_free(mcmem, size);
5130Sstevel@tonic-gate return (status);
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate * input: id, nbanks and allocate space for various length of bankids
5170Sstevel@tonic-gate *
5180Sstevel@tonic-gate * return 0: base, size, number of banks, and all bank ids,
5190Sstevel@tonic-gate * where global id is unique of all banks and local id
5200Sstevel@tonic-gate * is only unique for mc.
5210Sstevel@tonic-gate * EINVAL: either id isn't found or if given nbanks is less than
5220Sstevel@tonic-gate * that in kernel and nbanks of struct will be updated.
5230Sstevel@tonic-gate * EFAULT: if other errors in kernel.
5240Sstevel@tonic-gate */
5250Sstevel@tonic-gate case MCIOC_SEG:
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate if (copyin((void *)arg, &mcseg_in, sizeof (mcseg_in)) != 0)
5280Sstevel@tonic-gate return (EFAULT);
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate mutex_enter(&mcdatamutex);
5310Sstevel@tonic-gate if ((seg = mc_node_get(mcseg_in.id, seg_head)) == NULL) {
5320Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("MCIOC_SEG: seg not match, "
5330Sstevel@tonic-gate "id %d\n", mcseg_in.id));
5340Sstevel@tonic-gate mutex_exit(&mcdatamutex);
5350Sstevel@tonic-gate return (EFAULT);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if (mcseg_in.nbanks < seg->nbanks) {
5390Sstevel@tonic-gate mcseg_in.nbanks = seg->nbanks;
5400Sstevel@tonic-gate mutex_exit(&mcdatamutex);
5410Sstevel@tonic-gate if (copyout(&mcseg_in, (void *)arg, sizeof (mcseg_in)))
5420Sstevel@tonic-gate status = EFAULT;
5430Sstevel@tonic-gate else
5440Sstevel@tonic-gate status = EINVAL;
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate return (status);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate size = sizeof (*mcseg) + (seg->nbanks - 1) *
5500Sstevel@tonic-gate sizeof (mcseg->bankids[0]);
5510Sstevel@tonic-gate mcseg = kmem_zalloc(size, KM_SLEEP);
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate mcseg->id = seg->seg_node.id;
5540Sstevel@tonic-gate mcseg->ifactor = seg->ifactor;
5550Sstevel@tonic-gate mcseg->base = seg->base;
5560Sstevel@tonic-gate mcseg->size = seg->size;
5570Sstevel@tonic-gate mcseg->nbanks = seg->nbanks;
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate bank = seg->head;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("MCIOC_SEG:nbanks %d seg %p bank %p\n",
5620Sstevel@tonic-gate seg->nbanks, (void *) seg, (void *) bank));
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate i = 0;
5650Sstevel@tonic-gate while (bank != NULL) {
5660Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("MCIOC_SEG:idx %d bank_id %d\n",
5670Sstevel@tonic-gate i, bank->bank_node.id));
5680Sstevel@tonic-gate mcseg->bankids[i].globalid = bank->bank_node.id;
5690Sstevel@tonic-gate mcseg->bankids[i++].localid = bank->local_id;
5700Sstevel@tonic-gate bank = bank->next;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate ASSERT(i == seg->nbanks);
5730Sstevel@tonic-gate mutex_exit(&mcdatamutex);
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate if (copyout(mcseg, (void *)arg, size))
5760Sstevel@tonic-gate status = EFAULT;
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate kmem_free(mcseg, size);
5790Sstevel@tonic-gate return (status);
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate * input: id
5830Sstevel@tonic-gate *
5840Sstevel@tonic-gate * return 0: mask, match, size, and devgrpid,
5850Sstevel@tonic-gate * where global id is unique of all devgrps and local id
5860Sstevel@tonic-gate * is only unique for mc.
5870Sstevel@tonic-gate * EINVAL: if id isn't found
5880Sstevel@tonic-gate * EFAULT: if other errors in kernel.
5890Sstevel@tonic-gate */
5900Sstevel@tonic-gate case MCIOC_BANK:
5910Sstevel@tonic-gate if (copyin((void *)arg, &mcbank, sizeof (mcbank)) != 0)
5920Sstevel@tonic-gate return (EFAULT);
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("MCIOC_BANK: bank id %d\n", mcbank.id));
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate mutex_enter(&mcdatamutex);
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate if ((bank = mc_node_get(mcbank.id, bank_head)) == NULL) {
5990Sstevel@tonic-gate mutex_exit(&mcdatamutex);
6000Sstevel@tonic-gate return (EINVAL);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate mcbank.mask = bank->mask;
6040Sstevel@tonic-gate mcbank.match = bank->match;
6050Sstevel@tonic-gate mcbank.size = bank->size;
6060Sstevel@tonic-gate mcbank.devgrpid.globalid = bank->devgrp_id;
6070Sstevel@tonic-gate mcbank.devgrpid.localid =
6080Sstevel@tonic-gate bank->bank_node.id % NLOGBANKS_PER_SEG;
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate mutex_exit(&mcdatamutex);
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate if (copyout(&mcbank, (void *)arg, sizeof (mcbank)))
6130Sstevel@tonic-gate return (EFAULT);
6140Sstevel@tonic-gate return (0);
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate /*
6170Sstevel@tonic-gate * input:id and allocate space for various length of deviceids
6180Sstevel@tonic-gate *
6190Sstevel@tonic-gate * return 0: size and number of devices.
6200Sstevel@tonic-gate * EINVAL: id isn't found
6210Sstevel@tonic-gate * EFAULT: if other errors in kernel.
6220Sstevel@tonic-gate */
6230Sstevel@tonic-gate case MCIOC_DEVGRP:
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate if (copyin((void *)arg, &mcdevgrp, sizeof (mcdevgrp)) != 0)
6260Sstevel@tonic-gate return (EFAULT);
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate mutex_enter(&mcdatamutex);
6290Sstevel@tonic-gate if ((dgrp = mc_node_get(mcdevgrp.id, dgrp_head)) == NULL) {
6300Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("MCIOC_DEVGRP: not match, id "
6310Sstevel@tonic-gate "%d\n", mcdevgrp.id));
6320Sstevel@tonic-gate mutex_exit(&mcdatamutex);
6330Sstevel@tonic-gate return (EINVAL);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate mcdevgrp.ndevices = dgrp->ndevices;
6370Sstevel@tonic-gate mcdevgrp.size = dgrp->size;
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate mutex_exit(&mcdatamutex);
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate if (copyout(&mcdevgrp, (void *)arg, sizeof (mcdevgrp)))
6420Sstevel@tonic-gate status = EFAULT;
6430Sstevel@tonic-gate
6440Sstevel@tonic-gate return (status);
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate * input: nmcs and allocate space for various length of mcids
6480Sstevel@tonic-gate *
6490Sstevel@tonic-gate * return 0: number of mc, and all mcids,
6500Sstevel@tonic-gate * where glocal and local ids are identical.
6510Sstevel@tonic-gate * EINVAL: if the given nmcs is less than that in kernel and
6520Sstevel@tonic-gate * nmcs of struct will be updated.
6530Sstevel@tonic-gate * EFAULT: if other errors in kernel.
6540Sstevel@tonic-gate */
6550Sstevel@tonic-gate case MCIOC_CTRLCONF:
6560Sstevel@tonic-gate if (copyin((void *)arg, &mcctrlconf_in,
6570Sstevel@tonic-gate sizeof (mcctrlconf_in)) != 0)
6580Sstevel@tonic-gate return (EFAULT);
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate mutex_enter(&mcdatamutex);
6610Sstevel@tonic-gate if (mcctrlconf_in.nmcs < nmcs) {
6620Sstevel@tonic-gate mcctrlconf_in.nmcs = nmcs;
6630Sstevel@tonic-gate mutex_exit(&mcdatamutex);
6640Sstevel@tonic-gate if (copyout(&mcctrlconf_in, (void *)arg,
6650Sstevel@tonic-gate sizeof (mcctrlconf_in)))
6660Sstevel@tonic-gate status = EFAULT;
6670Sstevel@tonic-gate else
6680Sstevel@tonic-gate status = EINVAL;
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate return (status);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate * Cannot just use the size of the struct because of the various
6750Sstevel@tonic-gate * length struct
6760Sstevel@tonic-gate */
6770Sstevel@tonic-gate size = sizeof (*mcctrlconf) + ((nmcs - 1) *
6780Sstevel@tonic-gate sizeof (mcctrlconf->mcids[0]));
6790Sstevel@tonic-gate mcctrlconf = kmem_zalloc(size, KM_SLEEP);
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate mcctrlconf->nmcs = nmcs;
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate /* Get all MC ids and add to mcctrlconf */
6840Sstevel@tonic-gate mctrl = mctrl_head;
6850Sstevel@tonic-gate i = 0;
6860Sstevel@tonic-gate while (mctrl != NULL) {
6870Sstevel@tonic-gate mcctrlconf->mcids[i].globalid = mctrl->id;
6880Sstevel@tonic-gate mcctrlconf->mcids[i].localid = mctrl->id;
6890Sstevel@tonic-gate i++;
6900Sstevel@tonic-gate mctrl = mctrl->next;
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate ASSERT(i == nmcs);
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate mutex_exit(&mcdatamutex);
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate if (copyout(mcctrlconf, (void *)arg, size))
6970Sstevel@tonic-gate status = EFAULT;
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate kmem_free(mcctrlconf, size);
7000Sstevel@tonic-gate return (status);
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate * input:id, ndevgrps and allocate space for various length of devgrpids
7040Sstevel@tonic-gate *
7050Sstevel@tonic-gate * return 0: number of devgrp, and all devgrpids,
7060Sstevel@tonic-gate * is unique of all devgrps and local id is only unique
7070Sstevel@tonic-gate * for mc.
7080Sstevel@tonic-gate * EINVAL: either if id isn't found or if the given ndevgrps is
7090Sstevel@tonic-gate * less than that in kernel and ndevgrps of struct will
7100Sstevel@tonic-gate * be updated.
7110Sstevel@tonic-gate * EFAULT: if other errors in kernel.
7120Sstevel@tonic-gate */
7130Sstevel@tonic-gate case MCIOC_CONTROL:
7140Sstevel@tonic-gate if (copyin((void *)arg, &mccontrol_in,
7150Sstevel@tonic-gate sizeof (mccontrol_in)) != 0)
7160Sstevel@tonic-gate return (EFAULT);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate mutex_enter(&mcdatamutex);
7190Sstevel@tonic-gate if ((mcport = mc_node_get(mccontrol_in.id,
7200Sstevel@tonic-gate mctrl_head)) == NULL) {
7210Sstevel@tonic-gate mutex_exit(&mcdatamutex);
7220Sstevel@tonic-gate return (EINVAL);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate * mcport->ndevgrps zero means Memory Controller is disable.
7270Sstevel@tonic-gate */
7280Sstevel@tonic-gate if ((mccontrol_in.ndevgrps < mcport->ndevgrps) ||
7290Sstevel@tonic-gate (mcport->ndevgrps == 0)) {
7300Sstevel@tonic-gate mccontrol_in.ndevgrps = mcport->ndevgrps;
7310Sstevel@tonic-gate mutex_exit(&mcdatamutex);
7320Sstevel@tonic-gate if (copyout(&mccontrol_in, (void *)arg,
7330Sstevel@tonic-gate sizeof (mccontrol_in)))
7340Sstevel@tonic-gate status = EFAULT;
7350Sstevel@tonic-gate else if (mcport->ndevgrps != 0)
7360Sstevel@tonic-gate status = EINVAL;
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate return (status);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate size = sizeof (*mccontrol) + (mcport->ndevgrps - 1) *
7420Sstevel@tonic-gate sizeof (mccontrol->devgrpids[0]);
7430Sstevel@tonic-gate mccontrol = kmem_zalloc(size, KM_SLEEP);
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate mccontrol->id = mcport->mctrl_node.id;
7460Sstevel@tonic-gate mccontrol->ndevgrps = mcport->ndevgrps;
7470Sstevel@tonic-gate for (i = 0; i < mcport->ndevgrps; i++) {
7480Sstevel@tonic-gate mccontrol->devgrpids[i].globalid = mcport->devgrpids[i];
7490Sstevel@tonic-gate mccontrol->devgrpids[i].localid =
7500Sstevel@tonic-gate mcport->devgrpids[i] % NDGRPS_PER_MC;
7510Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("MCIOC_CONTROL: devgrp id %d\n",
7520Sstevel@tonic-gate i));
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate mutex_exit(&mcdatamutex);
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate if (copyout(mccontrol, (void *)arg, size))
7570Sstevel@tonic-gate status = EFAULT;
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate kmem_free(mccontrol, size);
7600Sstevel@tonic-gate return (status);
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate /*
7630Sstevel@tonic-gate * input:id
7640Sstevel@tonic-gate *
7650Sstevel@tonic-gate * return 0: CPU flushed successfully.
7660Sstevel@tonic-gate * EINVAL: the id wasn't found
7670Sstevel@tonic-gate */
7680Sstevel@tonic-gate case MCIOC_ECFLUSH:
7690Sstevel@tonic-gate mutex_enter(&cpu_lock);
7700Sstevel@tonic-gate cpu = cpu_get((processorid_t)arg);
7710Sstevel@tonic-gate mutex_exit(&cpu_lock);
7720Sstevel@tonic-gate if (cpu == NULL)
7730Sstevel@tonic-gate return (EINVAL);
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate xc_one(arg, (xcfunc_t *)cpu_flush_ecache, 0, 0);
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate return (0);
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate default:
7800Sstevel@tonic-gate DPRINTF(MC_CMD_DEBUG, ("DEFAULT: cmd is wrong\n"));
7810Sstevel@tonic-gate return (EFAULT);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate /*
7860Sstevel@tonic-gate * Gets the reg property from the memory node. This provides the various
7870Sstevel@tonic-gate * memory segments, at bank-boundries, dimm-pair boundries, in the form
7880Sstevel@tonic-gate * of [base, size] pairs. Continuous segments, spanning boundries are
7890Sstevel@tonic-gate * merged into one.
7900Sstevel@tonic-gate * Returns 0 for success and -1 for failure.
7910Sstevel@tonic-gate */
7920Sstevel@tonic-gate static int
mc_get_memory_reg_info(struct mc_soft_state * softsp)7930Sstevel@tonic-gate mc_get_memory_reg_info(struct mc_soft_state *softsp)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate dev_info_t *devi;
7960Sstevel@tonic-gate int len;
7970Sstevel@tonic-gate int i;
7980Sstevel@tonic-gate struct memory_reg_info *mregi;
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate _NOTE(ARGUNUSED(softsp))
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate if ((devi = ddi_find_devinfo("memory", -1, 0)) == NULL) {
8030Sstevel@tonic-gate DPRINTF(MC_REG_DEBUG,
8040Sstevel@tonic-gate ("mc-us3i: cannot find memory node under root\n"));
8050Sstevel@tonic-gate return (-1);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8090Sstevel@tonic-gate "reg", (caddr_t)®_info, &len) != DDI_PROP_SUCCESS) {
8100Sstevel@tonic-gate DPRINTF(MC_REG_DEBUG,
8110Sstevel@tonic-gate ("mc-us3i: reg undefined under memory\n"));
8120Sstevel@tonic-gate return (-1);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate nregs = len/sizeof (*mregi);
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate DPRINTF(MC_REG_DEBUG, ("mc_get_memory_reg_info: nregs %d"
8180Sstevel@tonic-gate "reg_info %p\n", nregs, (void *) reg_info));
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate mregi = reg_info;
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate /* debug printfs */
8230Sstevel@tonic-gate for (i = 0; i < nregs; i++) {
8240Sstevel@tonic-gate DPRINTF(MC_REG_DEBUG, (" [0x%lx, 0x%lx] ",
8250Sstevel@tonic-gate mregi->base, mregi->size));
8260Sstevel@tonic-gate mregi++;
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate return (0);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate * Initialize a logical bank
8340Sstevel@tonic-gate */
8350Sstevel@tonic-gate static struct bank_info *
mc_add_bank(int bankid,uint64_t mask,uint64_t match,uint64_t size,int dgrpid)8360Sstevel@tonic-gate mc_add_bank(int bankid, uint64_t mask, uint64_t match, uint64_t size,
8370Sstevel@tonic-gate int dgrpid)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate struct bank_info *banki;
8400Sstevel@tonic-gate
8410Sstevel@tonic-gate if ((banki = mc_node_get(bankid, bank_head)) != NULL) {
8420Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_bank: bank %d exists\n",
8430Sstevel@tonic-gate bankid));
8440Sstevel@tonic-gate return (banki);
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate banki = kmem_zalloc(sizeof (*banki), KM_SLEEP);
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate banki->bank_node.id = bankid;
8500Sstevel@tonic-gate banki->devgrp_id = dgrpid;
8510Sstevel@tonic-gate banki->mask = mask;
8520Sstevel@tonic-gate banki->match = match;
8530Sstevel@tonic-gate banki->base = match;
8540Sstevel@tonic-gate banki->size = size;
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate mc_node_add((mc_dlist_t *)banki, &bank_head, &bank_tail);
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_bank: id %d mask 0x%lx match 0x%lx"
8590Sstevel@tonic-gate " base 0x%lx size 0x%lx\n", bankid, mask, match,
8600Sstevel@tonic-gate banki->base, banki->size));
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate return (banki);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate * Use the bank's base address to find out whether to initialize a new segment,
8670Sstevel@tonic-gate * or weave the bank into an existing segment. If the tail bank of a previous
8680Sstevel@tonic-gate * segment is not continuous with the new bank, the new bank goes into a new
8690Sstevel@tonic-gate * segment.
8700Sstevel@tonic-gate */
8710Sstevel@tonic-gate static void
mc_add_segment(struct bank_info * banki)8720Sstevel@tonic-gate mc_add_segment(struct bank_info *banki)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate struct seg_info *segi;
8750Sstevel@tonic-gate struct bank_info *tb;
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate /* does this bank start a new segment? */
8780Sstevel@tonic-gate if ((segi = mc_node_get(seg_id, seg_head)) == NULL) {
8790Sstevel@tonic-gate /* this should happen for the first segment only */
8800Sstevel@tonic-gate goto new_seg;
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate
8830Sstevel@tonic-gate tb = segi->tail;
8840Sstevel@tonic-gate /* discontiguous banks go into a new segment, increment the seg_id */
8850Sstevel@tonic-gate if (banki->base > (tb->base + tb->size)) {
8860Sstevel@tonic-gate seg_id++;
8870Sstevel@tonic-gate goto new_seg;
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate
8900Sstevel@tonic-gate /* weave the bank into the segment */
8910Sstevel@tonic-gate segi->nbanks++;
8920Sstevel@tonic-gate tb->next = banki;
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate banki->seg_id = segi->seg_node.id;
8950Sstevel@tonic-gate banki->local_id = tb->local_id + 1;
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate /* contiguous or interleaved? */
8980Sstevel@tonic-gate if (banki->base != (tb->base + tb->size))
8990Sstevel@tonic-gate segi->ifactor++;
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate segi->size += banki->size;
9020Sstevel@tonic-gate segi->tail = banki;
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate memsize += banki->size;
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_segment: id %d add bank: id %d"
9070Sstevel@tonic-gate "size 0x%lx\n", segi->seg_node.id, banki->bank_node.id,
9080Sstevel@tonic-gate banki->size));
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate return;
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate new_seg:
9130Sstevel@tonic-gate segi = kmem_zalloc(sizeof (*segi), KM_SLEEP);
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate segi->seg_node.id = seg_id;
9160Sstevel@tonic-gate segi->nbanks = 1;
9170Sstevel@tonic-gate segi->ifactor = 1;
9180Sstevel@tonic-gate segi->base = banki->base;
9190Sstevel@tonic-gate segi->size = banki->size;
9200Sstevel@tonic-gate segi->head = banki;
9210Sstevel@tonic-gate segi->tail = banki;
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate banki->seg_id = segi->seg_node.id;
9240Sstevel@tonic-gate banki->local_id = 0;
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate mc_node_add((mc_dlist_t *)segi, &seg_head, &seg_tail);
9270Sstevel@tonic-gate nsegments++;
9280Sstevel@tonic-gate
9290Sstevel@tonic-gate memsize += banki->size;
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_segment: id %d new bank: id %d"
9320Sstevel@tonic-gate "size 0x%lx\n", segi->seg_node.id, banki->bank_node.id,
9330Sstevel@tonic-gate banki->size));
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate /*
9370Sstevel@tonic-gate * Returns the address bit number (row index) that controls the logical/external
9380Sstevel@tonic-gate * bank assignment in interleave of kind internal-external same dimm-pair,
9390Sstevel@tonic-gate * internal-external both dimm-pair. This is done by using the dimm-densities
9400Sstevel@tonic-gate * and part-type.
9410Sstevel@tonic-gate */
9420Sstevel@tonic-gate static int
get_row_shift(int row_index,struct dgrp_info * dgrp)9430Sstevel@tonic-gate get_row_shift(int row_index, struct dgrp_info *dgrp)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate int shift;
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate switch (dgrp->base_device) {
9480Sstevel@tonic-gate case BASE_DEVICE_128Mb:
9490Sstevel@tonic-gate case BASE_DEVICE_256Mb:
9500Sstevel@tonic-gate /* 128Mb and 256Mb devices have same bank select mask */
9510Sstevel@tonic-gate shift = ADDR_GEN_128Mb_X8_ROW_0;
9520Sstevel@tonic-gate break;
9530Sstevel@tonic-gate case BASE_DEVICE_512Mb:
9540Sstevel@tonic-gate case BASE_DEVICE_1Gb:
9550Sstevel@tonic-gate /* 512 and 1Gb devices have same bank select mask */
9560Sstevel@tonic-gate shift = ADDR_GEN_512Mb_X8_ROW_0;
9570Sstevel@tonic-gate break;
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate if (dgrp->part_type == PART_TYPE_X4)
9610Sstevel@tonic-gate shift += 1;
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate shift += row_index;
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate return (shift);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate static void
get_device_select(int interleave,struct dgrp_info * dgrp,int * ds_shift,int * bs_shift)9700Sstevel@tonic-gate get_device_select(int interleave, struct dgrp_info *dgrp,
9710Sstevel@tonic-gate int *ds_shift, int *bs_shift)
9720Sstevel@tonic-gate {
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate switch (interleave) {
9750Sstevel@tonic-gate case INTERLEAVE_DISABLE:
9760Sstevel@tonic-gate /* Fall Through */
9770Sstevel@tonic-gate case INTERLEAVE_INTERNAL:
9780Sstevel@tonic-gate /* Bit 33 selects the dimm group/pair */
9790Sstevel@tonic-gate *ds_shift = DIMM_PAIR_SELECT_SHIFT;
9800Sstevel@tonic-gate if (dgrp->nlogbanks == 2) {
9810Sstevel@tonic-gate /* Bit 32 selects the logical bank */
9820Sstevel@tonic-gate *bs_shift = LOG_BANK_SELECT_SHIFT;
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate break;
9850Sstevel@tonic-gate case INTERLEAVE_INTEXT_SAME_DIMM_PAIR:
9860Sstevel@tonic-gate /* Bit 33 selects the dimm group/pair */
9870Sstevel@tonic-gate *ds_shift = DIMM_PAIR_SELECT_SHIFT;
9880Sstevel@tonic-gate if (dgrp->nlogbanks == 2) {
9890Sstevel@tonic-gate /* Row[2] selects the logical bank */
9900Sstevel@tonic-gate *bs_shift = get_row_shift(2, dgrp);
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate break;
9930Sstevel@tonic-gate case INTERLEAVE_INTEXT_BOTH_DIMM_PAIR:
9940Sstevel@tonic-gate if (dgrp->nlogbanks == 2) {
9950Sstevel@tonic-gate /* Row[3] selects the dimm group/pair */
9960Sstevel@tonic-gate *ds_shift = get_row_shift(3, dgrp);
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate /* Row[2] selects the logical bank */
9990Sstevel@tonic-gate *bs_shift = get_row_shift(2, dgrp);
10000Sstevel@tonic-gate } else {
10010Sstevel@tonic-gate /* Row[2] selects the dimm group/pair */
10020Sstevel@tonic-gate *ds_shift = get_row_shift(2, dgrp);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate break;
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate static void
mc_add_xor_banks(struct mctrl_info * mctrl,uint64_t mask,uint64_t match,int interleave)10090Sstevel@tonic-gate mc_add_xor_banks(struct mctrl_info *mctrl,
10100Sstevel@tonic-gate uint64_t mask, uint64_t match, int interleave)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate int i, j, nbits, nbanks;
10130Sstevel@tonic-gate int bankid;
10140Sstevel@tonic-gate int dselect[4];
10150Sstevel@tonic-gate int ds_shift = -1, bs_shift = -1;
10160Sstevel@tonic-gate uint64_t id, size, xmatch;
10170Sstevel@tonic-gate struct bank_info *banki;
10180Sstevel@tonic-gate struct dgrp_info *dgrp;
10190Sstevel@tonic-gate
10200Sstevel@tonic-gate /* xor mode - assume 2 identical dimm-pairs */
10210Sstevel@tonic-gate if ((dgrp = mc_node_get(mctrl->devgrpids[0], dgrp_head)) == NULL) {
10220Sstevel@tonic-gate return;
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate
10250Sstevel@tonic-gate get_device_select(interleave, dgrp, &ds_shift, &bs_shift);
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate mask |= (ds_shift == -1 ? 0 : (1ULL << ds_shift));
10280Sstevel@tonic-gate mask |= (bs_shift == -1 ? 0 : (1ULL << bs_shift));
10290Sstevel@tonic-gate
10300Sstevel@tonic-gate /* xor enable means, bit 21 is used for dimm-pair select */
10310Sstevel@tonic-gate mask |= XOR_DEVICE_SELECT_MASK;
10320Sstevel@tonic-gate if (dgrp->nlogbanks == NLOGBANKS_PER_DGRP) {
10330Sstevel@tonic-gate /* bit 20 is used for logbank select */
10340Sstevel@tonic-gate mask |= XOR_BANK_SELECT_MASK;
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate /* find out the bits set to 1 in mask, nbits can be 2 or 4 */
10380Sstevel@tonic-gate nbits = 0;
10390Sstevel@tonic-gate for (i = 0; i <= DIMM_PAIR_SELECT_SHIFT; i++) {
10400Sstevel@tonic-gate if ((((mask >> i) & 1) == 1) && (nbits < 4)) {
10410Sstevel@tonic-gate dselect[nbits] = i;
10420Sstevel@tonic-gate nbits++;
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate /* number or banks can be 4 or 16 */
10470Sstevel@tonic-gate nbanks = 1 << nbits;
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate size = (dgrp->size * 2)/nbanks;
10500Sstevel@tonic-gate
10510Sstevel@tonic-gate bankid = mctrl->mctrl_node.id * NLOGBANKS_PER_MC;
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate /* each bit position of the mask decides the match & base for bank */
10540Sstevel@tonic-gate for (i = 0; i < nbanks; i++) {
10550Sstevel@tonic-gate xmatch = 0;
10560Sstevel@tonic-gate for (j = 0; j < nbits; j++) {
10570Sstevel@tonic-gate xmatch |= (i & (1ULL << j)) << (dselect[j] - j);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate /* xor ds bits to get the dimm-pair */
10600Sstevel@tonic-gate id = ((xmatch & (1ULL << ds_shift)) >> ds_shift) ^
10616803Spothier ((xmatch & (1ULL << XOR_DEVICE_SELECT_SHIFT)) >>
10626803Spothier XOR_DEVICE_SELECT_SHIFT);
10630Sstevel@tonic-gate banki = mc_add_bank(bankid, mask, match | xmatch, size,
10640Sstevel@tonic-gate mctrl->devgrpids[id]);
10650Sstevel@tonic-gate mc_add_segment(banki);
10660Sstevel@tonic-gate bankid++;
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate * Based on interleave, dimm-densities, part-type determine the mask
10720Sstevel@tonic-gate * and match per bank, construct the logical layout by adding segments
10730Sstevel@tonic-gate * and banks
10740Sstevel@tonic-gate */
10750Sstevel@tonic-gate static int
mc_add_dgrp_banks(uint64_t bankid,uint64_t dgrpid,uint64_t mask,uint64_t match,int interleave)10760Sstevel@tonic-gate mc_add_dgrp_banks(uint64_t bankid, uint64_t dgrpid,
10770Sstevel@tonic-gate uint64_t mask, uint64_t match, int interleave)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate int nbanks = 0;
10800Sstevel@tonic-gate struct bank_info *banki;
10810Sstevel@tonic-gate struct dgrp_info *dgrp;
10820Sstevel@tonic-gate int ds_shift = -1, bs_shift = -1;
10830Sstevel@tonic-gate uint64_t size;
10840Sstevel@tonic-gate uint64_t match_save;
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate if ((dgrp = mc_node_get(dgrpid, dgrp_head)) == NULL) {
10870Sstevel@tonic-gate return (0);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate get_device_select(interleave, dgrp, &ds_shift, &bs_shift);
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate mask |= (ds_shift == -1 ? 0 : (1ULL << ds_shift));
10930Sstevel@tonic-gate mask |= (bs_shift == -1 ? 0 : (1ULL << bs_shift));
10940Sstevel@tonic-gate match |= (ds_shift == -1 ? 0 : ((dgrpid & 1) << ds_shift));
10950Sstevel@tonic-gate match_save = match;
10960Sstevel@tonic-gate size = dgrp->size/dgrp->nlogbanks;
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate /* for bankid 0, 2, 4 .. */
10990Sstevel@tonic-gate match |= (bs_shift == -1 ? 0 : ((bankid & 1) << bs_shift));
11000Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_segments: interleave %d"
11010Sstevel@tonic-gate " mask 0x%lx bs_shift %d match 0x%lx\n",
11020Sstevel@tonic-gate interleave, mask, bs_shift, match));
11030Sstevel@tonic-gate banki = mc_add_bank(bankid, mask, match, size, dgrpid);
11040Sstevel@tonic-gate nbanks++;
11050Sstevel@tonic-gate mc_add_segment(banki);
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate if (dgrp->nlogbanks == 2) {
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate * Set match value to original before adding second
11100Sstevel@tonic-gate * logical bank interleaving information.
11110Sstevel@tonic-gate */
11120Sstevel@tonic-gate match = match_save;
11130Sstevel@tonic-gate bankid++;
11140Sstevel@tonic-gate match |= (bs_shift == -1 ? 0 : ((bankid & 1) << bs_shift));
11150Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_segments: interleave %d"
11160Sstevel@tonic-gate " mask 0x%lx shift %d match 0x%lx\n",
11170Sstevel@tonic-gate interleave, mask, bs_shift, match));
11180Sstevel@tonic-gate banki = mc_add_bank(bankid, mask, match, size, dgrpid);
11190Sstevel@tonic-gate nbanks++;
11200Sstevel@tonic-gate mc_add_segment(banki);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate
11230Sstevel@tonic-gate return (nbanks);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate /*
11270Sstevel@tonic-gate * Construct the logical layout
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate static void
mc_logical_layout(struct mctrl_info * mctrl,struct mc_soft_state * softsp)11300Sstevel@tonic-gate mc_logical_layout(struct mctrl_info *mctrl, struct mc_soft_state *softsp)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate int i;
11330Sstevel@tonic-gate uint64_t mcid, bankid, interleave, mask, match;
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate if (mctrl->ndevgrps == 0)
11360Sstevel@tonic-gate return;
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate mcid = mctrl->mctrl_node.id;
11390Sstevel@tonic-gate mask = MC_SELECT_MASK;
11400Sstevel@tonic-gate match = mcid << MC_SELECT_SHIFT;
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate interleave = (softsp->mcreg1 & MCREG1_INTERLEAVE_MASK) >>
11430Sstevel@tonic-gate MCREG1_INTERLEAVE_SHIFT;
11440Sstevel@tonic-gate
11450Sstevel@tonic-gate /* Two dimm pairs and xor bit set */
11460Sstevel@tonic-gate if (mctrl->ndevgrps == NDGRPS_PER_MC &&
11470Sstevel@tonic-gate (softsp->mcreg1 & MCREG1_XOR_ENABLE)) {
11480Sstevel@tonic-gate mc_add_xor_banks(mctrl, mask, match, interleave);
11490Sstevel@tonic-gate return;
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate * For xor bit unset or only one dimm pair.
11540Sstevel@tonic-gate * In one dimm pair case, even if xor bit is set, xor
11550Sstevel@tonic-gate * interleaving is only taking place in dimm's internal
11560Sstevel@tonic-gate * banks. Dimm and external bank select bits are the
11570Sstevel@tonic-gate * same as those without xor bit set.
11580Sstevel@tonic-gate */
11590Sstevel@tonic-gate bankid = mcid * NLOGBANKS_PER_MC;
11600Sstevel@tonic-gate for (i = 0; i < mctrl->ndevgrps; i++) {
11610Sstevel@tonic-gate bankid += mc_add_dgrp_banks(bankid, mctrl->devgrpids[i],
11626803Spothier mask, match, interleave);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate /*
11670Sstevel@tonic-gate * Get the dimm-pair's size from the reg_info
11680Sstevel@tonic-gate */
11690Sstevel@tonic-gate static uint64_t
get_devgrp_size(uint64_t start)11700Sstevel@tonic-gate get_devgrp_size(uint64_t start)
11710Sstevel@tonic-gate {
11720Sstevel@tonic-gate int i;
11730Sstevel@tonic-gate uint64_t size;
11740Sstevel@tonic-gate uint64_t end, reg_start, reg_end;
11750Sstevel@tonic-gate struct memory_reg_info *regi;
11760Sstevel@tonic-gate
11770Sstevel@tonic-gate /* dgrp end address */
11780Sstevel@tonic-gate end = start + DGRP_SIZE_MAX - 1;
11790Sstevel@tonic-gate
11800Sstevel@tonic-gate regi = reg_info;
11810Sstevel@tonic-gate size = 0;
11820Sstevel@tonic-gate for (i = 0; i < nregs; i++) {
11830Sstevel@tonic-gate reg_start = regi->base;
11840Sstevel@tonic-gate reg_end = regi->base + regi->size - 1;
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate /* completely outside */
11870Sstevel@tonic-gate if ((reg_end < start) || (reg_start > end)) {
11880Sstevel@tonic-gate regi++;
11890Sstevel@tonic-gate continue;
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate /* completely inside */
11930Sstevel@tonic-gate if ((reg_start <= start) && (reg_end >= end)) {
11940Sstevel@tonic-gate return (DGRP_SIZE_MAX);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate /* start is inside, but not the end, get the remainder */
11980Sstevel@tonic-gate if (reg_start < start) {
11990Sstevel@tonic-gate size = regi->size - (start - reg_start);
12000Sstevel@tonic-gate regi++;
12010Sstevel@tonic-gate continue;
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate /* add up size for all within range */
12050Sstevel@tonic-gate size += regi->size;
12060Sstevel@tonic-gate regi++;
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate return (size);
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate
12120Sstevel@tonic-gate /*
12130Sstevel@tonic-gate * Each device group is a pair (dimm-pair) of identical single/dual dimms.
12140Sstevel@tonic-gate * Determine the dimm-pair's dimm-densities and part-type using the MCR-I.
12150Sstevel@tonic-gate */
12160Sstevel@tonic-gate static void
mc_add_devgrp(int dgrpid,struct mc_soft_state * softsp)12170Sstevel@tonic-gate mc_add_devgrp(int dgrpid, struct mc_soft_state *softsp)
12180Sstevel@tonic-gate {
12190Sstevel@tonic-gate int i, mcid, devid, dgrpoffset;
12200Sstevel@tonic-gate struct dgrp_info *dgrp;
12210Sstevel@tonic-gate struct device_info *dev;
12220Sstevel@tonic-gate struct dimm_info *dimmp = (struct dimm_info *)softsp->memlayoutp;
12230Sstevel@tonic-gate
12240Sstevel@tonic-gate mcid = softsp->portid;
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate /* add the entry on dgrp_info list */
12270Sstevel@tonic-gate if ((dgrp = mc_node_get(dgrpid, dgrp_head)) != NULL) {
12280Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_devgrp: devgrp %d exists\n",
12290Sstevel@tonic-gate dgrpid));
12300Sstevel@tonic-gate return;
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate
12330Sstevel@tonic-gate dgrp = kmem_zalloc(sizeof (*dgrp), KM_SLEEP);
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate dgrp->dgrp_node.id = dgrpid;
12360Sstevel@tonic-gate
12370Sstevel@tonic-gate /* a devgrp has identical (type & size) pair */
12380Sstevel@tonic-gate if ((dgrpid & 1) == 0) {
12390Sstevel@tonic-gate /* dimm-pair 0, 2, 4, 6 */
12400Sstevel@tonic-gate if (softsp->mcreg1 & MCREG1_DIMM1_BANK1)
12410Sstevel@tonic-gate dgrp->nlogbanks = 2;
12420Sstevel@tonic-gate else
12430Sstevel@tonic-gate dgrp->nlogbanks = 1;
12440Sstevel@tonic-gate dgrp->base_device = (softsp->mcreg1 & MCREG1_ADDRGEN1_MASK) >>
12450Sstevel@tonic-gate MCREG1_ADDRGEN1_SHIFT;
12460Sstevel@tonic-gate dgrp->part_type = (softsp->mcreg1 & MCREG1_X4DIMM1_MASK) >>
12470Sstevel@tonic-gate MCREG1_X4DIMM1_SHIFT;
12480Sstevel@tonic-gate } else {
12490Sstevel@tonic-gate /* dimm-pair 1, 3, 5, 7 */
12500Sstevel@tonic-gate if (softsp->mcreg1 & MCREG1_DIMM2_BANK3)
12510Sstevel@tonic-gate dgrp->nlogbanks = 2;
12520Sstevel@tonic-gate else
12530Sstevel@tonic-gate dgrp->nlogbanks = 1;
12540Sstevel@tonic-gate dgrp->base_device = (softsp->mcreg1 & MCREG1_ADDRGEN2_MASK) >>
12550Sstevel@tonic-gate MCREG1_ADDRGEN2_SHIFT;
12560Sstevel@tonic-gate dgrp->part_type = (softsp->mcreg1 & MCREG1_X4DIMM2_MASK) >>
12570Sstevel@tonic-gate MCREG1_X4DIMM2_SHIFT;
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate dgrp->base = MC_BASE(mcid) + DGRP_BASE(dgrpid);
12610Sstevel@tonic-gate dgrp->size = get_devgrp_size(dgrp->base);
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_devgrp: id %d size %ld logbanks %d"
12640Sstevel@tonic-gate " base_device %d part_type %d\n", dgrpid, dgrp->size,
12650Sstevel@tonic-gate dgrp->nlogbanks, dgrp->base_device, dgrp->part_type));
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate dgrpoffset = dgrpid % NDGRPS_PER_MC;
12680Sstevel@tonic-gate dgrp->ndevices = NDIMMS_PER_DGRP;
12690Sstevel@tonic-gate /* add the entry for the (identical) pair of dimms/device */
12700Sstevel@tonic-gate for (i = 0; i < NDIMMS_PER_DGRP; i++) {
12710Sstevel@tonic-gate devid = dgrpid * NDIMMS_PER_DGRP + i;
12720Sstevel@tonic-gate dgrp->deviceids[i] = devid;
12730Sstevel@tonic-gate
12740Sstevel@tonic-gate if ((dev = mc_node_get(devid, device_head)) != NULL) {
12750Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_devgrp: device %d "
12760Sstevel@tonic-gate "exists\n", devid));
12770Sstevel@tonic-gate continue;
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate
12800Sstevel@tonic-gate dev = kmem_zalloc(sizeof (*dev), KM_SLEEP);
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate dev->dev_node.id = devid;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate dev->size = dgrp->size/2;
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate if (dimmp) {
12870Sstevel@tonic-gate (void) strncpy(dev->label, (char *)dimmp->label[
12880Sstevel@tonic-gate i + NDIMMS_PER_DGRP * dgrpoffset],
12890Sstevel@tonic-gate MAX_DEVLEN);
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_add_devgrp: dimm %d %s\n",
12920Sstevel@tonic-gate dev->dev_node.id, dev->label));
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate mc_node_add((mc_dlist_t *)dev, &device_head, &device_tail);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate
12980Sstevel@tonic-gate mc_node_add((mc_dlist_t *)dgrp, &dgrp_head, &dgrp_tail);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate /*
13020Sstevel@tonic-gate * Construct the physical and logical layout
13030Sstevel@tonic-gate */
13040Sstevel@tonic-gate static void
mc_construct(struct mc_soft_state * softsp)13050Sstevel@tonic-gate mc_construct(struct mc_soft_state *softsp)
13060Sstevel@tonic-gate {
13070Sstevel@tonic-gate int i, mcid, dgrpid;
13080Sstevel@tonic-gate struct mctrl_info *mctrl;
13090Sstevel@tonic-gate
13100Sstevel@tonic-gate mcid = softsp->portid;
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_construct: mcid %d, mcreg1 0x%lx\n",
13130Sstevel@tonic-gate mcid, softsp->mcreg1));
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate /*
13160Sstevel@tonic-gate * Construct the Physical & Logical Layout
13170Sstevel@tonic-gate */
13180Sstevel@tonic-gate mutex_enter(&mcdatamutex);
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate /* allocate for mctrl_info */
13210Sstevel@tonic-gate if ((mctrl = mc_node_get(mcid, mctrl_head)) != NULL) {
13220Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_construct: mctrl %d exists\n",
13230Sstevel@tonic-gate mcid));
13240Sstevel@tonic-gate mutex_exit(&mcdatamutex);
13250Sstevel@tonic-gate return;
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate mctrl = kmem_zalloc(sizeof (*mctrl), KM_SLEEP);
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate mctrl->mctrl_node.id = mcid;
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate i = 0;
13330Sstevel@tonic-gate dgrpid = mcid * NDGRPS_PER_MC;
13340Sstevel@tonic-gate if (softsp->mcreg1 & MCREG1_DIMM1_BANK0) {
13350Sstevel@tonic-gate mc_add_devgrp(dgrpid, softsp);
13360Sstevel@tonic-gate mctrl->devgrpids[i] = dgrpid;
13370Sstevel@tonic-gate mctrl->ndevgrps++;
13380Sstevel@tonic-gate i++;
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate if (softsp->mcreg1 & MCREG1_DIMM2_BANK2) {
13420Sstevel@tonic-gate dgrpid++;
13430Sstevel@tonic-gate mc_add_devgrp(dgrpid, softsp);
13440Sstevel@tonic-gate mctrl->devgrpids[i] = dgrpid;
13450Sstevel@tonic-gate mctrl->ndevgrps++;
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate
13480Sstevel@tonic-gate mc_logical_layout(mctrl, softsp);
13490Sstevel@tonic-gate
13500Sstevel@tonic-gate mctrl->dimminfop = (struct dimm_info *)softsp->memlayoutp;
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate nmcs++;
13530Sstevel@tonic-gate mc_node_add((mc_dlist_t *)mctrl, &mctrl_head, &mctrl_tail);
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate mutex_exit(&mcdatamutex);
13560Sstevel@tonic-gate
13570Sstevel@tonic-gate DPRINTF(MC_CNSTRC_DEBUG, ("mc_construct: nmcs %d memsize %ld"
13580Sstevel@tonic-gate "nsegments %d\n", nmcs, memsize, nsegments));
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate
13610Sstevel@tonic-gate /*
13620Sstevel@tonic-gate * Delete nodes related to the given MC on mc, device group, device,
13630Sstevel@tonic-gate * and bank lists. Moreover, delete corresponding segment if its connected
13640Sstevel@tonic-gate * banks are all removed.
13650Sstevel@tonic-gate */
13660Sstevel@tonic-gate static void
mc_delete(int mc_id)13670Sstevel@tonic-gate mc_delete(int mc_id)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate int i, j, dgrpid, devid, bankid;
13700Sstevel@tonic-gate struct mctrl_info *mctrl;
13710Sstevel@tonic-gate struct dgrp_info *dgrp;
13720Sstevel@tonic-gate struct device_info *devp;
13730Sstevel@tonic-gate struct seg_info *segi;
13740Sstevel@tonic-gate struct bank_info *banki;
13750Sstevel@tonic-gate
13760Sstevel@tonic-gate mutex_enter(&mcdatamutex);
13770Sstevel@tonic-gate
13780Sstevel@tonic-gate /* delete mctrl_info */
13790Sstevel@tonic-gate if ((mctrl = mc_node_get(mc_id, mctrl_head)) != NULL) {
13800Sstevel@tonic-gate mc_node_del((mc_dlist_t *)mctrl, &mctrl_head, &mctrl_tail);
13810Sstevel@tonic-gate kmem_free(mctrl, sizeof (*mctrl));
13820Sstevel@tonic-gate nmcs--;
13830Sstevel@tonic-gate } else
13840Sstevel@tonic-gate DPRINTF(MC_DESTRC_DEBUG, ("mc_delete: mctrl is not found\n"));
13850Sstevel@tonic-gate
13860Sstevel@tonic-gate /* delete device groups and devices of the detached MC */
13870Sstevel@tonic-gate for (i = 0; i < NDGRPS_PER_MC; i++) {
13880Sstevel@tonic-gate dgrpid = mc_id * NDGRPS_PER_MC + i;
13890Sstevel@tonic-gate if (!(dgrp = mc_node_get(dgrpid, dgrp_head))) {
13900Sstevel@tonic-gate continue;
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate for (j = 0; j < NDIMMS_PER_DGRP; j++) {
13940Sstevel@tonic-gate devid = dgrpid * NDIMMS_PER_DGRP + j;
13950Sstevel@tonic-gate if (devp = mc_node_get(devid, device_head)) {
13960Sstevel@tonic-gate mc_node_del((mc_dlist_t *)devp,
13970Sstevel@tonic-gate &device_head, &device_tail);
13980Sstevel@tonic-gate kmem_free(devp, sizeof (*devp));
13990Sstevel@tonic-gate } else
14000Sstevel@tonic-gate DPRINTF(MC_DESTRC_DEBUG,
14010Sstevel@tonic-gate ("mc_delete: no dev %d\n", devid));
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate
14040Sstevel@tonic-gate mc_node_del((mc_dlist_t *)dgrp, &dgrp_head, &dgrp_tail);
14050Sstevel@tonic-gate kmem_free(dgrp, sizeof (*dgrp));
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate /* delete all banks and associated segments */
14090Sstevel@tonic-gate for (i = 0; i < NLOGBANKS_PER_MC; i++) {
14100Sstevel@tonic-gate bankid = mc_id * NLOGBANKS_PER_MC + i;
14110Sstevel@tonic-gate if (!(banki = mc_node_get(bankid, bank_head))) {
14120Sstevel@tonic-gate continue;
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate /* bank and segments go together */
14161966Sarutz if ((segi = mc_node_get(banki->seg_id, seg_head)) != NULL) {
14170Sstevel@tonic-gate mc_node_del((mc_dlist_t *)segi, &seg_head, &seg_tail);
14180Sstevel@tonic-gate kmem_free(segi, sizeof (*segi));
14190Sstevel@tonic-gate nsegments--;
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate mc_node_del((mc_dlist_t *)banki, &bank_head, &bank_tail);
14230Sstevel@tonic-gate kmem_free(banki, sizeof (*banki));
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate mutex_exit(&mcdatamutex);
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate /*
14300Sstevel@tonic-gate * mc_dlist is a double linking list, including unique id, and pointers to
14310Sstevel@tonic-gate * next, and previous nodes. seg_info, bank_info, dgrp_info, device_info,
14320Sstevel@tonic-gate * and mctrl_info has it at the top to share the operations, add, del, and get.
14330Sstevel@tonic-gate *
14340Sstevel@tonic-gate * The new node is added at the tail and is not sorted.
14350Sstevel@tonic-gate *
14360Sstevel@tonic-gate * Input: The pointer of node to be added, head and tail of the list
14370Sstevel@tonic-gate */
14380Sstevel@tonic-gate
14390Sstevel@tonic-gate static void
mc_node_add(mc_dlist_t * node,mc_dlist_t ** head,mc_dlist_t ** tail)14400Sstevel@tonic-gate mc_node_add(mc_dlist_t *node, mc_dlist_t **head, mc_dlist_t **tail)
14410Sstevel@tonic-gate {
14420Sstevel@tonic-gate DPRINTF(MC_LIST_DEBUG, ("mc_node_add: node->id %d head %p tail %p\n",
14430Sstevel@tonic-gate node->id, (void *) *head, (void *) *tail));
14440Sstevel@tonic-gate
14450Sstevel@tonic-gate if (*head != NULL) {
14460Sstevel@tonic-gate node->prev = *tail;
14470Sstevel@tonic-gate node->next = (*tail)->next;
14480Sstevel@tonic-gate (*tail)->next = node;
14490Sstevel@tonic-gate *tail = node;
14500Sstevel@tonic-gate } else {
14510Sstevel@tonic-gate node->next = node->prev = NULL;
14520Sstevel@tonic-gate *head = *tail = node;
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate }
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate /*
14570Sstevel@tonic-gate * Input: The pointer of node to be deleted, head and tail of the list
14580Sstevel@tonic-gate *
14590Sstevel@tonic-gate * Deleted node will be at the following positions
14600Sstevel@tonic-gate * 1. At the tail of the list
14610Sstevel@tonic-gate * 2. At the head of the list
14620Sstevel@tonic-gate * 3. At the head and tail of the list, i.e. only one left.
14630Sstevel@tonic-gate * 4. At the middle of the list
14640Sstevel@tonic-gate */
14650Sstevel@tonic-gate
14660Sstevel@tonic-gate static void
mc_node_del(mc_dlist_t * node,mc_dlist_t ** head,mc_dlist_t ** tail)14670Sstevel@tonic-gate mc_node_del(mc_dlist_t *node, mc_dlist_t **head, mc_dlist_t **tail)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate if (node->next == NULL) {
14700Sstevel@tonic-gate /* deleted node is at the tail of list */
14710Sstevel@tonic-gate *tail = node->prev;
14720Sstevel@tonic-gate } else {
14730Sstevel@tonic-gate node->next->prev = node->prev;
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate
14760Sstevel@tonic-gate if (node->prev == NULL) {
14770Sstevel@tonic-gate /* deleted node is at the head of list */
14780Sstevel@tonic-gate *head = node->next;
14790Sstevel@tonic-gate } else {
14800Sstevel@tonic-gate node->prev->next = node->next;
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate
14840Sstevel@tonic-gate /*
14850Sstevel@tonic-gate * Search the list from the head of the list to match the given id
14860Sstevel@tonic-gate * Input: id and the head of the list
14870Sstevel@tonic-gate * Return: pointer of found node
14880Sstevel@tonic-gate */
14890Sstevel@tonic-gate static void *
mc_node_get(int id,mc_dlist_t * head)14900Sstevel@tonic-gate mc_node_get(int id, mc_dlist_t *head)
14910Sstevel@tonic-gate {
14920Sstevel@tonic-gate mc_dlist_t *node;
14930Sstevel@tonic-gate
14940Sstevel@tonic-gate node = head;
14950Sstevel@tonic-gate while (node != NULL) {
14960Sstevel@tonic-gate DPRINTF(MC_LIST_DEBUG, ("mc_node_get: id %d, given id %d\n",
14970Sstevel@tonic-gate node->id, id));
14980Sstevel@tonic-gate if (node->id == id)
14990Sstevel@tonic-gate break;
15000Sstevel@tonic-gate node = node->next;
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate return (node);
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate
15050Sstevel@tonic-gate /*
15060Sstevel@tonic-gate * Memory subsystem provides 144 bits (128 Data bits, 9 ECC bits and 7
15070Sstevel@tonic-gate * unused bits) interface via a pair of DIMMs. Mapping of Data/ECC bits
15080Sstevel@tonic-gate * to a specific DIMM pin is described by the memory-layout property
15090Sstevel@tonic-gate * via two tables: dimm table and pin table.
15100Sstevel@tonic-gate *
15110Sstevel@tonic-gate * Memory-layout property arranges data/ecc bits in the following order:
15120Sstevel@tonic-gate *
15130Sstevel@tonic-gate * Bit# 143 16 15 7 6 0
15140Sstevel@tonic-gate * | Data[127:0] | ECC[8:0] | Unused[6:0] |
15150Sstevel@tonic-gate *
15160Sstevel@tonic-gate * dimm table: 1 bit is used to store DIMM number (2 possible DIMMs) for
15170Sstevel@tonic-gate * each Data/ECC bit. Thus, it needs 18 bytes (144/8) to represent
15180Sstevel@tonic-gate * all Data/ECC bits in this table. Information is stored in big
15190Sstevel@tonic-gate * endian order, i.e. dimm_table[0] represents information for
15200Sstevel@tonic-gate * logical bit# 143 to 136.
15210Sstevel@tonic-gate *
15220Sstevel@tonic-gate * pin table: 1 byte is used to store pin position for each Data/ECC bit.
15230Sstevel@tonic-gate * Thus, this table is 144 bytes long. Information is stored in little
15240Sstevel@tonic-gate * endian order, i.e, pin_table[0] represents pin number of logical
15250Sstevel@tonic-gate * bit 0 and pin_table[143] contains pin number for logical bit 143
15260Sstevel@tonic-gate * (i.e. data bit# 127).
15270Sstevel@tonic-gate *
15280Sstevel@tonic-gate * qwordmap table below is used to map mc_get_mem_unum "synd_code" value into
15290Sstevel@tonic-gate * logical bit position assigned above by the memory-layout property.
15300Sstevel@tonic-gate */
15310Sstevel@tonic-gate
15320Sstevel@tonic-gate #define QWORD_SIZE 144
15330Sstevel@tonic-gate static uint8_t qwordmap[QWORD_SIZE] =
15340Sstevel@tonic-gate {
15350Sstevel@tonic-gate 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
15360Sstevel@tonic-gate 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
15370Sstevel@tonic-gate 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
15380Sstevel@tonic-gate 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
15390Sstevel@tonic-gate 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
15400Sstevel@tonic-gate 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
15410Sstevel@tonic-gate 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
15420Sstevel@tonic-gate 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
15430Sstevel@tonic-gate 7, 8, 9, 10, 11, 12, 13, 14, 15, 4, 5, 6, 0, 1, 2, 3
15440Sstevel@tonic-gate };
15450Sstevel@tonic-gate
15460Sstevel@tonic-gate
15470Sstevel@tonic-gate /* ARGSUSED */
15480Sstevel@tonic-gate static int
mc_get_mem_unum(int synd_code,uint64_t paddr,char * buf,int buflen,int * lenp)15490Sstevel@tonic-gate mc_get_mem_unum(int synd_code, uint64_t paddr, char *buf, int buflen, int *lenp)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate int i;
15520Sstevel@tonic-gate int pos_cacheline, position, index, idx4dimm;
15530Sstevel@tonic-gate int qwlayout = synd_code;
15540Sstevel@tonic-gate short offset, data;
15550Sstevel@tonic-gate char unum[UNUM_NAMLEN];
15560Sstevel@tonic-gate struct dimm_info *dimmp;
15570Sstevel@tonic-gate struct pin_info *pinp;
15580Sstevel@tonic-gate struct bank_info *bank;
15590Sstevel@tonic-gate struct mctrl_info *mctrl;
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate /*
15620Sstevel@tonic-gate * Enforce old Openboot requirement for synd code, either a single-bit
15630Sstevel@tonic-gate * code from 0..QWORD_SIZE-1 or -1 (multi-bit error).
15640Sstevel@tonic-gate */
15650Sstevel@tonic-gate if (qwlayout < -1 || qwlayout >= QWORD_SIZE)
15660Sstevel@tonic-gate return (EINVAL);
15670Sstevel@tonic-gate
15680Sstevel@tonic-gate unum[0] = '\0';
15690Sstevel@tonic-gate
15700Sstevel@tonic-gate DPRINTF(MC_GUNUM_DEBUG, ("mc_get_mem_unum:qwlayout %d phyaddr 0x%lx\n",
15710Sstevel@tonic-gate qwlayout, paddr));
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate * Scan all logical banks to get one responding to the physical
15750Sstevel@tonic-gate * address. Then compute the index to look up dimm and pin tables
15760Sstevel@tonic-gate * to generate the unmuber.
15770Sstevel@tonic-gate */
15780Sstevel@tonic-gate mutex_enter(&mcdatamutex);
15790Sstevel@tonic-gate bank = (struct bank_info *)bank_head;
15800Sstevel@tonic-gate while (bank != NULL) {
15810Sstevel@tonic-gate int mcid, mcdgrpid, dimmoffset;
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate * Physical Address is in a bank if (Addr & Mask) == Match
15850Sstevel@tonic-gate */
15860Sstevel@tonic-gate if ((paddr & bank->mask) != bank->match) {
15870Sstevel@tonic-gate bank = (struct bank_info *)bank->bank_node.next;
15880Sstevel@tonic-gate continue;
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate mcid = bank->bank_node.id / NLOGBANKS_PER_MC;
15920Sstevel@tonic-gate mctrl = mc_node_get(mcid, mctrl_head);
15930Sstevel@tonic-gate ASSERT(mctrl != NULL);
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate DPRINTF(MC_GUNUM_DEBUG, ("mc_get_mem_unum:mc %d bank %d "
15960Sstevel@tonic-gate "dgrp %d\n", mcid, bank->bank_node.id, bank->devgrp_id));
15970Sstevel@tonic-gate
15980Sstevel@tonic-gate mcdgrpid = bank->devgrp_id % NDGRPS_PER_MC;
15990Sstevel@tonic-gate dimmoffset = mcdgrpid * NDIMMS_PER_DGRP;
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate dimmp = (struct dimm_info *)mctrl->dimminfop;
16020Sstevel@tonic-gate if (dimmp == NULL) {
16030Sstevel@tonic-gate mutex_exit(&mcdatamutex);
16040Sstevel@tonic-gate return (ENXIO);
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate
16070Sstevel@tonic-gate if ((qwlayout >= 0) && (qwlayout < QWORD_SIZE)) {
16080Sstevel@tonic-gate /*
16090Sstevel@tonic-gate * single-bit error handling, we can identify specific
16100Sstevel@tonic-gate * DIMM.
16110Sstevel@tonic-gate */
16120Sstevel@tonic-gate
16130Sstevel@tonic-gate pinp = (struct pin_info *)&dimmp->data[0];
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate pos_cacheline = qwordmap[qwlayout];
16160Sstevel@tonic-gate position = 143 - pos_cacheline;
16170Sstevel@tonic-gate index = position / 8;
16180Sstevel@tonic-gate offset = 7 - (position % 8);
16190Sstevel@tonic-gate
16200Sstevel@tonic-gate DPRINTF(MC_GUNUM_DEBUG, ("mc_get_mem_unum:position "
16210Sstevel@tonic-gate "%d\n", position));
16220Sstevel@tonic-gate /*
16230Sstevel@tonic-gate * Trade-off: We cound't add pin number to
16240Sstevel@tonic-gate * unumber string because statistic number
16250Sstevel@tonic-gate * pumps up at the corresponding dimm not pin.
16260Sstevel@tonic-gate * (void) sprintf(unum, "Pin %1u ", (uint_t)
16270Sstevel@tonic-gate * pinp->pintable[pos_cacheline]);
16280Sstevel@tonic-gate */
16290Sstevel@tonic-gate DPRINTF(MC_GUNUM_DEBUG, ("mc_get_mem_unum:pin number "
16300Sstevel@tonic-gate "%1u\n", (uint_t)pinp->pintable[pos_cacheline]));
16310Sstevel@tonic-gate data = pinp->dimmtable[index];
16320Sstevel@tonic-gate idx4dimm = (data >> offset) & 1;
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate (void) strncpy(unum,
16350Sstevel@tonic-gate (char *)dimmp->label[dimmoffset + idx4dimm],
16360Sstevel@tonic-gate UNUM_NAMLEN);
16370Sstevel@tonic-gate
16380Sstevel@tonic-gate DPRINTF(MC_GUNUM_DEBUG,
16396803Spothier ("mc_get_mem_unum:unum %s\n", unum));
16400Sstevel@tonic-gate
16410Sstevel@tonic-gate /*
16420Sstevel@tonic-gate * platform hook for adding label information to unum.
16430Sstevel@tonic-gate */
16440Sstevel@tonic-gate mc_add_mem_unum_label(unum, mcid, mcdgrpid, idx4dimm);
16450Sstevel@tonic-gate } else {
16460Sstevel@tonic-gate char *p = unum;
16470Sstevel@tonic-gate size_t res = UNUM_NAMLEN;
16480Sstevel@tonic-gate
16490Sstevel@tonic-gate /*
16500Sstevel@tonic-gate * multi-bit error handling, we can only identify
16510Sstevel@tonic-gate * bank of DIMMs.
16520Sstevel@tonic-gate */
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate for (i = 0; (i < NDIMMS_PER_DGRP) && (res > 0); i++) {
16550Sstevel@tonic-gate (void) snprintf(p, res, "%s%s",
16560Sstevel@tonic-gate i == 0 ? "" : " ",
16570Sstevel@tonic-gate (char *)dimmp->label[dimmoffset + i]);
16580Sstevel@tonic-gate res -= strlen(p);
16590Sstevel@tonic-gate p += strlen(p);
16600Sstevel@tonic-gate }
16610Sstevel@tonic-gate
16620Sstevel@tonic-gate /*
16630Sstevel@tonic-gate * platform hook for adding label information
16640Sstevel@tonic-gate * to unum.
16650Sstevel@tonic-gate */
16660Sstevel@tonic-gate mc_add_mem_unum_label(unum, mcid, mcdgrpid, -1);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate mutex_exit(&mcdatamutex);
16690Sstevel@tonic-gate if ((strlen(unum) >= UNUM_NAMLEN) ||
16700Sstevel@tonic-gate (strlen(unum) >= buflen)) {
16716803Spothier return (ENAMETOOLONG);
16720Sstevel@tonic-gate } else {
16730Sstevel@tonic-gate (void) strncpy(buf, unum, UNUM_NAMLEN);
16740Sstevel@tonic-gate *lenp = strlen(buf);
16750Sstevel@tonic-gate return (0);
16760Sstevel@tonic-gate }
16770Sstevel@tonic-gate } /* end of while loop for logic bank list */
16780Sstevel@tonic-gate
16790Sstevel@tonic-gate mutex_exit(&mcdatamutex);
16800Sstevel@tonic-gate return (ENXIO);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate
16830Sstevel@tonic-gate static int
mc_get_mem_info(int synd_code,uint64_t paddr,uint64_t * mem_sizep,uint64_t * seg_sizep,uint64_t * bank_sizep,int * segsp,int * banksp,int * mcidp)16840Sstevel@tonic-gate mc_get_mem_info(int synd_code, uint64_t paddr,
16850Sstevel@tonic-gate uint64_t *mem_sizep, uint64_t *seg_sizep, uint64_t *bank_sizep,
16860Sstevel@tonic-gate int *segsp, int *banksp, int *mcidp)
16870Sstevel@tonic-gate {
16880Sstevel@tonic-gate struct bank_info *bankp;
16890Sstevel@tonic-gate
16900Sstevel@tonic-gate if (synd_code < -1 || synd_code >= QWORD_SIZE)
16910Sstevel@tonic-gate return (EINVAL);
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate /*
16940Sstevel@tonic-gate * Scan all logical banks to get one responding to the physical
16950Sstevel@tonic-gate * address. Then compute the index to look up dimm and pin tables
16960Sstevel@tonic-gate * to generate the unmuber.
16970Sstevel@tonic-gate */
16980Sstevel@tonic-gate mutex_enter(&mcdatamutex);
16990Sstevel@tonic-gate bankp = (struct bank_info *)bank_head;
17000Sstevel@tonic-gate while (bankp != NULL) {
17010Sstevel@tonic-gate struct seg_info *segp;
17020Sstevel@tonic-gate int mcid;
17030Sstevel@tonic-gate
17040Sstevel@tonic-gate /*
17050Sstevel@tonic-gate * Physical Address is in a bank if (Addr & Mask) == Match
17060Sstevel@tonic-gate */
17070Sstevel@tonic-gate if ((paddr & bankp->mask) != bankp->match) {
17080Sstevel@tonic-gate bankp = (struct bank_info *)bankp->bank_node.next;
17090Sstevel@tonic-gate continue;
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate mcid = bankp->bank_node.id / NLOGBANKS_PER_MC;
17130Sstevel@tonic-gate
17140Sstevel@tonic-gate /*
17150Sstevel@tonic-gate * Get the corresponding segment.
17160Sstevel@tonic-gate */
17170Sstevel@tonic-gate if ((segp = (struct seg_info *)mc_node_get(bankp->seg_id,
17180Sstevel@tonic-gate seg_head)) == NULL) {
17190Sstevel@tonic-gate mutex_exit(&mcdatamutex);
17200Sstevel@tonic-gate return (EFAULT);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate
17230Sstevel@tonic-gate *mem_sizep = memsize;
17240Sstevel@tonic-gate *seg_sizep = segp->size;
17250Sstevel@tonic-gate *bank_sizep = bankp->size;
17260Sstevel@tonic-gate *segsp = nsegments;
17270Sstevel@tonic-gate *banksp = segp->nbanks;
17280Sstevel@tonic-gate *mcidp = mcid;
17290Sstevel@tonic-gate
17300Sstevel@tonic-gate mutex_exit(&mcdatamutex);
17310Sstevel@tonic-gate return (0);
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate } /* end of while loop for logic bank list */
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate mutex_exit(&mcdatamutex);
17360Sstevel@tonic-gate return (ENXIO);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate /*
17390Sstevel@tonic-gate * mc-us3i driver allows a platform to add extra label
17400Sstevel@tonic-gate * information to the unum string. If a platform implements a
17410Sstevel@tonic-gate * kernel function called plat_add_mem_unum_label() it will be
17420Sstevel@tonic-gate * executed. This would typically be implemented in the platmod.
17430Sstevel@tonic-gate */
17440Sstevel@tonic-gate static void
mc_add_mem_unum_label(char * unum,int mcid,int bank,int dimm)17450Sstevel@tonic-gate mc_add_mem_unum_label(char *unum, int mcid, int bank, int dimm)
17460Sstevel@tonic-gate {
17470Sstevel@tonic-gate if (&plat_add_mem_unum_label)
17480Sstevel@tonic-gate plat_add_mem_unum_label(unum, mcid, bank, dimm);
17490Sstevel@tonic-gate }
1750