1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017-2018 The Linux Foundation. All rights reserved. */
4 #include <linux/completion.h>
5 #include <linux/circ_buf.h>
6 #include <linux/list.h>
9 #include "a6xx_gmu.xml.h"
12 #define HFI_MSG_ID(val) [val] = #val
14 static const char * const a6xx_hfi_msg_id[] = {
15 HFI_MSG_ID(HFI_H2F_MSG_INIT),
16 HFI_MSG_ID(HFI_H2F_MSG_FW_VERSION),
17 HFI_MSG_ID(HFI_H2F_MSG_BW_TABLE),
18 HFI_MSG_ID(HFI_H2F_MSG_PERF_TABLE),
19 HFI_MSG_ID(HFI_H2F_MSG_TEST),
20 HFI_MSG_ID(HFI_H2F_MSG_START),
21 HFI_MSG_ID(HFI_H2F_MSG_CORE_FW_START),
22 HFI_MSG_ID(HFI_H2F_MSG_GX_BW_PERF_VOTE),
23 HFI_MSG_ID(HFI_H2F_MSG_PREPARE_SLUMBER),
26 static int a6xx_hfi_queue_read(struct a6xx_gmu *gmu,
27 struct a6xx_hfi_queue *queue, u32 *data, u32 dwords)
29 struct a6xx_hfi_queue_header *header = queue->header;
30 u32 i, hdr, index = header->read_index;
32 if (header->read_index == header->write_index) {
33 header->rx_request = 1;
37 hdr = queue->data[index];
39 queue->history[(queue->history_idx++) % HFI_HISTORY_SZ] = index;
42 * If we are to assume that the GMU firmware is in fact a rational actor
43 * and is programmed to not send us a larger response than we expect
44 * then we can also assume that if the header size is unexpectedly large
45 * that it is due to memory corruption and/or hardware failure. In this
46 * case the only reasonable course of action is to BUG() to help harden
50 BUG_ON(HFI_HEADER_SIZE(hdr) > dwords);
52 for (i = 0; i < HFI_HEADER_SIZE(hdr); i++) {
53 data[i] = queue->data[index];
54 index = (index + 1) % header->size;
58 index = ALIGN(index, 4) % header->size;
60 header->read_index = index;
61 return HFI_HEADER_SIZE(hdr);
64 static int a6xx_hfi_queue_write(struct a6xx_gmu *gmu,
65 struct a6xx_hfi_queue *queue, u32 *data, u32 dwords)
67 struct a6xx_hfi_queue_header *header = queue->header;
68 u32 i, space, index = header->write_index;
70 spin_lock(&queue->lock);
72 space = CIRC_SPACE(header->write_index, header->read_index,
76 spin_unlock(&queue->lock);
80 queue->history[(queue->history_idx++) % HFI_HISTORY_SZ] = index;
82 for (i = 0; i < dwords; i++) {
83 queue->data[index] = data[i];
84 index = (index + 1) % header->size;
87 /* Cookify any non used data at the end of the write buffer */
89 for (; index % 4; index = (index + 1) % header->size)
90 queue->data[index] = 0xfafafafa;
93 header->write_index = index;
94 spin_unlock(&queue->lock);
96 gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET, 0x01);
100 static int a6xx_hfi_wait_for_ack(struct a6xx_gmu *gmu, u32 id, u32 seqnum,
101 u32 *payload, u32 payload_size)
103 struct a6xx_hfi_queue *queue = &gmu->queues[HFI_RESPONSE_QUEUE];
107 /* Wait for a response */
108 ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_GMU2HOST_INTR_INFO, val,
109 val & A6XX_GMU_GMU2HOST_INTR_INFO_MSGQ, 100, 5000);
112 DRM_DEV_ERROR(gmu->dev,
113 "Message %s id %d timed out waiting for response\n",
114 a6xx_hfi_msg_id[id], seqnum);
118 /* Clear the interrupt */
119 gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_CLR,
120 A6XX_GMU_GMU2HOST_INTR_INFO_MSGQ);
123 struct a6xx_hfi_msg_response resp;
125 /* Get the next packet */
126 ret = a6xx_hfi_queue_read(gmu, queue, (u32 *) &resp,
129 /* If the queue is empty our response never made it */
131 DRM_DEV_ERROR(gmu->dev,
132 "The HFI response queue is unexpectedly empty\n");
137 if (HFI_HEADER_ID(resp.header) == HFI_F2H_MSG_ERROR) {
138 struct a6xx_hfi_msg_error *error =
139 (struct a6xx_hfi_msg_error *) &resp;
141 DRM_DEV_ERROR(gmu->dev, "GMU firmware error %d\n",
146 if (seqnum != HFI_HEADER_SEQNUM(resp.ret_header)) {
147 DRM_DEV_ERROR(gmu->dev,
148 "Unexpected message id %d on the response queue\n",
149 HFI_HEADER_SEQNUM(resp.ret_header));
154 DRM_DEV_ERROR(gmu->dev,
155 "Message %s id %d returned error %d\n",
156 a6xx_hfi_msg_id[id], seqnum, resp.error);
160 /* All is well, copy over the buffer */
161 if (payload && payload_size)
162 memcpy(payload, resp.payload,
163 min_t(u32, payload_size, sizeof(resp.payload)));
169 static int a6xx_hfi_send_msg(struct a6xx_gmu *gmu, int id,
170 void *data, u32 size, u32 *payload, u32 payload_size)
172 struct a6xx_hfi_queue *queue = &gmu->queues[HFI_COMMAND_QUEUE];
173 int ret, dwords = size >> 2;
176 seqnum = atomic_inc_return(&queue->seqnum) % 0xfff;
178 /* First dword of the message is the message header - fill it in */
179 *((u32 *) data) = (seqnum << 20) | (HFI_MSG_CMD << 16) |
182 ret = a6xx_hfi_queue_write(gmu, queue, data, dwords);
184 DRM_DEV_ERROR(gmu->dev, "Unable to send message %s id %d\n",
185 a6xx_hfi_msg_id[id], seqnum);
189 return a6xx_hfi_wait_for_ack(gmu, id, seqnum, payload, payload_size);
192 static int a6xx_hfi_send_gmu_init(struct a6xx_gmu *gmu, int boot_state)
194 struct a6xx_hfi_msg_gmu_init_cmd msg = { 0 };
196 msg.dbg_buffer_addr = (u32) gmu->debug.iova;
197 msg.dbg_buffer_size = (u32) gmu->debug.size;
198 msg.boot_state = boot_state;
200 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_INIT, &msg, sizeof(msg),
204 static int a6xx_hfi_get_fw_version(struct a6xx_gmu *gmu, u32 *version)
206 struct a6xx_hfi_msg_fw_version msg = { 0 };
208 /* Currently supporting version 1.1 */
209 msg.supported_version = (1 << 28) | (1 << 16);
211 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_FW_VERSION, &msg, sizeof(msg),
212 version, sizeof(*version));
215 static int a6xx_hfi_send_perf_table_v1(struct a6xx_gmu *gmu)
217 struct a6xx_hfi_msg_perf_table_v1 msg = { 0 };
220 msg.num_gpu_levels = gmu->nr_gpu_freqs;
221 msg.num_gmu_levels = gmu->nr_gmu_freqs;
223 for (i = 0; i < gmu->nr_gpu_freqs; i++) {
224 msg.gx_votes[i].vote = gmu->gx_arc_votes[i];
225 msg.gx_votes[i].freq = gmu->gpu_freqs[i] / 1000;
228 for (i = 0; i < gmu->nr_gmu_freqs; i++) {
229 msg.cx_votes[i].vote = gmu->cx_arc_votes[i];
230 msg.cx_votes[i].freq = gmu->gmu_freqs[i] / 1000;
233 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_PERF_TABLE, &msg, sizeof(msg),
237 static int a6xx_hfi_send_perf_table(struct a6xx_gmu *gmu)
239 struct a6xx_hfi_msg_perf_table msg = { 0 };
242 msg.num_gpu_levels = gmu->nr_gpu_freqs;
243 msg.num_gmu_levels = gmu->nr_gmu_freqs;
245 for (i = 0; i < gmu->nr_gpu_freqs; i++) {
246 msg.gx_votes[i].vote = gmu->gx_arc_votes[i];
247 msg.gx_votes[i].acd = 0xffffffff;
248 msg.gx_votes[i].freq = gmu->gpu_freqs[i] / 1000;
251 for (i = 0; i < gmu->nr_gmu_freqs; i++) {
252 msg.cx_votes[i].vote = gmu->cx_arc_votes[i];
253 msg.cx_votes[i].freq = gmu->gmu_freqs[i] / 1000;
256 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_PERF_TABLE, &msg, sizeof(msg),
260 static void a618_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
262 /* Send a single "off" entry since the 618 GMU doesn't do bus scaling */
263 msg->bw_level_num = 1;
265 msg->ddr_cmds_num = 3;
266 msg->ddr_wait_bitmask = 0x01;
268 msg->ddr_cmds_addrs[0] = 0x50000;
269 msg->ddr_cmds_addrs[1] = 0x5003c;
270 msg->ddr_cmds_addrs[2] = 0x5000c;
272 msg->ddr_cmds_data[0][0] = 0x40000000;
273 msg->ddr_cmds_data[0][1] = 0x40000000;
274 msg->ddr_cmds_data[0][2] = 0x40000000;
277 * These are the CX (CNOC) votes - these are used by the GMU but the
278 * votes are known and fixed for the target
280 msg->cnoc_cmds_num = 1;
281 msg->cnoc_wait_bitmask = 0x01;
283 msg->cnoc_cmds_addrs[0] = 0x5007c;
284 msg->cnoc_cmds_data[0][0] = 0x40000000;
285 msg->cnoc_cmds_data[1][0] = 0x60000001;
288 static void a640_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
291 * Send a single "off" entry just to get things running
294 msg->bw_level_num = 1;
296 msg->ddr_cmds_num = 3;
297 msg->ddr_wait_bitmask = 0x01;
299 msg->ddr_cmds_addrs[0] = 0x50000;
300 msg->ddr_cmds_addrs[1] = 0x5003c;
301 msg->ddr_cmds_addrs[2] = 0x5000c;
303 msg->ddr_cmds_data[0][0] = 0x40000000;
304 msg->ddr_cmds_data[0][1] = 0x40000000;
305 msg->ddr_cmds_data[0][2] = 0x40000000;
308 * These are the CX (CNOC) votes - these are used by the GMU but the
309 * votes are known and fixed for the target
311 msg->cnoc_cmds_num = 3;
312 msg->cnoc_wait_bitmask = 0x01;
314 msg->cnoc_cmds_addrs[0] = 0x50034;
315 msg->cnoc_cmds_addrs[1] = 0x5007c;
316 msg->cnoc_cmds_addrs[2] = 0x5004c;
318 msg->cnoc_cmds_data[0][0] = 0x40000000;
319 msg->cnoc_cmds_data[0][1] = 0x00000000;
320 msg->cnoc_cmds_data[0][2] = 0x40000000;
322 msg->cnoc_cmds_data[1][0] = 0x60000001;
323 msg->cnoc_cmds_data[1][1] = 0x20000001;
324 msg->cnoc_cmds_data[1][2] = 0x60000001;
327 static void a650_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
330 * Send a single "off" entry just to get things running
333 msg->bw_level_num = 1;
335 msg->ddr_cmds_num = 3;
336 msg->ddr_wait_bitmask = 0x01;
338 msg->ddr_cmds_addrs[0] = 0x50000;
339 msg->ddr_cmds_addrs[1] = 0x50004;
340 msg->ddr_cmds_addrs[2] = 0x5007c;
342 msg->ddr_cmds_data[0][0] = 0x40000000;
343 msg->ddr_cmds_data[0][1] = 0x40000000;
344 msg->ddr_cmds_data[0][2] = 0x40000000;
347 * These are the CX (CNOC) votes - these are used by the GMU but the
348 * votes are known and fixed for the target
350 msg->cnoc_cmds_num = 1;
351 msg->cnoc_wait_bitmask = 0x01;
353 msg->cnoc_cmds_addrs[0] = 0x500a4;
354 msg->cnoc_cmds_data[0][0] = 0x40000000;
355 msg->cnoc_cmds_data[1][0] = 0x60000001;
358 static void a660_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
361 * Send a single "off" entry just to get things running
364 msg->bw_level_num = 1;
366 msg->ddr_cmds_num = 3;
367 msg->ddr_wait_bitmask = 0x01;
369 msg->ddr_cmds_addrs[0] = 0x50004;
370 msg->ddr_cmds_addrs[1] = 0x500a0;
371 msg->ddr_cmds_addrs[2] = 0x50000;
373 msg->ddr_cmds_data[0][0] = 0x40000000;
374 msg->ddr_cmds_data[0][1] = 0x40000000;
375 msg->ddr_cmds_data[0][2] = 0x40000000;
378 * These are the CX (CNOC) votes - these are used by the GMU but the
379 * votes are known and fixed for the target
381 msg->cnoc_cmds_num = 1;
382 msg->cnoc_wait_bitmask = 0x01;
384 msg->cnoc_cmds_addrs[0] = 0x50070;
385 msg->cnoc_cmds_data[0][0] = 0x40000000;
386 msg->cnoc_cmds_data[1][0] = 0x60000001;
389 static void adreno_7c3_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
392 * Send a single "off" entry just to get things running
395 msg->bw_level_num = 1;
397 msg->ddr_cmds_num = 3;
398 msg->ddr_wait_bitmask = 0x07;
400 msg->ddr_cmds_addrs[0] = 0x50004;
401 msg->ddr_cmds_addrs[1] = 0x50000;
402 msg->ddr_cmds_addrs[2] = 0x50088;
404 msg->ddr_cmds_data[0][0] = 0x40000000;
405 msg->ddr_cmds_data[0][1] = 0x40000000;
406 msg->ddr_cmds_data[0][2] = 0x40000000;
409 * These are the CX (CNOC) votes - these are used by the GMU but the
410 * votes are known and fixed for the target
412 msg->cnoc_cmds_num = 1;
413 msg->cnoc_wait_bitmask = 0x01;
415 msg->cnoc_cmds_addrs[0] = 0x5006c;
416 msg->cnoc_cmds_data[0][0] = 0x40000000;
417 msg->cnoc_cmds_data[1][0] = 0x60000001;
419 static void a6xx_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
421 /* Send a single "off" entry since the 630 GMU doesn't do bus scaling */
422 msg->bw_level_num = 1;
424 msg->ddr_cmds_num = 3;
425 msg->ddr_wait_bitmask = 0x07;
427 msg->ddr_cmds_addrs[0] = 0x50000;
428 msg->ddr_cmds_addrs[1] = 0x5005c;
429 msg->ddr_cmds_addrs[2] = 0x5000c;
431 msg->ddr_cmds_data[0][0] = 0x40000000;
432 msg->ddr_cmds_data[0][1] = 0x40000000;
433 msg->ddr_cmds_data[0][2] = 0x40000000;
436 * These are the CX (CNOC) votes. This is used but the values for the
437 * sdm845 GMU are known and fixed so we can hard code them.
440 msg->cnoc_cmds_num = 3;
441 msg->cnoc_wait_bitmask = 0x05;
443 msg->cnoc_cmds_addrs[0] = 0x50034;
444 msg->cnoc_cmds_addrs[1] = 0x5007c;
445 msg->cnoc_cmds_addrs[2] = 0x5004c;
447 msg->cnoc_cmds_data[0][0] = 0x40000000;
448 msg->cnoc_cmds_data[0][1] = 0x00000000;
449 msg->cnoc_cmds_data[0][2] = 0x40000000;
451 msg->cnoc_cmds_data[1][0] = 0x60000001;
452 msg->cnoc_cmds_data[1][1] = 0x20000001;
453 msg->cnoc_cmds_data[1][2] = 0x60000001;
457 static int a6xx_hfi_send_bw_table(struct a6xx_gmu *gmu)
459 struct a6xx_hfi_msg_bw_table msg = { 0 };
460 struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
461 struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
463 if (adreno_is_a618(adreno_gpu))
464 a618_build_bw_table(&msg);
465 else if (adreno_is_a640_family(adreno_gpu))
466 a640_build_bw_table(&msg);
467 else if (adreno_is_a650(adreno_gpu))
468 a650_build_bw_table(&msg);
469 else if (adreno_is_7c3(adreno_gpu))
470 adreno_7c3_build_bw_table(&msg);
471 else if (adreno_is_a660(adreno_gpu))
472 a660_build_bw_table(&msg);
474 a6xx_build_bw_table(&msg);
476 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_BW_TABLE, &msg, sizeof(msg),
480 static int a6xx_hfi_send_test(struct a6xx_gmu *gmu)
482 struct a6xx_hfi_msg_test msg = { 0 };
484 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_TEST, &msg, sizeof(msg),
488 static int a6xx_hfi_send_start(struct a6xx_gmu *gmu)
490 struct a6xx_hfi_msg_start msg = { 0 };
492 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_START, &msg, sizeof(msg),
496 static int a6xx_hfi_send_core_fw_start(struct a6xx_gmu *gmu)
498 struct a6xx_hfi_msg_core_fw_start msg = { 0 };
500 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_CORE_FW_START, &msg,
501 sizeof(msg), NULL, 0);
504 int a6xx_hfi_set_freq(struct a6xx_gmu *gmu, int index)
506 struct a6xx_hfi_gx_bw_perf_vote_cmd msg = { 0 };
508 msg.ack_type = 1; /* blocking */
510 msg.bw = 0; /* TODO: bus scaling */
512 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_GX_BW_PERF_VOTE, &msg,
513 sizeof(msg), NULL, 0);
516 int a6xx_hfi_send_prep_slumber(struct a6xx_gmu *gmu)
518 struct a6xx_hfi_prep_slumber_cmd msg = { 0 };
520 /* TODO: should freq and bw fields be non-zero ? */
522 return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_PREPARE_SLUMBER, &msg,
523 sizeof(msg), NULL, 0);
526 static int a6xx_hfi_start_v1(struct a6xx_gmu *gmu, int boot_state)
530 ret = a6xx_hfi_send_gmu_init(gmu, boot_state);
534 ret = a6xx_hfi_get_fw_version(gmu, NULL);
539 * We have to get exchange version numbers per the sequence but at this
540 * point th kernel driver doesn't need to know the exact version of
544 ret = a6xx_hfi_send_perf_table_v1(gmu);
548 ret = a6xx_hfi_send_bw_table(gmu);
553 * Let the GMU know that there won't be any more HFI messages until next
556 a6xx_hfi_send_test(gmu);
561 int a6xx_hfi_start(struct a6xx_gmu *gmu, int boot_state)
566 return a6xx_hfi_start_v1(gmu, boot_state);
569 ret = a6xx_hfi_send_perf_table(gmu);
573 ret = a6xx_hfi_send_bw_table(gmu);
577 ret = a6xx_hfi_send_core_fw_start(gmu);
582 * Downstream driver sends this in its "a6xx_hw_init" equivalent,
583 * but seems to be no harm in sending it here
585 ret = a6xx_hfi_send_start(gmu);
592 void a6xx_hfi_stop(struct a6xx_gmu *gmu)
596 for (i = 0; i < ARRAY_SIZE(gmu->queues); i++) {
597 struct a6xx_hfi_queue *queue = &gmu->queues[i];
602 if (queue->header->read_index != queue->header->write_index)
603 DRM_DEV_ERROR(gmu->dev, "HFI queue %d is not empty\n", i);
605 queue->header->read_index = 0;
606 queue->header->write_index = 0;
608 memset(&queue->history, 0xff, sizeof(queue->history));
609 queue->history_idx = 0;
613 static void a6xx_hfi_queue_init(struct a6xx_hfi_queue *queue,
614 struct a6xx_hfi_queue_header *header, void *virt, u64 iova,
617 spin_lock_init(&queue->lock);
618 queue->header = header;
620 atomic_set(&queue->seqnum, 0);
622 memset(&queue->history, 0xff, sizeof(queue->history));
623 queue->history_idx = 0;
625 /* Set up the shared memory header */
627 header->type = 10 << 8 | id;
629 header->size = SZ_4K >> 2;
630 header->msg_size = 0;
632 header->rx_watermark = 1;
633 header->tx_watermark = 1;
634 header->rx_request = 1;
635 header->tx_request = 0;
636 header->read_index = 0;
637 header->write_index = 0;
640 void a6xx_hfi_init(struct a6xx_gmu *gmu)
642 struct a6xx_gmu_bo *hfi = &gmu->hfi;
643 struct a6xx_hfi_queue_table_header *table = hfi->virt;
644 struct a6xx_hfi_queue_header *headers = hfi->virt + sizeof(*table);
649 * The table size is the size of the table header plus all of the queue
652 table_size = sizeof(*table);
653 table_size += (ARRAY_SIZE(gmu->queues) *
654 sizeof(struct a6xx_hfi_queue_header));
657 table->size = table_size;
658 /* First queue header is located immediately after the table header */
659 table->qhdr0_offset = sizeof(*table) >> 2;
660 table->qhdr_size = sizeof(struct a6xx_hfi_queue_header) >> 2;
661 table->num_queues = ARRAY_SIZE(gmu->queues);
662 table->active_queues = ARRAY_SIZE(gmu->queues);
666 a6xx_hfi_queue_init(&gmu->queues[0], &headers[0], hfi->virt + offset,
667 hfi->iova + offset, 0);
669 /* GMU response queue */
671 a6xx_hfi_queue_init(&gmu->queues[1], &headers[1], hfi->virt + offset,
672 hfi->iova + offset, gmu->legacy ? 4 : 1);