Lucas Stach | f6ffbd4 | 2018-05-08 16:20:54 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 2 | /* |
Lucas Stach | f6ffbd4 | 2018-05-08 16:20:54 +0200 | [diff] [blame] | 3 | * Copyright (C) 2014-2018 Etnaviv Project |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 6 | #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 Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 22 | struct etnaviv_iommuv1_domain { |
| 23 | struct etnaviv_iommu_domain base; |
| 24 | u32 *pgtable_cpu; |
| 25 | dma_addr_t pgtable_dma; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 28 | static struct etnaviv_iommuv1_domain * |
| 29 | to_etnaviv_domain(struct etnaviv_iommu_domain *domain) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 30 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 31 | return container_of(domain, struct etnaviv_iommuv1_domain, base); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 34 | static int __etnaviv_iommu_init(struct etnaviv_iommuv1_domain *etnaviv_domain) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 35 | { |
| 36 | u32 *p; |
Lucas Stach | 1a54049 | 2017-09-07 15:36:57 +0200 | [diff] [blame] | 37 | int i; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 38 | |
Lucas Stach | 1af998b | 2018-04-17 12:00:46 +0200 | [diff] [blame] | 39 | 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 Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 43 | if (!etnaviv_domain->base.bad_page_cpu) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 44 | return -ENOMEM; |
| 45 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 46 | p = etnaviv_domain->base.bad_page_cpu; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 47 | for (i = 0; i < SZ_4K / 4; i++) |
| 48 | *p++ = 0xdead55aa; |
| 49 | |
Lucas Stach | 1af998b | 2018-04-17 12:00:46 +0200 | [diff] [blame] | 50 | etnaviv_domain->pgtable_cpu = dma_alloc_wc(etnaviv_domain->base.dev, |
| 51 | PT_SIZE, |
| 52 | &etnaviv_domain->pgtable_dma, |
| 53 | GFP_KERNEL); |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 54 | if (!etnaviv_domain->pgtable_cpu) { |
Lucas Stach | 1af998b | 2018-04-17 12:00:46 +0200 | [diff] [blame] | 55 | dma_free_wc(etnaviv_domain->base.dev, SZ_4K, |
| 56 | etnaviv_domain->base.bad_page_cpu, |
| 57 | etnaviv_domain->base.bad_page_dma); |
Lucas Stach | 1a54049 | 2017-09-07 15:36:57 +0200 | [diff] [blame] | 58 | return -ENOMEM; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Lucas Stach | 2f20fc4 | 2017-11-27 15:33:28 +0100 | [diff] [blame] | 61 | memset32(etnaviv_domain->pgtable_cpu, etnaviv_domain->base.bad_page_dma, |
| 62 | PT_ENTRIES); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 63 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 67 | static void etnaviv_iommuv1_domain_free(struct etnaviv_iommu_domain *domain) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 68 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 69 | struct etnaviv_iommuv1_domain *etnaviv_domain = |
| 70 | to_etnaviv_domain(domain); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 71 | |
Lucas Stach | 1af998b | 2018-04-17 12:00:46 +0200 | [diff] [blame] | 72 | dma_free_wc(etnaviv_domain->base.dev, PT_SIZE, |
| 73 | etnaviv_domain->pgtable_cpu, etnaviv_domain->pgtable_dma); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 74 | |
Lucas Stach | 1af998b | 2018-04-17 12:00:46 +0200 | [diff] [blame] | 75 | 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 authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 78 | |
| 79 | kfree(etnaviv_domain); |
| 80 | } |
| 81 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 82 | static 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 authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 85 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 86 | struct etnaviv_iommuv1_domain *etnaviv_domain = to_etnaviv_domain(domain); |
Lucas Stach | bd2442b | 2017-09-07 15:56:01 +0200 | [diff] [blame] | 87 | unsigned int index = (iova - GPU_MEM_START) / SZ_4K; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 88 | |
| 89 | if (size != SZ_4K) |
| 90 | return -EINVAL; |
| 91 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 92 | etnaviv_domain->pgtable_cpu[index] = paddr; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 97 | static size_t etnaviv_iommuv1_unmap(struct etnaviv_iommu_domain *domain, |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 98 | unsigned long iova, size_t size) |
| 99 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 100 | struct etnaviv_iommuv1_domain *etnaviv_domain = |
| 101 | to_etnaviv_domain(domain); |
Lucas Stach | bd2442b | 2017-09-07 15:56:01 +0200 | [diff] [blame] | 102 | unsigned int index = (iova - GPU_MEM_START) / SZ_4K; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 103 | |
| 104 | if (size != SZ_4K) |
| 105 | return -EINVAL; |
| 106 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 107 | etnaviv_domain->pgtable_cpu[index] = etnaviv_domain->base.bad_page_dma; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 108 | |
| 109 | return SZ_4K; |
| 110 | } |
| 111 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 112 | static size_t etnaviv_iommuv1_dump_size(struct etnaviv_iommu_domain *domain) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 113 | { |
| 114 | return PT_SIZE; |
| 115 | } |
| 116 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 117 | static void etnaviv_iommuv1_dump(struct etnaviv_iommu_domain *domain, void *buf) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 118 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 119 | struct etnaviv_iommuv1_domain *etnaviv_domain = |
| 120 | to_etnaviv_domain(domain); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 121 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 122 | memcpy(buf, etnaviv_domain->pgtable_cpu, PT_SIZE); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Lucas Stach | 99aeeb7 | 2016-08-16 11:31:09 +0200 | [diff] [blame] | 125 | void etnaviv_iommuv1_restore(struct etnaviv_gpu *gpu) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 126 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 127 | struct etnaviv_iommuv1_domain *etnaviv_domain = |
Lucas Stach | 99aeeb7 | 2016-08-16 11:31:09 +0200 | [diff] [blame] | 128 | to_etnaviv_domain(gpu->mmu->domain); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 129 | u32 pgtable; |
| 130 | |
Lucas Stach | 99f861b | 2016-08-16 11:48:49 +0200 | [diff] [blame] | 131 | /* 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 authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 138 | /* set page table address in MC */ |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 139 | pgtable = (u32)etnaviv_domain->pgtable_dma; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 140 | |
| 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 Yongjun | f121e7d | 2018-01-11 11:34:59 +0000 | [diff] [blame] | 148 | static const struct etnaviv_iommu_domain_ops etnaviv_iommuv1_ops = { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 149 | .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 | |
| 156 | struct etnaviv_iommu_domain * |
| 157 | etnaviv_iommuv1_domain_alloc(struct etnaviv_gpu *gpu) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 158 | { |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 159 | struct etnaviv_iommuv1_domain *etnaviv_domain; |
| 160 | struct etnaviv_iommu_domain *domain; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 161 | int ret; |
| 162 | |
| 163 | etnaviv_domain = kzalloc(sizeof(*etnaviv_domain), GFP_KERNEL); |
| 164 | if (!etnaviv_domain) |
| 165 | return NULL; |
| 166 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 167 | domain = &etnaviv_domain->base; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 168 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 169 | 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 authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 173 | |
| 174 | ret = __etnaviv_iommu_init(etnaviv_domain); |
| 175 | if (ret) |
| 176 | goto out_free; |
| 177 | |
Lucas Stach | b670908 | 2017-09-07 17:06:28 +0200 | [diff] [blame] | 178 | return &etnaviv_domain->base; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 179 | |
| 180 | out_free: |
| 181 | kfree(etnaviv_domain); |
| 182 | return NULL; |
| 183 | } |