Commit 0c407de5 authored by Thierry Reding's avatar Thierry Reding

drm/tegra: Refactor IOMMU attach/detach

Attaching to and detaching from an IOMMU uses the same code sequence in
every driver, so factor it out into separate helpers.
Reviewed-by: default avatarDmitry Osipenko <digetx@gmail.com>
Signed-off-by: default avatarThierry Reding <treding@nvidia.com>
parent 230630bd
...@@ -1837,23 +1837,13 @@ static int tegra_dc_init(struct host1x_client *client) ...@@ -1837,23 +1837,13 @@ static int tegra_dc_init(struct host1x_client *client)
if (!dc->syncpt) if (!dc->syncpt)
dev_warn(dc->dev, "failed to allocate syncpoint\n"); dev_warn(dc->dev, "failed to allocate syncpoint\n");
if (tegra->domain) { dc->group = host1x_client_iommu_attach(client, true);
dc->group = iommu_group_get(client->dev); if (IS_ERR(dc->group)) {
err = PTR_ERR(dc->group);
if (dc->group && dc->group != tegra->group) { dev_err(client->dev, "failed to attach to domain: %d\n", err);
err = iommu_attach_group(tegra->domain, dc->group);
if (err < 0) {
dev_err(dc->dev,
"failed to attach to domain: %d\n",
err);
iommu_group_put(dc->group);
return err; return err;
} }
tegra->group = dc->group;
}
}
if (dc->soc->wgrps) if (dc->soc->wgrps)
primary = tegra_dc_add_shared_planes(drm, dc); primary = tegra_dc_add_shared_planes(drm, dc);
else else
...@@ -1916,15 +1906,7 @@ static int tegra_dc_init(struct host1x_client *client) ...@@ -1916,15 +1906,7 @@ static int tegra_dc_init(struct host1x_client *client)
if (!IS_ERR(primary)) if (!IS_ERR(primary))
drm_plane_cleanup(primary); drm_plane_cleanup(primary);
if (dc->group) { host1x_client_iommu_detach(client, dc->group);
if (dc->group == tegra->group) {
iommu_detach_group(tegra->domain, dc->group);
tegra->group = NULL;
}
iommu_group_put(dc->group);
}
host1x_syncpt_free(dc->syncpt); host1x_syncpt_free(dc->syncpt);
return err; return err;
...@@ -1932,9 +1914,7 @@ static int tegra_dc_init(struct host1x_client *client) ...@@ -1932,9 +1914,7 @@ static int tegra_dc_init(struct host1x_client *client)
static int tegra_dc_exit(struct host1x_client *client) static int tegra_dc_exit(struct host1x_client *client)
{ {
struct drm_device *drm = dev_get_drvdata(client->parent);
struct tegra_dc *dc = host1x_client_to_dc(client); struct tegra_dc *dc = host1x_client_to_dc(client);
struct tegra_drm *tegra = drm->dev_private;
int err; int err;
devm_free_irq(dc->dev, dc->irq, dc); devm_free_irq(dc->dev, dc->irq, dc);
...@@ -1945,15 +1925,7 @@ static int tegra_dc_exit(struct host1x_client *client) ...@@ -1945,15 +1925,7 @@ static int tegra_dc_exit(struct host1x_client *client)
return err; return err;
} }
if (dc->group) { host1x_client_iommu_detach(client, dc->group);
if (dc->group == tegra->group) {
iommu_detach_group(tegra->domain, dc->group);
tegra->group = NULL;
}
iommu_group_put(dc->group);
}
host1x_syncpt_free(dc->syncpt); host1x_syncpt_free(dc->syncpt);
return 0; return 0;
......
...@@ -1114,6 +1114,52 @@ int tegra_drm_unregister_client(struct tegra_drm *tegra, ...@@ -1114,6 +1114,52 @@ int tegra_drm_unregister_client(struct tegra_drm *tegra,
return 0; return 0;
} }
struct iommu_group *host1x_client_iommu_attach(struct host1x_client *client,
bool shared)
{
struct drm_device *drm = dev_get_drvdata(client->parent);
struct tegra_drm *tegra = drm->dev_private;
struct iommu_group *group = NULL;
int err;
if (tegra->domain) {
group = iommu_group_get(client->dev);
if (!group) {
dev_err(client->dev, "failed to get IOMMU group\n");
return ERR_PTR(-ENODEV);
}
if (!shared || (shared && (group != tegra->group))) {
err = iommu_attach_group(tegra->domain, group);
if (err < 0) {
iommu_group_put(group);
return ERR_PTR(err);
}
if (shared && !tegra->group)
tegra->group = group;
}
}
return group;
}
void host1x_client_iommu_detach(struct host1x_client *client,
struct iommu_group *group)
{
struct drm_device *drm = dev_get_drvdata(client->parent);
struct tegra_drm *tegra = drm->dev_private;
if (group) {
if (group == tegra->group) {
iommu_detach_group(tegra->domain, group);
tegra->group = NULL;
}
iommu_group_put(group);
}
}
void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *dma) void *tegra_drm_alloc(struct tegra_drm *tegra, size_t size, dma_addr_t *dma)
{ {
struct iova *alloc; struct iova *alloc;
......
...@@ -110,6 +110,10 @@ int tegra_drm_register_client(struct tegra_drm *tegra, ...@@ -110,6 +110,10 @@ int tegra_drm_register_client(struct tegra_drm *tegra,
struct tegra_drm_client *client); struct tegra_drm_client *client);
int tegra_drm_unregister_client(struct tegra_drm *tegra, int tegra_drm_unregister_client(struct tegra_drm *tegra,
struct tegra_drm_client *client); struct tegra_drm_client *client);
struct iommu_group *host1x_client_iommu_attach(struct host1x_client *client,
bool shared);
void host1x_client_iommu_detach(struct host1x_client *client,
struct iommu_group *group);
int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm); int tegra_drm_init(struct tegra_drm *tegra, struct drm_device *drm);
int tegra_drm_exit(struct tegra_drm *tegra); int tegra_drm_exit(struct tegra_drm *tegra);
......
...@@ -32,7 +32,6 @@ static int gr2d_init(struct host1x_client *client) ...@@ -32,7 +32,6 @@ static int gr2d_init(struct host1x_client *client)
struct tegra_drm_client *drm = host1x_to_drm_client(client); struct tegra_drm_client *drm = host1x_to_drm_client(client);
struct drm_device *dev = dev_get_drvdata(client->parent); struct drm_device *dev = dev_get_drvdata(client->parent);
unsigned long flags = HOST1X_SYNCPT_HAS_BASE; unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
struct tegra_drm *tegra = dev->dev_private;
struct gr2d *gr2d = to_gr2d(drm); struct gr2d *gr2d = to_gr2d(drm);
int err; int err;
...@@ -47,22 +46,14 @@ static int gr2d_init(struct host1x_client *client) ...@@ -47,22 +46,14 @@ static int gr2d_init(struct host1x_client *client)
goto put; goto put;
} }
if (tegra->domain) { gr2d->group = host1x_client_iommu_attach(client, false);
gr2d->group = iommu_group_get(client->dev); if (IS_ERR(gr2d->group)) {
err = PTR_ERR(gr2d->group);
if (gr2d->group) { dev_err(client->dev, "failed to attach to domain: %d\n", err);
err = iommu_attach_group(tegra->domain, gr2d->group);
if (err < 0) {
dev_err(client->dev,
"failed to attach to domain: %d\n",
err);
iommu_group_put(gr2d->group);
goto free; goto free;
} }
}
}
err = tegra_drm_register_client(tegra, drm); err = tegra_drm_register_client(dev->dev_private, drm);
if (err < 0) { if (err < 0) {
dev_err(client->dev, "failed to register client: %d\n", err); dev_err(client->dev, "failed to register client: %d\n", err);
goto detach; goto detach;
...@@ -71,10 +62,7 @@ static int gr2d_init(struct host1x_client *client) ...@@ -71,10 +62,7 @@ static int gr2d_init(struct host1x_client *client)
return 0; return 0;
detach: detach:
if (gr2d->group) { host1x_client_iommu_detach(client, gr2d->group);
iommu_detach_group(tegra->domain, gr2d->group);
iommu_group_put(gr2d->group);
}
free: free:
host1x_syncpt_free(client->syncpts[0]); host1x_syncpt_free(client->syncpts[0]);
put: put:
...@@ -94,14 +82,10 @@ static int gr2d_exit(struct host1x_client *client) ...@@ -94,14 +82,10 @@ static int gr2d_exit(struct host1x_client *client)
if (err < 0) if (err < 0)
return err; return err;
host1x_client_iommu_detach(client, gr2d->group);
host1x_syncpt_free(client->syncpts[0]); host1x_syncpt_free(client->syncpts[0]);
host1x_channel_put(gr2d->channel); host1x_channel_put(gr2d->channel);
if (gr2d->group) {
iommu_detach_group(tegra->domain, gr2d->group);
iommu_group_put(gr2d->group);
}
return 0; return 0;
} }
......
...@@ -42,7 +42,6 @@ static int gr3d_init(struct host1x_client *client) ...@@ -42,7 +42,6 @@ static int gr3d_init(struct host1x_client *client)
struct tegra_drm_client *drm = host1x_to_drm_client(client); struct tegra_drm_client *drm = host1x_to_drm_client(client);
struct drm_device *dev = dev_get_drvdata(client->parent); struct drm_device *dev = dev_get_drvdata(client->parent);
unsigned long flags = HOST1X_SYNCPT_HAS_BASE; unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
struct tegra_drm *tegra = dev->dev_private;
struct gr3d *gr3d = to_gr3d(drm); struct gr3d *gr3d = to_gr3d(drm);
int err; int err;
...@@ -57,20 +56,12 @@ static int gr3d_init(struct host1x_client *client) ...@@ -57,20 +56,12 @@ static int gr3d_init(struct host1x_client *client)
goto put; goto put;
} }
if (tegra->domain) { gr3d->group = host1x_client_iommu_attach(client, false);
gr3d->group = iommu_group_get(client->dev); if (IS_ERR(gr3d->group)) {
err = PTR_ERR(gr3d->group);
if (gr3d->group) { dev_err(client->dev, "failed to attach to domain: %d\n", err);
err = iommu_attach_group(tegra->domain, gr3d->group);
if (err < 0) {
dev_err(client->dev,
"failed to attach to domain: %d\n",
err);
iommu_group_put(gr3d->group);
goto free; goto free;
} }
}
}
err = tegra_drm_register_client(dev->dev_private, drm); err = tegra_drm_register_client(dev->dev_private, drm);
if (err < 0) { if (err < 0) {
...@@ -81,10 +72,7 @@ static int gr3d_init(struct host1x_client *client) ...@@ -81,10 +72,7 @@ static int gr3d_init(struct host1x_client *client)
return 0; return 0;
detach: detach:
if (gr3d->group) { host1x_client_iommu_detach(client, gr3d->group);
iommu_detach_group(tegra->domain, gr3d->group);
iommu_group_put(gr3d->group);
}
free: free:
host1x_syncpt_free(client->syncpts[0]); host1x_syncpt_free(client->syncpts[0]);
put: put:
...@@ -96,7 +84,6 @@ static int gr3d_exit(struct host1x_client *client) ...@@ -96,7 +84,6 @@ static int gr3d_exit(struct host1x_client *client)
{ {
struct tegra_drm_client *drm = host1x_to_drm_client(client); struct tegra_drm_client *drm = host1x_to_drm_client(client);
struct drm_device *dev = dev_get_drvdata(client->parent); struct drm_device *dev = dev_get_drvdata(client->parent);
struct tegra_drm *tegra = dev->dev_private;
struct gr3d *gr3d = to_gr3d(drm); struct gr3d *gr3d = to_gr3d(drm);
int err; int err;
...@@ -104,14 +91,10 @@ static int gr3d_exit(struct host1x_client *client) ...@@ -104,14 +91,10 @@ static int gr3d_exit(struct host1x_client *client)
if (err < 0) if (err < 0)
return err; return err;
host1x_client_iommu_detach(client, gr3d->group);
host1x_syncpt_free(client->syncpts[0]); host1x_syncpt_free(client->syncpts[0]);
host1x_channel_put(gr3d->channel); host1x_channel_put(gr3d->channel);
if (gr3d->group) {
iommu_detach_group(tegra->domain, gr3d->group);
iommu_group_put(gr3d->group);
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment