Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005, 2006 IBM Corporation
0004  * Copyright (C) 2014, 2015 Intel Corporation
0005  *
0006  * Authors:
0007  * Leendert van Doorn <leendert@watson.ibm.com>
0008  * Kylene Hall <kjhall@us.ibm.com>
0009  *
0010  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
0011  *
0012  * Device driver for TCG/TCPA TPM (trusted platform module).
0013  * Specifications at www.trustedcomputinggroup.org
0014  *
0015  * This device driver implements the TPM interface as defined in
0016  * the TCG TPM Interface Spec version 1.2, revision 1.0.
0017  */
0018 #include <linux/init.h>
0019 #include <linux/module.h>
0020 #include <linux/moduleparam.h>
0021 #include <linux/pnp.h>
0022 #include <linux/slab.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/wait.h>
0025 #include <linux/acpi.h>
0026 #include <linux/freezer.h>
0027 #include <linux/of.h>
0028 #include <linux/of_device.h>
0029 #include <linux/kernel.h>
0030 #include <linux/dmi.h>
0031 #include "tpm.h"
0032 #include "tpm_tis_core.h"
0033 
0034 struct tpm_info {
0035     struct resource res;
0036     /* irq > 0 means: use irq $irq;
0037      * irq = 0 means: autoprobe for an irq;
0038      * irq = -1 means: no irq support
0039      */
0040     int irq;
0041 };
0042 
0043 struct tpm_tis_tcg_phy {
0044     struct tpm_tis_data priv;
0045     void __iomem *iobase;
0046 };
0047 
0048 static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
0049 {
0050     return container_of(data, struct tpm_tis_tcg_phy, priv);
0051 }
0052 
0053 static int interrupts = -1;
0054 module_param(interrupts, int, 0444);
0055 MODULE_PARM_DESC(interrupts, "Enable interrupts");
0056 
0057 static bool itpm;
0058 module_param(itpm, bool, 0444);
0059 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
0060 
0061 static bool force;
0062 #ifdef CONFIG_X86
0063 module_param(force, bool, 0444);
0064 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
0065 #endif
0066 
0067 static int tpm_tis_disable_irq(const struct dmi_system_id *d)
0068 {
0069     if (interrupts == -1) {
0070         pr_notice("tpm_tis: %s detected: disabling interrupts.\n", d->ident);
0071         interrupts = 0;
0072     }
0073 
0074     return 0;
0075 }
0076 
0077 static const struct dmi_system_id tpm_tis_dmi_table[] = {
0078     {
0079         .callback = tpm_tis_disable_irq,
0080         .ident = "ThinkPad T490s",
0081         .matches = {
0082             DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0083             DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T490s"),
0084         },
0085     },
0086     {}
0087 };
0088 
0089 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
0090 static int has_hid(struct acpi_device *dev, const char *hid)
0091 {
0092     struct acpi_hardware_id *id;
0093 
0094     list_for_each_entry(id, &dev->pnp.ids, list)
0095         if (!strcmp(hid, id->id))
0096             return 1;
0097 
0098     return 0;
0099 }
0100 
0101 static inline int is_itpm(struct acpi_device *dev)
0102 {
0103     if (!dev)
0104         return 0;
0105     return has_hid(dev, "INTC0102");
0106 }
0107 #else
0108 static inline int is_itpm(struct acpi_device *dev)
0109 {
0110     return 0;
0111 }
0112 #endif
0113 
0114 #if defined(CONFIG_ACPI)
0115 #define DEVICE_IS_TPM2 1
0116 
0117 static const struct acpi_device_id tpm_acpi_tbl[] = {
0118     {"MSFT0101", DEVICE_IS_TPM2},
0119     {},
0120 };
0121 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
0122 
0123 static int check_acpi_tpm2(struct device *dev)
0124 {
0125     const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
0126     struct acpi_table_tpm2 *tbl;
0127     acpi_status st;
0128 
0129     if (!aid || aid->driver_data != DEVICE_IS_TPM2)
0130         return 0;
0131 
0132     /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
0133      * table is mandatory
0134      */
0135     st =
0136         acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
0137     if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
0138         dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
0139         return -EINVAL;
0140     }
0141 
0142     /* The tpm2_crb driver handles this device */
0143     if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
0144         return -ENODEV;
0145 
0146     return 0;
0147 }
0148 #else
0149 static int check_acpi_tpm2(struct device *dev)
0150 {
0151     return 0;
0152 }
0153 #endif
0154 
0155 static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
0156                   u8 *result, enum tpm_tis_io_mode io_mode)
0157 {
0158     struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
0159     __le16 result_le16;
0160     __le32 result_le32;
0161 
0162     switch (io_mode) {
0163     case TPM_TIS_PHYS_8:
0164         while (len--)
0165             *result++ = ioread8(phy->iobase + addr);
0166         break;
0167     case TPM_TIS_PHYS_16:
0168         result_le16 = cpu_to_le16(ioread16(phy->iobase + addr));
0169         memcpy(result, &result_le16, sizeof(u16));
0170         break;
0171     case TPM_TIS_PHYS_32:
0172         result_le32 = cpu_to_le32(ioread32(phy->iobase + addr));
0173         memcpy(result, &result_le32, sizeof(u32));
0174         break;
0175     }
0176 
0177     return 0;
0178 }
0179 
0180 static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
0181                    const u8 *value, enum tpm_tis_io_mode io_mode)
0182 {
0183     struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
0184 
0185     switch (io_mode) {
0186     case TPM_TIS_PHYS_8:
0187         while (len--)
0188             iowrite8(*value++, phy->iobase + addr);
0189         break;
0190     case TPM_TIS_PHYS_16:
0191         return -EINVAL;
0192     case TPM_TIS_PHYS_32:
0193         iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase + addr);
0194         break;
0195     }
0196 
0197     return 0;
0198 }
0199 
0200 static const struct tpm_tis_phy_ops tpm_tcg = {
0201     .read_bytes = tpm_tcg_read_bytes,
0202     .write_bytes = tpm_tcg_write_bytes,
0203 };
0204 
0205 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
0206 {
0207     struct tpm_tis_tcg_phy *phy;
0208     int irq = -1;
0209     int rc;
0210 
0211     dmi_check_system(tpm_tis_dmi_table);
0212 
0213     rc = check_acpi_tpm2(dev);
0214     if (rc)
0215         return rc;
0216 
0217     phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
0218     if (phy == NULL)
0219         return -ENOMEM;
0220 
0221     phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
0222     if (IS_ERR(phy->iobase))
0223         return PTR_ERR(phy->iobase);
0224 
0225     if (interrupts)
0226         irq = tpm_info->irq;
0227 
0228     if (itpm || is_itpm(ACPI_COMPANION(dev)))
0229         phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
0230 
0231     return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
0232                  ACPI_HANDLE(dev));
0233 }
0234 
0235 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
0236 
0237 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
0238                 const struct pnp_device_id *pnp_id)
0239 {
0240     struct tpm_info tpm_info = {};
0241     struct resource *res;
0242 
0243     res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
0244     if (!res)
0245         return -ENODEV;
0246     tpm_info.res = *res;
0247 
0248     if (pnp_irq_valid(pnp_dev, 0))
0249         tpm_info.irq = pnp_irq(pnp_dev, 0);
0250     else
0251         tpm_info.irq = -1;
0252 
0253     return tpm_tis_init(&pnp_dev->dev, &tpm_info);
0254 }
0255 
0256 /*
0257  * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module
0258  * parameter"). This commit added IFX0102 device ID, which is also used by
0259  * tpm_infineon but ignored to add quirks to probe which driver ought to be
0260  * used.
0261  */
0262 
0263 static struct pnp_device_id tpm_pnp_tbl[] = {
0264     {"PNP0C31", 0},     /* TPM */
0265     {"ATM1200", 0},     /* Atmel */
0266     {"IFX0102", 0},     /* Infineon */
0267     {"BCM0101", 0},     /* Broadcom */
0268     {"BCM0102", 0},     /* Broadcom */
0269     {"NSC1200", 0},     /* National */
0270     {"ICO0102", 0},     /* Intel */
0271     /* Add new here */
0272     {"", 0},        /* User Specified */
0273     {"", 0}         /* Terminator */
0274 };
0275 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
0276 
0277 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
0278 {
0279     struct tpm_chip *chip = pnp_get_drvdata(dev);
0280 
0281     tpm_chip_unregister(chip);
0282     tpm_tis_remove(chip);
0283 }
0284 
0285 static struct pnp_driver tis_pnp_driver = {
0286     .name = "tpm_tis",
0287     .id_table = tpm_pnp_tbl,
0288     .probe = tpm_tis_pnp_init,
0289     .remove = tpm_tis_pnp_remove,
0290     .driver = {
0291         .pm = &tpm_tis_pm,
0292     },
0293 };
0294 
0295 #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
0296 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
0297             sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
0298 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
0299 
0300 static struct platform_device *force_pdev;
0301 
0302 static int tpm_tis_plat_probe(struct platform_device *pdev)
0303 {
0304     struct tpm_info tpm_info = {};
0305     struct resource *res;
0306 
0307     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0308     if (res == NULL) {
0309         dev_err(&pdev->dev, "no memory resource defined\n");
0310         return -ENODEV;
0311     }
0312     tpm_info.res = *res;
0313 
0314     tpm_info.irq = platform_get_irq_optional(pdev, 0);
0315     if (tpm_info.irq <= 0) {
0316         if (pdev != force_pdev)
0317             tpm_info.irq = -1;
0318         else
0319             /* When forcing auto probe the IRQ */
0320             tpm_info.irq = 0;
0321     }
0322 
0323     return tpm_tis_init(&pdev->dev, &tpm_info);
0324 }
0325 
0326 static int tpm_tis_plat_remove(struct platform_device *pdev)
0327 {
0328     struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
0329 
0330     tpm_chip_unregister(chip);
0331     tpm_tis_remove(chip);
0332 
0333     return 0;
0334 }
0335 
0336 #ifdef CONFIG_OF
0337 static const struct of_device_id tis_of_platform_match[] = {
0338     {.compatible = "tcg,tpm-tis-mmio"},
0339     {},
0340 };
0341 MODULE_DEVICE_TABLE(of, tis_of_platform_match);
0342 #endif
0343 
0344 static struct platform_driver tis_drv = {
0345     .probe = tpm_tis_plat_probe,
0346     .remove = tpm_tis_plat_remove,
0347     .driver = {
0348         .name       = "tpm_tis",
0349         .pm     = &tpm_tis_pm,
0350         .of_match_table = of_match_ptr(tis_of_platform_match),
0351         .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
0352     },
0353 };
0354 
0355 static int tpm_tis_force_device(void)
0356 {
0357     struct platform_device *pdev;
0358     static const struct resource x86_resources[] = {
0359         DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN)
0360     };
0361 
0362     if (!force)
0363         return 0;
0364 
0365     /* The driver core will match the name tpm_tis of the device to
0366      * the tpm_tis platform driver and complete the setup via
0367      * tpm_tis_plat_probe
0368      */
0369     pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
0370                            ARRAY_SIZE(x86_resources));
0371     if (IS_ERR(pdev))
0372         return PTR_ERR(pdev);
0373     force_pdev = pdev;
0374 
0375     return 0;
0376 }
0377 
0378 static int __init init_tis(void)
0379 {
0380     int rc;
0381 
0382     rc = tpm_tis_force_device();
0383     if (rc)
0384         goto err_force;
0385 
0386     rc = platform_driver_register(&tis_drv);
0387     if (rc)
0388         goto err_platform;
0389 
0390 
0391     if (IS_ENABLED(CONFIG_PNP)) {
0392         rc = pnp_register_driver(&tis_pnp_driver);
0393         if (rc)
0394             goto err_pnp;
0395     }
0396 
0397     return 0;
0398 
0399 err_pnp:
0400     platform_driver_unregister(&tis_drv);
0401 err_platform:
0402     if (force_pdev)
0403         platform_device_unregister(force_pdev);
0404 err_force:
0405     return rc;
0406 }
0407 
0408 static void __exit cleanup_tis(void)
0409 {
0410     pnp_unregister_driver(&tis_pnp_driver);
0411     platform_driver_unregister(&tis_drv);
0412 
0413     if (force_pdev)
0414         platform_device_unregister(force_pdev);
0415 }
0416 
0417 module_init(init_tis);
0418 module_exit(cleanup_tis);
0419 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
0420 MODULE_DESCRIPTION("TPM Driver");
0421 MODULE_VERSION("2.0");
0422 MODULE_LICENSE("GPL");