xref: /openbmc/linux/sound/soc/sof/sof-client.c (revision bbb774d9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
6 //	    Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
7 //
8 
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>
16 #include "ops.h"
17 #include "sof-client.h"
18 #include "sof-priv.h"
19 
20 /**
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
26  */
27 struct sof_ipc_event_entry {
28 	u32 ipc_msg_type;
29 	struct sof_client_dev *cdev;
30 	sof_client_event_callback callback;
31 	struct list_head list;
32 };
33 
34 /**
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
39  */
40 struct sof_state_event_entry {
41 	struct sof_client_dev *cdev;
42 	sof_client_fw_state_callback callback;
43 	struct list_head list;
44 };
45 
46 static void sof_client_auxdev_release(struct device *dev)
47 {
48 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
49 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
50 
51 	kfree(cdev->auxdev.dev.platform_data);
52 	kfree(cdev);
53 }
54 
55 static int sof_client_dev_add_data(struct sof_client_dev *cdev, const void *data,
56 				   size_t size)
57 {
58 	void *d = NULL;
59 
60 	if (data) {
61 		d = kmemdup(data, size, GFP_KERNEL);
62 		if (!d)
63 			return -ENOMEM;
64 	}
65 
66 	cdev->auxdev.dev.platform_data = d;
67 	return 0;
68 }
69 
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)
72 {
73 	int ret = 0;
74 	int i;
75 
76 	if (sdev->pdata->ipc_type != SOF_IPC)
77 		return 0;
78 
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);
81 		if (ret < 0)
82 			break;
83 	}
84 
85 	if (ret) {
86 		for (; i >= 0; --i)
87 			sof_client_dev_unregister(sdev, "ipc_flood", i);
88 	}
89 
90 	return ret;
91 }
92 
93 static void sof_unregister_ipc_flood_test(struct snd_sof_dev *sdev)
94 {
95 	int i;
96 
97 	for (i = 0; i < CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST_NUM; i++)
98 		sof_client_dev_unregister(sdev, "ipc_flood", i);
99 }
100 #else
101 static inline int sof_register_ipc_flood_test(struct snd_sof_dev *sdev)
102 {
103 	return 0;
104 }
105 
106 static inline void sof_unregister_ipc_flood_test(struct snd_sof_dev *sdev) {}
107 #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST */
108 
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)
111 {
112 	return sof_client_dev_register(sdev, "msg_injector", 0, NULL, 0);
113 }
114 
115 static void sof_unregister_ipc_msg_injector(struct snd_sof_dev *sdev)
116 {
117 	sof_client_dev_unregister(sdev, "msg_injector", 0);
118 }
119 #else
120 static inline int sof_register_ipc_msg_injector(struct snd_sof_dev *sdev)
121 {
122 	return 0;
123 }
124 
125 static inline void sof_unregister_ipc_msg_injector(struct snd_sof_dev *sdev) {}
126 #endif /* CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR */
127 
128 int sof_register_clients(struct snd_sof_dev *sdev)
129 {
130 	int ret;
131 
132 	/* Register platform independent client devices */
133 	ret = sof_register_ipc_flood_test(sdev);
134 	if (ret) {
135 		dev_err(sdev->dev, "IPC flood test client registration failed\n");
136 		return ret;
137 	}
138 
139 	ret = sof_register_ipc_msg_injector(sdev);
140 	if (ret) {
141 		dev_err(sdev->dev, "IPC message injector client registration failed\n");
142 		goto err_msg_injector;
143 	}
144 
145 	/* Platform depndent client device registration */
146 
147 	if (sof_ops(sdev) && sof_ops(sdev)->register_ipc_clients)
148 		ret = sof_ops(sdev)->register_ipc_clients(sdev);
149 
150 	if (!ret)
151 		return 0;
152 
153 	sof_unregister_ipc_msg_injector(sdev);
154 
155 err_msg_injector:
156 	sof_unregister_ipc_flood_test(sdev);
157 
158 	return ret;
159 }
160 
161 void sof_unregister_clients(struct snd_sof_dev *sdev)
162 {
163 	if (sof_ops(sdev) && sof_ops(sdev)->unregister_ipc_clients)
164 		sof_ops(sdev)->unregister_ipc_clients(sdev);
165 
166 	sof_unregister_ipc_msg_injector(sdev);
167 	sof_unregister_ipc_flood_test(sdev);
168 }
169 
170 int sof_client_dev_register(struct snd_sof_dev *sdev, const char *name, u32 id,
171 			    const void *data, size_t size)
172 {
173 	struct auxiliary_device *auxdev;
174 	struct sof_client_dev *cdev;
175 	int ret;
176 
177 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
178 	if (!cdev)
179 		return -ENOMEM;
180 
181 	cdev->sdev = sdev;
182 	auxdev = &cdev->auxdev;
183 	auxdev->name = name;
184 	auxdev->dev.parent = sdev->dev;
185 	auxdev->dev.release = sof_client_auxdev_release;
186 	auxdev->id = id;
187 
188 	ret = sof_client_dev_add_data(cdev, data, size);
189 	if (ret < 0)
190 		goto err_dev_add_data;
191 
192 	ret = auxiliary_device_init(auxdev);
193 	if (ret < 0) {
194 		dev_err(sdev->dev, "failed to initialize client dev %s.%d\n", name, id);
195 		goto err_dev_init;
196 	}
197 
198 	ret = auxiliary_device_add(&cdev->auxdev);
199 	if (ret < 0) {
200 		dev_err(sdev->dev, "failed to add client dev %s.%d\n", name, id);
201 		/*
202 		 * sof_client_auxdev_release() will be invoked to free up memory
203 		 * allocations through put_device()
204 		 */
205 		auxiliary_device_uninit(&cdev->auxdev);
206 		return ret;
207 	}
208 
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);
213 
214 	return 0;
215 
216 err_dev_init:
217 	kfree(cdev->auxdev.dev.platform_data);
218 
219 err_dev_add_data:
220 	kfree(cdev);
221 
222 	return ret;
223 }
224 EXPORT_SYMBOL_NS_GPL(sof_client_dev_register, SND_SOC_SOF_CLIENT);
225 
226 void sof_client_dev_unregister(struct snd_sof_dev *sdev, const char *name, u32 id)
227 {
228 	struct sof_client_dev *cdev;
229 
230 	mutex_lock(&sdev->ipc_client_mutex);
231 
232 	/*
233 	 * sof_client_auxdev_release() will be invoked to free up memory
234 	 * allocations through put_device()
235 	 */
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);
241 			break;
242 		}
243 	}
244 
245 	mutex_unlock(&sdev->ipc_client_mutex);
246 }
247 EXPORT_SYMBOL_NS_GPL(sof_client_dev_unregister, SND_SOC_SOF_CLIENT);
248 
249 int sof_client_ipc_tx_message(struct sof_client_dev *cdev, void *ipc_msg,
250 			      void *reply_data, size_t reply_bytes)
251 {
252 	if (cdev->sdev->pdata->ipc_type == SOF_IPC) {
253 		struct sof_ipc_cmd_hdr *hdr = ipc_msg;
254 
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;
259 
260 		return sof_ipc_tx_message(cdev->sdev->ipc, ipc_msg, msg->data_size,
261 					  reply_data, reply_bytes);
262 	}
263 
264 	return -EINVAL;
265 }
266 EXPORT_SYMBOL_NS_GPL(sof_client_ipc_tx_message, SND_SOC_SOF_CLIENT);
267 
268 int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state)
269 {
270 	struct auxiliary_driver *adrv;
271 	struct sof_client_dev *cdev;
272 
273 	mutex_lock(&sdev->ipc_client_mutex);
274 
275 	list_for_each_entry(cdev, &sdev->ipc_client_list, list) {
276 		/* Skip devices without loaded driver */
277 		if (!cdev->auxdev.dev.driver)
278 			continue;
279 
280 		adrv = to_auxiliary_drv(cdev->auxdev.dev.driver);
281 		if (adrv->suspend)
282 			adrv->suspend(&cdev->auxdev, state);
283 	}
284 
285 	mutex_unlock(&sdev->ipc_client_mutex);
286 
287 	return 0;
288 }
289 EXPORT_SYMBOL_NS_GPL(sof_suspend_clients, SND_SOC_SOF_CLIENT);
290 
291 int sof_resume_clients(struct snd_sof_dev *sdev)
292 {
293 	struct auxiliary_driver *adrv;
294 	struct sof_client_dev *cdev;
295 
296 	mutex_lock(&sdev->ipc_client_mutex);
297 
298 	list_for_each_entry(cdev, &sdev->ipc_client_list, list) {
299 		/* Skip devices without loaded driver */
300 		if (!cdev->auxdev.dev.driver)
301 			continue;
302 
303 		adrv = to_auxiliary_drv(cdev->auxdev.dev.driver);
304 		if (adrv->resume)
305 			adrv->resume(&cdev->auxdev);
306 	}
307 
308 	mutex_unlock(&sdev->ipc_client_mutex);
309 
310 	return 0;
311 }
312 EXPORT_SYMBOL_NS_GPL(sof_resume_clients, SND_SOC_SOF_CLIENT);
313 
314 struct dentry *sof_client_get_debugfs_root(struct sof_client_dev *cdev)
315 {
316 	return cdev->sdev->debugfs_root;
317 }
318 EXPORT_SYMBOL_NS_GPL(sof_client_get_debugfs_root, SND_SOC_SOF_CLIENT);
319 
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)
322 {
323 	return cdev->sdev->dev;
324 }
325 EXPORT_SYMBOL_NS_GPL(sof_client_get_dma_dev, SND_SOC_SOF_CLIENT);
326 
327 const struct sof_ipc_fw_version *sof_client_get_fw_version(struct sof_client_dev *cdev)
328 {
329 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
330 
331 	return &sdev->fw_ready.version;
332 }
333 EXPORT_SYMBOL_NS_GPL(sof_client_get_fw_version, SND_SOC_SOF_CLIENT);
334 
335 size_t sof_client_get_ipc_max_payload_size(struct sof_client_dev *cdev)
336 {
337 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
338 
339 	return sdev->ipc->max_payload_size;
340 }
341 EXPORT_SYMBOL_NS_GPL(sof_client_get_ipc_max_payload_size, SND_SOC_SOF_CLIENT);
342 
343 enum sof_ipc_type sof_client_get_ipc_type(struct sof_client_dev *cdev)
344 {
345 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
346 
347 	return sdev->pdata->ipc_type;
348 }
349 EXPORT_SYMBOL_NS_GPL(sof_client_get_ipc_type, SND_SOC_SOF_CLIENT);
350 
351 /* module refcount management of SOF core */
352 int sof_client_core_module_get(struct sof_client_dev *cdev)
353 {
354 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
355 
356 	if (!try_module_get(sdev->dev->driver->owner))
357 		return -ENODEV;
358 
359 	return 0;
360 }
361 EXPORT_SYMBOL_NS_GPL(sof_client_core_module_get, SND_SOC_SOF_CLIENT);
362 
363 void sof_client_core_module_put(struct sof_client_dev *cdev)
364 {
365 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
366 
367 	module_put(sdev->dev->driver->owner);
368 }
369 EXPORT_SYMBOL_NS_GPL(sof_client_core_module_put, SND_SOC_SOF_CLIENT);
370 
371 /* IPC event handling */
372 void sof_client_ipc_rx_dispatcher(struct snd_sof_dev *sdev, void *msg_buf)
373 {
374 	struct sof_ipc_event_entry *event;
375 	u32 msg_type;
376 
377 	if (sdev->pdata->ipc_type == SOF_IPC) {
378 		struct sof_ipc_cmd_hdr *hdr = msg_buf;
379 
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;
383 
384 		msg_type = SOF_IPC4_NOTIFICATION_TYPE_GET(msg->primary);
385 	} else {
386 		dev_dbg_once(sdev->dev, "Not supported IPC version: %d\n",
387 			     sdev->pdata->ipc_type);
388 		return;
389 	}
390 
391 	mutex_lock(&sdev->client_event_handler_mutex);
392 
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);
396 	}
397 
398 	mutex_unlock(&sdev->client_event_handler_mutex);
399 }
400 
401 int sof_client_register_ipc_rx_handler(struct sof_client_dev *cdev,
402 				       u32 ipc_msg_type,
403 				       sof_client_event_callback callback)
404 {
405 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
406 	struct sof_ipc_event_entry *event;
407 
408 	if (!callback)
409 		return -EINVAL;
410 
411 	if (cdev->sdev->pdata->ipc_type == SOF_IPC) {
412 		if (!(ipc_msg_type & SOF_GLB_TYPE_MASK))
413 			return -EINVAL;
414 	} else if (cdev->sdev->pdata->ipc_type == SOF_INTEL_IPC4) {
415 		if (!(ipc_msg_type & SOF_IPC4_NOTIFICATION_TYPE_MASK))
416 			return -EINVAL;
417 	} else {
418 		dev_warn(sdev->dev, "%s: Not supported IPC version: %d\n",
419 			 __func__, sdev->pdata->ipc_type);
420 		return -EINVAL;
421 	}
422 
423 	event = kmalloc(sizeof(*event), GFP_KERNEL);
424 	if (!event)
425 		return -ENOMEM;
426 
427 	event->ipc_msg_type = ipc_msg_type;
428 	event->cdev = cdev;
429 	event->callback = callback;
430 
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);
435 
436 	return 0;
437 }
438 EXPORT_SYMBOL_NS_GPL(sof_client_register_ipc_rx_handler, SND_SOC_SOF_CLIENT);
439 
440 void sof_client_unregister_ipc_rx_handler(struct sof_client_dev *cdev,
441 					  u32 ipc_msg_type)
442 {
443 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
444 	struct sof_ipc_event_entry *event;
445 
446 	mutex_lock(&sdev->client_event_handler_mutex);
447 
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);
451 			kfree(event);
452 			break;
453 		}
454 	}
455 
456 	mutex_unlock(&sdev->client_event_handler_mutex);
457 }
458 EXPORT_SYMBOL_NS_GPL(sof_client_unregister_ipc_rx_handler, SND_SOC_SOF_CLIENT);
459 
460 /*DSP state notification and query */
461 void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev)
462 {
463 	struct sof_state_event_entry *event;
464 
465 	mutex_lock(&sdev->client_event_handler_mutex);
466 
467 	list_for_each_entry(event, &sdev->fw_state_handler_list, list)
468 		event->callback(event->cdev, sdev->fw_state);
469 
470 	mutex_unlock(&sdev->client_event_handler_mutex);
471 }
472 
473 int sof_client_register_fw_state_handler(struct sof_client_dev *cdev,
474 					 sof_client_fw_state_callback callback)
475 {
476 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
477 	struct sof_state_event_entry *event;
478 
479 	if (!callback)
480 		return -EINVAL;
481 
482 	event = kmalloc(sizeof(*event), GFP_KERNEL);
483 	if (!event)
484 		return -ENOMEM;
485 
486 	event->cdev = cdev;
487 	event->callback = callback;
488 
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);
493 
494 	return 0;
495 }
496 EXPORT_SYMBOL_NS_GPL(sof_client_register_fw_state_handler, SND_SOC_SOF_CLIENT);
497 
498 void sof_client_unregister_fw_state_handler(struct sof_client_dev *cdev)
499 {
500 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
501 	struct sof_state_event_entry *event;
502 
503 	mutex_lock(&sdev->client_event_handler_mutex);
504 
505 	list_for_each_entry(event, &sdev->fw_state_handler_list, list) {
506 		if (event->cdev == cdev) {
507 			list_del(&event->list);
508 			kfree(event);
509 			break;
510 		}
511 	}
512 
513 	mutex_unlock(&sdev->client_event_handler_mutex);
514 }
515 EXPORT_SYMBOL_NS_GPL(sof_client_unregister_fw_state_handler, SND_SOC_SOF_CLIENT);
516 
517 enum sof_fw_state sof_client_get_fw_state(struct sof_client_dev *cdev)
518 {
519 	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
520 
521 	return sdev->fw_state;
522 }
523 EXPORT_SYMBOL_NS_GPL(sof_client_get_fw_state, SND_SOC_SOF_CLIENT);
524