xilinx_tmr_inject.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Xilinx TMR Inject IP.
  4. *
  5. * Copyright (C) 2022 Advanced Micro Devices, Inc.
  6. *
  7. * Description:
  8. * This driver is developed for TMR Inject IP,The Triple Modular Redundancy(TMR)
  9. * Inject provides fault injection.
  10. */
  11. #include <asm/xilinx_mb_manager.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/fault-inject.h>
  17. /* TMR Inject Register offsets */
  18. #define XTMR_INJECT_CR_OFFSET 0x0
  19. #define XTMR_INJECT_AIR_OFFSET 0x4
  20. #define XTMR_INJECT_IIR_OFFSET 0xC
  21. #define XTMR_INJECT_EAIR_OFFSET 0x10
  22. #define XTMR_INJECT_ERR_OFFSET 0x204
  23. /* Register Bitmasks/shifts */
  24. #define XTMR_INJECT_CR_CPUID_SHIFT 8
  25. #define XTMR_INJECT_CR_IE_SHIFT 10
  26. #define XTMR_INJECT_IIR_ADDR_MASK GENMASK(31, 16)
  27. #define XTMR_INJECT_MAGIC_MAX_VAL 255
  28. /**
  29. * struct xtmr_inject_dev - Driver data for TMR Inject
  30. * @regs: device physical base address
  31. * @magic: Magic hardware configuration value
  32. */
  33. struct xtmr_inject_dev {
  34. void __iomem *regs;
  35. u32 magic;
  36. };
  37. static DECLARE_FAULT_ATTR(inject_fault);
  38. static char *inject_request;
  39. module_param(inject_request, charp, 0);
  40. MODULE_PARM_DESC(inject_request, "default fault injection attributes");
  41. static struct dentry *dbgfs_root;
  42. /* IO accessors */
  43. static inline void xtmr_inject_write(struct xtmr_inject_dev *xtmr_inject,
  44. u32 addr, u32 value)
  45. {
  46. iowrite32(value, xtmr_inject->regs + addr);
  47. }
  48. static inline u32 xtmr_inject_read(struct xtmr_inject_dev *xtmr_inject,
  49. u32 addr)
  50. {
  51. return ioread32(xtmr_inject->regs + addr);
  52. }
  53. static int xtmr_inject_set(void *data, u64 val)
  54. {
  55. if (val != 1)
  56. return -EINVAL;
  57. xmb_inject_err();
  58. return 0;
  59. }
  60. DEFINE_DEBUGFS_ATTRIBUTE(xtmr_inject_fops, NULL, xtmr_inject_set, "%llu\n");
  61. static void xtmr_init_debugfs(struct xtmr_inject_dev *xtmr_inject)
  62. {
  63. struct dentry *dir;
  64. dbgfs_root = debugfs_create_dir("xtmr_inject", NULL);
  65. dir = fault_create_debugfs_attr("inject_fault", dbgfs_root,
  66. &inject_fault);
  67. debugfs_create_file("inject_fault", 0200, dir, NULL,
  68. &xtmr_inject_fops);
  69. }
  70. static void xtmr_inject_init(struct xtmr_inject_dev *xtmr_inject)
  71. {
  72. u32 cr_val;
  73. if (inject_request)
  74. setup_fault_attr(&inject_fault, inject_request);
  75. /* Allow fault injection */
  76. cr_val = xtmr_inject->magic |
  77. (1 << XTMR_INJECT_CR_IE_SHIFT) |
  78. (1 << XTMR_INJECT_CR_CPUID_SHIFT);
  79. xtmr_inject_write(xtmr_inject, XTMR_INJECT_CR_OFFSET,
  80. cr_val);
  81. /* Initialize the address inject and instruction inject registers */
  82. xtmr_inject_write(xtmr_inject, XTMR_INJECT_AIR_OFFSET,
  83. XMB_INJECT_ERR_OFFSET);
  84. xtmr_inject_write(xtmr_inject, XTMR_INJECT_IIR_OFFSET,
  85. XMB_INJECT_ERR_OFFSET & XTMR_INJECT_IIR_ADDR_MASK);
  86. }
  87. /**
  88. * xtmr_inject_probe - Driver probe function
  89. * @pdev: Pointer to the platform_device structure
  90. *
  91. * This is the driver probe routine. It does all the memory
  92. * allocation for the device.
  93. *
  94. * Return: 0 on success and failure value on error
  95. */
  96. static int xtmr_inject_probe(struct platform_device *pdev)
  97. {
  98. struct xtmr_inject_dev *xtmr_inject;
  99. int err;
  100. xtmr_inject = devm_kzalloc(&pdev->dev, sizeof(*xtmr_inject),
  101. GFP_KERNEL);
  102. if (!xtmr_inject)
  103. return -ENOMEM;
  104. xtmr_inject->regs = devm_platform_ioremap_resource(pdev, 0);
  105. if (IS_ERR(xtmr_inject->regs))
  106. return PTR_ERR(xtmr_inject->regs);
  107. err = of_property_read_u32(pdev->dev.of_node, "xlnx,magic",
  108. &xtmr_inject->magic);
  109. if (err < 0) {
  110. dev_err(&pdev->dev, "unable to read xlnx,magic property");
  111. return err;
  112. }
  113. if (xtmr_inject->magic > XTMR_INJECT_MAGIC_MAX_VAL) {
  114. dev_err(&pdev->dev, "invalid xlnx,magic property value");
  115. return -EINVAL;
  116. }
  117. /* Initialize TMR Inject */
  118. xtmr_inject_init(xtmr_inject);
  119. xtmr_init_debugfs(xtmr_inject);
  120. platform_set_drvdata(pdev, xtmr_inject);
  121. return 0;
  122. }
  123. static void xtmr_inject_remove(struct platform_device *pdev)
  124. {
  125. debugfs_remove_recursive(dbgfs_root);
  126. dbgfs_root = NULL;
  127. }
  128. static const struct of_device_id xtmr_inject_of_match[] = {
  129. {
  130. .compatible = "xlnx,tmr-inject-1.0",
  131. },
  132. { /* end of table */ }
  133. };
  134. MODULE_DEVICE_TABLE(of, xtmr_inject_of_match);
  135. static struct platform_driver xtmr_inject_driver = {
  136. .driver = {
  137. .name = "xilinx-tmr_inject",
  138. .of_match_table = xtmr_inject_of_match,
  139. },
  140. .probe = xtmr_inject_probe,
  141. .remove_new = xtmr_inject_remove,
  142. };
  143. module_platform_driver(xtmr_inject_driver);
  144. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  145. MODULE_DESCRIPTION("Xilinx TMR Inject Driver");
  146. MODULE_LICENSE("GPL");