1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright (c) 2017-2018 The Linux Foundation. All rights reserved. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/completion.h>
5*4882a593Smuzhiyun #include <linux/circ_buf.h>
6*4882a593Smuzhiyun #include <linux/list.h>
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include "a6xx_gmu.h"
9*4882a593Smuzhiyun #include "a6xx_gmu.xml.h"
10*4882a593Smuzhiyun #include "a6xx_gpu.h"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define HFI_MSG_ID(val) [val] = #val
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun static const char * const a6xx_hfi_msg_id[] = {
15*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_INIT),
16*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_FW_VERSION),
17*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_BW_TABLE),
18*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_PERF_TABLE),
19*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_TEST),
20*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_START),
21*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_CORE_FW_START),
22*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_GX_BW_PERF_VOTE),
23*4882a593Smuzhiyun HFI_MSG_ID(HFI_H2F_MSG_PREPARE_SLUMBER),
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun
a6xx_hfi_queue_read(struct a6xx_gmu * gmu,struct a6xx_hfi_queue * queue,u32 * data,u32 dwords)26*4882a593Smuzhiyun static int a6xx_hfi_queue_read(struct a6xx_gmu *gmu,
27*4882a593Smuzhiyun struct a6xx_hfi_queue *queue, u32 *data, u32 dwords)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun struct a6xx_hfi_queue_header *header = queue->header;
30*4882a593Smuzhiyun u32 i, hdr, index = header->read_index;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun if (header->read_index == header->write_index) {
33*4882a593Smuzhiyun header->rx_request = 1;
34*4882a593Smuzhiyun return 0;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun hdr = queue->data[index];
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * If we are to assume that the GMU firmware is in fact a rational actor
41*4882a593Smuzhiyun * and is programmed to not send us a larger response than we expect
42*4882a593Smuzhiyun * then we can also assume that if the header size is unexpectedly large
43*4882a593Smuzhiyun * that it is due to memory corruption and/or hardware failure. In this
44*4882a593Smuzhiyun * case the only reasonable course of action is to BUG() to help harden
45*4882a593Smuzhiyun * the failure.
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun BUG_ON(HFI_HEADER_SIZE(hdr) > dwords);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun for (i = 0; i < HFI_HEADER_SIZE(hdr); i++) {
51*4882a593Smuzhiyun data[i] = queue->data[index];
52*4882a593Smuzhiyun index = (index + 1) % header->size;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (!gmu->legacy)
56*4882a593Smuzhiyun index = ALIGN(index, 4) % header->size;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun header->read_index = index;
59*4882a593Smuzhiyun return HFI_HEADER_SIZE(hdr);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
a6xx_hfi_queue_write(struct a6xx_gmu * gmu,struct a6xx_hfi_queue * queue,u32 * data,u32 dwords)62*4882a593Smuzhiyun static int a6xx_hfi_queue_write(struct a6xx_gmu *gmu,
63*4882a593Smuzhiyun struct a6xx_hfi_queue *queue, u32 *data, u32 dwords)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun struct a6xx_hfi_queue_header *header = queue->header;
66*4882a593Smuzhiyun u32 i, space, index = header->write_index;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun spin_lock(&queue->lock);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun space = CIRC_SPACE(header->write_index, header->read_index,
71*4882a593Smuzhiyun header->size);
72*4882a593Smuzhiyun if (space < dwords) {
73*4882a593Smuzhiyun header->dropped++;
74*4882a593Smuzhiyun spin_unlock(&queue->lock);
75*4882a593Smuzhiyun return -ENOSPC;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun for (i = 0; i < dwords; i++) {
79*4882a593Smuzhiyun queue->data[index] = data[i];
80*4882a593Smuzhiyun index = (index + 1) % header->size;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Cookify any non used data at the end of the write buffer */
84*4882a593Smuzhiyun if (!gmu->legacy) {
85*4882a593Smuzhiyun for (; index % 4; index = (index + 1) % header->size)
86*4882a593Smuzhiyun queue->data[index] = 0xfafafafa;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun header->write_index = index;
90*4882a593Smuzhiyun spin_unlock(&queue->lock);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun gmu_write(gmu, REG_A6XX_GMU_HOST2GMU_INTR_SET, 0x01);
93*4882a593Smuzhiyun return 0;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
a6xx_hfi_wait_for_ack(struct a6xx_gmu * gmu,u32 id,u32 seqnum,u32 * payload,u32 payload_size)96*4882a593Smuzhiyun static int a6xx_hfi_wait_for_ack(struct a6xx_gmu *gmu, u32 id, u32 seqnum,
97*4882a593Smuzhiyun u32 *payload, u32 payload_size)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun struct a6xx_hfi_queue *queue = &gmu->queues[HFI_RESPONSE_QUEUE];
100*4882a593Smuzhiyun u32 val;
101*4882a593Smuzhiyun int ret;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Wait for a response */
104*4882a593Smuzhiyun ret = gmu_poll_timeout(gmu, REG_A6XX_GMU_GMU2HOST_INTR_INFO, val,
105*4882a593Smuzhiyun val & A6XX_GMU_GMU2HOST_INTR_INFO_MSGQ, 100, 5000);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (ret) {
108*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev,
109*4882a593Smuzhiyun "Message %s id %d timed out waiting for response\n",
110*4882a593Smuzhiyun a6xx_hfi_msg_id[id], seqnum);
111*4882a593Smuzhiyun return -ETIMEDOUT;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Clear the interrupt */
115*4882a593Smuzhiyun gmu_write(gmu, REG_A6XX_GMU_GMU2HOST_INTR_CLR,
116*4882a593Smuzhiyun A6XX_GMU_GMU2HOST_INTR_INFO_MSGQ);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun for (;;) {
119*4882a593Smuzhiyun struct a6xx_hfi_msg_response resp;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* Get the next packet */
122*4882a593Smuzhiyun ret = a6xx_hfi_queue_read(gmu, queue, (u32 *) &resp,
123*4882a593Smuzhiyun sizeof(resp) >> 2);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* If the queue is empty our response never made it */
126*4882a593Smuzhiyun if (!ret) {
127*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev,
128*4882a593Smuzhiyun "The HFI response queue is unexpectedly empty\n");
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return -ENOENT;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (HFI_HEADER_ID(resp.header) == HFI_F2H_MSG_ERROR) {
134*4882a593Smuzhiyun struct a6xx_hfi_msg_error *error =
135*4882a593Smuzhiyun (struct a6xx_hfi_msg_error *) &resp;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev, "GMU firmware error %d\n",
138*4882a593Smuzhiyun error->code);
139*4882a593Smuzhiyun continue;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun if (seqnum != HFI_HEADER_SEQNUM(resp.ret_header)) {
143*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev,
144*4882a593Smuzhiyun "Unexpected message id %d on the response queue\n",
145*4882a593Smuzhiyun HFI_HEADER_SEQNUM(resp.ret_header));
146*4882a593Smuzhiyun continue;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (resp.error) {
150*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev,
151*4882a593Smuzhiyun "Message %s id %d returned error %d\n",
152*4882a593Smuzhiyun a6xx_hfi_msg_id[id], seqnum, resp.error);
153*4882a593Smuzhiyun return -EINVAL;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* All is well, copy over the buffer */
157*4882a593Smuzhiyun if (payload && payload_size)
158*4882a593Smuzhiyun memcpy(payload, resp.payload,
159*4882a593Smuzhiyun min_t(u32, payload_size, sizeof(resp.payload)));
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun return 0;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
a6xx_hfi_send_msg(struct a6xx_gmu * gmu,int id,void * data,u32 size,u32 * payload,u32 payload_size)165*4882a593Smuzhiyun static int a6xx_hfi_send_msg(struct a6xx_gmu *gmu, int id,
166*4882a593Smuzhiyun void *data, u32 size, u32 *payload, u32 payload_size)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun struct a6xx_hfi_queue *queue = &gmu->queues[HFI_COMMAND_QUEUE];
169*4882a593Smuzhiyun int ret, dwords = size >> 2;
170*4882a593Smuzhiyun u32 seqnum;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun seqnum = atomic_inc_return(&queue->seqnum) % 0xfff;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* First dword of the message is the message header - fill it in */
175*4882a593Smuzhiyun *((u32 *) data) = (seqnum << 20) | (HFI_MSG_CMD << 16) |
176*4882a593Smuzhiyun (dwords << 8) | id;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun ret = a6xx_hfi_queue_write(gmu, queue, data, dwords);
179*4882a593Smuzhiyun if (ret) {
180*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev, "Unable to send message %s id %d\n",
181*4882a593Smuzhiyun a6xx_hfi_msg_id[id], seqnum);
182*4882a593Smuzhiyun return ret;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun return a6xx_hfi_wait_for_ack(gmu, id, seqnum, payload, payload_size);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
a6xx_hfi_send_gmu_init(struct a6xx_gmu * gmu,int boot_state)188*4882a593Smuzhiyun static int a6xx_hfi_send_gmu_init(struct a6xx_gmu *gmu, int boot_state)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun struct a6xx_hfi_msg_gmu_init_cmd msg = { 0 };
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun msg.dbg_buffer_addr = (u32) gmu->debug.iova;
193*4882a593Smuzhiyun msg.dbg_buffer_size = (u32) gmu->debug.size;
194*4882a593Smuzhiyun msg.boot_state = boot_state;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_INIT, &msg, sizeof(msg),
197*4882a593Smuzhiyun NULL, 0);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
a6xx_hfi_get_fw_version(struct a6xx_gmu * gmu,u32 * version)200*4882a593Smuzhiyun static int a6xx_hfi_get_fw_version(struct a6xx_gmu *gmu, u32 *version)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun struct a6xx_hfi_msg_fw_version msg = { 0 };
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /* Currently supporting version 1.1 */
205*4882a593Smuzhiyun msg.supported_version = (1 << 28) | (1 << 16);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_FW_VERSION, &msg, sizeof(msg),
208*4882a593Smuzhiyun version, sizeof(*version));
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
a6xx_hfi_send_perf_table_v1(struct a6xx_gmu * gmu)211*4882a593Smuzhiyun static int a6xx_hfi_send_perf_table_v1(struct a6xx_gmu *gmu)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun struct a6xx_hfi_msg_perf_table_v1 msg = { 0 };
214*4882a593Smuzhiyun int i;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun msg.num_gpu_levels = gmu->nr_gpu_freqs;
217*4882a593Smuzhiyun msg.num_gmu_levels = gmu->nr_gmu_freqs;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun for (i = 0; i < gmu->nr_gpu_freqs; i++) {
220*4882a593Smuzhiyun msg.gx_votes[i].vote = gmu->gx_arc_votes[i];
221*4882a593Smuzhiyun msg.gx_votes[i].freq = gmu->gpu_freqs[i] / 1000;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun for (i = 0; i < gmu->nr_gmu_freqs; i++) {
225*4882a593Smuzhiyun msg.cx_votes[i].vote = gmu->cx_arc_votes[i];
226*4882a593Smuzhiyun msg.cx_votes[i].freq = gmu->gmu_freqs[i] / 1000;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_PERF_TABLE, &msg, sizeof(msg),
230*4882a593Smuzhiyun NULL, 0);
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
a6xx_hfi_send_perf_table(struct a6xx_gmu * gmu)233*4882a593Smuzhiyun static int a6xx_hfi_send_perf_table(struct a6xx_gmu *gmu)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun struct a6xx_hfi_msg_perf_table msg = { 0 };
236*4882a593Smuzhiyun int i;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun msg.num_gpu_levels = gmu->nr_gpu_freqs;
239*4882a593Smuzhiyun msg.num_gmu_levels = gmu->nr_gmu_freqs;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun for (i = 0; i < gmu->nr_gpu_freqs; i++) {
242*4882a593Smuzhiyun msg.gx_votes[i].vote = gmu->gx_arc_votes[i];
243*4882a593Smuzhiyun msg.gx_votes[i].acd = 0xffffffff;
244*4882a593Smuzhiyun msg.gx_votes[i].freq = gmu->gpu_freqs[i] / 1000;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun for (i = 0; i < gmu->nr_gmu_freqs; i++) {
248*4882a593Smuzhiyun msg.cx_votes[i].vote = gmu->cx_arc_votes[i];
249*4882a593Smuzhiyun msg.cx_votes[i].freq = gmu->gmu_freqs[i] / 1000;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_PERF_TABLE, &msg, sizeof(msg),
253*4882a593Smuzhiyun NULL, 0);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
a618_build_bw_table(struct a6xx_hfi_msg_bw_table * msg)256*4882a593Smuzhiyun static void a618_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun /* Send a single "off" entry since the 618 GMU doesn't do bus scaling */
259*4882a593Smuzhiyun msg->bw_level_num = 1;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun msg->ddr_cmds_num = 3;
262*4882a593Smuzhiyun msg->ddr_wait_bitmask = 0x01;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun msg->ddr_cmds_addrs[0] = 0x50000;
265*4882a593Smuzhiyun msg->ddr_cmds_addrs[1] = 0x5003c;
266*4882a593Smuzhiyun msg->ddr_cmds_addrs[2] = 0x5000c;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun msg->ddr_cmds_data[0][0] = 0x40000000;
269*4882a593Smuzhiyun msg->ddr_cmds_data[0][1] = 0x40000000;
270*4882a593Smuzhiyun msg->ddr_cmds_data[0][2] = 0x40000000;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun * These are the CX (CNOC) votes - these are used by the GMU but the
274*4882a593Smuzhiyun * votes are known and fixed for the target
275*4882a593Smuzhiyun */
276*4882a593Smuzhiyun msg->cnoc_cmds_num = 1;
277*4882a593Smuzhiyun msg->cnoc_wait_bitmask = 0x01;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun msg->cnoc_cmds_addrs[0] = 0x5007c;
280*4882a593Smuzhiyun msg->cnoc_cmds_data[0][0] = 0x40000000;
281*4882a593Smuzhiyun msg->cnoc_cmds_data[1][0] = 0x60000001;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
a640_build_bw_table(struct a6xx_hfi_msg_bw_table * msg)284*4882a593Smuzhiyun static void a640_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun /*
287*4882a593Smuzhiyun * Send a single "off" entry just to get things running
288*4882a593Smuzhiyun * TODO: bus scaling
289*4882a593Smuzhiyun */
290*4882a593Smuzhiyun msg->bw_level_num = 1;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun msg->ddr_cmds_num = 3;
293*4882a593Smuzhiyun msg->ddr_wait_bitmask = 0x01;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun msg->ddr_cmds_addrs[0] = 0x50000;
296*4882a593Smuzhiyun msg->ddr_cmds_addrs[1] = 0x5003c;
297*4882a593Smuzhiyun msg->ddr_cmds_addrs[2] = 0x5000c;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun msg->ddr_cmds_data[0][0] = 0x40000000;
300*4882a593Smuzhiyun msg->ddr_cmds_data[0][1] = 0x40000000;
301*4882a593Smuzhiyun msg->ddr_cmds_data[0][2] = 0x40000000;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /*
304*4882a593Smuzhiyun * These are the CX (CNOC) votes - these are used by the GMU but the
305*4882a593Smuzhiyun * votes are known and fixed for the target
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun msg->cnoc_cmds_num = 3;
308*4882a593Smuzhiyun msg->cnoc_wait_bitmask = 0x01;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun msg->cnoc_cmds_addrs[0] = 0x50034;
311*4882a593Smuzhiyun msg->cnoc_cmds_addrs[1] = 0x5007c;
312*4882a593Smuzhiyun msg->cnoc_cmds_addrs[2] = 0x5004c;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun msg->cnoc_cmds_data[0][0] = 0x40000000;
315*4882a593Smuzhiyun msg->cnoc_cmds_data[0][1] = 0x00000000;
316*4882a593Smuzhiyun msg->cnoc_cmds_data[0][2] = 0x40000000;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun msg->cnoc_cmds_data[1][0] = 0x60000001;
319*4882a593Smuzhiyun msg->cnoc_cmds_data[1][1] = 0x20000001;
320*4882a593Smuzhiyun msg->cnoc_cmds_data[1][2] = 0x60000001;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
a650_build_bw_table(struct a6xx_hfi_msg_bw_table * msg)323*4882a593Smuzhiyun static void a650_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun /*
326*4882a593Smuzhiyun * Send a single "off" entry just to get things running
327*4882a593Smuzhiyun * TODO: bus scaling
328*4882a593Smuzhiyun */
329*4882a593Smuzhiyun msg->bw_level_num = 1;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun msg->ddr_cmds_num = 3;
332*4882a593Smuzhiyun msg->ddr_wait_bitmask = 0x01;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun msg->ddr_cmds_addrs[0] = 0x50000;
335*4882a593Smuzhiyun msg->ddr_cmds_addrs[1] = 0x50004;
336*4882a593Smuzhiyun msg->ddr_cmds_addrs[2] = 0x5007c;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun msg->ddr_cmds_data[0][0] = 0x40000000;
339*4882a593Smuzhiyun msg->ddr_cmds_data[0][1] = 0x40000000;
340*4882a593Smuzhiyun msg->ddr_cmds_data[0][2] = 0x40000000;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /*
343*4882a593Smuzhiyun * These are the CX (CNOC) votes - these are used by the GMU but the
344*4882a593Smuzhiyun * votes are known and fixed for the target
345*4882a593Smuzhiyun */
346*4882a593Smuzhiyun msg->cnoc_cmds_num = 1;
347*4882a593Smuzhiyun msg->cnoc_wait_bitmask = 0x01;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun msg->cnoc_cmds_addrs[0] = 0x500a4;
350*4882a593Smuzhiyun msg->cnoc_cmds_data[0][0] = 0x40000000;
351*4882a593Smuzhiyun msg->cnoc_cmds_data[1][0] = 0x60000001;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
a6xx_build_bw_table(struct a6xx_hfi_msg_bw_table * msg)354*4882a593Smuzhiyun static void a6xx_build_bw_table(struct a6xx_hfi_msg_bw_table *msg)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun /* Send a single "off" entry since the 630 GMU doesn't do bus scaling */
357*4882a593Smuzhiyun msg->bw_level_num = 1;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun msg->ddr_cmds_num = 3;
360*4882a593Smuzhiyun msg->ddr_wait_bitmask = 0x07;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun msg->ddr_cmds_addrs[0] = 0x50000;
363*4882a593Smuzhiyun msg->ddr_cmds_addrs[1] = 0x5005c;
364*4882a593Smuzhiyun msg->ddr_cmds_addrs[2] = 0x5000c;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun msg->ddr_cmds_data[0][0] = 0x40000000;
367*4882a593Smuzhiyun msg->ddr_cmds_data[0][1] = 0x40000000;
368*4882a593Smuzhiyun msg->ddr_cmds_data[0][2] = 0x40000000;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun * These are the CX (CNOC) votes. This is used but the values for the
372*4882a593Smuzhiyun * sdm845 GMU are known and fixed so we can hard code them.
373*4882a593Smuzhiyun */
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun msg->cnoc_cmds_num = 3;
376*4882a593Smuzhiyun msg->cnoc_wait_bitmask = 0x05;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun msg->cnoc_cmds_addrs[0] = 0x50034;
379*4882a593Smuzhiyun msg->cnoc_cmds_addrs[1] = 0x5007c;
380*4882a593Smuzhiyun msg->cnoc_cmds_addrs[2] = 0x5004c;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun msg->cnoc_cmds_data[0][0] = 0x40000000;
383*4882a593Smuzhiyun msg->cnoc_cmds_data[0][1] = 0x00000000;
384*4882a593Smuzhiyun msg->cnoc_cmds_data[0][2] = 0x40000000;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun msg->cnoc_cmds_data[1][0] = 0x60000001;
387*4882a593Smuzhiyun msg->cnoc_cmds_data[1][1] = 0x20000001;
388*4882a593Smuzhiyun msg->cnoc_cmds_data[1][2] = 0x60000001;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun
a6xx_hfi_send_bw_table(struct a6xx_gmu * gmu)392*4882a593Smuzhiyun static int a6xx_hfi_send_bw_table(struct a6xx_gmu *gmu)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun struct a6xx_hfi_msg_bw_table msg = { 0 };
395*4882a593Smuzhiyun struct a6xx_gpu *a6xx_gpu = container_of(gmu, struct a6xx_gpu, gmu);
396*4882a593Smuzhiyun struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun if (adreno_is_a618(adreno_gpu))
399*4882a593Smuzhiyun a618_build_bw_table(&msg);
400*4882a593Smuzhiyun else if (adreno_is_a640(adreno_gpu))
401*4882a593Smuzhiyun a640_build_bw_table(&msg);
402*4882a593Smuzhiyun else if (adreno_is_a650(adreno_gpu))
403*4882a593Smuzhiyun a650_build_bw_table(&msg);
404*4882a593Smuzhiyun else
405*4882a593Smuzhiyun a6xx_build_bw_table(&msg);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_BW_TABLE, &msg, sizeof(msg),
408*4882a593Smuzhiyun NULL, 0);
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun
a6xx_hfi_send_test(struct a6xx_gmu * gmu)411*4882a593Smuzhiyun static int a6xx_hfi_send_test(struct a6xx_gmu *gmu)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun struct a6xx_hfi_msg_test msg = { 0 };
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_TEST, &msg, sizeof(msg),
416*4882a593Smuzhiyun NULL, 0);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
a6xx_hfi_send_start(struct a6xx_gmu * gmu)419*4882a593Smuzhiyun static int a6xx_hfi_send_start(struct a6xx_gmu *gmu)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun struct a6xx_hfi_msg_start msg = { 0 };
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_START, &msg, sizeof(msg),
424*4882a593Smuzhiyun NULL, 0);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
a6xx_hfi_send_core_fw_start(struct a6xx_gmu * gmu)427*4882a593Smuzhiyun static int a6xx_hfi_send_core_fw_start(struct a6xx_gmu *gmu)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct a6xx_hfi_msg_core_fw_start msg = { 0 };
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_CORE_FW_START, &msg,
432*4882a593Smuzhiyun sizeof(msg), NULL, 0);
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
a6xx_hfi_set_freq(struct a6xx_gmu * gmu,int index)435*4882a593Smuzhiyun int a6xx_hfi_set_freq(struct a6xx_gmu *gmu, int index)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun struct a6xx_hfi_gx_bw_perf_vote_cmd msg = { 0 };
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun msg.ack_type = 1; /* blocking */
440*4882a593Smuzhiyun msg.freq = index;
441*4882a593Smuzhiyun msg.bw = 0; /* TODO: bus scaling */
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_GX_BW_PERF_VOTE, &msg,
444*4882a593Smuzhiyun sizeof(msg), NULL, 0);
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
a6xx_hfi_send_prep_slumber(struct a6xx_gmu * gmu)447*4882a593Smuzhiyun int a6xx_hfi_send_prep_slumber(struct a6xx_gmu *gmu)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun struct a6xx_hfi_prep_slumber_cmd msg = { 0 };
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /* TODO: should freq and bw fields be non-zero ? */
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun return a6xx_hfi_send_msg(gmu, HFI_H2F_MSG_PREPARE_SLUMBER, &msg,
454*4882a593Smuzhiyun sizeof(msg), NULL, 0);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
a6xx_hfi_start_v1(struct a6xx_gmu * gmu,int boot_state)457*4882a593Smuzhiyun static int a6xx_hfi_start_v1(struct a6xx_gmu *gmu, int boot_state)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun int ret;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun ret = a6xx_hfi_send_gmu_init(gmu, boot_state);
462*4882a593Smuzhiyun if (ret)
463*4882a593Smuzhiyun return ret;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun ret = a6xx_hfi_get_fw_version(gmu, NULL);
466*4882a593Smuzhiyun if (ret)
467*4882a593Smuzhiyun return ret;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /*
470*4882a593Smuzhiyun * We have to get exchange version numbers per the sequence but at this
471*4882a593Smuzhiyun * point th kernel driver doesn't need to know the exact version of
472*4882a593Smuzhiyun * the GMU firmware
473*4882a593Smuzhiyun */
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun ret = a6xx_hfi_send_perf_table_v1(gmu);
476*4882a593Smuzhiyun if (ret)
477*4882a593Smuzhiyun return ret;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun ret = a6xx_hfi_send_bw_table(gmu);
480*4882a593Smuzhiyun if (ret)
481*4882a593Smuzhiyun return ret;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /*
484*4882a593Smuzhiyun * Let the GMU know that there won't be any more HFI messages until next
485*4882a593Smuzhiyun * boot
486*4882a593Smuzhiyun */
487*4882a593Smuzhiyun a6xx_hfi_send_test(gmu);
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun return 0;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
a6xx_hfi_start(struct a6xx_gmu * gmu,int boot_state)492*4882a593Smuzhiyun int a6xx_hfi_start(struct a6xx_gmu *gmu, int boot_state)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun int ret;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun if (gmu->legacy)
497*4882a593Smuzhiyun return a6xx_hfi_start_v1(gmu, boot_state);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun ret = a6xx_hfi_send_perf_table(gmu);
501*4882a593Smuzhiyun if (ret)
502*4882a593Smuzhiyun return ret;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun ret = a6xx_hfi_send_bw_table(gmu);
505*4882a593Smuzhiyun if (ret)
506*4882a593Smuzhiyun return ret;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun ret = a6xx_hfi_send_core_fw_start(gmu);
509*4882a593Smuzhiyun if (ret)
510*4882a593Smuzhiyun return ret;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun /*
513*4882a593Smuzhiyun * Downstream driver sends this in its "a6xx_hw_init" equivalent,
514*4882a593Smuzhiyun * but seems to be no harm in sending it here
515*4882a593Smuzhiyun */
516*4882a593Smuzhiyun ret = a6xx_hfi_send_start(gmu);
517*4882a593Smuzhiyun if (ret)
518*4882a593Smuzhiyun return ret;
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun return 0;
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
a6xx_hfi_stop(struct a6xx_gmu * gmu)523*4882a593Smuzhiyun void a6xx_hfi_stop(struct a6xx_gmu *gmu)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun int i;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(gmu->queues); i++) {
528*4882a593Smuzhiyun struct a6xx_hfi_queue *queue = &gmu->queues[i];
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun if (!queue->header)
531*4882a593Smuzhiyun continue;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun if (queue->header->read_index != queue->header->write_index)
534*4882a593Smuzhiyun DRM_DEV_ERROR(gmu->dev, "HFI queue %d is not empty\n", i);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun queue->header->read_index = 0;
537*4882a593Smuzhiyun queue->header->write_index = 0;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun
a6xx_hfi_queue_init(struct a6xx_hfi_queue * queue,struct a6xx_hfi_queue_header * header,void * virt,u64 iova,u32 id)541*4882a593Smuzhiyun static void a6xx_hfi_queue_init(struct a6xx_hfi_queue *queue,
542*4882a593Smuzhiyun struct a6xx_hfi_queue_header *header, void *virt, u64 iova,
543*4882a593Smuzhiyun u32 id)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun spin_lock_init(&queue->lock);
546*4882a593Smuzhiyun queue->header = header;
547*4882a593Smuzhiyun queue->data = virt;
548*4882a593Smuzhiyun atomic_set(&queue->seqnum, 0);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /* Set up the shared memory header */
551*4882a593Smuzhiyun header->iova = iova;
552*4882a593Smuzhiyun header->type = 10 << 8 | id;
553*4882a593Smuzhiyun header->status = 1;
554*4882a593Smuzhiyun header->size = SZ_4K >> 2;
555*4882a593Smuzhiyun header->msg_size = 0;
556*4882a593Smuzhiyun header->dropped = 0;
557*4882a593Smuzhiyun header->rx_watermark = 1;
558*4882a593Smuzhiyun header->tx_watermark = 1;
559*4882a593Smuzhiyun header->rx_request = 1;
560*4882a593Smuzhiyun header->tx_request = 0;
561*4882a593Smuzhiyun header->read_index = 0;
562*4882a593Smuzhiyun header->write_index = 0;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
a6xx_hfi_init(struct a6xx_gmu * gmu)565*4882a593Smuzhiyun void a6xx_hfi_init(struct a6xx_gmu *gmu)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun struct a6xx_gmu_bo *hfi = &gmu->hfi;
568*4882a593Smuzhiyun struct a6xx_hfi_queue_table_header *table = hfi->virt;
569*4882a593Smuzhiyun struct a6xx_hfi_queue_header *headers = hfi->virt + sizeof(*table);
570*4882a593Smuzhiyun u64 offset;
571*4882a593Smuzhiyun int table_size;
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun /*
574*4882a593Smuzhiyun * The table size is the size of the table header plus all of the queue
575*4882a593Smuzhiyun * headers
576*4882a593Smuzhiyun */
577*4882a593Smuzhiyun table_size = sizeof(*table);
578*4882a593Smuzhiyun table_size += (ARRAY_SIZE(gmu->queues) *
579*4882a593Smuzhiyun sizeof(struct a6xx_hfi_queue_header));
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun table->version = 0;
582*4882a593Smuzhiyun table->size = table_size;
583*4882a593Smuzhiyun /* First queue header is located immediately after the table header */
584*4882a593Smuzhiyun table->qhdr0_offset = sizeof(*table) >> 2;
585*4882a593Smuzhiyun table->qhdr_size = sizeof(struct a6xx_hfi_queue_header) >> 2;
586*4882a593Smuzhiyun table->num_queues = ARRAY_SIZE(gmu->queues);
587*4882a593Smuzhiyun table->active_queues = ARRAY_SIZE(gmu->queues);
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /* Command queue */
590*4882a593Smuzhiyun offset = SZ_4K;
591*4882a593Smuzhiyun a6xx_hfi_queue_init(&gmu->queues[0], &headers[0], hfi->virt + offset,
592*4882a593Smuzhiyun hfi->iova + offset, 0);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun /* GMU response queue */
595*4882a593Smuzhiyun offset += SZ_4K;
596*4882a593Smuzhiyun a6xx_hfi_queue_init(&gmu->queues[1], &headers[1], hfi->virt + offset,
597*4882a593Smuzhiyun hfi->iova + offset, gmu->legacy ? 4 : 1);
598*4882a593Smuzhiyun }
599