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
35 changes: 33 additions & 2 deletions Documentation/devicetree/bindings/display/apple,dcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,13 @@ properties:

apple,t8122-dcpext-firmware-ready:
$ref: /schemas/types.yaml#/definitions/uint32
const: 1
description: All external firmware reservations were committed by the loader.
enum: [1, 2]
description:
Set by the boot loader on a T8122 external coprocessor whose firmware
it has reserved. 2 is the boot loader's default. 1 is set only when
the owner asked for the coprocessor in the boot loader configuration,
because kernels that share the display DART unconditionally accept 1
and nothing else.

apple,pmp:
$ref: /schemas/types.yaml#/definitions/phandle
Expand Down Expand Up @@ -558,6 +563,32 @@ properties:
- iommus
additionalProperties: false

apple,no-shared-display-dart:
type: boolean
description:
The external coprocessor's display DART must stay out of the display
subsystem's IOMMU domain. Framebuffers are mirrored through the fbdma
child instead, which must be present.

fbdma:
type: object
description:
The display DART stream an external coprocessor scans framebuffers
through, with the display subsystem's DVA window.
properties:
iommus:
maxItems: 1
apple,dma-range:
$ref: /schemas/types.yaml#/definitions/uint64-array
description:
Base and size of the display subsystem's DVA window on this stream.
minItems: 2
maxItems: 2
required:
- iommus
- apple,dma-range
additionalProperties: false

panel:
type: object
properties:
Expand Down
16 changes: 16 additions & 0 deletions arch/arm64/boot/dts/apple/t8122-j615.dts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,19 @@
&port00 {
supports-clkreq;
};

/*
* The dcpext wiring is shared with the J613 (t8122-j613-j615.dtsi). fbdma is
* the display DART stream the dcpext scans framebuffers through, with the
* display subsystem's DVA window: on the J615 the gate must not add
* dispext0_dart to the display subsystem (apple,no-shared-display-dart), so
* appledrm mirrors framebuffers there instead.
*/
&dcpext {
apple,no-shared-display-dart;

fbdma {
iommus = <&dispext0_dart 0>;
apple,dma-range = /bits/ 64 <0x10000000000 0x1000000000>;
};
};
37 changes: 37 additions & 0 deletions drivers/gpu/drm/apple/apple_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,42 @@ static int apple_drm_gem_dumb_create(struct drm_file *file_priv,
return drm_gem_dma_dumb_create_internal(file_priv, drm, args);
}

/*
* A T8122 dcpext mirrors framebuffers into its own display DART domain.
* Remove a buffer's mirror before its pages and its DVA are released.
*/
static void apple_drm_gem_free(struct drm_gem_object *gem)
{
struct drm_crtc *crtc;

drm_for_each_crtc(crtc, gem->dev) {
if (t8132_ext_crtc(crtc))
continue;
dcp_fb_unmirror(to_apple_crtc(crtc)->dcp, to_drm_gem_dma_obj(gem));
}
drm_gem_dma_object_free(gem);
}

static const struct drm_gem_object_funcs apple_drm_gem_funcs = {
.free = apple_drm_gem_free,
.print_info = drm_gem_dma_object_print_info,
.get_sg_table = drm_gem_dma_object_get_sg_table,
.vmap = drm_gem_dma_object_vmap,
.mmap = drm_gem_dma_object_mmap,
.vm_ops = &drm_gem_dma_vm_ops,
};

static struct drm_gem_object *apple_drm_gem_create_object(struct drm_device *drm,
size_t size)
{
struct drm_gem_dma_object *obj = kzalloc_obj(*obj);

if (!obj)
return ERR_PTR(-ENOMEM);
obj->base.funcs = &apple_drm_gem_funcs;
return &obj->base;
}

