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
108 changes: 108 additions & 0 deletions drivers/media/platform/apple/isp/isp-cam-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
{}
};

Expand Down
39 changes: 39 additions & 0 deletions drivers/media/platform/apple/isp/isp-cam.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
134 changes: 127 additions & 7 deletions drivers/media/platform/apple/isp/isp-cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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))
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
Loading
Loading