Skip to content

Commit 404bfb7

Browse files
cyndisthierryreding
authored andcommitted
gpu: host1x: Add IOMMU support
Add support for the Host1x unit to be located behind an IOMMU. This is required when gather buffers may be allocated non-contiguously in physical memory, as can be the case when TegraDRM is also using the IOMMU. Signed-off-by: Mikko Perttunen <mperttunen@nvidia.com> Signed-off-by: Thierry Reding <treding@nvidia.com>
1 parent 8cadb01 commit 404bfb7

7 files changed

Lines changed: 177 additions & 39 deletions

File tree

‎drivers/gpu/host1x/cdma.c‎

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,15 @@ static void host1x_pushbuffer_destroy(struct push_buffer *pb)
5151
struct host1x_cdma *cdma = pb_to_cdma(pb);
5252
struct host1x *host1x = cdma_to_host1x(cdma);
5353

54-
if (pb->phys != 0)
55-
dma_free_wc(host1x->dev, pb->size_bytes + 4, pb->mapped,
56-
pb->phys);
54+
if (!pb->phys)
55+
return;
56+
57+
if (host1x->domain) {
58+
iommu_unmap(host1x->domain, pb->dma, pb->alloc_size);
59+
free_iova(&host1x->iova, iova_pfn(&host1x->iova, pb->dma));
60+
}
61+
62+
dma_free_wc(host1x->dev, pb->alloc_size, pb->mapped, pb->phys);
5763

5864
pb->mapped = NULL;
5965
pb->phys = 0;
@@ -66,28 +72,64 @@ static int host1x_pushbuffer_init(struct push_buffer *pb)
6672
{
6773
struct host1x_cdma *cdma = pb_to_cdma(pb);
6874
struct host1x *host1x = cdma_to_host1x(cdma);
75+
struct iova *alloc;
76+
u32 size;
77+
int err;
6978

7079
pb->mapped = NULL;
7180
pb->phys = 0;
72-
pb->size_bytes = HOST1X_PUSHBUFFER_SLOTS * 8;
81+
pb->size = HOST1X_PUSHBUFFER_SLOTS * 8;
82+
83+
size = pb->size + 4;
7384

7485
/* initialize buffer pointers */
75-
pb->fence = pb->size_bytes - 8;
86+
pb->fence = pb->size - 8;
7687
pb->pos = 0;
7788

78-
/* allocate and map pushbuffer memory */
79-
pb->mapped = dma_alloc_wc(host1x->dev, pb->size_bytes + 4, &pb->phys,
80-
GFP_KERNEL);
81-
if (!pb->mapped)
82-
goto fail;
89+
if (host1x->domain) {
90+
unsigned long shift;
91+
92+
size = iova_align(&host1x->iova, size);
93+
94+
pb->mapped = dma_alloc_wc(host1x->dev, size, &pb->phys,
95+
GFP_KERNEL);
96+
if (!pb->mapped)
97+
return -ENOMEM;
98+
99+
shift = iova_shift(&host1x->iova);
100+
alloc = alloc_iova(&host1x->iova, size >> shift,
101+
host1x->iova_end >> shift, true);
102+
if (!alloc) {
103+
err = -ENOMEM;
104+
goto iommu_free_mem;
105+
}
106+
107+
pb->dma = iova_dma_addr(&host1x->iova, alloc);
108+
err = iommu_map(host1x->domain, pb->dma, pb->phys, size,
109+
IOMMU_READ);
110+
if (err)
111+
goto iommu_free_iova;
112+
} else {
113+
pb->mapped = dma_alloc_wc(host1x->dev, size, &pb->phys,
114+
GFP_KERNEL);
115+
if (!pb->mapped)
116+
return -ENOMEM;
117+
118+
pb->dma = pb->phys;
119+
}
120+
121+
pb->alloc_size = size;
83122

84123
host1x_hw_pushbuffer_init(host1x, pb);
85124

86125
return 0;
87126

88-
fail:
89-
host1x_pushbuffer_destroy(pb);
90-
return -ENOMEM;
127+
iommu_free_iova:
128+
__free_iova(&host1x->iova, alloc);
129+
iommu_free_mem:
130+
dma_free_wc(host1x->dev, pb->alloc_size, pb->mapped, pb->phys);
131+
132+
return err;
91133
}
92134