#ifdef CONFIG_DRM_FBDEV_EMULATION
static int apple_drm_fbdev_probe(struct drm_fb_helper *helper,
struct drm_fb_helper_surface_size *sizes)
Expand Down Expand Up @@ -351,6 +387,7 @@ static const struct drm_driver apple_drm_driver = {
DRM_FBDEV_DMA_DRIVER_OPS,
#endif
.postclose = apple_drm_postclose,
.gem_create_object = apple_drm_gem_create_object,
.name = DRIVER_NAME,
.desc = DRIVER_DESC,
.major = 1,
Expand Down
13 changes: 13 additions & 0 deletions drivers/gpu/drm/apple/dcp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/notifier.h>
#include <linux/xarray.h>
#include <linux/spinlock.h>
#include <linux/mux/consumer.h>
#include <linux/phy/phy.h>
Expand Down Expand Up @@ -577,6 +578,18 @@ struct apple_dcp {
struct platform_device *piodma;
bool piodma_created;
struct iommu_domain *iommu_dom;
/*
* J615 dcpext: a child device on the display DART stream the external
* pipe scans framebuffers through. Framebuffers are mirrored into its
* domain at the display subsystem's addresses. fb_mirrors holds the
* mapped length of every mirrored GEM object, indexed by the object's
* address, until the object is freed; fb_mirror_lock protects it and
* fb_dom.
*/
struct platform_device *fbdma;
struct iommu_domain *fb_dom;
struct xarray fb_mirrors;
struct mutex fb_mirror_lock;
/* which of the nine cumulative A031 notify-client states to send next */
unsigned int a031_step;
struct apple_rtkit *rtk;
Expand Down
174 changes: 174 additions & 0 deletions drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <drm/drm_edid.h>
#include <drm/drm_fb_dma_helper.h>
#include <drm/drm_file.h>
#include <drm/drm_gem_dma_helper.h>
#include <drm/drm_drv.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
Expand Down Expand Up @@ -5705,12 +5706,35 @@ static void dcp_work_backlight_fallback(struct work_struct *work)
dev_warn(dcp->dev, "backlight registration failed: %d\n", ret);
}

/* Drop every framebuffer mirror before the fbdma domain goes away. */
static void dcp_fb_unmirror_all(struct apple_dcp *dcp)
{
unsigned long index;
void *entry;

guard(mutex)(&dcp->fb_mirror_lock);
if (dcp->fb_dom) {
xa_for_each(&dcp->fb_mirrors, index, entry) {
/* An object stays in fb_mirrors only while it is alive. */
struct drm_gem_dma_object *obj = (void *)index;

iommu_unmap(dcp->fb_dom, obj->dma_addr, xa_to_value(entry));
}
}
xa_destroy(&dcp->fb_mirrors);
WRITE_ONCE(dcp->fb_dom, NULL);
}

static void dcp_release_piodma_iommu_dev(struct apple_dcp *dcp)
{
if (dcp->retain_dma) {
WRITE_ONCE(dcp->quiescing, true);
return;
}
dcp_fb_unmirror_all(dcp);
if (dcp->fbdma)
of_platform_device_destroy(&dcp->fbdma->dev, NULL);
dcp->fbdma = NULL;
if (dcp->piodma) {
if (dcp->piodma_created)
of_platform_device_destroy(&dcp->piodma->dev, NULL);
Expand All @@ -5722,6 +5746,151 @@ static void dcp_release_piodma_iommu_dev(struct apple_dcp *dcp)
dcp->piodma_created = false;
}

/*
* J615 dcpext: the external pipe reads framebuffers through its
* own display DART, not the internal one the display subsystem's domain
* covers. An "fbdma" child, on that DART's stream 0 with the display
* subsystem's DVA window, gets a domain the framebuffers are mirrored into.
* Without the node nothing changes. The display gate disables the node when
* it shares that DART with the display subsystem instead, whose domain then
* already maps every framebuffer there.
*/
static int dcp_create_fbdma_dev(struct apple_dcp *dcp)
{
struct device_node *node __free(device_node) = NULL;
struct iommu_domain *dom;
int ret;

if (!dcp->ext_failsafe || dcp->fbdma)
return 0;
node = of_get_available_child_by_name(dcp->dev->of_node, "fbdma");
if (!node)
return 0;
dcp->fbdma = of_platform_device_create(node, NULL, dcp->dev);
if (!dcp->fbdma)
return dev_err_probe(dcp->dev, -ENODEV, "fbdma: could not create %pOF\n", node);
ret = dma_set_mask_and_coherent(&dcp->fbdma->dev, DMA_BIT_MASK(42));
if (!ret)
ret = of_dma_configure(&dcp->fbdma->dev, node, true);
dom = ret ? NULL : iommu_get_domain_for_dev(&dcp->fbdma->dev);
if (IS_ERR_OR_NULL(dom)) {
ret = ret ?: PTR_ERR_OR_ZERO(dom) ?: -ENODEV;
of_platform_device_destroy(&dcp->fbdma->dev, NULL);
dcp->fbdma = NULL;
return dev_err_probe(dcp->dev, ret, "fbdma: no IOMMU domain\n");
}
scoped_guard(mutex, &dcp->fb_mirror_lock)
WRITE_ONCE(dcp->fb_dom, dom);
dev_info(dcp->dev, "fbdma: framebuffers mirrored through %pOF\n", node);
return 0;
}

/*
* Map the pages behind [@iova, @iova + @size) of @src into the fbdma domain
* at the same addresses, read-only. The pages are taken from @src's own
* translations, so this needs no scatterlist: an imported buffer's belongs
* to its exporter. Each step looks up one of @src's smallest pages, so a hole
* cannot hide inside a step; that needs both domains to share that page size.
* Returns 0, or a negative errno with nothing left mapped.
*/
static int dcp_fb_mirror_range(struct apple_dcp *dcp, struct iommu_domain *src,
dma_addr_t iova, size_t size)
{
unsigned long pgsize;
phys_addr_t phys, run_phys = 0;
size_t off, run_len = 0, mapped = 0;
int ret = 0;

if (!src->pgsize_bitmap || !dcp->fb_dom->pgsize_bitmap)
return -EINVAL;
pgsize = 1UL << __ffs(src->pgsize_bitmap);
if (pgsize != 1UL << __ffs(dcp->fb_dom->pgsize_bitmap) ||
!IS_ALIGNED(iova | size, pgsize))
return -EINVAL;
for (off = 0; off <= size && !ret; off += pgsize) {
phys = off < size ? iommu_iova_to_phys(src, iova + off) : 0;
if (run_len && phys && phys == run_phys + run_len) {
run_len += pgsize;
continue;
}
/* The run of physically contiguous pages ends here. */
if (run_len) {
ret = iommu_map(dcp->fb_dom, iova + mapped, run_phys, run_len,
IOMMU_READ, GFP_KERNEL);
if (!ret)
mapped += run_len;
}
if (!ret && off < size && !phys)
ret = -EFAULT;
run_phys = phys;
run_len = pgsize;
}
if (ret && mapped)
iommu_unmap(dcp->fb_dom, iova, mapped);
return ret;
}

/*
* Map @fb's GEM objects into the fbdma domain at their display DVAs. A
* mapping lasts as long as its object and its DVA do: dcp_fb_unmirror()
* removes it when the object is freed.
*/
int dcp_fb_mirror(struct apple_dcp *dcp, struct drm_framebuffer *fb)
{
struct iommu_domain *src;
unsigned int i;

if (!READ_ONCE(dcp->fb_dom))
return 0;
src = iommu_get_domain_for_dev(drm_dev_dma_dev(fb->dev));
guard(mutex)(&dcp->fb_mirror_lock);
if (!dcp->fb_dom || IS_ERR_OR_NULL(src))
return -ENODEV;
for (i = 0; i < fb->format->num_planes; i++) {
struct drm_gem_dma_object *obj = drm_fb_dma_get_gem_obj(fb, i);
unsigned long index = (unsigned long)obj;
int ret;

if (!obj)
return -EINVAL;
if (xa_load(&dcp->fb_mirrors, index))
continue;
ret = dcp_fb_mirror_range(dcp, src, obj->dma_addr, obj->base.size);
if (!ret) {
ret = xa_err(xa_store(&dcp->fb_mirrors, index,
xa_mk_value(obj->base.size), GFP_KERNEL));
if (ret)
iommu_unmap(dcp->fb_dom, obj->dma_addr, obj->base.size);
}
if (ret) {
dev_warn_ratelimited(dcp->dev,
"fbdma: %pad (%zu bytes, %s) not mirrored: %d\n",
&obj->dma_addr, obj->base.size,
obj->sgt ? "imported" : "native", ret);
return ret;
}
}
return 0;
}

/* @obj is being freed: the external pipe must not read its pages any more. */
void dcp_fb_unmirror(struct platform_device *pdev, struct drm_gem_dma_object *obj)
{
struct apple_dcp *dcp;
void *entry;

if (!pdev)
return;
dcp = platform_get_drvdata(pdev);
if (!dcp || !READ_ONCE(dcp->fb_dom))
return;
guard(mutex)(&dcp->fb_mirror_lock);
entry = xa_erase(&dcp->fb_mirrors, (unsigned long)obj);
if (entry && dcp->fb_dom)
iommu_unmap(dcp->fb_dom, obj->dma_addr, xa_to_value(entry));
}
EXPORT_SYMBOL_GPL(dcp_fb_unmirror);

void dcp_retain_framebuffer(struct platform_device *pdev,
struct dcp_fb_reference *entry)
{
Expand Down Expand Up @@ -6915,6 +7084,9 @@ static int dcp_comp_bind_pipe(struct device *dev, struct device *main, void *dat
if (ret)
return dev_err_probe(dev, ret,
"Failed to create PIODMA iommu child device\n");
ret = dcp_create_fbdma_dev(dcp);
if (ret)
goto err_piodma;
if (!dcp->iommu_dom) {
ret = dev_err_probe(dev, -EPROBE_DEFER,
"PIODMA iommu domain is unavailable\n");
Expand Down Expand Up @@ -7883,6 +8055,8 @@ static int dcp_platform_probe_pipe(struct platform_device *pdev)

INIT_LIST_HEAD(&dcp->swapped_out_fbs);
mutex_init(&dcp->swapped_out_fbs_lock);
xa_init(&dcp->fb_mirrors);
mutex_init(&dcp->fb_mirror_lock);
init_completion(&dcp->v27_wake_video);
init_completion(&dcp->v27_wake_hotplug);
ret = dcp_backlight_init(dcp, dev);
Expand Down
3 changes: 3 additions & 0 deletions drivers/gpu/drm/apple/dcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ void dcp_retain_framebuffer(struct platform_device *pdev,
struct dcp_fb_reference *entry);
void dcp_arm_retained_framebuffers(struct apple_dcp *dcp, u32 swap_id);
void dcp_unarm_retained_framebuffers(struct apple_dcp *dcp, u32 swap_id);
int dcp_fb_mirror(struct apple_dcp *dcp, struct drm_framebuffer *fb);
struct drm_gem_dma_object;
void dcp_fb_unmirror(struct platform_device *pdev, struct drm_gem_dma_object *obj);
void dcp_release_retained_framebuffers(struct apple_dcp *dcp, u32 swap_id);
void dcp_release_all_retained_framebuffers(struct apple_dcp *dcp);
bool dcp_is_initialized(struct platform_device *pdev);
Expand Down
6 changes: 4 additions & 2 deletions drivers/gpu/drm/apple/iomfb_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -4598,9 +4598,11 @@ void DCP_FW_NAME(iomfb_flush)(struct apple_dcp *dcp, struct drm_crtc *crtc, stru
l = apl_plane->iomfb_surf;
req->swap.swap_enabled |= BIT(l);

if (!new_state->fb || !new_state->visible) {
if (!new_state->fb || !new_state->visible)
continue;
/* Never submit a surface with no translation in the scanout DART. */
if (dcp_fb_mirror(dcp, new_state->fb))
continue;
}
req->surf_null[l] = false;
has_surface = 1;

Expand Down
Loading