diff --git a/drivers/gpu/drm/apple/dcp.h b/drivers/gpu/drm/apple/dcp.h index 5ec4977996ad4..ed6facaf9772b 100644 --- a/drivers/gpu/drm/apple/dcp.h +++ b/drivers/gpu/drm/apple/dcp.h @@ -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); diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index cbc093e132fc8..7fde5687c65b0 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -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. @@ -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); @@ -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 */ @@ -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 diff --git a/drivers/gpu/drm/apple/iomfb_plane.h b/drivers/gpu/drm/apple/iomfb_plane.h index b7b22073eded4..6b931c8c07689 100644 --- a/drivers/gpu/drm/apple/iomfb_plane.h +++ b/drivers/gpu/drm/apple/iomfb_plane.h @@ -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__ */ diff --git a/drivers/gpu/drm/apple/plane.c b/drivers/gpu/drm/apple/plane.c index e21e626ddee88..7028c9c25cc2f 100644 --- a/drivers/gpu/drm/apple/plane.c +++ b/drivers/gpu/drm/apple/plane.c @@ -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)) @@ -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. @@ -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) @@ -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, @@ -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; } diff --git a/drivers/gpu/drm/apple/plane.h b/drivers/gpu/drm/apple/plane.h index ae516b3b0b7e3..64971f1893836 100644 --- a/drivers/gpu/drm/apple/plane.h +++ b/drivers/gpu/drm/apple/plane.h @@ -32,6 +32,8 @@ struct apple_plane_state { struct dcp_rect dst_rect; u64 iova; struct dcp_fb_reference *retirement; + struct dcp_panel_view panel_view; + bool has_panel_view; }; #define to_apple_plane_state(x) container_of(x, struct apple_plane_state, base) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/plane-layout.py b/tools/testing/selftests/drivers/gpu/apple-dcp/plane-layout.py index c00431b1e9b53..78681160da211 100755 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/plane-layout.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/plane-layout.py @@ -70,6 +70,8 @@ def unit(root, ref=None, mutation=None, callback_ref=None): #define check_add_overflow(a,b,c) __builtin_add_overflow((a),(b),(c)) #define DIV_ROUND_UP(n,d) (((n)+(d)-1)/(d)) #define DIV_ROUND_UP_ULL(n,d) (((u64)(n)+(d)-1)/(d)) +#define DIV_ROUND_CLOSEST_ULL(n,d) (((u64)(n)+((u64)(d)/2))/(u64)(d)) +#define max_t(type, x, y) ({ type _x = (x); type _y = (y); _x > _y ? _x : _y; }) #define ALIGN(n,a) (((n)+(a)-1)&~((a)-1)) #define roundup_pow_of_two(n) ((n) == 1 ? 1UL : 1UL << (64-__builtin_clzl((unsigned long)(n)-1))) #define fourcc_code(a,b,c,d) ((u32)(a)|((u32)(b)<<8)|((u32)(c)<<16)|((u32)(d)<<24)) @@ -130,6 +132,7 @@ def unit(root, ref=None, mutation=None, callback_ref=None): static struct apple_dcp *platform_get_drvdata(struct platform_device *pdev) { return NULL; } static bool dcp_is_ext26(struct apple_dcp *dcp) { return false; } static bool iomfb_ext26_backing_fresh(struct apple_dcp *dcp,struct drm_framebuffer *fb) { return true; } +static bool __attribute__((unused)) dcp_mode_panel_view(struct apple_dcp *dcp, const void *mode, void *view) { (void)dcp; (void)mode; (void)view; return false; } #define to_apple_plane_state(p) container_of(p,struct apple_plane_state,base) static struct drm_crtc_state crtc_state = { .mode = { 3840,2160 } }; static struct drm_plane_state *drm_atomic_get_new_plane_state(struct drm_atomic_commit *c,struct drm_plane *p) { return p->state; } diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py b/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py index 30eba776e8eee..b890bad04847a 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py @@ -1018,6 +1018,8 @@ def publication(root): { (void)c;assert(d->fw_compat != DCP_FIRMWARE_V_26_6_DESKTOP); } '''+firmware_lineage(root)+internal_inline(root,'dcp_fw_is_14_7')+r'''/* Off T6030 machines the direct crossbar output is not clocked separately. */ static int __attribute__((unused)) dcp_direct_crossbar_link(struct apple_dcp *d,bool up) { (void)d;(void)up;return 0; } +static unsigned int __attribute__((unused)) dcp_panel_view_add_modes(struct apple_dcp *d,struct drm_connector *c,const struct dcp_display_mode *m,unsigned int n) { (void)d;(void)c;(void)m;(void)n;return 0; } +static const struct dcp_display_mode * __attribute__((unused)) dcp_panel_view_find(struct apple_dcp *d,const struct dcp_display_mode *m,unsigned int n,const struct drm_display_mode *mode) { (void)d;(void)m;(void)n;(void)mode;return NULL; } ''' transport = '\n'.join(block(av,marker) for marker in [ 'static void dcpavserv_notify_edid(', 'static bool dcpavserv_hdr_class(',