Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 10 additions & 33 deletions drivers/soc/apple/mailbox-g17-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,29 @@ static void apple_g17_rx_util_min_test(struct kunit *test)
#endif
}

static void apple_g17_rx_hardirq_dispatch_test(struct kunit *test)
static void apple_g17_v8_excluded_test(struct kunit *test)
{
const struct apple_mbox_hw hw = {
.i2a_control = 0, .i2a_recv0 = 8, .i2a_recv1 = 16,
.control_empty = BIT(17),
};
struct apple_mbox mbox = { .hw = &hw, .rx = apple_g17_test_rx };
unsigned int count = 0;
void *regs = kunit_kzalloc(test, 32, GFP_KERNEL);

KUNIT_ASSERT_NOT_NULL(test, regs);
mbox.regs = (void __iomem *)regs;
const struct apple_mbox_hw hw_v8 = {
.i2a_control = 0, .i2a_recv0 = 8, .i2a_recv1 = 16,
.control_empty = BIT(17),
.is_ascwrap_v8 = true,
};
const struct apple_mbox_hw hw = { .control_empty = BIT(17) };
const struct apple_mbox_hw hw_v8 = { .control_empty = BIT(17), .is_ascwrap_v8 = true };
struct apple_mbox mbox = { .hw = &hw };
struct apple_mbox mbox_v8 = { .hw = &hw_v8 };

mbox.ascwrap_v6.role = APPLE_MBOX_ASCWRAP_V6_ROLE_GFX;
mbox.cookie = &count;
spin_lock_init(&mbox.rx_lock);

/* ASCWrap v6 GFX/GFX1 use the hard-IRQ split; ASCWrap v8 stays excluded
* so apple_mbox_recv_irq() keeps running apple_mbox_v8_rx_irq_guard().
/* ASCWrap v6 GFX/GFX1 take the threaded G17 receive handler; ASCWrap v8
* stays on apple_mbox_recv_irq() with apple_mbox_v8_rx_irq_guard().
*/
mbox.ascwrap_v6.role = APPLE_MBOX_ASCWRAP_V6_ROLE_GFX;
KUNIT_EXPECT_TRUE(test, apple_mbox_is_g17(&mbox));
mbox.ascwrap_v6.role = APPLE_MBOX_ASCWRAP_V6_ROLE_GFX1;
KUNIT_EXPECT_TRUE(test, apple_mbox_is_g17(&mbox));
mbox_v8.ascwrap_v6.role = APPLE_MBOX_ASCWRAP_V6_ROLE_GFX;
KUNIT_EXPECT_FALSE(test, apple_mbox_is_g17(&mbox_v8));
mbox_v8.ascwrap_v6.role = APPLE_MBOX_ASCWRAP_V6_ROLE_GFX1;
KUNIT_EXPECT_FALSE(test, apple_mbox_is_g17(&mbox_v8));

/* Empty FIFO finishes directly in hard IRQ without waking the thread. */
writel(BIT(17), mbox.regs);
KUNIT_EXPECT_EQ(test, apple_mbox_g17_recv_irq(0, &mbox), IRQ_HANDLED);
KUNIT_EXPECT_EQ(test, count, 0U);

/* Saturated budget wakes the oneshot throttle thread. */
writel(0, mbox.regs);
KUNIT_EXPECT_EQ(test, apple_mbox_g17_recv_irq(0, &mbox), IRQ_WAKE_THREAD);
KUNIT_EXPECT_EQ(test, count, (unsigned int)APPLE_G17_MBOX_RX_BUDGET);
}

static struct kunit_case apple_g17_stall_cases[] = {
KUNIT_CASE(apple_g17_rx_flood_bounded_test),
KUNIT_CASE(apple_g17_rx_hardirq_dispatch_test),
KUNIT_CASE(apple_g17_v8_excluded_test),
KUNIT_CASE(apple_g17_tx_stall_bounded_test),
KUNIT_CASE(apple_g17_rx_util_min_test),
{ }
Expand Down
55 changes: 17 additions & 38 deletions drivers/soc/apple/mailbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,9 @@ static int apple_mbox_poll_locked(struct apple_mbox *mbox)
}

