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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Documentation/devicetree/bindings/media/apple,t8140-isp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,23 @@ properties:
- description: ISP CLR domain
- description: ISP CVD domain
- description: ISP CVM domain
- description: Camera sensor 0 domain
- description: Camera sensor 1 domain
minItems: 14

memory-region:
maxItems: 1
description: Firmware heap region provided by the bootloader
minItems: 1
maxItems: 3
description:
Firmware heap region provided by the bootloader, then the firmware text
and data segments it loaded

memory-region-names:
minItems: 1
items:
- const: heap
- const: text
- const: data

apple,dart-vm-size:
$ref: /schemas/types.yaml#/definitions/uint64
Expand Down Expand Up @@ -174,7 +183,7 @@ examples:
<&ps_isp_fe>, <&ps_isp_dprx>, <&ps_isp_secure>,
<&ps_isp_sts0>, <&ps_isp_sts1>, <&ps_isp_pearl>,
<&ps_isp_be>, <&ps_isp_clr>, <&ps_isp_cvd>,
<&ps_isp_cvm>;
<&ps_isp_cvm>, <&ps_ispsens0>, <&ps_ispsens1>;
memory-region = <&isp_heap>;
memory-region-names = "heap";
apple,dart-vm-size = <0x0 0xa0000000>;
Expand Down
6 changes: 4 additions & 2 deletions arch/arm64/boot/dts/apple/t8140.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,9 @@
*/
isp_dart0: iommu@3228c0000 {
compatible = "apple,t8140-dart", "apple,t8110-dart";
reg = <0x3 0x228c0000 0x0 0xc000>;
reg = <0x3 0x228c0000 0x0 0xc000>,
<0x3 0x228d0000 0x0 0x4000>;
reg-names = "dart", "dapf";
#iommu-cells = <1>;
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 341 IRQ_TYPE_LEVEL_HIGH>;
Expand Down Expand Up @@ -1938,7 +1940,7 @@
<&ps_isp_fe>, <&ps_isp_dprx>, <&ps_isp_secure>,
<&ps_isp_sts0>, <&ps_isp_sts1>, <&ps_isp_pearl>,
<&ps_isp_be>, <&ps_isp_clr>, <&ps_isp_cvd>,
<&ps_isp_cvm>;
<&ps_isp_cvm>, <&ps_ispsens0>, <&ps_ispsens1>;
apple,dart-vm-size = <0x0 0xa0000000>;
status = "disabled";
};
Expand Down
10 changes: 9 additions & 1 deletion drivers/media/platform/apple/isp/isp-drv-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ kunit_test_suite(isp_dapf_test_suite);
/*
* A deferred start detects the camera once: its firmware must keep running
* or start again. Firmware that starts again is not resident, and only it
* is quiesced before the power-down.
* is quiesced before the power-down or idles after a stream.
*/
static void isp_test_firmware_lifetime(struct kunit *test)
{
Expand All @@ -131,6 +131,14 @@ static void isp_test_firmware_lifetime(struct kunit *test)
if (hw->coproc_quiesce)
KUNIT_EXPECT_TRUE_MSG(test, hw->fw_restore, "%s",
match->compatible);
/* Only firmware that starts again idles before it stops. */
if (hw->fw_idle_ms)
KUNIT_EXPECT_TRUE_MSG(test, hw->fw_restore, "%s",
match->compatible);
/* The fallback keeps restartable firmware resident. */
if (hw->resident_fallback)
KUNIT_EXPECT_TRUE_MSG(test, hw->fw_restore && hw->num_dapf,
"%s", match->compatible);
}
}

Expand Down
218 changes: 178 additions & 40 deletions drivers/media/platform/apple/isp/isp-drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,6 @@ static void apple_isp_kvfree(void *data)
kvfree(data);
}

/*
* Keep a copy of the firmware data segment as the boot loader loaded it
* (hw->fw_restore). This must run before the firmware first starts.
*/
static int apple_isp_copy_fw_data(struct apple_isp *isp)
{
struct device *dev = isp->dev;
struct resource res;
int err;

err = of_reserved_mem_region_to_resource_byname(dev->of_node, "data", &res);
if (err)
return err;

isp->fw_data_size = resource_size(&res);
isp->fw_data = devm_memremap(dev, res.start, isp->fw_data_size, MEMREMAP_WB);
if (IS_ERR(isp->fw_data))
return PTR_ERR(isp->fw_data);

isp->fw_data_copy = kvmalloc(isp->fw_data_size, GFP_KERNEL);
if (!isp->fw_data_copy)
return -ENOMEM;
err = devm_add_action_or_reset(dev, apple_isp_kvfree, isp->fw_data_copy);
if (err)
return err;

memcpy(isp->fw_data_copy, isp->fw_data, isp->fw_data_size);
return 0;
}

