Home | History | Annotate | Line # | Download | only in ibm4xx
cpu.c revision 1.23
      1  1.23  thorpej /*	$NetBSD: cpu.c,v 1.23 2006/05/05 18:04:42 thorpej 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.23  thorpej __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.23 2006/05/05 18:04:42 thorpej 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.1      eeh 
     45  1.13  thorpej #include <uvm/uvm_extern.h>
     46  1.13  thorpej 
     47  1.23  thorpej #include <prop/proplib.h>
     48  1.23  thorpej 
     49   1.2      eeh #include <machine/cpu.h>
     50   1.4   simonb #include <powerpc/ibm4xx/dev/plbvar.h>
     51   1.1      eeh 
     52   1.1      eeh struct cputab {
     53   1.1      eeh 	int version;
     54  1.19      scw 	const char *name;
     55   1.1      eeh };
     56   1.1      eeh static struct cputab models[] = {
     57  1.15   simonb 	{ PVR_401A1  >> 16,	"401A1" },
     58  1.15   simonb 	{ PVR_401B2  >> 16,	"401B21" },
     59  1.15   simonb 	{ PVR_401C2  >> 16,	"401C2" },
     60  1.15   simonb 	{ PVR_401D2  >> 16,	"401D2" },
     61  1.15   simonb 	{ PVR_401E2  >> 16,	"401E2" },
     62  1.15   simonb 	{ PVR_401F2  >> 16,	"401F2" },
     63  1.15   simonb 	{ PVR_401G2  >> 16,	"401G2" },
     64  1.15   simonb 	{ PVR_403    >> 16,	"403" },
     65  1.15   simonb 	{ PVR_405GP  >> 16,	"405GP" },
     66  1.15   simonb 	{ PVR_405GPR >> 16,	"405GPr" },
     67  1.15   simonb 	{ 0,			NULL }
     68   1.1      eeh };
     69   1.1      eeh 
     70   1.1      eeh static int	cpumatch(struct device *, struct cfdata *, void *);
     71   1.1      eeh static void	cpuattach(struct device *, struct device *, void *);
     72   1.1      eeh 
     73  1.10  thorpej CFATTACH_DECL(cpu, sizeof(struct device),
     74  1.10  thorpej     cpumatch, cpuattach, NULL, NULL);
     75   1.1      eeh 
     76   1.1      eeh int ncpus;
     77   1.1      eeh 
     78  1.11     matt struct cpu_info cpu_info[1];
     79  1.17    shige 
     80  1.17    shige char cpu_model[80];
     81   1.1      eeh 
     82   1.1      eeh int cpufound = 0;
     83   1.1      eeh 
     84   1.1      eeh static int
     85   1.1      eeh cpumatch(struct device *parent, struct cfdata *cf, void *aux)
     86   1.1      eeh {
     87   1.4   simonb 	struct plb_attach_args *paa = aux;
     88   1.1      eeh 
     89   1.1      eeh 	/* make sure that we're looking for a CPU */
     90   1.8  thorpej 	if (strcmp(paa->plb_name, cf->cf_name) != 0)
     91   1.3   simonb 		return (0);
     92   1.1      eeh 
     93   1.1      eeh 	return !cpufound;
     94   1.1      eeh }
     95   1.1      eeh 
     96   1.1      eeh static void
     97   1.1      eeh cpuattach(struct device *parent, struct device *self, void *aux)
     98   1.1      eeh {
     99   1.1      eeh 	int pvr, cpu;
    100   1.1      eeh 	int own, pcf, cas, pcl, aid;
    101   1.1      eeh 	struct cputab *cp = models;
    102   1.2      eeh 	unsigned int processor_freq;
    103  1.23  thorpej 	prop_number_t freq;
    104   1.2      eeh 
    105  1.23  thorpej 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    106  1.23  thorpej 	KASSERT(freq != NULL);
    107  1.23  thorpej 	processor_freq = (unsigned int) prop_number_integer_value(freq);
    108   1.1      eeh 
    109   1.1      eeh 	cpufound++;
    110   1.1      eeh 	ncpus++;
    111   1.1      eeh 
    112  1.22    perry 	__asm ("mfpvr %0" : "=r"(pvr));
    113   1.1      eeh 	cpu = pvr >> 16;
    114   1.1      eeh 
    115   1.1      eeh 	/* Break PVR up into separate fields and print them out. */
    116   1.1      eeh 	own = (pvr >> 20) & 0xfff;
    117   1.1      eeh 	pcf = (pvr >> 16) & 0xf;
    118   1.1      eeh 	cas = (pvr >> 10) & 0x3f;
    119   1.1      eeh 	pcl = (pvr >> 6) & 0xf;
    120   1.1      eeh 	aid = pvr & 0x3f;
    121   1.1      eeh 
    122   1.1      eeh 	while (cp->name) {
    123   1.1      eeh 		if (cp->version == cpu)
    124   1.1      eeh 			break;
    125   1.1      eeh 		cp++;
    126   1.1      eeh 	}
    127   1.1      eeh 	if (cp->name)
    128   1.1      eeh 		strcpy(cpu_model, cp->name);
    129   1.1      eeh 	else
    130   1.1      eeh 		sprintf(cpu_model, "Version 0x%x", cpu);
    131   1.1      eeh 	sprintf(cpu_model + strlen(cpu_model), " (Revision %d.%d)",
    132   1.1      eeh 		(pvr >> 8) & 0xff, pvr & 0xff);
    133   1.1      eeh 
    134   1.1      eeh #if 1
    135   1.2      eeh 	printf(": %dMHz %s\n", processor_freq / 1000 / 1000,
    136   1.1      eeh 	    cpu_model);
    137   1.1      eeh #endif
    138   1.1      eeh 
    139   1.1      eeh 	cpu_probe_cache();
    140   1.1      eeh 
    141   1.3   simonb 	printf("Instruction cache size %d line size %d\n",
    142   1.1      eeh 		curcpu()->ci_ci.icache_size, curcpu()->ci_ci.icache_line_size);
    143   1.3   simonb 	printf("Data cache size %d line size %d\n",
    144   1.1      eeh 		curcpu()->ci_ci.dcache_size, curcpu()->ci_ci.dcache_line_size);
    145   1.1      eeh 
    146   1.1      eeh #ifdef DEBUG
    147   1.1      eeh 	/* It sux that the cache info here is useless. */
    148   1.1      eeh 	printf("PVR: owner %x core family %x cache %x version %x asic %x\n",
    149   1.1      eeh 		own, pcf, cas, pcl, aid);
    150   1.1      eeh #endif
    151   1.1      eeh }
    152   1.1      eeh 
    153   1.1      eeh /*
    154   1.3   simonb  * This routine must be explicitly called to initialize the
    155   1.3   simonb  * CPU cache information so cache flushe and memcpy operation
    156   1.1      eeh  * work.
    157   1.1      eeh  */
    158   1.1      eeh void
    159   1.1      eeh cpu_probe_cache()
    160   1.1      eeh {
    161  1.19      scw 	int pvr;
    162   1.1      eeh 
    163   1.1      eeh 	/*
    164  1.18      wiz 	 * First we need to identify the CPU and determine the
    165   1.1      eeh 	 * cache line size, or things like memset/memcpy may lose
    166   1.1      eeh 	 * badly.
    167   1.1      eeh 	 */
    168  1.21    perry 	__asm volatile("mfpvr %0" : "=r" (pvr));
    169  1.19      scw 	switch (pvr & 0xffff0000) {
    170   1.1      eeh 	case PVR_401A1:
    171   1.1      eeh 		curcpu()->ci_ci.dcache_size = 1024;
    172   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    173   1.1      eeh 		curcpu()->ci_ci.icache_size = 2848;
    174   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    175   1.1      eeh 		break;
    176   1.1      eeh 	case PVR_401B2:
    177   1.1      eeh 		curcpu()->ci_ci.dcache_size = 8192;
    178   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    179   1.1      eeh 		curcpu()->ci_ci.icache_size = 16384;
    180   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    181   1.1      eeh 		break;
    182   1.1      eeh 	case PVR_401C2:
    183   1.1      eeh 		curcpu()->ci_ci.dcache_size = 8192;
    184   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    185   1.1      eeh 		curcpu()->ci_ci.icache_size = 0;
    186   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    187   1.1      eeh 		break;
    188   1.1      eeh 	case PVR_401D2:
    189   1.1      eeh 		curcpu()->ci_ci.dcache_size = 2848;
    190   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    191   1.1      eeh 		curcpu()->ci_ci.icache_size = 4096;
    192   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    193   1.1      eeh 		break;
    194   1.1      eeh 	case PVR_401E2:
    195   1.1      eeh 		curcpu()->ci_ci.dcache_size = 0;
    196   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    197   1.1      eeh 		curcpu()->ci_ci.icache_size = 0;
    198   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    199   1.1      eeh 		break;
    200   1.1      eeh 	case PVR_401F2:
    201   1.1      eeh 		curcpu()->ci_ci.dcache_size = 2048;
    202   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    203   1.1      eeh 		curcpu()->ci_ci.icache_size = 2848;
    204   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    205   1.1      eeh 		break;
    206   1.1      eeh 	case PVR_401G2:
    207   1.1      eeh 		curcpu()->ci_ci.dcache_size = 2848;
    208   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    209   1.1      eeh 		curcpu()->ci_ci.icache_size = 8192;
    210   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    211   1.1      eeh 		break;
    212   1.1      eeh 	case PVR_403:
    213  1.12  hannken 		curcpu()->ci_ci.dcache_size = 8192;
    214   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 16;
    215  1.12  hannken 		curcpu()->ci_ci.icache_size = 16384;
    216   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 16;
    217   1.1      eeh 		break;
    218   1.1      eeh 	case PVR_405GP:
    219   1.1      eeh 		curcpu()->ci_ci.dcache_size = 8192;
    220   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 32;
    221   1.1      eeh 		curcpu()->ci_ci.icache_size = 8192;
    222  1.14  msaitoh 		curcpu()->ci_ci.icache_line_size = 32;
    223  1.14  msaitoh 		break;
    224  1.14  msaitoh 	case PVR_405GPR:
    225  1.14  msaitoh 		curcpu()->ci_ci.dcache_size = 16384;
    226  1.14  msaitoh 		curcpu()->ci_ci.dcache_line_size = 32;
    227  1.14  msaitoh 		curcpu()->ci_ci.icache_size = 16384;
    228   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 32;
    229   1.1      eeh 		break;
    230   1.1      eeh 	default:
    231   1.3   simonb 		/*
    232   1.3   simonb 		 * Unknown CPU type.  For safety we'll specify a
    233   1.3   simonb 		 * cache with a 4-byte line size.  That way cache
    234   1.1      eeh 		 * flush routines won't miss any lines.
    235   1.1      eeh 		 */
    236   1.1      eeh 		curcpu()->ci_ci.dcache_line_size = 4;
    237   1.1      eeh 		curcpu()->ci_ci.icache_line_size = 4;
    238   1.1      eeh 		break;
    239   1.1      eeh 	}
    240   1.1      eeh 
    241   1.1      eeh }
    242   1.1      eeh 
    243   1.1      eeh /*
    244   1.1      eeh  * These small routines may have to be replaced,
    245   1.1      eeh  * if/when we support processors other that the 604.
    246   1.1      eeh  */
    247   1.1      eeh 
    248   1.1      eeh void
    249   1.1      eeh dcache_flush_page(vaddr_t va)
    250   1.1      eeh {
    251   1.1      eeh 	int i;
    252   1.1      eeh 
    253   1.1      eeh 	if (curcpu()->ci_ci.dcache_line_size)
    254  1.13  thorpej 		for (i = 0; i < PAGE_SIZE;
    255  1.13  thorpej 		     i += curcpu()->ci_ci.dcache_line_size)
    256  1.22    perry 			__asm volatile("dcbf %0,%1" : : "r" (va), "r" (i));
    257  1.22    perry 	__asm volatile("sync;isync" : : );
    258   1.1      eeh }
    259   1.1      eeh 
    260   1.1      eeh void
    261   1.1      eeh icache_flush_page(vaddr_t va)
    262   1.1      eeh {
    263   1.1      eeh 	int i;
    264   1.1      eeh 
    265   1.1      eeh 	if (curcpu()->ci_ci.icache_line_size)
    266  1.13  thorpej 		for (i = 0; i < PAGE_SIZE;
    267  1.13  thorpej 		     i += curcpu()->ci_ci.icache_line_size)
    268  1.22    perry 			__asm volatile("icbi %0,%1" : : "r" (va), "r" (i));
    269  1.22    perry 	__asm volatile("sync;isync" : : );
    270   1.1      eeh }
    271   1.1      eeh 
    272   1.1      eeh void
    273   1.1      eeh dcache_flush(vaddr_t va, vsize_t len)
    274   1.1      eeh {
    275   1.1      eeh 	int i;
    276   1.1      eeh 
    277   1.1      eeh 	if (len == 0)
    278   1.1      eeh 		return;
    279   1.1      eeh 
    280   1.1      eeh 	/* Make sure we flush all cache lines */
    281   1.1      eeh 	len += va & (curcpu()->ci_ci.dcache_line_size-1);
    282   1.1      eeh 	if (curcpu()->ci_ci.dcache_line_size)
    283   1.1      eeh 		for (i = 0; i < len; i += curcpu()->ci_ci.dcache_line_size)
    284  1.22    perry 			__asm volatile("dcbf %0,%1" : : "r" (va), "r" (i));
    285  1.22    perry 	__asm volatile("sync;isync" : : );
    286   1.1      eeh }
    287   1.1      eeh 
    288   1.1      eeh void
    289   1.1      eeh icache_flush(vaddr_t va, vsize_t len)
    290   1.1      eeh {
    291   1.1      eeh 	int i;
    292   1.1      eeh 
    293   1.1      eeh 	if (len == 0)
    294   1.1      eeh 		return;
    295   1.1      eeh 
    296   1.1      eeh 	/* Make sure we flush all cache lines */
    297   1.1      eeh 	len += va & (curcpu()->ci_ci.icache_line_size-1);
    298   1.1      eeh 	if (curcpu()->ci_ci.icache_line_size)
    299   1.1      eeh 		for (i = 0; i < len; i += curcpu()->ci_ci.icache_line_size)
    300  1.22    perry 			__asm volatile("icbi %0,%1" : : "r" (va), "r" (i));
    301  1.22    perry 	__asm volatile("sync;isync" : : );
    302   1.1      eeh }
    303