2006-06-26 11:24:59 +04:00
/*
* RNG driver for Intel RNGs
*
* Copyright 2005 ( c ) MontaVista Software , Inc .
*
* with the majority of the code coming from :
*
* Hardware driver for the Intel / AMD / VIA Random Number Generators ( RNG )
* ( c ) Copyright 2003 Red Hat Inc < jgarzik @ redhat . com >
*
* derived from
*
* Hardware driver for the AMD 768 Random Number Generator ( RNG )
2008-10-27 18:10:23 +03:00
* ( c ) Copyright 2001 Red Hat Inc
2006-06-26 11:24:59 +04:00
*
* derived from
*
* Hardware driver for Intel i810 Random Number Generator ( RNG )
* Copyright 2000 , 2001 Jeff Garzik < jgarzik @ pobox . com >
* Copyright 2000 , 2001 Philipp Rumpf < prumpf @ mandrakesoft . com >
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed " as is " without any
* warranty of any kind , whether express or implied .
*/
2007-05-08 11:25:08 +04:00
# include <linux/hw_random.h>
2006-06-26 11:24:59 +04:00
# include <linux/kernel.h>
2007-05-08 11:25:08 +04:00
# include <linux/module.h>
2006-06-26 11:24:59 +04:00
# include <linux/pci.h>
2007-05-08 11:25:08 +04:00
# include <linux/stop_machine.h>
2007-11-21 07:24:45 +03:00
# include <linux/delay.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2006-06-26 11:24:59 +04:00
# include <asm/io.h>
# define PFX KBUILD_MODNAME ": "
/*
* RNG registers
*/
# define INTEL_RNG_HW_STATUS 0
# define INTEL_RNG_PRESENT 0x40
# define INTEL_RNG_ENABLED 0x01
# define INTEL_RNG_STATUS 1
# define INTEL_RNG_DATA_PRESENT 0x01
# define INTEL_RNG_DATA 2
/*
* Magic address at which Intel PCI bridges locate the RNG
*/
# define INTEL_RNG_ADDR 0xFFBC015F
# define INTEL_RNG_ADDR_LEN 3
2006-09-29 12:59:42 +04:00
/*
* LPC bridge PCI config space registers
*/
# define FWH_DEC_EN1_REG_OLD 0xe3
# define FWH_DEC_EN1_REG_NEW 0xd9 /* high byte of 16-bit register */
# define FWH_F8_EN_MASK 0x80
# define BIOS_CNTL_REG_OLD 0x4e
# define BIOS_CNTL_REG_NEW 0xdc
# define BIOS_CNTL_WRITE_ENABLE_MASK 0x01
# define BIOS_CNTL_LOCK_ENABLE_MASK 0x02
/*
* Magic address at which Intel Firmware Hubs get accessed
*/
# define INTEL_FWH_ADDR 0xffff0000
# define INTEL_FWH_ADDR_LEN 2
/*
* Intel Firmware Hub command codes ( write to any address inside the device )
*/
# define INTEL_FWH_RESET_CMD 0xff /* aka READ_ARRAY */
# define INTEL_FWH_READ_ID_CMD 0x90
/*
* Intel Firmware Hub Read ID command result addresses
*/
# define INTEL_FWH_MANUFACTURER_CODE_ADDRESS 0x000000
# define INTEL_FWH_DEVICE_CODE_ADDRESS 0x000001
/*
* Intel Firmware Hub Read ID command result values
*/
# define INTEL_FWH_MANUFACTURER_CODE 0x89
# define INTEL_FWH_DEVICE_CODE_8M 0xac
# define INTEL_FWH_DEVICE_CODE_4M 0xad
2006-06-26 11:24:59 +04:00
/*
* Data for PCI driver interface
*
* This data only exists for exporting the supported
* PCI ids via MODULE_DEVICE_TABLE . We do not actually
* register a pci_driver , because someone else might one day
* want to register another driver on the same PCI id .
*/
static const struct pci_device_id pci_tbl [ ] = {
2006-09-29 12:59:42 +04:00
/* AA
2007-02-12 11:51:48 +03:00
{ PCI_DEVICE ( 0x8086 , 0x2418 ) } , */
{ PCI_DEVICE ( 0x8086 , 0x2410 ) } , /* AA */
2006-09-29 12:59:42 +04:00
/* AB
2007-02-12 11:51:48 +03:00
{ PCI_DEVICE ( 0x8086 , 0x2428 ) } , */
{ PCI_DEVICE ( 0x8086 , 0x2420 ) } , /* AB */
2006-09-29 12:59:42 +04:00
/* ??
2007-02-12 11:51:48 +03:00
{ PCI_DEVICE ( 0x8086 , 0x2430 ) } , */
2006-09-29 12:59:42 +04:00
/* BAM, CAM, DBM, FBM, GxM
2007-02-12 11:51:48 +03:00
{ PCI_DEVICE ( 0x8086 , 0x2448 ) } , */
{ PCI_DEVICE ( 0x8086 , 0x244c ) } , /* BAM */
{ PCI_DEVICE ( 0x8086 , 0x248c ) } , /* CAM */
{ PCI_DEVICE ( 0x8086 , 0x24cc ) } , /* DBM */
{ PCI_DEVICE ( 0x8086 , 0x2641 ) } , /* FBM */
{ PCI_DEVICE ( 0x8086 , 0x27b9 ) } , /* GxM */
{ PCI_DEVICE ( 0x8086 , 0x27bd ) } , /* GxM DH */
2006-09-29 12:59:42 +04:00
/* BA, CA, DB, Ex, 6300, Fx, 631x/632x, Gx
2007-02-12 11:51:48 +03:00
{ PCI_DEVICE ( 0x8086 , 0x244e ) } , */
{ PCI_DEVICE ( 0x8086 , 0x2440 ) } , /* BA */
{ PCI_DEVICE ( 0x8086 , 0x2480 ) } , /* CA */
{ PCI_DEVICE ( 0x8086 , 0x24c0 ) } , /* DB */
{ PCI_DEVICE ( 0x8086 , 0x24d0 ) } , /* Ex */
{ PCI_DEVICE ( 0x8086 , 0x25a1 ) } , /* 6300 */
{ PCI_DEVICE ( 0x8086 , 0x2640 ) } , /* Fx */
{ PCI_DEVICE ( 0x8086 , 0x2670 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2671 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2672 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2673 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2674 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2675 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2676 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2677 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2678 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x2679 ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x267a ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x267b ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x267c ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x267d ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x267e ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x267f ) } , /* 631x/632x */
{ PCI_DEVICE ( 0x8086 , 0x27b8 ) } , /* Gx */
2006-09-29 12:59:42 +04:00
/* E
2007-02-12 11:51:48 +03:00
{ PCI_DEVICE ( 0x8086 , 0x245e ) } , */
{ PCI_DEVICE ( 0x8086 , 0x2450 ) } , /* E */
2006-06-26 11:24:59 +04:00
{ 0 , } , /* terminate list */
} ;
MODULE_DEVICE_TABLE ( pci , pci_tbl ) ;
2007-01-11 10:15:41 +03:00
static __initdata int no_fwh_detect ;
module_param ( no_fwh_detect , int , 0 ) ;
MODULE_PARM_DESC ( no_fwh_detect , " Skip FWH detection: \n "
" positive value - skip if FWH space locked read-only \n "
" negative value - skip always " ) ;
2006-06-26 11:24:59 +04:00
static inline u8 hwstatus_get ( void __iomem * mem )
{
return readb ( mem + INTEL_RNG_HW_STATUS ) ;
}
static inline u8 hwstatus_set ( void __iomem * mem ,
u8 hw_status )
{
writeb ( hw_status , mem + INTEL_RNG_HW_STATUS ) ;
return hwstatus_get ( mem ) ;
}
2007-11-21 07:24:45 +03:00
static int intel_rng_data_present ( struct hwrng * rng , int wait )
2006-06-26 11:24:59 +04:00
{
void __iomem * mem = ( void __iomem * ) rng - > priv ;
2007-11-21 07:24:45 +03:00
int data , i ;
for ( i = 0 ; i < 20 ; i + + ) {
data = ! ! ( readb ( mem + INTEL_RNG_STATUS ) &
INTEL_RNG_DATA_PRESENT ) ;
if ( data | | ! wait )
break ;
udelay ( 10 ) ;
}
return data ;
2006-06-26 11:24:59 +04:00
}
static int intel_rng_data_read ( struct hwrng * rng , u32 * data )
{
void __iomem * mem = ( void __iomem * ) rng - > priv ;
* data = readb ( mem + INTEL_RNG_DATA ) ;
return 1 ;
}
static int intel_rng_init ( struct hwrng * rng )
{
void __iomem * mem = ( void __iomem * ) rng - > priv ;
u8 hw_status ;
int err = - EIO ;
hw_status = hwstatus_get ( mem ) ;
/* turn RNG h/w on, if it's off */
if ( ( hw_status & INTEL_RNG_ENABLED ) = = 0 )
hw_status = hwstatus_set ( mem , hw_status | INTEL_RNG_ENABLED ) ;
if ( ( hw_status & INTEL_RNG_ENABLED ) = = 0 ) {
2014-09-15 19:01:20 +04:00
pr_err ( PFX " cannot enable RNG, aborting \n " ) ;
2006-06-26 11:24:59 +04:00
goto out ;
}
err = 0 ;
out :
return err ;
}
static void intel_rng_cleanup ( struct hwrng * rng )
{
void __iomem * mem = ( void __iomem * ) rng - > priv ;
u8 hw_status ;
hw_status = hwstatus_get ( mem ) ;
if ( hw_status & INTEL_RNG_ENABLED )
hwstatus_set ( mem , hw_status & ~ INTEL_RNG_ENABLED ) ;
else
2014-09-15 19:01:20 +04:00
pr_warn ( PFX " unusual: RNG already disabled \n " ) ;
2006-06-26 11:24:59 +04:00
}
static struct hwrng intel_rng = {
. name = " intel " ,
. init = intel_rng_init ,
. cleanup = intel_rng_cleanup ,
. data_present = intel_rng_data_present ,
. data_read = intel_rng_data_read ,
} ;
2007-05-08 11:25:08 +04:00
struct intel_rng_hw {
struct pci_dev * dev ;
void __iomem * mem ;
u8 bios_cntl_off ;
u8 bios_cntl_val ;
u8 fwh_dec_en1_off ;
u8 fwh_dec_en1_val ;
} ;
2006-06-26 11:24:59 +04:00
2007-05-08 11:25:08 +04:00
static int __init intel_rng_hw_init ( void * _intel_rng_hw )
{
struct intel_rng_hw * intel_rng_hw = _intel_rng_hw ;
u8 mfc , dvc ;
2008-07-28 21:16:30 +04:00
/* interrupts disabled in stop_machine call */
2007-05-08 11:25:08 +04:00
if ( ! ( intel_rng_hw - > fwh_dec_en1_val & FWH_F8_EN_MASK ) )
pci_write_config_byte ( intel_rng_hw - > dev ,
intel_rng_hw - > fwh_dec_en1_off ,
intel_rng_hw - > fwh_dec_en1_val |
FWH_F8_EN_MASK ) ;
if ( ! ( intel_rng_hw - > bios_cntl_val & BIOS_CNTL_WRITE_ENABLE_MASK ) )
pci_write_config_byte ( intel_rng_hw - > dev ,
intel_rng_hw - > bios_cntl_off ,
intel_rng_hw - > bios_cntl_val |
BIOS_CNTL_WRITE_ENABLE_MASK ) ;
writeb ( INTEL_FWH_RESET_CMD , intel_rng_hw - > mem ) ;
writeb ( INTEL_FWH_READ_ID_CMD , intel_rng_hw - > mem ) ;
mfc = readb ( intel_rng_hw - > mem + INTEL_FWH_MANUFACTURER_CODE_ADDRESS ) ;
dvc = readb ( intel_rng_hw - > mem + INTEL_FWH_DEVICE_CODE_ADDRESS ) ;
writeb ( INTEL_FWH_RESET_CMD , intel_rng_hw - > mem ) ;
if ( ! ( intel_rng_hw - > bios_cntl_val &
( BIOS_CNTL_LOCK_ENABLE_MASK | BIOS_CNTL_WRITE_ENABLE_MASK ) ) )
pci_write_config_byte ( intel_rng_hw - > dev ,
intel_rng_hw - > bios_cntl_off ,
intel_rng_hw - > bios_cntl_val ) ;
if ( ! ( intel_rng_hw - > fwh_dec_en1_val & FWH_F8_EN_MASK ) )
pci_write_config_byte ( intel_rng_hw - > dev ,
intel_rng_hw - > fwh_dec_en1_off ,
intel_rng_hw - > fwh_dec_en1_val ) ;
2006-09-29 12:59:42 +04:00
2007-05-08 11:25:08 +04:00
if ( mfc ! = INTEL_FWH_MANUFACTURER_CODE | |
( dvc ! = INTEL_FWH_DEVICE_CODE_8M & &
dvc ! = INTEL_FWH_DEVICE_CODE_4M ) ) {
2014-09-15 19:01:20 +04:00
pr_notice ( PFX " FWH not detected \n " ) ;
2007-05-08 11:25:08 +04:00
return - ENODEV ;
}
return 0 ;
}
static int __init intel_init_hw_struct ( struct intel_rng_hw * intel_rng_hw ,
struct pci_dev * dev )
2006-09-29 12:59:42 +04:00
{
2007-05-08 11:25:08 +04:00
intel_rng_hw - > bios_cntl_val = 0xff ;
intel_rng_hw - > fwh_dec_en1_val = 0xff ;
intel_rng_hw - > dev = dev ;
/* Check for Intel 82802 */
if ( dev - > device < 0x2640 ) {
intel_rng_hw - > fwh_dec_en1_off = FWH_DEC_EN1_REG_OLD ;
intel_rng_hw - > bios_cntl_off = BIOS_CNTL_REG_OLD ;
} else {
intel_rng_hw - > fwh_dec_en1_off = FWH_DEC_EN1_REG_NEW ;
intel_rng_hw - > bios_cntl_off = BIOS_CNTL_REG_NEW ;
}
pci_read_config_byte ( dev , intel_rng_hw - > fwh_dec_en1_off ,
& intel_rng_hw - > fwh_dec_en1_val ) ;
pci_read_config_byte ( dev , intel_rng_hw - > bios_cntl_off ,
& intel_rng_hw - > bios_cntl_val ) ;
if ( ( intel_rng_hw - > bios_cntl_val &
( BIOS_CNTL_LOCK_ENABLE_MASK | BIOS_CNTL_WRITE_ENABLE_MASK ) )
= = BIOS_CNTL_LOCK_ENABLE_MASK ) {
static __initdata /*const*/ char warning [ ] =
2009-07-07 00:05:40 +04:00
PFX " Firmware space is locked read-only. If you can't or \n "
PFX " don't want to disable this in firmware setup, and if \n "
PFX " you are certain that your system has a functional \n "
PFX " RNG, try using the 'no_fwh_detect' option. \n " ;
2007-05-08 11:25:08 +04:00
if ( no_fwh_detect )
return - ENODEV ;
2014-09-15 19:01:20 +04:00
pr_warn ( " %s " , warning ) ;
2007-05-08 11:25:08 +04:00
return - EBUSY ;
}
intel_rng_hw - > mem = ioremap_nocache ( INTEL_FWH_ADDR , INTEL_FWH_ADDR_LEN ) ;
if ( intel_rng_hw - > mem = = NULL )
return - EBUSY ;
return 0 ;
2006-09-29 12:59:42 +04:00
}
2007-05-08 11:25:08 +04:00
2006-09-29 12:59:42 +04:00
2006-06-26 11:24:59 +04:00
static int __init mod_init ( void )
{
int err = - ENODEV ;
2007-05-08 11:25:08 +04:00
int i ;
2006-09-29 12:59:42 +04:00
struct pci_dev * dev = NULL ;
2007-05-08 11:25:08 +04:00
void __iomem * mem = mem ;
u8 hw_status ;
struct intel_rng_hw * intel_rng_hw ;
2006-06-26 11:24:59 +04:00
2006-09-29 12:59:42 +04:00
for ( i = 0 ; ! dev & & pci_tbl [ i ] . vendor ; + + i )
2007-05-08 11:25:08 +04:00
dev = pci_get_device ( pci_tbl [ i ] . vendor , pci_tbl [ i ] . device ,
NULL ) ;
2006-09-29 12:59:42 +04:00
if ( ! dev )
2006-06-26 11:24:59 +04:00
goto out ; /* Device not found. */
2007-01-11 10:15:41 +03:00
if ( no_fwh_detect < 0 ) {
pci_dev_put ( dev ) ;
goto fwh_done ;
}
2007-05-08 11:25:08 +04:00
intel_rng_hw = kmalloc ( sizeof ( * intel_rng_hw ) , GFP_KERNEL ) ;
if ( ! intel_rng_hw ) {
2007-01-11 10:15:41 +03:00
pci_dev_put ( dev ) ;
goto out ;
}
2007-05-08 11:25:08 +04:00
err = intel_init_hw_struct ( intel_rng_hw , dev ) ;
if ( err ) {
2006-09-29 12:59:42 +04:00
pci_dev_put ( dev ) ;
2007-05-08 11:25:08 +04:00
kfree ( intel_rng_hw ) ;
if ( err = = - ENODEV )
goto fwh_done ;
2006-09-29 12:59:42 +04:00
goto out ;
}
/*
* Since the BIOS code / data is going to disappear from its normal
* location with the Read ID command , all activity on the system
* must be stopped until the state is back to normal .
2007-05-08 11:25:08 +04:00
*
2008-07-28 21:16:30 +04:00
* Use stop_machine because IPIs can be blocked by disabling
2007-05-08 11:25:08 +04:00
* interrupts .
2006-09-29 12:59:42 +04:00
*/
2008-07-28 21:16:30 +04:00
err = stop_machine ( intel_rng_hw_init , intel_rng_hw , NULL ) ;
2006-09-29 12:59:42 +04:00
pci_dev_put ( dev ) ;
2007-05-08 11:25:08 +04:00
iounmap ( intel_rng_hw - > mem ) ;
kfree ( intel_rng_hw ) ;
if ( err )
2006-09-29 12:59:42 +04:00
goto out ;
2007-01-11 10:15:41 +03:00
fwh_done :
2006-06-26 11:24:59 +04:00
err = - ENOMEM ;
mem = ioremap ( INTEL_RNG_ADDR , INTEL_RNG_ADDR_LEN ) ;
if ( ! mem )
goto out ;
intel_rng . priv = ( unsigned long ) mem ;
2006-09-29 12:59:42 +04:00
/* Check for Random Number Generator */
2006-06-26 11:24:59 +04:00
err = - ENODEV ;
hw_status = hwstatus_get ( mem ) ;
2007-05-08 11:25:08 +04:00
if ( ( hw_status & INTEL_RNG_PRESENT ) = = 0 ) {
iounmap ( mem ) ;
goto out ;
}
2006-06-26 11:24:59 +04:00
2014-09-15 19:01:20 +04:00
pr_info ( " Intel 82802 RNG detected \n " ) ;
2006-06-26 11:24:59 +04:00
err = hwrng_register ( & intel_rng ) ;
if ( err ) {
2014-09-15 19:01:20 +04:00
pr_err ( PFX " RNG registering failed (%d) \n " ,
2006-06-26 11:24:59 +04:00
err ) ;
2007-05-08 11:25:08 +04:00
iounmap ( mem ) ;
2006-06-26 11:24:59 +04:00
}
out :
return err ;
}
static void __exit mod_exit ( void )
{
void __iomem * mem = ( void __iomem * ) intel_rng . priv ;
hwrng_unregister ( & intel_rng ) ;
iounmap ( mem ) ;
}
2007-01-11 10:15:43 +03:00
module_init ( mod_init ) ;
2006-06-26 11:24:59 +04:00
module_exit ( mod_exit ) ;
MODULE_DESCRIPTION ( " H/W RNG driver for Intel chipsets " ) ;
MODULE_LICENSE ( " GPL " ) ;