/*
* Threaded receive handler only (woken on G17 budget saturation): apply the
* consumer's minimum utilization to this thread so flood throttling does not
* leave schedutil raising its CPU cluster to the top RT default frequency.
* Threaded receive handler only: apply the consumer's minimum utilization to
* this thread. As an RT thread it otherwise runs with the RT default, which
* makes schedutil raise its CPU cluster to the top frequency on every message.
*/
static void apple_mbox_apply_rx_util_min(struct apple_mbox *mbox)
{
Expand Down Expand Up @@ -976,45 +976,24 @@ static irqreturn_t apple_mbox_recv_irq(int irq, void *data)
unsigned long flags;
int consumed = 0;

/* Only the G17 receive handler is threaded (see apple_mbox_probe()). */
if (mbox->rx_threaded)
apple_mbox_apply_rx_util_min(mbox);

spin_lock_irqsave(&mbox->rx_lock, flags);
if (likely(!READ_ONCE(mbox->removing)))
consumed = apple_mbox_poll_locked(mbox);
if (mbox->hw->is_ascwrap_v8)
apple_mbox_v8_rx_irq_guard(mbox, consumed);
spin_unlock_irqrestore(&mbox->rx_lock, flags);

return IRQ_HANDLED;
}

static irqreturn_t apple_mbox_g17_recv_irq(int irq, void *data)
{
struct apple_mbox *mbox = data;
unsigned long flags;
int consumed = 0;

spin_lock_irqsave(&mbox->rx_lock, flags);
if (likely(!READ_ONCE(mbox->removing)))
consumed = apple_mbox_poll_locked(mbox);
spin_unlock_irqrestore(&mbox->rx_lock, flags);

/*
* Normal completions drain completely in hard IRQ without waking the
* IRQ thread. Only a saturated budget hands off to the oneshot thread
* to sleep with the level line masked so a firmware flood cannot
* monopolize a CPU.
/* G17 uses a oneshot threaded IRQ. Leave it masked during this sleep
* so a firmware flood cannot monopolize a CPU at IRQ-thread priority.
* The level condition retriggers after return; no message is discarded.
*/
if (unlikely(consumed == APPLE_G17_MBOX_RX_BUDGET))
return IRQ_WAKE_THREAD;

return IRQ_HANDLED;
}

static irqreturn_t apple_mbox_g17_recv_thread(int irq, void *data)
{
struct apple_mbox *mbox = data;
if (apple_mbox_is_g17(mbox) && consumed == APPLE_G17_MBOX_RX_BUDGET)
usleep_range(1000, 2000);

apple_mbox_apply_rx_util_min(mbox);
usleep_range(1000, 2000);
return IRQ_HANDLED;
}

Expand Down Expand Up @@ -1659,14 +1638,14 @@ static int apple_mbox_probe(struct platform_device *pdev)

/*
* The G17 GPU coprocessors can flood the receive FIFO; their
* hard handler drains a bounded batch and wakes a thread (see
* apple_mbox_g17_recv_thread()), the thread a clamp applies to.
* receive handler runs in a oneshot IRQ thread and sleeps after
* a saturated batch (see apple_mbox_recv_irq()), the thread a
* clamp applies to.
*/
mbox->rx_threaded = apple_mbox_is_g17(mbox);
if (mbox->rx_threaded)
ret = devm_request_threaded_irq(dev, mbox->irq_recv_not_empty,
apple_mbox_g17_recv_irq,
apple_mbox_g17_recv_thread,
ret = devm_request_threaded_irq(dev, mbox->irq_recv_not_empty, NULL,
apple_mbox_recv_irq,
IRQF_ONESHOT | IRQF_NO_AUTOEN |
IRQF_NO_SUSPEND,
irqname, mbox);
Expand Down
6 changes: 3 additions & 3 deletions include/linux/soc/apple/mailbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void apple_mbox_set_recv_enabled(struct apple_mbox *mbox, bool enabled);
void apple_mbox_hold_rx(struct apple_mbox *mbox);
void apple_mbox_release_rx(struct apple_mbox *mbox);
/*
* Set the minimum utilization (0..1024) the G17 flood-throttle receive thread
* runs with, or -1 to restore the scheduler's default; the thread applies it
* when it next wakes. Only mailboxes with a threaded receive handler support it.
* Set the minimum utilization (0..1024) the threaded receive handler runs with,
* or -1 to restore the scheduler's default; the handler applies it to itself
* when it next runs. Only mailboxes with a threaded receive handler support it.
*/
int apple_mbox_set_rx_util_min(struct apple_mbox *mbox, int util_min);
int apple_mbox_send(struct apple_mbox *mbox, struct apple_mbox_msg msg,
Expand Down
Loading