2023-08-04 12:57:15

by Dhruva Gole

[permalink] [raw]
Subject: [PATCH V7 0/4] firmware: ti_sci: Introduce system suspend support

Abstract
********

This series introduces necessary ti_sci driver functionality to support
DeepSleep mode (suspend to mem) on TI K3 AM62x. DeepSleep mode is
described in section "6.2.4.4 DeepSleep" of the AM62x Technical Reference
Manual [0].

Summary
*******

This series is a fixup and rebase of the patch series by
Dave Gerlach [1]. It applies on top of next-20230731.

The kernel triggers entry to DeepSleep mode through the mem suspend
transition with the following:

* At the bootloader stage, one is expected to package the TIFS stub
which then gets pulled into the Tightly coupled memory of the Device Mgr
R5 when it starts up. If using U-Boot, then it requires tispl.bin to
contain the TIFS stub. Refer to ti-u-boot patch [3] for further
details. The supported firmware version is from TI Processor SDK
>= 09.00 ie. tag 09.00.00.006 from ti-linux-firmware [4].

* Use a TF-A binary that supports PSCI_SYSTEM_SUSPEND call. This causes
system to use PSCI system suspend as last step of mem sleep.

* The firmware requires that the OS sends a TISCI_MSG_PREPARE_SLEEP
message in order to provide details about suspend, so we must add the
ability to send this message. We also add TISCI_MSG_LPM_WAKE_REASON
and TISCI_MSG_SET_IO_ISOLATION messages as part of a new PM ops. These
messages are part of the TISCI PM Low Power Mode API [2]. (Patch 2)

* A memory address must be provided to the firmware using the above
message, which is allocated and managed by dma_alloc_attrs()
and friends. This memory address can be used by the firmware to
save necessary context at that physical location in the DDR RAM. (Patch 3)

* Finally, the ti_sci driver must actually send TISCI_MSG_PREPARE_SLEEP
message to firmware with the above information included, which it
does during the driver suspend handler when PM_MEM_SUSPEND is the
determined state being entered. (Patch 4)

It currently enables only DeepSleep mode, but even if any additional
modes are needed to be supported in future, they would not require any
changes to the TISCI LPM APIs [2]. The enabling of additional modes
would be done via GenPD changes that is currently in the works.

Testing:
*******

In can be tested with the following branch:
https://github.com/DhruvaG2000/v-linux/commits/lpm-upstream-6.5

Tested on SK-AM62B [6] here:
https://gist.github.com/DhruvaG2000/8410fac048c677c40cd94f5169b5b0b4

Limitations:
************

* It is critical for deepsleep to work that the bootloader has
loaded the TIFS stub at the desired location and that the DM has copied
it correctly into it's TCM. Linux has no way of knowing whether a valid
FS Stub exists and has been loaded or not. The Device Manager also
doesn't send any NACK if a proper TIFS Stub is not present to prevent
the deepsleep somehow. This problem is somewhat addressed in this patch
series where we check which SOCs support LPM and based on that set the
fw_caps.

* Currently, DeepSleep is only supported on SK-AM62B with DDR4.
Boards with LPDDR part like Beagle Play and AM62x LP have a known FW issue.

Base commit:
************

commit ec8939156379 (tag: next-20230731) ("Add linux-next specific files for 20230731")

origin:
linux-next https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git

Changelog:
**********

v7:
- Address Andrew's concerns on SYSFW fw_caps API
- Remove all the unused functions and variables including
set_io_isolation and wake_reason calls
- use dma_free_attrs
- remove IO isolation related code from linux side,
TODO: Add it in ATF enter_sleep perhaps?

