125cf1a30Sjl /* 225cf1a30Sjl * CDDL HEADER START 325cf1a30Sjl * 425cf1a30Sjl * The contents of this file are subject to the terms of the 525cf1a30Sjl * Common Development and Distribution License (the "License"). 625cf1a30Sjl * You may not use this file except in compliance with the License. 725cf1a30Sjl * 825cf1a30Sjl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 925cf1a30Sjl * or http://www.opensolaris.org/os/licensing. 1025cf1a30Sjl * See the License for the specific language governing permissions 1125cf1a30Sjl * and limitations under the License. 1225cf1a30Sjl * 1325cf1a30Sjl * When distributing Covered Code, include this CDDL HEADER in each 1425cf1a30Sjl * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1525cf1a30Sjl * If applicable, add the following below this CDDL HEADER, with the 1625cf1a30Sjl * fields enclosed by brackets "[]" replaced with your own identifying 1725cf1a30Sjl * information: Portions Copyright [yyyy] [name of copyright owner] 1825cf1a30Sjl * 1925cf1a30Sjl * CDDL HEADER END 2025cf1a30Sjl */ 2125cf1a30Sjl /* 2268ac2337Sjl * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 2325cf1a30Sjl * Use is subject to license terms. 2425cf1a30Sjl */ 2525cf1a30Sjl 2625cf1a30Sjl #pragma ident "%Z%%M% %I% %E% SMI" 2725cf1a30Sjl 2825cf1a30Sjl /* 2925cf1a30Sjl * DR memory support routines. 3025cf1a30Sjl */ 3125cf1a30Sjl 3225cf1a30Sjl #include <sys/note.h> 3325cf1a30Sjl #include <sys/debug.h> 3425cf1a30Sjl #include <sys/types.h> 3525cf1a30Sjl #include <sys/errno.h> 3625cf1a30Sjl #include <sys/param.h> 3725cf1a30Sjl #include <sys/dditypes.h> 3825cf1a30Sjl #include <sys/kmem.h> 3925cf1a30Sjl #include <sys/conf.h> 4025cf1a30Sjl #include <sys/ddi.h> 4125cf1a30Sjl #include <sys/sunddi.h> 4225cf1a30Sjl #include <sys/sunndi.h> 4325cf1a30Sjl #include <sys/ddi_impldefs.h> 4425cf1a30Sjl #include <sys/ndi_impldefs.h> 4525cf1a30Sjl #include <sys/sysmacros.h> 4625cf1a30Sjl #include <sys/machsystm.h> 4725cf1a30Sjl #include <sys/spitregs.h> 4825cf1a30Sjl #include <sys/cpuvar.h> 4925cf1a30Sjl #include <sys/promif.h> 5025cf1a30Sjl #include <vm/seg_kmem.h> 5125cf1a30Sjl #include <sys/lgrp.h> 5225cf1a30Sjl #include <sys/platform_module.h> 5325cf1a30Sjl 5425cf1a30Sjl #include <vm/page.h> 5525cf1a30Sjl 5625cf1a30Sjl #include <sys/dr.h> 5725cf1a30Sjl #include <sys/dr_util.h> 5825cf1a30Sjl #include <sys/drmach.h> 5968ac2337Sjl #include <sys/kobj.h> 6025cf1a30Sjl 6125cf1a30Sjl extern struct memlist *phys_install; 6268ac2337Sjl extern vnode_t *retired_pages; 6325cf1a30Sjl 6425cf1a30Sjl /* TODO: push this reference below drmach line */ 6525cf1a30Sjl extern int kcage_on; 6625cf1a30Sjl 6725cf1a30Sjl /* for the DR*INTERNAL_ERROR macros. see sys/dr.h. */ 6868ac2337Sjl static char *dr_ie_fmt = "dr_mem.c %d"; 6925cf1a30Sjl 7025cf1a30Sjl typedef enum { 7125cf1a30Sjl DR_TP_INVALID = -1, 7225cf1a30Sjl DR_TP_SAME, 7325cf1a30Sjl DR_TP_LARGE, 7425cf1a30Sjl DR_TP_NONRELOC, 7525cf1a30Sjl DR_TP_FLOATING 7625cf1a30Sjl } dr_target_pref_t; 7725cf1a30Sjl 7825cf1a30Sjl static int dr_post_detach_mem_unit(dr_mem_unit_t *mp); 7925cf1a30Sjl static int dr_reserve_mem_spans(memhandle_t *mhp, 8025cf1a30Sjl struct memlist *mlist); 8125cf1a30Sjl static int dr_select_mem_target(dr_handle_t *hp, 8225cf1a30Sjl dr_mem_unit_t *mp, struct memlist *ml); 8325cf1a30Sjl static void dr_init_mem_unit_data(dr_mem_unit_t *mp); 8425cf1a30Sjl static struct memlist *dr_memlist_del_retired_pages(struct memlist *ml); 8525cf1a30Sjl static dr_target_pref_t dr_get_target_preference(dr_handle_t *hp, 8625cf1a30Sjl dr_mem_unit_t *t_mp, dr_mem_unit_t *s_mp, 8725cf1a30Sjl struct memlist *s_ml, struct memlist *x_ml, 8825cf1a30Sjl struct memlist *b_ml); 8925cf1a30Sjl 9025cf1a30Sjl static int memlist_canfit(struct memlist *s_mlist, 9125cf1a30Sjl struct memlist *t_mlist); 9225cf1a30Sjl static int dr_del_mlist_query(struct memlist *mlist, 9325cf1a30Sjl memquery_t *mp); 9425cf1a30Sjl static struct memlist *dr_get_copy_mlist(struct memlist *s_ml, 9525cf1a30Sjl struct memlist *t_ml, dr_mem_unit_t *s_mp, 9625cf1a30Sjl dr_mem_unit_t *t_mp); 9725cf1a30Sjl static struct memlist *dr_get_nonreloc_mlist(struct memlist *s_ml, 9825cf1a30Sjl dr_mem_unit_t *s_mp); 9925cf1a30Sjl static int dr_memlist_canfit(struct memlist *s_mlist, 10025cf1a30Sjl struct memlist *t_mlist, dr_mem_unit_t *s_mp, 10125cf1a30Sjl dr_mem_unit_t *t_mp); 10225cf1a30Sjl 10325cf1a30Sjl /* 10425cf1a30Sjl * dr_mem_unit_t.sbm_flags 10525cf1a30Sjl */ 10625cf1a30Sjl #define DR_MFLAG_RESERVED 0x01 /* mem unit reserved for delete */ 10725cf1a30Sjl #define DR_MFLAG_SOURCE 0x02 /* source brd of copy/rename op */ 10825cf1a30Sjl #define DR_MFLAG_TARGET 0x04 /* target brd of copy/rename op */ 10925cf1a30Sjl #define DR_MFLAG_RELOWNER 0x20 /* memory release (delete) owner */ 11025cf1a30Sjl #define DR_MFLAG_RELDONE 0x40 /* memory release (delete) done */ 11125cf1a30Sjl 11225cf1a30Sjl /* helper macros */ 11325cf1a30Sjl #define _ptob64(p) ((uint64_t)(p) << PAGESHIFT) 11425cf1a30Sjl #define _b64top(b) ((pgcnt_t)((b) >> PAGESHIFT)) 11525cf1a30Sjl 11625cf1a30Sjl static struct memlist * 11725cf1a30Sjl dr_get_memlist(dr_mem_unit_t *mp) 11825cf1a30Sjl { 11925cf1a30Sjl struct memlist *mlist = NULL; 12025cf1a30Sjl sbd_error_t *err; 12125cf1a30Sjl static fn_t f = "dr_get_memlist"; 12225cf1a30Sjl 12325cf1a30Sjl PR_MEM("%s for %s...\n", f, mp->sbm_cm.sbdev_path); 12425cf1a30Sjl 12525cf1a30Sjl /* 12625cf1a30Sjl * Return cached memlist, if present. 12725cf1a30Sjl * This memlist will be present following an 12825cf1a30Sjl * unconfigure (a.k.a: detach) of this memunit. 12925cf1a30Sjl * It should only be used in the case were a configure 13025cf1a30Sjl * is bringing this memunit back in without going 13125cf1a30Sjl * through the disconnect and connect states. 13225cf1a30Sjl */ 13325cf1a30Sjl if (mp->sbm_mlist) { 13425cf1a30Sjl PR_MEM("%s: found cached memlist\n", f); 13525cf1a30Sjl 13625cf1a30Sjl mlist = memlist_dup(mp->sbm_mlist); 13725cf1a30Sjl } else { 13825cf1a30Sjl uint64_t basepa = _ptob64(mp->sbm_basepfn); 13925cf1a30Sjl 14025cf1a30Sjl /* attempt to construct a memlist using phys_install */ 14125cf1a30Sjl 14225cf1a30Sjl /* round down to slice base address */ 14325cf1a30Sjl basepa &= ~(mp->sbm_slice_size - 1); 14425cf1a30Sjl 14525cf1a30Sjl /* get a copy of phys_install to edit */ 14625cf1a30Sjl memlist_read_lock(); 14725cf1a30Sjl mlist = memlist_dup(phys_install); 14825cf1a30Sjl memlist_read_unlock(); 14925cf1a30Sjl 15025cf1a30Sjl /* trim lower irrelevant span */ 15125cf1a30Sjl if (mlist) 15225cf1a30Sjl mlist = memlist_del_span(mlist, 0ull, basepa); 15325cf1a30Sjl 15425cf1a30Sjl /* trim upper irrelevant span */ 15525cf1a30Sjl if (mlist) { 15625cf1a30Sjl uint64_t endpa; 15725cf1a30Sjl 15825cf1a30Sjl basepa += mp->sbm_slice_size; 15925cf1a30Sjl endpa = _ptob64(physmax + 1); 16025cf1a30Sjl if (endpa > basepa) 16125cf1a30Sjl mlist = memlist_del_span( 162*3103d4ceSjesusm mlist, basepa, 163*3103d4ceSjesusm endpa - basepa); 16425cf1a30Sjl } 16525cf1a30Sjl 16625cf1a30Sjl if (mlist) { 16725cf1a30Sjl /* successfully built a memlist */ 16825cf1a30Sjl PR_MEM("%s: derived memlist from phys_install\n", f); 16925cf1a30Sjl } 17025cf1a30Sjl 17125cf1a30Sjl /* if no mlist yet, try platform layer */ 17225cf1a30Sjl if (!mlist) { 17325cf1a30Sjl err = drmach_mem_get_memlist( 174*3103d4ceSjesusm mp->sbm_cm.sbdev_id, &mlist); 17525cf1a30Sjl if (err) { 17625cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 17725cf1a30Sjl mlist = NULL; /* paranoia */ 17825cf1a30Sjl } 17925cf1a30Sjl } 18025cf1a30Sjl } 18125cf1a30Sjl 18225cf1a30Sjl PR_MEM("%s: memlist for %s\n", f, mp->sbm_cm.sbdev_path); 18325cf1a30Sjl PR_MEMLIST_DUMP(mlist); 18425cf1a30Sjl 18525cf1a30Sjl return (mlist); 18625cf1a30Sjl } 18725cf1a30Sjl 18825cf1a30Sjl typedef struct { 18925cf1a30Sjl kcondvar_t cond; 19025cf1a30Sjl kmutex_t lock; 19125cf1a30Sjl int error; 19225cf1a30Sjl int done; 19325cf1a30Sjl } dr_release_mem_sync_t; 19425cf1a30Sjl 19525cf1a30Sjl /* 19625cf1a30Sjl * Memory has been logically removed by the time this routine is called. 19725cf1a30Sjl */ 19825cf1a30Sjl static void 19925cf1a30Sjl dr_mem_del_done(void *arg, int error) 20025cf1a30Sjl { 20125cf1a30Sjl dr_release_mem_sync_t *ds = arg; 20225cf1a30Sjl 20325cf1a30Sjl mutex_enter(&ds->lock); 20425cf1a30Sjl ds->error = error; 20525cf1a30Sjl ds->done = 1; 20625cf1a30Sjl cv_signal(&ds->cond); 20725cf1a30Sjl mutex_exit(&ds->lock); 20825cf1a30Sjl } 20925cf1a30Sjl 21025cf1a30Sjl /* 21125cf1a30Sjl * When we reach here the memory being drained should have 21225cf1a30Sjl * already been reserved in dr_pre_release_mem(). 21325cf1a30Sjl * Our only task here is to kick off the "drain" and wait 21425cf1a30Sjl * for it to finish. 21525cf1a30Sjl */ 21625cf1a30Sjl void 21725cf1a30Sjl dr_release_mem(dr_common_unit_t *cp) 21825cf1a30Sjl { 21925cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)cp; 22025cf1a30Sjl int err; 22125cf1a30Sjl dr_release_mem_sync_t rms; 22225cf1a30Sjl static fn_t f = "dr_release_mem"; 22325cf1a30Sjl 22425cf1a30Sjl /* check that this memory unit has been reserved */ 22525cf1a30Sjl if (!(mp->sbm_flags & DR_MFLAG_RELOWNER)) { 22625cf1a30Sjl DR_DEV_INTERNAL_ERROR(&mp->sbm_cm); 22725cf1a30Sjl return; 22825cf1a30Sjl } 22925cf1a30Sjl 23025cf1a30Sjl bzero((void *) &rms, sizeof (rms)); 23125cf1a30Sjl 23225cf1a30Sjl mutex_init(&rms.lock, NULL, MUTEX_DRIVER, NULL); 23325cf1a30Sjl cv_init(&rms.cond, NULL, CV_DRIVER, NULL); 23425cf1a30Sjl 23525cf1a30Sjl mutex_enter(&rms.lock); 23625cf1a30Sjl err = kphysm_del_start(mp->sbm_memhandle, 237*3103d4ceSjesusm dr_mem_del_done, (void *) &rms); 23825cf1a30Sjl if (err == KPHYSM_OK) { 23925cf1a30Sjl /* wait for completion or interrupt */ 24025cf1a30Sjl while (!rms.done) { 24125cf1a30Sjl if (cv_wait_sig(&rms.cond, &rms.lock) == 0) { 24225cf1a30Sjl /* then there is a pending UNIX signal */ 24325cf1a30Sjl (void) kphysm_del_cancel(mp->sbm_memhandle); 24425cf1a30Sjl 24525cf1a30Sjl /* wait for completion */ 24625cf1a30Sjl while (!rms.done) 24725cf1a30Sjl cv_wait(&rms.cond, &rms.lock); 24825cf1a30Sjl } 24925cf1a30Sjl } 25025cf1a30Sjl /* get the result of the memory delete operation */ 25125cf1a30Sjl err = rms.error; 25225cf1a30Sjl } 25325cf1a30Sjl mutex_exit(&rms.lock); 25425cf1a30Sjl 25525cf1a30Sjl cv_destroy(&rms.cond); 25625cf1a30Sjl mutex_destroy(&rms.lock); 25725cf1a30Sjl 25825cf1a30Sjl if (err != KPHYSM_OK) { 25925cf1a30Sjl int e_code; 26025cf1a30Sjl 26125cf1a30Sjl switch (err) { 26225cf1a30Sjl case KPHYSM_ENOWORK: 26325cf1a30Sjl e_code = ESBD_NOERROR; 26425cf1a30Sjl break; 26525cf1a30Sjl 26625cf1a30Sjl case KPHYSM_EHANDLE: 26725cf1a30Sjl case KPHYSM_ESEQUENCE: 26825cf1a30Sjl e_code = ESBD_INTERNAL; 26925cf1a30Sjl break; 27025cf1a30Sjl 27125cf1a30Sjl case KPHYSM_ENOTVIABLE: 27225cf1a30Sjl e_code = ESBD_MEM_NOTVIABLE; 27325cf1a30Sjl break; 27425cf1a30Sjl 27525cf1a30Sjl case KPHYSM_EREFUSED: 27625cf1a30Sjl e_code = ESBD_MEM_REFUSED; 27725cf1a30Sjl break; 27825cf1a30Sjl 27925cf1a30Sjl case KPHYSM_ENONRELOC: 28025cf1a30Sjl e_code = ESBD_MEM_NONRELOC; 28125cf1a30Sjl break; 28225cf1a30Sjl 28325cf1a30Sjl case KPHYSM_ECANCELLED: 28425cf1a30Sjl e_code = ESBD_MEM_CANCELLED; 28525cf1a30Sjl break; 28625cf1a30Sjl 28725cf1a30Sjl case KPHYSM_ERESOURCE: 28825cf1a30Sjl e_code = ESBD_MEMFAIL; 28925cf1a30Sjl break; 29025cf1a30Sjl 29125cf1a30Sjl default: 29225cf1a30Sjl cmn_err(CE_WARN, 293*3103d4ceSjesusm "%s: unexpected kphysm error code %d," 294*3103d4ceSjesusm " id 0x%p", 295*3103d4ceSjesusm f, err, mp->sbm_cm.sbdev_id); 29625cf1a30Sjl 29725cf1a30Sjl e_code = ESBD_IO; 29825cf1a30Sjl break; 29925cf1a30Sjl } 30025cf1a30Sjl 30125cf1a30Sjl if (e_code != ESBD_NOERROR) { 302b307f191Sbm dr_dev_err(CE_WARN, &mp->sbm_cm, e_code); 30325cf1a30Sjl } 30425cf1a30Sjl } 30525cf1a30Sjl } 30625cf1a30Sjl 30725cf1a30Sjl void 30825cf1a30Sjl dr_attach_mem(dr_handle_t *hp, dr_common_unit_t *cp) 30925cf1a30Sjl { 31025cf1a30Sjl _NOTE(ARGUNUSED(hp)) 31125cf1a30Sjl 31225cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)cp; 31325cf1a30Sjl struct memlist *ml, *mc; 31425cf1a30Sjl sbd_error_t *err; 31525cf1a30Sjl static fn_t f = "dr_attach_mem"; 31625cf1a30Sjl 31725cf1a30Sjl PR_MEM("%s...\n", f); 31825cf1a30Sjl 31925cf1a30Sjl dr_lock_status(hp->h_bd); 32025cf1a30Sjl err = drmach_configure(cp->sbdev_id, 0); 32125cf1a30Sjl dr_unlock_status(hp->h_bd); 32225cf1a30Sjl if (err) { 32325cf1a30Sjl DRERR_SET_C(&cp->sbdev_error, &err); 32425cf1a30Sjl return; 32525cf1a30Sjl } 32625cf1a30Sjl 32725cf1a30Sjl ml = dr_get_memlist(mp); 32825cf1a30Sjl for (mc = ml; mc; mc = mc->next) { 32925cf1a30Sjl int rv; 33025cf1a30Sjl sbd_error_t *err; 33125cf1a30Sjl 33225cf1a30Sjl rv = kphysm_add_memory_dynamic( 333*3103d4ceSjesusm (pfn_t)(mc->address >> PAGESHIFT), 334*3103d4ceSjesusm (pgcnt_t)(mc->size >> PAGESHIFT)); 33525cf1a30Sjl if (rv != KPHYSM_OK) { 33625cf1a30Sjl /* 33725cf1a30Sjl * translate kphysm error and 33825cf1a30Sjl * store in devlist error 33925cf1a30Sjl */ 34025cf1a30Sjl switch (rv) { 34125cf1a30Sjl case KPHYSM_ERESOURCE: 34225cf1a30Sjl rv = ESBD_NOMEM; 34325cf1a30Sjl break; 34425cf1a30Sjl 34525cf1a30Sjl case KPHYSM_EFAULT: 34625cf1a30Sjl rv = ESBD_FAULT; 34725cf1a30Sjl break; 34825cf1a30Sjl 34925cf1a30Sjl default: 35025cf1a30Sjl rv = ESBD_INTERNAL; 35125cf1a30Sjl break; 35225cf1a30Sjl } 35325cf1a30Sjl 35425cf1a30Sjl if (rv == ESBD_INTERNAL) { 35525cf1a30Sjl DR_DEV_INTERNAL_ERROR(&mp->sbm_cm); 35625cf1a30Sjl } else 35725cf1a30Sjl dr_dev_err(CE_WARN, &mp->sbm_cm, rv); 35825cf1a30Sjl break; 35925cf1a30Sjl } 36025cf1a30Sjl 36125cf1a30Sjl err = drmach_mem_add_span( 362*3103d4ceSjesusm mp->sbm_cm.sbdev_id, mc->address, mc->size); 36325cf1a30Sjl if (err) { 36425cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 36525cf1a30Sjl break; 36625cf1a30Sjl } 36725cf1a30Sjl } 36825cf1a30Sjl 36925cf1a30Sjl memlist_delete(ml); 37025cf1a30Sjl 37125cf1a30Sjl /* back out if configure failed */ 37225cf1a30Sjl if (mp->sbm_cm.sbdev_error != NULL) { 37325cf1a30Sjl dr_lock_status(hp->h_bd); 37425cf1a30Sjl err = drmach_unconfigure(cp->sbdev_id, 0); 37525cf1a30Sjl if (err) 37625cf1a30Sjl sbd_err_clear(&err); 37725cf1a30Sjl dr_unlock_status(hp->h_bd); 37825cf1a30Sjl } 37925cf1a30Sjl } 38025cf1a30Sjl 38125cf1a30Sjl static struct memlist * 38225cf1a30Sjl dr_memlist_del_retired_pages(struct memlist *mlist) 38325cf1a30Sjl { 38425cf1a30Sjl page_t *pp; 38525cf1a30Sjl pfn_t pfn; 38625cf1a30Sjl kmutex_t *vphm; 38768ac2337Sjl vnode_t *vp = retired_pages; 38825cf1a30Sjl static fn_t f = "dr_memlist_del_retired_pages"; 38925cf1a30Sjl 39025cf1a30Sjl vphm = page_vnode_mutex(vp); 39125cf1a30Sjl mutex_enter(vphm); 39225cf1a30Sjl 39325cf1a30Sjl PR_MEM("%s\n", f); 39425cf1a30Sjl 39525cf1a30Sjl if ((pp = vp->v_pages) == NULL) { 39625cf1a30Sjl mutex_exit(vphm); 39725cf1a30Sjl return (mlist); 39825cf1a30Sjl } 39925cf1a30Sjl 40025cf1a30Sjl do { 40125cf1a30Sjl ASSERT(pp != NULL); 40268ac2337Sjl ASSERT(pp->p_vnode == retired_pages); 40325cf1a30Sjl 4040c48eb93Sbm if (!page_try_reclaim_lock(pp, SE_SHARED, SE_RETIRED)) 40525cf1a30Sjl continue; 40625cf1a30Sjl 40725cf1a30Sjl pfn = page_pptonum(pp); 40825cf1a30Sjl 40925cf1a30Sjl ASSERT((pp->p_offset >> PAGESHIFT) == pfn); 41025cf1a30Sjl /* 41125cf1a30Sjl * Page retirement currently breaks large pages into PAGESIZE 41225cf1a30Sjl * pages. If this changes, need to remove the assert and deal 41325cf1a30Sjl * with different page sizes. 41425cf1a30Sjl */ 41525cf1a30Sjl ASSERT(pp->p_szc == 0); 41625cf1a30Sjl 41725cf1a30Sjl if (address_in_memlist(mlist, ptob(pfn), PAGESIZE)) { 41825cf1a30Sjl mlist = memlist_del_span(mlist, ptob(pfn), PAGESIZE); 41925cf1a30Sjl PR_MEM("deleted retired page 0x%lx (pfn 0x%lx) " 42025cf1a30Sjl "from memlist\n", ptob(pfn), pfn); 42125cf1a30Sjl } 42225cf1a30Sjl 42325cf1a30Sjl page_unlock(pp); 42425cf1a30Sjl } while ((pp = pp->p_vpnext) != vp->v_pages); 42525cf1a30Sjl 42625cf1a30Sjl mutex_exit(vphm); 42725cf1a30Sjl 42825cf1a30Sjl return (mlist); 42925cf1a30Sjl } 43025cf1a30Sjl 43125cf1a30Sjl static int 43225cf1a30Sjl dr_move_memory(dr_handle_t *hp, dr_mem_unit_t *s_mp, dr_mem_unit_t *t_mp) 43325cf1a30Sjl { 43425cf1a30Sjl int rv = -1; 43525cf1a30Sjl time_t copytime; 43625cf1a30Sjl drmachid_t cr_id; 43725cf1a30Sjl dr_sr_handle_t *srhp = NULL; 43825cf1a30Sjl dr_board_t *t_bp, *s_bp; 43925cf1a30Sjl struct memlist *c_ml, *d_ml; 44025cf1a30Sjl sbd_error_t *err; 44125cf1a30Sjl static fn_t f = "dr_move_memory"; 44225cf1a30Sjl 44325cf1a30Sjl PR_MEM("%s: (INLINE) moving memory from %s to %s\n", 444*3103d4ceSjesusm f, 445*3103d4ceSjesusm s_mp->sbm_cm.sbdev_path, 446*3103d4ceSjesusm t_mp->sbm_cm.sbdev_path); 44725cf1a30Sjl 44825cf1a30Sjl ASSERT(s_mp->sbm_flags & DR_MFLAG_SOURCE); 44925cf1a30Sjl ASSERT(s_mp->sbm_peer == t_mp); 45025cf1a30Sjl ASSERT(s_mp->sbm_mlist); 45125cf1a30Sjl 45225cf1a30Sjl ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET); 45325cf1a30Sjl ASSERT(t_mp->sbm_peer == s_mp); 45425cf1a30Sjl 45525cf1a30Sjl /* 45625cf1a30Sjl * create a memlist of spans to copy by removing 45725cf1a30Sjl * the spans that have been deleted, if any, from 45825cf1a30Sjl * the full source board memlist. s_mp->sbm_del_mlist 45925cf1a30Sjl * will be NULL if there were no spans deleted from 46025cf1a30Sjl * the source board. 46125cf1a30Sjl */ 46225cf1a30Sjl c_ml = memlist_dup(s_mp->sbm_mlist); 46325cf1a30Sjl d_ml = s_mp->sbm_del_mlist; 46425cf1a30Sjl while (d_ml != NULL) { 46525cf1a30Sjl c_ml = memlist_del_span(c_ml, d_ml->address, d_ml->size); 46625cf1a30Sjl d_ml = d_ml->next; 46725cf1a30Sjl } 46825cf1a30Sjl 46925cf1a30Sjl /* 47025cf1a30Sjl * Remove retired pages from the copy list. The page content 47125cf1a30Sjl * need not be copied since the pages are no longer in use. 47225cf1a30Sjl */ 47325cf1a30Sjl PR_MEM("%s: copy list before removing retired pages (if any):\n", f); 47425cf1a30Sjl PR_MEMLIST_DUMP(c_ml); 47525cf1a30Sjl 47625cf1a30Sjl c_ml = dr_memlist_del_retired_pages(c_ml); 47725cf1a30Sjl 47825cf1a30Sjl PR_MEM("%s: copy list after removing retired pages:\n", f); 47925cf1a30Sjl PR_MEMLIST_DUMP(c_ml); 48025cf1a30Sjl 48125cf1a30Sjl /* 48225cf1a30Sjl * With parallel copy, it shouldn't make a difference which 48325cf1a30Sjl * CPU is the actual master during copy-rename since all 48425cf1a30Sjl * CPUs participate in the parallel copy anyway. 48525cf1a30Sjl */ 48625cf1a30Sjl affinity_set(CPU_CURRENT); 48725cf1a30Sjl 48825cf1a30Sjl err = drmach_copy_rename_init( 489*3103d4ceSjesusm t_mp->sbm_cm.sbdev_id, s_mp->sbm_cm.sbdev_id, c_ml, &cr_id); 49025cf1a30Sjl if (err) { 49125cf1a30Sjl DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err); 49225cf1a30Sjl affinity_clear(); 49325cf1a30Sjl memlist_delete(c_ml); 49425cf1a30Sjl return (-1); 49525cf1a30Sjl } 49625cf1a30Sjl 49725cf1a30Sjl srhp = dr_get_sr_handle(hp); 49825cf1a30Sjl ASSERT(srhp); 49925cf1a30Sjl 50025cf1a30Sjl copytime = lbolt; 50125cf1a30Sjl 50225cf1a30Sjl /* Quiesce the OS. */ 50325cf1a30Sjl if (dr_suspend(srhp)) { 50425cf1a30Sjl cmn_err(CE_WARN, "%s: failed to quiesce OS" 505*3103d4ceSjesusm " for copy-rename", f); 50625cf1a30Sjl 50725cf1a30Sjl err = drmach_copy_rename_fini(cr_id); 50825cf1a30Sjl if (err) { 50925cf1a30Sjl /* 51025cf1a30Sjl * no error is expected since the program has 51125cf1a30Sjl * not yet run. 51225cf1a30Sjl */ 51325cf1a30Sjl 51425cf1a30Sjl /* catch this in debug kernels */ 51525cf1a30Sjl ASSERT(0); 51625cf1a30Sjl 51725cf1a30Sjl sbd_err_clear(&err); 51825cf1a30Sjl } 51925cf1a30Sjl 52025cf1a30Sjl /* suspend error reached via hp */ 52125cf1a30Sjl s_mp->sbm_cm.sbdev_error = hp->h_err; 52225cf1a30Sjl hp->h_err = NULL; 52325cf1a30Sjl goto done; 52425cf1a30Sjl } 52525cf1a30Sjl 52625cf1a30Sjl drmach_copy_rename(cr_id); 52725cf1a30Sjl 52825cf1a30Sjl /* Resume the OS. */ 52925cf1a30Sjl dr_resume(srhp); 53025cf1a30Sjl 53125cf1a30Sjl copytime = lbolt - copytime; 53225cf1a30Sjl 53325cf1a30Sjl if (err = drmach_copy_rename_fini(cr_id)) 53425cf1a30Sjl goto done; 53525cf1a30Sjl 53625cf1a30Sjl /* 53725cf1a30Sjl * Rename memory for lgroup. 53825cf1a30Sjl * Source and target board numbers are packaged in arg. 53925cf1a30Sjl */ 54025cf1a30Sjl s_bp = s_mp->sbm_cm.sbdev_bp; 54125cf1a30Sjl t_bp = t_mp->sbm_cm.sbdev_bp; 54225cf1a30Sjl 54325cf1a30Sjl lgrp_plat_config(LGRP_CONFIG_MEM_RENAME, 544*3103d4ceSjesusm (uintptr_t)(s_bp->b_num | (t_bp->b_num << 16))); 54525cf1a30Sjl 54625cf1a30Sjl 54725cf1a30Sjl PR_MEM("%s: copy-rename elapsed time = %ld ticks (%ld secs)\n", 548*3103d4ceSjesusm f, copytime, copytime / hz); 54925cf1a30Sjl 55025cf1a30Sjl rv = 0; 55125cf1a30Sjl done: 55225cf1a30Sjl if (srhp) 55325cf1a30Sjl dr_release_sr_handle(srhp); 55425cf1a30Sjl if (err) 55525cf1a30Sjl DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err); 55625cf1a30Sjl affinity_clear(); 55725cf1a30Sjl 55825cf1a30Sjl return (rv); 55925cf1a30Sjl } 56025cf1a30Sjl 56125cf1a30Sjl /* 56225cf1a30Sjl * If detaching node contains memory that is "non-permanent" 56325cf1a30Sjl * then the memory adr's are simply cleared. If the memory 56425cf1a30Sjl * is non-relocatable, then do a copy-rename. 56525cf1a30Sjl */ 56625cf1a30Sjl void 56725cf1a30Sjl dr_detach_mem(dr_handle_t *hp, dr_common_unit_t *cp) 56825cf1a30Sjl { 56925cf1a30Sjl int rv = 0; 57025cf1a30Sjl dr_mem_unit_t *s_mp = (dr_mem_unit_t *)cp; 57125cf1a30Sjl dr_mem_unit_t *t_mp; 57225cf1a30Sjl dr_state_t state; 57325cf1a30Sjl static fn_t f = "dr_detach_mem"; 57425cf1a30Sjl 57525cf1a30Sjl PR_MEM("%s...\n", f); 57625cf1a30Sjl 57725cf1a30Sjl /* lookup target mem unit and target board structure, if any */ 57825cf1a30Sjl if (s_mp->sbm_flags & DR_MFLAG_SOURCE) { 57925cf1a30Sjl t_mp = s_mp->sbm_peer; 58025cf1a30Sjl ASSERT(t_mp != NULL); 58125cf1a30Sjl ASSERT(t_mp->sbm_peer == s_mp); 58225cf1a30Sjl } else { 58325cf1a30Sjl t_mp = NULL; 58425cf1a30Sjl } 58525cf1a30Sjl 58625cf1a30Sjl /* verify mem unit's state is UNREFERENCED */ 58725cf1a30Sjl state = s_mp->sbm_cm.sbdev_state; 58825cf1a30Sjl if (state != DR_STATE_UNREFERENCED) { 58925cf1a30Sjl dr_dev_err(CE_IGNORE, &s_mp->sbm_cm, ESBD_STATE); 59025cf1a30Sjl return; 59125cf1a30Sjl } 59225cf1a30Sjl 59325cf1a30Sjl /* verify target mem unit's state is UNREFERENCED, if any */ 59425cf1a30Sjl if (t_mp != NULL) { 59525cf1a30Sjl state = t_mp->sbm_cm.sbdev_state; 59625cf1a30Sjl if (state != DR_STATE_UNREFERENCED) { 59725cf1a30Sjl dr_dev_err(CE_IGNORE, &t_mp->sbm_cm, ESBD_STATE); 59825cf1a30Sjl return; 59925cf1a30Sjl } 60025cf1a30Sjl } 60125cf1a30Sjl 60225cf1a30Sjl /* 60325cf1a30Sjl * If there is no target board (no copy/rename was needed), then 60425cf1a30Sjl * we're done! 60525cf1a30Sjl */ 60625cf1a30Sjl if (t_mp == NULL) { 60725cf1a30Sjl sbd_error_t *err; 60825cf1a30Sjl /* 60925cf1a30Sjl * Reprogram interconnect hardware and disable 61025cf1a30Sjl * memory controllers for memory node that's going away. 61125cf1a30Sjl */ 61225cf1a30Sjl 61325cf1a30Sjl err = drmach_mem_disable(s_mp->sbm_cm.sbdev_id); 61425cf1a30Sjl if (err) { 61525cf1a30Sjl DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err); 61625cf1a30Sjl rv = -1; 61725cf1a30Sjl } 61825cf1a30Sjl } else { 61925cf1a30Sjl rv = dr_move_memory(hp, s_mp, t_mp); 62025cf1a30Sjl PR_MEM("%s: %s memory COPY-RENAME (board %d -> %d)\n", 621*3103d4ceSjesusm f, 622*3103d4ceSjesusm rv ? "FAILED" : "COMPLETED", 623*3103d4ceSjesusm s_mp->sbm_cm.sbdev_bp->b_num, 624*3103d4ceSjesusm t_mp->sbm_cm.sbdev_bp->b_num); 62525cf1a30Sjl 62625cf1a30Sjl if (rv != 0) 62725cf1a30Sjl (void) dr_cancel_mem(s_mp); 62825cf1a30Sjl } 62925cf1a30Sjl 63025cf1a30Sjl if (rv == 0) { 63125cf1a30Sjl sbd_error_t *err; 63225cf1a30Sjl 63325cf1a30Sjl dr_lock_status(hp->h_bd); 63425cf1a30Sjl err = drmach_unconfigure(s_mp->sbm_cm.sbdev_id, 0); 63525cf1a30Sjl dr_unlock_status(hp->h_bd); 63625cf1a30Sjl if (err) 63725cf1a30Sjl sbd_err_clear(&err); 63825cf1a30Sjl } 63925cf1a30Sjl } 64025cf1a30Sjl 64125cf1a30Sjl /* 64225cf1a30Sjl * This routine acts as a wrapper for kphysm_del_span_query in order to 64325cf1a30Sjl * support potential memory holes in a board's physical address space. 64425cf1a30Sjl * It calls kphysm_del_span_query for each node in a memlist and accumulates 64525cf1a30Sjl * the results in *mp. 64625cf1a30Sjl */ 64725cf1a30Sjl static int 64825cf1a30Sjl dr_del_mlist_query(struct memlist *mlist, memquery_t *mp) 64925cf1a30Sjl { 65025cf1a30Sjl struct memlist *ml; 65125cf1a30Sjl int rv = 0; 65225cf1a30Sjl 65325cf1a30Sjl 65425cf1a30Sjl if (mlist == NULL) 65525cf1a30Sjl cmn_err(CE_WARN, "dr_del_mlist_query: mlist=NULL\n"); 65625cf1a30Sjl 65725cf1a30Sjl mp->phys_pages = 0; 65825cf1a30Sjl mp->managed = 0; 65925cf1a30Sjl mp->nonrelocatable = 0; 66025cf1a30Sjl mp->first_nonrelocatable = (pfn_t)-1; /* XXX */ 66125cf1a30Sjl mp->last_nonrelocatable = 0; 66225cf1a30Sjl 66325cf1a30Sjl for (ml = mlist; ml; ml = ml->next) { 66425cf1a30Sjl memquery_t mq; 66525cf1a30Sjl 66625cf1a30Sjl rv = kphysm_del_span_query( 667*3103d4ceSjesusm _b64top(ml->address), _b64top(ml->size), &mq); 66825cf1a30Sjl if (rv) 66925cf1a30Sjl break; 67025cf1a30Sjl 67125cf1a30Sjl mp->phys_pages += mq.phys_pages; 67225cf1a30Sjl mp->managed += mq.managed; 67325cf1a30Sjl mp->nonrelocatable += mq.nonrelocatable; 67425cf1a30Sjl 67525cf1a30Sjl if (mq.nonrelocatable != 0) { 67625cf1a30Sjl if (mq.first_nonrelocatable < mp->first_nonrelocatable) 67725cf1a30Sjl mp->first_nonrelocatable = 678*3103d4ceSjesusm mq.first_nonrelocatable; 67925cf1a30Sjl if (mq.last_nonrelocatable > mp->last_nonrelocatable) 68025cf1a30Sjl mp->last_nonrelocatable = 681*3103d4ceSjesusm mq.last_nonrelocatable; 68225cf1a30Sjl } 68325cf1a30Sjl } 68425cf1a30Sjl 68525cf1a30Sjl if (mp->nonrelocatable == 0) 68625cf1a30Sjl mp->first_nonrelocatable = 0; /* XXX */ 68725cf1a30Sjl 68825cf1a30Sjl return (rv); 68925cf1a30Sjl } 69025cf1a30Sjl 69125cf1a30Sjl /* 69225cf1a30Sjl * NOTE: This routine is only partially smart about multiple 69325cf1a30Sjl * mem-units. Need to make mem-status structure smart 69425cf1a30Sjl * about them also. 69525cf1a30Sjl */ 69625cf1a30Sjl int 69725cf1a30Sjl dr_mem_status(dr_handle_t *hp, dr_devset_t devset, sbd_dev_stat_t *dsp) 69825cf1a30Sjl { 69925cf1a30Sjl int m, mix; 70025cf1a30Sjl memdelstat_t mdst; 70125cf1a30Sjl memquery_t mq; 70225cf1a30Sjl dr_board_t *bp; 70325cf1a30Sjl dr_mem_unit_t *mp; 70425cf1a30Sjl sbd_mem_stat_t *msp; 70525cf1a30Sjl static fn_t f = "dr_mem_status"; 70625cf1a30Sjl 70725cf1a30Sjl bp = hp->h_bd; 70825cf1a30Sjl devset &= DR_DEVS_PRESENT(bp); 70925cf1a30Sjl 71025cf1a30Sjl for (m = mix = 0; m < MAX_MEM_UNITS_PER_BOARD; m++) { 71125cf1a30Sjl int rv; 71225cf1a30Sjl sbd_error_t *err; 71325cf1a30Sjl drmach_status_t pstat; 71425cf1a30Sjl dr_mem_unit_t *p_mp; 71525cf1a30Sjl 71625cf1a30Sjl if (DEVSET_IN_SET(devset, SBD_COMP_MEM, m) == 0) 71725cf1a30Sjl continue; 71825cf1a30Sjl 71925cf1a30Sjl mp = dr_get_mem_unit(bp, m); 72025cf1a30Sjl 72125cf1a30Sjl if (mp->sbm_cm.sbdev_state == DR_STATE_EMPTY) { 72225cf1a30Sjl /* present, but not fully initialized */ 72325cf1a30Sjl continue; 72425cf1a30Sjl } 72525cf1a30Sjl 72625cf1a30Sjl if (mp->sbm_cm.sbdev_id == (drmachid_t)0) 72725cf1a30Sjl continue; 72825cf1a30Sjl 72925cf1a30Sjl /* fetch platform status */ 73025cf1a30Sjl err = drmach_status(mp->sbm_cm.sbdev_id, &pstat); 73125cf1a30Sjl if (err) { 73225cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 73325cf1a30Sjl continue; 73425cf1a30Sjl } 73525cf1a30Sjl 73625cf1a30Sjl msp = &dsp->d_mem; 73725cf1a30Sjl bzero((caddr_t)msp, sizeof (*msp)); 73825cf1a30Sjl 73925cf1a30Sjl strncpy(msp->ms_cm.c_id.c_name, pstat.type, 740*3103d4ceSjesusm sizeof (msp->ms_cm.c_id.c_name)); 74125cf1a30Sjl msp->ms_cm.c_id.c_type = mp->sbm_cm.sbdev_type; 74225cf1a30Sjl msp->ms_cm.c_id.c_unit = SBD_NULL_UNIT; 74325cf1a30Sjl msp->ms_cm.c_cond = mp->sbm_cm.sbdev_cond; 74425cf1a30Sjl msp->ms_cm.c_busy = mp->sbm_cm.sbdev_busy | pstat.busy; 74525cf1a30Sjl msp->ms_cm.c_time = mp->sbm_cm.sbdev_time; 74625cf1a30Sjl msp->ms_cm.c_ostate = mp->sbm_cm.sbdev_ostate; 74725cf1a30Sjl 74825cf1a30Sjl msp->ms_totpages = mp->sbm_npages; 74925cf1a30Sjl msp->ms_basepfn = mp->sbm_basepfn; 75025cf1a30Sjl msp->ms_pageslost = mp->sbm_pageslost; 75125cf1a30Sjl msp->ms_cage_enabled = kcage_on; 75225cf1a30Sjl 75325cf1a30Sjl if (mp->sbm_flags & DR_MFLAG_RESERVED) 75425cf1a30Sjl p_mp = mp->sbm_peer; 75525cf1a30Sjl else 75625cf1a30Sjl p_mp = NULL; 75725cf1a30Sjl 75825cf1a30Sjl if (p_mp == NULL) { 75925cf1a30Sjl msp->ms_peer_is_target = 0; 76025cf1a30Sjl msp->ms_peer_ap_id[0] = '\0'; 76125cf1a30Sjl } else if (p_mp->sbm_flags & DR_MFLAG_RESERVED) { 76225cf1a30Sjl char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 76325cf1a30Sjl char *minor; 76425cf1a30Sjl 76525cf1a30Sjl /* 76625cf1a30Sjl * b_dip doesn't have to be held for ddi_pathname() 76725cf1a30Sjl * because the board struct (dr_board_t) will be 76825cf1a30Sjl * destroyed before b_dip detaches. 76925cf1a30Sjl */ 77025cf1a30Sjl (void) ddi_pathname(bp->b_dip, path); 77125cf1a30Sjl minor = strchr(p_mp->sbm_cm.sbdev_path, ':'); 77225cf1a30Sjl 77325cf1a30Sjl snprintf(msp->ms_peer_ap_id, 77425cf1a30Sjl sizeof (msp->ms_peer_ap_id), "%s%s", 77525cf1a30Sjl path, (minor == NULL) ? "" : minor); 77625cf1a30Sjl 77725cf1a30Sjl kmem_free(path, MAXPATHLEN); 77825cf1a30Sjl 77925cf1a30Sjl if (p_mp->sbm_flags & DR_MFLAG_TARGET) 78025cf1a30Sjl msp->ms_peer_is_target = 1; 78125cf1a30Sjl } 78225cf1a30Sjl 78325cf1a30Sjl if (mp->sbm_flags & DR_MFLAG_RELOWNER) 78425cf1a30Sjl rv = kphysm_del_status(mp->sbm_memhandle, &mdst); 78525cf1a30Sjl else 78625cf1a30Sjl rv = KPHYSM_EHANDLE; /* force 'if' to fail */ 78725cf1a30Sjl 78825cf1a30Sjl if (rv == KPHYSM_OK) { 78925cf1a30Sjl /* 79025cf1a30Sjl * Any pages above managed is "free", 79125cf1a30Sjl * i.e. it's collected. 79225cf1a30Sjl */ 79325cf1a30Sjl msp->ms_detpages += (uint_t)(mdst.collected + 79425cf1a30Sjl mdst.phys_pages - mdst.managed); 79525cf1a30Sjl } else { 79625cf1a30Sjl /* 79725cf1a30Sjl * If we're UNREFERENCED or UNCONFIGURED, 79825cf1a30Sjl * then the number of detached pages is 79925cf1a30Sjl * however many pages are on the board. 80025cf1a30Sjl * I.e. detached = not in use by OS. 80125cf1a30Sjl */ 80225cf1a30Sjl switch (msp->ms_cm.c_ostate) { 80325cf1a30Sjl /* 80425cf1a30Sjl * changed to use cfgadm states 80525cf1a30Sjl * 80625cf1a30Sjl * was: 80725cf1a30Sjl * case DR_STATE_UNREFERENCED: 80825cf1a30Sjl * case DR_STATE_UNCONFIGURED: 80925cf1a30Sjl */ 81025cf1a30Sjl case SBD_STAT_UNCONFIGURED: 81125cf1a30Sjl msp->ms_detpages = msp->ms_totpages; 81225cf1a30Sjl break; 81325cf1a30Sjl 81425cf1a30Sjl default: 81525cf1a30Sjl break; 81625cf1a30Sjl } 81725cf1a30Sjl } 81825cf1a30Sjl 81925cf1a30Sjl /* 82025cf1a30Sjl * kphysm_del_span_query can report non-reloc pages = total 82125cf1a30Sjl * pages for memory that is not yet configured 82225cf1a30Sjl */ 82325cf1a30Sjl if (mp->sbm_cm.sbdev_state != DR_STATE_UNCONFIGURED) { 82425cf1a30Sjl struct memlist *ml; 82525cf1a30Sjl 82625cf1a30Sjl ml = dr_get_memlist(mp); 82725cf1a30Sjl rv = ml ? dr_del_mlist_query(ml, &mq) : -1; 82825cf1a30Sjl memlist_delete(ml); 82925cf1a30Sjl 83025cf1a30Sjl if (rv == KPHYSM_OK) { 83125cf1a30Sjl msp->ms_managed_pages = mq.managed; 83225cf1a30Sjl msp->ms_noreloc_pages = mq.nonrelocatable; 83325cf1a30Sjl msp->ms_noreloc_first = 83425cf1a30Sjl mq.first_nonrelocatable; 83525cf1a30Sjl msp->ms_noreloc_last = 83625cf1a30Sjl mq.last_nonrelocatable; 83725cf1a30Sjl msp->ms_cm.c_sflags = 0; 83825cf1a30Sjl if (mq.nonrelocatable) { 83925cf1a30Sjl SBD_SET_SUSPEND(SBD_CMD_UNCONFIGURE, 84025cf1a30Sjl msp->ms_cm.c_sflags); 84125cf1a30Sjl } 84225cf1a30Sjl } else { 84325cf1a30Sjl PR_MEM("%s: kphysm_del_span_query() = %d\n", 84425cf1a30Sjl f, rv); 84525cf1a30Sjl } 84625cf1a30Sjl } 84725cf1a30Sjl 84825cf1a30Sjl /* 84925cf1a30Sjl * Check source unit state during copy-rename 85025cf1a30Sjl */ 85125cf1a30Sjl if ((mp->sbm_flags & DR_MFLAG_SOURCE) && 85225cf1a30Sjl (mp->sbm_cm.sbdev_state == DR_STATE_UNREFERENCED || 85325cf1a30Sjl mp->sbm_cm.sbdev_state == DR_STATE_RELEASE)) 85425cf1a30Sjl msp->ms_cm.c_ostate = SBD_STAT_CONFIGURED; 85525cf1a30Sjl 85625cf1a30Sjl mix++; 85725cf1a30Sjl dsp++; 85825cf1a30Sjl } 85925cf1a30Sjl 86025cf1a30Sjl return (mix); 86125cf1a30Sjl } 86225cf1a30Sjl 86325cf1a30Sjl int 86425cf1a30Sjl dr_pre_attach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 86525cf1a30Sjl { 86625cf1a30Sjl _NOTE(ARGUNUSED(hp)) 86725cf1a30Sjl 86825cf1a30Sjl int err_flag = 0; 86925cf1a30Sjl int d; 87025cf1a30Sjl sbd_error_t *err; 87125cf1a30Sjl static fn_t f = "dr_pre_attach_mem"; 87225cf1a30Sjl 87325cf1a30Sjl PR_MEM("%s...\n", f); 87425cf1a30Sjl 87525cf1a30Sjl for (d = 0; d < devnum; d++) { 87625cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d]; 87725cf1a30Sjl dr_state_t state; 87825cf1a30Sjl 87925cf1a30Sjl cmn_err(CE_CONT, "OS configure %s", mp->sbm_cm.sbdev_path); 88025cf1a30Sjl 88125cf1a30Sjl state = mp->sbm_cm.sbdev_state; 88225cf1a30Sjl switch (state) { 88325cf1a30Sjl case DR_STATE_UNCONFIGURED: 88425cf1a30Sjl PR_MEM("%s: recovering from UNCONFIG for %s\n", 885*3103d4ceSjesusm f, 886*3103d4ceSjesusm mp->sbm_cm.sbdev_path); 88725cf1a30Sjl 88825cf1a30Sjl /* use memlist cached by dr_post_detach_mem_unit */ 88925cf1a30Sjl ASSERT(mp->sbm_mlist != NULL); 89025cf1a30Sjl PR_MEM("%s: re-configuring cached memlist for %s:\n", 891*3103d4ceSjesusm f, mp->sbm_cm.sbdev_path); 89225cf1a30Sjl PR_MEMLIST_DUMP(mp->sbm_mlist); 89325cf1a30Sjl 89425cf1a30Sjl /* kphysm del handle should be have been freed */ 89525cf1a30Sjl ASSERT((mp->sbm_flags & DR_MFLAG_RELOWNER) == 0); 89625cf1a30Sjl 89725cf1a30Sjl /*FALLTHROUGH*/ 89825cf1a30Sjl 89925cf1a30Sjl case DR_STATE_CONNECTED: 90025cf1a30Sjl PR_MEM("%s: reprogramming mem hardware on %s\n", 901*3103d4ceSjesusm f, mp->sbm_cm.sbdev_bp->b_path); 90225cf1a30Sjl 90325cf1a30Sjl PR_MEM("%s: enabling %s\n", 904*3103d4ceSjesusm f, mp->sbm_cm.sbdev_path); 90525cf1a30Sjl 90625cf1a30Sjl err = drmach_mem_enable(mp->sbm_cm.sbdev_id); 90725cf1a30Sjl if (err) { 90825cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 90925cf1a30Sjl err_flag = 1; 91025cf1a30Sjl } 91125cf1a30Sjl break; 91225cf1a30Sjl 91325cf1a30Sjl default: 91425cf1a30Sjl dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_STATE); 91525cf1a30Sjl err_flag = 1; 91625cf1a30Sjl break; 91725cf1a30Sjl } 91825cf1a30Sjl 91925cf1a30Sjl /* exit for loop if error encountered */ 92025cf1a30Sjl if (err_flag) 92125cf1a30Sjl break; 92225cf1a30Sjl } 92325cf1a30Sjl 92425cf1a30Sjl return (err_flag ? -1 : 0); 92525cf1a30Sjl } 92625cf1a30Sjl 92768ac2337Sjl static void 92868ac2337Sjl dr_update_mc_memory() 92968ac2337Sjl { 93068ac2337Sjl void (*mc_update_mlist)(void); 93168ac2337Sjl 93268ac2337Sjl /* 93368ac2337Sjl * mc-opl is configured during drmach_mem_new but the memory 93468ac2337Sjl * has not been added to phys_install at that time. 93568ac2337Sjl * we must inform mc-opl to update the mlist after we 93668ac2337Sjl * attach or detach a system board. 93768ac2337Sjl */ 93868ac2337Sjl 93968ac2337Sjl mc_update_mlist = (void (*)(void)) 94068ac2337Sjl modgetsymvalue("opl_mc_update_mlist", 0); 94168ac2337Sjl 94268ac2337Sjl if (mc_update_mlist != NULL) { 94368ac2337Sjl (*mc_update_mlist)(); 94468ac2337Sjl } 94568ac2337Sjl } 94668ac2337Sjl 94725cf1a30Sjl int 94825cf1a30Sjl dr_post_attach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 94925cf1a30Sjl { 95025cf1a30Sjl _NOTE(ARGUNUSED(hp)) 95125cf1a30Sjl 95225cf1a30Sjl int d; 95325cf1a30Sjl static fn_t f = "dr_post_attach_mem"; 95425cf1a30Sjl 95525cf1a30Sjl PR_MEM("%s...\n", f); 95625cf1a30Sjl 95725cf1a30Sjl for (d = 0; d < devnum; d++) { 95825cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d]; 95925cf1a30Sjl struct memlist *mlist, *ml; 96025cf1a30Sjl 96125cf1a30Sjl mlist = dr_get_memlist(mp); 96225cf1a30Sjl if (mlist == NULL) { 96368ac2337Sjl /* OPL supports memoryless board */ 96425cf1a30Sjl continue; 96525cf1a30Sjl } 96625cf1a30Sjl 96725cf1a30Sjl /* 96825cf1a30Sjl * Verify the memory really did successfully attach 96925cf1a30Sjl * by checking for its existence in phys_install. 97025cf1a30Sjl */ 97125cf1a30Sjl memlist_read_lock(); 97225cf1a30Sjl if (memlist_intersect(phys_install, mlist) == 0) { 97325cf1a30Sjl memlist_read_unlock(); 97425cf1a30Sjl 97525cf1a30Sjl DR_DEV_INTERNAL_ERROR(&mp->sbm_cm); 97625cf1a30Sjl 97725cf1a30Sjl PR_MEM("%s: %s memlist not in phys_install", 978*3103d4ceSjesusm f, mp->sbm_cm.sbdev_path); 97925cf1a30Sjl 98025cf1a30Sjl memlist_delete(mlist); 98125cf1a30Sjl continue; 98225cf1a30Sjl } 98325cf1a30Sjl memlist_read_unlock(); 98425cf1a30Sjl 98525cf1a30Sjl for (ml = mlist; ml != NULL; ml = ml->next) { 98625cf1a30Sjl sbd_error_t *err; 98725cf1a30Sjl 98825cf1a30Sjl err = drmach_mem_add_span( 989*3103d4ceSjesusm mp->sbm_cm.sbdev_id, 990*3103d4ceSjesusm ml->address, 991*3103d4ceSjesusm ml->size); 99225cf1a30Sjl if (err) 99325cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 99425cf1a30Sjl } 99525cf1a30Sjl 99625cf1a30Sjl memlist_delete(mlist); 99725cf1a30Sjl 99825cf1a30Sjl /* 99925cf1a30Sjl * Destroy cached memlist, if any. 100025cf1a30Sjl * There will be a cached memlist in sbm_mlist if 100125cf1a30Sjl * this board is being configured directly after 100225cf1a30Sjl * an unconfigure. 100325cf1a30Sjl * To support this transition, dr_post_detach_mem 100425cf1a30Sjl * left a copy of the last known memlist in sbm_mlist. 100525cf1a30Sjl * This memlist could differ from any derived from 100625cf1a30Sjl * hardware if while this memunit was last configured 100725cf1a30Sjl * the system detected and deleted bad pages from 100825cf1a30Sjl * phys_install. The location of those bad pages 100925cf1a30Sjl * will be reflected in the cached memlist. 101025cf1a30Sjl */ 101125cf1a30Sjl if (mp->sbm_mlist) { 101225cf1a30Sjl memlist_delete(mp->sbm_mlist); 101325cf1a30Sjl mp->sbm_mlist = NULL; 101425cf1a30Sjl } 101525cf1a30Sjl } 101625cf1a30Sjl 101768ac2337Sjl dr_update_mc_memory(); 101868ac2337Sjl 101925cf1a30Sjl return (0); 102025cf1a30Sjl } 102125cf1a30Sjl 102225cf1a30Sjl int 102325cf1a30Sjl dr_pre_detach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 102425cf1a30Sjl { 102525cf1a30Sjl _NOTE(ARGUNUSED(hp)) 102625cf1a30Sjl 102725cf1a30Sjl int d; 102825cf1a30Sjl 102925cf1a30Sjl for (d = 0; d < devnum; d++) { 103025cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d]; 103125cf1a30Sjl 103225cf1a30Sjl cmn_err(CE_CONT, "OS unconfigure %s", mp->sbm_cm.sbdev_path); 103325cf1a30Sjl } 103425cf1a30Sjl 103525cf1a30Sjl return (0); 103625cf1a30Sjl } 103725cf1a30Sjl 103825cf1a30Sjl int 103925cf1a30Sjl dr_post_detach_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 104025cf1a30Sjl { 104125cf1a30Sjl _NOTE(ARGUNUSED(hp)) 104225cf1a30Sjl 104325cf1a30Sjl int d, rv; 104425cf1a30Sjl static fn_t f = "dr_post_detach_mem"; 104525cf1a30Sjl 104625cf1a30Sjl PR_MEM("%s...\n", f); 104725cf1a30Sjl 104825cf1a30Sjl rv = 0; 104925cf1a30Sjl for (d = 0; d < devnum; d++) { 105025cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d]; 105125cf1a30Sjl 105225cf1a30Sjl ASSERT(mp->sbm_cm.sbdev_bp == hp->h_bd); 105325cf1a30Sjl 105425cf1a30Sjl if (dr_post_detach_mem_unit(mp)) 105525cf1a30Sjl rv = -1; 105625cf1a30Sjl } 105768ac2337Sjl dr_update_mc_memory(); 105825cf1a30Sjl 105925cf1a30Sjl return (rv); 106025cf1a30Sjl } 106125cf1a30Sjl 106225cf1a30Sjl static void 106325cf1a30Sjl dr_add_memory_spans(dr_mem_unit_t *mp, struct memlist *ml) 106425cf1a30Sjl { 106525cf1a30Sjl static fn_t f = "dr_add_memory_spans"; 106625cf1a30Sjl 106725cf1a30Sjl PR_MEM("%s...", f); 106825cf1a30Sjl PR_MEMLIST_DUMP(ml); 106925cf1a30Sjl 107025cf1a30Sjl #ifdef DEBUG 107125cf1a30Sjl memlist_read_lock(); 107225cf1a30Sjl if (memlist_intersect(phys_install, ml)) { 107325cf1a30Sjl PR_MEM("%s:WARNING: memlist intersects with phys_install\n", f); 107425cf1a30Sjl } 107525cf1a30Sjl memlist_read_unlock(); 107625cf1a30Sjl #endif 107725cf1a30Sjl 107825cf1a30Sjl for (; ml; ml = ml->next) { 107925cf1a30Sjl pfn_t base; 108025cf1a30Sjl pgcnt_t npgs; 108125cf1a30Sjl int rv; 108225cf1a30Sjl sbd_error_t *err; 108325cf1a30Sjl 108425cf1a30Sjl base = _b64top(ml->address); 108525cf1a30Sjl npgs = _b64top(ml->size); 108625cf1a30Sjl 108725cf1a30Sjl rv = kphysm_add_memory_dynamic(base, npgs); 108825cf1a30Sjl 108925cf1a30Sjl err = drmach_mem_add_span( 1090*3103d4ceSjesusm mp->sbm_cm.sbdev_id, 1091*3103d4ceSjesusm ml->address, 1092*3103d4ceSjesusm ml->size); 109325cf1a30Sjl 109425cf1a30Sjl if (err) 109525cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 109625cf1a30Sjl 109725cf1a30Sjl if (rv != KPHYSM_OK) { 109825cf1a30Sjl cmn_err(CE_WARN, "%s:" 1099*3103d4ceSjesusm " unexpected kphysm_add_memory_dynamic" 1100*3103d4ceSjesusm " return value %d;" 1101*3103d4ceSjesusm " basepfn=0x%lx, npages=%ld\n", 1102*3103d4ceSjesusm f, rv, base, npgs); 110325cf1a30Sjl 110425cf1a30Sjl continue; 110525cf1a30Sjl } 110625cf1a30Sjl } 110725cf1a30Sjl } 110825cf1a30Sjl 110968ac2337Sjl static int 111068ac2337Sjl memlist_touch(struct memlist *ml, uint64_t add) 111168ac2337Sjl { 111268ac2337Sjl while (ml != NULL) { 111368ac2337Sjl if ((add == ml->address) || 1114*3103d4ceSjesusm (add == (ml->address + ml->size))) 111568ac2337Sjl return (1); 111668ac2337Sjl ml = ml->next; 111768ac2337Sjl } 111868ac2337Sjl return (0); 111968ac2337Sjl } 112068ac2337Sjl 112168ac2337Sjl static sbd_error_t * 112268ac2337Sjl dr_process_excess_mlist(dr_mem_unit_t *s_mp, 112368ac2337Sjl dr_mem_unit_t *t_mp, struct memlist *t_excess_mlist) 112468ac2337Sjl { 112568ac2337Sjl struct memlist *ml; 112668ac2337Sjl sbd_error_t *err; 112768ac2337Sjl static fn_t f = "dr_process_excess_mlist"; 112868ac2337Sjl uint64_t new_pa, nbytes; 112968ac2337Sjl int rv; 113068ac2337Sjl 113168ac2337Sjl err = NULL; 113268ac2337Sjl 113368ac2337Sjl /* 113468ac2337Sjl * After the small <-> big copy-rename, 113568ac2337Sjl * the original address space for the 113668ac2337Sjl * source board may have excess to be 113768ac2337Sjl * deleted. This is a case different 113868ac2337Sjl * from the big->small excess source 113968ac2337Sjl * memory case listed below. 114068ac2337Sjl * Remove s_mp->sbm_del_mlist from 114168ac2337Sjl * the kernel cage glist. 114268ac2337Sjl */ 114368ac2337Sjl for (ml = s_mp->sbm_del_mlist; ml; 1144*3103d4ceSjesusm ml = ml->next) { 114568ac2337Sjl PR_MEM("%s: delete small<->big copy-" 114668ac2337Sjl "rename source excess memory", f); 114768ac2337Sjl PR_MEMLIST_DUMP(ml); 114868ac2337Sjl 114968ac2337Sjl err = drmach_mem_del_span( 1150*3103d4ceSjesusm s_mp->sbm_cm.sbdev_id, 1151*3103d4ceSjesusm ml->address, ml->size); 115268ac2337Sjl if (err) 115368ac2337Sjl DRERR_SET_C(&s_mp-> 115468ac2337Sjl sbm_cm.sbdev_error, &err); 115568ac2337Sjl ASSERT(err == NULL); 115668ac2337Sjl } 115768ac2337Sjl 115868ac2337Sjl PR_MEM("%s: adding back remaining portion" 1159*3103d4ceSjesusm " of %s, memlist:\n", 1160*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 116168ac2337Sjl PR_MEMLIST_DUMP(t_excess_mlist); 116268ac2337Sjl 116368ac2337Sjl for (ml = t_excess_mlist; ml; ml = ml->next) { 1164*3103d4ceSjesusm struct memlist ml0; 116568ac2337Sjl 1166*3103d4ceSjesusm ml0.address = ml->address; 1167*3103d4ceSjesusm ml0.size = ml->size; 1168*3103d4ceSjesusm ml0.next = ml0.prev = NULL; 116968ac2337Sjl 1170*3103d4ceSjesusm /* 1171*3103d4ceSjesusm * If the memory object is 256 MB aligned (max page size 1172*3103d4ceSjesusm * on OPL, it will not be coalesced to the adjacent memory 1173*3103d4ceSjesusm * chunks. The coalesce logic assumes contiguous page 1174*3103d4ceSjesusm * structures for contiguous memory and we hit panic. 1175*3103d4ceSjesusm * For anything less than 256 MB alignment, we have 1176*3103d4ceSjesusm * to make sure that it is not adjacent to anything. 1177*3103d4ceSjesusm * If the new chunk is adjacent to phys_install, we 1178*3103d4ceSjesusm * truncate it to 4MB boundary. 4 MB is somewhat 1179*3103d4ceSjesusm * arbitrary. However we do not want to create 1180*3103d4ceSjesusm * very small segments because they can cause problem. 1181*3103d4ceSjesusm * The extreme case of 8K segment will fail 1182*3103d4ceSjesusm * kphysm_add_memory_dynamic(), e.g. 1183*3103d4ceSjesusm */ 1184*3103d4ceSjesusm if ((ml->address & (MH_MPSS_ALIGNMENT - 1)) || 1185*3103d4ceSjesusm (ml->size & (MH_MPSS_ALIGNMENT - 1))) { 118668ac2337Sjl 118768ac2337Sjl memlist_read_lock(); 118868ac2337Sjl rv = memlist_touch(phys_install, ml0.address); 118968ac2337Sjl memlist_read_unlock(); 119068ac2337Sjl 119168ac2337Sjl if (rv) { 1192*3103d4ceSjesusm new_pa = roundup(ml0.address + 1, MH_MIN_ALIGNMENT); 1193*3103d4ceSjesusm nbytes = (new_pa - ml0.address); 1194*3103d4ceSjesusm if (nbytes >= ml0.size) { 1195*3103d4ceSjesusm t_mp->sbm_dyn_segs = 1196*3103d4ceSjesusm memlist_del_span(t_mp->sbm_dyn_segs, 1197*3103d4ceSjesusm ml0.address, ml0.size); 1198*3103d4ceSjesusm continue; 1199*3103d4ceSjesusm } 120068ac2337Sjl t_mp->sbm_dyn_segs = 120168ac2337Sjl memlist_del_span(t_mp->sbm_dyn_segs, 120268ac2337Sjl ml0.address, nbytes); 1203*3103d4ceSjesusm ml0.size -= nbytes; 1204*3103d4ceSjesusm ml0.address = new_pa; 120568ac2337Sjl } 120668ac2337Sjl 120768ac2337Sjl if (ml0.size == 0) { 1208*3103d4ceSjesusm continue; 120968ac2337Sjl } 121068ac2337Sjl 121168ac2337Sjl memlist_read_lock(); 121268ac2337Sjl rv = memlist_touch(phys_install, ml0.address + ml0.size); 121368ac2337Sjl memlist_read_unlock(); 121468ac2337Sjl 121568ac2337Sjl if (rv) { 1216*3103d4ceSjesusm new_pa = rounddown(ml0.address + ml0.size - 1, 1217*3103d4ceSjesusm MH_MIN_ALIGNMENT); 1218*3103d4ceSjesusm nbytes = (ml0.address + ml0.size - new_pa); 1219*3103d4ceSjesusm if (nbytes >= ml0.size) { 1220*3103d4ceSjesusm t_mp->sbm_dyn_segs = 1221*3103d4ceSjesusm memlist_del_span(t_mp->sbm_dyn_segs, 1222*3103d4ceSjesusm ml0.address, ml0.size); 1223*3103d4ceSjesusm continue; 1224*3103d4ceSjesusm } 122568ac2337Sjl t_mp->sbm_dyn_segs = 122668ac2337Sjl memlist_del_span(t_mp->sbm_dyn_segs, 122768ac2337Sjl new_pa, nbytes); 1228*3103d4ceSjesusm ml0.size -= nbytes; 122968ac2337Sjl } 123068ac2337Sjl 123168ac2337Sjl if (ml0.size > 0) { 1232*3103d4ceSjesusm dr_add_memory_spans(s_mp, &ml0); 1233*3103d4ceSjesusm } 1234*3103d4ceSjesusm } else if (ml0.size > 0) { 1235*3103d4ceSjesusm dr_add_memory_spans(s_mp, &ml0); 123668ac2337Sjl } 123768ac2337Sjl } 123868ac2337Sjl memlist_delete(t_excess_mlist); 123968ac2337Sjl return (err); 124068ac2337Sjl } 124168ac2337Sjl 124225cf1a30Sjl static int 124325cf1a30Sjl dr_post_detach_mem_unit(dr_mem_unit_t *s_mp) 124425cf1a30Sjl { 124525cf1a30Sjl uint64_t sz = s_mp->sbm_slice_size; 124625cf1a30Sjl uint64_t sm = sz - 1; 124725cf1a30Sjl /* old and new below refer to PAs before and after copy-rename */ 124825cf1a30Sjl uint64_t s_old_basepa, s_new_basepa; 124925cf1a30Sjl uint64_t t_old_basepa, t_new_basepa; 125025cf1a30Sjl dr_mem_unit_t *t_mp, *x_mp; 125125cf1a30Sjl drmach_mem_info_t minfo; 125225cf1a30Sjl struct memlist *ml; 125325cf1a30Sjl struct memlist *t_excess_mlist; 125425cf1a30Sjl int rv; 125525cf1a30Sjl int s_excess_mem_deleted = 0; 125625cf1a30Sjl sbd_error_t *err; 125725cf1a30Sjl static fn_t f = "dr_post_detach_mem_unit"; 125825cf1a30Sjl 125925cf1a30Sjl PR_MEM("%s...\n", f); 126025cf1a30Sjl 126125cf1a30Sjl /* s_mp->sbm_del_mlist could be NULL, meaning no deleted spans */ 126225cf1a30Sjl PR_MEM("%s: %s: deleted memlist (EMPTY maybe okay):\n", 1263*3103d4ceSjesusm f, s_mp->sbm_cm.sbdev_path); 126425cf1a30Sjl PR_MEMLIST_DUMP(s_mp->sbm_del_mlist); 126525cf1a30Sjl 126625cf1a30Sjl /* sanity check */ 126725cf1a30Sjl ASSERT(s_mp->sbm_del_mlist == NULL || 1268*3103d4ceSjesusm (s_mp->sbm_flags & DR_MFLAG_RELDONE) != 0); 126925cf1a30Sjl 127025cf1a30Sjl if (s_mp->sbm_flags & DR_MFLAG_SOURCE) { 127125cf1a30Sjl t_mp = s_mp->sbm_peer; 127225cf1a30Sjl ASSERT(t_mp != NULL); 127325cf1a30Sjl ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET); 127425cf1a30Sjl ASSERT(t_mp->sbm_peer == s_mp); 127525cf1a30Sjl 127625cf1a30Sjl ASSERT(t_mp->sbm_flags & DR_MFLAG_RELDONE); 127725cf1a30Sjl ASSERT(t_mp->sbm_del_mlist); 127825cf1a30Sjl 127925cf1a30Sjl PR_MEM("%s: target %s: deleted memlist:\n", 1280*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 128125cf1a30Sjl PR_MEMLIST_DUMP(t_mp->sbm_del_mlist); 128225cf1a30Sjl } else { 128325cf1a30Sjl /* this is no target unit */ 128425cf1a30Sjl t_mp = NULL; 128525cf1a30Sjl } 128625cf1a30Sjl 128725cf1a30Sjl /* 128825cf1a30Sjl * Verify the memory really did successfully detach 128925cf1a30Sjl * by checking for its non-existence in phys_install. 129025cf1a30Sjl */ 129125cf1a30Sjl rv = 0; 129225cf1a30Sjl memlist_read_lock(); 129325cf1a30Sjl if (s_mp->sbm_flags & DR_MFLAG_RELDONE) { 129425cf1a30Sjl x_mp = s_mp; 129525cf1a30Sjl rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist); 129625cf1a30Sjl } 129725cf1a30Sjl if (rv == 0 && t_mp && (t_mp->sbm_flags & DR_MFLAG_RELDONE)) { 129825cf1a30Sjl x_mp = t_mp; 129925cf1a30Sjl rv = memlist_intersect(phys_install, x_mp->sbm_del_mlist); 130025cf1a30Sjl } 130125cf1a30Sjl memlist_read_unlock(); 130225cf1a30Sjl 130325cf1a30Sjl if (rv) { 130425cf1a30Sjl /* error: memlist still in phys_install */ 130525cf1a30Sjl DR_DEV_INTERNAL_ERROR(&x_mp->sbm_cm); 130625cf1a30Sjl } 130725cf1a30Sjl 130825cf1a30Sjl /* 130925cf1a30Sjl * clean mem unit state and bail out if an error has been recorded. 131025cf1a30Sjl */ 131125cf1a30Sjl rv = 0; 131225cf1a30Sjl if (s_mp->sbm_cm.sbdev_error) { 131325cf1a30Sjl PR_MEM("%s: %s flags=%x", f, 1314*3103d4ceSjesusm s_mp->sbm_cm.sbdev_path, s_mp->sbm_flags); 131525cf1a30Sjl DR_DEV_CLR_UNREFERENCED(&s_mp->sbm_cm); 131625cf1a30Sjl DR_DEV_CLR_RELEASED(&s_mp->sbm_cm); 131725cf1a30Sjl dr_device_transition(&s_mp->sbm_cm, DR_STATE_CONFIGURED); 131825cf1a30Sjl rv = -1; 131925cf1a30Sjl } 132025cf1a30Sjl if (t_mp != NULL && t_mp->sbm_cm.sbdev_error != NULL) { 132125cf1a30Sjl PR_MEM("%s: %s flags=%x", f, 1322*3103d4ceSjesusm s_mp->sbm_cm.sbdev_path, s_mp->sbm_flags); 132325cf1a30Sjl DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm); 132425cf1a30Sjl DR_DEV_CLR_RELEASED(&t_mp->sbm_cm); 132525cf1a30Sjl dr_device_transition(&t_mp->sbm_cm, DR_STATE_CONFIGURED); 132625cf1a30Sjl rv = -1; 132725cf1a30Sjl } 132825cf1a30Sjl if (rv) 132925cf1a30Sjl goto cleanup; 133025cf1a30Sjl 133125cf1a30Sjl s_old_basepa = _ptob64(s_mp->sbm_basepfn); 133225cf1a30Sjl err = drmach_mem_get_info(s_mp->sbm_cm.sbdev_id, &minfo); 133325cf1a30Sjl ASSERT(err == NULL); 133425cf1a30Sjl s_new_basepa = minfo.mi_basepa; 133525cf1a30Sjl 133625cf1a30Sjl PR_MEM("%s:s_old_basepa: 0x%lx\n", f, s_old_basepa); 133725cf1a30Sjl PR_MEM("%s:s_new_basepa: 0x%lx\n", f, s_new_basepa); 133825cf1a30Sjl 133925cf1a30Sjl if (t_mp != NULL) { 134025cf1a30Sjl struct memlist *s_copy_mlist; 134125cf1a30Sjl 134225cf1a30Sjl t_old_basepa = _ptob64(t_mp->sbm_basepfn); 134325cf1a30Sjl err = drmach_mem_get_info(t_mp->sbm_cm.sbdev_id, &minfo); 134425cf1a30Sjl ASSERT(err == NULL); 134525cf1a30Sjl t_new_basepa = minfo.mi_basepa; 134625cf1a30Sjl 134725cf1a30Sjl PR_MEM("%s:t_old_basepa: 0x%lx\n", f, t_old_basepa); 134825cf1a30Sjl PR_MEM("%s:t_new_basepa: 0x%lx\n", f, t_new_basepa); 134925cf1a30Sjl 135025cf1a30Sjl /* 135125cf1a30Sjl * Construct copy list with original source addresses. 135225cf1a30Sjl * Used to add back excess target mem. 135325cf1a30Sjl */ 135425cf1a30Sjl s_copy_mlist = memlist_dup(s_mp->sbm_mlist); 135525cf1a30Sjl for (ml = s_mp->sbm_del_mlist; ml; ml = ml->next) { 135625cf1a30Sjl s_copy_mlist = memlist_del_span(s_copy_mlist, 135725cf1a30Sjl ml->address, ml->size); 135825cf1a30Sjl } 135925cf1a30Sjl 136025cf1a30Sjl PR_MEM("%s: source copy list:\n:", f); 136125cf1a30Sjl PR_MEMLIST_DUMP(s_copy_mlist); 136225cf1a30Sjl 136325cf1a30Sjl /* 136425cf1a30Sjl * We had to swap mem-units, so update 136525cf1a30Sjl * memlists accordingly with new base 136625cf1a30Sjl * addresses. 136725cf1a30Sjl */ 136825cf1a30Sjl for (ml = t_mp->sbm_mlist; ml; ml = ml->next) { 136925cf1a30Sjl ml->address -= t_old_basepa; 137025cf1a30Sjl ml->address += t_new_basepa; 137125cf1a30Sjl } 137225cf1a30Sjl 137325cf1a30Sjl /* 137425cf1a30Sjl * There is no need to explicitly rename the target delete 137525cf1a30Sjl * memlist, because sbm_del_mlist and sbm_mlist always 137625cf1a30Sjl * point to the same memlist for a copy/rename operation. 137725cf1a30Sjl */ 137825cf1a30Sjl ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist); 137925cf1a30Sjl 138025cf1a30Sjl PR_MEM("%s: renamed target memlist and delete memlist:\n", f); 138125cf1a30Sjl PR_MEMLIST_DUMP(t_mp->sbm_mlist); 138225cf1a30Sjl 138325cf1a30Sjl for (ml = s_mp->sbm_mlist; ml; ml = ml->next) { 138425cf1a30Sjl ml->address -= s_old_basepa; 138525cf1a30Sjl ml->address += s_new_basepa; 138625cf1a30Sjl } 138725cf1a30Sjl 138825cf1a30Sjl PR_MEM("%s: renamed source memlist:\n", f); 138925cf1a30Sjl PR_MEMLIST_DUMP(s_mp->sbm_mlist); 139068ac2337Sjl PR_MEM("%s: source dyn seg memlist:\n", f); 139168ac2337Sjl PR_MEMLIST_DUMP(s_mp->sbm_dyn_segs); 139225cf1a30Sjl 139325cf1a30Sjl /* 139425cf1a30Sjl * Keep track of dynamically added segments 139525cf1a30Sjl * since they cannot be split if we need to delete 139625cf1a30Sjl * excess source memory later for this board. 139725cf1a30Sjl */ 139825cf1a30Sjl if (t_mp->sbm_dyn_segs) 139925cf1a30Sjl memlist_delete(t_mp->sbm_dyn_segs); 140025cf1a30Sjl t_mp->sbm_dyn_segs = s_mp->sbm_dyn_segs; 140125cf1a30Sjl s_mp->sbm_dyn_segs = NULL; 140225cf1a30Sjl 140325cf1a30Sjl /* 140425cf1a30Sjl * Add back excess target memory. 140525cf1a30Sjl * Subtract out the portion of the target memory 140625cf1a30Sjl * node that was taken over by the source memory 140725cf1a30Sjl * node. 140825cf1a30Sjl */ 140925cf1a30Sjl t_excess_mlist = memlist_dup(t_mp->sbm_mlist); 141025cf1a30Sjl for (ml = s_copy_mlist; ml; ml = ml->next) { 141125cf1a30Sjl t_excess_mlist = 141225cf1a30Sjl memlist_del_span(t_excess_mlist, 141325cf1a30Sjl ml->address, ml->size); 141425cf1a30Sjl } 141568ac2337Sjl PR_MEM("%s: excess memlist:\n", f); 141668ac2337Sjl PR_MEMLIST_DUMP(t_excess_mlist); 141725cf1a30Sjl 141825cf1a30Sjl /* 141925cf1a30Sjl * Update dynamically added segs 142025cf1a30Sjl */ 142125cf1a30Sjl for (ml = s_mp->sbm_del_mlist; ml; ml = ml->next) { 142225cf1a30Sjl t_mp->sbm_dyn_segs = 142325cf1a30Sjl memlist_del_span(t_mp->sbm_dyn_segs, 142425cf1a30Sjl ml->address, ml->size); 142525cf1a30Sjl } 142625cf1a30Sjl for (ml = t_excess_mlist; ml; ml = ml->next) { 142725cf1a30Sjl t_mp->sbm_dyn_segs = 142825cf1a30Sjl memlist_cat_span(t_mp->sbm_dyn_segs, 142925cf1a30Sjl ml->address, ml->size); 143025cf1a30Sjl } 143125cf1a30Sjl PR_MEM("%s: %s: updated dynamic seg list:\n", 143225cf1a30Sjl f, t_mp->sbm_cm.sbdev_path); 143325cf1a30Sjl PR_MEMLIST_DUMP(t_mp->sbm_dyn_segs); 143425cf1a30Sjl 143525cf1a30Sjl if (t_excess_mlist != NULL) { 143668ac2337Sjl err = dr_process_excess_mlist(s_mp, t_mp, 1437*3103d4ceSjesusm t_excess_mlist); 143825cf1a30Sjl s_excess_mem_deleted = 1; 143925cf1a30Sjl } 144068ac2337Sjl 144125cf1a30Sjl memlist_delete(s_copy_mlist); 144225cf1a30Sjl 144325cf1a30Sjl #ifdef DEBUG 144425cf1a30Sjl /* 144525cf1a30Sjl * s_mp->sbm_del_mlist may still needed 144625cf1a30Sjl */ 144725cf1a30Sjl PR_MEM("%s: source delete memeory flag %d", 144825cf1a30Sjl f, s_excess_mem_deleted); 144925cf1a30Sjl PR_MEM("%s: source delete memlist", f); 145025cf1a30Sjl PR_MEMLIST_DUMP(s_mp->sbm_del_mlist); 145125cf1a30Sjl #endif 145225cf1a30Sjl 145325cf1a30Sjl } 145425cf1a30Sjl 145525cf1a30Sjl if (t_mp != NULL) { 145625cf1a30Sjl /* delete target's entire address space */ 145725cf1a30Sjl err = drmach_mem_del_span( 1458*3103d4ceSjesusm t_mp->sbm_cm.sbdev_id, t_old_basepa & ~ sm, sz); 145925cf1a30Sjl if (err) 146025cf1a30Sjl DRERR_SET_C(&t_mp->sbm_cm.sbdev_error, &err); 146125cf1a30Sjl ASSERT(err == NULL); 146225cf1a30Sjl 146325cf1a30Sjl /* 146425cf1a30Sjl * After the copy/rename, the original address space 146525cf1a30Sjl * for the source board (which is now located on the 146625cf1a30Sjl * target board) may now have some excess to be deleted. 146725cf1a30Sjl * Those excess memory on the source board are kept in 146825cf1a30Sjl * source board's sbm_del_mlist 146925cf1a30Sjl */ 147025cf1a30Sjl for (ml = s_mp->sbm_del_mlist; !s_excess_mem_deleted && ml; 1471*3103d4ceSjesusm ml = ml->next) { 147225cf1a30Sjl PR_MEM("%s: delete source excess memory", f); 147325cf1a30Sjl PR_MEMLIST_DUMP(ml); 147425cf1a30Sjl 147525cf1a30Sjl err = drmach_mem_del_span(s_mp->sbm_cm.sbdev_id, 1476*3103d4ceSjesusm ml->address, ml->size); 147725cf1a30Sjl if (err) 147825cf1a30Sjl DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err); 147925cf1a30Sjl ASSERT(err == NULL); 148025cf1a30Sjl } 148125cf1a30Sjl 148225cf1a30Sjl } else { 148325cf1a30Sjl /* delete board's entire address space */ 148425cf1a30Sjl err = drmach_mem_del_span(s_mp->sbm_cm.sbdev_id, 1485*3103d4ceSjesusm s_old_basepa & ~ sm, sz); 148625cf1a30Sjl if (err) 148725cf1a30Sjl DRERR_SET_C(&s_mp->sbm_cm.sbdev_error, &err); 148825cf1a30Sjl ASSERT(err == NULL); 148925cf1a30Sjl } 149025cf1a30Sjl 149125cf1a30Sjl cleanup: 149225cf1a30Sjl /* clean up target mem unit */ 149325cf1a30Sjl if (t_mp != NULL) { 149425cf1a30Sjl memlist_delete(t_mp->sbm_del_mlist); 149525cf1a30Sjl /* no need to delete sbm_mlist, it shares sbm_del_mlist */ 149625cf1a30Sjl 149725cf1a30Sjl t_mp->sbm_del_mlist = NULL; 149825cf1a30Sjl t_mp->sbm_mlist = NULL; 149925cf1a30Sjl t_mp->sbm_peer = NULL; 150025cf1a30Sjl t_mp->sbm_flags = 0; 150125cf1a30Sjl t_mp->sbm_cm.sbdev_busy = 0; 150225cf1a30Sjl dr_init_mem_unit_data(t_mp); 150325cf1a30Sjl 150425cf1a30Sjl } 150525cf1a30Sjl if (t_mp != NULL && t_mp->sbm_cm.sbdev_error == NULL) { 150625cf1a30Sjl /* 150725cf1a30Sjl * now that copy/rename has completed, undo this 150825cf1a30Sjl * work that was done in dr_release_mem_done. 150925cf1a30Sjl */ 151025cf1a30Sjl DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm); 151125cf1a30Sjl DR_DEV_CLR_RELEASED(&t_mp->sbm_cm); 151225cf1a30Sjl dr_device_transition(&t_mp->sbm_cm, DR_STATE_CONFIGURED); 151325cf1a30Sjl } 151425cf1a30Sjl 151525cf1a30Sjl /* 151625cf1a30Sjl * clean up (source) board's mem unit structure. 151725cf1a30Sjl * NOTE: sbm_mlist is retained if no error has been record (in other 151825cf1a30Sjl * words, when s_mp->sbm_cm.sbdev_error is NULL). This memlist is 151925cf1a30Sjl * referred to elsewhere as the cached memlist. The cached memlist 152025cf1a30Sjl * is used to re-attach (configure back in) this memunit from the 152125cf1a30Sjl * unconfigured state. The memlist is retained because it may 152225cf1a30Sjl * represent bad pages that were detected while the memory was 152325cf1a30Sjl * configured into the OS. The OS deletes bad pages from phys_install. 152425cf1a30Sjl * Those deletes, if any, will be represented in the cached mlist. 152525cf1a30Sjl */ 152625cf1a30Sjl if (s_mp->sbm_del_mlist && s_mp->sbm_del_mlist != s_mp->sbm_mlist) 152725cf1a30Sjl memlist_delete(s_mp->sbm_del_mlist); 152825cf1a30Sjl 152925cf1a30Sjl if (s_mp->sbm_cm.sbdev_error && s_mp->sbm_mlist) { 153025cf1a30Sjl memlist_delete(s_mp->sbm_mlist); 153125cf1a30Sjl s_mp->sbm_mlist = NULL; 153225cf1a30Sjl } 153325cf1a30Sjl 153425cf1a30Sjl if (s_mp->sbm_dyn_segs != NULL && s_mp->sbm_cm.sbdev_error == 0) { 153525cf1a30Sjl memlist_delete(s_mp->sbm_dyn_segs); 153625cf1a30Sjl s_mp->sbm_dyn_segs = NULL; 153725cf1a30Sjl } 153825cf1a30Sjl 153925cf1a30Sjl s_mp->sbm_del_mlist = NULL; 154025cf1a30Sjl s_mp->sbm_peer = NULL; 154125cf1a30Sjl s_mp->sbm_flags = 0; 154225cf1a30Sjl s_mp->sbm_cm.sbdev_busy = 0; 154325cf1a30Sjl dr_init_mem_unit_data(s_mp); 154425cf1a30Sjl 154525cf1a30Sjl PR_MEM("%s: cached memlist for %s:", f, s_mp->sbm_cm.sbdev_path); 154625cf1a30Sjl PR_MEMLIST_DUMP(s_mp->sbm_mlist); 154725cf1a30Sjl 154825cf1a30Sjl return (0); 154925cf1a30Sjl } 155025cf1a30Sjl 155125cf1a30Sjl /* 155225cf1a30Sjl * Successful return from this function will have the memory 155325cf1a30Sjl * handle in bp->b_dev[..mem-unit...].sbm_memhandle allocated 155425cf1a30Sjl * and waiting. This routine's job is to select the memory that 155525cf1a30Sjl * actually has to be released (detached) which may not necessarily 155625cf1a30Sjl * be the same memory node that came in in devlist[], 155725cf1a30Sjl * i.e. a copy-rename is needed. 155825cf1a30Sjl */ 155925cf1a30Sjl int 156025cf1a30Sjl dr_pre_release_mem(dr_handle_t *hp, dr_common_unit_t **devlist, int devnum) 156125cf1a30Sjl { 156225cf1a30Sjl int d; 156325cf1a30Sjl int err_flag = 0; 156425cf1a30Sjl static fn_t f = "dr_pre_release_mem"; 156525cf1a30Sjl 156625cf1a30Sjl PR_MEM("%s...\n", f); 156725cf1a30Sjl 156825cf1a30Sjl for (d = 0; d < devnum; d++) { 156925cf1a30Sjl dr_mem_unit_t *mp = (dr_mem_unit_t *)devlist[d]; 157025cf1a30Sjl int rv; 157125cf1a30Sjl memquery_t mq; 157225cf1a30Sjl struct memlist *ml; 157325cf1a30Sjl 157425cf1a30Sjl if (mp->sbm_cm.sbdev_error) { 157525cf1a30Sjl err_flag = 1; 157625cf1a30Sjl continue; 157725cf1a30Sjl } else if (!kcage_on) { 157825cf1a30Sjl dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_KCAGE_OFF); 157925cf1a30Sjl err_flag = 1; 158025cf1a30Sjl continue; 158125cf1a30Sjl } 158225cf1a30Sjl 158325cf1a30Sjl if (mp->sbm_flags & DR_MFLAG_RESERVED) { 158425cf1a30Sjl /* 158525cf1a30Sjl * Board is currently involved in a delete 158625cf1a30Sjl * memory operation. Can't detach this guy until 158725cf1a30Sjl * that operation completes. 158825cf1a30Sjl */ 158925cf1a30Sjl dr_dev_err(CE_WARN, &mp->sbm_cm, ESBD_INVAL); 159025cf1a30Sjl err_flag = 1; 159125cf1a30Sjl break; 159225cf1a30Sjl } 159325cf1a30Sjl 159425cf1a30Sjl /* flags should be clean at this time */ 159525cf1a30Sjl ASSERT(mp->sbm_flags == 0); 159625cf1a30Sjl 159725cf1a30Sjl ASSERT(mp->sbm_mlist == NULL); 159825cf1a30Sjl ASSERT(mp->sbm_del_mlist == NULL); 159925cf1a30Sjl if (mp->sbm_mlist != NULL) { 160025cf1a30Sjl memlist_delete(mp->sbm_mlist); 160125cf1a30Sjl mp->sbm_mlist = NULL; 160225cf1a30Sjl } 160325cf1a30Sjl 160425cf1a30Sjl ml = dr_get_memlist(mp); 160525cf1a30Sjl if (ml == NULL) { 160625cf1a30Sjl err_flag = 1; 160725cf1a30Sjl PR_MEM("%s: no memlist found for %s\n", 160825cf1a30Sjl f, mp->sbm_cm.sbdev_path); 160925cf1a30Sjl continue; 161025cf1a30Sjl } 161125cf1a30Sjl 161225cf1a30Sjl /* 161325cf1a30Sjl * Check whether the detaching memory requires a 161425cf1a30Sjl * copy-rename. 161525cf1a30Sjl */ 161625cf1a30Sjl ASSERT(mp->sbm_npages != 0); 161768ac2337Sjl 161825cf1a30Sjl rv = dr_del_mlist_query(ml, &mq); 161925cf1a30Sjl if (rv != KPHYSM_OK) { 162025cf1a30Sjl memlist_delete(ml); 162125cf1a30Sjl DR_DEV_INTERNAL_ERROR(&mp->sbm_cm); 162225cf1a30Sjl err_flag = 1; 162325cf1a30Sjl break; 162425cf1a30Sjl } 162525cf1a30Sjl 162625cf1a30Sjl if (mq.nonrelocatable != 0) { 162725cf1a30Sjl if (!(dr_cmd_flags(hp) & 1628*3103d4ceSjesusm (SBD_FLAG_FORCE | SBD_FLAG_QUIESCE_OKAY))) { 162925cf1a30Sjl memlist_delete(ml); 163025cf1a30Sjl /* caller wasn't prompted for a suspend */ 163125cf1a30Sjl dr_dev_err(CE_WARN, &mp->sbm_cm, 1632*3103d4ceSjesusm ESBD_QUIESCE_REQD); 163325cf1a30Sjl err_flag = 1; 163425cf1a30Sjl break; 163525cf1a30Sjl } 163625cf1a30Sjl } 163725cf1a30Sjl 163825cf1a30Sjl /* allocate a kphysm handle */ 163925cf1a30Sjl rv = kphysm_del_gethandle(&mp->sbm_memhandle); 164025cf1a30Sjl if (rv != KPHYSM_OK) { 164125cf1a30Sjl memlist_delete(ml); 164225cf1a30Sjl 164325cf1a30Sjl DR_DEV_INTERNAL_ERROR(&mp->sbm_cm); 164425cf1a30Sjl err_flag = 1; 164525cf1a30Sjl break; 164625cf1a30Sjl } 164725cf1a30Sjl mp->sbm_flags |= DR_MFLAG_RELOWNER; 164825cf1a30Sjl 164925cf1a30Sjl if ((mq.nonrelocatable != 0) || 1650*3103d4ceSjesusm dr_reserve_mem_spans(&mp->sbm_memhandle, ml)) { 165125cf1a30Sjl /* 165225cf1a30Sjl * Either the detaching memory node contains 165325cf1a30Sjl * non-reloc memory or we failed to reserve the 165425cf1a30Sjl * detaching memory node (which did _not_ have 165525cf1a30Sjl * any non-reloc memory, i.e. some non-reloc mem 165625cf1a30Sjl * got onboard). 165725cf1a30Sjl */ 165825cf1a30Sjl 165925cf1a30Sjl if (dr_select_mem_target(hp, mp, ml)) { 166025cf1a30Sjl int rv; 166125cf1a30Sjl 166225cf1a30Sjl /* 166325cf1a30Sjl * We had no luck locating a target 166425cf1a30Sjl * memory node to be the recipient of 166525cf1a30Sjl * the non-reloc memory on the node 166625cf1a30Sjl * we're trying to detach. 166725cf1a30Sjl * Clean up be disposing the mem handle 166825cf1a30Sjl * and the mem list. 166925cf1a30Sjl */ 167025cf1a30Sjl rv = kphysm_del_release(mp->sbm_memhandle); 167125cf1a30Sjl if (rv != KPHYSM_OK) { 167225cf1a30Sjl /* 167325cf1a30Sjl * can do nothing but complain 167425cf1a30Sjl * and hope helpful for debug 167525cf1a30Sjl */ 167625cf1a30Sjl cmn_err(CE_WARN, "%s: unexpected" 1677*3103d4ceSjesusm " kphysm_del_release return" 1678*3103d4ceSjesusm " value %d", 1679*3103d4ceSjesusm f, rv); 168025cf1a30Sjl } 168125cf1a30Sjl mp->sbm_flags &= ~DR_MFLAG_RELOWNER; 168225cf1a30Sjl 168325cf1a30Sjl memlist_delete(ml); 168425cf1a30Sjl 168525cf1a30Sjl /* make sure sbm_flags is clean */ 168625cf1a30Sjl ASSERT(mp->sbm_flags == 0); 168725cf1a30Sjl 168825cf1a30Sjl dr_dev_err(CE_WARN, 1689*3103d4ceSjesusm &mp->sbm_cm, ESBD_NO_TARGET); 169025cf1a30Sjl 169125cf1a30Sjl err_flag = 1; 169225cf1a30Sjl break; 169325cf1a30Sjl } 169425cf1a30Sjl 169525cf1a30Sjl /* 169625cf1a30Sjl * ml is not memlist_delete'd here because 169725cf1a30Sjl * it has been assigned to mp->sbm_mlist 169825cf1a30Sjl * by dr_select_mem_target. 169925cf1a30Sjl */ 170025cf1a30Sjl } else { 170125cf1a30Sjl /* no target needed to detach this board */ 170225cf1a30Sjl mp->sbm_flags |= DR_MFLAG_RESERVED; 170325cf1a30Sjl mp->sbm_peer = NULL; 170425cf1a30Sjl mp->sbm_del_mlist = ml; 170525cf1a30Sjl mp->sbm_mlist = ml; 170625cf1a30Sjl mp->sbm_cm.sbdev_busy = 1; 170725cf1a30Sjl } 170825cf1a30Sjl #ifdef DEBUG 170925cf1a30Sjl ASSERT(mp->sbm_mlist != NULL); 171025cf1a30Sjl 171125cf1a30Sjl if (mp->sbm_flags & DR_MFLAG_SOURCE) { 171225cf1a30Sjl PR_MEM("%s: release of %s requires copy/rename;" 1713*3103d4ceSjesusm " selected target board %s\n", 1714*3103d4ceSjesusm f, 1715*3103d4ceSjesusm mp->sbm_cm.sbdev_path, 1716*3103d4ceSjesusm mp->sbm_peer->sbm_cm.sbdev_path); 171725cf1a30Sjl } else { 171825cf1a30Sjl PR_MEM("%s: copy/rename not required to release %s\n", 1719*3103d4ceSjesusm f, mp->sbm_cm.sbdev_path); 172025cf1a30Sjl } 172125cf1a30Sjl 172225cf1a30Sjl ASSERT(mp->sbm_flags & DR_MFLAG_RELOWNER); 172325cf1a30Sjl ASSERT(mp->sbm_flags & DR_MFLAG_RESERVED); 172425cf1a30Sjl #endif 172525cf1a30Sjl } 172625cf1a30Sjl 172725cf1a30Sjl return (err_flag ? -1 : 0); 172825cf1a30Sjl } 172925cf1a30Sjl 173025cf1a30Sjl void 173125cf1a30Sjl dr_release_mem_done(dr_common_unit_t *cp) 173225cf1a30Sjl { 173325cf1a30Sjl dr_mem_unit_t *s_mp = (dr_mem_unit_t *)cp; 173425cf1a30Sjl dr_mem_unit_t *t_mp, *mp; 173525cf1a30Sjl int rv; 173625cf1a30Sjl static fn_t f = "dr_release_mem_done"; 173725cf1a30Sjl 173825cf1a30Sjl /* 173925cf1a30Sjl * This unit will be flagged with DR_MFLAG_SOURCE, if it 174025cf1a30Sjl * has a target unit. 174125cf1a30Sjl */ 174225cf1a30Sjl if (s_mp->sbm_flags & DR_MFLAG_SOURCE) { 174325cf1a30Sjl t_mp = s_mp->sbm_peer; 174425cf1a30Sjl ASSERT(t_mp != NULL); 174525cf1a30Sjl ASSERT(t_mp->sbm_peer == s_mp); 174625cf1a30Sjl ASSERT(t_mp->sbm_flags & DR_MFLAG_TARGET); 174725cf1a30Sjl ASSERT(t_mp->sbm_flags & DR_MFLAG_RESERVED); 174825cf1a30Sjl } else { 174925cf1a30Sjl /* this is no target unit */ 175025cf1a30Sjl t_mp = NULL; 175125cf1a30Sjl } 175225cf1a30Sjl 175325cf1a30Sjl /* free delete handle */ 175425cf1a30Sjl ASSERT(s_mp->sbm_flags & DR_MFLAG_RELOWNER); 175525cf1a30Sjl ASSERT(s_mp->sbm_flags & DR_MFLAG_RESERVED); 175625cf1a30Sjl rv = kphysm_del_release(s_mp->sbm_memhandle); 175725cf1a30Sjl if (rv != KPHYSM_OK) { 175825cf1a30Sjl /* 175925cf1a30Sjl * can do nothing but complain 176025cf1a30Sjl * and hope helpful for debug 176125cf1a30Sjl */ 176225cf1a30Sjl cmn_err(CE_WARN, "%s: unexpected kphysm_del_release" 1763*3103d4ceSjesusm " return value %d", f, rv); 176425cf1a30Sjl } 176525cf1a30Sjl s_mp->sbm_flags &= ~DR_MFLAG_RELOWNER; 176625cf1a30Sjl 176725cf1a30Sjl /* 176825cf1a30Sjl * If an error was encountered during release, clean up 176925cf1a30Sjl * the source (and target, if present) unit data. 177025cf1a30Sjl */ 177125cf1a30Sjl /* XXX Can we know that sbdev_error was encountered during release? */ 177225cf1a30Sjl if (s_mp->sbm_cm.sbdev_error != NULL) { 177325cf1a30Sjl 177425cf1a30Sjl if (t_mp != NULL) { 177525cf1a30Sjl ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist); 177625cf1a30Sjl t_mp->sbm_del_mlist = NULL; 177725cf1a30Sjl 177825cf1a30Sjl if (t_mp->sbm_mlist != NULL) { 177925cf1a30Sjl memlist_delete(t_mp->sbm_mlist); 178025cf1a30Sjl t_mp->sbm_mlist = NULL; 178125cf1a30Sjl } 178225cf1a30Sjl 178325cf1a30Sjl t_mp->sbm_peer = NULL; 178425cf1a30Sjl t_mp->sbm_flags = 0; 178525cf1a30Sjl t_mp->sbm_cm.sbdev_busy = 0; 178625cf1a30Sjl } 178725cf1a30Sjl 178825cf1a30Sjl if (s_mp->sbm_del_mlist != s_mp->sbm_mlist) 178925cf1a30Sjl memlist_delete(s_mp->sbm_del_mlist); 179025cf1a30Sjl s_mp->sbm_del_mlist = NULL; 179125cf1a30Sjl 179225cf1a30Sjl if (s_mp->sbm_mlist != NULL) { 179325cf1a30Sjl memlist_delete(s_mp->sbm_mlist); 179425cf1a30Sjl s_mp->sbm_mlist = NULL; 179525cf1a30Sjl } 179625cf1a30Sjl 179725cf1a30Sjl s_mp->sbm_peer = NULL; 179825cf1a30Sjl s_mp->sbm_flags = 0; 179925cf1a30Sjl s_mp->sbm_cm.sbdev_busy = 0; 180025cf1a30Sjl 180125cf1a30Sjl /* bail out */ 180225cf1a30Sjl return; 180325cf1a30Sjl } 180425cf1a30Sjl 180525cf1a30Sjl DR_DEV_SET_RELEASED(&s_mp->sbm_cm); 180625cf1a30Sjl dr_device_transition(&s_mp->sbm_cm, DR_STATE_RELEASE); 180725cf1a30Sjl 180825cf1a30Sjl if (t_mp != NULL) { 180925cf1a30Sjl /* 181025cf1a30Sjl * the kphysm delete operation that drained the source 181125cf1a30Sjl * board also drained this target board. Since the source 181225cf1a30Sjl * board drain is now known to have succeeded, we know this 181325cf1a30Sjl * target board is drained too. 181425cf1a30Sjl * 181525cf1a30Sjl * because DR_DEV_SET_RELEASED and dr_device_transition 181625cf1a30Sjl * is done here, the dr_release_dev_done should not 181725cf1a30Sjl * fail. 181825cf1a30Sjl */ 181925cf1a30Sjl DR_DEV_SET_RELEASED(&t_mp->sbm_cm); 182025cf1a30Sjl dr_device_transition(&t_mp->sbm_cm, DR_STATE_RELEASE); 182125cf1a30Sjl 182225cf1a30Sjl /* 182325cf1a30Sjl * NOTE: do not transition target's board state, 182425cf1a30Sjl * even if the mem-unit was the last configure 182525cf1a30Sjl * unit of the board. When copy/rename completes 182625cf1a30Sjl * this mem-unit will transitioned back to 182725cf1a30Sjl * the configured state. In the meantime, the 182825cf1a30Sjl * board's must remain as is. 182925cf1a30Sjl */ 183025cf1a30Sjl } 183125cf1a30Sjl 183225cf1a30Sjl /* if board(s) had deleted memory, verify it is gone */ 183325cf1a30Sjl rv = 0; 183425cf1a30Sjl memlist_read_lock(); 183525cf1a30Sjl if (s_mp->sbm_del_mlist != NULL) { 183625cf1a30Sjl mp = s_mp; 183725cf1a30Sjl rv = memlist_intersect(phys_install, mp->sbm_del_mlist); 183825cf1a30Sjl } 183925cf1a30Sjl if (rv == 0 && t_mp && t_mp->sbm_del_mlist != NULL) { 184025cf1a30Sjl mp = t_mp; 184125cf1a30Sjl rv = memlist_intersect(phys_install, mp->sbm_del_mlist); 184225cf1a30Sjl } 184325cf1a30Sjl memlist_read_unlock(); 184425cf1a30Sjl if (rv) { 184525cf1a30Sjl cmn_err(CE_WARN, "%s: %smem-unit (%d.%d): " 1846*3103d4ceSjesusm "deleted memory still found in phys_install", 1847*3103d4ceSjesusm f, 1848*3103d4ceSjesusm (mp == t_mp ? "target " : ""), 1849*3103d4ceSjesusm mp->sbm_cm.sbdev_bp->b_num, 1850*3103d4ceSjesusm mp->sbm_cm.sbdev_unum); 185125cf1a30Sjl 185225cf1a30Sjl DR_DEV_INTERNAL_ERROR(&s_mp->sbm_cm); 185325cf1a30Sjl return; 185425cf1a30Sjl } 185525cf1a30Sjl 185625cf1a30Sjl s_mp->sbm_flags |= DR_MFLAG_RELDONE; 185725cf1a30Sjl if (t_mp != NULL) 185825cf1a30Sjl t_mp->sbm_flags |= DR_MFLAG_RELDONE; 185925cf1a30Sjl 186025cf1a30Sjl /* this should not fail */ 186125cf1a30Sjl if (dr_release_dev_done(&s_mp->sbm_cm) != 0) { 186225cf1a30Sjl /* catch this in debug kernels */ 186325cf1a30Sjl ASSERT(0); 186425cf1a30Sjl return; 186525cf1a30Sjl } 186625cf1a30Sjl 186725cf1a30Sjl PR_MEM("%s: marking %s release DONE\n", 1868*3103d4ceSjesusm f, s_mp->sbm_cm.sbdev_path); 186925cf1a30Sjl 187025cf1a30Sjl s_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED; 187125cf1a30Sjl 187225cf1a30Sjl if (t_mp != NULL) { 187325cf1a30Sjl /* should not fail */ 187425cf1a30Sjl rv = dr_release_dev_done(&t_mp->sbm_cm); 187525cf1a30Sjl if (rv != 0) { 187625cf1a30Sjl /* catch this in debug kernels */ 187725cf1a30Sjl ASSERT(0); 187825cf1a30Sjl return; 187925cf1a30Sjl } 188025cf1a30Sjl 188125cf1a30Sjl PR_MEM("%s: marking %s release DONE\n", 1882*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 188325cf1a30Sjl 188425cf1a30Sjl t_mp->sbm_cm.sbdev_ostate = SBD_STAT_UNCONFIGURED; 188525cf1a30Sjl } 188625cf1a30Sjl } 188725cf1a30Sjl 188825cf1a30Sjl /*ARGSUSED*/ 188925cf1a30Sjl int 189025cf1a30Sjl dr_disconnect_mem(dr_mem_unit_t *mp) 189125cf1a30Sjl { 189225cf1a30Sjl static fn_t f = "dr_disconnect_mem"; 189325cf1a30Sjl update_membounds_t umb; 189425cf1a30Sjl 189525cf1a30Sjl #ifdef DEBUG 189625cf1a30Sjl int state = mp->sbm_cm.sbdev_state; 189725cf1a30Sjl ASSERT(state == DR_STATE_CONNECTED || 1898*3103d4ceSjesusm state == DR_STATE_UNCONFIGURED); 189925cf1a30Sjl #endif 190025cf1a30Sjl 190125cf1a30Sjl PR_MEM("%s...\n", f); 190225cf1a30Sjl 190325cf1a30Sjl if (mp->sbm_del_mlist && mp->sbm_del_mlist != mp->sbm_mlist) 190425cf1a30Sjl memlist_delete(mp->sbm_del_mlist); 190525cf1a30Sjl mp->sbm_del_mlist = NULL; 190625cf1a30Sjl 190725cf1a30Sjl if (mp->sbm_mlist) { 190825cf1a30Sjl memlist_delete(mp->sbm_mlist); 190925cf1a30Sjl mp->sbm_mlist = NULL; 191025cf1a30Sjl } 191125cf1a30Sjl 191225cf1a30Sjl /* 191325cf1a30Sjl * Remove memory from lgroup 191425cf1a30Sjl * For now, only board info is required. 191525cf1a30Sjl */ 191625cf1a30Sjl umb.u_board = mp->sbm_cm.sbdev_bp->b_num; 191725cf1a30Sjl umb.u_base = (uint64_t)-1; 191825cf1a30Sjl umb.u_len = (uint64_t)-1; 191925cf1a30Sjl 192025cf1a30Sjl lgrp_plat_config(LGRP_CONFIG_MEM_DEL, (uintptr_t)&umb); 192125cf1a30Sjl 192225cf1a30Sjl return (0); 192325cf1a30Sjl } 192425cf1a30Sjl 192525cf1a30Sjl int 192625cf1a30Sjl dr_cancel_mem(dr_mem_unit_t *s_mp) 192725cf1a30Sjl { 192825cf1a30Sjl dr_mem_unit_t *t_mp; 192925cf1a30Sjl dr_state_t state; 193025cf1a30Sjl static fn_t f = "dr_cancel_mem"; 193125cf1a30Sjl 193225cf1a30Sjl state = s_mp->sbm_cm.sbdev_state; 193325cf1a30Sjl 193425cf1a30Sjl if (s_mp->sbm_flags & DR_MFLAG_TARGET) { 193525cf1a30Sjl /* must cancel source board, not target board */ 193625cf1a30Sjl /* TODO: set error */ 193725cf1a30Sjl return (-1); 193825cf1a30Sjl } else if (s_mp->sbm_flags & DR_MFLAG_SOURCE) { 193925cf1a30Sjl t_mp = s_mp->sbm_peer; 194025cf1a30Sjl ASSERT(t_mp != NULL); 194125cf1a30Sjl ASSERT(t_mp->sbm_peer == s_mp); 194225cf1a30Sjl 194325cf1a30Sjl /* must always match the source board's state */ 194425cf1a30Sjl /* TODO: is this assertion correct? */ 194525cf1a30Sjl ASSERT(t_mp->sbm_cm.sbdev_state == state); 194625cf1a30Sjl } else { 194725cf1a30Sjl /* this is no target unit */ 194825cf1a30Sjl t_mp = NULL; 194925cf1a30Sjl } 195025cf1a30Sjl 195125cf1a30Sjl switch (state) { 195225cf1a30Sjl case DR_STATE_UNREFERENCED: /* state set by dr_release_dev_done */ 195325cf1a30Sjl ASSERT((s_mp->sbm_flags & DR_MFLAG_RELOWNER) == 0); 195425cf1a30Sjl 195525cf1a30Sjl if (t_mp != NULL && t_mp->sbm_del_mlist != NULL) { 195625cf1a30Sjl PR_MEM("%s: undoing target %s memory delete\n", 1957*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 195825cf1a30Sjl dr_add_memory_spans(t_mp, t_mp->sbm_del_mlist); 195925cf1a30Sjl 196025cf1a30Sjl DR_DEV_CLR_UNREFERENCED(&t_mp->sbm_cm); 196125cf1a30Sjl } 196225cf1a30Sjl 196325cf1a30Sjl if (s_mp->sbm_del_mlist != NULL) { 196425cf1a30Sjl PR_MEM("%s: undoing %s memory delete\n", 1965*3103d4ceSjesusm f, s_mp->sbm_cm.sbdev_path); 196625cf1a30Sjl 196725cf1a30Sjl dr_add_memory_spans(s_mp, s_mp->sbm_del_mlist); 196825cf1a30Sjl } 196925cf1a30Sjl 197025cf1a30Sjl /*FALLTHROUGH*/ 197125cf1a30Sjl 197225cf1a30Sjl /* TODO: should no longer be possible to see the release state here */ 197325cf1a30Sjl case DR_STATE_RELEASE: /* state set by dr_release_mem_done */ 197425cf1a30Sjl 197525cf1a30Sjl ASSERT((s_mp->sbm_flags & DR_MFLAG_RELOWNER) == 0); 197625cf1a30Sjl 197725cf1a30Sjl if (t_mp != NULL) { 197825cf1a30Sjl ASSERT(t_mp->sbm_del_mlist == t_mp->sbm_mlist); 197925cf1a30Sjl t_mp->sbm_del_mlist = NULL; 198025cf1a30Sjl 198125cf1a30Sjl if (t_mp->sbm_mlist != NULL) { 198225cf1a30Sjl memlist_delete(t_mp->sbm_mlist); 198325cf1a30Sjl t_mp->sbm_mlist = NULL; 198425cf1a30Sjl } 198525cf1a30Sjl 198625cf1a30Sjl t_mp->sbm_peer = NULL; 198725cf1a30Sjl t_mp->sbm_flags = 0; 198825cf1a30Sjl t_mp->sbm_cm.sbdev_busy = 0; 198925cf1a30Sjl dr_init_mem_unit_data(t_mp); 199025cf1a30Sjl 199125cf1a30Sjl DR_DEV_CLR_RELEASED(&t_mp->sbm_cm); 199225cf1a30Sjl 199325cf1a30Sjl dr_device_transition( 1994*3103d4ceSjesusm &t_mp->sbm_cm, DR_STATE_CONFIGURED); 199525cf1a30Sjl } 199625cf1a30Sjl 199725cf1a30Sjl if (s_mp->sbm_del_mlist != s_mp->sbm_mlist) 199825cf1a30Sjl memlist_delete(s_mp->sbm_del_mlist); 199925cf1a30Sjl s_mp->sbm_del_mlist = NULL; 200025cf1a30Sjl 200125cf1a30Sjl if (s_mp->sbm_mlist != NULL) { 200225cf1a30Sjl memlist_delete(s_mp->sbm_mlist); 200325cf1a30Sjl s_mp->sbm_mlist = NULL; 200425cf1a30Sjl } 200525cf1a30Sjl 200625cf1a30Sjl s_mp->sbm_peer = NULL; 200725cf1a30Sjl s_mp->sbm_flags = 0; 200825cf1a30Sjl s_mp->sbm_cm.sbdev_busy = 0; 200925cf1a30Sjl dr_init_mem_unit_data(s_mp); 201025cf1a30Sjl 201125cf1a30Sjl return (0); 201225cf1a30Sjl 201325cf1a30Sjl default: 201425cf1a30Sjl PR_MEM("%s: WARNING unexpected state (%d) for %s\n", 2015*3103d4ceSjesusm f, (int)state, s_mp->sbm_cm.sbdev_path); 201625cf1a30Sjl 201725cf1a30Sjl return (-1); 201825cf1a30Sjl } 201925cf1a30Sjl /*NOTREACHED*/ 202025cf1a30Sjl } 202125cf1a30Sjl 202225cf1a30Sjl void 202325cf1a30Sjl dr_init_mem_unit(dr_mem_unit_t *mp) 202425cf1a30Sjl { 202525cf1a30Sjl dr_state_t new_state; 202625cf1a30Sjl 202725cf1a30Sjl 202825cf1a30Sjl if (DR_DEV_IS_ATTACHED(&mp->sbm_cm)) { 202925cf1a30Sjl new_state = DR_STATE_CONFIGURED; 203025cf1a30Sjl mp->sbm_cm.sbdev_cond = SBD_COND_OK; 203125cf1a30Sjl } else if (DR_DEV_IS_PRESENT(&mp->sbm_cm)) { 203225cf1a30Sjl new_state = DR_STATE_CONNECTED; 203325cf1a30Sjl mp->sbm_cm.sbdev_cond = SBD_COND_OK; 203425cf1a30Sjl } else if (mp->sbm_cm.sbdev_id != (drmachid_t)0) { 203525cf1a30Sjl new_state = DR_STATE_OCCUPIED; 203625cf1a30Sjl } else { 203725cf1a30Sjl new_state = DR_STATE_EMPTY; 203825cf1a30Sjl } 203925cf1a30Sjl 204025cf1a30Sjl if (DR_DEV_IS_PRESENT(&mp->sbm_cm)) 204125cf1a30Sjl dr_init_mem_unit_data(mp); 204225cf1a30Sjl 204325cf1a30Sjl /* delay transition until fully initialized */ 204425cf1a30Sjl dr_device_transition(&mp->sbm_cm, new_state); 204525cf1a30Sjl } 204625cf1a30Sjl 204725cf1a30Sjl static void 204825cf1a30Sjl dr_init_mem_unit_data(dr_mem_unit_t *mp) 204925cf1a30Sjl { 205025cf1a30Sjl drmachid_t id = mp->sbm_cm.sbdev_id; 205125cf1a30Sjl drmach_mem_info_t minfo; 205225cf1a30Sjl sbd_error_t *err; 205325cf1a30Sjl static fn_t f = "dr_init_mem_unit_data"; 205425cf1a30Sjl update_membounds_t umb; 205525cf1a30Sjl 205625cf1a30Sjl PR_MEM("%s...\n", f); 205725cf1a30Sjl 205825cf1a30Sjl /* a little sanity checking */ 205925cf1a30Sjl ASSERT(mp->sbm_peer == NULL); 206025cf1a30Sjl ASSERT(mp->sbm_flags == 0); 206125cf1a30Sjl 206225cf1a30Sjl if (err = drmach_mem_get_info(id, &minfo)) { 206325cf1a30Sjl DRERR_SET_C(&mp->sbm_cm.sbdev_error, &err); 206425cf1a30Sjl return; 206525cf1a30Sjl } 206625cf1a30Sjl mp->sbm_basepfn = _b64top(minfo.mi_basepa); 206725cf1a30Sjl mp->sbm_npages = _b64top(minfo.mi_size); 206825cf1a30Sjl mp->sbm_alignment_mask = _b64top(minfo.mi_alignment_mask); 206925cf1a30Sjl mp->sbm_slice_size = minfo.mi_slice_size; 207025cf1a30Sjl 207125cf1a30Sjl /* 207225cf1a30Sjl * Add memory to lgroup 207325cf1a30Sjl */ 207425cf1a30Sjl umb.u_board = mp->sbm_cm.sbdev_bp->b_num; 207525cf1a30Sjl umb.u_base = (uint64_t)mp->sbm_basepfn << MMU_PAGESHIFT; 207625cf1a30Sjl umb.u_len = (uint64_t)mp->sbm_npages << MMU_PAGESHIFT; 207725cf1a30Sjl 207825cf1a30Sjl lgrp_plat_config(LGRP_CONFIG_MEM_ADD, (uintptr_t)&umb); 207925cf1a30Sjl 208025cf1a30Sjl PR_MEM("%s: %s (basepfn = 0x%lx, npgs = %ld)\n", 2081*3103d4ceSjesusm f, mp->sbm_cm.sbdev_path, mp->sbm_basepfn, mp->sbm_npages); 208225cf1a30Sjl } 208325cf1a30Sjl 208425cf1a30Sjl static int 208525cf1a30Sjl dr_reserve_mem_spans(memhandle_t *mhp, struct memlist *ml) 208625cf1a30Sjl { 208725cf1a30Sjl int err; 208825cf1a30Sjl pfn_t base; 208925cf1a30Sjl pgcnt_t npgs; 209025cf1a30Sjl struct memlist *mc; 209125cf1a30Sjl static fn_t f = "dr_reserve_mem_spans"; 209225cf1a30Sjl 209325cf1a30Sjl PR_MEM("%s...\n", f); 209425cf1a30Sjl 209525cf1a30Sjl /* 209625cf1a30Sjl * Walk the supplied memlist scheduling each span for removal 209725cf1a30Sjl * with kphysm_del_span. It is possible that a span may intersect 209825cf1a30Sjl * an area occupied by the cage. 209925cf1a30Sjl */ 210025cf1a30Sjl for (mc = ml; mc != NULL; mc = mc->next) { 210125cf1a30Sjl base = _b64top(mc->address); 210225cf1a30Sjl npgs = _b64top(mc->size); 210325cf1a30Sjl 210425cf1a30Sjl err = kphysm_del_span(*mhp, base, npgs); 210525cf1a30Sjl if (err != KPHYSM_OK) { 210625cf1a30Sjl cmn_err(CE_WARN, "%s memory reserve failed." 2107*3103d4ceSjesusm " unexpected kphysm_del_span return value %d;" 2108*3103d4ceSjesusm " basepfn=0x%lx npages=%ld", 2109*3103d4ceSjesusm f, err, base, npgs); 211025cf1a30Sjl 211125cf1a30Sjl return (-1); 211225cf1a30Sjl } 211325cf1a30Sjl } 211425cf1a30Sjl 211525cf1a30Sjl return (0); 211625cf1a30Sjl } 211725cf1a30Sjl 211825cf1a30Sjl #define DR_SMT_NPREF_SETS 6 211925cf1a30Sjl #define DR_SMT_NUNITS_PER_SET MAX_BOARDS * MAX_MEM_UNITS_PER_BOARD 212025cf1a30Sjl 212125cf1a30Sjl /* debug counters */ 212225cf1a30Sjl int dr_smt_realigned; 212325cf1a30Sjl int dr_smt_preference[DR_SMT_NPREF_SETS]; 212425cf1a30Sjl 212525cf1a30Sjl #ifdef DEBUG 212625cf1a30Sjl uint_t dr_ignore_board; /* if bit[bnum-1] set, board won't be candidate */ 212725cf1a30Sjl #endif 212825cf1a30Sjl 212925cf1a30Sjl /* 213025cf1a30Sjl * Find and reserve a copy/rename target board suitable for the 213125cf1a30Sjl * given source board. 213225cf1a30Sjl * All boards in the system are examined and categorized in relation to 213325cf1a30Sjl * their memory size versus the source board's memory size. Order of 213425cf1a30Sjl * preference is: 213525cf1a30Sjl * 1st copy all source, source/target same size 213625cf1a30Sjl * 2nd copy all source, larger target 213725cf1a30Sjl * 3rd copy nonrelocatable source span 213825cf1a30Sjl */ 213925cf1a30Sjl static int 214025cf1a30Sjl dr_select_mem_target(dr_handle_t *hp, 214125cf1a30Sjl dr_mem_unit_t *s_mp, struct memlist *s_ml) 214225cf1a30Sjl { 214325cf1a30Sjl dr_target_pref_t preference; /* lower value is higher preference */ 214425cf1a30Sjl int idx; 214525cf1a30Sjl dr_mem_unit_t **sets; 214625cf1a30Sjl 214725cf1a30Sjl int t_bd; 214825cf1a30Sjl int t_unit; 214925cf1a30Sjl int rv; 215025cf1a30Sjl dr_board_t *s_bp, *t_bp; 215125cf1a30Sjl dr_mem_unit_t *t_mp, *c_mp; 215225cf1a30Sjl struct memlist *d_ml, *t_ml, *ml, *b_ml, *x_ml = NULL; 215325cf1a30Sjl memquery_t s_mq = {0}; 215425cf1a30Sjl static fn_t f = "dr_select_mem_target"; 215525cf1a30Sjl 215625cf1a30Sjl PR_MEM("%s...\n", f); 215725cf1a30Sjl 215825cf1a30Sjl ASSERT(s_ml != NULL); 215925cf1a30Sjl 216025cf1a30Sjl sets = GETSTRUCT(dr_mem_unit_t *, DR_SMT_NUNITS_PER_SET * 216125cf1a30Sjl DR_SMT_NPREF_SETS); 216225cf1a30Sjl 216325cf1a30Sjl s_bp = hp->h_bd; 216425cf1a30Sjl /* calculate the offset into the slice of the last source board pfn */ 216525cf1a30Sjl ASSERT(s_mp->sbm_npages != 0); 216625cf1a30Sjl 216725cf1a30Sjl /* 216825cf1a30Sjl * Find non-relocatable span on source board. 216925cf1a30Sjl */ 217025cf1a30Sjl rv = kphysm_del_span_query(s_mp->sbm_basepfn, s_mp->sbm_npages, &s_mq); 217125cf1a30Sjl if (rv != KPHYSM_OK) { 217225cf1a30Sjl PR_MEM("%s: %s: unexpected kphysm_del_span_query" 217325cf1a30Sjl " return value %d; basepfn 0x%lx, npages %ld\n", 217425cf1a30Sjl f, s_mp->sbm_cm.sbdev_path, rv, s_mp->sbm_basepfn, 217525cf1a30Sjl s_mp->sbm_npages); 217625cf1a30Sjl return (-1); 217725cf1a30Sjl } 217825cf1a30Sjl 217925cf1a30Sjl ASSERT(s_mq.phys_pages != 0); 218025cf1a30Sjl ASSERT(s_mq.nonrelocatable != 0); 218125cf1a30Sjl 218225cf1a30Sjl PR_MEM("%s: %s: nonrelocatable span (0x%lx..0x%lx)\n", f, 218325cf1a30Sjl s_mp->sbm_cm.sbdev_path, s_mq.first_nonrelocatable, 218425cf1a30Sjl s_mq.last_nonrelocatable); 218525cf1a30Sjl 218625cf1a30Sjl /* break down s_ml if it contains dynamic segments */ 218725cf1a30Sjl b_ml = memlist_dup(s_ml); 218825cf1a30Sjl 218925cf1a30Sjl for (ml = s_mp->sbm_dyn_segs; ml; ml = ml->next) { 219025cf1a30Sjl b_ml = memlist_del_span(b_ml, ml->address, ml->size); 219125cf1a30Sjl b_ml = memlist_cat_span(b_ml, ml->address, ml->size); 219225cf1a30Sjl } 219325cf1a30Sjl 219425cf1a30Sjl 219525cf1a30Sjl /* 219625cf1a30Sjl * Make one pass through all memory units on all boards 219725cf1a30Sjl * and categorize them with respect to the source board. 219825cf1a30Sjl */ 219925cf1a30Sjl for (t_bd = 0; t_bd < MAX_BOARDS; t_bd++) { 220025cf1a30Sjl /* 220125cf1a30Sjl * The board structs are a contiguous array 220225cf1a30Sjl * so we take advantage of that to find the 220325cf1a30Sjl * correct board struct pointer for a given 220425cf1a30Sjl * board number. 220525cf1a30Sjl */ 220625cf1a30Sjl t_bp = dr_lookup_board(t_bd); 220725cf1a30Sjl 220825cf1a30Sjl /* source board can not be its own target */ 220925cf1a30Sjl if (s_bp->b_num == t_bp->b_num) 221025cf1a30Sjl continue; 221125cf1a30Sjl 221225cf1a30Sjl for (t_unit = 0; t_unit < MAX_MEM_UNITS_PER_BOARD; t_unit++) { 221325cf1a30Sjl 221425cf1a30Sjl t_mp = dr_get_mem_unit(t_bp, t_unit); 221525cf1a30Sjl 221625cf1a30Sjl /* this memory node must be attached */ 221725cf1a30Sjl if (!DR_DEV_IS_ATTACHED(&t_mp->sbm_cm)) 221825cf1a30Sjl continue; 221925cf1a30Sjl 222025cf1a30Sjl /* source unit can not be its own target */ 222125cf1a30Sjl if (s_mp == t_mp) { 222225cf1a30Sjl /* catch this is debug kernels */ 222325cf1a30Sjl ASSERT(0); 222425cf1a30Sjl continue; 222525cf1a30Sjl } 222625cf1a30Sjl 222725cf1a30Sjl /* 222825cf1a30Sjl * this memory node must not already be reserved 222925cf1a30Sjl * by some other memory delete operation. 223025cf1a30Sjl */ 223125cf1a30Sjl if (t_mp->sbm_flags & DR_MFLAG_RESERVED) 223225cf1a30Sjl continue; 223325cf1a30Sjl 223425cf1a30Sjl /* get target board memlist */ 223525cf1a30Sjl t_ml = dr_get_memlist(t_mp); 223625cf1a30Sjl if (t_ml == NULL) { 223725cf1a30Sjl cmn_err(CE_WARN, "%s: no memlist for" 223825cf1a30Sjl " mem-unit %d, board %d", f, 223925cf1a30Sjl t_mp->sbm_cm.sbdev_bp->b_num, 224025cf1a30Sjl t_mp->sbm_cm.sbdev_unum); 224125cf1a30Sjl continue; 224225cf1a30Sjl } 224325cf1a30Sjl 224425cf1a30Sjl preference = dr_get_target_preference(hp, t_mp, s_mp, 224525cf1a30Sjl t_ml, s_ml, b_ml); 224625cf1a30Sjl 2247*3103d4ceSjesusm memlist_delete(t_ml); 2248*3103d4ceSjesusm 224925cf1a30Sjl if (preference == DR_TP_INVALID) 225025cf1a30Sjl continue; 225125cf1a30Sjl 225225cf1a30Sjl dr_smt_preference[preference]++; 225325cf1a30Sjl 225425cf1a30Sjl /* calculate index to start of preference set */ 225525cf1a30Sjl idx = DR_SMT_NUNITS_PER_SET * preference; 225625cf1a30Sjl /* calculate offset to respective element */ 225725cf1a30Sjl idx += t_bd * MAX_MEM_UNITS_PER_BOARD + t_unit; 225825cf1a30Sjl 225925cf1a30Sjl ASSERT(idx < DR_SMT_NUNITS_PER_SET * DR_SMT_NPREF_SETS); 226025cf1a30Sjl sets[idx] = t_mp; 226125cf1a30Sjl } 226225cf1a30Sjl } 226325cf1a30Sjl 226425cf1a30Sjl if (b_ml != NULL) 226525cf1a30Sjl memlist_delete(b_ml); 226625cf1a30Sjl 226725cf1a30Sjl /* 226825cf1a30Sjl * NOTE: this would be a good place to sort each candidate 226925cf1a30Sjl * set in to some desired order, e.g. memory size in ascending 227025cf1a30Sjl * order. Without an additional sorting step here, the order 227125cf1a30Sjl * within a set is ascending board number order. 227225cf1a30Sjl */ 227325cf1a30Sjl 227425cf1a30Sjl c_mp = NULL; 227525cf1a30Sjl x_ml = NULL; 227625cf1a30Sjl t_ml = NULL; 227725cf1a30Sjl for (idx = 0; idx < DR_SMT_NUNITS_PER_SET * DR_SMT_NPREF_SETS; idx++) { 227825cf1a30Sjl memquery_t mq; 227925cf1a30Sjl 228025cf1a30Sjl preference = (dr_target_pref_t)(idx / DR_SMT_NUNITS_PER_SET); 228125cf1a30Sjl 228225cf1a30Sjl ASSERT(preference != DR_TP_INVALID); 228325cf1a30Sjl 228425cf1a30Sjl /* cleanup t_ml after previous pass */ 228525cf1a30Sjl if (t_ml != NULL) { 228625cf1a30Sjl memlist_delete(t_ml); 228725cf1a30Sjl t_ml = NULL; 228825cf1a30Sjl } 228925cf1a30Sjl 229025cf1a30Sjl /* get candidate target board mem unit */ 229125cf1a30Sjl t_mp = sets[idx]; 229225cf1a30Sjl if (t_mp == NULL) 229325cf1a30Sjl continue; 229425cf1a30Sjl 229525cf1a30Sjl /* get target board memlist */ 229625cf1a30Sjl t_ml = dr_get_memlist(t_mp); 229725cf1a30Sjl if (t_ml == NULL) { 229825cf1a30Sjl cmn_err(CE_WARN, "%s: no memlist for" 2299*3103d4ceSjesusm " mem-unit %d, board %d", 2300*3103d4ceSjesusm f, 2301*3103d4ceSjesusm t_mp->sbm_cm.sbdev_bp->b_num, 2302*3103d4ceSjesusm t_mp->sbm_cm.sbdev_unum); 230325cf1a30Sjl 230425cf1a30Sjl continue; 230525cf1a30Sjl } 230625cf1a30Sjl 230725cf1a30Sjl PR_MEM("%s: checking for no-reloc in %s, " 2308*3103d4ceSjesusm " basepfn=0x%lx, npages=%ld\n", 2309*3103d4ceSjesusm f, 2310*3103d4ceSjesusm t_mp->sbm_cm.sbdev_path, 2311*3103d4ceSjesusm t_mp->sbm_basepfn, 2312*3103d4ceSjesusm t_mp->sbm_npages); 231325cf1a30Sjl 231425cf1a30Sjl rv = dr_del_mlist_query(t_ml, &mq); 231525cf1a30Sjl if (rv != KPHYSM_OK) { 231625cf1a30Sjl PR_MEM("%s: kphysm_del_span_query:" 2317*3103d4ceSjesusm " unexpected return value %d\n", f, rv); 231825cf1a30Sjl 231925cf1a30Sjl continue; 232025cf1a30Sjl } 232125cf1a30Sjl 232225cf1a30Sjl if (mq.nonrelocatable != 0) { 232325cf1a30Sjl PR_MEM("%s: candidate %s has" 2324*3103d4ceSjesusm " nonrelocatable span [0x%lx..0x%lx]\n", 2325*3103d4ceSjesusm f, 2326*3103d4ceSjesusm t_mp->sbm_cm.sbdev_path, 2327*3103d4ceSjesusm mq.first_nonrelocatable, 2328*3103d4ceSjesusm mq.last_nonrelocatable); 232925cf1a30Sjl 233025cf1a30Sjl continue; 233125cf1a30Sjl } 233225cf1a30Sjl 233325cf1a30Sjl #ifdef DEBUG 233425cf1a30Sjl /* 233525cf1a30Sjl * This is a debug tool for excluding certain boards 233625cf1a30Sjl * from being selected as a target board candidate. 233725cf1a30Sjl * dr_ignore_board is only tested by this driver. 233825cf1a30Sjl * It must be set with adb, obp, /etc/system or your 233925cf1a30Sjl * favorite debugger. 234025cf1a30Sjl */ 234125cf1a30Sjl if (dr_ignore_board & 2342*3103d4ceSjesusm (1 << (t_mp->sbm_cm.sbdev_bp->b_num - 1))) { 234325cf1a30Sjl PR_MEM("%s: dr_ignore_board flag set," 2344*3103d4ceSjesusm " ignoring %s as candidate\n", 2345*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 234625cf1a30Sjl continue; 234725cf1a30Sjl } 234825cf1a30Sjl #endif 234925cf1a30Sjl 235025cf1a30Sjl /* 235125cf1a30Sjl * Reserve excess source board memory, if any. 235225cf1a30Sjl * 235325cf1a30Sjl * Only the nonrelocatable source span will be copied 235425cf1a30Sjl * so schedule the rest of the source mem to be deleted. 235525cf1a30Sjl */ 235625cf1a30Sjl switch (preference) { 235725cf1a30Sjl case DR_TP_NONRELOC: 235825cf1a30Sjl /* 235925cf1a30Sjl * Get source copy memlist and use it to construct 236025cf1a30Sjl * delete memlist. 236125cf1a30Sjl */ 236225cf1a30Sjl d_ml = memlist_dup(s_ml); 236325cf1a30Sjl x_ml = dr_get_copy_mlist(s_ml, t_ml, s_mp, t_mp); 236425cf1a30Sjl 236525cf1a30Sjl /* XXX */ 236625cf1a30Sjl ASSERT(d_ml != NULL); 236725cf1a30Sjl ASSERT(x_ml != NULL); 236825cf1a30Sjl 236925cf1a30Sjl for (ml = x_ml; ml != NULL; ml = ml->next) { 237025cf1a30Sjl d_ml = memlist_del_span(d_ml, ml->address, 237125cf1a30Sjl ml->size); 237225cf1a30Sjl } 237325cf1a30Sjl 237425cf1a30Sjl PR_MEM("%s: %s: reserving src brd memlist:\n", f, 237525cf1a30Sjl s_mp->sbm_cm.sbdev_path); 237625cf1a30Sjl PR_MEMLIST_DUMP(d_ml); 237725cf1a30Sjl 237825cf1a30Sjl /* reserve excess spans */ 237925cf1a30Sjl if (dr_reserve_mem_spans(&s_mp->sbm_memhandle, 238025cf1a30Sjl d_ml) != 0) { 238125cf1a30Sjl /* likely more non-reloc pages appeared */ 238225cf1a30Sjl /* TODO: restart from top? */ 238325cf1a30Sjl continue; 238425cf1a30Sjl } 238525cf1a30Sjl break; 238625cf1a30Sjl default: 238725cf1a30Sjl d_ml = NULL; 238825cf1a30Sjl break; 238925cf1a30Sjl } 239025cf1a30Sjl 239125cf1a30Sjl s_mp->sbm_flags |= DR_MFLAG_RESERVED; 239225cf1a30Sjl 239325cf1a30Sjl /* 239425cf1a30Sjl * reserve all memory on target board. 239525cf1a30Sjl * NOTE: source board's memhandle is used. 239625cf1a30Sjl * 239725cf1a30Sjl * If this succeeds (eq 0), then target selection is 239825cf1a30Sjl * complete and all unwanted memory spans, both source and 239925cf1a30Sjl * target, have been reserved. Loop is terminated. 240025cf1a30Sjl */ 240125cf1a30Sjl if (dr_reserve_mem_spans(&s_mp->sbm_memhandle, t_ml) == 0) { 240225cf1a30Sjl PR_MEM("%s: %s: target board memory reserved\n", 2403*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 240425cf1a30Sjl 240525cf1a30Sjl /* a candidate target board is now reserved */ 240625cf1a30Sjl t_mp->sbm_flags |= DR_MFLAG_RESERVED; 240725cf1a30Sjl c_mp = t_mp; 240825cf1a30Sjl 240925cf1a30Sjl /* *** EXITING LOOP *** */ 241025cf1a30Sjl break; 241125cf1a30Sjl } 241225cf1a30Sjl 241325cf1a30Sjl /* did not successfully reserve the target board. */ 241425cf1a30Sjl PR_MEM("%s: could not reserve target %s\n", 2415*3103d4ceSjesusm f, t_mp->sbm_cm.sbdev_path); 241625cf1a30Sjl 241725cf1a30Sjl /* 241825cf1a30Sjl * NOTE: an undo of the dr_reserve_mem_span work 241925cf1a30Sjl * will happen automatically when the memhandle 242025cf1a30Sjl * (s_mp->sbm_memhandle) is kphysm_del_release'd. 242125cf1a30Sjl */ 242225cf1a30Sjl 242325cf1a30Sjl s_mp->sbm_flags &= ~DR_MFLAG_RESERVED; 242425cf1a30Sjl } 242525cf1a30Sjl 242625cf1a30Sjl /* clean up after memlist editing logic */ 242725cf1a30Sjl if (x_ml != NULL) 242825cf1a30Sjl memlist_delete(x_ml); 242925cf1a30Sjl 243025cf1a30Sjl FREESTRUCT(sets, dr_mem_unit_t *, DR_SMT_NUNITS_PER_SET * 243125cf1a30Sjl DR_SMT_NPREF_SETS); 243225cf1a30Sjl 243325cf1a30Sjl /* 243425cf1a30Sjl * c_mp will be NULL when the entire sets[] array 243525cf1a30Sjl * has been searched without reserving a target board. 243625cf1a30Sjl */ 243725cf1a30Sjl if (c_mp == NULL) { 243825cf1a30Sjl PR_MEM("%s: %s: target selection failed.\n", 2439*3103d4ceSjesusm f, s_mp->sbm_cm.sbdev_path); 244025cf1a30Sjl 244125cf1a30Sjl if (t_ml != NULL) 244225cf1a30Sjl memlist_delete(t_ml); 244325cf1a30Sjl 244425cf1a30Sjl return (-1); 244525cf1a30Sjl } 244625cf1a30Sjl 244725cf1a30Sjl PR_MEM("%s: found target %s for source %s\n", 2448*3103d4ceSjesusm f, 2449*3103d4ceSjesusm c_mp->sbm_cm.sbdev_path, 2450*3103d4ceSjesusm s_mp->sbm_cm.sbdev_path); 245125cf1a30Sjl 245225cf1a30Sjl s_mp->sbm_peer = c_mp; 245325cf1a30Sjl s_mp->sbm_flags |= DR_MFLAG_SOURCE; 245425cf1a30Sjl s_mp->sbm_del_mlist = d_ml; /* spans to be deleted, if any */ 245525cf1a30Sjl s_mp->sbm_mlist = s_ml; 245625cf1a30Sjl s_mp->sbm_cm.sbdev_busy = 1; 245725cf1a30Sjl 245825cf1a30Sjl c_mp->sbm_peer = s_mp; 245925cf1a30Sjl c_mp->sbm_flags |= DR_MFLAG_TARGET; 246025cf1a30Sjl c_mp->sbm_del_mlist = t_ml; /* spans to be deleted */ 246125cf1a30Sjl c_mp->sbm_mlist = t_ml; 246225cf1a30Sjl c_mp->sbm_cm.sbdev_busy = 1; 246325cf1a30Sjl 246425cf1a30Sjl return (0); 246525cf1a30Sjl } 246625cf1a30Sjl 246725cf1a30Sjl /* 246825cf1a30Sjl * Returns target preference rank: 246925cf1a30Sjl * -1 not a valid copy-rename target board 247025cf1a30Sjl * 0 copy all source, source/target same size 247125cf1a30Sjl * 1 copy all source, larger target 247225cf1a30Sjl * 2 copy nonrelocatable source span 247325cf1a30Sjl */ 247425cf1a30Sjl static dr_target_pref_t 247525cf1a30Sjl dr_get_target_preference(dr_handle_t *hp, 247625cf1a30Sjl dr_mem_unit_t *t_mp, dr_mem_unit_t *s_mp, 247725cf1a30Sjl struct memlist *t_ml, struct memlist *s_ml, 247825cf1a30Sjl struct memlist *b_ml) 247925cf1a30Sjl { 248025cf1a30Sjl dr_target_pref_t preference; 248125cf1a30Sjl struct memlist *s_nonreloc_ml = NULL; 248225cf1a30Sjl drmachid_t t_id; 248325cf1a30Sjl static fn_t f = "dr_get_target_preference"; 248425cf1a30Sjl 248525cf1a30Sjl t_id = t_mp->sbm_cm.sbdev_bp->b_id; 248625cf1a30Sjl 248725cf1a30Sjl /* 248825cf1a30Sjl * Can the entire source board be copied? 248925cf1a30Sjl */ 249025cf1a30Sjl if (dr_memlist_canfit(s_ml, t_ml, s_mp, t_mp)) { 249125cf1a30Sjl if (s_mp->sbm_npages == t_mp->sbm_npages) 249225cf1a30Sjl preference = DR_TP_SAME; /* same size */ 249325cf1a30Sjl else 249425cf1a30Sjl preference = DR_TP_LARGE; /* larger target */ 249525cf1a30Sjl } else { 249625cf1a30Sjl /* 249725cf1a30Sjl * Entire source won't fit so try non-relocatable memory only 249825cf1a30Sjl * (target aligned). 249925cf1a30Sjl */ 250025cf1a30Sjl s_nonreloc_ml = dr_get_nonreloc_mlist(b_ml, s_mp); 250125cf1a30Sjl if (s_nonreloc_ml == NULL) { 250225cf1a30Sjl PR_MEM("%s: dr_get_nonreloc_mlist failed\n", f); 250325cf1a30Sjl preference = DR_TP_INVALID; 250425cf1a30Sjl } 250525cf1a30Sjl if (dr_memlist_canfit(s_nonreloc_ml, t_ml, s_mp, t_mp)) 250625cf1a30Sjl preference = DR_TP_NONRELOC; 250725cf1a30Sjl else 250825cf1a30Sjl preference = DR_TP_INVALID; 250925cf1a30Sjl } 251025cf1a30Sjl 251125cf1a30Sjl if (s_nonreloc_ml != NULL) 251225cf1a30Sjl memlist_delete(s_nonreloc_ml); 251325cf1a30Sjl 251425cf1a30Sjl /* 251525cf1a30Sjl * Force floating board preference lower than all other boards 251625cf1a30Sjl * if the force flag is present; otherwise disallow the board. 251725cf1a30Sjl */ 251825cf1a30Sjl if ((preference != DR_TP_INVALID) && drmach_board_is_floating(t_id)) { 251925cf1a30Sjl if (dr_cmd_flags(hp) & SBD_FLAG_FORCE) 252025cf1a30Sjl preference += DR_TP_FLOATING; 252125cf1a30Sjl else 252225cf1a30Sjl preference = DR_TP_INVALID; 252325cf1a30Sjl } 252425cf1a30Sjl 252525cf1a30Sjl PR_MEM("%s: %s preference=%d\n", f, t_mp->sbm_cm.sbdev_path, 252625cf1a30Sjl preference); 252725cf1a30Sjl 252825cf1a30Sjl return (preference); 252925cf1a30Sjl } 253025cf1a30Sjl 253125cf1a30Sjl /* 253225cf1a30Sjl * Create a memlist representing the source memory that will be copied to 253325cf1a30Sjl * the target board. The memory to be copied is the maximum amount that 253425cf1a30Sjl * will fit on the target board. 253525cf1a30Sjl */ 253625cf1a30Sjl static struct memlist * 253725cf1a30Sjl dr_get_copy_mlist(struct memlist *s_mlist, struct memlist *t_mlist, 253825cf1a30Sjl dr_mem_unit_t *s_mp, dr_mem_unit_t *t_mp) 253925cf1a30Sjl { 254025cf1a30Sjl struct memlist *t_ml, *s_copy_ml, *s_del_ml, *ml, *x_ml; 254125cf1a30Sjl uint64_t s_slice_mask, s_slice_base; 254225cf1a30Sjl uint64_t t_slice_mask, t_slice_base; 254325cf1a30Sjl static fn_t f = "dr_get_copy_mlist"; 254425cf1a30Sjl 254525cf1a30Sjl ASSERT(s_mlist != NULL); 254625cf1a30Sjl ASSERT(t_mlist != NULL); 254725cf1a30Sjl ASSERT(t_mp->sbm_slice_size == s_mp->sbm_slice_size); 254825cf1a30Sjl 254925cf1a30Sjl s_slice_mask = s_mp->sbm_slice_size - 1; 255025cf1a30Sjl s_slice_base = s_mlist->address & ~s_slice_mask; 255125cf1a30Sjl 255225cf1a30Sjl t_slice_mask = t_mp->sbm_slice_size - 1; 255325cf1a30Sjl t_slice_base = t_mlist->address & ~t_slice_mask; 255425cf1a30Sjl 255525cf1a30Sjl t_ml = memlist_dup(t_mlist); 255625cf1a30Sjl s_del_ml = memlist_dup(s_mlist); 255725cf1a30Sjl s_copy_ml = memlist_dup(s_mlist); 255825cf1a30Sjl 255925cf1a30Sjl /* XXX */ 256025cf1a30Sjl ASSERT(t_ml != NULL); 256125cf1a30Sjl ASSERT(s_del_ml != NULL); 256225cf1a30Sjl ASSERT(s_copy_ml != NULL); 256325cf1a30Sjl 256425cf1a30Sjl /* 256525cf1a30Sjl * To construct the source copy memlist: 256625cf1a30Sjl * 256725cf1a30Sjl * The target memlist is converted to the post-rename 256825cf1a30Sjl * source addresses. This is the physical address range 256925cf1a30Sjl * the target will have after the copy-rename. Overlaying 257025cf1a30Sjl * and deleting this from the current source memlist will 257125cf1a30Sjl * give the source delete memlist. The copy memlist is 257225cf1a30Sjl * the reciprocal of the source delete memlist. 257325cf1a30Sjl */ 257425cf1a30Sjl for (ml = t_ml; ml != NULL; ml = ml->next) { 257525cf1a30Sjl /* 257625cf1a30Sjl * Normalize relative to target slice base PA 257725cf1a30Sjl * in order to preseve slice offsets. 257825cf1a30Sjl */ 257925cf1a30Sjl ml->address -= t_slice_base; 258025cf1a30Sjl /* 258125cf1a30Sjl * Convert to source slice PA address. 258225cf1a30Sjl */ 258325cf1a30Sjl ml->address += s_slice_base; 258425cf1a30Sjl } 258525cf1a30Sjl 258625cf1a30Sjl for (ml = t_ml; ml != NULL; ml = ml->next) { 258725cf1a30Sjl s_del_ml = memlist_del_span(s_del_ml, ml->address, ml->size); 258825cf1a30Sjl } 258925cf1a30Sjl 259025cf1a30Sjl /* 259125cf1a30Sjl * Expand the delete mlist to fully include any dynamic segments 259225cf1a30Sjl * it intersects with. 259325cf1a30Sjl */ 259425cf1a30Sjl for (x_ml = NULL, ml = s_del_ml; ml != NULL; ml = ml->next) { 259525cf1a30Sjl uint64_t del_base = ml->address; 259625cf1a30Sjl uint64_t del_end = ml->address + ml->size; 259725cf1a30Sjl struct memlist *dyn; 259825cf1a30Sjl 259925cf1a30Sjl for (dyn = s_mp->sbm_dyn_segs; dyn != NULL; dyn = dyn->next) { 260025cf1a30Sjl uint64_t dyn_base = dyn->address; 260125cf1a30Sjl uint64_t dyn_end = dyn->address + dyn->size; 260225cf1a30Sjl 260325cf1a30Sjl if (del_base > dyn_base && del_base < dyn_end) 260425cf1a30Sjl del_base = dyn_base; 260525cf1a30Sjl 260625cf1a30Sjl if (del_end > dyn_base && del_end < dyn_end) 260725cf1a30Sjl del_end = dyn_end; 260825cf1a30Sjl } 260925cf1a30Sjl 261025cf1a30Sjl x_ml = memlist_cat_span(x_ml, del_base, del_end - del_base); 261125cf1a30Sjl } 261225cf1a30Sjl 261325cf1a30Sjl memlist_delete(s_del_ml); 261425cf1a30Sjl s_del_ml = x_ml; 261525cf1a30Sjl 261625cf1a30Sjl for (ml = s_del_ml; ml != NULL; ml = ml->next) { 261725cf1a30Sjl s_copy_ml = memlist_del_span(s_copy_ml, ml->address, ml->size); 261825cf1a30Sjl } 261925cf1a30Sjl 262025cf1a30Sjl PR_MEM("%s: source delete mlist\n", f); 262125cf1a30Sjl PR_MEMLIST_DUMP(s_del_ml); 262225cf1a30Sjl 262325cf1a30Sjl PR_MEM("%s: source copy mlist\n", f); 262425cf1a30Sjl PR_MEMLIST_DUMP(s_copy_ml); 262525cf1a30Sjl 262625cf1a30Sjl memlist_delete(t_ml); 262725cf1a30Sjl memlist_delete(s_del_ml); 262825cf1a30Sjl 262925cf1a30Sjl return (s_copy_ml); 263025cf1a30Sjl } 263125cf1a30Sjl 263225cf1a30Sjl /* 263325cf1a30Sjl * Scan the non-relocatable spans on the source memory 263425cf1a30Sjl * and construct a minimum mlist that includes all non-reloc 263525cf1a30Sjl * memory subject to target alignment, and dynamic segment 263625cf1a30Sjl * constraints where only whole dynamic segments may be deleted. 263725cf1a30Sjl */ 263825cf1a30Sjl static struct memlist * 263925cf1a30Sjl dr_get_nonreloc_mlist(struct memlist *s_ml, dr_mem_unit_t *s_mp) 264025cf1a30Sjl { 264125cf1a30Sjl struct memlist *x_ml = NULL; 264225cf1a30Sjl struct memlist *ml; 264325cf1a30Sjl static fn_t f = "dr_get_nonreloc_mlist"; 264425cf1a30Sjl 264525cf1a30Sjl PR_MEM("%s: checking for split of dyn seg list:\n", f); 264625cf1a30Sjl PR_MEMLIST_DUMP(s_mp->sbm_dyn_segs); 264725cf1a30Sjl 264825cf1a30Sjl for (ml = s_ml; ml; ml = ml->next) { 264925cf1a30Sjl int rv; 265025cf1a30Sjl uint64_t nr_base, nr_end; 265125cf1a30Sjl memquery_t mq; 265225cf1a30Sjl struct memlist *dyn; 265325cf1a30Sjl 265425cf1a30Sjl rv = kphysm_del_span_query( 2655*3103d4ceSjesusm _b64top(ml->address), _b64top(ml->size), &mq); 265625cf1a30Sjl if (rv) { 265725cf1a30Sjl memlist_delete(x_ml); 265825cf1a30Sjl return (NULL); 265925cf1a30Sjl } 266025cf1a30Sjl 266125cf1a30Sjl if (mq.nonrelocatable == 0) 266225cf1a30Sjl continue; 266325cf1a30Sjl 266425cf1a30Sjl PR_MEM("%s: non-reloc span: 0x%lx, 0x%lx (%lx, %lx)\n", f, 2665*3103d4ceSjesusm _ptob64(mq.first_nonrelocatable), 2666*3103d4ceSjesusm _ptob64(mq.last_nonrelocatable), 2667*3103d4ceSjesusm mq.first_nonrelocatable, 2668*3103d4ceSjesusm mq.last_nonrelocatable); 266925cf1a30Sjl 267025cf1a30Sjl /* 267125cf1a30Sjl * Align the span at both ends to allow for possible 267225cf1a30Sjl * cage expansion. 267325cf1a30Sjl */ 267425cf1a30Sjl nr_base = _ptob64(mq.first_nonrelocatable); 267525cf1a30Sjl nr_end = _ptob64(mq.last_nonrelocatable + 1); 267625cf1a30Sjl 267725cf1a30Sjl PR_MEM("%s: adjusted non-reloc span: 0x%lx, 0x%lx\n", 2678*3103d4ceSjesusm f, nr_base, nr_end); 267925cf1a30Sjl 268025cf1a30Sjl /* 268125cf1a30Sjl * Expand the non-reloc span to fully include any 268225cf1a30Sjl * dynamic segments it intersects with. 268325cf1a30Sjl */ 268425cf1a30Sjl for (dyn = s_mp->sbm_dyn_segs; dyn != NULL; dyn = dyn->next) { 268525cf1a30Sjl uint64_t dyn_base = dyn->address; 268625cf1a30Sjl uint64_t dyn_end = dyn->address + dyn->size; 268725cf1a30Sjl 268825cf1a30Sjl if (nr_base > dyn_base && nr_base < dyn_end) 268925cf1a30Sjl nr_base = dyn_base; 269025cf1a30Sjl 269125cf1a30Sjl if (nr_end > dyn_base && nr_end < dyn_end) 269225cf1a30Sjl nr_end = dyn_end; 269325cf1a30Sjl } 269425cf1a30Sjl 269525cf1a30Sjl x_ml = memlist_cat_span(x_ml, nr_base, nr_end - nr_base); 269625cf1a30Sjl } 269725cf1a30Sjl 269825cf1a30Sjl if (x_ml == NULL) { 269925cf1a30Sjl PR_MEM("%s: source didn't have any non-reloc pages!\n", f); 270025cf1a30Sjl return (NULL); 270125cf1a30Sjl } 270225cf1a30Sjl 270325cf1a30Sjl PR_MEM("%s: %s: edited source memlist:\n", f, s_mp->sbm_cm.sbdev_path); 270425cf1a30Sjl PR_MEMLIST_DUMP(x_ml); 270525cf1a30Sjl 270625cf1a30Sjl return (x_ml); 270725cf1a30Sjl } 270825cf1a30Sjl 270925cf1a30Sjl /* 271025cf1a30Sjl * Check if source memlist can fit in target memlist while maintaining 271125cf1a30Sjl * relative offsets within board. 271225cf1a30Sjl */ 271325cf1a30Sjl static int 271425cf1a30Sjl dr_memlist_canfit(struct memlist *s_mlist, struct memlist *t_mlist, 271525cf1a30Sjl dr_mem_unit_t *s_mp, dr_mem_unit_t *t_mp) 271625cf1a30Sjl { 271725cf1a30Sjl int canfit = 0; 271825cf1a30Sjl struct memlist *s_ml, *t_ml, *ml; 271925cf1a30Sjl uint64_t s_slice_mask, t_slice_mask; 272025cf1a30Sjl static fn_t f = "dr_mlist_canfit"; 272125cf1a30Sjl 272225cf1a30Sjl s_ml = memlist_dup(s_mlist); 272325cf1a30Sjl t_ml = memlist_dup(t_mlist); 272425cf1a30Sjl 272525cf1a30Sjl if (s_ml == NULL || t_ml == NULL) { 272625cf1a30Sjl cmn_err(CE_WARN, "%s: memlist_dup failed\n", f); 272725cf1a30Sjl goto done; 272825cf1a30Sjl } 272925cf1a30Sjl 273025cf1a30Sjl s_slice_mask = s_mp->sbm_slice_size - 1; 273125cf1a30Sjl t_slice_mask = t_mp->sbm_slice_size - 1; 273225cf1a30Sjl 273325cf1a30Sjl /* 273425cf1a30Sjl * Normalize to slice relative offsets. 273525cf1a30Sjl */ 273625cf1a30Sjl for (ml = s_ml; ml; ml = ml->next) 273725cf1a30Sjl ml->address &= s_slice_mask; 273825cf1a30Sjl 273925cf1a30Sjl for (ml = t_ml; ml; ml = ml->next) 274025cf1a30Sjl ml->address &= t_slice_mask; 274125cf1a30Sjl 274225cf1a30Sjl canfit = memlist_canfit(s_ml, t_ml); 274325cf1a30Sjl done: 274425cf1a30Sjl memlist_delete(s_ml); 274525cf1a30Sjl memlist_delete(t_ml); 274625cf1a30Sjl 274725cf1a30Sjl return (canfit); 274825cf1a30Sjl } 274925cf1a30Sjl 275025cf1a30Sjl /* 275125cf1a30Sjl * Memlist support. 275225cf1a30Sjl */ 275325cf1a30Sjl 275425cf1a30Sjl /* 275525cf1a30Sjl * Determine whether the source memlist (s_mlist) will 275625cf1a30Sjl * fit into the target memlist (t_mlist) in terms of 275725cf1a30Sjl * size and holes. Assumes the caller has normalized the 275825cf1a30Sjl * memlist physical addresses for comparison. 275925cf1a30Sjl */ 276025cf1a30Sjl static int 276125cf1a30Sjl memlist_canfit(struct memlist *s_mlist, struct memlist *t_mlist) 276225cf1a30Sjl { 276325cf1a30Sjl int rv = 0; 276425cf1a30Sjl struct memlist *s_ml, *t_ml; 276525cf1a30Sjl 276625cf1a30Sjl if ((s_mlist == NULL) || (t_mlist == NULL)) 276725cf1a30Sjl return (0); 276825cf1a30Sjl 276925cf1a30Sjl s_ml = s_mlist; 277025cf1a30Sjl for (t_ml = t_mlist; t_ml && s_ml; t_ml = t_ml->next) { 277125cf1a30Sjl uint64_t s_start, s_end; 277225cf1a30Sjl uint64_t t_start, t_end; 277325cf1a30Sjl 277425cf1a30Sjl t_start = t_ml->address; 277525cf1a30Sjl t_end = t_start + t_ml->size; 277625cf1a30Sjl 277725cf1a30Sjl for (; s_ml; s_ml = s_ml->next) { 277825cf1a30Sjl s_start = s_ml->address; 277925cf1a30Sjl s_end = s_start + s_ml->size; 278025cf1a30Sjl 278125cf1a30Sjl if ((s_start < t_start) || (s_end > t_end)) 278225cf1a30Sjl break; 278325cf1a30Sjl } 278425cf1a30Sjl } 278525cf1a30Sjl 278625cf1a30Sjl /* 278725cf1a30Sjl * If we ran out of source memlist chunks that mean 278825cf1a30Sjl * we found a home for all of them. 278925cf1a30Sjl */ 279025cf1a30Sjl if (s_ml == NULL) 279125cf1a30Sjl rv = 1; 279225cf1a30Sjl 279325cf1a30Sjl return (rv); 279425cf1a30Sjl } 2795