]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
habanalabs: return current power via INFO IOCTL
authorSagiv Ozeri <sozeri@habana.ai>
Tue, 23 Feb 2021 16:00:05 +0000 (18:00 +0200)
committerOded Gabbay <ogabbay@kernel.org>
Fri, 9 Apr 2021 11:09:23 +0000 (14:09 +0300)
Add driver implementation for reading the current power from the device
CPU F/W.

Signed-off-by: Sagiv Ozeri <sozeri@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/misc/habanalabs/common/firmware_if.c
drivers/misc/habanalabs/common/habanalabs.h
drivers/misc/habanalabs/common/habanalabs_ioctl.c
drivers/misc/habanalabs/include/common/cpucp_if.h
include/uapi/misc/habanalabs.h

index 6f3692bf5efffb8c338cfd06bb46fbf6ea9199f1..2a58edaf984a70232d325c6d43e7018f10558542 100644 (file)
@@ -565,6 +565,29 @@ int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u16 pll_index,
        return rc;
 }
 
+int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power)
+{
+       struct cpucp_packet pkt;
+       u64 result;
+       int rc;
+
+       memset(&pkt, 0, sizeof(pkt));
+
+       pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
+                               CPUCP_PKT_CTL_OPCODE_SHIFT);
+
+       rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
+                       HL_CPUCP_INFO_TIMEOUT_USEC, &result);
+       if (rc) {
+               dev_err(hdev->dev, "Failed to read power, error %d\n", rc);
+               return rc;
+       }
+
+       *power = result;
+
+       return rc;
+}
+
 static void detect_cpu_boot_status(struct hl_device *hdev, u32 status)
 {
        /* Some of the status codes below are deprecated in newer f/w
index fdb2a8c91f60168e6d6d01bd433eddec247b1f77..392a4a569049b82b152e6ebccf17150b6f21c289 100644 (file)
@@ -2361,6 +2361,7 @@ int hl_fw_cpucp_total_energy_get(struct hl_device *hdev,
                        u64 *total_energy);
 int hl_fw_cpucp_pll_info_get(struct hl_device *hdev, u16 pll_index,
                u16 *pll_freq_arr);
+int hl_fw_cpucp_power_get(struct hl_device *hdev, u64 *power);
 int hl_fw_init_cpu(struct hl_device *hdev, u32 cpu_boot_status_reg,
                        u32 msg_to_cpu_reg, u32 cpu_msg_status_reg,
                        u32 cpu_security_boot_status_reg, u32 boot_err0_reg,
index 848c2e58830100b74b1eb6e6a3850e7d783b3fcd..9fc429b82a9223194120fc754998125e76483501 100644 (file)
@@ -446,6 +446,25 @@ static int pll_frequency_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
                min((size_t) max_size, sizeof(freq_info))) ? -EFAULT : 0;
 }
 
+static int power_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
+{
+       struct hl_device *hdev = hpriv->hdev;
+       u32 max_size = args->return_size;
+       struct hl_power_info power_info = {0};
+       void __user *out = (void __user *) (uintptr_t) args->return_pointer;
+       int rc;
+
+       if ((!max_size) || (!out))
+               return -EINVAL;
+
+       rc = hl_fw_cpucp_power_get(hdev, &power_info.power);
+       if (rc)
+               return rc;
+
+       return copy_to_user(out, &power_info,
+               min((size_t) max_size, sizeof(power_info))) ? -EFAULT : 0;
+}
+
 static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
                                struct device *dev)
 {
@@ -526,6 +545,9 @@ static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
        case HL_INFO_PLL_FREQUENCY:
                return pll_frequency_info(hpriv, args);
 
+       case HL_INFO_POWER:
+               return power_info(hpriv, args);
+
        default:
                dev_err(dev, "Invalid request %d\n", args->op);
                rc = -ENOTTY;
index bf4e7900d8c8f0602265599c7ff1698f1ce54fe5..6ba480a316ced1d5c05bba1470d949b6e949b847 100644 (file)
@@ -296,6 +296,9 @@ enum pq_init_status {
  *       The result is composed of 4 outputs, each is 16-bit
  *       frequency in MHz.
  *
+ * CPUCP_PACKET_POWER_GET
+ *       Fetch the present power consumption of the device (Current * Voltage).
+ *
  */
 
 enum cpucp_packet_id {
@@ -329,6 +332,8 @@ enum cpucp_packet_id {
        CPUCP_PACKET_PCIE_REPLAY_CNT_GET,       /* internal */
        CPUCP_PACKET_TOTAL_ENERGY_GET,          /* internal */
        CPUCP_PACKET_PLL_INFO_GET,              /* internal */
+       CPUCP_PACKET_NIC_STATUS,                /* internal */
+       CPUCP_PACKET_POWER_GET,                 /* internal */
 };
 
 #define CPUCP_PACKET_FENCE_VAL 0xFE8CE7A5
index 05c7cf4e727ec58e3e95955d7d5d24e46a052804..92fd000ce0d3c5b51a8f07fc9cb419359097931d 100644 (file)
@@ -297,6 +297,7 @@ enum hl_device_status {
 #define HL_INFO_SYNC_MANAGER           14
 #define HL_INFO_TOTAL_ENERGY           15
 #define HL_INFO_PLL_FREQUENCY          16
+#define HL_INFO_POWER                  17
 
 #define HL_INFO_VERSION_MAX_LEN        128
 #define HL_INFO_CARD_NAME_MAX_LEN      16
@@ -410,6 +411,14 @@ struct hl_pll_frequency_info {
        __u16 output[HL_PLL_NUM_OUTPUTS];
 };
 
+/**
+ * struct hl_power_info - power information
+ * @power: power consumption
+ */
+struct hl_power_info {
+       __u64 power;
+};
+
 /**
  * struct hl_info_sync_manager - sync manager information
  * @first_available_sync_object: first available sob