93135
/*
@@ -101,7 +143,7 @@ static void host1x_pushbuffer_push(struct push_buffer *pb, u32 op1, u32 op2)
101143
WARN_ON(pb->pos == pb->fence);
102144
*(p++) = op1;
103145
*(p++) = op2;
104-
pb->pos = (pb->pos + 8) & (pb->size_bytes - 1);
146+
pb->pos = (pb->pos + 8) & (pb->size - 1);
105147
}
106148

107149
/*
@@ -111,15 +153,15 @@ static void host1x_pushbuffer_push(struct push_buffer *pb, u32 op1, u32 op2)
111153
static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots)
112154
{
113155
/* Advance the next write position */
114-
pb->fence = (pb->fence + slots * 8) & (pb->size_bytes - 1);
156+
pb->fence = (pb->fence + slots * 8) & (pb->size - 1);
115157
}
116158

117159
/*
118160
* Return the number of two word slots free in the push buffer
119161
*/
120162
static u32 host1x_pushbuffer_space(struct push_buffer *pb)
121163
{
122-
return ((pb->fence - pb->pos) & (pb->size_bytes - 1)) / 8;
164+
return ((pb->fence - pb->pos) & (pb->size - 1)) / 8;
123165
}
124166

125167
/*

‎drivers/gpu/host1x/cdma.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ struct host1x_job;
4343

4444
struct push_buffer {
4545
void *mapped; /* mapped pushbuffer memory */
46-
dma_addr_t phys; /* physical address of pushbuffer */
46+
dma_addr_t dma; /* device address of pushbuffer */
47+
phys_addr_t phys; /* physical address of pushbuffer */
4748
u32 fence; /* index we've written */
4849
u32 pos; /* index to write to */
49-
u32 size_bytes;
50+
u32 size;
51+
u32 alloc_size;
5052
};
5153

5254
struct buffer_timeout {

‎drivers/gpu/host1x/dev.c‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#define CREATE_TRACE_POINTS
2929
#include <trace/events/host1x.h>
30+
#undef CREATE_TRACE_POINTS
3031

3132
#include "bus.h"
3233
#include "dev.h"
@@ -168,16 +169,37 @@ static int host1x_probe(struct platform_device *pdev)
168169
return err;
169170
}
170171

172+
if (iommu_present(&platform_bus_type)) {
173+
struct iommu_domain_geometry *geometry;
174+
unsigned long order;
175+
176+
host->domain = iommu_domain_alloc(&platform_bus_type);
177+
if (!host->domain)
178+
return -ENOMEM;
179+
180+
err = iommu_attach_device(host->domain, &pdev->dev);
181+
if (err)
182+
goto fail_free_domain;
183+
184+
geometry = &host->domain->geometry;
185+
186+
order = __ffs(host->domain->pgsize_bitmap);
187+
init_iova_domain(&host->iova, 1UL << order,
188+
geometry->aperture_start >> order,
189+
geometry->aperture_end >> order);
190+
host->iova_end = geometry->aperture_end;
191+
}
192+
171193
err = host1x_channel_list_init(host);
172194
if (err) {
173195
dev_err(&pdev->dev, "failed to initialize channel list\n");
174-
return err;
196+
goto fail_detach_device;
175197
}
176198

177199
err = clk_prepare_enable(host->clk);
178200
if (err < 0) {
179201
dev_err(&pdev->dev, "failed to enable clock\n");
180-
return err;
202+
goto fail_detach_device;
181203
}
182204

