xref: /OK3568_Linux_fs/kernel/sound/soc/sof/intel/intel-ipc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // This file is provided under a dual BSD/GPLv2 license.  When using or
4*4882a593Smuzhiyun // redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Copyright(c) 2019 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun //
8*4882a593Smuzhiyun // Authors: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* Intel-specific SOF IPC code */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/export.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <sound/pcm.h>
18*4882a593Smuzhiyun #include <sound/sof/stream.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "../ops.h"
21*4882a593Smuzhiyun #include "../sof-priv.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun struct intel_stream {
24*4882a593Smuzhiyun 	size_t posn_offset;
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /* Mailbox-based Intel IPC implementation */
intel_ipc_msg_data(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream,void * p,size_t sz)28*4882a593Smuzhiyun void intel_ipc_msg_data(struct snd_sof_dev *sdev,
29*4882a593Smuzhiyun 			struct snd_pcm_substream *substream,
30*4882a593Smuzhiyun 			void *p, size_t sz)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	if (!substream || !sdev->stream_box.size) {
33*4882a593Smuzhiyun 		sof_mailbox_read(sdev, sdev->dsp_box.offset, p, sz);
34*4882a593Smuzhiyun 	} else {
35*4882a593Smuzhiyun 		struct intel_stream *stream = substream->runtime->private_data;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 		/* The stream might already be closed */
38*4882a593Smuzhiyun 		if (stream)
39*4882a593Smuzhiyun 			sof_mailbox_read(sdev, stream->posn_offset, p, sz);
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun EXPORT_SYMBOL_NS(intel_ipc_msg_data, SND_SOC_SOF_INTEL_HIFI_EP_IPC);
43*4882a593Smuzhiyun 
intel_ipc_pcm_params(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream,const struct sof_ipc_pcm_params_reply * reply)44*4882a593Smuzhiyun int intel_ipc_pcm_params(struct snd_sof_dev *sdev,
45*4882a593Smuzhiyun 			 struct snd_pcm_substream *substream,
46*4882a593Smuzhiyun 			 const struct sof_ipc_pcm_params_reply *reply)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct intel_stream *stream = substream->runtime->private_data;
49*4882a593Smuzhiyun 	size_t posn_offset = reply->posn_offset;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	/* check if offset is overflow or it is not aligned */
52*4882a593Smuzhiyun 	if (posn_offset > sdev->stream_box.size ||
53*4882a593Smuzhiyun 	    posn_offset % sizeof(struct sof_ipc_stream_posn) != 0)
54*4882a593Smuzhiyun 		return -EINVAL;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	stream->posn_offset = sdev->stream_box.offset + posn_offset;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	dev_dbg(sdev->dev, "pcm: stream dir %d, posn mailbox offset is %zu",
59*4882a593Smuzhiyun 		substream->stream, stream->posn_offset);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun EXPORT_SYMBOL_NS(intel_ipc_pcm_params, SND_SOC_SOF_INTEL_HIFI_EP_IPC);
64*4882a593Smuzhiyun 
intel_pcm_open(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream)65*4882a593Smuzhiyun int intel_pcm_open(struct snd_sof_dev *sdev,
66*4882a593Smuzhiyun 		   struct snd_pcm_substream *substream)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	struct intel_stream *stream = kmalloc(sizeof(*stream), GFP_KERNEL);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (!stream)
71*4882a593Smuzhiyun 		return -ENOMEM;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* binding pcm substream to hda stream */
74*4882a593Smuzhiyun 	substream->runtime->private_data = stream;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return 0;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun EXPORT_SYMBOL_NS(intel_pcm_open, SND_SOC_SOF_INTEL_HIFI_EP_IPC);
79*4882a593Smuzhiyun 
intel_pcm_close(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream)80*4882a593Smuzhiyun int intel_pcm_close(struct snd_sof_dev *sdev,
81*4882a593Smuzhiyun 		    struct snd_pcm_substream *substream)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	struct intel_stream *stream = substream->runtime->private_data;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	substream->runtime->private_data = NULL;
86*4882a593Smuzhiyun 	kfree(stream);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	return 0;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun EXPORT_SYMBOL_NS(intel_pcm_close, SND_SOC_SOF_INTEL_HIFI_EP_IPC);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
93