Olav Haugan | ab77b1b | 2012-02-28 09:19:22 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <mach/msm_subsystem_map.h> |
| 14 | #include <linux/memory_alloc.h> |
| 15 | #include <linux/iommu.h> |
| 16 | #include <asm/sizes.h> |
| 17 | #include <asm/page.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <mach/iommu.h> |
| 20 | #include <mach/iommu_domains.h> |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 21 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 23 | /* dummy 4k for overmapping */ |
| 24 | char iommu_dummy[2*PAGE_SIZE-4]; |
| 25 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | struct msm_iommu_domain { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 27 | /* iommu domain to map in */ |
| 28 | struct iommu_domain *domain; |
| 29 | /* total number of allocations from this domain */ |
| 30 | atomic_t allocation_cnt; |
| 31 | /* number of iova pools */ |
| 32 | int npools; |
| 33 | /* |
| 34 | * array of gen_pools for allocating iovas. |
| 35 | * behavior is undefined if these overlap |
| 36 | */ |
| 37 | struct mem_pool *iova_pools; |
| 38 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 41 | |
| 42 | struct { |
| 43 | char *name; |
| 44 | int domain; |
| 45 | } msm_iommu_ctx_names[] = { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | |
| 49 | static struct msm_iommu_domain msm_iommu_domains[] = { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 52 | int msm_iommu_map_extra(struct iommu_domain *domain, |
| 53 | unsigned long start_iova, |
| 54 | unsigned long size, |
| 55 | int cached) |
| 56 | { |
| 57 | int i, ret; |
| 58 | unsigned long temp_iova; |
| 59 | |
| 60 | for (i = size, temp_iova = start_iova; i > 0; i -= SZ_4K, |
| 61 | temp_iova += SZ_4K) { |
| 62 | ret = iommu_map(domain, temp_iova, |
| 63 | PFN_ALIGN(virt_to_phys(iommu_dummy)), |
| 64 | get_order(SZ_4K), |
| 65 | 0); |
| 66 | |
| 67 | if (ret) { |
| 68 | pr_err("%s: could not map %lx to dummy page in domain" |
| 69 | " %p\n", |
| 70 | __func__, temp_iova, domain); |
| 71 | goto out; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | |
| 77 | out: |
| 78 | |
| 79 | for ( ; i < size; i += SZ_4K, temp_iova -= SZ_4K) |
| 80 | iommu_unmap(domain, temp_iova, get_order(SZ_4K)); |
| 81 | |
| 82 | return -EINVAL; |
| 83 | |
| 84 | } |
| 85 | |
| 86 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 87 | struct iommu_domain *msm_get_iommu_domain(int domain_num) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 89 | if (domain_num >= 0 && domain_num < MAX_DOMAINS) |
| 90 | return msm_iommu_domains[domain_num].domain; |
| 91 | else |
| 92 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Olav Haugan | ab77b1b | 2012-02-28 09:19:22 -0800 | [diff] [blame^] | 95 | static unsigned long subsystem_to_domain_tbl[] = { |
| 96 | VIDEO_DOMAIN, |
| 97 | VIDEO_DOMAIN, |
| 98 | CAMERA_DOMAIN, |
| 99 | DISPLAY_DOMAIN, |
| 100 | ROTATOR_DOMAIN, |
| 101 | 0xFFFFFFFF |
| 102 | }; |
| 103 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 104 | unsigned long msm_subsystem_get_domain_no(int subsys_id) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 105 | { |
Olav Haugan | ab77b1b | 2012-02-28 09:19:22 -0800 | [diff] [blame^] | 106 | if (subsys_id > INVALID_SUBSYS_ID && subsys_id <= MAX_SUBSYSTEM_ID && |
| 107 | subsys_id < ARRAY_SIZE(subsystem_to_domain_tbl)) |
| 108 | return subsystem_to_domain_tbl[subsys_id]; |
| 109 | else |
| 110 | return subsystem_to_domain_tbl[MAX_SUBSYSTEM_ID]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 111 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 113 | unsigned long msm_subsystem_get_partition_no(int subsys_id) |
| 114 | { |
| 115 | switch (subsys_id) { |
| 116 | case MSM_SUBSYSTEM_VIDEO_FWARE: |
| 117 | return VIDEO_FIRMWARE_POOL; |
| 118 | case MSM_SUBSYSTEM_VIDEO: |
Olav Haugan | ee36536 | 2012-02-16 09:31:37 -0800 | [diff] [blame] | 119 | return VIDEO_MAIN_POOL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 120 | case MSM_SUBSYSTEM_CAMERA: |
| 121 | case MSM_SUBSYSTEM_DISPLAY: |
| 122 | case MSM_SUBSYSTEM_ROTATOR: |
Olav Haugan | ee36536 | 2012-02-16 09:31:37 -0800 | [diff] [blame] | 123 | return GEN_POOL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 124 | default: |
| 125 | return 0xFFFFFFFF; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | unsigned long msm_allocate_iova_address(unsigned int iommu_domain, |
| 130 | unsigned int partition_no, |
| 131 | unsigned long size, |
| 132 | unsigned long align) |
| 133 | { |
| 134 | struct mem_pool *pool; |
| 135 | unsigned long iova; |
| 136 | |
| 137 | if (iommu_domain >= MAX_DOMAINS) |
| 138 | return 0; |
| 139 | |
| 140 | if (partition_no >= msm_iommu_domains[iommu_domain].npools) |
| 141 | return 0; |
| 142 | |
| 143 | pool = &msm_iommu_domains[iommu_domain].iova_pools[partition_no]; |
| 144 | |
| 145 | if (!pool->gpool) |
| 146 | return 0; |
| 147 | |
| 148 | iova = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align)); |
| 149 | if (iova) |
| 150 | pool->free -= size; |
| 151 | |
| 152 | return iova; |
| 153 | } |
| 154 | |
| 155 | void msm_free_iova_address(unsigned long iova, |
| 156 | unsigned int iommu_domain, |
| 157 | unsigned int partition_no, |
| 158 | unsigned long size) |
| 159 | { |
| 160 | struct mem_pool *pool; |
| 161 | |
| 162 | if (iommu_domain >= MAX_DOMAINS) { |
| 163 | WARN(1, "Invalid domain %d\n", iommu_domain); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if (partition_no >= msm_iommu_domains[iommu_domain].npools) { |
| 168 | WARN(1, "Invalid partition %d for domain %d\n", |
| 169 | partition_no, iommu_domain); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | pool = &msm_iommu_domains[iommu_domain].iova_pools[partition_no]; |
| 174 | |
| 175 | if (!pool) |
| 176 | return; |
| 177 | |
| 178 | pool->free += size; |
| 179 | gen_pool_free(pool->gpool, iova, size); |
| 180 | } |
| 181 | |
| 182 | int msm_use_iommu() |
| 183 | { |
Laura Abbott | d561f34 | 2012-02-07 16:26:39 -0800 | [diff] [blame] | 184 | /* Kill use of the iommu by these clients for now. */ |
| 185 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static int __init msm_subsystem_iommu_init(void) |
| 189 | { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 190 | int i, j; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 192 | for (i = 0; i < ARRAY_SIZE(msm_iommu_domains); i++) { |
| 193 | msm_iommu_domains[i].domain = iommu_domain_alloc(0); |
| 194 | if (!msm_iommu_domains[i].domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 197 | for (j = 0; j < msm_iommu_domains[i].npools; j++) { |
| 198 | struct mem_pool *pool = &msm_iommu_domains[i]. |
| 199 | iova_pools[j]; |
| 200 | mutex_init(&pool->pool_mutex); |
| 201 | pool->gpool = gen_pool_create(PAGE_SHIFT, -1); |
| 202 | |
| 203 | if (!pool->gpool) { |
| 204 | pr_err("%s: domain %d: could not allocate iova" |
| 205 | " pool. iommu programming will not work" |
| 206 | " with iova space %d\n", __func__, |
| 207 | i, j); |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | if (gen_pool_add(pool->gpool, pool->paddr, pool->size, |
| 212 | -1)) { |
| 213 | pr_err("%s: domain %d: could not add memory to" |
| 214 | " iova pool. iommu programming will not" |
| 215 | " work with iova space %d\n", __func__, |
| 216 | i, j); |
| 217 | gen_pool_destroy(pool->gpool); |
| 218 | pool->gpool = NULL; |
| 219 | continue; |
| 220 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | for (i = 0; i < ARRAY_SIZE(msm_iommu_ctx_names); i++) { |
| 225 | int domain_idx; |
| 226 | struct device *ctx = msm_iommu_get_ctx( |
| 227 | msm_iommu_ctx_names[i].name); |
| 228 | |
| 229 | if (!ctx) |
| 230 | continue; |
| 231 | |
| 232 | domain_idx = msm_iommu_ctx_names[i].domain; |
| 233 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 234 | if (!msm_iommu_domains[domain_idx].domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | continue; |
| 236 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 237 | if (iommu_attach_device(msm_iommu_domains[domain_idx].domain, |
| 238 | ctx)) { |
| 239 | WARN(1, "%s: could not attach domain %d to context %s." |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | " iommu programming will not occur.\n", |
| 241 | __func__, domain_idx, |
| 242 | msm_iommu_ctx_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 243 | continue; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | device_initcall(msm_subsystem_iommu_init); |