Skip to content

Commit af1cbfb

Browse files
committed
gpu: host1x: Support DMA mapping of buffers
If host1x_bo_pin() returns an SG table, create a DMA mapping for the buffer. For buffers that the host1x client has already mapped itself, host1x_bo_pin() returns NULL and the existing DMA address is used. Signed-off-by: Thierry Reding <treding@nvidia.com>
1 parent b78e70c commit af1cbfb

4 files changed

Lines changed: 87 additions & 24 deletions

File tree

‎drivers/gpu/drm/tegra/gem.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ static struct sg_table *tegra_bo_pin(struct device *dev, struct host1x_bo *bo,
3434
struct sg_table *sgt;
3535
int err;
3636

37-
if (phys)
37+
/*
38+
* If we've manually mapped the buffer object through the IOMMU, make
39+
* sure to return the IOVA address of our mapping.
40+
*/
41+
if (phys && obj->mm) {
3842
*phys = obj->iova;
43+
return NULL;
44+
}
3945

46+
/*
47+
* If we don't have a mapping for this buffer yet, return an SG table
48+
* so that host1x can do the mapping for us via the DMA API.
49+
*/
4050
sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
4151
if (!sgt)
4252
return ERR_PTR(-ENOMEM);
@@ -62,8 +72,10 @@ static struct sg_table *tegra_bo_pin(struct device *dev, struct host1x_bo *bo,
6272

6373
static void tegra_bo_unpin(struct device *dev, struct sg_table *sgt)
6474
{
65-
sg_free_table(sgt);
66-
kfree(sgt);
75+
if (sgt) {
76+
sg_free_table(sgt);
77+
kfree(sgt);
78+
}
6779
}
6880

6981
static void *tegra_bo_mmap(struct host1x_bo *bo)

‎drivers/gpu/host1x/dev.c‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
#include <trace/events/host1x.h>
1919
#undef CREATE_TRACE_POINTS
2020

21-
#if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
22-
#include <asm/dma-iommu.h>
23-
#endif
24-
2521
#include "bus.h"
2622
#include "channel.h"
2723
#include "debug.h"
@@ -276,17 +272,13 @@ static int host1x_probe(struct platform_device *pdev)
276272
dev_err(&pdev->dev, "failed to get reset: %d\n", err);
277273
return err;
278274
}
279-
#if IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)
280-
if (host->dev->archdata.mapping) {
281-
struct dma_iommu_mapping *mapping =
282-
to_dma_iommu_mapping(host->dev);
283-
arm_iommu_detach_device(host->dev);
284-
arm_iommu_release_mapping(mapping);
285-
}
286-
#endif
275+
287276
if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
288277
goto skip_iommu;
289278

279+
if (iommu_get_domain_for_dev(&pdev->dev))
280+
goto skip_iommu;
281+
290282
host->group = iommu_group_get(&pdev->dev);
291283
if (host->group) {
292284
struct iommu_domain_geometry *geometry;

‎drivers/gpu/host1x/job.c‎

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,69 @@ EXPORT_SYMBOL(host1x_job_add_gather);
9999

100100
static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
101101
{
102-
struct device *dev = job->client->dev;
102+
struct host1x_client *client = job->client;
103+
struct device *dev = client->dev;
103104
unsigned int i;
104105
int err;
105106

106107
job->num_unpins = 0;
107108

108109
for (i = 0; i < job->num_relocs; i++) {
109110
struct host1x_reloc *reloc = &job->relocs[i];
111+
dma_addr_t phys_addr, *phys;
110112
struct sg_table *sgt;
111-
dma_addr_t phys_addr;
112113

113114
reloc->target.bo = host1x_bo_get(reloc->target.bo);
114115
if (!reloc->target.bo) {
115116
err = -EINVAL;
116117
goto unpin;
117118
}
118119

119-
sgt = host1x_bo_pin(dev, reloc->target.bo, &phys_addr);
120+
if (client->group)
121+
phys = &phys_addr;
122+
else
123+
phys = NULL;
124+
125+
sgt = host1x_bo_pin(dev, reloc->target.bo, phys);
120126
if (IS_ERR(sgt)) {
121127
err = PTR_ERR(sgt);
122128
goto unpin;
123129
}
124130

131+
if (sgt) {
132+
unsigned long mask = HOST1X_RELOC_READ |
133+
HOST1X_RELOC_WRITE;
134+
enum dma_data_direction dir;
135+
136+
switch (reloc->flags & mask) {
137+
case HOST1X_RELOC_READ:
138+
dir = DMA_TO_DEVICE;
139+
break;
140+
141+
case HOST1X_RELOC_WRITE:
142+
dir = DMA_FROM_DEVICE;
143+
break;
144+
145+
case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
146+
dir = DMA_BIDIRECTIONAL;
147+
break;
148+
149+
default:
150+
err = -EINVAL;
151+
goto unpin;
152+
}
153+
154+
err = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
155+
if (!err) {
156+
err = -ENOMEM;
157+
goto unpin;
158+
}
159+
160+
job->unpins[job->num_unpins].dev = dev;
161+
job->unpins[job->num_unpins].dir = dir;
162+
phys_addr = sg_dma_address(sgt->sgl);
163+
}
164+
125165
job->addr_phys[job->num_unpins] = phys_addr;
126166
job->unpins[job->num_unpins].bo = reloc->target.bo;
127167
job->unpins[job->num_unpins].sgt = sgt;
@@ -144,7 +184,7 @@ static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
144184
goto unpin;
145185
}
146186

147-
sgt = host1x_bo_pin(host->dev, g->bo, &phys_addr);
187+
sgt = host1x_bo_pin(host->dev, g->bo, NULL);
148188
if (IS_ERR(sgt)) {
149189
err = PTR_ERR(sgt);
150190
goto unpin;
@@ -172,15 +212,24 @@ static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
172212
goto unpin;
173213
}
174214

175-
job->addr_phys[job->num_unpins] =
176-
iova_dma_addr(&host->iova, alloc);
177215
job->unpins[job->num_unpins].size = gather_size;
216+
phys_addr = iova_dma_addr(&host->iova, alloc);
178217
} else {
179-
job->addr_phys[job->num_unpins] = phys_addr;
218+
err = dma_map_sg(host->dev, sgt->sgl, sgt->nents,
219+
DMA_TO_DEVICE);
220+
if (!err) {
221+
err = -ENOMEM;
222+
goto unpin;
223+
}
224+
225+
job->unpins[job->num_unpins].dev = host->dev;
226+
phys_addr = sg_dma_address(sgt->sgl);
180227
}
181228

182-
job->gather_addr_phys[i] = job->addr_phys[job->num_unpins];
229+
job->addr_phys[job->num_unpins] = phys_addr;
230+
job->gather_addr_phys[i] = phys_addr;
183231

232+
job->unpins[job->num_unpins].dir = DMA_TO_DEVICE;
184233
job->unpins[job->num_unpins].bo = g->bo;
185234
job->unpins[job->num_unpins].sgt = sgt;
186235
job->num_unpins++;
@@ -567,6 +616,8 @@ void host1x_job_unpin(struct host1x_job *job)
567616

568617
for (i = 0; i < job->num_unpins; i++) {
569618
struct host1x_job_unpin_data *unpin = &job->unpins[i];
619+
struct device *dev = unpin->dev ?: host->dev;
620+
struct sg_table *sgt = unpin->sgt;
570621

571622
if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
572623
unpin->size && host->domain) {
@@ -576,7 +627,11 @@ void host1x_job_unpin(struct host1x_job *job)
576627
iova_pfn(&host->iova, job->addr_phys[i]));
577628
}
578629

579-
host1x_bo_unpin(host->dev, unpin->bo, unpin->sgt);
630+
if (unpin->dev && sgt)
631+
dma_unmap_sg(unpin->dev, sgt->sgl, sgt->nents,
632+
unpin->dir);
633+
634+
host1x_bo_unpin(dev, unpin->bo, sgt);
580635
host1x_bo_put(unpin->bo);
581636
}
582637

‎drivers/gpu/host1x/job.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef __HOST1X_JOB_H
99
#define __HOST1X_JOB_H
1010

11+
#include <linux/dma-direction.h>
12+
1113
struct host1x_job_gather {
1214
unsigned int words;
1315
dma_addr_t base;
@@ -19,7 +21,9 @@ struct host1x_job_gather {
1921
struct host1x_job_unpin_data {
2022
struct host1x_bo *bo;
2123
struct sg_table *sgt;
24+
struct device *dev;
2225
size_t size;
26+
enum dma_data_direction dir;
2327
};
2428

2529
/*

0 commit comments

Comments
 (0)