2 * Copyright 2019 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
29 #include "dmub_types.h"
32 #if defined(__cplusplus)
36 struct dmub_rb_init_params {
56 static inline bool dmub_rb_empty(struct dmub_rb *rb)
58 return (rb->wrpt == rb->rptr);
61 static inline bool dmub_rb_full(struct dmub_rb *rb)
65 if (rb->wrpt >= rb->rptr)
66 data_count = rb->wrpt - rb->rptr;
68 data_count = rb->capacity - (rb->rptr - rb->wrpt);
70 return (data_count == (rb->capacity - DMUB_RB_CMD_SIZE));
73 static inline bool dmub_rb_push_front(struct dmub_rb *rb,
74 const union dmub_rb_cmd *cmd)
76 uint64_t volatile *dst = (uint64_t volatile *)(rb->base_address) + rb->wrpt / sizeof(uint64_t);
77 const uint64_t *src = (const uint64_t *)cmd;
84 for (i = 0; i < DMUB_RB_CMD_SIZE / sizeof(uint64_t); i++)
87 rb->wrpt += DMUB_RB_CMD_SIZE;
89 if (rb->wrpt >= rb->capacity)
90 rb->wrpt %= rb->capacity;
95 static inline bool dmub_rb_front(struct dmub_rb *rb,
96 union dmub_rb_cmd *cmd)
98 uint8_t *rd_ptr = (uint8_t *)rb->base_address + rb->rptr;
100 if (dmub_rb_empty(rb))
103 dmub_memcpy(cmd, rd_ptr, DMUB_RB_CMD_SIZE);
108 static inline bool dmub_rb_pop_front(struct dmub_rb *rb)
110 if (dmub_rb_empty(rb))
113 rb->rptr += DMUB_RB_CMD_SIZE;
115 if (rb->rptr >= rb->capacity)
116 rb->rptr %= rb->capacity;
121 static inline void dmub_rb_flush_pending(const struct dmub_rb *rb)
123 uint32_t rptr = rb->rptr;
124 uint32_t wptr = rb->wrpt;
126 while (rptr != wptr) {
127 uint64_t volatile *data = (uint64_t volatile *)rb->base_address + rptr / sizeof(uint64_t);
128 //uint64_t volatile *p = (uint64_t volatile *)data;
132 for (i = 0; i < DMUB_RB_CMD_SIZE / sizeof(uint64_t); i++)
135 rptr += DMUB_RB_CMD_SIZE;
136 if (rptr >= rb->capacity)
137 rptr %= rb->capacity;
141 static inline void dmub_rb_init(struct dmub_rb *rb,
142 struct dmub_rb_init_params *init_params)
144 rb->base_address = init_params->base_address;
145 rb->capacity = init_params->capacity;
146 rb->rptr = init_params->read_ptr;
147 rb->wrpt = init_params->write_ptr;
150 #if defined(__cplusplus)
154 #endif /* _DMUB_RB_H_ */