static void apple_isp_detach_genpd(struct apple_isp *isp)
{
if (isp->pd_count <= 1)
Expand Down Expand Up @@ -181,17 +151,12 @@ static int apple_isp_map_fw_mmio(struct apple_isp *isp)
return 0;
}

/* The IOVA range of a memory-region, from its last iommu-addresses entry. */
static int isp_of_region_iova(struct device *dev, const char *name,
phys_addr_t *base, size_t *size)
/* The IOVA range of memory-region @idx, from its last iommu-addresses entry. */
static int isp_of_region_iova_idx(struct device *dev, int idx,
phys_addr_t *base, size_t *size)
{
const __be32 *maps, *end;
int idx, len;

idx = of_property_match_string(dev->of_node, "memory-region-names",
name);
if (idx < 0)
return -ENOENT;
int len;

struct device_node *np __free(device_node) =
of_parse_phandle(dev->of_node, "memory-region", idx);
Expand All @@ -212,6 +177,139 @@ static int isp_of_region_iova(struct device *dev, const char *name,
return 0;
}

/* The IOVA range of a named memory-region. */
static int isp_of_region_iova(struct device *dev, const char *name,
phys_addr_t *base, size_t *size)
{
int idx;

idx = of_property_match_string(dev->of_node, "memory-region-names",
name);
if (idx < 0)
return -ENOENT;

return isp_of_region_iova_idx(dev, idx, base, size);
}

/*
* The memory-region of the firmware data segment: the one named "data", or,
* where the boot loader lists the firmware segments without names (T8140),
* the one that ends where the heap starts. The loader places the text and
* data segments and the heap back to back in the firmware's address space.
*/
static int isp_of_fw_data_region(struct device *dev)
{
phys_addr_t heap_base, base;
size_t heap_size, size;
int idx, count;

idx = of_property_match_string(dev->of_node, "memory-region-names",
"data");
if (idx >= 0)
return idx;

if (isp_of_region_iova(dev, "heap", &heap_base, &heap_size))
return -ENOENT;

count = of_count_phandle_with_args(dev->of_node, "memory-region", NULL);
for (idx = 0; idx < count; idx++) {
if (isp_of_region_iova_idx(dev, idx, &base, &size))
continue;
if (size && base + size == heap_base)
return idx;
}

return -ENOENT;
}

static void *apple_isp_copy_region(struct device *dev, struct resource *res,
void **copy)
{
void *mem;

mem = devm_memremap(dev, res->start, resource_size(res), MEMREMAP_WB);
if (IS_ERR(mem))
return mem;

*copy = kvmalloc(resource_size(res), GFP_KERNEL);
if (!*copy)
return ERR_PTR(-ENOMEM);
if (devm_add_action_or_reset(dev, apple_isp_kvfree, *copy))
return ERR_PTR(-ENOMEM);

memcpy(*copy, mem, resource_size(res));
return mem;
}

/*
* Keep a copy of the firmware data segment as the boot loader loaded it
* (hw->fw_restore), and of the heap where the boot loader provides its
* memory instead of the host. This must run before the firmware first
* starts.
*/
static int apple_isp_copy_fw_data(struct apple_isp *isp)
{
struct device *dev = isp->dev;
struct resource res;
int idx, err;

idx = isp_of_fw_data_region(dev);
if (idx < 0)
return idx;

err = of_reserved_mem_region_to_resource(dev->of_node, idx, &res);
if (err)
return err;

isp->fw_data_size = resource_size(&res);
isp->fw_data = apple_isp_copy_region(dev, &res, &isp->fw_data_copy);
if (IS_ERR(isp->fw_data))
return PTR_ERR(isp->fw_data);

/* A host-backed main memory tail is cleared instead. */
if (isp->fw.data_top ||
of_reserved_mem_region_to_resource_byname(dev->of_node, "heap", &res))
return 0;

isp->fw_heap_size = resource_size(&res);
isp->fw_heap = apple_isp_copy_region(dev, &res, &isp->fw_heap_copy);
return PTR_ERR_OR_ZERO(isp->fw_heap);
}

/*
* Firmware that starts again needs the DART address filter registers, to
* program the filter after each power-up, and its data segment. Device
* trees from before those were described run it resident instead
* (hw->resident_fallback), as it ran before.
*/
static void apple_isp_select_hw(struct apple_isp *isp)
{
struct device *dev = isp->dev;
struct apple_isp_hw *hw = &isp->hw_local;

*hw = *isp->hw;
isp->hw = hw;

if (!hw->resident_fallback)
return;

struct device_node *dart __free(device_node) =
of_parse_phandle(dev->of_node, "iommus", 0);

if (dart && of_property_match_string(dart, "reg-names", "dapf") >= 0 &&
isp_of_fw_data_region(dev) >= 0)
return;

dev_info(dev, "no address filter or firmware data in the device tree, keeping the firmware resident\n");
hw->fw_restore = false;
hw->deferred_start = false;
hw->resident_fw = true;
hw->coproc_quiesce = 0;
hw->dapf = NULL;
hw->num_dapf = 0;
hw->fw_idle_ms = 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.
Expand Down Expand Up @@ -577,6 +675,8 @@ static int apple_isp_probe(struct platform_device *pdev)
goto free_isp;
}

apple_isp_select_hw(isp);

err = apple_isp_attach_genpd(isp);
if (err) {
dev_err(dev, "failed to attatch power domains\n");
Expand Down Expand Up @@ -647,6 +747,7 @@ static int apple_isp_probe(struct platform_device *pdev)
}

mutex_init(&isp->video_lock);
apple_isp_firmware_init(isp);
spin_lock_init(&isp->buf_lock);
init_waitqueue_head(&isp->wait);
INIT_LIST_HEAD(&isp->gc);
Expand Down Expand Up @@ -725,6 +826,7 @@ static void apple_isp_remove(struct platform_device *pdev)

apple_isp_remove_video(isp);
/* Stop resident firmware before freeing what it may still use. */
apple_isp_firmware_flush(isp);
apple_isp_firmware_halt(isp);
apple_isp_free_video(isp);
pm_runtime_disable(isp->dev);
Expand Down Expand Up @@ -922,6 +1024,31 @@ static const struct isp_mmio_window apple_isp_fw_mmio_t8140[] = {
{ 0x401660000, 0x4000 },
};

/*
* The windows the boot loader programs into the first ISP DART's address
* filter, in its order, as they read back on the J700. The DART is in the
* ISP system domain, so they are programmed again after each power-up.
*/
static const struct isp_dapf_window apple_isp_dapf_t8140[] = {
{ 0x300704000, 0x300704063 }, /* PMGR, ISP power states */
{ 0x300730000, 0x300733fff },
{ 0x300e3c000, 0x300e3c003 },
{ 0x31062c000, 0x310648003 },
{ 0x401660000, 0x401660003 },
{ 0x220004000, 0x2200162ff }, /* DSID broadcast-clear windows */
{ 0x220044000, 0x2200562ff },
{ 0x220084000, 0x2200962ff },
{ 0x2200c4000, 0x2200d62ff },
{ 0x220104000, 0x2201162ff },
{ 0x302824000, 0x302824007 }, /* PMP bandwidth scratch */
{ 0x3003c0000, 0x3003e4003 }, /* PMGR, with the PMP clock scratch */
};

/*
* The firmware starts again from its data segment as loaded once the
* coprocessor was quiesced before the power-down, as T8132's does, so the
* ISP is powered only while the camera is in use.
*/
static const struct apple_isp_hw apple_isp_hw_t8140 = {
.gen = ISP_GEN_T8140,
.fw_abi = ISP_FW_ABI_H17,
Expand Down Expand Up @@ -949,11 +1076,18 @@ static const struct apple_isp_hw apple_isp_hw_t8140 = {
.mbox2_offset = 0x410,
.mbox_irq_route = true,
.coproc_control = ISP_COPROC_CONTROL_T8140,
.coproc_status = ISP_COPROC_STATUS_T8140,
.coproc_quiesce = ISP_COPROC_QUIESCE_T8140,
.fw_mmio = apple_isp_fw_mmio_t8140,
.num_fw_mmio = ARRAY_SIZE(apple_isp_fw_mmio_t8140),
.dapf = apple_isp_dapf_t8140,
.num_dapf = ARRAY_SIZE(apple_isp_dapf_t8140),
.boot_mode = 1,
.capture_meta_size = ISP_CAPTURE_META_SIZE_T8140,
.resident_fw = true,
.fw_restore = true,
.deferred_start = true,
.resident_fallback = true,
.fw_idle_ms = 2000,
.meta_exposure = true,
};

Expand Down Expand Up @@ -1256,7 +1390,9 @@ static __maybe_unused int apple_isp_suspend(struct device *dev)
/*
* Resident firmware stays up, idle, across suspend-to-idle and
* suspend-to-RAM: its domains stay on, see apple_isp_attach_genpd().
* Firmware that starts again and still idles after a stream stops.
*/
apple_isp_firmware_flush(isp);
return 0;
}

Expand All @@ -1265,6 +1401,7 @@ static __maybe_unused int apple_isp_freeze(struct device *dev)
struct apple_isp *isp = dev_get_drvdata(dev);

apple_isp_video_suspend(isp);
apple_isp_firmware_flush(isp);

/*
* Hibernation hands the system from one kernel to another, and
Expand Down Expand Up @@ -1314,6 +1451,7 @@ static void apple_isp_shutdown(struct platform_device *pdev)
return;
/* A stream that still runs is stopped cleanly first, as for hibernation */
apple_isp_video_suspend(isp);
apple_isp_firmware_flush(isp);
mutex_lock(&isp->video_lock);
apple_isp_firmware_halt(isp);
mutex_unlock(&isp->video_lock);
Expand Down
Loading
Loading