v6:
- link to v6 [5]
- Loading of FS Stub from linux no longer needed, hence drop that patch,
- Drop 1/6 and 5/6 from the previous series [4].
- Add system suspend resume callbacks which were removed in
commit 9225bcdedf16297a346082e7d23b0e8434aa98ed ("firmware: ti_sci: Use
system_state to determine polling")
- Use IO isolation while putting the system in suspend to RAM

v5:
- Add support (patch 3) for detecting the low power modes (LPM) of the
FW/SoC with a recently introduced core TISCI_MSG_QUERY_FW_CAPS message.
- Use TISCI_MSG_QUERY_FW_CAPS instead of misusing the TISCI_MSG_PREPARE_SLEEP
to detect the FW/SoC low power caps (patch 4).
- Take into account the supported LPMs in ti_sci_prepare_system_suspend()
and handle the case when CONFIG_SUSPEND is not enabled (patch 6) that
was reported by Roger Quadros and LKP.
- Pick up Rob Herring's "Reviewed-by" tag for the binding patch.

v4:
- Fix checkpacth warnings in patches 2 and 3.
- Drop the links with anchors in patch 2.

v3:
- Fix the compile warnings on 32-bit platforms reported by the kernel
test robot in patches (3,5).
- Pick up Roger's "Tested-by" tags.

v2:
- Addressed comments received for v1 series [1].
- Updated v1 patch 5 to use pm notifier to avoid firmware loading
issues.
- Dropped the reserved region requirement and allocate DMA memory
instead. The reserved region binding patch is also removed.
- Introduce two more TISCI LPM messages that are supported in SysFW.
- Fixes in error handling.

References:
***********

[0] https://www.ti.com/lit/pdf/spruiv7
[1] https://lore.kernel.org/lkml/[email protected]
[2] https://software-dl.ti.com/tisci/esd/latest/2_tisci_msgs/pm/lpm.html
[3] https://git.ti.com/cgit/ti-u-boot/ti-u-boot/commit/?h=ti-u-boot-2023.04&id=91886b68025c7ad121e62d1fc1fa4601eeb736cd
[4] https://git.ti.com/cgit/processor-firmware/ti-linux-firmware/commit/?h=ti-linux-firmware-next&id=905eb58564581d951d148f45828e8c8a142a5938
[5] https://lore.kernel.org/all/[email protected]/
[6] https://www.ti.com/tool/SK-AM62B


Cc: Andrew Davis <[email protected]>
Cc: Vibhore Vardhan <[email protected]>
Cc: Kevin Hilman <[email protected]>
Cc: Vignesh Raghavendra <[email protected]>
Cc: Tony Lindgren <[email protected]>

Dave Gerlach (2):
firmware: ti_sci: Introduce Power Management Ops
firmware: ti_sci: Allocate memory for Low Power Modes

Dhruva Gole (1):
firmware: ti_sci: Add system suspend call

Georgi Vlaev (1):
firmware: ti_sci: Add support for querying the firmware caps

drivers/firmware/ti_sci.c | 234 ++++++++++++++++++++++++-
drivers/firmware/ti_sci.h | 56 +++++-
include/linux/soc/ti/ti_sci_protocol.h | 15 ++
3 files changed, 303 insertions(+), 2 deletions(-)

--
2.34.1



2023-08-04 13:15:44

by Dhruva Gole

[permalink] [raw]
Subject: [PATCH V7 2/4] firmware: ti_sci: Add support for querying the firmware caps

From: Georgi Vlaev <[email protected]>

This patch adds support for the TISCI_MSG_QUERY_FW_CAPS
message, used to retrieve the firmware capabilities of the
currently running system firmware. The message belongs to
the TISCI general core message API [1] and is available in
SysFW version 08.04.03 and above. Currently, the message is
supported on devices with split architecture of the system
firmware (DM + TIFS) like AM62x. Old revisions or not yet
supported platforms will NACK this request.

We're using this message locally in ti_sci.c to get the low
power featutes of the FW/SoC. As there's no other kernel
consumers yet, this is not added to struct ti_sci_core_ops.

Also have a has_lpm list of SOCs to ensure that proper LPM
capabilities are used and if any versions of firmware that happen to
advertise garbage value for fw_caps are ignored.
This was needed as the API is buggy on TI SYSFW v09.00 and below

[1] https://software-dl.ti.com/tisci/esd/latest/2_tisci_msgs/general/core.html

Signed-off-by: Georgi Vlaev <[email protected]>
[[email protected]: add workaround for sysfw fw_caps inconsistency]
Signed-off-by: Dhruva Gole <[email protected]>
---
drivers/firmware/ti_sci.c | 81 +++++++++++++++++++++++++++++++++++++++
drivers/firmware/ti_sci.h | 26 +++++++++++++
2 files changed, 107 insertions(+)

diff --git a/drivers/firmware/ti_sci.c b/drivers/firmware/ti_sci.c
index fc35d78b7e42..ac64e328c313 100644
--- a/drivers/firmware/ti_sci.c
+++ b/drivers/firmware/ti_sci.c
@@ -21,6 +21,7 @@
#include <linux/slab.h>
#include <linux/soc/ti/ti-msgmgr.h>
#include <linux/soc/ti/ti_sci_protocol.h>
+#include <linux/sys_soc.h>
#include <linux/reboot.h>

#include "ti_sci.h"
@@ -1723,6 +1724,86 @@ static int ti_sci_cmd_prepare_sleep(const struct ti_sci_handle *handle, u8 mode,
return ret;
}

+/*
+ * This is the list of SoCs not affected by SYSFW Bug causing the fw_caps
+ * to return garbage values.
+ * As and when new SoC's start supporting low power modes, this struct can
+ * be updated with those new SOC family entries.
+ */
+static const struct soc_device_attribute has_lpm[] = {
+ { .family = "AM62X" },
+ { /* sentinel */ }
+};
+
+/**
+ * ti_sci_msg_cmd_query_fw_caps() - Get the FW/SoC capabilities
+ * @handle: Pointer to TI SCI handle
+ * @fw_caps: Each bit in fw_caps indicating one FW/SOC capability
+ *
+ * Return: 0 if all went well, else returns appropriate error value.
+ */
+static int ti_sci_msg_cmd_query_fw_caps(const struct ti_sci_handle *handle,
+ u64 *fw_caps)
+{
+ struct ti_sci_info *info;
+ struct ti_sci_xfer *xfer;
+ struct ti_sci_msg_resp_query_fw_caps *resp;
+ struct device *dev;
+ int ret = 0;
+
+ if (IS_ERR(handle))
+ return PTR_ERR(handle);
+ if (!handle)
+ return -EINVAL;
+
+ info = handle_to_ti_sci_info(handle);
+ dev = info->dev;
+
+ xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_FW_CAPS,
+ TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
+ sizeof(struct ti_sci_msg_hdr),
+ sizeof(*resp));
+ if (IS_ERR(xfer)) {
+ ret = PTR_ERR(xfer);
+ dev_err(dev, "Message alloc failed(%d)\n", ret);
+ return ret;
+ }
+
+ ret = ti_sci_do_xfer(info, xfer);
+ if (ret) {
+ dev_err(dev, "Mbox send fail %d\n", ret);
+ goto fail;
+ }
+
+ resp = (struct ti_sci_msg_resp_query_fw_caps *)xfer->xfer_buf;
+
+ if (!ti_sci_is_response_ack(resp)) {
+ ret = -ENODEV;
+ goto fail;
+ }
+
+ /*
+ * fw_caps 1st bit is used to check Generic capability. Other than
+ * that the 1:4 bits are used for various LPM capabilities.
+ * The API is buggy on SYSFW 9.00 and below, on some devices.
+ * Hence, to avoid any sort of bugs arising due to garbage values
+ * Let's allow the fw_caps to be set to whatever the firmware
+ * says only on devices listed under has_lpm. These devices should
+ * have lpm features tested and implemented in the firmware
+ * and only then should they be added to has_lpm struct.
+ * Otherwise, set the value to 1 that is the default.
+ */
+ if (fw_caps && soc_device_match(has_lpm))
+ *fw_caps = resp->fw_caps;
+ else
+ *fw_caps = resp->fw_caps & MSG_FLAG_CAPS_GENERIC;
+
+fail:
+ ti_sci_put_one_xfer(&info->minfo, xfer);
+
+ return ret;
+}
+
static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
{
struct ti_sci_info *info;
diff --git a/drivers/firmware/ti_sci.h b/drivers/firmware/ti_sci.h
index 9c9b4dae9521..56f66368746b 100644
--- a/drivers/firmware/ti_sci.h
+++ b/drivers/firmware/ti_sci.h
@@ -19,6 +19,7 @@
#define TI_SCI_MSG_WAKE_REASON 0x0003
#define TI_SCI_MSG_GOODBYE 0x0004
#define TI_SCI_MSG_SYS_RESET 0x0005
+#define TI_SCI_MSG_QUERY_FW_CAPS 0x0022

/* Device requests */
#define TI_SCI_MSG_SET_DEVICE_STATE 0x0200
@@ -135,6 +136,31 @@ struct ti_sci_msg_req_reboot {
struct ti_sci_msg_hdr hdr;
} __packed;

+/**
+ * struct ti_sci_msg_resp_query_fw_caps - Response for query firmware caps
+ * @hdr: Generic header
+ * @fw_caps: Each bit in fw_caps indicating one FW/SOC capability
+ * MSG_FLAG_CAPS_GENERIC: Generic capability (LPM not supported)
+ * MSG_FLAG_CAPS_LPM_DEEP_SLEEP: Deep Sleep LPM
+ * MSG_FLAG_CAPS_LPM_MCU_ONLY: MCU only LPM
+ * MSG_FLAG_CAPS_LPM_STANDBY: Standby LPM
+ * MSG_FLAG_CAPS_LPM_PARTIAL_IO: Partial IO in LPM
+ *
+ * Response to a generic message with message type TI_SCI_MSG_QUERY_FW_CAPS
+ * providing currently available SOC/firmware capabilities. SoC that don't
+ * support low power modes return only MSG_FLAG_CAPS_GENERIC capability.
+ */
+struct ti_sci_msg_resp_query_fw_caps {
+ struct ti_sci_msg_hdr hdr;
+#define MSG_FLAG_CAPS_GENERIC TI_SCI_MSG_FLAG(0)
+#define MSG_FLAG_CAPS_LPM_DEEP_SLEEP TI_SCI_MSG_FLAG(1)
+#define MSG_FLAG_CAPS_LPM_MCU_ONLY TI_SCI_MSG_FLAG(2)
+#define MSG_FLAG_CAPS_LPM_STANDBY TI_SCI_MSG_FLAG(3)
+#define MSG_FLAG_CAPS_LPM_PARTIAL_IO TI_SCI_MSG_FLAG(4)
+#define MSG_MASK_CAPS_LPM GENMASK_ULL(4, 1)
+ u64 fw_caps;
+} __packed;
+
/**
* struct ti_sci_msg_req_set_device_state - Set the desired state of the device
* @hdr: Generic header
--
2.34.1


2023-08-04 13:16:15

by Dhruva Gole

[permalink] [raw]
Subject: [PATCH V7 1/4] firmware: ti_sci: Introduce Power Management Ops

From: Dave Gerlach <[email protected]>

Introduce TISCI_MSG_PREPARE_SLEEP power management op supported by the
TISCI Low Power Mode API [1]. This message is currently supported only on
AM62x platforms.

It can prepare the SOC for entering into a low power mode and
provide details to firmware about the state being entered.

While at it, update the Copyright date as well for ti_sci

[1] https://software-dl.ti.com/tisci/esd/latest/2_tisci_msgs/pm/lpm.html

Signed-off-by: Dave Gerlach <[email protected]>
Signed-off-by: Dhruva Gole <[email protected]>
---
drivers/firmware/ti_sci.c | 64 +++++++++++++++++++++++++-
drivers/firmware/ti_sci.h | 30 +++++++++++-
include/linux/soc/ti/ti_sci_protocol.h | 15 ++++++
3 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/ti_sci.c b/drivers/firmware/ti_sci.c
index 26a37f47f4ca..fc35d78b7e42 100644
--- a/drivers/firmware/ti_sci.c
+++ b/drivers/firmware/ti_sci.c
@@ -2,7 +2,7 @@
/*
* Texas Instruments System Control Interface Protocol Driver
*
- * Copyright (C) 2015-2022 Texas Instruments Incorporated - https://www.ti.com/
+ * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
* Nishanth Menon
*/

@@ -1664,6 +1664,65 @@ static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
return ret;
}

