diff --git a/Documentation/devicetree/bindings/media/apple,isp.yaml b/Documentation/devicetree/bindings/media/apple,isp.yaml index 714c83aa1e392..b6af1f5eea8fd 100644 --- a/Documentation/devicetree/bindings/media/apple,isp.yaml +++ b/Documentation/devicetree/bindings/media/apple,isp.yaml @@ -92,6 +92,25 @@ 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,mlvnr-sensor-config: + $ref: /schemas/types.yaml#/definitions/uint32-array + items: + - description: Default sensor configuration index to replace + - description: Sensor configuration index used when ANE MLVNR is active + description: + Optional. Pair of sensor configuration indices . When the ANE + peer endpoint is active for MLVNR, presets using configuration + (such as a binned mode without MLVNR networks) switch to configuration + with their crop rectangles scaled to 's dimensions. Without + the ANE endpoint, the default presets on are used. + apple,camera-indicator: $ref: /schemas/types.yaml#/definitions/phandle description: diff --git a/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml b/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml index 0c8d5ca5a5af7..803b35976381d 100644 --- a/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml +++ b/Documentation/devicetree/bindings/media/apple,t8140-isp.yaml @@ -90,6 +90,25 @@ 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,mlvnr-sensor-config: + $ref: /schemas/types.yaml#/definitions/uint32-array + items: + - description: Default sensor configuration index to replace + - description: Sensor configuration index used when ANE MLVNR is active + description: + Optional. Pair of sensor configuration indices . When the ANE + peer endpoint is active for MLVNR, presets using configuration + (such as a binned mode without MLVNR networks) switch to configuration + with their crop rectangles scaled to 's dimensions. Without + the ANE endpoint, the default presets on are used. + 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/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi b/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi index 846d027a131ea..f26c29c5fbf72 100644 --- a/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi +++ b/arch/arm64/boot/dts/apple/t8142-j813-isp.dtsi @@ -204,6 +204,8 @@ apple,dart-vm-size = <0x0 0xe0000000>; apple,platform-id = <0x71>; apple,temporal-filter = <1>; + /* ADT isp function-ane_ep_control/data_param_set/get */ + apple,ane = <&ane>; }; }; @@ -218,15 +220,21 @@ &isp { apple,temporal-filter = <1>; apple,center-stage; + /* + * When the ANE peer endpoint comes up with ispane-j813.afpp, switch + * binned config 8 (1520x1440) presets to full-resolution config 7 + * (3040x2880) for MLVNR. Without the .afpp file, keep binned config 8. + */ + apple,mlvnr-sensor-config = <8 7>; /* * All presets scale from the binned sensor mode config 8 (3040x2880 * binned 2x2 to 1520x1440), with centred crops. Binning averages four * pixels: indoors it shows about half the colour noise of the - * full-resolution config 7, for some fine detail. In the other binned - * mode, config 10, the firmware's AWB keeps the setfile's default - * white point and the picture is magenta; in configs 7 and 8 it - * converges. + * full-resolution config 7 without MLVNR, for some fine detail. In + * the other binned mode, config 10, the firmware's AWB keeps the + * setfile's default white point and the picture is magenta; in + * configs 7 and 8 it converges. * * The driver derives the other call sizes from these crops only * without upscaling. The binned crops are too small for the portrait diff --git a/drivers/accel/ane/ane_fw_cold.c b/drivers/accel/ane/ane_fw_cold.c index abd8574683fce..9e3e16799f489 100644 --- a/drivers/accel/ane/ane_fw_cold.c +++ b/drivers/accel/ane/ane_fw_cold.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,15 @@ struct ane_fw_mapping { bool dma_mapped; }; +struct ane_fw_peer_map { + struct list_head node; + /* The peer's surface ID; 0 for its rings and networks, freed on detach. */ + u32 id; + u64 dva; + size_t size; + struct drm_mm_node range; +}; + struct ane_fw_backend { struct platform_device *pdev; const struct ane_fw_platform *soc; @@ -179,6 +189,15 @@ struct ane_fw_backend { /* A power-down outside a sleep: a stuck power word cannot wait for one. */ bool runtime_off; u64 recoveries, recover_failures, recover_limited; + /* The ISP's peer endpoint while attached; peer_maps/peer_stale under window_lock. */ + struct apple_ane_peer peer; + struct ane_h16_endpoint peer_ep; + struct list_head peer_maps; + struct list_head peer_stale; + struct work_struct peer_work; + bool peer_on; + u32 peer_mcache; + bool peer_mcache_ready; }; /* One ANE per SoC. The observer exists before platform driver registration: @@ -262,6 +281,8 @@ static irqreturn_t ane_fw_irq(int irq, void *data) WRITE_ONCE(b->irq_seq, b->irq_seq + 1); } wake_up(&b->irq_wq); + if (READ_ONCE(b->peer_on)) + queue_work(system_highpri_wq, &b->peer_work); return IRQ_HANDLED; } @@ -630,6 +651,7 @@ static int ane_fw_startup(struct ane_fw_backend *b); static int ane_fw_first_bringup(struct ane_fw_backend *b); static ssize_t ane_fw_stage_action(struct device *dev, const char *buf, size_t count); static int ane_fw_rvbar_ready(struct ane_fw_backend *b); +static void ane_fw_peer_drain_stale(struct ane_fw_backend *b); /* A power word goes off whole, and off means target, actual and BUSY clear. */ #define ANE_FW_PS_OFF 0x300 @@ -764,6 +786,7 @@ static int ane_fw_warm_boot(struct ane_fw_backend *b) ret = iommu_attach_device(b->domain, dev); if (ret) return ret; + ane_fw_peer_drain_stale(b); ret = ane_fw_dapf(b); if (ret) return ret; @@ -921,6 +944,7 @@ static int ane_fw_cold_cycle_boot(struct ane_fw_backend *b, bool recover) ret = iommu_attach_device(b->domain, dev); if (ret) return ret; + ane_fw_peer_drain_stale(b); ret = ane_fw_dapf(b); if (ret) return ret; @@ -2257,6 +2281,308 @@ static void ane_fw_cold_shared_note(void *ctx, const char *event, u64 dva, u64 s event, tag, size, dva); } +static int ane_fw_peer_map_pages(struct ane_fw_backend *b, u32 id, struct sg_table *sgt, + size_t size, u64 *dva) +{ + struct ane_fw_peer_map *m; + ssize_t mapped; + int ret; + + if (!size || !IS_ALIGNED(size, SZ_16K) || !b->domain) + return -EINVAL; + m = kzalloc_obj(*m); + if (!m) + return -ENOMEM; + m->id = id; + m->size = size; + mutex_lock(&b->window_lock); + ret = drm_mm_insert_node_in_range(&b->dva_window, &m->range, size, SZ_16K, 0, + ANE_FW_ALLOC_DVA, ANE_FW_ALLOC_DVA + ANE_FW_WINDOW, + DRM_MM_INSERT_LOW); + if (!ret) { + m->dva = m->range.start; + list_add_tail(&m->node, &b->peer_maps); + } + mutex_unlock(&b->window_lock); + if (ret) { + kfree(m); + return ret; + } + mapped = iommu_map_sgtable(b->domain, m->dva, sgt, + IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE); + if (mapped != (ssize_t)size) { + if (mapped > 0) + iommu_unmap(b->domain, m->dva, mapped); + mutex_lock(&b->window_lock); + list_del(&m->node); + drm_mm_remove_node(&m->range); + mutex_unlock(&b->window_lock); + kfree(m); + return mapped < 0 ? mapped : -EIO; + } + *dva = m->dva; + return 0; +} + +static void ane_fw_peer_unmap_one(struct ane_fw_backend *b, struct ane_fw_peer_map *m) +{ + if (b->domain) + iommu_unmap(b->domain, m->dva, m->size); + mutex_lock(&b->window_lock); + list_del(&m->node); + drm_mm_remove_node(&m->range); + mutex_unlock(&b->window_lock); + if (m->id && b->peer.ops->put_surface) + b->peer.ops->put_surface(b->peer.ctx, m->id); + kfree(m); +} + +static int ane_fw_peer_map(void *ctx, u32 id, u64 *dva, u64 *size) +{ + struct ane_fw_backend *b = ctx; + struct sg_table *sgt; + size_t bytes; + int ret; + + if (!b->peer_on || !id) + return -ENODEV; + ret = b->peer.ops->get_surface(b->peer.ctx, id, &sgt, &bytes); + if (ret) + return ret; + ret = ane_fw_peer_map_pages(b, id, sgt, bytes, dva); + if (ret) { + if (b->peer.ops->put_surface) + b->peer.ops->put_surface(b->peer.ctx, id); + return ret; + } + *size = bytes; + dev_dbg(&b->pdev->dev, "H17 peer surface %#x mapped at %#llx, %zx bytes\n", + id, *dva, bytes); + return 0; +} + +static int ane_fw_peer_unmap(void *ctx, u64 dva) +{ + struct ane_fw_backend *b = ctx; + struct ane_fw_peer_map *m, *found = NULL; + + mutex_lock(&b->window_lock); + list_for_each_entry(m, &b->peer_maps, node) { + if (m->id && m->dva == dva) { + found = m; + break; + } + } + mutex_unlock(&b->window_lock); + if (!found) + return -ENOENT; + dev_dbg(&b->pdev->dev, "H17 peer surface %#x unmapped\n", found->id); + ane_fw_peer_unmap_one(b, found); + return 0; +} + +/* + * Drop peer DVA mappings kept when a detach arrived while the engine and its + * DARTs were powered off. Caller guarantees the engine and DARTs are on. + */ +static void ane_fw_peer_drain_stale(struct ane_fw_backend *b) +{ + LIST_HEAD(stale); + struct ane_fw_peer_map *m, *next; + + mutex_lock(&b->window_lock); + list_splice_init(&b->peer_stale, &stale); + mutex_unlock(&b->window_lock); + list_for_each_entry_safe(m, next, &stale, node) { + if (b->domain) + iommu_unmap(b->domain, m->dva, m->size); + mutex_lock(&b->window_lock); + list_del(&m->node); + drm_mm_remove_node(&m->range); + mutex_unlock(&b->window_lock); + kfree(m); + } +} + +static void ane_fw_peer_unmap_all(struct ane_fw_backend *b, bool hw_on) +{ + LIST_HEAD(maps); + struct ane_fw_peer_map *m, *next; + + mutex_lock(&b->window_lock); + list_splice_init(&b->peer_maps, &maps); + mutex_unlock(&b->window_lock); + list_for_each_entry_safe(m, next, &maps, node) { + list_del(&m->node); + /* Always release the peer's surface reference immediately. */ + if (m->id && b->peer.ops->put_surface) { + b->peer.ops->put_surface(b->peer.ctx, m->id); + m->id = 0; + } + if (hw_on) { + if (b->domain) + iommu_unmap(b->domain, m->dva, m->size); + mutex_lock(&b->window_lock); + drm_mm_remove_node(&m->range); + mutex_unlock(&b->window_lock); + kfree(m); + } else { + mutex_lock(&b->window_lock); + list_add_tail(&m->node, &b->peer_stale); + mutex_unlock(&b->window_lock); + } + } +} + +static void ane_fw_peer_arm(struct ane_fw_backend *b) +{ + if (b->irq < 0 || b->soc->irq_role != ANE_FW_IRQ_CHANNELS) + return; + scoped_guard(spinlock_irqsave, &b->irq_lock) { + if (READ_ONCE(b->peer_on) && b->irq_wait && !b->irq_armed) { + b->irq_armed = true; + enable_irq(b->irq); + } + } +} + +static void ane_fw_peer_work(struct work_struct *work) +{ + struct ane_fw_backend *b = container_of(work, struct ane_fw_backend, peer_work); + int ret = 0; + + mutex_lock(b->session.lock); + if (b->peer_on) + ret = ane_h16_session_service(&b->session); + mutex_unlock(b->session.lock); + if (ret) + dev_err_ratelimited(&b->pdev->dev, "H17 peer service failed, error %d\n", ret); + ane_fw_peer_arm(b); +} + +struct device *ane_fw_cold_peer_find(struct device_node *np) +{ + struct ane_fw_backend *b = ane_backend; + + if (!b || !b->pdev || b->pdev->dev.of_node != np || !b->session.lock) + return NULL; + if (READ_ONCE(b->stage) == ANE_STAGE_QUARANTINED) + return NULL; + get_device(&b->pdev->dev); + return &b->pdev->dev; +} + +int ane_fw_cold_peer_attach(struct device *dev, const struct apple_ane_peer *peer, + phys_addr_t *doorbell, u32 *doorbell_value) +{ + struct ane_fw_backend *b = ane_backend; + const struct ane_fw_platform *soc; + struct resource *res; + u64 tx, rx, afpp = 0; + u32 mcache = 0; + int ret; + + if (!b || !b->pdev || &b->pdev->dev != dev) + return -ENODEV; + soc = b->soc; + 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(b->pdev, IORESOURCE_MEM, "engine"); + if (!soc->peer_doorbell || !res) + return -EOPNOTSUPP; + /* Keep the engine from runtime-suspending while the peer is attached. */ + ret = pm_runtime_resume_and_get(dev); + if (ret < 0) + return ret; + ret = ane_h16_session_enter(&b->session, false); + if (ret) + goto put; + if (b->peer_on) { + ret = -EBUSY; + goto unlock; + } + ane_fw_peer_drain_stale(b); + mcache = b->peer_mcache; + b->peer = *peer; + ret = ane_fw_peer_map_pages(b, 0, peer->tx, peer->ring_size, &tx); + if (!ret) + ret = ane_fw_peer_map_pages(b, 0, peer->rx, peer->ring_size, &rx); + if (!ret && peer->networks) + ret = ane_fw_peer_map_pages(b, 0, peer->networks, + ALIGN(peer->networks_size, SZ_16K), &afpp); + if (ret) + goto unmap; + b->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, + .mcache_ready = b->peer_mcache_ready, + }; + /* Map requests may follow SET2 at once. */ + WRITE_ONCE(b->peer_on, true); + ret = ane_h16_session_peer_set(&b->session, &b->peer_ep, &mcache); + if (ret) { + WRITE_ONCE(b->peer_on, false); + goto unmap; + } + mutex_unlock(b->session.lock); + ane_fw_peer_arm(b); + *doorbell = res->start + soc->peer_doorbell; + *doorbell_value = soc->peer_doorbell_value; + dev_info(dev, "H17 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_fw_peer_unmap_all(b, true); +unlock: + mutex_unlock(b->session.lock); +put: + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); + dev_err(dev, "H17 peer endpoint refused, error %d\n", ret); + return ret; +} + +void ane_fw_cold_peer_detach(struct device *dev) +{ + struct ane_fw_backend *b = ane_backend; + bool hw_on; + int ret; + + if (!b || !b->pdev || &b->pdev->dev != dev) + return; + mutex_lock(b->session.lock); + if (!b->peer_on) { + mutex_unlock(b->session.lock); + return; + } + /* + * Under the session lock, sleep/park transitions set asleep before + * cutting power; if the engine is already off, defer DART unmaps to + * the next power-up/attach so we never touch powered-off ANE DARTs. + */ + hw_on = READ_ONCE(b->powered) && !b->session.asleep && !b->session.parked; + ret = ane_h16_session_peer_unset(&b->session, &b->peer_ep); + WRITE_ONCE(b->peer_on, false); + ane_fw_peer_unmap_all(b, hw_on); + mutex_unlock(b->session.lock); + cancel_work_sync(&b->peer_work); + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); + if (ret) + dev_err(dev, "H17 peer endpoint unset failed, error %d\n", ret); + else + dev_info(dev, "H17 peer endpoint unset%s\n", hw_on ? "" : " (DART unmap deferred)"); +} + static const struct ane_fw_transport_ops ane_fw_ops = { .load64 = ane_fw_load64, .store64 = ane_fw_store64, .read_barrier = ane_fw_rmb, .write_barrier = ane_fw_wmb, @@ -2264,6 +2590,8 @@ static const struct ane_fw_transport_ops ane_fw_ops = { .now_ns = ane_fw_now, .relax = ane_fw_relax, .pause = ane_fw_pause, .alloc = ane_fw_alloc, .refused = ane_fw_refused, .shared_note = ane_fw_cold_shared_note, + .peer_map = ane_fw_peer_map, + .peer_unmap = ane_fw_peer_unmap, }; /* @@ -2724,6 +3052,59 @@ static int ane_fw_pmu_base(struct ane_fw_backend *b) return ret; } +static int ane_fw_reply_check(const u8 *reply, u16 id) +{ + if (get_unaligned_le32(reply) || get_unaligned_le16(reply + 4) != id) + return -EPROTO; + return get_unaligned_le16(reply + 6) ? -EREMOTEIO : 0; +} + +/* + * Configure the peer mcache spill DSID once per firmware boot, before any + * user client creates a process: sending MCACHE_DSID_SET after a client + * process exists leaves it unanswered and quarantines the session. + */ +static int ane_fw_peer_mcache_setup(struct ane_fw_backend *b) +{ + u8 command[ANE_H16_DSID_SET_SIZE]; + u16 id; + int ret; + + b->peer_mcache = 0; + b->peer_mcache_ready = false; + if (!b->soc->peer_spill_dsid) + return 0; + + ret = ane_h16_mcache_encode(command, ANE_H16_MCACHE_SIZE); + if (ret) + return ret; + id = get_unaligned_le16(command + 4); + ret = ane_fw_command(b, ANE_FW_IO, command, ANE_H16_MCACHE_SIZE, + ktime_get_ns() + ANE_FW_TIMEOUT_NS); + if (!ret) + ret = ane_fw_reply_check(command, id); + if (ret) + return ret; + b->peer_mcache = get_unaligned_le32(command + 8); + + ret = ane_h16_dsid_set_encode(command, ANE_H16_DSID_SET_SIZE, 0, + b->soc->peer_spill_dsid); + if (ret) + return ret; + id = get_unaligned_le16(command + 4); + ret = ane_fw_command(b, ANE_FW_IO, command, ANE_H16_DSID_SET_SIZE, + ktime_get_ns() + ANE_FW_TIMEOUT_NS); + if (!ret) + ret = ane_fw_reply_check(command, id); + if (ret) + return ret; + + b->peer_mcache_ready = true; + dev_info(&b->pdev->dev, "H17 ANE: mcache size %#x, spill DSID %#x\n", + b->peer_mcache, b->soc->peer_spill_dsid); + return 0; +} + static int ane_fw_startup(struct ane_fw_backend *b) { static const struct { u16 id, size; } commands[] = { @@ -2762,7 +3143,7 @@ static int ane_fw_startup(struct ane_fw_backend *b) ane_wr32(b, 1, b->engine + b->soc->scratch + 0x38); ane_wr32(b, 0, b->engine + b->soc->scratch + 0x40); } - return 0; + return ane_fw_peer_mcache_setup(b); } /* Private program mappings come from the same window as SHAREDMALLOC. */ @@ -3310,6 +3691,9 @@ int ane_fw_staged_init(struct platform_device *pdev, const struct ane_fw_platfor mutex_init(&b->window_lock); mutex_init(&b->lock); INIT_LIST_HEAD(&b->mappings); + INIT_LIST_HEAD(&b->peer_maps); + INIT_LIST_HEAD(&b->peer_stale); + INIT_WORK(&b->peer_work, ane_fw_peer_work); if (of_property_count_strings(np, "reg-names") != soc->reg_count || of_irq_count(np) != soc->interrupt_count || of_count_phandle_with_args(np, "power-domains", "#power-domain-cells") != 1) diff --git a/drivers/accel/ane/ane_fw_cold.h b/drivers/accel/ane/ane_fw_cold.h index d4000bb708f14..7b0b77b60ba76 100644 --- a/drivers/accel/ane/ane_fw_cold.h +++ b/drivers/accel/ane/ane_fw_cold.h @@ -361,4 +361,10 @@ int ane_fw_staged_runtime_resume(struct device *dev); void ane_fw_staged_start(void); bool ane_fw_staged_exit(void); +struct apple_ane_peer; +struct device *ane_fw_cold_peer_find(struct device_node *np); +int ane_fw_cold_peer_attach(struct device *dev, const struct apple_ane_peer *peer, + phys_addr_t *doorbell, u32 *doorbell_value); +void ane_fw_cold_peer_detach(struct device *dev); + #endif /* __ANE_FW_COLD_H__ */ diff --git a/drivers/accel/ane/ane_fw_platform.c b/drivers/accel/ane/ane_fw_platform.c index dfc53778d13f9..f215205749bd0 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, @@ -592,6 +597,7 @@ static const struct ane_fw_bridge_op ane_t8142_bridge_ops[] = { .hwx_subtype = 9, \ .task_generation = 19, \ .interrupt_count = 2, \ + .irq_role = ANE_FW_IRQ_CHANNELS, \ .owner = ANE_FW_OWNER_EXCLAVE, \ .reg_names = ane_h17_reg_names, \ .reg_sizes = ane_h17_reg_sizes, \ @@ -600,6 +606,10 @@ static const struct ane_fw_bridge_op ane_t8142_bridge_ops[] = { .mailbox = 0x1608000, \ .scratch = 0x1880028, \ .doorbell = 0x1884000, \ + /* ADT dart-isp dapf-instance-0: 0x421660000..0x421660003. */ \ + .peer_doorbell = 0x1660000, \ + .peer_doorbell_value = 0x1, \ + .peer_spill_dsid = 0xe, \ .irq_status = 0x188c000, \ .irq_ack = 0x1890000, \ .stream_mask = BIT(0), \ 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..f3c7dc5a0c941 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 @@ -28,6 +29,7 @@ #include #include +#include "ane_fw_cold.h" #include "ane_fw_sleep.h" #include "ane_fw_platform.h" #include "ane_h16_accel.h" @@ -54,8 +56,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 +184,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 +2031,325 @@ 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 *cold_dev; + 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 device *cold_dev = NULL, *target; + 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; + } + } + } + if (!found) + cold_dev = ane_fw_cold_peer_find(np); + of_node_put(np); + if (!found && !cold_dev) + return ERR_PTR(-EAGAIN); + target = found ? found->dev : cold_dev; + ane = kzalloc_obj(*ane); + if (!ane) { + put_device(target); + return ERR_PTR(-ENOMEM); + } + ane->h = found; + ane->cold_dev = cold_dev; + /* System sleep: the peer suspends first and resumes last. */ + ane->link = device_link_add(dev, target, DL_FLAG_STATELESS); + if (!ane->link) + dev_warn(dev, "no device link to %s\n", dev_name(target)); + 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 ? ane->h->dev : ane->cold_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; + const struct ane_fw_platform *soc; + struct resource *res; + u64 tx, rx, afpp = 0; + u32 mcache = 0; + int ret; + + if (ane->cold_dev) + return ane_fw_cold_peer_attach(ane->cold_dev, peer, doorbell, doorbell_value); + h = ane->h; + soc = h->boot.soc; + + 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; + int ret; + + if (ane->cold_dev) { + ane_fw_cold_peer_detach(ane->cold_dev); + return; + } + h = ane->h; + + 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 +2374,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 +2472,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 +3041,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 +3078,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 +3116,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 +3183,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..73ed213e081b5 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,190 @@ 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; + } + if (!e->mcache_ready) { + 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); + } else { + ret = 0; + } + /* 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 +2859,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..c6d0f12c1db7b 100644 --- a/drivers/accel/ane/ane_h16_program.h +++ b/drivers/accel/ane/ane_h16_program.h @@ -31,6 +31,41 @@ #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; + /* + * MCACHE_SIZE_GET and MCACHE_DSID_SET were already sent at firmware + * startup (before any user process was created): skip them at attach. + */ + bool mcache_ready; +}; #define ANE_H16_REPORT_MAX 0x100 /* * One program's LOAD and CREATE, which stay in the command arena while the @@ -150,6 +185,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 +451,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 +531,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..d7ea36ab99479 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,260 @@ 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 mcache_ready set (MCACHE_SIZE_GET and MCACHE_DSID_SET already sent at + * firmware startup before any client process existed), peer_set skips them and + * sends only POWER_GATE, LOAD_AFPP and IPC_ENDPOINT_SET2. + */ +static void program_peer_mcache_ready_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 = 0x4a1668000, .doorbell_value = 0x10, + .afpp_dva = FW_MAP_DVA + SZ_32K, .afpp_size = 0xc5a240, + .spill_dsid = 0x10, + .mcache_ready = true, + }; + struct prog_fw *f = fw_create(test); + u32 mcache = 0x300000; + + 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, 3U); + KUNIT_EXPECT_EQ(test, f->ids[0], (u16)ANE_H16_POWER_GATE); + KUNIT_EXPECT_EQ(test, f->ids[1], (u16)ANE_H16_LOAD_AFPP); + KUNIT_EXPECT_EQ(test, f->ids[2], (u16)ANE_H16_IPC_ENDPOINT_SET2); + KUNIT_EXPECT_EQ(test, mcache, 0x300000U); + KUNIT_EXPECT_TRUE(test, f->s.peer); + KUNIT_ASSERT_EQ(test, ane_h16_session_peer_unset(&f->s, &e), 0); + KUNIT_EXPECT_FALSE(test, f->s.peer); + KUNIT_EXPECT_FALSE(test, f->s.quarantined); +} + +/* + * 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 +5212,13 @@ 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_mcache_ready_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..a280bacaf1746 --- /dev/null +++ b/drivers/media/platform/apple/isp/isp-ane-test.c @@ -0,0 +1,114 @@ +// 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 = true; + KUNIT_EXPECT_TRUE(test, isp_ane_mlvnr_usable(isp)); + isp_ane_stream_failed(isp); + KUNIT_EXPECT_TRUE(test, isp->ane_failed); + /* Firmware still running keeps the endpoint set: a retry gets no MLVNR. */ + KUNIT_EXPECT_FALSE(test, isp_ane_mlvnr_usable(isp)); + isp->ane_failed = false; + 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..a43c6e35cea77 --- /dev/null +++ b/drivers/media/platform/apple/isp/isp-ane.c @@ -0,0 +1,309 @@ +// 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; + } + WRITE_ONCE(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; +} + +/* + * MLVNR runs only on a working endpoint. A failed one may still be set in + * firmware that keeps running between streams: no MLVNR on it. + */ +bool isp_ane_mlvnr_usable(struct apple_isp *isp) +{ + return isp->ane_set && !isp->ane_failed; +} + +/* + * 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); + WRITE_ONCE(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..60c52e5820aa0 --- /dev/null +++ b/drivers/media/platform/apple/isp/isp-ane.h @@ -0,0 +1,16 @@ +/* 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); +bool isp_ane_mlvnr_usable(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-test.c b/drivers/media/platform/apple/isp/isp-cam-test.c index d1d812a7a3026..89dc7db2f9f6c 100644 --- a/drivers/media/platform/apple/isp/isp-cam-test.c +++ b/drivers/media/platform/apple/isp/isp-cam-test.c @@ -1115,6 +1115,113 @@ static void isp_test_failed_fw_stop_lights_off(struct kunit *test) KUNIT_EXPECT_EQ(test, trace.set_off_at, 1); } +struct isp_test_mlvnr_cap { + unsigned int mlvnr_calls; + unsigned int sharpness_calls; + struct cmd_apple_ch_mlvnr_mode_config_set last_mlvnr; + struct cmd_ch_sharpness_set last_sharpness; +}; + +static int isp_test_mlvnr_cmd_stub(struct apple_isp *isp, void *args, + u32 insize, u32 outsize, int timeout) +{ + struct kunit *test = kunit_get_current_test(); + struct isp_test_mlvnr_cap *cap = test->priv; + u64 opcode; + + KUNIT_ASSERT_GE(test, insize, sizeof(opcode)); + memcpy(&opcode, args, sizeof(opcode)); + + if (opcode == ((u64)CISP_CMD_APPLE_CH_MLVNR_MODE_CONFIG_SET << 32)) { + KUNIT_ASSERT_EQ(test, insize, sizeof(cap->last_mlvnr)); + KUNIT_ASSERT_EQ(test, outsize, sizeof(cap->last_mlvnr)); + memcpy(&cap->last_mlvnr, args, sizeof(cap->last_mlvnr)); + cap->mlvnr_calls++; + } else if (opcode == ((u64)CISP_CMD_CH_SHARPNESS_SET << 32)) { + KUNIT_ASSERT_EQ(test, insize, sizeof(cap->last_sharpness)); + KUNIT_ASSERT_EQ(test, outsize, 0); + memcpy(&cap->last_sharpness, args, sizeof(cap->last_sharpness)); + cap->sharpness_calls++; + } else { + KUNIT_FAIL(test, "unexpected ISP opcode 0x%llx", opcode); + } + return 0; +} + +static void isp_test_mlvnr_config_switch(struct kunit *test) +{ + struct apple_isp *isp = isp_test_capture_alloc(test); + struct isp_format *fmt = isp_get_format(isp, 0); + struct isp_preset binned = { + .index = 8, + .input_dim = { 1520, 1440 }, + .output_dim = { 1920, 1080 }, + .crop_offset = { 0, 292 }, + .crop_size = { 1520, 856 }, + }; + struct isp_preset active; + struct isp_test_mlvnr_cap cap = {}; + + fmt->version = 0x958; + fmt->preset = &binned; + isp->frame_rate = 30; + test->priv = ∩ + kunit_activate_static_stub(test, isp_cmd_send, isp_test_mlvnr_cmd_stub); + + /* Without ANE endpoint, keep default binned config and skip MLVNR. */ + isp->has_mlvnr_config = true; + isp->mlvnr_config_from = 8; + isp->mlvnr_config_to = 7; + isp->mlvnr_input_dim = (struct coord){ 3040, 2880 }; + isp->ane_set = false; + KUNIT_EXPECT_FALSE(test, isp_resolve_mlvnr_preset(isp, &binned, &active)); + KUNIT_EXPECT_EQ(test, active.index, 8); + KUNIT_EXPECT_EQ(test, active.crop_offset.y, 292); + KUNIT_EXPECT_EQ(test, active.crop_size.x, 1520); + KUNIT_EXPECT_EQ(test, isp_ch_configure_mlvnr(isp, 0, false), 0); + KUNIT_EXPECT_EQ(test, cap.mlvnr_calls, 0); + KUNIT_EXPECT_EQ(test, cap.sharpness_calls, 0); + + /* With ANE endpoint up, switch config 8 -> 7 and scale crop 2x. */ + isp->ane_set = true; + KUNIT_EXPECT_TRUE(test, isp_resolve_mlvnr_preset(isp, &binned, &active)); + KUNIT_EXPECT_EQ(test, active.index, 7); + KUNIT_EXPECT_EQ(test, active.input_dim.x, 3040); + KUNIT_EXPECT_EQ(test, active.input_dim.y, 2880); + KUNIT_EXPECT_EQ(test, active.crop_offset.x, 0); + KUNIT_EXPECT_EQ(test, active.crop_offset.y, 584); + KUNIT_EXPECT_EQ(test, active.crop_size.x, 3040); + KUNIT_EXPECT_EQ(test, active.crop_size.y, 1712); + + KUNIT_EXPECT_EQ(test, isp_ch_configure_mlvnr(isp, 0, true), 0); + KUNIT_EXPECT_EQ(test, cap.mlvnr_calls, 1); + KUNIT_EXPECT_EQ(test, cap.last_mlvnr.mode, 2); + KUNIT_EXPECT_EQ(test, cap.last_mlvnr.enter_gain, 0); + KUNIT_EXPECT_EQ(test, cap.last_mlvnr.exit_gain, 0); + KUNIT_EXPECT_EQ(test, cap.sharpness_calls, 1); + KUNIT_EXPECT_EQ(test, cap.last_sharpness.strength, ISP_MLVNR_SHARPNESS); + + /* After an endpoint failure, keep binned config and restore sharpness. */ + isp->ane_failed = true; + KUNIT_EXPECT_FALSE(test, isp_resolve_mlvnr_preset(isp, &binned, &active)); + KUNIT_EXPECT_EQ(test, active.index, 8); + KUNIT_EXPECT_EQ(test, isp_ch_configure_mlvnr(isp, 0, false), 0); + KUNIT_EXPECT_EQ(test, cap.mlvnr_calls, 1); + KUNIT_EXPECT_EQ(test, cap.sharpness_calls, 2); + KUNIT_EXPECT_EQ(test, cap.last_sharpness.strength, ISP_DEFAULT_SHARPNESS); + isp->ane_failed = false; + + /* Without valid target config geometry, keep binned config and disable MLVNR. */ + isp->mlvnr_input_dim = (struct coord){}; + KUNIT_EXPECT_FALSE(test, isp_resolve_mlvnr_preset(isp, &binned, &active)); + KUNIT_EXPECT_EQ(test, active.index, 8); + KUNIT_EXPECT_EQ(test, isp_ch_configure_mlvnr(isp, 0, false), 0); + KUNIT_EXPECT_EQ(test, cap.mlvnr_calls, 2); + KUNIT_EXPECT_EQ(test, cap.last_mlvnr.mode, 0); + KUNIT_EXPECT_EQ(test, cap.sharpness_calls, 3); + KUNIT_EXPECT_EQ(test, cap.last_sharpness.strength, ISP_DEFAULT_SHARPNESS); +} + static void isp_test_setfile_platform_select(struct kunit *test) { static const struct apple_isp_hw hw_legacy = { @@ -1171,6 +1278,7 @@ static struct kunit_case isp_cam_test_cases[] = { KUNIT_CASE(isp_test_preview_command_failures), KUNIT_CASE(isp_test_temporal_filter_band0), KUNIT_CASE(isp_test_temporal_filter_off), + KUNIT_CASE(isp_test_mlvnr_config_switch), KUNIT_CASE(isp_test_power_line_frequency), KUNIT_CASE(isp_test_snf_radial), KUNIT_CASE(isp_test_setfile_platform_select), diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index 4ae4f2dea3210..a675255abea63 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -3,11 +3,13 @@ #include #include +#include #include #include #include #include +#include "isp-ane.h" #include "isp-cam.h" #include "isp-cmd.h" #include "isp-fw.h" @@ -379,6 +381,51 @@ static int isp_ch_cache_sensor_configs(struct apple_isp *isp, u32 ch, u32 count) return err; } +static int isp_ch_cache_mlvnr_config(struct apple_isp *isp, u32 ch, u32 count) +{ + struct cmd_ch_camera_config_h17 args; + struct cmd_ch_camera_config *config = &args.config; + int err; + + if (!isp->has_mlvnr_config) + return 0; + + isp->mlvnr_input_dim = (struct coord){}; + if (isp->hw->fw_abi != ISP_FW_ABI_H17 || + isp->mlvnr_config_to >= count) { + dev_warn(isp->dev, + "MLVNR sensor config %u unavailable (firmware has %u)\n", + isp->mlvnr_config_to, count); + return 0; + } + + err = isp_cmd_ch_camera_config_get(isp, ch, isp->mlvnr_config_to, &args); + if (err) { + dev_warn(isp->dev, "MLVNR sensor config %u query failed: %d\n", + isp->mlvnr_config_to, err); + return READ_ONCE(isp->ipc_error); + } + + if (config->opcode != (u64)CISP_CMD_CH_CAMERA_CONFIG_GET << 32 || + config->chan != ch || config->preset != isp->mlvnr_config_to || + !config->out_width || !config->out_height || + config->out_width > 16384 || config->out_height > 16384 || + (config->out_width & 1) || (config->out_height & 1)) { + dev_warn(isp->dev, + "MLVNR sensor config %u has invalid geometry %ux%u\n", + isp->mlvnr_config_to, config->out_width, + config->out_height); + return 0; + } + + isp->mlvnr_input_dim = (struct coord){ config->out_width, + config->out_height }; + dev_info(isp->dev, "MLVNR sensor config %u -> %u (%ux%u)\n", + isp->mlvnr_config_from, isp->mlvnr_config_to, + isp->mlvnr_input_dim.x, isp->mlvnr_input_dim.y); + return 0; +} + /* Common call sizes, offered only when a known processed crop contains them. */ static const struct coord isp_call_sizes[] = { { 640, 480 }, { 1280, 720 }, { 1920, 1080 }, { 1080, 1920 }, @@ -571,6 +618,8 @@ static int isp_ch_cache_sensor_info(struct apple_isp *isp, u32 ch) err = isp_check_presets(isp, args->num_presets); if (!err) err = isp_ch_cache_sensor_configs(isp, ch, args->num_presets); + if (!err) + err = isp_ch_cache_mlvnr_config(isp, ch, args->num_presets); if (!err) err = isp_add_call_sizes(isp); if (!err) @@ -986,6 +1035,84 @@ static int isp_ch_configure_power_line(struct apple_isp *isp, u32 ch) hz << 16); } +/* + * When DT provides apple,mlvnr-sensor-config = , presets on @from + * (for example a binned mode without MLVNR networks) switch to @to with + * their crop scaled to @to's frame dimensions while the ANE endpoint is up. + * Returns true when MLVNR may be enabled on the resulting configuration. + */ +static bool isp_resolve_mlvnr_preset(struct apple_isp *isp, + const struct isp_preset *src, + struct isp_preset *out) +{ + *out = *src; + + if (!isp_ane_mlvnr_usable(isp)) + return false; + + if (!isp->has_mlvnr_config || src->index != isp->mlvnr_config_from) + return true; + + if (!isp->mlvnr_input_dim.x || !isp->mlvnr_input_dim.y || + !src->input_dim.x || !src->input_dim.y) + return false; + + out->index = isp->mlvnr_config_to; + out->input_dim = isp->mlvnr_input_dim; + out->crop_offset.x = round_down((u64)src->crop_offset.x * + isp->mlvnr_input_dim.x / + src->input_dim.x, 2); + out->crop_offset.y = round_down((u64)src->crop_offset.y * + isp->mlvnr_input_dim.y / + src->input_dim.y, 2); + out->crop_size.x = round_down((u64)src->crop_size.x * + isp->mlvnr_input_dim.x / + src->input_dim.x, 2); + out->crop_size.y = round_down((u64)src->crop_size.y * + isp->mlvnr_input_dim.y / + src->input_dim.y, 2); + if (!out->crop_size.x || !out->crop_size.y) { + *out = *src; + return false; + } + return true; +} + +/* + * With a working ANE endpoint, MLVNR for the whole stream: automatic mode + * (2) from a total gain of 0 and at the stream's frame rate or below, so it + * never leaves and has nothing to fade out. On the J616s in room light this + * is as clean as with higher gain thresholds, which only take MLVNR away. + * + * With the noise gone, sharpen more than the firmware's 0x80: on the J616s + * 0xc0 gives clearly crisper edges for little noise, 0xff turns gritty. + * Without MLVNR the sharpening is mostly grain, so it stays as it is. + */ +#define ISP_MLVNR_SHARPNESS 0xc0 +#define ISP_DEFAULT_SHARPNESS 0x80 + +static int isp_ch_configure_mlvnr(struct apple_isp *isp, u32 ch, bool enable) +{ + int err; + + /* Firmware that kept running after the endpoint failed still has 0xc0. */ + if (!isp_ane_mlvnr_usable(isp)) + return isp->ane_failed ? + isp_cmd_ch_sharpness_set(isp, ch, ISP_DEFAULT_SHARPNESS) : 0; + if (!enable) { + err = isp_cmd_apple_ch_mlvnr_mode_config_set(isp, ch, 0, + 0, 0, 0, 0); + if (err) + return err; + return isp_cmd_ch_sharpness_set(isp, ch, ISP_DEFAULT_SHARPNESS); + } + err = isp_cmd_apple_ch_mlvnr_mode_config_set(isp, ch, 2, 0, 0, + isp->frame_rate, 0); + if (err) + return err; + return isp_cmd_ch_sharpness_set(isp, ch, ISP_MLVNR_SHARPNESS); +} + static int isp_ch_configure_temporal_filter(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); @@ -1071,7 +1198,9 @@ static int isp_ch_configure_pools(struct apple_isp *isp, u32 ch) static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); + struct isp_preset active; bool h17 = isp->hw->fw_abi == ISP_FW_ABI_H17; + bool mlvnr_ok; int err; err = isp_cmd_flicker_sensor_set(isp, 0); @@ -1102,11 +1231,16 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) return err; select_config: + mlvnr_ok = !isp_is_p010(isp) && + isp_resolve_mlvnr_preset(isp, fmt->preset, &active); + if (isp_is_p010(isp)) + active = *fmt->preset; + err = isp_cmd_ch_sbs_enable(isp, ch, 1); if (err) return err; - err = isp_cmd_ch_camera_config_select(isp, ch, fmt->preset->index); + err = isp_cmd_ch_camera_config_select(isp, ch, active.index); if (err) return err; @@ -1119,15 +1253,15 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) if (err) return err; - err = isp_cmd_ch_crop_set(isp, ch, fmt->preset->crop_offset.x, - fmt->preset->crop_offset.y, - fmt->preset->crop_size.x, - fmt->preset->crop_size.y); + err = isp_cmd_ch_crop_set(isp, ch, active.crop_offset.x, + active.crop_offset.y, + active.crop_size.x, + active.crop_size.y); if (err) return err; - err = isp_cmd_ch_output_config_set(isp, ch, fmt->preset->output_dim.x, - fmt->preset->output_dim.y, + err = isp_cmd_ch_output_config_set(isp, ch, active.output_dim.x, + active.output_dim.y, fmt->strides, CISP_COLORSPACE_REC709, isp_is_p010(isp) ? CISP_OUTPUT_FORMAT_P010 : CISP_OUTPUT_FORMAT_YUV_2PLANE); @@ -1203,6 +1337,10 @@ static int isp_ch_configure_capture(struct apple_isp *isp, u32 ch) if (err) return err; + err = isp_ch_configure_mlvnr(isp, ch, mlvnr_ok); + 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..ee169f2cae4b6 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 = { @@ -681,6 +717,16 @@ int isp_cmd_apple_ch_temporal_filter_enable(struct apple_isp *isp, u32 chan) return CISP_SEND_IN(isp, args); } +int isp_cmd_ch_sharpness_set(struct apple_isp *isp, u32 chan, u32 strength) +{ + struct cmd_ch_sharpness_set args = { + .opcode = CISP_OPCODE(CISP_CMD_CH_SHARPNESS_SET), + .chan = chan, + .strength = strength, + }; + return CISP_SEND_IN(isp, args); +} + int isp_cmd_apple_ch_temporal_filter_disable(struct apple_isp *isp, u32 chan) { struct cmd_apple_ch_temporal_filter_disable args = { diff --git a/drivers/media/platform/apple/isp/isp-cmd.h b/drivers/media/platform/apple/isp/isp-cmd.h index b7488d08644b6..cb10d6cd67040 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.h +++ b/drivers/media/platform/apple/isp/isp-cmd.h @@ -66,6 +66,7 @@ #define CISP_CMD_CH_CROP_SCL1_GET 0x080b #define CISP_CMD_CH_CROP_SCL1_SET 0x080c #define CISP_CMD_CH_SCALER_CROP_SCL1_SET 0x080d +#define CISP_CMD_CH_SHARPNESS_SET 0x0a09 #define CISP_CMD_CH_ALS_ENABLE 0x0a1c #define CISP_CMD_CH_ALS_DISABLE 0x0a1d #define CISP_CMD_CH_CNR_START 0x0a2f @@ -99,6 +100,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 +259,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 +313,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 +687,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; @@ -716,13 +745,30 @@ struct cmd_apple_ch_temporal_filter_enable { } __packed; static_assert(sizeof(struct cmd_apple_ch_temporal_filter_enable) == 0xc); +/* + * Edge sharpening of the channel's output, 0x80 unless set. The word before + * the strength reads back as 0. + */ +struct cmd_ch_sharpness_set { + u64 opcode; + u32 chan; + u32 unk_0c; + u32 strength; +} __packed; +static_assert(sizeof(struct cmd_ch_sharpness_set) == 0x14); + struct cmd_apple_ch_temporal_filter_disable { u64 opcode; u32 chan; } __packed; static_assert(sizeof(struct cmd_apple_ch_temporal_filter_disable) == 0xc); +int isp_cmd_ch_sharpness_set(struct apple_isp *isp, u32 chan, u32 strength); 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..308a12d0edf04 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" @@ -912,14 +913,24 @@ static void apple_isp_resume_work(struct work_struct *work) * Idle firmware that starts again from its own memory is stopped before a * system sleep begins, as macOS's camera service powers an idle ISP off * before the kernel sleeps: the sleep then finds it off, as after a stream. + * A stream holding an ANE peer endpoint is also stopped here (with higher + * notifier priority than the ANE's PM notifier) so the ISP releases the + * endpoint before the ANE powers off at PM_SUSPEND_PREPARE. */ static int apple_isp_pm_notify(struct notifier_block *nb, unsigned long event, void *unused) { struct apple_isp *isp = container_of(nb, struct apple_isp, pm_nb); - if (event == PM_SUSPEND_PREPARE || event == PM_HIBERNATION_PREPARE) + if (event == PM_SUSPEND_PREPARE || event == PM_HIBERNATION_PREPARE) { + if (READ_ONCE(isp->ane_attached)) + apple_isp_video_suspend(isp); apple_isp_firmware_flush(isp); + } else if (event == PM_POST_SUSPEND || event == PM_POST_HIBERNATION) { + if (test_bit(ISP_STATE_SLEEPING, &isp->state) && + !READ_ONCE(isp->fw_sleep_stopped)) + queue_work(system_freezable_wq, &isp->resume_work); + } return NOTIFY_DONE; } @@ -985,6 +996,23 @@ static int apple_isp_probe(struct platform_device *pdev) isp->center_stage = of_property_read_bool(dev->of_node, "apple,center-stage"); + { + u32 mlvnr_cfg[2]; + + err = of_property_read_u32_array(dev->of_node, + "apple,mlvnr-sensor-config", + mlvnr_cfg, 2); + if (!err) { + isp->has_mlvnr_config = true; + isp->mlvnr_config_from = mlvnr_cfg[0]; + isp->mlvnr_config_to = mlvnr_cfg[1]; + } else if (err != -EINVAL) { + dev_warn(dev, + "ignoring invalid 'apple,mlvnr-sensor-config': %d\n", + err); + } + } + err = apple_isp_init_presets(isp); if (err) { dev_err(dev, "failed to initialize presets\n"); @@ -1133,6 +1161,7 @@ static int apple_isp_probe(struct platform_device *pdev) if (isp->hw->fw_idle_ms && isp->hw->resident_fw) { isp->pm_nb.notifier_call = apple_isp_pm_notify; + isp->pm_nb.priority = 1; register_pm_notifier(&isp->pm_nb); } @@ -1142,6 +1171,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 +1205,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 +1580,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, @@ -1636,6 +1674,10 @@ static const struct apple_isp_hw apple_isp_hw_t8142 = { .bandwidth_size = 0x8, .mbox_irq_enable = ISP_MBOX_IRQ_ENABLE_T6031, + /* ADT dart-ane dapf-instance-0: 0x4a1668000..0x4a1668003. */ + .peer_doorbell = 0x4a1668000, + .peer_doorbell_value = 0x10, + /* * No BES scaler: the firmware asserts on CH_CROP_SET ("BesProc is * removed/not present. Must config AuxSclPr") and takes the SCL1 set. @@ -1899,6 +1941,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..35362750e0662 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; @@ -401,6 +410,11 @@ struct apple_isp { bool center_stage; struct isp_preset *presets; int num_presets; + /* Optional DT apple,mlvnr-sensor-config = override. */ + bool has_mlvnr_config; + u32 mlvnr_config_from; + u32 mlvnr_config_to; + struct coord mlvnr_input_dim; /* Read-only sensor configuration replies, cached before video registration. */ unsigned int num_sensor_configs; struct isp_sensor_config *sensor_configs; @@ -469,6 +483,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 +526,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..8e6021a0d4cc9 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,80 @@ 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); + /* A second forced stop powers nothing down either. */ + if (!err && apple_isp_firmware_halt(isp)) + err = -ETIMEDOUT; + 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) +/* + * Stop the firmware; resident firmware without hw->fw_restart stays down. + * Returns true when it did not stop and was forced off. + */ +bool 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; + return false; - 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)); + return forced; } diff --git a/drivers/media/platform/apple/isp/isp-fw.h b/drivers/media/platform/apple/isp/isp-fw.h index acb97c821c602..2de786b9f097f 100644 --- a/drivers/media/platform/apple/isp/isp-fw.h +++ b/drivers/media/platform/apple/isp/isp-fw.h @@ -11,7 +11,7 @@ void apple_isp_free_firmware_surface(struct apple_isp *isp); int apple_isp_firmware_boot(struct apple_isp *isp); void apple_isp_firmware_shutdown(struct apple_isp *isp); -void apple_isp_firmware_halt(struct apple_isp *isp); +bool apple_isp_firmware_halt(struct apple_isp *isp); void apple_isp_firmware_init(struct apple_isp *isp); void apple_isp_firmware_flush(struct apple_isp *isp); 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 diff --git a/tools/asahi/isp-preview-flow-tests.py b/tools/asahi/isp-preview-flow-tests.py index 4e06b21f67d32..dab1df098436c 100644 --- a/tools/asahi/isp-preview-flow-tests.py +++ b/tools/asahi/isp-preview-flow-tests.py @@ -15,11 +15,11 @@ #include typedef unsigned int u32; struct coord {int x,y;}; -struct preset {int index;struct coord crop_offset,crop_size,output_dim;}; -struct isp_format {struct preset *preset;int strides[2];}; +struct isp_preset {int index;struct coord crop_offset,crop_size,output_dim;}; +struct isp_format {struct isp_preset *preset;int strides[2];}; struct hw {int fw_abi;bool lpdp;}; struct apple_isp {struct hw *hw;int temporal_filter;}; -static struct preset preset; +static struct isp_preset preset; static struct isp_format fmt={.preset=&preset}; static struct isp_format *isp_get_format(struct apple_isp *i,u32 ch){return &fmt;} #define ISP_FW_ABI_H17 1 @@ -35,6 +35,8 @@ effect={'isp_cmd_apple_ch_temporal_filter_start':'temporal_start++;','isp_cmd_apple_ch_motion_history_start':'history++;','isp_cmd_apple_ch_temporal_filter_enable':'enable++;','isp_cmd_apple_ch_temporal_filter_disable':'disable++;if(fail_disable)return -EIO;','isp_ch_configure_pools':'pools++;'}.get(name,'') code+='static int '+name+'(struct apple_isp *i,...){'+effect+'return 0;}\n' code += 'static bool isp_is_p010(struct apple_isp *i){return false;}\n' +code += 'static bool isp_resolve_mlvnr_preset(struct apple_isp *i,const struct isp_preset *src,struct isp_preset *out){*out=*src;return false;}\n' +code += 'static int isp_ch_configure_mlvnr(struct apple_isp *i,u32 ch,bool enable){return 0;}\n' code += 'static int isp_ch_configure_temporal_filter(struct apple_isp *i,u32 ch){temporal_start++;history++;enable++;return 0;}\n' code+=body+r''' int main(void){ diff --git a/tools/asahi/isp/buffers.c b/tools/asahi/isp/buffers.c index a85bc74fde028..7a061ab031248 100644 --- a/tools/asahi/isp/buffers.c +++ b/tools/asahi/isp/buffers.c @@ -289,6 +289,15 @@ static int apple_isp_start_capture(struct apple_isp *isp) return 0; } +/* The ANE endpoint's fallback bookkeeping (isp-ane.c) has no part in these paths. */ +static void isp_ane_stream_failed(struct apple_isp *isp) +{ +} + +static void isp_ane_stream_started(struct apple_isp *isp) +{ +} + static int isp_cmd_ch_stop(struct apple_isp *isp, int channel) { return isp->ipc_error; @@ -328,7 +337,7 @@ static unsigned int isp_num_rendered(struct apple_isp *isp) { return ISP_MAX_BUF static int isp_ipc_resync(struct apple_isp *isp) { return 0; } static void isp_meta_read_exposure(struct apple_isp *isp, struct isp_surf *surf) { } static bool isp_p010_buffer_matches(struct isp_format *fmt, struct isp_buffer *buf, const struct isp_buflist_buffer *desc) { return false; } -static void apple_isp_firmware_halt(struct apple_isp *isp) { quiescent = true; } +static bool apple_isp_firmware_halt(struct apple_isp *isp) { quiescent = true; return false; } /* DRIVER FUNCTIONS */ int main(void) { diff --git a/tools/asahi/isp/session.c b/tools/asahi/isp/session.c index cba9a3f88257e..34b8302b49271 100644 --- a/tools/asahi/isp/session.c +++ b/tools/asahi/isp/session.c @@ -70,6 +70,8 @@ struct apple_isp { bool ipc_quiescing; bool fw_idle; struct delayed_work fw_idle_work; + struct delayed_work fw_recover_work; + bool fw_recovering; struct isp_channel *chan_io, *chan_sm, *chan_tm, *chan_bt; struct isp_channel *ipc_stale; bool ipc_failed; @@ -280,6 +282,19 @@ static void flush_workqueue(void *wq) { } +/* The ANE endpoint (isp-ane.c): these fixtures run without one. */ +static void isp_ane_unset(struct apple_isp *isp) +{ +} + +static void isp_ane_detach(struct apple_isp *isp) +{ +} + +static void isp_ane_firmware_failed(struct apple_isp *isp) +{ +} + static int pm_runtime_resume_and_get(void *dev) { return 0; @@ -290,7 +305,7 @@ static void pm_runtime_put_sync(void *dev) halts++; } -static void apple_isp_firmware_halt(struct apple_isp *isp); +static bool apple_isp_firmware_halt(struct apple_isp *isp); static void disable_irq(int irq) {