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 | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 26 | |
| 27 | static int kgsl_iommu_pt_equal(struct kgsl_pagetable *pt, |
| 28 | unsigned int pt_base) |
| 29 | { |
Shubhraprakash Das | 528aa46 | 2012-03-01 14:56:28 -0700 | [diff] [blame] | 30 | struct iommu_domain *domain = pt ? pt->priv : NULL; |
| 31 | return domain && pt_base && ((unsigned int)domain == pt_base); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void kgsl_iommu_destroy_pagetable(void *mmu_specific_pt) |
| 35 | { |
| 36 | struct iommu_domain *domain = mmu_specific_pt; |
| 37 | if (domain) |
| 38 | iommu_domain_free(domain); |
| 39 | } |
| 40 | |
| 41 | void *kgsl_iommu_create_pagetable(void) |
| 42 | { |
| 43 | struct iommu_domain *domain = iommu_domain_alloc(0); |
| 44 | if (!domain) |
| 45 | KGSL_CORE_ERR("Failed to create iommu domain\n"); |
| 46 | |
| 47 | return domain; |
| 48 | } |
| 49 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 50 | /* |
| 51 | * kgsl_detach_pagetable_iommu_domain - Detach the IOMMU unit from a |
| 52 | * pagetable |
| 53 | * @mmu - Pointer to the device mmu structure |
| 54 | * @priv - Flag indicating whether the private or user context is to be |
| 55 | * detached |
| 56 | * |
| 57 | * Detach the IOMMU unit with the domain that is contained in the |
| 58 | * hwpagetable of the given mmu. After detaching the IOMMU unit is not |
| 59 | * in use because the PTBR will not be set after a detach |
| 60 | * Return - void |
| 61 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 62 | static void kgsl_detach_pagetable_iommu_domain(struct kgsl_mmu *mmu) |
| 63 | { |
| 64 | struct iommu_domain *domain; |
| 65 | struct kgsl_iommu *iommu = mmu->priv; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 66 | int i, j; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 67 | |
| 68 | BUG_ON(mmu->hwpagetable == NULL); |
| 69 | BUG_ON(mmu->hwpagetable->priv == NULL); |
| 70 | |
| 71 | domain = mmu->hwpagetable->priv; |
| 72 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 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].attached) { |
| 77 | iommu_detach_device(domain, |
| 78 | iommu_unit->dev[j].dev); |
| 79 | iommu_unit->dev[j].attached = false; |
| 80 | KGSL_MEM_INFO(mmu->device, "iommu %p detached " |
| 81 | "from user dev of MMU: %p\n", |
| 82 | domain, mmu); |
| 83 | } |
| 84 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 88 | /* |
| 89 | * kgsl_attach_pagetable_iommu_domain - Attach the IOMMU unit to a |
| 90 | * pagetable, i.e set the IOMMU's PTBR to the pagetable address and |
| 91 | * setup other IOMMU registers for the device so that it becomes |
| 92 | * active |
| 93 | * @mmu - Pointer to the device mmu structure |
| 94 | * @priv - Flag indicating whether the private or user context is to be |
| 95 | * attached |
| 96 | * |
| 97 | * Attach the IOMMU unit with the domain that is contained in the |
| 98 | * hwpagetable of the given mmu. |
| 99 | * Return - 0 on success else error code |
| 100 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 101 | static int kgsl_attach_pagetable_iommu_domain(struct kgsl_mmu *mmu) |
| 102 | { |
| 103 | struct iommu_domain *domain; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 104 | struct kgsl_iommu *iommu = mmu->priv; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 105 | int i, j, ret = 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 106 | |
| 107 | BUG_ON(mmu->hwpagetable == NULL); |
| 108 | BUG_ON(mmu->hwpagetable->priv == NULL); |
| 109 | |
| 110 | domain = mmu->hwpagetable->priv; |
| 111 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 112 | /* |
| 113 | * Loop through all the iommu devcies under all iommu units and |
| 114 | * attach the domain |
| 115 | */ |
| 116 | for (i = 0; i < iommu->unit_count; i++) { |
| 117 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[i]; |
| 118 | for (j = 0; j < iommu_unit->dev_count; j++) { |
| 119 | if (!iommu_unit->dev[j].attached) { |
| 120 | ret = iommu_attach_device(domain, |
| 121 | iommu_unit->dev[j].dev); |
| 122 | if (ret) { |
| 123 | KGSL_MEM_ERR(mmu->device, |
| 124 | "Failed to attach device, err %d\n", |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 125 | ret); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 126 | goto done; |
| 127 | } |
| 128 | iommu_unit->dev[j].attached = true; |
| 129 | KGSL_MEM_INFO(mmu->device, |
| 130 | "iommu pt %p attached to dev %p, ctx_id %d\n", |
| 131 | domain, iommu_unit->dev[j].dev, |
| 132 | iommu_unit->dev[j].ctx_id); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 133 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 134 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 135 | } |
| 136 | done: |
| 137 | return ret; |
| 138 | } |
| 139 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 140 | /* |
| 141 | * _get_iommu_ctxs - Get device pointer to IOMMU contexts |
| 142 | * @mmu - Pointer to mmu device |
| 143 | * data - Pointer to the platform data containing information about |
| 144 | * iommu devices for one iommu unit |
| 145 | * unit_id - The IOMMU unit number. This is not a specific ID but just |
| 146 | * a serial number. The serial numbers are treated as ID's of the |
| 147 | * IOMMU units |
| 148 | * |
| 149 | * Return - 0 on success else error code |
| 150 | */ |
| 151 | static int _get_iommu_ctxs(struct kgsl_mmu *mmu, |
| 152 | struct kgsl_device_iommu_data *data, unsigned int unit_id) |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 153 | { |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 154 | struct kgsl_iommu *iommu = mmu->priv; |
| 155 | struct kgsl_iommu_unit *iommu_unit = &iommu->iommu_units[unit_id]; |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 156 | int i; |
| 157 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 158 | if (data->iommu_ctx_count > KGSL_IOMMU_MAX_DEVS_PER_UNIT) { |
| 159 | KGSL_CORE_ERR("Too many iommu devices defined for an " |
| 160 | "IOMMU unit\n"); |
| 161 | return -EINVAL; |
| 162 | } |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 163 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 164 | for (i = 0; i < data->iommu_ctx_count; i++) { |
| 165 | if (!data->iommu_ctxs[i].iommu_ctx_name) |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 166 | continue; |
| 167 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 168 | iommu_unit->dev[iommu_unit->dev_count].dev = |
| 169 | msm_iommu_get_ctx(data->iommu_ctxs[i].iommu_ctx_name); |
| 170 | if (iommu_unit->dev[iommu_unit->dev_count].dev == NULL) { |
| 171 | KGSL_CORE_ERR("Failed to get iommu dev handle for " |
| 172 | "device %s\n", data->iommu_ctxs[i].iommu_ctx_name); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | } |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 175 | if (KGSL_IOMMU_CONTEXT_USER != data->iommu_ctxs[i].ctx_id && |
| 176 | KGSL_IOMMU_CONTEXT_PRIV != data->iommu_ctxs[i].ctx_id) { |
| 177 | KGSL_CORE_ERR("Invalid context ID defined: %d\n", |
| 178 | data->iommu_ctxs[i].ctx_id); |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | iommu_unit->dev[iommu_unit->dev_count].ctx_id = |
| 182 | data->iommu_ctxs[i].ctx_id; |
| 183 | KGSL_DRV_INFO(mmu->device, |
| 184 | "Obtained dev handle %p for iommu context %s\n", |
| 185 | iommu_unit->dev[iommu_unit->dev_count].dev, |
| 186 | data->iommu_ctxs[i].iommu_ctx_name); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 187 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 188 | iommu_unit->dev_count++; |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 194 | /* |
| 195 | * kgsl_get_iommu_ctxt - Get device pointer to IOMMU contexts |
| 196 | * @mmu - Pointer to mmu device |
| 197 | * |
| 198 | * Get the device pointers for the IOMMU user and priv contexts of the |
| 199 | * kgsl device |
| 200 | * Return - 0 on success else error code |
| 201 | */ |
| 202 | static int kgsl_get_iommu_ctxt(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 203 | { |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 204 | struct platform_device *pdev = |
| 205 | container_of(mmu->device->parentdev, struct platform_device, |
| 206 | dev); |
| 207 | struct kgsl_device_platform_data *pdata_dev = pdev->dev.platform_data; |
| 208 | struct kgsl_iommu *iommu = mmu->device->mmu.priv; |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 209 | int i, ret = 0; |
| 210 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 211 | /* Go through the IOMMU data and get all the context devices */ |
| 212 | if (KGSL_IOMMU_MAX_UNITS < pdata_dev->iommu_count) { |
| 213 | KGSL_CORE_ERR("Too many IOMMU units defined\n"); |
| 214 | ret = -EINVAL; |
| 215 | goto done; |
| 216 | } |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 217 | |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 218 | for (i = 0; i < pdata_dev->iommu_count; i++) { |
| 219 | ret = _get_iommu_ctxs(mmu, &pdata_dev->iommu_data[i], i); |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 220 | if (ret) |
| 221 | break; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 222 | } |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 223 | iommu->unit_count = pdata_dev->iommu_count; |
| 224 | done: |
Jordan Crouse | 46cf4bb | 2012-02-21 08:54:52 -0700 | [diff] [blame] | 225 | return ret; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 226 | } |
| 227 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 228 | static void kgsl_iommu_setstate(struct kgsl_mmu *mmu, |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 229 | struct kgsl_pagetable *pagetable) |
| 230 | { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 231 | if (mmu->flags & KGSL_FLAGS_STARTED) { |
| 232 | /* page table not current, then setup mmu to use new |
| 233 | * specified page table |
| 234 | */ |
| 235 | if (mmu->hwpagetable != pagetable) { |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 236 | kgsl_idle(mmu->device, KGSL_TIMEOUT_DEFAULT); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 237 | kgsl_detach_pagetable_iommu_domain(mmu); |
| 238 | mmu->hwpagetable = pagetable; |
| 239 | if (mmu->hwpagetable) |
| 240 | kgsl_attach_pagetable_iommu_domain(mmu); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 245 | static int kgsl_iommu_init(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 246 | { |
| 247 | /* |
| 248 | * intialize device mmu |
| 249 | * |
| 250 | * call this with the global lock held |
| 251 | */ |
| 252 | int status = 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 253 | struct kgsl_iommu *iommu; |
| 254 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 255 | iommu = kzalloc(sizeof(struct kgsl_iommu), GFP_KERNEL); |
| 256 | if (!iommu) { |
| 257 | KGSL_CORE_ERR("kzalloc(%d) failed\n", |
| 258 | sizeof(struct kgsl_iommu)); |
| 259 | return -ENOMEM; |
| 260 | } |
| 261 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 262 | mmu->priv = iommu; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 263 | status = kgsl_get_iommu_ctxt(mmu); |
| 264 | if (status) |
| 265 | goto done; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 266 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 267 | 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] | 268 | __func__); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 269 | done: |
| 270 | if (status) { |
| 271 | kfree(iommu); |
| 272 | mmu->priv = NULL; |
| 273 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 274 | return status; |
| 275 | } |
| 276 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 277 | static int kgsl_iommu_start(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 278 | { |
| 279 | int status; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 280 | |
| 281 | if (mmu->flags & KGSL_FLAGS_STARTED) |
| 282 | return 0; |
| 283 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 284 | kgsl_regwrite(mmu->device, MH_MMU_CONFIG, 0x00000000); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 285 | if (mmu->defaultpagetable == NULL) |
| 286 | mmu->defaultpagetable = |
| 287 | kgsl_mmu_getpagetable(KGSL_MMU_GLOBAL_PT); |
| 288 | /* Return error if the default pagetable doesn't exist */ |
| 289 | if (mmu->defaultpagetable == NULL) |
| 290 | return -ENOMEM; |
| 291 | mmu->hwpagetable = mmu->defaultpagetable; |
| 292 | |
| 293 | status = kgsl_attach_pagetable_iommu_domain(mmu); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 294 | if (!status) { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 295 | mmu->flags |= KGSL_FLAGS_STARTED; |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 296 | } else { |
| 297 | kgsl_detach_pagetable_iommu_domain(mmu); |
| 298 | mmu->hwpagetable = NULL; |
| 299 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 300 | |
| 301 | return status; |
| 302 | } |
| 303 | |
| 304 | static int |
| 305 | kgsl_iommu_unmap(void *mmu_specific_pt, |
| 306 | struct kgsl_memdesc *memdesc) |
| 307 | { |
| 308 | int ret; |
| 309 | unsigned int range = memdesc->size; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 310 | struct iommu_domain *domain = (struct iommu_domain *) |
| 311 | mmu_specific_pt; |
| 312 | |
| 313 | /* All GPU addresses as assigned are page aligned, but some |
| 314 | functions purturb the gpuaddr with an offset, so apply the |
| 315 | mask here to make sure we have the right address */ |
| 316 | |
| 317 | unsigned int gpuaddr = memdesc->gpuaddr & KGSL_MMU_ALIGN_MASK; |
| 318 | |
| 319 | if (range == 0 || gpuaddr == 0) |
| 320 | return 0; |
| 321 | |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 322 | ret = iommu_unmap_range(domain, gpuaddr, range); |
| 323 | if (ret) |
| 324 | KGSL_CORE_ERR("iommu_unmap_range(%p, %x, %d) failed " |
| 325 | "with err: %d\n", domain, gpuaddr, |
| 326 | range, ret); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int |
| 332 | kgsl_iommu_map(void *mmu_specific_pt, |
| 333 | struct kgsl_memdesc *memdesc, |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 334 | unsigned int protflags, |
| 335 | unsigned int *tlb_flags) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 336 | { |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 337 | int ret; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 338 | unsigned int iommu_virt_addr; |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 339 | struct iommu_domain *domain = mmu_specific_pt; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 340 | |
| 341 | BUG_ON(NULL == domain); |
| 342 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 343 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 344 | iommu_virt_addr = memdesc->gpuaddr; |
| 345 | |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 346 | ret = iommu_map_range(domain, iommu_virt_addr, memdesc->sg, |
Olav Haugan | f310cf2 | 2012-05-08 08:42:49 -0700 | [diff] [blame^] | 347 | memdesc->size, (IOMMU_READ | IOMMU_WRITE)); |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 348 | if (ret) { |
| 349 | KGSL_CORE_ERR("iommu_map_range(%p, %x, %p, %d, %d) " |
| 350 | "failed with err: %d\n", domain, |
| 351 | iommu_virt_addr, memdesc->sg, memdesc->size, |
Stepan Moskovchenko | 6ee3be8 | 2011-11-08 15:24:53 -0800 | [diff] [blame] | 352 | 0, ret); |
Shubhraprakash Das | 08894b9 | 2011-10-14 11:42:25 -0600 | [diff] [blame] | 353 | return ret; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 354 | } |
| 355 | |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 356 | #ifdef CONFIG_KGSL_PER_PROCESS_PAGE_TABLE |
| 357 | /* |
| 358 | * Flushing only required if per process pagetables are used. With |
| 359 | * global case, flushing will happen inside iommu_map function |
| 360 | */ |
| 361 | if (!ret) |
| 362 | *tlb_flags = UINT_MAX; |
| 363 | #endif |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | |
Shubhraprakash Das | 7944795 | 2012-04-26 18:12:23 -0600 | [diff] [blame] | 367 | static void kgsl_iommu_stop(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 368 | { |
| 369 | /* |
| 370 | * stop device mmu |
| 371 | * |
| 372 | * call this with the global lock held |
| 373 | */ |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 374 | |
| 375 | if (mmu->flags & KGSL_FLAGS_STARTED) { |
| 376 | /* detach iommu attachment */ |
| 377 | kgsl_detach_pagetable_iommu_domain(mmu); |
Shubhraprakash Das | eb6df1d | 2012-05-01 00:55:35 -0600 | [diff] [blame] | 378 | mmu->hwpagetable = NULL; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 379 | |
| 380 | mmu->flags &= ~KGSL_FLAGS_STARTED; |
| 381 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 382 | } |
| 383 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 384 | static int kgsl_iommu_close(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 385 | { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 386 | if (mmu->defaultpagetable) |
| 387 | kgsl_mmu_putpagetable(mmu->defaultpagetable); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static unsigned int |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 393 | kgsl_iommu_get_current_ptbase(struct kgsl_mmu *mmu) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 394 | { |
| 395 | /* Current base is always the hwpagetables domain as we |
| 396 | * do not use per process pagetables right not for iommu. |
| 397 | * This will change when we switch to per process pagetables. |
| 398 | */ |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 399 | return (unsigned int)mmu->hwpagetable->priv; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | struct kgsl_mmu_ops iommu_ops = { |
| 403 | .mmu_init = kgsl_iommu_init, |
| 404 | .mmu_close = kgsl_iommu_close, |
| 405 | .mmu_start = kgsl_iommu_start, |
| 406 | .mmu_stop = kgsl_iommu_stop, |
| 407 | .mmu_setstate = kgsl_iommu_setstate, |
| 408 | .mmu_device_setstate = NULL, |
| 409 | .mmu_pagefault = NULL, |
| 410 | .mmu_get_current_ptbase = kgsl_iommu_get_current_ptbase, |
| 411 | }; |
| 412 | |
| 413 | struct kgsl_mmu_pt_ops iommu_pt_ops = { |
| 414 | .mmu_map = kgsl_iommu_map, |
| 415 | .mmu_unmap = kgsl_iommu_unmap, |
| 416 | .mmu_create_pagetable = kgsl_iommu_create_pagetable, |
| 417 | .mmu_destroy_pagetable = kgsl_iommu_destroy_pagetable, |
| 418 | .mmu_pt_equal = kgsl_iommu_pt_equal, |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 419 | }; |