Skip to content
Closed
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
2 changes: 2 additions & 0 deletions drivers/gpu/drm/apple/dcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ bool dcp_crtc_needs_route_start(struct apple_dcp *dcp);
bool dcp_crtc_route_ready(struct drm_crtc *crtc, struct drm_atomic_commit *commit,
bool fresh);
bool dcp_has_mode(struct apple_dcp *dcp, const struct drm_display_mode *mode);
bool dcp_mode_panel_view(struct apple_dcp *dcp, const struct drm_display_mode *mode,
struct dcp_panel_view *view);
bool dcp_crtc_mode_fixup(struct drm_crtc *crtc,
const struct drm_display_mode *mode,
struct drm_display_mode *adjusted_mode);
Expand Down
266 changes: 265 additions & 1 deletion drivers/gpu/drm/apple/iomfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <linux/iommu.h>
#include <linux/kref.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/power_supply.h>
#include <linux/ratelimit.h>
Expand Down Expand Up @@ -2352,6 +2353,246 @@ bool dcp_modes_for_connector(struct apple_dcp *dcp,
return dcp->modes_admitted && READ_ONCE(dcp->connector) == connector;
}

static bool panel_views = true;
module_param(panel_views, bool, 0444);
MODULE_PARM_DESC(panel_views,
"Offer modes smaller than the internal panel, shown on a part of it (default: on)");

#define DCP_PANEL_VIEW_MAX_SIZES 8

struct dcp_panel_view_size {
u32 w;
u32 h;
};

static bool dcp_is_j716c(void)
{
static int j716c = -1;

if (j716c < 0) {
static const char *const match[] = { "apple,j716c", NULL };

j716c = of_machine_compatible_match(match) ? 1 : 0;
}
return j716c == 1;
}

static bool dcp_panel_views_enabled(struct apple_dcp *dcp)
{
return panel_views &&
dcp->fw_compat == DCP_FIRMWARE_V_27_0 &&
dcp->fixed_connector_type == DRM_MODE_CONNECTOR_eDP &&
!dcp->notch_height &&
dcp_is_j716c();
}

static u32 dcp_panel_view_notch(struct apple_dcp *dcp)
{
if (dcp->notch_height)
return dcp->notch_height;
return dcp_is_j716c() ? 74 : 0;
}

static bool dcp_panel_view_fit(u32 panel_w, u32 panel_h, u32 notch,
u32 mode_w, u32 mode_h,
struct dcp_panel_view *view)
{
u32 avail_h, dst_w, dst_h;
u64 a, b;

if (!panel_w || !panel_h || panel_h <= notch || !mode_w || !mode_h)
return false;
if (mode_w == panel_w && mode_h == panel_h)
return false;

avail_h = panel_h - notch;
if (mode_w > panel_w || mode_h > avail_h)
return false;

a = (u64)mode_w * avail_h;
b = (u64)mode_h * panel_w;
if (a < b) {
dst_w = min_t(u64, DIV_ROUND_CLOSEST_ULL(a, mode_h), panel_w);
dst_h = avail_h;
} else {
dst_w = panel_w;
dst_h = min_t(u64, DIV_ROUND_CLOSEST_ULL(b, mode_w), avail_h);
}

if (dst_w > 2 * mode_w || dst_h > 2 * mode_h)
return false;

if (view) {
view->mode_w = mode_w;
view->mode_h = mode_h;
view->dst_x = (panel_w - dst_w) / 2;
view->dst_y = max(notch, (panel_h - dst_h) / 2);
view->dst_w = dst_w;
view->dst_h = dst_h;
}
return true;
}

static unsigned int dcp_panel_view_sizes(u32 panel_w, u32 panel_h, u32 notch,
struct dcp_panel_view_size out[DCP_PANEL_VIEW_MAX_SIZES])
{
static const struct dcp_panel_view_size common[] = {
{ 2560, 1600 },
{ 2560, 1440 },
{ 1920, 1200 },
{ 1920, 1080 },
{ 1728, 1080 },
};
struct dcp_panel_view_size cand[DCP_PANEL_VIEW_MAX_SIZES];
unsigned int ncand = 0, nout = 0, i, j;

if (notch && panel_h > notch) {
cand[ncand].w = panel_w;
cand[ncand].h = panel_h - notch;
ncand++;
}
if ((panel_w & 0xf) == 0) {
cand[ncand].w = panel_w;
cand[ncand].h = (panel_w >> 1) + (panel_w >> 4);
ncand++;
}
for (i = 0; i < ARRAY_SIZE(common) && ncand < ARRAY_SIZE(cand); i++)
cand[ncand++] = common[i];

for (i = 0; i < ncand && nout < DCP_PANEL_VIEW_MAX_SIZES; i++) {
if (!dcp_panel_view_fit(panel_w, panel_h, notch,
cand[i].w, cand[i].h, NULL))
continue;
for (j = 0; j < nout; j++) {
if (out[j].w == cand[i].w && out[j].h == cand[i].h)
break;
}
if (j == nout)
out[nout++] = cand[i];
}
return nout;
}

