Home | History | Annotate | Line # | Download | only in pci
amdpm.c revision 1.5.2.4
      1 /*	$NetBSD: amdpm.c,v 1.5.2.4 2005/11/10 14:06:00 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Enami Tsugutomo.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.5.2.4 2005/11/10 14:06:00 skrll Exp $");
     41 
     42 #include "opt_amdpm.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/callout.h>
     49 #include <sys/rnd.h>
     50 
     51 #include <dev/pci/pcivar.h>
     52 #include <dev/pci/pcireg.h>
     53 #include <dev/pci/pcidevs.h>
     54 
     55 #include <dev/pci/amdpmreg.h>
     56 
     57 struct amdpm_softc {
     58 	struct device sc_dev;
     59 
     60 	pci_chipset_tag_t sc_pc;
     61 	pcitag_t sc_tag;
     62 
     63 	bus_space_tag_t sc_iot;
     64 	bus_space_handle_t sc_ioh;		/* PMxx space */
     65 
     66 	struct callout sc_rnd_ch;
     67 	rndsource_element_t sc_rnd_source;
     68 #ifdef AMDPM_RND_COUNTERS
     69 	struct evcnt sc_rnd_hits;
     70 	struct evcnt sc_rnd_miss;
     71 	struct evcnt sc_rnd_data[256];
     72 #endif
     73 };
     74 
     75 static void	amdpm_rnd_callout(void *);
     76 
     77 #ifdef AMDPM_RND_COUNTERS
     78 #define	AMDPM_RNDCNT_INCR(ev)	(ev)->ev_count++
     79 #else
     80 #define	AMDPM_RNDCNT_INCR(ev)	/* nothing */
     81 #endif
     82 
     83 static int
     84 amdpm_match(struct device *parent, struct cfdata *match, void *aux)
     85 {
     86 	struct pci_attach_args *pa = aux;
     87 
     88 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
     89 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC)
     90 		return (1);
     91 	return (0);
     92 }
     93 
     94 static void
     95 amdpm_attach(struct device *parent, struct device *self, void *aux)
     96 {
     97 	struct amdpm_softc *sc = (struct amdpm_softc *) self;
     98 	struct pci_attach_args *pa = aux;
     99 	pcireg_t reg;
    100 	u_int32_t pmreg;
    101 	int i;
    102 
    103 	aprint_naive("\n");
    104 	aprint_normal("\n");
    105 
    106 	sc->sc_pc = pa->pa_pc;
    107 	sc->sc_tag = pa->pa_tag;
    108 	sc->sc_iot = pa->pa_iot;
    109 
    110 #if 0
    111 	aprint_normal("%s: ", sc->sc_dev.dv_xname);
    112 	pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    113 #endif
    114 
    115 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    116 	if ((reg & AMDPM_PMIOEN) == 0) {
    117 		aprint_error("%s: PMxx space isn't enabled\n",
    118 		    sc->sc_dev.dv_xname);
    119 		return;
    120 	}
    121 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
    122 	if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
    123 	    0, &sc->sc_ioh)) {
    124 		aprint_error("%s: failed to map PMxx space\n",
    125 		    sc->sc_dev.dv_xname);
    126 		return;
    127 	}
    128 
    129 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    130 	if (reg & AMDPM_RNGEN) {
    131 		/* Check to see if we can read data from the RNG. */
    132 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    133 		    AMDPM_RNGDATA);
    134 		for (i = 0; i < 1000; i++) {
    135 			pmreg = bus_space_read_4(sc->sc_iot,
    136 			    sc->sc_ioh, AMDPM_RNGSTAT);
    137 			if (pmreg & AMDPM_RNGDONE)
    138 				break;
    139 			delay(1);
    140 		}
    141 		if ((pmreg & AMDPM_RNGDONE) != 0) {
    142 			aprint_normal("%s: "
    143 			    "random number generator enabled (apprx. %dms)\n",
    144 			    sc->sc_dev.dv_xname, i);
    145 			callout_init(&sc->sc_rnd_ch);
    146 			rnd_attach_source(&sc->sc_rnd_source,
    147 			    sc->sc_dev.dv_xname, RND_TYPE_RNG,
    148 			    /*
    149 			     * XXX Careful!  The use of RND_FLAG_NO_ESTIMATE
    150 			     * XXX here is unobvious: we later feed raw bits
    151 			     * XXX into the "entropy pool" with rnd_add_data,
    152 			     * XXX explicitly supplying an entropy estimate.
    153 			     * XXX In this context, NO_ESTIMATE serves only
    154 			     * XXX to prevent rnd_add_data from trying to
    155 			     * XXX use the *time at which we added the data*
    156 			     * XXX as entropy, which is not a good idea since
    157 			     * XXX we add data periodically from a callout.
    158 			     */
    159 			    RND_FLAG_NO_ESTIMATE);
    160 #ifdef AMDPM_RND_COUNTERS
    161 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
    162 			    NULL, sc->sc_dev.dv_xname, "rnd hits");
    163 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
    164 			    NULL, sc->sc_dev.dv_xname, "rnd miss");
    165 			for (i = 0; i < 256; i++) {
    166 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
    167 				    EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
    168 				    "rnd data");
    169 			}
    170 #endif
    171 			amdpm_rnd_callout(sc);
    172 		}
    173 	}
    174 }
    175 
    176 CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
    177     amdpm_match, amdpm_attach, NULL, NULL);
    178 
    179 static void
    180 amdpm_rnd_callout(void *v)
    181 {
    182 	struct amdpm_softc *sc = v;
    183 	u_int32_t reg;
    184 #ifdef AMDPM_RND_COUNTERS
    185 	int i;
    186 #endif
    187 
    188 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
    189 	    AMDPM_RNGDONE) != 0) {
    190 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
    191 		rnd_add_data(&sc->sc_rnd_source, &reg,
    192 		    sizeof(reg), sizeof(reg) * NBBY);
    193 #ifdef AMDPM_RND_COUNTERS
    194 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
    195 		for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
    196 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
    197 #endif
    198 	} else
    199 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
    200 	callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
    201 }
    202