.. | .. |
---|
31 | 31 | #include <linux/clk-provider.h> |
---|
32 | 32 | #include <linux/pm_domain.h> |
---|
33 | 33 | #include <linux/pm_runtime.h> |
---|
34 | | -#include <linux/devfreq_cooling.h> |
---|
35 | 34 | #include <linux/regmap.h> |
---|
36 | 35 | #include <linux/of_address.h> |
---|
37 | 36 | |
---|
38 | 37 | #ifndef FPGA_PLATFORM |
---|
39 | 38 | #include <soc/rockchip/rockchip_iommu.h> |
---|
40 | | -#include <soc/rockchip/rockchip_opp_select.h> |
---|
41 | | -#include <soc/rockchip/rockchip_system_monitor.h> |
---|
42 | | -#include <soc/rockchip/rockchip_ipa.h> |
---|
43 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
44 | | -#include <../drivers/devfreq/governor.h> |
---|
45 | | -#endif |
---|
46 | 39 | #endif |
---|
47 | 40 | |
---|
48 | 41 | #include "rknpu_ioctl.h" |
---|
.. | .. |
---|
50 | 43 | #include "rknpu_fence.h" |
---|
51 | 44 | #include "rknpu_drv.h" |
---|
52 | 45 | #include "rknpu_gem.h" |
---|
| 46 | +#include "rknpu_devfreq.h" |
---|
53 | 47 | |
---|
54 | 48 | #ifdef CONFIG_ROCKCHIP_RKNPU_DRM_GEM |
---|
55 | 49 | #include <drm/drm_device.h> |
---|
.. | .. |
---|
118 | 112 | .num_resets = ARRAY_SIZE(rknpu_resets), |
---|
119 | 113 | .nbuf_phyaddr = 0, |
---|
120 | 114 | .nbuf_size = 0, |
---|
121 | | - .max_submit_number = (1 << 12) - 1 |
---|
| 115 | + .max_submit_number = (1 << 12) - 1, |
---|
| 116 | + .core_mask = 0x1, |
---|
122 | 117 | }; |
---|
123 | 118 | |
---|
124 | 119 | static const struct rknpu_config rk3588_rknpu_config = { |
---|
.. | .. |
---|
137 | 132 | .num_resets = ARRAY_SIZE(rk3588_npu_resets), |
---|
138 | 133 | .nbuf_phyaddr = 0, |
---|
139 | 134 | .nbuf_size = 0, |
---|
140 | | - .max_submit_number = (1 << 12) - 1 |
---|
| 135 | + .max_submit_number = (1 << 12) - 1, |
---|
| 136 | + .core_mask = 0x7, |
---|
| 137 | +}; |
---|
| 138 | + |
---|
| 139 | +static const struct rknpu_config rk3583_rknpu_config = { |
---|
| 140 | + .bw_priority_addr = 0x0, |
---|
| 141 | + .bw_priority_length = 0x0, |
---|
| 142 | + .dma_mask = DMA_BIT_MASK(40), |
---|
| 143 | + .pc_data_amount_scale = 2, |
---|
| 144 | + .pc_task_number_bits = 12, |
---|
| 145 | + .pc_task_number_mask = 0xfff, |
---|
| 146 | + .pc_task_status_offset = 0x3c, |
---|
| 147 | + .pc_dma_ctrl = 0, |
---|
| 148 | + .bw_enable = 0, |
---|
| 149 | + .irqs = rk3588_npu_irqs, |
---|
| 150 | + .resets = rk3588_npu_resets, |
---|
| 151 | + .num_irqs = 2, |
---|
| 152 | + .num_resets = 2, |
---|
| 153 | + .nbuf_phyaddr = 0, |
---|
| 154 | + .nbuf_size = 0, |
---|
| 155 | + .max_submit_number = (1 << 12) - 1, |
---|
| 156 | + .core_mask = 0x3, |
---|
141 | 157 | }; |
---|
142 | 158 | |
---|
143 | 159 | static const struct rknpu_config rv1106_rknpu_config = { |
---|
.. | .. |
---|
156 | 172 | .num_resets = ARRAY_SIZE(rknpu_resets), |
---|
157 | 173 | .nbuf_phyaddr = 0, |
---|
158 | 174 | .nbuf_size = 0, |
---|
159 | | - .max_submit_number = (1 << 16) - 1 |
---|
| 175 | + .max_submit_number = (1 << 16) - 1, |
---|
| 176 | + .core_mask = 0x1, |
---|
160 | 177 | }; |
---|
161 | 178 | |
---|
162 | 179 | static const struct rknpu_config rk3562_rknpu_config = { |
---|
.. | .. |
---|
175 | 192 | .num_resets = ARRAY_SIZE(rknpu_resets), |
---|
176 | 193 | .nbuf_phyaddr = 0xfe400000, |
---|
177 | 194 | .nbuf_size = 256 * 1024, |
---|
178 | | - .max_submit_number = (1 << 16) - 1 |
---|
| 195 | + .max_submit_number = (1 << 16) - 1, |
---|
| 196 | + .core_mask = 0x1, |
---|
179 | 197 | }; |
---|
180 | 198 | |
---|
181 | 199 | /* driver probe and init */ |
---|
.. | .. |
---|
627 | 645 | if (job) { |
---|
628 | 646 | now = ktime_get(); |
---|
629 | 647 | subcore_data->timer.busy_time += |
---|
630 | | - ktime_us_delta(now, job->hw_recoder_time); |
---|
| 648 | + ktime_sub(now, job->hw_recoder_time); |
---|
631 | 649 | job->hw_recoder_time = now; |
---|
632 | 650 | } |
---|
633 | 651 | |
---|
634 | | - subcore_data->timer.busy_time_record = |
---|
| 652 | + subcore_data->timer.total_busy_time = |
---|
635 | 653 | subcore_data->timer.busy_time; |
---|
636 | 654 | subcore_data->timer.busy_time = 0; |
---|
| 655 | + |
---|
637 | 656 | spin_unlock_irqrestore(&rknpu_dev->irq_lock, flags); |
---|
638 | 657 | } |
---|
639 | 658 | |
---|
.. | .. |
---|
801 | 820 | } |
---|
802 | 821 | |
---|
803 | 822 | #ifndef FPGA_PLATFORM |
---|
804 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
805 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
806 | | - rockchip_monitor_volt_adjust_lock(rknpu_dev->mdev_info); |
---|
807 | | -#endif |
---|
| 823 | + rknpu_devfreq_lock(rknpu_dev); |
---|
808 | 824 | #endif |
---|
809 | 825 | |
---|
810 | 826 | if (rknpu_dev->multiple_domains) { |
---|
.. | .. |
---|
863 | 879 | |
---|
864 | 880 | out: |
---|
865 | 881 | #ifndef FPGA_PLATFORM |
---|
866 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
867 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
868 | | - rockchip_monitor_volt_adjust_unlock(rknpu_dev->mdev_info); |
---|
869 | | -#endif |
---|
| 882 | + rknpu_devfreq_unlock(rknpu_dev); |
---|
870 | 883 | #endif |
---|
871 | 884 | |
---|
872 | 885 | return ret; |
---|
.. | .. |
---|
880 | 893 | int ret; |
---|
881 | 894 | bool val; |
---|
882 | 895 | |
---|
883 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
884 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
885 | | - rockchip_monitor_volt_adjust_lock(rknpu_dev->mdev_info); |
---|
886 | | -#endif |
---|
| 896 | + rknpu_devfreq_lock(rknpu_dev); |
---|
887 | 897 | #endif |
---|
888 | 898 | |
---|
889 | 899 | pm_runtime_put_sync(dev); |
---|
.. | .. |
---|
905 | 915 | if (ret) { |
---|
906 | 916 | LOG_DEV_ERROR(dev, "iommu still enabled\n"); |
---|
907 | 917 | pm_runtime_get_sync(dev); |
---|
908 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
909 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
910 | | - rockchip_monitor_volt_adjust_unlock( |
---|
911 | | - rknpu_dev->mdev_info); |
---|
912 | | -#endif |
---|
| 918 | + rknpu_devfreq_unlock(rknpu_dev); |
---|
913 | 919 | return ret; |
---|
914 | 920 | } |
---|
915 | 921 | #else |
---|
.. | .. |
---|
925 | 931 | } |
---|
926 | 932 | |
---|
927 | 933 | #ifndef FPGA_PLATFORM |
---|
928 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
929 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
930 | | - rockchip_monitor_volt_adjust_unlock(rknpu_dev->mdev_info); |
---|
931 | | -#endif |
---|
| 934 | + rknpu_devfreq_unlock(rknpu_dev); |
---|
932 | 935 | #endif |
---|
933 | 936 | |
---|
934 | 937 | clk_bulk_disable_unprepare(rknpu_dev->num_clks, rknpu_dev->clks); |
---|
.. | .. |
---|
944 | 947 | return 0; |
---|
945 | 948 | } |
---|
946 | 949 | |
---|
947 | | -#ifndef FPGA_PLATFORM |
---|
948 | | -#if KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
949 | | -static struct monitor_dev_profile npu_mdevp = { |
---|
950 | | - .type = MONITOR_TYPE_DEV, |
---|
951 | | - .low_temp_adjust = rockchip_monitor_dev_low_temp_adjust, |
---|
952 | | - .high_temp_adjust = rockchip_monitor_dev_high_temp_adjust, |
---|
953 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE |
---|
954 | | - .update_volt = rockchip_monitor_check_rate_volt, |
---|
955 | | -#endif |
---|
956 | | -}; |
---|
957 | | - |
---|
958 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE |
---|
959 | | -static int npu_opp_helper(struct dev_pm_set_opp_data *data) |
---|
960 | | -{ |
---|
961 | | - struct device *dev = data->dev; |
---|
962 | | - struct dev_pm_opp_supply *old_supply_vdd = &data->old_opp.supplies[0]; |
---|
963 | | - struct dev_pm_opp_supply *old_supply_mem = &data->old_opp.supplies[1]; |
---|
964 | | - struct dev_pm_opp_supply *new_supply_vdd = &data->new_opp.supplies[0]; |
---|
965 | | - struct dev_pm_opp_supply *new_supply_mem = &data->new_opp.supplies[1]; |
---|
966 | | - struct regulator *vdd_reg = data->regulators[0]; |
---|
967 | | - struct regulator *mem_reg = data->regulators[1]; |
---|
968 | | - struct clk *clk = data->clk; |
---|
969 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
970 | | - struct rockchip_opp_info *opp_info = &rknpu_dev->opp_info; |
---|
971 | | - unsigned long old_freq = data->old_opp.rate; |
---|
972 | | - unsigned long new_freq = data->new_opp.rate; |
---|
973 | | - bool is_set_rm = true; |
---|
974 | | - bool is_set_clk = true; |
---|
975 | | - u32 target_rm = UINT_MAX; |
---|
976 | | - int ret = 0; |
---|
977 | | - |
---|
978 | | - if (!pm_runtime_active(dev)) { |
---|
979 | | - is_set_rm = false; |
---|
980 | | - if (opp_info->scmi_clk) |
---|
981 | | - is_set_clk = false; |
---|
982 | | - } |
---|
983 | | - |
---|
984 | | - ret = clk_bulk_prepare_enable(opp_info->num_clks, opp_info->clks); |
---|
985 | | - if (ret < 0) { |
---|
986 | | - LOG_DEV_ERROR(dev, "failed to enable opp clks\n"); |
---|
987 | | - return ret; |
---|
988 | | - } |
---|
989 | | - rockchip_get_read_margin(dev, opp_info, new_supply_vdd->u_volt, |
---|
990 | | - &target_rm); |
---|
991 | | - |
---|
992 | | - /* Change frequency */ |
---|
993 | | - LOG_DEV_DEBUG(dev, "switching OPP: %lu Hz --> %lu Hz\n", old_freq, |
---|
994 | | - new_freq); |
---|
995 | | - /* Scaling up? Scale voltage before frequency */ |
---|
996 | | - if (new_freq >= old_freq) { |
---|
997 | | - rockchip_set_intermediate_rate(dev, opp_info, clk, old_freq, |
---|
998 | | - new_freq, true, is_set_clk); |
---|
999 | | - ret = regulator_set_voltage(mem_reg, new_supply_mem->u_volt, |
---|
1000 | | - INT_MAX); |
---|
1001 | | - if (ret) { |
---|
1002 | | - LOG_DEV_ERROR(dev, |
---|
1003 | | - "failed to set volt %lu uV for mem reg\n", |
---|
1004 | | - new_supply_mem->u_volt); |
---|
1005 | | - goto restore_voltage; |
---|
1006 | | - } |
---|
1007 | | - ret = regulator_set_voltage(vdd_reg, new_supply_vdd->u_volt, |
---|
1008 | | - INT_MAX); |
---|
1009 | | - if (ret) { |
---|
1010 | | - LOG_DEV_ERROR(dev, |
---|
1011 | | - "failed to set volt %lu uV for vdd reg\n", |
---|
1012 | | - new_supply_vdd->u_volt); |
---|
1013 | | - goto restore_voltage; |
---|
1014 | | - } |
---|
1015 | | - rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm); |
---|
1016 | | - if (is_set_clk && clk_set_rate(clk, new_freq)) { |
---|
1017 | | - ret = -EINVAL; |
---|
1018 | | - LOG_DEV_ERROR(dev, "failed to set clk rate: %d\n", ret); |
---|
1019 | | - goto restore_rm; |
---|
1020 | | - } |
---|
1021 | | - /* Scaling down? Scale voltage after frequency */ |
---|
1022 | | - } else { |
---|
1023 | | - rockchip_set_intermediate_rate(dev, opp_info, clk, old_freq, |
---|
1024 | | - new_freq, false, is_set_clk); |
---|
1025 | | - rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm); |
---|
1026 | | - if (is_set_clk && clk_set_rate(clk, new_freq)) { |
---|
1027 | | - ret = -EINVAL; |
---|
1028 | | - LOG_DEV_ERROR(dev, "failed to set clk rate: %d\n", ret); |
---|
1029 | | - goto restore_rm; |
---|
1030 | | - } |
---|
1031 | | - ret = regulator_set_voltage(vdd_reg, new_supply_vdd->u_volt, |
---|
1032 | | - INT_MAX); |
---|
1033 | | - if (ret) { |
---|
1034 | | - LOG_DEV_ERROR(dev, |
---|
1035 | | - "failed to set volt %lu uV for vdd reg\n", |
---|
1036 | | - new_supply_vdd->u_volt); |
---|
1037 | | - goto restore_freq; |
---|
1038 | | - } |
---|
1039 | | - ret = regulator_set_voltage(mem_reg, new_supply_mem->u_volt, |
---|
1040 | | - INT_MAX); |
---|
1041 | | - if (ret) { |
---|
1042 | | - LOG_DEV_ERROR(dev, |
---|
1043 | | - "failed to set volt %lu uV for mem reg\n", |
---|
1044 | | - new_supply_mem->u_volt); |
---|
1045 | | - goto restore_freq; |
---|
1046 | | - } |
---|
1047 | | - } |
---|
1048 | | - |
---|
1049 | | - clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks); |
---|
1050 | | - |
---|
1051 | | - return 0; |
---|
1052 | | - |
---|
1053 | | -restore_freq: |
---|
1054 | | - if (is_set_clk && clk_set_rate(clk, old_freq)) |
---|
1055 | | - LOG_DEV_ERROR(dev, "failed to restore old-freq %lu Hz\n", |
---|
1056 | | - old_freq); |
---|
1057 | | -restore_rm: |
---|
1058 | | - rockchip_get_read_margin(dev, opp_info, old_supply_vdd->u_volt, |
---|
1059 | | - &target_rm); |
---|
1060 | | - rockchip_set_read_margin(dev, opp_info, opp_info->current_rm, |
---|
1061 | | - is_set_rm); |
---|
1062 | | -restore_voltage: |
---|
1063 | | - regulator_set_voltage(mem_reg, old_supply_mem->u_volt, INT_MAX); |
---|
1064 | | - regulator_set_voltage(vdd_reg, old_supply_vdd->u_volt, INT_MAX); |
---|
1065 | | - clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks); |
---|
1066 | | - |
---|
1067 | | - return ret; |
---|
1068 | | -} |
---|
1069 | | - |
---|
1070 | | -static int npu_devfreq_target(struct device *dev, unsigned long *freq, |
---|
1071 | | - u32 flags) |
---|
1072 | | -{ |
---|
1073 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
1074 | | - struct dev_pm_opp *opp; |
---|
1075 | | - unsigned long opp_volt; |
---|
1076 | | - int ret = 0; |
---|
1077 | | - |
---|
1078 | | - if (!npu_mdevp.is_checked) |
---|
1079 | | - return -EINVAL; |
---|
1080 | | - |
---|
1081 | | - opp = devfreq_recommended_opp(dev, freq, flags); |
---|
1082 | | - if (IS_ERR(opp)) |
---|
1083 | | - return PTR_ERR(opp); |
---|
1084 | | - opp_volt = dev_pm_opp_get_voltage(opp); |
---|
1085 | | - dev_pm_opp_put(opp); |
---|
1086 | | - |
---|
1087 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE |
---|
1088 | | - rockchip_monitor_volt_adjust_lock(rknpu_dev->mdev_info); |
---|
1089 | | -#endif |
---|
1090 | | - ret = dev_pm_opp_set_rate(dev, *freq); |
---|
1091 | | - if (!ret) { |
---|
1092 | | - rknpu_dev->current_freq = *freq; |
---|
1093 | | - if (rknpu_dev->devfreq) |
---|
1094 | | - rknpu_dev->devfreq->last_status.current_frequency = |
---|
1095 | | - *freq; |
---|
1096 | | - rknpu_dev->current_volt = opp_volt; |
---|
1097 | | - LOG_DEV_INFO(dev, "set rknpu freq: %lu, volt: %lu\n", |
---|
1098 | | - rknpu_dev->current_freq, rknpu_dev->current_volt); |
---|
1099 | | - } |
---|
1100 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE |
---|
1101 | | - rockchip_monitor_volt_adjust_unlock(rknpu_dev->mdev_info); |
---|
1102 | | -#endif |
---|
1103 | | - |
---|
1104 | | - return ret; |
---|
1105 | | -} |
---|
1106 | | - |
---|
1107 | | -#else |
---|
1108 | | - |
---|
1109 | | -static int npu_devfreq_target(struct device *dev, unsigned long *target_freq, |
---|
1110 | | - u32 flags) |
---|
1111 | | -{ |
---|
1112 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
1113 | | - struct dev_pm_opp *opp = NULL; |
---|
1114 | | - unsigned long freq = *target_freq; |
---|
1115 | | - unsigned long old_freq = rknpu_dev->current_freq; |
---|
1116 | | - unsigned long volt, old_volt = rknpu_dev->current_volt; |
---|
1117 | | - int ret = -EINVAL; |
---|
1118 | | - |
---|
1119 | | -#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE |
---|
1120 | | - rcu_read_lock(); |
---|
1121 | | -#endif |
---|
1122 | | - |
---|
1123 | | - opp = devfreq_recommended_opp(dev, &freq, flags); |
---|
1124 | | - if (IS_ERR(opp)) { |
---|
1125 | | -#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE |
---|
1126 | | - rcu_read_unlock(); |
---|
1127 | | -#endif |
---|
1128 | | - LOG_DEV_ERROR(dev, "failed to get opp (%ld)\n", PTR_ERR(opp)); |
---|
1129 | | - return PTR_ERR(opp); |
---|
1130 | | - } |
---|
1131 | | - volt = dev_pm_opp_get_voltage(opp); |
---|
1132 | | -#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE |
---|
1133 | | - rcu_read_unlock(); |
---|
1134 | | -#endif |
---|
1135 | | - |
---|
1136 | | - /* |
---|
1137 | | - * Only update if there is a change of frequency |
---|
1138 | | - */ |
---|
1139 | | - if (old_freq == freq) { |
---|
1140 | | - *target_freq = freq; |
---|
1141 | | - if (old_volt == volt) |
---|
1142 | | - return 0; |
---|
1143 | | - ret = regulator_set_voltage(rknpu_dev->vdd, volt, INT_MAX); |
---|
1144 | | - if (ret) { |
---|
1145 | | - LOG_DEV_ERROR(dev, "failed to set volt %lu\n", volt); |
---|
1146 | | - return ret; |
---|
1147 | | - } |
---|
1148 | | - rknpu_dev->current_volt = volt; |
---|
1149 | | - return 0; |
---|
1150 | | - } |
---|
1151 | | - |
---|
1152 | | - if (rknpu_dev->vdd && old_volt != volt && old_freq < freq) { |
---|
1153 | | - ret = regulator_set_voltage(rknpu_dev->vdd, volt, INT_MAX); |
---|
1154 | | - if (ret) { |
---|
1155 | | - LOG_DEV_ERROR(dev, "failed to increase volt %lu\n", |
---|
1156 | | - volt); |
---|
1157 | | - return ret; |
---|
1158 | | - } |
---|
1159 | | - } |
---|
1160 | | - LOG_DEV_DEBUG(dev, "%luHz %luuV -> %luHz %luuV\n", old_freq, old_volt, |
---|
1161 | | - freq, volt); |
---|
1162 | | - ret = clk_set_rate(rknpu_dev->clks[0].clk, freq); |
---|
1163 | | - if (ret) { |
---|
1164 | | - LOG_DEV_ERROR(dev, "failed to set clock %lu\n", freq); |
---|
1165 | | - return ret; |
---|
1166 | | - } |
---|
1167 | | - *target_freq = freq; |
---|
1168 | | - rknpu_dev->current_freq = freq; |
---|
1169 | | - |
---|
1170 | | - if (rknpu_dev->devfreq) |
---|
1171 | | - rknpu_dev->devfreq->last_status.current_frequency = freq; |
---|
1172 | | - |
---|
1173 | | - if (rknpu_dev->vdd && old_volt != volt && old_freq > freq) { |
---|
1174 | | - ret = regulator_set_voltage(rknpu_dev->vdd, volt, INT_MAX); |
---|
1175 | | - if (ret) { |
---|
1176 | | - LOG_DEV_ERROR(dev, "failed to decrease volt %lu\n", |
---|
1177 | | - volt); |
---|
1178 | | - return ret; |
---|
1179 | | - } |
---|
1180 | | - } |
---|
1181 | | - rknpu_dev->current_volt = volt; |
---|
1182 | | - |
---|
1183 | | - LOG_DEV_INFO(dev, "set rknpu freq: %lu, volt: %lu\n", |
---|
1184 | | - rknpu_dev->current_freq, rknpu_dev->current_volt); |
---|
1185 | | - |
---|
1186 | | - return ret; |
---|
1187 | | -} |
---|
1188 | | -#endif |
---|
1189 | | - |
---|
1190 | | -static int npu_devfreq_get_dev_status(struct device *dev, |
---|
1191 | | - struct devfreq_dev_status *stat) |
---|
1192 | | -{ |
---|
1193 | | - return 0; |
---|
1194 | | -} |
---|
1195 | | - |
---|
1196 | | -static int npu_devfreq_get_cur_freq(struct device *dev, unsigned long *freq) |
---|
1197 | | -{ |
---|
1198 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
1199 | | - |
---|
1200 | | - *freq = rknpu_dev->current_freq; |
---|
1201 | | - |
---|
1202 | | - return 0; |
---|
1203 | | -} |
---|
1204 | | - |
---|
1205 | | -static struct devfreq_dev_profile npu_devfreq_profile = { |
---|
1206 | | - .polling_ms = 50, |
---|
1207 | | - .target = npu_devfreq_target, |
---|
1208 | | - .get_dev_status = npu_devfreq_get_dev_status, |
---|
1209 | | - .get_cur_freq = npu_devfreq_get_cur_freq, |
---|
1210 | | -}; |
---|
1211 | | -#endif |
---|
1212 | | - |
---|
1213 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
1214 | | -static int devfreq_rknpu_ondemand_func(struct devfreq *df, unsigned long *freq) |
---|
1215 | | -{ |
---|
1216 | | - struct rknpu_device *rknpu_dev = df->data; |
---|
1217 | | - |
---|
1218 | | - if (rknpu_dev) |
---|
1219 | | - *freq = rknpu_dev->ondemand_freq; |
---|
1220 | | - else |
---|
1221 | | - *freq = df->previous_freq; |
---|
1222 | | - |
---|
1223 | | - return 0; |
---|
1224 | | -} |
---|
1225 | | - |
---|
1226 | | -static int devfreq_rknpu_ondemand_handler(struct devfreq *devfreq, |
---|
1227 | | - unsigned int event, void *data) |
---|
1228 | | -{ |
---|
1229 | | - return 0; |
---|
1230 | | -} |
---|
1231 | | - |
---|
1232 | | -static struct devfreq_governor devfreq_rknpu_ondemand = { |
---|
1233 | | - .name = "rknpu_ondemand", |
---|
1234 | | - .get_target_freq = devfreq_rknpu_ondemand_func, |
---|
1235 | | - .event_handler = devfreq_rknpu_ondemand_handler, |
---|
1236 | | -}; |
---|
1237 | | -#endif |
---|
1238 | | - |
---|
1239 | | -#if KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
1240 | | -static unsigned long npu_get_static_power(struct devfreq *devfreq, |
---|
1241 | | - unsigned long voltage) |
---|
1242 | | -{ |
---|
1243 | | - struct device *dev = devfreq->dev.parent; |
---|
1244 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
1245 | | - |
---|
1246 | | - if (!rknpu_dev->model_data) |
---|
1247 | | - return 0; |
---|
1248 | | - |
---|
1249 | | - return rockchip_ipa_get_static_power(rknpu_dev->model_data, voltage); |
---|
1250 | | -} |
---|
1251 | | - |
---|
1252 | | -static struct devfreq_cooling_power npu_cooling_power = { |
---|
1253 | | - .get_static_power = &npu_get_static_power, |
---|
1254 | | -}; |
---|
1255 | | - |
---|
1256 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE |
---|
1257 | | -static int rk3588_npu_get_soc_info(struct device *dev, struct device_node *np, |
---|
1258 | | - int *bin, int *process) |
---|
1259 | | -{ |
---|
1260 | | - int ret = 0; |
---|
1261 | | - u8 value = 0; |
---|
1262 | | - |
---|
1263 | | - if (!bin) |
---|
1264 | | - return 0; |
---|
1265 | | - |
---|
1266 | | - if (of_property_match_string(np, "nvmem-cell-names", |
---|
1267 | | - "specification_serial_number") >= 0) { |
---|
1268 | | - ret = rockchip_nvmem_cell_read_u8( |
---|
1269 | | - np, "specification_serial_number", &value); |
---|
1270 | | - if (ret) { |
---|
1271 | | - LOG_DEV_ERROR( |
---|
1272 | | - dev, |
---|
1273 | | - "Failed to get specification_serial_number\n"); |
---|
1274 | | - return ret; |
---|
1275 | | - } |
---|
1276 | | - /* RK3588M */ |
---|
1277 | | - if (value == 0xd) |
---|
1278 | | - *bin = 1; |
---|
1279 | | - /* RK3588J */ |
---|
1280 | | - else if (value == 0xa) |
---|
1281 | | - *bin = 2; |
---|
1282 | | - } |
---|
1283 | | - if (*bin < 0) |
---|
1284 | | - *bin = 0; |
---|
1285 | | - LOG_DEV_INFO(dev, "bin=%d\n", *bin); |
---|
1286 | | - |
---|
1287 | | - return ret; |
---|
1288 | | -} |
---|
1289 | | - |
---|
1290 | | -static int rk3588_npu_set_soc_info(struct device *dev, struct device_node *np, |
---|
1291 | | - int bin, int process, int volt_sel) |
---|
1292 | | -{ |
---|
1293 | | - struct opp_table *opp_table; |
---|
1294 | | - u32 supported_hw[2]; |
---|
1295 | | - |
---|
1296 | | - if (volt_sel < 0) |
---|
1297 | | - return 0; |
---|
1298 | | - if (bin < 0) |
---|
1299 | | - bin = 0; |
---|
1300 | | - |
---|
1301 | | - if (!of_property_read_bool(np, "rockchip,supported-hw")) |
---|
1302 | | - return 0; |
---|
1303 | | - |
---|
1304 | | - /* SoC Version */ |
---|
1305 | | - supported_hw[0] = BIT(bin); |
---|
1306 | | - /* Speed Grade */ |
---|
1307 | | - supported_hw[1] = BIT(volt_sel); |
---|
1308 | | - opp_table = dev_pm_opp_set_supported_hw(dev, supported_hw, 2); |
---|
1309 | | - if (IS_ERR(opp_table)) { |
---|
1310 | | - LOG_DEV_ERROR(dev, "failed to set supported opp\n"); |
---|
1311 | | - return PTR_ERR(opp_table); |
---|
1312 | | - } |
---|
1313 | | - |
---|
1314 | | - return 0; |
---|
1315 | | -} |
---|
1316 | | - |
---|
1317 | | -static int rk3588_npu_set_read_margin(struct device *dev, |
---|
1318 | | - struct rockchip_opp_info *opp_info, |
---|
1319 | | - u32 rm) |
---|
1320 | | -{ |
---|
1321 | | - u32 offset = 0, val = 0; |
---|
1322 | | - int i, ret = 0; |
---|
1323 | | - |
---|
1324 | | - if (!opp_info->grf || !opp_info->volt_rm_tbl) |
---|
1325 | | - return 0; |
---|
1326 | | - |
---|
1327 | | - if (rm == opp_info->current_rm || rm == UINT_MAX) |
---|
1328 | | - return 0; |
---|
1329 | | - |
---|
1330 | | - LOG_DEV_DEBUG(dev, "set rm to %d\n", rm); |
---|
1331 | | - |
---|
1332 | | - for (i = 0; i < 3; i++) { |
---|
1333 | | - ret = regmap_read(opp_info->grf, offset, &val); |
---|
1334 | | - if (ret < 0) { |
---|
1335 | | - LOG_DEV_ERROR(dev, "failed to get rm from 0x%x\n", |
---|
1336 | | - offset); |
---|
1337 | | - return ret; |
---|
1338 | | - } |
---|
1339 | | - val &= ~0x1c; |
---|
1340 | | - regmap_write(opp_info->grf, offset, val | (rm << 2)); |
---|
1341 | | - offset += 4; |
---|
1342 | | - } |
---|
1343 | | - opp_info->current_rm = rm; |
---|
1344 | | - |
---|
1345 | | - return 0; |
---|
1346 | | -} |
---|
1347 | | - |
---|
1348 | | -static const struct rockchip_opp_data rk3588_npu_opp_data = { |
---|
1349 | | - .get_soc_info = rk3588_npu_get_soc_info, |
---|
1350 | | - .set_soc_info = rk3588_npu_set_soc_info, |
---|
1351 | | - .set_read_margin = rk3588_npu_set_read_margin, |
---|
1352 | | -}; |
---|
1353 | | - |
---|
1354 | | -static const struct of_device_id rockchip_npu_of_match[] = { |
---|
1355 | | - { |
---|
1356 | | - .compatible = "rockchip,rk3588", |
---|
1357 | | - .data = (void *)&rk3588_npu_opp_data, |
---|
1358 | | - }, |
---|
1359 | | - {}, |
---|
1360 | | -}; |
---|
1361 | | - |
---|
1362 | | -static int rknpu_devfreq_init(struct rknpu_device *rknpu_dev) |
---|
1363 | | -{ |
---|
1364 | | - struct device *dev = rknpu_dev->dev; |
---|
1365 | | - struct devfreq_dev_profile *dp = &npu_devfreq_profile; |
---|
1366 | | - struct dev_pm_opp *opp; |
---|
1367 | | - struct opp_table *reg_table = NULL; |
---|
1368 | | - struct opp_table *opp_table = NULL; |
---|
1369 | | - const char *const reg_names[] = { "rknpu", "mem" }; |
---|
1370 | | - int ret = -EINVAL; |
---|
1371 | | - |
---|
1372 | | - if (of_find_property(dev->of_node, "rknpu-supply", NULL) && |
---|
1373 | | - of_find_property(dev->of_node, "mem-supply", NULL)) { |
---|
1374 | | - reg_table = dev_pm_opp_set_regulators(dev, reg_names, 2); |
---|
1375 | | - if (IS_ERR(reg_table)) |
---|
1376 | | - return PTR_ERR(reg_table); |
---|
1377 | | - opp_table = |
---|
1378 | | - dev_pm_opp_register_set_opp_helper(dev, npu_opp_helper); |
---|
1379 | | - if (IS_ERR(opp_table)) { |
---|
1380 | | - dev_pm_opp_put_regulators(reg_table); |
---|
1381 | | - return PTR_ERR(opp_table); |
---|
1382 | | - } |
---|
1383 | | - } else { |
---|
1384 | | - reg_table = dev_pm_opp_set_regulators(dev, reg_names, 1); |
---|
1385 | | - if (IS_ERR(reg_table)) |
---|
1386 | | - return PTR_ERR(reg_table); |
---|
1387 | | - } |
---|
1388 | | - |
---|
1389 | | - rockchip_get_opp_data(rockchip_npu_of_match, &rknpu_dev->opp_info); |
---|
1390 | | - ret = rockchip_init_opp_table(dev, &rknpu_dev->opp_info, "npu_leakage", |
---|
1391 | | - "rknpu"); |
---|
1392 | | - if (ret) { |
---|
1393 | | - LOG_DEV_ERROR(dev, "failed to init_opp_table\n"); |
---|
1394 | | - return ret; |
---|
1395 | | - } |
---|
1396 | | - |
---|
1397 | | - rknpu_dev->current_freq = clk_get_rate(rknpu_dev->clks[0].clk); |
---|
1398 | | - |
---|
1399 | | - opp = devfreq_recommended_opp(dev, &rknpu_dev->current_freq, 0); |
---|
1400 | | - if (IS_ERR(opp)) { |
---|
1401 | | - ret = PTR_ERR(opp); |
---|
1402 | | - goto err_remove_table; |
---|
1403 | | - } |
---|
1404 | | - dev_pm_opp_put(opp); |
---|
1405 | | - dp->initial_freq = rknpu_dev->current_freq; |
---|
1406 | | - |
---|
1407 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
1408 | | - ret = devfreq_add_governor(&devfreq_rknpu_ondemand); |
---|
1409 | | - if (ret) { |
---|
1410 | | - LOG_DEV_ERROR(dev, "failed to add rknpu_ondemand governor\n"); |
---|
1411 | | - goto err_remove_table; |
---|
1412 | | - } |
---|
1413 | | -#endif |
---|
1414 | | - |
---|
1415 | | - rknpu_dev->devfreq = devm_devfreq_add_device(dev, dp, "rknpu_ondemand", |
---|
1416 | | - (void *)rknpu_dev); |
---|
1417 | | - if (IS_ERR(rknpu_dev->devfreq)) { |
---|
1418 | | - LOG_DEV_ERROR(dev, "failed to add devfreq\n"); |
---|
1419 | | - ret = PTR_ERR(rknpu_dev->devfreq); |
---|
1420 | | - goto err_remove_governor; |
---|
1421 | | - } |
---|
1422 | | - devm_devfreq_register_opp_notifier(dev, rknpu_dev->devfreq); |
---|
1423 | | - |
---|
1424 | | - rknpu_dev->devfreq->last_status.current_frequency = dp->initial_freq; |
---|
1425 | | - rknpu_dev->devfreq->last_status.total_time = 1; |
---|
1426 | | - rknpu_dev->devfreq->last_status.busy_time = 1; |
---|
1427 | | - |
---|
1428 | | - npu_mdevp.data = rknpu_dev->devfreq; |
---|
1429 | | - npu_mdevp.opp_info = &rknpu_dev->opp_info; |
---|
1430 | | - rknpu_dev->mdev_info = |
---|
1431 | | - rockchip_system_monitor_register(dev, &npu_mdevp); |
---|
1432 | | - if (IS_ERR(rknpu_dev->mdev_info)) { |
---|
1433 | | - LOG_DEV_DEBUG(dev, "without system monitor\n"); |
---|
1434 | | - rknpu_dev->mdev_info = NULL; |
---|
1435 | | - npu_mdevp.is_checked = true; |
---|
1436 | | - } |
---|
1437 | | - rknpu_dev->current_freq = clk_get_rate(rknpu_dev->clks[0].clk); |
---|
1438 | | - rknpu_dev->current_volt = regulator_get_voltage(rknpu_dev->vdd); |
---|
1439 | | - |
---|
1440 | | - of_property_read_u32(dev->of_node, "dynamic-power-coefficient", |
---|
1441 | | - (u32 *)&npu_cooling_power.dyn_power_coeff); |
---|
1442 | | - rknpu_dev->model_data = |
---|
1443 | | - rockchip_ipa_power_model_init(dev, "npu_leakage"); |
---|
1444 | | - if (IS_ERR_OR_NULL(rknpu_dev->model_data)) { |
---|
1445 | | - rknpu_dev->model_data = NULL; |
---|
1446 | | - LOG_DEV_ERROR(dev, "failed to initialize power model\n"); |
---|
1447 | | - } else if (rknpu_dev->model_data->dynamic_coefficient) { |
---|
1448 | | - npu_cooling_power.dyn_power_coeff = |
---|
1449 | | - rknpu_dev->model_data->dynamic_coefficient; |
---|
1450 | | - } |
---|
1451 | | - if (!npu_cooling_power.dyn_power_coeff) { |
---|
1452 | | - LOG_DEV_ERROR(dev, "failed to get dynamic-coefficient\n"); |
---|
1453 | | - goto out; |
---|
1454 | | - } |
---|
1455 | | - |
---|
1456 | | - rknpu_dev->devfreq_cooling = of_devfreq_cooling_register_power( |
---|
1457 | | - dev->of_node, rknpu_dev->devfreq, &npu_cooling_power); |
---|
1458 | | - if (IS_ERR_OR_NULL(rknpu_dev->devfreq_cooling)) |
---|
1459 | | - LOG_DEV_ERROR(dev, "failed to register cooling device\n"); |
---|
1460 | | - |
---|
1461 | | -out: |
---|
1462 | | - return 0; |
---|
1463 | | - |
---|
1464 | | -err_remove_governor: |
---|
1465 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
1466 | | - devfreq_remove_governor(&devfreq_rknpu_ondemand); |
---|
1467 | | -#endif |
---|
1468 | | -err_remove_table: |
---|
1469 | | - dev_pm_opp_of_remove_table(dev); |
---|
1470 | | - |
---|
1471 | | - rknpu_dev->devfreq = NULL; |
---|
1472 | | - |
---|
1473 | | - return ret; |
---|
1474 | | -} |
---|
1475 | | - |
---|
1476 | | -#else |
---|
1477 | | - |
---|
1478 | | -static int npu_devfreq_adjust_current_freq_volt(struct device *dev, |
---|
1479 | | - struct rknpu_device *rknpu_dev) |
---|
1480 | | -{ |
---|
1481 | | - unsigned long volt, old_freq, freq; |
---|
1482 | | - struct dev_pm_opp *opp = NULL; |
---|
1483 | | - int ret = -EINVAL; |
---|
1484 | | - |
---|
1485 | | - old_freq = clk_get_rate(rknpu_dev->clks[0].clk); |
---|
1486 | | - freq = old_freq; |
---|
1487 | | - |
---|
1488 | | -#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE |
---|
1489 | | - rcu_read_lock(); |
---|
1490 | | -#endif |
---|
1491 | | - |
---|
1492 | | - opp = devfreq_recommended_opp(dev, &freq, 0); |
---|
1493 | | - volt = dev_pm_opp_get_voltage(opp); |
---|
1494 | | - |
---|
1495 | | -#if KERNEL_VERSION(4, 11, 0) > LINUX_VERSION_CODE |
---|
1496 | | - rcu_read_unlock(); |
---|
1497 | | -#endif |
---|
1498 | | - |
---|
1499 | | - if (freq >= old_freq && rknpu_dev->vdd) { |
---|
1500 | | - ret = regulator_set_voltage(rknpu_dev->vdd, volt, INT_MAX); |
---|
1501 | | - if (ret) { |
---|
1502 | | - LOG_DEV_ERROR(dev, "failed to set volt %lu\n", volt); |
---|
1503 | | - return ret; |
---|
1504 | | - } |
---|
1505 | | - } |
---|
1506 | | - LOG_DEV_DEBUG(dev, "adjust current freq=%luHz, volt=%luuV\n", freq, |
---|
1507 | | - volt); |
---|
1508 | | - ret = clk_set_rate(rknpu_dev->clks[0].clk, freq); |
---|
1509 | | - if (ret) { |
---|
1510 | | - LOG_DEV_ERROR(dev, "failed to set clock %lu\n", freq); |
---|
1511 | | - return ret; |
---|
1512 | | - } |
---|
1513 | | - if (freq < old_freq && rknpu_dev->vdd) { |
---|
1514 | | - ret = regulator_set_voltage(rknpu_dev->vdd, volt, INT_MAX); |
---|
1515 | | - if (ret) { |
---|
1516 | | - LOG_DEV_ERROR(dev, "failed to set volt %lu\n", volt); |
---|
1517 | | - return ret; |
---|
1518 | | - } |
---|
1519 | | - } |
---|
1520 | | - rknpu_dev->current_freq = freq; |
---|
1521 | | - rknpu_dev->current_volt = volt; |
---|
1522 | | - |
---|
1523 | | - return 0; |
---|
1524 | | -} |
---|
1525 | | - |
---|
1526 | | -static int rknpu_devfreq_init(struct rknpu_device *rknpu_dev) |
---|
1527 | | -{ |
---|
1528 | | - struct device *dev = rknpu_dev->dev; |
---|
1529 | | - struct devfreq_dev_profile *dp = &npu_devfreq_profile; |
---|
1530 | | - int ret = -EINVAL; |
---|
1531 | | - |
---|
1532 | | - ret = rockchip_init_opp_table(dev, NULL, "npu_leakage", "rknpu"); |
---|
1533 | | - if (ret) { |
---|
1534 | | - LOG_DEV_ERROR(dev, "failed to init_opp_table\n"); |
---|
1535 | | - return ret; |
---|
1536 | | - } |
---|
1537 | | - |
---|
1538 | | - ret = npu_devfreq_adjust_current_freq_volt(dev, rknpu_dev); |
---|
1539 | | - if (ret) { |
---|
1540 | | - LOG_DEV_ERROR(dev, "failed to adjust current freq volt\n"); |
---|
1541 | | - goto err_remove_table; |
---|
1542 | | - } |
---|
1543 | | - dp->initial_freq = rknpu_dev->current_freq; |
---|
1544 | | - |
---|
1545 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
1546 | | - ret = devfreq_add_governor(&devfreq_rknpu_ondemand); |
---|
1547 | | - if (ret) { |
---|
1548 | | - LOG_DEV_ERROR(dev, "failed to add rknpu_ondemand governor\n"); |
---|
1549 | | - goto err_remove_table; |
---|
1550 | | - } |
---|
1551 | | -#endif |
---|
1552 | | - |
---|
1553 | | - rknpu_dev->devfreq = devm_devfreq_add_device(dev, dp, "rknpu_ondemand", |
---|
1554 | | - (void *)rknpu_dev); |
---|
1555 | | - if (IS_ERR(rknpu_dev->devfreq)) { |
---|
1556 | | - LOG_DEV_ERROR(dev, "failed to add devfreq\n"); |
---|
1557 | | - ret = PTR_ERR(rknpu_dev->devfreq); |
---|
1558 | | - goto err_remove_governor; |
---|
1559 | | - } |
---|
1560 | | - devm_devfreq_register_opp_notifier(dev, rknpu_dev->devfreq); |
---|
1561 | | - |
---|
1562 | | - rknpu_dev->devfreq->last_status.current_frequency = dp->initial_freq; |
---|
1563 | | - rknpu_dev->devfreq->last_status.total_time = 1; |
---|
1564 | | - rknpu_dev->devfreq->last_status.busy_time = 1; |
---|
1565 | | - |
---|
1566 | | - npu_mdevp.data = rknpu_dev->devfreq; |
---|
1567 | | - rknpu_dev->mdev_info = |
---|
1568 | | - rockchip_system_monitor_register(dev, &npu_mdevp); |
---|
1569 | | - if (IS_ERR(rknpu_dev->mdev_info)) { |
---|
1570 | | - LOG_DEV_DEBUG(dev, "without system monitor\n"); |
---|
1571 | | - rknpu_dev->mdev_info = NULL; |
---|
1572 | | - } |
---|
1573 | | - rknpu_dev->current_freq = clk_get_rate(rknpu_dev->clks[0].clk); |
---|
1574 | | - rknpu_dev->current_volt = regulator_get_voltage(rknpu_dev->vdd); |
---|
1575 | | - |
---|
1576 | | - of_property_read_u32(dev->of_node, "dynamic-power-coefficient", |
---|
1577 | | - (u32 *)&npu_cooling_power.dyn_power_coeff); |
---|
1578 | | - rknpu_dev->model_data = |
---|
1579 | | - rockchip_ipa_power_model_init(dev, "npu_leakage"); |
---|
1580 | | - if (IS_ERR_OR_NULL(rknpu_dev->model_data)) { |
---|
1581 | | - rknpu_dev->model_data = NULL; |
---|
1582 | | - LOG_DEV_ERROR(dev, "failed to initialize power model\n"); |
---|
1583 | | - } else if (rknpu_dev->model_data->dynamic_coefficient) { |
---|
1584 | | - npu_cooling_power.dyn_power_coeff = |
---|
1585 | | - rknpu_dev->model_data->dynamic_coefficient; |
---|
1586 | | - } |
---|
1587 | | - |
---|
1588 | | - if (!npu_cooling_power.dyn_power_coeff) { |
---|
1589 | | - LOG_DEV_ERROR(dev, "failed to get dynamic-coefficient\n"); |
---|
1590 | | - goto out; |
---|
1591 | | - } |
---|
1592 | | - |
---|
1593 | | - rknpu_dev->devfreq_cooling = of_devfreq_cooling_register_power( |
---|
1594 | | - dev->of_node, rknpu_dev->devfreq, &npu_cooling_power); |
---|
1595 | | - if (IS_ERR_OR_NULL(rknpu_dev->devfreq_cooling)) |
---|
1596 | | - LOG_DEV_ERROR(dev, "failed to register cooling device\n"); |
---|
1597 | | - |
---|
1598 | | -out: |
---|
1599 | | - return 0; |
---|
1600 | | - |
---|
1601 | | -err_remove_governor: |
---|
1602 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
1603 | | - devfreq_remove_governor(&devfreq_rknpu_ondemand); |
---|
1604 | | -#endif |
---|
1605 | | -err_remove_table: |
---|
1606 | | - dev_pm_opp_of_remove_table(dev); |
---|
1607 | | - |
---|
1608 | | - rknpu_dev->devfreq = NULL; |
---|
1609 | | - |
---|
1610 | | - return ret; |
---|
1611 | | -} |
---|
1612 | | -#endif |
---|
1613 | | -#endif |
---|
1614 | | - |
---|
1615 | | -static int rknpu_devfreq_remove(struct rknpu_device *rknpu_dev) |
---|
1616 | | -{ |
---|
1617 | | - if (rknpu_dev->devfreq) { |
---|
1618 | | - devfreq_unregister_opp_notifier(rknpu_dev->dev, |
---|
1619 | | - rknpu_dev->devfreq); |
---|
1620 | | - dev_pm_opp_of_remove_table(rknpu_dev->dev); |
---|
1621 | | -#ifdef CONFIG_PM_DEVFREQ |
---|
1622 | | - devfreq_remove_governor(&devfreq_rknpu_ondemand); |
---|
1623 | | -#endif |
---|
1624 | | - } |
---|
1625 | | - |
---|
1626 | | - return 0; |
---|
1627 | | -} |
---|
1628 | | - |
---|
1629 | | -#endif |
---|
1630 | | - |
---|
1631 | 950 | static int rknpu_register_irq(struct platform_device *pdev, |
---|
1632 | 951 | struct rknpu_device *rknpu_dev) |
---|
1633 | 952 | { |
---|
1634 | 953 | const struct rknpu_config *config = rknpu_dev->config; |
---|
1635 | 954 | struct device *dev = &pdev->dev; |
---|
1636 | | -#if KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
1637 | 955 | struct resource *res; |
---|
1638 | | -#endif |
---|
1639 | 956 | int i, ret, irq; |
---|
1640 | 957 | |
---|
1641 | | -#if KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
1642 | 958 | res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, |
---|
1643 | 959 | config->irqs[0].name); |
---|
1644 | 960 | if (res) { |
---|
.. | .. |
---|
1677 | 993 | return ret; |
---|
1678 | 994 | } |
---|
1679 | 995 | } |
---|
1680 | | -#else |
---|
1681 | | - /* there are irq names in dts */ |
---|
1682 | | - for (i = 0; i < config->num_irqs; i++) { |
---|
1683 | | - irq = platform_get_irq_byname(pdev, config->irqs[i].name); |
---|
1684 | | - if (irq < 0) { |
---|
1685 | | - LOG_DEV_ERROR(dev, "no npu %s in dts\n", |
---|
1686 | | - config->irqs[i].name); |
---|
1687 | | - return irq; |
---|
1688 | | - } |
---|
1689 | | - |
---|
1690 | | - ret = devm_request_irq(dev, irq, config->irqs[i].irq_hdl, |
---|
1691 | | - IRQF_SHARED, dev_name(dev), rknpu_dev); |
---|
1692 | | - if (ret < 0) { |
---|
1693 | | - LOG_DEV_ERROR(dev, "request %s failed: %d\n", |
---|
1694 | | - config->irqs[i].name, ret); |
---|
1695 | | - return ret; |
---|
1696 | | - } |
---|
1697 | | - } |
---|
1698 | | -#endif |
---|
1699 | 996 | |
---|
1700 | 997 | return 0; |
---|
1701 | 998 | } |
---|
.. | .. |
---|
1775 | 1072 | return 0; |
---|
1776 | 1073 | } |
---|
1777 | 1074 | |
---|
| 1075 | +static int rknpu_get_invalid_core_mask(struct device *dev) |
---|
| 1076 | +{ |
---|
| 1077 | + int ret = 0; |
---|
| 1078 | + u8 invalid_core_mask = 0; |
---|
| 1079 | + |
---|
| 1080 | + if (of_property_match_string(dev->of_node, "nvmem-cell-names", |
---|
| 1081 | + "cores") >= 0) { |
---|
| 1082 | + ret = rockchip_nvmem_cell_read_u8(dev->of_node, "cores", |
---|
| 1083 | + &invalid_core_mask); |
---|
| 1084 | + /* The default valid npu cores for RK3583 are core0 and core1 */ |
---|
| 1085 | + invalid_core_mask |= RKNPU_CORE2_MASK; |
---|
| 1086 | + if (ret) { |
---|
| 1087 | + LOG_DEV_ERROR( |
---|
| 1088 | + dev, |
---|
| 1089 | + "failed to get specification_serial_number\n"); |
---|
| 1090 | + return invalid_core_mask; |
---|
| 1091 | + } |
---|
| 1092 | + } |
---|
| 1093 | + |
---|
| 1094 | + return (int)invalid_core_mask; |
---|
| 1095 | +} |
---|
| 1096 | + |
---|
1778 | 1097 | static int rknpu_probe(struct platform_device *pdev) |
---|
1779 | 1098 | { |
---|
1780 | 1099 | struct resource *res = NULL; |
---|
.. | .. |
---|
1806 | 1125 | if (!config) |
---|
1807 | 1126 | return -EINVAL; |
---|
1808 | 1127 | |
---|
| 1128 | + if (match->data == (void *)&rk3588_rknpu_config) { |
---|
| 1129 | + int invalid_core_mask = rknpu_get_invalid_core_mask(dev); |
---|
| 1130 | + /* The default valid npu cores for RK3583 are core0 and core1 */ |
---|
| 1131 | + if (invalid_core_mask & RKNPU_CORE2_MASK) { |
---|
| 1132 | + if ((invalid_core_mask & RKNPU_CORE0_MASK) || |
---|
| 1133 | + (invalid_core_mask & RKNPU_CORE1_MASK)) { |
---|
| 1134 | + LOG_DEV_ERROR( |
---|
| 1135 | + dev, |
---|
| 1136 | + "rknpu core invalid, invalid core mask: %#x\n", |
---|
| 1137 | + invalid_core_mask); |
---|
| 1138 | + return -ENODEV; |
---|
| 1139 | + } |
---|
| 1140 | + config = &rk3583_rknpu_config; |
---|
| 1141 | + } |
---|
| 1142 | + } |
---|
| 1143 | + |
---|
1809 | 1144 | rknpu_dev->config = config; |
---|
1810 | 1145 | rknpu_dev->dev = dev; |
---|
1811 | 1146 | |
---|
.. | .. |
---|
1834 | 1169 | } |
---|
1835 | 1170 | |
---|
1836 | 1171 | #ifndef FPGA_PLATFORM |
---|
1837 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
1838 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
1839 | | - if (strstr(__clk_get_name(rknpu_dev->clks[0].clk), "scmi")) |
---|
1840 | | - rknpu_dev->opp_info.scmi_clk = rknpu_dev->clks[0].clk; |
---|
1841 | | -#endif |
---|
1842 | | - |
---|
1843 | 1172 | rknpu_dev->vdd = devm_regulator_get_optional(dev, "rknpu"); |
---|
1844 | 1173 | if (IS_ERR(rknpu_dev->vdd)) { |
---|
1845 | 1174 | if (PTR_ERR(rknpu_dev->vdd) != -ENODEV) { |
---|
.. | .. |
---|
1966 | 1295 | virt_dev = dev_pm_domain_attach_by_name(dev, "npu1"); |
---|
1967 | 1296 | if (!IS_ERR(virt_dev)) |
---|
1968 | 1297 | rknpu_dev->genpd_dev_npu1 = virt_dev; |
---|
1969 | | - virt_dev = dev_pm_domain_attach_by_name(dev, "npu2"); |
---|
1970 | | - if (!IS_ERR(virt_dev)) |
---|
1971 | | - rknpu_dev->genpd_dev_npu2 = virt_dev; |
---|
| 1298 | + if (config->num_irqs > 2) { |
---|
| 1299 | + virt_dev = dev_pm_domain_attach_by_name(dev, "npu2"); |
---|
| 1300 | + if (!IS_ERR(virt_dev)) |
---|
| 1301 | + rknpu_dev->genpd_dev_npu2 = virt_dev; |
---|
| 1302 | + } |
---|
1972 | 1303 | rknpu_dev->multiple_domains = true; |
---|
1973 | 1304 | } |
---|
1974 | 1305 | |
---|
.. | .. |
---|
1977 | 1308 | goto err_remove_drv; |
---|
1978 | 1309 | |
---|
1979 | 1310 | #ifndef FPGA_PLATFORM |
---|
1980 | | -#if KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
1981 | 1311 | rknpu_devfreq_init(rknpu_dev); |
---|
1982 | | -#endif |
---|
1983 | 1312 | #endif |
---|
1984 | 1313 | |
---|
1985 | 1314 | // set default power put delay to 3s |
---|
.. | .. |
---|
2088 | 1417 | } |
---|
2089 | 1418 | |
---|
2090 | 1419 | #ifndef FPGA_PLATFORM |
---|
2091 | | -#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \ |
---|
2092 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
2093 | 1420 | static int rknpu_runtime_suspend(struct device *dev) |
---|
2094 | 1421 | { |
---|
2095 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
2096 | | - struct rockchip_opp_info *opp_info = &rknpu_dev->opp_info; |
---|
2097 | | - |
---|
2098 | | - if (opp_info->scmi_clk) { |
---|
2099 | | - if (clk_set_rate(opp_info->scmi_clk, POWER_DOWN_FREQ)) |
---|
2100 | | - LOG_DEV_ERROR(dev, "failed to restore clk rate\n"); |
---|
2101 | | - } |
---|
2102 | | - opp_info->current_rm = UINT_MAX; |
---|
2103 | | - |
---|
2104 | | - return 0; |
---|
| 1422 | + return rknpu_devfreq_runtime_suspend(dev); |
---|
2105 | 1423 | } |
---|
2106 | 1424 | |
---|
2107 | 1425 | static int rknpu_runtime_resume(struct device *dev) |
---|
2108 | 1426 | { |
---|
2109 | | - struct rknpu_device *rknpu_dev = dev_get_drvdata(dev); |
---|
2110 | | - struct rockchip_opp_info *opp_info = &rknpu_dev->opp_info; |
---|
2111 | | - int ret = 0; |
---|
2112 | | - |
---|
2113 | | - if (!rknpu_dev->current_freq || !rknpu_dev->current_volt) |
---|
2114 | | - return 0; |
---|
2115 | | - |
---|
2116 | | - ret = clk_bulk_prepare_enable(opp_info->num_clks, opp_info->clks); |
---|
2117 | | - if (ret) { |
---|
2118 | | - LOG_DEV_ERROR(dev, "failed to enable opp clks\n"); |
---|
2119 | | - return ret; |
---|
2120 | | - } |
---|
2121 | | - |
---|
2122 | | - if (opp_info->data && opp_info->data->set_read_margin) |
---|
2123 | | - opp_info->data->set_read_margin(dev, opp_info, |
---|
2124 | | - opp_info->target_rm); |
---|
2125 | | - if (opp_info->scmi_clk) { |
---|
2126 | | - if (clk_set_rate(opp_info->scmi_clk, rknpu_dev->current_freq)) |
---|
2127 | | - LOG_DEV_ERROR(dev, "failed to set power down rate\n"); |
---|
2128 | | - } |
---|
2129 | | - |
---|
2130 | | - clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks); |
---|
2131 | | - |
---|
2132 | | - return ret; |
---|
| 1427 | + return rknpu_devfreq_runtime_resume(dev); |
---|
2133 | 1428 | } |
---|
2134 | 1429 | |
---|
2135 | 1430 | static const struct dev_pm_ops rknpu_pm_ops = { |
---|
.. | .. |
---|
2139 | 1434 | NULL) |
---|
2140 | 1435 | }; |
---|
2141 | 1436 | #endif |
---|
2142 | | -#endif |
---|
2143 | 1437 | |
---|
2144 | 1438 | static struct platform_driver rknpu_driver = { |
---|
2145 | 1439 | .probe = rknpu_probe, |
---|
.. | .. |
---|
2148 | 1442 | .owner = THIS_MODULE, |
---|
2149 | 1443 | .name = "RKNPU", |
---|
2150 | 1444 | #ifndef FPGA_PLATFORM |
---|
2151 | | -#if KERNEL_VERSION(5, 5, 0) < LINUX_VERSION_CODE && \ |
---|
2152 | | - KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE |
---|
2153 | 1445 | .pm = &rknpu_pm_ops, |
---|
2154 | | -#endif |
---|
2155 | 1446 | #endif |
---|
2156 | 1447 | .of_match_table = of_match_ptr(rknpu_of_match), |
---|
2157 | 1448 | }, |
---|