static bool dcp_panel_view_offered(u32 panel_w, u32 panel_h, u32 notch,
u32 mode_w, u32 mode_h)
{
struct dcp_panel_view_size sizes[DCP_PANEL_VIEW_MAX_SIZES];
unsigned int n, i;

n = dcp_panel_view_sizes(panel_w, panel_h, notch, sizes);
for (i = 0; i < n; i++) {
if (sizes[i].w == mode_w && sizes[i].h == mode_h)
return true;
}
return false;
}

static int dcp_panel_view_native(struct apple_dcp *dcp,
const struct dcp_display_mode *modes,
unsigned int count)
{
int best = -1;
u64 best_area = 0;
unsigned int i;

for (i = 0; i < count; i++) {
const struct drm_display_mode *m = &modes[i].mode;
u64 area;

if (modes[i].automatic_rgb8_quirk)
continue;
if (dcp->hw.only_120hz_modes && drm_mode_vrefresh(m) != 120)
continue;
area = (u64)m->hdisplay * m->vdisplay;
if (best < 0 || area > best_area) {
best = i;
best_area = area;
}
}
return best;
}

static int dcp_panel_view_lookup(struct apple_dcp *dcp,
const struct dcp_display_mode *modes,
unsigned int count,
const struct drm_display_mode *mode)
{
const struct drm_display_mode *base;
int best;

if (!dcp_panel_views_enabled(dcp) || !count)
return -1;

best = dcp_panel_view_native(dcp, modes, count);
if (best < 0)
return -1;

base = &modes[best].mode;
if (mode->clock != base->clock ||
mode->hsync_start != base->hsync_start ||
mode->hsync_end != base->hsync_end ||
mode->htotal != base->htotal ||
mode->hskew != base->hskew ||
mode->vsync_start != base->vsync_start ||
mode->vsync_end != base->vsync_end ||
mode->vtotal != base->vtotal ||
mode->vscan != base->vscan)
return -1;

if (!dcp_panel_view_offered(base->hdisplay, base->vdisplay,
dcp_panel_view_notch(dcp),
mode->hdisplay, mode->vdisplay))
return -1;

return best;
}

static unsigned int dcp_panel_view_add_modes(struct apple_dcp *dcp,
struct drm_connector *connector,
const struct dcp_display_mode *modes,
unsigned int nr_modes)
{
const struct drm_display_mode *base;
struct dcp_panel_view_size views[DCP_PANEL_VIEW_MAX_SIZES];
unsigned int n_views, added = 0, j;
int base_idx;

if (!dcp_panel_views_enabled(dcp))
return 0;

base_idx = dcp_panel_view_native(dcp, modes, nr_modes);
if (base_idx < 0)
return 0;

base = &modes[base_idx].mode;
n_views = dcp_panel_view_sizes(base->hdisplay, base->vdisplay,
dcp_panel_view_notch(dcp), views);
for (j = 0; j < n_views; j++) {
struct drm_display_mode *mode = drm_mode_duplicate(connector->dev, base);

if (!mode)
break;
mode->hdisplay = views[j].w;
mode->vdisplay = views[j].h;
mode->type &= ~DRM_MODE_TYPE_PREFERRED;
drm_mode_set_name(mode);
drm_mode_probed_add(connector, mode);
added++;
}
return added;
}

static const struct dcp_display_mode *
dcp_panel_view_find(struct apple_dcp *dcp,
const struct dcp_display_mode *modes,
unsigned int count,
const struct drm_display_mode *mode)
{
int pv = dcp_panel_view_lookup(dcp, modes, count, mode);

return pv >= 0 ? &modes[pv] : NULL;
}

/*
* Retained arrays are not freed while mode readers can still refer to them.
* With @connector, only the connector that owns the admitted catalog sees it.
Expand Down Expand Up @@ -3013,6 +3254,8 @@ int dcp_get_modes(struct drm_connector *connector)
}
vrr_capable |= modes[i].vrr;
}
if (published)
published += dcp_panel_view_add_modes(dcp, connector, modes, nr_modes);
drm_connector_set_vrr_capable_property(connector, vrr_capable);
modes_done:
mutex_unlock(&dcp->dcpavserv.lock);
Expand Down Expand Up @@ -3605,7 +3848,7 @@ dcp_find_mode(struct apple_dcp *dcp, const struct drm_display_mode *mode)
}
}

return NULL;
return dcp_panel_view_find(dcp, modes, count, mode);
}

/* The user may own drm_display_mode, so we need to search for our copy */
Expand All @@ -3622,6 +3865,27 @@ bool lookup_mode(struct apple_dcp *dcp, const struct drm_display_mode *mode,
return true;
}

