Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  dcdbas.c: Dell Systems Management Base Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  The Dell Systems Management Base Driver provides a sysfs interface for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  systems management software to perform System Management Interrupts (SMIs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  and Host Control Actions (power cycle or power off after OS shutdown) on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  Dell systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  See Documentation/driver-api/dcdbas.rst for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *  Copyright (C) 1995-2006 Dell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/mc146818rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include "dcdbas.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define DRIVER_NAME		"dcdbas"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define DRIVER_VERSION		"5.6.0-3.4"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define DRIVER_DESCRIPTION	"Dell Systems Management Base Driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static struct platform_device *dcdbas_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static u8 *smi_data_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static dma_addr_t smi_data_buf_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static unsigned long smi_data_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static unsigned long max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static u32 smi_data_buf_phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static DEFINE_MUTEX(smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static u8 *bios_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static unsigned int host_control_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static unsigned int host_control_smi_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static unsigned int host_control_on_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static bool wsmt_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * smi_data_buf_free: free SMI data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static void smi_data_buf_free(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (!smi_data_buf || wsmt_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		__func__, smi_data_buf_phys_addr, smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			  smi_data_buf_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	smi_data_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	smi_data_buf_handle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	smi_data_buf_phys_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	smi_data_buf_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * smi_data_buf_realloc: grow SMI data buffer if needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static int smi_data_buf_realloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	dma_addr_t handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (smi_data_buf_size >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (size > max_smi_data_buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	/* new buffer is needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		dev_dbg(&dcdbas_pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			"%s: failed to allocate memory size %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			__func__, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	/* memory zeroed by dma_alloc_coherent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (smi_data_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		memcpy(buf, smi_data_buf, smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	/* free any existing buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	smi_data_buf_free();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* set up new buffer for use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	smi_data_buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	smi_data_buf_handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	smi_data_buf_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		__func__, smi_data_buf_phys_addr, smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 					   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static ssize_t smi_data_buf_size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				      struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return sprintf(buf, "%lu\n", smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static ssize_t smi_data_buf_size_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				       const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	unsigned long buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	buf_size = simple_strtoul(buf, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* make sure SMI data buffer is at least buf_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	mutex_lock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	ret = smi_data_buf_realloc(buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	mutex_unlock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			     struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			     char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	mutex_lock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 					smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	mutex_unlock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			      struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			      char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if ((pos + count) > max_smi_data_buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	mutex_lock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	ret = smi_data_buf_realloc(pos + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	memcpy(smi_data_buf + pos, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	mutex_unlock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static ssize_t host_control_action_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 					struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return sprintf(buf, "%u\n", host_control_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static ssize_t host_control_action_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 					 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 					 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* make sure buffer is available for host control command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	mutex_lock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	mutex_unlock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	host_control_action = simple_strtoul(buf, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static ssize_t host_control_smi_type_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 					  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 					  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return sprintf(buf, "%u\n", host_control_smi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static ssize_t host_control_smi_type_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 					   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 					   const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	host_control_smi_type = simple_strtoul(buf, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static ssize_t host_control_on_shutdown_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 					     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 					     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return sprintf(buf, "%u\n", host_control_on_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static ssize_t host_control_on_shutdown_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 					      struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 					      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int raise_smi(void *par)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct smi_cmd *smi_cmd = par;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (smp_processor_id() != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* generate SMI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	/* inb to force posted write through and make SMI happen now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	asm volatile (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		"outb %b0,%w1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		"inb %w1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		: /* no output args */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		: "a" (smi_cmd->command_code),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		  "d" (smi_cmd->command_address),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		  "b" (smi_cmd->ebx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		  "c" (smi_cmd->ecx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		: "memory"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * dcdbas_smi_request: generate SMI request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  * Called with smi_data_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int dcdbas_smi_request(struct smi_cmd *smi_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (smi_cmd->magic != SMI_CMD_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			 __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		return -EBADR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	/* SMI requires CPU 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * smi_request_store:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * The valid values are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * 0: zero SMI data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * 1: generate calling interface SMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * 2: generate raw SMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * User application writes smi_cmd to smi_data before telling driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * to generate SMI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static ssize_t smi_request_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct smi_cmd *smi_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	unsigned long val = simple_strtoul(buf, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	mutex_lock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (smi_data_buf_size < sizeof(struct smi_cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	smi_cmd = (struct smi_cmd *)smi_data_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		/* Raw SMI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		ret = dcdbas_smi_request(smi_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		 * Calling Interface SMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		 * Provide physical address of command buffer field within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		 * the struct smi_cmd to BIOS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		 * Because the address that smi_cmd (smi_data_buf) points to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		 * will be from memremap() of a non-memory address if WSMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		 * is present, we can't use virt_to_phys() on smi_cmd, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		 * we have to use the physical address that was saved when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		 * the virtual address for smi_cmd was received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		smi_cmd->ebx = smi_data_buf_phys_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 				offsetof(struct smi_cmd, command_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		ret = dcdbas_smi_request(smi_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		memset(smi_data_buf, 0, smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	mutex_unlock(&smi_data_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) EXPORT_SYMBOL(dcdbas_smi_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * host_control_smi: generate host control SMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * Caller must set up the host control command in smi_data_buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static int host_control_smi(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct apm_cmd *apm_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	u32 num_ticks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	s8 cmd_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	u8 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	apm_cmd = (struct apm_cmd *)smi_data_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	switch (host_control_smi_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	case HC_SMITYPE_TYPE1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		spin_lock_irqsave(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		/* write SMI data buffer physical address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		data = (u8 *)&smi_data_buf_phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		for (index = PE1300_CMOS_CMD_STRUCT_PTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		     index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		     index++, data++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			outb(index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			     (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			outb(*data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			     (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		/* first set status to -1 as called by spec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		/* generate SMM call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		spin_unlock_irqrestore(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		/* wait a few to see if it executed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		       == ESM_STATUS_CMD_UNSUCCESSFUL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			num_ticks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			if (num_ticks == EXPIRED_TIMER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 				return -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	case HC_SMITYPE_TYPE2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	case HC_SMITYPE_TYPE3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		spin_lock_irqsave(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		/* write SMI data buffer physical address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		data = (u8 *)&smi_data_buf_phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		for (index = PE1400_CMOS_CMD_STRUCT_PTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		     index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		     index++, data++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		/* generate SMM call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (host_control_smi_type == HC_SMITYPE_TYPE3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		/* restore RTC index pointer since it was written to above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		CMOS_READ(RTC_REG_C);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		spin_unlock_irqrestore(&rtc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		/* read control port back to serialize write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		cmd_status = inb(PE1400_APM_CONTROL_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		/* wait a few to see if it executed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			num_ticks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			if (num_ticks == EXPIRED_TIMER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 				return -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			__func__, host_control_smi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * dcdbas_host_control: initiate host control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * This function is called by the driver after the system has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  * finished shutting down if the user application specified a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  * host control action to perform on shutdown.  It is safe to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  * use smi_data_buf at this point because the system has finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  * shutting down and no userspace apps are running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static void dcdbas_host_control(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct apm_cmd *apm_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	u8 action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (host_control_action == HC_ACTION_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	action = host_control_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	host_control_action = HC_ACTION_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (!smi_data_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (smi_data_buf_size < sizeof(struct apm_cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	apm_cmd = (struct apm_cmd *)smi_data_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	/* power off takes precedence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		apm_cmd->command = ESM_APM_POWER_CYCLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		apm_cmd->reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		*((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		host_control_smi();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	} else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		apm_cmd->command = ESM_APM_POWER_CYCLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		apm_cmd->reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		*((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		host_control_smi();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /* WSMT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static u8 checksum(u8 *buffer, u8 length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	u8 sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	u8 *end = buffer + length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	while (buffer < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		sum += *buffer++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	return sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static inline struct smm_eps_table *check_eps_table(u8 *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	struct smm_eps_table *eps = (struct smm_eps_table *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	if (checksum(addr, eps->length) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	return eps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static int dcdbas_check_wsmt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	const struct dmi_device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct acpi_table_wsmt *wsmt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	struct smm_eps_table *eps = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	u64 bios_buf_paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	u64 remap_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	u8 *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (!wsmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	/* Check if WSMT ACPI table shows that protection is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	    !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	 * BIOS could provide the address/size of the protected buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	 * in an SMBIOS string or in an EPS structure in 0xFxxxx.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	/* Check SMBIOS for buffer address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		if (sscanf(dev->name, "30[%16llx;%8llx]", &bios_buf_paddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		    &remap_size) == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			goto remap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	/* Scan for EPS (entry point structure) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	for (addr = (u8 *)__va(0xf0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	     addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	     addr += 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		eps = check_eps_table(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		if (eps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (!eps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	bios_buf_paddr = eps->smm_comm_buff_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	remap_size = eps->num_of_4k_pages * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) remap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	 * Get physical address of buffer and map to virtual address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	 * Table gives size in 4K pages, regardless of actual system page size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (upper_32_bits(bios_buf_paddr + 8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	 * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	 * bytes are used for a semaphore, not the data buffer itself).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	if (!bios_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	/* First 8 bytes is for a semaphore, not part of the smi_data_buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	smi_data_buf_phys_addr = bios_buf_paddr + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	smi_data_buf = bios_buffer + 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	smi_data_buf_size = remap_size - 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	max_smi_data_buf_size = smi_data_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	wsmt_enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	dev_info(&dcdbas_pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		 "WSMT found, using firmware-provided SMI buffer.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)  * dcdbas_reboot_notify: handle reboot notification for host control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 				void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	case SYS_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	case SYS_HALT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	case SYS_POWER_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		if (host_control_on_shutdown) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			/* firmware is going to perform host control action */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			printk(KERN_WARNING "Please wait for shutdown "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			       "action to complete...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			dcdbas_host_control();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static struct notifier_block dcdbas_reboot_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	.notifier_call = dcdbas_reboot_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	.next = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	.priority = INT_MIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) static DCDBAS_BIN_ATTR_RW(smi_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static struct bin_attribute *dcdbas_bin_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	&bin_attr_smi_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) static DCDBAS_DEV_ATTR_WO(smi_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) static DCDBAS_DEV_ATTR_RW(host_control_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) static struct attribute *dcdbas_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	&dev_attr_smi_data_buf_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	&dev_attr_smi_data_buf_phys_addr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	&dev_attr_smi_request.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	&dev_attr_host_control_action.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	&dev_attr_host_control_smi_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	&dev_attr_host_control_on_shutdown.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) static const struct attribute_group dcdbas_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	.attrs = dcdbas_dev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	.bin_attrs = dcdbas_bin_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static int dcdbas_probe(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	host_control_action = HC_ACTION_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	host_control_smi_type = HC_SMITYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	dcdbas_pdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	/* Check if ACPI WSMT table specifies protected SMI buffer address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	error = dcdbas_check_wsmt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	 * BIOS SMI calls require buffer addresses be in 32-bit address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	 * This is done by setting the DMA mask below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	register_reboot_notifier(&dcdbas_reboot_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	dev_info(&dev->dev, "%s (version %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		 DRIVER_DESCRIPTION, DRIVER_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) static int dcdbas_remove(struct platform_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	unregister_reboot_notifier(&dcdbas_reboot_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) static struct platform_driver dcdbas_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		.name	= DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	.probe		= dcdbas_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	.remove		= dcdbas_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static const struct platform_device_info dcdbas_dev_info __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	.name		= DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	.id		= -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	.dma_mask	= DMA_BIT_MASK(32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) static struct platform_device *dcdbas_pdev_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)  * dcdbas_init: initialize driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) static int __init dcdbas_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	error = platform_driver_register(&dcdbas_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (IS_ERR(dcdbas_pdev_reg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		error = PTR_ERR(dcdbas_pdev_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		goto err_unregister_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)  err_unregister_driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	platform_driver_unregister(&dcdbas_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)  * dcdbas_exit: perform driver cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static void __exit dcdbas_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	 * make sure functions that use dcdbas_pdev are called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	 * before platform_device_unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	unregister_reboot_notifier(&dcdbas_reboot_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	 * We have to free the buffer here instead of dcdbas_remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	 * because only in module exit function we can be sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	 * all sysfs attributes belonging to this module have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	 * released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	if (dcdbas_pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		smi_data_buf_free();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	if (bios_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		memunmap(bios_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	platform_device_unregister(dcdbas_pdev_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	platform_driver_unregister(&dcdbas_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) subsys_initcall_sync(dcdbas_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) module_exit(dcdbas_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) MODULE_VERSION(DRIVER_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) MODULE_AUTHOR("Dell Inc.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) /* Any System or BIOS claiming to be by Dell */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");