Skip to content

Commit 23cca30

Browse files
refactor(mt-spi): buffer allocation optimisations
1 parent a6db23e commit 23cca30

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

hw/arm/apple-silicon/mt-spi.c

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -156,93 +156,96 @@ struct AppleMTSPIState {
156156
#define PATH_STAGE_LINGER_IN_RANGE (6)
157157
#define PATH_STAGE_OUT_OF_RANGE (7)
158158

159-
static void apple_mt_spi_buf_free(AppleMTSPIBuffer *buf)
159+
static inline void apple_mt_spi_buf_free(AppleMTSPIBuffer *buf)
160160
{
161161
g_free(buf->data);
162162
*buf = (AppleMTSPIBuffer){ 0 };
163163
}
164164

165-
static void apple_mt_spi_buf_ensure_capacity(AppleMTSPIBuffer *buf,
166-
size_t bytes)
165+
static inline void apple_mt_spi_buf_ensure_capacity(AppleMTSPIBuffer *buf,
166+
size_t bytes)
167167
{
168168
if ((buf->len + bytes) > buf->capacity) {
169169
buf->capacity = buf->len + bytes;
170170
buf->data = g_realloc(buf->data, buf->capacity);
171171
}
172172
}
173173

174-
static void apple_mt_spi_buf_set_capacity(AppleMTSPIBuffer *buf,
175-
size_t capacity)
174+
static inline void apple_mt_spi_buf_set_capacity(AppleMTSPIBuffer *buf,
175+
size_t capacity)
176176
{
177177
assert_cmphex(capacity, >=, buf->capacity);
178178
buf->capacity = capacity;
179179
buf->data = g_realloc(buf->data, buf->capacity);
180180
}
181181

182-
static void apple_mt_spi_buf_set_len(AppleMTSPIBuffer *buf, uint8_t val,
183-
size_t len)
182+
static inline void apple_mt_spi_buf_set_len(AppleMTSPIBuffer *buf, uint8_t val,
183+
size_t len)
184184
{
185185
assert_cmphex(len, >=, buf->len);
186186
apple_mt_spi_buf_ensure_capacity(buf, len - buf->len);
187187
memset(buf->data + buf->len, val, len - buf->len);
188188
buf->len = len;
189189
}
190190

191-
static bool apple_mt_spi_buf_is_empty(const AppleMTSPIBuffer *buf)
191+
static inline bool apple_mt_spi_buf_is_empty(const AppleMTSPIBuffer *buf)
192192
{
193193
return buf->len == 0;
194194
}
195195

196-
static size_t apple_mt_spi_buf_get_pos(const AppleMTSPIBuffer *buf)
196+
static inline size_t apple_mt_spi_buf_get_pos(const AppleMTSPIBuffer *buf)
197197
{
198198
assert_false(apple_mt_spi_buf_is_empty(buf));
199199
return buf->len - 1;
200200
}
201201

202-
static bool apple_mt_spi_buf_is_full(const AppleMTSPIBuffer *buf)
202+
static inline bool apple_mt_spi_buf_is_full(const AppleMTSPIBuffer *buf)
203203
{
204204
return !apple_mt_spi_buf_is_empty(buf) && buf->len == buf->capacity;
205205
}
206206

207-
static bool apple_mt_spi_buf_pos_at_start(const AppleMTSPIBuffer *buf)
207+
static inline bool apple_mt_spi_buf_pos_at_start(const AppleMTSPIBuffer *buf)
208208
{
209209
return apple_mt_spi_buf_get_pos(buf) == 0;
210210
}
211211

212-
static bool apple_mt_spi_buf_read_pos_at_end(const AppleMTSPIBuffer *buf)
212+
static inline bool apple_mt_spi_buf_read_pos_at_end(const AppleMTSPIBuffer *buf)
213213
{
214214
return (buf->read_pos + 1) == buf->len;
215215
}
216216

217-
static void apple_mt_spi_buf_push_byte(AppleMTSPIBuffer *buf, uint8_t val)
217+
static inline void apple_mt_spi_buf_push_byte(AppleMTSPIBuffer *buf,
218+
uint8_t val)
218219
{
219220
apple_mt_spi_buf_ensure_capacity(buf, sizeof(val));
220221
buf->data[buf->len] = val;
221222
buf->len += sizeof(val);
222223
}
223224

224-
static void apple_mt_spi_buf_push_word(AppleMTSPIBuffer *buf, uint16_t val)
225+
static inline void apple_mt_spi_buf_push_word(AppleMTSPIBuffer *buf,
226+
uint16_t val)
225227
{
226228
apple_mt_spi_buf_ensure_capacity(buf, sizeof(val));
227229
stw_le_p(buf->data + buf->len, val);
228230
buf->len += sizeof(val);
229231
}
230232

231-
static void apple_mt_spi_buf_push_dword(AppleMTSPIBuffer *buf, uint32_t val)
233+
static inline void apple_mt_spi_buf_push_dword(AppleMTSPIBuffer *buf,
234+
uint32_t val)
232235
{
233236
apple_mt_spi_buf_ensure_capacity(buf, sizeof(val));
234237
stl_le_p(buf->data + buf->len, val);
235238
buf->len += sizeof(val);
236239
}
237240

238-
static void apple_mt_spi_buf_push_crc16(AppleMTSPIBuffer *buf)
241+
static inline void apple_mt_spi_buf_push_crc16(AppleMTSPIBuffer *buf)
239242
{
240243
assert_false(apple_mt_spi_buf_is_empty(buf));
241244
apple_mt_spi_buf_push_word(buf, crc16(0, buf->data, buf->len));
242245
}
243246

244-
static void apple_mt_spi_buf_append(AppleMTSPIBuffer *buf,
245-
AppleMTSPIBuffer *other_buf)
247+
static inline void apple_mt_spi_buf_append(AppleMTSPIBuffer *buf,
248+
AppleMTSPIBuffer *other_buf)
246249
{
247250
if (!apple_mt_spi_buf_is_empty(other_buf)) {
248251
apple_mt_spi_buf_ensure_capacity(buf, other_buf->len);
@@ -252,7 +255,7 @@ static void apple_mt_spi_buf_append(AppleMTSPIBuffer *buf,
252255
apple_mt_spi_buf_free(other_buf);
253256
}
254257

255-
static uint8_t apple_mt_spi_buf_pop(AppleMTSPIBuffer *buf)
258+
static inline uint8_t apple_mt_spi_buf_pop(AppleMTSPIBuffer *buf)
256259
{
257260
uint8_t ret;
258261

@@ -436,6 +439,7 @@ static void apple_mt_spi_handle_hbpp(AppleMTSPIState *s)
436439
break;
437440
case HBPP_PACKET_MEM_READ:
438441
if (apple_mt_spi_buf_pos_at_start(&s->rx)) {
442+
apple_mt_spi_buf_ensure_capacity(&s->pending_hbpp, 2 + 4 + 2);
439443
apple_mt_spi_push_pending_hbpp_word(s, HBPP_PACKET_ACK_RD_REQ);
440444
apple_mt_spi_push_pending_hbpp_dword(s, 0x00000000); // value
441445
apple_mt_spi_push_pending_hbpp_word(s, 0x0000); // crc16 of value
@@ -468,6 +472,7 @@ static void apple_mt_spi_push_ll_hdr(AppleMTSPIBuffer *buf, uint8_t type,
468472
uint16_t payload_remaining,
469473
uint16_t payload_length)
470474
{
475+
apple_mt_spi_buf_ensure_capacity(buf, 8);
471476
apple_mt_spi_buf_push_byte(buf, type);
472477
apple_mt_spi_buf_push_byte(buf, interface);
473478
apple_mt_spi_buf_push_word(buf, payload_off);
@@ -493,17 +498,16 @@ static uint8_t apple_mt_spi_ll_read_payload_byte(AppleMTSPIBuffer *buf,
493498
{
494499
assert_false(apple_mt_spi_buf_is_empty(buf));
495500
assert_cmphex(sizeof(uint32_t) + sizeof(uint64_t) + off + sizeof(uint8_t),
496-
<=, buf->len);
501+
<=, buf->len);
497502
return buf->data[sizeof(uint32_t) + sizeof(uint64_t) + off];
498503
}
499504

500505
static uint16_t apple_mt_spi_ll_read_payload_word(AppleMTSPIBuffer *buf,
501506
size_t off)
502507
{
503508
assert_false(apple_mt_spi_buf_is_empty(buf));
504-
assert_cmphex(sizeof(uint32_t) + sizeof(uint64_t) + off +
505-
sizeof(uint16_t),
506-
<=, buf->len);
509+
assert_cmphex(sizeof(uint32_t) + sizeof(uint64_t) + off + sizeof(uint16_t),
510+
<=, buf->len);
507511
return lduw_le_p(buf->data + sizeof(uint32_t) + sizeof(uint64_t) + off);
508512
}
509513

@@ -513,6 +517,7 @@ static void apple_mt_spi_push_hid_hdr(AppleMTSPIBuffer *buf, uint8_t type,
513517
uint16_t length_requested,
514518
uint16_t payload_length)
515519
{
520+
apple_mt_spi_buf_ensure_capacity(buf, 8);
516521
apple_mt_spi_buf_push_byte(buf, type);
517522
apple_mt_spi_buf_push_byte(buf, report_id);
518523
apple_mt_spi_buf_push_byte(buf, packet_status);
@@ -527,6 +532,7 @@ static void apple_mt_spi_push_report_hdr(AppleMTSPIBuffer *buf, uint8_t type,
527532
uint8_t frame_number,
528533
uint16_t payload_length)
529534
{
535+
apple_mt_spi_buf_ensure_capacity(buf, 9);
530536
apple_mt_spi_push_hid_hdr(buf, type, report_id, packet_status, frame_number,
531537
0, payload_length + sizeof(report_id));
532538
apple_mt_spi_buf_push_byte(buf, report_id);
@@ -557,11 +563,13 @@ static void apple_mt_spi_handle_get_feature(AppleMTSPIState *s)
557563

558564
switch (report_id) {
559565
case HID_REPORT_FAMILY_ID:
566+
apple_mt_spi_buf_ensure_capacity(&packet->buf, 9 + 1 + 2);
560567
apple_mt_spi_push_report_byte(
561568
&packet->buf, HID_CONTROL_PACKET_SET_OUTPUT_REPORT, report_id,
562569
HID_PACKET_STATUS_SUCCESS, frame_number, MT_FAMILY_ID);
563570
break;
564571
case HID_REPORT_BASIC_DEVICE_INFO:
572+
apple_mt_spi_buf_ensure_capacity(&packet->buf, 9 + 5 + 2);
565573
apple_mt_spi_push_report_hdr(
566574
&packet->buf, HID_CONTROL_PACKET_SET_OUTPUT_REPORT, report_id,
567575
HID_PACKET_STATUS_SUCCESS, frame_number, 5);
@@ -571,6 +579,7 @@ static void apple_mt_spi_handle_get_feature(AppleMTSPIState *s)
571579
apple_mt_spi_buf_push_word(&packet->buf, MT_BCD_VER);
572580
break;
573581
case HID_REPORT_SENSOR_SURFACE_DESC:
582+
apple_mt_spi_buf_ensure_capacity(&packet->buf, 9 + 16 + 2);
574583
apple_mt_spi_push_report_hdr(
575584
&packet->buf, HID_CONTROL_PACKET_SET_OUTPUT_REPORT, report_id,
576585
HID_PACKET_STATUS_SUCCESS, frame_number, 16);
@@ -584,6 +593,7 @@ static void apple_mt_spi_handle_get_feature(AppleMTSPIState *s)
584593
apple_mt_spi_buf_push_word(&packet->buf, MT_SENSOR_SURFACE_HEIGHT);
585594
break;
586595
case HID_REPORT_SENSOR_REGION_PARAM:
596+
apple_mt_spi_buf_ensure_capacity(&packet->buf, 9 + 6 + 2);
587597
apple_mt_spi_push_report_hdr(
588598
&packet->buf, HID_CONTROL_PACKET_SET_OUTPUT_REPORT, report_id,
589599
HID_PACKET_STATUS_SUCCESS, frame_number, 6);
@@ -592,6 +602,7 @@ static void apple_mt_spi_handle_get_feature(AppleMTSPIState *s)
592602
apple_mt_spi_buf_push_word(&packet->buf, 0x200);
593603
break;
594604
case HID_REPORT_SENSOR_REGION_DESC:
605+
apple_mt_spi_buf_ensure_capacity(&packet->buf, 9 + 22 + 2);
595606
apple_mt_spi_push_report_hdr(
596607
&packet->buf, HID_CONTROL_PACKET_SET_OUTPUT_REPORT, report_id,
597608
HID_PACKET_STATUS_SUCCESS, frame_number, 22); // 1 + 7*3
@@ -794,6 +805,7 @@ static void apple_mt_spi_send_path_update(AppleMTSPIState *s, uint64_t ts,
794805
y_delta = s->y - s->prev_y;
795806

796807
packet->type = LL_PACKET_LOSSLESS_OUTPUT;
808+
apple_mt_spi_buf_ensure_capacity(&packet->buf, 9 + 27 + 20 + 2);
797809
apple_mt_spi_push_report_hdr(&packet->buf, HID_TRANSFER_PACKET_OUTPUT,
798810
HID_REPORT_BINARY_PATH_OR_IMAGE,
799811
HID_PACKET_STATUS_SUCCESS, s->frame, 27 + 20);

0 commit comments

Comments
 (0)