1128059Smarkm/*-
2128059Smarkm * Copyright (c) 2004 Mark R V Murray
3128059Smarkm * All rights reserved.
4128059Smarkm *
5128059Smarkm * Redistribution and use in source and binary forms, with or without
6128059Smarkm * modification, are permitted provided that the following conditions
7128059Smarkm * are met:
8128059Smarkm * 1. Redistributions of source code must retain the above copyright
9128059Smarkm *    notice, this list of conditions and the following disclaimer
10128059Smarkm *    in this position and unchanged.
11128059Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12128059Smarkm *    notice, this list of conditions and the following disclaimer in the
13128059Smarkm *    documentation and/or other materials provided with the distribution.
14128059Smarkm *
15128059Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16128059Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17128059Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18128059Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19128059Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20128059Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21128059Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22128059Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23128059Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24128059Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25128059Smarkm *
26128059Smarkm */
27128059Smarkm
28128059Smarkm#include <sys/cdefs.h>
29128059Smarkm__FBSDID("$FreeBSD$");
30128059Smarkm
31240950Skib#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
32240950Skib#include "opt_cpu.h"
33240950Skib#endif
34240950Skib
35128059Smarkm#include <sys/types.h>
36146797Sscottl#include <sys/param.h>
37240950Skib#include <sys/systm.h>
38240950Skib#include <sys/kernel.h>
39128059Smarkm#include <sys/malloc.h>
40128059Smarkm#include <sys/random.h>
41128059Smarkm#include <sys/selinfo.h>
42128059Smarkm#include <sys/sysctl.h>
43128059Smarkm
44232013Sjkim#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
45128059Smarkm#include <machine/cpufunc.h>
46141405Siedowse#include <machine/cputypes.h>
47160311Smr#include <machine/md_var.h>
48160311Smr#include <machine/specialreg.h>
49128059Smarkm#endif
50128059Smarkm
51128059Smarkm#include <dev/random/randomdev.h>
52128059Smarkm#include <dev/random/randomdev_soft.h>
53128059Smarkm
54240950Skib#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
55240950Skib#ifdef PADLOCK_RNG
56240950Skibextern struct random_systat random_nehemiah;
57240950Skib#endif
58240950Skib#ifdef RDRAND_RNG
59240950Skibextern struct random_systat random_ivy;
60240950Skib#endif
61240950Skib#endif
62240950Skib
63128059Smarkmvoid
64128059Smarkmrandom_ident_hardware(struct random_systat *systat)
65128059Smarkm{
66128059Smarkm
67128059Smarkm	/* Set default to software */
68128059Smarkm	*systat = random_yarrow;
69128059Smarkm
70128059Smarkm	/* Then go looking for hardware */
71232013Sjkim#if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
72240950Skib#ifdef PADLOCK_RNG
73160326Smr	if (via_feature_rng & VIA_HAS_RNG) {
74240950Skib		int enable;
75240950Skib
76260644Sdelphij		enable = 0;
77240950Skib		TUNABLE_INT_FETCH("hw.nehemiah_rng_enable", &enable);
78240950Skib		if (enable)
79240950Skib			*systat = random_nehemiah;
80128059Smarkm	}
81128059Smarkm#endif
82240950Skib#ifdef RDRAND_RNG
83240950Skib	if (cpu_feature2 & CPUID2_RDRAND) {
84240950Skib		int enable;
85240950Skib
86260644Sdelphij		enable = 0;
87240950Skib		TUNABLE_INT_FETCH("hw.ivy_rng_enable", &enable);
88240950Skib		if (enable)
89240950Skib			*systat = random_ivy;
90240950Skib	}
91240950Skib#endif
92240950Skib#endif
93128059Smarkm}
94