Skip to content
Merged
26 changes: 26 additions & 0 deletions drivers/media/platform/apple/isp/.kunitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Full driver suites on arm64 (requires an arm64 QEMU):
# ./tools/testing/kunit/kunit.py run --arch=arm64 \
# --kunitconfig=drivers/media/platform/apple/isp
# The independent IPC layout suite can also run alone on UML.
CONFIG_KUNIT=y
CONFIG_COMPILE_TEST=y
CONFIG_OF=y
CONFIG_PM=y
CONFIG_MEDIA_SUPPORT=y
CONFIG_MEDIA_SUPPORT_FILTER=y
CONFIG_MEDIA_CAMERA_SUPPORT=y
CONFIG_MEDIA_CONTROLLER=y
CONFIG_VIDEO_DEV=y
CONFIG_MEDIA_PLATFORM_SUPPORT=y
CONFIG_MEDIA_PLATFORM_DRIVERS=y
CONFIG_V4L_PLATFORM_DRIVERS=y
CONFIG_VIDEO_V4L2_SUBDEV_API=y
CONFIG_VIDEO_APPLE_ISP=y
CONFIG_VIDEO_APPLE_ISP_KUNIT_TEST=y
CONFIG_VIDEO_APPLE_ISP_IPC_LAYOUT_KUNIT_TEST=y
CONFIG_DRM=y
CONFIG_IOMMU_SUPPORT=y
CONFIG_FW_LOADER=y
CONFIG_DEBUG_KERNEL=y
CONFIG_PROVE_LOCKING=y
CONFIG_DEBUG_SPINLOCK=y
4 changes: 2 additions & 2 deletions drivers/media/platform/apple/isp/isp-cam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,8 @@ void apple_isp_stop_capture(struct apple_isp *isp)
{
if (isp->ipc_failed || isp->fw_state == ISP_FW_DEAD)
return;
isp_cmd_ch_stop(isp, 0); // TODO channel mask
isp_cmd_ch_buffer_return(isp, isp->current_ch);
if (!isp_cmd_ch_stop(isp, 0)) /* TODO channel mask */
isp_cmd_ch_buffer_return(isp, isp->current_ch);

/* ... until its buffers are back. */
apple_isp_wdt_stop(isp);
Expand Down
3 changes: 3 additions & 0 deletions drivers/media/platform/apple/isp/isp-cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int isp_cmd_send(struct apple_isp *isp, void *args, u32 insize, u32 outsize, int
struct isp_message *req;
int err;

if (READ_ONCE(isp->ipc_error))
return READ_ONCE(isp->ipc_error);

#if IS_ENABLED(CONFIG_VIDEO_APPLE_ISP_KUNIT_TEST)
KUNIT_STATIC_STUB_REDIRECT(isp_cmd_send, isp, args, insize, outsize, timeout);
#endif
Expand Down
4 changes: 4 additions & 0 deletions drivers/media/platform/apple/isp/isp-drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ struct apple_isp {
} fw;
/* changes under video_lock once the video device is registered */
enum isp_fw_state fw_state;
/* First IPC failure; cleared only after fresh channel initialization. */
int ipc_error;
/* End the current receive invocation before resident reclamation. */
bool ipc_quiescing;
/* firmware that starts again, idling until fw_idle_work stops it */
bool fw_idle;
struct delayed_work fw_idle_work;
Expand Down
53 changes: 44 additions & 9 deletions drivers/media/platform/apple/isp/isp-fw.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,24 @@ static irqreturn_t apple_isp_isr(int irq, void *dev)
static irqreturn_t apple_isp_isr_thread(int irq, void *dev)
{
struct apple_isp *isp = dev;
bool more;

wake_up_all(&isp->wait);
do {
wake_up_all(&isp->wait);

ipc_chan_handle(isp, isp->chan_sm);
wake_up_all(&isp->wait); /* Some commands depend on sm */
more = ipc_chan_handle(isp, isp->chan_sm) == -EAGAIN;
wake_up_all(&isp->wait); /* Some commands depend on sm */

ipc_chan_handle(isp, isp->chan_tm);
more |= ipc_chan_handle(isp, isp->chan_tm) == -EAGAIN;
more |= ipc_chan_handle(isp, isp->chan_bt) == -EAGAIN;
wake_up_all(&isp->wait);

ipc_chan_handle(isp, isp->chan_bt);
wake_up_all(&isp->wait);
/* Fair turns across channels, without depending on another IRQ. */
cond_resched();
} while (more && !READ_ONCE(isp->ipc_error) &&
!READ_ONCE(isp->ipc_quiescing));

apple_isp_video_error(isp);

return IRQ_HANDLED;
}
Expand Down Expand Up @@ -894,6 +902,9 @@ static int isp_stop_command_processor(struct apple_isp *isp)
{
int res, ret;

if (READ_ONCE(isp->ipc_error))
return READ_ONCE(isp->ipc_error);

isp_gpio_write32(isp, ISP_GPIO_0, 0xf7fbdff9);

/*
Expand Down Expand Up @@ -1041,6 +1052,7 @@ static int isp_firmware_boot(struct apple_isp *isp)
/* Stage 1 stops the coprocessor and powers it down itself. */
isp_err(isp, "failed firmware boot stage 1: %d\n", err);
isp_collect_gc_surface(isp);
cmpxchg(&isp->ipc_error, 0, err);
/* The firmware may have run: leave its memory as loaded. */
isp_restore_fw_memory(isp);
return err;
Expand All @@ -1059,6 +1071,10 @@ static int isp_firmware_boot(struct apple_isp *isp)
goto stop;
}

/* The old coprocessor stopped and all channel slots were initialized. */
WRITE_ONCE(isp->ipc_error, 0);
WRITE_ONCE(isp->ipc_quiescing, false);

err = isp_enable_irq(isp);
if (err < 0) {
isp_err(isp, "failed to enable interrupts: %d\n", err);
Expand All @@ -1076,8 +1092,10 @@ static int isp_firmware_boot(struct apple_isp *isp)
return 0;

disable_irqs:
cmpxchg(&isp->ipc_error, 0, err);
isp_disable_irq(isp);
stop:
cmpxchg(&isp->ipc_error, 0, err);
isp_firmware_stop(isp);
return err;
}
Expand All @@ -1092,6 +1110,8 @@ static void isp_firmware_shutdown(struct apple_isp *isp)
*/
if (isp_stop_command_processor(isp))
dev_warn(isp->dev, "firmware did not suspend, stopping it\n");
/* Also stop a receive thread whose producer never leaves a gap. */
cmpxchg(&isp->ipc_error, 0, -ESHUTDOWN);
isp_disable_irq(isp);
isp_firmware_stop(isp);
}
Expand All @@ -1115,6 +1135,9 @@ int apple_isp_firmware_boot(struct apple_isp *isp)
break;
case ISP_FW_RUNNING:
/* Resident or idling firmware keeps running between streams. */
err = READ_ONCE(isp->ipc_error);
if (err)
return err;
isp->fw_idle = false;
return 0;
case ISP_FW_DEAD:
Expand Down Expand Up @@ -1151,7 +1174,8 @@ int apple_isp_firmware_boot(struct apple_isp *isp)
/* The end of a stream or of the camera detection */
void apple_isp_firmware_shutdown(struct apple_isp *isp)
{
if (!isp->hw->resident_fw && !isp->hw->fw_idle_ms) {
if ((!isp->hw->resident_fw && !isp->hw->fw_idle_ms) ||
READ_ONCE(isp->ipc_error)) {
apple_isp_firmware_halt(isp);
return;
}
Expand All @@ -1163,9 +1187,20 @@ void apple_isp_firmware_shutdown(struct apple_isp *isp)
* the interrupt thread to finish the free requests the firmware has
* already sent.
*/
if (isp->fw_state == ISP_FW_RUNNING)
synchronize_irq(isp->irq);
if (isp->fw_state != ISP_FW_RUNNING)
return;

/* Finish a fair receive round even if the producer keeps refilling. */
WRITE_ONCE(isp->ipc_quiescing, true);
disable_irq(isp->irq);
/* No receive handler can race this lifetime decision while disabled. */
if (READ_ONCE(isp->ipc_error)) {
apple_isp_firmware_halt(isp);
return;
}
isp_free_released_surfaces(isp);
WRITE_ONCE(isp->ipc_quiescing, false);
enable_irq(isp->irq);

/* Not on isp->wq: the stop flushes that. */
if (isp->hw->fw_idle_ms && isp->fw_state == ISP_FW_RUNNING) {
Expand Down
36 changes: 29 additions & 7 deletions drivers/media/platform/apple/isp/isp-ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,27 @@ static inline bool chan_rx_done(struct apple_isp *isp, struct isp_channel *chan)

int ipc_chan_handle(struct apple_isp *isp, struct isp_channel *chan)
{
unsigned int budget = isp_is_p010(isp) ? chan->num : UINT_MAX;
int err = 0;
int err = -EAGAIN;

mutex_lock(&chan->lock);
/* A busy producer must not starve the other completion/control rings. */
while (budget--) {
/* One ring per turn, even if the producer refills acknowledged slots. */
for (u32 i = 0; i < chan->num; i++) {
if (READ_ONCE(isp->ipc_error)) {
err = READ_ONCE(isp->ipc_error);
break;
}
chan_read_msg(isp, chan, &chan->req);
if (chan_rx_done(isp, chan)) {
err = 0;
break;
}
err = chan_handle_once(isp, chan);
if (err < 0) {
cmpxchg(&isp->ipc_error, 0, err);
wake_up_all(&isp->wait);
break;
}
err = -EAGAIN;
}
mutex_unlock(&chan->lock);

Expand Down Expand Up @@ -241,7 +247,10 @@ int ipc_chan_send(struct apple_isp *isp, struct isp_channel *chan,
unsigned long timeout)
{
long t;
int err;
int err = READ_ONCE(isp->ipc_error);

if (err)
return err;

#if IS_ENABLED(CONFIG_VIDEO_APPLE_ISP_KUNIT_TEST)
KUNIT_STATIC_STUB_REDIRECT(ipc_chan_send, isp, chan, timeout);
Expand All @@ -262,7 +271,9 @@ int ipc_chan_send(struct apple_isp *isp, struct isp_channel *chan,
if (!timeout)
return 0;

t = wait_event_timeout(isp->wait, chan_tx_done(isp, chan), timeout);
t = wait_event_timeout(isp->wait,
READ_ONCE(isp->ipc_error) || chan_tx_done(isp, chan),
timeout);
if (t == 0) {
/* Resolved before the next request, see isp_ipc_resync(). */
if (isp_is_p010(isp))
Expand All @@ -271,10 +282,21 @@ int ipc_chan_send(struct apple_isp *isp, struct isp_channel *chan,
"%s: timed out on request [0x%llx, 0x%llx, 0x%llx]\n",
chan->name, chan->req.arg0, chan->req.arg1,
chan->req.arg2);
return -ETIME;
/*
* P010 resolves a late full-payload ACK before reusing the ring.
* Keep that recoverable timeout path; compact sessions stay poisoned.
*/
if (isp_is_p010(isp))
return -ETIME;
cmpxchg(&isp->ipc_error, 0, -ETIME);
wake_up_all(&isp->wait);
return READ_ONCE(isp->ipc_error);
}

chan->deadline_active = false;
if (READ_ONCE(isp->ipc_error))
return READ_ONCE(isp->ipc_error);

isp_dbg(isp, "%s: request success (%ld)\n", chan->name, t);

return 0;
Expand Down
97 changes: 95 additions & 2 deletions drivers/media/platform/apple/isp/isp-v4l2-test.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* KUnit tests for the videobuf2 queue of the Apple ISP driver, built into
* isp-v4l2.c. They check the queue parameters vb2 accepts and the buffer
* limit queue_setup() enforces, without any hardware.
* isp-v4l2.c. They check queue parameters, buffer limits and suspended
* return ownership and error wakeups without any hardware.
*/

#include <kunit/device.h>
#include <kunit/test.h>
#include <kunit/static_stub.h>
#include <linux/bitmap.h>
#include <linux/completion.h>
#include <linux/kthread.h>

#define ISP_TEST_FRAME_SIZE 0x1000

Expand Down Expand Up @@ -401,6 +403,94 @@ static void isp_test_meta_exposure(struct kunit *test)
KUNIT_EXPECT_EQ(test, isp->frame_gain, 600);
}

/* Returned suspended buffers remain queued, but no longer outstanding. */
static void isp_test_suspended_return(struct kunit *test)
{
struct apple_isp *isp = isp_test_alloc(test);
struct apple_isp_hw hw = {};
struct isp_surf surface = {};
struct isp_channel chan = {};
struct isp_buffer *buf;
struct isp_buflist *bl;

isp->hw = &hw;
isp->fw_iova_mask = U64_MAX;
spin_lock_init(&isp->buf_lock);
INIT_LIST_HEAD(&isp->bufs_pending);
INIT_LIST_HEAD(&isp->bufs_submitted);
buf = kunit_kzalloc(test, sizeof(*buf), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, buf);
buf->surfs[0].iova = 0x300000;
list_add_tail(&buf->link, &isp->bufs_submitted);
bl = kunit_kzalloc(test, sizeof(*bl) + sizeof(bl->buffers[0]), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, bl);
bl->num_buffers = 1;
bl->buffers[0].iovas[0] = buf->surfs[0].iova;
bl->buffers[0].pool_type = CISP_POOL_TYPE_RENDERED;
surface.iova = 0x1000;
surface.virt = bl;
surface.size = sizeof(*bl) + sizeof(bl->buffers[0]);
isp->bt_surf = &surface;
chan.req.arg0 = surface.iova;
chan.req.arg1 = surface.size;
set_bit(ISP_STATE_SLEEPING, &isp->state);
KUNIT_EXPECT_EQ(test, ipc_bt_handle(isp, &chan), 0);
KUNIT_EXPECT_TRUE(test, list_empty(&isp->bufs_submitted));
KUNIT_EXPECT_PTR_EQ(test, isp->bufs_pending.next, &buf->link);
KUNIT_EXPECT_EQ(test, isp->sequence, 0);
}

struct isp_test_wait {
struct vb2_queue *queue;
struct completion entered;
struct completion exited;
long remaining;
};

static int isp_test_queue_waiter(void *arg)
{
struct isp_test_wait *w = arg;
DEFINE_WAIT(wait);

prepare_to_wait(&w->queue->done_wq, &wait, TASK_UNINTERRUPTIBLE);
complete(&w->entered);
w->remaining = schedule_timeout(5 * HZ);
finish_wait(&w->queue->done_wq, &wait);
complete(&w->exited);
while (!kthread_should_stop())
schedule_timeout_interruptible(HZ);
return 0;
}

/* Use the real vb2 error wakeup, waitqueue and startup notification lock. */
static void isp_test_error_wakeup(struct kunit *test)
{
struct apple_isp *isp = isp_test_alloc(test);
struct isp_test_wait w = { .queue = &isp->vbq };
struct task_struct *task;

spin_lock_init(&isp->buf_lock);
init_waitqueue_head(&isp->vbq.done_wq);
isp->ipc_error = -EIO;
apple_isp_video_error(isp);
KUNIT_EXPECT_FALSE(test, isp->vbq.error);
set_bit(ISP_STATE_STREAMING, &isp->state);
isp->ipc_error = 0;
apple_isp_video_error(isp);
KUNIT_EXPECT_FALSE(test, isp->vbq.error);
init_completion(&w.entered);
init_completion(&w.exited);
task = kthread_run(isp_test_queue_waiter, &w, "isp-test-wait");
KUNIT_ASSERT_FALSE(test, IS_ERR(task));
wait_for_completion(&w.entered);
isp->ipc_error = -EIO;
apple_isp_video_error(isp);
wait_for_completion(&w.exited);
kthread_stop(task);
KUNIT_EXPECT_TRUE(test, isp->vbq.error);
KUNIT_EXPECT_GT(test, w.remaining, 0L);
}

static void isp_test_exposure_control_gate(struct kunit *test)
{
static const struct apple_isp_hw hw_none = { };
Expand Down Expand Up @@ -433,6 +523,9 @@ static struct kunit_case isp_v4l2_test_cases[] = {
KUNIT_CASE(isp_test_p010_format),
KUNIT_CASE(isp_test_p010_admission),
KUNIT_CASE(isp_test_p010_completion_identity),

KUNIT_CASE(isp_test_suspended_return),
KUNIT_CASE(isp_test_error_wakeup),
KUNIT_CASE(isp_test_queue_init),
KUNIT_CASE(isp_test_reqbufs_limit),
KUNIT_CASE(isp_test_create_bufs_limit),
Expand Down
Loading