xref: /openbmc/linux/sound/soc/sof/ops.h (revision cf9441ad)
1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
2 /*
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * Copyright(c) 2018 Intel Corporation. All rights reserved.
7  *
8  * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9  */
10 
11 #ifndef __SOUND_SOC_SOF_IO_H
12 #define __SOUND_SOC_SOF_IO_H
13 
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <sound/pcm.h>
19 #include "sof-priv.h"
20 
21 #define sof_ops(sdev) \
22 	((sdev)->pdata->desc->ops)
23 
24 /* Mandatory operations are verified during probing */
25 
26 /* init */
27 static inline int snd_sof_probe(struct snd_sof_dev *sdev)
28 {
29 	return sof_ops(sdev)->probe(sdev);
30 }
31 
32 static inline int snd_sof_remove(struct snd_sof_dev *sdev)
33 {
34 	if (sof_ops(sdev)->remove)
35 		return sof_ops(sdev)->remove(sdev);
36 
37 	return 0;
38 }
39 
40 /* control */
41 
42 /*
43  * snd_sof_dsp_run returns the core mask of the cores that are available
44  * after successful fw boot
45  */
46 static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
47 {
48 	return sof_ops(sdev)->run(sdev);
49 }
50 
51 static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev)
52 {
53 	if (sof_ops(sdev)->stall)
54 		return sof_ops(sdev)->stall(sdev);
55 
56 	return 0;
57 }
58 
59 static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
60 {
61 	if (sof_ops(sdev)->reset)
62 		return sof_ops(sdev)->reset(sdev);
63 
64 	return 0;
65 }
66 
67 /* dsp core power up/power down */
68 static inline int snd_sof_dsp_core_power_up(struct snd_sof_dev *sdev,
69 					    unsigned int core_mask)
70 {
71 	if (sof_ops(sdev)->core_power_up)
72 		return sof_ops(sdev)->core_power_up(sdev, core_mask);
73 
74 	return 0;
75 }
76 
77 static inline int snd_sof_dsp_core_power_down(struct snd_sof_dev *sdev,
78 					      unsigned int core_mask)
79 {
80 	if (sof_ops(sdev)->core_power_down)
81 		return sof_ops(sdev)->core_power_down(sdev, core_mask);
82 
83 	return 0;
84 }
85 
86 /* pre/post fw load */
87 static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
88 {
89 	if (sof_ops(sdev)->pre_fw_run)
90 		return sof_ops(sdev)->pre_fw_run(sdev);
91 
92 	return 0;
93 }
94 
95 static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
96 {
97 	if (sof_ops(sdev)->post_fw_run)
98 		return sof_ops(sdev)->post_fw_run(sdev);
99 
100 	return 0;
101 }
102 
103 /* misc */
104 
105 /**
106  * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
107  *
108  * @sdev: sof device
109  * @type: section type as described by snd_sof_fw_blk_type
110  *
111  * Returns the corresponding BAR index (a positive integer) or -EINVAL
112  * in case there is no mapping
113  */
114 static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
115 {
116 	if (sof_ops(sdev)->get_bar_index)
117 		return sof_ops(sdev)->get_bar_index(sdev, type);
118 
119 	return sdev->mmio_bar;
120 }
121 
122 /* power management */
123 static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
124 {
125 	if (sof_ops(sdev)->resume)
126 		return sof_ops(sdev)->resume(sdev);
127 
128 	return 0;
129 }
130 
131 static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev)
132 {
133 	if (sof_ops(sdev)->suspend)
134 		return sof_ops(sdev)->suspend(sdev);
135 
136 	return 0;
137 }
138 
139 static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
140 {
141 	if (sof_ops(sdev)->runtime_resume)
142 		return sof_ops(sdev)->runtime_resume(sdev);
143 
144 	return 0;
145 }
146 
147 static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
148 {
149 	if (sof_ops(sdev)->runtime_suspend)
150 		return sof_ops(sdev)->runtime_suspend(sdev);
151 
152 	return 0;
153 }
154 
155 static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
156 {
157 	if (sof_ops(sdev)->runtime_idle)
158 		return sof_ops(sdev)->runtime_idle(sdev);
159 
160 	return 0;
161 }
162 
163 static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
164 {
165 	if (sof_ops(sdev)->set_hw_params_upon_resume)
166 		return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
167 	return 0;
168 }
169 
170 static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
171 {
172 	if (sof_ops(sdev)->set_clk)
173 		return sof_ops(sdev)->set_clk(sdev, freq);
174 
175 	return 0;
176 }
177 
178 /* debug */
179 static inline void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, u32 flags)
180 {
181 	if (sof_ops(sdev)->dbg_dump)
182 		return sof_ops(sdev)->dbg_dump(sdev, flags);
183 }
184 
185 static inline void snd_sof_ipc_dump(struct snd_sof_dev *sdev)
186 {
187 	if (sof_ops(sdev)->ipc_dump)
188 		return sof_ops(sdev)->ipc_dump(sdev);
189 }
190 
191 /* register IO */
192 static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
193 				     u32 offset, u32 value)
194 {
195 	if (sof_ops(sdev)->write) {
196 		sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
197 		return;
198 	}
199 
200 	dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
201 }
202 
203 static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
204 				       u32 offset, u64 value)
205 {
206 	if (sof_ops(sdev)->write64) {
207 		sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
208 		return;
209 	}
210 
211 	dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
212 }
213 
214 static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
215 				   u32 offset)
216 {
217 	if (sof_ops(sdev)->read)
218 		return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
219 
220 	dev_err(sdev->dev, "error: %s not defined\n", __func__);
221 	return -ENOTSUPP;
222 }
223 
224 static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
225 				     u32 offset)
226 {
227 	if (sof_ops(sdev)->read64)
228 		return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
229 
230 	dev_err(sdev->dev, "error: %s not defined\n", __func__);
231 	return -ENOTSUPP;
232 }
233 
234 /* block IO */
235 static inline void snd_sof_dsp_block_read(struct snd_sof_dev *sdev, u32 bar,
236 					  u32 offset, void *dest, size_t bytes)
237 {
238 	sof_ops(sdev)->block_read(sdev, bar, offset, dest, bytes);
239 }
240 
241 static inline void snd_sof_dsp_block_write(struct snd_sof_dev *sdev, u32 bar,
242 					   u32 offset, void *src, size_t bytes)
243 {
244 	sof_ops(sdev)->block_write(sdev, bar, offset, src, bytes);
245 }
246 
247 /* ipc */
248 static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
249 				       struct snd_sof_ipc_msg *msg)
250 {
251 	return sof_ops(sdev)->send_msg(sdev, msg);
252 }
253 
254 /* host DMA trace */
255 static inline int snd_sof_dma_trace_init(struct snd_sof_dev *sdev,
256 					 u32 *stream_tag)
257 {
258 	if (sof_ops(sdev)->trace_init)
259 		return sof_ops(sdev)->trace_init(sdev, stream_tag);
260 
261 	return 0;
262 }
263 
264 static inline int snd_sof_dma_trace_release(struct snd_sof_dev *sdev)
265 {
266 	if (sof_ops(sdev)->trace_release)
267 		return sof_ops(sdev)->trace_release(sdev);
268 
269 	return 0;
270 }
271 
272 static inline int snd_sof_dma_trace_trigger(struct snd_sof_dev *sdev, int cmd)
273 {
274 	if (sof_ops(sdev)->trace_trigger)
275 		return sof_ops(sdev)->trace_trigger(sdev, cmd);
276 
277 	return 0;
278 }
279 
280 /* host PCM ops */
281 static inline int
282 snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
283 			  struct snd_pcm_substream *substream)
284 {
285 	if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
286 		return sof_ops(sdev)->pcm_open(sdev, substream);
287 
288 	return 0;
289 }
290 
291 /* disconnect pcm substream to a host stream */
292 static inline int
293 snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
294 			   struct snd_pcm_substream *substream)
295 {
296 	if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
297 		return sof_ops(sdev)->pcm_close(sdev, substream);
298 
299 	return 0;
300 }
301 
302 /* host stream hw params */
303 static inline int
304 snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
305 			       struct snd_pcm_substream *substream,
306 			       struct snd_pcm_hw_params *params,
307 			       struct sof_ipc_stream_params *ipc_params)
308 {
309 	if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
310 		return sof_ops(sdev)->pcm_hw_params(sdev, substream,
311 						    params, ipc_params);
312 
313 	return 0;
314 }
315 
316 /* host stream hw free */
317 static inline int
318 snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
319 			     struct snd_pcm_substream *substream)
320 {
321 	if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
322 		return sof_ops(sdev)->pcm_hw_free(sdev, substream);
323 
324 	return 0;
325 }
326 
327 /* host stream trigger */
328 static inline int
329 snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
330 			     struct snd_pcm_substream *substream, int cmd)
331 {
332 	if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
333 		return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
334 
335 	return 0;
336 }
337 
338 /* host DSP message data */
339 static inline void snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
340 					struct snd_pcm_substream *substream,
341 					void *p, size_t sz)
342 {
343 	sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
344 }
345 
346 /* host configure DSP HW parameters */
347 static inline int
348 snd_sof_ipc_pcm_params(struct snd_sof_dev *sdev,
349 		       struct snd_pcm_substream *substream,
350 		       const struct sof_ipc_pcm_params_reply *reply)
351 {
352 	return sof_ops(sdev)->ipc_pcm_params(sdev, substream, reply);
353 }
354 
355 /* host stream pointer */
356 static inline snd_pcm_uframes_t
357 snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
358 			     struct snd_pcm_substream *substream)
359 {
360 	if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
361 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
362 
363 	return 0;
364 }
365 
366 static inline const struct snd_sof_dsp_ops
367 *sof_get_ops(const struct sof_dev_desc *d,
368 	     const struct sof_ops_table mach_ops[], int asize)
369 {
370 	int i;
371 
372 	for (i = 0; i < asize; i++) {
373 		if (d == mach_ops[i].desc)
374 			return mach_ops[i].ops;
375 	}
376 
377 	/* not found */
378 	return NULL;
379 }
380 
381 /**
382  * snd_sof_dsp_register_poll_timeout - Periodically poll an address
383  * until a condition is met or a timeout occurs
384  * @op: accessor function (takes @addr as its only argument)
385  * @addr: Address to poll
386  * @val: Variable to read the value into
387  * @cond: Break condition (usually involving @val)
388  * @sleep_us: Maximum time to sleep between reads in us (0
389  *            tight-loops).  Should be less than ~20ms since usleep_range
390  *            is used (see Documentation/timers/timers-howto.rst).
391  * @timeout_us: Timeout in us, 0 means never timeout
392  *
393  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
394  * case, the last read value at @addr is stored in @val. Must not
395  * be called from atomic context if sleep_us or timeout_us are used.
396  *
397  * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
398  */
399 #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
400 ({ \
401 	u64 __timeout_us = (timeout_us); \
402 	unsigned long __sleep_us = (sleep_us); \
403 	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
404 	might_sleep_if((__sleep_us) != 0); \
405 	for (;;) {							\
406 		(val) = snd_sof_dsp_read(sdev, bar, offset);		\
407 		if (cond) { \
408 			dev_dbg(sdev->dev, \
409 				"FW Poll Status: reg=%#x successful\n", (val)); \
410 			break; \
411 		} \
412 		if (__timeout_us && \
413 		    ktime_compare(ktime_get(), __timeout) > 0) { \
414 			(val) = snd_sof_dsp_read(sdev, bar, offset); \
415 			dev_dbg(sdev->dev, \
416 				"FW Poll Status: reg=%#x timedout\n", (val)); \
417 			break; \
418 		} \
419 		if (__sleep_us) \
420 			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
421 	} \
422 	(cond) ? 0 : -ETIMEDOUT; \
423 })
424 
425 /* This is for registers bits with attribute RWC */
426 bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
427 			     u32 mask, u32 value);
428 
429 bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
430 				      u32 offset, u32 mask, u32 value);
431 
432 bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
433 					u32 offset, u64 mask, u64 value);
434 
435 bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
436 			     u32 mask, u32 value);
437 
438 bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
439 			       u32 offset, u64 mask, u64 value);
440 
441 void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
442 				    u32 offset, u32 mask, u32 value);
443 
444 int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
445 			      u32 mask, u32 target, u32 timeout_ms,
446 			      u32 interval_us);
447 
448 void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset);
449 #endif
450