xref: /netbsd-src/sys/arch/powerpc/ibm4xx/cpu.c (revision f10e8fc157b578b98320eb1c228c17449576e984)
1*f10e8fc1Srin /*	$NetBSD: cpu.c,v 1.39 2022/10/05 08:18:00 rin Exp $	*/
24b971968Seeh 
34b971968Seeh /*
44b971968Seeh  * Copyright 2001 Wasabi Systems, Inc.
54b971968Seeh  * All rights reserved.
64b971968Seeh  *
74b971968Seeh  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
84b971968Seeh  *
94b971968Seeh  * Redistribution and use in source and binary forms, with or without
104b971968Seeh  * modification, are permitted provided that the following conditions
114b971968Seeh  * are met:
124b971968Seeh  * 1. Redistributions of source code must retain the above copyright
134b971968Seeh  *    notice, this list of conditions and the following disclaimer.
144b971968Seeh  * 2. Redistributions in binary form must reproduce the above copyright
154b971968Seeh  *    notice, this list of conditions and the following disclaimer in the
164b971968Seeh  *    documentation and/or other materials provided with the distribution.
174b971968Seeh  * 3. All advertising materials mentioning features or use of this software
184b971968Seeh  *    must display the following acknowledgement:
194b971968Seeh  *      This product includes software developed for the NetBSD Project by
204b971968Seeh  *      Wasabi Systems, Inc.
214b971968Seeh  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
224b971968Seeh  *    or promote products derived from this software without specific prior
234b971968Seeh  *    written permission.
244b971968Seeh  *
254b971968Seeh  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
264b971968Seeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
274b971968Seeh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
284b971968Seeh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
294b971968Seeh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
304b971968Seeh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
314b971968Seeh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
324b971968Seeh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
334b971968Seeh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
344b971968Seeh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
354b971968Seeh  * POSSIBILITY OF SUCH DAMAGE.
364b971968Seeh  */
374b971968Seeh 
38ed517291Slukem #include <sys/cdefs.h>
39*f10e8fc1Srin __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.39 2022/10/05 08:18:00 rin Exp $");
40ed517291Slukem 
414b971968Seeh #include <sys/param.h>
424b971968Seeh #include <sys/systm.h>
434b971968Seeh #include <sys/device.h>
4478037d3fSfreza #include <sys/evcnt.h>
451fd2c684Smatt #include <sys/cpu.h>
464b971968Seeh 
47c9228c8dSthorpej #include <uvm/uvm_extern.h>
48c9228c8dSthorpej 
49fb44a857Sthorpej #include <prop/proplib.h>
50fb44a857Sthorpej 
511fd2c684Smatt #include <powerpc/ibm4xx/cpu.h>
526bf1aaf8Ssimonb #include <powerpc/ibm4xx/dev/plbvar.h>
534b971968Seeh 
544b971968Seeh struct cputab {
5560d10418Sfreza 	u_int version;
5660d10418Sfreza 	u_int mask;
5747e69aa5Sscw 	const char *name;
583edffcf9Smatt 	struct cache_info ci;
594b971968Seeh };
603edffcf9Smatt 
614f384b1aSmatt static const struct cputab models[] = {
623edffcf9Smatt 	{
633edffcf9Smatt 		.version = PVR_401A1,
643edffcf9Smatt 		.mask = 0xffff0000,
653edffcf9Smatt 		.name = "401A1",
663edffcf9Smatt 		.ci = {
673edffcf9Smatt 			.dcache_size = 1024,
683edffcf9Smatt 			.dcache_line_size = 16,
691f97927dSsimonb 			.icache_size = 2048,
703edffcf9Smatt 			.icache_line_size = 16,
713edffcf9Smatt 		}
723edffcf9Smatt 	}, {
733edffcf9Smatt 		.version = PVR_401B2,
743edffcf9Smatt 		.mask = 0xffff0000,
753edffcf9Smatt 		.name = "401B21",
763edffcf9Smatt 		.ci = {
773edffcf9Smatt 			.dcache_size = 8192,
783edffcf9Smatt 			.dcache_line_size = 16,
793edffcf9Smatt 			.icache_size = 16384,
803edffcf9Smatt 			.icache_line_size = 16,
813edffcf9Smatt 		}
823edffcf9Smatt 	}, {
833edffcf9Smatt 		.version = PVR_401C2,
843edffcf9Smatt 		.mask = 0xffff0000,
853edffcf9Smatt 		.name = "401C2",
863edffcf9Smatt 		.ci = {
873edffcf9Smatt 			.dcache_size = 8192,
883edffcf9Smatt 			.dcache_line_size = 16,
893edffcf9Smatt 			.icache_size = 0,
903edffcf9Smatt 			.icache_line_size = 16,
913edffcf9Smatt 		}
923edffcf9Smatt 	}, {
933edffcf9Smatt 		.version = PVR_401D2,
943edffcf9Smatt 		.mask = 0xffff0000,
953edffcf9Smatt 		.name = "401D2",
963edffcf9Smatt 		.ci = {
971f97927dSsimonb 			.dcache_size = 2048,
983edffcf9Smatt 			.dcache_line_size = 16,
993edffcf9Smatt 			.icache_size = 4096,
1003edffcf9Smatt 			.icache_line_size = 16,
1013edffcf9Smatt 		}
1023edffcf9Smatt 	}, {
1033edffcf9Smatt 		.version = PVR_401E2,
1043edffcf9Smatt 		.mask = 0xffff0000,
1053edffcf9Smatt 		.name = "401E2",
1063edffcf9Smatt 		.ci = {
1073edffcf9Smatt 			.dcache_size = 0,
1083edffcf9Smatt 			.dcache_line_size = 16,
1093edffcf9Smatt 			.icache_size = 0,
1103edffcf9Smatt 			.icache_line_size = 16,
1113edffcf9Smatt 		}
1123edffcf9Smatt 	}, {
1133edffcf9Smatt 		.version = PVR_401F2,
1143edffcf9Smatt 		.mask = 0xffff0000,
1153edffcf9Smatt 		.name = "401F2",
1163edffcf9Smatt 		.ci = {
1173edffcf9Smatt 			.dcache_size = 2048,
1183edffcf9Smatt 			.dcache_line_size = 16,
1191f97927dSsimonb 			.icache_size = 2048,
1203edffcf9Smatt 			.icache_line_size = 16,
1213edffcf9Smatt 		}
1223edffcf9Smatt 	}, {
1233edffcf9Smatt 		.version = PVR_401G2,
1243edffcf9Smatt 		.mask = 0xffff0000,
1253edffcf9Smatt 		.name = "401G2",
1263edffcf9Smatt 		.ci = {
1271f97927dSsimonb 			.dcache_size = 2048,
1283edffcf9Smatt 			.dcache_line_size = 16,
1293edffcf9Smatt 			.icache_size = 8192,
1303edffcf9Smatt 			.icache_line_size = 16,
1313edffcf9Smatt 		}
1323edffcf9Smatt 	}, {
1332ccbbad8Srin 		.version = PVR_403GA,	/* XXX no MMU */
1342ccbbad8Srin 		.mask = 0xffffff00,
1352ccbbad8Srin 		.name = "403GA",
1362ccbbad8Srin 		.ci = {
1372ccbbad8Srin 			.dcache_size = 1024,
1382ccbbad8Srin 			.dcache_line_size = 16,
1392ccbbad8Srin 			.icache_size = 2048,
1402ccbbad8Srin 			.icache_line_size = 16,
1412ccbbad8Srin 		}
1422ccbbad8Srin 	}, {
1432ccbbad8Srin 		.version = PVR_403GB,	/* XXX no MMU */
1442ccbbad8Srin 		.mask = 0xffffff00,
1452ccbbad8Srin 		.name = "403GB",
1462ccbbad8Srin 		.ci = {
1472ccbbad8Srin 			.dcache_size = 1024,
1482ccbbad8Srin 			.dcache_line_size = 16,
1492ccbbad8Srin 			.icache_size = 2048,
1502ccbbad8Srin 			.icache_line_size = 16,
1512ccbbad8Srin 		}
1522ccbbad8Srin 	}, {
1532ccbbad8Srin 		.version = PVR_403GC,
1542ccbbad8Srin 		.mask = 0xffffff00,
1552ccbbad8Srin 		.name = "403GC",
1562ccbbad8Srin 		.ci = {
1572ccbbad8Srin 			.dcache_size = 1024,
1582ccbbad8Srin 			.dcache_line_size = 16,
1592ccbbad8Srin 			.icache_size = 2048,
1602ccbbad8Srin 			.icache_line_size = 16,
1612ccbbad8Srin 		}
1622ccbbad8Srin 	}, {
1632ccbbad8Srin 		.version = PVR_403GCX,
1642ccbbad8Srin 		.mask = 0xffffff00,
1652ccbbad8Srin 		.name = "403GCX",
1663edffcf9Smatt 		.ci = {
1673edffcf9Smatt 			.dcache_size = 8192,
1683edffcf9Smatt 			.dcache_line_size = 16,
1693edffcf9Smatt 			.icache_size = 16384,
1703edffcf9Smatt 			.icache_line_size = 16,
1713edffcf9Smatt 		}
1723edffcf9Smatt 	}, {
1733edffcf9Smatt 		.version = PVR_405GP,
1743edffcf9Smatt 		.mask = 0xffff0000,
1753edffcf9Smatt 		.name = "405GP",
1763edffcf9Smatt 		.ci = {
1773edffcf9Smatt 			.dcache_size = 8192,
1783edffcf9Smatt 			.dcache_line_size = 32,
1792d558d53Srin 			.icache_size = 16384,
1803edffcf9Smatt 			.icache_line_size = 32,
1813edffcf9Smatt 		}
1823edffcf9Smatt 	}, {
1833edffcf9Smatt 		.version = PVR_405GPR,
1843edffcf9Smatt 		.mask = 0xffff0000,
1853edffcf9Smatt 		.name = "405GPr",
1863edffcf9Smatt 		.ci = {
1873edffcf9Smatt 			.dcache_size = 16384,
1883edffcf9Smatt 			.dcache_line_size = 32,
1893edffcf9Smatt 			.icache_size = 16384,
1903edffcf9Smatt 			.icache_line_size = 32,
1913edffcf9Smatt 		}
1923edffcf9Smatt 	}, {
1933edffcf9Smatt 		.version = PVR_405D5X1,
1943edffcf9Smatt 		.mask = 0xfffff000,
1953edffcf9Smatt 		.name = "Xilinx Virtex II Pro",
1963edffcf9Smatt 		.ci = {
1973edffcf9Smatt 			.dcache_size = 16384,
1983edffcf9Smatt 			.dcache_line_size = 32,
1993edffcf9Smatt 			.icache_size = 16384,
2003edffcf9Smatt 			.icache_line_size = 32,
2013edffcf9Smatt 		}
2023edffcf9Smatt 	}, {
2033edffcf9Smatt 		.version = PVR_405D5X2,
2043edffcf9Smatt 		.mask = 0xfffff000,
2053edffcf9Smatt 		.name = "Xilinx Virtex 4 FX",
2063edffcf9Smatt 		.ci = {
2073edffcf9Smatt 			.dcache_size = 16384,
2083edffcf9Smatt 			.dcache_line_size = 32,
2093edffcf9Smatt 			.icache_size = 16384,
2103edffcf9Smatt 			.icache_line_size = 32,
2113edffcf9Smatt 		}
2123edffcf9Smatt 	}, {
2133edffcf9Smatt 		.version = PVR_405EX,
2143edffcf9Smatt 		.mask = 0xffff0000,
2153edffcf9Smatt 		.name = "405EX",
2163edffcf9Smatt 		.ci = {
2173edffcf9Smatt 			.dcache_size = 16384,
2183edffcf9Smatt 			.dcache_line_size = 32,
2193edffcf9Smatt 			.icache_size = 16384,
2203edffcf9Smatt 			.icache_line_size = 32,
2213edffcf9Smatt 		}
2223edffcf9Smatt 	}, {
2233edffcf9Smatt 		.version = 0,
2243edffcf9Smatt 		.mask = 0,
2253edffcf9Smatt 		.name = NULL,
2263edffcf9Smatt 		.ci = {
2273edffcf9Smatt 			/*
2283edffcf9Smatt 			 * Unknown CPU type.  For safety we'll specify a
2293edffcf9Smatt 			 * cache with a 4-byte line size.  That way cache
2303edffcf9Smatt 			 * flush routines won't miss any lines.
2313edffcf9Smatt 			 */
2323edffcf9Smatt 			.dcache_line_size = 4,
2333edffcf9Smatt 			.icache_line_size = 4,
2343edffcf9Smatt 		},
2353edffcf9Smatt 	},
2364b971968Seeh };
2374b971968Seeh 
238612b7ee5Smatt static int	cpumatch(device_t, cfdata_t, void *);
239612b7ee5Smatt static void	cpuattach(device_t, device_t, void *);
2404b971968Seeh 
2413edffcf9Smatt CFATTACH_DECL_NEW(cpu, 0, cpumatch, cpuattach, NULL, NULL);
2424b971968Seeh 
2434b971968Seeh int ncpus;
2444b971968Seeh 
24578037d3fSfreza struct cpu_info cpu_info[1] = {
24678037d3fSfreza 	{
24778037d3fSfreza 		/* XXX add more ci_ev_* as we teach 4xx about them */
24878037d3fSfreza 		.ci_ev_clock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
24978037d3fSfreza 		    NULL, "cpu0", "clock"),
25078037d3fSfreza 		.ci_ev_statclock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
25178037d3fSfreza 		    NULL, "cpu0", "stat clock"),
2522783731bSad 		.ci_curlwp = &lwp0,
25378037d3fSfreza 	}
25478037d3fSfreza };
2554b971968Seeh 
2563edffcf9Smatt bool cpufound;
2574b971968Seeh 
2584b971968Seeh static int
cpumatch(device_t parent,cfdata_t cf,void * aux)259612b7ee5Smatt cpumatch(device_t parent, cfdata_t cf, void *aux)
2604b971968Seeh {
2616bf1aaf8Ssimonb 	struct plb_attach_args *paa = aux;
2624b971968Seeh 
2634b971968Seeh 	/* make sure that we're looking for a CPU */
264d1ad2ac4Sthorpej 	if (strcmp(paa->plb_name, cf->cf_name) != 0)
2654b971968Seeh 		return (0);
2664b971968Seeh 
2674b971968Seeh 	return !cpufound;
2684b971968Seeh }
2694b971968Seeh 
2704b971968Seeh static void
cpuattach(device_t parent,device_t self,void * aux)271612b7ee5Smatt cpuattach(device_t parent, device_t self, void *aux)
2724b971968Seeh {
2733edffcf9Smatt 	struct cpu_info * const ci = curcpu();
2743edffcf9Smatt 	const struct cputab *cp;
27560d10418Sfreza 	u_int processor_freq;
276fb44a857Sthorpej 	prop_number_t freq;
2770754ce03Seeh 
278fb44a857Sthorpej 	freq = prop_dictionary_get(board_properties, "processor-frequency");
279fb44a857Sthorpej 	KASSERT(freq != NULL);
280fb44a857Sthorpej 	processor_freq = (unsigned int) prop_number_integer_value(freq);
2814b971968Seeh 
2823edffcf9Smatt 	cpufound = true;
2834b971968Seeh 	ncpus++;
2844b971968Seeh 
2853edffcf9Smatt 	const u_int pvr = mfpvr();
2863edffcf9Smatt 	for (cp = models; cp->name != NULL; cp++) {
2873edffcf9Smatt 		if ((pvr & cp->mask) == cp->version) {
288e691d98fSchristos 			cpu_setmodel("%s", cp->name);
2893edffcf9Smatt 			break;
2903edffcf9Smatt 		}
2913edffcf9Smatt 	}
2923edffcf9Smatt 	if (__predict_false(cp->name == NULL))
293e691d98fSchristos 		cpu_setmodel("Version 0x%x", pvr);
2944b971968Seeh 
2950e408d46Srin 	aprint_normal(": %uMHz %s (PVR 0x%08x)\n",
2963edffcf9Smatt 	    (processor_freq + 500000) / 1000000,
297e691d98fSchristos 	    (cp->name != NULL ? cpu_getmodel() : "unknown model"),
2983edffcf9Smatt 	    pvr);
2994b971968Seeh 
3004b971968Seeh 	cpu_probe_cache();
3014b971968Seeh 
30260d10418Sfreza 	/* We would crash later on anyway so just make the reason obvious */
3033edffcf9Smatt 	if (ci->ci_ci.icache_size == 0 && ci->ci_ci.dcache_size == 0)
3043edffcf9Smatt 		panic("%s: %s: could not detect cache size",
3053edffcf9Smatt 		    __func__, device_xname(self));
3064b971968Seeh 
3073edffcf9Smatt 	aprint_normal_dev(self, "%uKB/%uB L1 instruction cache\n",
3083edffcf9Smatt 	    ci->ci_ci.icache_size / 1024, ci->ci_ci.icache_line_size);
3093edffcf9Smatt 	aprint_normal_dev(self, "%uKB/%uB L1 data cache\n",
3103edffcf9Smatt 	    ci->ci_ci.dcache_size / 1024, ci->ci_ci.dcache_line_size);
3114b971968Seeh }
3124b971968Seeh 
3134b971968Seeh /*
3144b971968Seeh  * This routine must be explicitly called to initialize the
3154b971968Seeh  * CPU cache information so cache flushe and memcpy operation
3164b971968Seeh  * work.
3174b971968Seeh  */
3184b971968Seeh void
cpu_probe_cache(void)319df7f595eScegger cpu_probe_cache(void)
3204b971968Seeh {
3213edffcf9Smatt 	struct cpu_info * const ci = curcpu();
3224f384b1aSmatt 	const struct cputab *cp = models;
3232692e2e2Skiyohara 
3243edffcf9Smatt 	const u_int pvr = mfpvr();
3253edffcf9Smatt 	for (cp = models; cp->name != NULL; cp++) {
3262692e2e2Skiyohara 		if ((pvr & cp->mask) == cp->version)
3272692e2e2Skiyohara 			break;
3282692e2e2Skiyohara 	}
3292692e2e2Skiyohara 
3304b971968Seeh 	/*
3313edffcf9Smatt 	 * Copy the cache from the cputab into cpu_info.
3324b971968Seeh 	 */
3333edffcf9Smatt 	ci->ci_ci = cp->ci;
3344b971968Seeh }
3354b971968Seeh 
3364b971968Seeh /*
3374b971968Seeh  * These small routines may have to be replaced,
3384b971968Seeh  * if/when we support processors other that the 604.
3394b971968Seeh  */
3404b971968Seeh 
3414b971968Seeh void
dcache_wbinv_page(vaddr_t va)3423edffcf9Smatt dcache_wbinv_page(vaddr_t va)
3434b971968Seeh {
3443edffcf9Smatt 	const size_t dcache_line_size = curcpu()->ci_ci.dcache_line_size;
3454b971968Seeh 
3463edffcf9Smatt 	if (dcache_line_size) {
3473edffcf9Smatt 		for (size_t i = 0; i < PAGE_SIZE; i += dcache_line_size) {
3483edffcf9Smatt 			__asm volatile ("dcbf %0,%1" : : "b" (va), "r" (i));
3493edffcf9Smatt 		}
350*f10e8fc1Srin 		__asm volatile ("sync; isync");
3514b971968Seeh 	}
3524b971968Seeh }
353