Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  SMI methods for use with dell-smbios
  4 *
  5 *  Copyright (c) Red Hat <mjg@redhat.com>
  6 *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  7 *  Copyright (c) 2014 Pali Rohár <pali@kernel.org>
  8 *  Copyright (c) 2017 Dell Inc.
  9 */
 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 11
 12#include <linux/dmi.h>
 13#include <linux/gfp.h>
 14#include <linux/io.h>
 15#include <linux/module.h>
 16#include <linux/mutex.h>
 17#include <linux/platform_device.h>
 18#include "dcdbas.h"
 19#include "dell-smbios.h"
 20
 21static int da_command_address;
 22static int da_command_code;
 23static struct smi_buffer smi_buf;
 24static struct calling_interface_buffer *buffer;
 25static struct platform_device *platform_device;
 26static DEFINE_MUTEX(smm_mutex);
 27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 28static void parse_da_table(const struct dmi_header *dm)
 29{
 30	struct calling_interface_structure *table =
 31		container_of(dm, struct calling_interface_structure, header);
 32
 33	/* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
 34	 * 6 bytes of entry
 35	 */
 36	if (dm->length < 17)
 37		return;
 38
 39	da_command_address = table->cmdIOAddress;
 40	da_command_code = table->cmdIOCode;
 41}
 42
 43static void find_cmd_address(const struct dmi_header *dm, void *dummy)
 44{
 45	switch (dm->type) {
 46	case 0xda: /* Calling interface */
 47		parse_da_table(dm);
 48		break;
 49	}
 50}
 51
 52static int dell_smbios_smm_call(struct calling_interface_buffer *input)
 53{
 54	struct smi_cmd command;
 55	size_t size;
 56
 57	size = sizeof(struct calling_interface_buffer);
 58	command.magic = SMI_CMD_MAGIC;
 59	command.command_address = da_command_address;
 60	command.command_code = da_command_code;
 61	command.ebx = smi_buf.dma;
 62	command.ecx = 0x42534931;
 63
 64	mutex_lock(&smm_mutex);
 65	memcpy(buffer, input, size);
 66	dcdbas_smi_request(&command);
 67	memcpy(input, buffer, size);
 68	mutex_unlock(&smm_mutex);
 69	return 0;
 70}
 71
 72/* When enabled this indicates that SMM won't work */
 73static bool test_wsmt_enabled(void)
 74{
 75	struct calling_interface_token *wsmt;
 76
 77	/* if token doesn't exist, SMM will work */
 78	wsmt = dell_smbios_find_token(WSMT_EN_TOKEN);
 79	if (!wsmt)
 80		return false;
 81
 82	/* If token exists, try to access over SMM but set a dummy return.
 83	 * - If WSMT disabled it will be overwritten by SMM
 84	 * - If WSMT enabled then dummy value will remain
 85	 */
 86	buffer->cmd_class = CLASS_TOKEN_READ;
 87	buffer->cmd_select = SELECT_TOKEN_STD;
 88	memset(buffer, 0, sizeof(struct calling_interface_buffer));
 89	buffer->input[0] = wsmt->location;
 90	buffer->output[0] = 99;
 91	dell_smbios_smm_call(buffer);
 92	if (buffer->output[0] == 99)
 93		return true;
 94
 95	return false;
 96}
 97
 98int init_dell_smbios_smm(void)
 99{
100	int ret;
101	/*
102	 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
103	 * is passed to SMI handler.
104	 */
105	ret = dcdbas_smi_alloc(&smi_buf, PAGE_SIZE);
106	if (ret)
107		return ret;
108	buffer = (void *)smi_buf.virt;
109
110	dmi_walk(find_cmd_address, NULL);
111
112	if (test_wsmt_enabled()) {
113		pr_debug("Disabling due to WSMT enabled\n");
114		ret = -ENODEV;
115		goto fail_wsmt;
116	}
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_wsmt:
142fail_platform_device_alloc:
143	dcdbas_smi_free(&smi_buf);
144	return ret;
145}
146
147void exit_dell_smbios_smm(void)
148{
149	if (platform_device) {
150		dell_smbios_unregister_device(&platform_device->dev);
151		platform_device_unregister(platform_device);
152		dcdbas_smi_free(&smi_buf);
153	}
154}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  SMI methods for use with dell-smbios
  4 *
  5 *  Copyright (c) Red Hat <mjg@redhat.com>
  6 *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  7 *  Copyright (c) 2014 Pali Rohár <pali@kernel.org>
  8 *  Copyright (c) 2017 Dell Inc.
  9 */
 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 11
 12#include <linux/dmi.h>
 13#include <linux/gfp.h>
 14#include <linux/io.h>
 15#include <linux/module.h>
 16#include <linux/mutex.h>
 17#include <linux/platform_device.h>
 18#include "dcdbas.h"
 19#include "dell-smbios.h"
 20
 21static int da_command_address;
 22static int da_command_code;
 
 23static struct calling_interface_buffer *buffer;
 24static struct platform_device *platform_device;
 25static DEFINE_MUTEX(smm_mutex);
 26
 27static const struct dmi_system_id dell_device_table[] __initconst = {
 28	{
 29		.ident = "Dell laptop",
 30		.matches = {
 31			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 32			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
 33		},
 34	},
 35	{
 36		.matches = {
 37			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 38			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
 39		},
 40	},
 41	{
 42		.matches = {
 43			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 44			DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
 45		},
 46	},
 47	{
 48		.ident = "Dell Computer Corporation",
 49		.matches = {
 50			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
 51			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
 52		},
 53	},
 54	{ }
 55};
 56MODULE_DEVICE_TABLE(dmi, dell_device_table);
 57
 58static void parse_da_table(const struct dmi_header *dm)
 59{
 60	struct calling_interface_structure *table =
 61		container_of(dm, struct calling_interface_structure, header);
 62
 63	/* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
 64	 * 6 bytes of entry
 65	 */
 66	if (dm->length < 17)
 67		return;
 68
 69	da_command_address = table->cmdIOAddress;
 70	da_command_code = table->cmdIOCode;
 71}
 72
 73static void find_cmd_address(const struct dmi_header *dm, void *dummy)
 74{
 75	switch (dm->type) {
 76	case 0xda: /* Calling interface */
 77		parse_da_table(dm);
 78		break;
 79	}
 80}
 81
 82static int dell_smbios_smm_call(struct calling_interface_buffer *input)
 83{
 84	struct smi_cmd command;
 85	size_t size;
 86
 87	size = sizeof(struct calling_interface_buffer);
 88	command.magic = SMI_CMD_MAGIC;
 89	command.command_address = da_command_address;
 90	command.command_code = da_command_code;
 91	command.ebx = virt_to_phys(buffer);
 92	command.ecx = 0x42534931;
 93
 94	mutex_lock(&smm_mutex);
 95	memcpy(buffer, input, size);
 96	dcdbas_smi_request(&command);
 97	memcpy(input, buffer, size);
 98	mutex_unlock(&smm_mutex);
 99	return 0;
100}
101
102/* When enabled this indicates that SMM won't work */
103static bool test_wsmt_enabled(void)
104{
105	struct calling_interface_token *wsmt;
106
107	/* if token doesn't exist, SMM will work */
108	wsmt = dell_smbios_find_token(WSMT_EN_TOKEN);
109	if (!wsmt)
110		return false;
111
112	/* If token exists, try to access over SMM but set a dummy return.
113	 * - If WSMT disabled it will be overwritten by SMM
114	 * - If WSMT enabled then dummy value will remain
115	 */
116	buffer->cmd_class = CLASS_TOKEN_READ;
117	buffer->cmd_select = SELECT_TOKEN_STD;
118	memset(buffer, 0, sizeof(struct calling_interface_buffer));
119	buffer->input[0] = wsmt->location;
120	buffer->output[0] = 99;
121	dell_smbios_smm_call(buffer);
122	if (buffer->output[0] == 99)
123		return true;
124
125	return false;
126}
127
128int init_dell_smbios_smm(void)
129{
130	int ret;
131	/*
132	 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
133	 * is passed to SMI handler.
134	 */
135	buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
136	if (!buffer)
137		return -ENOMEM;
 
138
139	dmi_walk(find_cmd_address, NULL);
140
141	if (test_wsmt_enabled()) {
142		pr_debug("Disabling due to WSMT enabled\n");
143		ret = -ENODEV;
144		goto fail_wsmt;
145	}
146
147	platform_device = platform_device_alloc("dell-smbios", 1);
148	if (!platform_device) {
149		ret = -ENOMEM;
150		goto fail_platform_device_alloc;
151	}
152
153	ret = platform_device_add(platform_device);
154	if (ret)
155		goto fail_platform_device_add;
156
157	ret = dell_smbios_register_device(&platform_device->dev,
158					  &dell_smbios_smm_call);
159	if (ret)
160		goto fail_register;
161
162	return 0;
163
164fail_register:
165	platform_device_del(platform_device);
166
167fail_platform_device_add:
168	platform_device_put(platform_device);
169
170fail_wsmt:
171fail_platform_device_alloc:
172	free_page((unsigned long)buffer);
173	return ret;
174}
175
176void exit_dell_smbios_smm(void)
177{
178	if (platform_device) {
179		dell_smbios_unregister_device(&platform_device->dev);
180		platform_device_unregister(platform_device);
181		free_page((unsigned long)buffer);
182	}
183}