1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
5 // Authors: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
6 // Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
9 #include <linux/debugfs.h>
10 #include <linux/errno.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <sound/sof/ipc4/header.h>
17 #include "sof-client.h"
21 * struct sof_ipc_event_entry - IPC client event description
22 * @ipc_msg_type: IPC msg type of the event the client is interested
23 * @cdev: sof_client_dev of the requesting client
24 * @callback: Callback function of the client
25 * @list: item in SOF core client event list
27 struct sof_ipc_event_entry {
29 struct sof_client_dev *cdev;
30 sof_client_event_callback callback;
31 struct list_head list;
35 * struct sof_state_event_entry - DSP panic event subscription entry
36 * @cdev: sof_client_dev of the requesting client
37 * @callback: Callback function of the client
38 * @list: item in SOF core client event list
40 struct sof_state_event_entry {
41 struct sof_client_dev *cdev;
42 sof_client_fw_state_callback callback;
43 struct list_head list;
46 static void sof_client_auxdev_release(struct device *dev)
48 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
49 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
51 kfree(cdev->auxdev.dev.platform_data);
55 static int sof_client_dev_add_data(struct sof_client_dev *cdev, const void *data,
61 d = kmemdup(data, size, GFP_KERNEL);
66 cdev->auxdev.dev.platform_data = d;
70 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
71 static int sof_register_ipc_flood_test(struct snd_sof_dev *sdev)
76 if (sdev->pdata->ipc_type != SOF_IPC)
79 for (i = 0; i < CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST_NUM; i++) {
80 ret = sof_client_dev_register(sdev, "ipc_flood", i, NULL, 0);
87 sof_client_dev_unregister(sdev, "ipc_flood", i);
93 static void sof_unregister_ipc_flood_test(struct snd_sof_dev *sdev)
97 for (i = 0; i < CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST_NUM; i++)
98 sof_client_dev_unregister(sdev, "ipc_flood", i);
101 static inline int sof_register_ipc_flood_test(struct snd_sof_dev *sdev)
106 static inline void sof_unregister_ipc_flood_test(struct snd_sof_dev *sdev) {}
107 #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST */
109 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR)
110 static int sof_register_ipc_msg_injector(struct snd_sof_dev *sdev)
112 return sof_client_dev_register(sdev, "msg_injector", 0, NULL, 0);
115 static void sof_unregister_ipc_msg_injector(struct snd_sof_dev *sdev)
117 sof_client_dev_unregister(sdev, "msg_injector", 0);
120 static inline int sof_register_ipc_msg_injector(struct snd_sof_dev *sdev)
125 static inline void sof_unregister_ipc_msg_injector(struct snd_sof_dev *sdev) {}
126 #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR */
128 int sof_register_clients(struct snd_sof_dev *sdev)
132 /* Register platform independent client devices */
133 ret = sof_register_ipc_flood_test(sdev);
135 dev_err(sdev->dev, "IPC flood test client registration failed\n");
139 ret = sof_register_ipc_msg_injector(sdev);
141 dev_err(sdev->dev, "IPC message injector client registration failed\n");
142 goto err_msg_injector;
145 /* Platform depndent client device registration */
147 if (sof_ops(sdev) && sof_ops(sdev)->register_ipc_clients)
148 ret = sof_ops(sdev)->register_ipc_clients(sdev);
153 sof_unregister_ipc_msg_injector(sdev);
156 sof_unregister_ipc_flood_test(sdev);
161 void sof_unregister_clients(struct snd_sof_dev *sdev)
163 if (sof_ops(sdev) && sof_ops(sdev)->unregister_ipc_clients)
164 sof_ops(sdev)->unregister_ipc_clients(sdev);
166 sof_unregister_ipc_msg_injector(sdev);
167 sof_unregister_ipc_flood_test(sdev);
170 int sof_client_dev_register(struct snd_sof_dev *sdev, const char *name, u32 id,
171 const void *data, size_t size)
173 struct auxiliary_device *auxdev;
174 struct sof_client_dev *cdev;
177 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
182 auxdev = &cdev->auxdev;
184 auxdev->dev.parent = sdev->dev;
185 auxdev->dev.release = sof_client_auxdev_release;
188 ret = sof_client_dev_add_data(cdev, data, size);
190 goto err_dev_add_data;
192 ret = auxiliary_device_init(auxdev);
194 dev_err(sdev->dev, "failed to initialize client dev %s.%d\n", name, id);
198 ret = auxiliary_device_add(&cdev->auxdev);
200 dev_err(sdev->dev, "failed to add client dev %s.%d\n", name, id);
202 * sof_client_auxdev_release() will be invoked to free up memory
203 * allocations through put_device()
205 auxiliary_device_uninit(&cdev->auxdev);
209 /* add to list of SOF client devices */
210 mutex_lock(&sdev->ipc_client_mutex);
211 list_add(&cdev->list, &sdev->ipc_client_list);
212 mutex_unlock(&sdev->ipc_client_mutex);
217 kfree(cdev->auxdev.dev.platform_data);
224 EXPORT_SYMBOL_NS_GPL(sof_client_dev_register, SND_SOC_SOF_CLIENT);
226 void sof_client_dev_unregister(struct snd_sof_dev *sdev, const char *name, u32 id)
228 struct sof_client_dev *cdev;
230 mutex_lock(&sdev->ipc_client_mutex);
233 * sof_client_auxdev_release() will be invoked to free up memory
234 * allocations through put_device()
236 list_for_each_entry(cdev, &sdev->ipc_client_list, list) {
237 if (!strcmp(cdev->auxdev.name, name) && cdev->auxdev.id == id) {
238 list_del(&cdev->list);
239 auxiliary_device_delete(&cdev->auxdev);
240 auxiliary_device_uninit(&cdev->auxdev);
245 mutex_unlock(&sdev->ipc_client_mutex);
247 EXPORT_SYMBOL_NS_GPL(sof_client_dev_unregister, SND_SOC_SOF_CLIENT);
249 int sof_client_ipc_tx_message(struct sof_client_dev *cdev, void *ipc_msg,
250 void *reply_data, size_t reply_bytes)
252 if (cdev->sdev->pdata->ipc_type == SOF_IPC) {
253 struct sof_ipc_cmd_hdr *hdr = ipc_msg;
255 return sof_ipc_tx_message(cdev->sdev->ipc, ipc_msg, hdr->size,
256 reply_data, reply_bytes);
257 } else if (cdev->sdev->pdata->ipc_type == SOF_INTEL_IPC4) {
258 struct sof_ipc4_msg *msg = ipc_msg;
260 return sof_ipc_tx_message(cdev->sdev->ipc, ipc_msg, msg->data_size,
261 reply_data, reply_bytes);
266 EXPORT_SYMBOL_NS_GPL(sof_client_ipc_tx_message, SND_SOC_SOF_CLIENT);
268 int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state)
270 struct auxiliary_driver *adrv;
271 struct sof_client_dev *cdev;
273 mutex_lock(&sdev->ipc_client_mutex);
275 list_for_each_entry(cdev, &sdev->ipc_client_list, list) {
276 /* Skip devices without loaded driver */
277 if (!cdev->auxdev.dev.driver)
280 adrv = to_auxiliary_drv(cdev->auxdev.dev.driver);
282 adrv->suspend(&cdev->auxdev, state);
285 mutex_unlock(&sdev->ipc_client_mutex);
289 EXPORT_SYMBOL_NS_GPL(sof_suspend_clients, SND_SOC_SOF_CLIENT);
291 int sof_resume_clients(struct snd_sof_dev *sdev)
293 struct auxiliary_driver *adrv;
294 struct sof_client_dev *cdev;
296 mutex_lock(&sdev->ipc_client_mutex);
298 list_for_each_entry(cdev, &sdev->ipc_client_list, list) {
299 /* Skip devices without loaded driver */
300 if (!cdev->auxdev.dev.driver)
303 adrv = to_auxiliary_drv(cdev->auxdev.dev.driver);
305 adrv->resume(&cdev->auxdev);
308 mutex_unlock(&sdev->ipc_client_mutex);
312 EXPORT_SYMBOL_NS_GPL(sof_resume_clients, SND_SOC_SOF_CLIENT);
314 struct dentry *sof_client_get_debugfs_root(struct sof_client_dev *cdev)
316 return cdev->sdev->debugfs_root;
318 EXPORT_SYMBOL_NS_GPL(sof_client_get_debugfs_root, SND_SOC_SOF_CLIENT);
320 /* DMA buffer allocation in client drivers must use the core SOF device */
321 struct device *sof_client_get_dma_dev(struct sof_client_dev *cdev)
323 return cdev->sdev->dev;
325 EXPORT_SYMBOL_NS_GPL(sof_client_get_dma_dev, SND_SOC_SOF_CLIENT);
327 const struct sof_ipc_fw_version *sof_client_get_fw_version(struct sof_client_dev *cdev)
329 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
331 return &sdev->fw_ready.version;
333 EXPORT_SYMBOL_NS_GPL(sof_client_get_fw_version, SND_SOC_SOF_CLIENT);
335 size_t sof_client_get_ipc_max_payload_size(struct sof_client_dev *cdev)
337 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
339 return sdev->ipc->max_payload_size;
341 EXPORT_SYMBOL_NS_GPL(sof_client_get_ipc_max_payload_size, SND_SOC_SOF_CLIENT);
343 enum sof_ipc_type sof_client_get_ipc_type(struct sof_client_dev *cdev)
345 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
347 return sdev->pdata->ipc_type;
349 EXPORT_SYMBOL_NS_GPL(sof_client_get_ipc_type, SND_SOC_SOF_CLIENT);
351 /* module refcount management of SOF core */
352 int sof_client_core_module_get(struct sof_client_dev *cdev)
354 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
356 if (!try_module_get(sdev->dev->driver->owner))
361 EXPORT_SYMBOL_NS_GPL(sof_client_core_module_get, SND_SOC_SOF_CLIENT);
363 void sof_client_core_module_put(struct sof_client_dev *cdev)
365 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
367 module_put(sdev->dev->driver->owner);
369 EXPORT_SYMBOL_NS_GPL(sof_client_core_module_put, SND_SOC_SOF_CLIENT);
371 /* IPC event handling */
372 void sof_client_ipc_rx_dispatcher(struct snd_sof_dev *sdev, void *msg_buf)
374 struct sof_ipc_event_entry *event;
377 if (sdev->pdata->ipc_type == SOF_IPC) {
378 struct sof_ipc_cmd_hdr *hdr = msg_buf;
380 msg_type = hdr->cmd & SOF_GLB_TYPE_MASK;
381 } else if (sdev->pdata->ipc_type == SOF_INTEL_IPC4) {
382 struct sof_ipc4_msg *msg = msg_buf;
384 msg_type = SOF_IPC4_NOTIFICATION_TYPE_GET(msg->primary);
386 dev_dbg_once(sdev->dev, "Not supported IPC version: %d\n",
387 sdev->pdata->ipc_type);
391 mutex_lock(&sdev->client_event_handler_mutex);
393 list_for_each_entry(event, &sdev->ipc_rx_handler_list, list) {
394 if (event->ipc_msg_type == msg_type)
395 event->callback(event->cdev, msg_buf);
398 mutex_unlock(&sdev->client_event_handler_mutex);
401 int sof_client_register_ipc_rx_handler(struct sof_client_dev *cdev,
403 sof_client_event_callback callback)
405 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
406 struct sof_ipc_event_entry *event;
411 if (cdev->sdev->pdata->ipc_type == SOF_IPC) {
412 if (!(ipc_msg_type & SOF_GLB_TYPE_MASK))
414 } else if (cdev->sdev->pdata->ipc_type == SOF_INTEL_IPC4) {
415 if (!(ipc_msg_type & SOF_IPC4_NOTIFICATION_TYPE_MASK))
418 dev_warn(sdev->dev, "%s: Not supported IPC version: %d\n",
419 __func__, sdev->pdata->ipc_type);
423 event = kmalloc(sizeof(*event), GFP_KERNEL);
427 event->ipc_msg_type = ipc_msg_type;
429 event->callback = callback;
431 /* add to list of SOF client devices */
432 mutex_lock(&sdev->client_event_handler_mutex);
433 list_add(&event->list, &sdev->ipc_rx_handler_list);
434 mutex_unlock(&sdev->client_event_handler_mutex);
438 EXPORT_SYMBOL_NS_GPL(sof_client_register_ipc_rx_handler, SND_SOC_SOF_CLIENT);
440 void sof_client_unregister_ipc_rx_handler(struct sof_client_dev *cdev,
443 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
444 struct sof_ipc_event_entry *event;
446 mutex_lock(&sdev->client_event_handler_mutex);
448 list_for_each_entry(event, &sdev->ipc_rx_handler_list, list) {
449 if (event->cdev == cdev && event->ipc_msg_type == ipc_msg_type) {
450 list_del(&event->list);
456 mutex_unlock(&sdev->client_event_handler_mutex);
458 EXPORT_SYMBOL_NS_GPL(sof_client_unregister_ipc_rx_handler, SND_SOC_SOF_CLIENT);
460 /*DSP state notification and query */
461 void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev)
463 struct sof_state_event_entry *event;
465 mutex_lock(&sdev->client_event_handler_mutex);
467 list_for_each_entry(event, &sdev->fw_state_handler_list, list)
468 event->callback(event->cdev, sdev->fw_state);
470 mutex_unlock(&sdev->client_event_handler_mutex);
473 int sof_client_register_fw_state_handler(struct sof_client_dev *cdev,
474 sof_client_fw_state_callback callback)
476 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
477 struct sof_state_event_entry *event;
482 event = kmalloc(sizeof(*event), GFP_KERNEL);
487 event->callback = callback;
489 /* add to list of SOF client devices */
490 mutex_lock(&sdev->client_event_handler_mutex);
491 list_add(&event->list, &sdev->fw_state_handler_list);
492 mutex_unlock(&sdev->client_event_handler_mutex);
496 EXPORT_SYMBOL_NS_GPL(sof_client_register_fw_state_handler, SND_SOC_SOF_CLIENT);
498 void sof_client_unregister_fw_state_handler(struct sof_client_dev *cdev)
500 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
501 struct sof_state_event_entry *event;
503 mutex_lock(&sdev->client_event_handler_mutex);
505 list_for_each_entry(event, &sdev->fw_state_handler_list, list) {
506 if (event->cdev == cdev) {
507 list_del(&event->list);
513 mutex_unlock(&sdev->client_event_handler_mutex);
515 EXPORT_SYMBOL_NS_GPL(sof_client_unregister_fw_state_handler, SND_SOC_SOF_CLIENT);
517 enum sof_fw_state sof_client_get_fw_state(struct sof_client_dev *cdev)
519 struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
521 return sdev->fw_state;
523 EXPORT_SYMBOL_NS_GPL(sof_client_get_fw_state, SND_SOC_SOF_CLIENT);