intel-rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <linux/slab.h>
  33. #include <asm/io.h>
  34. #define PFX KBUILD_MODNAME ": "
  35. /*
  36. * RNG registers
  37. */
  38. #define INTEL_RNG_HW_STATUS 0
  39. #define INTEL_RNG_PRESENT 0x40
  40. #define INTEL_RNG_ENABLED 0x01
  41. #define INTEL_RNG_STATUS 1
  42. #define INTEL_RNG_DATA_PRESENT 0x01
  43. #define INTEL_RNG_DATA 2
  44. /*
  45. * Magic address at which Intel PCI bridges locate the RNG
  46. */
  47. #define INTEL_RNG_ADDR 0xFFBC015F
  48. #define INTEL_RNG_ADDR_LEN 3
  49. /*
  50. * LPC bridge PCI config space registers
  51. */
  52. #define FWH_DEC_EN1_REG_OLD 0xe3
  53. #define FWH_DEC_EN1_REG_NEW 0xd9 /* high byte of 16-bit register */
  54. #define FWH_F8_EN_MASK 0x80
  55. #define BIOS_CNTL_REG_OLD 0x4e
  56. #define BIOS_CNTL_REG_NEW 0xdc
  57. #define BIOS_CNTL_WRITE_ENABLE_MASK 0x01
  58. #define BIOS_CNTL_LOCK_ENABLE_MASK 0x02
  59. /*
  60. * Magic address at which Intel Firmware Hubs get accessed
  61. */
  62. #define INTEL_FWH_ADDR 0xffff0000
  63. #define INTEL_FWH_ADDR_LEN 2
  64. /*
  65. * Intel Firmware Hub command codes (write to any address inside the device)
  66. */
  67. #define INTEL_FWH_RESET_CMD 0xff /* aka READ_ARRAY */
  68. #define INTEL_FWH_READ_ID_CMD 0x90
  69. /*
  70. * Intel Firmware Hub Read ID command result addresses
  71. */
  72. #define INTEL_FWH_MANUFACTURER_CODE_ADDRESS 0x000000
  73. #define INTEL_FWH_DEVICE_CODE_ADDRESS 0x000001
  74. /*
  75. * Intel Firmware Hub Read ID command result values
  76. */
  77. #define INTEL_FWH_MANUFACTURER_CODE 0x89
  78. #define INTEL_FWH_DEVICE_CODE_8M 0xac
  79. #define INTEL_FWH_DEVICE_CODE_4M 0xad
  80. /*
  81. * Data for PCI driver interface
  82. *
  83. * This data only exists for exporting the supported
  84. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  85. * register a pci_driver, because someone else might one day
  86. * want to register another driver on the same PCI id.
  87. */
  88. static const struct pci_device_id pci_tbl[] = {
  89. /* AA
  90. { PCI_DEVICE(0x8086, 0x2418) }, */
  91. { PCI_DEVICE(0x8086, 0x2410) }, /* AA */
  92. /* AB
  93. { PCI_DEVICE(0x8086, 0x2428) }, */
  94. { PCI_DEVICE(0x8086, 0x2420) }, /* AB */
  95. /* ??
  96. { PCI_DEVICE(0x8086, 0x2430) }, */
  97. /* BAM, CAM, DBM, FBM, GxM
  98. { PCI_DEVICE(0x8086, 0x2448) }, */
  99. { PCI_DEVICE(0x8086, 0x244c) }, /* BAM */
  100. { PCI_DEVICE(0x8086, 0x248c) }, /* CAM */
  101. { PCI_DEVICE(0x8086, 0x24cc) }, /* DBM */
  102. { PCI_DEVICE(0x8086, 0x2641) }, /* FBM */
  103. { PCI_DEVICE(0x8086, 0x27b9) }, /* GxM */
  104. { PCI_DEVICE(0x8086, 0x27bd) }, /* GxM DH */
  105. /* BA, CA, DB, Ex, 6300, Fx, 631x/632x, Gx
  106. { PCI_DEVICE(0x8086, 0x244e) }, */
  107. { PCI_DEVICE(0x8086, 0x2440) }, /* BA */
  108. { PCI_DEVICE(0x8086, 0x2480) }, /* CA */
  109. { PCI_DEVICE(0x8086, 0x24c0) }, /* DB */
  110. { PCI_DEVICE(0x8086, 0x24d0) }, /* Ex */
  111. { PCI_DEVICE(0x8086, 0x25a1) }, /* 6300 */
  112. { PCI_DEVICE(0x8086, 0x2640) }, /* Fx */
  113. { PCI_DEVICE(0x8086, 0x2670) }, /* 631x/632x */
  114. { PCI_DEVICE(0x8086, 0x2671) }, /* 631x/632x */
  115. { PCI_DEVICE(0x8086, 0x2672) }, /* 631x/632x */
  116. { PCI_DEVICE(0x8086, 0x2673) }, /* 631x/632x */
  117. { PCI_DEVICE(0x8086, 0x2674) }, /* 631x/632x */
  118. { PCI_DEVICE(0x8086, 0x2675) }, /* 631x/632x */
  119. { PCI_DEVICE(0x8086, 0x2676) }, /* 631x/632x */
  120. { PCI_DEVICE(0x8086, 0x2677) }, /* 631x/632x */
  121. { PCI_DEVICE(0x8086, 0x2678) }, /* 631x/632x */
  122. { PCI_DEVICE(0x8086, 0x2679) }, /* 631x/632x */
  123. { PCI_DEVICE(0x8086, 0x267a) }, /* 631x/632x */
  124. { PCI_DEVICE(0x8086, 0x267b) }, /* 631x/632x */
  125. { PCI_DEVICE(0x8086, 0x267c) }, /* 631x/632x */
  126. { PCI_DEVICE(0x8086, 0x267d) }, /* 631x/632x */
  127. { PCI_DEVICE(0x8086, 0x267e) }, /* 631x/632x */
  128. { PCI_DEVICE(0x8086, 0x267f) }, /* 631x/632x */
  129. { PCI_DEVICE(0x8086, 0x27b8) }, /* Gx */
  130. /* E
  131. { PCI_DEVICE(0x8086, 0x245e) }, */
  132. { PCI_DEVICE(0x8086, 0x2450) }, /* E */
  133. { 0, }, /* terminate list */
  134. };
  135. MODULE_DEVICE_TABLE(pci, pci_tbl);
  136. static __initdata int no_fwh_detect;
  137. module_param(no_fwh_detect, int, 0);
  138. MODULE_PARM_DESC(no_fwh_detect, "Skip FWH detection:\n"
  139. " positive value - skip if FWH space locked read-only\n"
  140. " negative value - skip always");
  141. static inline u8 hwstatus_get(void __iomem *mem)
  142. {
  143. return readb(mem + INTEL_RNG_HW_STATUS);
  144. }
  145. static inline u8 hwstatus_set(void __iomem *mem,
  146. u8 hw_status)
  147. {
  148. writeb(hw_status, mem + INTEL_RNG_HW_STATUS);
  149. return hwstatus_get(mem);
  150. }
  151. static int intel_rng_data_present(struct hwrng *rng, int wait)
  152. {
  153. void __iomem *mem = (void __iomem *)rng->priv;
  154. int data, i;
  155. for (i = 0; i < 20; i++) {
  156. data = !!(readb(mem + INTEL_RNG_STATUS) &
  157. INTEL_RNG_DATA_PRESENT);
  158. if (data || !wait)
  159. break;
  160. udelay(10);
  161. }
  162. return data;
  163. }
  164. static int intel_rng_data_read(struct hwrng *rng, u32 *data)
  165. {
  166. void __iomem *mem = (void __iomem *)rng->priv;
  167. *data = readb(mem + INTEL_RNG_DATA);
  168. return 1;
  169. }
  170. static int intel_rng_init(struct hwrng *rng)
  171. {
  172. void __iomem *mem = (void __iomem *)rng->priv;
  173. u8 hw_status;
  174. int err = -EIO;
  175. hw_status = hwstatus_get(mem);
  176. /* turn RNG h/w on, if it's off */
  177. if ((hw_status & INTEL_RNG_ENABLED) == 0)
  178. hw_status = hwstatus_set(mem, hw_status | INTEL_RNG_ENABLED);
  179. if ((hw_status & INTEL_RNG_ENABLED) == 0) {
  180. pr_err(PFX "cannot enable RNG, aborting\n");
  181. goto out;
  182. }
  183. err = 0;
  184. out:
  185. return err;
  186. }
  187. static void intel_rng_cleanup(struct hwrng *rng)
  188. {
  189. void __iomem *mem = (void __iomem *)rng->priv;
  190. u8 hw_status;
  191. hw_status = hwstatus_get(mem);
  192. if (hw_status & INTEL_RNG_ENABLED)
  193. hwstatus_set(mem, hw_status & ~INTEL_RNG_ENABLED);
  194. else
  195. pr_warn(PFX "unusual: RNG already disabled\n");
  196. }
  197. static struct hwrng intel_rng = {
  198. .name = "intel",
  199. .init = intel_rng_init,
  200. .cleanup = intel_rng_cleanup,
  201. .data_present = intel_rng_data_present,
  202. .data_read = intel_rng_data_read,
  203. };
  204. struct intel_rng_hw {
  205. struct pci_dev *dev;
  206. void __iomem *mem;
  207. u8 bios_cntl_off;
  208. u8 bios_cntl_val;
  209. u8 fwh_dec_en1_off;
  210. u8 fwh_dec_en1_val;
  211. };
  212. static int __init intel_rng_hw_init(void *_intel_rng_hw)
  213. {
  214. struct intel_rng_hw *intel_rng_hw = _intel_rng_hw;
  215. u8 mfc, dvc;
  216. /* interrupts disabled in stop_machine call */
  217. if (!(intel_rng_hw->fwh_dec_en1_val & FWH_F8_EN_MASK))
  218. pci_write_config_byte(intel_rng_hw->dev,
  219. intel_rng_hw->fwh_dec_en1_off,
  220. intel_rng_hw->fwh_dec_en1_val |
  221. FWH_F8_EN_MASK);
  222. if (!(intel_rng_hw->bios_cntl_val & BIOS_CNTL_WRITE_ENABLE_MASK))
  223. pci_write_config_byte(intel_rng_hw->dev,
  224. intel_rng_hw->bios_cntl_off,
  225. intel_rng_hw->bios_cntl_val |
  226. BIOS_CNTL_WRITE_ENABLE_MASK);
  227. writeb(INTEL_FWH_RESET_CMD, intel_rng_hw->mem);
  228. writeb(INTEL_FWH_READ_ID_CMD, intel_rng_hw->mem);
  229. mfc = readb(intel_rng_hw->mem + INTEL_FWH_MANUFACTURER_CODE_ADDRESS);
  230. dvc = readb(intel_rng_hw->mem + INTEL_FWH_DEVICE_CODE_ADDRESS);
  231. writeb(INTEL_FWH_RESET_CMD, intel_rng_hw->mem);
  232. if (!(intel_rng_hw->bios_cntl_val &
  233. (BIOS_CNTL_LOCK_ENABLE_MASK|BIOS_CNTL_WRITE_ENABLE_MASK)))
  234. pci_write_config_byte(intel_rng_hw->dev,
  235. intel_rng_hw->bios_cntl_off,
  236. intel_rng_hw->bios_cntl_val);
  237. if (!(intel_rng_hw->fwh_dec_en1_val & FWH_F8_EN_MASK))
  238. pci_write_config_byte(intel_rng_hw->dev,
  239. intel_rng_hw->fwh_dec_en1_off,
  240. intel_rng_hw->fwh_dec_en1_val);
  241. if (mfc != INTEL_FWH_MANUFACTURER_CODE ||
  242. (dvc != INTEL_FWH_DEVICE_CODE_8M &&
  243. dvc != INTEL_FWH_DEVICE_CODE_4M)) {
  244. pr_notice(PFX "FWH not detected\n");
  245. return -ENODEV;
  246. }
  247. return 0;
  248. }
  249. static int __init intel_init_hw_struct(struct intel_rng_hw *intel_rng_hw,
  250. struct pci_dev *dev)
  251. {
  252. intel_rng_hw->bios_cntl_val = 0xff;
  253. intel_rng_hw->fwh_dec_en1_val = 0xff;
  254. intel_rng_hw->dev = dev;
  255. /* Check for Intel 82802 */
  256. if (dev->device < 0x2640) {
  257. intel_rng_hw->fwh_dec_en1_off = FWH_DEC_EN1_REG_OLD;
  258. intel_rng_hw->bios_cntl_off = BIOS_CNTL_REG_OLD;
  259. } else {
  260. intel_rng_hw->fwh_dec_en1_off = FWH_DEC_EN1_REG_NEW;
  261. intel_rng_hw->bios_cntl_off = BIOS_CNTL_REG_NEW;
  262. }
  263. pci_read_config_byte(dev, intel_rng_hw->fwh_dec_en1_off,
  264. &intel_rng_hw->fwh_dec_en1_val);
  265. pci_read_config_byte(dev, intel_rng_hw->bios_cntl_off,
  266. &intel_rng_hw->bios_cntl_val);
  267. if ((intel_rng_hw->bios_cntl_val &
  268. (BIOS_CNTL_LOCK_ENABLE_MASK|BIOS_CNTL_WRITE_ENABLE_MASK))
  269. == BIOS_CNTL_LOCK_ENABLE_MASK) {
  270. static __initdata /*const*/ char 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. pr_warn("%s", 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. pr_info("Intel 82802 RNG detected\n");
  343. err = hwrng_register(&intel_rng);
  344. if (err) {
  345. pr_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");