summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDawid Niedzwiecki <dn@semihalf.com>2021-11-25 11:49:04 +0100
committerCommit Bot <commit-bot@chromium.org>2021-12-07 08:50:14 +0000
commitbb4c47af02a21be2cc9205adc8bae83862de780a (patch)
tree723db42d9b84715efba744c2920ad74a2c52439a
parente3dc95984501e49a9718fb207a17a30ec7945ef4 (diff)
downloadchrome-ec-bb4c47af02a21be2cc9205adc8bae83862de780a.tar.gz
usb: use atomic_t where possible
There are many places where atomic_t can be used and passed to atomic_* function without casting, so change that. Since atomic is a signed type, do not modify the type timer_active, timer_disabled and dpm_dp_flag, because mixing types in the shift operations create differences in asm code. It shouldn't change the generated code. The change will be useful for incoming commits related to modifying atomic_t caused by a change in Zephyr upstream (atomic_t from int to long). BUG=b:207082842 TEST=make buildall && zmake testall BRANCH=main Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: Ia1e5447c8c47ca3deccbe5c250ac4d45ceb5b133 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3302208 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Dawid Niedzwiecki <dawidn@google.com>
-rw-r--r--baseboard/asurada/usb_pd_policy.c2
-rw-r--r--common/usb_common.c6
-rw-r--r--common/usb_pd_host_cmd.c2
-rw-r--r--common/usb_pd_protocol.c4
-rw-r--r--common/usbc/dp_alt_mode.c8
-rw-r--r--common/usbc/usb_pd_dpm.c8
-rw-r--r--common/usbc/usb_pd_timer.c35
-rw-r--r--common/usbc/usb_pe_drp_sm.c8
-rw-r--r--common/usbc/usb_prl_sm.c10
-rw-r--r--common/usbc/usb_tc_drp_acc_trysrc_sm.c10
-rw-r--r--common/usbc_ocp.c2
-rw-r--r--include/usb_pd_timer.h28
-rw-r--r--zephyr/projects/corsola/src/usb_pd_policy.c2
13 files changed, 67 insertions, 58 deletions
diff --git a/baseboard/asurada/usb_pd_policy.c b/baseboard/asurada/usb_pd_policy.c
index f9ba7e5a4d..6a7621f450 100644
--- a/baseboard/asurada/usb_pd_policy.c
+++ b/baseboard/asurada/usb_pd_policy.c
@@ -155,7 +155,7 @@ __override void svdm_exit_dp_mode(int port)
int pd_snk_is_vbus_provided(int port)
{
- static int vbus_prev[CONFIG_USB_PD_PORT_MAX_COUNT];
+ static atomic_t vbus_prev[CONFIG_USB_PD_PORT_MAX_COUNT];
int vbus;
if ((IS_ENABLED(BOARD_HAYATO) && board_get_version() < 4) ||
diff --git a/common/usb_common.c b/common/usb_common.c
index d5f42edca2..207b7d4de3 100644
--- a/common/usb_common.c
+++ b/common/usb_common.c
@@ -132,7 +132,7 @@ int remote_flashing(int argc, char **argv)
struct ec_params_usb_pd_rw_hash_entry rw_hash_table[RW_HASH_ENTRIES];
#endif /* CONFIG_COMMON_RUNTIME */
-static __maybe_unused uint32_t pd_host_event_status __aligned(4);
+static __maybe_unused atomic_t pd_host_event_status __aligned(4);
bool pd_firmware_upgrade_check_power_readiness(int port)
{
@@ -578,7 +578,7 @@ static void pd_send_hard_reset(int port)
#ifdef CONFIG_USBC_OCP
-static uint32_t port_oc_reset_req;
+static atomic_t port_oc_reset_req;
static void re_enable_ports(void)
{
@@ -852,7 +852,7 @@ void pd_set_vbus_discharge(int port, int enable)
#endif /* CONFIG_USB_PD_DISCHARGE */
#ifdef CONFIG_USB_PD_TCPM_TCPCI
-static uint32_t pd_ports_to_resume;
+static atomic_t pd_ports_to_resume;
static void resume_pd_port(void)
{
uint32_t port;
diff --git a/common/usb_pd_host_cmd.c b/common/usb_pd_host_cmd.c
index 47ec36ad5f..3cc3584e92 100644
--- a/common/usb_pd_host_cmd.c
+++ b/common/usb_pd_host_cmd.c
@@ -548,7 +548,7 @@ DECLARE_HOST_COMMAND(EC_CMD_PD_CONTROL, pd_control, EC_VER_MASK(0));
* Note: this variable must be aligned on 4-byte boundary because we pass the
* address to atomic_ functions which use assembly to access them.
*/
-static uint32_t pd_host_event_status __aligned(4);
+static atomic_t pd_host_event_status __aligned(4);
static enum ec_status
hc_pd_host_event_status(struct host_cmd_handler_args *args)
diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c
index abf75e8004..94eb1a3aa7 100644
--- a/common/usb_pd_protocol.c
+++ b/common/usb_pd_protocol.c
@@ -243,9 +243,9 @@ static struct pd_protocol {
/* Time to debounce exit low power mode */
uint64_t low_power_exit_time;
/* Tasks to notify after TCPC has been reset */
- int tasks_waiting_on_reset;
+ atomic_t tasks_waiting_on_reset;
/* Tasks preventing TCPC from entering low power mode */
- int tasks_preventing_lpm;
+ atomic_t tasks_preventing_lpm;
#endif
#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
diff --git a/common/usbc/dp_alt_mode.c b/common/usbc/dp_alt_mode.c
index 2a532466ac..1ebc5d3d04 100644
--- a/common/usbc/dp_alt_mode.c
+++ b/common/usbc/dp_alt_mode.c
@@ -58,10 +58,12 @@ static const uint8_t state_vdm_cmd[DP_STATE_COUNT] = {
*/
#define DP_FLAG_RETRY BIT(0)
-static uint32_t dpm_dp_flags[CONFIG_USB_PD_PORT_MAX_COUNT];
+static atomic_t dpm_dp_flags[CONFIG_USB_PD_PORT_MAX_COUNT];
-#define DP_SET_FLAG(port, flag) atomic_or(&dpm_dp_flags[port], (flag))
-#define DP_CLR_FLAG(port, flag) atomic_clear_bits(&dpm_dp_flags[port], (flag))
+#define DP_SET_FLAG(port, flag) \
+ atomic_or(&dpm_dp_flags[port], (flag))
+#define DP_CLR_FLAG(port, flag) \
+ atomic_clear_bits(&dpm_dp_flags[port], (flag))
#define DP_CHK_FLAG(port, flag) (dpm_dp_flags[port] & (flag))
bool dp_is_active(int port)
diff --git a/common/usbc/usb_pd_dpm.c b/common/usbc/usb_pd_dpm.c
index 9587de4973..4290d96e9b 100644
--- a/common/usbc/usb_pd_dpm.c
+++ b/common/usbc/usb_pd_dpm.c
@@ -37,7 +37,7 @@
#define DPM_ATTENION_MAX_VDO 2
static struct {
- uint32_t flags;
+ atomic_t flags;
uint32_t vdm_attention[DPM_ATTENION_MAX_VDO];
int vdm_cnt;
mutex_t vdm_attention_mutex;
@@ -503,11 +503,11 @@ static uint32_t max_current_claimed;
K_MUTEX_DEFINE(max_current_claimed_lock);
/* Ports with PD sink needing > 1.5 A */
-static uint32_t sink_max_pdo_requested;
+static atomic_t sink_max_pdo_requested;
/* Ports with FRS source needing > 1.5 A */
-static uint32_t source_frs_max_requested;
+static atomic_t source_frs_max_requested;
/* Ports with non-PD sinks, so current requirements are unknown */
-static uint32_t non_pd_sink_max_requested;
+static atomic_t non_pd_sink_max_requested;
#define LOWEST_PORT(p) __builtin_ctz(p) /* Undefined behavior if p == 0 */
diff --git a/common/usbc/usb_pd_timer.c b/common/usbc/usb_pd_timer.c
index 8cbfbef6ca..4d284024be 100644
--- a/common/usbc/usb_pd_timer.c
+++ b/common/usbc/usb_pd_timer.c
@@ -21,30 +21,35 @@
#define NO_TIMEOUT (-1)
#define EXPIRE_NOW (0)
-#define PD_SET_ACTIVE(p, m) pd_timer_atomic_op( \
- atomic_or, \
- timer_active[p], \
+#define PD_SET_ACTIVE(p, m) pd_timer_atomic_op( \
+ atomic_or, \
+ (atomic_t *)timer_active[p], \
(m))
-#define PD_CLR_ACTIVE(p, m) pd_timer_atomic_op( \
- atomic_clear_bits, \
- timer_active[p], \
+#define PD_CLR_ACTIVE(p, m) pd_timer_atomic_op( \
+ atomic_clear_bits, \
+ (atomic_t *)timer_active[p], \
(m))
-#define PD_CHK_ACTIVE(p, m) ((timer_active[p][0] & ((m) >> 32)) | \
+#define PD_CHK_ACTIVE(p, m) ((timer_active[p][0] & ((m) >> 32)) | \
(timer_active[p][1] & (m)))
-#define PD_SET_DISABLED(p, m) pd_timer_atomic_op( \
- atomic_or, \
- timer_disabled[p], \
+#define PD_SET_DISABLED(p, m) pd_timer_atomic_op( \
+ atomic_or, \
+ (atomic_t *)timer_disabled[p], \
(m))
-#define PD_CLR_DISABLED(p, m) pd_timer_atomic_op( \
- atomic_clear_bits, \
- timer_disabled[p], \
+#define PD_CLR_DISABLED(p, m) pd_timer_atomic_op( \
+ atomic_clear_bits, \
+ (atomic_t *)timer_disabled[p], \
(m))
#define PD_CHK_DISABLED(p, m) ((timer_disabled[p][0] & ((m) >> 32)) | \
(timer_disabled[p][1] & (m)))
#define TIMER_FIELD_NUM_UINT32S 2
+/*
+ * Use uint32_t for timer_active and timer_disabled instead of atomic_t,
+ * because mixing types signed and unsigned around shifting may lead to
+ * undefined behavior.
+ */
test_mockable_static
uint32_t timer_active[MAX_PD_PORTS][TIMER_FIELD_NUM_UINT32S];
test_mockable_static
@@ -119,9 +124,9 @@ __maybe_unused static __const_data const char * const pd_timer_names[] = {
*/
test_mockable_static void pd_timer_atomic_op(
atomic_val_t (*op)(atomic_t*, atomic_val_t),
- uint32_t *const timer_field, const uint64_t mask_val)
+ atomic_t *const timer_field, const uint64_t mask_val)
{
- uint32_t *atomic_timer_field;
+ atomic_t *atomic_timer_field;
union mask64_t {
struct {
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
diff --git a/common/usbc/usb_pe_drp_sm.c b/common/usbc/usb_pe_drp_sm.c
index bbe499b612..60a2255215 100644
--- a/common/usbc/usb_pe_drp_sm.c
+++ b/common/usbc/usb_pe_drp_sm.c
@@ -577,9 +577,9 @@ static struct policy_engine {
/* current port data role (DFP or UFP) */
enum pd_data_role data_role;
/* state machine flags */
- uint32_t flags;
+ atomic_t flags;
/* Device Policy Manager Request */
- uint32_t dpm_request;
+ atomic_t dpm_request;
uint32_t dpm_curr_request;
/* last requested voltage PDO index */
int requested_idx;
@@ -588,7 +588,7 @@ static struct policy_engine {
* Port events - PD_STATUS_EVENT_* values
* Set from PD task but may be cleared by host command
*/
- uint32_t events;
+ atomic_t events;
/* port address where soft resets are sent */
enum tcpci_msg_type soft_reset_sop;
@@ -7283,7 +7283,7 @@ uint8_t pd_get_src_cap_cnt(int port)
}
/* Track access to the PD discovery structures during HC execution */
-uint32_t task_access[CONFIG_USB_PD_PORT_MAX_COUNT][DISCOVERY_TYPE_COUNT];
+atomic_t task_access[CONFIG_USB_PD_PORT_MAX_COUNT][DISCOVERY_TYPE_COUNT];
void pd_dfp_discovery_init(int port)
{
diff --git a/common/usbc/usb_prl_sm.c b/common/usbc/usb_prl_sm.c
index b85a7ee553..d9c3849f58 100644
--- a/common/usbc/usb_prl_sm.c
+++ b/common/usbc/usb_prl_sm.c
@@ -294,7 +294,7 @@ static struct rx_chunked {
/* state machine context */
struct sm_ctx ctx;
/* PRL_FLAGS */
- uint32_t flags;
+ atomic_t flags;
/* error to report when moving to rch_report_error state */
enum pe_error error;
} rch[CONFIG_USB_PD_PORT_MAX_COUNT];
@@ -304,7 +304,7 @@ static struct tx_chunked {
/* state machine context */
struct sm_ctx ctx;
/* state machine flags */
- uint32_t flags;
+ atomic_t flags;
/* error to report when moving to tch_report_error state */
enum pe_error error;
} tch[CONFIG_USB_PD_PORT_MAX_COUNT];
@@ -322,7 +322,7 @@ static struct protocol_layer_tx {
/* state machine context */
struct sm_ctx ctx;
/* state machine flags */
- uint32_t flags;
+ atomic_t flags;
/* last message type we transmitted */
enum tcpci_msg_type last_xmit_type;
/* message id counters for all 6 port partners */
@@ -336,13 +336,13 @@ static struct protocol_hard_reset {
/* state machine context */
struct sm_ctx ctx;
/* state machine flags */
- uint32_t flags;
+ atomic_t flags;
} prl_hr[CONFIG_USB_PD_PORT_MAX_COUNT];
/* Chunking Message Object */
static struct pd_message {
/* message status flags */
- uint32_t flags;
+ atomic_t flags;
/* SOP* */
enum tcpci_msg_type xmit_type;
/* type of message */
diff --git a/common/usbc/usb_tc_drp_acc_trysrc_sm.c b/common/usbc/usb_tc_drp_acc_trysrc_sm.c
index 3fa9528699..9e03875eb0 100644
--- a/common/usbc/usb_tc_drp_acc_trysrc_sm.c
+++ b/common/usbc/usb_tc_drp_acc_trysrc_sm.c
@@ -401,7 +401,7 @@ static struct type_c {
* else they're disabled if bits PD_DISABLED_NO_CONNECTION or
* PD_DISABLED_BY_POLICY are set.
*/
- uint32_t pd_disabled_mask;
+ atomic_t pd_disabled_mask;
/*
* Timer for handling TOGGLE_OFF/FORCE_SINK mode when auto-toggle
* enabled. See drp_auto_toggle_next_state() for details.
@@ -414,13 +414,13 @@ static struct type_c {
/* Port polarity */
enum tcpc_cc_polarity polarity;
/* port flags, see TC_FLAGS_* */
- uint32_t flags;
+ atomic_t flags;
/* The cc state */
enum pd_cc_states cc_state;
/* Tasks to notify after TCPC has been reset */
- int tasks_waiting_on_reset;
+ atomic_t tasks_waiting_on_reset;
/* Tasks preventing TCPC from entering low power mode */
- int tasks_preventing_lpm;
+ atomic_t tasks_preventing_lpm;
/* Voltage on CC pin */
enum tcpc_cc_voltage_status cc_voltage;
/* Type-C current */
@@ -457,7 +457,7 @@ static void set_state_tc(const int port, const enum usb_tc_state new_state);
test_export_static enum usb_tc_state get_state_tc(const int port);
/* Enable variable for Try.SRC states */
-static uint32_t pd_try_src;
+static atomic_t pd_try_src;
static volatile enum try_src_override_t pd_try_src_override;
static void pd_update_try_source(void);
diff --git a/common/usbc_ocp.c b/common/usbc_ocp.c
index e20cf9f1f8..2654993576 100644
--- a/common/usbc_ocp.c
+++ b/common/usbc_ocp.c
@@ -42,7 +42,7 @@
static uint8_t oc_event_cnt_tbl[CONFIG_USB_PD_PORT_MAX_COUNT];
/* A flag for ports with sink device connected. */
-static uint32_t snk_connected_ports;
+static atomic_t snk_connected_ports;
static void clear_oc_tbl(void)
{
diff --git a/include/usb_pd_timer.h b/include/usb_pd_timer.h
index 8e5984779d..1ed268f14d 100644
--- a/include/usb_pd_timer.h
+++ b/include/usb_pd_timer.h
@@ -10,6 +10,8 @@
#include <stdbool.h>
+#include "atomic.h"
+
/*
* List of all timers that will be managed by usb_pd_timer
*/
@@ -343,28 +345,28 @@ extern uint32_t timer_disabled[MAX_PD_PORTS][TIMER_FIELD_NUM_UINT32S];
/* exported: do not call directly, only for the defined macros */
extern void pd_timer_atomic_op(
atomic_val_t (*op)(atomic_t*, atomic_val_t),
- uint32_t *const timer_field, const uint64_t mask);
+ atomic_t *const timer_field, const uint64_t mask);
/* exported: set/clear/check the current timer_active for a timer */
-#define PD_SET_ACTIVE(p, m) pd_timer_atomic_op( \
- atomic_or, \
- timer_active[p], \
+#define PD_SET_ACTIVE(p, m) pd_timer_atomic_op( \
+ atomic_or, \
+ (atomic_t *)timer_active[p], \
(m))
-#define PD_CLR_ACTIVE(p, m) pd_timer_atomic_op( \
- atomic_clear_bits, \
- timer_active[p], \
+#define PD_CLR_ACTIVE(p, m) pd_timer_atomic_op( \
+ atomic_clear_bits, \
+ (atomic_t *)timer_active[p], \
(m))
#define PD_CHK_ACTIVE(p, m) ((timer_active[p][0] & ((m) >> 32)) | \
(timer_active[p][1] & (m)))
/* exported: set/clear/check the current timer_disabled for a timer */
-#define PD_SET_DISABLED(p, m) pd_timer_atomic_op( \
- atomic_or, \
- timer_disabled[p], \
+#define PD_SET_DISABLED(p, m) pd_timer_atomic_op( \
+ atomic_or, \
+ (atomic_t *)timer_disabled[p], \
(m))
-#define PD_CLR_DISABLED(p, m) pd_timer_atomic_op( \
- atomic_clear_bits, \
- timer_disabled[p], \
+#define PD_CLR_DISABLED(p, m) pd_timer_atomic_op( \
+ atomic_clear_bits, \
+ (atomic_t *)timer_disabled[p], \
(m))
#define PD_CHK_DISABLED(p, m) ((timer_disabled[p][0] & ((m) >> 32)) | \
(timer_disabled[p][1] & (m)))
diff --git a/zephyr/projects/corsola/src/usb_pd_policy.c b/zephyr/projects/corsola/src/usb_pd_policy.c
index a30c5e2fe3..63131b8290 100644
--- a/zephyr/projects/corsola/src/usb_pd_policy.c
+++ b/zephyr/projects/corsola/src/usb_pd_policy.c
@@ -155,7 +155,7 @@ __override void svdm_exit_dp_mode(int port)
int pd_snk_is_vbus_provided(int port)
{
- static int vbus_prev[CONFIG_USB_PD_PORT_MAX_COUNT];
+ static atomic_t vbus_prev[CONFIG_USB_PD_PORT_MAX_COUNT];
int vbus;
/*