xref: /onnv-gate/usr/src/psm/stand/cpr/sparcv9/sun4u/pages.c (revision 3446:5903aece022d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3446Smrj  * Common Development and Distribution License (the "License").
6*3446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/cpr.h>
300Sstevel@tonic-gate #include <sys/ddi.h>
310Sstevel@tonic-gate #include "cprboot.h"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * check if any cpd_t pages clash with the statefile buffer and shuffle
360Sstevel@tonic-gate  * buf pages to free space; since kpages are saved in ascending order,
370Sstevel@tonic-gate  * any buf pages preceding the current statefile buffer offset can be
380Sstevel@tonic-gate  * written because those pages have already been read and restored
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate static void
shuffle_pages(cpd_t * descp)410Sstevel@tonic-gate shuffle_pages(cpd_t *descp)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	pfn_t low_src_ppn, dst_ppn, tail_ppn, new_ppn;
440Sstevel@tonic-gate 	size_t dst_off;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	/*
470Sstevel@tonic-gate 	 * set the lowest source buf ppn for the (precede) comparison
480Sstevel@tonic-gate 	 * below; the ORIG macro is used for the case where the src buf
490Sstevel@tonic-gate 	 * page had already been moved - and would confuse the compare
500Sstevel@tonic-gate 	 */
510Sstevel@tonic-gate 	low_src_ppn = SF_ORIG_PPN(sfile.buf_offset);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	tail_ppn = descp->cpd_pfn + descp->cpd_pages;
540Sstevel@tonic-gate 	for (dst_ppn = descp->cpd_pfn; dst_ppn < tail_ppn; dst_ppn++) {
550Sstevel@tonic-gate 		/*
560Sstevel@tonic-gate 		 * if the dst page is outside the range of statefile
570Sstevel@tonic-gate 		 * buffer phys pages, it's OK to write that page;
580Sstevel@tonic-gate 		 * buf pages may have been moved outside the range,
590Sstevel@tonic-gate 		 * but only to locations isolated from any dst page
600Sstevel@tonic-gate 		 */
610Sstevel@tonic-gate 		if (dst_ppn < sfile.low_ppn || dst_ppn > sfile.high_ppn) {
620Sstevel@tonic-gate 			SF_STAT_INC(outside);
630Sstevel@tonic-gate 			continue;
640Sstevel@tonic-gate 		}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 		/*
670Sstevel@tonic-gate 		 * the dst page is inside the range of buf ppns;
680Sstevel@tonic-gate 		 * dont need to move the buf page if the dst page
690Sstevel@tonic-gate 		 * precedes the lowest src buf page
700Sstevel@tonic-gate 		 */
710Sstevel@tonic-gate 		if (dst_ppn < low_src_ppn) {
720Sstevel@tonic-gate 			SF_STAT_INC(precede);
730Sstevel@tonic-gate 			continue;
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 		/*
770Sstevel@tonic-gate 		 * the dst page clashes with the statefile buffer;
780Sstevel@tonic-gate 		 * move the buf page to a free location and update
790Sstevel@tonic-gate 		 * the buffer map
800Sstevel@tonic-gate 		 */
810Sstevel@tonic-gate 		new_ppn = find_apage();
820Sstevel@tonic-gate 		phys_xcopy(PN_TO_ADDR(dst_ppn), PN_TO_ADDR(new_ppn),
830Sstevel@tonic-gate 		    MMU_PAGESIZE);
840Sstevel@tonic-gate 		dst_off = mmu_ptob(dst_ppn - sfile.low_ppn);
850Sstevel@tonic-gate 		SF_BUF_PPN(dst_off) = new_ppn;
860Sstevel@tonic-gate 		SF_STAT_INC(move);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * map-in source statefile buffer pages (read-only) at CB_SRC_VIRT;
930Sstevel@tonic-gate  * sets the starting source vaddr with correct page offset
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate static void
mapin_buf_pages(size_t datalen,caddr_t * srcp)960Sstevel@tonic-gate mapin_buf_pages(size_t datalen, caddr_t *srcp)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	int dtlb_index, pg_off;
990Sstevel@tonic-gate 	caddr_t vaddr, tail;
1000Sstevel@tonic-gate 	size_t off, bytes;
1010Sstevel@tonic-gate 	pfn_t src_ppn;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	dtlb_index = cb_dents - CB_MAX_KPAGES - 1;
1040Sstevel@tonic-gate 	off = sfile.buf_offset;
1050Sstevel@tonic-gate 	pg_off = off & MMU_PAGEOFFSET;
1060Sstevel@tonic-gate 	bytes = PAGE_ROUNDUP(pg_off + datalen);
1070Sstevel@tonic-gate 	vaddr = (caddr_t)CB_SRC_VIRT;
1080Sstevel@tonic-gate 	*srcp = vaddr + pg_off;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	for (tail = vaddr + bytes; vaddr < tail; vaddr += MMU_PAGESIZE) {
1110Sstevel@tonic-gate 		src_ppn = SF_BUF_PPN(off);
1120Sstevel@tonic-gate 		cb_mapin(vaddr, src_ppn, TTE8K, 0, dtlb_index);
1130Sstevel@tonic-gate 		dtlb_index--;
1140Sstevel@tonic-gate 		off += MMU_PAGESIZE;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * map-in destination kernel pages (read/write) at CB_DST_VIRT
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate static void
mapin_dst_pages(cpd_t * descp)1230Sstevel@tonic-gate mapin_dst_pages(cpd_t *descp)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	int dtlb_index, pages;
1260Sstevel@tonic-gate 	caddr_t vaddr;
1270Sstevel@tonic-gate 	pfn_t dst_ppn;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	dtlb_index = cb_dents - 1;
1300Sstevel@tonic-gate 	vaddr = (caddr_t)CB_DST_VIRT;
1310Sstevel@tonic-gate 	dst_ppn = descp->cpd_pfn;
1320Sstevel@tonic-gate 	for (pages = 0; pages < descp->cpd_pages; pages++) {
1330Sstevel@tonic-gate 		cb_mapin(vaddr, dst_ppn, TTE8K, TTE_HWWR_INT, dtlb_index);
1340Sstevel@tonic-gate 		dtlb_index--;
1350Sstevel@tonic-gate 		vaddr += MMU_PAGESIZE;
1360Sstevel@tonic-gate 		dst_ppn++;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * run a checksum on un/compressed data when flag is set
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static int
kdata_cksum(void * data,cpd_t * descp,uint_t flag)1450Sstevel@tonic-gate kdata_cksum(void *data, cpd_t *descp, uint_t flag)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	uint_t sum, expect;
1480Sstevel@tonic-gate 	size_t len;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if ((descp->cpd_flag & flag) == 0)
1510Sstevel@tonic-gate 		return (0);
1520Sstevel@tonic-gate 	else if (flag == CPD_CSUM) {
1530Sstevel@tonic-gate 		expect = descp->cpd_csum;
1540Sstevel@tonic-gate 		len = descp->cpd_length;
1550Sstevel@tonic-gate 	} else {
1560Sstevel@tonic-gate 		expect = descp->cpd_usum;
1570Sstevel@tonic-gate 		len = mmu_ptob(descp->cpd_pages);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	sum = checksum32(data, len);
1600Sstevel@tonic-gate 	if (sum != expect) {
1610Sstevel@tonic-gate 		prom_printf("\n%scompressed data checksum error, "
1620Sstevel@tonic-gate 		    "expect 0x%x, got 0x%x\n", (flag == CPD_USUM) ? "un" : "",
1630Sstevel@tonic-gate 		    expect, sum);
1640Sstevel@tonic-gate 		return (ERR);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	return (0);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * primary kpage restoration routine
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static int
restore_page_group(cpd_t * descp)1750Sstevel@tonic-gate restore_page_group(cpd_t *descp)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	caddr_t dst, datap;
1780Sstevel@tonic-gate 	size_t size, len;
1790Sstevel@tonic-gate 	caddr_t src;
1800Sstevel@tonic-gate 	int raw;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate #if defined(lint)
1830Sstevel@tonic-gate 	(void) compress(0, 0, 0);
1840Sstevel@tonic-gate #endif
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * move any source buf pages that clash with dst kernel pages;
1880Sstevel@tonic-gate 	 * create tlb entries for the orig/new source buf pages and
1890Sstevel@tonic-gate 	 * the dst kpages
1900Sstevel@tonic-gate 	 */
1910Sstevel@tonic-gate 	shuffle_pages(descp);
1920Sstevel@tonic-gate 	mapin_buf_pages(descp->cpd_length, &src);
1930Sstevel@tonic-gate 	mapin_dst_pages(descp);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * for compressed pages, run a checksum at the src vaddr and
1970Sstevel@tonic-gate 	 * decompress to the mapped-in dst kpages; for uncompressed pages,
1980Sstevel@tonic-gate 	 * just copy direct; uncompressed checksums are used for either
1990Sstevel@tonic-gate 	 * uncompressed src data or decompressed result data
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	dst = (caddr_t)CB_DST_VIRT;
2020Sstevel@tonic-gate 	if (descp->cpd_flag & CPD_COMPRESS) {
2030Sstevel@tonic-gate 		if (kdata_cksum(src, descp, CPD_CSUM))
2040Sstevel@tonic-gate 			return (ERR);
2050Sstevel@tonic-gate 		size = mmu_ptob(descp->cpd_pages);
2060Sstevel@tonic-gate 		len = decompress(src, dst, descp->cpd_length, size);
2070Sstevel@tonic-gate 		if (len != size) {
208785Seota 			prom_printf("\nbad decompressed len %lu, size %lu\n",
2090Sstevel@tonic-gate 			    len, size);
2100Sstevel@tonic-gate 			return (ERR);
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 		raw = 0;
2130Sstevel@tonic-gate 		datap = dst;
2140Sstevel@tonic-gate 	} else {
2150Sstevel@tonic-gate 		raw = 1;
2160Sstevel@tonic-gate 		datap = src;
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 	if (kdata_cksum(datap, descp, CPD_USUM))
2190Sstevel@tonic-gate 		return (ERR);
2200Sstevel@tonic-gate 	if (raw)
2210Sstevel@tonic-gate 		bcopy(src, dst, descp->cpd_length);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * advance past the kdata for this cpd_t
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	SF_ADV(descp->cpd_length);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	return (0);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * mapin part of the statefile buffer, copy to the virt destination,
2340Sstevel@tonic-gate  * and advance the statefile buffer offset.  this is used primarily
2350Sstevel@tonic-gate  * to copy thousands of tiny cpd_t into aligned struct space.
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate static void
get_phys_data(void * vdst,size_t size)2380Sstevel@tonic-gate get_phys_data(void *vdst, size_t size)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	caddr_t src;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	mapin_buf_pages(size, &src);
2430Sstevel@tonic-gate 	bcopy(src, vdst, size);
2440Sstevel@tonic-gate 	SF_ADV(size);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * clear leftover locked dtlb entries
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate static void
dtlb_cleanup(void)2520Sstevel@tonic-gate dtlb_cleanup(void)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	int dtlb_index;
2550Sstevel@tonic-gate 	caddr_t vaddr;
2560Sstevel@tonic-gate 	tte_t tte;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	CB_VENTRY(dtlb_cleanup);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	dtlb_index = cb_dents - CB_MAX_KPAGES - CB_MAX_BPAGES - 1;
2610Sstevel@tonic-gate 	for (; dtlb_index < cb_dents; dtlb_index++) {
2620Sstevel@tonic-gate 		get_dtlb_entry(dtlb_index, &vaddr, &tte);
2630Sstevel@tonic-gate 		if (TTE_IS_LOCKED(&tte)) {
2640Sstevel@tonic-gate 			tte.ll = 0;
2650Sstevel@tonic-gate 			set_dtlb_entry(dtlb_index, (caddr_t)0, &tte);
2660Sstevel@tonic-gate 			CB_VPRINTF(("    cleared dtlb entry %x\n", dtlb_index));
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * before calling this routine, all cprboot phys pages
2740Sstevel@tonic-gate  * are isolated from kernel pages; now we can restore
2750Sstevel@tonic-gate  * kpages from the statefile buffer
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate int
cb_restore_kpages(void)2780Sstevel@tonic-gate cb_restore_kpages(void)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	int npages, compressed, regular;
2810Sstevel@tonic-gate 	cpd_t desc;
2820Sstevel@tonic-gate 	char *str;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	str = "cb_restore_kpages";
2850Sstevel@tonic-gate 	CB_VPRINTF((ent_fmt, str, entry));
2860Sstevel@tonic-gate 
287*3446Smrj 	CPR_DEBUG(CPR_DEBUG1, "%s: restoring kpages... ", prog);
2880Sstevel@tonic-gate 	npages = compressed = regular = 0;
2890Sstevel@tonic-gate 	while (npages < sfile.kpages) {
2900Sstevel@tonic-gate 		get_phys_data(&desc, sizeof (desc));
2910Sstevel@tonic-gate 		if (desc.cpd_magic != CPR_PAGE_MAGIC) {
2920Sstevel@tonic-gate 			prom_printf("\nbad page magic 0x%x, expect 0x%x\n",
2930Sstevel@tonic-gate 			    desc.cpd_magic, CPR_PAGE_MAGIC);
2940Sstevel@tonic-gate 			return (ERR);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		if (restore_page_group(&desc))
2970Sstevel@tonic-gate 			return (ERR);
2980Sstevel@tonic-gate 		npages += desc.cpd_pages;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		if (desc.cpd_flag & CPD_COMPRESS)
3010Sstevel@tonic-gate 			compressed += desc.cpd_pages;
3020Sstevel@tonic-gate 		else
3030Sstevel@tonic-gate 			regular += desc.cpd_pages;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		/*
3060Sstevel@tonic-gate 		 * display a spin char for every 32 page groups
3070Sstevel@tonic-gate 		 * (a full spin <= each MB restored)
3080Sstevel@tonic-gate 		 */
3090Sstevel@tonic-gate 		if ((sfile.ngroups++ & 0x1f) == 0)
3100Sstevel@tonic-gate 			cb_spin();
3110Sstevel@tonic-gate 	}
312*3446Smrj 	CPR_DEBUG(CPR_DEBUG1, " \b\n");
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	dtlb_cleanup();
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (verbose) {
3170Sstevel@tonic-gate 		prom_printf("\npage stats: total %d, outside %d, "
3180Sstevel@tonic-gate 		    "move %d, precede %d\n", sfile.kpages, sfile.outside,
3190Sstevel@tonic-gate 		    sfile.move, sfile.precede);
3200Sstevel@tonic-gate 		prom_printf("page stats: ngroups %d, recycle %d\n",
3210Sstevel@tonic-gate 		    sfile.ngroups, sfile.recycle);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
324*3446Smrj 	CPR_DEBUG(CPR_DEBUG4,
3250Sstevel@tonic-gate 	    "%s: total=%d, npages=%d, compressed=%d, regular=%d\n",
326*3446Smrj 	    str, sfile.kpages, npages, compressed, regular);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * sanity check
3300Sstevel@tonic-gate 	 */
3310Sstevel@tonic-gate 	if (npages != sfile.kpages) {
3320Sstevel@tonic-gate 		prom_printf("\n%s: page count mismatch, expect %d, got %d\n",
3330Sstevel@tonic-gate 		    str, sfile.kpages, npages);
3340Sstevel@tonic-gate 		return (ERR);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	return (0);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * check and update the statefile terminator;
3430Sstevel@tonic-gate  * on exit there will be a leftover tlb entry,
3440Sstevel@tonic-gate  * but it will soon get replaced by restore_tlb()
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate int
cb_terminator(void)3470Sstevel@tonic-gate cb_terminator(void)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	ctrm_t cterm;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	CB_VENTRY(cb_terminator);
3520Sstevel@tonic-gate 	get_phys_data(&cterm, sizeof (cterm));
3530Sstevel@tonic-gate 	if (cterm.magic != CPR_TERM_MAGIC) {
3540Sstevel@tonic-gate 		prom_printf("\nbad term magic 0x%x, expect 0x%x\n",
3550Sstevel@tonic-gate 		    cterm.magic, CPR_TERM_MAGIC);
3560Sstevel@tonic-gate 		return (ERR);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 	cterm.tm_cprboot_start.tv_sec = cb_msec / 1000;
3590Sstevel@tonic-gate 	cb_mapin((caddr_t)CB_DST_VIRT, cterm.pfn,
3600Sstevel@tonic-gate 	    TTE8K, TTE_HWWR_INT, cb_dents - 1);
3610Sstevel@tonic-gate 	cpr_update_terminator(&cterm, (caddr_t)CB_DST_VIRT);
3620Sstevel@tonic-gate 	return (0);
3630Sstevel@tonic-gate }
364