+/**
+ * ti_sci_cmd_prepare_sleep() - Prepare system for system suspend
+ * @handle: pointer to TI SCI handle
+ * @mode: Device identifier
+ * @ctx_lo: Low part of address for context save
+ * @ctx_hi: High part of address for context save
+ * @debug_flags: Debug flags to pass to firmware
+ *
+ * Return: 0 if all went well, else returns appropriate error value.
+ */
+static int ti_sci_cmd_prepare_sleep(const struct ti_sci_handle *handle, u8 mode,
+ u32 ctx_lo, u32 ctx_hi, u32 debug_flags)
+{
+ struct ti_sci_info *info;
+ struct ti_sci_msg_req_prepare_sleep *req;
+ struct ti_sci_msg_hdr *resp;
+ struct ti_sci_xfer *xfer;
+ struct device *dev;
+ int ret = 0;
+
+ if (IS_ERR(handle))
+ return PTR_ERR(handle);
+ if (!handle)
+ return -EINVAL;
+
+ info = handle_to_ti_sci_info(handle);
+ dev = info->dev;
+
+ xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PREPARE_SLEEP,
+ TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
+ sizeof(*req), sizeof(*resp));
+ if (IS_ERR(xfer)) {
+ ret = PTR_ERR(xfer);
+ dev_err(dev, "Message alloc failed(%d)\n", ret);
+ return ret;
+ }
+
+ req = (struct ti_sci_msg_req_prepare_sleep *)xfer->xfer_buf;
+ req->mode = mode;
+ req->ctx_lo = ctx_lo;
+ req->ctx_hi = ctx_hi;
+ req->debug_flags = debug_flags;
+
+ ret = ti_sci_do_xfer(info, xfer);
+ if (ret) {
+ dev_err(dev, "Mbox send fail %d\n", ret);
+ goto fail;
+ }
+
+ resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
+
+ ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
+
+fail:
+ ti_sci_put_one_xfer(&info->minfo, xfer);
+
+ return ret;
+}
+
static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
{
struct ti_sci_info *info;
@@ -2806,6 +2865,7 @@ static void ti_sci_setup_ops(struct ti_sci_info *info)
struct ti_sci_core_ops *core_ops = &ops->core_ops;
struct ti_sci_dev_ops *dops = &ops->dev_ops;
struct ti_sci_clk_ops *cops = &ops->clk_ops;
+ struct ti_sci_pm_ops *pmops = &ops->pm_ops;
struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
@@ -2845,6 +2905,8 @@ static void ti_sci_setup_ops(struct ti_sci_info *info)
cops->set_freq = ti_sci_cmd_clk_set_freq;
cops->get_freq = ti_sci_cmd_clk_get_freq;

+ pmops->prepare_sleep = ti_sci_cmd_prepare_sleep;
+
rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
rm_core_ops->get_range_from_shost =
ti_sci_cmd_get_resource_range_from_shost;
diff --git a/drivers/firmware/ti_sci.h b/drivers/firmware/ti_sci.h
index ef3a8214d002..9c9b4dae9521 100644
--- a/drivers/firmware/ti_sci.h
+++ b/drivers/firmware/ti_sci.h
@@ -6,7 +6,7 @@
* The system works in a message response protocol
* See: http://processors.wiki.ti.com/index.php/TISCI for details
*
- * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
+ * Copyright (C) 2015-2023 Texas Instruments Incorporated - https://www.ti.com/
*/

#ifndef __TI_SCI_H
@@ -35,6 +35,9 @@
#define TI_SCI_MSG_QUERY_CLOCK_FREQ 0x010d
#define TI_SCI_MSG_GET_CLOCK_FREQ 0x010e

+/* Low Power Mode Requests */
+#define TI_SCI_MSG_PREPARE_SLEEP 0x0300
+
/* Resource Management Requests */
#define TI_SCI_MSG_GET_RESOURCE_RANGE 0x1500

@@ -545,6 +548,31 @@ struct ti_sci_msg_resp_get_clock_freq {
u64 freq_hz;
} __packed;

+#define TISCI_MSG_VALUE_SLEEP_MODE_DEEP_SLEEP 0x0
+
+/**
+ * struct tisci_msg_prepare_sleep_req - Request for TISCI_MSG_PREPARE_SLEEP.
+ *
+ * @hdr TISCI header to provide ACK/NAK flags to the host.
+ * @mode Low power mode to enter.
+ * @ctx_lo Low 32-bits of physical pointer to address to use for context save.
+ * @ctx_hi High 32-bits of physical pointer to address to use for context save.
+ * @debug_flags Flags that can be set to halt the sequence during suspend or
+ * resume to allow JTAG connection and debug.
+ *
+ * This message is used as the first step of entering a low power mode. It
+ * allows configurable information, including which state to enter to be
+ * easily shared from the application, as this is a non-secure message and
+ * therefore can be sent by anyone.
+ */
+struct ti_sci_msg_req_prepare_sleep {
+ struct ti_sci_msg_hdr hdr;
+ u8 mode;
+ u32 ctx_lo;
+ u32 ctx_hi;
+ u32 debug_flags;
+} __packed;
+
#define TI_SCI_IRQ_SECONDARY_HOST_INVALID 0xff

/**
diff --git a/include/linux/soc/ti/ti_sci_protocol.h b/include/linux/soc/ti/ti_sci_protocol.h
index bd0d11af76c5..566e442b6a43 100644
--- a/include/linux/soc/ti/ti_sci_protocol.h
+++ b/include/linux/soc/ti/ti_sci_protocol.h
@@ -195,6 +195,20 @@ struct ti_sci_clk_ops {
u64 *current_freq);
};

+/**
+ * struct ti_sci_pm_ops - Low Power Mode (LPM) control operations
+ * @prepare_sleep: Prepare to enter low power mode
+ * - mode: Low power mode to enter.
+ * - ctx_lo: Low 32-bits of physical address for context save.
+ * - ctx_hi: High 32-bits of physical address for context save.
+ * - ctx_lo: 'true' if frequency change is desired.
+ * - debug_flags: JTAG control flags for debug.
+ */
+struct ti_sci_pm_ops {
+ int (*prepare_sleep)(const struct ti_sci_handle *handle, u8 mode,
+ u32 ctx_lo, u32 ctx_hi, u32 flags);
+};
+
/**
* struct ti_sci_resource_desc - Description of TI SCI resource instance range.
* @start: Start index of the first resource range.
@@ -539,6 +553,7 @@ struct ti_sci_ops {
struct ti_sci_core_ops core_ops;
struct ti_sci_dev_ops dev_ops;
struct ti_sci_clk_ops clk_ops;
+ struct ti_sci_pm_ops pm_ops;
struct ti_sci_rm_core_ops rm_core_ops;
struct ti_sci_rm_irq_ops rm_irq_ops;
struct ti_sci_rm_ringacc_ops rm_ring_ops;
--
2.34.1


2023-08-04 13:17:46

by Dhruva Gole

[permalink] [raw]
Subject: [PATCH V7 3/4] firmware: ti_sci: Allocate memory for Low Power Modes

From: Dave Gerlach <[email protected]>

A region of memory in DDR must be used during Deep Sleep for saving
of some system context when using the ti_sci firmware. From DM's point
of view, this can be any contiguous region in the DDR, so can allocate
512KB of DMA reserved memory in probe(), instead of another carveout.

Also send a TISCI_MSG_QUERY_FW_CAPS message to the firmware during
probe to determine if any low power modes are supported and if
ti_sci_init_suspend should be called based on the response received.

Signed-off-by: Dave Gerlach <[email protected]>
Signed-off-by: Vibhore Vardhan <[email protected]>
Signed-off-by: Georgi Vlaev <[email protected]>
Tested-by: Roger Quadros <[email protected]>
[[email protected]: Use dma_alloc_attrs instead of dma_alloc_coherent]
Signed-off-by: Dhruva Gole <[email protected]>
---
drivers/firmware/ti_sci.c | 44 +++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)

diff --git a/drivers/firmware/ti_sci.c b/drivers/firmware/ti_sci.c
index ac64e328c313..512338699153 100644
--- a/drivers/firmware/ti_sci.c
+++ b/drivers/firmware/ti_sci.c
@@ -10,6 +10,7 @@

#include <linux/bitmap.h>
#include <linux/debugfs.h>
+#include <linux/dma-mapping.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/iopoll.h>
@@ -26,6 +27,9 @@

#include "ti_sci.h"

+/* Low power mode memory context size */
+#define LPM_CTX_MEM_SIZE 0x80000
+
/* List of all TI SCI devices active in system */
static LIST_HEAD(ti_sci_list);
/* Protection for the entire list */
@@ -97,6 +101,9 @@ struct ti_sci_desc {
* @minfo: Message info
* @node: list head
* @host_id: Host ID
+ * @ctx_mem_addr: Low power context memory phys address
+ * @ctx_mem_buf: Low power context memory buffer
+ * @fw_caps: FW/SoC low power capabilities
* @users: Number of users of this instance
*/
struct ti_sci_info {
@@ -114,6 +121,9 @@ struct ti_sci_info {
struct ti_sci_xfers_info minfo;
struct list_head node;
u8 host_id;
+ dma_addr_t ctx_mem_addr;
+ void *ctx_mem_buf;
+ u64 fw_caps;
/* protected by ti_sci_list_mutex */
int users;
};
@@ -3423,6 +3433,25 @@ static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
return NOTIFY_BAD;
}

+static int ti_sci_init_suspend(struct platform_device *pdev,
+ struct ti_sci_info *info)
+{
+ struct device *dev = &pdev->dev;
+
+ dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
+ info->ctx_mem_buf = dma_alloc_attrs(info->dev, LPM_CTX_MEM_SIZE,
+ &info->ctx_mem_addr,
+ GFP_KERNEL,
+ DMA_ATTR_NO_KERNEL_MAPPING |
+ DMA_ATTR_FORCE_CONTIGUOUS);
+ if (!info->ctx_mem_buf) {
+ dev_err(info->dev, "Failed to allocate LPM context memory\n");
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
/* Description for K2G */
static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
.default_host_id = 2,
@@ -3573,6 +3602,15 @@ static int ti_sci_probe(struct platform_device *pdev)
}
}

+ /*
+ * Check if the firmware supports any optional low power modes
+ * and initialize them if present. Old revisions of TIFS (< 08.04)
+ * will NACK the request.
+ */
+ ret = ti_sci_msg_cmd_query_fw_caps(&info->handle, &info->fw_caps);
+ if (!ret && (info->fw_caps & MSG_MASK_CAPS_LPM))
+ ti_sci_init_suspend(pdev, info);
+
dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
info->handle.version.abi_major, info->handle.version.abi_minor,
info->handle.version.firmware_revision,
@@ -3620,6 +3658,12 @@ static int ti_sci_remove(struct platform_device *pdev)
mbox_free_channel(info->chan_rx);
}

+ if (info->ctx_mem_buf)
+ dma_free_attrs(info->dev, LPM_CTX_MEM_SIZE,
+ info->ctx_mem_buf,
+ info->ctx_mem_addr,
+ DMA_ATTR_NO_KERNEL_MAPPING |
+ DMA_ATTR_FORCE_CONTIGUOUS);
return ret;
}