183205
err = host1x_syncpt_init(host);
@@ -206,6 +228,15 @@ static int host1x_probe(struct platform_device *pdev)
206228
host1x_syncpt_deinit(host);
207229
fail_unprepare_disable:
208230
clk_disable_unprepare(host->clk);
231+
fail_detach_device:
232+
if (host->domain) {
233+
put_iova_domain(&host->iova);
234+
iommu_detach_device(host->domain, &pdev->dev);
235+
}
236+
fail_free_domain:
237+
if (host->domain)
238+
iommu_domain_free(host->domain);
239+
209240
return err;
210241
}
211242

@@ -218,6 +249,12 @@ static int host1x_remove(struct platform_device *pdev)
218249
host1x_syncpt_deinit(host);
219250
clk_disable_unprepare(host->clk);
220251

252+
if (host->domain) {
253+
put_iova_domain(&host->iova);
254+
iommu_detach_device(host->domain, &pdev->dev);
255+
iommu_domain_free(host->domain);
256+
}
257+
221258
return 0;
222259
}
223260

‎drivers/gpu/host1x/dev.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <linux/platform_device.h>
2121
#include <linux/device.h>
22+
#include <linux/iommu.h>
23+
#include <linux/iova.h>
2224

2325
#include "channel.h"
2426
#include "syncpt.h"
@@ -108,6 +110,10 @@ struct host1x {
108110
struct device *dev;
109111
struct clk *clk;
110112

113+
struct iommu_domain *domain;
114+
struct iova_domain iova;
115+
dma_addr_t iova_end;
116+
111117
struct mutex intr_mutex;
112118
int intr_syncpt_irq;
113119

‎drivers/gpu/host1x/hw/cdma_hw.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
static void push_buffer_init(struct push_buffer *pb)
3232
{
33-
*(u32 *)(pb->mapped + pb->size_bytes) = host1x_opcode_restart(0);
33+
*(u32 *)(pb->mapped + pb->size) = host1x_opcode_restart(0);
3434
}
3535

3636
/*
@@ -55,8 +55,8 @@ static void cdma_timeout_cpu_incr(struct host1x_cdma *cdma, u32 getptr,
5555
*(p++) = HOST1X_OPCODE_NOP;
5656
*(p++) = HOST1X_OPCODE_NOP;
5757
dev_dbg(host1x->dev, "%s: NOP at %pad+%#x\n", __func__,
58-
&pb->phys, getptr);
59-
getptr = (getptr + 8) & (pb->size_bytes - 1);
58+
&pb->dma, getptr);
59+
getptr = (getptr + 8) & (pb->size - 1);
6060
}
6161

6262
wmb();
@@ -78,10 +78,9 @@ static void cdma_start(struct host1x_cdma *cdma)
7878
HOST1X_CHANNEL_DMACTRL);
7979

8080
/* set base, put and end pointer */
81-
host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
81+
host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
8282
host1x_ch_writel(ch, cdma->push_buffer.pos, HOST1X_CHANNEL_DMAPUT);
83-
host1x_ch_writel(ch, cdma->push_buffer.phys +
84-
cdma->push_buffer.size_bytes + 4,
83+
host1x_ch_writel(ch, cdma->push_buffer.dma + cdma->push_buffer.size + 4,
8584
HOST1X_CHANNEL_DMAEND);
8685

8786
/* reset GET */
@@ -115,9 +114,8 @@ static void cdma_timeout_restart(struct host1x_cdma *cdma, u32 getptr)
115114
HOST1X_CHANNEL_DMACTRL);
116115

117116
/* set base, end pointer (all of memory) */
118-
host1x_ch_writel(ch, cdma->push_buffer.phys, HOST1X_CHANNEL_DMASTART);
119-
host1x_ch_writel(ch, cdma->push_buffer.phys +
120-
cdma->push_buffer.size_bytes,
117+
host1x_ch_writel(ch, cdma->push_buffer.dma, HOST1X_CHANNEL_DMASTART);
118+
host1x_ch_writel(ch, cdma->push_buffer.dma + cdma->push_buffer.size,
121119
HOST1X_CHANNEL_DMAEND);
122120

123121
/* set GET, by loading the value in PUT (then reset GET) */

0 commit comments

Comments
 (0)