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> |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 16 | #include <linux/platform_device.h> |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include <asm/sizes.h> |
| 19 | #include <asm/page.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <mach/iommu.h> |
| 22 | #include <mach/iommu_domains.h> |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 23 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 25 | /* dummy 4k for overmapping */ |
| 26 | char iommu_dummy[2*PAGE_SIZE-4]; |
| 27 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 28 | struct msm_iommu_domain_state { |
| 29 | struct msm_iommu_domain *domains; |
| 30 | int ndomains; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 33 | static struct msm_iommu_domain_state domain_state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 35 | int msm_iommu_map_extra(struct iommu_domain *domain, |
| 36 | unsigned long start_iova, |
| 37 | unsigned long size, |
| 38 | int cached) |
| 39 | { |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 40 | int i, ret = 0; |
| 41 | struct scatterlist *sglist; |
| 42 | unsigned int nrpages = PFN_ALIGN(size) >> PAGE_SHIFT; |
| 43 | struct page *dummy_page = phys_to_page( |
| 44 | PFN_ALIGN(virt_to_phys(iommu_dummy))); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 45 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 46 | sglist = vmalloc(sizeof(*sglist) * nrpages); |
| 47 | if (!sglist) { |
| 48 | ret = -ENOMEM; |
| 49 | goto err1; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 52 | sg_init_table(sglist, nrpages); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 53 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 54 | for (i = 0; i < nrpages; i++) |
| 55 | sg_set_page(&sglist[i], dummy_page, PAGE_SIZE, 0); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 56 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 57 | ret = iommu_map_range(domain, start_iova, sglist, size, cached); |
| 58 | if (ret) { |
| 59 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 60 | __func__, start_iova, domain); |
| 61 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 62 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 63 | vfree(sglist); |
| 64 | err1: |
| 65 | return ret; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 69 | struct iommu_domain *msm_get_iommu_domain(int domain_num) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 70 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 71 | if (domain_num >= 0 && domain_num < domain_state.ndomains) |
| 72 | return domain_state.domains[domain_num].domain; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 73 | else |
| 74 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 77 | unsigned long msm_allocate_iova_address(unsigned int iommu_domain, |
| 78 | unsigned int partition_no, |
| 79 | unsigned long size, |
| 80 | unsigned long align) |
| 81 | { |
| 82 | struct mem_pool *pool; |
| 83 | unsigned long iova; |
| 84 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 85 | if (iommu_domain >= domain_state.ndomains) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 88 | if (partition_no >= domain_state.domains[iommu_domain].npools) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 89 | return 0; |
| 90 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 91 | pool = &domain_state.domains[iommu_domain].iova_pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 92 | |
| 93 | if (!pool->gpool) |
| 94 | return 0; |
| 95 | |
| 96 | iova = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align)); |
| 97 | if (iova) |
| 98 | pool->free -= size; |
| 99 | |
| 100 | return iova; |
| 101 | } |
| 102 | |
| 103 | void msm_free_iova_address(unsigned long iova, |
| 104 | unsigned int iommu_domain, |
| 105 | unsigned int partition_no, |
| 106 | unsigned long size) |
| 107 | { |
| 108 | struct mem_pool *pool; |
| 109 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 110 | if (iommu_domain >= domain_state.ndomains) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 111 | WARN(1, "Invalid domain %d\n", iommu_domain); |
| 112 | return; |
| 113 | } |
| 114 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 115 | if (partition_no >= domain_state.domains[iommu_domain].npools) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 116 | WARN(1, "Invalid partition %d for domain %d\n", |
| 117 | partition_no, iommu_domain); |
| 118 | return; |
| 119 | } |
| 120 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 121 | pool = &domain_state.domains[iommu_domain].iova_pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 122 | |
| 123 | if (!pool) |
| 124 | return; |
| 125 | |
| 126 | pool->free += size; |
| 127 | gen_pool_free(pool->gpool, iova, size); |
| 128 | } |
| 129 | |
| 130 | int msm_use_iommu() |
| 131 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 132 | /* |
| 133 | * If there are no domains, don't bother trying to use the iommu |
| 134 | */ |
| 135 | return domain_state.ndomains && iommu_found(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 138 | static int __init iommu_domain_probe(struct platform_device *pdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 140 | struct iommu_domains_pdata *p = pdev->dev.platform_data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 141 | int i, j; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 142 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 143 | if (!p) |
| 144 | return -ENODEV; |
| 145 | |
| 146 | domain_state.domains = p->domains; |
| 147 | domain_state.ndomains = p->ndomains; |
| 148 | |
| 149 | for (i = 0; i < domain_state.ndomains; i++) { |
| 150 | domain_state.domains[i].domain = iommu_domain_alloc( |
| 151 | p->domain_alloc_flags); |
| 152 | if (!domain_state.domains[i].domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 153 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 155 | for (j = 0; j < domain_state.domains[i].npools; j++) { |
| 156 | struct mem_pool *pool = &domain_state.domains[i]. |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 157 | iova_pools[j]; |
| 158 | mutex_init(&pool->pool_mutex); |
Olav Haugan | 2d19103 | 2012-02-28 09:46:31 -0800 | [diff] [blame] | 159 | if (pool->size) { |
| 160 | pool->gpool = gen_pool_create(PAGE_SHIFT, -1); |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 161 | |
Olav Haugan | 2d19103 | 2012-02-28 09:46:31 -0800 | [diff] [blame] | 162 | if (!pool->gpool) { |
| 163 | pr_err("%s: could not allocate pool\n", |
| 164 | __func__); |
| 165 | pr_err("%s: domain %d iova space %d\n", |
| 166 | __func__, i, j); |
| 167 | continue; |
| 168 | } |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 169 | |
Olav Haugan | 2d19103 | 2012-02-28 09:46:31 -0800 | [diff] [blame] | 170 | if (gen_pool_add(pool->gpool, pool->paddr, |
| 171 | pool->size, -1)) { |
| 172 | pr_err("%s: could not add memory\n", |
| 173 | __func__); |
| 174 | pr_err("%s: domain %d pool %d\n", |
| 175 | __func__, i, j); |
| 176 | gen_pool_destroy(pool->gpool); |
| 177 | pool->gpool = NULL; |
| 178 | continue; |
| 179 | } |
| 180 | } else { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 181 | pool->gpool = NULL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 182 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 186 | for (i = 0; i < p->nnames; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 187 | int domain_idx; |
| 188 | struct device *ctx = msm_iommu_get_ctx( |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 189 | p->domain_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 190 | |
| 191 | if (!ctx) |
| 192 | continue; |
| 193 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 194 | domain_idx = p->domain_names[i].domain; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 196 | if (!domain_state.domains[domain_idx].domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | continue; |
| 198 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 199 | if (iommu_attach_device(domain_state.domains[domain_idx].domain, |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 200 | ctx)) { |
| 201 | WARN(1, "%s: could not attach domain %d to context %s." |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 202 | " iommu programming will not occur.\n", |
| 203 | __func__, domain_idx, |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 204 | p->domain_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 205 | continue; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame^] | 211 | |
| 212 | static struct platform_driver iommu_domain_driver = { |
| 213 | .driver = { |
| 214 | .name = "iommu_domains", |
| 215 | .owner = THIS_MODULE |
| 216 | }, |
| 217 | }; |
| 218 | |
| 219 | static int __init msm_subsystem_iommu_init(void) |
| 220 | { |
| 221 | return platform_driver_probe(&iommu_domain_driver, iommu_domain_probe); |
| 222 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | device_initcall(msm_subsystem_iommu_init); |