diff --git a/Documentation/devicetree/bindings/media/apple,t8132-isp.yaml b/Documentation/devicetree/bindings/media/apple,t8132-isp.yaml index 3861d13a0bde9..3aa3375d8cfc7 100644 --- a/Documentation/devicetree/bindings/media/apple,t8132-isp.yaml +++ b/Documentation/devicetree/bindings/media/apple,t8132-isp.yaml @@ -105,6 +105,22 @@ properties: description: Firmware temporal-filter strength selected for the attached sensor. + apple,dapf-ranges: + $ref: /schemas/types.yaml#/definitions/uint64-matrix + minItems: 1 + maxItems: 16 + items: + items: + - description: Physical start address, inclusive + - description: Physical end address, inclusive + description: + The address filter windows of the first ISP DART, in record order, as + the bootloader finds them in its hardware description. Each pair holds + inclusive 64-bit physical bounds, not a base and a length. The filter + loses them when the ISP powers down, so they are programmed again + before the firmware starts. The firmware reaches these registers + through identity mappings of the 16 KiB pages that contain them. + sensor-presets: type: object description: @@ -188,6 +204,8 @@ examples: memory-region-names = "text", "data", "heap"; apple,dart-vm-size = /bits/ 64 <0xdec00000>; apple,platform-id = <0x17>; + apple,dapf-ranges = /bits/ 64 <0x380704000 0x380704063>, + /bits/ 64 <0x382824000 0x382824004>; sensor-presets { preset0 { diff --git a/arch/arm64/boot/dts/apple/t8132-isp.dtsi b/arch/arm64/boot/dts/apple/t8132-isp.dtsi index 2efed49eafc00..a6fe4cf1d52c8 100644 --- a/arch/arm64/boot/dts/apple/t8132-isp.dtsi +++ b/arch/arm64/boot/dts/apple/t8132-isp.dtsi @@ -7,8 +7,9 @@ * that move with the boot firmware. The boot loader fills these * placeholders from the ADT's isp segment-ranges: reg, and * iommu-addresses where the firmware runs them, from 0x100_0000_0000 on. - * It then sets apple,platform-id from the ADT isp sensor-type and enables - * the ISP and its DARTs. The driver checks that the coprocessor's reset + * It then sets apple,platform-id from the ADT isp sensor-type, copies the + * address filter windows of the ADT's ISP DART into apple,dapf-ranges and + * enables the ISP and its DARTs. The driver checks that the coprocessor's reset * vector is this text before it starts the firmware. * * Copyright The Asahi Linux Contributors diff --git a/drivers/media/platform/apple/isp/isp-drv-test.c b/drivers/media/platform/apple/isp/isp-drv-test.c index 2ff43d1c7ae26..37cffe14aa9b8 100644 --- a/drivers/media/platform/apple/isp/isp-drv-test.c +++ b/drivers/media/platform/apple/isp/isp-drv-test.c @@ -97,8 +97,82 @@ static void isp_test_dapf_program_t6050(struct kunit *test) isp_test_dapf_program_mask(test, 0xcc3); } +/* The J713 ADT's ISP DART address filter, as the boot loader copies it */ +static const struct isp_dapf_window isp_test_dapf_j713[] = { + { 0x380704000, 0x380704063 }, + { 0x380730000, 0x380733fff }, + { 0x380e3c000, 0x380e3c003 }, + { 0x39062c000, 0x390648003 }, + { 0x501660000, 0x501660003 }, + { 0x220004000, 0x2200162fc }, + { 0x220044000, 0x2200562fc }, + { 0x2201c4000, 0x2201d62fc }, + { 0x222004000, 0x2220162fc }, + { 0x222044000, 0x2220562fc }, + { 0x2221c4000, 0x2221d62fc }, + { 0x3803c0000, 0x3803e4000 }, + { 0x382824000, 0x382824004 }, +}; + +/* + * The MMIO windows made from the address filter are whole 16 KiB pages, + * sorted and apart from each other, and hold every filter window. + */ +static void isp_test_dapf_mmio_windows(struct kunit *test) +{ + const unsigned int num = ARRAY_SIZE(isp_test_dapf_j713); + struct isp_mmio_window mmio[ARRAY_SIZE(isp_test_dapf_j713)]; + unsigned int used = isp_dapf_mmio_windows(isp_test_dapf_j713, num, mmio); + + /* None of the J713's windows share a page. */ + KUNIT_EXPECT_EQ(test, used, num); + for (unsigned int i = 0; i < used; i++) { + KUNIT_EXPECT_EQ(test, mmio[i].base % SZ_16K, 0); + KUNIT_EXPECT_EQ(test, mmio[i].size % SZ_16K, 0); + KUNIT_EXPECT_GT(test, mmio[i].size, 0); + if (i) + KUNIT_EXPECT_GT(test, mmio[i].base, + mmio[i - 1].base + mmio[i - 1].size); + } + for (unsigned int i = 0; i < num; i++) { + const struct isp_dapf_window *w = &isp_test_dapf_j713[i]; + bool inside = false; + + for (unsigned int j = 0; j < used; j++) + if (w->start >= mmio[j].base && + w->end < mmio[j].base + mmio[j].size) + inside = true; + KUNIT_EXPECT_TRUE_MSG(test, inside, "window %u 0x%llx-0x%llx", + i, w->start, w->end); + } + /* The PMGR window ends on a page boundary, inclusively. */ + KUNIT_EXPECT_EQ(test, mmio[6].base, 0x3803c0000); + KUNIT_EXPECT_EQ(test, mmio[6].size, 0x28000); +} + +/* Windows in one page, or in adjacent pages, are mapped as one. */ +static void isp_test_dapf_mmio_merge(struct kunit *test) +{ + static const struct isp_dapf_window dapf[] = { + { 0x108000, 0x108003 }, + { 0x101000, 0x101003 }, + { 0x103ffc, 0x104003 }, + { 0x200000, 0x200003 }, + }; + struct isp_mmio_window mmio[ARRAY_SIZE(dapf)]; + unsigned int used = isp_dapf_mmio_windows(dapf, ARRAY_SIZE(dapf), mmio); + + KUNIT_ASSERT_EQ(test, used, 2); + KUNIT_EXPECT_EQ(test, mmio[0].base, 0x100000); + KUNIT_EXPECT_EQ(test, mmio[0].size, 0xc000); + KUNIT_EXPECT_EQ(test, mmio[1].base, 0x200000); + KUNIT_EXPECT_EQ(test, mmio[1].size, 0x4000); +} + static struct kunit_case isp_dapf_test_cases[] = { KUNIT_CASE(isp_test_dapf_in_fw_mmio), + KUNIT_CASE(isp_test_dapf_mmio_windows), + KUNIT_CASE(isp_test_dapf_mmio_merge), KUNIT_CASE(isp_test_dapf_program), KUNIT_CASE(isp_test_dapf_program_t6050), {} diff --git a/drivers/media/platform/apple/isp/isp-drv.c b/drivers/media/platform/apple/isp/isp-drv.c index f0410cea88b5f..40fd29f743bd9 100644 --- a/drivers/media/platform/apple/isp/isp-drv.c +++ b/drivers/media/platform/apple/isp/isp-drv.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include @@ -327,6 +329,107 @@ static void apple_isp_select_hw(struct apple_isp *isp) hw->fw_idle_ms = 0; } +/* A T8132 address filter bank has 16 slices. */ +#define ISP_MAX_DT_DAPF 16 + +static int isp_mmio_window_cmp(const void *a, const void *b) +{ + const struct isp_mmio_window *wa = a, *wb = b; + + return wa->base < wb->base ? -1 : wa->base > wb->base; +} + +/* + * The firmware reaches the address filter windows through its DART at their + * physical addresses: the 16 KiB pages that contain them, sorted, with + * overlapping and adjacent ones merged so that each is mapped once. Returns + * their count; @out has room for @num. + */ +static unsigned int isp_dapf_mmio_windows(const struct isp_dapf_window *dapf, + unsigned int num, + struct isp_mmio_window *out) +{ + unsigned int used = 0; + + for (unsigned int i = 0; i < num; i++) { + u64 base = ALIGN_DOWN(dapf[i].start, SZ_16K); + + out[i].base = base; + out[i].size = ALIGN(dapf[i].end + 1, SZ_16K) - base; + } + sort(out, num, sizeof(*out), isp_mmio_window_cmp, NULL); + + for (unsigned int i = 0; i < num; i++) { + struct isp_mmio_window *last = used ? &out[used - 1] : NULL; + + if (last && out[i].base <= last->base + last->size) { + last->size = max(last->size, + out[i].base + out[i].size - last->base); + continue; + } + out[used++] = out[i]; + } + + return used; +} + +/* + * With hw->dapf_from_dt, apple,dapf-ranges has the address filter windows of + * the ADT's ISP DART, inclusive start and end pairs in record order, which + * the boot loader copies. Without them the firmware cannot run again after + * its first power-down, so the camera is not used. + */ +static int apple_isp_of_dapf(struct apple_isp *isp) +{ + struct device *dev = isp->dev; + struct apple_isp_hw *hw = &isp->hw_local; + struct isp_dapf_window *dapf; + struct isp_mmio_window *mmio; + unsigned int num; + u64 *ranges; + int n; + + if (!hw->dapf_from_dt) + return 0; + + n = of_property_count_u64_elems(dev->of_node, "apple,dapf-ranges"); + if (n == -EINVAL) + return dev_err_probe(dev, -ENODEV, + "the boot loader gave no address filter (apple,dapf-ranges)\n"); + if (n <= 0 || n % 2 || n / 2 > ISP_MAX_DT_DAPF) + return dev_err_probe(dev, -EINVAL, + "apple,dapf-ranges needs 1 to %d start and end pairs\n", + ISP_MAX_DT_DAPF); + num = n / 2; + + ranges = devm_kcalloc(dev, n, sizeof(*ranges), GFP_KERNEL); + dapf = devm_kcalloc(dev, num, sizeof(*dapf), GFP_KERNEL); + mmio = devm_kcalloc(dev, num, sizeof(*mmio), GFP_KERNEL); + if (!ranges || !dapf || !mmio) + return -ENOMEM; + + n = of_property_read_u64_array(dev->of_node, "apple,dapf-ranges", ranges, n); + if (n) + return dev_err_probe(dev, n, "failed to read apple,dapf-ranges\n"); + + for (unsigned int i = 0; i < num; i++) { + dapf[i].start = ranges[2 * i]; + dapf[i].end = ranges[2 * i + 1]; + if (dapf[i].end < dapf[i].start || dapf[i].end > U64_MAX - SZ_16K) + return dev_err_probe(dev, -EINVAL, + "address filter window %u 0x%llx-0x%llx is invalid\n", + i, dapf[i].start, dapf[i].end); + } + devm_kfree(dev, ranges); + + hw->dapf = dapf; + hw->num_dapf = num; + hw->fw_mmio = mmio; + hw->num_fw_mmio = isp_dapf_mmio_windows(dapf, num, mmio); + + return 0; +} + /* * The firmware reaches its fixed MMIO windows through the first DART, whose * address filter the ADT describes for them. Its registers are on that DART. @@ -752,6 +855,10 @@ static int apple_isp_probe(struct platform_device *pdev) apple_isp_select_hw(isp); + err = apple_isp_of_dapf(isp); + if (err) + goto free_isp; + err = apple_isp_attach_genpd(isp); if (err) { dev_err(dev, "failed to attatch power domains\n"); @@ -1224,36 +1331,6 @@ static const struct apple_isp_hw apple_isp_hw_t6040 = { .meta_exposure = true, }; -/* T8132: the firmware's MMIO windows, from the J713 ADT's DART address filter */ -static const struct isp_mmio_window apple_isp_fw_mmio_t8132[] = { - { 0x220004000, 0x1d4000 }, /* DSID broadcast-clear windows */ - { 0x222004000, 0x1d4000 }, - { 0x3803c0000, 0x28000 }, /* PMGR, with the PMP clock scratch */ - { 0x380704000, 0x4000 }, /* PMGR, ISP power states */ - { 0x380730000, 0x4000 }, - { 0x380e3c000, 0x4000 }, - { 0x382824000, 0x4000 }, /* PMP bandwidth scratch */ - { 0x39062c000, 0x20000 }, - { 0x501660000, 0x4000 }, -}; - -/* The ADT's DART address filter windows, in its order and with its ends */ -static const struct isp_dapf_window apple_isp_dapf_t8132[] = { - { 0x380704000, 0x380704063 }, /* PMGR, ISP power states */ - { 0x380730000, 0x380733fff }, - { 0x380e3c000, 0x380e3c003 }, - { 0x39062c000, 0x390648003 }, - { 0x501660000, 0x501660003 }, - { 0x220004000, 0x2200162fc }, /* DSID broadcast-clear windows */ - { 0x220044000, 0x2200562fc }, - { 0x2201c4000, 0x2201d62fc }, - { 0x222004000, 0x2220162fc }, - { 0x222044000, 0x2220562fc }, - { 0x2221c4000, 0x2221d62fc }, - { 0x3803c0000, 0x3803e4000 }, /* PMGR, with the PMP clock scratch */ - { 0x382824000, 0x382824004 }, /* PMP bandwidth scratch */ -}; - /* * T8132 runs T6040's coprocessor and firmware interface, booted the same * way. Its two AMCCs (ADT mcc amcc_aperture_count) give two broadcast-clear @@ -1261,7 +1338,7 @@ static const struct isp_dapf_window apple_isp_dapf_t8132[] = { * sent: T8132's PMP scratch offsets are not established. Its firmware * starts again from its data segment as loaded once the coprocessor was * quiesced before the power-down, so the ISP is powered only while the - * camera is in use. + * camera is in use. The boot loader gives its address filter windows. */ static const struct apple_isp_hw apple_isp_hw_t8132 = { .gen = ISP_GEN_T6040, @@ -1278,10 +1355,7 @@ static const struct apple_isp_hw apple_isp_hw_t8132 = { .scl1 = true, .lpdp = false, .meta_size = ISP_META_SIZE_T8140, - .fw_mmio = apple_isp_fw_mmio_t8132, - .num_fw_mmio = ARRAY_SIZE(apple_isp_fw_mmio_t8132), - .dapf = apple_isp_dapf_t8132, - .num_dapf = ARRAY_SIZE(apple_isp_dapf_t8132), + .dapf_from_dt = true, .coproc_control = ISP_COPROC_CONTROL_T8140, .coproc_status = ISP_COPROC_STATUS_T8140, diff --git a/drivers/media/platform/apple/isp/isp-drv.h b/drivers/media/platform/apple/isp/isp-drv.h index 3551018baf174..9cc72ac64f692 100644 --- a/drivers/media/platform/apple/isp/isp-drv.h +++ b/drivers/media/platform/apple/isp/isp-drv.h @@ -223,6 +223,12 @@ struct apple_isp_hw { unsigned int num_dapf; u32 dapf_sidmask; + /* + * The boot loader gives the address filter windows from the ADT, in + * apple,dapf-ranges, instead of dapf; fw_mmio is then made from them. + */ + bool dapf_from_dt; + /* ISP_GPIO_6 boot mode */ u32 boot_mode; diff --git a/drivers/media/platform/apple/isp/isp-fw.c b/drivers/media/platform/apple/isp/isp-fw.c index 09b6912286505..80ee510320503 100644 --- a/drivers/media/platform/apple/isp/isp-fw.c +++ b/drivers/media/platform/apple/isp/isp-fw.c @@ -5,6 +5,7 @@ #include "isp-ipc-layout.h" #include +#include #include #include #include @@ -419,10 +420,19 @@ static int isp_reset_coproc(struct apple_isp *isp) return 0; } +/* Write CPU stores out to memory, for a coprocessor that might not snoop them. */ +static void isp_clean_to_poc(void *addr, size_t size) +{ +#ifdef CONFIG_ARM64 + dcache_clean_poc((unsigned long)addr, (unsigned long)addr + size); +#endif +} + /* * With hw->fw_restore, put the firmware memory back as the boot loader left * it: the data segment, and a heap the boot loader provides, from their - * copies, and the host-backed main memory above the data cleared. + * copies, and the host-backed main memory above the data cleared. All of it + * is cleaned to the point of coherency before the coprocessor starts. */ static void isp_restore_fw_memory(struct apple_isp *isp) { @@ -432,11 +442,16 @@ static void isp_restore_fw_memory(struct apple_isp *isp) return; memcpy(isp->fw_data, isp->fw_data_copy, isp->fw_data_size); - if (isp->fw_heap) + isp_clean_to_poc(isp->fw_data, isp->fw_data_size); + if (isp->fw_heap) { memcpy(isp->fw_heap, isp->fw_heap_copy, isp->fw_heap_size); + isp_clean_to_poc(isp->fw_heap, isp->fw_heap_size); + } if (tail) - for (u32 i = 0; i < tail->num_pages; i++) + for (u32 i = 0; i < tail->num_pages; i++) { clear_highpage(tail->pages[i]); + isp_clean_to_poc(page_address(tail->pages[i]), PAGE_SIZE); + } } /*