diff --git a/hw/occ-sensor.c b/hw/occ-sensor.c index da846bd92a8f..e9ac5df4b3e9 100644 --- a/hw/occ-sensor.c +++ b/hw/occ-sensor.c @@ -116,7 +116,7 @@ struct occ_sensor_data_header *get_sensor_header_block(int occ_num) static inline struct occ_sensor_name *get_names_block(struct occ_sensor_data_header *hb) { - return ((struct occ_sensor_name *)((u64)hb + hb->names_offset)); + return ((struct occ_sensor_name *)((u64)hb + be32_to_cpu(hb->names_offset))); } static inline u32 sensor_handler(int occ_num, int sensor_id, int attr) @@ -131,11 +131,11 @@ static inline u32 sensor_handler(int occ_num, int sensor_id, int attr) */ static void scale_sensor(struct occ_sensor_name *md, u64 *sensor) { - u32 factor = md->scale_factor; + u32 factor = be32_to_cpu(md->scale_factor); int i; s8 exp; - if (md->type == OCC_SENSOR_TYPE_CURRENT) + if (be16_to_cpu(md->type) == OCC_SENSOR_TYPE_CURRENT) *sensor *= 1000; //convert to mA *sensor *= factor >> 8; @@ -152,7 +152,7 @@ static void scale_sensor(struct occ_sensor_name *md, u64 *sensor) static void scale_energy(struct occ_sensor_name *md, u64 *sensor) { - u32 factor = md->freq; + u32 factor = be32_to_cpu(md->freq); int i; s8 exp; @@ -174,17 +174,17 @@ static u64 read_sensor(struct occ_sensor_record *sensor, int attr) { switch (attr) { case SENSOR_SAMPLE: - return sensor->sample; + return be16_to_cpu(sensor->sample); case SENSOR_SAMPLE_MIN: - return sensor->sample_min; + return be16_to_cpu(sensor->sample_min); case SENSOR_SAMPLE_MAX: - return sensor->sample_max; + return be16_to_cpu(sensor->sample_max); case SENSOR_CSM_MIN: - return sensor->csm_min; + return be16_to_cpu(sensor->csm_min); case SENSOR_CSM_MAX: - return sensor->csm_max; + return be16_to_cpu(sensor->csm_max); case SENSOR_ACCUMULATOR: - return sensor->accumulator; + return be64_to_cpu(sensor->accumulator); default: break; } @@ -197,14 +197,16 @@ static void *select_sensor_buffer(struct occ_sensor_data_header *hb, int id) struct occ_sensor_name *md; u8 *ping, *pong; void *buffer = NULL; + u32 reading_offset; if (!hb) return NULL; md = get_names_block(hb); - ping = (u8 *)((u64)hb + hb->reading_ping_offset); - pong = (u8 *)((u64)hb + hb->reading_pong_offset); + ping = (u8 *)((u64)hb + be32_to_cpu(hb->reading_ping_offset)); + pong = (u8 *)((u64)hb + be32_to_cpu(hb->reading_pong_offset)); + reading_offset = be32_to_cpu(md[id].reading_offset); /* Check which buffer is valid and read the data from that. * Ping Pong Action @@ -216,11 +218,11 @@ static void *select_sensor_buffer(struct occ_sensor_data_header *hb, int id) if (*ping && *pong) { u64 tping, tpong; - u64 ping_buf = (u64)ping + md[id].reading_offset; - u64 pong_buf = (u64)pong + md[id].reading_offset; + u64 ping_buf = (u64)ping + reading_offset; + u64 pong_buf = (u64)pong + reading_offset; - tping = ((struct occ_sensor_record *)ping_buf)->timestamp; - tpong = ((struct occ_sensor_record *)pong_buf)->timestamp; + tping = be64_to_cpu(((struct occ_sensor_record *)ping_buf)->timestamp); + tpong = be64_to_cpu(((struct occ_sensor_record *)pong_buf)->timestamp); if (tping > tpong) buffer = ping; @@ -236,7 +238,7 @@ static void *select_sensor_buffer(struct occ_sensor_data_header *hb, int id) } assert(buffer); - buffer = (void *)((u64)buffer + md[id].reading_offset); + buffer = (void *)((u64)buffer + reading_offset); return buffer; } @@ -265,7 +267,7 @@ int occ_sensor_read(u32 handle, __be64 *data) if (hb->valid != 1) return OPAL_HARDWARE; - if (id > hb->nr_sensors) + if (id > be16_to_cpu(hb->nr_sensors)) return OPAL_PARAMETER; buff = select_sensor_buffer(hb, id); @@ -277,7 +279,7 @@ int occ_sensor_read(u32 handle, __be64 *data) return OPAL_SUCCESS; md = get_names_block(hb); - if (md[id].type == OCC_SENSOR_TYPE_POWER && attr == SENSOR_ACCUMULATOR) + if (be16_to_cpu(md[id].type) == OCC_SENSOR_TYPE_POWER && attr == SENSOR_ACCUMULATOR) scale_energy(&md[id], &d); else scale_sensor(&md[id], &d); @@ -323,7 +325,8 @@ static bool occ_sensor_sanity(struct occ_sensor_data_header *hb, int chipid) return false; } - if (!hb->names_offset || !hb->reading_ping_offset || + if (!hb->names_offset || + !hb->reading_ping_offset || !hb->reading_pong_offset) { prerror("OCC: Chip %d Invalid sensor buffer pointers\n", chipid); @@ -360,9 +363,10 @@ static void add_sensor_label(struct dt_node *node, struct occ_sensor_name *md, { char sname[30] = ""; char prefix[30] = ""; + uint16_t location = be16_to_cpu(md->location); int i; - if (md->location != OCC_SENSOR_LOC_SYSTEM) + if (location != OCC_SENSOR_LOC_SYSTEM) snprintf(prefix, sizeof(prefix), "%s %d ", "Chip", chipid); for (i = 0; i < ARRAY_SIZE(str_maps); i++) @@ -371,7 +375,7 @@ static void add_sensor_label(struct dt_node *node, struct occ_sensor_name *md, char *end; int num = -1; - if (md->location != OCC_SENSOR_LOC_CORE) + if (location != OCC_SENSOR_LOC_CORE) num = parse_entity(md->name, &end); if (num != -1) { @@ -387,7 +391,7 @@ static void add_sensor_label(struct dt_node *node, struct occ_sensor_name *md, } /* Fallback to OCC literal if mapping is not found */ - if (md->location == OCC_SENSOR_LOC_SYSTEM) { + if (location == OCC_SENSOR_LOC_SYSTEM) { dt_add_property_string(node, "label", md->name); } else { snprintf(sname, sizeof(sname), "%s%s", prefix, md->name); @@ -447,15 +451,15 @@ static bool check_sensor_sample(struct occ_sensor_data_header *hb, u32 offset) { struct occ_sensor_record *ping, *pong; - ping = (struct occ_sensor_record *)((u64)hb + hb->reading_ping_offset - + offset); - pong = (struct occ_sensor_record *)((u64)hb + hb->reading_pong_offset - + offset); + ping = (struct occ_sensor_record *)((u64)hb + + be32_to_cpu(hb->reading_ping_offset) + offset); + pong = (struct occ_sensor_record *)((u64)hb + + be32_to_cpu(hb->reading_pong_offset) + offset); return ping->sample || pong->sample; } static void add_sensor_node(const char *loc, const char *type, int i, int attr, - struct occ_sensor_name *md, u32 *phandle, u32 *ptype, + struct occ_sensor_name *md, __be32 *phandle, u32 *ptype, u32 pir, u32 occ_num, u32 chipid) { char name[30]; @@ -471,10 +475,10 @@ static void add_sensor_node(const char *loc, const char *type, int i, int attr, dt_add_property_string(node, "occ_label", md->name); add_sensor_label(node, md, chipid); - if (md->location == OCC_SENSOR_LOC_CORE) + if (be16_to_cpu(md->location) == OCC_SENSOR_LOC_CORE) dt_add_property_cells(node, "ibm,pir", pir); - *ptype = md->type; + *ptype = be16_to_cpu(md->type); if (attr == SENSOR_SAMPLE) { handler = sensor_handler(occ_num, i, SENSOR_CSM_MAX); @@ -485,7 +489,7 @@ static void add_sensor_node(const char *loc, const char *type, int i, int attr, } dt_add_property_string(node, "compatible", "ibm,opal-sensor"); - *phandle = node->phandle; + *phandle = cpu_to_be32(node->phandle); } bool occ_sensors_init(void) @@ -523,7 +527,9 @@ bool occ_sensors_init(void) for_each_chip(chip) { struct occ_sensor_data_header *hb; struct occ_sensor_name *md; - u32 *phandles, *ptype, phcount = 0; + __be32 *phandles; + u32 *ptype, phcount = 0; + unsigned int nr_sensors; hb = get_sensor_header_block(occ_num); md = get_names_block(hb); @@ -532,30 +538,34 @@ bool occ_sensors_init(void) if (!occ_sensor_sanity(hb, chip->id)) continue; - phandles = malloc(hb->nr_sensors * sizeof(u32)); + nr_sensors = be16_to_cpu(hb->nr_sensors); + + phandles = malloc(nr_sensors * sizeof(__be32)); assert(phandles); - ptype = malloc(hb->nr_sensors * sizeof(u32)); + ptype = malloc(nr_sensors * sizeof(u32)); assert(ptype); - for (i = 0; i < hb->nr_sensors; i++) { - const char *type, *loc; + for (i = 0; i < nr_sensors; i++) { + const char *type_name, *loc; struct cpu_thread *c = NULL; uint32_t pir = 0; + uint16_t type = be16_to_cpu(md[i].type); + uint16_t location = be16_to_cpu(md[i].location); if (md[i].structure_type != OCC_SENSOR_READING_FULL) continue; - if (!(md[i].type & HWMON_SENSORS_MASK)) + if (!(type & HWMON_SENSORS_MASK)) continue; - if (md[i].location == OCC_SENSOR_LOC_GPU && !has_gpu) + if (location == OCC_SENSOR_LOC_GPU && !has_gpu) continue; - if (md[i].type == OCC_SENSOR_TYPE_POWER && - !check_sensor_sample(hb, md[i].reading_offset)) + if (type == OCC_SENSOR_TYPE_POWER && + !check_sensor_sample(hb, be32_to_cpu(md[i].reading_offset))) continue; - if (md[i].location == OCC_SENSOR_LOC_CORE) { + if (location == OCC_SENSOR_LOC_CORE) { int num = parse_entity(md[i].name, NULL); for_each_available_core_in_chip(c, chip->id) @@ -566,16 +576,16 @@ bool occ_sensors_init(void) pir = c->pir; } - type = get_sensor_type_string(md[i].type); - loc = get_sensor_loc_string(md[i].location); + type_name = get_sensor_type_string(type); + loc = get_sensor_loc_string(location); - add_sensor_node(loc, type, i, SENSOR_SAMPLE, &md[i], + add_sensor_node(loc, type_name, i, SENSOR_SAMPLE, &md[i], &phandles[phcount], &ptype[phcount], pir, occ_num, chip->id); phcount++; /* Add energy sensors */ - if (md[i].type == OCC_SENSOR_TYPE_POWER && + if (type == OCC_SENSOR_TYPE_POWER && md[i].structure_type == OCC_SENSOR_READING_FULL) { add_sensor_node(loc, "energy", i, SENSOR_ACCUMULATOR, &md[i], diff --git a/hw/occ.c b/hw/occ.c index 46210ba2091b..3542e44a3a41 100644 --- a/hw/occ.c +++ b/hw/occ.c @@ -97,7 +97,7 @@ struct occ_pstate_table { u8 flags; u8 vdd; u8 vcs; - u32 freq_khz; + __be32 freq_khz; } pstates[MAX_PSTATES]; s8 core_max[MAX_P8_CORES]; u8 pad[100]; @@ -115,7 +115,7 @@ struct occ_pstate_table { u8 id; u8 flags; u16 reserved; - u32 freq_khz; + __be32 freq_khz; } pstates[MAX_PSTATES]; u8 core_max[MAX_P9_CORES]; u8 pad[56]; @@ -375,7 +375,7 @@ static bool wait_for_all_occ_init(void) chip->occ_functional = true; prlog(PR_DEBUG, "OCC: Chip %02x Data (%016llx) = %016llx\n", - chip->id, (uint64_t)occ_data, *(uint64_t *)occ_data); + chip->id, (uint64_t)occ_data, be64_to_cpu(*(__be64 *)occ_data)); } end_time = mftb(); prlog(PR_NOTICE, "OCC: All Chip Rdy after %lu ms\n", @@ -398,8 +398,8 @@ static bool wait_for_all_occ_init(void) * the list and break from the loop as this is the last valid * element in the pstate table. */ -static void parse_pstates_v2(struct occ_pstate_table *data, u32 *dt_id, - u32 *dt_freq, int nr_pstates, int pmax, int pmin) +static void parse_pstates_v2(struct occ_pstate_table *data, __be32 *dt_id, + __be32 *dt_freq, int nr_pstates, int pmax, int pmin) { int i, j; @@ -407,8 +407,8 @@ static void parse_pstates_v2(struct occ_pstate_table *data, u32 *dt_id, if (cmp_pstates(data->v2.pstates[i].id, pmax) > 0) continue; - dt_id[j] = data->v2.pstates[i].id; - dt_freq[j] = data->v2.pstates[i].freq_khz / 1000; + dt_id[j] = cpu_to_be32(data->v2.pstates[i].id); + dt_freq[j] = cpu_to_be32(be32_to_cpu(data->v2.pstates[i].freq_khz) / 1000); j++; if (data->v2.pstates[i].id == pmin) @@ -420,8 +420,8 @@ static void parse_pstates_v2(struct occ_pstate_table *data, u32 *dt_id, nr_pstates, j); } -static void parse_pstates_v9(struct occ_pstate_table *data, u32 *dt_id, - u32 *dt_freq, int nr_pstates, int pmax, int pmin) +static void parse_pstates_v9(struct occ_pstate_table *data, __be32 *dt_id, + __be32 *dt_freq, int nr_pstates, int pmax, int pmin) { int i, j; @@ -429,8 +429,8 @@ static void parse_pstates_v9(struct occ_pstate_table *data, u32 *dt_id, if (cmp_pstates(data->v9.pstates[i].id, pmax) > 0) continue; - dt_id[j] = data->v9.pstates[i].id; - dt_freq[j] = data->v9.pstates[i].freq_khz / 1000; + dt_id[j] = cpu_to_be32(data->v9.pstates[i].id); + dt_freq[j] = cpu_to_be32(be32_to_cpu(data->v9.pstates[i].freq_khz) / 1000); j++; if (data->v9.pstates[i].id == pmin) @@ -482,7 +482,7 @@ static bool add_cpu_pstate_properties(struct dt_node *power_mgt, uint64_t occ_data_area; struct occ_pstate_table *occ_data; /* Arrays for device tree */ - u32 *dt_id, *dt_freq; + __be32 *dt_id, *dt_freq; int pmax, pmin, pnom; u8 nr_pstates; bool ultra_turbo_supported; @@ -500,8 +500,8 @@ static bool add_cpu_pstate_properties(struct dt_node *power_mgt, occ_data_area = (uint64_t)occ_data; prlog(PR_DEBUG, "OCC: Data (%16llx) = %16llx %16llx\n", occ_data_area, - *(uint64_t *)occ_data_area, - *(uint64_t *)(occ_data_area + 8)); + be64_to_cpu(*(__be64 *)occ_data_area), + be64_to_cpu(*(__be64 *)(occ_data_area + 8))); if (!occ_data->valid) { /** @@ -629,9 +629,9 @@ static bool add_cpu_pstate_properties(struct dt_node *power_mgt, return false; } - dt_id = malloc(nr_pstates * sizeof(u32)); + dt_id = malloc(nr_pstates * sizeof(__be32)); assert(dt_id); - dt_freq = malloc(nr_pstates * sizeof(u32)); + dt_freq = malloc(nr_pstates * sizeof(__be32)); assert(dt_freq); switch (major) { @@ -649,9 +649,9 @@ static bool add_cpu_pstate_properties(struct dt_node *power_mgt, /* Add the device-tree entries */ dt_add_property(power_mgt, "ibm,pstate-ids", dt_id, - nr_pstates * sizeof(u32)); + nr_pstates * sizeof(__be32)); dt_add_property(power_mgt, "ibm,pstate-frequencies-mhz", dt_freq, - nr_pstates * sizeof(u32)); + nr_pstates * sizeof(__be32)); dt_add_property_cells(power_mgt, "ibm,pstate-min", pmin); dt_add_property_cells(power_mgt, "ibm,pstate-nominal", pnom); dt_add_property_cells(power_mgt, "ibm,pstate-max", pmax); @@ -667,7 +667,7 @@ static bool add_cpu_pstate_properties(struct dt_node *power_mgt, if (ultra_turbo_supported) { int pturbo, pultra_turbo; u8 nr_cores = get_available_nr_cores_in_chip(chip->id); - u32 *dt_cmax; + __be32 *dt_cmax; dt_cmax = malloc(nr_cores * sizeof(u32)); assert(dt_cmax); @@ -676,13 +676,13 @@ static bool add_cpu_pstate_properties(struct dt_node *power_mgt, pturbo = occ_data->v2.pstate_turbo; pultra_turbo = occ_data->v2.pstate_ultra_turbo; for (i = 0; i < nr_cores; i++) - dt_cmax[i] = occ_data->v2.core_max[i]; + dt_cmax[i] = cpu_to_be32(occ_data->v2.core_max[i]); break; case 0x9: pturbo = occ_data->v9.pstate_turbo; pultra_turbo = occ_data->v9.pstate_ultra_turbo; for (i = 0; i < nr_cores; i++) - dt_cmax[i] = occ_data->v9.core_max[i]; + dt_cmax[i] = cpu_to_be32(occ_data->v9.core_max[i]); break; default: return false; @@ -1602,7 +1602,7 @@ int occ_sensor_group_enable(u32 group_hndl, int token, bool enable) return opal_occ_command(&chips[i], token, &sensor_mask_data); } -void occ_add_sensor_groups(struct dt_node *sg, u32 *phandles, u32 *ptype, +void occ_add_sensor_groups(struct dt_node *sg, __be32 *phandles, u32 *ptype, int nr_phandles, int chipid) { struct group_info { @@ -1689,7 +1689,7 @@ void occ_add_sensor_groups(struct dt_node *sg, u32 *phandles, u32 *ptype, dt_add_property_cells(node, "ibm,chip-id", chipid); dt_add_property_cells(node, "reg", handle); if (groups[j].ops == OPAL_SENSOR_GROUP_ENABLE) { - u32 *_phandles; + __be32 *_phandles; int k, pcount = 0; _phandles = malloc(sizeof(u32) * nr_phandles); diff --git a/include/occ.h b/include/occ.h index ab45e9e46dd2..967c3fd18971 100644 --- a/include/occ.h +++ b/include/occ.h @@ -34,7 +34,7 @@ bool occ_get_gpu_presence(struct proc_chip *chip, int gpu_num); extern bool occ_sensors_init(void); extern int occ_sensor_read(u32 handle, __be64 *data); extern int occ_sensor_group_clear(u32 group_hndl, int token); -extern void occ_add_sensor_groups(struct dt_node *sg, u32 *phandles, +extern void occ_add_sensor_groups(struct dt_node *sg, __be32 *phandles, u32 *ptype, int nr_phandles, int chipid); extern int occ_sensor_group_enable(u32 group_hndl, int token, bool enable); @@ -186,15 +186,15 @@ enum sensor_struct_type { struct occ_sensor_data_header { u8 valid; u8 version; - u16 nr_sensors; + __be16 nr_sensors; u8 reading_version; u8 pad[3]; - u32 names_offset; + __be32 names_offset; u8 names_version; u8 name_length; u16 reserved; - u32 reading_ping_offset; - u32 reading_pong_offset; + __be32 reading_ping_offset; + __be32 reading_pong_offset; } __attribute__((__packed__)); /** @@ -220,13 +220,13 @@ struct occ_sensor_data_header { struct occ_sensor_name { char name[MAX_CHARS_SENSOR_NAME]; char units[MAX_CHARS_SENSOR_UNIT]; - u16 gsid; - u32 freq; - u32 scale_factor; - u16 type; - u16 location; + __be16 gsid; + __be32 freq; + __be32 scale_factor; + __be16 type; + __be16 location; u8 structure_type; - u32 reading_offset; + __be32 reading_offset; u8 sensor_data; u8 pad[8]; } __attribute__((__packed__)); @@ -258,18 +258,18 @@ struct occ_sensor_name { */ struct occ_sensor_record { u16 gsid; - u64 timestamp; - u16 sample; - u16 sample_min; - u16 sample_max; - u16 csm_min; - u16 csm_max; - u16 profiler_min; - u16 profiler_max; - u16 job_scheduler_min; - u16 job_scheduler_max; - u64 accumulator; - u32 update_tag; + __be64 timestamp; + __be16 sample; + __be16 sample_min; + __be16 sample_max; + __be16 csm_min; + __be16 csm_max; + __be16 profiler_min; + __be16 profiler_max; + __be16 job_scheduler_min; + __be16 job_scheduler_max; + __be64 accumulator; + __be32 update_tag; u8 pad[8]; } __attribute__((__packed__)); @@ -284,8 +284,8 @@ struct occ_sensor_record { */ struct occ_sensor_counter { u16 gsid; - u64 timestamp; - u64 accumulator; + __be64 timestamp; + __be64 accumulator; u8 sample; u8 pad[5]; } __attribute__((__packed__));