1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright IBM Corp 2019 3 4 #include <linux/bitops.h> 5 #include <linux/device.h> 6 #include <linux/export.h> 7 #include <linux/hwmon-sysfs.h> 8 #include <linux/kernel.h> 9 #include <linux/sysfs.h> 10 11 #include "common.h" 12 13 /* OCC status register */ 14 #define OCC_STAT_MASTER BIT(7) 15 #define OCC_STAT_ACTIVE BIT(0) 16 17 /* OCC extended status register */ 18 #define OCC_EXT_STAT_DVFS_OT BIT(7) 19 #define OCC_EXT_STAT_DVFS_POWER BIT(6) 20 #define OCC_EXT_STAT_MEM_THROTTLE BIT(5) 21 #define OCC_EXT_STAT_QUICK_DROP BIT(4) 22 #define OCC_EXT_STAT_DVFS_VDD BIT(3) 23 #define OCC_EXT_STAT_GPU_THROTTLE GENMASK(2, 0) 24 25 static ssize_t occ_sysfs_show(struct device *dev, 26 struct device_attribute *attr, char *buf) 27 { 28 int rc; 29 int val = 0; 30 struct occ *occ = dev_get_drvdata(dev); 31 struct occ_poll_response_header *header; 32 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 33 34 rc = occ_update_response(occ); 35 if (rc) 36 return rc; 37 38 header = (struct occ_poll_response_header *)occ->resp.data; 39 40 switch (sattr->index) { 41 case 0: 42 val = !!(header->status & OCC_STAT_MASTER); 43 break; 44 case 1: 45 val = !!(header->status & OCC_STAT_ACTIVE); 46 break; 47 case 2: 48 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_OT); 49 break; 50 case 3: 51 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_POWER); 52 break; 53 case 4: 54 val = !!(header->ext_status & OCC_EXT_STAT_MEM_THROTTLE); 55 break; 56 case 5: 57 val = !!(header->ext_status & OCC_EXT_STAT_QUICK_DROP); 58 break; 59 case 6: 60 val = header->occ_state; 61 break; 62 case 7: 63 if (header->status & OCC_STAT_MASTER) 64 val = hweight8(header->occs_present); 65 else 66 val = 1; 67 break; 68 case 8: 69 val = header->ips_status; 70 break; 71 case 9: 72 val = header->mode; 73 break; 74 case 10: 75 val = !!(header->ext_status & OCC_EXT_STAT_DVFS_VDD); 76 break; 77 case 11: 78 val = header->ext_status & OCC_EXT_STAT_GPU_THROTTLE; 79 break; 80 default: 81 return -EINVAL; 82 } 83 84 return sysfs_emit(buf, "%d\n", val); 85 } 86 87 static ssize_t occ_error_show(struct device *dev, 88 struct device_attribute *attr, char *buf) 89 { 90 struct occ *occ = dev_get_drvdata(dev); 91 92 occ_update_response(occ); 93 94 return sysfs_emit(buf, "%d\n", occ->error); 95 } 96 97 static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0); 98 static SENSOR_DEVICE_ATTR(occ_active, 0444, occ_sysfs_show, NULL, 1); 99 static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2); 100 static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3); 101 static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4); 102 static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5); 103 static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6); 104 static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7); 105 static SENSOR_DEVICE_ATTR(occ_ips_status, 0444, occ_sysfs_show, NULL, 8); 106 static SENSOR_DEVICE_ATTR(occ_mode, 0444, occ_sysfs_show, NULL, 9); 107 static SENSOR_DEVICE_ATTR(occ_dvfs_vdd, 0444, occ_sysfs_show, NULL, 10); 108 static SENSOR_DEVICE_ATTR(occ_gpu_throttle, 0444, occ_sysfs_show, NULL, 11); 109 static DEVICE_ATTR_RO(occ_error); 110 111 static struct attribute *occ_attributes[] = { 112 &sensor_dev_attr_occ_master.dev_attr.attr, 113 &sensor_dev_attr_occ_active.dev_attr.attr, 114 &sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr, 115 &sensor_dev_attr_occ_dvfs_power.dev_attr.attr, 116 &sensor_dev_attr_occ_mem_throttle.dev_attr.attr, 117 &sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr, 118 &sensor_dev_attr_occ_state.dev_attr.attr, 119 &sensor_dev_attr_occs_present.dev_attr.attr, 120 &sensor_dev_attr_occ_ips_status.dev_attr.attr, 121 &sensor_dev_attr_occ_mode.dev_attr.attr, 122 &sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr, 123 &sensor_dev_attr_occ_gpu_throttle.dev_attr.attr, 124 &dev_attr_occ_error.attr, 125 NULL 126 }; 127 128 static const struct attribute_group occ_sysfs = { 129 .attrs = occ_attributes, 130 }; 131 132 void occ_sysfs_poll_done(struct occ *occ) 133 { 134 const char *name; 135 struct occ_poll_response_header *header = 136 (struct occ_poll_response_header *)occ->resp.data; 137 138 /* 139 * On the first poll response, we haven't yet created the sysfs 140 * attributes, so don't make any notify calls. 141 */ 142 if (!occ->hwmon) 143 goto done; 144 145 if ((header->status & OCC_STAT_MASTER) != 146 (occ->prev_stat & OCC_STAT_MASTER)) { 147 name = sensor_dev_attr_occ_master.dev_attr.attr.name; 148 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 149 } 150 151 if ((header->status & OCC_STAT_ACTIVE) != 152 (occ->prev_stat & OCC_STAT_ACTIVE)) { 153 name = sensor_dev_attr_occ_active.dev_attr.attr.name; 154 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 155 } 156 157 if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) != 158 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) { 159 name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name; 160 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 161 } 162 163 if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) != 164 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) { 165 name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name; 166 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 167 } 168 169 if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) != 170 (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) { 171 name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name; 172 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 173 } 174 175 if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) != 176 (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) { 177 name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name; 178 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 179 } 180 181 if ((header->ext_status & OCC_EXT_STAT_DVFS_VDD) != 182 (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_VDD)) { 183 name = sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr.name; 184 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 185 } 186 187 if ((header->ext_status & OCC_EXT_STAT_GPU_THROTTLE) != 188 (occ->prev_ext_stat & OCC_EXT_STAT_GPU_THROTTLE)) { 189 name = sensor_dev_attr_occ_gpu_throttle.dev_attr.attr.name; 190 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 191 } 192 193 if ((header->status & OCC_STAT_MASTER) && 194 header->occs_present != occ->prev_occs_present) { 195 name = sensor_dev_attr_occs_present.dev_attr.attr.name; 196 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 197 } 198 199 if (header->ips_status != occ->prev_ips_status) { 200 name = sensor_dev_attr_occ_ips_status.dev_attr.attr.name; 201 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 202 } 203 204 if (header->mode != occ->prev_mode) { 205 name = sensor_dev_attr_occ_mode.dev_attr.attr.name; 206 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 207 } 208 209 if (occ->error && occ->error != occ->prev_error) { 210 name = dev_attr_occ_error.attr.name; 211 sysfs_notify(&occ->bus_dev->kobj, NULL, name); 212 } 213 214 /* no notifications for OCC state; doesn't indicate error condition */ 215 216 done: 217 occ->prev_error = occ->error; 218 occ->prev_stat = header->status; 219 occ->prev_ext_stat = header->ext_status; 220 occ->prev_occs_present = header->occs_present; 221 occ->prev_ips_status = header->ips_status; 222 occ->prev_mode = header->mode; 223 } 224 225 int occ_setup_sysfs(struct occ *occ) 226 { 227 return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs); 228 } 229 230 void occ_shutdown(struct occ *occ) 231 { 232 sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs); 233 } 234 EXPORT_SYMBOL_GPL(occ_shutdown); 235