blob: 53eabb14fb481b258836d913972979e65e47a1dd [file] [log] [blame]
Mario Limonciello549b4932017-11-01 14:25:31 -05001/*
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
24static int da_command_address;
25static int da_command_code;
26static struct calling_interface_buffer *buffer;
27struct platform_device *platform_device;
28static DEFINE_MUTEX(smm_mutex);
29
30static 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};
59MODULE_DEVICE_TABLE(dmi, dell_device_table);
60
61static 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
76static 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
85int 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
105static 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
135fail_register:
136 platform_device_del(platform_device);
137
138fail_platform_device_add:
139 platform_device_put(platform_device);
140
141fail_platform_device_alloc:
142 free_page((unsigned long)buffer);
143 return ret;
144}
145
146static 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
155subsys_initcall(dell_smbios_smm_init);
156module_exit(dell_smbios_smm_exit);
157
158MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
159MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
160MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
161MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
162MODULE_DESCRIPTION("Dell SMBIOS communications over SMI");
163MODULE_LICENSE("GPL");