xref: /netbsd-src/sys/arch/mips/cavium/dev/octeon_rnm.c (revision ec33c2331e32e794ea26f05e8ce39ece740d5474)
1*ec33c233Sriastradh /*	$NetBSD: octeon_rnm.c,v 1.16 2023/03/21 22:07:29 riastradh Exp $	*/
2f693c922Shikaru 
3f693c922Shikaru /*
4f693c922Shikaru  * Copyright (c) 2007 Internet Initiative Japan, Inc.
5f693c922Shikaru  * All rights reserved.
6f693c922Shikaru  *
7f693c922Shikaru  * Redistribution and use in source and binary forms, with or without
8f693c922Shikaru  * modification, are permitted provided that the following conditions
9f693c922Shikaru  * are met:
10f693c922Shikaru  * 1. Redistributions of source code must retain the above copyright
11f693c922Shikaru  *    notice, this list of conditions and the following disclaimer.
12f693c922Shikaru  * 2. Redistributions in binary form must reproduce the above copyright
13f693c922Shikaru  *    notice, this list of conditions and the following disclaimer in the
14f693c922Shikaru  *    documentation and/or other materials provided with the distribution.
15f693c922Shikaru  *
16f693c922Shikaru  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17f693c922Shikaru  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f693c922Shikaru  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f693c922Shikaru  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20f693c922Shikaru  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f693c922Shikaru  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f693c922Shikaru  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f693c922Shikaru  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f693c922Shikaru  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f693c922Shikaru  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f693c922Shikaru  * SUCH DAMAGE.
27f693c922Shikaru  */
28f693c922Shikaru 
294e0bb03bSriastradh /*
304e0bb03bSriastradh  * Cavium Octeon Random Number Generator / Random Number Memory `RNM'
314e0bb03bSriastradh  *
324e0bb03bSriastradh  *	The RNM unit consists of:
334e0bb03bSriastradh  *
344e0bb03bSriastradh  *	1. 128 ring oscillators
354e0bb03bSriastradh  *	2. an LFSR/SHA-1 conditioner
364e0bb03bSriastradh  *	3. a 512-byte FIFO
374e0bb03bSriastradh  *
384e0bb03bSriastradh  *	When the unit is enabled, there are three modes of operation:
394e0bb03bSriastradh  *
404e0bb03bSriastradh  *	(a) deterministic: the ring oscillators are disabled and the
414e0bb03bSriastradh  *	    LFSR/SHA-1 conditioner operates on fixed inputs to give
424e0bb03bSriastradh  *	    reproducible results for testing,
434e0bb03bSriastradh  *
444e0bb03bSriastradh  *	(b) conditioned entropy: the ring oscillators are enabled and
454e0bb03bSriastradh  *	    samples from them are fed through the LFSR/SHA-1
464e0bb03bSriastradh  *	    conditioner before being put into the FIFO, and
474e0bb03bSriastradh  *
484e0bb03bSriastradh  *	(c) raw entropy: the ring oscillators are enabled, and a group
494e0bb03bSriastradh  *	    of eight of them selected at any one time is sampled and
504e0bb03bSriastradh  *	    fed into the FIFO.
514e0bb03bSriastradh  *
524e0bb03bSriastradh  *	Details:
534e0bb03bSriastradh  *
544e0bb03bSriastradh  *	- The FIFO is refilled whenever we read out of it, either with
554e0bb03bSriastradh  *	  a load address or an IOBDMA operation.
564e0bb03bSriastradh  *
574e0bb03bSriastradh  *	- The conditioner takes 81 cycles to produce a 64-bit block of
584e0bb03bSriastradh  *	  output in the FIFO whether in deterministic or conditioned
594e0bb03bSriastradh  *	  entropy mode, each block consisting of the first 64 bits of a
604e0bb03bSriastradh  *	  SHA-1 hash.
614e0bb03bSriastradh  *
624e0bb03bSriastradh  *	- A group of eight ring oscillators take 8 cycles to produce a
634e0bb03bSriastradh  *	  64-bit block of output in the FIFO in raw entropy mode, each
644e0bb03bSriastradh  *	  block consisting of eight consecutive samples from each RO in
654e0bb03bSriastradh  *	  parallel.
664e0bb03bSriastradh  *
674e0bb03bSriastradh  *	The first sample of each RO always seems to be zero.  Further,
684e0bb03bSriastradh  *	consecutive samples from a single ring oscillator are not
694e0bb03bSriastradh  *	independent, so naive debiasing like a von Neumann extractor
709b4b6c52Sriastradh  *	falls flat on its face.  And parallel ring oscillators powered
719b4b6c52Sriastradh  *	by the same source may not be independent either, if they end
729b4b6c52Sriastradh  *	up locked.
734e0bb03bSriastradh  *
749b4b6c52Sriastradh  *	We read out one FIFO's worth of raw samples from groups of 8
759b4b6c52Sriastradh  *	ring oscillators at a time, of 128 total, by going through them
769b4b6c52Sriastradh  *	round robin.  We take 32 consecutive samples from each ring
779b4b6c52Sriastradh  *	oscillator in a group of 8 in parallel before we count one bit
789b4b6c52Sriastradh  *	of entropy.  To get 256 bits of entropy, we read 4Kbit of data
799b4b6c52Sriastradh  *	from each of two 8-RO groups.
804e0bb03bSriastradh  *
819b4b6c52Sriastradh  *	We could use the on-board LFSR/SHA-1 conditioner like the Linux
829b4b6c52Sriastradh  *	driver written by Cavium does, but it's not clear how many RO
839b4b6c52Sriastradh  *	samples go into the conditioner, and our entropy pool is a
849b4b6c52Sriastradh  *	perfectly good conditioner itself, so it seems there is little
859b4b6c52Sriastradh  *	advantage -- other than expedience -- to using the LFSR/SHA-1
869b4b6c52Sriastradh  *	conditioner.  All the manual says is that it samples 125 of the
879b4b6c52Sriastradh  *	128 ROs.  But the Cavium SHA-1 CPU instruction is advertised to
889b4b6c52Sriastradh  *	have a latency of 100 cycles, so it seems implausible that much
899b4b6c52Sriastradh  *	more than one sample from each RO could be squeezed in there.
909b4b6c52Sriastradh  *
919b4b6c52Sriastradh  *	The hardware exposes only 64 bits of each SHA-1 hash, and the
929b4b6c52Sriastradh  *	Linux driver uses 32 bits of that -- which, if treated as full
939b4b6c52Sriastradh  *	entropy, would mean an assessment of 3.9 bits of RO samples to
949b4b6c52Sriastradh  *	get 1 bit of entropy, whereas we take 256 bits of RO samples to
959b4b6c52Sriastradh  *	get one bit of entropy, so this seems reasonably conservative.
964e0bb03bSriastradh  *
974e0bb03bSriastradh  * Reference: Cavium Networks OCTEON Plus CN50XX Hardware Reference
984e0bb03bSriastradh  * Manual, CN50XX-HM-0.99E PRELIMINARY, July 2008.
994e0bb03bSriastradh  */
1004e0bb03bSriastradh 
101f693c922Shikaru #include <sys/cdefs.h>
102*ec33c233Sriastradh __KERNEL_RCSID(0, "$NetBSD: octeon_rnm.c,v 1.16 2023/03/21 22:07:29 riastradh Exp $");
103f693c922Shikaru 
104f693c922Shikaru #include <sys/param.h>
105f693c922Shikaru #include <sys/device.h>
106f693c922Shikaru #include <sys/kernel.h>
107f693c922Shikaru #include <sys/rndsource.h>
108be355864Ssimonb #include <sys/systm.h>
109f693c922Shikaru 
110f693c922Shikaru #include <mips/locore.h>
111b9fcd28bSsimonb #include <mips/cavium/octeonreg.h>
112b9fcd28bSsimonb #include <mips/cavium/octeonvar.h>
113f693c922Shikaru #include <mips/cavium/include/iobusvar.h>
114f693c922Shikaru #include <mips/cavium/dev/octeon_rnmreg.h>
115f693c922Shikaru #include <mips/cavium/dev/octeon_corereg.h>
116f693c922Shikaru 
117f693c922Shikaru #include <sys/bus.h>
118f693c922Shikaru 
1193f508e4dSsimonb //#define	OCTRNM_DEBUG
1204e0bb03bSriastradh 
1214e0bb03bSriastradh #define	ENT_DELAY_CLOCK 8	/* cycles for each 64-bit RO sample batch */
122*ec33c233Sriastradh #define	LFSR_DELAY_CLOCK 81	/* cycles to fill LFSR buffer */
123*ec33c233Sriastradh #define	SHA1_DELAY_CLOCK 81	/* cycles to compute SHA-1 output */
1244e0bb03bSriastradh #define	NROGROUPS	16
1254e0bb03bSriastradh #define	RNG_FIFO_WORDS	(512/sizeof(uint64_t))
126f693c922Shikaru 
1273f508e4dSsimonb struct octrnm_softc {
128fed1041eSsimonb 	uint64_t		sc_sample[RNG_FIFO_WORDS];
129f693c922Shikaru 	bus_space_tag_t		sc_bust;
130f693c922Shikaru 	bus_space_handle_t	sc_regh;
131f693c922Shikaru 	krndsource_t		sc_rndsrc;	/* /dev/random source */
1324e0bb03bSriastradh 	unsigned		sc_rogroup;
133f693c922Shikaru };
134f693c922Shikaru 
1353f508e4dSsimonb static int octrnm_match(device_t, struct cfdata *, void *);
1363f508e4dSsimonb static void octrnm_attach(device_t, device_t, void *);
1373f508e4dSsimonb static void octrnm_rng(size_t, void *);
1383f508e4dSsimonb static void octrnm_reset(struct octrnm_softc *);
1393f508e4dSsimonb static void octrnm_conditioned_deterministic(struct octrnm_softc *);
1403f508e4dSsimonb static void octrnm_conditioned_entropy(struct octrnm_softc *);
1413f508e4dSsimonb static void octrnm_raw_entropy(struct octrnm_softc *, unsigned);
1423f508e4dSsimonb static uint64_t octrnm_load(struct octrnm_softc *);
1433f508e4dSsimonb static void octrnm_iobdma(struct octrnm_softc *, uint64_t *, unsigned);
1443f508e4dSsimonb static void octrnm_delay(uint32_t);
145f693c922Shikaru 
1463f508e4dSsimonb CFATTACH_DECL_NEW(octrnm, sizeof(struct octrnm_softc),
1473f508e4dSsimonb     octrnm_match, octrnm_attach, NULL, NULL);
148f693c922Shikaru 
149f693c922Shikaru static int
octrnm_match(device_t parent,struct cfdata * cf,void * aux)1503f508e4dSsimonb octrnm_match(device_t parent, struct cfdata *cf, void *aux)
151f693c922Shikaru {
152f693c922Shikaru 	struct iobus_attach_args *aa = aux;
153f693c922Shikaru 
154f693c922Shikaru 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
1554e0bb03bSriastradh 		return 0;
156f693c922Shikaru 	if (cf->cf_unit != aa->aa_unitno)
1574e0bb03bSriastradh 		return 0;
1584e0bb03bSriastradh 	return 1;
159f693c922Shikaru }
160f693c922Shikaru 
161f693c922Shikaru static void
octrnm_attach(device_t parent,device_t self,void * aux)1623f508e4dSsimonb octrnm_attach(device_t parent, device_t self, void *aux)
163f693c922Shikaru {
1643f508e4dSsimonb 	struct octrnm_softc *sc = device_private(self);
165f693c922Shikaru 	struct iobus_attach_args *aa = aux;
1664e0bb03bSriastradh 	uint64_t bist_status, sample, expected = UINT64_C(0xd654ff35fadf866b);
167f693c922Shikaru 
168f693c922Shikaru 	aprint_normal("\n");
169f693c922Shikaru 
1704e0bb03bSriastradh 	/* Map the device registers, all two of them.  */
171f693c922Shikaru 	sc->sc_bust = aa->aa_bust;
172ec1a4f37Ssimonb 	if (bus_space_map(aa->aa_bust, aa->aa_unit->addr, RNM_SIZE,
173ec1a4f37Ssimonb 	    0, &sc->sc_regh) != 0) {
174ec1a4f37Ssimonb 		aprint_error_dev(self, "unable to map device\n");
175ec1a4f37Ssimonb 		return;
176ec1a4f37Ssimonb 	}
177ec1a4f37Ssimonb 
1784e0bb03bSriastradh 	/* Verify that the built-in self-test succeeded.  */
179ec1a4f37Ssimonb 	bist_status = bus_space_read_8(sc->sc_bust, sc->sc_regh,
180ec1a4f37Ssimonb 	    RNM_BIST_STATUS_OFFSET);
181ec1a4f37Ssimonb 	if (bist_status) {
182ec1a4f37Ssimonb 		aprint_error_dev(self, "RNG built in self test failed: %#lx\n",
183ec1a4f37Ssimonb 		    bist_status);
184ec1a4f37Ssimonb 		return;
185ec1a4f37Ssimonb 	}
186f693c922Shikaru 
187be355864Ssimonb 	/*
1884e0bb03bSriastradh 	 * Reset the core, enable the RNG engine without entropy, wait
1894e0bb03bSriastradh 	 * 81 cycles for it to produce a single sample, and draw the
1904e0bb03bSriastradh 	 * deterministic sample to test.
191be355864Ssimonb 	 *
1924e0bb03bSriastradh 	 * XXX Verify that the output matches the SHA-1 computation
1934e0bb03bSriastradh 	 * described by the data sheet, not just a known answer.
194be355864Ssimonb 	 */
1953f508e4dSsimonb 	octrnm_reset(sc);
1963f508e4dSsimonb 	octrnm_conditioned_deterministic(sc);
197*ec33c233Sriastradh 	octrnm_delay(LFSR_DELAY_CLOCK + SHA1_DELAY_CLOCK);
1983f508e4dSsimonb 	sample = octrnm_load(sc);
1994e0bb03bSriastradh 	if (sample != expected)
2004e0bb03bSriastradh 		aprint_error_dev(self, "self-test: read %016"PRIx64","
2014e0bb03bSriastradh 		    " expected %016"PRIx64, sample, expected);
202be355864Ssimonb 
203be355864Ssimonb 	/*
2044e0bb03bSriastradh 	 * Reset the core again to clear the FIFO, and enable the RNG
2054e0bb03bSriastradh 	 * engine with entropy exposed directly.  Start from the first
2064e0bb03bSriastradh 	 * group of ring oscillators; as we gather samples we will
2074e0bb03bSriastradh 	 * rotate through the rest of them.
208be355864Ssimonb 	 */
2093f508e4dSsimonb 	octrnm_reset(sc);
2104e0bb03bSriastradh 	sc->sc_rogroup = 0;
2113f508e4dSsimonb 	octrnm_raw_entropy(sc, sc->sc_rogroup);
2123f508e4dSsimonb 	octrnm_delay(ENT_DELAY_CLOCK*RNG_FIFO_WORDS);
213f693c922Shikaru 
2144e0bb03bSriastradh 	/* Attach the rndsource.  */
2153f508e4dSsimonb 	rndsource_setcb(&sc->sc_rndsrc, octrnm_rng, sc);
216be355864Ssimonb 	rnd_attach_source(&sc->sc_rndsrc, device_xname(self), RND_TYPE_RNG,
217be355864Ssimonb 	    RND_FLAG_DEFAULT | RND_FLAG_HASCB);
218f693c922Shikaru }
219f693c922Shikaru 
220f693c922Shikaru static void
octrnm_rng(size_t nbytes,void * vsc)2213f508e4dSsimonb octrnm_rng(size_t nbytes, void *vsc)
222f693c922Shikaru {
2239b4b6c52Sriastradh 	const unsigned BPB = 256; /* bits of data per bit of entropy */
2243f508e4dSsimonb 	struct octrnm_softc *sc = vsc;
225fed1041eSsimonb 	uint64_t *samplepos;
2264e0bb03bSriastradh 	size_t needed = NBBY*nbytes;
2274e0bb03bSriastradh 	unsigned i;
228f693c922Shikaru 
2294e0bb03bSriastradh 	/* Sample the ring oscillators round-robin.  */
2304e0bb03bSriastradh 	while (needed) {
231f693c922Shikaru 		/*
2324e0bb03bSriastradh 		 * Switch to the next RO group once we drain the FIFO.
2334e0bb03bSriastradh 		 * By the time rnd_add_data is done, we will have
2344e0bb03bSriastradh 		 * processed all 512 bytes of the FIFO.  We assume it
2354e0bb03bSriastradh 		 * takes at least one cycle per byte (realistically,
2364e0bb03bSriastradh 		 * more like ~80cpb to draw from the FIFO and then
2374e0bb03bSriastradh 		 * process it with rnd_add_data), so there is no need
2384e0bb03bSriastradh 		 * for any other delays.
239f693c922Shikaru 		 */
2404e0bb03bSriastradh 		sc->sc_rogroup++;
2414e0bb03bSriastradh 		sc->sc_rogroup %= NROGROUPS;
2423f508e4dSsimonb 		octrnm_raw_entropy(sc, sc->sc_rogroup);
2434e0bb03bSriastradh 
2444e0bb03bSriastradh 		/*
245fed1041eSsimonb 		 * Gather quarter the FIFO at a time -- we are limited
246fed1041eSsimonb 		 * to 128 bytes because of limits on the CVMSEG buffer.
2474e0bb03bSriastradh 		 */
248fed1041eSsimonb 		CTASSERT(sizeof sc->sc_sample == 512);
249fed1041eSsimonb 		CTASSERT(__arraycount(sc->sc_sample) == RNG_FIFO_WORDS);
250fed1041eSsimonb 		for (samplepos = sc->sc_sample, i = 0; i < 4; i++) {
2513f508e4dSsimonb 			octrnm_iobdma(sc, samplepos, RNG_FIFO_WORDS / 4);
252fed1041eSsimonb 			samplepos += RNG_FIFO_WORDS / 4;
2534e0bb03bSriastradh 		}
2543f508e4dSsimonb #ifdef OCTRNM_DEBUG
255fed1041eSsimonb 		hexdump(printf, "rnm", sc->sc_sample, sizeof sc->sc_sample);
256fed1041eSsimonb #endif
257fed1041eSsimonb 		rnd_add_data_sync(&sc->sc_rndsrc, sc->sc_sample,
258fed1041eSsimonb 		    sizeof sc->sc_sample, NBBY*sizeof(sc->sc_sample)/BPB);
259fed1041eSsimonb 		needed -= MIN(needed, MAX(1, NBBY*sizeof(sc->sc_sample)/BPB));
2604e0bb03bSriastradh 
2618c13a1c7Sriastradh 		/* Now's a good time to yield.  */
2628c13a1c7Sriastradh 		preempt_point();
2634e0bb03bSriastradh 	}
2644e0bb03bSriastradh 
2654e0bb03bSriastradh 	/* Zero the sample.  */
266fed1041eSsimonb 	explicit_memset(sc->sc_sample, 0, sizeof sc->sc_sample);
267f693c922Shikaru }
268f693c922Shikaru 
2694e0bb03bSriastradh /*
2703f508e4dSsimonb  * octrnm_reset(sc)
2714e0bb03bSriastradh  *
2724e0bb03bSriastradh  *	Reset the RNM unit, disabling it and clearing the FIFO.
2734e0bb03bSriastradh  */
2744e0bb03bSriastradh static void
octrnm_reset(struct octrnm_softc * sc)2753f508e4dSsimonb octrnm_reset(struct octrnm_softc *sc)
2764e0bb03bSriastradh {
2774e0bb03bSriastradh 
2784e0bb03bSriastradh 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
2794e0bb03bSriastradh 	    RNM_CTL_STATUS_RNG_RST|RNM_CTL_STATUS_RNM_RST);
2804e0bb03bSriastradh }
2814e0bb03bSriastradh 
2824e0bb03bSriastradh /*
2833f508e4dSsimonb  * octrnm_conditioned_deterministic(sc)
2844e0bb03bSriastradh  *
2854e0bb03bSriastradh  *	Switch the RNM unit into the deterministic LFSR/SHA-1 mode with
2864e0bb03bSriastradh  *	no entropy, for the next data loaded into the FIFO.
2874e0bb03bSriastradh  */
2884e0bb03bSriastradh static void
octrnm_conditioned_deterministic(struct octrnm_softc * sc)2893f508e4dSsimonb octrnm_conditioned_deterministic(struct octrnm_softc *sc)
2904e0bb03bSriastradh {
2914e0bb03bSriastradh 
2924e0bb03bSriastradh 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
2934e0bb03bSriastradh 	    RNM_CTL_STATUS_RNG_EN);
2944e0bb03bSriastradh }
2954e0bb03bSriastradh 
2964e0bb03bSriastradh /*
2973f508e4dSsimonb  * octrnm_conditioned_entropy(sc)
2984e0bb03bSriastradh  *
2994e0bb03bSriastradh  *	Switch the RNM unit to generate ring oscillator samples
3004e0bb03bSriastradh  *	conditioned with an LFSR/SHA-1, for the next data loaded into
3014e0bb03bSriastradh  *	the FIFO.
3024e0bb03bSriastradh  */
3034e0bb03bSriastradh static void __unused
octrnm_conditioned_entropy(struct octrnm_softc * sc)3043f508e4dSsimonb octrnm_conditioned_entropy(struct octrnm_softc *sc)
3054e0bb03bSriastradh {
3064e0bb03bSriastradh 
3074e0bb03bSriastradh 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
3084e0bb03bSriastradh 	    RNM_CTL_STATUS_RNG_EN|RNM_CTL_STATUS_ENT_EN);
3094e0bb03bSriastradh }
3104e0bb03bSriastradh 
3114e0bb03bSriastradh /*
3123f508e4dSsimonb  * octrnm_raw_entropy(sc, rogroup)
3134e0bb03bSriastradh  *
3144e0bb03bSriastradh  *	Switch the RNM unit to generate raw ring oscillator samples
3154e0bb03bSriastradh  *	from the specified group of eight ring oscillator.
3164e0bb03bSriastradh  */
3174e0bb03bSriastradh static void
octrnm_raw_entropy(struct octrnm_softc * sc,unsigned rogroup)3183f508e4dSsimonb octrnm_raw_entropy(struct octrnm_softc *sc, unsigned rogroup)
3194e0bb03bSriastradh {
3204e0bb03bSriastradh 	uint64_t ctl = 0;
3214e0bb03bSriastradh 
3224e0bb03bSriastradh 	ctl |= RNM_CTL_STATUS_RNG_EN;	/* enable FIFO */
3234e0bb03bSriastradh 	ctl |= RNM_CTL_STATUS_ENT_EN;	/* enable entropy source */
3244e0bb03bSriastradh 	ctl |= RNM_CTL_STATUS_EXP_ENT;	/* expose entropy without LFSR/SHA-1 */
3254e0bb03bSriastradh 	ctl |= __SHIFTIN(rogroup, RNM_CTL_STATUS_ENT_SEL_MASK);
3264e0bb03bSriastradh 
3274e0bb03bSriastradh 	bus_space_write_8(sc->sc_bust, sc->sc_regh, RNM_CTL_STATUS_OFFSET,
3284e0bb03bSriastradh 	    ctl);
3294e0bb03bSriastradh }
3304e0bb03bSriastradh 
3314e0bb03bSriastradh /*
3323f508e4dSsimonb  * octrnm_load(sc)
3334e0bb03bSriastradh  *
3344e0bb03bSriastradh  *	Load a single 64-bit word out of the FIFO.
3354e0bb03bSriastradh  */
336be355864Ssimonb static uint64_t
octrnm_load(struct octrnm_softc * sc)3373f508e4dSsimonb octrnm_load(struct octrnm_softc *sc)
338f693c922Shikaru {
339b9fcd28bSsimonb 	uint64_t addr = OCTEON_ADDR_IO_DID(RNM_MAJOR_DID, RNM_SUB_DID);
340f693c922Shikaru 
341f693c922Shikaru 	return octeon_xkphys_read_8(addr);
342f693c922Shikaru }
3434e0bb03bSriastradh 
3444e0bb03bSriastradh /*
3453f508e4dSsimonb  * octrnm_iobdma(sc, buf, nwords)
3464e0bb03bSriastradh  *
3474e0bb03bSriastradh  *	Load nwords, at most 32, out of the FIFO into buf.
3484e0bb03bSriastradh  */
3494e0bb03bSriastradh static void
octrnm_iobdma(struct octrnm_softc * sc,uint64_t * buf,unsigned nwords)3503f508e4dSsimonb octrnm_iobdma(struct octrnm_softc *sc, uint64_t *buf, unsigned nwords)
3514e0bb03bSriastradh {
352b9fcd28bSsimonb  	/* ``scraddr'' part is index in 64-bit words, not address */
3534e0bb03bSriastradh 	size_t scraddr = OCTEON_CVMSEG_OFFSET(csm_rnm);
354b9fcd28bSsimonb 	uint64_t iobdma = IOBDMA_CREATE(RNM_MAJOR_DID, RNM_SUB_DID,
355b9fcd28bSsimonb 	    scraddr / sizeof(uint64_t), nwords, 0);
3564e0bb03bSriastradh 
357fed1041eSsimonb 	KASSERT(nwords < 128);			/* iobdma address restriction */
3581778ca0fSsimonb 	KASSERT(nwords <= CVMSEG_LM_RNM_SIZE);	/* size of CVMSEG LM buffer */
3594e0bb03bSriastradh 
3604e0bb03bSriastradh 	octeon_iobdma_write_8(iobdma);
3614e0bb03bSriastradh 	OCTEON_SYNCIOBDMA;
3624e0bb03bSriastradh 	for (; nwords --> 0; scraddr += 8)
3634e0bb03bSriastradh 		*buf++ = octeon_cvmseg_read_8(scraddr);
3644e0bb03bSriastradh }
3654e0bb03bSriastradh 
3664e0bb03bSriastradh /*
3673f508e4dSsimonb  * octrnm_delay(ncycles)
3684e0bb03bSriastradh  *
3694e0bb03bSriastradh  *	Wait ncycles, at most UINT32_MAX/2 so we behave reasonably even
3704e0bb03bSriastradh  *	if the cycle counter rolls over.
3714e0bb03bSriastradh  */
3724e0bb03bSriastradh static void
octrnm_delay(uint32_t ncycles)3733f508e4dSsimonb octrnm_delay(uint32_t ncycles)
3744e0bb03bSriastradh {
3754e0bb03bSriastradh 	uint32_t deadline = mips3_cp0_count_read() + ncycles;
3764e0bb03bSriastradh 
3774e0bb03bSriastradh 	KASSERT(ncycles <= UINT32_MAX/2);
3784e0bb03bSriastradh 
3794e0bb03bSriastradh 	while ((deadline - mips3_cp0_count_read()) < ncycles)
3804e0bb03bSriastradh 		continue;
3814e0bb03bSriastradh }
382