diff --git a/drivers/media/platform/apple/isp/isp-cam-test.c b/drivers/media/platform/apple/isp/isp-cam-test.c index a98b38a16fd78..846253d48a82a 100644 --- a/drivers/media/platform/apple/isp/isp-cam-test.c +++ b/drivers/media/platform/apple/isp/isp-cam-test.c @@ -993,6 +993,113 @@ static void isp_test_stream_failure_lights_off(struct kunit *test) KUNIT_EXPECT_EQ(test, trace.set_off_at, 3); } +struct isp_test_lsc_wire { + bool h17; + u8 info_calls; + u8 grid_calls; +}; + +static int isp_test_lsc_info_stub(struct apple_isp *isp, void *args, + u32 insize, u32 outsize, int timeout) +{ + struct kunit *test = kunit_get_current_test(); + struct isp_test_lsc_wire *w = test->priv; + + w->info_calls++; + if (w->h17) { + struct cmd_ch_sensor_lsc_info_h17 *r = isp->cmd_virt; + + KUNIT_EXPECT_EQ(test, insize, 0x14); + KUNIT_EXPECT_EQ(test, outsize, 0x14); + memcpy(r, args, sizeof(*r)); + r->num_x = 3; + r->num_y = 3; + r->bit_depth = 10; + r->data_size = 3 * 3 * 4 * 2; + } else { + struct cmd_ch_sensor_lsc_info_legacy *r = isp->cmd_virt; + + KUNIT_EXPECT_EQ(test, insize, 0x10); + KUNIT_EXPECT_EQ(test, outsize, 0x10); + memcpy(r, args, sizeof(*r)); + r->num_x = 3; + r->num_y = 3; + r->data_size = 3 * 3 * 4; + } + return 0; +} + +static int isp_test_lsc_grid_stub(struct apple_isp *isp, void *args, + u32 insize, u32 outsize, int timeout) +{ + struct kunit *test = kunit_get_current_test(); + struct isp_test_lsc_wire *w = test->priv; + + w->grid_calls++; + if (w->h17) { + struct cmd_ch_sensor_lsc_grid_h17 *h = args; + + KUNIT_EXPECT_EQ(test, insize, 0x114 + 72); + h->valid = 1; + h->x_coords[0] = 0x00000000; + h->x_coords[1] = 0x3f000000; + h->x_coords[2] = 0x3f800000; + h->y_coords[0] = 0x00000000; + h->y_coords[1] = 0x3f000000; + h->y_coords[2] = 0x3f800000; + memset((u8 *)args + sizeof(*h), 0x42, 72); + } else { + struct cmd_ch_sensor_lsc_grid_legacy *h = args; + + KUNIT_EXPECT_EQ(test, insize, 0xb0 + 36); + h->valid = 1; + h->x_coords[0] = 0x00000000; + h->x_coords[1] = 0x3f000000; + h->x_coords[2] = 0x3f800000; + h->y_coords[0] = 0x00000000; + h->y_coords[1] = 0x3f000000; + h->y_coords[2] = 0x3f800000; + memset((u8 *)args + sizeof(*h), 0x55, 36); + } + return 0; +} + +static void isp_test_permodule_lsc_wire(struct kunit *test) +{ + static const struct apple_isp_hw hw_legacy = { + .gen = ISP_GEN_T8112, .fw_abi = ISP_FW_ABI_LEGACY, + }; + static const struct apple_isp_hw hw_h17 = { + .gen = ISP_GEN_T6040, .fw_abi = ISP_FW_ABI_H17, + }; + struct apple_isp *isp = isp_test_capture_alloc(test); + struct isp_test_lsc_wire w = {}; + + isp->cmd_virt = kunit_kzalloc(test, 0x40, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, isp->cmd_virt); + test->priv = &w; + kunit_activate_static_stub(test, isp_cmd_send, isp_test_lsc_info_stub); + kunit_activate_static_stub(test, isp_cmd_send_data, isp_test_lsc_grid_stub); + + isp->hw = &hw_legacy; + w.h17 = false; + isp_ch_cache_permodule_lsc(isp, 0); + KUNIT_EXPECT_TRUE(test, isp->lsc.valid); + KUNIT_EXPECT_EQ(test, isp->lsc.bit_depth, 8); + KUNIT_EXPECT_EQ(test, isp->lsc.data_size, 36); + KUNIT_EXPECT_EQ(test, isp->lsc.data[0], 0x55); + + isp->hw = &hw_h17; + w.h17 = true; + isp_ch_cache_permodule_lsc(isp, 0); + KUNIT_EXPECT_TRUE(test, isp->lsc.valid); + KUNIT_EXPECT_EQ(test, isp->lsc.bit_depth, 10); + KUNIT_EXPECT_EQ(test, isp->lsc.data_size, 72); + KUNIT_EXPECT_EQ(test, isp->lsc.data[0], 0x42); + KUNIT_EXPECT_EQ(test, w.info_calls, 2); + KUNIT_EXPECT_EQ(test, w.grid_calls, 2); +} + static void isp_test_failed_fw_stop_lights_off(struct kunit *test) { struct apple_isp *isp = isp_test_indicator_alloc(test); @@ -1033,6 +1140,7 @@ static struct kunit_case isp_cam_test_cases[] = { KUNIT_CASE(isp_test_indicator_on_precedes_stream), KUNIT_CASE(isp_test_stream_failure_lights_off), KUNIT_CASE(isp_test_failed_fw_stop_lights_off), + KUNIT_CASE(isp_test_permodule_lsc_wire), {} }; diff --git a/drivers/media/platform/apple/isp/isp-cam.c b/drivers/media/platform/apple/isp/isp-cam.c index 544a347c7c90a..ba1da5687b9e1 100644 --- a/drivers/media/platform/apple/isp/isp-cam.c +++ b/drivers/media/platform/apple/isp/isp-cam.c @@ -457,6 +457,42 @@ static int isp_add_call_sizes(struct apple_isp *isp) return 0; } +static int isp_ch_cache_permodule_lsc(struct apple_isp *isp, u32 ch) +{ + struct isp_permodule_lsc *lsc = &isp->lsc; + u8 num_x, num_y, bit_depth; + u16 data_size; + int err; + + lsc->valid = false; + devm_kfree(isp->dev, lsc->data); + lsc->data = NULL; + + err = isp_cmd_ch_sensor_lsc_info_get(isp, ch, &num_x, &num_y, + &data_size, &bit_depth); + if (err) { + dev_dbg(isp->dev, "ch %u: no per-module LSC info (%d)\n", + ch, err); + return READ_ONCE(isp->ipc_error); + } + + lsc->num_x = num_x; + lsc->num_y = num_y; + lsc->data_size = data_size; + lsc->bit_depth = bit_depth; + + err = isp_cmd_ch_sensor_lsc_grid_get(isp, ch, lsc); + if (err) { + dev_dbg(isp->dev, "ch %u: per-module LSC grid unavailable (%d)\n", + ch, err); + return READ_ONCE(isp->ipc_error); + } + + dev_info(isp->dev, "ch %u: per-module LSC %ux%u (%u-bit)\n", + ch, lsc->num_x, lsc->num_y, lsc->bit_depth); + return 0; +} + static int isp_ch_cache_sensor_info(struct apple_isp *isp, u32 ch) { struct isp_format *fmt = isp_get_format(isp, ch); @@ -513,6 +549,8 @@ static int isp_ch_cache_sensor_info(struct apple_isp *isp, u32 ch) err = isp_ch_cache_sensor_configs(isp, ch, args->num_presets); if (!err) err = isp_add_call_sizes(isp); + if (!err) + err = isp_ch_cache_permodule_lsc(isp, ch); exit: kfree(args); @@ -850,6 +888,7 @@ static int isp_ch_load_setfile(struct apple_isp *isp, u32 ch) memcpy(isp->data_surf->virt, fw->data, len); memset(isp->data_surf->virt + len, 0, setfile->size - len); release_firmware(fw); + /* Seen on T6040 only; T8140 runs its setfiles as they are. */ if (isp->hw->gen == ISP_GEN_T6040 || isp->hw->setfile_stride_fixup) isp_setfile_fixup_h17(isp->data_surf->virt, len); diff --git a/drivers/media/platform/apple/isp/isp-cmd.c b/drivers/media/platform/apple/isp/isp-cmd.c index 7fb5203e71aea..791535ae8320b 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.c +++ b/drivers/media/platform/apple/isp/isp-cmd.c @@ -21,10 +21,10 @@ #define CISP_POST_IN(x, a) (isp_cmd_send((x), &(a), sizeof(a), 0, 0)) #define CISP_POST_INOUT(x, a) (isp_cmd_send((x), &(a), sizeof(a), sizeof(a), 0)) -/* Send through the command area on a command channel. */ -static int isp_cmd_send_chan(struct apple_isp *isp, struct isp_channel *chan, - void *args, u32 insize, u32 outsize, - unsigned long timeout) +/* Send through a caller-supplied DMA command buffer on a command channel. */ +static int isp_cmd_send_buf(struct apple_isp *isp, struct isp_channel *chan, + dma_addr_t iova, void *virt, void *args, + u32 insize, u32 outsize, unsigned long timeout) { struct isp_message *req; int err; @@ -39,11 +39,11 @@ static int isp_cmd_send_chan(struct apple_isp *isp, struct isp_channel *chan, return err; req = &chan->req; - req->arg0 = isp->cmd_iova; + req->arg0 = iova; req->arg1 = insize; req->arg2 = outsize; - memcpy(isp->cmd_virt, args, insize); + memcpy(virt, args, insize); err = ipc_chan_send(isp, chan, timeout); /* * An answered request leaves the ring in step, so a malformed answer @@ -55,7 +55,7 @@ static int isp_cmd_send_chan(struct apple_isp *isp, struct isp_channel *chan, if (!err && timeout && (rsp->arg1 != outsize || rsp->arg2 || rsp->arg3 || rsp->arg4 || rsp->arg5 || rsp->arg6 || rsp->arg7 || - get_unaligned_le64(isp->cmd_virt) != get_unaligned_le64(args))) + get_unaligned_le64(virt) != get_unaligned_le64(args))) err = -EIO; } if (err) { @@ -70,6 +70,15 @@ static int isp_cmd_send_chan(struct apple_isp *isp, struct isp_channel *chan, return err; } +/* Send through the command area on a command channel. */ +static int isp_cmd_send_chan(struct apple_isp *isp, struct isp_channel *chan, + void *args, u32 insize, u32 outsize, + unsigned long timeout) +{ + return isp_cmd_send_buf(isp, chan, isp->cmd_iova, isp->cmd_virt, args, + insize, outsize, timeout); +} + int isp_cmd_send(struct apple_isp *isp, void *args, u32 insize, u32 outsize, int timeout) { if (READ_ONCE(isp->ipc_error)) @@ -80,6 +89,33 @@ int isp_cmd_send(struct apple_isp *isp, void *args, u32 insize, u32 outsize, int timeout); } +/* + * Send a command whose payload exceeds the IPC command area through the + * data surface, copying the reply back into @args. Callers must not overlap + * another user of @isp->data_surf. + */ +int isp_cmd_send_data(struct apple_isp *isp, void *args, u32 insize, + u32 outsize, int timeout) +{ + int err; + + if (READ_ONCE(isp->ipc_error)) + return READ_ONCE(isp->ipc_error); + + KUNIT_STATIC_STUB_REDIRECT(isp_cmd_send_data, isp, args, insize, + outsize, timeout); + if (!isp->data_surf || insize > isp->data_surf->size || + outsize > isp->data_surf->size) + return -ENOSPC; + + err = isp_cmd_send_buf(isp, isp->chan_io, isp->data_surf->iova, + isp->data_surf->virt, args, insize, outsize, + timeout); + if (!err && outsize) + memcpy(args, isp->data_surf->virt, outsize); + return err; +} + static int cisp_send_read(struct apple_isp *isp, void *args, u32 insize, u32 outsize) { @@ -846,3 +882,87 @@ int isp_cmd_ch_property_read(struct apple_isp *isp, u32 chan, u32 prop, u32 *val return ret; } + +int isp_cmd_ch_sensor_lsc_info_get(struct apple_isp *isp, u32 chan, + u8 *num_x, u8 *num_y, u16 *data_size, + u8 *bit_depth) +{ + bool h17 = isp->hw->fw_abi == ISP_FW_ABI_H17; + struct cmd_ch_sensor_lsc_info_h17 args = { + .opcode = CISP_OPCODE(CISP_CMD_CH_SENSOR_PERMODULE_LSC_INFO_GET), + .chan = chan, + }; + u32 wire_size = h17 ? sizeof(struct cmd_ch_sensor_lsc_info_h17) : + sizeof(struct cmd_ch_sensor_lsc_info_legacy); + u8 max_x = h17 ? ISP_LSC_MAX_GRID_X : ISP_LSC_LEGACY_MAX_GRID_X; + u8 max_y = h17 ? ISP_LSC_MAX_GRID_Y : ISP_LSC_LEGACY_MAX_GRID_Y; + u16 max_sz = h17 ? ISP_LSC_MAX_DATA_SIZE : ISP_LSC_LEGACY_MAX_DATA_SIZE; + u8 bd; + int err; + + err = cisp_send_read(isp, &args, wire_size, wire_size); + if (err) + return err; + + bd = h17 ? args.bit_depth : 8; + if (args.num_x < 2 || args.num_x > max_x || + args.num_y < 2 || args.num_y > max_y || + bd < 8 || bd > 16 || args.data_size > max_sz || + args.data_size < (size_t)args.num_x * args.num_y * 4 * (bd >= 9 ? 2 : 1)) + return -EINVAL; + + *num_x = args.num_x; + *num_y = args.num_y; + *data_size = args.data_size; + *bit_depth = bd; + return 0; +} + +int isp_cmd_ch_sensor_lsc_grid_get(struct apple_isp *isp, u32 chan, + struct isp_permodule_lsc *lsc) +{ + bool h17 = isp->hw->fw_abi == ISP_FW_ABI_H17; + size_t hdr_size = h17 ? sizeof(struct cmd_ch_sensor_lsc_grid_h17) : + sizeof(struct cmd_ch_sensor_lsc_grid_legacy); + size_t cmd_size = hdr_size + lsc->data_size; + struct cmd_ch_sensor_lsc_grid_legacy *leg; + struct cmd_ch_sensor_lsc_grid_h17 *ext; + void *buf; + u8 *data; + int err; + + buf = kzalloc(cmd_size, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + leg = buf; + ext = buf; + leg->opcode = CISP_OPCODE(CISP_CMD_CH_SENSOR_PERMODULE_LSC_GRID_GET); + leg->chan = chan; + + err = isp_cmd_send_data(isp, buf, cmd_size, cmd_size, CISP_TIMEOUT); + if (err) + goto out; + + if (!leg->valid) { + err = -ENODATA; + goto out; + } + + data = devm_kmemdup(isp->dev, buf + hdr_size, lsc->data_size, GFP_KERNEL); + if (!data) { + err = -ENOMEM; + goto out; + } + + memcpy(lsc->x_coords, h17 ? ext->x_coords : leg->x_coords, + lsc->num_x * sizeof(u32)); + memcpy(lsc->y_coords, h17 ? ext->y_coords : leg->y_coords, + lsc->num_y * sizeof(u32)); + lsc->data = data; + lsc->valid = true; + +out: + kfree(buf); + return err; +} diff --git a/drivers/media/platform/apple/isp/isp-cmd.h b/drivers/media/platform/apple/isp/isp-cmd.h index ab98f3447d6f7..b7488d08644b6 100644 --- a/drivers/media/platform/apple/isp/isp-cmd.h +++ b/drivers/media/platform/apple/isp/isp-cmd.h @@ -277,6 +277,8 @@ static_assert(sizeof(struct cmd_flicker_sensor_set) == 0xc); int isp_cmd_send(struct apple_isp *isp, void *args, u32 insize, u32 outsize, int timeout); +int isp_cmd_send_data(struct apple_isp *isp, void *args, u32 insize, + u32 outsize, int timeout); int isp_cmd_start(struct apple_isp *isp, u32 mode); int isp_cmd_stop(struct apple_isp *isp, u32 mode); int isp_cmd_stop_cold(struct apple_isp *isp, u32 mode); @@ -831,4 +833,50 @@ int isp_cmd_ch_semantic_video_enable(struct apple_isp *isp, u32 chan, u32 enable); int isp_cmd_ch_semantic_awb_enable(struct apple_isp *isp, u32 chan, u32 enable); +struct cmd_ch_sensor_lsc_info_legacy { + u64 opcode; + u32 chan; + u8 num_x; + u8 num_y; + u16 data_size; +} __packed; +static_assert(sizeof(struct cmd_ch_sensor_lsc_info_legacy) == 0x10); + +struct cmd_ch_sensor_lsc_info_h17 { + u64 opcode; + u32 chan; + u8 num_x; + u8 num_y; + u16 data_size; + u8 bit_depth; + u8 pad_11[3]; +} __packed; +static_assert(sizeof(struct cmd_ch_sensor_lsc_info_h17) == 0x14); + +struct cmd_ch_sensor_lsc_grid_legacy { + u64 opcode; + u32 chan; + u8 valid; + u8 pad_d[3]; + u32 x_coords[ISP_LSC_LEGACY_MAX_GRID_X]; + u32 y_coords[ISP_LSC_LEGACY_MAX_GRID_Y]; +} __packed; +static_assert(sizeof(struct cmd_ch_sensor_lsc_grid_legacy) == 0xb0); + +struct cmd_ch_sensor_lsc_grid_h17 { + u64 opcode; + u32 chan; + u8 valid; + u8 pad_d[3]; + u32 x_coords[ISP_LSC_MAX_GRID_X]; + u32 y_coords[ISP_LSC_MAX_GRID_Y]; +} __packed; +static_assert(sizeof(struct cmd_ch_sensor_lsc_grid_h17) == 0x114); + +int isp_cmd_ch_sensor_lsc_info_get(struct apple_isp *isp, u32 chan, + u8 *num_x, u8 *num_y, u16 *data_size, + u8 *bit_depth); +int isp_cmd_ch_sensor_lsc_grid_get(struct apple_isp *isp, u32 chan, + struct isp_permodule_lsc *lsc); + #endif /* __ISP_CMD_H__ */ diff --git a/drivers/media/platform/apple/isp/isp-drv.h b/drivers/media/platform/apple/isp/isp-drv.h index b3dccaf9a7508..fc16196ed5a99 100644 --- a/drivers/media/platform/apple/isp/isp-drv.h +++ b/drivers/media/platform/apple/isp/isp-drv.h @@ -360,6 +360,24 @@ struct isp_format { size_t total_size; }; +#define ISP_LSC_LEGACY_MAX_GRID_X 23 +#define ISP_LSC_LEGACY_MAX_GRID_Y 17 +#define ISP_LSC_LEGACY_MAX_DATA_SIZE 1564 +#define ISP_LSC_MAX_GRID_X 27 +#define ISP_LSC_MAX_GRID_Y 38 +#define ISP_LSC_MAX_DATA_SIZE 0x2010 + +struct isp_permodule_lsc { + bool valid; + u8 num_x; + u8 num_y; + u8 bit_depth; + u16 data_size; + u32 x_coords[ISP_LSC_MAX_GRID_X]; + u32 y_coords[ISP_LSC_MAX_GRID_Y]; + u8 *data; +}; + struct apple_isp { struct device *dev; const struct apple_isp_hw *hw; @@ -381,6 +399,7 @@ struct apple_isp { int num_channels; struct isp_format fmts[ISP_MAX_CHANNELS]; + struct isp_permodule_lsc lsc; unsigned int current_ch; struct video_device vdev; diff --git a/tools/asahi/isp-tests.py b/tools/asahi/isp-tests.py index e649d3d83cbd6..34b717c7da335 100644 --- a/tools/asahi/isp-tests.py +++ b/tools/asahi/isp-tests.py @@ -53,6 +53,9 @@ def main(): # #1445 sends through a per-channel helper; the latch guards it too. if 'static int isp_cmd_send_chan(' in inputs['isp-cmd.c']: groups['isp-cmd.c'] = ['isp_cmd_send_chan'] + groups['isp-cmd.c'] + # The per-channel helper may wrap a buffer-level sender that owns the latch. + if 'static int isp_cmd_send_buf(' in inputs['isp-cmd.c']: + groups['isp-cmd.c'] = ['isp_cmd_send_buf'] + groups['isp-cmd.c'] if args.suite == 'session': groups['isp-ipc.c'] += ['chan_rx_done', 'ipc_chan_handle'] groups['isp-fw.c'] += ['apple_isp_isr_thread'] @@ -69,10 +72,13 @@ def main(): guard = 'if (READ_ONCE(isp->ipc_error))\n\t\treturn READ_ONCE(isp->ipc_error);' if guard not in bodies['isp_cmd_send']: raise RuntimeError('overwrite negative control no longer matches its guard') - for name in ('isp_cmd_send', 'isp_cmd_send_chan'): + # The lowest sender holds the latch; a wrapper above it may not. + sender = next(n for n in ('isp_cmd_send_buf', 'isp_cmd_send_chan', + 'isp_cmd_send') if n in bodies) + if guard not in bodies[sender]: + raise RuntimeError('overwrite negative control no longer matches ' + sender) + for name in ('isp_cmd_send', 'isp_cmd_send_chan', 'isp_cmd_send_buf'): if name in bodies: - if guard not in bodies[name]: - raise RuntimeError('overwrite negative control no longer matches ' + name) bodies[name] = bodies[name].replace(guard, '') elif args.control == 'stop': bodies['apple_isp_stop_capture'] = bodies['apple_isp_stop_capture'].replace( diff --git a/tools/asahi/isp/session.c b/tools/asahi/isp/session.c index ddf524257c135..cba9a3f88257e 100644 --- a/tools/asahi/isp/session.c +++ b/tools/asahi/isp/session.c @@ -11,6 +11,7 @@ typedef uint64_t u64; typedef uint32_t u32; +typedef u64 dma_addr_t; #define READ_ONCE(x) (x) #define WRITE_ONCE(x, v) ((x) = (v)) #define cmpxchg(p, old, v) __sync_val_compare_and_swap(p, old, v)