Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * IBM Real-Time Linux driver
0004  *
0005  * Copyright (C) IBM Corporation, 2010
0006  *
0007  * Author: Keith Mannthey <kmannth@us.ibm.com>
0008  *         Vernon Mauery <vernux@us.ibm.com>
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/delay.h>
0015 #include <linux/module.h>
0016 #include <linux/io.h>
0017 #include <linux/dmi.h>
0018 #include <linux/efi.h>
0019 #include <linux/mutex.h>
0020 #include <asm/bios_ebda.h>
0021 
0022 #include <linux/io-64-nonatomic-lo-hi.h>
0023 
0024 static bool force;
0025 module_param(force, bool, 0);
0026 MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
0027 
0028 static bool debug;
0029 module_param(debug, bool, 0644);
0030 MODULE_PARM_DESC(debug, "Show debug output");
0031 
0032 MODULE_LICENSE("GPL");
0033 MODULE_AUTHOR("Keith Mannthey <kmmanth@us.ibm.com>");
0034 MODULE_AUTHOR("Vernon Mauery <vernux@us.ibm.com>");
0035 
0036 #define RTL_ADDR_TYPE_IO    1
0037 #define RTL_ADDR_TYPE_MMIO  2
0038 
0039 #define RTL_CMD_ENTER_PRTM  1
0040 #define RTL_CMD_EXIT_PRTM   2
0041 
0042 /* The RTL table as presented by the EBDA: */
0043 struct ibm_rtl_table {
0044     char signature[5]; /* signature should be "_RTL_" */
0045     u8 version;
0046     u8 rt_status;
0047     u8 command;
0048     u8 command_status;
0049     u8 cmd_address_type;
0050     u8 cmd_granularity;
0051     u8 cmd_offset;
0052     u16 reserve1;
0053     u32 cmd_port_address; /* platform dependent address */
0054     u32 cmd_port_value;   /* platform dependent value */
0055 } __attribute__((packed));
0056 
0057 /* to locate "_RTL_" signature do a masked 5-byte integer compare */
0058 #define RTL_SIGNATURE 0x0000005f4c54525fULL
0059 #define RTL_MASK      0x000000ffffffffffULL
0060 
0061 #define RTL_DEBUG(fmt, ...)             \
0062 do {                            \
0063     if (debug)                  \
0064         pr_info(fmt, ##__VA_ARGS__);        \
0065 } while (0)
0066 
0067 static DEFINE_MUTEX(rtl_lock);
0068 static struct ibm_rtl_table __iomem *rtl_table;
0069 static void __iomem *ebda_map;
0070 static void __iomem *rtl_cmd_addr;
0071 static u8 rtl_cmd_type;
0072 static u8 rtl_cmd_width;
0073 
0074 static void __iomem *rtl_port_map(phys_addr_t addr, unsigned long len)
0075 {
0076     if (rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
0077         return ioremap(addr, len);
0078     return ioport_map(addr, len);
0079 }
0080 
0081 static void rtl_port_unmap(void __iomem *addr)
0082 {
0083     if (addr && rtl_cmd_type == RTL_ADDR_TYPE_MMIO)
0084         iounmap(addr);
0085     else
0086         ioport_unmap(addr);
0087 }
0088 
0089 static int ibm_rtl_write(u8 value)
0090 {
0091     int ret = 0, count = 0;
0092     u32 cmd_port_val;
0093 
0094     RTL_DEBUG("%s(%d)\n", __func__, value);
0095 
0096     value = value == 1 ? RTL_CMD_ENTER_PRTM : RTL_CMD_EXIT_PRTM;
0097 
0098     mutex_lock(&rtl_lock);
0099 
0100     if (ioread8(&rtl_table->rt_status) != value) {
0101         iowrite8(value, &rtl_table->command);
0102 
0103         switch (rtl_cmd_width) {
0104         case 8:
0105             cmd_port_val = ioread8(&rtl_table->cmd_port_value);
0106             RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
0107             iowrite8((u8)cmd_port_val, rtl_cmd_addr);
0108             break;
0109         case 16:
0110             cmd_port_val = ioread16(&rtl_table->cmd_port_value);
0111             RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
0112             iowrite16((u16)cmd_port_val, rtl_cmd_addr);
0113             break;
0114         case 32:
0115             cmd_port_val = ioread32(&rtl_table->cmd_port_value);
0116             RTL_DEBUG("cmd_port_val = %u\n", cmd_port_val);
0117             iowrite32(cmd_port_val, rtl_cmd_addr);
0118             break;
0119         }
0120 
0121         while (ioread8(&rtl_table->command)) {
0122             msleep(10);
0123             if (count++ > 500) {
0124                 pr_err("Hardware not responding to "
0125                        "mode switch request\n");
0126                 ret = -EIO;
0127                 break;
0128             }
0129 
0130         }
0131 
0132         if (ioread8(&rtl_table->command_status)) {
0133             RTL_DEBUG("command_status reports failed command\n");
0134             ret = -EIO;
0135         }
0136     }
0137 
0138     mutex_unlock(&rtl_lock);
0139     return ret;
0140 }
0141 
0142 static ssize_t rtl_show_version(struct device *dev,
0143                                 struct device_attribute *attr,
0144                                 char *buf)
0145 {
0146     return sprintf(buf, "%d\n", (int)ioread8(&rtl_table->version));
0147 }
0148 
0149 static ssize_t rtl_show_state(struct device *dev,
0150                               struct device_attribute *attr,
0151                               char *buf)
0152 {
0153     return sprintf(buf, "%d\n", ioread8(&rtl_table->rt_status));
0154 }
0155 
0156 static ssize_t rtl_set_state(struct device *dev,
0157                              struct device_attribute *attr,
0158                              const char *buf,
0159                              size_t count)
0160 {
0161     ssize_t ret;
0162 
0163     if (count < 1 || count > 2)
0164         return -EINVAL;
0165 
0166     switch (buf[0]) {
0167     case '0':
0168         ret = ibm_rtl_write(0);
0169         break;
0170     case '1':
0171         ret = ibm_rtl_write(1);
0172         break;
0173     default:
0174         ret = -EINVAL;
0175     }
0176     if (ret >= 0)
0177         ret = count;
0178 
0179     return ret;
0180 }
0181 
0182 static struct bus_type rtl_subsys = {
0183     .name = "ibm_rtl",
0184     .dev_name = "ibm_rtl",
0185 };
0186 
0187 static DEVICE_ATTR(version, S_IRUGO, rtl_show_version, NULL);
0188 static DEVICE_ATTR(state, 0600, rtl_show_state, rtl_set_state);
0189 
0190 static struct device_attribute *rtl_attributes[] = {
0191     &dev_attr_version,
0192     &dev_attr_state,
0193     NULL
0194 };
0195 
0196 
0197 static int rtl_setup_sysfs(void) {
0198     int ret, i;
0199 
0200     ret = subsys_system_register(&rtl_subsys, NULL);
0201     if (!ret) {
0202         for (i = 0; rtl_attributes[i]; i ++)
0203             device_create_file(rtl_subsys.dev_root, rtl_attributes[i]);
0204     }
0205     return ret;
0206 }
0207 
0208 static void rtl_teardown_sysfs(void) {
0209     int i;
0210     for (i = 0; rtl_attributes[i]; i ++)
0211         device_remove_file(rtl_subsys.dev_root, rtl_attributes[i]);
0212     bus_unregister(&rtl_subsys);
0213 }
0214 
0215 
0216 static const struct dmi_system_id ibm_rtl_dmi_table[] __initconst = {
0217     {                                                  \
0218         .matches = {                               \
0219             DMI_MATCH(DMI_SYS_VENDOR, "IBM"),  \
0220         },                                         \
0221     },
0222     { }
0223 };
0224 
0225 static int __init ibm_rtl_init(void) {
0226     unsigned long ebda_addr, ebda_size;
0227     unsigned int ebda_kb;
0228     int ret = -ENODEV, i;
0229 
0230     if (force)
0231         pr_warn("module loaded by force\n");
0232     /* first ensure that we are running on IBM HW */
0233     else if (efi_enabled(EFI_BOOT) || !dmi_check_system(ibm_rtl_dmi_table))
0234         return -ENODEV;
0235 
0236     /* Get the address for the Extended BIOS Data Area */
0237     ebda_addr = get_bios_ebda();
0238     if (!ebda_addr) {
0239         RTL_DEBUG("no BIOS EBDA found\n");
0240         return -ENODEV;
0241     }
0242 
0243     ebda_map = ioremap(ebda_addr, 4);
0244     if (!ebda_map)
0245         return -ENOMEM;
0246 
0247     /* First word in the EDBA is the Size in KB */
0248     ebda_kb = ioread16(ebda_map);
0249     RTL_DEBUG("EBDA is %d kB\n", ebda_kb);
0250 
0251     if (ebda_kb == 0)
0252         goto out;
0253 
0254     iounmap(ebda_map);
0255     ebda_size = ebda_kb*1024;
0256 
0257     /* Remap the whole table */
0258     ebda_map = ioremap(ebda_addr, ebda_size);
0259     if (!ebda_map)
0260         return -ENOMEM;
0261 
0262     /* search for the _RTL_ signature at the start of the table */
0263     for (i = 0 ; i < ebda_size/sizeof(unsigned int); i++) {
0264         struct ibm_rtl_table __iomem * tmp;
0265         tmp = (struct ibm_rtl_table __iomem *) (ebda_map+i);
0266         if ((readq(&tmp->signature) & RTL_MASK) == RTL_SIGNATURE) {
0267             phys_addr_t addr;
0268             unsigned int plen;
0269             RTL_DEBUG("found RTL_SIGNATURE at %p\n", tmp);
0270             rtl_table = tmp;
0271             /* The address, value, width and offset are platform
0272              * dependent and found in the ibm_rtl_table */
0273             rtl_cmd_width = ioread8(&rtl_table->cmd_granularity);
0274             rtl_cmd_type = ioread8(&rtl_table->cmd_address_type);
0275             RTL_DEBUG("rtl_cmd_width = %u, rtl_cmd_type = %u\n",
0276                   rtl_cmd_width, rtl_cmd_type);
0277             addr = ioread32(&rtl_table->cmd_port_address);
0278             RTL_DEBUG("addr = %#llx\n", (unsigned long long)addr);
0279             plen = rtl_cmd_width/sizeof(char);
0280             rtl_cmd_addr = rtl_port_map(addr, plen);
0281             RTL_DEBUG("rtl_cmd_addr = %p\n", rtl_cmd_addr);
0282             if (!rtl_cmd_addr) {
0283                 ret = -ENOMEM;
0284                 break;
0285             }
0286             ret = rtl_setup_sysfs();
0287             break;
0288         }
0289     }
0290 
0291 out:
0292     if (ret) {
0293         iounmap(ebda_map);
0294         rtl_port_unmap(rtl_cmd_addr);
0295     }
0296 
0297     return ret;
0298 }
0299 
0300 static void __exit ibm_rtl_exit(void)
0301 {
0302     if (rtl_table) {
0303         RTL_DEBUG("cleaning up");
0304         /* do not leave the machine in SMI-free mode */
0305         ibm_rtl_write(0);
0306         /* unmap, unlink and remove all traces */
0307         rtl_teardown_sysfs();
0308         iounmap(ebda_map);
0309         rtl_port_unmap(rtl_cmd_addr);
0310     }
0311 }
0312 
0313 module_init(ibm_rtl_init);
0314 module_exit(ibm_rtl_exit);