2017-12-14 13:49:34 +08:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
|
|
|
|
// Copyright(c) 2015-17 Intel Corporation.
|
|
|
|
|
|
|
|
#include <linux/acpi.h>
|
2020-01-15 08:08:43 +08:00
|
|
|
#include <linux/delay.h>
|
2017-12-14 13:49:34 +08:00
|
|
|
#include <linux/mod_devicetable.h>
|
2017-12-14 13:49:37 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
|
|
|
#include <linux/soundwire/sdw_registers.h>
|
2017-12-14 13:49:34 +08:00
|
|
|
#include <linux/soundwire/sdw.h>
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
#include <linux/soundwire/sdw_type.h>
|
2017-12-14 13:49:34 +08:00
|
|
|
#include "bus.h"
|
2020-05-19 04:35:51 +08:00
|
|
|
#include "sysfs_local.h"
|
2017-12-14 13:49:34 +08:00
|
|
|
|
2022-08-23 12:50:02 +08:00
|
|
|
static DEFINE_IDA(sdw_bus_ida);
|
2022-08-23 12:50:03 +08:00
|
|
|
static DEFINE_IDA(sdw_peripheral_ida);
|
2020-05-19 01:43:20 +08:00
|
|
|
|
|
|
|
static int sdw_get_id(struct sdw_bus *bus)
|
|
|
|
{
|
2022-08-23 12:50:02 +08:00
|
|
|
int rc = ida_alloc(&sdw_bus_ida, GFP_KERNEL);
|
2020-05-19 01:43:20 +08:00
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
bus->id = rc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:34 +08:00
|
|
|
/**
|
2020-05-19 01:43:18 +08:00
|
|
|
* sdw_bus_master_add() - add a bus Master instance
|
2017-12-14 13:49:34 +08:00
|
|
|
* @bus: bus instance
|
2020-05-19 01:43:18 +08:00
|
|
|
* @parent: parent device
|
|
|
|
* @fwnode: firmware node handle
|
2017-12-14 13:49:34 +08:00
|
|
|
*
|
|
|
|
* Initializes the bus instance, read properties and create child
|
|
|
|
* devices.
|
|
|
|
*/
|
2020-05-19 01:43:18 +08:00
|
|
|
int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
|
|
|
|
struct fwnode_handle *fwnode)
|
2017-12-14 13:49:34 +08:00
|
|
|
{
|
2018-04-26 21:08:33 +08:00
|
|
|
struct sdw_master_prop *prop = NULL;
|
2017-12-14 13:49:34 +08:00
|
|
|
int ret;
|
|
|
|
|
2020-05-19 01:43:21 +08:00
|
|
|
if (!parent) {
|
|
|
|
pr_err("SoundWire parent device is not set\n");
|
2017-12-14 13:49:34 +08:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2020-05-19 01:43:20 +08:00
|
|
|
ret = sdw_get_id(bus);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0) {
|
2020-05-19 01:43:21 +08:00
|
|
|
dev_err(parent, "Failed to get bus id\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sdw_master_device_add(bus, parent, fwnode);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0) {
|
2020-05-19 01:43:21 +08:00
|
|
|
dev_err(parent, "Failed to add master device at link %d\n",
|
|
|
|
bus->link_id);
|
2020-05-19 01:43:20 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:37 +08:00
|
|
|
if (!bus->ops) {
|
2019-05-01 23:57:45 +08:00
|
|
|
dev_err(bus->dev, "SoundWire Bus ops are not set\n");
|
2017-12-14 13:49:37 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:15:20 +08:00
|
|
|
if (!bus->compute_params) {
|
|
|
|
dev_err(bus->dev,
|
|
|
|
"Bandwidth allocation not configured, compute_params no set\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:37 +08:00
|
|
|
mutex_init(&bus->msg_lock);
|
2017-12-14 13:49:34 +08:00
|
|
|
mutex_init(&bus->bus_lock);
|
|
|
|
INIT_LIST_HEAD(&bus->slaves);
|
2018-04-26 21:08:08 +08:00
|
|
|
INIT_LIST_HEAD(&bus->m_rt_list);
|
2017-12-14 13:49:34 +08:00
|
|
|
|
2018-07-27 17:14:16 +08:00
|
|
|
/*
|
|
|
|
* Initialize multi_link flag
|
|
|
|
*/
|
|
|
|
bus->multi_link = false;
|
2017-12-14 13:49:35 +08:00
|
|
|
if (bus->ops->read_prop) {
|
|
|
|
ret = bus->ops->read_prop(bus);
|
|
|
|
if (ret < 0) {
|
2019-05-02 18:59:24 +08:00
|
|
|
dev_err(bus->dev,
|
|
|
|
"Bus read properties failed:%d\n", ret);
|
2017-12-14 13:49:35 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 02:58:18 +08:00
|
|
|
sdw_bus_debugfs_init(bus);
|
|
|
|
|
2017-12-14 13:49:34 +08:00
|
|
|
/*
|
2019-05-01 23:57:28 +08:00
|
|
|
* Device numbers in SoundWire are 0 through 15. Enumeration device
|
2017-12-14 13:49:34 +08:00
|
|
|
* number (0), Broadcast device number (15), Group numbers (12 and
|
|
|
|
* 13) and Master device number (14) are not used for assignment so
|
|
|
|
* mask these and other higher bits.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Set higher order bits */
|
|
|
|
*bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
|
|
|
|
|
|
|
|
/* Set enumuration device number and broadcast device number */
|
|
|
|
set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
|
|
|
|
set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
|
|
|
|
|
|
|
|
/* Set group device numbers and master device number */
|
|
|
|
set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
|
|
|
|
set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
|
|
|
|
set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SDW is an enumerable bus, but devices can be powered off. So,
|
|
|
|
* they won't be able to report as present.
|
|
|
|
*
|
|
|
|
* Create Slave devices based on Slaves described in
|
|
|
|
* the respective firmware (ACPI/DT)
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
|
|
|
|
ret = sdw_acpi_find_slaves(bus);
|
2019-08-30 00:35:12 +08:00
|
|
|
else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
|
|
|
|
ret = sdw_of_find_slaves(bus);
|
2017-12-14 13:49:34 +08:00
|
|
|
else
|
|
|
|
ret = -ENOTSUPP; /* No ACPI/DT so error out */
|
|
|
|
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0) {
|
2017-12-14 13:49:34 +08:00
|
|
|
dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:08:28 +08:00
|
|
|
/*
|
2018-04-26 21:08:33 +08:00
|
|
|
* Initialize clock values based on Master properties. The max
|
2019-05-23 03:47:22 +08:00
|
|
|
* frequency is read from max_clk_freq property. Current assumption
|
2018-04-26 21:08:33 +08:00
|
|
|
* is that the bus will start at highest clock frequency when
|
|
|
|
* powered on.
|
|
|
|
*
|
2018-04-26 21:08:28 +08:00
|
|
|
* Default active bank will be 0 as out of reset the Slaves have
|
|
|
|
* to start with bank 0 (Table 40 of Spec)
|
|
|
|
*/
|
2018-04-26 21:08:33 +08:00
|
|
|
prop = &bus->prop;
|
2019-05-23 03:47:22 +08:00
|
|
|
bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
|
2018-04-26 21:08:33 +08:00
|
|
|
bus->params.curr_dr_freq = bus->params.max_dr_freq;
|
2018-04-26 21:08:28 +08:00
|
|
|
bus->params.curr_bank = SDW_BANK0;
|
|
|
|
bus->params.next_bank = SDW_BANK1;
|
|
|
|
|
2017-12-14 13:49:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-19 01:43:18 +08:00
|
|
|
EXPORT_SYMBOL(sdw_bus_master_add);
|
2017-12-14 13:49:34 +08:00
|
|
|
|
|
|
|
static int sdw_delete_slave(struct device *dev, void *data)
|
|
|
|
{
|
|
|
|
struct sdw_slave *slave = dev_to_sdw_dev(dev);
|
|
|
|
struct sdw_bus *bus = slave->bus;
|
|
|
|
|
2020-01-15 08:08:41 +08:00
|
|
|
pm_runtime_disable(dev);
|
|
|
|
|
2019-08-22 02:58:18 +08:00
|
|
|
sdw_slave_debugfs_exit(slave);
|
|
|
|
|
2017-12-14 13:49:34 +08:00
|
|
|
mutex_lock(&bus->bus_lock);
|
|
|
|
|
2022-08-23 12:50:03 +08:00
|
|
|
if (slave->dev_num) { /* clear dev_num if assigned */
|
2017-12-14 13:49:34 +08:00
|
|
|
clear_bit(slave->dev_num, bus->assigned);
|
2022-08-23 12:50:03 +08:00
|
|
|
if (bus->dev_num_ida_min)
|
|
|
|
ida_free(&sdw_peripheral_ida, slave->dev_num);
|
|
|
|
}
|
2017-12-14 13:49:34 +08:00
|
|
|
list_del_init(&slave->node);
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
|
|
|
|
device_unregister(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-19 01:43:18 +08:00
|
|
|
* sdw_bus_master_delete() - delete the bus master instance
|
2017-12-14 13:49:34 +08:00
|
|
|
* @bus: bus to be deleted
|
|
|
|
*
|
|
|
|
* Remove the instance, delete the child devices.
|
|
|
|
*/
|
2020-05-19 01:43:18 +08:00
|
|
|
void sdw_bus_master_delete(struct sdw_bus *bus)
|
2017-12-14 13:49:34 +08:00
|
|
|
{
|
|
|
|
device_for_each_child(bus->dev, NULL, sdw_delete_slave);
|
2020-05-19 01:43:21 +08:00
|
|
|
sdw_master_device_del(bus);
|
2019-08-22 02:58:18 +08:00
|
|
|
|
|
|
|
sdw_bus_debugfs_exit(bus);
|
2022-08-23 12:50:02 +08:00
|
|
|
ida_free(&sdw_bus_ida, bus->id);
|
2017-12-14 13:49:34 +08:00
|
|
|
}
|
2020-05-19 01:43:18 +08:00
|
|
|
EXPORT_SYMBOL(sdw_bus_master_delete);
|
2017-12-14 13:49:34 +08:00
|
|
|
|
2017-12-14 13:49:37 +08:00
|
|
|
/*
|
|
|
|
* SDW IO Calls
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline int find_response_code(enum sdw_command_response resp)
|
|
|
|
{
|
|
|
|
switch (resp) {
|
|
|
|
case SDW_CMD_OK:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SDW_CMD_IGNORED:
|
|
|
|
return -ENODATA;
|
|
|
|
|
|
|
|
case SDW_CMD_TIMEOUT:
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
|
|
|
|
{
|
|
|
|
int retry = bus->prop.err_threshold;
|
|
|
|
enum sdw_command_response resp;
|
|
|
|
int ret = 0, i;
|
|
|
|
|
|
|
|
for (i = 0; i <= retry; i++) {
|
|
|
|
resp = bus->ops->xfer_msg(bus, msg);
|
|
|
|
ret = find_response_code(resp);
|
|
|
|
|
|
|
|
/* if cmd is ok or ignored return */
|
|
|
|
if (ret == 0 || ret == -ENODATA)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int do_transfer_defer(struct sdw_bus *bus,
|
2023-01-19 15:32:09 +08:00
|
|
|
struct sdw_msg *msg)
|
2017-12-14 13:49:37 +08:00
|
|
|
{
|
2023-01-19 15:32:09 +08:00
|
|
|
struct sdw_defer *defer = &bus->defer_msg;
|
2017-12-14 13:49:37 +08:00
|
|
|
int retry = bus->prop.err_threshold;
|
|
|
|
enum sdw_command_response resp;
|
|
|
|
int ret = 0, i;
|
|
|
|
|
|
|
|
defer->msg = msg;
|
|
|
|
defer->length = msg->len;
|
2018-07-27 17:14:12 +08:00
|
|
|
init_completion(&defer->complete);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
|
|
|
for (i = 0; i <= retry; i++) {
|
2023-01-19 15:32:10 +08:00
|
|
|
resp = bus->ops->xfer_msg_defer(bus, msg);
|
2017-12-14 13:49:37 +08:00
|
|
|
ret = find_response_code(resp);
|
|
|
|
/* if cmd is ok or ignored return */
|
|
|
|
if (ret == 0 || ret == -ENODATA)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:45:20 +08:00
|
|
|
static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = do_transfer(bus, msg);
|
|
|
|
if (ret != 0 && ret != -ENODATA)
|
2021-01-15 13:37:36 +08:00
|
|
|
dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
|
|
|
|
msg->dev_num, ret,
|
|
|
|
(msg->flags & SDW_MSG_FLAG_WRITE) ? "write" : "read",
|
|
|
|
msg->addr, msg->len);
|
2020-09-08 21:45:20 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:37 +08:00
|
|
|
/**
|
|
|
|
* sdw_transfer() - Synchronous transfer message to a SDW Slave device
|
|
|
|
* @bus: SDW bus
|
|
|
|
* @msg: SDW message to be xfered
|
|
|
|
*/
|
|
|
|
int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&bus->msg_lock);
|
|
|
|
|
2020-09-08 21:45:20 +08:00
|
|
|
ret = sdw_transfer_unlocked(bus, msg);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
|
|
|
mutex_unlock(&bus->msg_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-14 09:10:42 +08:00
|
|
|
/**
|
|
|
|
* sdw_show_ping_status() - Direct report of PING status, to be used by Peripheral drivers
|
|
|
|
* @bus: SDW bus
|
|
|
|
* @sync_delay: Delay before reading status
|
|
|
|
*/
|
|
|
|
void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay)
|
|
|
|
{
|
|
|
|
u32 status;
|
|
|
|
|
|
|
|
if (!bus->ops->read_ping_status)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait for peripheral to sync if desired. 10-15ms should be more than
|
|
|
|
* enough in most cases.
|
|
|
|
*/
|
|
|
|
if (sync_delay)
|
|
|
|
usleep_range(10000, 15000);
|
|
|
|
|
|
|
|
mutex_lock(&bus->msg_lock);
|
|
|
|
|
|
|
|
status = bus->ops->read_ping_status(bus);
|
|
|
|
|
|
|
|
mutex_unlock(&bus->msg_lock);
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
dev_warn(bus->dev, "%s: no peripherals attached\n", __func__);
|
|
|
|
else
|
|
|
|
dev_dbg(bus->dev, "PING status: %#x\n", status);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_show_ping_status);
|
|
|
|
|
2017-12-14 13:49:37 +08:00
|
|
|
/**
|
|
|
|
* sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
|
|
|
|
* @bus: SDW bus
|
|
|
|
* @msg: SDW message to be xfered
|
|
|
|
*
|
|
|
|
* Caller needs to hold the msg_lock lock while calling this
|
|
|
|
*/
|
2023-01-19 15:32:09 +08:00
|
|
|
int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg)
|
2017-12-14 13:49:37 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!bus->ops->xfer_msg_defer)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2023-01-19 15:32:09 +08:00
|
|
|
ret = do_transfer_defer(bus, msg);
|
2017-12-14 13:49:37 +08:00
|
|
|
if (ret != 0 && ret != -ENODATA)
|
|
|
|
dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
|
2019-05-01 23:57:27 +08:00
|
|
|
msg->dev_num, ret);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
|
2019-05-01 23:57:27 +08:00
|
|
|
u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
|
2017-12-14 13:49:37 +08:00
|
|
|
{
|
|
|
|
memset(msg, 0, sizeof(*msg));
|
|
|
|
msg->addr = addr; /* addr is 16 bit and truncated here */
|
|
|
|
msg->len = count;
|
|
|
|
msg->dev_num = dev_num;
|
|
|
|
msg->flags = flags;
|
|
|
|
msg->buf = buf;
|
|
|
|
|
2020-05-08 08:30:45 +08:00
|
|
|
if (addr < SDW_REG_NO_PAGE) /* no paging area */
|
2017-12-14 13:49:37 +08:00
|
|
|
return 0;
|
2020-05-08 08:30:45 +08:00
|
|
|
|
|
|
|
if (addr >= SDW_REG_MAX) { /* illegal addr */
|
2017-12-14 13:49:37 +08:00
|
|
|
pr_err("SDW: Invalid address %x passed\n", addr);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
|
|
|
|
if (slave && !slave->prop.paging_support)
|
|
|
|
return 0;
|
2019-05-01 23:57:28 +08:00
|
|
|
/* no need for else as that will fall-through to paging */
|
2017-12-14 13:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* paging mandatory */
|
|
|
|
if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
|
|
|
|
pr_err("SDW: Invalid device for paging :%d\n", dev_num);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slave) {
|
|
|
|
pr_err("SDW: No slave for paging addr\n");
|
|
|
|
return -EINVAL;
|
2020-05-08 08:30:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!slave->prop.paging_support) {
|
2017-12-14 13:49:37 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"address %x needs paging but no support\n", addr);
|
2017-12-14 13:49:37 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:44:57 +08:00
|
|
|
msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
|
|
|
|
msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
|
2017-12-14 13:49:37 +08:00
|
|
|
msg->addr |= BIT(15);
|
|
|
|
msg->page = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-15 08:08:38 +08:00
|
|
|
/*
|
|
|
|
* Read/Write IO functions.
|
|
|
|
* no_pm versions can only be called by the bus, e.g. while enumerating or
|
|
|
|
* handling suspend-resume sequences.
|
|
|
|
* all clients need to use the pm versions
|
|
|
|
*/
|
|
|
|
|
2022-11-25 22:20:25 +08:00
|
|
|
int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
|
2020-01-15 08:08:38 +08:00
|
|
|
{
|
|
|
|
struct sdw_msg msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_fill_msg(&msg, slave, addr, count,
|
|
|
|
slave->dev_num, SDW_MSG_FLAG_READ, val);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-07-14 11:22:06 +08:00
|
|
|
ret = sdw_transfer(slave->bus, &msg);
|
|
|
|
if (slave->is_mockup_device)
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
2020-01-15 08:08:38 +08:00
|
|
|
}
|
2022-11-25 22:20:25 +08:00
|
|
|
EXPORT_SYMBOL(sdw_nread_no_pm);
|
2020-01-15 08:08:38 +08:00
|
|
|
|
2022-11-25 22:20:25 +08:00
|
|
|
int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
|
2020-01-15 08:08:38 +08:00
|
|
|
{
|
|
|
|
struct sdw_msg msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_fill_msg(&msg, slave, addr, count,
|
2021-06-16 22:59:01 +08:00
|
|
|
slave->dev_num, SDW_MSG_FLAG_WRITE, (u8 *)val);
|
2020-01-15 08:08:38 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-07-14 11:22:06 +08:00
|
|
|
ret = sdw_transfer(slave->bus, &msg);
|
|
|
|
if (slave->is_mockup_device)
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
2020-01-15 08:08:38 +08:00
|
|
|
}
|
2022-11-25 22:20:25 +08:00
|
|
|
EXPORT_SYMBOL(sdw_nwrite_no_pm);
|
2020-01-15 08:08:38 +08:00
|
|
|
|
2021-01-22 15:06:29 +08:00
|
|
|
int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
|
2020-01-15 08:08:38 +08:00
|
|
|
{
|
|
|
|
return sdw_nwrite_no_pm(slave, addr, 1, &value);
|
|
|
|
}
|
2021-01-22 15:06:29 +08:00
|
|
|
EXPORT_SYMBOL(sdw_write_no_pm);
|
2020-01-15 08:08:38 +08:00
|
|
|
|
2020-01-15 08:08:43 +08:00
|
|
|
static int
|
|
|
|
sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
|
|
|
|
{
|
|
|
|
struct sdw_msg msg;
|
|
|
|
u8 buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
|
|
|
SDW_MSG_FLAG_READ, &buf);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2020-01-15 08:08:43 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = sdw_transfer(bus, &msg);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-05-08 08:30:45 +08:00
|
|
|
|
|
|
|
return buf;
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
|
|
|
|
{
|
|
|
|
struct sdw_msg msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
|
|
|
SDW_MSG_FLAG_WRITE, &value);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2020-01-15 08:08:43 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return sdw_transfer(bus, &msg);
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:45:20 +08:00
|
|
|
int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
|
|
|
|
{
|
|
|
|
struct sdw_msg msg;
|
|
|
|
u8 buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
|
|
|
SDW_MSG_FLAG_READ, &buf);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2020-09-08 21:45:20 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = sdw_transfer_unlocked(bus, &msg);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
|
|
|
|
|
|
|
|
int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
|
|
|
|
{
|
|
|
|
struct sdw_msg msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
|
|
|
SDW_MSG_FLAG_WRITE, &value);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2020-09-08 21:45:20 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return sdw_transfer_unlocked(bus, &msg);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
|
|
|
|
|
2021-01-22 15:06:29 +08:00
|
|
|
int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
|
2020-01-15 08:08:43 +08:00
|
|
|
{
|
|
|
|
u8 buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_nread_no_pm(slave, addr, 1, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return buf;
|
|
|
|
}
|
2021-01-22 15:06:29 +08:00
|
|
|
EXPORT_SYMBOL(sdw_read_no_pm);
|
2020-01-15 08:08:43 +08:00
|
|
|
|
2021-06-15 02:08:11 +08:00
|
|
|
int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
|
2021-01-22 15:06:26 +08:00
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
tmp = sdw_read_no_pm(slave, addr);
|
|
|
|
if (tmp < 0)
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
tmp = (tmp & ~mask) | val;
|
|
|
|
return sdw_write_no_pm(slave, addr, tmp);
|
|
|
|
}
|
2021-06-15 02:08:11 +08:00
|
|
|
EXPORT_SYMBOL(sdw_update_no_pm);
|
|
|
|
|
|
|
|
/* Read-Modify-Write Slave register */
|
|
|
|
int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
tmp = sdw_read(slave, addr);
|
|
|
|
if (tmp < 0)
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
tmp = (tmp & ~mask) | val;
|
|
|
|
return sdw_write(slave, addr, tmp);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_update);
|
2021-01-22 15:06:26 +08:00
|
|
|
|
2017-12-14 13:49:37 +08:00
|
|
|
/**
|
|
|
|
* sdw_nread() - Read "n" contiguous SDW Slave registers
|
|
|
|
* @slave: SDW Slave
|
|
|
|
* @addr: Register address
|
|
|
|
* @count: length
|
|
|
|
* @val: Buffer for values to be read
|
|
|
|
*/
|
|
|
|
int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-04-27 07:56:20 +08:00
|
|
|
ret = pm_runtime_resume_and_get(&slave->dev);
|
|
|
|
if (ret < 0 && ret != -EACCES)
|
2017-12-14 13:49:37 +08:00
|
|
|
return ret;
|
2020-01-15 08:08:38 +08:00
|
|
|
|
|
|
|
ret = sdw_nread_no_pm(slave, addr, count, val);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
2021-01-22 15:06:33 +08:00
|
|
|
pm_runtime_mark_last_busy(&slave->dev);
|
|
|
|
pm_runtime_put(&slave->dev);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_nread);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_nwrite() - Write "n" contiguous SDW Slave registers
|
|
|
|
* @slave: SDW Slave
|
|
|
|
* @addr: Register address
|
|
|
|
* @count: length
|
2021-06-16 22:59:01 +08:00
|
|
|
* @val: Buffer for values to be written
|
2017-12-14 13:49:37 +08:00
|
|
|
*/
|
2021-06-16 22:59:01 +08:00
|
|
|
int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
|
2017-12-14 13:49:37 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-04-27 07:56:20 +08:00
|
|
|
ret = pm_runtime_resume_and_get(&slave->dev);
|
|
|
|
if (ret < 0 && ret != -EACCES)
|
2017-12-14 13:49:37 +08:00
|
|
|
return ret;
|
2020-01-15 08:08:38 +08:00
|
|
|
|
|
|
|
ret = sdw_nwrite_no_pm(slave, addr, count, val);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
2021-01-22 15:06:33 +08:00
|
|
|
pm_runtime_mark_last_busy(&slave->dev);
|
|
|
|
pm_runtime_put(&slave->dev);
|
2017-12-14 13:49:37 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_nwrite);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_read() - Read a SDW Slave register
|
|
|
|
* @slave: SDW Slave
|
|
|
|
* @addr: Register address
|
|
|
|
*/
|
|
|
|
int sdw_read(struct sdw_slave *slave, u32 addr)
|
|
|
|
{
|
|
|
|
u8 buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sdw_nread(slave, addr, 1, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-05-08 08:30:45 +08:00
|
|
|
|
|
|
|
return buf;
|
2017-12-14 13:49:37 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_read);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_write() - Write a SDW Slave register
|
|
|
|
* @slave: SDW Slave
|
|
|
|
* @addr: Register address
|
|
|
|
* @value: Register value
|
|
|
|
*/
|
|
|
|
int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
|
|
|
|
{
|
|
|
|
return sdw_nwrite(slave, addr, 1, &value);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_write);
|
|
|
|
|
2017-12-14 13:49:39 +08:00
|
|
|
/*
|
|
|
|
* SDW alert handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* called with bus_lock held */
|
|
|
|
static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
|
|
|
|
{
|
2021-03-02 17:11:17 +08:00
|
|
|
struct sdw_slave *slave;
|
2017-12-14 13:49:39 +08:00
|
|
|
|
|
|
|
list_for_each_entry(slave, &bus->slaves, node) {
|
|
|
|
if (slave->dev_num == i)
|
|
|
|
return slave;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:47:17 +08:00
|
|
|
int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
|
2017-12-14 13:49:39 +08:00
|
|
|
{
|
2019-10-23 07:48:08 +08:00
|
|
|
if (slave->id.mfg_id != id.mfg_id ||
|
2019-05-01 23:57:29 +08:00
|
|
|
slave->id.part_id != id.part_id ||
|
2019-10-23 07:48:08 +08:00
|
|
|
slave->id.class_id != id.class_id ||
|
|
|
|
(slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
|
|
|
|
slave->id.unique_id != id.unique_id))
|
2017-12-14 13:49:39 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-30 22:47:17 +08:00
|
|
|
EXPORT_SYMBOL(sdw_compare_devid);
|
2017-12-14 13:49:39 +08:00
|
|
|
|
|
|
|
/* called with bus_lock held */
|
|
|
|
static int sdw_get_device_num(struct sdw_slave *slave)
|
|
|
|
{
|
|
|
|
int bit;
|
|
|
|
|
2022-08-23 12:50:03 +08:00
|
|
|
if (slave->bus->dev_num_ida_min) {
|
|
|
|
bit = ida_alloc_range(&sdw_peripheral_ida,
|
|
|
|
slave->bus->dev_num_ida_min, SDW_MAX_DEVICES,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (bit < 0)
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
|
|
|
|
if (bit == SDW_MAX_DEVICES) {
|
|
|
|
bit = -ENODEV;
|
|
|
|
goto err;
|
|
|
|
}
|
2017-12-14 13:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not update dev_num in Slave data structure here,
|
|
|
|
* Update once program dev_num is successful
|
|
|
|
*/
|
|
|
|
set_bit(bit, slave->bus->assigned);
|
|
|
|
|
|
|
|
err:
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdw_assign_device_num(struct sdw_slave *slave)
|
|
|
|
{
|
2021-01-22 15:06:34 +08:00
|
|
|
struct sdw_bus *bus = slave->bus;
|
2017-12-14 13:49:39 +08:00
|
|
|
int ret, dev_num;
|
2020-01-14 06:56:37 +08:00
|
|
|
bool new_device = false;
|
2017-12-14 13:49:39 +08:00
|
|
|
|
|
|
|
/* check first if device number is assigned, if so reuse that */
|
|
|
|
if (!slave->dev_num) {
|
2020-01-14 06:56:37 +08:00
|
|
|
if (!slave->dev_num_sticky) {
|
|
|
|
mutex_lock(&slave->bus->bus_lock);
|
|
|
|
dev_num = sdw_get_device_num(slave);
|
|
|
|
mutex_unlock(&slave->bus->bus_lock);
|
|
|
|
if (dev_num < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(bus->dev, "Get dev_num failed: %d\n",
|
2020-01-14 06:56:37 +08:00
|
|
|
dev_num);
|
|
|
|
return dev_num;
|
|
|
|
}
|
|
|
|
slave->dev_num = dev_num;
|
|
|
|
slave->dev_num_sticky = dev_num;
|
|
|
|
new_device = true;
|
|
|
|
} else {
|
|
|
|
slave->dev_num = slave->dev_num_sticky;
|
2017-12-14 13:49:39 +08:00
|
|
|
}
|
2020-01-14 06:56:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!new_device)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_dbg(bus->dev,
|
2020-04-20 02:51:17 +08:00
|
|
|
"Slave already registered, reusing dev_num:%d\n",
|
|
|
|
slave->dev_num);
|
2017-12-14 13:49:39 +08:00
|
|
|
|
2020-01-14 06:56:37 +08:00
|
|
|
/* Clear the slave->dev_num to transfer message on device 0 */
|
|
|
|
dev_num = slave->dev_num;
|
|
|
|
slave->dev_num = 0;
|
2017-12-14 13:49:39 +08:00
|
|
|
|
2020-01-15 08:08:39 +08:00
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
|
2017-12-14 13:49:39 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(bus->dev, "Program device_num %d failed: %d\n",
|
2019-08-06 08:55:09 +08:00
|
|
|
dev_num, ret);
|
2017-12-14 13:49:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After xfer of msg, restore dev_num */
|
2020-01-14 06:56:37 +08:00
|
|
|
slave->dev_num = slave->dev_num_sticky;
|
2017-12-14 13:49:39 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:34 +08:00
|
|
|
void sdw_extract_slave_id(struct sdw_bus *bus,
|
2019-05-01 23:57:27 +08:00
|
|
|
u64 addr, struct sdw_slave_id *id)
|
2017-12-14 13:49:34 +08:00
|
|
|
{
|
2019-05-01 23:57:45 +08:00
|
|
|
dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
|
2017-12-14 13:49:34 +08:00
|
|
|
|
2020-02-26 01:00:41 +08:00
|
|
|
id->sdw_version = SDW_VERSION(addr);
|
|
|
|
id->unique_id = SDW_UNIQUE_ID(addr);
|
|
|
|
id->mfg_id = SDW_MFG_ID(addr);
|
|
|
|
id->part_id = SDW_PART_ID(addr);
|
|
|
|
id->class_id = SDW_CLASS_ID(addr);
|
2017-12-14 13:49:34 +08:00
|
|
|
|
|
|
|
dev_dbg(bus->dev,
|
2021-01-15 13:37:34 +08:00
|
|
|
"SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
|
|
|
|
id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
|
2017-12-14 13:49:34 +08:00
|
|
|
}
|
2021-03-30 22:47:17 +08:00
|
|
|
EXPORT_SYMBOL(sdw_extract_slave_id);
|
2017-12-14 13:49:39 +08:00
|
|
|
|
2022-09-15 00:02:48 +08:00
|
|
|
static int sdw_program_device_num(struct sdw_bus *bus, bool *programmed)
|
2017-12-14 13:49:39 +08:00
|
|
|
{
|
|
|
|
u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
|
|
|
|
struct sdw_slave *slave, *_s;
|
|
|
|
struct sdw_slave_id id;
|
|
|
|
struct sdw_msg msg;
|
2021-03-09 18:48:16 +08:00
|
|
|
bool found;
|
2017-12-14 13:49:39 +08:00
|
|
|
int count = 0, ret;
|
|
|
|
u64 addr;
|
|
|
|
|
2022-09-15 00:02:48 +08:00
|
|
|
*programmed = false;
|
|
|
|
|
2017-12-14 13:49:39 +08:00
|
|
|
/* No Slave, so use raw xfer api */
|
|
|
|
ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
|
2019-05-01 23:57:27 +08:00
|
|
|
SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
|
2017-12-14 13:49:39 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = sdw_transfer(bus, &msg);
|
|
|
|
if (ret == -ENODATA) { /* end of device id reads */
|
2019-08-06 08:55:09 +08:00
|
|
|
dev_dbg(bus->dev, "No more devices to enumerate\n");
|
2017-12-14 13:49:39 +08:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(bus->dev, "DEVID read fail:%d\n", ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct the addr and extract. Cast the higher shift
|
|
|
|
* bits to avoid truncation due to size limit.
|
|
|
|
*/
|
|
|
|
addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
|
2018-01-09 00:52:42 +08:00
|
|
|
((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
|
|
|
|
((u64)buf[0] << 40);
|
2017-12-14 13:49:39 +08:00
|
|
|
|
|
|
|
sdw_extract_slave_id(bus, addr, &id);
|
|
|
|
|
2021-03-09 18:48:16 +08:00
|
|
|
found = false;
|
2017-12-14 13:49:39 +08:00
|
|
|
/* Now compare with entries */
|
|
|
|
list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
|
|
|
|
if (sdw_compare_devid(slave, id) == 0) {
|
|
|
|
found = true;
|
|
|
|
|
2022-09-15 00:02:46 +08:00
|
|
|
/*
|
|
|
|
* To prevent skipping state-machine stages don't
|
|
|
|
* program a device until we've seen it UNATTACH.
|
|
|
|
* Must return here because no other device on #0
|
|
|
|
* can be detected until this one has been
|
|
|
|
* assigned a device ID.
|
|
|
|
*/
|
|
|
|
if (slave->status != SDW_SLAVE_UNATTACHED)
|
|
|
|
return 0;
|
|
|
|
|
2017-12-14 13:49:39 +08:00
|
|
|
/*
|
|
|
|
* Assign a new dev_num to this Slave and
|
|
|
|
* not mark it present. It will be marked
|
|
|
|
* present after it reports ATTACHED on new
|
|
|
|
* dev_num
|
|
|
|
*/
|
|
|
|
ret = sdw_assign_device_num(slave);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(bus->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"Assign dev_num failed:%d\n",
|
2017-12-14 13:49:39 +08:00
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:02:48 +08:00
|
|
|
*programmed = true;
|
|
|
|
|
2017-12-14 13:49:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:57:30 +08:00
|
|
|
if (!found) {
|
2017-12-14 13:49:39 +08:00
|
|
|
/* TODO: Park this device in Group 13 */
|
2020-09-25 03:44:29 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* add Slave device even if there is no platform
|
|
|
|
* firmware description. There will be no driver probe
|
|
|
|
* but the user/integration will be able to see the
|
|
|
|
* device, enumeration status and device number in sysfs
|
|
|
|
*/
|
|
|
|
sdw_slave_add(bus, &id, NULL);
|
|
|
|
|
2019-05-01 23:57:45 +08:00
|
|
|
dev_err(bus->dev, "Slave Entry not found\n");
|
2017-12-14 13:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check till error out or retry (count) exhausts.
|
|
|
|
* Device can drop off and rejoin during enumeration
|
|
|
|
* so count till twice the bound.
|
|
|
|
*/
|
|
|
|
|
|
|
|
} while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sdw_modify_slave_status(struct sdw_slave *slave,
|
2019-05-01 23:57:27 +08:00
|
|
|
enum sdw_slave_status status)
|
2017-12-14 13:49:39 +08:00
|
|
|
{
|
2021-01-22 15:06:34 +08:00
|
|
|
struct sdw_bus *bus = slave->bus;
|
|
|
|
|
|
|
|
mutex_lock(&bus->bus_lock);
|
2020-01-15 08:08:36 +08:00
|
|
|
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_vdbg(bus->dev,
|
2022-08-23 13:01:57 +08:00
|
|
|
"changing status slave %d status %d new status %d\n",
|
|
|
|
slave->dev_num, slave->status, status);
|
2020-01-15 08:08:36 +08:00
|
|
|
|
|
|
|
if (status == SDW_SLAVE_UNATTACHED) {
|
|
|
|
dev_dbg(&slave->dev,
|
2022-08-23 13:01:57 +08:00
|
|
|
"initializing enumeration and init completion for Slave %d\n",
|
|
|
|
slave->dev_num);
|
2020-01-15 08:08:36 +08:00
|
|
|
|
|
|
|
init_completion(&slave->enumeration_complete);
|
2020-01-15 08:08:37 +08:00
|
|
|
init_completion(&slave->initialization_complete);
|
2020-01-15 08:08:36 +08:00
|
|
|
|
|
|
|
} else if ((status == SDW_SLAVE_ATTACHED) &&
|
|
|
|
(slave->status == SDW_SLAVE_UNATTACHED)) {
|
|
|
|
dev_dbg(&slave->dev,
|
2022-08-23 13:01:57 +08:00
|
|
|
"signaling enumeration completion for Slave %d\n",
|
|
|
|
slave->dev_num);
|
2020-01-15 08:08:36 +08:00
|
|
|
|
|
|
|
complete(&slave->enumeration_complete);
|
|
|
|
}
|
2017-12-14 13:49:39 +08:00
|
|
|
slave->status = status;
|
2021-01-22 15:06:34 +08:00
|
|
|
mutex_unlock(&bus->bus_lock);
|
2017-12-14 13:49:39 +08:00
|
|
|
}
|
|
|
|
|
2020-01-15 08:08:43 +08:00
|
|
|
static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
|
|
|
|
enum sdw_clk_stop_mode mode,
|
|
|
|
enum sdw_clk_stop_type type)
|
|
|
|
{
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
int ret = 0;
|
2020-01-15 08:08:43 +08:00
|
|
|
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
mutex_lock(&slave->sdw_dev_lock);
|
|
|
|
|
|
|
|
if (slave->probed) {
|
|
|
|
struct device *dev = &slave->dev;
|
|
|
|
struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
|
|
|
|
|
|
|
if (drv->ops && drv->ops->clk_stop)
|
|
|
|
ret = drv->ops->clk_stop(slave, mode, type);
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
mutex_unlock(&slave->sdw_dev_lock);
|
|
|
|
|
|
|
|
return ret;
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
|
|
|
|
enum sdw_clk_stop_mode mode,
|
|
|
|
bool prepare)
|
|
|
|
{
|
|
|
|
bool wake_en;
|
|
|
|
u32 val = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
wake_en = slave->prop.wake_capable;
|
|
|
|
|
|
|
|
if (prepare) {
|
|
|
|
val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
|
|
|
|
|
|
|
|
if (mode == SDW_CLK_STOP_MODE1)
|
|
|
|
val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
|
|
|
|
|
|
|
|
if (wake_en)
|
|
|
|
val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
|
|
|
|
} else {
|
2021-03-02 17:11:13 +08:00
|
|
|
ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
|
|
|
|
if (ret < 0) {
|
2021-05-11 11:00:47 +08:00
|
|
|
if (ret != -ENODATA)
|
|
|
|
dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
|
2021-03-02 17:11:13 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
val = ret;
|
2020-01-15 08:08:43 +08:00
|
|
|
val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
|
|
|
|
|
2021-05-11 11:00:47 +08:00
|
|
|
if (ret < 0 && ret != -ENODATA)
|
|
|
|
dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
|
2020-01-15 08:08:43 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
|
|
|
|
{
|
|
|
|
int retry = bus->clk_stop_timeout;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
do {
|
2021-03-02 17:11:13 +08:00
|
|
|
val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
|
|
|
|
if (val < 0) {
|
2021-07-14 09:42:09 +08:00
|
|
|
if (val != -ENODATA)
|
|
|
|
dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
|
2021-03-02 17:11:13 +08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
val &= SDW_SCP_STAT_CLK_STP_NF;
|
2020-01-15 08:08:43 +08:00
|
|
|
if (!val) {
|
2021-05-11 11:00:48 +08:00
|
|
|
dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
|
2021-03-02 17:11:15 +08:00
|
|
|
dev_num);
|
2020-01-15 08:08:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep_range(1000, 1500);
|
|
|
|
retry--;
|
|
|
|
} while (retry);
|
|
|
|
|
2021-05-11 11:00:48 +08:00
|
|
|
dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
|
2020-01-15 08:08:43 +08:00
|
|
|
dev_num);
|
|
|
|
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
|
|
|
|
*
|
|
|
|
* @bus: SDW bus instance
|
|
|
|
*
|
|
|
|
* Query Slave for clock stop mode and prepare for that mode.
|
|
|
|
*/
|
|
|
|
int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
|
|
|
|
{
|
|
|
|
bool simple_clk_stop = true;
|
|
|
|
struct sdw_slave *slave;
|
|
|
|
bool is_slave = false;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In order to save on transition time, prepare
|
|
|
|
* each Slave and then wait for all Slave(s) to be
|
|
|
|
* prepared for clock stop.
|
2021-05-11 11:00:47 +08:00
|
|
|
* If one of the Slave devices has lost sync and
|
|
|
|
* replies with Command Ignored/-ENODATA, we continue
|
|
|
|
* the loop
|
2020-01-15 08:08:43 +08:00
|
|
|
*/
|
|
|
|
list_for_each_entry(slave, &bus->slaves, node) {
|
|
|
|
if (!slave->dev_num)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (slave->status != SDW_SLAVE_ATTACHED &&
|
|
|
|
slave->status != SDW_SLAVE_ALERT)
|
|
|
|
continue;
|
|
|
|
|
2020-05-31 23:18:06 +08:00
|
|
|
/* Identify if Slave(s) are available on Bus */
|
|
|
|
is_slave = true;
|
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
ret = sdw_slave_clk_stop_callback(slave,
|
|
|
|
SDW_CLK_STOP_MODE0,
|
2020-01-15 08:08:43 +08:00
|
|
|
SDW_CLK_PRE_PREPARE);
|
2021-05-11 11:00:47 +08:00
|
|
|
if (ret < 0 && ret != -ENODATA) {
|
|
|
|
dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
|
2020-01-15 08:08:43 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
/* Only prepare a Slave device if needed */
|
|
|
|
if (!slave->prop.simple_clk_stop_capable) {
|
2020-01-15 08:08:43 +08:00
|
|
|
simple_clk_stop = false;
|
2021-05-11 11:00:45 +08:00
|
|
|
|
|
|
|
ret = sdw_slave_clk_stop_prepare(slave,
|
|
|
|
SDW_CLK_STOP_MODE0,
|
|
|
|
true);
|
2021-05-11 11:00:47 +08:00
|
|
|
if (ret < 0 && ret != -ENODATA) {
|
|
|
|
dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
|
2021-05-11 11:00:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:54:39 +08:00
|
|
|
/* Skip remaining clock stop preparation if no Slave is attached */
|
|
|
|
if (!is_slave)
|
2021-05-11 11:00:47 +08:00
|
|
|
return 0;
|
2021-01-26 16:54:39 +08:00
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
/*
|
|
|
|
* Don't wait for all Slaves to be ready if they follow the simple
|
|
|
|
* state machine
|
|
|
|
*/
|
2021-01-26 16:54:39 +08:00
|
|
|
if (!simple_clk_stop) {
|
2020-01-15 08:08:43 +08:00
|
|
|
ret = sdw_bus_wait_for_clk_prep_deprep(bus,
|
|
|
|
SDW_BROADCAST_DEV_NUM);
|
2021-05-11 11:00:47 +08:00
|
|
|
/*
|
|
|
|
* if there are no Slave devices present and the reply is
|
|
|
|
* Command_Ignored/-ENODATA, we don't need to continue with the
|
|
|
|
* flow and can just return here. The error code is not modified
|
|
|
|
* and its handling left as an exercise for the caller.
|
|
|
|
*/
|
2020-01-15 08:08:43 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inform slaves that prep is done */
|
|
|
|
list_for_each_entry(slave, &bus->slaves, node) {
|
|
|
|
if (!slave->dev_num)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (slave->status != SDW_SLAVE_ATTACHED &&
|
|
|
|
slave->status != SDW_SLAVE_ALERT)
|
|
|
|
continue;
|
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
ret = sdw_slave_clk_stop_callback(slave,
|
|
|
|
SDW_CLK_STOP_MODE0,
|
|
|
|
SDW_CLK_POST_PREPARE);
|
2020-01-15 08:08:43 +08:00
|
|
|
|
2021-05-11 11:00:47 +08:00
|
|
|
if (ret < 0 && ret != -ENODATA) {
|
|
|
|
dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
|
|
|
|
return ret;
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 11:00:47 +08:00
|
|
|
return 0;
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_bus_clk_stop: stop bus clock
|
|
|
|
*
|
|
|
|
* @bus: SDW bus instance
|
|
|
|
*
|
|
|
|
* After preparing the Slaves for clock stop, stop the clock by broadcasting
|
|
|
|
* write to SCP_CTRL register.
|
|
|
|
*/
|
|
|
|
int sdw_bus_clk_stop(struct sdw_bus *bus)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* broadcast clock stop now, attached Slaves will ACK this,
|
|
|
|
* unattached will ignore
|
|
|
|
*/
|
|
|
|
ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
|
|
|
|
SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
|
|
|
|
if (ret < 0) {
|
2021-05-11 11:00:47 +08:00
|
|
|
if (ret != -ENODATA)
|
|
|
|
dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
|
2020-01-15 08:08:43 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_bus_clk_stop);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_bus_exit_clk_stop: Exit clock stop mode
|
|
|
|
*
|
|
|
|
* @bus: SDW bus instance
|
|
|
|
*
|
|
|
|
* This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
|
|
|
|
* exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
|
|
|
|
* back.
|
|
|
|
*/
|
|
|
|
int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
|
|
|
|
{
|
|
|
|
bool simple_clk_stop = true;
|
|
|
|
struct sdw_slave *slave;
|
|
|
|
bool is_slave = false;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In order to save on transition time, de-prepare
|
|
|
|
* each Slave and then wait for all Slave(s) to be
|
|
|
|
* de-prepared after clock resume.
|
|
|
|
*/
|
|
|
|
list_for_each_entry(slave, &bus->slaves, node) {
|
|
|
|
if (!slave->dev_num)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (slave->status != SDW_SLAVE_ATTACHED &&
|
|
|
|
slave->status != SDW_SLAVE_ALERT)
|
|
|
|
continue;
|
|
|
|
|
2020-05-31 23:18:06 +08:00
|
|
|
/* Identify if Slave(s) are available on Bus */
|
|
|
|
is_slave = true;
|
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
|
2020-01-15 08:08:43 +08:00
|
|
|
SDW_CLK_PRE_DEPREPARE);
|
|
|
|
if (ret < 0)
|
2021-05-11 11:00:47 +08:00
|
|
|
dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
|
2020-01-15 08:08:43 +08:00
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
/* Only de-prepare a Slave device if needed */
|
|
|
|
if (!slave->prop.simple_clk_stop_capable) {
|
|
|
|
simple_clk_stop = false;
|
2020-01-15 08:08:43 +08:00
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
|
|
|
|
false);
|
2020-01-15 08:08:43 +08:00
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
if (ret < 0)
|
2021-05-11 11:00:47 +08:00
|
|
|
dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
|
2021-05-11 11:00:45 +08:00
|
|
|
}
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:54:39 +08:00
|
|
|
/* Skip remaining clock stop de-preparation if no Slave is attached */
|
2020-05-31 23:18:06 +08:00
|
|
|
if (!is_slave)
|
|
|
|
return 0;
|
|
|
|
|
2021-05-11 11:00:45 +08:00
|
|
|
/*
|
|
|
|
* Don't wait for all Slaves to be ready if they follow the simple
|
|
|
|
* state machine
|
|
|
|
*/
|
2021-05-11 11:00:47 +08:00
|
|
|
if (!simple_clk_stop) {
|
|
|
|
ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
|
|
|
|
if (ret < 0)
|
2021-10-12 18:15:21 +08:00
|
|
|
dev_warn(bus->dev, "clock stop deprepare wait failed:%d\n", ret);
|
2021-05-11 11:00:47 +08:00
|
|
|
}
|
2021-01-26 16:54:39 +08:00
|
|
|
|
2020-01-15 08:08:43 +08:00
|
|
|
list_for_each_entry(slave, &bus->slaves, node) {
|
|
|
|
if (!slave->dev_num)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (slave->status != SDW_SLAVE_ATTACHED &&
|
|
|
|
slave->status != SDW_SLAVE_ALERT)
|
|
|
|
continue;
|
|
|
|
|
2021-05-11 11:00:47 +08:00
|
|
|
ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
|
|
|
|
SDW_CLK_POST_DEPREPARE);
|
|
|
|
if (ret < 0)
|
|
|
|
dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
|
2020-01-15 08:08:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
|
|
|
|
|
2018-04-26 21:08:23 +08:00
|
|
|
int sdw_configure_dpn_intr(struct sdw_slave *slave,
|
2019-05-01 23:57:27 +08:00
|
|
|
int port, bool enable, int mask)
|
2018-04-26 21:08:23 +08:00
|
|
|
{
|
|
|
|
u32 addr;
|
|
|
|
int ret;
|
|
|
|
u8 val = 0;
|
|
|
|
|
2020-09-21 03:32:05 +08:00
|
|
|
if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
|
|
|
|
dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
|
|
|
|
enable ? "on" : "off");
|
|
|
|
mask |= SDW_DPN_INT_TEST_FAIL;
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:08:23 +08:00
|
|
|
addr = SDW_DPN_INTMASK(port);
|
|
|
|
|
|
|
|
/* Set/Clear port ready interrupt mask */
|
|
|
|
if (enable) {
|
|
|
|
val |= mask;
|
|
|
|
val |= SDW_DPN_INT_PORT_READY;
|
|
|
|
} else {
|
|
|
|
val &= ~(mask);
|
|
|
|
val &= ~SDW_DPN_INT_PORT_READY;
|
|
|
|
}
|
|
|
|
|
2022-11-25 22:20:28 +08:00
|
|
|
ret = sdw_update_no_pm(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
|
2018-04-26 21:08:23 +08:00
|
|
|
if (ret < 0)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DPN_INTMASK write failed:%d\n", val);
|
2018-04-26 21:08:23 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-09 04:54:36 +08:00
|
|
|
static int sdw_slave_set_frequency(struct sdw_slave *slave)
|
|
|
|
{
|
|
|
|
u32 mclk_freq = slave->bus->prop.mclk_freq;
|
|
|
|
u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
|
|
|
|
unsigned int scale;
|
|
|
|
u8 scale_index;
|
|
|
|
u8 base;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* frequency base and scale registers are required for SDCA
|
2022-11-18 10:58:07 +08:00
|
|
|
* devices. They may also be used for 1.2+/non-SDCA devices.
|
|
|
|
* Driver can set the property, we will need a DisCo property
|
|
|
|
* to discover this case from platform firmware.
|
2020-06-09 04:54:36 +08:00
|
|
|
*/
|
2022-11-18 10:58:07 +08:00
|
|
|
if (!slave->id.class_id && !slave->prop.clock_reg_supported)
|
2020-06-09 04:54:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!mclk_freq) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map base frequency using Table 89 of SoundWire 1.2 spec.
|
|
|
|
* The order of the tests just follows the specification, this
|
|
|
|
* is not a selection between possible values or a search for
|
|
|
|
* the best value but just a mapping. Only one case per platform
|
|
|
|
* is relevant.
|
|
|
|
* Some BIOS have inconsistent values for mclk_freq but a
|
|
|
|
* correct root so we force the mclk_freq to avoid variations.
|
|
|
|
*/
|
|
|
|
if (!(19200000 % mclk_freq)) {
|
|
|
|
mclk_freq = 19200000;
|
|
|
|
base = SDW_SCP_BASE_CLOCK_19200000_HZ;
|
|
|
|
} else if (!(24000000 % mclk_freq)) {
|
|
|
|
mclk_freq = 24000000;
|
|
|
|
base = SDW_SCP_BASE_CLOCK_24000000_HZ;
|
|
|
|
} else if (!(24576000 % mclk_freq)) {
|
|
|
|
mclk_freq = 24576000;
|
|
|
|
base = SDW_SCP_BASE_CLOCK_24576000_HZ;
|
|
|
|
} else if (!(22579200 % mclk_freq)) {
|
|
|
|
mclk_freq = 22579200;
|
|
|
|
base = SDW_SCP_BASE_CLOCK_22579200_HZ;
|
|
|
|
} else if (!(32000000 % mclk_freq)) {
|
|
|
|
mclk_freq = 32000000;
|
|
|
|
base = SDW_SCP_BASE_CLOCK_32000000_HZ;
|
|
|
|
} else {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"Unsupported clock base, mclk %d\n",
|
|
|
|
mclk_freq);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mclk_freq % curr_freq) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"mclk %d is not multiple of bus curr_freq %d\n",
|
|
|
|
mclk_freq, curr_freq);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
scale = mclk_freq / curr_freq;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map scale to Table 90 of SoundWire 1.2 spec - and check
|
|
|
|
* that the scale is a power of two and maximum 64
|
|
|
|
*/
|
|
|
|
scale_index = ilog2(scale);
|
|
|
|
|
|
|
|
if (BIT(scale_index) != scale || scale_index > 6) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"No match found for scale %d, bus mclk %d curr_freq %d\n",
|
|
|
|
scale, mclk_freq, curr_freq);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
scale_index++;
|
|
|
|
|
2021-01-22 15:06:27 +08:00
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
|
2020-06-09 04:54:36 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize scale for both banks */
|
2021-01-22 15:06:27 +08:00
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
|
2020-06-09 04:54:36 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2021-01-22 15:06:27 +08:00
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
|
2020-06-09 04:54:36 +08:00
|
|
|
if (ret < 0)
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
|
|
|
|
|
|
|
|
dev_dbg(&slave->dev,
|
|
|
|
"Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
|
|
|
|
base, scale_index, mclk_freq, curr_freq);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:39 +08:00
|
|
|
static int sdw_initialize_slave(struct sdw_slave *slave)
|
|
|
|
{
|
|
|
|
struct sdw_slave_prop *prop = &slave->prop;
|
2021-03-02 16:27:19 +08:00
|
|
|
int status;
|
2017-12-14 13:49:39 +08:00
|
|
|
int ret;
|
|
|
|
u8 val;
|
|
|
|
|
2020-06-09 04:54:36 +08:00
|
|
|
ret = sdw_slave_set_frequency(slave);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-03-02 16:27:19 +08:00
|
|
|
if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
|
|
|
|
/* Clear bus clash interrupt before enabling interrupt mask */
|
|
|
|
status = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
|
|
|
if (status < 0) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (status & SDW_SCP_INT1_BUS_CLASH) {
|
|
|
|
dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
|
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
|
|
|
|
!(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
|
|
|
|
/* Clear parity interrupt before enabling interrupt mask */
|
|
|
|
status = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
|
|
|
if (status < 0) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (status & SDW_SCP_INT1_PARITY) {
|
|
|
|
dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
|
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&slave->dev,
|
|
|
|
"SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:39 +08:00
|
|
|
/*
|
2020-09-08 21:45:15 +08:00
|
|
|
* Set SCP_INT1_MASK register, typically bus clash and
|
|
|
|
* implementation-defined interrupt mask. The Parity detection
|
|
|
|
* may not always be correct on startup so its use is
|
|
|
|
* device-dependent, it might e.g. only be enabled in
|
|
|
|
* steady-state after a couple of frames.
|
2017-12-14 13:49:39 +08:00
|
|
|
*/
|
2020-09-08 21:45:15 +08:00
|
|
|
val = slave->prop.scp_int1_mask;
|
2017-12-14 13:49:39 +08:00
|
|
|
|
|
|
|
/* Enable SCP interrupts */
|
2021-01-22 15:06:26 +08:00
|
|
|
ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
|
2017-12-14 13:49:39 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_SCP_INTMASK1 write failed:%d\n", ret);
|
2017-12-14 13:49:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No need to continue if DP0 is not present */
|
|
|
|
if (!slave->prop.dp0_prop)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Enable DP0 interrupts */
|
2019-05-23 03:47:25 +08:00
|
|
|
val = prop->dp0_prop->imp_def_interrupts;
|
2017-12-14 13:49:39 +08:00
|
|
|
val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
|
|
|
|
|
2021-01-22 15:06:26 +08:00
|
|
|
ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
|
2020-02-28 06:09:49 +08:00
|
|
|
if (ret < 0)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DP0_INTMASK read failed:%d\n", ret);
|
2020-02-28 06:09:49 +08:00
|
|
|
return ret;
|
2017-12-14 13:49:39 +08:00
|
|
|
}
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
|
|
|
|
{
|
2020-11-24 09:33:17 +08:00
|
|
|
u8 clear, impl_int_mask;
|
2017-12-14 13:49:40 +08:00
|
|
|
int status, status2, ret, count = 0;
|
|
|
|
|
2021-01-22 15:06:28 +08:00
|
|
|
status = sdw_read_no_pm(slave, SDW_DP0_INT);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (status < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DP0_INT read failed:%d\n", status);
|
2017-12-14 13:49:40 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2020-11-24 09:33:17 +08:00
|
|
|
clear = status & ~SDW_DP0_INTERRUPTS;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
if (status & SDW_DP0_INT_TEST_FAIL) {
|
2019-05-01 23:57:45 +08:00
|
|
|
dev_err(&slave->dev, "Test fail for port 0\n");
|
2017-12-14 13:49:40 +08:00
|
|
|
clear |= SDW_DP0_INT_TEST_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assumption: PORT_READY interrupt will be received only for
|
|
|
|
* ports implementing Channel Prepare state machine (CP_SM)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (status & SDW_DP0_INT_PORT_READY) {
|
|
|
|
complete(&slave->port_ready[0]);
|
|
|
|
clear |= SDW_DP0_INT_PORT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status & SDW_DP0_INT_BRA_FAILURE) {
|
2019-05-01 23:57:45 +08:00
|
|
|
dev_err(&slave->dev, "BRA failed\n");
|
2017-12-14 13:49:40 +08:00
|
|
|
clear |= SDW_DP0_INT_BRA_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_int_mask = SDW_DP0_INT_IMPDEF1 |
|
|
|
|
SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
|
|
|
|
|
|
|
|
if (status & impl_int_mask) {
|
|
|
|
clear |= impl_int_mask;
|
|
|
|
*slave_status = clear;
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:33:17 +08:00
|
|
|
/* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DP0_INT write failed:%d\n", ret);
|
2017-12-14 13:49:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read DP0 interrupt again */
|
2021-01-22 15:06:28 +08:00
|
|
|
status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (status2 < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DP0_INT read failed:%d\n", status2);
|
2018-01-09 00:52:44 +08:00
|
|
|
return status2;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
2020-11-24 09:33:14 +08:00
|
|
|
/* filter to limit loop to interrupts identified in the first status read */
|
2017-12-14 13:49:40 +08:00
|
|
|
status &= status2;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* we can get alerts while processing so keep retrying */
|
2020-11-24 09:33:17 +08:00
|
|
|
} while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
if (count == SDW_READ_INTR_CLEAR_RETRY)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdw_handle_port_interrupt(struct sdw_slave *slave,
|
2019-05-01 23:57:27 +08:00
|
|
|
int port, u8 *slave_status)
|
2017-12-14 13:49:40 +08:00
|
|
|
{
|
2020-11-24 09:33:18 +08:00
|
|
|
u8 clear, impl_int_mask;
|
2017-12-14 13:49:40 +08:00
|
|
|
int status, status2, ret, count = 0;
|
|
|
|
u32 addr;
|
|
|
|
|
|
|
|
if (port == 0)
|
|
|
|
return sdw_handle_dp0_interrupt(slave, slave_status);
|
|
|
|
|
|
|
|
addr = SDW_DPN_INT(port);
|
2021-01-22 15:06:28 +08:00
|
|
|
status = sdw_read_no_pm(slave, addr);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (status < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DPN_INT read failed:%d\n", status);
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2020-11-24 09:33:18 +08:00
|
|
|
clear = status & ~SDW_DPN_INTERRUPTS;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
if (status & SDW_DPN_INT_TEST_FAIL) {
|
2019-05-01 23:57:45 +08:00
|
|
|
dev_err(&slave->dev, "Test fail for port:%d\n", port);
|
2017-12-14 13:49:40 +08:00
|
|
|
clear |= SDW_DPN_INT_TEST_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assumption: PORT_READY interrupt will be received only
|
|
|
|
* for ports implementing CP_SM.
|
|
|
|
*/
|
|
|
|
if (status & SDW_DPN_INT_PORT_READY) {
|
|
|
|
complete(&slave->port_ready[port]);
|
|
|
|
clear |= SDW_DPN_INT_PORT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_int_mask = SDW_DPN_INT_IMPDEF1 |
|
|
|
|
SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
|
|
|
|
|
|
|
|
if (status & impl_int_mask) {
|
|
|
|
clear |= impl_int_mask;
|
|
|
|
*slave_status = clear;
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:33:18 +08:00
|
|
|
/* clear the interrupt but don't touch reserved fields */
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_write_no_pm(slave, addr, clear);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DPN_INT write failed:%d\n", ret);
|
2017-12-14 13:49:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read DPN interrupt again */
|
2021-01-22 15:06:28 +08:00
|
|
|
status2 = sdw_read_no_pm(slave, addr);
|
2018-01-09 00:52:44 +08:00
|
|
|
if (status2 < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_DPN_INT read failed:%d\n", status2);
|
2018-01-09 00:52:44 +08:00
|
|
|
return status2;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
2020-11-24 09:33:14 +08:00
|
|
|
/* filter to limit loop to interrupts identified in the first status read */
|
2017-12-14 13:49:40 +08:00
|
|
|
status &= status2;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* we can get alerts while processing so keep retrying */
|
2020-11-24 09:33:18 +08:00
|
|
|
} while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
if (count == SDW_READ_INTR_CLEAR_RETRY)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdw_handle_slave_alerts(struct sdw_slave *slave)
|
|
|
|
{
|
|
|
|
struct sdw_slave_intr_status slave_intr;
|
2019-08-30 02:11:35 +08:00
|
|
|
u8 clear = 0, bit, port_status[15] = {0};
|
2017-12-14 13:49:40 +08:00
|
|
|
int port_num, stat, ret, count = 0;
|
|
|
|
unsigned long port;
|
2020-11-24 09:33:15 +08:00
|
|
|
bool slave_notify;
|
2020-11-04 23:23:58 +08:00
|
|
|
u8 sdca_cascade = 0;
|
2017-12-14 13:49:40 +08:00
|
|
|
u8 buf, buf2[2], _buf, _buf2[2];
|
2020-09-08 21:45:18 +08:00
|
|
|
bool parity_check;
|
|
|
|
bool parity_quirk;
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
|
|
|
|
|
2022-04-27 07:56:20 +08:00
|
|
|
ret = pm_runtime_resume_and_get(&slave->dev);
|
2020-01-15 08:08:42 +08:00
|
|
|
if (ret < 0 && ret != -EACCES) {
|
|
|
|
dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:09:33 +08:00
|
|
|
/* Read Intstat 1, Intstat 2 and Intstat 3 registers */
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_SCP_INT1 read failed:%d\n", ret);
|
2020-01-15 08:08:42 +08:00
|
|
|
goto io_err;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
2019-05-02 18:59:26 +08:00
|
|
|
buf = ret;
|
2017-12-14 13:49:40 +08:00
|
|
|
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_SCP_INT2/3 read failed:%d\n", ret);
|
2020-01-15 08:08:42 +08:00
|
|
|
goto io_err;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
2022-11-18 10:58:06 +08:00
|
|
|
if (slave->id.class_id) {
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_read_no_pm(slave, SDW_DP0_INT);
|
2020-11-04 23:23:58 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2020-11-04 23:23:58 +08:00
|
|
|
"SDW_DP0_INT read failed:%d\n", ret);
|
|
|
|
goto io_err;
|
|
|
|
}
|
|
|
|
sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
|
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
do {
|
2020-11-24 09:33:15 +08:00
|
|
|
slave_notify = false;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
/*
|
|
|
|
* Check parity, bus clash and Slave (impl defined)
|
|
|
|
* interrupt
|
|
|
|
*/
|
|
|
|
if (buf & SDW_SCP_INT1_PARITY) {
|
2020-09-08 21:45:18 +08:00
|
|
|
parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
|
|
|
|
parity_quirk = !slave->first_interrupt_done &&
|
|
|
|
(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
|
|
|
|
|
|
|
|
if (parity_check && !parity_quirk)
|
2020-09-08 21:45:16 +08:00
|
|
|
dev_err(&slave->dev, "Parity error detected\n");
|
2017-12-14 13:49:40 +08:00
|
|
|
clear |= SDW_SCP_INT1_PARITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf & SDW_SCP_INT1_BUS_CLASH) {
|
2020-09-08 21:45:16 +08:00
|
|
|
if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
|
|
|
|
dev_err(&slave->dev, "Bus clash detected\n");
|
2017-12-14 13:49:40 +08:00
|
|
|
clear |= SDW_SCP_INT1_BUS_CLASH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When bus clash or parity errors are detected, such errors
|
|
|
|
* are unlikely to be recoverable errors.
|
|
|
|
* TODO: In such scenario, reset bus. Make this configurable
|
|
|
|
* via sysfs property with bus reset being the default.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buf & SDW_SCP_INT1_IMPL_DEF) {
|
2020-09-08 21:45:16 +08:00
|
|
|
if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
|
|
|
|
dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
|
|
|
|
slave_notify = true;
|
|
|
|
}
|
2017-12-14 13:49:40 +08:00
|
|
|
clear |= SDW_SCP_INT1_IMPL_DEF;
|
|
|
|
}
|
|
|
|
|
2020-11-04 23:23:58 +08:00
|
|
|
/* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
|
|
|
|
if (sdca_cascade)
|
|
|
|
slave_notify = true;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
/* Check port 0 - 3 interrupts */
|
|
|
|
port = buf & SDW_SCP_INT1_PORT0_3;
|
|
|
|
|
|
|
|
/* To get port number corresponding to bits, shift it */
|
2020-09-03 19:44:57 +08:00
|
|
|
port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
|
2017-12-14 13:49:40 +08:00
|
|
|
for_each_set_bit(bit, &port, 8) {
|
|
|
|
sdw_handle_port_interrupt(slave, bit,
|
2019-05-01 23:57:27 +08:00
|
|
|
&port_status[bit]);
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if cascade 2 interrupt is present */
|
|
|
|
if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
|
|
|
|
port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
|
|
|
|
for_each_set_bit(bit, &port, 8) {
|
|
|
|
/* scp2 ports start from 4 */
|
2022-09-17 22:02:56 +08:00
|
|
|
port_num = bit + 4;
|
2017-12-14 13:49:40 +08:00
|
|
|
sdw_handle_port_interrupt(slave,
|
|
|
|
port_num,
|
|
|
|
&port_status[port_num]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now check last cascade */
|
|
|
|
if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
|
|
|
|
port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
|
|
|
|
for_each_set_bit(bit, &port, 8) {
|
|
|
|
/* scp3 ports start from 11 */
|
2022-09-17 22:02:56 +08:00
|
|
|
port_num = bit + 11;
|
2017-12-14 13:49:40 +08:00
|
|
|
sdw_handle_port_interrupt(slave,
|
|
|
|
port_num,
|
|
|
|
&port_status[port_num]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the Slave driver */
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
if (slave_notify) {
|
|
|
|
mutex_lock(&slave->sdw_dev_lock);
|
|
|
|
|
|
|
|
if (slave->probed) {
|
|
|
|
struct device *dev = &slave->dev;
|
|
|
|
struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
|
|
|
|
|
|
|
if (drv->ops && drv->ops->interrupt_callback) {
|
|
|
|
slave_intr.sdca_cascade = sdca_cascade;
|
|
|
|
slave_intr.control_port = clear;
|
|
|
|
memcpy(slave_intr.port, &port_status,
|
|
|
|
sizeof(slave_intr.port));
|
|
|
|
|
|
|
|
drv->ops->interrupt_callback(slave, &slave_intr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&slave->sdw_dev_lock);
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ack interrupt */
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"SDW_SCP_INT1 write failed:%d\n", ret);
|
2020-01-15 08:08:42 +08:00
|
|
|
goto io_err;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
2020-09-08 21:45:17 +08:00
|
|
|
/* at this point all initial interrupt sources were handled */
|
|
|
|
slave->first_interrupt_done = true;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
/*
|
|
|
|
* Read status again to ensure no new interrupts arrived
|
|
|
|
* while servicing interrupts.
|
|
|
|
*/
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2021-03-02 17:11:16 +08:00
|
|
|
"SDW_SCP_INT1 recheck read failed:%d\n", ret);
|
2020-01-15 08:08:42 +08:00
|
|
|
goto io_err;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
2019-05-02 18:59:26 +08:00
|
|
|
_buf = ret;
|
2017-12-14 13:49:40 +08:00
|
|
|
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, _buf2);
|
2017-12-14 13:49:40 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2021-03-02 17:11:16 +08:00
|
|
|
"SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
|
2020-01-15 08:08:42 +08:00
|
|
|
goto io_err;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
2022-11-18 10:58:06 +08:00
|
|
|
if (slave->id.class_id) {
|
2021-01-22 15:06:28 +08:00
|
|
|
ret = sdw_read_no_pm(slave, SDW_DP0_INT);
|
2020-11-04 23:23:58 +08:00
|
|
|
if (ret < 0) {
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2021-03-02 17:11:16 +08:00
|
|
|
"SDW_DP0_INT recheck read failed:%d\n", ret);
|
2020-11-04 23:23:58 +08:00
|
|
|
goto io_err;
|
|
|
|
}
|
|
|
|
sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
|
|
|
|
}
|
|
|
|
|
2020-11-24 09:33:14 +08:00
|
|
|
/*
|
|
|
|
* Make sure no interrupts are pending, but filter to limit loop
|
|
|
|
* to interrupts identified in the first status read
|
|
|
|
*/
|
2017-12-14 13:49:40 +08:00
|
|
|
buf &= _buf;
|
|
|
|
buf2[0] &= _buf2[0];
|
|
|
|
buf2[1] &= _buf2[1];
|
2020-11-04 23:23:58 +08:00
|
|
|
stat = buf || buf2[0] || buf2[1] || sdca_cascade;
|
2017-12-14 13:49:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit loop if Slave is continuously in ALERT state even
|
|
|
|
* after servicing the interrupt multiple times.
|
|
|
|
*/
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* we can get alerts while processing so keep retrying */
|
|
|
|
} while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
|
|
|
|
|
|
|
|
if (count == SDW_READ_INTR_CLEAR_RETRY)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
|
2017-12-14 13:49:40 +08:00
|
|
|
|
2020-01-15 08:08:42 +08:00
|
|
|
io_err:
|
|
|
|
pm_runtime_mark_last_busy(&slave->dev);
|
|
|
|
pm_runtime_put_autosuspend(&slave->dev);
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdw_update_slave_status(struct sdw_slave *slave,
|
2019-05-01 23:57:27 +08:00
|
|
|
enum sdw_slave_status status)
|
2017-12-14 13:49:40 +08:00
|
|
|
{
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
int ret = 0;
|
2017-12-14 13:49:40 +08:00
|
|
|
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
mutex_lock(&slave->sdw_dev_lock);
|
|
|
|
|
|
|
|
if (slave->probed) {
|
|
|
|
struct device *dev = &slave->dev;
|
|
|
|
struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
|
|
|
|
|
|
|
if (drv->ops && drv->ops->update_status)
|
|
|
|
ret = drv->ops->update_status(slave, status);
|
2020-01-15 08:08:35 +08:00
|
|
|
}
|
|
|
|
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
mutex_unlock(&slave->sdw_dev_lock);
|
2020-01-15 08:08:35 +08:00
|
|
|
|
soundwire: revisit driver bind/unbind and callbacks
In the SoundWire probe, we store a pointer from the driver ops into
the 'slave' structure. This can lead to kernel oopses when unbinding
codec drivers, e.g. with the following sequence to remove machine
driver and codec driver.
/sbin/modprobe -r snd_soc_sof_sdw
/sbin/modprobe -r snd_soc_rt711
The full details can be found in the BugLink below, for reference the
two following examples show different cases of driver ops/callbacks
being invoked after the driver .remove().
kernel: BUG: kernel NULL pointer dereference, address: 0000000000000150
kernel: Workqueue: events cdns_update_slave_status_work [soundwire_cadence]
kernel: RIP: 0010:mutex_lock+0x19/0x30
kernel: Call Trace:
kernel: ? sdw_handle_slave_status+0x426/0xe00 [soundwire_bus 94ff184bf398570c3f8ff7efe9e32529f532e4ae]
kernel: ? newidle_balance+0x26a/0x400
kernel: ? cdns_update_slave_status_work+0x1e9/0x200 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: BUG: unable to handle page fault for address: ffffffffc07654c8
kernel: Workqueue: pm pm_runtime_work
kernel: RIP: 0010:sdw_bus_prep_clk_stop+0x6f/0x160 [soundwire_bus]
kernel: Call Trace:
kernel: <TASK>
kernel: sdw_cdns_clock_stop+0xb5/0x1b0 [soundwire_cadence 1bcf98eebe5ba9833cd433323769ac923c9c6f82]
kernel: intel_suspend_runtime+0x5f/0x120 [soundwire_intel aca858f7c87048d3152a4a41bb68abb9b663a1dd]
kernel: ? dpm_sysfs_remove+0x60/0x60
This was not detected earlier in Intel tests since the tests first
remove the parent PCI device and shut down the bus. The sequence
above is a corner case which keeps the bus operational but without a
driver bound.
While trying to solve this kernel oopses, it became clear that the
existing SoundWire bus does not deal well with the unbind case.
Commit 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
added a 'probed' status variable and a 'probe_complete'
struct completion. This status is however not reset on remove and
likewise the 'probe complete' is not re-initialized, so the
bind/unbind/bind test cases would fail. The timeout used before the
'update_status' callback was also a bad idea in hindsight, there
should really be no timing assumption as to if and when a driver is
bound to a device.
An initial draft was based on device_lock() and device_unlock() was
tested. This proved too complicated, with deadlocks created during the
suspend-resume sequences, which also use the same device_lock/unlock()
as the bind/unbind sequences. On a CometLake device, a bad DSDT/BIOS
caused spurious resumes and the use of device_lock() caused hangs
during suspend. After multiple weeks or testing and painful
reverse-engineering of deadlocks on different devices, we looked for
alternatives that did not interfere with the device core.
A bus notifier was used successfully to keep track of DRIVER_BOUND and
DRIVER_UNBIND events. This solved the bind-unbind-bind case in tests,
but it can still be defeated with a theoretical corner case where the
memory is freed by a .remove while the callback is in use. The
notifier only helps make sure the driver callbacks are valid, but not
that the memory allocated in probe remains valid while the callbacks
are invoked.
This patch suggests the introduction of a new 'sdw_dev_lock' mutex
protecting probe/remove and all driver callbacks. Since this mutex is
'local' to SoundWire only, it does not interfere with existing locks
and does not create deadlocks. In addition, this patch removes the
'probe_complete' completion, instead we directly invoke the
'update_status' from the probe routine. That removes any sort of
timing dependency and a much better support for the device/driver
model, the driver could be bound before the bus started, or eons after
the bus started and the hardware would be properly initialized in all
cases.
BugLink: https://github.com/thesofproject/linux/issues/3531
Fixes: 56d4fe31af77 ("soundwire: Add MIPI DisCo property helpers")
Fixes: 528be501b7d4a ("soundwire: sdw_slave: add probe_complete structure and new fields")
Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Rander Wang <rander.wang@intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Link: https://lore.kernel.org/r/20220621225641.221170-2-pierre-louis.bossart@linux.intel.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>
2022-06-22 06:56:38 +08:00
|
|
|
return ret;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sdw_handle_slave_status() - Handle Slave status
|
|
|
|
* @bus: SDW bus instance
|
|
|
|
* @status: Status for all Slave(s)
|
|
|
|
*/
|
|
|
|
int sdw_handle_slave_status(struct sdw_bus *bus,
|
2019-05-01 23:57:27 +08:00
|
|
|
enum sdw_slave_status status[])
|
2017-12-14 13:49:40 +08:00
|
|
|
{
|
|
|
|
enum sdw_slave_status prev_status;
|
|
|
|
struct sdw_slave *slave;
|
2022-09-15 00:02:48 +08:00
|
|
|
bool attached_initializing, id_programmed;
|
2017-12-14 13:49:40 +08:00
|
|
|
int i, ret = 0;
|
|
|
|
|
2020-01-11 05:57:31 +08:00
|
|
|
/* first check if any Slaves fell off the bus */
|
|
|
|
for (i = 1; i <= SDW_MAX_DEVICES; i++) {
|
|
|
|
mutex_lock(&bus->bus_lock);
|
|
|
|
if (test_bit(i, bus->assigned) == false) {
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
|
|
|
|
slave = sdw_get_slave(bus, i);
|
|
|
|
if (!slave)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (status[i] == SDW_SLAVE_UNATTACHED &&
|
2022-01-26 09:15:27 +08:00
|
|
|
slave->status != SDW_SLAVE_UNATTACHED) {
|
|
|
|
dev_warn(&slave->dev, "Slave %d state check1: UNATTACHED, status was %d\n",
|
|
|
|
i, slave->status);
|
2020-01-11 05:57:31 +08:00
|
|
|
sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
2022-09-15 00:02:45 +08:00
|
|
|
|
|
|
|
/* Ensure driver knows that peripheral unattached */
|
|
|
|
ret = sdw_update_slave_status(slave, status[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
dev_warn(&slave->dev, "Update Slave status failed:%d\n", ret);
|
2022-01-26 09:15:27 +08:00
|
|
|
}
|
2020-01-11 05:57:31 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
if (status[0] == SDW_SLAVE_ATTACHED) {
|
2019-08-06 08:55:09 +08:00
|
|
|
dev_dbg(bus->dev, "Slave attached, programming device number\n");
|
2022-09-15 00:02:48 +08:00
|
|
|
|
2019-07-26 07:40:10 +08:00
|
|
|
/*
|
2022-09-15 00:02:48 +08:00
|
|
|
* Programming a device number will have side effects,
|
|
|
|
* so we deal with other devices at a later time.
|
|
|
|
* This relies on those devices reporting ATTACHED, which will
|
|
|
|
* trigger another call to this function. This will only
|
|
|
|
* happen if at least one device ID was programmed.
|
|
|
|
* Error returns from sdw_program_device_num() are currently
|
|
|
|
* ignored because there's no useful recovery that can be done.
|
|
|
|
* Returning the error here could result in the current status
|
|
|
|
* of other devices not being handled, because if no device IDs
|
|
|
|
* were programmed there's nothing to guarantee a status change
|
|
|
|
* to trigger another call to this function.
|
2019-07-26 07:40:10 +08:00
|
|
|
*/
|
2022-09-15 00:02:48 +08:00
|
|
|
sdw_program_device_num(bus, &id_programmed);
|
|
|
|
if (id_programmed)
|
|
|
|
return 0;
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue to check other slave statuses */
|
|
|
|
for (i = 1; i <= SDW_MAX_DEVICES; i++) {
|
|
|
|
mutex_lock(&bus->bus_lock);
|
|
|
|
if (test_bit(i, bus->assigned) == false) {
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
|
|
|
|
slave = sdw_get_slave(bus, i);
|
|
|
|
if (!slave)
|
|
|
|
continue;
|
|
|
|
|
2020-01-15 08:08:37 +08:00
|
|
|
attached_initializing = false;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
switch (status[i]) {
|
|
|
|
case SDW_SLAVE_UNATTACHED:
|
|
|
|
if (slave->status == SDW_SLAVE_UNATTACHED)
|
|
|
|
break;
|
|
|
|
|
2022-01-26 09:15:27 +08:00
|
|
|
dev_warn(&slave->dev, "Slave %d state check2: UNATTACHED, status was %d\n",
|
|
|
|
i, slave->status);
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDW_SLAVE_ALERT:
|
|
|
|
ret = sdw_handle_slave_alerts(slave);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"Slave %d alert handling failed: %d\n",
|
2017-12-14 13:49:40 +08:00
|
|
|
i, ret);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDW_SLAVE_ATTACHED:
|
|
|
|
if (slave->status == SDW_SLAVE_ATTACHED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
prev_status = slave->status;
|
|
|
|
sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
|
|
|
|
|
|
|
|
if (prev_status == SDW_SLAVE_ALERT)
|
|
|
|
break;
|
|
|
|
|
2020-01-15 08:08:37 +08:00
|
|
|
attached_initializing = true;
|
|
|
|
|
2017-12-14 13:49:40 +08:00
|
|
|
ret = sdw_initialize_slave(slave);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"Slave %d initialization failed: %d\n",
|
2017-12-14 13:49:40 +08:00
|
|
|
i, ret);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev, "Invalid slave %d status:%d\n",
|
2019-05-01 23:57:27 +08:00
|
|
|
i, status[i]);
|
2017-12-14 13:49:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sdw_update_slave_status(slave, status[i]);
|
2021-03-02 17:11:14 +08:00
|
|
|
if (ret < 0)
|
2021-01-22 15:06:34 +08:00
|
|
|
dev_err(&slave->dev,
|
2019-05-01 23:57:45 +08:00
|
|
|
"Update Slave status failed:%d\n", ret);
|
2021-01-26 16:54:02 +08:00
|
|
|
if (attached_initializing) {
|
|
|
|
dev_dbg(&slave->dev,
|
2022-08-23 13:01:57 +08:00
|
|
|
"signaling initialization completion for Slave %d\n",
|
|
|
|
slave->dev_num);
|
2021-01-26 16:54:02 +08:00
|
|
|
|
2020-01-15 08:08:37 +08:00
|
|
|
complete(&slave->initialization_complete);
|
2022-04-20 10:32:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the manager became pm_runtime active, the peripherals will be
|
|
|
|
* restarted and attach, but their pm_runtime status may remain
|
|
|
|
* suspended. If the 'update_slave_status' callback initiates
|
|
|
|
* any sort of deferred processing, this processing would not be
|
|
|
|
* cancelled on pm_runtime suspend.
|
|
|
|
* To avoid such zombie states, we queue a request to resume.
|
|
|
|
* This would be a no-op in case the peripheral was being resumed
|
|
|
|
* by e.g. the ALSA/ASoC framework.
|
|
|
|
*/
|
|
|
|
pm_request_resume(&slave->dev);
|
2021-01-26 16:54:02 +08:00
|
|
|
}
|
2017-12-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_handle_slave_status);
|
2020-01-15 08:08:40 +08:00
|
|
|
|
|
|
|
void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
|
|
|
|
{
|
|
|
|
struct sdw_slave *slave;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Check all non-zero devices */
|
|
|
|
for (i = 1; i <= SDW_MAX_DEVICES; i++) {
|
|
|
|
mutex_lock(&bus->bus_lock);
|
|
|
|
if (test_bit(i, bus->assigned) == false) {
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mutex_unlock(&bus->bus_lock);
|
|
|
|
|
|
|
|
slave = sdw_get_slave(bus, i);
|
|
|
|
if (!slave)
|
|
|
|
continue;
|
|
|
|
|
2020-09-08 21:45:17 +08:00
|
|
|
if (slave->status != SDW_SLAVE_UNATTACHED) {
|
2020-01-15 08:08:40 +08:00
|
|
|
sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
2020-09-08 21:45:17 +08:00
|
|
|
slave->first_interrupt_done = false;
|
2021-07-14 11:36:09 +08:00
|
|
|
sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
2020-09-08 21:45:17 +08:00
|
|
|
}
|
2020-01-15 08:08:40 +08:00
|
|
|
|
|
|
|
/* keep track of request, used in pm_runtime resume */
|
|
|
|
slave->unattach_request = request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(sdw_clear_slave_status);
|