2 * SMI methods for use with dell-smbios
7 * Copyright (c) 2017 Dell Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/dmi.h>
16 #include <linux/gfp.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/platform_device.h>
22 #include "dell-smbios.h"
24 static int da_command_address;
25 static int da_command_code;
26 static struct calling_interface_buffer *buffer;
27 static struct platform_device *platform_device;
28 static DEFINE_MUTEX(smm_mutex);
30 static const struct dmi_system_id dell_device_table[] __initconst = {
32 .ident = "Dell laptop",
34 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
35 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
40 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
41 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
46 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
47 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
51 .ident = "Dell Computer Corporation",
53 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
54 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
59 MODULE_DEVICE_TABLE(dmi, dell_device_table);
61 static void parse_da_table(const struct dmi_header *dm)
63 struct calling_interface_structure *table =
64 container_of(dm, struct calling_interface_structure, header);
66 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
72 da_command_address = table->cmdIOAddress;
73 da_command_code = table->cmdIOCode;
76 static void find_cmd_address(const struct dmi_header *dm, void *dummy)
79 case 0xda: /* Calling interface */
85 static int dell_smbios_smm_call(struct calling_interface_buffer *input)
87 struct smi_cmd command;
90 size = sizeof(struct calling_interface_buffer);
91 command.magic = SMI_CMD_MAGIC;
92 command.command_address = da_command_address;
93 command.command_code = da_command_code;
94 command.ebx = virt_to_phys(buffer);
95 command.ecx = 0x42534931;
97 mutex_lock(&smm_mutex);
98 memcpy(buffer, input, size);
99 dcdbas_smi_request(&command);
100 memcpy(input, buffer, size);
101 mutex_unlock(&smm_mutex);
105 /* When enabled this indicates that SMM won't work */
106 static bool test_wsmt_enabled(void)
108 struct calling_interface_token *wsmt;
110 /* if token doesn't exist, SMM will work */
111 wsmt = dell_smbios_find_token(WSMT_EN_TOKEN);
115 /* If token exists, try to access over SMM but set a dummy return.
116 * - If WSMT disabled it will be overwritten by SMM
117 * - If WSMT enabled then dummy value will remain
119 buffer->cmd_class = CLASS_TOKEN_READ;
120 buffer->cmd_select = SELECT_TOKEN_STD;
121 memset(buffer, 0, sizeof(struct calling_interface_buffer));
122 buffer->input[0] = wsmt->location;
123 buffer->output[0] = 99;
124 dell_smbios_smm_call(buffer);
125 if (buffer->output[0] == 99)
131 int init_dell_smbios_smm(void)
135 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
136 * is passed to SMI handler.
138 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
142 dmi_walk(find_cmd_address, NULL);
144 if (test_wsmt_enabled()) {
145 pr_debug("Disabling due to WSMT enabled\n");
150 platform_device = platform_device_alloc("dell-smbios", 1);
151 if (!platform_device) {
153 goto fail_platform_device_alloc;
156 ret = platform_device_add(platform_device);
158 goto fail_platform_device_add;
160 ret = dell_smbios_register_device(&platform_device->dev,
161 &dell_smbios_smm_call);
168 platform_device_del(platform_device);
170 fail_platform_device_add:
171 platform_device_put(platform_device);
174 fail_platform_device_alloc:
175 free_page((unsigned long)buffer);
179 void exit_dell_smbios_smm(void)
181 if (platform_device) {
182 dell_smbios_unregister_device(&platform_device->dev);
183 platform_device_unregister(platform_device);
184 free_page((unsigned long)buffer);