Home | History | Annotate | Line # | Download | only in ibm4xx
cpu.c revision 1.19.2.3
      1  1.19.2.3     yamt /*	$NetBSD: cpu.c,v 1.19.2.3 2007/12/07 17:25:52 yamt Exp $	*/
      2       1.1      eeh 
      3       1.1      eeh /*
      4       1.1      eeh  * Copyright 2001 Wasabi Systems, Inc.
      5       1.1      eeh  * All rights reserved.
      6       1.1      eeh  *
      7       1.1      eeh  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      8       1.1      eeh  *
      9       1.1      eeh  * Redistribution and use in source and binary forms, with or without
     10       1.1      eeh  * modification, are permitted provided that the following conditions
     11       1.1      eeh  * are met:
     12       1.1      eeh  * 1. Redistributions of source code must retain the above copyright
     13       1.1      eeh  *    notice, this list of conditions and the following disclaimer.
     14       1.1      eeh  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1      eeh  *    notice, this list of conditions and the following disclaimer in the
     16       1.1      eeh  *    documentation and/or other materials provided with the distribution.
     17       1.1      eeh  * 3. All advertising materials mentioning features or use of this software
     18       1.1      eeh  *    must display the following acknowledgement:
     19       1.1      eeh  *      This product includes software developed for the NetBSD Project by
     20       1.1      eeh  *      Wasabi Systems, Inc.
     21       1.1      eeh  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.1      eeh  *    or promote products derived from this software without specific prior
     23       1.1      eeh  *    written permission.
     24       1.1      eeh  *
     25       1.1      eeh  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.1      eeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.1      eeh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.1      eeh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.1      eeh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.1      eeh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.1      eeh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.1      eeh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.1      eeh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.1      eeh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.1      eeh  * POSSIBILITY OF SUCH DAMAGE.
     36       1.1      eeh  */
     37      1.16    lukem 
     38      1.16    lukem #include <sys/cdefs.h>
     39  1.19.2.3     yamt __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.19.2.3 2007/12/07 17:25:52 yamt Exp $");
     40       1.1      eeh 
     41       1.1      eeh #include <sys/param.h>
     42       1.1      eeh #include <sys/systm.h>
     43       1.1      eeh #include <sys/device.h>
     44  1.19.2.2     yamt #include <sys/evcnt.h>
     45       1.1      eeh 
     46      1.13  thorpej #include <uvm/uvm_extern.h>
     47      1.13  thorpej 
     48  1.19.2.1     yamt #include <prop/proplib.h>
     49  1.19.2.1     yamt 
     50       1.2      eeh #include <machine/cpu.h>
     51       1.4   simonb #include <powerpc/ibm4xx/dev/plbvar.h>
     52       1.1      eeh 
     53       1.1      eeh struct cputab {
     54  1.19.2.2     yamt 	u_int version;
     55  1.19.2.2     yamt 	u_int mask;
     56      1.19      scw 	const char *name;
     57       1.1      eeh };
     58       1.1      eeh static struct cputab models[] = {
     59  1.19.2.2     yamt 	{ PVR_401A1, 	0xffff0000,	"401A1" 	},
     60  1.19.2.2     yamt 	{ PVR_401B2, 	0xffff0000,	"401B21" 	},
     61  1.19.2.2     yamt 	{ PVR_401C2, 	0xffff0000,	"401C2" 	},
     62  1.19.2.2     yamt 	{ PVR_401D2, 	0xffff0000,	"401D2" 	},
     63  1.19.2.2     yamt 	{ PVR_401E2, 	0xffff0000,	"401E2" 	},
     64  1.19.2.2     yamt 	{ PVR_401F2, 	0xffff0000,	"401F2" 	},
     65  1.19.2.2     yamt 	{ PVR_401G2, 	0xffff0000,	"401G2" 	},
     66  1.19.2.2     yamt 	{ PVR_403, 	0xffff0000,	"403" 		},
     67  1.19.2.2     yamt 	{ PVR_405GP, 	0xffff0000,	"405GP" 	},
     68  1.19.2.2     yamt 	{ PVR_405GPR, 	0xffff0000,	"405GPr" 	},
     69  1.19.2.2     yamt 	{ PVR_405D5X1, 	0xfffff000, 	"Xilinx Virtex II Pro" 	},
     70  1.19.2.2     yamt 	{ PVR_405D5X2, 	0xfffff000, 	"Xilinx Virtex 4 FX" 	},
     71  1.19.2.2     yamt 	{ 0, 		0,		NULL 		}
     72       1.1      eeh };
     73       1.1      eeh 
     74       1.1      eeh static int	cpumatch(struct device *, struct cfdata *, void *);
     75       1.1      eeh static void	cpuattach(struct device *, struct device *, void *);
     76       1.1      eeh 
     77      1.10  thorpej CFATTACH_DECL(cpu, sizeof(struct device),
     78      1.10  thorpej     cpumatch, cpuattach, NULL, NULL);
     79       1.1      eeh 
     80       1.1      eeh int ncpus;
     81       1.1      eeh 
     82  1.19.2.2     yamt struct cpu_info cpu_info[1] = {
     83  1.19.2.2     yamt 	{
     84  1.19.2.2     yamt 		/* XXX add more ci_ev_* as we teach 4xx about them */
     85  1.19.2.2     yamt 		.ci_ev_clock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
     86  1.19.2.2     yamt 		    NULL, "cpu0", "clock"),
     87  1.19.2.2     yamt 		.ci_ev_statclock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
     88  1.19.2.2     yamt 		    NULL, "cpu0", "stat clock"),
     89  1.19.2.2     yamt 		.ci_ev_softclock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
     90  1.19.2.2     yamt 		    NULL, "cpu0", "soft clock"),
     91  1.19.2.2     yamt 		.ci_ev_softnet = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
     92  1.19.2.2     yamt 		    NULL, "cpu0", "soft net"),
     93  1.19.2.2     yamt 		.ci_ev_softserial = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
     94  1.19.2.2     yamt 		    NULL, "cpu0", "soft serial"),
     95  1.19.2.3     yamt 		.ci_curlwp = &lwp0,
     96  1.19.2.2     yamt 	}
     97  1.19.2.2     yamt };
     98      1.17    shige 
     99      1.17    shige char cpu_model[80];
    100       1.1      eeh 
    101       1.1      eeh int cpufound = 0;
    102       1.1      eeh 
    103       1.1      eeh static int
    104       1.1      eeh cpumatch(struct device *parent, struct cfdata *cf, void *aux)
    105       1.1      eeh {
    106       1.4   simonb 	struct plb_attach_args *paa = aux;
    107       1.1      eeh 
    108       1.1      eeh 	/* make sure that we're looking for a CPU */
    109       1.8  thorpej 	if (strcmp(paa->plb_name, cf->cf_name) != 0)
    110       1.3   simonb 		return (0);
    111       1.1      eeh 
    112       1.1      eeh 	return !cpufound;
    113       1.1      eeh }
    114       1.1      eeh 
    115       1.1      eeh static void
    116       1.1      eeh cpuattach(struct device *parent, struct device *self, void *aux)
    117       1.1      eeh {
    118       1.1      eeh 	struct cputab *cp = models;
    119  1.19.2.2     yamt 	u_int pvr;
    120  1.19.2.2     yamt 	u_int processor_freq;
    121  1.19.2.1     yamt 	prop_number_t freq;
    122       1.2      eeh 
    123  1.19.2.1     yamt 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    124  1.19.2.1     yamt 	KASSERT(freq != NULL);
    125  1.19.2.1     yamt 	processor_freq = (unsigned int) prop_number_integer_value(freq);
    126       1.1      eeh 
    127       1.1      eeh 	cpufound++;
    128       1.1      eeh 	ncpus++;
    129       1.1      eeh 
    130  1.19.2.2     yamt 	pvr = mfpvr();
    131       1.1      eeh 	while (cp->name) {
    132  1.19.2.2     yamt 		if ((pvr & cp->mask) == cp->version)
    133       1.1      eeh 			break;
    134       1.1      eeh 		cp++;
    135       1.1      eeh 	}
    136       1.1      eeh 	if (cp->name)
    137       1.1      eeh 		strcpy(cpu_model, cp->name);
    138       1.1      eeh 	else
    139  1.19.2.2     yamt 		sprintf(cpu_model, "Version 0x%x", pvr);
    140  1.19.2.2     yamt 
    141  1.19.2.2     yamt 	printf(": %dMHz %s (PVR 0x%x)\n", processor_freq / 1000 / 1000,
    142  1.19.2.2     yamt 	    cp->name ? cp->name : "unknown model", pvr);
    143       1.1      eeh 
    144       1.1      eeh 	cpu_probe_cache();
    145       1.1      eeh 
    146  1.19.2.2     yamt 	/* We would crash later on anyway so just make the reason obvious */
    147  1.19.2.2     yamt 	if (curcpu()->ci_ci.icache_size == 0 &&
    148  1.19.2.2     yamt 	    curcpu()->ci_ci.dcache_size == 0)
    149  1.19.2.2     yamt 		panic("%s could not detect cache size", device_xname(self));
    150  1.19.2.2     yamt 
    151  1.19.2.2     yamt 	printf("%s: Instruction cache size %d line size %d\n",
    152  1.19.2.2     yamt 	    device_xname(self),
    153  1.19.2.2     yamt 	    curcpu()->ci_ci.icache_size, curcpu()->ci_ci.icache_line_size);
    154  1.19.2.2     yamt 	printf("%s: Data cache size %d line size %d\n",
    155  1.19.2.2     yamt 	    device_xname(self),
    156  1.19.2.2     yamt 	    curcpu()->ci_ci.dcache_size, curcpu()->ci_ci.dcache_line_size);
    157       1.1      eeh }
    158       1.1      eeh 
    159       1.1      eeh /*
    160       1.3   simonb  * This routine must be explicitly called to initialize the
    161       1.3   simonb  * CPU cache information so cache flushe and memcpy operation
    162       1.1      eeh  * work.
    163       1.1      eeh  */
    164       1.1      eeh void
    165       1.1      eeh cpu_probe_cache()
    166       1.1      eeh {
    167       1.1      eeh 	/*
    168      1.18      wiz 	 * First we need to identify the CPU and determine the
    169       1.1      eeh 	 * cache line size, or things like memset/memcpy may lose
    170       1.1      eeh 	 * badly.
    171       1.1      eeh 	 */
    172  1.19.2.2     yamt 	switch (mfpvr() & 0xffff0000) {
    173       1.1      eeh 	case PVR_401A1:
    174       1.1      eeh 		curcpu()->ci_ci.dcache_size = 1024;
    175       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    176       1.1      eeh 		curcpu()->ci_ci.icache_size = 2848;
    177       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    178       1.1      eeh 		break;
    179       1.1      eeh 	case PVR_401B2:
    180       1.1      eeh 		curcpu()->ci_ci.dcache_size = 8192;
    181       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    182       1.1      eeh 		curcpu()->ci_ci.icache_size = 16384;
    183       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    184       1.1      eeh 		break;
    185       1.1      eeh 	case PVR_401C2:
    186       1.1      eeh 		curcpu()->ci_ci.dcache_size = 8192;
    187       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    188       1.1      eeh 		curcpu()->ci_ci.icache_size = 0;
    189       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    190       1.1      eeh 		break;
    191       1.1      eeh 	case PVR_401D2:
    192       1.1      eeh 		curcpu()->ci_ci.dcache_size = 2848;
    193       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    194       1.1      eeh 		curcpu()->ci_ci.icache_size = 4096;
    195       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    196       1.1      eeh 		break;
    197       1.1      eeh 	case PVR_401E2:
    198       1.1      eeh 		curcpu()->ci_ci.dcache_size = 0;
    199       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    200       1.1      eeh 		curcpu()->ci_ci.icache_size = 0;
    201       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    202       1.1      eeh 		break;
    203       1.1      eeh 	case PVR_401F2:
    204       1.1      eeh 		curcpu()->ci_ci.dcache_size = 2048;
    205       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    206       1.1      eeh 		curcpu()->ci_ci.icache_size = 2848;
    207       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    208       1.1      eeh 		break;
    209       1.1      eeh 	case PVR_401G2:
    210       1.1      eeh 		curcpu()->ci_ci.dcache_size = 2848;
    211       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    212       1.1      eeh 		curcpu()->ci_ci.icache_size = 8192;
    213       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    214       1.1      eeh 		break;
    215       1.1      eeh 	case PVR_403:
    216      1.12  hannken 		curcpu()->ci_ci.dcache_size = 8192;
    217       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    218      1.12  hannken 		curcpu()->ci_ci.icache_size = 16384;
    219       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    220       1.1      eeh 		break;
    221       1.1      eeh 	case PVR_405GP:
    222       1.1      eeh 		curcpu()->ci_ci.dcache_size = 8192;
    223       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 32;
    224       1.1      eeh 		curcpu()->ci_ci.icache_size = 8192;
    225      1.14  msaitoh 		curcpu()->ci_ci.icache_line_size = 32;
    226      1.14  msaitoh 		break;
    227      1.14  msaitoh 	case PVR_405GPR:
    228  1.19.2.2     yamt 	case PVR_405D5X1:
    229  1.19.2.2     yamt 	case PVR_405D5X2:
    230      1.14  msaitoh 		curcpu()->ci_ci.dcache_size = 16384;
    231      1.14  msaitoh 		curcpu()->ci_ci.dcache_line_size = 32;
    232      1.14  msaitoh 		curcpu()->ci_ci.icache_size = 16384;
    233       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 32;
    234       1.1      eeh 		break;
    235       1.1      eeh 	default:
    236       1.3   simonb 		/*
    237       1.3   simonb 		 * Unknown CPU type.  For safety we'll specify a
    238       1.3   simonb 		 * cache with a 4-byte line size.  That way cache
    239       1.1      eeh 		 * flush routines won't miss any lines.
    240       1.1      eeh 		 */
    241       1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 4;
    242       1.1      eeh 		curcpu()->ci_ci.icache_line_size = 4;
    243       1.1      eeh 		break;
    244       1.1      eeh 	}
    245       1.1      eeh 
    246       1.1      eeh }
    247       1.1      eeh 
    248       1.1      eeh /*
    249       1.1      eeh  * These small routines may have to be replaced,
    250       1.1      eeh  * if/when we support processors other that the 604.
    251       1.1      eeh  */
    252       1.1      eeh 
    253       1.1      eeh void
    254       1.1      eeh dcache_flush_page(vaddr_t va)
    255       1.1      eeh {
    256       1.1      eeh 	int i;
    257       1.1      eeh 
    258       1.1      eeh 	if (curcpu()->ci_ci.dcache_line_size)
    259      1.13  thorpej 		for (i = 0; i < PAGE_SIZE;
    260      1.13  thorpej 		     i += curcpu()->ci_ci.dcache_line_size)
    261  1.19.2.1     yamt 			__asm volatile("dcbf %0,%1" : : "r" (va), "r" (i));
    262  1.19.2.1     yamt 	__asm volatile("sync;isync" : : );
    263       1.1      eeh }
    264       1.1      eeh 
    265       1.1      eeh void
    266       1.1      eeh icache_flush_page(vaddr_t va)
    267       1.1      eeh {
    268       1.1      eeh 	int i;
    269       1.1      eeh 
    270       1.1      eeh 	if (curcpu()->ci_ci.icache_line_size)
    271      1.13  thorpej 		for (i = 0; i < PAGE_SIZE;
    272      1.13  thorpej 		     i += curcpu()->ci_ci.icache_line_size)
    273  1.19.2.1     yamt 			__asm volatile("icbi %0,%1" : : "r" (va), "r" (i));
    274  1.19.2.1     yamt 	__asm volatile("sync;isync" : : );
    275       1.1      eeh }
    276       1.1      eeh 
    277       1.1      eeh void
    278       1.1      eeh dcache_flush(vaddr_t va, vsize_t len)
    279       1.1      eeh {
    280       1.1      eeh 	int i;
    281       1.1      eeh 
    282       1.1      eeh 	if (len == 0)
    283       1.1      eeh 		return;
    284       1.1      eeh 
    285       1.1      eeh 	/* Make sure we flush all cache lines */
    286       1.1      eeh 	len += va & (curcpu()->ci_ci.dcache_line_size-1);
    287       1.1      eeh 	if (curcpu()->ci_ci.dcache_line_size)
    288       1.1      eeh 		for (i = 0; i < len; i += curcpu()->ci_ci.dcache_line_size)
    289  1.19.2.1     yamt 			__asm volatile("dcbf %0,%1" : : "r" (va), "r" (i));
    290  1.19.2.1     yamt 	__asm volatile("sync;isync" : : );
    291       1.1      eeh }
    292       1.1      eeh 
    293       1.1      eeh void
    294       1.1      eeh icache_flush(vaddr_t va, vsize_t len)
    295       1.1      eeh {
    296       1.1      eeh 	int i;
    297       1.1      eeh 
    298       1.1      eeh 	if (len == 0)
    299       1.1      eeh 		return;
    300       1.1      eeh 
    301       1.1      eeh 	/* Make sure we flush all cache lines */
    302       1.1      eeh 	len += va & (curcpu()->ci_ci.icache_line_size-1);
    303       1.1      eeh 	if (curcpu()->ci_ci.icache_line_size)
    304       1.1      eeh 		for (i = 0; i < len; i += curcpu()->ci_ci.icache_line_size)
    305  1.19.2.1     yamt 			__asm volatile("icbi %0,%1" : : "r" (va), "r" (i));
    306  1.19.2.1     yamt 	__asm volatile("sync;isync" : : );
    307       1.1      eeh }
    308