2015-12-04 00:21:29 +07:00
|
|
|
/*
|
2016-08-20 05:16:58 +07:00
|
|
|
* Copyright (C) 2016 Etnaviv Project
|
2015-12-04 00:21:29 +07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
|
|
* the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/sizes.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
|
2017-01-16 22:09:51 +07:00
|
|
|
#include "etnaviv_cmdbuf.h"
|
2015-12-04 00:21:29 +07:00
|
|
|
#include "etnaviv_gpu.h"
|
2016-08-20 05:16:58 +07:00
|
|
|
#include "etnaviv_mmu.h"
|
2015-12-04 00:21:29 +07:00
|
|
|
#include "etnaviv_iommu.h"
|
2016-08-20 05:16:58 +07:00
|
|
|
#include "state.xml.h"
|
2015-12-04 00:21:29 +07:00
|
|
|
#include "state_hi.xml.h"
|
|
|
|
|
2016-08-20 05:16:58 +07:00
|
|
|
#define MMUv2_PTE_PRESENT BIT(0)
|
|
|
|
#define MMUv2_PTE_EXCEPTION BIT(1)
|
|
|
|
#define MMUv2_PTE_WRITEABLE BIT(2)
|
2015-12-04 00:21:29 +07:00
|
|
|
|
2016-08-20 05:16:58 +07:00
|
|
|
#define MMUv2_MTLB_MASK 0xffc00000
|
|
|
|
#define MMUv2_MTLB_SHIFT 22
|
|
|
|
#define MMUv2_STLB_MASK 0x003ff000
|
|
|
|
#define MMUv2_STLB_SHIFT 12
|
|
|
|
|
|
|
|
#define MMUv2_MAX_STLB_ENTRIES 1024
|
|
|
|
|
|
|
|
struct etnaviv_iommuv2_domain {
|
2017-09-07 22:06:28 +07:00
|
|
|
struct etnaviv_iommu_domain base;
|
2018-01-22 18:38:52 +07:00
|
|
|
/* P(age) T(able) A(rray) */
|
|
|
|
u64 *pta_cpu;
|
|
|
|
dma_addr_t pta_dma;
|
2016-08-20 05:16:58 +07:00
|
|
|
/* M(aster) TLB aka first level pagetable */
|
|
|
|
u32 *mtlb_cpu;
|
|
|
|
dma_addr_t mtlb_dma;
|
|
|
|
/* S(lave) TLB aka second level pagetable */
|
|
|
|
u32 *stlb_cpu[1024];
|
|
|
|
dma_addr_t stlb_dma[1024];
|
|
|
|
};
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
static struct etnaviv_iommuv2_domain *
|
|
|
|
to_etnaviv_domain(struct etnaviv_iommu_domain *domain)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
2017-09-07 22:06:28 +07:00
|
|
|
return container_of(domain, struct etnaviv_iommuv2_domain, base);
|
2016-08-20 05:16:58 +07:00
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
static int etnaviv_iommuv2_map(struct etnaviv_iommu_domain *domain,
|
|
|
|
unsigned long iova, phys_addr_t paddr,
|
|
|
|
size_t size, int prot)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(domain);
|
|
|
|
int mtlb_entry, stlb_entry;
|
|
|
|
u32 entry = (u32)paddr | MMUv2_PTE_PRESENT;
|
|
|
|
|
|
|
|
if (size != SZ_4K)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
if (prot & ETNAVIV_PROT_WRITE)
|
2016-08-20 05:16:58 +07:00
|
|
|
entry |= MMUv2_PTE_WRITEABLE;
|
|
|
|
|
|
|
|
mtlb_entry = (iova & MMUv2_MTLB_MASK) >> MMUv2_MTLB_SHIFT;
|
|
|
|
stlb_entry = (iova & MMUv2_STLB_MASK) >> MMUv2_STLB_SHIFT;
|
|
|
|
|
|
|
|
etnaviv_domain->stlb_cpu[mtlb_entry][stlb_entry] = entry;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
static size_t etnaviv_iommuv2_unmap(struct etnaviv_iommu_domain *domain,
|
|
|
|
unsigned long iova, size_t size)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(domain);
|
|
|
|
int mtlb_entry, stlb_entry;
|
|
|
|
|
|
|
|
if (size != SZ_4K)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mtlb_entry = (iova & MMUv2_MTLB_MASK) >> MMUv2_MTLB_SHIFT;
|
|
|
|
stlb_entry = (iova & MMUv2_STLB_MASK) >> MMUv2_STLB_SHIFT;
|
|
|
|
|
|
|
|
etnaviv_domain->stlb_cpu[mtlb_entry][stlb_entry] = MMUv2_PTE_EXCEPTION;
|
|
|
|
|
|
|
|
return SZ_4K;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int etnaviv_iommuv2_init(struct etnaviv_iommuv2_domain *etnaviv_domain)
|
|
|
|
{
|
|
|
|
u32 *p;
|
|
|
|
int ret, i, j;
|
|
|
|
|
|
|
|
/* allocate scratch page */
|
2017-09-07 22:06:28 +07:00
|
|
|
etnaviv_domain->base.bad_page_cpu = dma_alloc_coherent(
|
|
|
|
etnaviv_domain->base.dev,
|
|
|
|
SZ_4K,
|
|
|
|
&etnaviv_domain->base.bad_page_dma,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!etnaviv_domain->base.bad_page_cpu) {
|
2016-08-20 05:16:58 +07:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_mem;
|
|
|
|
}
|
2017-09-07 22:06:28 +07:00
|
|
|
p = etnaviv_domain->base.bad_page_cpu;
|
2016-08-20 05:16:58 +07:00
|
|
|
for (i = 0; i < SZ_4K / 4; i++)
|
|
|
|
*p++ = 0xdead55aa;
|
|
|
|
|
2018-01-22 18:38:52 +07:00
|
|
|
etnaviv_domain->pta_cpu = dma_alloc_coherent(etnaviv_domain->base.dev,
|
|
|
|
SZ_4K,
|
|
|
|
&etnaviv_domain->pta_dma,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!etnaviv_domain->pta_cpu) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_mem;
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
etnaviv_domain->mtlb_cpu = dma_alloc_coherent(etnaviv_domain->base.dev,
|
2016-08-20 05:16:58 +07:00
|
|
|
SZ_4K,
|
|
|
|
&etnaviv_domain->mtlb_dma,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!etnaviv_domain->mtlb_cpu) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pre-populate STLB pages (may want to switch to on-demand later) */
|
|
|
|
for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++) {
|
|
|
|
etnaviv_domain->stlb_cpu[i] =
|
2017-09-07 22:06:28 +07:00
|
|
|
dma_alloc_coherent(etnaviv_domain->base.dev,
|
2016-08-20 05:16:58 +07:00
|
|
|
SZ_4K,
|
|
|
|
&etnaviv_domain->stlb_dma[i],
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!etnaviv_domain->stlb_cpu[i]) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_mem;
|
|
|
|
}
|
|
|
|
p = etnaviv_domain->stlb_cpu[i];
|
|
|
|
for (j = 0; j < SZ_4K / 4; j++)
|
|
|
|
*p++ = MMUv2_PTE_EXCEPTION;
|
|
|
|
|
|
|
|
etnaviv_domain->mtlb_cpu[i] = etnaviv_domain->stlb_dma[i] |
|
|
|
|
MMUv2_PTE_PRESENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_mem:
|
2017-09-07 22:06:28 +07:00
|
|
|
if (etnaviv_domain->base.bad_page_cpu)
|
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
|
|
|
etnaviv_domain->base.bad_page_cpu,
|
|
|
|
etnaviv_domain->base.bad_page_dma);
|
2016-08-20 05:16:58 +07:00
|
|
|
|
2018-01-22 18:38:52 +07:00
|
|
|
if (etnaviv_domain->pta_cpu)
|
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
|
|
|
etnaviv_domain->pta_cpu,
|
|
|
|
etnaviv_domain->pta_dma);
|
|
|
|
|
2016-08-20 05:16:58 +07:00
|
|
|
if (etnaviv_domain->mtlb_cpu)
|
2017-09-07 22:06:28 +07:00
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
2016-08-20 05:16:58 +07:00
|
|
|
etnaviv_domain->mtlb_cpu,
|
|
|
|
etnaviv_domain->mtlb_dma);
|
|
|
|
|
|
|
|
for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++) {
|
|
|
|
if (etnaviv_domain->stlb_cpu[i])
|
2017-09-07 22:06:28 +07:00
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
2016-08-20 05:16:58 +07:00
|
|
|
etnaviv_domain->stlb_cpu[i],
|
|
|
|
etnaviv_domain->stlb_dma[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
static void etnaviv_iommuv2_domain_free(struct etnaviv_iommu_domain *domain)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(domain);
|
|
|
|
int i;
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
|
|
|
etnaviv_domain->base.bad_page_cpu,
|
|
|
|
etnaviv_domain->base.bad_page_dma);
|
2016-08-20 05:16:58 +07:00
|
|
|
|
2018-01-22 18:38:52 +07:00
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
|
|
|
etnaviv_domain->pta_cpu,
|
|
|
|
etnaviv_domain->pta_dma);
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
2016-08-20 05:16:58 +07:00
|
|
|
etnaviv_domain->mtlb_cpu,
|
|
|
|
etnaviv_domain->mtlb_dma);
|
|
|
|
|
|
|
|
for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++) {
|
|
|
|
if (etnaviv_domain->stlb_cpu[i])
|
2017-09-07 22:06:28 +07:00
|
|
|
dma_free_coherent(etnaviv_domain->base.dev, SZ_4K,
|
2016-08-20 05:16:58 +07:00
|
|
|
etnaviv_domain->stlb_cpu[i],
|
|
|
|
etnaviv_domain->stlb_dma[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
vfree(etnaviv_domain);
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
static size_t etnaviv_iommuv2_dump_size(struct etnaviv_iommu_domain *domain)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(domain);
|
|
|
|
size_t dump_size = SZ_4K;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++)
|
|
|
|
if (etnaviv_domain->mtlb_cpu[i] & MMUv2_PTE_PRESENT)
|
|
|
|
dump_size += SZ_4K;
|
|
|
|
|
|
|
|
return dump_size;
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
static void etnaviv_iommuv2_dump(struct etnaviv_iommu_domain *domain, void *buf)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(domain);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(buf, etnaviv_domain->mtlb_cpu, SZ_4K);
|
|
|
|
buf += SZ_4K;
|
|
|
|
for (i = 0; i < MMUv2_MAX_STLB_ENTRIES; i++, buf += SZ_4K)
|
|
|
|
if (etnaviv_domain->mtlb_cpu[i] & MMUv2_PTE_PRESENT)
|
|
|
|
memcpy(buf, etnaviv_domain->stlb_cpu[i], SZ_4K);
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:38:52 +07:00
|
|
|
static void etnaviv_iommuv2_restore_nonsec(struct etnaviv_gpu *gpu)
|
2016-08-20 05:16:58 +07:00
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(gpu->mmu->domain);
|
|
|
|
u16 prefetch;
|
|
|
|
|
|
|
|
/* If the MMU is already enabled the state is still there. */
|
|
|
|
if (gpu_read(gpu, VIVS_MMUv2_CONTROL) & VIVS_MMUv2_CONTROL_ENABLE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
prefetch = etnaviv_buffer_config_mmuv2(gpu,
|
|
|
|
(u32)etnaviv_domain->mtlb_dma,
|
2017-09-07 22:06:28 +07:00
|
|
|
(u32)etnaviv_domain->base.bad_page_dma);
|
2017-11-24 22:56:37 +07:00
|
|
|
etnaviv_gpu_start_fe(gpu, (u32)etnaviv_cmdbuf_get_pa(&gpu->buffer),
|
2017-01-16 23:00:08 +07:00
|
|
|
prefetch);
|
2016-08-20 05:16:58 +07:00
|
|
|
etnaviv_gpu_wait_idle(gpu, 100);
|
|
|
|
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_CONTROL, VIVS_MMUv2_CONTROL_ENABLE);
|
|
|
|
}
|
2017-09-07 22:06:28 +07:00
|
|
|
|
2018-01-22 18:38:52 +07:00
|
|
|
static void etnaviv_iommuv2_restore_sec(struct etnaviv_gpu *gpu)
|
|
|
|
{
|
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain =
|
|
|
|
to_etnaviv_domain(gpu->mmu->domain);
|
|
|
|
u16 prefetch;
|
|
|
|
|
|
|
|
/* If the MMU is already enabled the state is still there. */
|
|
|
|
if (gpu_read(gpu, VIVS_MMUv2_SEC_CONTROL) & VIVS_MMUv2_SEC_CONTROL_ENABLE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_PTA_ADDRESS_LOW,
|
|
|
|
lower_32_bits(etnaviv_domain->pta_dma));
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_PTA_ADDRESS_HIGH,
|
|
|
|
upper_32_bits(etnaviv_domain->pta_dma));
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_PTA_CONTROL, VIVS_MMUv2_PTA_CONTROL_ENABLE);
|
|
|
|
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_NONSEC_SAFE_ADDR_LOW,
|
|
|
|
lower_32_bits(etnaviv_domain->base.bad_page_dma));
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_SEC_SAFE_ADDR_LOW,
|
|
|
|
lower_32_bits(etnaviv_domain->base.bad_page_dma));
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_SAFE_ADDRESS_CONFIG,
|
|
|
|
VIVS_MMUv2_SAFE_ADDRESS_CONFIG_NON_SEC_SAFE_ADDR_HIGH(
|
|
|
|
upper_32_bits(etnaviv_domain->base.bad_page_dma)) |
|
|
|
|
VIVS_MMUv2_SAFE_ADDRESS_CONFIG_SEC_SAFE_ADDR_HIGH(
|
|
|
|
upper_32_bits(etnaviv_domain->base.bad_page_dma)));
|
|
|
|
|
|
|
|
etnaviv_domain->pta_cpu[0] = etnaviv_domain->mtlb_dma |
|
|
|
|
VIVS_MMUv2_CONFIGURATION_MODE_MODE4_K;
|
|
|
|
|
|
|
|
/* trigger a PTA load through the FE */
|
|
|
|
prefetch = etnaviv_buffer_config_pta(gpu);
|
|
|
|
etnaviv_gpu_start_fe(gpu, (u32)etnaviv_cmdbuf_get_pa(&gpu->buffer),
|
|
|
|
prefetch);
|
|
|
|
etnaviv_gpu_wait_idle(gpu, 100);
|
|
|
|
|
|
|
|
gpu_write(gpu, VIVS_MMUv2_SEC_CONTROL, VIVS_MMUv2_SEC_CONTROL_ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void etnaviv_iommuv2_restore(struct etnaviv_gpu *gpu)
|
|
|
|
{
|
|
|
|
switch (gpu->sec_mode) {
|
|
|
|
case ETNA_SEC_NONE:
|
|
|
|
etnaviv_iommuv2_restore_nonsec(gpu);
|
|
|
|
break;
|
|
|
|
case ETNA_SEC_KERNEL:
|
|
|
|
etnaviv_iommuv2_restore_sec(gpu);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN(1, "unhandled GPU security mode\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 18:34:59 +07:00
|
|
|
static const struct etnaviv_iommu_domain_ops etnaviv_iommuv2_ops = {
|
2017-09-07 22:06:28 +07:00
|
|
|
.free = etnaviv_iommuv2_domain_free,
|
|
|
|
.map = etnaviv_iommuv2_map,
|
|
|
|
.unmap = etnaviv_iommuv2_unmap,
|
|
|
|
.dump_size = etnaviv_iommuv2_dump_size,
|
|
|
|
.dump = etnaviv_iommuv2_dump,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct etnaviv_iommu_domain *
|
|
|
|
etnaviv_iommuv2_domain_alloc(struct etnaviv_gpu *gpu)
|
2015-12-04 00:21:29 +07:00
|
|
|
{
|
2016-08-20 05:16:58 +07:00
|
|
|
struct etnaviv_iommuv2_domain *etnaviv_domain;
|
2017-09-07 22:06:28 +07:00
|
|
|
struct etnaviv_iommu_domain *domain;
|
2016-08-20 05:16:58 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
etnaviv_domain = vzalloc(sizeof(*etnaviv_domain));
|
|
|
|
if (!etnaviv_domain)
|
|
|
|
return NULL;
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
domain = &etnaviv_domain->base;
|
2016-08-20 05:16:58 +07:00
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
domain->dev = gpu->dev;
|
|
|
|
domain->base = 0;
|
|
|
|
domain->size = (u64)SZ_1G * 4;
|
|
|
|
domain->ops = &etnaviv_iommuv2_ops;
|
2016-08-20 05:16:58 +07:00
|
|
|
|
|
|
|
ret = etnaviv_iommuv2_init(etnaviv_domain);
|
|
|
|
if (ret)
|
|
|
|
goto out_free;
|
|
|
|
|
2017-09-07 22:06:28 +07:00
|
|
|
return &etnaviv_domain->base;
|
2016-08-20 05:16:58 +07:00
|
|
|
|
|
|
|
out_free:
|
|
|
|
vfree(etnaviv_domain);
|
2015-12-04 00:21:29 +07:00
|
|
|
return NULL;
|
|
|
|
}
|