intel-rng.c 11 KB

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