bool dcp_mode_panel_view(struct apple_dcp *dcp, const struct drm_display_mode *mode,
struct dcp_panel_view *view)
{
struct dcp_display_mode *modes;
unsigned int count;
int best;

if (!dcp_panel_views_enabled(dcp))
return false;

modes = dcp_modes_snapshot(dcp, NULL, &count);
best = dcp_panel_view_lookup(dcp, modes, count, mode);
if (best < 0)
return false;

return dcp_panel_view_fit(modes[best].mode.hdisplay,
modes[best].mode.vdisplay,
dcp_panel_view_notch(dcp),
mode->hdisplay, mode->vdisplay, view);
}

/*
* H17P keeps the mode the bootloader programmed and never sends
* set_digital_out_mode (see DCP_INHERIT_BOOT_MODE in iomfb_template.c), so a
Expand Down
10 changes: 10 additions & 0 deletions drivers/gpu/drm/apple/iomfb_plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ struct dcp_surface {
u32 unk_denom;
} __packed;

/* Scaled sub-rectangle on an internal panel backing a smaller DRM mode. */
struct dcp_panel_view {
u32 mode_w;
u32 mode_h;
u32 dst_x;
u32 dst_y;
u32 dst_w;
u32 dst_h;
};

#endif /* __APPLE_IOMFB_PLANE_H__ */
38 changes: 35 additions & 3 deletions drivers/gpu/drm/apple/plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,12 @@ static int apple_plane_atomic_check(struct drm_plane *plane,
if (IS_ERR(crtc_state))
return PTR_ERR(crtc_state);

struct apple_plane_state *apple_state = to_apple_plane_state(new_plane_state);
struct platform_device *pdev = to_apple_crtc(new_plane_state->crtc)->dcp;
struct apple_dcp *target = pdev ? platform_get_drvdata(pdev) : NULL;
int min_scale = FRAC_16_16(1, 2);

{
struct platform_device *pdev = to_apple_crtc(new_plane_state->crtc)->dcp;
struct apple_dcp *target = pdev ? platform_get_drvdata(pdev) : NULL;
struct drm_framebuffer *fb = new_plane_state->fb;

if (target && dcp_is_ext26(target) && fb && !iomfb_ext26_backing_fresh(target, fb))
Expand All @@ -301,6 +304,19 @@ static int apple_plane_atomic_check(struct drm_plane *plane,
return -EINVAL;
}

if (target && dcp_mode_panel_view(target, &crtc_state->mode, &apple_state->panel_view)) {
const struct dcp_panel_view *v = &apple_state->panel_view;
u64 sx = DIV_ROUND_UP_ULL((u64)v->dst_w << 15, v->mode_w);
u64 sy = DIV_ROUND_UP_ULL((u64)v->dst_h << 15, v->mode_h);

min_scale = (int)max_t(u64, sx, sy);
apple_state->has_panel_view =
v->dst_x || v->dst_y || v->dst_w != v->mode_w || v->dst_h != v->mode_h;
} else {
memset(&apple_state->panel_view, 0, sizeof(apple_state->panel_view));
apple_state->has_panel_view = false;
}

/*
* DCP limits downscaling to 2x and upscaling to 4x. Attempting to
* scale outside these bounds errors out when swapping.
Expand All @@ -314,7 +330,7 @@ static int apple_plane_atomic_check(struct drm_plane *plane,
* hardware will fill in zeroes (black).
*/
ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
FRAC_16_16(1, 2),
min_scale,
FRAC_16_16(4, 1),
true, true);
if (ret < 0)
Expand Down Expand Up @@ -508,6 +524,20 @@ static void apple_plane_atomic_update(struct drm_plane *plane,

new_state->src_rect = drm_to_dcp_rect_fp(&base->src);
new_state->dst_rect = drm_to_dcp_rect(&base->dst);
if (new_state->has_panel_view) {
const struct dcp_panel_view *v = &new_state->panel_view;
u32 x1 = DIV_ROUND_CLOSEST_ULL((u64)base->dst.x1 * v->dst_w, v->mode_w);
u32 x2 = DIV_ROUND_CLOSEST_ULL((u64)base->dst.x2 * v->dst_w, v->mode_w);
u32 y1 = DIV_ROUND_CLOSEST_ULL((u64)base->dst.y1 * v->dst_h, v->mode_h);
u32 y2 = DIV_ROUND_CLOSEST_ULL((u64)base->dst.y2 * v->dst_h, v->mode_h);

new_state->dst_rect = (struct dcp_rect){
.x = v->dst_x + x1,
.y = v->dst_y + y1,
.w = x2 - x1,
.h = y2 - y1,
};
}

new_state->surf = (struct dcp_surface){
.is_premultiplied = is_premultiplied,
Expand Down Expand Up @@ -687,6 +717,8 @@ apple_plane_duplicate_state(struct drm_plane *plane)
apple_plane_state->src_rect = old_apple_plane_state->src_rect;
apple_plane_state->dst_rect = old_apple_plane_state->dst_rect;
apple_plane_state->iova = old_apple_plane_state->iova;
apple_plane_state->panel_view = old_apple_plane_state->panel_view;
apple_plane_state->has_panel_view = old_apple_plane_state->has_panel_view;

return &apple_plane_state->base;
}
Expand Down
Loading
Loading