--
2.34.1


2023-08-08 18:44:25

by Nishanth Menon

[permalink] [raw]
Subject: Re: [PATCH V7 2/4] firmware: ti_sci: Add support for querying the firmware caps

On 17:20-20230804, Dhruva Gole wrote:
> From: Georgi Vlaev <[email protected]>
>
[...]

> + /*
> + * fw_caps 1st bit is used to check Generic capability. Other than
> + * that the 1:4 bits are used for various LPM capabilities.
> + * The API is buggy on SYSFW 9.00 and below, on some devices.
> + * Hence, to avoid any sort of bugs arising due to garbage values
> + * Let's allow the fw_caps to be set to whatever the firmware
> + * says only on devices listed under has_lpm. These devices should
> + * have lpm features tested and implemented in the firmware
> + * and only then should they be added to has_lpm struct.
> + * Otherwise, set the value to 1 that is the default.
> + */
> + if (fw_caps && soc_device_match(has_lpm))
> + *fw_caps = resp->fw_caps;
> + else
> + *fw_caps = resp->fw_caps & MSG_FLAG_CAPS_GENERIC;

Fix your firmware please. drop the has_lpm stuff.. that is what caps is
for.

As part of ti_sci_setup_ops you get info where you can check ABI version
where this is valid and which is not, ti_sci_msg_cmd_query_fw_caps can
be populated based on that check. That is the reason info is passed to
setup_ops and why we have ABI IDs in the first place.

[...]

--
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3 1A34 DDB5 849D 1736 249D