]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/platform/x86/dell-smbios-smm.c
53eabb14fb481b258836d913972979e65e47a1dd
[mirror_ubuntu-jammy-kernel.git] / drivers / platform / x86 / dell-smbios-smm.c
1 /*
2 * SMI methods for use with dell-smbios
3 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
7 * Copyright (c) 2017 Dell Inc.
8 *
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.
12 */
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/dmi.h>
16 #include <linux/gfp.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/platform_device.h>
21 #include "../../firmware/dcdbas.h"
22 #include "dell-smbios.h"
23
24 static int da_command_address;
25 static int da_command_code;
26 static struct calling_interface_buffer *buffer;
27 struct platform_device *platform_device;
28 static DEFINE_MUTEX(smm_mutex);
29
30 static const struct dmi_system_id dell_device_table[] __initconst = {
31 {
32 .ident = "Dell laptop",
33 .matches = {
34 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
35 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
36 },
37 },
38 {
39 .matches = {
40 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
41 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
42 },
43 },
44 {
45 .matches = {
46 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
47 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
48 },
49 },
50 {
51 .ident = "Dell Computer Corporation",
52 .matches = {
53 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
54 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
55 },
56 },
57 { }
58 };
59 MODULE_DEVICE_TABLE(dmi, dell_device_table);
60
61 static void __init parse_da_table(const struct dmi_header *dm)
62 {
63 struct calling_interface_structure *table =
64 container_of(dm, struct calling_interface_structure, header);
65
66 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
67 * 6 bytes of entry
68 */
69 if (dm->length < 17)
70 return;
71
72 da_command_address = table->cmdIOAddress;
73 da_command_code = table->cmdIOCode;
74 }
75
76 static void __init find_cmd_address(const struct dmi_header *dm, void *dummy)
77 {
78 switch (dm->type) {
79 case 0xda: /* Calling interface */
80 parse_da_table(dm);
81 break;
82 }
83 }
84
85 int dell_smbios_smm_call(struct calling_interface_buffer *input)
86 {
87 struct smi_cmd command;
88 size_t size;
89
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;
96
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);
102 return 0;
103 }
104
105 static int __init dell_smbios_smm_init(void)
106 {
107 int ret;
108 /*
109 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
110 * is passed to SMI handler.
111 */
112 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
113 if (!buffer)
114 return -ENOMEM;
115
116 dmi_walk(find_cmd_address, NULL);
117
118 platform_device = platform_device_alloc("dell-smbios", 1);
119 if (!platform_device) {
120 ret = -ENOMEM;
121 goto fail_platform_device_alloc;
122 }
123
124 ret = platform_device_add(platform_device);
125 if (ret)
126 goto fail_platform_device_add;
127
128 ret = dell_smbios_register_device(&platform_device->dev,
129 &dell_smbios_smm_call);
130 if (ret)
131 goto fail_register;
132
133 return 0;
134
135 fail_register:
136 platform_device_del(platform_device);
137
138 fail_platform_device_add:
139 platform_device_put(platform_device);
140
141 fail_platform_device_alloc:
142 free_page((unsigned long)buffer);
143 return ret;
144 }
145
146 static void __exit dell_smbios_smm_exit(void)
147 {
148 if (platform_device) {
149 dell_smbios_unregister_device(&platform_device->dev);
150 platform_device_unregister(platform_device);
151 free_page((unsigned long)buffer);
152 }
153 }
154
155 subsys_initcall(dell_smbios_smm_init);
156 module_exit(dell_smbios_smm_exit);
157
158 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
159 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
160 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
161 MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
162 MODULE_DESCRIPTION("Dell SMBIOS communications over SMI");
163 MODULE_LICENSE("GPL");