1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8
9 #include <linux/devcoredump.h>
10 #include <linux/slab.h>
11 #include "avs.h"
12 #include "messages.h"
13 #include "path.h"
14 #include "topology.h"
15
16 static int __maybe_unused
apl_enable_logs(struct avs_dev * adev,enum avs_log_enable enable,u32 aging_period,u32 fifo_full_period,unsigned long resource_mask,u32 * priorities)17 apl_enable_logs(struct avs_dev *adev, enum avs_log_enable enable, u32 aging_period,
18 u32 fifo_full_period, unsigned long resource_mask, u32 *priorities)
19 {
20 struct apl_log_state_info *info;
21 u32 size, num_cores = adev->hw_cfg.dsp_cores;
22 int ret, i;
23
24 if (fls_long(resource_mask) > num_cores)
25 return -EINVAL;
26 size = struct_size(info, logs_core, num_cores);
27 info = kzalloc(size, GFP_KERNEL);
28 if (!info)
29 return -ENOMEM;
30
31 info->aging_timer_period = aging_period;
32 info->fifo_full_timer_period = fifo_full_period;
33 info->core_mask = resource_mask;
34 if (enable)
35 for_each_set_bit(i, &resource_mask, num_cores) {
36 info->logs_core[i].enable = enable;
37 info->logs_core[i].min_priority = *priorities++;
38 }
39 else
40 for_each_set_bit(i, &resource_mask, num_cores)
41 info->logs_core[i].enable = enable;
42
43 ret = avs_ipc_set_enable_logs(adev, (u8 *)info, size);
44 kfree(info);
45 if (ret)
46 return AVS_IPC_RET(ret);
47
48 return 0;
49 }
50
apl_log_buffer_status(struct avs_dev * adev,union avs_notify_msg * msg)51 static int apl_log_buffer_status(struct avs_dev *adev, union avs_notify_msg *msg)
52 {
53 struct apl_log_buffer_layout layout;
54 void __iomem *addr, *buf;
55
56 addr = avs_log_buffer_addr(adev, msg->log.core);
57 if (!addr)
58 return -ENXIO;
59
60 memcpy_fromio(&layout, addr, sizeof(layout));
61
62 if (!avs_logging_fw(adev))
63 /* consume the logs regardless of consumer presence */
64 goto update_read_ptr;
65
66 buf = apl_log_payload_addr(addr);
67
68 if (layout.read_ptr > layout.write_ptr) {
69 avs_dump_fw_log(adev, buf + layout.read_ptr,
70 apl_log_payload_size(adev) - layout.read_ptr);
71 layout.read_ptr = 0;
72 }
73 avs_dump_fw_log_wakeup(adev, buf + layout.read_ptr, layout.write_ptr - layout.read_ptr);
74
75 update_read_ptr:
76 writel(layout.write_ptr, addr);
77 return 0;
78 }
79
apl_wait_log_entry(struct avs_dev * adev,u32 core,struct apl_log_buffer_layout * layout)80 static int apl_wait_log_entry(struct avs_dev *adev, u32 core, struct apl_log_buffer_layout *layout)
81 {
82 unsigned long timeout;
83 void __iomem *addr;
84
85 addr = avs_log_buffer_addr(adev, core);
86 if (!addr)
87 return -ENXIO;
88
89 timeout = jiffies + msecs_to_jiffies(10);
90
91 do {
92 memcpy_fromio(layout, addr, sizeof(*layout));
93 if (layout->read_ptr != layout->write_ptr)
94 return 0;
95 usleep_range(500, 1000);
96 } while (!time_after(jiffies, timeout));
97
98 return -ETIMEDOUT;
99 }
100
101 /* reads log header and tests its type */
102 #define apl_is_entry_stackdump(addr) ((readl(addr) >> 30) & 0x1)
103
apl_coredump(struct avs_dev * adev,union avs_notify_msg * msg)104 static int apl_coredump(struct avs_dev *adev, union avs_notify_msg *msg)
105 {
106 struct apl_log_buffer_layout layout;
107 void __iomem *addr, *buf;
108 size_t dump_size;
109 u16 offset = 0;
110 u8 *dump, *pos;
111
112 dump_size = AVS_FW_REGS_SIZE + msg->ext.coredump.stack_dump_size;
113 dump = vzalloc(dump_size);
114 if (!dump)
115 return -ENOMEM;
116
117 memcpy_fromio(dump, avs_sram_addr(adev, AVS_FW_REGS_WINDOW), AVS_FW_REGS_SIZE);
118
119 if (!msg->ext.coredump.stack_dump_size)
120 goto exit;
121
122 /* Dump the registers even if an external error prevents gathering the stack. */
123 addr = avs_log_buffer_addr(adev, msg->ext.coredump.core_id);
124 if (!addr)
125 goto exit;
126
127 buf = apl_log_payload_addr(addr);
128 memcpy_fromio(&layout, addr, sizeof(layout));
129 if (!apl_is_entry_stackdump(buf + layout.read_ptr)) {
130 union avs_notify_msg lbs_msg = AVS_NOTIFICATION(LOG_BUFFER_STATUS);
131
132 /*
133 * DSP awaits the remaining logs to be
134 * gathered before dumping stack
135 */
136 lbs_msg.log.core = msg->ext.coredump.core_id;
137 avs_log_buffer_status_locked(adev, &lbs_msg);
138 }
139
140 pos = dump + AVS_FW_REGS_SIZE;
141 /* gather the stack */
142 do {
143 u32 count;
144
145 if (apl_wait_log_entry(adev, msg->ext.coredump.core_id, &layout))
146 break;
147
148 if (layout.read_ptr > layout.write_ptr) {
149 count = apl_log_payload_size(adev) - layout.read_ptr;
150 memcpy_fromio(pos + offset, buf + layout.read_ptr, count);
151 layout.read_ptr = 0;
152 offset += count;
153 }
154 count = layout.write_ptr - layout.read_ptr;
155 memcpy_fromio(pos + offset, buf + layout.read_ptr, count);
156 offset += count;
157
158 /* update read pointer */
159 writel(layout.write_ptr, addr);
160 } while (offset < msg->ext.coredump.stack_dump_size);
161
162 exit:
163 dev_coredumpv(adev->dev, dump, dump_size, GFP_KERNEL);
164
165 return 0;
166 }
167
apl_lp_streaming(struct avs_dev * adev)168 static bool apl_lp_streaming(struct avs_dev *adev)
169 {
170 struct avs_path *path;
171
172 /* Any gateway without buffer allocated in LP area disqualifies D0IX. */
173 list_for_each_entry(path, &adev->path_list, node) {
174 struct avs_path_pipeline *ppl;
175
176 list_for_each_entry(ppl, &path->ppl_list, node) {
177 struct avs_path_module *mod;
178
179 list_for_each_entry(mod, &ppl->mod_list, node) {
180 struct avs_tplg_modcfg_ext *cfg;
181
182 cfg = mod->template->cfg_ext;
183
184 /* only copiers have gateway attributes */
185 if (!guid_equal(&cfg->type, &AVS_COPIER_MOD_UUID))
186 continue;
187 /* non-gateway copiers do not prevent PG */
188 if (cfg->copier.dma_type == INVALID_OBJECT_ID)
189 continue;
190
191 if (!mod->gtw_attrs.lp_buffer_alloc)
192 return false;
193 }
194 }
195 }
196
197 return true;
198 }
199
apl_d0ix_toggle(struct avs_dev * adev,struct avs_ipc_msg * tx,bool wake)200 static bool apl_d0ix_toggle(struct avs_dev *adev, struct avs_ipc_msg *tx, bool wake)
201 {
202 /* wake in all cases */
203 if (wake)
204 return true;
205
206 /*
207 * If no pipelines are running, allow for d0ix schedule.
208 * If all gateways have lp=1, allow for d0ix schedule.
209 * If any gateway with lp=0 is allocated, abort scheduling d0ix.
210 *
211 * Note: for cAVS 1.5+ and 1.8, D0IX is LP-firmware transition,
212 * not the power-gating mechanism known from cAVS 2.0.
213 */
214 return apl_lp_streaming(adev);
215 }
216
apl_set_d0ix(struct avs_dev * adev,bool enable)217 static int apl_set_d0ix(struct avs_dev *adev, bool enable)
218 {
219 bool streaming = false;
220 int ret;
221
222 if (enable)
223 /* Either idle or all gateways with lp=1. */
224 streaming = !list_empty(&adev->path_list);
225
226 ret = avs_ipc_set_d0ix(adev, enable, streaming);
227 return AVS_IPC_RET(ret);
228 }
229
230 const struct avs_dsp_ops apl_dsp_ops = {
231 .power = avs_dsp_core_power,
232 .reset = avs_dsp_core_reset,
233 .stall = avs_dsp_core_stall,
234 .irq_handler = avs_dsp_irq_handler,
235 .irq_thread = avs_dsp_irq_thread,
236 .int_control = avs_dsp_interrupt_control,
237 .load_basefw = avs_hda_load_basefw,
238 .load_lib = avs_hda_load_library,
239 .transfer_mods = avs_hda_transfer_modules,
240 .log_buffer_offset = skl_log_buffer_offset,
241 .log_buffer_status = apl_log_buffer_status,
242 .coredump = apl_coredump,
243 .d0ix_toggle = apl_d0ix_toggle,
244 .set_d0ix = apl_set_d0ix,
245 AVS_SET_ENABLE_LOGS_OP(apl)
246 };
247