Home | History | Annotate | Line # | Download | only in dev
octeon_rnm.c revision 1.2.4.2
      1  1.2.4.2  martin /*	$NetBSD: octeon_rnm.c,v 1.2.4.2 2020/05/19 17:39:04 martin Exp $	*/
      2      1.1  hikaru 
      3      1.1  hikaru /*
      4      1.1  hikaru  * Copyright (c) 2007 Internet Initiative Japan, Inc.
      5      1.1  hikaru  * All rights reserved.
      6      1.1  hikaru  *
      7      1.1  hikaru  * Redistribution and use in source and binary forms, with or without
      8      1.1  hikaru  * modification, are permitted provided that the following conditions
      9      1.1  hikaru  * are met:
     10      1.1  hikaru  * 1. Redistributions of source code must retain the above copyright
     11      1.1  hikaru  *    notice, this list of conditions and the following disclaimer.
     12      1.1  hikaru  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  hikaru  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  hikaru  *    documentation and/or other materials provided with the distribution.
     15      1.1  hikaru  *
     16      1.1  hikaru  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17      1.1  hikaru  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1  hikaru  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1  hikaru  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20      1.1  hikaru  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.1  hikaru  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.1  hikaru  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1  hikaru  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.1  hikaru  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  hikaru  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  hikaru  * SUCH DAMAGE.
     27      1.1  hikaru  */
     28      1.1  hikaru 
     29  1.2.4.1  martin /*
     30  1.2.4.1  martin  * Cavium Octeon Random Number Generator / Random Number Memory `RNM'
     31  1.2.4.1  martin  *
     32  1.2.4.1  martin  *	The RNM unit consists of:
     33  1.2.4.1  martin  *
     34  1.2.4.1  martin  *	1. 128 ring oscillators
     35  1.2.4.1  martin  *	2. an LFSR/SHA-1 conditioner
     36  1.2.4.1  martin  *	3. a 512-byte FIFO
     37  1.2.4.1  martin  *
     38  1.2.4.1  martin  *	When the unit is enabled, there are three modes of operation:
     39  1.2.4.1  martin  *
     40  1.2.4.1  martin  *	(a) deterministic: the ring oscillators are disabled and the
     41  1.2.4.1  martin  *	    LFSR/SHA-1 conditioner operates on fixed inputs to give
     42  1.2.4.1  martin  *	    reproducible results for testing,
     43  1.2.4.1  martin  *
     44  1.2.4.1  martin  *	(b) conditioned entropy: the ring oscillators are enabled and
     45  1.2.4.1  martin  *	    samples from them are fed through the LFSR/SHA-1
     46  1.2.4.1  martin  *	    conditioner before being put into the FIFO, and
     47  1.2.4.1  martin  *
     48  1.2.4.1  martin  *	(c) raw entropy: the ring oscillators are enabled, and a group
     49  1.2.4.1  martin  *	    of eight of them selected at any one time is sampled and
     50  1.2.4.1  martin  *	    fed into the FIFO.
     51  1.2.4.1  martin  *
     52  1.2.4.1  martin  *	Details:
     53  1.2.4.1  martin  *
     54  1.2.4.1  martin  *	- The FIFO is refilled whenever we read out of it, either with
     55  1.2.4.1  martin  *	  a load address or an IOBDMA operation.
     56  1.2.4.1  martin  *
     57  1.2.4.1  martin  *	- The conditioner takes 81 cycles to produce a 64-bit block of
     58  1.2.4.1  martin  *	  output in the FIFO whether in deterministic or conditioned
     59  1.2.4.1  martin  *	  entropy mode, each block consisting of the first 64 bits of a
     60  1.2.4.1  martin  *	  SHA-1 hash.
     61  1.2.4.1  martin  *
     62  1.2.4.1  martin  *	- A group of eight ring oscillators take 8 cycles to produce a
     63  1.2.4.1  martin  *	  64-bit block of output in the FIFO in raw entropy mode, each
     64  1.2.4.1  martin  *	  block consisting of eight consecutive samples from each RO in
     65  1.2.4.1  martin  *	  parallel.
     66  1.2.4.1  martin  *
     67  1.2.4.1  martin  *	The first sample of each RO always seems to be zero.  Further,
     68  1.2.4.1  martin  *	consecutive samples from a single ring oscillator are not
     69  1.2.4.1  martin  *	independent, so naive debiasing like a von Neumann extractor
     70  1.2.4.1  martin  *	falls flat on its face.  And parallel ring oscillators powered
     71  1.2.4.1  martin  *	by the same source may not be independent either, if they end
     72  1.2.4.1  martin  *	up locked.
     73  1.2.4.1  martin  *
     74  1.2.4.1  martin  *	We read out one FIFO's worth of raw samples from groups of 8
     75  1.2.4.1  martin  *	ring oscillators at a time, of 128 total, by going through them
     76  1.2.4.1  martin  *	round robin.  We take 32 consecutive samples from each ring
     77  1.2.4.1  martin  *	oscillator in a group of 8 in parallel before we count one bit
     78  1.2.4.1  martin  *	of entropy.  To get 256 bits of entropy, we read 4Kbit of data
     79  1.2.4.1  martin  *	from each of two 8-RO groups.
     80  1.2.4.1  martin  *
     81  1.2.4.1  martin  *	We could use the on-board LFSR/SHA-1 conditioner like the Linux
     82  1.2.4.1  martin  *	driver written by Cavium does, but it's not clear how many RO
     83  1.2.4.1  martin  *	samples go into the conditioner, and our entropy pool is a
     84  1.2.4.1  martin  *	perfectly good conditioner itself, so it seems there is little
     85  1.2.4.1  martin  *	advantage -- other than expedience -- to using the LFSR/SHA-1
     86  1.2.4.1  martin  *	conditioner.  All the manual says is that it samples 125 of the
     87  1.2.4.1  martin  *	128 ROs.  But the Cavium SHA-1 CPU instruction is advertised to
     88  1.2.4.1  martin  *	have a latency of 100 cycles, so it seems implausible that much
     89  1.2.4.1  martin  *	more than one sample from each RO could be squeezed in there.
     90  1.2.4.1  martin  *
     91  1.2.4.1  martin  *	The hardware exposes only 64 bits of each SHA-1 hash, and the
     92  1.2.4.1  martin  *	Linux driver uses 32 bits of that -- which, if treated as full
     93  1.2.4.1  martin  *	entropy, would mean an assessment of 3.9 bits of RO samples to
     94  1.2.4.1  martin  *	get 1 bit of entropy, whereas we take 256 bits of RO samples to
     95  1.2.4.1  martin  *	get one bit of entropy, so this seems reasonably conservative.
     96  1.2.4.1  martin  *
     97  1.2.4.1  martin  * Reference: Cavium Networks OCTEON Plus CN50XX Hardware Reference
     98  1.2.4.1  martin  * Manual, CN50XX-HM-0.99E PRELIMINARY, July 2008.
     99  1.2.4.1  martin  */
    100  1.2.4.1  martin 
    101      1.1  hikaru #include <sys/cdefs.h>
    102  1.2.4.2  martin __KERNEL_RCSID(0, "$NetBSD: octeon_rnm.c,v 1.2.4.2 2020/05/19 17:39:04 martin Exp $");
    103      1.1  hikaru 
    104      1.1  hikaru #include <sys/param.h>
    105      1.1  hikaru #include <sys/device.h>
    106      1.1  hikaru #include <sys/kernel.h>
    107      1.1  hikaru #include <sys/rndsource.h>
    108  1.2.4.1  martin #include <sys/systm.h>
    109      1.1  hikaru 
    110      1.1  hikaru #include <mips/locore.h>
    111      1.1  hikaru #include <mips/cavium/include/iobusvar.h>
    112      1.1  hikaru #include <mips/cavium/dev/octeon_rnmreg.h>
    113      1.1  hikaru #include <mips/cavium/dev/octeon_corereg.h>
    114      1.1  hikaru #include <mips/cavium/octeonvar.h>
    115      1.1  hikaru 
    116      1.1  hikaru #include <sys/bus.h>
    117      1.1  hikaru 
    118  1.2.4.1  martin //#define	OCTEON_RNM_DEBUG
    119      1.1  hikaru 
    120  1.2.4.1  martin #define	ENT_DELAY_CLOCK 8	/* cycles for each 64-bit RO sample batch */
    121  1.2.4.1  martin #define	RNG_DELAY_CLOCK 81	/* cycles for each SHA-1 output */
    122  1.2.4.1  martin #define	NROGROUPS	16
    123  1.2.4.1  martin #define	RNG_FIFO_WORDS	(512/sizeof(uint64_t))
    124      1.1  hikaru 
    125      1.1  hikaru struct octeon_rnm_softc {
    126      1.1  hikaru 	bus_space_tag_t		sc_bust;
    127      1.1  hikaru 	bus_space_handle_t	sc_regh;
    128  1.2.4.1  martin 	kmutex_t		sc_lock;
    129      1.1  hikaru 	krndsource_t		sc_rndsrc;	/* /dev/random source */
    130  1.2.4.1  martin 	unsigned		sc_rogroup;
    131      1.1  hikaru };
    132      1.1  hikaru 
    133      1.1  hikaru static int octeon_rnm_match(device_t, struct cfdata *, void *);
    134      1.1  hikaru static void octeon_rnm_attach(device_t, device_t, void *);
    135  1.2.4.1  martin static void octeon_rnm_rng(size_t, void *);
    136  1.2.4.1  martin static void octeon_rnm_reset(struct octeon_rnm_softc *);
    137  1.2.4.1  martin static void octeon_rnm_conditioned_deterministic(struct octeon_rnm_softc *);
    138  1.2.4.1  martin static void octeon_rnm_conditioned_entropy(struct octeon_rnm_softc *);
    139  1.2.4.1  martin static void octeon_rnm_raw_entropy(struct octeon_rnm_softc *, unsigned);
    140  1.2.4.1  martin static uint64_t octeon_rnm_load(struct octeon_rnm_softc *);
    141  1.2.4.1  martin static void octeon_rnm_iobdma(struct octeon_rnm_softc *, uint64_t *, unsigned);
    142  1.2.4.1  martin static void octeon_rnm_delay(uint32_t);
    143      1.1  hikaru 
    144      1.1  hikaru CFATTACH_DECL_NEW(octeon_rnm, sizeof(struct octeon_rnm_softc),
    145      1.1  hikaru     octeon_rnm_match, octeon_rnm_attach, NULL, NULL);
    146      1.1  hikaru 
    147      1.1  hikaru static int
    148      1.1  hikaru octeon_rnm_match(device_t parent, struct cfdata *cf, void *aux)
    149      1.1  hikaru {
    150      1.1  hikaru 	struct iobus_attach_args *aa = aux;
    151      1.1  hikaru 
    152      1.1  hikaru 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
    153  1.2.4.1  martin 		return 0;
    154      1.1  hikaru 	if (cf->cf_unit != aa->aa_unitno)
    155  1.2.4.1  martin 		return 0;
    156  1.2.4.1  martin 	return 1;
    157      1.1  hikaru }
    158      1.1  hikaru 
    159      1.1  hikaru static void
    160      1.1  hikaru octeon_rnm_attach(device_t parent, device_t self, void *aux)
    161      1.1  hikaru {
    162      1.1  hikaru 	struct octeon_rnm_softc *sc = device_private(self);
    163      1.1  hikaru 	struct iobus_attach_args *aa = aux;
    164  1.2.4.1  martin 	uint64_t bist_status, sample, expected = UINT64_C(0xd654ff35fadf866b);
    165      1.1  hikaru 
    166      1.1  hikaru 	aprint_normal("\n");
    167      1.1  hikaru 
    168  1.2.4.1  martin 	/* Map the device registers, all two of them.  */
    169      1.1  hikaru 	sc->sc_bust = aa->aa_bust;
    170  1.2.4.1  martin 	if (bus_space_map(aa->aa_bust, aa->aa_unit->addr, RNM_SIZE,
    171  1.2.4.1  martin 	    0, &sc->sc_regh) != 0) {
    172  1.2.4.1  martin 		aprint_error_dev(self, "unable to map device\n");
    173  1.2.4.1  martin 		return;
    174  1.2.4.1  martin 	}
    175      1.1  hikaru 
    176  1.2.4.1  martin 	/* Verify that the built-in self-test succeeded.  */
    177  1.2.4.1  martin 	bist_status = bus_space_read_8(sc->sc_bust, sc->sc_regh,
    178  1.2.4.1  martin 	    RNM_BIST_STATUS_OFFSET);
    179  1.2.4.1  martin 	if (bist_status) {
    180  1.2.4.1  martin 		aprint_error_dev(self, "RNG built in self test failed: %#lx\n",
    181  1.2.4.1  martin 		    bist_status);
    182  1.2.4.1  martin 		return;
    183  1.2.4.1  martin 	}
    184      1.1  hikaru 
    185  1.2.4.1  martin 	/* Create a mutex to serialize access to the FIFO.  */
    186  1.2.4.1  martin 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    187      1.1  hikaru 
    188  1.2.4.1  martin 	/*
    189  1.2.4.1  martin 	 * Reset the core, enable the RNG engine without entropy, wait
    190  1.2.4.1  martin 	 * 81 cycles for it to produce a single sample, and draw the
    191  1.2.4.1  martin 	 * deterministic sample to test.
    192  1.2.4.1  martin 	 *
    193  1.2.4.1  martin 	 * XXX Verify that the output matches the SHA-1 computation
    194  1.2.4.1  martin 	 * described by the data sheet, not just a known answer.
    195  1.2.4.1  martin 	 */
    196  1.2.4.1  martin 	octeon_rnm_reset(sc);
    197  1.2.4.1  martin 	octeon_rnm_conditioned_deterministic(sc);
    198  1.2.4.1  martin 	octeon_rnm_delay(RNG_DELAY_CLOCK*1);
    199  1.2.4.1  martin 	sample = octeon_rnm_load(sc);
    200  1.2.4.1  martin 	if (sample != expected)
    201  1.2.4.1  martin 		aprint_error_dev(self, "self-test: read %016"PRIx64","
    202  1.2.4.1  martin 		    " expected %016"PRIx64, sample, expected);
    203  1.2.4.1  martin 
    204  1.2.4.1  martin 	/*
    205  1.2.4.1  martin 	 * Reset the core again to clear the FIFO, and enable the RNG
    206  1.2.4.1  martin 	 * engine with entropy exposed directly.  Start from the first
    207  1.2.4.1  martin 	 * group of ring oscillators; as we gather samples we will
    208  1.2.4.1  martin 	 * rotate through the rest of them.
    209  1.2.4.1  martin 	 */
    210  1.2.4.1  martin 	octeon_rnm_reset(sc);
    211  1.2.4.1  martin 	sc->sc_rogroup = 0;
    212  1.2.4.1  martin 	octeon_rnm_raw_entropy(sc, sc->sc_rogroup);
    213  1.2.4.1  martin 	octeon_rnm_delay(ENT_DELAY_CLOCK*RNG_FIFO_WORDS);
    214  1.2.4.1  martin 
    215  1.2.4.1  martin 	/* Attach the rndsource.  */
    216  1.2.4.1  martin 	rndsource_setcb(&sc->sc_rndsrc, octeon_rnm_rng, sc);
    217  1.2.4.2  martin 	rnd_attach_source(&sc->sc_rndsrc, device_xname(self), RND_TYPE_UNKNOWN,
    218  1.2.4.1  martin 	    RND_FLAG_DEFAULT | RND_FLAG_HASCB);
    219      1.1  hikaru }
    220      1.1  hikaru 
    221      1.1  hikaru static void
    222  1.2.4.1  martin octeon_rnm_rng(size_t nbytes, void *vsc)
    223      1.1  hikaru {
    224  1.2.4.1  martin 	const unsigned BPB = 256; /* bits of data per bit of entropy */
    225  1.2.4.1  martin 	uint64_t sample[32];
    226      1.1  hikaru 	struct octeon_rnm_softc *sc = vsc;
    227  1.2.4.1  martin 	size_t needed = NBBY*nbytes;
    228  1.2.4.1  martin 	unsigned i;
    229      1.1  hikaru 
    230  1.2.4.1  martin 	/* Sample the ring oscillators round-robin.  */
    231  1.2.4.1  martin 	mutex_enter(&sc->sc_lock);
    232  1.2.4.1  martin 	while (needed) {
    233      1.1  hikaru 		/*
    234  1.2.4.1  martin 		 * Switch to the next RO group once we drain the FIFO.
    235  1.2.4.1  martin 		 * By the time rnd_add_data is done, we will have
    236  1.2.4.1  martin 		 * processed all 512 bytes of the FIFO.  We assume it
    237  1.2.4.1  martin 		 * takes at least one cycle per byte (realistically,
    238  1.2.4.1  martin 		 * more like ~80cpb to draw from the FIFO and then
    239  1.2.4.1  martin 		 * process it with rnd_add_data), so there is no need
    240  1.2.4.1  martin 		 * for any other delays.
    241      1.1  hikaru 		 */
    242  1.2.4.1  martin 		sc->sc_rogroup++;
    243  1.2.4.1  martin 		sc->sc_rogroup %= NROGROUPS;
    244  1.2.4.1  martin 		octeon_rnm_raw_entropy(sc, sc->sc_rogroup);
    245  1.2.4.1  martin 
    246  1.2.4.1  martin 		/*
    247  1.2.4.1  martin 		 * Gather half the FIFO at a time -- we are limited to
    248  1.2.4.1  martin 		 * 256 bytes because of limits on the CVMSEG buffer.
    249  1.2.4.1  martin 		 */
    250  1.2.4.1  martin 		CTASSERT(sizeof sample == 256);
    251  1.2.4.1  martin 		CTASSERT(2*__arraycount(sample) == RNG_FIFO_WORDS);
    252  1.2.4.1  martin 		for (i = 0; i < 2; i++) {
    253  1.2.4.1  martin 			octeon_rnm_iobdma(sc, sample, __arraycount(sample));
    254  1.2.4.1  martin #ifdef OCTEON_RNM_DEBUG
    255  1.2.4.1  martin 			hexdump(printf, "rnm", sample, sizeof sample);
    256  1.2.4.1  martin #endif
    257  1.2.4.1  martin 			rnd_add_data_sync(&sc->sc_rndsrc, sample,
    258  1.2.4.1  martin 			    sizeof sample, NBBY*sizeof(sample)/BPB);
    259  1.2.4.1  martin 			needed -= MIN(needed, MAX(1, NBBY*sizeof(sample)/BPB));
    260  1.2.4.1  martin 		}
    261  1.2.4.1  martin 
    262  1.2.4.1  martin 		/* Yield if requested.  */
    263  1.2.4.1  martin 		if (__predict_false(curcpu()->ci_schedstate.spc_flags &
    264  1.2.4.1  martin 			SPCF_SHOULDYIELD)) {
    265  1.2.4.1  martin 			mutex_exit(&sc->sc_lock);
    266  1.2.4.1  martin 			preempt();
    267  1.2.4.1  martin 			mutex_enter(&sc->sc_lock);
    268  1.2.4.1  martin 		}
    269      1.1  hikaru 	}
    270  1.2.4.1  martin 	mutex_exit(&sc->sc_lock);
    271  1.2.4.1  martin 
    272  1.2.4.1  martin 	/* Zero the sample.  */
    273  1.2.4.1  martin 	explicit_memset(sample, 0, sizeof sample);
    274  1.2.4.1  martin }
    275  1.2.4.1  martin 
    276  1.2.4.1  martin /*
    277  1.2.4.1  martin  * octeon_rnm_reset(sc)
    278  1.2.4.1  martin  *
    279  1.2.4.1  martin  *	Reset the RNM unit, disabling it and clearing the FIFO.
    280  1.2.4.1  martin  */
    281  1.2.4.1  martin static void
    282  1.2.4.1  martin octeon_rnm_reset(struct octeon_rnm_softc *sc)
    283  1.2.4.1  martin {
    284  1.2.4.1  martin 
    285  1.2.4.1  martin 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
    286  1.2.4.1  martin 	    RNM_CTL_STATUS_RNG_RST|RNM_CTL_STATUS_RNM_RST);
    287  1.2.4.1  martin }
    288      1.1  hikaru 
    289  1.2.4.1  martin /*
    290  1.2.4.1  martin  * octeon_rnm_conditioned_deterministic(sc)
    291  1.2.4.1  martin  *
    292  1.2.4.1  martin  *	Switch the RNM unit into the deterministic LFSR/SHA-1 mode with
    293  1.2.4.1  martin  *	no entropy, for the next data loaded into the FIFO.
    294  1.2.4.1  martin  */
    295  1.2.4.1  martin static void
    296  1.2.4.1  martin octeon_rnm_conditioned_deterministic(struct octeon_rnm_softc *sc)
    297  1.2.4.1  martin {
    298  1.2.4.1  martin 
    299  1.2.4.1  martin 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
    300  1.2.4.1  martin 	    RNM_CTL_STATUS_RNG_EN);
    301  1.2.4.1  martin }
    302  1.2.4.1  martin 
    303  1.2.4.1  martin /*
    304  1.2.4.1  martin  * octeon_rnm_conditioned_entropy(sc)
    305  1.2.4.1  martin  *
    306  1.2.4.1  martin  *	Switch the RNM unit to generate ring oscillator samples
    307  1.2.4.1  martin  *	conditioned with an LFSR/SHA-1, for the next data loaded into
    308  1.2.4.1  martin  *	the FIFO.
    309  1.2.4.1  martin  */
    310  1.2.4.1  martin static void __unused
    311  1.2.4.1  martin octeon_rnm_conditioned_entropy(struct octeon_rnm_softc *sc)
    312  1.2.4.1  martin {
    313  1.2.4.1  martin 
    314  1.2.4.1  martin 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
    315  1.2.4.1  martin 	    RNM_CTL_STATUS_RNG_EN|RNM_CTL_STATUS_ENT_EN);
    316      1.1  hikaru }
    317      1.1  hikaru 
    318  1.2.4.1  martin /*
    319  1.2.4.1  martin  * octeon_rnm_raw_entropy(sc, rogroup)
    320  1.2.4.1  martin  *
    321  1.2.4.1  martin  *	Switch the RNM unit to generate raw ring oscillator samples
    322  1.2.4.1  martin  *	from the specified group of eight ring oscillator.
    323  1.2.4.1  martin  */
    324  1.2.4.1  martin static void
    325  1.2.4.1  martin octeon_rnm_raw_entropy(struct octeon_rnm_softc *sc, unsigned rogroup)
    326  1.2.4.1  martin {
    327  1.2.4.1  martin 	uint64_t ctl = 0;
    328  1.2.4.1  martin 
    329  1.2.4.1  martin 	ctl |= RNM_CTL_STATUS_RNG_EN;	/* enable FIFO */
    330  1.2.4.1  martin 	ctl |= RNM_CTL_STATUS_ENT_EN;	/* enable entropy source */
    331  1.2.4.1  martin 	ctl |= RNM_CTL_STATUS_EXP_ENT;	/* expose entropy without LFSR/SHA-1 */
    332  1.2.4.1  martin 	ctl |= __SHIFTIN(rogroup, RNM_CTL_STATUS_ENT_SEL_MASK);
    333  1.2.4.1  martin 
    334  1.2.4.1  martin 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
    335  1.2.4.1  martin 	    ctl);
    336  1.2.4.1  martin }
    337  1.2.4.1  martin 
    338  1.2.4.1  martin /*
    339  1.2.4.1  martin  * octeon_rnm_load(sc)
    340  1.2.4.1  martin  *
    341  1.2.4.1  martin  *	Load a single 64-bit word out of the FIFO.
    342  1.2.4.1  martin  */
    343  1.2.4.1  martin static uint64_t
    344      1.1  hikaru octeon_rnm_load(struct octeon_rnm_softc *sc)
    345      1.1  hikaru {
    346      1.1  hikaru 	uint64_t addr =
    347      1.1  hikaru 	    RNM_OPERATION_BASE_IO_BIT |
    348      1.1  hikaru 	    __BITS64_SET(RNM_OPERATION_BASE_MAJOR_DID, 0x08) |
    349      1.1  hikaru 	    __BITS64_SET(RNM_OPERATION_BASE_SUB_DID, 0x00);
    350      1.1  hikaru 
    351      1.1  hikaru 	return octeon_xkphys_read_8(addr);
    352      1.1  hikaru }
    353      1.1  hikaru 
    354  1.2.4.1  martin /*
    355  1.2.4.1  martin  * octeon_rnm_iobdma(sc, buf, nwords)
    356  1.2.4.1  martin  *
    357  1.2.4.1  martin  *	Load nwords, at most 32, out of the FIFO into buf.
    358  1.2.4.1  martin  */
    359  1.2.4.1  martin static void
    360  1.2.4.1  martin octeon_rnm_iobdma(struct octeon_rnm_softc *sc, uint64_t *buf, unsigned nwords)
    361      1.1  hikaru {
    362  1.2.4.1  martin 	size_t scraddr = OCTEON_CVMSEG_OFFSET(csm_rnm);
    363  1.2.4.1  martin 	uint64_t iobdma =
    364  1.2.4.1  martin 	    __SHIFTIN(scraddr/sizeof(uint64_t), IOBDMA_SCRADDR) |
    365  1.2.4.1  martin 	    __SHIFTIN(nwords, IOBDMA_LEN) |
    366  1.2.4.1  martin 	    __SHIFTIN(RNM_IOBDMA_MAJORDID, IOBDMA_MAJORDID) |
    367  1.2.4.1  martin 	    __SHIFTIN(RNM_IOBDMA_SUBDID, IOBDMA_SUBDID);
    368  1.2.4.1  martin 
    369  1.2.4.1  martin 	KASSERT(nwords < 256);	/* iobdma address restriction */
    370  1.2.4.1  martin 	KASSERT(nwords <= 32);	/* octeon_cvmseg_map limitation */
    371  1.2.4.1  martin 
    372  1.2.4.1  martin 	octeon_iobdma_write_8(iobdma);
    373  1.2.4.1  martin 	OCTEON_SYNCIOBDMA;
    374  1.2.4.1  martin 	for (; nwords --> 0; scraddr += 8)
    375  1.2.4.1  martin 		*buf++ = octeon_cvmseg_read_8(scraddr);
    376      1.1  hikaru }
    377      1.1  hikaru 
    378  1.2.4.1  martin /*
    379  1.2.4.1  martin  * octeon_rnm_delay(ncycles)
    380  1.2.4.1  martin  *
    381  1.2.4.1  martin  *	Wait ncycles, at most UINT32_MAX/2 so we behave reasonably even
    382  1.2.4.1  martin  *	if the cycle counter rolls over.
    383  1.2.4.1  martin  */
    384  1.2.4.1  martin static void
    385  1.2.4.1  martin octeon_rnm_delay(uint32_t ncycles)
    386      1.1  hikaru {
    387  1.2.4.1  martin 	uint32_t deadline = mips3_cp0_count_read() + ncycles;
    388      1.1  hikaru 
    389  1.2.4.1  martin 	KASSERT(ncycles <= UINT32_MAX/2);
    390      1.1  hikaru 
    391  1.2.4.1  martin 	while ((deadline - mips3_cp0_count_read()) < ncycles)
    392  1.2.4.1  martin 		continue;
    393      1.1  hikaru }
    394