diff --git a/Documentation/devicetree/bindings/media/apple,isp.yaml b/Documentation/devicetree/bindings/media/apple,isp.yaml index 714c83aa1e392..728fdbbb3bdc7 100644 --- a/Documentation/devicetree/bindings/media/apple,isp.yaml +++ b/Documentation/devicetree/bindings/media/apple,isp.yaml @@ -92,6 +92,13 @@ properties: description: Firmware temporal-filter strength selected for the attached sensor. + apple,ane: + $ref: /schemas/types.yaml#/definitions/phandle + description: + The Neural Engine the ISP firmware runs its ML noise reduction (MLVNR) + on. The two coprocessors exchange messages on rings in shared memory + and ring each other's doorbells. + apple,camera-indicator: $ref: /schemas/types.yaml#/definitions/phandle description: diff --git a/arch/arm64/boot/dts/apple/t604x.dtsi b/arch/arm64/boot/dts/apple/t604x.dtsi index 4e25d040be20e..30dd31f0beb52 100644 --- a/arch/arm64/boot/dts/apple/t604x.dtsi +++ b/arch/arm64/boot/dts/apple/t604x.dtsi @@ -1436,6 +1436,8 @@ <&ps_isp_clr>, <&ps_ispsens0>; /* the shared memory window above the firmware's 32 MiB */ apple,dart-vm-size = /bits/ 64 <0xde600000>; + /* ADT isp0 function-ane_ep_control/data_param_set/get */ + apple,ane = <&ane>; status = "disabled"; }; diff --git a/drivers/accel/ane/ane_fw_platform.c b/drivers/accel/ane/ane_fw_platform.c index dfc53778d13f9..ea216b46a7306 100644 --- a/drivers/accel/ane/ane_fw_platform.c +++ b/drivers/accel/ane/ane_fw_platform.c @@ -293,6 +293,11 @@ const struct ane_fw_platform ane_h16_platform = { .mailbox = 0x1608000, .scratch = 0x1880020, .doorbell = 0x1884000, + /* macOS gives the ISP 0x485660000 and 1 on the J616s. */ + .peer_doorbell = 0x1660000, + .peer_doorbell_value = 0x1, + /* The DSID macOS grants the J616s camera's spill (0x300000 bytes). */ + .peer_spill_dsid = 0xe, .irq_status = 0x188c000, .irq_ack = 0x1890000, .power_words = ane_h16_power_words, diff --git a/drivers/accel/ane/ane_fw_platform.h b/drivers/accel/ane/ane_fw_platform.h index a1b087cac09a0..0eb05babe71f3 100644 --- a/drivers/accel/ane/ane_fw_platform.h +++ b/drivers/accel/ane/ane_fw_platform.h @@ -71,6 +71,15 @@ struct ane_fw_platform { u32 mailbox; u32 scratch; u32 doorbell; + /* + * The doorbell a peer coprocessor's firmware rings (the ISP, see + * ), in the engine window, and the value it + * writes there. Zero without a peer endpoint. + */ + u32 peer_doorbell; + u32 peer_doorbell_value; + /* The mcache DSID for the peer networks' intermediate spill; 0 for none. */ + u32 peer_spill_dsid; u32 irq_status; u32 irq_ack; const u32 *power_words; diff --git a/drivers/accel/ane/ane_fw_protocol_test.c b/drivers/accel/ane/ane_fw_protocol_test.c index fb98428f3f238..688c0b9b0af12 100644 --- a/drivers/accel/ane/ane_fw_protocol_test.c +++ b/drivers/accel/ane/ane_fw_protocol_test.c @@ -16,6 +16,12 @@ static void ane_h16_id_table_test(struct kunit *test) { 0x20c, 0xca8, 0xcd8, 0x18, 7, false }, { 0x2d, 0xc, 0xc, 1, 7, false }, { 0x300, 0x60, 0x60, 1, 6, true }, + { 0x26, 0xc, 0xc, 1, 1, false }, + { 0x2a, 0x30, 0x30, 1, 1, false }, + { 0x2b, 0x30, 0x30, 1, 1, false }, + { 0x2e, 0x20, 0x20, 1, 1, false }, + { 0x205, 0x38, 0x38, 1, 1, false }, + { 0x206, 0x38, 0x38, 1, 1, false }, }; u8 *command = kunit_kzalloc(test, 0xcd8, GFP_KERNEL); unsigned int i; diff --git a/drivers/accel/ane/ane_fw_transport.h b/drivers/accel/ane/ane_fw_transport.h index 1dd61374768fe..93825c2b28fc3 100644 --- a/drivers/accel/ane/ane_fw_transport.h +++ b/drivers/accel/ane/ane_fw_transport.h @@ -102,6 +102,15 @@ struct ane_fw_transport_ops { void (*refused)(void *ctx, u64 word0, u64 word1, u64 word2, int error); /* Optional: a shared allocation was freed ("FREE") or served again ("reuse"). */ void (*shared_note)(void *ctx, const char *event, u64 dva, u64 size, u32 tag); + /* + * Optional: a peer coprocessor's surface, which the firmware names by + * the ID the peer's firmware got for it (SHAREDMALLOC with zero DVA + * and size). peer_map maps it and returns its DVA and size; + * peer_unmap gives back a DVA peer_map returned, and -ENOENT means it + * is not a peer surface (an ordinary FREE then). + */ + int (*peer_map)(void *ctx, u32 id, u64 *dva, u64 *size); + int (*peer_unmap)(void *ctx, u64 dva); /* * Optional: true once the firmware's CPU no longer runs, so no reply can * come. A wait then ends at once with -ESHUTDOWN instead of at its @@ -832,6 +841,20 @@ static inline int ane_fw_transport_shared_reserve(struct ane_fw_transport *t, si static inline int ane_fw_transport_irq(struct ane_fw_transport *t, u32 *pending); +/* Answer a peer surface map (DVA|1, size, ID) or unmap (1, 0, 0) in place. */ +static inline int ane_fw_transport_peer_reply(struct ane_fw_transport *t, void *slot, + u64 word0, u64 word1, u64 word2) +{ + t->ops->store64(t->ctx, (u8 *)slot + 8, word1); + t->ops->store64(t->ctx, (u8 *)slot + 16, word2); + t->ops->write_barrier(t->ctx); + t->ops->store64(t->ctx, slot, word0); + t->ops->write_barrier(t->ctx); + ane_fw_transport_doorbell(t, ANE_FW_SHAREDMALLOC); + ane_fw_transport_advance(t, ANE_FW_SHAREDMALLOC); + return 0; +} + static inline int ane_fw_transport_shared(struct ane_fw_transport *t) { u64 word0, word1, word2; @@ -870,11 +893,28 @@ static inline int ane_fw_transport_shared(struct ane_fw_transport *t) if (!word0 && !word1 && !word2) return -EAGAIN; if (word0) { + if (t->ops->peer_unmap && !word1 && !word2) { + ret = t->ops->peer_unmap(t->ctx, word0); + if (!ret) + return ane_fw_transport_peer_reply(t, p, 1, 0, 0); + if (ret != -ENOENT) + goto refused; + } ret = ane_fw_transport_free(t, p, word0, word1, word2); if (ret) goto refused; return 0; } + if (!word1 && t->ops->peer_map) { + u64 dva, bytes; + + ret = word2 > U32_MAX ? -E2BIG : t->ops->peer_map(t->ctx, word2, &dva, &bytes); + if (!ret && (!dva || (dva & 1) || !bytes)) + ret = -EPROTO; + if (ret) + goto refused; + return ane_fw_transport_peer_reply(t, p, dva | 1, bytes, word2); + } ret = ane_fw_shared_size(word0, word1, word2, &size); if (ret) goto refused; diff --git a/drivers/accel/ane/ane_fw_transport_test.c b/drivers/accel/ane/ane_fw_transport_test.c index 8a4c06b756452..bc7bbf5ff0520 100644 --- a/drivers/accel/ane/ane_fw_transport_test.c +++ b/drivers/accel/ane/ane_fw_transport_test.c @@ -202,6 +202,44 @@ static const struct ane_fw_transport_ops ane_test_ops = { .refused = ane_test_refused, }; +/* The ISP peer: surface id 0x3c maps at TEST_PEER_DVA; one unmap is known. */ +#define TEST_PEER_DVA 0x7000000000ULL + +static int ane_test_peer_map(void *ctx, u32 id, u64 *dva, u64 *size) +{ + struct ane_transport_test *f = ctx; + + f->allocs++; + if (id != 0x3c) + return -ENOENT; + *dva = TEST_PEER_DVA; + *size = SZ_64K; + return 0; +} + +static int ane_test_peer_unmap(void *ctx, u64 dva) +{ + struct ane_transport_test *f = ctx; + + f->refused[2] = dva; + return dva == TEST_PEER_DVA ? 0 : -ENOENT; +} + +static const struct ane_fw_transport_ops ane_test_peer_ops = { + .load64 = ane_test_load, + .store64 = ane_test_store, + .read_barrier = ane_test_rmb, + .write_barrier = ane_test_wmb, + .read32 = ane_test_read32, + .write32 = ane_test_write32, + .now_ns = ane_test_now, + .relax = ane_test_relax, + .alloc = ane_test_alloc, + .refused = ane_test_refused, + .peer_map = ane_test_peer_map, + .peer_unmap = ane_test_peer_unmap, +}; + static void ane_test_table(struct ane_transport_test *f) { static const char * const names[] = { @@ -755,6 +793,64 @@ static void ane_transport_shared_test(struct kunit *test) KUNIT_EXPECT_EQ(test, f->t.shared_count, 2U); } +/* + * The ISP peer's surfaces: a map request (zero DVA and size, the surface ID + * in word 2) is answered DVA|1, size, ID; an unmap of that DVA with 1, 0, 0; + * an unmap of another DVA is an ordinary FREE. + */ +static void ane_transport_peer_test(struct kunit *test) +{ + struct ane_transport_test *f = test->priv; + u8 *slot; + + f->t.ops = &ane_test_peer_ops; + KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0); + ane_test_shared_request(f, 0, 0, 0x3c); + slot = ane_test_slot(f, ANE_FW_SHAREDMALLOC); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot), TEST_PEER_DVA | 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot + 8), (u64)SZ_64K); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot + 16), 0x3cULL); + KUNIT_EXPECT_EQ(test, f->last_value, (u32)BIT(5)); + /* A peer surface is not shared memory: nothing counted against the quota. */ + KUNIT_EXPECT_EQ(test, f->t.shared_count, 0U); + + ane_test_shared_request(f, TEST_PEER_DVA, 0, 0); + slot = ane_test_slot(f, ANE_FW_SHAREDMALLOC); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot), 1ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot + 8), 0ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot + 16), 0ULL); + KUNIT_EXPECT_EQ(test, f->refusals, 0U); + + /* An unknown surface ID cannot be answered: the transport fails. */ + ane_test_shared_request(f, 0, 0, 0x3d); + KUNIT_EXPECT_EQ(test, ane_fw_transport_shared(&f->t), -ENOENT); + KUNIT_EXPECT_EQ(test, f->refusals, 1U); + KUNIT_EXPECT_EQ(test, f->refused[2], 0x3dULL); +} + +/* An unmap the peer does not know is an ordinary FREE. */ +static void ane_transport_peer_free_test(struct kunit *test) +{ + struct ane_transport_test *f = test->priv; + u8 *slot; + u64 dva; + + f->t.ops = &ane_test_peer_ops; + KUNIT_ASSERT_EQ(test, ane_fw_transport_arm(&f->t), 0); + ane_test_shared_request(f, 0, SZ_2M, ANE_FW_SHARED_EXPP); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + dva = f->t.shared[0].dva; + f->t.allow_free = true; + ane_test_shared_request(f, dva, 0, 0); + slot = ane_test_slot(f, ANE_FW_SHAREDMALLOC); + KUNIT_ASSERT_EQ(test, ane_fw_transport_shared(&f->t), 0); + KUNIT_EXPECT_EQ(test, f->refused[2], dva); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(slot), dva | 1); + KUNIT_EXPECT_TRUE(test, f->t.shared_retired[0]); +} + /* The body of a fresh zero-header request is not its publication boundary. */ static void ane_transport_shared_notification_test(struct kunit *test) { @@ -1721,6 +1817,8 @@ static void ane_transport_terminal_wrap_test(struct kunit *test) } static struct kunit_case ane_transport_cases[] = { + KUNIT_CASE(ane_transport_peer_test), + KUNIT_CASE(ane_transport_peer_free_test), KUNIT_CASE(ane_transport_terminal_ack_test), KUNIT_CASE(ane_transport_terminal_bounds_test), KUNIT_CASE(ane_transport_terminal_wrap_test), diff --git a/drivers/accel/ane/ane_h16_backend.c b/drivers/accel/ane/ane_h16_backend.c index da7ddc1bb98f2..75025296085cf 100644 --- a/drivers/accel/ane/ane_h16_backend.c +++ b/drivers/accel/ane/ane_h16_backend.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -54,8 +55,20 @@ struct ane_h16_mapping { struct drm_mm_node range; }; +/* A peer's ring or surface mapped into the firmware domain (apple_ane_peer_attach()). */ +struct ane_h16_peer_map { + struct list_head node; + /* The peer's surface ID; 0 for its rings, which the firmware never frees. */ + u32 id; + u64 dva; + size_t size; + struct drm_mm_node range; +}; + struct ane_h16_device { struct ane_h16_boot boot; + /* In ane_h16_devices, for apple_ane_get(); under ane_h16_lock. */ + struct list_head node; struct device *dev; void __iomem *window[ANE_H16_WINDOWS]; struct device *power[ANE_FW_DOMAINS_MAX]; @@ -170,9 +183,22 @@ struct ane_h16_device { u64 recover_failures; struct ane_h16_session session; struct work_struct start; + /* + * The ISP's endpoint while it is attached: peer, peer_ep and peer_on + * change under the session lock (peer_on is read by the IRQ handler); + * peer_maps is under window_lock. peer_work serves SHAREDMALLOC on + * the channel IRQ, since the peer's map requests come while nothing + * waits. + */ + struct apple_ane_peer peer; + struct ane_h16_endpoint peer_ep; + struct list_head peer_maps; + struct work_struct peer_work; + bool peer_on; }; static DEFINE_MUTEX(ane_h16_lock); +static LIST_HEAD(ane_h16_devices); static bool ane_h16_released; /* /ane-h16, shared by bound devices; protected by ane_h16_lock. */ static struct dentry *ane_h16_debugfs; @@ -2004,6 +2030,308 @@ static void ane_h16_hung(void *ctx, int error) queue_work(system_dfl_wq, &h->recover); } +/* + * The ISP peer (). On macOS the camera driver powers + * the engine, the engine's driver gives the firmware the ISP's rings + * (MCACHE_SIZE_GET, IPC_ENDPOINT_SET2) and the ISP firmware then runs its + * MLVNR networks here. The engine's firmware asks for the ISP's surfaces by + * ID on SHAREDMALLOC; they are mapped from the program window and unmapped + * when the firmware frees them or the peer detaches. + */ +static int ane_h16_peer_map_pages(struct ane_h16_device *h, u32 id, struct sg_table *sgt, + size_t size, u64 *dva) +{ + struct ane_h16_peer_map *m; + ssize_t mapped; + int ret; + + if (!size || !IS_ALIGNED(size, SZ_16K)) + return -EINVAL; + m = kzalloc_obj(*m); + if (!m) + return -ENOMEM; + m->id = id; + m->size = size; + mutex_lock(&h->window_lock); + ret = drm_mm_insert_node_in_range(&h->program_window, &m->range, size, SZ_16K, 0, 0, + ANE_H16_PROGRAM_WINDOW, DRM_MM_INSERT_LOW); + if (!ret) { + m->dva = h->program_start + m->range.start; + list_add_tail(&m->node, &h->peer_maps); + } + mutex_unlock(&h->window_lock); + if (ret) { + kfree(m); + return ret; + } + /* A failed call has already unmapped what it mapped. */ + mapped = iommu_map_sgtable(h->domain, m->dva, sgt, + IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE); + if (mapped != (ssize_t)size) { + if (mapped > 0) + iommu_unmap(h->domain, m->dva, mapped); + mutex_lock(&h->window_lock); + list_del(&m->node); + drm_mm_remove_node(&m->range); + mutex_unlock(&h->window_lock); + kfree(m); + return mapped < 0 ? mapped : -EIO; + } + *dva = m->dva; + return 0; +} + +/* iommu_unmap() ends with the TLB invalidation: the engine can't reach it after. */ +static void ane_h16_peer_unmap_one(struct ane_h16_device *h, struct ane_h16_peer_map *m) +{ + iommu_unmap(h->domain, m->dva, m->size); + mutex_lock(&h->window_lock); + list_del(&m->node); + drm_mm_remove_node(&m->range); + mutex_unlock(&h->window_lock); + if (m->id && h->peer.ops->put_surface) + h->peer.ops->put_surface(h->peer.ctx, m->id); + kfree(m); +} + +/* SHAREDMALLOC asks for the peer's surface id: under the session lock. */ +static int ane_h16_peer_map(void *ctx, u32 id, u64 *dva, u64 *size) +{ + struct ane_h16_device *h = ctx; + struct sg_table *sgt; + size_t bytes; + int ret; + + if (!h->peer_on || !id) + return -ENODEV; + ret = h->peer.ops->get_surface(h->peer.ctx, id, &sgt, &bytes); + if (ret) + return ret; + ret = ane_h16_peer_map_pages(h, id, sgt, bytes, dva); + if (ret) { + if (h->peer.ops->put_surface) + h->peer.ops->put_surface(h->peer.ctx, id); + return ret; + } + *size = bytes; + dev_dbg(h->dev, "H16 peer surface %#x mapped at %#llx, %zx bytes\n", id, *dva, bytes); + return 0; +} + +static int ane_h16_peer_unmap(void *ctx, u64 dva) +{ + struct ane_h16_device *h = ctx; + struct ane_h16_peer_map *m, *found = NULL; + + mutex_lock(&h->window_lock); + list_for_each_entry(m, &h->peer_maps, node) { + if (m->id && m->dva == dva) { + found = m; + break; + } + } + mutex_unlock(&h->window_lock); + if (!found) + return -ENOENT; + dev_dbg(h->dev, "H16 peer surface %#x unmapped\n", found->id); + ane_h16_peer_unmap_one(h, found); + return 0; +} + +static void ane_h16_peer_unmap_all(struct ane_h16_device *h) +{ + struct ane_h16_peer_map *m, *next; + + list_for_each_entry_safe(m, next, &h->peer_maps, node) + ane_h16_peer_unmap_one(h, m); +} + +/* With a peer attached the channel IRQ stays enabled between waits. */ +static void ane_h16_peer_arm(struct ane_h16_device *h) +{ + scoped_guard(spinlock_irqsave, &h->irq_lock) { + if (READ_ONCE(h->peer_on) && h->irq_wait && !h->irq_armed) { + h->irq_armed = true; + enable_irq(h->irq); + } + } +} + +static void ane_h16_peer_work(struct work_struct *work) +{ + struct ane_h16_device *h = container_of(work, struct ane_h16_device, peer_work); + int ret = 0; + + mutex_lock(h->session.lock); + if (h->peer_on) + ret = ane_h16_session_service(&h->session); + mutex_unlock(h->session.lock); + if (ret) + dev_err_ratelimited(h->dev, "H16 peer service failed, error %d\n", ret); + ane_h16_peer_arm(h); +} + +/* A peer's hold on the engine: the device and, for system sleep order, a link. */ +struct apple_ane { + struct ane_h16_device *h; + struct device_link *link; +}; + +struct apple_ane *apple_ane_get(struct device *dev) +{ + struct device_node *np = of_parse_phandle(dev->of_node, "apple,ane", 0); + struct ane_h16_device *h, *found = NULL; + struct apple_ane *ane; + + if (!np) + return NULL; + if (!of_device_is_available(np)) { + of_node_put(np); + return NULL; + } + scoped_guard(mutex, &ane_h16_lock) { + list_for_each_entry(h, &ane_h16_devices, node) { + if (h->dev->of_node == np && READ_ONCE(h->program_ready) && + h->session.lock) { + found = h; + get_device(h->dev); + break; + } + } + } + of_node_put(np); + if (!found) + return ERR_PTR(-EAGAIN); + ane = kzalloc_obj(*ane); + if (!ane) { + put_device(found->dev); + return ERR_PTR(-ENOMEM); + } + ane->h = found; + /* System sleep: the peer suspends first and resumes last. */ + ane->link = device_link_add(dev, found->dev, DL_FLAG_STATELESS); + if (!ane->link) + dev_warn(dev, "no device link to %s\n", dev_name(found->dev)); + return ane; +} +EXPORT_SYMBOL_GPL(apple_ane_get); + +void apple_ane_put(struct apple_ane *ane) +{ + if (IS_ERR_OR_NULL(ane)) + return; + if (ane->link) + device_link_del(ane->link); + put_device(ane->h->dev); + kfree(ane); +} +EXPORT_SYMBOL_GPL(apple_ane_put); + +int apple_ane_peer_attach(struct apple_ane *ane, const struct apple_ane_peer *peer, + phys_addr_t *doorbell, u32 *doorbell_value) +{ + struct ane_h16_device *h = ane->h; + const struct ane_fw_platform *soc = h->boot.soc; + struct resource *res; + u64 tx, rx, afpp = 0; + u32 mcache = 0; + int ret; + + if (!peer || !peer->ops || !peer->ops->get_surface || !peer->tx || !peer->rx || + !peer->ring_size || peer->ring_size > U32_MAX || !peer->doorbell) + return -EINVAL; + res = platform_get_resource_byname(to_platform_device(h->dev), IORESOURCE_MEM, + "engine"); + if (!soc->peer_doorbell || !res) + return -EOPNOTSUPP; + /* The engine stays up (not parked) while the peer is attached. */ + ret = pm_runtime_resume_and_get(h->dev); + if (ret < 0) + return ret; + ret = ane_h16_session_enter(&h->session, false); + if (ret) + goto put; + if (h->peer_on) { + ret = -EBUSY; + goto unlock; + } + h->peer = *peer; + ret = ane_h16_peer_map_pages(h, 0, peer->tx, peer->ring_size, &tx); + if (!ret) + ret = ane_h16_peer_map_pages(h, 0, peer->rx, peer->ring_size, &rx); + if (!ret && peer->networks) + ret = ane_h16_peer_map_pages(h, 0, peer->networks, + ALIGN(peer->networks_size, SZ_16K), &afpp); + if (ret) + goto unmap; + h->peer_ep = (struct ane_h16_endpoint) { + .index = 0, + .tx_dva = tx, + .tx_size = peer->ring_size, + .rx_dva = rx, + .rx_size = peer->ring_size, + .doorbell = peer->doorbell, + .doorbell_value = peer->doorbell_value, + .afpp_dva = afpp, + .afpp_size = peer->networks ? peer->networks_size : 0, + .spill_dsid = peer->networks ? soc->peer_spill_dsid : 0, + }; + /* Map requests may follow SET2 at once. */ + WRITE_ONCE(h->peer_on, true); + ret = ane_h16_session_peer_set(&h->session, &h->peer_ep, &mcache); + if (ret) { + WRITE_ONCE(h->peer_on, false); + goto unmap; + } + mutex_unlock(h->session.lock); + ane_h16_peer_arm(h); + *doorbell = res->start + soc->peer_doorbell; + *doorbell_value = soc->peer_doorbell_value; + dev_info(h->dev, "H16 peer endpoint set: rings %#llx/%#llx, networks %#llx/%#zx, mcache %#x\n", + tx, rx, afpp, peer->networks ? peer->networks_size : 0, mcache); + return 0; +unmap: + ane_h16_peer_unmap_all(h); +unlock: + mutex_unlock(h->session.lock); +put: + pm_runtime_mark_last_busy(h->dev); + pm_runtime_put_autosuspend(h->dev); + dev_err(h->dev, "H16 peer endpoint refused, error %d\n", ret); + return ret; +} +EXPORT_SYMBOL_GPL(apple_ane_peer_attach); + +void apple_ane_peer_detach(struct apple_ane *ane) +{ + struct ane_h16_device *h = ane->h; + int ret; + + mutex_lock(h->session.lock); + if (!h->peer_on) { + mutex_unlock(h->session.lock); + return; + } + /* The firmware asks to unmap the peer's surfaces while this settles. */ + ret = ane_h16_session_peer_unset(&h->session, &h->peer_ep); + WRITE_ONCE(h->peer_on, false); + /* + * What is left (the rings, and everything after a failed UNSET2) goes + * too: a firmware still using it faults in the DART instead of + * reaching memory the peer frees next. + */ + ane_h16_peer_unmap_all(h); + mutex_unlock(h->session.lock); + cancel_work_sync(&h->peer_work); + pm_runtime_mark_last_busy(h->dev); + pm_runtime_put_autosuspend(h->dev); + if (ret) + dev_err(h->dev, "H16 peer endpoint unset failed, error %d\n", ret); + else + dev_info(h->dev, "H16 peer endpoint unset\n"); +} +EXPORT_SYMBOL_GPL(apple_ane_peer_detach); + static const struct ane_h16_session_ops ane_h16_session_ops = { .map = ane_h16_program_map, .unmap = ane_h16_program_unmap, @@ -2028,6 +2356,8 @@ static const struct ane_fw_transport_ops ane_h16_transport_ops = { .refused = ane_h16_refused, .shared_note = ane_h16_shared_note, .stopped = ane_h16_cpu_stopped, + .peer_map = ane_h16_peer_map, + .peer_unmap = ane_h16_peer_unmap, }; /* Pre-RUN rollback of everything a stage acquired. h itself stays. */ @@ -2124,6 +2454,8 @@ static irqreturn_t ane_h16_irq(int irq, void *data) WRITE_ONCE(h->irq_seq, h->irq_seq + 1); } wake_up(&h->irq_wq); + if (READ_ONCE(h->peer_on)) + queue_work(system_highpri_wq, &h->peer_work); return IRQ_HANDLED; } @@ -2691,6 +3023,8 @@ int ane_h16_backend_probe(struct platform_device *pdev, const struct ane_fw_plat init_waitqueue_head(&h->irq_wq); mutex_init(&h->window_lock); INIT_LIST_HEAD(&h->mappings); + INIT_LIST_HEAD(&h->peer_maps); + INIT_WORK(&h->peer_work, ane_h16_peer_work); /* Offsets from program_start, which the preload's build decides. */ drm_mm_init(&h->program_window, 0, ANE_H16_PROGRAM_WINDOW); h->boot.soc = soc; @@ -2726,6 +3060,7 @@ int ane_h16_backend_probe(struct platform_device *pdev, const struct ane_fw_plat debugfs_create_u64("recoveries", 0400, h->debugfs, &h->recoveries); debugfs_create_u64("recover_failures", 0400, h->debugfs, &h->recover_failures); platform_set_drvdata(pdev, h); + list_add_tail(&h->node, &ane_h16_devices); INIT_WORK(&h->recover, ane_h16_recover_work); INIT_WORK(&h->start, ane_h16_start_work); mutex_unlock(&ane_h16_lock); @@ -2763,6 +3098,7 @@ int ane_h16_backend_probe(struct platform_device *pdev, const struct ane_fw_plat debugfs_remove_recursive(ane_h16_debugfs); ane_h16_debugfs = NULL; } + list_del(&h->node); mutex_unlock(&ane_h16_lock); platform_set_drvdata(pdev, NULL); drm_mm_takedown(&h->program_window); @@ -2829,6 +3165,7 @@ void ane_h16_backend_remove(struct platform_device *pdev) debugfs_remove_recursive(ane_h16_debugfs); ane_h16_debugfs = NULL; } + list_del_init(&h->node); if (keep) { /* The module reference taken before RUN makes this unreachable by rmmod. */ dev_warn(h->dev, "H16 remove held; reboot reclaims firmware DMA\n"); diff --git a/drivers/accel/ane/ane_h16_program.c b/drivers/accel/ane/ane_h16_program.c index f4812de167edc..22a8c12ba6c81 100644 --- a/drivers/accel/ane/ane_h16_program.c +++ b/drivers/accel/ane/ane_h16_program.c @@ -261,6 +261,86 @@ int ane_h16_gate_encode(void *command, size_t length, u32 value) } EXPORT_SYMBOL_GPL(ane_h16_gate_encode); +/* MCACHE_SIZE_GET: the firmware answers with the mcache size it wants at +0x08. */ +int ane_h16_mcache_encode(void *command, size_t length) +{ + if (!command || length != ANE_H16_MCACHE_SIZE) + return -EINVAL; + memset(command, 0, length); + return ane_h16_command_prepare(command, length, ANE_H16_MCACHE_SIZE_GET, ANE_FW_IO); +} +EXPORT_SYMBOL_GPL(ane_h16_mcache_encode); + +/* + * LOAD_AFPP and UNLOAD_AFPP: the peer's network file as one buffer, its + * DVA and size, behind two words that macOS sets to 1 (a descriptor and a + * valid flag). UNLOAD names the same buffer. + */ +int ane_h16_afpp_encode(void *command, size_t length, bool load, u64 dva, u64 size) +{ + u8 *p = command; + + if (!p || length != ANE_H16_AFPP_SIZE || !dva || !size) + return -EINVAL; + memset(p, 0, length); + put_unaligned_le32(1, p + 0x08); + put_unaligned_le32(1, p + 0x18); + put_unaligned_le64(dva, p + 0x20); + put_unaligned_le64(size, p + 0x28); + return ane_h16_command_prepare(p, length, load ? ANE_H16_LOAD_AFPP : ANE_H16_UNLOAD_AFPP, + ANE_FW_IO); +} +EXPORT_SYMBOL_GPL(ane_h16_afpp_encode); + +/* + * MCACHE_DSID_SET: a count, then (kind, DSID) pairs, kind 3 the networks' + * prefetch and kind 4 their intermediate spill. macOS sends it after + * MCACHE_SIZE_GET with prefetch 0 and the spill DSID it granted the size to. + */ +int ane_h16_dsid_set_encode(void *command, size_t length, u32 prefetch, u32 spill) +{ + u8 *p = command; + + if (!p || length != ANE_H16_DSID_SET_SIZE) + return -EINVAL; + memset(p, 0, length); + put_unaligned_le32(2, p + 0x08); + put_unaligned_le32(3, p + 0x10); + put_unaligned_le32(prefetch, p + 0x14); + put_unaligned_le32(4, p + 0x18); + put_unaligned_le32(spill, p + 0x1c); + return ane_h16_command_prepare(p, length, ANE_H16_MCACHE_DSID_SET, ANE_FW_IO); +} +EXPORT_SYMBOL_GPL(ane_h16_dsid_set_encode); + +/* + * IPC_ENDPOINT_SET2 and UNSET2 carry the same packed body: the endpoint + * index, the ring the engine reads (the peer writes it) and the ring the + * engine writes (DVA, then length), and the peer's doorbell with its value. + * The two u64 at +0x0c and +0x24 are only 4-byte aligned. With the rings the + * other way round, the J616s firmware takes the peer's doorbell but never + * reads the peer's ring; with them this way it serves the ISP. + */ +int ane_h16_endpoint_encode(void *command, size_t length, bool set, + const struct ane_h16_endpoint *e) +{ + u8 *p = command; + + if (!p || !e || length != ANE_H16_ENDPOINT_SIZE) + return -EINVAL; + memset(p, 0, length); + put_unaligned_le32(e->index, p + 0x08); + put_unaligned_le64(e->rx_dva, p + 0x0c); + put_unaligned_le32(e->rx_size, p + 0x14); + put_unaligned_le64(e->tx_dva, p + 0x18); + put_unaligned_le32(e->tx_size, p + 0x20); + put_unaligned_le64(e->doorbell, p + 0x24); + put_unaligned_le32(e->doorbell_value, p + 0x2c); + return ane_h16_command_prepare(p, length, set ? ANE_H16_IPC_ENDPOINT_SET2 : + ANE_H16_IPC_ENDPOINT_UNSET2, ANE_FW_IO); +} +EXPORT_SYMBOL_GPL(ane_h16_endpoint_encode); + /* * Short commands (0x002d, TERMINATE, UNLOAD) have no recorded reply body, so * only the transport reply shape is a predicate: same length and command, @@ -1643,7 +1723,7 @@ static int h16_gate_release(struct ane_h16_session *s) { int ret; - if (!s->gate_held) + if (!s->gate_held || s->peer) return 0; s->gate_held = false; if (s->quarantined) @@ -1709,6 +1789,185 @@ static void h16_gate_work(struct work_struct *work) mutex_unlock(s->lock); } +static unsigned int h16_dump_fwlog(struct ane_h16_session *s); + +/* + * Send one command that the firmware answers in place, with a reply of the + * same length. As for every command, a refusal (nonzero status) fails the + * transport, and the session quarantines for the backend to recover. + */ +static int h16_call(struct ane_h16_session *s, void *command, size_t length, + const u8 **reply) +{ + u16 id = get_unaligned_le16((u8 *)command + 4); + int ret = h16_send(s, ANE_FW_IO, command, length, reply); + + if (!ret) + ret = h16_reply_header(*reply, length, length, id); + if (!ret) + return 0; + /* The firmware's own account, before recovery boots it again. */ + s->ops->log(s->ctx, "command failed", id, (u64)-ret); + h16_dump_fwlog(s); + return h16_fail(s, ret); +} + +/* + * Give the firmware a peer's endpoint, as macOS does when the camera starts: + * the mcache size first (returned in mcache_size), then IPC_ENDPOINT_SET2. + * Gating goes off and stays off until the endpoint is unset, since the peer + * submits work on its own. Caller holds the session lock with the engine up. + */ +int ane_h16_session_peer_set(struct ane_h16_session *s, const struct ane_h16_endpoint *e, + u32 *mcache_size) +{ + size_t mark = ane_fw_transport_mark(s->transport); + /* The largest of MCACHE_SIZE_GET, LOAD_AFPP and SET2. */ + u8 command[ANE_H16_AFPP_SIZE]; + const u8 *reply; + int ret; + + if (s->quarantined) + return s->error ?: -EIO; + if (s->asleep || s->parked) + return -EAGAIN; + if (s->peer) + return -EBUSY; + if (!s->gate_held) { + ret = h16_gate(s, 0); + if (ret) { + s->ops->log(s->ctx, "peer gate failed", (u64)-ret, 0); + return h16_fail(s, ret); + } + s->gate_held = true; + } + ret = ane_h16_mcache_encode(command, ANE_H16_MCACHE_SIZE); + if (!ret) + ret = h16_call(s, command, ANE_H16_MCACHE_SIZE, &reply); + if (!ret) + *mcache_size = get_unaligned_le32(reply + 8); + if (!ret) + s->ops->log(s->ctx, "peer mcache size", *mcache_size, 0); + if (!ret && e->spill_dsid) + ret = ane_h16_dsid_set_encode(command, ANE_H16_DSID_SET_SIZE, 0, e->spill_dsid); + if (!ret && e->spill_dsid) + ret = h16_call(s, command, ANE_H16_DSID_SET_SIZE, &reply); + /* The networks go first: the firmware takes none once the endpoint is set. */ + if (!ret && e->afpp_size) + ret = ane_h16_afpp_encode(command, ANE_H16_AFPP_SIZE, true, e->afpp_dva, + e->afpp_size); + if (!ret && e->afpp_size) + ret = h16_call(s, command, ANE_H16_AFPP_SIZE, &reply); + if (!ret) + ret = ane_h16_endpoint_encode(command, ANE_H16_ENDPOINT_SIZE, true, e); + if (!ret) + ret = h16_call(s, command, ANE_H16_ENDPOINT_SIZE, &reply); + if (!s->quarantined) + h16_pool_release(s, mark); + if (ret) { + if (!s->running && !s->gate_hold_ns) + h16_gate_release(s); + return ret; + } + s->peer = true; + s->ops->log(s->ctx, "peer endpoint set", e->index, *mcache_size); + return 0; +} +EXPORT_SYMBOL_GPL(ane_h16_session_peer_set); + +/* + * IPC_ENDPOINT_UNSET2, then serve what the firmware sends right after (it + * asks to unmap the peer's surfaces). Gating goes back on when idle. + */ +int ane_h16_session_peer_unset(struct ane_h16_session *s, const struct ane_h16_endpoint *e) +{ + size_t mark = ane_fw_transport_mark(s->transport); + /* The largest of MCACHE_SIZE_GET, LOAD_AFPP and SET2. */ + u8 command[ANE_H16_AFPP_SIZE]; + const u8 *reply; + int ret; + + if (!s->peer) + return 0; + s->peer = false; + if (s->quarantined) + return s->error ?: -EIO; + /* The firmware is suspended or off: it lost the endpoint with it. */ + if (s->asleep || s->parked) + return 0; + ret = ane_h16_endpoint_encode(command, ANE_H16_ENDPOINT_SIZE, false, e); + if (!ret) + ret = h16_call(s, command, ANE_H16_ENDPOINT_SIZE, &reply); + if (!s->quarantined) + h16_pool_release(s, mark); + if (!ret) { + ret = ane_fw_transport_settle(s->transport, s->settle_ns, + max(s->settle_ns, ANE_H16_SETTLE_LIMIT_NS)); + ret = ret < 0 ? h16_fail(s, ret) : 0; + } + /* The networks go after the endpoint, as macOS unloads them. */ + if (!ret && e->afpp_size) { + mark = ane_fw_transport_mark(s->transport); + ret = ane_h16_afpp_encode(command, ANE_H16_AFPP_SIZE, false, e->afpp_dva, + e->afpp_size); + if (!ret) + ret = h16_call(s, command, ANE_H16_AFPP_SIZE, &reply); + if (!s->quarantined) + h16_pool_release(s, mark); + } + if (!s->running && !s->gate_hold_ns && !s->quarantined) + h16_gate_release(s); + else if (s->gate_held && !s->running && s->lock) + mod_delayed_work(system_dfl_wq, &s->gate_work, + nsecs_to_jiffies(s->gate_hold_ns)); + s->ops->log(s->ctx, "peer endpoint unset", e->index, (u64)-ret); + return ret; +} +EXPORT_SYMBOL_GPL(ane_h16_session_peer_unset); + +static int h16_report(struct ane_h16_session *s, const u8 *report, size_t size); + +/* + * Serve the firmware outside any request. With a peer endpoint set it asks + * the host to map the peer's surfaces (SHAREDMALLOC) and posts reports on + * IO_T2H while nothing waits; an unread report holds one of its command + * slots, and with all of them held it asserts ("CRPCClient.cpp, 303: out of + * command cache", a few seconds into an MLVNR stream). Reports go to a waiting + * request as in + * h16_wait_report(), the others are taken and dropped. Caller holds the + * session lock. + */ +int ane_h16_session_service(struct ane_h16_session *s) +{ + struct ane_fw_transport *t = s->transport; + u8 report[ANE_H16_REPORT_MAX]; + u32 pending; + size_t size; + int ret; + + if (s->asleep || s->parked || s->quarantined) + return 0; + ret = ane_fw_transport_irq(t, &pending); + while (!ret) + ret = ane_fw_transport_shared(t); + if (ret == -EAGAIN) + ret = 0; + while (!ret) { + ret = ane_fw_transport_consume(t, report, sizeof(report), &size); + if (ret) + break; + ret = h16_report(s, report, size); + if (ret == -ENOENT || ret == -ENOMSG) { + s->peer_reports++; + ret = 0; + } + } + if (ret == -EAGAIN) + ret = ane_fw_transport_terminal(t); + return ret ? h16_fail(s, ret) : 0; +} +EXPORT_SYMBOL_GPL(ane_h16_session_service); + /* * One firmware log record: a 2-byte header, then NUL-terminated text in the * rest of the 0x80 bytes. Copies the printable text (other bytes become '.') @@ -2595,6 +2854,8 @@ void ane_h16_session_restart(struct ane_h16_session *s) bitmap_zero(s->records, ANE_H16_RECORDS); /* A firmware booted again from the preload starts with gating on. */ s->gate_held = false; + /* And without a peer endpoint: unsetting it would assert. */ + s->peer = false; s->executed = 0; memset(s->pending.slots, 0, sizeof(s->pending.slots)); s->pending.error = 0; diff --git a/drivers/accel/ane/ane_h16_program.h b/drivers/accel/ane/ane_h16_program.h index e3f5abd791e30..3db19b2fe14ed 100644 --- a/drivers/accel/ane/ane_h16_program.h +++ b/drivers/accel/ane/ane_h16_program.h @@ -31,6 +31,36 @@ #define ANE_H16_TERMINATE_SIZE 0x10 #define ANE_H16_UNLOAD_SIZE 0xc #define ANE_H16_GATE_SIZE 0xc +#define ANE_H16_MCACHE_SIZE 0xc +#define ANE_H16_ENDPOINT_SIZE 0x30 +#define ANE_H16_AFPP_SIZE 0x38 +#define ANE_H16_DSID_SET_SIZE 0x20 + +/* + * A peer coprocessor's message endpoint (IPC_ENDPOINT_SET2): the ring the + * engine writes and the peer reads (tx), the ring the peer writes (rx), both + * as engine DVAs, and the peer's doorbell with the value that rings it. + */ +struct ane_h16_endpoint { + u32 index; + u64 tx_dva; + u32 tx_size; + u64 rx_dva; + u32 rx_size; + u64 doorbell; + u32 doorbell_value; + /* + * The peer's networks (AFPP): the firmware asserts on SET2 unless + * they are loaded first. Zero size for none. + */ + u64 afpp_dva; + u64 afpp_size; + /* + * The mcache DSID the firmware spills the networks' intermediates to + * (it asserts on the first frame without one); 0 for none. + */ + u32 spill_dsid; +}; #define ANE_H16_REPORT_MAX 0x100 /* * One program's LOAD and CREATE, which stay in the command arena while the @@ -150,6 +180,11 @@ int ane_h16_terminate_encode(void *command, size_t length, u32 program, u32 proc int ane_h16_unload_encode(void *command, size_t length, u32 program); int ane_h16_id_reply(const void *reply, size_t length, const void *request); int ane_h16_gate_encode(void *command, size_t length, u32 value); +int ane_h16_mcache_encode(void *command, size_t length); +int ane_h16_afpp_encode(void *command, size_t length, bool load, u64 dva, u64 size); +int ane_h16_dsid_set_encode(void *command, size_t length, u32 prefetch, u32 spill); +int ane_h16_endpoint_encode(void *command, size_t length, bool set, + const struct ane_h16_endpoint *e); int ane_h16_process_bindings(const struct ane_h16_hwx *hwx, const void *generic, size_t generic_size, @@ -411,6 +446,13 @@ struct ane_h16_session { */ u64 gate_hold_ns; bool gate_held; + /* + * A peer endpoint is set (ane_h16_session_peer_set()): the peer hands + * the firmware work of its own, so gating stays off until it is unset. + */ + bool peer; + /* Reports served outside any request with no request waiting for them. */ + u64 peer_reports; u64 gate_last_ns; struct delayed_work gate_work; /* Firmware phases of the last successful EXECUTE. */ @@ -484,6 +526,10 @@ void ane_h16_session_recovered(struct ane_h16_session *s); int ane_h16_session_replay(struct ane_h16_session *s, unsigned int *replayed, u64 *first_ns); bool ane_h16_program_lost(const struct ane_h16_session *s, const struct ane_h16_program *p); +int ane_h16_session_peer_set(struct ane_h16_session *s, const struct ane_h16_endpoint *e, + u32 *mcache_size); +int ane_h16_session_peer_unset(struct ane_h16_session *s, const struct ane_h16_endpoint *e); +int ane_h16_session_service(struct ane_h16_session *s); void ane_h16_session_sync(struct ane_h16_session *s, struct ane_h16_program *p, bool for_device); diff --git a/drivers/accel/ane/ane_h16_program_test.c b/drivers/accel/ane/ane_h16_program_test.c index 98c56f388fb33..12396638de5c1 100644 --- a/drivers/accel/ane/ane_h16_program_test.c +++ b/drivers/accel/ane/ane_h16_program_test.c @@ -37,6 +37,8 @@ enum fw_mode { FW_REPORT_LATE, /* Reports and the clock are driven by the test under the session mutex. */ FW_REPORT_PIPELINE, + /* IPC_ENDPOINT_SET2 answered with a nonzero status. */ + FW_ENDPOINT_NAK, }; /* A synchronous fake firmware that answers on each doorbell. */ @@ -306,6 +308,13 @@ static void fw_command(struct prog_fw *f, unsigned int ch) f->exec_length = min_t(size_t, length, sizeof(f->exec)); memcpy(f->exec, cmd, f->exec_length); break; + case ANE_H16_MCACHE_SIZE_GET: + put_unaligned_le32(0x300000, cmd + 8); + break; + case ANE_H16_IPC_ENDPOINT_SET2: + if (f->mode == FW_ENDPOINT_NAK) + put_unaligned_le16(1, cmd + 6); + break; } put_unaligned_le64(length, slot + 8); put_unaligned_le64(0, slot + 16); @@ -1409,6 +1418,229 @@ static void program_replies_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ane_h16_gate_encode(request, 0xc, 2), -EINVAL); } +/* + * The ISP peer endpoint on the J616s: SET2 and UNSET2 share the packed body + * with u64s at the 4-byte aligned +0x0c and +0x24. + */ +static void program_endpoint_encode_test(struct kunit *test) +{ + const struct ane_h16_endpoint e = { + .index = 0, + .tx_dva = 0x100002000000, + .tx_size = 0x4000, + .rx_dva = 0x100002004000, + .rx_size = 0x4000, + .doorbell = 0x581668000, + .doorbell_value = 0x10, + }; + u8 command[ANE_H16_ENDPOINT_SIZE]; + + memset(command, 0xa5, sizeof(command)); + KUNIT_ASSERT_EQ(test, ane_h16_endpoint_encode(command, sizeof(command), true, &e), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command), 0U); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 4), (u16)0x2a); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 6), (u16)0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x08), 0U); + /* The ring the engine reads first, then the one it writes. */ + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x0c), 0x100002004000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x14), 0x4000U); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x18), 0x100002000000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x20), 0x4000U); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x24), 0x581668000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x2c), 0x10U); + KUNIT_ASSERT_EQ(test, ane_h16_endpoint_encode(command, sizeof(command), false, &e), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 4), (u16)0x2b); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x24), 0x581668000ULL); + KUNIT_EXPECT_EQ(test, ane_h16_endpoint_encode(command, 0x2c, true, &e), -EINVAL); + KUNIT_EXPECT_EQ(test, ane_h16_endpoint_encode(command, sizeof(command), true, NULL), + -EINVAL); + + memset(command, 0xa5, ANE_H16_MCACHE_SIZE); + KUNIT_ASSERT_EQ(test, ane_h16_mcache_encode(command, ANE_H16_MCACHE_SIZE), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 4), (u16)0x26); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 8), 0U); + KUNIT_EXPECT_EQ(test, ane_h16_mcache_encode(command, 0x10), -EINVAL); +} + +/* + * The ISP peer endpoint: gating goes off, then MCACHE_SIZE_GET and SET2; it + * stays off while the endpoint is set, and goes back on after UNSET2. + */ +static void program_peer_endpoint_test(struct kunit *test) +{ + const struct ane_h16_endpoint e = { + .tx_dva = FW_MAP_DVA, .tx_size = SZ_16K, + .rx_dva = FW_MAP_DVA + SZ_16K, .rx_size = SZ_16K, + .doorbell = 0x581668000, .doorbell_value = 0x10, + }; + struct prog_fw *f = fw_create(test); + u32 mcache = 0; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_set(&f->s, &e, &mcache), 0); + KUNIT_EXPECT_EQ(test, mcache, 0x300000U); + KUNIT_ASSERT_EQ(test, f->count, 3U); + KUNIT_EXPECT_EQ(test, f->ids[0], (u16)ANE_H16_POWER_GATE); + KUNIT_EXPECT_EQ(test, f->values[0], 0U); + KUNIT_EXPECT_EQ(test, f->ids[1], (u16)ANE_H16_MCACHE_SIZE_GET); + KUNIT_EXPECT_EQ(test, f->ids[2], (u16)ANE_H16_IPC_ENDPOINT_SET2); + KUNIT_EXPECT_TRUE(test, f->s.peer); + KUNIT_EXPECT_TRUE(test, f->s.gate_held); + KUNIT_EXPECT_EQ(test, ane_h16_session_peer_set(&f->s, &e, &mcache), -EBUSY); + /* No idle expiry closes the gate under the peer. */ + KUNIT_EXPECT_EQ(test, ane_h16_session_gate_expire(&f->s), 0); + KUNIT_EXPECT_TRUE(test, f->s.gate_held); + + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_unset(&f->s, &e), 0); + KUNIT_ASSERT_EQ(test, f->count, 5U); + KUNIT_EXPECT_EQ(test, f->ids[3], (u16)ANE_H16_IPC_ENDPOINT_UNSET2); + KUNIT_EXPECT_EQ(test, f->ids[4], (u16)ANE_H16_POWER_GATE); + KUNIT_EXPECT_EQ(test, f->values[4], 1U); + KUNIT_EXPECT_FALSE(test, f->s.peer); + KUNIT_EXPECT_FALSE(test, f->s.gate_held); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + /* A second unset sends nothing. */ + KUNIT_EXPECT_EQ(test, ane_h16_session_peer_unset(&f->s, &e), 0); + KUNIT_EXPECT_EQ(test, f->count, 5U); +} + +/* + * With the peer's networks, LOAD_AFPP goes between MCACHE_SIZE_GET and SET2, + * and UNLOAD_AFPP after UNSET2, both naming the same buffer. + */ +static void program_peer_afpp_test(struct kunit *test) +{ + const struct ane_h16_endpoint e = { + .tx_dva = FW_MAP_DVA, .tx_size = SZ_16K, + .rx_dva = FW_MAP_DVA + SZ_16K, .rx_size = SZ_16K, + .doorbell = 0x581668000, .doorbell_value = 0x10, + .afpp_dva = FW_MAP_DVA + SZ_32K, .afpp_size = 0x90a980, + .spill_dsid = 0xe, + }; + struct prog_fw *f = fw_create(test); + u8 command[ANE_H16_AFPP_SIZE]; + u32 mcache = 0; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_set(&f->s, &e, &mcache), 0); + KUNIT_ASSERT_EQ(test, f->count, 5U); + KUNIT_EXPECT_EQ(test, f->ids[1], (u16)ANE_H16_MCACHE_SIZE_GET); + KUNIT_EXPECT_EQ(test, f->ids[2], (u16)ANE_H16_MCACHE_DSID_SET); + /* The pair count, as the first word after the header. */ + KUNIT_EXPECT_EQ(test, f->values[2], 2U); + KUNIT_EXPECT_EQ(test, f->ids[3], (u16)ANE_H16_LOAD_AFPP); + KUNIT_EXPECT_EQ(test, f->ids[4], (u16)ANE_H16_IPC_ENDPOINT_SET2); + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_unset(&f->s, &e), 0); + KUNIT_ASSERT_EQ(test, f->count, 8U); + KUNIT_EXPECT_EQ(test, f->ids[5], (u16)ANE_H16_IPC_ENDPOINT_UNSET2); + KUNIT_EXPECT_EQ(test, f->ids[6], (u16)ANE_H16_UNLOAD_AFPP); + KUNIT_EXPECT_EQ(test, f->ids[7], (u16)ANE_H16_POWER_GATE); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + + memset(command, 0xa5, sizeof(command)); + KUNIT_ASSERT_EQ(test, ane_h16_afpp_encode(command, sizeof(command), true, + 0x100016fc000, 0x90a980), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 4), (u16)0x205); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x08), 1U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x0c), 0U); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x10), 0ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x18), 1U); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x20), 0x100016fc000ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x28), 0x90a980ULL); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(command + 0x30), 0ULL); + KUNIT_ASSERT_EQ(test, ane_h16_afpp_encode(command, sizeof(command), false, + 0x100016fc000, 0x90a980), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 4), (u16)0x206); + KUNIT_EXPECT_EQ(test, ane_h16_afpp_encode(command, sizeof(command), true, 0, 1), -EINVAL); + + /* MCACHE_DSID_SET as the J616s camera start sends it: prefetch 0, spill 0xe. */ + memset(command, 0xa5, sizeof(command)); + KUNIT_ASSERT_EQ(test, ane_h16_dsid_set_encode(command, ANE_H16_DSID_SET_SIZE, 0, 0xe), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 4), (u16)0x2e); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(command + 6), (u16)0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x08), 2U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x0c), 0U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x10), 3U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x14), 0U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x18), 4U); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(command + 0x1c), 0xeU); + KUNIT_EXPECT_EQ(test, ane_h16_dsid_set_encode(command, 0x1c, 0, 0xe), -EINVAL); + KUNIT_EXPECT_EQ(test, ane_h16_afpp_encode(command, 0x30, true, 1, 1), -EINVAL); +} + +/* + * With a peer endpoint set and no request waiting, the firmware's reports + * are still taken: each unread one holds a firmware command slot. + */ +static void program_peer_service_reports_test(struct kunit *test) +{ + const struct ane_h16_endpoint e = { + .tx_dva = FW_MAP_DVA, .tx_size = SZ_16K, + .rx_dva = FW_MAP_DVA + SZ_16K, .rx_size = SZ_16K, + .doorbell = 0x581668000, .doorbell_value = 0x10, + }; + struct prog_fw *f = fw_create(test); + u32 mcache = 0; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_set(&f->s, &e, &mcache), 0); + for (i = 0; i < 3; i++) + fw_report(f, 0x1234 + i, 0x11, 0, 3); + KUNIT_EXPECT_EQ(test, ane_h16_session_service(&f->s), 0); + KUNIT_EXPECT_EQ(test, f->s.peer_reports, 3ULL); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); + /* Taken and acknowledged: nothing is left to serve. */ + KUNIT_EXPECT_EQ(test, ane_h16_session_service(&f->s), 0); + KUNIT_EXPECT_EQ(test, f->s.peer_reports, 3ULL); +} + +/* + * A firmware booted again (a recovery) knows no endpoint: unsetting it sends + * nothing to the new firmware, which would assert on it. + */ +static void program_peer_restart_test(struct kunit *test) +{ + const struct ane_h16_endpoint e = { + .tx_dva = FW_MAP_DVA, .tx_size = SZ_16K, + .rx_dva = FW_MAP_DVA + SZ_16K, .rx_size = SZ_16K, + .doorbell = 0x581668000, .doorbell_value = 0x10, + }; + struct prog_fw *f = fw_create(test); + u32 mcache = 0; + unsigned int count; + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_set(&f->s, &e, &mcache), 0); + ane_h16_session_restart(&f->s); + KUNIT_EXPECT_FALSE(test, f->s.peer); + count = f->count; + KUNIT_EXPECT_EQ(test, ane_h16_session_peer_unset(&f->s, &e), 0); + KUNIT_EXPECT_EQ(test, f->count, count); +} + +/* A refused SET2 fails the transport like any refused command: quarantine. */ +static void program_peer_endpoint_refused_test(struct kunit *test) +{ + const struct ane_h16_endpoint e = { + .tx_dva = FW_MAP_DVA, .tx_size = SZ_16K, + .rx_dva = FW_MAP_DVA + SZ_16K, .rx_size = SZ_16K, + .doorbell = 0x581668000, .doorbell_value = 0x10, + }; + struct prog_fw *f = fw_create(test); + u32 mcache = 0; + + KUNIT_ASSERT_NOT_NULL(test, f); + f->mode = FW_ENDPOINT_NAK; + KUNIT_EXPECT_EQ(test, ane_h16_session_peer_set(&f->s, &e, &mcache), -EREMOTEIO); + KUNIT_EXPECT_FALSE(test, f->s.peer); + KUNIT_EXPECT_TRUE(test, f->s.quarantined); + /* Nothing more goes to a quarantined firmware, the closing gate neither. */ + KUNIT_EXPECT_EQ(test, f->count, 3U); + KUNIT_EXPECT_EQ(test, ane_h16_session_peer_unset(&f->s, &e), 0); + KUNIT_EXPECT_EQ(test, f->count, 3U); +} + static void program_deny_list_test(struct kunit *test) { static const u16 denied[] = { 0x27, 0x28, 0x30, 0x31, 0x34 }; @@ -4949,6 +5181,12 @@ static void program_rpm_s2r_while_parked_test(struct kunit *test) } static struct kunit_case program_cases[] = { + KUNIT_CASE(program_endpoint_encode_test), + KUNIT_CASE(program_peer_endpoint_test), + KUNIT_CASE(program_peer_endpoint_refused_test), + KUNIT_CASE(program_peer_afpp_test), + KUNIT_CASE(program_peer_service_reports_test), + KUNIT_CASE(program_peer_restart_test), KUNIT_CASE_SLOW(program_pool_rewind_test), KUNIT_CASE_SLOW(program_pool_resident_test), KUNIT_CASE_SLOW(program_pool_records_reuse_test), diff --git a/drivers/accel/ane/ane_h16_protocol.h b/drivers/accel/ane/ane_h16_protocol.h index a3ef6a9c25ae8..5c652bf947695 100644 --- a/drivers/accel/ane/ane_h16_protocol.h +++ b/drivers/accel/ane/ane_h16_protocol.h @@ -13,6 +13,15 @@ enum ane_h16_command { ANE_H16_EXECUTE = 0x020c, ANE_H16_POWER_GATE = 0x002d, ANE_H16_EVENT_REPORT = 0x0300, + /* The ISP peer endpoint, from an H16 macOS 27.0 camera start. */ + ANE_H16_MCACHE_SIZE_GET = 0x0026, + ANE_H16_IPC_ENDPOINT_SET2 = 0x002a, + ANE_H16_IPC_ENDPOINT_UNSET2 = 0x002b, + /* The mcache DSIDs for the peer's networks (prefetch, intermediate spill). */ + ANE_H16_MCACHE_DSID_SET = 0x002e, + /* The peer's networks (AFPP) the firmware needs before SET2. */ + ANE_H16_LOAD_AFPP = 0x0205, + ANE_H16_UNLOAD_AFPP = 0x0206, }; struct ane_h16_command_info { @@ -34,6 +43,12 @@ static inline const struct ane_h16_command_info *ane_h16_command_info(u16 id) { ANE_H16_EXECUTE, 0xca8, 0xcd8, 0x18, 7, false }, { ANE_H16_POWER_GATE, 0xc, 0xc, 1, 7, false }, { ANE_H16_EVENT_REPORT, 0x60, 0x60, 1, 6, true }, + { ANE_H16_MCACHE_SIZE_GET, 0xc, 0xc, 1, 1, false }, + { ANE_H16_IPC_ENDPOINT_SET2, 0x30, 0x30, 1, 1, false }, + { ANE_H16_IPC_ENDPOINT_UNSET2, 0x30, 0x30, 1, 1, false }, + { ANE_H16_MCACHE_DSID_SET, 0x20, 0x20, 1, 1, false }, + { ANE_H16_LOAD_AFPP, 0x38, 0x38, 1, 1, false }, + { ANE_H16_UNLOAD_AFPP, 0x38, 0x38, 1, 1, false }, }; unsigned int i; diff --git a/drivers/media/platform/apple/isp/Kconfig b/drivers/media/platform/apple/isp/Kconfig index 48f9b401861b8..1f1d4c99a9459 100644 --- a/drivers/media/platform/apple/isp/Kconfig +++ b/drivers/media/platform/apple/isp/Kconfig @@ -14,6 +14,8 @@ config VIDEO_APPLE_ISP # The camera indicator contract is exported by DRM_APPLE: a built-in # ISP cannot link against a modular DRM_APPLE. depends on DRM_APPLE || !DRM_APPLE + # MLVNR runs on the ANE: a built-in ISP cannot link against a modular ANE. + depends on DRM_ACCEL_ANE || !DRM_ACCEL_ANE help Driver for the image signal processor that runs the built-in camera of Apple silicon machines. The ISP firmware is loaded by diff --git a/drivers/media/platform/apple/isp/Makefile b/drivers/media/platform/apple/isp/Makefile index c07e54a522131..847df6050c4f9 100644 --- a/drivers/media/platform/apple/isp/Makefile +++ b/drivers/media/platform/apple/isp/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only -apple-isp-y := isp-cam.o isp-cmd.o isp-drv.o isp-fw.o isp-iommu.o isp-ipc.o isp-v4l2.o +apple-isp-y := isp-ane.o isp-cam.o isp-cmd.o isp-drv.o isp-fw.o isp-iommu.o isp-ipc.o isp-v4l2.o obj-$(CONFIG_VIDEO_APPLE_ISP) += apple-isp.o obj-$(CONFIG_VIDEO_APPLE_ISP_IPC_LAYOUT_KUNIT_TEST) += isp-ipc-layout-test.o diff --git a/drivers/media/platform/apple/isp/isp-ane-test.c b/drivers/media/platform/apple/isp/isp-ane-test.c new file mode 100644 index 0000000000000..de0737bcc9cf5 --- /dev/null +++ b/drivers/media/platform/apple/isp/isp-ane-test.c @@ -0,0 +1,106 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * KUnit tests for the ANE's view of the ISP's surfaces, built into + * isp-ane.c: the ANE finds a SHAREDMALLOC surface by the ID the firmware + * got for it, and holds it until it lets go, also after the firmware freed it. + */ + +#include +#include + +static struct apple_isp *isp_ane_test_alloc(struct kunit *test, struct isp_surf **surfs) +{ + struct apple_isp *isp = kunit_kzalloc(test, sizeof(*isp), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, isp); + INIT_LIST_HEAD(&isp->gc); + INIT_LIST_HEAD(&isp->gc_released); + mutex_init(&isp->gc_lock); + for (int i = 0; i < 2; i++) { + surfs[i] = kunit_kzalloc(test, sizeof(*surfs[i]), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, surfs[i]); + surfs[i]->id = 0x3c + i; + surfs[i]->size = SZ_64K; + refcount_set(&surfs[i]->refcount, 1); + list_add_tail(&surfs[i]->head, &isp->gc); + } + return isp; +} + +static void isp_test_ane_surface_by_id(struct kunit *test) +{ + struct isp_surf *surfs[2]; + struct apple_isp *isp = isp_ane_test_alloc(test, surfs); + struct sg_table *sgt; + size_t size; + + KUNIT_ASSERT_EQ(test, isp_ane_get_surface(isp, 0x3d, &sgt, &size), 0); + KUNIT_EXPECT_PTR_EQ(test, sgt, &surfs[1]->sgt); + KUNIT_EXPECT_EQ(test, size, (size_t)SZ_64K); + KUNIT_EXPECT_EQ(test, refcount_read(&surfs[1]->refcount), 2); + KUNIT_EXPECT_EQ(test, refcount_read(&surfs[0]->refcount), 1); + KUNIT_EXPECT_EQ(test, isp_ane_get_surface(isp, 0x3e, &sgt, &size), -ENOENT); + KUNIT_EXPECT_EQ(test, isp_ane_get_surface(isp, 0, &sgt, &size), -ENOENT); + isp_ane_put_surface(isp, 0x3d); + KUNIT_EXPECT_EQ(test, refcount_read(&surfs[1]->refcount), 1); +} + +/* A surface the firmware freed is not given out, but still let go of. */ +static void isp_test_ane_surface_released(struct kunit *test) +{ + struct isp_surf *surfs[2]; + struct apple_isp *isp = isp_ane_test_alloc(test, surfs); + struct sg_table *sgt; + size_t size; + + KUNIT_ASSERT_EQ(test, isp_ane_get_surface(isp, 0x3c, &sgt, &size), 0); + list_move_tail(&surfs[0]->head, &isp->gc_released); + KUNIT_EXPECT_EQ(test, isp_ane_get_surface(isp, 0x3c, &sgt, &size), -ENOENT); + isp_ane_put_surface(isp, 0x3c); + KUNIT_EXPECT_EQ(test, refcount_read(&surfs[0]->refcount), 1); +} + +/* A stream that fails with an untested endpoint turns the endpoint off for good. */ +static void isp_test_ane_fallback(struct kunit *test) +{ + struct apple_isp *isp = kunit_kzalloc(test, sizeof(*isp), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, isp); + isp->dev = kunit_device_register(test, "apple-isp-ane-test"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, isp->dev); + /* No endpoint: a failed stream changes nothing. */ + isp_ane_stream_failed(isp); + KUNIT_EXPECT_FALSE(test, isp->ane_failed); + /* An endpoint that worked for a stream: a later failure is not its fault. */ + isp->ane_untested = true; + isp_ane_stream_started(isp); + isp_ane_stream_failed(isp); + KUNIT_EXPECT_FALSE(test, isp->ane_failed); + /* Set, then the stream failed (also after the firmware already stopped). */ + isp->ane_untested = true; + isp->ane_set = false; + isp_ane_stream_failed(isp); + KUNIT_EXPECT_TRUE(test, isp->ane_failed); + + /* A firmware stopped by force with the endpoint attached ends it too. */ + isp->ane_failed = false; + isp_ane_firmware_failed(isp); + KUNIT_EXPECT_FALSE(test, isp->ane_failed); + isp->ane_attached = true; + isp_ane_firmware_failed(isp); + KUNIT_EXPECT_TRUE(test, isp->ane_failed); +} + +static struct kunit_case isp_ane_test_cases[] = { + KUNIT_CASE(isp_test_ane_fallback), + KUNIT_CASE(isp_test_ane_surface_by_id), + KUNIT_CASE(isp_test_ane_surface_released), + {} +}; + +static struct kunit_suite isp_ane_test_suite = { + .name = "apple-isp-ane", + .test_cases = isp_ane_test_cases, +}; + +kunit_test_suite(isp_ane_test_suite); diff --git a/drivers/media/platform/apple/isp/isp-ane.c b/drivers/media/platform/apple/isp/isp-ane.c new file mode 100644 index 0000000000000..eafbc8badbda0 --- /dev/null +++ b/drivers/media/platform/apple/isp/isp-ane.c @@ -0,0 +1,300 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * The ANE peer endpoint. The ISP firmware runs MLVNR, its ML noise + * reduction, on the Neural Engine itself: macOS powers the ANE when the ISP + * firmware starts, gives the ANE firmware the ISP's two message rings and + * the ISP's doorbell, then gives the ISP firmware the same rings and the + * ANE's doorbell (CISP_CMD_IPC_ENDPOINT_SET2). The ANE firmware then asks + * for the ISP's surfaces by the IDs the ISP firmware got for them at + * SHAREDMALLOC. Without the endpoint, MLVNR is never configured. + */ +#include +#include +#include +#include +#include +#include + +#include "isp-ane.h" +#include "isp-cmd.h" +#include "isp-iommu.h" + +/* Each ring, as macOS allocates them. */ +#define ISP_ANE_RING_SIZE SZ_16K + +/* + * The networks the ANE firmware loads for the ISP before the endpoint. macOS + * keeps one file per board (/System/Library/Isp/afpp//ispane.afpp, + * read by the camera daemon at boot); here it is apple/ispane-.afpp, + * taken from the Mac's own install like the setfiles, with the board from + * the root compatible ("apple,j616s"). Without it the ANE firmware asserts + * on the endpoint, so the ISP runs without one. + */ +static void isp_ane_networks_name(char *name, size_t size) +{ + const char *model = NULL, *board; + + of_property_read_string_index(of_root, "compatible", 0, &model); + board = model ? strchr(model, ',') : NULL; + snprintf(name, size, "apple/ispane-%s.afpp", board ? board + 1 : model ?: "unknown"); +} + +/* A live surface the firmware has, by ID; caller holds gc_lock. */ +static struct isp_surf *isp_ane_find(struct apple_isp *isp, struct list_head *list, u32 id) +{ + struct isp_surf *surf; + + list_for_each_entry(surf, list, head) + if (surf->id == id) + return surf; + return NULL; +} + +/* + * The ANE maps the surface until put_surface: the reference keeps it from + * being freed with the stream's released surfaces (isp_free_released_surfaces()). + */ +static int isp_ane_get_surface(void *ctx, u32 id, struct sg_table **sgt, size_t *size) +{ + struct apple_isp *isp = ctx; + struct isp_surf *surf; + + guard(mutex)(&isp->gc_lock); + surf = isp_ane_find(isp, &isp->gc, id); + if (!surf) + return -ENOENT; + refcount_inc(&surf->refcount); + *sgt = &surf->sgt; + *size = surf->size; + return 0; +} + +static void isp_ane_put_surface(void *ctx, u32 id) +{ + struct apple_isp *isp = ctx; + struct isp_surf *surf; + + guard(mutex)(&isp->gc_lock); + surf = isp_ane_find(isp, &isp->gc, id) ?: isp_ane_find(isp, &isp->gc_released, id); + if (!WARN_ON(!surf)) + refcount_dec(&surf->refcount); +} + +static const struct apple_ane_peer_ops isp_ane_ops = { + .get_surface = isp_ane_get_surface, + .put_surface = isp_ane_put_surface, +}; + +static void isp_ane_free_networks(struct apple_isp *isp) +{ + if (!isp->ane_net_pages) + return; + sg_free_table(&isp->ane_net_sgt); + for (unsigned int i = 0; i < isp->ane_net_npages; i++) + if (isp->ane_net_pages[i]) + __free_page(isp->ane_net_pages[i]); + kvfree(isp->ane_net_pages); + isp->ane_net_pages = NULL; + isp->ane_net_npages = 0; +} + +/* + * Read the networks once, into whole pages the ANE maps: they stay for the + * driver's lifetime, as macOS keeps them from boot. + */ +static int isp_ane_read_networks(struct apple_isp *isp) +{ + const struct firmware *fw; + char name[64]; + unsigned int n; + int err; + + if (isp->ane_net_pages) + return 0; + /* Looked for at each firmware start: it may be installed later. */ + isp_ane_networks_name(name, sizeof(name)); + err = firmware_request_nowarn(&fw, name, isp->dev); + if (err) { + if (!isp->ane_net_missing) + dev_info(isp->dev, "no %s (%d): no ANE endpoint, no MLVNR\n", + name, err); + isp->ane_net_missing = true; + return err; + } + n = DIV_ROUND_UP(ALIGN(fw->size, SZ_16K), PAGE_SIZE); + err = -ENOMEM; + isp->ane_net_pages = kvcalloc(n, sizeof(*isp->ane_net_pages), GFP_KERNEL); + if (!fw->size || !isp->ane_net_pages) + goto release; + isp->ane_net_npages = n; + for (unsigned int i = 0; i < n; i++) { + size_t off = (size_t)i * PAGE_SIZE; + + isp->ane_net_pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO); + if (!isp->ane_net_pages[i]) + goto release; + if (off < fw->size) + memcpy_to_page(isp->ane_net_pages[i], 0, fw->data + off, + min_t(size_t, PAGE_SIZE, fw->size - off)); + } + err = sg_alloc_table_from_pages(&isp->ane_net_sgt, isp->ane_net_pages, n, 0, + (size_t)n * PAGE_SIZE, GFP_KERNEL); + if (err) + goto release; + isp->ane_net_size = fw->size; + release_firmware(fw); + return 0; +release: + release_firmware(fw); + isp_ane_free_networks(isp); + return err; +} + +static void isp_ane_free_rings(struct apple_isp *isp) +{ + for (unsigned int i = 0; i < ARRAY_SIZE(isp->ane_rings); i++) { + if (isp->ane_rings[i]) + isp_free_surface(isp, isp->ane_rings[i]); + isp->ane_rings[i] = NULL; + } +} + +/* + * After START, before the first channel command, as macOS does. Without the + * ANE (not described, disabled or not up yet) the firmware runs without + * MLVNR, as it always has. + */ +void isp_ane_attach(struct apple_isp *isp) +{ + struct apple_ane_peer peer; + phys_addr_t doorbell; + u32 value; + int err; + + if (!isp->hw->peer_doorbell || isp->ane_failed) + return; + if (!isp->ane) { + struct apple_ane *ane = apple_ane_get(isp->dev); + + if (IS_ERR_OR_NULL(ane)) { + if (ane) + dev_info(isp->dev, "ANE not up yet: no MLVNR this time\n"); + return; + } + isp->ane = ane; + } + if (isp_ane_read_networks(isp)) + return; + + for (unsigned int i = 0; i < ARRAY_SIZE(isp->ane_rings); i++) { + isp->ane_rings[i] = isp_alloc_surface(isp, ISP_ANE_RING_SIZE); + if (!isp->ane_rings[i]) { + isp_ane_free_rings(isp); + return; + } + } + peer = (struct apple_ane_peer) { + .ops = &isp_ane_ops, + .ctx = isp, + /* The ANE writes the ring the ISP reads. */ + .tx = &isp->ane_rings[0]->sgt, + .rx = &isp->ane_rings[1]->sgt, + .ring_size = ISP_ANE_RING_SIZE, + .doorbell = isp->hw->peer_doorbell, + .doorbell_value = isp->hw->peer_doorbell_value, + .networks = &isp->ane_net_sgt, + .networks_size = isp->ane_net_size, + }; + err = apple_ane_peer_attach(isp->ane, &peer, &doorbell, &value); + if (err) { + dev_warn(isp->dev, "ANE endpoint refused: %d, no MLVNR\n", err); + isp_ane_free_rings(isp); + return; + } + isp->ane_attached = true; + isp->ane_doorbell = doorbell; + isp->ane_doorbell_value = value; + + err = isp_cmd_ipc_endpoint_set2(isp, true, isp->ane_rings[0]->iova, + isp->ane_rings[1]->iova, ISP_ANE_RING_SIZE, + doorbell, value); + if (err) { + dev_warn(isp->dev, "firmware refused the ANE endpoint: %d\n", err); + return; + } + isp->ane_set = true; + isp->ane_untested = true; + dev_info(isp->dev, "ANE endpoint set\n"); +} + +/* + * The camera must work without MLVNR: a stream that fails to start with the + * endpoint set (the firmware's ANE client asserting) stops the endpoint for + * every later stream, until the driver is loaded again. + */ +void isp_ane_stream_failed(struct apple_isp *isp) +{ + /* The firmware may already have stopped, with the endpoint. */ + if (!isp->ane_untested || isp->ane_failed) + return; + isp->ane_failed = true; + dev_warn(isp->dev, "stream failed with the ANE endpoint set: no ANE endpoint (MLVNR) until the driver reloads\n"); +} + +void isp_ane_stream_started(struct apple_isp *isp) +{ + isp->ane_untested = false; +} + +/* + * A firmware that had to be stopped by force with the endpoint set (its ANE + * client asserted mid-stream) likewise ends the endpoint until reload. + */ +void isp_ane_firmware_failed(struct apple_isp *isp) +{ + if (!isp->ane_attached || isp->ane_failed) + return; + isp->ane_failed = true; + dev_warn(isp->dev, "firmware failed with the ANE endpoint set: no ANE endpoint (MLVNR) until the driver reloads\n"); +} + +/* Before the firmware stops: it lets go of the endpoint first, as on macOS. */ +void isp_ane_unset(struct apple_isp *isp) +{ + int err; + + if (!isp->ane_set) + return; + isp->ane_set = false; + err = isp_cmd_ipc_endpoint_set2(isp, false, isp->ane_rings[0]->iova, + isp->ane_rings[1]->iova, ISP_ANE_RING_SIZE, + isp->ane_doorbell, isp->ane_doorbell_value); + if (err) + dev_warn(isp->dev, "firmware did not unset the ANE endpoint: %d\n", err); +} + +/* + * With the coprocessor stopped but still powered, before its surfaces are + * freed: the ANE unsets the endpoint and unmaps every surface it had, then + * the rings go. + */ +void isp_ane_detach(struct apple_isp *isp) +{ + isp->ane_set = false; + if (isp->ane_attached) + apple_ane_peer_detach(isp->ane); + isp->ane_attached = false; + isp_ane_free_rings(isp); +} + +/* At remove, after the last firmware stop. */ +void isp_ane_put(struct apple_isp *isp) +{ + apple_ane_put(isp->ane); + isp->ane = NULL; + isp_ane_free_networks(isp); +} + +#if IS_ENABLED(CONFIG_VIDEO_APPLE_ISP_KUNIT_TEST) +#include "isp-ane-test.c" +#endif diff --git a/drivers/media/platform/apple/isp/isp-ane.h b/drivers/media/platform/apple/isp/isp-ane.h new file mode 100644 index 0000000000000..f14aeec7dff05 --- /dev/null +++ b/drivers/media/platform/apple/isp/isp-ane.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef __ISP_ANE_H__ +#define __ISP_ANE_H__ + +#include "isp-drv.h" + +void isp_ane_attach(struct apple_isp *isp); +void isp_ane_stream_failed(struct apple_isp *isp); +void isp_ane_stream_started(struct apple_isp *isp); +void isp_ane_firmware_failed(struct apple_isp *isp); +void isp_ane_unset(struct apple_isp *isp); +void isp_ane_detach(struct apple_isp *isp); +void isp_ane_put(struct apple_isp *isp); + +#endif /* __ISP_ANE_H__ */ diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index 4ae4f2dea3210..907d706ec045c 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -986,6 +986,35 @@ static int isp_ch_configure_power_line(struct apple_isp *isp, u32 ch) hz << 16); } +/* + * MLVNR's total gain thresholds (enter, exit) per sensor, as macOS's camera + * start sets them (the firmware echoes them in its log): the J616s IMX958. + */ +static const struct { + u32 sensor; + u32 enter_gain, exit_gain; +} isp_mlvnr_gains[] = { + { 0x958, 3584, 2816 }, +}; + +/* + * With the ANE endpoint set, MLVNR as macOS configures it: automatic, at the + * sensor's gains, 30 fps both ways, fading out over 30 frames. + */ +static int isp_ch_configure_mlvnr(struct apple_isp *isp, u32 ch) +{ + u32 sensor = isp_get_format(isp, ch)->version; + + if (!isp->ane_set) + return 0; + for (unsigned int i = 0; i < ARRAY_SIZE(isp_mlvnr_gains); i++) + if (isp_mlvnr_gains[i].sensor == sensor) + return isp_cmd_apple_ch_mlvnr_mode_config_set(isp, ch, 2, + isp_mlvnr_gains[i].enter_gain, + isp_mlvnr_gains[i].exit_gain, 30, 30); + return 0; +} + static int isp_ch_configure_temporal_filter(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); @@ -1203,6 +1232,10 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) if (err) return err; + err = isp_ch_configure_mlvnr(isp, ch); + if (err) + return err; + if (h17) return isp_cmd_ch_master_slave_sync_mode_set(isp, ch, 0); diff --git a/drivers/media/platform/apple/isp/isp-cmd.c b/drivers/media/platform/apple/isp/isp-cmd.c index 791535ae8320b..87c5ee386daca 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.c +++ b/drivers/media/platform/apple/isp/isp-cmd.c @@ -348,6 +348,24 @@ int isp_cmd_flicker_sensor_set(struct apple_isp *isp, u32 mode) return CISP_SEND_INOUT(isp, args); } +int isp_cmd_ipc_endpoint_set2(struct apple_isp *isp, bool set, u64 read_ring, + u64 write_ring, u32 ring_size, u64 doorbell, + u32 doorbell_value) +{ + struct cmd_ipc_endpoint_set2 args = { + .opcode = CISP_OPCODE(set ? CISP_CMD_IPC_ENDPOINT_SET2 : + CISP_CMD_IPC_ENDPOINT_UNSET2), + .index = 0, + .read_ring = read_ring, + .read_size = ring_size, + .write_ring = write_ring, + .write_size = ring_size, + .doorbell = doorbell, + .doorbell_value = doorbell_value, + }; + return CISP_SEND_INOUT(isp, args); +} + int isp_cmd_ch_info_get(struct apple_isp *isp, u32 chan, struct cmd_ch_info *args) { @@ -634,6 +652,24 @@ int isp_cmd_ch_buffer_pool_return(struct apple_isp *isp, u32 chan) return CISP_SEND_IN(isp, args); } +int isp_cmd_apple_ch_mlvnr_mode_config_set(struct apple_isp *isp, u32 chan, + u32 mode, u32 enter_gain, + u32 exit_gain, u32 fps, + u32 ramp_frames) +{ + struct cmd_apple_ch_mlvnr_mode_config_set args = { + .opcode = CISP_OPCODE(CISP_CMD_APPLE_CH_MLVNR_MODE_CONFIG_SET), + .chan = chan, + .mode = mode, + .enter_gain = enter_gain, + .exit_gain = exit_gain, + .enter_fps = fps, + .exit_fps = fps, + .ramp_frames = ramp_frames, + }; + return CISP_SEND_INOUT(isp, args); +} + int isp_cmd_apple_ch_temporal_filter_start(struct apple_isp *isp, u32 chan, u32 arg) { struct cmd_apple_ch_temporal_filter_start args = { diff --git a/drivers/media/platform/apple/isp/isp-cmd.h b/drivers/media/platform/apple/isp/isp-cmd.h index b7488d08644b6..cc74433811718 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.h +++ b/drivers/media/platform/apple/isp/isp-cmd.h @@ -99,6 +99,7 @@ #define CISP_CMD_APPLE_CH_TEMPORAL_FILTER_DISABLE 0xc114 #define CISP_CMD_APPLE_CH_TEMPORAL_FILTER_BAND0_MODULATION 0xc11c #define CISP_CMD_APPLE_CH_TEMPORAL_FILTER_BAND0_THRESHOLD 0xc11d +#define CISP_CMD_APPLE_CH_MLVNR_MODE_CONFIG_SET 0xc11f #define CISP_CMD_APPLE_CH_TEMPORAL_FILTER_HIGH_STRENGTH 0xc120 /* Opcodes of the DEBUG channel, which has its own numbering */ @@ -257,15 +258,21 @@ struct cmd_fid_exit { } __packed; static_assert(sizeof(struct cmd_fid_exit) == 0x8); +/* + * The ANE peer endpoint: the ring the ISP reads (the ANE writes it), the ring + * the ISP writes, both as ISP IOVAs, and the ANE's doorbell with the value + * that rings it. The ANE gets the same layout (IPC_ENDPOINT_SET2 0x002a). + * UNSET2 sends it again. + */ struct cmd_ipc_endpoint_set2 { u64 opcode; - u32 unk; - u64 addr1; - u32 size1; - u64 addr2; - u32 size2; - u64 regs; - u32 unk2; + u32 index; + u64 read_ring; + u32 read_size; + u64 write_ring; + u32 write_size; + u64 doorbell; + u32 doorbell_value; } __packed; static_assert(sizeof(struct cmd_ipc_endpoint_set2) == 0x30); @@ -305,6 +312,9 @@ int isp_cmd_pmp_ctrl_set(struct apple_isp *isp, u64 clock_scratch, int isp_cmd_fid_enter(struct apple_isp *isp); int isp_cmd_fid_exit(struct apple_isp *isp); int isp_cmd_flicker_sensor_set(struct apple_isp *isp, u32 mode); +int isp_cmd_ipc_endpoint_set2(struct apple_isp *isp, bool set, u64 read_ring, + u64 write_ring, u32 ring_size, u64 doorbell, + u32 doorbell_value); struct cmd_ch_start { u64 opcode; @@ -676,6 +686,24 @@ struct cmd_apple_ch_temporal_filter_start_h17 { } __packed; static_assert(sizeof(struct cmd_apple_ch_temporal_filter_start_h17) == 0x18); +/* + * MLVNR, the firmware's ML noise reduction on the ANE (the firmware echoes + * the words with these names): mode 2 is automatic, entering at a total gain + * of enter_gain and at most enter_fps, leaving below exit_gain or above + * exit_fps, fading out over ramp_frames. + */ +struct cmd_apple_ch_mlvnr_mode_config_set { + u64 opcode; + u32 chan; + u32 mode; + u32 enter_gain; + u32 exit_gain; + u32 enter_fps; + u32 exit_fps; + u32 ramp_frames; +} __packed; +static_assert(sizeof(struct cmd_apple_ch_mlvnr_mode_config_set) == 0x24); + /* High-strength filtering (0xc120) and band 0 modulation (0xc11c) */ struct cmd_apple_ch_temporal_filter_enable_set { u64 opcode; @@ -723,6 +751,10 @@ struct cmd_apple_ch_temporal_filter_disable { static_assert(sizeof(struct cmd_apple_ch_temporal_filter_disable) == 0xc); int isp_cmd_apple_ch_temporal_filter_start(struct apple_isp *isp, u32 chan, u32 arg); +int isp_cmd_apple_ch_mlvnr_mode_config_set(struct apple_isp *isp, u32 chan, + u32 mode, u32 enter_gain, + u32 exit_gain, u32 fps, + u32 ramp_frames); int isp_cmd_apple_ch_temporal_filter_stop(struct apple_isp *isp, u32 chan); int isp_cmd_apple_ch_motion_history_start(struct apple_isp *isp, u32 chan); int isp_cmd_apple_ch_motion_history_stop(struct apple_isp *isp, u32 chan); diff --git a/drivers/media/platform/apple/isp/isp-drv.c b/drivers/media/platform/apple/isp/isp-drv.c index db4e9d1135ef0..df81e2809b4dd 100644 --- a/drivers/media/platform/apple/isp/isp-drv.c +++ b/drivers/media/platform/apple/isp/isp-drv.c @@ -21,6 +21,7 @@ #include #include +#include "isp-ane.h" #include "isp-cam.h" #include "isp-fw.h" #include "isp-iommu.h" @@ -1142,6 +1143,8 @@ static int apple_isp_probe(struct platform_device *pdev) halt_firmware: apple_isp_firmware_halt(isp); + /* A forced stop schedules a restart: none after the last halt. */ + cancel_delayed_work_sync(&isp->fw_recover_work); disable_rpm: pm_runtime_disable(dev); pm_runtime_set_suspended(dev); @@ -1174,6 +1177,9 @@ static void apple_isp_remove(struct platform_device *pdev) /* Stop resident firmware before freeing what it may still use. */ apple_isp_firmware_flush(isp); apple_isp_firmware_halt(isp); + /* A forced stop schedules a restart: none after the last halt. */ + cancel_delayed_work_sync(&isp->fw_recover_work); + isp_ane_put(isp); apple_isp_free_video(isp); pm_runtime_disable(isp->dev); apple_isp_free_firmware_surface(isp); @@ -1546,6 +1552,10 @@ static const struct apple_isp_hw apple_isp_hw_t6041 = { .bandwidth_size = 0x8, .mbox_irq_enable = ISP_MBOX_IRQ_ENABLE_T6031, + /* The J616s's macOS camera start; the ANE may write it (t604x.dtsi). */ + .peer_doorbell = 0x581668000, + .peer_doorbell_value = 0x10, + .scl1 = true, .lpdp = false, .meta_size = ISP_META_SIZE_T8140, @@ -1899,6 +1909,8 @@ static void apple_isp_shutdown(struct platform_device *pdev) mutex_lock(&isp->video_lock); apple_isp_firmware_halt(isp); mutex_unlock(&isp->video_lock); + /* A forced stop schedules a restart: none after the last halt. */ + cancel_delayed_work_sync(&isp->fw_recover_work); } static ssize_t sensor_show(struct device *dev, struct device_attribute *attr, diff --git a/drivers/media/platform/apple/isp/isp-drv.h b/drivers/media/platform/apple/isp/isp-drv.h index 4ddc028373772..d8dd1fb1b52d2 100644 --- a/drivers/media/platform/apple/isp/isp-drv.h +++ b/drivers/media/platform/apple/isp/isp-drv.h @@ -94,6 +94,8 @@ struct isp_surf { dma_addr_t iova; void *virt; refcount_t refcount; + /* The ID the firmware got for a SHAREDMALLOC surface, 0 for none. */ + u32 id; bool gc; bool submitted; bool iova_reserved; /* @iova comes from &apple_isp.iovas */ @@ -213,6 +215,13 @@ struct apple_isp_hw { */ u32 coproc_quiesce; + /* + * The ISP doorbell the ANE rings for the ANE peer endpoint (see + * isp-ane.c), and the value it writes; 0 without the endpoint. + */ + u64 peer_doorbell; + u32 peer_doorbell_value; + /* windows mapped 1:1 for the firmware */ const struct isp_mmio_window *fw_mmio; unsigned int num_fw_mmio; @@ -469,6 +478,12 @@ struct apple_isp { /* firmware that starts again, idling until fw_idle_work stops it */ bool fw_idle; struct delayed_work fw_idle_work; + /* + * Firmware that crashed cannot power the camera module down (its + * status light stays on): it is started once more and stopped cleanly. + */ + struct delayed_work fw_recover_work; + bool fw_recovering; /* the camera was detected; until then a deferred start is pending */ bool camera_detected; /* configurations the firmware has, once a deferred start detected them */ @@ -506,7 +521,35 @@ struct apple_isp { */ struct list_head gc; struct list_head gc_released; - struct mutex gc_lock; /* protects @gc and @gc_released */ + struct mutex gc_lock; /* protects @gc, @gc_released and @surf_id */ + /* the last SHAREDMALLOC surface ID given to the firmware */ + u32 surf_id; + /* + * The ANE the firmware runs its networks on (isp-ane.c): its rings, + * whether the ANE side is attached, and whether the firmware has the + * endpoint. + */ + struct apple_ane *ane; + struct isp_surf *ane_rings[2]; + /* + * The ANE networks file (isp-ane.c), read once; no pages: not read yet. + * ane_net_missing: its absence was reported. + */ + struct page **ane_net_pages; + unsigned int ane_net_npages; + struct sg_table ane_net_sgt; + size_t ane_net_size; + bool ane_net_missing; + phys_addr_t ane_doorbell; + u32 ane_doorbell_value; + bool ane_attached; + bool ane_set; + /* + * ane_untested: the endpoint was set and no stream has started since. + * ane_failed: a stream failed that way, so no endpoint until reload. + */ + bool ane_untested; + bool ane_failed; struct workqueue_struct *wq; int num_ipc_chans; diff --git a/drivers/media/platform/apple/isp/isp-fw.c b/drivers/media/platform/apple/isp/isp-fw.c index 991c0d82b6153..a98ccabd013f4 100644 --- a/drivers/media/platform/apple/isp/isp-fw.c +++ b/drivers/media/platform/apple/isp/isp-fw.c @@ -18,6 +18,7 @@ #include #include +#include "isp-ane.h" #include "isp-cam.h" #include "isp-cmd.h" #include "isp-fw.h" @@ -1174,6 +1175,8 @@ static int isp_start_command_processor(struct apple_isp *isp) if (err) return err; + isp_ane_attach(isp); + /* Now we can access CISP_CMD_CH_* commands */ return 0; @@ -1202,6 +1205,12 @@ static void isp_collect_gc_surface(struct apple_isp *isp) */ static void isp_firmware_stop(struct apple_isp *isp) { + /* + * The ANE lets go of the endpoint while the ISP is still powered, as + * on macOS: the ANE firmware may ring the ISP's doorbell until then, + * and an access to the powered-down block hangs the fabric. + */ + isp_ane_detach(isp); isp_firmware_shutdown_stage1(isp); isp_firmware_shutdown_stage3(isp); @@ -1269,20 +1278,30 @@ static int isp_firmware_boot(struct apple_isp *isp) return err; } -static void isp_firmware_shutdown(struct apple_isp *isp) +/* Returns true when the firmware did not stop and was forced off. */ +static bool isp_firmware_shutdown(struct apple_isp *isp) { + bool forced; + flush_workqueue(isp->wq); + if (!READ_ONCE(isp->ipc_error)) + isp_ane_unset(isp); + /* * Stop the coprocessor before releasing any memory the firmware * uses, also when it did not acknowledge the stop request. */ - if (isp_stop_command_processor(isp)) + forced = isp_stop_command_processor(isp); + if (forced) { dev_warn(isp->dev, "firmware did not stop, stopping the coprocessor\n"); + isp_ane_firmware_failed(isp); + } /* 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); + return forced; } /* @@ -1428,37 +1447,75 @@ static void apple_isp_fw_idle_work(struct work_struct *work) mutex_unlock(&isp->video_lock); } +/* + * A firmware that did not stop was forced off (an ISP assert, 2026-11-21, or + * a stop command that timed out, "coproc not in WFI"), so it never sent the + * camera module its power-down: the sensor stayed powered and its green light + * on until the next camera use. Firmware that starts again (hw->fw_restart) is + * booted once more and stopped the normal way, which powers the module down. + * Only once: a second forced stop is left as it is. + */ +static void apple_isp_fw_recover_work(struct work_struct *work) +{ + struct apple_isp *isp = container_of(to_delayed_work(work), + struct apple_isp, fw_recover_work); + int err; + + mutex_lock(&isp->video_lock); + if (isp->fw_state == ISP_FW_OFF) { + isp->fw_recovering = true; + err = apple_isp_firmware_boot(isp); + if (!err) + apple_isp_firmware_halt(isp); + isp->fw_recovering = false; + if (err) + dev_warn(isp->dev, "crashed firmware: restart to power the camera down failed (%d); the camera light may stay on until the next camera start\n", + err); + else + dev_info(isp->dev, "crashed firmware: restarted and stopped it to power the camera down\n"); + } + mutex_unlock(&isp->video_lock); +} + void apple_isp_firmware_init(struct apple_isp *isp) { INIT_DELAYED_WORK(&isp->fw_idle_work, apple_isp_fw_idle_work); + INIT_DELAYED_WORK(&isp->fw_recover_work, apple_isp_fw_recover_work); } /* Stop idling firmware now, and wait for a stop that was due. */ void apple_isp_firmware_flush(struct apple_isp *isp) { - if (!isp->hw->fw_idle_ms) - return; - - cancel_delayed_work_sync(&isp->fw_idle_work); - mutex_lock(&isp->video_lock); - if (isp->fw_idle) - apple_isp_firmware_halt(isp); - mutex_unlock(&isp->video_lock); + if (isp->hw->fw_idle_ms) { + cancel_delayed_work_sync(&isp->fw_idle_work); + mutex_lock(&isp->video_lock); + if (isp->fw_idle) + apple_isp_firmware_halt(isp); + mutex_unlock(&isp->video_lock); + } + /* Last: the halt above may have scheduled it. */ + cancel_delayed_work_sync(&isp->fw_recover_work); } /* Stop the firmware; resident firmware without hw->fw_restart stays down. */ void apple_isp_firmware_halt(struct apple_isp *isp) { + bool forced; + /* Only a capture services the watchdog, but be sure. */ apple_isp_wdt_stop(isp); if (isp->fw_state != ISP_FW_RUNNING) return; - isp_firmware_shutdown(isp); + /* After an IPC error the stop is forced too: the firmware takes no stop. */ + forced = isp_firmware_shutdown(isp); pm_runtime_put_sync(isp->dev); isp->ipc_stale = NULL; isp->fw_idle = false; isp->fw_state = isp->hw->resident_fw && !isp->hw->fw_restart ? ISP_FW_DEAD : ISP_FW_OFF; + if (forced && isp->hw->fw_restart && !isp->fw_recovering) + mod_delayed_work(system_dfl_wq, &isp->fw_recover_work, + msecs_to_jiffies(500)); } diff --git a/drivers/media/platform/apple/isp/isp-iommu.c b/drivers/media/platform/apple/isp/isp-iommu.c index 12dbf6d47cccb..756eef467a45f 100644 --- a/drivers/media/platform/apple/isp/isp-iommu.c +++ b/drivers/media/platform/apple/isp/isp-iommu.c @@ -269,6 +269,10 @@ void isp_free_released_surfaces(struct apple_isp *isp) mutex_lock(&isp->gc_lock); list_splice_init(&isp->gc_released, &released); + /* A surface the ANE still maps stays until it lets go (isp-ane.c). */ + list_for_each_entry_safe(surf, tmp, &released, head) + if (refcount_read(&surf->refcount) > 1) + list_move_tail(&surf->head, &isp->gc_released); mutex_unlock(&isp->gc_lock); list_for_each_entry_safe(surf, tmp, &released, head) { diff --git a/drivers/media/platform/apple/isp/isp-ipc.c b/drivers/media/platform/apple/isp/isp-ipc.c index 922e990010487..d1e0c3338a081 100644 --- a/drivers/media/platform/apple/isp/isp-ipc.c +++ b/drivers/media/platform/apple/isp/isp-ipc.c @@ -379,9 +379,20 @@ int ipc_sm_handle(struct apple_isp *isp, struct isp_channel *chan) } surf->type = req->arg2; + /* + * macOS gives each surface an ID here. The firmware names + * surfaces to the ANE by it (isp-ane.c), so only firmware with + * an ANE endpoint gets one. + */ + if (isp->hw->peer_doorbell) { + mutex_lock(&isp->gc_lock); + surf->id = ++isp->surf_id ?: ++isp->surf_id; + mutex_unlock(&isp->gc_lock); + } + rsp->arg0 = surf->iova | ISP_IPC_FLAG_ACK; rsp->arg1 = 0x0; - rsp->arg2 = 0x0; /* macOS uses this to index surfaces */ + rsp->arg2 = surf->id; switch (surf->type) { case 0x4c4f47: /* "LOG" */ diff --git a/drivers/media/platform/apple/isp/isp-v4l2.c b/drivers/media/platform/apple/isp/isp-v4l2.c index dd463374061b8..37ad1882ae6eb 100644 --- a/drivers/media/platform/apple/isp/isp-v4l2.c +++ b/drivers/media/platform/apple/isp/isp-v4l2.c @@ -15,6 +15,7 @@ #include #include +#include "isp-ane.h" #include "isp-cam.h" #include "isp-cmd.h" #include "isp-iommu.h" @@ -559,6 +560,7 @@ static int apple_isp_start_streaming(struct apple_isp *isp) err = apple_isp_start_camera(isp); if (err) { dev_err(isp->dev, "failed to start camera: %d\n", err); + isp_ane_stream_failed(isp); return err; } @@ -583,6 +585,7 @@ static int apple_isp_start_streaming(struct apple_isp *isp) err = apple_isp_start_capture(isp); if (err) { dev_err(isp->dev, "failed to start capture: %d\n", err); + isp_ane_stream_failed(isp); goto stop_camera; } @@ -603,10 +606,12 @@ static int apple_isp_start_streaming(struct apple_isp *isp) set_bit(ISP_STATE_STREAMING, &isp->state); spin_unlock_irqrestore(&isp->buf_lock, flags); if (err) { + isp_ane_stream_failed(isp); apple_isp_stop_capture(isp); goto stop_camera; } + isp_ane_stream_started(isp); return 0; stop_camera: diff --git a/include/linux/soc/apple/ane.h b/include/linux/soc/apple/ane.h new file mode 100644 index 0000000000000..ef2605c655090 --- /dev/null +++ b/include/linux/soc/apple/ane.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef _LINUX_SOC_APPLE_ANE_H +#define _LINUX_SOC_APPLE_ANE_H + +#include +#include +#include +#include + +/* + * A coprocessor whose firmware runs networks on the Neural Engine itself + * (the ISP's MLVNR). Both firmwares exchange messages on two rings the peer + * owns: the engine writes tx and the peer reads it, the peer writes rx. Each + * side rings the other's doorbell. The engine's firmware names the peer's + * surfaces by the IDs the peer's firmware got for them; get_surface returns + * the pages, which stay valid until put_surface or detach. + */ +struct apple_ane_peer_ops { + int (*get_surface)(void *ctx, u32 id, struct sg_table **sgt, size_t *size); + void (*put_surface)(void *ctx, u32 id); +}; + +struct apple_ane_peer { + const struct apple_ane_peer_ops *ops; + void *ctx; + struct sg_table *tx; + struct sg_table *rx; + size_t ring_size; + /* The peer's doorbell (physical) and the value that rings it. */ + phys_addr_t doorbell; + u32 doorbell_value; + /* + * The peer's networks file (the ISP's AFPP), loaded into the engine's + * firmware before the endpoint: networks_size bytes at the start of + * networks, which covers whole 16 KiB pages. NULL for none. + */ + struct sg_table *networks; + size_t networks_size; +}; + +struct apple_ane; + +#if IS_REACHABLE(CONFIG_DRM_ACCEL_ANE) +/* + * The Neural Engine of the "apple,ane" phandle of dev, once its firmware + * serves requests: NULL without the phandle or with the engine disabled, + * -EAGAIN while it is not up yet. + */ +struct apple_ane *apple_ane_get(struct device *dev); +void apple_ane_put(struct apple_ane *ane); +/* + * Keep the engine up and give its firmware the peer's endpoint. Returns the + * engine's doorbell and value for the peer's firmware. + */ +int apple_ane_peer_attach(struct apple_ane *ane, const struct apple_ane_peer *peer, + phys_addr_t *doorbell, u32 *doorbell_value); +/* Unset the endpoint, unmap every peer surface and let the engine idle. */ +void apple_ane_peer_detach(struct apple_ane *ane); +#else +static inline struct apple_ane *apple_ane_get(struct device *dev) +{ + return NULL; +} + +static inline void apple_ane_put(struct apple_ane *ane) {} + +static inline int apple_ane_peer_attach(struct apple_ane *ane, + const struct apple_ane_peer *peer, + phys_addr_t *doorbell, u32 *doorbell_value) +{ + return -ENODEV; +} + +static inline void apple_ane_peer_detach(struct apple_ane *ane) {} +#endif +#endif