1 /* $OpenBSD: amdpm.c,v 1.3 2002/11/04 17:12:34 fgsch 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/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/timeout.h> 44 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/pcireg.h> 47 #include <dev/pci/pcidevs.h> 48 49 #include <dev/rndvar.h> 50 #include <dev/pci/amdpmreg.h> 51 52 struct amdpm_softc { 53 struct device sc_dev; 54 55 pci_chipset_tag_t sc_pc; 56 pcitag_t sc_tag; 57 58 bus_space_tag_t sc_iot; 59 bus_space_handle_t sc_ioh; /* PMxx space */ 60 61 struct timeout sc_rnd_ch; 62 #ifdef AMDPM_RND_COUNTERS 63 struct evcnt sc_rnd_hits; 64 struct evcnt sc_rnd_miss; 65 struct evcnt sc_rnd_data[256]; 66 #endif 67 }; 68 69 int amdpm_match(struct device *, void *, void *); 70 void amdpm_attach(struct device *, struct device *, void *); 71 void amdpm_rnd_callout(void *); 72 73 struct cfattach amdpm_ca = { 74 sizeof(struct amdpm_softc), amdpm_match, amdpm_attach 75 }; 76 77 struct cfdriver amdpm_cd = { 78 NULL, "amdpm", DV_DULL 79 }; 80 81 #ifdef AMDPM_RND_COUNTERS 82 #define AMDPM_RNDCNT_INCR(ev) (ev)->ev_count++ 83 #else 84 #define AMDPM_RNDCNT_INCR(ev) /* nothing */ 85 #endif 86 87 int 88 amdpm_match(struct device *parent, void *match, void *aux) 89 { 90 struct pci_attach_args *pa = aux; 91 92 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD && 93 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC) 94 return (1); 95 return (0); 96 } 97 98 void 99 amdpm_attach(struct device *parent, struct device *self, void *aux) 100 { 101 struct amdpm_softc *sc = (struct amdpm_softc *) self; 102 struct pci_attach_args *pa = aux; 103 struct timeval tv1, tv2; 104 pcireg_t reg; 105 int i; 106 107 sc->sc_pc = pa->pa_pc; 108 sc->sc_tag = pa->pa_tag; 109 sc->sc_iot = pa->pa_iot; 110 111 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG); 112 if ((reg & AMDPM_PMIOEN) == 0) { 113 printf(": PMxx space isn't enabled\n"); 114 return; 115 } 116 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR); 117 if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE, 118 0, &sc->sc_ioh)) { 119 printf(": failed to map PMxx space\n"); 120 return; 121 } 122 123 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG); 124 if (reg & AMDPM_RNGEN) { 125 /* Check to see if we can read data from the RNG. */ 126 (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh, 127 AMDPM_RNGDATA); 128 /* benchmark the RNG */ 129 microtime(&tv1); 130 for (i = 2 * 1024; i--; ) { 131 while(!(bus_space_read_1(sc->sc_iot, sc->sc_ioh, 132 AMDPM_RNGSTAT) & AMDPM_RNGDONE)) 133 ; 134 (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh, 135 AMDPM_RNGDATA); 136 } 137 microtime(&tv2); 138 139 timersub(&tv2, &tv1, &tv1); 140 if (tv1.tv_sec) 141 tv1.tv_usec += 1000000 * tv1.tv_sec; 142 printf(": rng active, %dKb/sec", 8 * 1000000 / tv1.tv_usec); 143 144 #ifdef AMDPM_RND_COUNTERS 145 evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC, 146 NULL, sc->sc_dev.dv_xname, "rnd hits"); 147 evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC, 148 NULL, sc->sc_dev.dv_xname, "rnd miss"); 149 for (i = 0; i < 256; i++) { 150 evcnt_attach_dynamic(&sc->sc_rnd_data[i], 151 EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname, 152 "rnd data"); 153 } 154 #endif 155 timeout_set(&sc->sc_rnd_ch, amdpm_rnd_callout, sc); 156 amdpm_rnd_callout(sc); 157 } 158 } 159 160 void 161 amdpm_rnd_callout(void *v) 162 { 163 struct amdpm_softc *sc = v; 164 u_int32_t reg; 165 #ifdef AMDPM_RND_COUNTERS 166 int i; 167 #endif 168 169 if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) & 170 AMDPM_RNGDONE) != 0) { 171 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA); 172 add_true_randomness(reg); 173 #ifdef AMDPM_RND_COUNTERS 174 AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits); 175 for (i = 0; i < sizeof(reg); i++, reg >>= NBBY) 176 AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]); 177 #endif 178 } else 179 AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss); 180 timeout_add(&sc->sc_rnd_ch, 1); 181 } 182