Home | History | Annotate | Line # | Download | only in ibm4xx
cpu.c revision 1.28.6.1
      1  1.28.6.1    cherry /*	$NetBSD: cpu.c,v 1.28.6.1 2011/06/23 14:19:29 cherry 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.28.6.1    cherry __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.28.6.1 2011/06/23 14:19:29 cherry 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.24     freza #include <sys/evcnt.h>
     45  1.28.6.1    cherry #include <sys/cpu.h>
     46       1.1       eeh 
     47      1.13   thorpej #include <uvm/uvm_extern.h>
     48      1.13   thorpej 
     49      1.23   thorpej #include <prop/proplib.h>
     50      1.23   thorpej 
     51  1.28.6.1    cherry #include <powerpc/ibm4xx/cpu.h>
     52       1.4    simonb #include <powerpc/ibm4xx/dev/plbvar.h>
     53       1.1       eeh 
     54       1.1       eeh struct cputab {
     55      1.25     freza 	u_int version;
     56      1.25     freza 	u_int mask;
     57      1.19       scw 	const char *name;
     58  1.28.6.1    cherry 	struct cache_info ci;
     59       1.1       eeh };
     60  1.28.6.1    cherry 
     61  1.28.6.1    cherry static const struct cputab models[] = {
     62  1.28.6.1    cherry 	{
     63  1.28.6.1    cherry 		.version = PVR_401A1,
     64  1.28.6.1    cherry 		.mask = 0xffff0000,
     65  1.28.6.1    cherry 		.name = "401A1",
     66  1.28.6.1    cherry 		.ci = {
     67  1.28.6.1    cherry 			.dcache_size = 1024,
     68  1.28.6.1    cherry 			.dcache_line_size = 16,
     69  1.28.6.1    cherry 			.icache_size = 2848,
     70  1.28.6.1    cherry 			.icache_line_size = 16,
     71  1.28.6.1    cherry 		}
     72  1.28.6.1    cherry 	}, {
     73  1.28.6.1    cherry 		.version = PVR_401B2,
     74  1.28.6.1    cherry 		.mask = 0xffff0000,
     75  1.28.6.1    cherry 		.name = "401B21",
     76  1.28.6.1    cherry 		.ci = {
     77  1.28.6.1    cherry 			.dcache_size = 8192,
     78  1.28.6.1    cherry 			.dcache_line_size = 16,
     79  1.28.6.1    cherry 			.icache_size = 16384,
     80  1.28.6.1    cherry 			.icache_line_size = 16,
     81  1.28.6.1    cherry 		}
     82  1.28.6.1    cherry 	}, {
     83  1.28.6.1    cherry 		.version = PVR_401C2,
     84  1.28.6.1    cherry 		.mask = 0xffff0000,
     85  1.28.6.1    cherry 		.name = "401C2",
     86  1.28.6.1    cherry 		.ci = {
     87  1.28.6.1    cherry 			.dcache_size = 8192,
     88  1.28.6.1    cherry 			.dcache_line_size = 16,
     89  1.28.6.1    cherry 			.icache_size = 0,
     90  1.28.6.1    cherry 			.icache_line_size = 16,
     91  1.28.6.1    cherry 		}
     92  1.28.6.1    cherry 	}, {
     93  1.28.6.1    cherry 		.version = PVR_401D2,
     94  1.28.6.1    cherry 		.mask = 0xffff0000,
     95  1.28.6.1    cherry 		.name = "401D2",
     96  1.28.6.1    cherry 		.ci = {
     97  1.28.6.1    cherry 			.dcache_size = 2848,
     98  1.28.6.1    cherry 			.dcache_line_size = 16,
     99  1.28.6.1    cherry 			.icache_size = 4096,
    100  1.28.6.1    cherry 			.icache_line_size = 16,
    101  1.28.6.1    cherry 		}
    102  1.28.6.1    cherry 	}, {
    103  1.28.6.1    cherry 		.version = PVR_401E2,
    104  1.28.6.1    cherry 		.mask = 0xffff0000,
    105  1.28.6.1    cherry 		.name = "401E2",
    106  1.28.6.1    cherry 		.ci = {
    107  1.28.6.1    cherry 			.dcache_size = 0,
    108  1.28.6.1    cherry 			.dcache_line_size = 16,
    109  1.28.6.1    cherry 			.icache_size = 0,
    110  1.28.6.1    cherry 			.icache_line_size = 16,
    111  1.28.6.1    cherry 		}
    112  1.28.6.1    cherry 	}, {
    113  1.28.6.1    cherry 		.version = PVR_401F2,
    114  1.28.6.1    cherry 		.mask = 0xffff0000,
    115  1.28.6.1    cherry 		.name = "401F2",
    116  1.28.6.1    cherry 		.ci = {
    117  1.28.6.1    cherry 			.dcache_size = 2048,
    118  1.28.6.1    cherry 			.dcache_line_size = 16,
    119  1.28.6.1    cherry 			.icache_size = 2848,
    120  1.28.6.1    cherry 			.icache_line_size = 16,
    121  1.28.6.1    cherry 		}
    122  1.28.6.1    cherry 	}, {
    123  1.28.6.1    cherry 		.version = PVR_401G2,
    124  1.28.6.1    cherry 		.mask = 0xffff0000,
    125  1.28.6.1    cherry 		.name = "401G2",
    126  1.28.6.1    cherry 		.ci = {
    127  1.28.6.1    cherry 			.dcache_size = 2848,
    128  1.28.6.1    cherry 			.dcache_line_size = 16,
    129  1.28.6.1    cherry 			.icache_size = 8192,
    130  1.28.6.1    cherry 			.icache_line_size = 16,
    131  1.28.6.1    cherry 		}
    132  1.28.6.1    cherry 	}, {
    133  1.28.6.1    cherry 		.version = PVR_403,
    134  1.28.6.1    cherry 		.mask = 0xffff0000,
    135  1.28.6.1    cherry 		.name = "403",
    136  1.28.6.1    cherry 		.ci = {
    137  1.28.6.1    cherry 			.dcache_size = 8192,
    138  1.28.6.1    cherry 			.dcache_line_size = 16,
    139  1.28.6.1    cherry 			.icache_size = 16384,
    140  1.28.6.1    cherry 			.icache_line_size = 16,
    141  1.28.6.1    cherry 		}
    142  1.28.6.1    cherry 	}, {
    143  1.28.6.1    cherry 		.version = PVR_405GP,
    144  1.28.6.1    cherry 		.mask = 0xffff0000,
    145  1.28.6.1    cherry 		.name = "405GP",
    146  1.28.6.1    cherry 		.ci = {
    147  1.28.6.1    cherry 			.dcache_size = 8192,
    148  1.28.6.1    cherry 			.dcache_line_size = 32,
    149  1.28.6.1    cherry 			.icache_size = 8192,
    150  1.28.6.1    cherry 			.icache_line_size = 32,
    151  1.28.6.1    cherry 		}
    152  1.28.6.1    cherry 	}, {
    153  1.28.6.1    cherry 		.version = PVR_405GPR,
    154  1.28.6.1    cherry 		.mask = 0xffff0000,
    155  1.28.6.1    cherry 		.name = "405GPr",
    156  1.28.6.1    cherry 		.ci = {
    157  1.28.6.1    cherry 			.dcache_size = 16384,
    158  1.28.6.1    cherry 			.dcache_line_size = 32,
    159  1.28.6.1    cherry 			.icache_size = 16384,
    160  1.28.6.1    cherry 			.icache_line_size = 32,
    161  1.28.6.1    cherry 		}
    162  1.28.6.1    cherry 	}, {
    163  1.28.6.1    cherry 		.version = PVR_405D5X1,
    164  1.28.6.1    cherry 		.mask = 0xfffff000,
    165  1.28.6.1    cherry 		.name = "Xilinx Virtex II Pro",
    166  1.28.6.1    cherry 		.ci = {
    167  1.28.6.1    cherry 			.dcache_size = 16384,
    168  1.28.6.1    cherry 			.dcache_line_size = 32,
    169  1.28.6.1    cherry 			.icache_size = 16384,
    170  1.28.6.1    cherry 			.icache_line_size = 32,
    171  1.28.6.1    cherry 		}
    172  1.28.6.1    cherry 	}, {
    173  1.28.6.1    cherry 		.version = PVR_405D5X2,
    174  1.28.6.1    cherry 		.mask = 0xfffff000,
    175  1.28.6.1    cherry 		.name = "Xilinx Virtex 4 FX",
    176  1.28.6.1    cherry 		.ci = {
    177  1.28.6.1    cherry 			.dcache_size = 16384,
    178  1.28.6.1    cherry 			.dcache_line_size = 32,
    179  1.28.6.1    cherry 			.icache_size = 16384,
    180  1.28.6.1    cherry 			.icache_line_size = 32,
    181  1.28.6.1    cherry 		}
    182  1.28.6.1    cherry 	}, {
    183  1.28.6.1    cherry 		.version = PVR_405EX,
    184  1.28.6.1    cherry 		.mask = 0xffff0000,
    185  1.28.6.1    cherry 		.name = "405EX",
    186  1.28.6.1    cherry 		.ci = {
    187  1.28.6.1    cherry 			.dcache_size = 16384,
    188  1.28.6.1    cherry 			.dcache_line_size = 32,
    189  1.28.6.1    cherry 			.icache_size = 16384,
    190  1.28.6.1    cherry 			.icache_line_size = 32,
    191  1.28.6.1    cherry 		}
    192  1.28.6.1    cherry 	}, {
    193  1.28.6.1    cherry 		.version = 0,
    194  1.28.6.1    cherry 		.mask = 0,
    195  1.28.6.1    cherry 		.name = NULL,
    196  1.28.6.1    cherry 		.ci = {
    197  1.28.6.1    cherry 			/*
    198  1.28.6.1    cherry 			 * Unknown CPU type.  For safety we'll specify a
    199  1.28.6.1    cherry 			 * cache with a 4-byte line size.  That way cache
    200  1.28.6.1    cherry 			 * flush routines won't miss any lines.
    201  1.28.6.1    cherry 			 */
    202  1.28.6.1    cherry 			.dcache_line_size = 4,
    203  1.28.6.1    cherry 			.icache_line_size = 4,
    204  1.28.6.1    cherry 		},
    205  1.28.6.1    cherry 	},
    206       1.1       eeh };
    207       1.1       eeh 
    208  1.28.6.1    cherry static int	cpumatch(device_t, cfdata_t, void *);
    209  1.28.6.1    cherry static void	cpuattach(device_t, device_t, void *);
    210       1.1       eeh 
    211  1.28.6.1    cherry CFATTACH_DECL_NEW(cpu, 0, cpumatch, cpuattach, NULL, NULL);
    212       1.1       eeh 
    213       1.1       eeh int ncpus;
    214       1.1       eeh 
    215      1.24     freza struct cpu_info cpu_info[1] = {
    216      1.24     freza 	{
    217      1.24     freza 		/* XXX add more ci_ev_* as we teach 4xx about them */
    218      1.24     freza 		.ci_ev_clock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
    219      1.24     freza 		    NULL, "cpu0", "clock"),
    220      1.24     freza 		.ci_ev_statclock = EVCNT_INITIALIZER(EVCNT_TYPE_INTR,
    221      1.24     freza 		    NULL, "cpu0", "stat clock"),
    222      1.26        ad 		.ci_curlwp = &lwp0,
    223      1.24     freza 	}
    224      1.24     freza };
    225      1.17     shige 
    226      1.17     shige char cpu_model[80];
    227       1.1       eeh 
    228  1.28.6.1    cherry bool cpufound;
    229       1.1       eeh 
    230       1.1       eeh static int
    231  1.28.6.1    cherry cpumatch(device_t parent, cfdata_t cf, void *aux)
    232       1.1       eeh {
    233       1.4    simonb 	struct plb_attach_args *paa = aux;
    234       1.1       eeh 
    235       1.1       eeh 	/* make sure that we're looking for a CPU */
    236       1.8   thorpej 	if (strcmp(paa->plb_name, cf->cf_name) != 0)
    237       1.3    simonb 		return (0);
    238       1.1       eeh 
    239       1.1       eeh 	return !cpufound;
    240       1.1       eeh }
    241       1.1       eeh 
    242       1.1       eeh static void
    243  1.28.6.1    cherry cpuattach(device_t parent, device_t self, void *aux)
    244       1.1       eeh {
    245  1.28.6.1    cherry 	struct cpu_info * const ci = curcpu();
    246  1.28.6.1    cherry 	const struct cputab *cp;
    247      1.25     freza 	u_int processor_freq;
    248      1.23   thorpej 	prop_number_t freq;
    249       1.2       eeh 
    250      1.23   thorpej 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    251      1.23   thorpej 	KASSERT(freq != NULL);
    252      1.23   thorpej 	processor_freq = (unsigned int) prop_number_integer_value(freq);
    253       1.1       eeh 
    254  1.28.6.1    cherry 	cpufound = true;
    255       1.1       eeh 	ncpus++;
    256       1.1       eeh 
    257  1.28.6.1    cherry 	const u_int pvr = mfpvr();
    258  1.28.6.1    cherry 	for (cp = models; cp->name != NULL; cp++) {
    259  1.28.6.1    cherry 		if ((pvr & cp->mask) == cp->version) {
    260  1.28.6.1    cherry 			strcpy(cpu_model, cp->name);
    261       1.1       eeh 			break;
    262  1.28.6.1    cherry 		}
    263       1.1       eeh 	}
    264  1.28.6.1    cherry 	if (__predict_false(cp->name == NULL))
    265      1.25     freza 		sprintf(cpu_model, "Version 0x%x", pvr);
    266      1.25     freza 
    267  1.28.6.1    cherry 	aprint_normal(": %uMHz %s (PVR 0x%x)\n",
    268  1.28.6.1    cherry 	    (processor_freq + 500000) / 1000000,
    269  1.28.6.1    cherry 	    (cp->name != NULL ? cpu_model : "unknown model"),
    270  1.28.6.1    cherry 	    pvr);
    271       1.1       eeh 
    272       1.1       eeh 	cpu_probe_cache();
    273       1.1       eeh 
    274      1.25     freza 	/* We would crash later on anyway so just make the reason obvious */
    275  1.28.6.1    cherry 	if (ci->ci_ci.icache_size == 0 && ci->ci_ci.dcache_size == 0)
    276  1.28.6.1    cherry 		panic("%s: %s: could not detect cache size",
    277  1.28.6.1    cherry 		    __func__, device_xname(self));
    278  1.28.6.1    cherry 
    279  1.28.6.1    cherry 	aprint_normal_dev(self, "%uKB/%uB L1 instruction cache\n",
    280  1.28.6.1    cherry 	    ci->ci_ci.icache_size / 1024, ci->ci_ci.icache_line_size);
    281  1.28.6.1    cherry 	aprint_normal_dev(self, "%uKB/%uB L1 data cache\n",
    282  1.28.6.1    cherry 	    ci->ci_ci.dcache_size / 1024, ci->ci_ci.dcache_line_size);
    283       1.1       eeh }
    284       1.1       eeh 
    285       1.1       eeh /*
    286       1.3    simonb  * This routine must be explicitly called to initialize the
    287       1.3    simonb  * CPU cache information so cache flushe and memcpy operation
    288       1.1       eeh  * work.
    289       1.1       eeh  */
    290       1.1       eeh void
    291      1.27    cegger cpu_probe_cache(void)
    292       1.1       eeh {
    293  1.28.6.1    cherry 	struct cpu_info * const ci = curcpu();
    294  1.28.6.1    cherry 	const struct cputab *cp = models;
    295      1.28  kiyohara 
    296  1.28.6.1    cherry 	const u_int pvr = mfpvr();
    297  1.28.6.1    cherry 	for (cp = models; cp->name != NULL; cp++) {
    298      1.28  kiyohara 		if ((pvr & cp->mask) == cp->version)
    299      1.28  kiyohara 			break;
    300      1.28  kiyohara 	}
    301      1.28  kiyohara 
    302       1.1       eeh 	/*
    303  1.28.6.1    cherry 	 * Copy the cache from the cputab into cpu_info.
    304       1.1       eeh 	 */
    305  1.28.6.1    cherry 	ci->ci_ci = cp->ci;
    306       1.1       eeh }
    307       1.1       eeh 
    308       1.1       eeh /*
    309       1.1       eeh  * These small routines may have to be replaced,
    310       1.1       eeh  * if/when we support processors other that the 604.
    311       1.1       eeh  */
    312       1.1       eeh 
    313       1.1       eeh void
    314  1.28.6.1    cherry dcache_wbinv_page(vaddr_t va)
    315       1.1       eeh {
    316  1.28.6.1    cherry 	const size_t dcache_line_size = curcpu()->ci_ci.dcache_line_size;
    317       1.1       eeh 
    318  1.28.6.1    cherry 	if (dcache_line_size) {
    319  1.28.6.1    cherry 		for (size_t i = 0; i < PAGE_SIZE; i += dcache_line_size) {
    320  1.28.6.1    cherry 			__asm volatile("dcbf %0,%1" : : "b" (va), "r" (i));
    321  1.28.6.1    cherry 		}
    322  1.28.6.1    cherry 		__asm volatile("sync;isync" : : );
    323  1.28.6.1    cherry 	}
    324       1.1       eeh }
    325