intel-rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * RNG driver for Intel RNGs
  3. *
  4. * Copyright 2005 (c) MontaVista Software, Inc.
  5. *
  6. * with the majority of the code coming from:
  7. *
  8. * Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  9. * (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  10. *
  11. * derived from
  12. *
  13. * Hardware driver for the AMD 768 Random Number Generator (RNG)
  14. * (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
  15. *
  16. * derived from
  17. *
  18. * Hardware driver for Intel i810 Random Number Generator (RNG)
  19. * Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  20. * Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  21. *
  22. * This file is licensed under the terms of the GNU General Public
  23. * License version 2. This program is licensed "as is" without any
  24. * warranty of any kind, whether express or implied.
  25. */
  26. #include <linux/hw_random.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/pci.h>
  30. #include <linux/stop_machine.h>
  31. #include <asm/io.h>
  32. #define PFX KBUILD_MODNAME ": "
  33. /*
  34. * RNG registers
  35. */
  36. #define INTEL_RNG_HW_STATUS 0
  37. #define INTEL_RNG_PRESENT 0x40
  38. #define INTEL_RNG_ENABLED 0x01
  39. #define INTEL_RNG_STATUS 1
  40. #define INTEL_RNG_DATA_PRESENT 0x01
  41. #define INTEL_RNG_DATA 2
  42. /*
  43. * Magic address at which Intel PCI bridges locate the RNG
  44. */
  45. #define INTEL_RNG_ADDR 0xFFBC015F
  46. #define INTEL_RNG_ADDR_LEN 3
  47. /*
  48. * LPC bridge PCI config space registers
  49. */
  50. #define FWH_DEC_EN1_REG_OLD 0xe3
  51. #define FWH_DEC_EN1_REG_NEW 0xd9 /* high byte of 16-bit register */
  52. #define FWH_F8_EN_MASK 0x80
  53. #define BIOS_CNTL_REG_OLD 0x4e
  54. #define BIOS_CNTL_REG_NEW 0xdc
  55. #define BIOS_CNTL_WRITE_ENABLE_MASK 0x01
  56. #define BIOS_CNTL_LOCK_ENABLE_MASK 0x02
  57. /*
  58. * Magic address at which Intel Firmware Hubs get accessed
  59. */
  60. #define INTEL_FWH_ADDR 0xffff0000
  61. #define INTEL_FWH_ADDR_LEN 2
  62. /*
  63. * Intel Firmware Hub command codes (write to any address inside the device)
  64. */
  65. #define INTEL_FWH_RESET_CMD 0xff /* aka READ_ARRAY */
  66. #define INTEL_FWH_READ_ID_CMD 0x90
  67. /*
  68. * Intel Firmware Hub Read ID command result addresses
  69. */
  70. #define INTEL_FWH_MANUFACTURER_CODE_ADDRESS 0x000000
  71. #define INTEL_FWH_DEVICE_CODE_ADDRESS 0x000001
  72. /*
  73. * Intel Firmware Hub Read ID command result values
  74. */
  75. #define INTEL_FWH_MANUFACTURER_CODE 0x89
  76. #define INTEL_FWH_DEVICE_CODE_8M 0xac
  77. #define INTEL_FWH_DEVICE_CODE_4M 0xad
  78. /*
  79. * Data for PCI driver interface
  80. *
  81. * This data only exists for exporting the supported
  82. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  83. * register a pci_driver, because someone else might one day
  84. * want to register another driver on the same PCI id.
  85. */
  86. static const struct pci_device_id pci_tbl[] = {
  87. /* AA
  88. { PCI_DEVICE(0x8086, 0x2418) }, */
  89. { PCI_DEVICE(0x8086, 0x2410) }, /* AA */
  90. /* AB
  91. { PCI_DEVICE(0x8086, 0x2428) }, */
  92. { PCI_DEVICE(0x8086, 0x2420) }, /* AB */
  93. /* ??
  94. { PCI_DEVICE(0x8086, 0x2430) }, */
  95. /* BAM, CAM, DBM, FBM, GxM
  96. { PCI_DEVICE(0x8086, 0x2448) }, */
  97. { PCI_DEVICE(0x8086, 0x244c) }, /* BAM */
  98. { PCI_DEVICE(0x8086, 0x248c) }, /* CAM */
  99. { PCI_DEVICE(0x8086, 0x24cc) }, /* DBM */
  100. { PCI_DEVICE(0x8086, 0x2641) }, /* FBM */
  101. { PCI_DEVICE(0x8086, 0x27b9) }, /* GxM */
  102. { PCI_DEVICE(0x8086, 0x27bd) }, /* GxM DH */
  103. /* BA, CA, DB, Ex, 6300, Fx, 631x/632x, Gx
  104. { PCI_DEVICE(0x8086, 0x244e) }, */
  105. { PCI_DEVICE(0x8086, 0x2440) }, /* BA */
  106. { PCI_DEVICE(0x8086, 0x2480) }, /* CA */
  107. { PCI_DEVICE(0x8086, 0x24c0) }, /* DB */
  108. { PCI_DEVICE(0x8086, 0x24d0) }, /* Ex */
  109. { PCI_DEVICE(0x8086, 0x25a1) }, /* 6300 */
  110. { PCI_DEVICE(0x8086, 0x2640) }, /* Fx */
  111. { PCI_DEVICE(0x8086, 0x2670) }, /* 631x/632x */
  112. { PCI_DEVICE(0x8086, 0x2671) }, /* 631x/632x */
  113. { PCI_DEVICE(0x8086, 0x2672) }, /* 631x/632x */
  114. { PCI_DEVICE(0x8086, 0x2673) }, /* 631x/632x */
  115. { PCI_DEVICE(0x8086, 0x2674) }, /* 631x/632x */
  116. { PCI_DEVICE(0x8086, 0x2675) }, /* 631x/632x */
  117. { PCI_DEVICE(0x8086, 0x2676) }, /* 631x/632x */
  118. { PCI_DEVICE(0x8086, 0x2677) }, /* 631x/632x */
  119. { PCI_DEVICE(0x8086, 0x2678) }, /* 631x/632x */
  120. { PCI_DEVICE(0x8086, 0x2679) }, /* 631x/632x */
  121. { PCI_DEVICE(0x8086, 0x267a) }, /* 631x/632x */
  122. { PCI_DEVICE(0x8086, 0x267b) }, /* 631x/632x */
  123. { PCI_DEVICE(0x8086, 0x267c) }, /* 631x/632x */
  124. { PCI_DEVICE(0x8086, 0x267d) }, /* 631x/632x */
  125. { PCI_DEVICE(0x8086, 0x267e) }, /* 631x/632x */
  126. { PCI_DEVICE(0x8086, 0x267f) }, /* 631x/632x */
  127. { PCI_DEVICE(0x8086, 0x27b8) }, /* Gx */
  128. /* E
  129. { PCI_DEVICE(0x8086, 0x245e) }, */
  130. { PCI_DEVICE(0x8086, 0x2450) }, /* E */
  131. { 0, }, /* terminate list */
  132. };
  133. MODULE_DEVICE_TABLE(pci, pci_tbl);
  134. static __initdata int no_fwh_detect;
  135. module_param(no_fwh_detect, int, 0);
  136. MODULE_PARM_DESC(no_fwh_detect, "Skip FWH detection:\n"
  137. " positive value - skip if FWH space locked read-only\n"
  138. " negative value - skip always");
  139. static inline u8 hwstatus_get(void __iomem *mem)
  140. {
  141. return readb(mem + INTEL_RNG_HW_STATUS);
  142. }
  143. static inline u8 hwstatus_set(void __iomem *mem,
  144. u8 hw_status)
  145. {
  146. writeb(hw_status, mem + INTEL_RNG_HW_STATUS);
  147. return hwstatus_get(mem);
  148. }
  149. static int intel_rng_data_present(struct hwrng *rng)
  150. {
  151. void __iomem *mem = (void __iomem *)rng->priv;
  152. return !!(readb(mem + INTEL_RNG_STATUS) & INTEL_RNG_DATA_PRESENT);
  153. }
  154. static int intel_rng_data_read(struct hwrng *rng, u32 *data)
  155. {
  156. void __iomem *mem = (void __iomem *)rng->priv;
  157. *data = readb(mem + INTEL_RNG_DATA);
  158. return 1;
  159. }
  160. static int intel_rng_init(struct hwrng *rng)
  161. {
  162. void __iomem *mem = (void __iomem *)rng->priv;
  163. u8 hw_status;
  164. int err = -EIO;
  165. hw_status = hwstatus_get(mem);
  166. /* turn RNG h/w on, if it's off */
  167. if ((hw_status & INTEL_RNG_ENABLED) == 0)
  168. hw_status = hwstatus_set(mem, hw_status | INTEL_RNG_ENABLED);
  169. if ((hw_status & INTEL_RNG_ENABLED) == 0) {
  170. printk(KERN_ERR PFX "cannot enable RNG, aborting\n");
  171. goto out;
  172. }
  173. err = 0;
  174. out:
  175. return err;
  176. }
  177. static void intel_rng_cleanup(struct hwrng *rng)
  178. {
  179. void __iomem *mem = (void __iomem *)rng->priv;
  180. u8 hw_status;
  181. hw_status = hwstatus_get(mem);
  182. if (hw_status & INTEL_RNG_ENABLED)
  183. hwstatus_set(mem, hw_status & ~INTEL_RNG_ENABLED);
  184. else
  185. printk(KERN_WARNING PFX "unusual: RNG already disabled\n");
  186. }
  187. static struct hwrng intel_rng = {
  188. .name = "intel",
  189. .init = intel_rng_init,
  190. .cleanup = intel_rng_cleanup,
  191. .data_present = intel_rng_data_present,
  192. .data_read = intel_rng_data_read,
  193. };
  194. struct intel_rng_hw {
  195. struct pci_dev *dev;
  196. void __iomem *mem;
  197. u8 bios_cntl_off;
  198. u8 bios_cntl_val;
  199. u8 fwh_dec_en1_off;
  200. u8 fwh_dec_en1_val;
  201. };
  202. static int __init intel_rng_hw_init(void *_intel_rng_hw)
  203. {
  204. struct intel_rng_hw *intel_rng_hw = _intel_rng_hw;
  205. u8 mfc, dvc;
  206. /* interrupts disabled in stop_machine_run call */
  207. if (!(intel_rng_hw->fwh_dec_en1_val & FWH_F8_EN_MASK))
  208. pci_write_config_byte(intel_rng_hw->dev,
  209. intel_rng_hw->fwh_dec_en1_off,
  210. intel_rng_hw->fwh_dec_en1_val |
  211. FWH_F8_EN_MASK);
  212. if (!(intel_rng_hw->bios_cntl_val & BIOS_CNTL_WRITE_ENABLE_MASK))
  213. pci_write_config_byte(intel_rng_hw->dev,
  214. intel_rng_hw->bios_cntl_off,
  215. intel_rng_hw->bios_cntl_val |
  216. BIOS_CNTL_WRITE_ENABLE_MASK);
  217. writeb(INTEL_FWH_RESET_CMD, intel_rng_hw->mem);
  218. writeb(INTEL_FWH_READ_ID_CMD, intel_rng_hw->mem);
  219. mfc = readb(intel_rng_hw->mem + INTEL_FWH_MANUFACTURER_CODE_ADDRESS);
  220. dvc = readb(intel_rng_hw->mem + INTEL_FWH_DEVICE_CODE_ADDRESS);
  221. writeb(INTEL_FWH_RESET_CMD, intel_rng_hw->mem);
  222. if (!(intel_rng_hw->bios_cntl_val &
  223. (BIOS_CNTL_LOCK_ENABLE_MASK|BIOS_CNTL_WRITE_ENABLE_MASK)))
  224. pci_write_config_byte(intel_rng_hw->dev,
  225. intel_rng_hw->bios_cntl_off,
  226. intel_rng_hw->bios_cntl_val);
  227. if (!(intel_rng_hw->fwh_dec_en1_val & FWH_F8_EN_MASK))
  228. pci_write_config_byte(intel_rng_hw->dev,
  229. intel_rng_hw->fwh_dec_en1_off,
  230. intel_rng_hw->fwh_dec_en1_val);
  231. if (mfc != INTEL_FWH_MANUFACTURER_CODE ||
  232. (dvc != INTEL_FWH_DEVICE_CODE_8M &&
  233. dvc != INTEL_FWH_DEVICE_CODE_4M)) {
  234. printk(KERN_ERR PFX "FWH not detected\n");
  235. return -ENODEV;
  236. }
  237. return 0;
  238. }
  239. static int __init intel_init_hw_struct(struct intel_rng_hw *intel_rng_hw,
  240. struct pci_dev *dev)
  241. {
  242. intel_rng_hw->bios_cntl_val = 0xff;
  243. intel_rng_hw->fwh_dec_en1_val = 0xff;
  244. intel_rng_hw->dev = dev;
  245. /* Check for Intel 82802 */
  246. if (dev->device < 0x2640) {
  247. intel_rng_hw->fwh_dec_en1_off = FWH_DEC_EN1_REG_OLD;
  248. intel_rng_hw->bios_cntl_off = BIOS_CNTL_REG_OLD;
  249. } else {
  250. intel_rng_hw->fwh_dec_en1_off = FWH_DEC_EN1_REG_NEW;
  251. intel_rng_hw->bios_cntl_off = BIOS_CNTL_REG_NEW;
  252. }
  253. pci_read_config_byte(dev, intel_rng_hw->fwh_dec_en1_off,
  254. &intel_rng_hw->fwh_dec_en1_val);
  255. pci_read_config_byte(dev, intel_rng_hw->bios_cntl_off,
  256. &intel_rng_hw->bios_cntl_val);
  257. if ((intel_rng_hw->bios_cntl_val &
  258. (BIOS_CNTL_LOCK_ENABLE_MASK|BIOS_CNTL_WRITE_ENABLE_MASK))
  259. == BIOS_CNTL_LOCK_ENABLE_MASK) {
  260. static __initdata /*const*/ char warning[] =
  261. KERN_WARNING PFX "Firmware space is locked read-only. "
  262. KERN_WARNING PFX "If you can't or\n don't want to "
  263. KERN_WARNING PFX "disable this in firmware setup, and "
  264. KERN_WARNING PFX "if\n you are certain that your "
  265. KERN_WARNING PFX "system has a functional\n RNG, try"
  266. KERN_WARNING PFX "using the 'no_fwh_detect' option.\n";
  267. if (no_fwh_detect)
  268. return -ENODEV;
  269. printk(warning);
  270. return -EBUSY;
  271. }
  272. intel_rng_hw->mem = ioremap_nocache(INTEL_FWH_ADDR, INTEL_FWH_ADDR_LEN);
  273. if (intel_rng_hw->mem == NULL)
  274. return -EBUSY;
  275. return 0;
  276. }
  277. static int __init mod_init(void)
  278. {
  279. int err = -ENODEV;
  280. int i;
  281. struct pci_dev *dev = NULL;
  282. void __iomem *mem = mem;
  283. u8 hw_status;
  284. struct intel_rng_hw *intel_rng_hw;
  285. for (i = 0; !dev && pci_tbl[i].vendor; ++i)
  286. dev = pci_get_device(pci_tbl[i].vendor, pci_tbl[i].device,
  287. NULL);
  288. if (!dev)
  289. goto out; /* Device not found. */
  290. if (no_fwh_detect < 0) {
  291. pci_dev_put(dev);
  292. goto fwh_done;
  293. }
  294. intel_rng_hw = kmalloc(sizeof(*intel_rng_hw), GFP_KERNEL);
  295. if (!intel_rng_hw) {
  296. pci_dev_put(dev);
  297. goto out;
  298. }
  299. err = intel_init_hw_struct(intel_rng_hw, dev);
  300. if (err) {
  301. pci_dev_put(dev);
  302. kfree(intel_rng_hw);
  303. if (err == -ENODEV)
  304. goto fwh_done;
  305. goto out;
  306. }
  307. /*
  308. * Since the BIOS code/data is going to disappear from its normal
  309. * location with the Read ID command, all activity on the system
  310. * must be stopped until the state is back to normal.
  311. *
  312. * Use stop_machine_run because IPIs can be blocked by disabling
  313. * interrupts.
  314. */
  315. err = stop_machine_run(intel_rng_hw_init, intel_rng_hw, NR_CPUS);
  316. pci_dev_put(dev);
  317. iounmap(intel_rng_hw->mem);
  318. kfree(intel_rng_hw);
  319. if (err)
  320. goto out;
  321. fwh_done:
  322. err = -ENOMEM;
  323. mem = ioremap(INTEL_RNG_ADDR, INTEL_RNG_ADDR_LEN);
  324. if (!mem)
  325. goto out;
  326. intel_rng.priv = (unsigned long)mem;
  327. /* Check for Random Number Generator */
  328. err = -ENODEV;
  329. hw_status = hwstatus_get(mem);
  330. if ((hw_status & INTEL_RNG_PRESENT) == 0) {
  331. iounmap(mem);
  332. goto out;
  333. }
  334. printk(KERN_INFO "Intel 82802 RNG detected\n");
  335. err = hwrng_register(&intel_rng);
  336. if (err) {
  337. printk(KERN_ERR PFX "RNG registering failed (%d)\n",
  338. err);
  339. iounmap(mem);
  340. }
  341. out:
  342. return err;
  343. }
  344. static void __exit mod_exit(void)
  345. {
  346. void __iomem *mem = (void __iomem *)intel_rng.priv;
  347. hwrng_unregister(&intel_rng);
  348. iounmap(mem);
  349. }
  350. module_init(mod_init);
  351. module_exit(mod_exit);
  352. MODULE_DESCRIPTION("H/W RNG driver for Intel chipsets");
  353. MODULE_LICENSE("GPL");