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> |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 16 | #include <linux/vmalloc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 17 | #include <asm/sizes.h> |
| 18 | #include <asm/page.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <mach/iommu.h> |
| 21 | #include <mach/iommu_domains.h> |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 22 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 23 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 24 | /* dummy 4k for overmapping */ |
| 25 | char iommu_dummy[2*PAGE_SIZE-4]; |
| 26 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | struct msm_iommu_domain { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 28 | /* iommu domain to map in */ |
| 29 | struct iommu_domain *domain; |
| 30 | /* total number of allocations from this domain */ |
| 31 | atomic_t allocation_cnt; |
| 32 | /* number of iova pools */ |
| 33 | int npools; |
| 34 | /* |
| 35 | * array of gen_pools for allocating iovas. |
| 36 | * behavior is undefined if these overlap |
| 37 | */ |
| 38 | struct mem_pool *iova_pools; |
| 39 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | |
| 43 | struct { |
| 44 | char *name; |
| 45 | int domain; |
| 46 | } msm_iommu_ctx_names[] = { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | |
| 50 | static struct msm_iommu_domain msm_iommu_domains[] = { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 53 | int msm_iommu_map_extra(struct iommu_domain *domain, |
| 54 | unsigned long start_iova, |
| 55 | unsigned long size, |
| 56 | int cached) |
| 57 | { |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 58 | int i, ret = 0; |
| 59 | struct scatterlist *sglist; |
| 60 | unsigned int nrpages = PFN_ALIGN(size) >> PAGE_SHIFT; |
| 61 | struct page *dummy_page = phys_to_page( |
| 62 | PFN_ALIGN(virt_to_phys(iommu_dummy))); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 63 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 64 | sglist = vmalloc(sizeof(*sglist) * nrpages); |
| 65 | if (!sglist) { |
| 66 | ret = -ENOMEM; |
| 67 | goto err1; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 70 | sg_init_table(sglist, nrpages); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 71 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 72 | for (i = 0; i < nrpages; i++) |
| 73 | sg_set_page(&sglist[i], dummy_page, PAGE_SIZE, 0); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 74 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 75 | ret = iommu_map_range(domain, start_iova, sglist, size, cached); |
| 76 | if (ret) { |
| 77 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 78 | __func__, start_iova, domain); |
| 79 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 80 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame^] | 81 | vfree(sglist); |
| 82 | err1: |
| 83 | return ret; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 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); |