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
92 changes: 87 additions & 5 deletions src/afk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "afk.h"
#include "assert.h"
#include "malloc.h"
#include "memory.h"
#include "string.h"
#include "utils.h"

Expand Down Expand Up @@ -631,6 +632,8 @@ static int afkv2_copy_response(afk_epic_ep_t *epic, void *payload, size_t size,
return -1;
payload = epic->rxbuf.bfr;
size = oob->rxlen;
/* The IOP wrote it: drop any stale cached copy before reading */
dc_ivac_range(payload, size);
dma_rmb();
}
}
Expand All @@ -643,10 +646,9 @@ static int afkv2_copy_response(afk_epic_ep_t *epic, void *payload, size_t size,
return 0;
}

/* Until the native PHY callbacks are implemented, complete peer commands with
* kIOReturnUnsupported. The firmware accepts an empty response; for OOB
* commands retain the descriptor addresses but return zero bytes. Never map or
* dereference a firmware-supplied address merely to reject an operation. */
/* Complete a peer command this side does not take with kIOReturnUnsupported. The firmware
* accepts an empty response; for OOB commands retain the descriptor addresses but return zero
* bytes. Never map or dereference a firmware-supplied address merely to reject an operation. */
static int afkv2_reject_command(afk_epic_ep_t *epic, const struct afkv2_hdr *hdr,
const struct afkv2_msg *msg, const void *payload, size_t size)
{
Expand Down Expand Up @@ -679,14 +681,92 @@ static int afkv2_reject_command(afk_epic_ep_t *epic, const struct afkv2_hdr *hdr
return afk_epic_tx(epic, 0, 0, &reply, length);
}

/*
* The IOP's own service call to us with its request and reply in its buffers (flags bit 0): the
* request at txbuf, the reply to rxbuf, each named with its length and bounded by the command's
* capacity (value). Both buffers are the IOP's, mapped in its DART; each is used only when every
* page of it lands in RAM, and nothing is mapped for it. The status goes back in the response's
* value, with the descriptor; a failed call returns no bytes.
*/
#define AFKV2_OOB_MAX SZ_16K

static int afkv2_handle_oob_command(afk_epic_ep_t *epic, const struct afkv2_hdr *hdr,
const struct afkv2_msg *msg, const void *payload, size_t size,
afk_epic_service_t *service)
{
const struct afkv2_cmd *cmd = payload;
if (size != sizeof(*cmd) + sizeof(struct afkv2_oob))
return afkv2_reject_command(epic, hdr, msg, payload, size);
const struct afkv2_oob *oob = (const void *)(cmd + 1);
rtkit_dev_t *rtk = epic->afk->rtk;

if (oob->txlen < sizeof(struct afkv2_service_call) ||
oob->rxlen < sizeof(struct afkv2_service_call) || oob->txlen > AFKV2_OOB_MAX ||
oob->rxlen > AFKV2_OOB_MAX || oob->rxlen > cmd->value ||
!rtkit_access_remote(rtk, oob->rxbuf, NULL, oob->rxlen, true))
return afkv2_reject_command(epic, hdr, msg, payload, size);

u8 *request = malloc(oob->txlen), *response = calloc(1, oob->rxlen);
if (!request || !response) {
free(request);
free(response);
return -1;
}

u32 status = 0xe00002c7; /* kIOReturnUnsupported */
const struct afkv2_service_call *call = (const void *)request;
if (!rtkit_access_remote(rtk, oob->txbuf, request, oob->txlen, false) ||
call->magic != 0x69706378 || call->length != oob->txlen - sizeof(*call))
goto reply;

struct afkv2_service_call *rcall = (void *)response;
memcpy(rcall, call, sizeof(*call));
rcall->length = oob->rxlen - sizeof(*call);
status = service->ops->call_v2(service, call->group, call->command, call + 1, call->length,
rcall + 1, rcall->length);
if (status)
printf("AFKV2: service callback group %u command %u status %#x\n", call->group,
call->command, status);
else
dprintf("AFKV2: service callback group %u command %u succeeded\n", call->group,
call->command);
if (!status && !rtkit_access_remote(rtk, oob->rxbuf, response, oob->rxlen, true))
status = 0xe00002bc; /* kIOReturnError */

reply:;
struct {
struct afkv2_hdr hdr;
struct afkv2_msg msg;
struct afkv2_cmd cmd;
struct afkv2_oob oob;
} PACKED packet = {
.hdr = {.seq = epic->seq++,
.interface = hdr->interface,
.length = sizeof(struct afkv2_msg) + sizeof(struct afkv2_cmd) + sizeof(*oob)},
.msg = {.token = msg->token, .type = msg->type, .category = AFKV2_RESPONSE},
.cmd = {.flags = 1, .id = cmd->id, .value = status},
.oob = *oob,
};
if (status)
packet.oob.rxlen = 0;
int ret = afk_epic_tx(epic, 0, 0, &packet, sizeof(packet));
free(request);
free(response);
return ret;
}

static int afkv2_handle_command(afk_epic_ep_t *epic, const struct afkv2_hdr *hdr,
const struct afkv2_msg *msg, const void *payload, size_t size)
{
if (size < sizeof(struct afkv2_cmd) || msg->category != AFKV2_COMMAND)
return -1;
const struct afkv2_cmd *cmd = payload;
afk_epic_service_t *service = afk_epic_find_service(epic, hdr->interface);
if (msg->type != SUBTYPE_STD_SERVICE || cmd->flags || !service || !service->ops->call_v2)
if (msg->type != SUBTYPE_STD_SERVICE || !service || !service->ops->call_v2)
return afkv2_reject_command(epic, hdr, msg, payload, size);
if (cmd->flags == 1)
return afkv2_handle_oob_command(epic, hdr, msg, payload, size, service);
if (cmd->flags)
return afkv2_reject_command(epic, hdr, msg, payload, size);

/* Only the observed inline callback form is supported. Validate both
Expand Down Expand Up @@ -753,6 +833,8 @@ static int afkv2_command(afk_epic_ep_t *epic, int channel, u16 type, void *txbuf
.txlen = txsize},
};
memcpy(epic->txbuf.bfr, txbuf, txsize);
/* The IOP reads it from memory, not from this CPU's cache */
dc_cvac_range(epic->txbuf.bfr, txsize);
if (afk_epic_tx(epic, 0, 0, &packet, sizeof(packet)) < 0)
return -1;
u32 usec = rtkit_get_timeout(epic->afk->rtk);
Expand Down
1 change: 1 addition & 0 deletions src/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ static int dcp_hdmi_dptx_init(dcp_dev_t *dcp, const display_config_t *cfg)
printf("dcp: failed to find dp2hdmi-gpio node '%s'\n", cfg->dp2hdmi_gpio);
return -1;
}
dcp->dp2hdmi = true;
int hpd = adt_gpio_function_pin(adt, node, "function-hdmi_hpd", &dcp->hpd_pin);
dcp->hpd_on_ap = hpd <= 0;
if (hpd)
Expand Down
1 change: 1 addition & 0 deletions src/dcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef struct dcp_dev {
u32 dp2hdmi_pwr_gpio;
u32 hdmi_pwr_gpio;
bool hpd_on_ap; /* the converter's HPD reaches an AP gpio (ADT function-hdmi_hpd) */
bool dp2hdmi; /* the dptx-phy feeds a DP-to-HDMI converter (cfg->dp2hdmi_gpio) */
struct adt_gpio_pin hpd_pin; /* that gpio, reg 0 when it is not an AP-visible pin */
bool hdmi_5v_was_on; /* the connector's 5 V was on before this stage switched it */
} dcp_dev_t;
Expand Down
145 changes: 134 additions & 11 deletions src/dcp/dptx_port_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
#include "../utils.h"

#define DCP_DPTX_PORT_ENDPOINT 0x2a
/*
* Firmware that announces no 0x2a hosts the dptx-port service on 0x2c instead (the T6050 DCP's
* firmware 27 does), and speaks a later message layout there: connection and hotplug replies
* carry no status word, the requested display comes back without one, and the firmware's own
* calls include its link record calls (group 1), drive settings in a longer record and
* single-pipe tiling hints. The endpoint the firmware announced is what selects it.
*/
#define DCP_DPTX_PORT_ENDPOINT_V27 0x2c

#define TXBUF_LEN 0x4000
#define RXBUF_LEN 0x4000
Expand Down Expand Up @@ -92,17 +100,23 @@ typedef struct dptx_port {
afk_epic_service_t *service;
dptx_phy_t *phy;
u32 link_rate, pending_link_rate;
dptx_drive_settings_t drive[4];
dptx_drive_settings_t drive[4]; /* v27: the lanes' settings as last set */
} dptx_port_t;

typedef struct dcp_dptx_if {
afk_epic_ep_t *epic;
dcp_dev_t *dcp;
dptx_phy_t *phy;
bool v27; /* the service runs on DCP_DPTX_PORT_ENDPOINT_V27 */

struct dptx_port port[2];
} dcp_dptx_if_t;

static bool dptx_v27(afk_epic_service_t *service)
{
return ((dcp_dptx_if_t *)service->intf)->v27;
}

static int afk_service_call(afk_epic_service_t *service, u16 group, u32 command, const void *data,
size_t data_len, size_t data_pad, void *output, size_t output_len,
size_t output_pad)
Expand Down Expand Up @@ -162,10 +176,13 @@ int dptxport_validate_connection(afk_epic_service_t *service, u8 core, u8 atc, u

cmd.target = target;
cmd.unk = 0x100;
ret = afk_service_call(service, 0, 12, &cmd, sizeof(cmd), 40, &resp, sizeof(resp), 16);
if (dptx_v27(service))
ret = afk_service_call(service, 0, 12, &cmd, sizeof(cmd), 40, &resp, 8, 40);
else
ret = afk_service_call(service, 0, 12, &cmd, sizeof(cmd), 40, &resp, sizeof(resp), 16);
if (ret)
return ret;
if (afk_epic_is_v2(service->epic) && resp.status)
if (afk_epic_is_v2(service->epic) && !dptx_v27(service) && resp.status)
return (s32)resp.status;

if (resp.target != target)
Expand All @@ -186,25 +203,32 @@ int dptxport_connect(afk_epic_service_t *service, u8 core, u8 atc, u8 die)
FIELD_PREP(DCPDPTX_REMOTE_PORT_DIE, die) | DCPDPTX_REMOTE_PORT_CONNECTED;

cmd.target = target;
if (afk_epic_is_v2(service->epic))
cmd.unk = 0x100; /* IODPTXPortAttributes supportsHPD */
ret = afk_service_call(service, 0, 11, &cmd, sizeof(cmd), 24, &resp, sizeof(resp), 0);
/* IODPTXPortAttributes supportsHPD. On v27 a port behind a DP-to-HDMI converter does not
* claim it: the AP reports the converter's HPD (dcp_dptx_needs_ap_hpd()). */
if (afk_epic_is_v2(service->epic) &&
!(dptx_v27(service) && ((dcp_dptx_if_t *)service->intf)->dcp->dp2hdmi))
cmd.unk = 0x100;
if (dptx_v27(service))
ret = afk_service_call(service, 0, 11, &cmd, sizeof(cmd), 24, &resp, 8, 24);
else
ret = afk_service_call(service, 0, 11, &cmd, sizeof(cmd), 24, &resp, sizeof(resp), 0);
if (ret)
return ret;
if (afk_epic_is_v2(service->epic) && resp.status)
if (afk_epic_is_v2(service->epic) && !dptx_v27(service) && resp.status)
return (s32)resp.status;

if (resp.target != target)
return -1;
if (resp.unk != 0x100)
/* the firmware echoes the attributes it took */
if (resp.unk != (afk_epic_is_v2(service->epic) ? cmd.unk : 0x100))
return -1;

return 0;
}

int dptxport_request_display(afk_epic_service_t *service)
{
if (afk_epic_is_v2(service->epic)) {
if (afk_epic_is_v2(service->epic) && !dptx_v27(service)) {
u32 status = 0;
int ret = afk_service_call(service, 0, 6, NULL, 0, 16, &status, sizeof(status), 12);
return ret ? ret : (s32)status;
Expand Down Expand Up @@ -235,6 +259,9 @@ int dptxport_set_hpd(afk_epic_service_t *service, bool hpd)
ret = afk_service_call(service, 8, 8, &cmd, sizeof(cmd), 12, &resp, sizeof(resp), 12);
if (ret)
return ret;
/* v27 echoes the state it took, with no status word */
if (dptx_v27(service))
return resp.unk == cmd.unk ? 0 : -1;
if (afk_epic_is_v2(service->epic)) {
u32 status;
memcpy(&status, resp._pad0, sizeof(status));
Expand Down Expand Up @@ -534,9 +561,83 @@ static void dptxport_init(afk_epic_service_t *service, const char *name, const c
}
}

/*
* The v27 firmware's calls whose layout differs (DCP_DPTX_PORT_ENDPOINT_V27). Returns 1 for a
* call this does not take, which the common handler then answers.
*/
static u64 dptxport_call_v27(afk_epic_service_t *service, u16 group, u32 command, const void *data,
size_t data_size, void *reply, size_t reply_size)
{
dptx_port_t *port = service->cookie;

/* Group 1, commands 3 and 4: link records that carry the firmware's own pointers, not lane
* settings. Returned as given, with their result word (at 0 and at 16) and everything past
* the first 32 bytes cleared. */
if (group == 1 && (command == 3 || command == 4)) {
size_t n = min(data_size, reply_size);
size_t result = command == 3 ? 0 : 16;
if (n < result + 4)
return 0xe00002c2; /* kIOReturnBadArgument */
memcpy(reply, data, n);
memset(reply + result, 0, 4);
if (n > 32)
memset(reply + 32, 0, n - 32);
return 0;
}
if (group)
return 1;

switch (command) {
case DPTX_APCALL_SET_TILED_DISPLAY_HINTS:
/* A 160-byte record: the hint flags at 0, the status at 0x90. One pipe, no tiling. */
if (data_size != 160 || reply_size < 160)
return 0xe00002c2;
memcpy(reply, data, 160);
memset(reply + 0x90, 0, 4);
return 0;
case DPTX_APCALL_SET_DRIVE_SETTINGS:
case DPTX_APCALL_GET_DRIVE_SETTINGS: {
/* The lane count at 16 (set) or 0 (get), the lanes' settings from 32. */
bool set = command == DPTX_APCALL_SET_DRIVE_SETTINGS;
u32 count;
if (!port || !port->phy || data_size < 32 || reply_size < 32)
return 0xe00002c2;
memcpy(&count, data + (set ? 16 : 0), sizeof(count));
if (count > ARRAY_SIZE(port->drive) ||
(set && data_size < 32 + count * sizeof(*port->drive)) ||
reply_size < 32 + count * sizeof(*port->drive))
return 0xe00002c2;
if (set) {
dptx_drive_settings_t settings[ARRAY_SIZE(port->drive)];
memcpy(settings, data + 32, count * sizeof(*settings));
/* DisplayPort levels: voltage swing and pre-emphasis 0..3, their sum at most 3 */
for (u32 i = 0; i < count; i++)
if (settings[i].format || settings[i].voltage > 3 ||
settings[i].pre_emphasis > 3 ||
settings[i].voltage + settings[i].pre_emphasis > 3)
return 0xe00002c2;
if (count && dptx_phy_set_drive_settings(port->phy, settings, count))
return 0xe00002bc; /* kIOReturnError */
memcpy(port->drive, settings, count * sizeof(*settings));
}
memset(reply, 0, reply_size);
memcpy(reply + (set ? 16 : 0), &count, sizeof(count));
memcpy(reply + 32, port->drive, count * sizeof(*port->drive));
return 0;
}
default:
return 1;
}
}

static u32 dptxport_call_v2(afk_epic_service_t *service, u16 group, u32 command, const void *data,
size_t data_size, void *reply, size_t reply_size)
{
if (dptx_v27(service)) {
u64 ret = dptxport_call_v27(service, group, command, data, data_size, reply, reply_size);
if (ret != 1)
return ret;
}
if (group && !(group == 1 && (command == DPTX_APCALL_SET_DRIVE_SETTINGS ||
command == DPTX_APCALL_GET_DRIVE_SETTINGS)))
return 0xe00002c7; /* kIOReturnUnsupported */
Expand Down Expand Up @@ -727,7 +828,17 @@ int dcp_dptx_connect(dcp_dptx_if_t *dptx, dptx_phy_t *phy, u32 die, u32 port)

dptx->port[port].phy = dptx->phy = phy;

int ret = dptxport_connect(dptx->port[port].service, 0, dptx_phy_dcp_output(phy), die);
int ret;
/* v27 checks the route before it takes the connection */
if (dptx->v27) {
ret = dptxport_validate_connection(dptx->port[port].service, 0, dptx_phy_dcp_output(phy),
die);
if (ret) {
printf("DPTXPort: v27 route to output %u refused: %d\n", dptx_phy_dcp_output(phy), ret);
return ret;
}
}
ret = dptxport_connect(dptx->port[port].service, 0, dptx_phy_dcp_output(phy), die);
if (ret)
return ret;
return dptxport_request_display(dptx->port[port].service);
Expand Down Expand Up @@ -756,14 +867,26 @@ bool dcp_dptx_is_afkv2(const dcp_dptx_if_t *dptx)
return dptx && afk_epic_is_v2(dptx->epic);
}

/* The dptx-port service's endpoint: 0x2a unless the firmware announced only 0x2c. */
static u32 dcp_dptx_endpoint(rtkit_dev_t *rtk, bool *v27)
{
*v27 = !rtkit_has_endpoint(rtk, DCP_DPTX_PORT_ENDPOINT) &&
rtkit_has_endpoint(rtk, DCP_DPTX_PORT_ENDPOINT_V27);
if (!*v27)
return DCP_DPTX_PORT_ENDPOINT;
printf("dcp-dptx: dptx-port on endpoint %#x (v27 layout)\n", DCP_DPTX_PORT_ENDPOINT_V27);
return DCP_DPTX_PORT_ENDPOINT_V27;
}

dcp_dptx_if_t *dcp_dptx_init(dcp_dev_t *dcp, u32 num_dptxports)
{
dcp_dptx_if_t *dptx = calloc(1, sizeof(dcp_dptx_if_t));
if (!dptx)
return NULL;

dptx->dcp = dcp;
dptx->epic = afk_epic_start_ep(dcp->afk, DCP_DPTX_PORT_ENDPOINT, dcp_dptx_ops, true);
u32 ep = dcp_dptx_endpoint(dcp->rtkit, &dptx->v27);
dptx->epic = afk_epic_start_ep(dcp->afk, ep, dcp_dptx_ops, true);
if (!dptx->epic) {
printf("dcp-dptx: failed to initialize EPIC\n");
goto err_free;
Expand Down
Loading
Loading