Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * APEI Error INJection support
0004  *
0005  * EINJ provides a hardware error injection mechanism, this is useful
0006  * for debugging and testing of other APEI and RAS features.
0007  *
0008  * For more information about EINJ, please refer to ACPI Specification
0009  * version 4.0, section 17.5.
0010  *
0011  * Copyright 2009-2010 Intel Corp.
0012  *   Author: Huang Ying <ying.huang@intel.com>
0013  */
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/init.h>
0018 #include <linux/io.h>
0019 #include <linux/debugfs.h>
0020 #include <linux/seq_file.h>
0021 #include <linux/nmi.h>
0022 #include <linux/delay.h>
0023 #include <linux/mm.h>
0024 #include <asm/unaligned.h>
0025 
0026 #include "apei-internal.h"
0027 
0028 #undef pr_fmt
0029 #define pr_fmt(fmt) "EINJ: " fmt
0030 
0031 #define SLEEP_UNIT_MIN      1000            /* 1ms */
0032 #define SLEEP_UNIT_MAX      5000            /* 5ms */
0033 /* Firmware should respond within 1 seconds */
0034 #define FIRMWARE_TIMEOUT    (1 * USEC_PER_SEC)
0035 #define ACPI5_VENDOR_BIT    BIT(31)
0036 #define MEM_ERROR_MASK      (ACPI_EINJ_MEMORY_CORRECTABLE | \
0037                 ACPI_EINJ_MEMORY_UNCORRECTABLE | \
0038                 ACPI_EINJ_MEMORY_FATAL)
0039 
0040 /*
0041  * ACPI version 5 provides a SET_ERROR_TYPE_WITH_ADDRESS action.
0042  */
0043 static int acpi5;
0044 
0045 struct set_error_type_with_address {
0046     u32 type;
0047     u32 vendor_extension;
0048     u32 flags;
0049     u32 apicid;
0050     u64 memory_address;
0051     u64 memory_address_range;
0052     u32 pcie_sbdf;
0053 };
0054 enum {
0055     SETWA_FLAGS_APICID = 1,
0056     SETWA_FLAGS_MEM = 2,
0057     SETWA_FLAGS_PCIE_SBDF = 4,
0058 };
0059 
0060 /*
0061  * Vendor extensions for platform specific operations
0062  */
0063 struct vendor_error_type_extension {
0064     u32 length;
0065     u32 pcie_sbdf;
0066     u16 vendor_id;
0067     u16 device_id;
0068     u8  rev_id;
0069     u8  reserved[3];
0070 };
0071 
0072 static u32 notrigger;
0073 
0074 static u32 vendor_flags;
0075 static struct debugfs_blob_wrapper vendor_blob;
0076 static char vendor_dev[64];
0077 
0078 /*
0079  * Some BIOSes allow parameters to the SET_ERROR_TYPE entries in the
0080  * EINJ table through an unpublished extension. Use with caution as
0081  * most will ignore the parameter and make their own choice of address
0082  * for error injection.  This extension is used only if
0083  * param_extension module parameter is specified.
0084  */
0085 struct einj_parameter {
0086     u64 type;
0087     u64 reserved1;
0088     u64 reserved2;
0089     u64 param1;
0090     u64 param2;
0091 };
0092 
0093 #define EINJ_OP_BUSY            0x1
0094 #define EINJ_STATUS_SUCCESS     0x0
0095 #define EINJ_STATUS_FAIL        0x1
0096 #define EINJ_STATUS_INVAL       0x2
0097 
0098 #define EINJ_TAB_ENTRY(tab)                     \
0099     ((struct acpi_whea_header *)((char *)(tab) +            \
0100                     sizeof(struct acpi_table_einj)))
0101 
0102 static bool param_extension;
0103 module_param(param_extension, bool, 0);
0104 
0105 static struct acpi_table_einj *einj_tab;
0106 
0107 static struct apei_resources einj_resources;
0108 
0109 static struct apei_exec_ins_type einj_ins_type[] = {
0110     [ACPI_EINJ_READ_REGISTER] = {
0111         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0112         .run   = apei_exec_read_register,
0113     },
0114     [ACPI_EINJ_READ_REGISTER_VALUE] = {
0115         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0116         .run   = apei_exec_read_register_value,
0117     },
0118     [ACPI_EINJ_WRITE_REGISTER] = {
0119         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0120         .run   = apei_exec_write_register,
0121     },
0122     [ACPI_EINJ_WRITE_REGISTER_VALUE] = {
0123         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0124         .run   = apei_exec_write_register_value,
0125     },
0126     [ACPI_EINJ_NOOP] = {
0127         .flags = 0,
0128         .run   = apei_exec_noop,
0129     },
0130 };
0131 
0132 /*
0133  * Prevent EINJ interpreter to run simultaneously, because the
0134  * corresponding firmware implementation may not work properly when
0135  * invoked simultaneously.
0136  */
0137 static DEFINE_MUTEX(einj_mutex);
0138 
0139 static void *einj_param;
0140 
0141 static void einj_exec_ctx_init(struct apei_exec_context *ctx)
0142 {
0143     apei_exec_ctx_init(ctx, einj_ins_type, ARRAY_SIZE(einj_ins_type),
0144                EINJ_TAB_ENTRY(einj_tab), einj_tab->entries);
0145 }
0146 
0147 static int __einj_get_available_error_type(u32 *type)
0148 {
0149     struct apei_exec_context ctx;
0150     int rc;
0151 
0152     einj_exec_ctx_init(&ctx);
0153     rc = apei_exec_run(&ctx, ACPI_EINJ_GET_ERROR_TYPE);
0154     if (rc)
0155         return rc;
0156     *type = apei_exec_ctx_get_output(&ctx);
0157 
0158     return 0;
0159 }
0160 
0161 /* Get error injection capabilities of the platform */
0162 static int einj_get_available_error_type(u32 *type)
0163 {
0164     int rc;
0165 
0166     mutex_lock(&einj_mutex);
0167     rc = __einj_get_available_error_type(type);
0168     mutex_unlock(&einj_mutex);
0169 
0170     return rc;
0171 }
0172 
0173 static int einj_timedout(u64 *t)
0174 {
0175     if ((s64)*t < SLEEP_UNIT_MIN) {
0176         pr_warn(FW_WARN "Firmware does not respond in time\n");
0177         return 1;
0178     }
0179     *t -= SLEEP_UNIT_MIN;
0180     usleep_range(SLEEP_UNIT_MIN, SLEEP_UNIT_MAX);
0181 
0182     return 0;
0183 }
0184 
0185 static void check_vendor_extension(u64 paddr,
0186                    struct set_error_type_with_address *v5param)
0187 {
0188     int offset = v5param->vendor_extension;
0189     struct  vendor_error_type_extension *v;
0190     u32 sbdf;
0191 
0192     if (!offset)
0193         return;
0194     v = acpi_os_map_iomem(paddr + offset, sizeof(*v));
0195     if (!v)
0196         return;
0197     sbdf = v->pcie_sbdf;
0198     sprintf(vendor_dev, "%x:%x:%x.%x vendor_id=%x device_id=%x rev_id=%x\n",
0199         sbdf >> 24, (sbdf >> 16) & 0xff,
0200         (sbdf >> 11) & 0x1f, (sbdf >> 8) & 0x7,
0201          v->vendor_id, v->device_id, v->rev_id);
0202     acpi_os_unmap_iomem(v, sizeof(*v));
0203 }
0204 
0205 static void *einj_get_parameter_address(void)
0206 {
0207     int i;
0208     u64 pa_v4 = 0, pa_v5 = 0;
0209     struct acpi_whea_header *entry;
0210 
0211     entry = EINJ_TAB_ENTRY(einj_tab);
0212     for (i = 0; i < einj_tab->entries; i++) {
0213         if (entry->action == ACPI_EINJ_SET_ERROR_TYPE &&
0214             entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
0215             entry->register_region.space_id ==
0216             ACPI_ADR_SPACE_SYSTEM_MEMORY)
0217             pa_v4 = get_unaligned(&entry->register_region.address);
0218         if (entry->action == ACPI_EINJ_SET_ERROR_TYPE_WITH_ADDRESS &&
0219             entry->instruction == ACPI_EINJ_WRITE_REGISTER &&
0220             entry->register_region.space_id ==
0221             ACPI_ADR_SPACE_SYSTEM_MEMORY)
0222             pa_v5 = get_unaligned(&entry->register_region.address);
0223         entry++;
0224     }
0225     if (pa_v5) {
0226         struct set_error_type_with_address *v5param;
0227 
0228         v5param = acpi_os_map_iomem(pa_v5, sizeof(*v5param));
0229         if (v5param) {
0230             acpi5 = 1;
0231             check_vendor_extension(pa_v5, v5param);
0232             return v5param;
0233         }
0234     }
0235     if (param_extension && pa_v4) {
0236         struct einj_parameter *v4param;
0237 
0238         v4param = acpi_os_map_iomem(pa_v4, sizeof(*v4param));
0239         if (!v4param)
0240             return NULL;
0241         if (v4param->reserved1 || v4param->reserved2) {
0242             acpi_os_unmap_iomem(v4param, sizeof(*v4param));
0243             return NULL;
0244         }
0245         return v4param;
0246     }
0247 
0248     return NULL;
0249 }
0250 
0251 /* do sanity check to trigger table */
0252 static int einj_check_trigger_header(struct acpi_einj_trigger *trigger_tab)
0253 {
0254     if (trigger_tab->header_size != sizeof(struct acpi_einj_trigger))
0255         return -EINVAL;
0256     if (trigger_tab->table_size > PAGE_SIZE ||
0257         trigger_tab->table_size < trigger_tab->header_size)
0258         return -EINVAL;
0259     if (trigger_tab->entry_count !=
0260         (trigger_tab->table_size - trigger_tab->header_size) /
0261         sizeof(struct acpi_einj_entry))
0262         return -EINVAL;
0263 
0264     return 0;
0265 }
0266 
0267 static struct acpi_generic_address *einj_get_trigger_parameter_region(
0268     struct acpi_einj_trigger *trigger_tab, u64 param1, u64 param2)
0269 {
0270     int i;
0271     struct acpi_whea_header *entry;
0272 
0273     entry = (struct acpi_whea_header *)
0274         ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
0275     for (i = 0; i < trigger_tab->entry_count; i++) {
0276         if (entry->action == ACPI_EINJ_TRIGGER_ERROR &&
0277         entry->instruction <= ACPI_EINJ_WRITE_REGISTER_VALUE &&
0278         entry->register_region.space_id ==
0279             ACPI_ADR_SPACE_SYSTEM_MEMORY &&
0280         (entry->register_region.address & param2) == (param1 & param2))
0281             return &entry->register_region;
0282         entry++;
0283     }
0284 
0285     return NULL;
0286 }
0287 /* Execute instructions in trigger error action table */
0288 static int __einj_error_trigger(u64 trigger_paddr, u32 type,
0289                 u64 param1, u64 param2)
0290 {
0291     struct acpi_einj_trigger *trigger_tab = NULL;
0292     struct apei_exec_context trigger_ctx;
0293     struct apei_resources trigger_resources;
0294     struct acpi_whea_header *trigger_entry;
0295     struct resource *r;
0296     u32 table_size;
0297     int rc = -EIO;
0298     struct acpi_generic_address *trigger_param_region = NULL;
0299 
0300     r = request_mem_region(trigger_paddr, sizeof(*trigger_tab),
0301                    "APEI EINJ Trigger Table");
0302     if (!r) {
0303         pr_err("Can not request [mem %#010llx-%#010llx] for Trigger table\n",
0304                (unsigned long long)trigger_paddr,
0305                (unsigned long long)trigger_paddr +
0306                 sizeof(*trigger_tab) - 1);
0307         goto out;
0308     }
0309     trigger_tab = ioremap_cache(trigger_paddr, sizeof(*trigger_tab));
0310     if (!trigger_tab) {
0311         pr_err("Failed to map trigger table!\n");
0312         goto out_rel_header;
0313     }
0314     rc = einj_check_trigger_header(trigger_tab);
0315     if (rc) {
0316         pr_warn(FW_BUG "Invalid trigger error action table.\n");
0317         goto out_rel_header;
0318     }
0319 
0320     /* No action structures in the TRIGGER_ERROR table, nothing to do */
0321     if (!trigger_tab->entry_count)
0322         goto out_rel_header;
0323 
0324     rc = -EIO;
0325     table_size = trigger_tab->table_size;
0326     r = request_mem_region(trigger_paddr + sizeof(*trigger_tab),
0327                    table_size - sizeof(*trigger_tab),
0328                    "APEI EINJ Trigger Table");
0329     if (!r) {
0330         pr_err("Can not request [mem %#010llx-%#010llx] for Trigger Table Entry\n",
0331                (unsigned long long)trigger_paddr + sizeof(*trigger_tab),
0332                (unsigned long long)trigger_paddr + table_size - 1);
0333         goto out_rel_header;
0334     }
0335     iounmap(trigger_tab);
0336     trigger_tab = ioremap_cache(trigger_paddr, table_size);
0337     if (!trigger_tab) {
0338         pr_err("Failed to map trigger table!\n");
0339         goto out_rel_entry;
0340     }
0341     trigger_entry = (struct acpi_whea_header *)
0342         ((char *)trigger_tab + sizeof(struct acpi_einj_trigger));
0343     apei_resources_init(&trigger_resources);
0344     apei_exec_ctx_init(&trigger_ctx, einj_ins_type,
0345                ARRAY_SIZE(einj_ins_type),
0346                trigger_entry, trigger_tab->entry_count);
0347     rc = apei_exec_collect_resources(&trigger_ctx, &trigger_resources);
0348     if (rc)
0349         goto out_fini;
0350     rc = apei_resources_sub(&trigger_resources, &einj_resources);
0351     if (rc)
0352         goto out_fini;
0353     /*
0354      * Some firmware will access target address specified in
0355      * param1 to trigger the error when injecting memory error.
0356      * This will cause resource conflict with regular memory.  So
0357      * remove it from trigger table resources.
0358      */
0359     if ((param_extension || acpi5) && (type & MEM_ERROR_MASK) && param2) {
0360         struct apei_resources addr_resources;
0361         apei_resources_init(&addr_resources);
0362         trigger_param_region = einj_get_trigger_parameter_region(
0363             trigger_tab, param1, param2);
0364         if (trigger_param_region) {
0365             rc = apei_resources_add(&addr_resources,
0366                 trigger_param_region->address,
0367                 trigger_param_region->bit_width/8, true);
0368             if (rc)
0369                 goto out_fini;
0370             rc = apei_resources_sub(&trigger_resources,
0371                     &addr_resources);
0372         }
0373         apei_resources_fini(&addr_resources);
0374         if (rc)
0375             goto out_fini;
0376     }
0377     rc = apei_resources_request(&trigger_resources, "APEI EINJ Trigger");
0378     if (rc)
0379         goto out_fini;
0380     rc = apei_exec_pre_map_gars(&trigger_ctx);
0381     if (rc)
0382         goto out_release;
0383 
0384     rc = apei_exec_run(&trigger_ctx, ACPI_EINJ_TRIGGER_ERROR);
0385 
0386     apei_exec_post_unmap_gars(&trigger_ctx);
0387 out_release:
0388     apei_resources_release(&trigger_resources);
0389 out_fini:
0390     apei_resources_fini(&trigger_resources);
0391 out_rel_entry:
0392     release_mem_region(trigger_paddr + sizeof(*trigger_tab),
0393                table_size - sizeof(*trigger_tab));
0394 out_rel_header:
0395     release_mem_region(trigger_paddr, sizeof(*trigger_tab));
0396 out:
0397     if (trigger_tab)
0398         iounmap(trigger_tab);
0399 
0400     return rc;
0401 }
0402 
0403 static int __einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
0404                    u64 param3, u64 param4)
0405 {
0406     struct apei_exec_context ctx;
0407     u64 val, trigger_paddr, timeout = FIRMWARE_TIMEOUT;
0408     int rc;
0409 
0410     einj_exec_ctx_init(&ctx);
0411 
0412     rc = apei_exec_run_optional(&ctx, ACPI_EINJ_BEGIN_OPERATION);
0413     if (rc)
0414         return rc;
0415     apei_exec_ctx_set_input(&ctx, type);
0416     if (acpi5) {
0417         struct set_error_type_with_address *v5param = einj_param;
0418 
0419         v5param->type = type;
0420         if (type & ACPI5_VENDOR_BIT) {
0421             switch (vendor_flags) {
0422             case SETWA_FLAGS_APICID:
0423                 v5param->apicid = param1;
0424                 break;
0425             case SETWA_FLAGS_MEM:
0426                 v5param->memory_address = param1;
0427                 v5param->memory_address_range = param2;
0428                 break;
0429             case SETWA_FLAGS_PCIE_SBDF:
0430                 v5param->pcie_sbdf = param1;
0431                 break;
0432             }
0433             v5param->flags = vendor_flags;
0434         } else if (flags) {
0435                 v5param->flags = flags;
0436                 v5param->memory_address = param1;
0437                 v5param->memory_address_range = param2;
0438                 v5param->apicid = param3;
0439                 v5param->pcie_sbdf = param4;
0440         } else {
0441             switch (type) {
0442             case ACPI_EINJ_PROCESSOR_CORRECTABLE:
0443             case ACPI_EINJ_PROCESSOR_UNCORRECTABLE:
0444             case ACPI_EINJ_PROCESSOR_FATAL:
0445                 v5param->apicid = param1;
0446                 v5param->flags = SETWA_FLAGS_APICID;
0447                 break;
0448             case ACPI_EINJ_MEMORY_CORRECTABLE:
0449             case ACPI_EINJ_MEMORY_UNCORRECTABLE:
0450             case ACPI_EINJ_MEMORY_FATAL:
0451                 v5param->memory_address = param1;
0452                 v5param->memory_address_range = param2;
0453                 v5param->flags = SETWA_FLAGS_MEM;
0454                 break;
0455             case ACPI_EINJ_PCIX_CORRECTABLE:
0456             case ACPI_EINJ_PCIX_UNCORRECTABLE:
0457             case ACPI_EINJ_PCIX_FATAL:
0458                 v5param->pcie_sbdf = param1;
0459                 v5param->flags = SETWA_FLAGS_PCIE_SBDF;
0460                 break;
0461             }
0462         }
0463     } else {
0464         rc = apei_exec_run(&ctx, ACPI_EINJ_SET_ERROR_TYPE);
0465         if (rc)
0466             return rc;
0467         if (einj_param) {
0468             struct einj_parameter *v4param = einj_param;
0469             v4param->param1 = param1;
0470             v4param->param2 = param2;
0471         }
0472     }
0473     rc = apei_exec_run(&ctx, ACPI_EINJ_EXECUTE_OPERATION);
0474     if (rc)
0475         return rc;
0476     for (;;) {
0477         rc = apei_exec_run(&ctx, ACPI_EINJ_CHECK_BUSY_STATUS);
0478         if (rc)
0479             return rc;
0480         val = apei_exec_ctx_get_output(&ctx);
0481         if (!(val & EINJ_OP_BUSY))
0482             break;
0483         if (einj_timedout(&timeout))
0484             return -EIO;
0485     }
0486     rc = apei_exec_run(&ctx, ACPI_EINJ_GET_COMMAND_STATUS);
0487     if (rc)
0488         return rc;
0489     val = apei_exec_ctx_get_output(&ctx);
0490     if (val != EINJ_STATUS_SUCCESS)
0491         return -EBUSY;
0492 
0493     rc = apei_exec_run(&ctx, ACPI_EINJ_GET_TRIGGER_TABLE);
0494     if (rc)
0495         return rc;
0496     trigger_paddr = apei_exec_ctx_get_output(&ctx);
0497     if (notrigger == 0) {
0498         rc = __einj_error_trigger(trigger_paddr, type, param1, param2);
0499         if (rc)
0500             return rc;
0501     }
0502     rc = apei_exec_run_optional(&ctx, ACPI_EINJ_END_OPERATION);
0503 
0504     return rc;
0505 }
0506 
0507 /* Inject the specified hardware error */
0508 static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
0509                  u64 param3, u64 param4)
0510 {
0511     int rc;
0512     u64 base_addr, size;
0513 
0514     /* If user manually set "flags", make sure it is legal */
0515     if (flags && (flags &
0516         ~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
0517         return -EINVAL;
0518 
0519     /*
0520      * We need extra sanity checks for memory errors.
0521      * Other types leap directly to injection.
0522      */
0523 
0524     /* ensure param1/param2 existed */
0525     if (!(param_extension || acpi5))
0526         goto inject;
0527 
0528     /* ensure injection is memory related */
0529     if (type & ACPI5_VENDOR_BIT) {
0530         if (vendor_flags != SETWA_FLAGS_MEM)
0531             goto inject;
0532     } else if (!(type & MEM_ERROR_MASK) && !(flags & SETWA_FLAGS_MEM))
0533         goto inject;
0534 
0535     /*
0536      * Disallow crazy address masks that give BIOS leeway to pick
0537      * injection address almost anywhere. Insist on page or
0538      * better granularity and that target address is normal RAM or
0539      * NVDIMM.
0540      */
0541     base_addr = param1 & param2;
0542     size = ~param2 + 1;
0543 
0544     if (((param2 & PAGE_MASK) != PAGE_MASK) ||
0545         ((region_intersects(base_addr, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)
0546                 != REGION_INTERSECTS) &&
0547          (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY)
0548                 != REGION_INTERSECTS) &&
0549          (region_intersects(base_addr, size, IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED)
0550                 != REGION_INTERSECTS) &&
0551          !arch_is_platform_page(base_addr)))
0552         return -EINVAL;
0553 
0554     if (is_zero_pfn(base_addr >> PAGE_SHIFT))
0555         return -EADDRINUSE;
0556 
0557 inject:
0558     mutex_lock(&einj_mutex);
0559     rc = __einj_error_inject(type, flags, param1, param2, param3, param4);
0560     mutex_unlock(&einj_mutex);
0561 
0562     return rc;
0563 }
0564 
0565 static u32 error_type;
0566 static u32 error_flags;
0567 static u64 error_param1;
0568 static u64 error_param2;
0569 static u64 error_param3;
0570 static u64 error_param4;
0571 static struct dentry *einj_debug_dir;
0572 
0573 static int available_error_type_show(struct seq_file *m, void *v)
0574 {
0575     int rc;
0576     u32 available_error_type = 0;
0577 
0578     rc = einj_get_available_error_type(&available_error_type);
0579     if (rc)
0580         return rc;
0581     if (available_error_type & 0x0001)
0582         seq_printf(m, "0x00000001\tProcessor Correctable\n");
0583     if (available_error_type & 0x0002)
0584         seq_printf(m, "0x00000002\tProcessor Uncorrectable non-fatal\n");
0585     if (available_error_type & 0x0004)
0586         seq_printf(m, "0x00000004\tProcessor Uncorrectable fatal\n");
0587     if (available_error_type & 0x0008)
0588         seq_printf(m, "0x00000008\tMemory Correctable\n");
0589     if (available_error_type & 0x0010)
0590         seq_printf(m, "0x00000010\tMemory Uncorrectable non-fatal\n");
0591     if (available_error_type & 0x0020)
0592         seq_printf(m, "0x00000020\tMemory Uncorrectable fatal\n");
0593     if (available_error_type & 0x0040)
0594         seq_printf(m, "0x00000040\tPCI Express Correctable\n");
0595     if (available_error_type & 0x0080)
0596         seq_printf(m, "0x00000080\tPCI Express Uncorrectable non-fatal\n");
0597     if (available_error_type & 0x0100)
0598         seq_printf(m, "0x00000100\tPCI Express Uncorrectable fatal\n");
0599     if (available_error_type & 0x0200)
0600         seq_printf(m, "0x00000200\tPlatform Correctable\n");
0601     if (available_error_type & 0x0400)
0602         seq_printf(m, "0x00000400\tPlatform Uncorrectable non-fatal\n");
0603     if (available_error_type & 0x0800)
0604         seq_printf(m, "0x00000800\tPlatform Uncorrectable fatal\n");
0605 
0606     return 0;
0607 }
0608 
0609 DEFINE_SHOW_ATTRIBUTE(available_error_type);
0610 
0611 static int error_type_get(void *data, u64 *val)
0612 {
0613     *val = error_type;
0614 
0615     return 0;
0616 }
0617 
0618 static int error_type_set(void *data, u64 val)
0619 {
0620     int rc;
0621     u32 available_error_type = 0;
0622     u32 tval, vendor;
0623 
0624     /*
0625      * Vendor defined types have 0x80000000 bit set, and
0626      * are not enumerated by ACPI_EINJ_GET_ERROR_TYPE
0627      */
0628     vendor = val & ACPI5_VENDOR_BIT;
0629     tval = val & 0x7fffffff;
0630 
0631     /* Only one error type can be specified */
0632     if (tval & (tval - 1))
0633         return -EINVAL;
0634     if (!vendor) {
0635         rc = einj_get_available_error_type(&available_error_type);
0636         if (rc)
0637             return rc;
0638         if (!(val & available_error_type))
0639             return -EINVAL;
0640     }
0641     error_type = val;
0642 
0643     return 0;
0644 }
0645 
0646 DEFINE_DEBUGFS_ATTRIBUTE(error_type_fops, error_type_get, error_type_set,
0647              "0x%llx\n");
0648 
0649 static int error_inject_set(void *data, u64 val)
0650 {
0651     if (!error_type)
0652         return -EINVAL;
0653 
0654     return einj_error_inject(error_type, error_flags, error_param1, error_param2,
0655         error_param3, error_param4);
0656 }
0657 
0658 DEFINE_DEBUGFS_ATTRIBUTE(error_inject_fops, NULL, error_inject_set, "%llu\n");
0659 
0660 static int einj_check_table(struct acpi_table_einj *einj_tab)
0661 {
0662     if ((einj_tab->header_length !=
0663          (sizeof(struct acpi_table_einj) - sizeof(einj_tab->header)))
0664         && (einj_tab->header_length != sizeof(struct acpi_table_einj)))
0665         return -EINVAL;
0666     if (einj_tab->header.length < sizeof(struct acpi_table_einj))
0667         return -EINVAL;
0668     if (einj_tab->entries !=
0669         (einj_tab->header.length - sizeof(struct acpi_table_einj)) /
0670         sizeof(struct acpi_einj_entry))
0671         return -EINVAL;
0672 
0673     return 0;
0674 }
0675 
0676 static int __init einj_init(void)
0677 {
0678     int rc;
0679     acpi_status status;
0680     struct apei_exec_context ctx;
0681 
0682     if (acpi_disabled) {
0683         pr_info("ACPI disabled.\n");
0684         return -ENODEV;
0685     }
0686 
0687     status = acpi_get_table(ACPI_SIG_EINJ, 0,
0688                 (struct acpi_table_header **)&einj_tab);
0689     if (status == AE_NOT_FOUND) {
0690         pr_warn("EINJ table not found.\n");
0691         return -ENODEV;
0692     }
0693     else if (ACPI_FAILURE(status)) {
0694         pr_err("Failed to get EINJ table: %s\n",
0695                 acpi_format_exception(status));
0696         return -EINVAL;
0697     }
0698 
0699     rc = einj_check_table(einj_tab);
0700     if (rc) {
0701         pr_warn(FW_BUG "Invalid EINJ table.\n");
0702         goto err_put_table;
0703     }
0704 
0705     rc = -ENOMEM;
0706     einj_debug_dir = debugfs_create_dir("einj", apei_get_debugfs_dir());
0707 
0708     debugfs_create_file("available_error_type", S_IRUSR, einj_debug_dir,
0709                 NULL, &available_error_type_fops);
0710     debugfs_create_file_unsafe("error_type", 0600, einj_debug_dir,
0711                    NULL, &error_type_fops);
0712     debugfs_create_file_unsafe("error_inject", 0200, einj_debug_dir,
0713                    NULL, &error_inject_fops);
0714 
0715     apei_resources_init(&einj_resources);
0716     einj_exec_ctx_init(&ctx);
0717     rc = apei_exec_collect_resources(&ctx, &einj_resources);
0718     if (rc) {
0719         pr_err("Error collecting EINJ resources.\n");
0720         goto err_fini;
0721     }
0722 
0723     rc = apei_resources_request(&einj_resources, "APEI EINJ");
0724     if (rc) {
0725         pr_err("Error requesting memory/port resources.\n");
0726         goto err_fini;
0727     }
0728 
0729     rc = apei_exec_pre_map_gars(&ctx);
0730     if (rc) {
0731         pr_err("Error pre-mapping GARs.\n");
0732         goto err_release;
0733     }
0734 
0735     einj_param = einj_get_parameter_address();
0736     if ((param_extension || acpi5) && einj_param) {
0737         debugfs_create_x32("flags", S_IRUSR | S_IWUSR, einj_debug_dir,
0738                    &error_flags);
0739         debugfs_create_x64("param1", S_IRUSR | S_IWUSR, einj_debug_dir,
0740                    &error_param1);
0741         debugfs_create_x64("param2", S_IRUSR | S_IWUSR, einj_debug_dir,
0742                    &error_param2);
0743         debugfs_create_x64("param3", S_IRUSR | S_IWUSR, einj_debug_dir,
0744                    &error_param3);
0745         debugfs_create_x64("param4", S_IRUSR | S_IWUSR, einj_debug_dir,
0746                    &error_param4);
0747         debugfs_create_x32("notrigger", S_IRUSR | S_IWUSR,
0748                    einj_debug_dir, &notrigger);
0749     }
0750 
0751     if (vendor_dev[0]) {
0752         vendor_blob.data = vendor_dev;
0753         vendor_blob.size = strlen(vendor_dev);
0754         debugfs_create_blob("vendor", S_IRUSR, einj_debug_dir,
0755                     &vendor_blob);
0756         debugfs_create_x32("vendor_flags", S_IRUSR | S_IWUSR,
0757                    einj_debug_dir, &vendor_flags);
0758     }
0759 
0760     pr_info("Error INJection is initialized.\n");
0761 
0762     return 0;
0763 
0764 err_release:
0765     apei_resources_release(&einj_resources);
0766 err_fini:
0767     apei_resources_fini(&einj_resources);
0768     debugfs_remove_recursive(einj_debug_dir);
0769 err_put_table:
0770     acpi_put_table((struct acpi_table_header *)einj_tab);
0771 
0772     return rc;
0773 }
0774 
0775 static void __exit einj_exit(void)
0776 {
0777     struct apei_exec_context ctx;
0778 
0779     if (einj_param) {
0780         acpi_size size = (acpi5) ?
0781             sizeof(struct set_error_type_with_address) :
0782             sizeof(struct einj_parameter);
0783 
0784         acpi_os_unmap_iomem(einj_param, size);
0785     }
0786     einj_exec_ctx_init(&ctx);
0787     apei_exec_post_unmap_gars(&ctx);
0788     apei_resources_release(&einj_resources);
0789     apei_resources_fini(&einj_resources);
0790     debugfs_remove_recursive(einj_debug_dir);
0791     acpi_put_table((struct acpi_table_header *)einj_tab);
0792 }
0793 
0794 module_init(einj_init);
0795 module_exit(einj_exit);
0796 
0797 MODULE_AUTHOR("Huang Ying");
0798 MODULE_DESCRIPTION("APEI Error INJection support");
0799 MODULE_LICENSE("GPL");