Shubhraprakash Das | 7944795 | 2012-04-26 18:12:23 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [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 <linux/types.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/genalloc.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/iommu.h> |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 19 | #include <linux/msm_kgsl.h> |
| 20 | |
| 21 | #include "kgsl.h" |
| 22 | #include "kgsl_device.h" |
| 23 | #include "kgsl_mmu.h" |
| 24 | #include "kgsl_sharedmem.h" |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 25 | #include "kgsl_iommu.h" |
Shubhraprakash Das | e9541a3 | 2012-05-09 22:25:55 -0600 | [diff] [blame] | 26 | #include "adreno_pm4types.h" |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 27 | #include "adreno.h" |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 28 | |
Shubhraprakash Das | 9fb38ac | 2012-05-01 00:41:30 -0600 | [diff] [blame] | 29 | /* |
| 30 | * kgsl_iommu_disable_clk - Disable iommu clocks |
| 31 | * @mmu - Pointer to mmu structure |
| 32 | * |
| 33 | * Disables iommu clocks |
| 34 | * Return - void |
| 35 | */ |
| 36 | static void kgsl_iommu_disable_clk(struct kgsl_mmu *mmu) |
| 37 | { |
| 38 | struct kgsl_iommu *iommu = mmu->priv; |
| 39 | struct msm_iommu_drvdata *iommu_drvdata; |
| 40 | int i, j; |
| 41 | |
| 42 | for (i = 0; i < iommu->unit_count; i++) { |
| 43 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 44 | for (j = 0; j < iommu_unit->dev_count; j++) { |
| 45 | if (!iommu_unit->dev[j].clk_enabled) |
| 46 | continue; |
| 47 | iommu_drvdata = dev_get_drvdata( |
| 48 | iommu_unit->dev[j].dev->parent); |
| 49 | if (iommu_drvdata->clk) |
| 50 | clk_disable_unprepare(iommu_drvdata->clk); |
| 51 | clk_disable_unprepare(iommu_drvdata->pclk); |
| 52 | iommu_unit->dev[j].clk_enabled = false; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * kgsl_iommu_enable_clk - Enable iommu clocks |
| 59 | * @mmu - Pointer to mmu structure |
| 60 | * @ctx_id - The context bank whose clocks are to be turned on |
| 61 | * |
| 62 | * Enables iommu clocks of a given context |
| 63 | * Return: 0 on success else error code |
| 64 | */ |
| 65 | static int kgsl_iommu_enable_clk(struct kgsl_mmu *mmu, |
| 66 | int ctx_id) |
| 67 | { |
| 68 | int ret = 0; |
| 69 | int i, j; |
| 70 | struct kgsl_iommu *iommu = mmu->priv; |
| 71 | struct msm_iommu_drvdata *iommu_drvdata; |
| 72 | |
| 73 | for (i = 0; i < iommu->unit_count; i++) { |
| 74 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 75 | for (j = 0; j < iommu_unit->dev_count; j++) { |
| 76 | if (iommu_unit->dev[j].clk_enabled || |
| 77 | ctx_id != iommu_unit->dev[j].ctx_id) |
| 78 | continue; |
| 79 | iommu_drvdata = |
| 80 | dev_get_drvdata(iommu_unit->dev[j].dev->parent); |
| 81 | ret = clk_prepare_enable(iommu_drvdata->pclk); |
| 82 | if (ret) |
| 83 | goto done; |
| 84 | if (iommu_drvdata->clk) { |
| 85 | ret = clk_prepare_enable(iommu_drvdata->clk); |
| 86 | if (ret) { |
| 87 | clk_disable_unprepare( |
| 88 | iommu_drvdata->pclk); |
| 89 | goto done; |
| 90 | } |
| 91 | } |
| 92 | iommu_unit->dev[j].clk_enabled = true; |
| 93 | } |
| 94 | } |
| 95 | done: |
| 96 | if (ret) |
| 97 | kgsl_iommu_disable_clk(mmu); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Shubhraprakash Das | 48d9730 | 2012-05-07 12:16:08 -0600 | [diff] [blame] | 101 | /* |
| 102 | * kgsl_iommu_pt_equal - Check if pagetables are equal |
| 103 | * @pt - Pointer to pagetable |
| 104 | * @pt_base - Address of a pagetable that the IOMMU register is |
| 105 | * programmed with |
| 106 | * |
| 107 | * Checks whether the pt_base is equal to the base address of |
| 108 | * the pagetable which is contained in the pt structure |
| 109 | * Return - Non-zero if the pagetable addresses are equal else 0 |
| 110 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 111 | static int kgsl_iommu_pt_equal(struct kgsl_pagetable *pt, |
| 112 | unsigned int pt_base) |
| 113 | { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 114 | struct kgsl_iommu_pt *iommu_pt = pt ? pt->priv : NULL; |
| 115 | unsigned int domain_ptbase = iommu_pt ? |
| 116 | iommu_get_pt_base_addr(iommu_pt->domain) : 0; |
Shubhraprakash Das | 48d9730 | 2012-05-07 12:16:08 -0600 | [diff] [blame] | 117 | /* Only compare the valid address bits of the pt_base */ |
| 118 | domain_ptbase &= (KGSL_IOMMU_TTBR0_PA_MASK << |
| 119 | KGSL_IOMMU_TTBR0_PA_SHIFT); |
| 120 | pt_base &= (KGSL_IOMMU_TTBR0_PA_MASK << |
| 121 | KGSL_IOMMU_TTBR0_PA_SHIFT); |
| 122 | return domain_ptbase && pt_base && |
| 123 | (domain_ptbase == pt_base); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 126 | /* |
| 127 | * kgsl_iommu_destroy_pagetable - Free up reaources help by a pagetable |
| 128 | * @mmu_specific_pt - Pointer to pagetable which is to be freed |
| 129 | * |
| 130 | * Return - void |
| 131 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 132 | static void kgsl_iommu_destroy_pagetable(void *mmu_specific_pt) |
| 133 | { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 134 | struct kgsl_iommu_pt *iommu_pt = mmu_specific_pt; |
| 135 | if (iommu_pt->domain) |
| 136 | iommu_domain_free(iommu_pt->domain); |
| 137 | if (iommu_pt->iommu) { |
| 138 | if ((KGSL_IOMMU_ASID_REUSE == iommu_pt->asid) && |
| 139 | iommu_pt->iommu->asid_reuse) |
| 140 | iommu_pt->iommu->asid_reuse--; |
| 141 | if (!iommu_pt->iommu->asid_reuse || |
| 142 | (KGSL_IOMMU_ASID_REUSE != iommu_pt->asid)) |
| 143 | clear_bit(iommu_pt->asid, iommu_pt->iommu->asids); |
| 144 | } |
| 145 | kfree(iommu_pt); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 148 | /* |
| 149 | * kgsl_iommu_create_pagetable - Create a IOMMU pagetable |
| 150 | * |
| 151 | * Allocate memory to hold a pagetable and allocate the IOMMU |
| 152 | * domain which is the actual IOMMU pagetable |
| 153 | * Return - void |
| 154 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 155 | void *kgsl_iommu_create_pagetable(void) |
| 156 | { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 157 | struct kgsl_iommu_pt *iommu_pt; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 158 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 159 | iommu_pt = kzalloc(sizeof(struct kgsl_iommu_pt), GFP_KERNEL); |
| 160 | if (!iommu_pt) { |
| 161 | KGSL_CORE_ERR("kzalloc(%d) failed\n", |
| 162 | sizeof(struct kgsl_iommu_pt)); |
| 163 | return NULL; |
| 164 | } |
Jordan Crouse | 523800f | 2012-04-16 14:14:45 -0600 | [diff] [blame] | 165 | iommu_pt->domain = iommu_domain_alloc(MSM_IOMMU_DOMAIN_PT_CACHEABLE); |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 166 | if (!iommu_pt->domain) { |
| 167 | KGSL_CORE_ERR("Failed to create iommu domain\n"); |
| 168 | kfree(iommu_pt); |
| 169 | return NULL; |
| 170 | } |
| 171 | return iommu_pt; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 172 | } |
| 173 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 174 | /* |
| 175 | * kgsl_detach_pagetable_iommu_domain - Detach the IOMMU unit from a |
| 176 | * pagetable |
| 177 | * @mmu - Pointer to the device mmu structure |
| 178 | * @priv - Flag indicating whether the private or user context is to be |
| 179 | * detached |
| 180 | * |
| 181 | * Detach the IOMMU unit with the domain that is contained in the |
| 182 | * hwpagetable of the given mmu. After detaching the IOMMU unit is not |
| 183 | * in use because the PTBR will not be set after a detach |
| 184 | * Return - void |
| 185 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 186 | static void kgsl_detach_pagetable_iommu_domain(struct kgsl_mmu *mmu) |
| 187 | { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 188 | struct kgsl_iommu_pt *iommu_pt; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 189 | struct kgsl_iommu *iommu = mmu->priv; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 190 | int i, j; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 191 | |
| 192 | BUG_ON(mmu->hwpagetable == NULL); |
| 193 | BUG_ON(mmu->hwpagetable->priv == NULL); |
| 194 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 195 | iommu_pt = mmu->hwpagetable->priv; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 196 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 197 | for (i = 0; i < iommu->unit_count; i++) { |
| 198 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 199 | for (j = 0; j < iommu_unit->dev_count; j++) { |
| 200 | if (iommu_unit->dev[j].attached) { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 201 | iommu_detach_device(iommu_pt->domain, |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 202 | iommu_unit->dev[j].dev); |
| 203 | iommu_unit->dev[j].attached = false; |
| 204 | KGSL_MEM_INFO(mmu->device, "iommu %p detached " |
| 205 | "from user dev of MMU: %p\n", |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 206 | iommu_pt->domain, mmu); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 207 | } |
| 208 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 212 | /* |
| 213 | * kgsl_attach_pagetable_iommu_domain - Attach the IOMMU unit to a |
| 214 | * pagetable, i.e set the IOMMU's PTBR to the pagetable address and |
| 215 | * setup other IOMMU registers for the device so that it becomes |
| 216 | * active |
| 217 | * @mmu - Pointer to the device mmu structure |
| 218 | * @priv - Flag indicating whether the private or user context is to be |
| 219 | * attached |
| 220 | * |
| 221 | * Attach the IOMMU unit with the domain that is contained in the |
| 222 | * hwpagetable of the given mmu. |
| 223 | * Return - 0 on success else error code |
| 224 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 225 | static int kgsl_attach_pagetable_iommu_domain(struct kgsl_mmu *mmu) |
| 226 | { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 227 | struct kgsl_iommu_pt *iommu_pt; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 228 | struct kgsl_iommu *iommu = mmu->priv; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 229 | int i, j, ret = 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 230 | |
| 231 | BUG_ON(mmu->hwpagetable == NULL); |
| 232 | BUG_ON(mmu->hwpagetable->priv == NULL); |
| 233 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 234 | iommu_pt = mmu->hwpagetable->priv; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 235 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 236 | /* |
| 237 | * Loop through all the iommu devcies under all iommu units and |
| 238 | * attach the domain |
| 239 | */ |
| 240 | for (i = 0; i < iommu->unit_count; i++) { |
| 241 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 242 | for (j = 0; j < iommu_unit->dev_count; j++) { |
| 243 | if (!iommu_unit->dev[j].attached) { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 244 | ret = iommu_attach_device(iommu_pt->domain, |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 245 | iommu_unit->dev[j].dev); |
| 246 | if (ret) { |
| 247 | KGSL_MEM_ERR(mmu->device, |
| 248 | "Failed to attach device, err %d\n", |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 249 | ret); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 250 | goto done; |
| 251 | } |
| 252 | iommu_unit->dev[j].attached = true; |
| 253 | KGSL_MEM_INFO(mmu->device, |
| 254 | "iommu pt %p attached to dev %p, ctx_id %d\n", |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 255 | iommu_pt->domain, iommu_unit->dev[j].dev, |
| 256 | iommu_unit->dev[j].ctx_id); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 257 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 258 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 259 | } |
| 260 | done: |
| 261 | return ret; |
| 262 | } |
| 263 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 264 | /* |
| 265 | * _get_iommu_ctxs - Get device pointer to IOMMU contexts |
| 266 | * @mmu - Pointer to mmu device |
| 267 | * data - Pointer to the platform data containing information about |
| 268 | * iommu devices for one iommu unit |
| 269 | * unit_id - The IOMMU unit number. This is not a specific ID but just |
| 270 | * a serial number. The serial numbers are treated as ID's of the |
| 271 | * IOMMU units |
| 272 | * |
| 273 | * Return - 0 on success else error code |
| 274 | */ |
| 275 | static int _get_iommu_ctxs(struct kgsl_mmu *mmu, |
| 276 | struct kgsl_device_iommu_data *data, unsigned int unit_id) |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 277 | { |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 278 | struct kgsl_iommu *iommu = mmu->priv; |
| 279 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[unit_id]; |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 280 | int i; |
| 281 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 282 | if (data->iommu_ctx_count > KGSL_IOMMU_MAX_DEVS_PER_UNIT) { |
| 283 | KGSL_CORE_ERR("Too many iommu devices defined for an " |
| 284 | "IOMMU unit\n"); |
| 285 | return -EINVAL; |
| 286 | } |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 287 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 288 | for (i = 0; i < data->iommu_ctx_count; i++) { |
| 289 | if (!data->iommu_ctxs[i].iommu_ctx_name) |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 290 | continue; |
| 291 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 292 | iommu_unit->dev[iommu_unit->dev_count].dev = |
| 293 | msm_iommu_get_ctx(data->iommu_ctxs[i].iommu_ctx_name); |
| 294 | if (iommu_unit->dev[iommu_unit->dev_count].dev == NULL) { |
| 295 | KGSL_CORE_ERR("Failed to get iommu dev handle for " |
| 296 | "device %s\n", data->iommu_ctxs[i].iommu_ctx_name); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | } |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 299 | if (KGSL_IOMMU_CONTEXT_USER != data->iommu_ctxs[i].ctx_id && |
| 300 | KGSL_IOMMU_CONTEXT_PRIV != data->iommu_ctxs[i].ctx_id) { |
| 301 | KGSL_CORE_ERR("Invalid context ID defined: %d\n", |
| 302 | data->iommu_ctxs[i].ctx_id); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | iommu_unit->dev[iommu_unit->dev_count].ctx_id = |
| 306 | data->iommu_ctxs[i].ctx_id; |
| 307 | KGSL_DRV_INFO(mmu->device, |
| 308 | "Obtained dev handle %p for iommu context %s\n", |
| 309 | iommu_unit->dev[iommu_unit->dev_count].dev, |
| 310 | data->iommu_ctxs[i].iommu_ctx_name); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 311 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 312 | iommu_unit->dev_count++; |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 318 | /* |
| 319 | * kgsl_get_iommu_ctxt - Get device pointer to IOMMU contexts |
| 320 | * @mmu - Pointer to mmu device |
| 321 | * |
| 322 | * Get the device pointers for the IOMMU user and priv contexts of the |
| 323 | * kgsl device |
| 324 | * Return - 0 on success else error code |
| 325 | */ |
| 326 | static int kgsl_get_iommu_ctxt(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 327 | { |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 328 | struct platform_device *pdev = |
| 329 | container_of(mmu->device->parentdev, struct platform_device, |
| 330 | dev); |
| 331 | struct kgsl_device_platform_data *pdata_dev = pdev->dev.platform_data; |
| 332 | struct kgsl_iommu *iommu = mmu->device->mmu.priv; |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 333 | int i, ret = 0; |
| 334 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 335 | /* Go through the IOMMU data and get all the context devices */ |
| 336 | if (KGSL_IOMMU_MAX_UNITS < pdata_dev->iommu_count) { |
| 337 | KGSL_CORE_ERR("Too many IOMMU units defined\n"); |
| 338 | ret = -EINVAL; |
| 339 | goto done; |
| 340 | } |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 341 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 342 | for (i = 0; i < pdata_dev->iommu_count; i++) { |
| 343 | ret = _get_iommu_ctxs(mmu, &pdata_dev->iommu_data[i], i); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 344 | if (ret) |
| 345 | break; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 346 | } |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 347 | iommu->unit_count = pdata_dev->iommu_count; |
| 348 | done: |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 349 | return ret; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 350 | } |
| 351 | |
Shubhraprakash Das | e9eefd7 | 2012-05-01 01:44:59 -0600 | [diff] [blame] | 352 | /* |
| 353 | * kgsl_set_register_map - Map the IOMMU regsiters in the memory descriptors |
| 354 | * of the respective iommu units |
| 355 | * @mmu - Pointer to mmu structure |
| 356 | * |
| 357 | * Return - 0 on success else error code |
| 358 | */ |
| 359 | static int kgsl_set_register_map(struct kgsl_mmu *mmu) |
| 360 | { |
| 361 | struct platform_device *pdev = |
| 362 | container_of(mmu->device->parentdev, struct platform_device, |
| 363 | dev); |
| 364 | struct kgsl_device_platform_data *pdata_dev = pdev->dev.platform_data; |
| 365 | struct kgsl_iommu *iommu = mmu->device->mmu.priv; |
| 366 | struct kgsl_iommu_unit *iommu_unit; |
| 367 | int i = 0, ret = 0; |
| 368 | |
| 369 | for (; i < pdata_dev->iommu_count; i++) { |
| 370 | struct kgsl_device_iommu_data data = pdata_dev->iommu_data[i]; |
| 371 | iommu_unit = &iommu->iommu_units[i]; |
| 372 | /* set up the IOMMU register map for the given IOMMU unit */ |
| 373 | if (!data.physstart || !data.physend) { |
| 374 | KGSL_CORE_ERR("The register range for IOMMU unit not" |
| 375 | " specified\n"); |
| 376 | ret = -EINVAL; |
| 377 | goto err; |
| 378 | } |
| 379 | iommu_unit->reg_map.hostptr = ioremap(data.physstart, |
| 380 | data.physend - data.physstart + 1); |
| 381 | if (!iommu_unit->reg_map.hostptr) { |
| 382 | KGSL_CORE_ERR("Failed to map SMMU register address " |
| 383 | "space from %x to %x\n", data.physstart, |
| 384 | data.physend - data.physstart + 1); |
| 385 | ret = -ENOMEM; |
| 386 | i--; |
| 387 | goto err; |
| 388 | } |
| 389 | iommu_unit->reg_map.size = data.physend - data.physstart + 1; |
| 390 | iommu_unit->reg_map.physaddr = data.physstart; |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 391 | memdesc_sg_phys(&iommu_unit->reg_map, data.physstart, |
| 392 | iommu_unit->reg_map.size); |
Shubhraprakash Das | e9eefd7 | 2012-05-01 01:44:59 -0600 | [diff] [blame] | 393 | } |
| 394 | iommu->unit_count = pdata_dev->iommu_count; |
| 395 | return ret; |
| 396 | err: |
| 397 | /* Unmap any mapped IOMMU regions */ |
| 398 | for (; i >= 0; i--) { |
| 399 | iommu_unit = &iommu->iommu_units[i]; |
| 400 | iounmap(iommu_unit->reg_map.hostptr); |
| 401 | iommu_unit->reg_map.size = 0; |
| 402 | iommu_unit->reg_map.physaddr = 0; |
| 403 | } |
| 404 | return ret; |
| 405 | } |
| 406 | |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 407 | /* |
| 408 | * kgsl_iommu_pt_get_base_addr - Get the address of the pagetable that the |
| 409 | * IOMMU ttbr0 register is programmed with |
| 410 | * @pt - kgsl pagetable pointer that contains the IOMMU domain pointer |
| 411 | * |
| 412 | * Return - actual pagetable address that the ttbr0 register is programmed |
| 413 | * with |
| 414 | */ |
| 415 | static unsigned int kgsl_iommu_pt_get_base_addr(struct kgsl_pagetable *pt) |
| 416 | { |
| 417 | struct kgsl_iommu_pt *iommu_pt = pt->priv; |
| 418 | return iommu_get_pt_base_addr(iommu_pt->domain); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * kgsl_iommu_get_pt_lsb - Return the lsb of the ttbr0 IOMMU register |
| 423 | * @mmu - Pointer to mmu structure |
| 424 | * @hostptr - Pointer to the IOMMU register map. This is used to match |
| 425 | * the iommu device whose lsb value is to be returned |
| 426 | * @ctx_id - The context bank whose lsb valus is to be returned |
| 427 | * Return - returns the lsb which is the last 14 bits of the ttbr0 IOMMU |
| 428 | * register. ttbr0 is the actual PTBR for of the IOMMU. The last 14 bits |
| 429 | * are only programmed once in the beginning when a domain is attached |
| 430 | * does not change. |
| 431 | */ |
| 432 | static int kgsl_iommu_get_pt_lsb(struct kgsl_mmu *mmu, |
| 433 | unsigned int unit_id, |
| 434 | enum kgsl_iommu_context_id ctx_id) |
| 435 | { |
| 436 | struct kgsl_iommu *iommu = mmu->priv; |
| 437 | int i, j; |
| 438 | for (i = 0; i < iommu->unit_count; i++) { |
| 439 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 440 | for (j = 0; j < iommu_unit->dev_count; j++) |
| 441 | if (unit_id == i && |
| 442 | ctx_id == iommu_unit->dev[j].ctx_id) |
| 443 | return iommu_unit->dev[j].pt_lsb; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 448 | static void kgsl_iommu_setstate(struct kgsl_mmu *mmu, |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 449 | struct kgsl_pagetable *pagetable) |
| 450 | { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 451 | if (mmu->flags & KGSL_FLAGS_STARTED) { |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 452 | struct kgsl_iommu *iommu = mmu->priv; |
| 453 | struct kgsl_iommu_pt *iommu_pt = pagetable->priv; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 454 | /* page table not current, then setup mmu to use new |
| 455 | * specified page table |
| 456 | */ |
| 457 | if (mmu->hwpagetable != pagetable) { |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 458 | unsigned int flags = 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 459 | mmu->hwpagetable = pagetable; |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 460 | /* force tlb flush if asid is reused */ |
| 461 | if (iommu->asid_reuse && |
| 462 | (KGSL_IOMMU_ASID_REUSE == iommu_pt->asid)) |
| 463 | flags |= KGSL_MMUFLAGS_TLBFLUSH; |
| 464 | flags |= kgsl_mmu_pt_get_flags(mmu->hwpagetable, |
| 465 | mmu->device->id); |
| 466 | kgsl_setstate(mmu, KGSL_MMUFLAGS_PTUPDATE | flags); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 471 | static int kgsl_iommu_init(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 472 | { |
| 473 | /* |
| 474 | * intialize device mmu |
| 475 | * |
| 476 | * call this with the global lock held |
| 477 | */ |
| 478 | int status = 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 479 | struct kgsl_iommu *iommu; |
| 480 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 481 | iommu = kzalloc(sizeof(struct kgsl_iommu), GFP_KERNEL); |
| 482 | if (!iommu) { |
| 483 | KGSL_CORE_ERR("kzalloc(%d) failed\n", |
| 484 | sizeof(struct kgsl_iommu)); |
| 485 | return -ENOMEM; |
| 486 | } |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 487 | iommu->asids = kzalloc(BITS_TO_LONGS(KGSL_IOMMU_MAX_ASIDS) * |
| 488 | sizeof(unsigned long), GFP_KERNEL); |
| 489 | if (!iommu->asids) { |
| 490 | KGSL_CORE_ERR("kzalloc(%d) failed\n", |
| 491 | sizeof(struct kgsl_iommu)); |
| 492 | status = -ENOMEM; |
| 493 | goto done; |
| 494 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 495 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 496 | mmu->priv = iommu; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 497 | status = kgsl_get_iommu_ctxt(mmu); |
| 498 | if (status) |
| 499 | goto done; |
Shubhraprakash Das | e9eefd7 | 2012-05-01 01:44:59 -0600 | [diff] [blame] | 500 | status = kgsl_set_register_map(mmu); |
| 501 | if (status) |
| 502 | goto done; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 503 | |
Shubhraprakash Das | e9541a3 | 2012-05-09 22:25:55 -0600 | [diff] [blame] | 504 | /* A nop is required in an indirect buffer when switching |
| 505 | * pagetables in-stream */ |
| 506 | kgsl_sharedmem_writel(&mmu->setstate_memory, |
| 507 | KGSL_IOMMU_SETSTATE_NOP_OFFSET, |
| 508 | cp_nop_packet(1)); |
| 509 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 510 | dev_info(mmu->device->dev, "|%s| MMU type set for device is IOMMU\n", |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 511 | __func__); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 512 | done: |
| 513 | if (status) { |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 514 | kfree(iommu->asids); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 515 | kfree(iommu); |
| 516 | mmu->priv = NULL; |
| 517 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 518 | return status; |
| 519 | } |
| 520 | |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 521 | /* |
| 522 | * kgsl_iommu_setup_defaultpagetable - Setup the initial defualtpagetable |
| 523 | * for iommu. This function is only called once during first start, successive |
| 524 | * start do not call this funciton. |
| 525 | * @mmu - Pointer to mmu structure |
| 526 | * |
| 527 | * Create the initial defaultpagetable and setup the iommu mappings to it |
| 528 | * Return - 0 on success else error code |
| 529 | */ |
| 530 | static int kgsl_iommu_setup_defaultpagetable(struct kgsl_mmu *mmu) |
| 531 | { |
| 532 | int status = 0; |
| 533 | int i = 0; |
| 534 | struct kgsl_iommu *iommu = mmu->priv; |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 535 | struct kgsl_iommu_pt *iommu_pt; |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 536 | |
| 537 | mmu->defaultpagetable = kgsl_mmu_getpagetable(KGSL_MMU_GLOBAL_PT); |
| 538 | /* Return error if the default pagetable doesn't exist */ |
| 539 | if (mmu->defaultpagetable == NULL) { |
| 540 | status = -ENOMEM; |
| 541 | goto err; |
| 542 | } |
| 543 | /* Map the IOMMU regsiters to only defaultpagetable */ |
| 544 | for (i = 0; i < iommu->unit_count; i++) { |
| 545 | iommu->iommu_units[i].reg_map.priv |= KGSL_MEMFLAGS_GLOBAL; |
| 546 | status = kgsl_mmu_map(mmu->defaultpagetable, |
| 547 | &(iommu->iommu_units[i].reg_map), |
| 548 | GSL_PT_PAGE_RV | GSL_PT_PAGE_WV); |
| 549 | if (status) { |
| 550 | iommu->iommu_units[i].reg_map.priv &= |
| 551 | ~KGSL_MEMFLAGS_GLOBAL; |
| 552 | goto err; |
| 553 | } |
| 554 | } |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 555 | /* |
| 556 | * The dafault pagetable always has asid 0 assigned by the iommu driver |
| 557 | * and asid 1 is assigned to the private context. |
| 558 | */ |
| 559 | iommu_pt = mmu->defaultpagetable->priv; |
| 560 | iommu_pt->asid = 0; |
| 561 | set_bit(0, iommu->asids); |
| 562 | set_bit(1, iommu->asids); |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 563 | return status; |
| 564 | err: |
| 565 | for (i--; i >= 0; i--) { |
| 566 | kgsl_mmu_unmap(mmu->defaultpagetable, |
| 567 | &(iommu->iommu_units[i].reg_map)); |
| 568 | iommu->iommu_units[i].reg_map.priv &= ~KGSL_MEMFLAGS_GLOBAL; |
| 569 | } |
| 570 | if (mmu->defaultpagetable) { |
| 571 | kgsl_mmu_putpagetable(mmu->defaultpagetable); |
| 572 | mmu->defaultpagetable = NULL; |
| 573 | } |
| 574 | return status; |
| 575 | } |
| 576 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 577 | static int kgsl_iommu_start(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 578 | { |
| 579 | int status; |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 580 | struct kgsl_iommu *iommu = mmu->priv; |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 581 | int i, j; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 582 | |
| 583 | if (mmu->flags & KGSL_FLAGS_STARTED) |
| 584 | return 0; |
| 585 | |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 586 | if (mmu->defaultpagetable == NULL) { |
| 587 | status = kgsl_iommu_setup_defaultpagetable(mmu); |
| 588 | if (status) |
| 589 | return -ENOMEM; |
| 590 | } |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 591 | /* We use the GPU MMU to control access to IOMMU registers on a225, |
| 592 | * hence we still keep the MMU active on a225 */ |
| 593 | if (adreno_is_a225(ADRENO_DEVICE(mmu->device))) { |
| 594 | struct kgsl_mh *mh = &(mmu->device->mh); |
| 595 | kgsl_regwrite(mmu->device, MH_MMU_CONFIG, 0x00000001); |
| 596 | kgsl_regwrite(mmu->device, MH_MMU_MPU_END, |
| 597 | mh->mpu_base + |
Shubhraprakash Das | c6e2101 | 2012-05-11 17:24:51 -0600 | [diff] [blame] | 598 | iommu->iommu_units |
| 599 | [iommu->unit_count - 1].reg_map.gpuaddr - |
| 600 | PAGE_SIZE); |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 601 | } else { |
| 602 | kgsl_regwrite(mmu->device, MH_MMU_CONFIG, 0x00000000); |
| 603 | } |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 604 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 605 | mmu->hwpagetable = mmu->defaultpagetable; |
| 606 | |
| 607 | status = kgsl_attach_pagetable_iommu_domain(mmu); |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 608 | if (status) { |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 609 | mmu->hwpagetable = NULL; |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 610 | goto done; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 611 | } |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 612 | status = kgsl_iommu_enable_clk(mmu, KGSL_IOMMU_CONTEXT_USER); |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 613 | if (status) { |
| 614 | KGSL_CORE_ERR("clk enable failed\n"); |
| 615 | goto done; |
| 616 | } |
| 617 | status = kgsl_iommu_enable_clk(mmu, KGSL_IOMMU_CONTEXT_PRIV); |
| 618 | if (status) { |
| 619 | KGSL_CORE_ERR("clk enable failed\n"); |
| 620 | goto done; |
| 621 | } |
| 622 | /* Get the lsb value of pagetables set in the IOMMU ttbr0 register as |
| 623 | * that value should not change when we change pagetables, so while |
| 624 | * changing pagetables we can use this lsb value of the pagetable w/o |
| 625 | * having to read it again |
| 626 | */ |
| 627 | for (i = 0; i < iommu->unit_count; i++) { |
| 628 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 629 | for (j = 0; j < iommu_unit->dev_count; j++) |
| 630 | iommu_unit->dev[j].pt_lsb = KGSL_IOMMMU_PT_LSB( |
| 631 | KGSL_IOMMU_GET_IOMMU_REG( |
| 632 | iommu_unit->reg_map.hostptr, |
| 633 | iommu_unit->dev[j].ctx_id, |
| 634 | TTBR0)); |
| 635 | } |
| 636 | iommu->asid = KGSL_IOMMU_GET_IOMMU_REG( |
| 637 | iommu->iommu_units[0].reg_map.hostptr, |
| 638 | KGSL_IOMMU_CONTEXT_USER, |
| 639 | CONTEXTIDR); |
| 640 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 641 | kgsl_iommu_disable_clk(mmu); |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 642 | mmu->flags |= KGSL_FLAGS_STARTED; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 643 | |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 644 | done: |
| 645 | if (status) { |
| 646 | kgsl_iommu_disable_clk(mmu); |
| 647 | kgsl_detach_pagetable_iommu_domain(mmu); |
| 648 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 649 | return status; |
| 650 | } |
| 651 | |
| 652 | static int |
| 653 | kgsl_iommu_unmap(void *mmu_specific_pt, |
| 654 | struct kgsl_memdesc *memdesc) |
| 655 | { |
| 656 | int ret; |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 657 | unsigned int range = kgsl_sg_size(memdesc->sg, memdesc->sglen); |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 658 | struct kgsl_iommu_pt *iommu_pt = mmu_specific_pt; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 659 | |
| 660 | /* All GPU addresses as assigned are page aligned, but some |
| 661 | functions purturb the gpuaddr with an offset, so apply the |
| 662 | mask here to make sure we have the right address */ |
| 663 | |
| 664 | unsigned int gpuaddr = memdesc->gpuaddr & KGSL_MMU_ALIGN_MASK; |
| 665 | |
| 666 | if (range == 0 || gpuaddr == 0) |
| 667 | return 0; |
| 668 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 669 | ret = iommu_unmap_range(iommu_pt->domain, gpuaddr, range); |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 670 | if (ret) |
| 671 | KGSL_CORE_ERR("iommu_unmap_range(%p, %x, %d) failed " |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 672 | "with err: %d\n", iommu_pt->domain, gpuaddr, |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 673 | range, ret); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int |
| 679 | kgsl_iommu_map(void *mmu_specific_pt, |
| 680 | struct kgsl_memdesc *memdesc, |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 681 | unsigned int protflags, |
| 682 | unsigned int *tlb_flags) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 683 | { |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 684 | int ret; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 685 | unsigned int iommu_virt_addr; |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 686 | struct kgsl_iommu_pt *iommu_pt = mmu_specific_pt; |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 687 | int size = kgsl_sg_size(memdesc->sg, memdesc->sglen); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 688 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 689 | BUG_ON(NULL == iommu_pt); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 690 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 691 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 692 | iommu_virt_addr = memdesc->gpuaddr; |
| 693 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 694 | ret = iommu_map_range(iommu_pt->domain, iommu_virt_addr, memdesc->sg, |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 695 | size, (IOMMU_READ | IOMMU_WRITE)); |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 696 | if (ret) { |
| 697 | KGSL_CORE_ERR("iommu_map_range(%p, %x, %p, %d, %d) " |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 698 | "failed with err: %d\n", iommu_pt->domain, |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 699 | iommu_virt_addr, memdesc->sg, size, |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 700 | (IOMMU_READ | IOMMU_WRITE), ret); |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 701 | return ret; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 702 | } |
| 703 | |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 704 | #ifdef CONFIG_KGSL_PER_PROCESS_PAGE_TABLE |
| 705 | /* |
| 706 | * Flushing only required if per process pagetables are used. With |
| 707 | * global case, flushing will happen inside iommu_map function |
| 708 | */ |
| 709 | if (!ret) |
| 710 | *tlb_flags = UINT_MAX; |
| 711 | #endif |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 712 | return ret; |
| 713 | } |
| 714 | |
Shubhraprakash Das | 7944795 | 2012-04-26 18:12:23 -0600 | [diff] [blame] | 715 | static void kgsl_iommu_stop(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 716 | { |
| 717 | /* |
| 718 | * stop device mmu |
| 719 | * |
| 720 | * call this with the global lock held |
| 721 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 722 | |
| 723 | if (mmu->flags & KGSL_FLAGS_STARTED) { |
Shubhraprakash Das | bb5ad2a | 2012-05-09 22:58:52 -0600 | [diff] [blame] | 724 | kgsl_regwrite(mmu->device, MH_MMU_CONFIG, 0x00000000); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 725 | /* detach iommu attachment */ |
| 726 | kgsl_detach_pagetable_iommu_domain(mmu); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 727 | mmu->hwpagetable = NULL; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 728 | |
| 729 | mmu->flags &= ~KGSL_FLAGS_STARTED; |
| 730 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 731 | } |
| 732 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 733 | static int kgsl_iommu_close(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 734 | { |
Shubhraprakash Das | 589c7fe | 2012-05-04 17:30:20 -0600 | [diff] [blame] | 735 | struct kgsl_iommu *iommu = mmu->priv; |
| 736 | int i; |
| 737 | for (i = 0; i < iommu->unit_count; i++) { |
| 738 | if (iommu->iommu_units[i].reg_map.gpuaddr) |
| 739 | kgsl_mmu_unmap(mmu->defaultpagetable, |
| 740 | &(iommu->iommu_units[i].reg_map)); |
| 741 | if (iommu->iommu_units[i].reg_map.hostptr) |
| 742 | iounmap(iommu->iommu_units[i].reg_map.hostptr); |
| 743 | kgsl_sg_free(iommu->iommu_units[i].reg_map.sg, |
| 744 | iommu->iommu_units[i].reg_map.sglen); |
| 745 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 746 | if (mmu->defaultpagetable) |
| 747 | kgsl_mmu_putpagetable(mmu->defaultpagetable); |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 748 | kfree(iommu->asids); |
| 749 | kfree(iommu); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static unsigned int |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 755 | kgsl_iommu_get_current_ptbase(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 756 | { |
Shubhraprakash Das | 2b8716b | 2012-05-04 16:58:40 -0600 | [diff] [blame] | 757 | unsigned int pt_base; |
| 758 | struct kgsl_iommu *iommu = mmu->priv; |
| 759 | /* Return the current pt base by reading IOMMU pt_base register */ |
| 760 | kgsl_iommu_enable_clk(mmu, KGSL_IOMMU_CONTEXT_USER); |
| 761 | pt_base = readl_relaxed(iommu->iommu_units[0].reg_map.hostptr + |
| 762 | (KGSL_IOMMU_CONTEXT_USER << KGSL_IOMMU_CTX_SHIFT) + |
| 763 | KGSL_IOMMU_TTBR0); |
| 764 | kgsl_iommu_disable_clk(mmu); |
| 765 | return pt_base & (KGSL_IOMMU_TTBR0_PA_MASK << |
| 766 | KGSL_IOMMU_TTBR0_PA_SHIFT); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 767 | } |
| 768 | |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 769 | /* |
| 770 | * kgsl_iommu_get_hwpagetable_asid - Returns asid(application space ID) for a |
| 771 | * pagetable |
| 772 | * @mmu - Pointer to mmu structure |
| 773 | * |
| 774 | * Allocates an asid to a IOMMU domain if it does not already have one. asid's |
| 775 | * are unique identifiers for pagetable that can be used to selectively flush |
| 776 | * tlb entries of the IOMMU unit. |
| 777 | * Return - asid to be used with the IOMMU domain |
| 778 | */ |
| 779 | static int kgsl_iommu_get_hwpagetable_asid(struct kgsl_mmu *mmu) |
| 780 | { |
| 781 | struct kgsl_iommu *iommu = mmu->priv; |
| 782 | struct kgsl_iommu_pt *iommu_pt = mmu->hwpagetable->priv; |
| 783 | |
| 784 | /* |
| 785 | * If the iommu pagetable does not have any asid assigned and is not the |
| 786 | * default pagetable then assign asid. |
| 787 | */ |
| 788 | if (!iommu_pt->asid && iommu_pt != mmu->defaultpagetable->priv) { |
| 789 | iommu_pt->asid = find_first_zero_bit(iommu->asids, |
| 790 | KGSL_IOMMU_MAX_ASIDS); |
| 791 | /* No free bits means reuse asid */ |
| 792 | if (iommu_pt->asid >= KGSL_IOMMU_MAX_ASIDS) { |
| 793 | iommu_pt->asid = KGSL_IOMMU_ASID_REUSE; |
| 794 | iommu->asid_reuse++; |
| 795 | } |
| 796 | set_bit(iommu_pt->asid, iommu->asids); |
| 797 | /* |
| 798 | * Store pointer to asids list so that during pagetable destroy |
| 799 | * the asid assigned to this pagetable may be cleared |
| 800 | */ |
| 801 | iommu_pt->iommu = iommu; |
| 802 | } |
| 803 | /* Return the asid + the constant part of asid that never changes */ |
| 804 | return (iommu_pt->asid & (KGSL_IOMMU_CONTEXTIDR_ASID_MASK << |
| 805 | KGSL_IOMMU_CONTEXTIDR_ASID_SHIFT)) + |
| 806 | (iommu->asid & ~(KGSL_IOMMU_CONTEXTIDR_ASID_MASK << |
| 807 | KGSL_IOMMU_CONTEXTIDR_ASID_SHIFT)); |
| 808 | } |
| 809 | |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 810 | /* |
| 811 | * kgsl_iommu_default_setstate - Change the IOMMU pagetable or flush IOMMU tlb |
| 812 | * of the primary context bank |
| 813 | * @mmu - Pointer to mmu structure |
| 814 | * @flags - Flags indicating whether pagetable has to chnage or tlb is to be |
| 815 | * flushed or both |
| 816 | * |
| 817 | * Based on flags set the new pagetable fo the IOMMU unit or flush it's tlb or |
| 818 | * do both by doing direct register writes to the IOMMu registers through the |
| 819 | * cpu |
| 820 | * Return - void |
| 821 | */ |
| 822 | static void kgsl_iommu_default_setstate(struct kgsl_mmu *mmu, |
| 823 | uint32_t flags) |
| 824 | { |
| 825 | struct kgsl_iommu *iommu = mmu->priv; |
| 826 | int temp; |
| 827 | int i; |
| 828 | unsigned int pt_base = kgsl_iommu_pt_get_base_addr( |
| 829 | mmu->hwpagetable); |
| 830 | unsigned int pt_val; |
| 831 | |
| 832 | if (kgsl_iommu_enable_clk(mmu, KGSL_IOMMU_CONTEXT_USER)) { |
| 833 | KGSL_DRV_ERR(mmu->device, "Failed to enable iommu clocks\n"); |
| 834 | return; |
| 835 | } |
| 836 | /* Mask off the lsb of the pt base address since lsb will not change */ |
| 837 | pt_base &= (KGSL_IOMMU_TTBR0_PA_MASK << KGSL_IOMMU_TTBR0_PA_SHIFT); |
| 838 | if (flags & KGSL_MMUFLAGS_PTUPDATE) { |
| 839 | kgsl_idle(mmu->device, KGSL_TIMEOUT_DEFAULT); |
| 840 | for (i = 0; i < iommu->unit_count; i++) { |
| 841 | /* get the lsb value which should not change when |
| 842 | * changing ttbr0 */ |
| 843 | pt_val = kgsl_iommu_get_pt_lsb(mmu, i, |
| 844 | KGSL_IOMMU_CONTEXT_USER); |
| 845 | pt_val += pt_base; |
| 846 | |
| 847 | KGSL_IOMMU_SET_IOMMU_REG( |
| 848 | iommu->iommu_units[i].reg_map.hostptr, |
| 849 | KGSL_IOMMU_CONTEXT_USER, TTBR0, pt_val); |
| 850 | |
| 851 | mb(); |
| 852 | temp = KGSL_IOMMU_GET_IOMMU_REG( |
| 853 | iommu->iommu_units[i].reg_map.hostptr, |
| 854 | KGSL_IOMMU_CONTEXT_USER, TTBR0); |
| 855 | /* Set asid */ |
| 856 | KGSL_IOMMU_SET_IOMMU_REG( |
| 857 | iommu->iommu_units[i].reg_map.hostptr, |
| 858 | KGSL_IOMMU_CONTEXT_USER, CONTEXTIDR, |
| 859 | kgsl_iommu_get_hwpagetable_asid(mmu)); |
| 860 | mb(); |
| 861 | temp = KGSL_IOMMU_GET_IOMMU_REG( |
| 862 | iommu->iommu_units[i].reg_map.hostptr, |
| 863 | KGSL_IOMMU_CONTEXT_USER, CONTEXTIDR); |
| 864 | } |
| 865 | } |
| 866 | /* Flush tlb */ |
| 867 | if (flags & KGSL_MMUFLAGS_TLBFLUSH) { |
| 868 | for (i = 0; i < iommu->unit_count; i++) { |
| 869 | KGSL_IOMMU_SET_IOMMU_REG( |
| 870 | iommu->iommu_units[i].reg_map.hostptr, |
| 871 | KGSL_IOMMU_CONTEXT_USER, CTX_TLBIASID, |
| 872 | kgsl_iommu_get_hwpagetable_asid(mmu)); |
| 873 | mb(); |
| 874 | } |
| 875 | } |
| 876 | /* Disable smmu clock */ |
| 877 | kgsl_iommu_disable_clk(mmu); |
| 878 | } |
| 879 | |
Shubhraprakash Das | a5b1db4 | 2012-05-09 18:02:34 -0600 | [diff] [blame] | 880 | /* |
| 881 | * kgsl_iommu_get_reg_map_desc - Returns an array of pointers that contain |
| 882 | * the address of memory descriptors which map the IOMMU registers |
| 883 | * @mmu - Pointer to mmu structure |
| 884 | * @reg_map_desc - Out parameter in which the address of the array containing |
| 885 | * pointers to register map descriptors is returned. The caller is supposed |
| 886 | * to free this array |
| 887 | * |
| 888 | * Return - The number of iommu units which is also the number of register |
| 889 | * mapped descriptor arrays which the out parameter will have |
| 890 | */ |
| 891 | static int kgsl_iommu_get_reg_map_desc(struct kgsl_mmu *mmu, |
| 892 | void **reg_map_desc) |
| 893 | { |
| 894 | struct kgsl_iommu *iommu = mmu->priv; |
| 895 | void **reg_desc_ptr; |
| 896 | int i; |
| 897 | |
| 898 | /* |
| 899 | * Alocate array of pointers that will hold address of the register map |
| 900 | * descriptors |
| 901 | */ |
| 902 | reg_desc_ptr = kmalloc(iommu->unit_count * |
| 903 | sizeof(struct kgsl_memdesc *), GFP_KERNEL); |
| 904 | if (!reg_desc_ptr) { |
| 905 | KGSL_CORE_ERR("Failed to kmalloc(%d)\n", |
| 906 | iommu->unit_count * sizeof(struct kgsl_memdesc *)); |
| 907 | return -ENOMEM; |
| 908 | } |
| 909 | |
| 910 | for (i = 0; i < iommu->unit_count; i++) |
| 911 | reg_desc_ptr[i] = &(iommu->iommu_units[i].reg_map); |
| 912 | |
| 913 | *reg_map_desc = reg_desc_ptr; |
| 914 | return i; |
| 915 | } |
| 916 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 917 | struct kgsl_mmu_ops iommu_ops = { |
| 918 | .mmu_init = kgsl_iommu_init, |
| 919 | .mmu_close = kgsl_iommu_close, |
| 920 | .mmu_start = kgsl_iommu_start, |
| 921 | .mmu_stop = kgsl_iommu_stop, |
| 922 | .mmu_setstate = kgsl_iommu_setstate, |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 923 | .mmu_device_setstate = kgsl_iommu_default_setstate, |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 924 | .mmu_pagefault = NULL, |
| 925 | .mmu_get_current_ptbase = kgsl_iommu_get_current_ptbase, |
Shubhraprakash Das | 9fb38ac | 2012-05-01 00:41:30 -0600 | [diff] [blame] | 926 | .mmu_enable_clk = kgsl_iommu_enable_clk, |
| 927 | .mmu_disable_clk = kgsl_iommu_disable_clk, |
Shubhraprakash Das | d3f937c | 2012-05-07 12:44:40 -0600 | [diff] [blame] | 928 | .mmu_get_hwpagetable_asid = kgsl_iommu_get_hwpagetable_asid, |
Shubhraprakash Das | fce2736 | 2012-05-09 17:44:14 -0600 | [diff] [blame] | 929 | .mmu_get_pt_lsb = kgsl_iommu_get_pt_lsb, |
Shubhraprakash Das | a5b1db4 | 2012-05-09 18:02:34 -0600 | [diff] [blame] | 930 | .mmu_get_reg_map_desc = kgsl_iommu_get_reg_map_desc, |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | struct kgsl_mmu_pt_ops iommu_pt_ops = { |
| 934 | .mmu_map = kgsl_iommu_map, |
| 935 | .mmu_unmap = kgsl_iommu_unmap, |
| 936 | .mmu_create_pagetable = kgsl_iommu_create_pagetable, |
| 937 | .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable, |
| 938 | .mmu_pt_equal = kgsl_iommu_pt_equal, |
Shubhraprakash Das | 5a610b5 | 2012-05-09 17:31:54 -0600 | [diff] [blame] | 939 | .mmu_pt_get_base_addr = kgsl_iommu_pt_get_base_addr, |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 940 | }; |