Skip to content

Commit 06867a3

Browse files
committed
gpu: host1x: Set DMA mask based on IOMMU setup
If the Tegra DRM clients are backed by an IOMMU, push buffers are likely to be allocated beyond the 32-bit boundary if sufficient system memory is available. This is problematic on earlier generations of Tegra where host1x supports a maximum of 32 address bits for the GATHER opcode. More recent versions of Tegra (Tegra186 and later) have a wide variant of the GATHER opcode, which allows addressing up to 64 bits of memory. If host1x itself is behind an IOMMU as well this doesn't matter because the IOMMU's input address space is restricted to 32 bits on generations without support for wide GATHER opcodes. However, if host1x is not behind an IOMMU, it won't be able to process push buffers beyond the 32-bit boundary on Tegra generations that don't support wide GATHER opcodes. Restrict the DMA mask to 32 bits on these generations prevents buffers from being allocated from beyond the 32-bit boundary. Signed-off-by: Thierry Reding <treding@nvidia.com>
1 parent af1cbfb commit 06867a3

2 files changed

Lines changed: 136 additions & 79 deletions

File tree

‎drivers/gpu/host1x/dev.c‎

Lines changed: 135 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static const struct host1x_info host1x01_info = {
7373
.init = host1x01_init,
7474
.sync_offset = 0x3000,
7575
.dma_mask = DMA_BIT_MASK(32),
76+
.has_wide_gather = false,
7677
.has_hypervisor = false,
7778
.num_sid_entries = 0,
7879
.sid_table = NULL,
@@ -86,6 +87,7 @@ static const struct host1x_info host1x02_info = {
8687
.init = host1x02_init,
8788
.sync_offset = 0x3000,
8889
.dma_mask = DMA_BIT_MASK(32),
90+
.has_wide_gather = false,
8991
.has_hypervisor = false,
9092
.num_sid_entries = 0,
9193
.sid_table = NULL,
@@ -99,6 +101,7 @@ static const struct host1x_info host1x04_info = {
99101
.init = host1x04_init,
100102
.sync_offset = 0x2100,
101103
.dma_mask = DMA_BIT_MASK(34),
104+
.has_wide_gather = false,
102105
.has_hypervisor = false,
103106
.num_sid_entries = 0,
104107
.sid_table = NULL,
@@ -112,6 +115,7 @@ static const struct host1x_info host1x05_info = {
112115
.init = host1x05_init,
113116
.sync_offset = 0x2100,
114117
.dma_mask = DMA_BIT_MASK(34),
118+
.has_wide_gather = false,
115119
.has_hypervisor = false,
116120
.num_sid_entries = 0,
117121
.sid_table = NULL,
@@ -134,6 +138,7 @@ static const struct host1x_info host1x06_info = {
134138
.init = host1x06_init,
135139
.sync_offset = 0x0,
136140
.dma_mask = DMA_BIT_MASK(40),
141+
.has_wide_gather = true,
137142
.has_hypervisor = true,
138143
.num_sid_entries = ARRAY_SIZE(tegra186_sid_table),
139144
.sid_table = tegra186_sid_table,
@@ -156,6 +161,7 @@ static const struct host1x_info host1x07_info = {
156161
.init = host1x07_init,
157162
.sync_offset = 0x0,
158163
.dma_mask = DMA_BIT_MASK(40),
164+
.has_wide_gather = true,
159165
.has_hypervisor = true,
160166
.num_sid_entries = ARRAY_SIZE(tegra194_sid_table),
161167
.sid_table = tegra194_sid_table,
@@ -186,6 +192,117 @@ static void host1x_setup_sid_table(struct host1x *host)
186192
}
187193
}
188194

195+
static struct iommu_domain *host1x_iommu_attach(struct host1x *host)
196+
{
197+
struct iommu_domain *domain = iommu_get_domain_for_dev(host->dev);
198+
int err;
199+
200+
/*
201+
* If the host1x firewall is enabled, there's no need to enable IOMMU
202+
* support. Similarly, if host1x is already attached to an IOMMU (via
203+
* the DMA API), don't try to attach again.
204+
*/
205+
if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) || domain)
206+
return domain;
207+
208+
host->group = iommu_group_get(host->dev);
209+
if (host->group) {
210+
struct iommu_domain_geometry *geometry;
211+
dma_addr_t start, end;
212+
unsigned long order;
213+
214+
err = iova_cache_get();
215+
if (err < 0)
216+
goto put_group;
217+
218+
host->domain = iommu_domain_alloc(&platform_bus_type);
219+
if (!host->domain) {
220+
err = -ENOMEM;
221+
goto put_cache;
222+
}
223+
224+
err = iommu_attach_group(host->domain, host->group);
225+
if (err) {
226+
if (err == -ENODEV)
227+
err = 0;
228+
229+
goto free_domain;
230+
}
231+
232+
geometry = &host->domain->geometry;
233+
start = geometry->aperture_start & host->info->dma_mask;
234+
end = geometry->aperture_end & host->info->dma_mask;
235+
236+
order = __ffs(host->domain->pgsize_bitmap);
237+
init_iova_domain(&host->iova, 1UL << order, start >> order);
238+
host->iova_end = end;
239+
240+
domain = host->domain;
241+
}
242+
243+
return domain;
244+
245+
free_domain:
246+
iommu_domain_free(host->domain);
247+
host->domain = NULL;
248+
put_cache:
249+
iova_cache_put();
250+
put_group:
251+
iommu_group_put(host->group);
252+
host->group = NULL;
253+
254+
return ERR_PTR(err);
255+
}
256+
257+
static int host1x_iommu_init(struct host1x *host)
258+
{
259+
u64 mask = host->info->dma_mask;
260+
struct iommu_domain *domain;
261+
int err;
262+
263+
domain = host1x_iommu_attach(host);
264+
if (IS_ERR(domain)) {
265+
err = PTR_ERR(domain);
266+
dev_err(host->dev, "failed to attach to IOMMU: %d\n", err);
267+
return err;
268+
}
269+
270+
/*
271+
* If we're not behind an IOMMU make sure we don't get push buffers
272+
* that are allocated outside of the range addressable by the GATHER
273+
* opcode.
274+
*
275+
* Newer generations of Tegra (Tegra186 and later) support a wide
276+
* variant of the GATHER opcode that allows addressing more bits.
277+
*/
278+
if (!domain && !host->info->has_wide_gather)
279+
mask = DMA_BIT_MASK(32);
280+
281+
err = dma_coerce_mask_and_coherent(host->dev, mask);
282+
if (err < 0) {
283+
dev_err(host->dev, "failed to set DMA mask: %d\n", err);
284+
return err;
285+
}
286+
287+
return 0;
288+
}
289+
290+
static void host1x_iommu_exit(struct host1x *host)
291+
{
292+
if (host->domain) {
293+
put_iova_domain(&host->iova);
294+
iommu_detach_group(host->domain, host->group);
295+
296+
iommu_domain_free(host->domain);
297+
host->domain = NULL;
298+
299+
iova_cache_put();
300+
301+
iommu_group_put(host->group);
302+
host->group = NULL;
303+
}
304+
}
305+
189306
static int host1x_probe(struct platform_device *pdev)
190307
{
191308
struct host1x *host;
@@ -248,8 +365,6 @@ static int host1x_probe(struct platform_device *pdev)
248365
host->dev->dma_parms = &host->dma_parms;
249366
dma_set_max_seg_size(host->dev, UINT_MAX);
250367

251-
dma_set_mask_and_coherent(host->dev, host->info->dma_mask);
252-
253368
if (host->info->init) {
254369
err = host->info->init(host);
255370
if (err)
@@ -273,82 +388,41 @@ static int host1x_probe(struct platform_device *pdev)
273388
return err;
274389
}
275390

276-
if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
277-
goto skip_iommu;
278-
279-
if (iommu_get_domain_for_dev(&pdev->dev))
280-
goto skip_iommu;
281-
282-
host->group = iommu_group_get(&pdev->dev);
283-
if (host->group) {
284-
struct iommu_domain_geometry *geometry;
285-
u64 mask = dma_get_mask(host->dev);
286-
dma_addr_t start, end;
287-
unsigned long order;
288-
289-
err = iova_cache_get();
290-
if (err < 0)
291-
goto put_group;
292-
293-
host->domain = iommu_domain_alloc(&platform_bus_type);
294-
if (!host->domain) {
295-
err = -ENOMEM;
296-
goto put_cache;
297-
}
298-
299-
err = iommu_attach_group(host->domain, host->group);
300-
if (err) {
301-
if (err == -ENODEV) {
302-
iommu_domain_free(host->domain);
303-
host->domain = NULL;
304-
iova_cache_put();
305-
iommu_group_put(host->group);
306-
host->group = NULL;
307-
goto skip_iommu;
308-
}
309-
310-
goto fail_free_domain;
311-
}
312-
313-
geometry = &host->domain->geometry;
314-
start = geometry->aperture_start & mask;
315-
end = geometry->aperture_end & mask;
316-
317-
order = __ffs(host->domain->pgsize_bitmap);
318-
init_iova_domain(&host->iova, 1UL << order, start >> order);
319-
host->iova_end = end;
391+
err = host1x_iommu_init(host);
392+
if (err < 0) {
393+
dev_err(&pdev->dev, "failed to setup IOMMU: %d\n", err);
394+
return err;
320395
}
321396

322-
skip_iommu:
323397
err = host1x_channel_list_init(&host->channel_list,
324398
host->info->nb_channels);
325399
if (err) {
326400
dev_err(&pdev->dev, "failed to initialize channel list\n");
327-
goto fail_detach_device;
401+
goto iommu_exit;
328402
}
329403

330404
err = clk_prepare_enable(host->clk);
331405
if (err < 0) {
332406
dev_err(&pdev->dev, "failed to enable clock\n");
333-
goto fail_free_channels;
407+
goto free_channels;
334408
}
335409

336410
err = reset_control_deassert(host->rst);
337411
if (err < 0) {
338412
dev_err(&pdev->dev, "failed to deassert reset: %d\n", err);
339-
goto fail_unprepare_disable;
413+
goto unprepare_disable;
340414
}
341415

342416
err = host1x_syncpt_init(host);
343417
if (err) {
344418
dev_err(&pdev->dev, "failed to initialize syncpts\n");
345-
goto fail_reset_assert;
419+
goto reset_assert;
346420
}
347421

348422
err = host1x_intr_init(host, syncpt_irq);
349423
if (err) {
350424
dev_err(&pdev->dev, "failed to initialize interrupts\n");
351-
goto fail_deinit_syncpt;
425+
goto deinit_syncpt;
352426
}
353427

354428
host1x_debug_init(host);
@@ -358,33 +432,22 @@ static int host1x_probe(struct platform_device *pdev)
358432

359433
err = host1x_register(host);
360434
if (err < 0)
361-
goto fail_deinit_intr;
435+
goto deinit_intr;
362436

363437
return 0;
364438

365-
fail_deinit_intr:
439+
deinit_intr:
366440
host1x_intr_deinit(host);
367-
fail_deinit_syncpt:
441+
deinit_syncpt:
368442
host1x_syncpt_deinit(host);
369-
fail_reset_assert:
443+
reset_assert:
370444
reset_control_assert(host->rst);
371-
fail_unprepare_disable:
445+
unprepare_disable:
372446
clk_disable_unprepare(host->clk);
373-
fail_free_channels:
447+
free_channels:
374448
host1x_channel_list_free(&host->channel_list);
375-
fail_detach_device:
376-
if (host->group && host->domain) {
377-
put_iova_domain(&host->iova);
378-
iommu_detach_group(host->domain, host->group);
379-
}
380-
fail_free_domain:
381-
if (host->domain)
382-
iommu_domain_free(host->domain);
383-
put_cache:
384-
if (host->group)
385-
iova_cache_put();
386-
put_group:
387-
iommu_group_put(host->group);
449+
iommu_exit:
450+
host1x_iommu_exit(host);
388451

389452
return err;
390453
}
@@ -399,14 +462,7 @@ static int host1x_remove(struct platform_device *pdev)
399462
host1x_syncpt_deinit(host);
400463
reset_control_assert(host->rst);
401464
clk_disable_unprepare(host->clk);
402-
403-
if (host->domain) {
404-
put_iova_domain(&host->iova);
405-
iommu_detach_group(host->domain, host->group);
406-
iommu_domain_free(host->domain);
407-
iova_cache_put();
408-
iommu_group_put(host->group);
409-
}
465+
host1x_iommu_exit(host);
410466

411467
return 0;
412468
}

‎drivers/gpu/host1x/dev.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct host1x_info {
9797
int (*init)(struct host1x *host1x); /* initialize per SoC ops */
9898
unsigned int sync_offset; /* offset of syncpoint registers */
9999
u64 dma_mask; /* mask of addressable memory */
100+
bool has_wide_gather; /* supports GATHER_W opcode */
100101
bool has_hypervisor; /* has hypervisor registers */
101102
unsigned int num_sid_entries;
102103
const struct host1x_sid_entry *sid_table;

0 commit comments

Comments
 (0)