projects/linux·blob master · refs

659 lines · 16.7 KiB

1/* SPDX-License-Identifier: (GPL-2.0-only 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
7 *
8 * Author: Liam Girdwood <[email protected]>
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
24static inline int sof_ops_init(struct snd_sof_dev *sdev)
25{
26 if (sdev->pdata->desc->ops_init)
27 return sdev->pdata->desc->ops_init(sdev);
28
29 return 0;
30}
31
32static inline void sof_ops_free(struct snd_sof_dev *sdev)
33{
34 if (sdev->pdata->desc->ops_free)
35 sdev->pdata->desc->ops_free(sdev);
36}
37
38/* Mandatory operations are verified during probing */
39
40/* init */
41static inline int snd_sof_probe_early(struct snd_sof_dev *sdev)
42{
43 if (sof_ops(sdev)->probe_early)
44 return sof_ops(sdev)->probe_early(sdev);
45
46 return 0;
47}
48
49static inline int snd_sof_probe(struct snd_sof_dev *sdev)
50{
51 return sof_ops(sdev)->probe(sdev);
52}
53
54static inline void snd_sof_remove(struct snd_sof_dev *sdev)
55{
56 if (sof_ops(sdev)->remove)
57 sof_ops(sdev)->remove(sdev);
58}
59
60static inline void snd_sof_remove_late(struct snd_sof_dev *sdev)
61{
62 if (sof_ops(sdev)->remove_late)
63 sof_ops(sdev)->remove_late(sdev);
64}
65
66static inline int snd_sof_shutdown(struct snd_sof_dev *sdev)
67{
68 if (sof_ops(sdev)->shutdown)
69 return sof_ops(sdev)->shutdown(sdev);
70
71 return 0;
72}
73
74/* control */
75
76/*
77 * snd_sof_dsp_run returns the core mask of the cores that are available
78 * after successful fw boot
79 */
80static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
81{
82 return sof_ops(sdev)->run(sdev);
83}
84
85static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask)
86{
87 if (sof_ops(sdev)->stall)
88 return sof_ops(sdev)->stall(sdev, core_mask);
89
90 return 0;
91}
92
93static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
94{
95 if (sof_ops(sdev)->reset)
96 return sof_ops(sdev)->reset(sdev);
97
98 return 0;
99}
100
101/* dsp core get/put */
102static inline int snd_sof_dsp_core_get(struct snd_sof_dev *sdev, int core)
103{
104 if (core > sdev->num_cores - 1) {
105 dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
106 sdev->num_cores);
107 return -EINVAL;
108 }
109
110 if (sof_ops(sdev)->core_get) {
111 int ret;
112
113 /* if current ref_count is > 0, increment it and return */
114 if (sdev->dsp_core_ref_count[core] > 0) {
115 sdev->dsp_core_ref_count[core]++;
116 return 0;
117 }
118
119 /* power up the core */
120 ret = sof_ops(sdev)->core_get(sdev, core);
121 if (ret < 0)
122 return ret;
123
124 /* increment ref_count */
125 sdev->dsp_core_ref_count[core]++;
126
127 /* and update enabled_cores_mask */
128 sdev->enabled_cores_mask |= BIT(core);
129
130 dev_dbg(sdev->dev, "Core %d powered up\n", core);
131 }
132
133 return 0;
134}
135
136static inline int snd_sof_dsp_core_put(struct snd_sof_dev *sdev, int core)
137{
138 if (core > sdev->num_cores - 1) {
139 dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
140 sdev->num_cores);
141 return -EINVAL;
142 }
143
144 if (sof_ops(sdev)->core_put) {
145 int ret;
146
147 /* decrement ref_count and return if it is > 0 */
148 if (--(sdev->dsp_core_ref_count[core]) > 0)
149 return 0;
150
151 /* power down the core */
152 ret = sof_ops(sdev)->core_put(sdev, core);
153 if (ret < 0)
154 return ret;
155
156 /* and update enabled_cores_mask */
157 sdev->enabled_cores_mask &= ~BIT(core);
158
159 dev_dbg(sdev->dev, "Core %d powered down\n", core);
160 }
161
162 return 0;
163}
164
165/* pre/post fw load */
166static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
167{
168 if (sof_ops(sdev)->pre_fw_run)
169 return sof_ops(sdev)->pre_fw_run(sdev);
170
171 return 0;
172}
173
174static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
175{
176 if (sof_ops(sdev)->post_fw_run)
177 return sof_ops(sdev)->post_fw_run(sdev);
178
179 return 0;
180}
181
182/* parse platform specific extended manifest */
183static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev,
184 const struct sof_ext_man_elem_header *hdr)
185{
186 if (sof_ops(sdev)->parse_platform_ext_manifest)
187 return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr);
188
189 return 0;
190}
191
192/* misc */
193
194/**
195 * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
196 *
197 * @sdev: sof device
198 * @type: section type as described by snd_sof_fw_blk_type
199 *
200 * Returns the corresponding BAR index (a positive integer) or -EINVAL
201 * in case there is no mapping
202 */
203static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
204{
205 if (sof_ops(sdev)->get_bar_index)
206 return sof_ops(sdev)->get_bar_index(sdev, type);
207
208 return sdev->mmio_bar;
209}
210
211static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
212{
213 if (sof_ops(sdev)->get_mailbox_offset)
214 return sof_ops(sdev)->get_mailbox_offset(sdev);
215
216 dev_err(sdev->dev, "error: %s not defined\n", __func__);
217 return -EOPNOTSUPP;
218}
219
220static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
221 u32 id)
222{
223 if (sof_ops(sdev)->get_window_offset)
224 return sof_ops(sdev)->get_window_offset(sdev, id);
225
226 dev_err(sdev->dev, "error: %s not defined\n", __func__);
227 return -EOPNOTSUPP;
228}
229/* power management */
230static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
231{
232 if (sof_ops(sdev)->resume)
233 return sof_ops(sdev)->resume(sdev);
234
235 return 0;
236}
237
238static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
239 u32 target_state)
240{
241 if (sof_ops(sdev)->suspend)
242 return sof_ops(sdev)->suspend(sdev, target_state);
243
244 return 0;
245}
246
247static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
248{
249 if (sof_ops(sdev)->runtime_resume)
250 return sof_ops(sdev)->runtime_resume(sdev);
251
252 return 0;
253}
254
255static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
256{
257 if (sof_ops(sdev)->runtime_suspend)
258 return sof_ops(sdev)->runtime_suspend(sdev);
259
260 return 0;
261}
262
263static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
264{
265 if (sof_ops(sdev)->runtime_idle)
266 return sof_ops(sdev)->runtime_idle(sdev);
267
268 return 0;
269}
270
271static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
272{
273 if (sof_ops(sdev)->set_hw_params_upon_resume)
274 return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
275 return 0;
276}
277
278static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
279{
280 if (sof_ops(sdev)->set_clk)
281 return sof_ops(sdev)->set_clk(sdev, freq);
282
283 return 0;
284}
285
286static inline int
287snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
288 const struct sof_dsp_power_state *target_state)
289{
290 guard(mutex)(&sdev->power_state_access);
291
292 if (sof_ops(sdev)->set_power_state)
293 return sof_ops(sdev)->set_power_state(sdev, target_state);
294
295 return 0;
296}
297
298/* debug */
299void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, const char *msg, u32 flags);
300
301static inline int snd_sof_debugfs_add_region_item(struct snd_sof_dev *sdev,
302 enum snd_sof_fw_blk_type blk_type, u32 offset, size_t size,
303 const char *name, enum sof_debugfs_access_type access_type)
304{
305 if (sof_ops(sdev) && sof_ops(sdev)->debugfs_add_region_item)
306 return sof_ops(sdev)->debugfs_add_region_item(sdev, blk_type, offset,
307 size, name, access_type);
308
309 return 0;
310}
311
312/* register IO */
313static inline void snd_sof_dsp_write8(struct snd_sof_dev *sdev, u32 bar,
314 u32 offset, u8 value)
315{
316 if (sof_ops(sdev)->write8)
317 sof_ops(sdev)->write8(sdev, sdev->bar[bar] + offset, value);
318 else
319 writeb(value, sdev->bar[bar] + offset);
320}
321
322static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
323 u32 offset, u32 value)
324{
325 if (sof_ops(sdev)->write)
326 sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
327 else
328 writel(value, sdev->bar[bar] + offset);
329}
330
331static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
332 u32 offset, u64 value)
333{
334 if (sof_ops(sdev)->write64)
335 sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
336 else
337 writeq(value, sdev->bar[bar] + offset);
338}
339
340static inline u8 snd_sof_dsp_read8(struct snd_sof_dev *sdev, u32 bar,
341 u32 offset)
342{
343 if (sof_ops(sdev)->read8)
344 return sof_ops(sdev)->read8(sdev, sdev->bar[bar] + offset);
345 else
346 return readb(sdev->bar[bar] + offset);
347}
348
349static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
350 u32 offset)
351{
352 if (sof_ops(sdev)->read)
353 return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
354 else
355 return readl(sdev->bar[bar] + offset);
356}
357
358static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
359 u32 offset)
360{
361 if (sof_ops(sdev)->read64)
362 return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
363 else
364 return readq(sdev->bar[bar] + offset);
365}
366
367static inline void snd_sof_dsp_update8(struct snd_sof_dev *sdev, u32 bar,
368 u32 offset, u8 mask, u8 value)
369{
370 u8 reg;
371
372 reg = snd_sof_dsp_read8(sdev, bar, offset);
373 reg &= ~mask;
374 reg |= value;
375 snd_sof_dsp_write8(sdev, bar, offset, reg);
376}
377
378/* block IO */
379static inline int snd_sof_dsp_block_read(struct snd_sof_dev *sdev,
380 enum snd_sof_fw_blk_type blk_type,
381 u32 offset, void *dest, size_t bytes)
382{
383 return sof_ops(sdev)->block_read(sdev, blk_type, offset, dest, bytes);
384}
385
386static inline int snd_sof_dsp_block_write(struct snd_sof_dev *sdev,
387 enum snd_sof_fw_blk_type blk_type,
388 u32 offset, void *src, size_t bytes)
389{
390 return sof_ops(sdev)->block_write(sdev, blk_type, offset, src, bytes);
391}
392
393/* mailbox IO */
394static inline void snd_sof_dsp_mailbox_read(struct snd_sof_dev *sdev,
395 u32 offset, void *dest, size_t bytes)
396{
397 if (sof_ops(sdev)->mailbox_read)
398 sof_ops(sdev)->mailbox_read(sdev, offset, dest, bytes);
399}
400
401static inline void snd_sof_dsp_mailbox_write(struct snd_sof_dev *sdev,
402 u32 offset, void *src, size_t bytes)
403{
404 if (sof_ops(sdev)->mailbox_write)
405 sof_ops(sdev)->mailbox_write(sdev, offset, src, bytes);
406}
407
408/* ipc */
409static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
410 struct snd_sof_ipc_msg *msg)
411{
412 return sof_ops(sdev)->send_msg(sdev, msg);
413}
414
415/* host PCM ops */
416static inline int
417snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
418 struct snd_pcm_substream *substream)
419{
420 if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
421 return sof_ops(sdev)->pcm_open(sdev, substream);
422
423 return 0;
424}
425
426/* disconnect pcm substream to a host stream */
427static inline int
428snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
429 struct snd_pcm_substream *substream)
430{
431 if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
432 return sof_ops(sdev)->pcm_close(sdev, substream);
433
434 return 0;
435}
436
437/* host stream hw params */
438static inline int
439snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
440 struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params *params,
442 struct snd_sof_platform_stream_params *platform_params)
443{
444 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
445 return sof_ops(sdev)->pcm_hw_params(sdev, substream, params,
446 platform_params);
447
448 return 0;
449}
450
451/* host stream hw free */
452static inline int
453snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
454 struct snd_pcm_substream *substream)
455{
456 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
457 return sof_ops(sdev)->pcm_hw_free(sdev, substream);
458
459 return 0;
460}
461
462/* host stream trigger */
463static inline int
464snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
465 struct snd_pcm_substream *substream, int cmd)
466{
467 if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
468 return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
469
470 return 0;
471}
472
473/* Firmware loading */
474static inline int snd_sof_load_firmware(struct snd_sof_dev *sdev)
475{
476 dev_dbg(sdev->dev, "loading firmware\n");
477
478 return sof_ops(sdev)->load_firmware(sdev);
479}
480
481/* host DSP message data */
482static inline int snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
483 struct snd_sof_pcm_stream *sps,
484 void *p, size_t sz)
485{
486 return sof_ops(sdev)->ipc_msg_data(sdev, sps, p, sz);
487}
488/* host side configuration of the stream's data offset in stream mailbox area */
489static inline int
490snd_sof_set_stream_data_offset(struct snd_sof_dev *sdev,
491 struct snd_sof_pcm_stream *sps,
492 size_t posn_offset)
493{
494 if (sof_ops(sdev) && sof_ops(sdev)->set_stream_data_offset)
495 return sof_ops(sdev)->set_stream_data_offset(sdev, sps,
496 posn_offset);
497
498 return 0;
499}
500
501/* host stream pointer */
502static inline snd_pcm_uframes_t
503snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
504 struct snd_pcm_substream *substream)
505{
506 if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
507 return sof_ops(sdev)->pcm_pointer(sdev, substream);
508
509 return 0;
510}
511
512/* pcm ack */
513static inline int snd_sof_pcm_platform_ack(struct snd_sof_dev *sdev,
514 struct snd_pcm_substream *substream)
515{
516 if (sof_ops(sdev) && sof_ops(sdev)->pcm_ack)
517 return sof_ops(sdev)->pcm_ack(sdev, substream);
518
519 return 0;
520}
521
522static inline u64
523snd_sof_pcm_get_dai_frame_counter(struct snd_sof_dev *sdev,
524 struct snd_soc_component *component,
525 struct snd_pcm_substream *substream)
526{
527 if (sof_ops(sdev) && sof_ops(sdev)->get_dai_frame_counter)
528 return sof_ops(sdev)->get_dai_frame_counter(sdev, component,
529 substream);
530
531 return 0;
532}
533
534static inline u64
535snd_sof_pcm_get_host_byte_counter(struct snd_sof_dev *sdev,
536 struct snd_soc_component *component,
537 struct snd_pcm_substream *substream)
538{
539 if (sof_ops(sdev) && sof_ops(sdev)->get_host_byte_counter)
540 return sof_ops(sdev)->get_host_byte_counter(sdev, component,
541 substream);
542
543 return 0;
544}
545
546/* machine driver */
547static inline int
548snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
549{
550 if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
551 return sof_ops(sdev)->machine_register(sdev, pdata);
552
553 return 0;
554}
555
556static inline void
557snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
558{
559 if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
560 sof_ops(sdev)->machine_unregister(sdev, pdata);
561}
562
563static inline struct snd_soc_acpi_mach *
564snd_sof_machine_select(struct snd_sof_dev *sdev)
565{
566 if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
567 return sof_ops(sdev)->machine_select(sdev);
568
569 return NULL;
570}
571
572static inline void
573snd_sof_set_mach_params(struct snd_soc_acpi_mach *mach,
574 struct snd_sof_dev *sdev)
575{
576 if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
577 sof_ops(sdev)->set_mach_params(mach, sdev);
578}
579
580static inline bool
581snd_sof_is_chain_dma_supported(struct snd_sof_dev *sdev, u32 dai_type)
582{
583 if (sof_ops(sdev) && sof_ops(sdev)->is_chain_dma_supported)
584 return sof_ops(sdev)->is_chain_dma_supported(sdev, dai_type);
585
586 return false;
587}
588
589/**
590 * snd_sof_dsp_register_poll_timeout - Periodically poll an address
591 * until a condition is met or a timeout occurs
592 * @op: accessor function (takes @addr as its only argument)
593 * @addr: Address to poll
594 * @val: Variable to read the value into
595 * @cond: Break condition (usually involving @val)
596 * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). Please
597 * read usleep_range() function description for details and
598 * limitations.
599 * @timeout_us: Timeout in us, 0 means never timeout
600 *
601 * Returns: 0 on success and -ETIMEDOUT upon a timeout. In either
602 * case, the last read value at @addr is stored in @val. Must not
603 * be called from atomic context if sleep_us or timeout_us are used.
604 *
605 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
606 */
607#define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
608({ \
609 u64 __timeout_us = (timeout_us); \
610 unsigned long __sleep_us = (sleep_us); \
611 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
612 might_sleep_if((__sleep_us) != 0); \
613 for (;;) { \
614 (val) = snd_sof_dsp_read(sdev, bar, offset); \
615 if (cond) { \
616 dev_dbg(sdev->dev, \
617 "FW Poll Status: reg[%#x]=%#x successful\n", \
618 (offset), (val)); \
619 break; \
620 } \
621 if (__timeout_us && \
622 ktime_compare(ktime_get(), __timeout) > 0) { \
623 (val) = snd_sof_dsp_read(sdev, bar, offset); \
624 dev_dbg(sdev->dev, \
625 "FW Poll Status: reg[%#x]=%#x timedout\n", \
626 (offset), (val)); \
627 break; \
628 } \
629 if (__sleep_us) \
630 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
631 } \
632 (cond) ? 0 : -ETIMEDOUT; \
633})
634
635/* This is for registers bits with attribute RWC */
636bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
637 u32 mask, u32 value);
638
639bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
640 u32 offset, u32 mask, u32 value);
641
642bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
643 u32 offset, u64 mask, u64 value);
644
645bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
646 u32 mask, u32 value);
647
648bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
649 u32 offset, u64 mask, u64 value);
650
651void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
652 u32 offset, u32 mask, u32 value);
653
654int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
655 u32 mask, u32 target, u32 timeout_ms,
656 u32 interval_us);
657
658void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset, bool non_recoverable);
659#endif