of.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2012 IBM Corporation
  4. *
  5. * Author: Ashley Lai <ashleydlai@gmail.com>
  6. * Nayna Jain <nayna@linux.vnet.ibm.com>
  7. *
  8. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  9. *
  10. * Read the event log created by the firmware on PPC64
  11. */
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_reserved_mem.h>
  19. #include <linux/tpm_eventlog.h>
  20. #include "../tpm.h"
  21. #include "common.h"
  22. static int tpm_read_log_memory_region(struct tpm_chip *chip)
  23. {
  24. struct device_node *node;
  25. struct resource res;
  26. int rc;
  27. node = of_parse_phandle(chip->dev.parent->of_node, "memory-region", 0);
  28. if (!node)
  29. return -ENODEV;
  30. rc = of_address_to_resource(node, 0, &res);
  31. of_node_put(node);
  32. if (rc)
  33. return rc;
  34. chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
  35. MEMREMAP_WB);
  36. if (IS_ERR(chip->log.bios_event_log))
  37. return -ENOMEM;
  38. chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
  39. return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
  40. EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  41. }
  42. int tpm_read_log_of(struct tpm_chip *chip)
  43. {
  44. struct device_node *np;
  45. const u32 *sizep;
  46. const u64 *basep;
  47. struct tpm_bios_log *log;
  48. u32 size;
  49. u64 base;
  50. log = &chip->log;
  51. if (chip->dev.parent && chip->dev.parent->of_node)
  52. np = chip->dev.parent->of_node;
  53. else
  54. return -ENODEV;
  55. if (of_property_read_bool(np, "powered-while-suspended"))
  56. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  57. sizep = of_get_property(np, "linux,sml-size", NULL);
  58. basep = of_get_property(np, "linux,sml-base", NULL);
  59. if (sizep == NULL && basep == NULL)
  60. return tpm_read_log_memory_region(chip);
  61. if (sizep == NULL || basep == NULL)
  62. return -EIO;
  63. /*
  64. * For both vtpm/tpm, firmware has log addr and log size in big
  65. * endian format. But in case of vtpm, there is a method called
  66. * sml-handover which is run during kernel init even before
  67. * device tree is setup. This sml-handover function takes care
  68. * of endianness and writes to sml-base and sml-size in little
  69. * endian format. For this reason, vtpm doesn't need conversion
  70. * but physical tpm needs the conversion.
  71. */
  72. if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
  73. of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
  74. size = be32_to_cpup((__force __be32 *)sizep);
  75. base = be64_to_cpup((__force __be64 *)basep);
  76. } else {
  77. size = *sizep;
  78. base = *basep;
  79. }
  80. if (size == 0) {
  81. dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
  82. return -EIO;
  83. }
  84. log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
  85. if (!log->bios_event_log)
  86. return -ENOMEM;
  87. log->bios_event_log_end = log->bios_event_log + size;
  88. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  89. return EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
  90. return EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  91. }