blob: b163bdbcb8803b1644d191dda9ccec5c4bb26cb0 [file] [log] [blame]
Lucas Stachf6ffbd42018-05-08 16:20:54 +02001// SPDX-License-Identifier: GPL-2.0
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01002/*
Lucas Stachf6ffbd42018-05-08 16:20:54 +02003 * Copyright (C) 2014-2018 Etnaviv Project
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01004 */
5
The etnaviv authorsa8c21a52015-12-03 18:21:29 +01006#include <linux/platform_device.h>
7#include <linux/sizes.h>
8#include <linux/slab.h>
9#include <linux/dma-mapping.h>
10#include <linux/bitops.h>
11
12#include "etnaviv_gpu.h"
13#include "etnaviv_mmu.h"
14#include "etnaviv_iommu.h"
15#include "state_hi.xml.h"
16
17#define PT_SIZE SZ_2M
18#define PT_ENTRIES (PT_SIZE / sizeof(u32))
19
20#define GPU_MEM_START 0x80000000
21
Lucas Stachb6709082017-09-07 17:06:28 +020022struct etnaviv_iommuv1_domain {
23 struct etnaviv_iommu_domain base;
24 u32 *pgtable_cpu;
25 dma_addr_t pgtable_dma;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010026};
27
Lucas Stachb6709082017-09-07 17:06:28 +020028static struct etnaviv_iommuv1_domain *
29to_etnaviv_domain(struct etnaviv_iommu_domain *domain)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010030{
Lucas Stachb6709082017-09-07 17:06:28 +020031 return container_of(domain, struct etnaviv_iommuv1_domain, base);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010032}
33
Lucas Stachb6709082017-09-07 17:06:28 +020034static int __etnaviv_iommu_init(struct etnaviv_iommuv1_domain *etnaviv_domain)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010035{
36 u32 *p;
Lucas Stach1a540492017-09-07 15:36:57 +020037 int i;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010038
Lucas Stach1af998b2018-04-17 12:00:46 +020039 etnaviv_domain->base.bad_page_cpu =
40 dma_alloc_wc(etnaviv_domain->base.dev, SZ_4K,
41 &etnaviv_domain->base.bad_page_dma,
42 GFP_KERNEL);
Lucas Stachb6709082017-09-07 17:06:28 +020043 if (!etnaviv_domain->base.bad_page_cpu)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010044 return -ENOMEM;
45
Lucas Stachb6709082017-09-07 17:06:28 +020046 p = etnaviv_domain->base.bad_page_cpu;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010047 for (i = 0; i < SZ_4K / 4; i++)
48 *p++ = 0xdead55aa;
49
Lucas Stach1af998b2018-04-17 12:00:46 +020050 etnaviv_domain->pgtable_cpu = dma_alloc_wc(etnaviv_domain->base.dev,
51 PT_SIZE,
52 &etnaviv_domain->pgtable_dma,
53 GFP_KERNEL);
Lucas Stachb6709082017-09-07 17:06:28 +020054 if (!etnaviv_domain->pgtable_cpu) {
Lucas Stach1af998b2018-04-17 12:00:46 +020055 dma_free_wc(etnaviv_domain->base.dev, SZ_4K,
56 etnaviv_domain->base.bad_page_cpu,
57 etnaviv_domain->base.bad_page_dma);
Lucas Stach1a540492017-09-07 15:36:57 +020058 return -ENOMEM;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010059 }
60
Lucas Stach2f20fc42017-11-27 15:33:28 +010061 memset32(etnaviv_domain->pgtable_cpu, etnaviv_domain->base.bad_page_dma,
62 PT_ENTRIES);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010063
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010064 return 0;
65}
66
Lucas Stachb6709082017-09-07 17:06:28 +020067static void etnaviv_iommuv1_domain_free(struct etnaviv_iommu_domain *domain)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010068{
Lucas Stachb6709082017-09-07 17:06:28 +020069 struct etnaviv_iommuv1_domain *etnaviv_domain =
70 to_etnaviv_domain(domain);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010071
Lucas Stach1af998b2018-04-17 12:00:46 +020072 dma_free_wc(etnaviv_domain->base.dev, PT_SIZE,
73 etnaviv_domain->pgtable_cpu, etnaviv_domain->pgtable_dma);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010074
Lucas Stach1af998b2018-04-17 12:00:46 +020075 dma_free_wc(etnaviv_domain->base.dev, SZ_4K,
76 etnaviv_domain->base.bad_page_cpu,
77 etnaviv_domain->base.bad_page_dma);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010078
79 kfree(etnaviv_domain);
80}
81
Lucas Stachb6709082017-09-07 17:06:28 +020082static int etnaviv_iommuv1_map(struct etnaviv_iommu_domain *domain,
83 unsigned long iova, phys_addr_t paddr,
84 size_t size, int prot)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010085{
Lucas Stachb6709082017-09-07 17:06:28 +020086 struct etnaviv_iommuv1_domain *etnaviv_domain = to_etnaviv_domain(domain);
Lucas Stachbd2442b2017-09-07 15:56:01 +020087 unsigned int index = (iova - GPU_MEM_START) / SZ_4K;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010088
89 if (size != SZ_4K)
90 return -EINVAL;
91
Lucas Stachb6709082017-09-07 17:06:28 +020092 etnaviv_domain->pgtable_cpu[index] = paddr;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010093
94 return 0;
95}
96
Lucas Stachb6709082017-09-07 17:06:28 +020097static size_t etnaviv_iommuv1_unmap(struct etnaviv_iommu_domain *domain,
The etnaviv authorsa8c21a52015-12-03 18:21:29 +010098 unsigned long iova, size_t size)
99{
Lucas Stachb6709082017-09-07 17:06:28 +0200100 struct etnaviv_iommuv1_domain *etnaviv_domain =
101 to_etnaviv_domain(domain);
Lucas Stachbd2442b2017-09-07 15:56:01 +0200102 unsigned int index = (iova - GPU_MEM_START) / SZ_4K;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100103
104 if (size != SZ_4K)
105 return -EINVAL;
106
Lucas Stachb6709082017-09-07 17:06:28 +0200107 etnaviv_domain->pgtable_cpu[index] = etnaviv_domain->base.bad_page_dma;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100108
109 return SZ_4K;
110}
111
Lucas Stachb6709082017-09-07 17:06:28 +0200112static size_t etnaviv_iommuv1_dump_size(struct etnaviv_iommu_domain *domain)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100113{
114 return PT_SIZE;
115}
116
Lucas Stachb6709082017-09-07 17:06:28 +0200117static void etnaviv_iommuv1_dump(struct etnaviv_iommu_domain *domain, void *buf)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100118{
Lucas Stachb6709082017-09-07 17:06:28 +0200119 struct etnaviv_iommuv1_domain *etnaviv_domain =
120 to_etnaviv_domain(domain);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100121
Lucas Stachb6709082017-09-07 17:06:28 +0200122 memcpy(buf, etnaviv_domain->pgtable_cpu, PT_SIZE);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100123}
124
Lucas Stach99aeeb72016-08-16 11:31:09 +0200125void etnaviv_iommuv1_restore(struct etnaviv_gpu *gpu)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100126{
Lucas Stachb6709082017-09-07 17:06:28 +0200127 struct etnaviv_iommuv1_domain *etnaviv_domain =
Lucas Stach99aeeb72016-08-16 11:31:09 +0200128 to_etnaviv_domain(gpu->mmu->domain);
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100129 u32 pgtable;
130
Lucas Stach99f861b2016-08-16 11:48:49 +0200131 /* set base addresses */
132 gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_RA, gpu->memory_base);
133 gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_FE, gpu->memory_base);
134 gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_TX, gpu->memory_base);
135 gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_PEZ, gpu->memory_base);
136 gpu_write(gpu, VIVS_MC_MEMORY_BASE_ADDR_PE, gpu->memory_base);
137
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100138 /* set page table address in MC */
Lucas Stachb6709082017-09-07 17:06:28 +0200139 pgtable = (u32)etnaviv_domain->pgtable_dma;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100140
141 gpu_write(gpu, VIVS_MC_MMU_FE_PAGE_TABLE, pgtable);
142 gpu_write(gpu, VIVS_MC_MMU_TX_PAGE_TABLE, pgtable);
143 gpu_write(gpu, VIVS_MC_MMU_PE_PAGE_TABLE, pgtable);
144 gpu_write(gpu, VIVS_MC_MMU_PEZ_PAGE_TABLE, pgtable);
145 gpu_write(gpu, VIVS_MC_MMU_RA_PAGE_TABLE, pgtable);
146}
147
Wei Yongjunf121e7d2018-01-11 11:34:59 +0000148static const struct etnaviv_iommu_domain_ops etnaviv_iommuv1_ops = {
Lucas Stachb6709082017-09-07 17:06:28 +0200149 .free = etnaviv_iommuv1_domain_free,
150 .map = etnaviv_iommuv1_map,
151 .unmap = etnaviv_iommuv1_unmap,
152 .dump_size = etnaviv_iommuv1_dump_size,
153 .dump = etnaviv_iommuv1_dump,
154};
155
156struct etnaviv_iommu_domain *
157etnaviv_iommuv1_domain_alloc(struct etnaviv_gpu *gpu)
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100158{
Lucas Stachb6709082017-09-07 17:06:28 +0200159 struct etnaviv_iommuv1_domain *etnaviv_domain;
160 struct etnaviv_iommu_domain *domain;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100161 int ret;
162
163 etnaviv_domain = kzalloc(sizeof(*etnaviv_domain), GFP_KERNEL);
164 if (!etnaviv_domain)
165 return NULL;
166
Lucas Stachb6709082017-09-07 17:06:28 +0200167 domain = &etnaviv_domain->base;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100168
Lucas Stachb6709082017-09-07 17:06:28 +0200169 domain->dev = gpu->dev;
170 domain->base = GPU_MEM_START;
171 domain->size = PT_ENTRIES * SZ_4K;
172 domain->ops = &etnaviv_iommuv1_ops;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100173
174 ret = __etnaviv_iommu_init(etnaviv_domain);
175 if (ret)
176 goto out_free;
177
Lucas Stachb6709082017-09-07 17:06:28 +0200178 return &etnaviv_domain->base;
The etnaviv authorsa8c21a52015-12-03 18:21:29 +0100179
180out_free:
181 kfree(etnaviv_domain);
182 return NULL;
183}