1 // SPDX-License-Identifier: GPL-2.0-only
3 * dcdbas.c: Dell Systems Management Base Driver
5 * The Dell Systems Management Base Driver provides a sysfs interface for
6 * systems management software to perform System Management Interrupts (SMIs)
7 * and Host Control Actions (power cycle or power off after OS shutdown) on
10 * See Documentation/driver-api/dcdbas.rst for more information.
12 * Copyright (C) 1995-2006 Dell Inc.
15 #include <linux/platform_device.h>
16 #include <linux/acpi.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/errno.h>
19 #include <linux/cpu.h>
20 #include <linux/gfp.h>
21 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/module.h>
26 #include <linux/reboot.h>
27 #include <linux/sched.h>
28 #include <linux/smp.h>
29 #include <linux/spinlock.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/mutex.h>
36 #define DRIVER_NAME "dcdbas"
37 #define DRIVER_VERSION "5.6.0-3.3"
38 #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
40 static struct platform_device
*dcdbas_pdev
;
42 static u8
*smi_data_buf
;
43 static dma_addr_t smi_data_buf_handle
;
44 static unsigned long smi_data_buf_size
;
45 static unsigned long max_smi_data_buf_size
= MAX_SMI_DATA_BUF_SIZE
;
46 static u32 smi_data_buf_phys_addr
;
47 static DEFINE_MUTEX(smi_data_lock
);
48 static u8
*eps_buffer
;
50 static unsigned int host_control_action
;
51 static unsigned int host_control_smi_type
;
52 static unsigned int host_control_on_shutdown
;
54 static bool wsmt_enabled
;
57 * smi_data_buf_free: free SMI data buffer
59 static void smi_data_buf_free(void)
61 if (!smi_data_buf
|| wsmt_enabled
)
64 dev_dbg(&dcdbas_pdev
->dev
, "%s: phys: %x size: %lu\n",
65 __func__
, smi_data_buf_phys_addr
, smi_data_buf_size
);
67 dma_free_coherent(&dcdbas_pdev
->dev
, smi_data_buf_size
, smi_data_buf
,
70 smi_data_buf_handle
= 0;
71 smi_data_buf_phys_addr
= 0;
72 smi_data_buf_size
= 0;
76 * smi_data_buf_realloc: grow SMI data buffer if needed
78 static int smi_data_buf_realloc(unsigned long size
)
83 if (smi_data_buf_size
>= size
)
86 if (size
> max_smi_data_buf_size
)
89 /* new buffer is needed */
90 buf
= dma_alloc_coherent(&dcdbas_pdev
->dev
, size
, &handle
, GFP_KERNEL
);
92 dev_dbg(&dcdbas_pdev
->dev
,
93 "%s: failed to allocate memory size %lu\n",
97 /* memory zeroed by dma_alloc_coherent */
100 memcpy(buf
, smi_data_buf
, smi_data_buf_size
);
102 /* free any existing buffer */
105 /* set up new buffer for use */
107 smi_data_buf_handle
= handle
;
108 smi_data_buf_phys_addr
= (u32
) virt_to_phys(buf
);
109 smi_data_buf_size
= size
;
111 dev_dbg(&dcdbas_pdev
->dev
, "%s: phys: %x size: %lu\n",
112 __func__
, smi_data_buf_phys_addr
, smi_data_buf_size
);
117 static ssize_t
smi_data_buf_phys_addr_show(struct device
*dev
,
118 struct device_attribute
*attr
,
121 return sprintf(buf
, "%x\n", smi_data_buf_phys_addr
);
124 static ssize_t
smi_data_buf_size_show(struct device
*dev
,
125 struct device_attribute
*attr
,
128 return sprintf(buf
, "%lu\n", smi_data_buf_size
);
131 static ssize_t
smi_data_buf_size_store(struct device
*dev
,
132 struct device_attribute
*attr
,
133 const char *buf
, size_t count
)
135 unsigned long buf_size
;
138 buf_size
= simple_strtoul(buf
, NULL
, 10);
140 /* make sure SMI data buffer is at least buf_size */
141 mutex_lock(&smi_data_lock
);
142 ret
= smi_data_buf_realloc(buf_size
);
143 mutex_unlock(&smi_data_lock
);
150 static ssize_t
smi_data_read(struct file
*filp
, struct kobject
*kobj
,
151 struct bin_attribute
*bin_attr
,
152 char *buf
, loff_t pos
, size_t count
)
156 mutex_lock(&smi_data_lock
);
157 ret
= memory_read_from_buffer(buf
, count
, &pos
, smi_data_buf
,
159 mutex_unlock(&smi_data_lock
);
163 static ssize_t
smi_data_write(struct file
*filp
, struct kobject
*kobj
,
164 struct bin_attribute
*bin_attr
,
165 char *buf
, loff_t pos
, size_t count
)
169 if ((pos
+ count
) > max_smi_data_buf_size
)
172 mutex_lock(&smi_data_lock
);
174 ret
= smi_data_buf_realloc(pos
+ count
);
178 memcpy(smi_data_buf
+ pos
, buf
, count
);
181 mutex_unlock(&smi_data_lock
);
185 static ssize_t
host_control_action_show(struct device
*dev
,
186 struct device_attribute
*attr
,
189 return sprintf(buf
, "%u\n", host_control_action
);
192 static ssize_t
host_control_action_store(struct device
*dev
,
193 struct device_attribute
*attr
,
194 const char *buf
, size_t count
)
198 /* make sure buffer is available for host control command */
199 mutex_lock(&smi_data_lock
);
200 ret
= smi_data_buf_realloc(sizeof(struct apm_cmd
));
201 mutex_unlock(&smi_data_lock
);
205 host_control_action
= simple_strtoul(buf
, NULL
, 10);
209 static ssize_t
host_control_smi_type_show(struct device
*dev
,
210 struct device_attribute
*attr
,
213 return sprintf(buf
, "%u\n", host_control_smi_type
);
216 static ssize_t
host_control_smi_type_store(struct device
*dev
,
217 struct device_attribute
*attr
,
218 const char *buf
, size_t count
)
220 host_control_smi_type
= simple_strtoul(buf
, NULL
, 10);
224 static ssize_t
host_control_on_shutdown_show(struct device
*dev
,
225 struct device_attribute
*attr
,
228 return sprintf(buf
, "%u\n", host_control_on_shutdown
);
231 static ssize_t
host_control_on_shutdown_store(struct device
*dev
,
232 struct device_attribute
*attr
,
233 const char *buf
, size_t count
)
235 host_control_on_shutdown
= simple_strtoul(buf
, NULL
, 10);
239 static int raise_smi(void *par
)
241 struct smi_cmd
*smi_cmd
= par
;
243 if (smp_processor_id() != 0) {
244 dev_dbg(&dcdbas_pdev
->dev
, "%s: failed to get CPU 0\n",
250 /* inb to force posted write through and make SMI happen now */
254 : /* no output args */
255 : "a" (smi_cmd
->command_code
),
256 "d" (smi_cmd
->command_address
),
265 * dcdbas_smi_request: generate SMI request
267 * Called with smi_data_lock.
269 int dcdbas_smi_request(struct smi_cmd
*smi_cmd
)
273 if (smi_cmd
->magic
!= SMI_CMD_MAGIC
) {
274 dev_info(&dcdbas_pdev
->dev
, "%s: invalid magic value\n",
279 /* SMI requires CPU 0 */
281 ret
= smp_call_on_cpu(0, raise_smi
, smi_cmd
, true);
290 * The valid values are:
291 * 0: zero SMI data buffer
292 * 1: generate calling interface SMI
293 * 2: generate raw SMI
295 * User application writes smi_cmd to smi_data before telling driver
298 static ssize_t
smi_request_store(struct device
*dev
,
299 struct device_attribute
*attr
,
300 const char *buf
, size_t count
)
302 struct smi_cmd
*smi_cmd
;
303 unsigned long val
= simple_strtoul(buf
, NULL
, 10);
306 mutex_lock(&smi_data_lock
);
308 if (smi_data_buf_size
< sizeof(struct smi_cmd
)) {
312 smi_cmd
= (struct smi_cmd
*)smi_data_buf
;
317 ret
= dcdbas_smi_request(smi_cmd
);
323 * Calling Interface SMI
325 * Provide physical address of command buffer field within
326 * the struct smi_cmd to BIOS.
328 * Because the address that smi_cmd (smi_data_buf) points to
329 * will be from memremap() of a non-memory address if WSMT
330 * is present, we can't use virt_to_phys() on smi_cmd, so
331 * we have to use the physical address that was saved when
332 * the virtual address for smi_cmd was received.
334 smi_cmd
->ebx
= smi_data_buf_phys_addr
+
335 offsetof(struct smi_cmd
, command_buffer
);
336 ret
= dcdbas_smi_request(smi_cmd
);
341 memset(smi_data_buf
, 0, smi_data_buf_size
);
350 mutex_unlock(&smi_data_lock
);
353 EXPORT_SYMBOL(dcdbas_smi_request
);
356 * host_control_smi: generate host control SMI
358 * Caller must set up the host control command in smi_data_buf.
360 static int host_control_smi(void)
362 struct apm_cmd
*apm_cmd
;
369 apm_cmd
= (struct apm_cmd
*)smi_data_buf
;
370 apm_cmd
->status
= ESM_STATUS_CMD_UNSUCCESSFUL
;
372 switch (host_control_smi_type
) {
373 case HC_SMITYPE_TYPE1
:
374 spin_lock_irqsave(&rtc_lock
, flags
);
375 /* write SMI data buffer physical address */
376 data
= (u8
*)&smi_data_buf_phys_addr
;
377 for (index
= PE1300_CMOS_CMD_STRUCT_PTR
;
378 index
< (PE1300_CMOS_CMD_STRUCT_PTR
+ 4);
381 (CMOS_BASE_PORT
+ CMOS_PAGE2_INDEX_PORT_PIIX4
));
383 (CMOS_BASE_PORT
+ CMOS_PAGE2_DATA_PORT_PIIX4
));
386 /* first set status to -1 as called by spec */
387 cmd_status
= ESM_STATUS_CMD_UNSUCCESSFUL
;
388 outb((u8
) cmd_status
, PCAT_APM_STATUS_PORT
);
390 /* generate SMM call */
391 outb(ESM_APM_CMD
, PCAT_APM_CONTROL_PORT
);
392 spin_unlock_irqrestore(&rtc_lock
, flags
);
394 /* wait a few to see if it executed */
395 num_ticks
= TIMEOUT_USEC_SHORT_SEMA_BLOCKING
;
396 while ((cmd_status
= inb(PCAT_APM_STATUS_PORT
))
397 == ESM_STATUS_CMD_UNSUCCESSFUL
) {
399 if (num_ticks
== EXPIRED_TIMER
)
404 case HC_SMITYPE_TYPE2
:
405 case HC_SMITYPE_TYPE3
:
406 spin_lock_irqsave(&rtc_lock
, flags
);
407 /* write SMI data buffer physical address */
408 data
= (u8
*)&smi_data_buf_phys_addr
;
409 for (index
= PE1400_CMOS_CMD_STRUCT_PTR
;
410 index
< (PE1400_CMOS_CMD_STRUCT_PTR
+ 4);
412 outb(index
, (CMOS_BASE_PORT
+ CMOS_PAGE1_INDEX_PORT
));
413 outb(*data
, (CMOS_BASE_PORT
+ CMOS_PAGE1_DATA_PORT
));
416 /* generate SMM call */
417 if (host_control_smi_type
== HC_SMITYPE_TYPE3
)
418 outb(ESM_APM_CMD
, PCAT_APM_CONTROL_PORT
);
420 outb(ESM_APM_CMD
, PE1400_APM_CONTROL_PORT
);
422 /* restore RTC index pointer since it was written to above */
423 CMOS_READ(RTC_REG_C
);
424 spin_unlock_irqrestore(&rtc_lock
, flags
);
426 /* read control port back to serialize write */
427 cmd_status
= inb(PE1400_APM_CONTROL_PORT
);
429 /* wait a few to see if it executed */
430 num_ticks
= TIMEOUT_USEC_SHORT_SEMA_BLOCKING
;
431 while (apm_cmd
->status
== ESM_STATUS_CMD_UNSUCCESSFUL
) {
433 if (num_ticks
== EXPIRED_TIMER
)
439 dev_dbg(&dcdbas_pdev
->dev
, "%s: invalid SMI type %u\n",
440 __func__
, host_control_smi_type
);
448 * dcdbas_host_control: initiate host control
450 * This function is called by the driver after the system has
451 * finished shutting down if the user application specified a
452 * host control action to perform on shutdown. It is safe to
453 * use smi_data_buf at this point because the system has finished
454 * shutting down and no userspace apps are running.
456 static void dcdbas_host_control(void)
458 struct apm_cmd
*apm_cmd
;
461 if (host_control_action
== HC_ACTION_NONE
)
464 action
= host_control_action
;
465 host_control_action
= HC_ACTION_NONE
;
468 dev_dbg(&dcdbas_pdev
->dev
, "%s: no SMI buffer\n", __func__
);
472 if (smi_data_buf_size
< sizeof(struct apm_cmd
)) {
473 dev_dbg(&dcdbas_pdev
->dev
, "%s: SMI buffer too small\n",
478 apm_cmd
= (struct apm_cmd
*)smi_data_buf
;
480 /* power off takes precedence */
481 if (action
& HC_ACTION_HOST_CONTROL_POWEROFF
) {
482 apm_cmd
->command
= ESM_APM_POWER_CYCLE
;
483 apm_cmd
->reserved
= 0;
484 *((s16
*)&apm_cmd
->parameters
.shortreq
.parm
[0]) = (s16
) 0;
486 } else if (action
& HC_ACTION_HOST_CONTROL_POWERCYCLE
) {
487 apm_cmd
->command
= ESM_APM_POWER_CYCLE
;
488 apm_cmd
->reserved
= 0;
489 *((s16
*)&apm_cmd
->parameters
.shortreq
.parm
[0]) = (s16
) 20;
496 static u8
checksum(u8
*buffer
, u8 length
)
499 u8
*end
= buffer
+ length
;
506 static inline struct smm_eps_table
*check_eps_table(u8
*addr
)
508 struct smm_eps_table
*eps
= (struct smm_eps_table
*)addr
;
510 if (strncmp(eps
->smm_comm_buff_anchor
, SMM_EPS_SIG
, 4) != 0)
513 if (checksum(addr
, eps
->length
) != 0)
519 static int dcdbas_check_wsmt(void)
521 struct acpi_table_wsmt
*wsmt
= NULL
;
522 struct smm_eps_table
*eps
= NULL
;
526 acpi_get_table(ACPI_SIG_WSMT
, 0, (struct acpi_table_header
**)&wsmt
);
530 /* Check if WSMT ACPI table shows that protection is enabled */
531 if (!(wsmt
->protection_flags
& ACPI_WSMT_FIXED_COMM_BUFFERS
) ||
532 !(wsmt
->protection_flags
& ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION
))
535 /* Scan for EPS (entry point structure) */
536 for (addr
= (u8
*)__va(0xf0000);
537 addr
< (u8
*)__va(0x100000 - sizeof(struct smm_eps_table
));
539 eps
= check_eps_table(addr
);
545 dev_dbg(&dcdbas_pdev
->dev
, "found WSMT, but no EPS found\n");
550 * Get physical address of buffer and map to virtual address.
551 * Table gives size in 4K pages, regardless of actual system page size.
553 if (upper_32_bits(eps
->smm_comm_buff_addr
+ 8)) {
554 dev_warn(&dcdbas_pdev
->dev
, "found WSMT, but EPS buffer address is above 4GB\n");
558 * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8
559 * bytes are used for a semaphore, not the data buffer itself).
561 remap_size
= eps
->num_of_4k_pages
* PAGE_SIZE
;
562 if (remap_size
> MAX_SMI_DATA_BUF_SIZE
+ 8)
563 remap_size
= MAX_SMI_DATA_BUF_SIZE
+ 8;
564 eps_buffer
= memremap(eps
->smm_comm_buff_addr
, remap_size
, MEMREMAP_WB
);
566 dev_warn(&dcdbas_pdev
->dev
, "found WSMT, but failed to map EPS buffer\n");
570 /* First 8 bytes is for a semaphore, not part of the smi_data_buf */
571 smi_data_buf_phys_addr
= eps
->smm_comm_buff_addr
+ 8;
572 smi_data_buf
= eps_buffer
+ 8;
573 smi_data_buf_size
= remap_size
- 8;
574 max_smi_data_buf_size
= smi_data_buf_size
;
576 dev_info(&dcdbas_pdev
->dev
,
577 "WSMT found, using firmware-provided SMI buffer.\n");
582 * dcdbas_reboot_notify: handle reboot notification for host control
584 static int dcdbas_reboot_notify(struct notifier_block
*nb
, unsigned long code
,
591 if (host_control_on_shutdown
) {
592 /* firmware is going to perform host control action */
593 printk(KERN_WARNING
"Please wait for shutdown "
594 "action to complete...\n");
595 dcdbas_host_control();
603 static struct notifier_block dcdbas_reboot_nb
= {
604 .notifier_call
= dcdbas_reboot_notify
,
609 static DCDBAS_BIN_ATTR_RW(smi_data
);
611 static struct bin_attribute
*dcdbas_bin_attrs
[] = {
616 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size
);
617 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr
);
618 static DCDBAS_DEV_ATTR_WO(smi_request
);
619 static DCDBAS_DEV_ATTR_RW(host_control_action
);
620 static DCDBAS_DEV_ATTR_RW(host_control_smi_type
);
621 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown
);
623 static struct attribute
*dcdbas_dev_attrs
[] = {
624 &dev_attr_smi_data_buf_size
.attr
,
625 &dev_attr_smi_data_buf_phys_addr
.attr
,
626 &dev_attr_smi_request
.attr
,
627 &dev_attr_host_control_action
.attr
,
628 &dev_attr_host_control_smi_type
.attr
,
629 &dev_attr_host_control_on_shutdown
.attr
,
633 static const struct attribute_group dcdbas_attr_group
= {
634 .attrs
= dcdbas_dev_attrs
,
635 .bin_attrs
= dcdbas_bin_attrs
,
638 static int dcdbas_probe(struct platform_device
*dev
)
642 host_control_action
= HC_ACTION_NONE
;
643 host_control_smi_type
= HC_SMITYPE_NONE
;
647 /* Check if ACPI WSMT table specifies protected SMI buffer address */
648 error
= dcdbas_check_wsmt();
653 * BIOS SMI calls require buffer addresses be in 32-bit address space.
654 * This is done by setting the DMA mask below.
656 error
= dma_set_coherent_mask(&dcdbas_pdev
->dev
, DMA_BIT_MASK(32));
660 error
= sysfs_create_group(&dev
->dev
.kobj
, &dcdbas_attr_group
);
664 register_reboot_notifier(&dcdbas_reboot_nb
);
666 dev_info(&dev
->dev
, "%s (version %s)\n",
667 DRIVER_DESCRIPTION
, DRIVER_VERSION
);
672 static int dcdbas_remove(struct platform_device
*dev
)
674 unregister_reboot_notifier(&dcdbas_reboot_nb
);
675 sysfs_remove_group(&dev
->dev
.kobj
, &dcdbas_attr_group
);
680 static struct platform_driver dcdbas_driver
= {
684 .probe
= dcdbas_probe
,
685 .remove
= dcdbas_remove
,
688 static const struct platform_device_info dcdbas_dev_info __initconst
= {
691 .dma_mask
= DMA_BIT_MASK(32),
694 static struct platform_device
*dcdbas_pdev_reg
;
697 * dcdbas_init: initialize driver
699 static int __init
dcdbas_init(void)
703 error
= platform_driver_register(&dcdbas_driver
);
707 dcdbas_pdev_reg
= platform_device_register_full(&dcdbas_dev_info
);
708 if (IS_ERR(dcdbas_pdev_reg
)) {
709 error
= PTR_ERR(dcdbas_pdev_reg
);
710 goto err_unregister_driver
;
715 err_unregister_driver
:
716 platform_driver_unregister(&dcdbas_driver
);
721 * dcdbas_exit: perform driver cleanup
723 static void __exit
dcdbas_exit(void)
726 * make sure functions that use dcdbas_pdev are called
727 * before platform_device_unregister
729 unregister_reboot_notifier(&dcdbas_reboot_nb
);
732 * We have to free the buffer here instead of dcdbas_remove
733 * because only in module exit function we can be sure that
734 * all sysfs attributes belonging to this module have been
740 memunmap(eps_buffer
);
741 platform_device_unregister(dcdbas_pdev_reg
);
742 platform_driver_unregister(&dcdbas_driver
);
745 subsys_initcall_sync(dcdbas_init
);
746 module_exit(dcdbas_exit
);
748 MODULE_DESCRIPTION(DRIVER_DESCRIPTION
" (version " DRIVER_VERSION
")");
749 MODULE_VERSION(DRIVER_VERSION
);
750 MODULE_AUTHOR("Dell Inc.");
751 MODULE_LICENSE("GPL");
752 /* Any System or BIOS claiming to be by Dell */
753 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");