Skip to contentSkip to conversation

platform/x86: wmi: Support reading/writing 16 bit EC values

PATCH Superseded Armin Wolf 3 participants 11 messages Mar 8, 2024 – Mar 11, 2024

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Armin Wolf

author
#0 lore raw

PATCH v4 1/2 platform/x86: wmi: Support reading/writing 16 bit EC values

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Changes since v3:
- change type of variable i to size_t

Changes since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
 drivers/platform/x86/wmi.c | 49 ++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +39 -10
index 1920e115da89..d9bf6d452b3a 100644
@@ -1153,6 +1153,34 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
 	return 0;
 }
 
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_read(address + i, &buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_write(address + i, buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
 /*
  * WMI can have EmbeddedControl access regions. In which case, we just want to
  * hand these off to the EC driver.
@@ -1162,27 +1190,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 			  u32 bits, u64 *value,
 			  void *handler_context, void *region_context)
 {
-	int result = 0;
-	u8 temp = 0;
+	int bytes = bits / BITS_PER_BYTE;
+	int ret;
+
+	if (!value)
+		return AE_NULL_ENTRY;
 
-	if ((address > 0xFF) || !value)
+	if (!bytes || bytes > sizeof(*value))
 		return AE_BAD_PARAMETER;
 
-	if (function != ACPI_READ && function != ACPI_WRITE)
+	if (address > U8_MAX || address + bytes - 1 > U8_MAX)
 		return AE_BAD_PARAMETER;
 
-	if (bits != 8)
+	if (function != ACPI_READ && function != ACPI_WRITE)
 		return AE_BAD_PARAMETER;
 
 	if (function == ACPI_READ) {
-		result = ec_read(address, &temp);
-		*value = temp;
+		ret = ec_read_multiple(address, (u8 *)value, bytes);
 	} else {
-		temp = 0xff & *value;
-		result = ec_write(address, temp);
+		ret = ec_write_multiple(address, (u8 *)value, bytes);
 	}
 
-	switch (result) {
+	switch (ret) {
 	case -EINVAL:
 		return AE_BAD_PARAMETER;
 	case -ENODEV:

--
2.39.2

author
#1 lore raw

PATCH v4 2/2 platform/x86: wmi: Avoid returning AE_OK upon unknown error

If an error code other than EINVAL, ENODEV or ETIME is returned by ec_read()/ec_write(), then AE_OK is wrongly returned.

Fix this by only returning AE_OK if the return code is 0, and return AE_ERROR otherwise.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
 drivers/platform/x86/wmi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/platform/x86/wmi.c +3 -1
index d9bf6d452b3a..84d1ccf6bc14 100644
@@ -1218,8 +1218,10 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 		return AE_NOT_FOUND;
 	case -ETIME:
 		return AE_TIME;
-	default:
+	case 0:
 		return AE_OK;
+	default:
+		return AE_ERROR;
 	}
 }
 

--
2.39.2

#2 lore raw

Re: [PATCH v4 2/2] platform/x86: wmi: Avoid returning AE_OK upon unknown error

Quoted 20 lines · Armin Wolf

If an error code other than EINVAL, ENODEV or ETIME is returned by ec_read()/ec_write(), then AE_OK is wrongly returned.

Fix this by only returning AE_OK if the return code is 0, and return AE_ERROR otherwise.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
 drivers/platform/x86/wmi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/platform/x86/wmi.c +0 -0
index d9bf6d452b3a..84d1ccf6bc14 100644

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1225

Quoted 9 lines
drivers/platform/x86/wmi.c +3 -1
@@ -1218,8 +1218,10 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 		return AE_NOT_FOUND;
 	case -ETIME:
 		return AE_TIME;
-	default:
+	case 0:
 		return AE_OK;
+	default:
+		return AE_ERROR;
 	}

After checking the callers of acpi_wmi_ec_space_handler() it looks like there is no benefit in returning different ACPI status per error values. It is not being used. why no just return for result < 0 AE_ERROR and return for other cases?

Quoted 6 lines
drivers/platform/x86/wmi.c +0 -1
 }
 
--

2.39.2

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
author
#3 lore raw

Re: [PATCH v4 2/2] platform/x86: wmi: Avoid returning AE_OK upon unknown error

Am 09.03.24 um 18:41 schrieb Kuppuswamy Sathyanarayanan:

Quoted 16 lines · Armin Wolf
Quoted 15 lines

If an error code other than EINVAL, ENODEV or ETIME is returned by ec_read()/ec_write(), then AE_OK is wrongly returned.

Fix this by only returning AE_OK if the return code is 0, and return AE_ERROR otherwise.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
  drivers/platform/x86/wmi.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines
Quoted 5 lines
drivers/platform/x86/wmi.c +0 -0
index d9bf6d452b3a..84d1ccf6bc14 100644

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1225

Quoted 9 lines
drivers/platform/x86/wmi.c +3 -1
@@ -1218,8 +1218,10 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
  		return AE_NOT_FOUND;
  	case -ETIME:
  		return AE_TIME;
-	default:
+	case 0:
  		return AE_OK;
+	default:
+		return AE_ERROR;
  	}

After checking the callers of acpi_wmi_ec_space_handler() it looks like there is no benefit in returning different ACPI status per error values. It is not being used. why no just return for result < 0 AE_ERROR and return for other cases?

Hi,

those handler functions are being called in acpi_ev_address_space_dispatch(), which uses the return value to print error messages. So it makes sense to return different ACPI error values here.

Thanks,
Armin Wolf

Quoted 6 lines
Quoted 6 lines
drivers/platform/x86/wmi.c +0 -1
  }
 
--

2.39.2

#4 lore raw

Re: [PATCH v4 2/2] platform/x86: wmi: Avoid returning AE_OK upon unknown error

Quoted 16 lines · Armin Wolf

Am 09.03.24 um 18:41 schrieb Kuppuswamy Sathyanarayanan:

Quoted 13 lines · Armin Wolf
Quoted 12 lines

If an error code other than EINVAL, ENODEV or ETIME is returned by ec_read()/ec_write(), then AE_OK is wrongly returned.

Fix this by only returning AE_OK if the return code is 0, and return AE_ERROR otherwise.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---

Got it.

Reviewed-by: Kuppuswamy Sathyanarayanan
<[email protected]>

Quoted 33 lines
Quoted 18 lines
Quoted 8 lines
  drivers/platform/x86/wmi.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/platform/x86/wmi.c +0 -0
index d9bf6d452b3a..84d1ccf6bc14 100644

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1225

Quoted 9 lines
drivers/platform/x86/wmi.c +3 -1
@@ -1218,8 +1218,10 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
             return AE_NOT_FOUND;
     case -ETIME:
             return AE_TIME;
-    default:
+    case 0:
             return AE_OK;
+    default:
+            return AE_ERROR;
     }

After checking the callers of acpi_wmi_ec_space_handler() it looks like there is no benefit in returning different ACPI status per error values. It is not being used. why no just return for result < 0 AE_ERROR and return for other cases?

Hi,

those handler functions are being called in acpi_ev_address_space_dispatch(), which uses the return value to print error messages. So it makes sense to return different ACPI error values here.

Thanks,
Armin Wolf

Quoted 6 lines
Quoted 6 lines
drivers/platform/x86/wmi.c +0 -1
  }
 
--

2.39.2

Reviewed-by on v4 2/2
#5 lore raw

Re: [PATCH v4 2/2] platform/x86: wmi: Avoid returning AE_OK upon unknown error

In reply to Armin Wolf
Quoted 6 lines · Armin Wolf

If an error code other than EINVAL, ENODEV or ETIME is returned by ec_read()/ec_write(), then AE_OK is wrongly returned.

Fix this by only returning AE_OK if the return code is 0, and return AE_ERROR otherwise.

Quoted 6 lines

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---

Looks good to me.

Quoted 23 lines
 drivers/platform/x86/wmi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/platform/x86/wmi.c +3 -2
index d9bf6d452b3a..84d1ccf6bc14 100644
@@ -1218,8 +1218,10 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 		return AE_NOT_FOUND;
 	case -ETIME:
 		return AE_TIME;
-	default:
+	case 0:
 		return AE_OK;
+	default:
+		return AE_ERROR;
 	}
 }
 
--

2.39.2

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#6 lore raw
In reply to Armin Wolf
Quoted 36 lines · Armin Wolf

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Changes since v3:
- change type of variable i to size_t

Changes since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
 drivers/platform/x86/wmi.c | 49 ++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +3 -0
index 1920e115da89..d9bf6d452b3a 100644
@@ -1153,6 +1153,34 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
 	return 0;
 }
 
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1168

Quoted 10 lines
drivers/platform/x86/wmi.c +10 -0
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_read(address + i, &buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}

Why not use ec_transaction?

Quoted 4 lines
drivers/platform/x86/wmi.c +4 -0
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1182

Quoted 10 lines
drivers/platform/x86/wmi.c +10 -0
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_write(address + i, buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}

Same as above.

Quoted 15 lines
drivers/platform/x86/wmi.c +6 -2
+
 /*
  * WMI can have EmbeddedControl access regions. In which case, we just want to
  * hand these off to the EC driver.
@@ -1162,27 +1190,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 			  u32 bits, u64 *value,
 			  void *handler_context, void *region_context)
 {
-	int result = 0;
-	u8 temp = 0;
+	int bytes = bits / BITS_PER_BYTE;
+	int ret;
+
+	if (!value)
+		return AE_NULL_ENTRY;

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1174

Quoted 10 lines
drivers/platform/x86/wmi.c +2 -3
 
-	if ((address > 0xFF) || !value)
+	if (!bytes || bytes > sizeof(*value))
 		return AE_BAD_PARAMETER;
 
-	if (function != ACPI_READ && function != ACPI_WRITE)
+	if (address > U8_MAX || address + bytes - 1 > U8_MAX)
 		return AE_BAD_PARAMETER;
 
-	if (bits != 8)

Since you want to support only 16 bit reads/writes, can you check for >16

Quoted 22 lines
drivers/platform/x86/wmi.c +4 -6
+	if (function != ACPI_READ && function != ACPI_WRITE)
 		return AE_BAD_PARAMETER;
 
 	if (function == ACPI_READ) {
-		result = ec_read(address, &temp);
-		*value = temp;
+		ret = ec_read_multiple(address, (u8 *)value, bytes);
 	} else {
-		temp = 0xff & *value;
-		result = ec_write(address, temp);
+		ret = ec_write_multiple(address, (u8 *)value, bytes);
 	}
 
-	switch (result) {
+	switch (ret) {
 	case -EINVAL:
 		return AE_BAD_PARAMETER;
 	case -ENODEV:
--

2.39.2

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
author
#7 lore raw

Am 09.03.24 um 18:07 schrieb Kuppuswamy Sathyanarayanan:

Quoted 34 lines · Armin Wolf
Quoted 33 lines

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Changes since v3:
- change type of variable i to size_t

Changes since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
  drivers/platform/x86/wmi.c | 49 ++++++++++++++++++++++++++++++--------
  1 file changed, 39 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +0 -0
index 1920e115da89..d9bf6d452b3a 100644
@@ -1153,6 +1153,34 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
  	return 0;
  }
 
Quoted 14 lines

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1168

Quoted 13 lines
drivers/platform/x86/wmi.c +13 -0
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_read(address + i, &buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}

Why not use ec_transaction?

Hi,

because ec_transaction() is meant to send raw commands to the EC. And AFAIK read/write transactions can only transfer a single byte at once, so using ec_transaction() would yield no benefit here.

Quoted 36 lines
Quoted 5 lines
drivers/platform/x86/wmi.c +4 -0
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1182

Quoted 10 lines
drivers/platform/x86/wmi.c +10 -0
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_write(address + i, buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}

Same as above.

Quoted 20 lines
drivers/platform/x86/wmi.c +7 -3
+
  /*
   * WMI can have EmbeddedControl access regions. In which case, we just want to
   * hand these off to the EC driver.
@@ -1162,27 +1190,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
  			  u32 bits, u64 *value,
  			  void *handler_context, void *region_context)
  {
-	int result = 0;
-	u8 temp = 0;
+	int bytes = bits / BITS_PER_BYTE;
+	int ret;
+
+	if (!value)
+		return AE_NULL_ENTRY;
 
-	if ((address > 0xFF) || !value)
+	if (!bytes || bytes > sizeof(*value))
  		return AE_BAD_PARAMETER;
 
Quoted 6 lines

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1174

Quoted 5 lines
drivers/platform/x86/wmi.c +1 -2
-	if (function != ACPI_READ && function != ACPI_WRITE)
+	if (address > U8_MAX || address + bytes - 1 > U8_MAX)
  		return AE_BAD_PARAMETER;
 
-	if (bits != 8)

Since you want to support only 16 bit reads/writes, can you check for >16

The 16 bit reads/writes where meant as an example, ACPI code can request much larger values. The WMI EC handler should be able to handle those, just like the regular ACPI EC handler.

Thanks,
Armin Wolf

Quoted 22 lines
Quoted 22 lines
drivers/platform/x86/wmi.c +4 -6
+	if (function != ACPI_READ && function != ACPI_WRITE)
  		return AE_BAD_PARAMETER;
 
  	if (function == ACPI_READ) {
-		result = ec_read(address, &temp);
-		*value = temp;
+		ret = ec_read_multiple(address, (u8 *)value, bytes);
  	} else {
-		temp = 0xff & *value;
-		result = ec_write(address, temp);
+		ret = ec_write_multiple(address, (u8 *)value, bytes);
  	}
 
-	switch (result) {
+	switch (ret) {
  	case -EINVAL:
  		return AE_BAD_PARAMETER;
  	case -ENODEV:
--

2.39.2

#8 lore raw
Quoted 7 lines · Armin Wolf

Am 09.03.24 um 18:07 schrieb Kuppuswamy Sathyanarayanan:

Quoted 4 lines · Armin Wolf
Quoted 3 lines

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal

/s/prefectly/perfectly

Quoted 45 lines
Quoted 30 lines
Quoted 30 lines

according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Changes since v3:
- change type of variable i to size_t

Changes since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
  drivers/platform/x86/wmi.c | 49 ++++++++++++++++++++++++++++++--------
  1 file changed, 39 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +0 -0
index 1920e115da89..d9bf6d452b3a 100644
@@ -1153,6 +1153,34 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
     return 0;
  }
 
Quoted 14 lines

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1168

Quoted 13 lines
drivers/platform/x86/wmi.c +13 -0
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+    size_t i;
+    int ret;
+
+    for (i = 0; i < bytes; i++) {
+            ret = ec_read(address + i, &buffer[i]);
+            if (ret < 0)
+                    return ret;
+    }
+
+    return 0;
+}

Why not use ec_transaction?

Quoted 4 lines

Hi,

because ec_transaction() is meant to send raw commands to the EC. And AFAIK read/write transactions can only transfer a single byte at once, so using ec_transaction() would yield no benefit here.

From the implementation, I don't see any length restriction. If it is a functional restriction, then fine.

int ec_transaction(u8 command,
const u8 *wdata, unsigned wdata_len, u8 *rdata, unsigned rdata_len)

{
        struct transaction t = {.command = command,
                                .wdata = wdata, .rdata = rdata,
                                .wlen = wdata_len, .rlen = rdata_len};

        if (!first_ec)
                return -ENODEV;

        return acpi_ec_transaction(first_ec, &t);
}

EXPORT_SYMBOL(ec_transaction);

Quoted 41 lines
Quoted 41 lines
Quoted 6 lines
drivers/platform/x86/wmi.c +4 -0
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+    size_t i;

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1182

Quoted 10 lines
drivers/platform/x86/wmi.c +10 -0
+    int ret;
+
+    for (i = 0; i < bytes; i++) {
+            ret = ec_write(address + i, buffer[i]);
+            if (ret < 0)
+                    return ret;
+    }
+
+    return 0;
+}

Same as above.

Quoted 24 lines
drivers/platform/x86/wmi.c +8 -4
+
  /*
   * WMI can have EmbeddedControl access regions. In which case, we just want to
   * hand these off to the EC driver.
@@ -1162,27 +1190,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
                       u32 bits, u64 *value,
                       void *handler_context, void *region_context)
  {
-    int result = 0;
-    u8 temp = 0;
+    int bytes = bits / BITS_PER_BYTE;
+    int ret;
+
+    if (!value)
+            return AE_NULL_ENTRY;
 
-    if ((address > 0xFF) || !value)
+    if (!bytes || bytes > sizeof(*value))
             return AE_BAD_PARAMETER;
 
-    if (function != ACPI_READ && function != ACPI_WRITE)
+    if (address > U8_MAX || address + bytes - 1 > U8_MAX)
             return AE_BAD_PARAMETER;
 
Quoted 6 lines
Quoted 2 lines

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1174

Quoted 1 line
drivers/platform/x86/wmi.c +0 -1
-    if (bits != 8)

Since you want to support only 16 bit reads/writes, can you check for >16

The 16 bit reads/writes where meant as an example, ACPI code can request much larger values. The WMI EC handler should be able to handle those, just like the regular ACPI EC handler.

Got it.

Quoted 25 lines

Thanks,
Armin Wolf

Quoted 22 lines
Quoted 22 lines
drivers/platform/x86/wmi.c +4 -6
+    if (function != ACPI_READ && function != ACPI_WRITE)
             return AE_BAD_PARAMETER;
 
     if (function == ACPI_READ) {
-            result = ec_read(address, &temp);
-            *value = temp;
+            ret = ec_read_multiple(address, (u8 *)value, bytes);
     } else {
-            temp = 0xff & *value;
-            result = ec_write(address, temp);
+            ret = ec_write_multiple(address, (u8 *)value, bytes);
     }
 
-    switch (result) {
+    switch (ret) {
     case -EINVAL:
             return AE_BAD_PARAMETER;
     case -ENODEV:
--

2.39.2

author
#9 lore raw

Am 10.03.24 um 03:39 schrieb Kuppuswamy Sathyanarayanan:

Quoted 73 lines · Armin Wolf
Quoted 6 lines

Am 09.03.24 um 18:07 schrieb Kuppuswamy Sathyanarayanan:

Quoted 4 lines · Armin Wolf
Quoted 3 lines

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal

/s/prefectly/perfectly

Quoted 44 lines
Quoted 30 lines
Quoted 30 lines

according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Changes since v3:
- change type of variable i to size_t

Changes since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
   drivers/platform/x86/wmi.c | 49 ++++++++++++++++++++++++++++++--------
   1 file changed, 39 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +0 -0
index 1920e115da89..d9bf6d452b3a 100644
@@ -1153,6 +1153,34 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
      return 0;
   }
 
Quoted 14 lines

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1168

Quoted 13 lines
drivers/platform/x86/wmi.c +13 -0
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+    size_t i;
+    int ret;
+
+    for (i = 0; i < bytes; i++) {
+            ret = ec_read(address + i, &buffer[i]);
+            if (ret < 0)
+                    return ret;
+    }
+
+    return 0;
+}

Why not use ec_transaction?

Quoted 4 lines

Hi,

because ec_transaction() is meant to send raw commands to the EC. And AFAIK read/write transactions can only transfer a single byte at once, so using ec_transaction() would yield no benefit here.

From the implementation, I don't see any length restriction. If it is
a functional restriction, then fine.

int ec_transaction(u8 command,
const u8 *wdata, unsigned wdata_len, u8 *rdata, unsigned rdata_len)

{
         struct transaction t = {.command = command,
                                 .wdata = wdata, .rdata = rdata,
                                 .wlen = wdata_len, .rlen = rdata_len};

         if (!first_ec)
                 return -ENODEV;

         return acpi_ec_transaction(first_ec, &t);
}
Quoted 1 line

EXPORT_SYMBOL(ec_transaction);

Since we are using the ACPI_EC_COMMAND_READ/_WRITE, we can only read/write a single byte, as specified in ACPI (12.3.1 and 12.3.2).

Thanks,
Armin Wolf

Quoted 71 lines
Quoted 35 lines
Quoted 35 lines
Quoted 4 lines
drivers/platform/x86/wmi.c +4 -0
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+    size_t i;

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1182

Quoted 10 lines
drivers/platform/x86/wmi.c +10 -0
+    int ret;
+
+    for (i = 0; i < bytes; i++) {
+            ret = ec_write(address + i, buffer[i]);
+            if (ret < 0)
+                    return ret;
+    }
+
+    return 0;
+}

Same as above.

Quoted 20 lines
drivers/platform/x86/wmi.c +7 -3
+
   /*
    * WMI can have EmbeddedControl access regions. In which case, we just want to
    * hand these off to the EC driver.
@@ -1162,27 +1190,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
                        u32 bits, u64 *value,
                        void *handler_context, void *region_context)
   {
-    int result = 0;
-    u8 temp = 0;
+    int bytes = bits / BITS_PER_BYTE;
+    int ret;
+
+    if (!value)
+            return AE_NULL_ENTRY;
 
-    if ((address > 0xFF) || !value)
+    if (!bytes || bytes > sizeof(*value))
              return AE_BAD_PARAMETER;
 
Quoted 9 lines
Quoted 6 lines

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1174

Quoted 5 lines
drivers/platform/x86/wmi.c +1 -2
-    if (function != ACPI_READ && function != ACPI_WRITE)
+    if (address > U8_MAX || address + bytes - 1 > U8_MAX)
              return AE_BAD_PARAMETER;
 
-    if (bits != 8)

Since you want to support only 16 bit reads/writes, can you check for >16

The 16 bit reads/writes where meant as an example, ACPI code can request much larger values. The WMI EC handler should be able to handle those, just like the regular ACPI EC handler.

Got it.

Quoted 25 lines

Thanks,
Armin Wolf

Quoted 22 lines
Quoted 22 lines
drivers/platform/x86/wmi.c +4 -6
+    if (function != ACPI_READ && function != ACPI_WRITE)
              return AE_BAD_PARAMETER;
 
      if (function == ACPI_READ) {
-            result = ec_read(address, &temp);
-            *value = temp;
+            ret = ec_read_multiple(address, (u8 *)value, bytes);
      } else {
-            temp = 0xff & *value;
-            result = ec_write(address, temp);
+            ret = ec_write_multiple(address, (u8 *)value, bytes);
      }
 
-    switch (result) {
+    switch (ret) {
      case -EINVAL:
              return AE_BAD_PARAMETER;
      case -ENODEV:
--

2.39.2

maintainer Reviewed-by on v4 1/2
#10 lore raw
In reply to Armin Wolf
Quoted 93 lines · Armin Wolf

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Changes since v3:
- change type of variable i to size_t

Changes since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
 drivers/platform/x86/wmi.c | 49 ++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +37 -7
index 1920e115da89..d9bf6d452b3a 100644
@@ -1153,6 +1153,34 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
 	return 0;
 }
 
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_read(address + i, &buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	size_t i;
+	int ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_write(address + i, buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
 /*
  * WMI can have EmbeddedControl access regions. In which case, we just want to
  * hand these off to the EC driver.
@@ -1162,27 +1190,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 			  u32 bits, u64 *value,
 			  void *handler_context, void *region_context)
 {
-	int result = 0;
-	u8 temp = 0;
+	int bytes = bits / BITS_PER_BYTE;
+	int ret;
+
+	if (!value)
+		return AE_NULL_ENTRY;
 
-	if ((address > 0xFF) || !value)
+	if (!bytes || bytes > sizeof(*value))
 		return AE_BAD_PARAMETER;
 
-	if (function != ACPI_READ && function != ACPI_WRITE)
+	if (address > U8_MAX || address + bytes - 1 > U8_MAX)
 		return AE_BAD_PARAMETER;
 
-	if (bits != 8)
+	if (function != ACPI_READ && function != ACPI_WRITE)
 		return AE_BAD_PARAMETER;
 
 	if (function == ACPI_READ) {
-		result = ec_read(address, &temp);
-		*value = temp;
+		ret = ec_read_multiple(address, (u8 *)value, bytes);
 	} else {

drivers/platform/x86/wmi.c acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address, · line 1217

Quoted 10 lines
drivers/platform/x86/wmi.c +2 -3
-		temp = 0xff & *value;
-		result = ec_write(address, temp);
+		ret = ec_write_multiple(address, (u8 *)value, bytes);
 	}
 
-	switch (result) {
+	switch (ret) {
 	case -EINVAL:
 		return AE_BAD_PARAMETER;
 	case -ENODEV:

Seems okay now, thanks.

Signature · 1 line
 i.