Jordan Crouse | 0071401 | 2012-03-16 14:53:40 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2002,2007-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 <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/sched.h> |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 19 | #include <linux/iommu.h> |
Jordan Crouse | 817e0b9 | 2012-02-04 10:23:53 -0700 | [diff] [blame] | 20 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 21 | |
| 22 | #include "kgsl.h" |
| 23 | #include "kgsl_mmu.h" |
| 24 | #include "kgsl_device.h" |
| 25 | #include "kgsl_sharedmem.h" |
Jeremy Gebben | a3d07a4 | 2011-10-17 12:08:16 -0600 | [diff] [blame] | 26 | #include "adreno_postmortem.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | |
| 28 | #define KGSL_MMU_ALIGN_SHIFT 13 |
| 29 | #define KGSL_MMU_ALIGN_MASK (~((1 << KGSL_MMU_ALIGN_SHIFT) - 1)) |
| 30 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 31 | static enum kgsl_mmutype kgsl_mmu_type; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | |
| 33 | static void pagetable_remove_sysfs_objects(struct kgsl_pagetable *pagetable); |
| 34 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | static int kgsl_cleanup_pt(struct kgsl_pagetable *pt) |
| 36 | { |
| 37 | int i; |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 38 | /* For IOMMU only unmap the global structures to global pt */ |
Shubhraprakash Das | 6b30c9f | 2012-04-20 01:15:55 -0600 | [diff] [blame] | 39 | if ((KGSL_MMU_TYPE_NONE != kgsl_mmu_type) && |
| 40 | (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type) && |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 41 | (KGSL_MMU_GLOBAL_PT != pt->name)) |
| 42 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 43 | for (i = 0; i < KGSL_DEVICE_MAX; i++) { |
| 44 | struct kgsl_device *device = kgsl_driver.devp[i]; |
| 45 | if (device) |
| 46 | device->ftbl->cleanup_pt(device, pt); |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
Shubhraprakash Das | 6b30c9f | 2012-04-20 01:15:55 -0600 | [diff] [blame] | 51 | |
| 52 | static int kgsl_setup_pt(struct kgsl_pagetable *pt) |
| 53 | { |
| 54 | int i = 0; |
| 55 | int status = 0; |
| 56 | |
| 57 | /* For IOMMU only map the global structures to global pt */ |
| 58 | if ((KGSL_MMU_TYPE_NONE != kgsl_mmu_type) && |
| 59 | (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type) && |
| 60 | (KGSL_MMU_GLOBAL_PT != pt->name)) |
| 61 | return 0; |
| 62 | for (i = 0; i < KGSL_DEVICE_MAX; i++) { |
| 63 | struct kgsl_device *device = kgsl_driver.devp[i]; |
| 64 | if (device) { |
| 65 | status = device->ftbl->setup_pt(device, pt); |
| 66 | if (status) |
| 67 | goto error_pt; |
| 68 | } |
| 69 | } |
| 70 | return status; |
| 71 | error_pt: |
| 72 | while (i >= 0) { |
| 73 | struct kgsl_device *device = kgsl_driver.devp[i]; |
| 74 | if (device) |
| 75 | device->ftbl->cleanup_pt(device, pt); |
| 76 | i--; |
| 77 | } |
| 78 | return status; |
| 79 | } |
| 80 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | static void kgsl_destroy_pagetable(struct kref *kref) |
| 82 | { |
| 83 | struct kgsl_pagetable *pagetable = container_of(kref, |
| 84 | struct kgsl_pagetable, refcount); |
| 85 | unsigned long flags; |
| 86 | |
| 87 | spin_lock_irqsave(&kgsl_driver.ptlock, flags); |
| 88 | list_del(&pagetable->list); |
| 89 | spin_unlock_irqrestore(&kgsl_driver.ptlock, flags); |
| 90 | |
| 91 | pagetable_remove_sysfs_objects(pagetable); |
| 92 | |
| 93 | kgsl_cleanup_pt(pagetable); |
| 94 | |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 95 | if (pagetable->kgsl_pool) |
| 96 | gen_pool_destroy(pagetable->kgsl_pool); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 97 | if (pagetable->pool) |
| 98 | gen_pool_destroy(pagetable->pool); |
| 99 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 100 | pagetable->pt_ops->mmu_destroy_pagetable(pagetable->priv); |
| 101 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 102 | kfree(pagetable); |
| 103 | } |
| 104 | |
| 105 | static inline void kgsl_put_pagetable(struct kgsl_pagetable *pagetable) |
| 106 | { |
| 107 | if (pagetable) |
| 108 | kref_put(&pagetable->refcount, kgsl_destroy_pagetable); |
| 109 | } |
| 110 | |
| 111 | static struct kgsl_pagetable * |
| 112 | kgsl_get_pagetable(unsigned long name) |
| 113 | { |
| 114 | struct kgsl_pagetable *pt, *ret = NULL; |
| 115 | unsigned long flags; |
| 116 | |
| 117 | spin_lock_irqsave(&kgsl_driver.ptlock, flags); |
| 118 | list_for_each_entry(pt, &kgsl_driver.pagetable_list, list) { |
| 119 | if (pt->name == name) { |
| 120 | ret = pt; |
| 121 | kref_get(&ret->refcount); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | spin_unlock_irqrestore(&kgsl_driver.ptlock, flags); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static struct kgsl_pagetable * |
| 131 | _get_pt_from_kobj(struct kobject *kobj) |
| 132 | { |
| 133 | unsigned long ptname; |
| 134 | |
| 135 | if (!kobj) |
| 136 | return NULL; |
| 137 | |
| 138 | if (sscanf(kobj->name, "%ld", &ptname) != 1) |
| 139 | return NULL; |
| 140 | |
| 141 | return kgsl_get_pagetable(ptname); |
| 142 | } |
| 143 | |
| 144 | static ssize_t |
| 145 | sysfs_show_entries(struct kobject *kobj, |
| 146 | struct kobj_attribute *attr, |
| 147 | char *buf) |
| 148 | { |
| 149 | struct kgsl_pagetable *pt; |
| 150 | int ret = 0; |
| 151 | |
| 152 | pt = _get_pt_from_kobj(kobj); |
| 153 | |
| 154 | if (pt) |
Jeremy Gebben | a87bb86 | 2011-08-08 16:09:38 -0600 | [diff] [blame] | 155 | ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.entries); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 156 | |
| 157 | kgsl_put_pagetable(pt); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static ssize_t |
| 162 | sysfs_show_mapped(struct kobject *kobj, |
| 163 | struct kobj_attribute *attr, |
| 164 | char *buf) |
| 165 | { |
| 166 | struct kgsl_pagetable *pt; |
| 167 | int ret = 0; |
| 168 | |
| 169 | pt = _get_pt_from_kobj(kobj); |
| 170 | |
| 171 | if (pt) |
Jeremy Gebben | a87bb86 | 2011-08-08 16:09:38 -0600 | [diff] [blame] | 172 | ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.mapped); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 173 | |
| 174 | kgsl_put_pagetable(pt); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static ssize_t |
| 179 | sysfs_show_va_range(struct kobject *kobj, |
| 180 | struct kobj_attribute *attr, |
| 181 | char *buf) |
| 182 | { |
| 183 | struct kgsl_pagetable *pt; |
| 184 | int ret = 0; |
| 185 | |
| 186 | pt = _get_pt_from_kobj(kobj); |
| 187 | |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 188 | if (pt) { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 189 | ret += snprintf(buf, PAGE_SIZE, "0x%x\n", |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 190 | kgsl_mmu_get_ptsize()); |
| 191 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | |
| 193 | kgsl_put_pagetable(pt); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static ssize_t |
| 198 | sysfs_show_max_mapped(struct kobject *kobj, |
| 199 | struct kobj_attribute *attr, |
| 200 | char *buf) |
| 201 | { |
| 202 | struct kgsl_pagetable *pt; |
| 203 | int ret = 0; |
| 204 | |
| 205 | pt = _get_pt_from_kobj(kobj); |
| 206 | |
| 207 | if (pt) |
Jeremy Gebben | a87bb86 | 2011-08-08 16:09:38 -0600 | [diff] [blame] | 208 | ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.max_mapped); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | |
| 210 | kgsl_put_pagetable(pt); |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | static ssize_t |
| 215 | sysfs_show_max_entries(struct kobject *kobj, |
| 216 | struct kobj_attribute *attr, |
| 217 | char *buf) |
| 218 | { |
| 219 | struct kgsl_pagetable *pt; |
| 220 | int ret = 0; |
| 221 | |
| 222 | pt = _get_pt_from_kobj(kobj); |
| 223 | |
| 224 | if (pt) |
Jeremy Gebben | a87bb86 | 2011-08-08 16:09:38 -0600 | [diff] [blame] | 225 | ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.max_entries); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | |
| 227 | kgsl_put_pagetable(pt); |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | static struct kobj_attribute attr_entries = { |
| 232 | .attr = { .name = "entries", .mode = 0444 }, |
| 233 | .show = sysfs_show_entries, |
| 234 | .store = NULL, |
| 235 | }; |
| 236 | |
| 237 | static struct kobj_attribute attr_mapped = { |
| 238 | .attr = { .name = "mapped", .mode = 0444 }, |
| 239 | .show = sysfs_show_mapped, |
| 240 | .store = NULL, |
| 241 | }; |
| 242 | |
| 243 | static struct kobj_attribute attr_va_range = { |
| 244 | .attr = { .name = "va_range", .mode = 0444 }, |
| 245 | .show = sysfs_show_va_range, |
| 246 | .store = NULL, |
| 247 | }; |
| 248 | |
| 249 | static struct kobj_attribute attr_max_mapped = { |
| 250 | .attr = { .name = "max_mapped", .mode = 0444 }, |
| 251 | .show = sysfs_show_max_mapped, |
| 252 | .store = NULL, |
| 253 | }; |
| 254 | |
| 255 | static struct kobj_attribute attr_max_entries = { |
| 256 | .attr = { .name = "max_entries", .mode = 0444 }, |
| 257 | .show = sysfs_show_max_entries, |
| 258 | .store = NULL, |
| 259 | }; |
| 260 | |
| 261 | static struct attribute *pagetable_attrs[] = { |
| 262 | &attr_entries.attr, |
| 263 | &attr_mapped.attr, |
| 264 | &attr_va_range.attr, |
| 265 | &attr_max_mapped.attr, |
| 266 | &attr_max_entries.attr, |
| 267 | NULL, |
| 268 | }; |
| 269 | |
| 270 | static struct attribute_group pagetable_attr_group = { |
| 271 | .attrs = pagetable_attrs, |
| 272 | }; |
| 273 | |
| 274 | static void |
| 275 | pagetable_remove_sysfs_objects(struct kgsl_pagetable *pagetable) |
| 276 | { |
| 277 | if (pagetable->kobj) |
| 278 | sysfs_remove_group(pagetable->kobj, |
| 279 | &pagetable_attr_group); |
| 280 | |
| 281 | kobject_put(pagetable->kobj); |
| 282 | } |
| 283 | |
| 284 | static int |
| 285 | pagetable_add_sysfs_objects(struct kgsl_pagetable *pagetable) |
| 286 | { |
| 287 | char ptname[16]; |
| 288 | int ret = -ENOMEM; |
| 289 | |
| 290 | snprintf(ptname, sizeof(ptname), "%d", pagetable->name); |
| 291 | pagetable->kobj = kobject_create_and_add(ptname, |
| 292 | kgsl_driver.ptkobj); |
| 293 | if (pagetable->kobj == NULL) |
| 294 | goto err; |
| 295 | |
| 296 | ret = sysfs_create_group(pagetable->kobj, &pagetable_attr_group); |
| 297 | |
| 298 | err: |
| 299 | if (ret) { |
| 300 | if (pagetable->kobj) |
| 301 | kobject_put(pagetable->kobj); |
| 302 | |
| 303 | pagetable->kobj = NULL; |
| 304 | } |
| 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 309 | unsigned int kgsl_mmu_get_ptsize(void) |
| 310 | { |
| 311 | /* |
| 312 | * For IOMMU, we could do up to 4G virtual range if we wanted to, but |
| 313 | * it makes more sense to return a smaller range and leave the rest of |
| 314 | * the virtual range for future improvements |
| 315 | */ |
| 316 | |
| 317 | if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type) |
| 318 | return CONFIG_MSM_KGSL_PAGE_TABLE_SIZE; |
| 319 | else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type) |
| 320 | return SZ_2G; |
| 321 | else |
| 322 | return 0; |
| 323 | } |
| 324 | |
Sushmita Susheelendra | 354d971 | 2011-07-28 17:16:49 -0600 | [diff] [blame] | 325 | int |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 326 | kgsl_mmu_get_ptname_from_ptbase(unsigned int pt_base) |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 327 | { |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 328 | struct kgsl_pagetable *pt; |
| 329 | int ptid = -1; |
| 330 | |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 331 | spin_lock(&kgsl_driver.ptlock); |
| 332 | list_for_each_entry(pt, &kgsl_driver.pagetable_list, list) { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 333 | if (pt->pt_ops->mmu_pt_equal(pt, pt_base)) { |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 334 | ptid = (int) pt->name; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | spin_unlock(&kgsl_driver.ptlock); |
| 339 | |
Sushmita Susheelendra | 354d971 | 2011-07-28 17:16:49 -0600 | [diff] [blame] | 340 | return ptid; |
| 341 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 342 | EXPORT_SYMBOL(kgsl_mmu_get_ptname_from_ptbase); |
Sushmita Susheelendra | 354d971 | 2011-07-28 17:16:49 -0600 | [diff] [blame] | 343 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 344 | int kgsl_mmu_init(struct kgsl_device *device) |
| 345 | { |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 346 | int status = 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 347 | struct kgsl_mmu *mmu = &device->mmu; |
| 348 | |
| 349 | mmu->device = device; |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 350 | status = kgsl_allocate_contiguous(&mmu->setstate_memory, PAGE_SIZE); |
| 351 | if (status) |
| 352 | return status; |
| 353 | kgsl_sharedmem_set(&mmu->setstate_memory, 0, 0, |
| 354 | mmu->setstate_memory.size); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 355 | |
| 356 | if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type) { |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 357 | dev_info(device->dev, "|%s| MMU type set for device is " |
Shubhraprakash Das | f5526a1 | 2012-04-20 00:48:33 -0600 | [diff] [blame] | 358 | "NOMMU\n", __func__); |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 359 | goto done; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 360 | } else if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type) |
| 361 | mmu->mmu_ops = &gpummu_ops; |
| 362 | else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type) |
| 363 | mmu->mmu_ops = &iommu_ops; |
| 364 | |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 365 | status = mmu->mmu_ops->mmu_init(mmu); |
| 366 | done: |
| 367 | if (status) |
| 368 | kgsl_sharedmem_free(&mmu->setstate_memory); |
| 369 | return status; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 370 | } |
| 371 | EXPORT_SYMBOL(kgsl_mmu_init); |
| 372 | |
| 373 | int kgsl_mmu_start(struct kgsl_device *device) |
| 374 | { |
| 375 | struct kgsl_mmu *mmu = &device->mmu; |
| 376 | |
| 377 | if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) { |
| 378 | kgsl_regwrite(device, MH_MMU_CONFIG, 0); |
Shubhraprakash Das | 6b30c9f | 2012-04-20 01:15:55 -0600 | [diff] [blame] | 379 | /* Setup gpuaddr of global mappings */ |
| 380 | if (!mmu->setstate_memory.gpuaddr) |
| 381 | kgsl_setup_pt(NULL); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 382 | return 0; |
| 383 | } else { |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 384 | return mmu->mmu_ops->mmu_start(mmu); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | EXPORT_SYMBOL(kgsl_mmu_start); |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 388 | |
Jeremy Gebben | f4ea082 | 2012-04-05 16:27:08 -0600 | [diff] [blame] | 389 | static void mh_axi_error(struct kgsl_device *device, const char* type) |
| 390 | { |
| 391 | unsigned int reg, gpu_err, phys_err, pt_base; |
| 392 | |
| 393 | kgsl_regread(device, MH_AXI_ERROR, ®); |
| 394 | pt_base = kgsl_mmu_get_current_ptbase(&device->mmu); |
| 395 | /* |
| 396 | * Read gpu virtual and physical addresses that |
| 397 | * caused the error from the debug data. |
| 398 | */ |
| 399 | kgsl_regwrite(device, MH_DEBUG_CTRL, 44); |
| 400 | kgsl_regread(device, MH_DEBUG_DATA, &gpu_err); |
| 401 | kgsl_regwrite(device, MH_DEBUG_CTRL, 45); |
| 402 | kgsl_regread(device, MH_DEBUG_DATA, &phys_err); |
| 403 | KGSL_MEM_CRIT(device, |
| 404 | "axi %s error: %08x pt %08x gpu %08x phys %08x\n", |
| 405 | type, reg, pt_base, gpu_err, phys_err); |
| 406 | } |
| 407 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | void kgsl_mh_intrcallback(struct kgsl_device *device) |
| 409 | { |
| 410 | unsigned int status = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | |
| 412 | kgsl_regread(device, MH_INTERRUPT_STATUS, &status); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | |
| 414 | if (status & MH_INTERRUPT_MASK__AXI_READ_ERROR) |
Jeremy Gebben | f4ea082 | 2012-04-05 16:27:08 -0600 | [diff] [blame] | 415 | mh_axi_error(device, "read"); |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 416 | if (status & MH_INTERRUPT_MASK__AXI_WRITE_ERROR) |
Jeremy Gebben | f4ea082 | 2012-04-05 16:27:08 -0600 | [diff] [blame] | 417 | mh_axi_error(device, "write"); |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 418 | if (status & MH_INTERRUPT_MASK__MMU_PAGE_FAULT) |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 419 | device->mmu.mmu_ops->mmu_pagefault(&device->mmu); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 420 | |
Jordan Crouse | c8c9fcd | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 421 | status &= KGSL_MMU_INT_MASK; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | kgsl_regwrite(device, MH_INTERRUPT_CLEAR, status); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | } |
| 424 | EXPORT_SYMBOL(kgsl_mh_intrcallback); |
| 425 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 426 | static struct kgsl_pagetable *kgsl_mmu_createpagetableobject( |
| 427 | unsigned int name) |
| 428 | { |
| 429 | int status = 0; |
| 430 | struct kgsl_pagetable *pagetable = NULL; |
| 431 | unsigned long flags; |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 432 | unsigned int ptsize; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 433 | |
| 434 | pagetable = kzalloc(sizeof(struct kgsl_pagetable), GFP_KERNEL); |
| 435 | if (pagetable == NULL) { |
| 436 | KGSL_CORE_ERR("kzalloc(%d) failed\n", |
| 437 | sizeof(struct kgsl_pagetable)); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | kref_init(&pagetable->refcount); |
| 442 | |
| 443 | spin_lock_init(&pagetable->lock); |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 444 | |
| 445 | ptsize = kgsl_mmu_get_ptsize(); |
| 446 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 447 | pagetable->name = name; |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 448 | pagetable->max_entries = KGSL_PAGETABLE_ENTRIES(ptsize); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 449 | |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 450 | /* |
| 451 | * create a separate kgsl pool for IOMMU, global mappings can be mapped |
| 452 | * just once from this pool of the defaultpagetable |
| 453 | */ |
| 454 | if ((KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) && |
| 455 | (KGSL_MMU_GLOBAL_PT == name)) { |
Jeremy Gebben | c589ccb | 2012-05-16 10:26:20 -0600 | [diff] [blame] | 456 | pagetable->kgsl_pool = gen_pool_create(KGSL_MMU_ALIGN_SHIFT, |
| 457 | -1); |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 458 | if (pagetable->kgsl_pool == NULL) { |
| 459 | KGSL_CORE_ERR("gen_pool_create(%d) failed\n", |
Jeremy Gebben | c589ccb | 2012-05-16 10:26:20 -0600 | [diff] [blame] | 460 | KGSL_MMU_ALIGN_SHIFT); |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 461 | goto err_alloc; |
| 462 | } |
| 463 | if (gen_pool_add(pagetable->kgsl_pool, |
| 464 | KGSL_IOMMU_GLOBAL_MEM_BASE, |
| 465 | KGSL_IOMMU_GLOBAL_MEM_SIZE, -1)) { |
| 466 | KGSL_CORE_ERR("gen_pool_add failed\n"); |
| 467 | goto err_kgsl_pool; |
| 468 | } |
| 469 | } |
| 470 | |
Jeremy Gebben | c589ccb | 2012-05-16 10:26:20 -0600 | [diff] [blame] | 471 | pagetable->pool = gen_pool_create(KGSL_MMU_ALIGN_SHIFT, -1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 472 | if (pagetable->pool == NULL) { |
Jeremy Gebben | c589ccb | 2012-05-16 10:26:20 -0600 | [diff] [blame] | 473 | KGSL_CORE_ERR("gen_pool_create(%d) failed\n", |
| 474 | KGSL_MMU_ALIGN_SHIFT); |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 475 | goto err_kgsl_pool; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 478 | if (gen_pool_add(pagetable->pool, KGSL_PAGETABLE_BASE, |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 479 | ptsize, -1)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 480 | KGSL_CORE_ERR("gen_pool_add failed\n"); |
| 481 | goto err_pool; |
| 482 | } |
| 483 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 484 | if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type) |
| 485 | pagetable->pt_ops = &gpummu_pt_ops; |
| 486 | else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type) |
| 487 | pagetable->pt_ops = &iommu_pt_ops; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 488 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 489 | pagetable->priv = pagetable->pt_ops->mmu_create_pagetable(); |
| 490 | if (!pagetable->priv) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 491 | goto err_pool; |
| 492 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 493 | status = kgsl_setup_pt(pagetable); |
| 494 | if (status) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 495 | goto err_mmu_create; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 496 | |
| 497 | spin_lock_irqsave(&kgsl_driver.ptlock, flags); |
| 498 | list_add(&pagetable->list, &kgsl_driver.pagetable_list); |
| 499 | spin_unlock_irqrestore(&kgsl_driver.ptlock, flags); |
| 500 | |
| 501 | /* Create the sysfs entries */ |
| 502 | pagetable_add_sysfs_objects(pagetable); |
| 503 | |
| 504 | return pagetable; |
| 505 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 506 | err_mmu_create: |
| 507 | pagetable->pt_ops->mmu_destroy_pagetable(pagetable->priv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 508 | err_pool: |
| 509 | gen_pool_destroy(pagetable->pool); |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 510 | err_kgsl_pool: |
| 511 | if (pagetable->kgsl_pool) |
| 512 | gen_pool_destroy(pagetable->kgsl_pool); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 513 | err_alloc: |
| 514 | kfree(pagetable); |
| 515 | |
| 516 | return NULL; |
| 517 | } |
| 518 | |
| 519 | struct kgsl_pagetable *kgsl_mmu_getpagetable(unsigned long name) |
| 520 | { |
| 521 | struct kgsl_pagetable *pt; |
| 522 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 523 | if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type) |
| 524 | return (void *)(-1); |
| 525 | |
Shubhraprakash Das | d8cbcd1 | 2012-05-07 16:11:32 -0600 | [diff] [blame] | 526 | #ifndef CONFIG_KGSL_PER_PROCESS_PAGE_TABLE |
| 527 | name = KGSL_MMU_GLOBAL_PT; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 528 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 529 | pt = kgsl_get_pagetable(name); |
| 530 | |
| 531 | if (pt == NULL) |
| 532 | pt = kgsl_mmu_createpagetableobject(name); |
| 533 | |
| 534 | return pt; |
| 535 | } |
| 536 | |
| 537 | void kgsl_mmu_putpagetable(struct kgsl_pagetable *pagetable) |
| 538 | { |
| 539 | kgsl_put_pagetable(pagetable); |
| 540 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 541 | EXPORT_SYMBOL(kgsl_mmu_putpagetable); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 542 | |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 543 | void kgsl_setstate(struct kgsl_mmu *mmu, uint32_t flags) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 544 | { |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 545 | struct kgsl_device *device = mmu->device; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 546 | if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type) |
| 547 | return; |
Shubhraprakash Das | c6e2101 | 2012-05-11 17:24:51 -0600 | [diff] [blame] | 548 | else if (device->ftbl->setstate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 549 | device->ftbl->setstate(device, flags); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 550 | else if (mmu->mmu_ops->mmu_device_setstate) |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 551 | mmu->mmu_ops->mmu_device_setstate(mmu, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 552 | } |
| 553 | EXPORT_SYMBOL(kgsl_setstate); |
| 554 | |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 555 | void kgsl_mh_start(struct kgsl_device *device) |
| 556 | { |
| 557 | struct kgsl_mh *mh = &device->mh; |
| 558 | /* force mmu off to for now*/ |
| 559 | kgsl_regwrite(device, MH_MMU_CONFIG, 0); |
| 560 | kgsl_idle(device, KGSL_TIMEOUT_DEFAULT); |
| 561 | |
| 562 | /* define physical memory range accessible by the core */ |
| 563 | kgsl_regwrite(device, MH_MMU_MPU_BASE, mh->mpu_base); |
| 564 | kgsl_regwrite(device, MH_MMU_MPU_END, |
| 565 | mh->mpu_base + mh->mpu_range); |
| 566 | kgsl_regwrite(device, MH_ARBITER_CONFIG, mh->mharb); |
| 567 | |
| 568 | if (mh->mh_intf_cfg1 != 0) |
| 569 | kgsl_regwrite(device, MH_CLNT_INTF_CTRL_CONFIG1, |
| 570 | mh->mh_intf_cfg1); |
| 571 | |
| 572 | if (mh->mh_intf_cfg2 != 0) |
| 573 | kgsl_regwrite(device, MH_CLNT_INTF_CTRL_CONFIG2, |
| 574 | mh->mh_intf_cfg2); |
| 575 | |
| 576 | /* |
| 577 | * Interrupts are enabled on a per-device level when |
| 578 | * kgsl_pwrctrl_irq() is called |
| 579 | */ |
| 580 | } |
| 581 | |
Jeremy Gebben | 1b9b1f14 | 2012-05-16 10:43:28 -0600 | [diff] [blame] | 582 | static inline struct gen_pool * |
| 583 | _get_pool(struct kgsl_pagetable *pagetable, unsigned int flags) |
| 584 | { |
| 585 | if (pagetable->kgsl_pool && |
| 586 | (KGSL_MEMFLAGS_GLOBAL & flags)) |
| 587 | return pagetable->kgsl_pool; |
| 588 | return pagetable->pool; |
| 589 | } |
| 590 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 591 | int |
| 592 | kgsl_mmu_map(struct kgsl_pagetable *pagetable, |
| 593 | struct kgsl_memdesc *memdesc, |
| 594 | unsigned int protflags) |
| 595 | { |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 596 | int ret; |
Jeremy Gebben | 1b9b1f14 | 2012-05-16 10:43:28 -0600 | [diff] [blame] | 597 | struct gen_pool *pool; |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 598 | int size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 599 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 600 | if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) { |
Jordan Crouse | 40861a4 | 2012-02-06 10:18:23 -0700 | [diff] [blame] | 601 | if (memdesc->sglen == 1) { |
Shubhraprakash Das | 4d6af2b | 2012-04-20 00:35:03 -0600 | [diff] [blame] | 602 | memdesc->gpuaddr = sg_dma_address(memdesc->sg); |
| 603 | if (!memdesc->gpuaddr) |
| 604 | memdesc->gpuaddr = sg_phys(memdesc->sg); |
| 605 | if (!memdesc->gpuaddr) { |
| 606 | KGSL_CORE_ERR("Unable to get a valid physical " |
| 607 | "address for memdesc\n"); |
| 608 | return -EINVAL; |
| 609 | } |
Jordan Crouse | 40861a4 | 2012-02-06 10:18:23 -0700 | [diff] [blame] | 610 | return 0; |
| 611 | } else { |
| 612 | KGSL_CORE_ERR("Memory is not contigious " |
| 613 | "(sglen = %d)\n", memdesc->sglen); |
| 614 | return -EINVAL; |
| 615 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 616 | } |
Jordan Crouse | 40861a4 | 2012-02-06 10:18:23 -0700 | [diff] [blame] | 617 | |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 618 | size = kgsl_sg_size(memdesc->sg, memdesc->sglen); |
| 619 | |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 620 | /* Allocate from kgsl pool if it exists for global mappings */ |
Jeremy Gebben | 1b9b1f14 | 2012-05-16 10:43:28 -0600 | [diff] [blame] | 621 | pool = _get_pool(pagetable, memdesc->priv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 622 | |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 623 | memdesc->gpuaddr = gen_pool_alloc(pool, size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | if (memdesc->gpuaddr == 0) { |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 625 | KGSL_CORE_ERR("gen_pool_alloc(%d) failed from pool: %s\n", |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 626 | size, |
Jeremy Gebben | 1b9b1f14 | 2012-05-16 10:43:28 -0600 | [diff] [blame] | 627 | (pool == pagetable->kgsl_pool) ? |
| 628 | "kgsl_pool" : "general_pool"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 629 | KGSL_CORE_ERR(" [%d] allocated=%d, entries=%d\n", |
| 630 | pagetable->name, pagetable->stats.mapped, |
| 631 | pagetable->stats.entries); |
| 632 | return -ENOMEM; |
| 633 | } |
| 634 | |
Shubhraprakash Das | badaeda | 2012-03-21 00:31:39 -0600 | [diff] [blame] | 635 | if (KGSL_MMU_TYPE_IOMMU != kgsl_mmu_get_mmutype()) |
| 636 | spin_lock(&pagetable->lock); |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 637 | ret = pagetable->pt_ops->mmu_map(pagetable->priv, memdesc, protflags, |
| 638 | &pagetable->tlb_flags); |
Shubhraprakash Das | badaeda | 2012-03-21 00:31:39 -0600 | [diff] [blame] | 639 | if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) |
| 640 | spin_lock(&pagetable->lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 641 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 642 | if (ret) |
| 643 | goto err_free_gpuaddr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 644 | |
| 645 | /* Keep track of the statistics for the sysfs files */ |
| 646 | |
| 647 | KGSL_STATS_ADD(1, pagetable->stats.entries, |
| 648 | pagetable->stats.max_entries); |
| 649 | |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 650 | KGSL_STATS_ADD(size, pagetable->stats.mapped, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 651 | pagetable->stats.max_mapped); |
| 652 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 653 | spin_unlock(&pagetable->lock); |
| 654 | |
| 655 | return 0; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 656 | |
| 657 | err_free_gpuaddr: |
| 658 | spin_unlock(&pagetable->lock); |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 659 | gen_pool_free(pool, memdesc->gpuaddr, size); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 660 | memdesc->gpuaddr = 0; |
| 661 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 662 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 663 | EXPORT_SYMBOL(kgsl_mmu_map); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 664 | |
| 665 | int |
| 666 | kgsl_mmu_unmap(struct kgsl_pagetable *pagetable, |
| 667 | struct kgsl_memdesc *memdesc) |
| 668 | { |
Jeremy Gebben | 1b9b1f14 | 2012-05-16 10:43:28 -0600 | [diff] [blame] | 669 | struct gen_pool *pool; |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 670 | int size; |
| 671 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 672 | if (memdesc->size == 0 || memdesc->gpuaddr == 0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 673 | return 0; |
| 674 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 675 | if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) { |
| 676 | memdesc->gpuaddr = 0; |
| 677 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 678 | } |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 679 | |
| 680 | size = kgsl_sg_size(memdesc->sg, memdesc->sglen); |
| 681 | |
Shubhraprakash Das | badaeda | 2012-03-21 00:31:39 -0600 | [diff] [blame] | 682 | if (KGSL_MMU_TYPE_IOMMU != kgsl_mmu_get_mmutype()) |
| 683 | spin_lock(&pagetable->lock); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 684 | pagetable->pt_ops->mmu_unmap(pagetable->priv, memdesc); |
Shubhraprakash Das | badaeda | 2012-03-21 00:31:39 -0600 | [diff] [blame] | 685 | if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) |
| 686 | spin_lock(&pagetable->lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 687 | /* Remove the statistics */ |
| 688 | pagetable->stats.entries--; |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 689 | pagetable->stats.mapped -= size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 690 | |
| 691 | spin_unlock(&pagetable->lock); |
| 692 | |
Jeremy Gebben | 1b9b1f14 | 2012-05-16 10:43:28 -0600 | [diff] [blame] | 693 | pool = _get_pool(pagetable, memdesc->priv); |
Jordan Crouse | 3c86ca8 | 2012-05-21 08:41:52 -0600 | [diff] [blame^] | 694 | gen_pool_free(pool, memdesc->gpuaddr, size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 695 | |
Jeremy Gebben | 7faf9ec | 2012-03-21 14:09:55 -0600 | [diff] [blame] | 696 | /* |
| 697 | * Don't clear the gpuaddr on global mappings because they |
| 698 | * may be in use by other pagetables |
| 699 | */ |
| 700 | if (!(memdesc->priv & KGSL_MEMFLAGS_GLOBAL)) |
| 701 | memdesc->gpuaddr = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | EXPORT_SYMBOL(kgsl_mmu_unmap); |
| 705 | |
| 706 | int kgsl_mmu_map_global(struct kgsl_pagetable *pagetable, |
| 707 | struct kgsl_memdesc *memdesc, unsigned int protflags) |
| 708 | { |
| 709 | int result = -EINVAL; |
| 710 | unsigned int gpuaddr = 0; |
| 711 | |
| 712 | if (memdesc == NULL) { |
| 713 | KGSL_CORE_ERR("invalid memdesc\n"); |
| 714 | goto error; |
| 715 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 716 | /* Not all global mappings are needed for all MMU types */ |
| 717 | if (!memdesc->size) |
| 718 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 719 | |
| 720 | gpuaddr = memdesc->gpuaddr; |
Shubhraprakash Das | 84fdb11 | 2012-04-04 12:49:31 -0600 | [diff] [blame] | 721 | memdesc->priv |= KGSL_MEMFLAGS_GLOBAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 722 | |
| 723 | result = kgsl_mmu_map(pagetable, memdesc, protflags); |
| 724 | if (result) |
| 725 | goto error; |
| 726 | |
| 727 | /*global mappings must have the same gpu address in all pagetables*/ |
| 728 | if (gpuaddr && gpuaddr != memdesc->gpuaddr) { |
| 729 | KGSL_CORE_ERR("pt %p addr mismatch phys 0x%08x" |
| 730 | "gpu 0x%0x 0x%08x", pagetable, memdesc->physaddr, |
| 731 | gpuaddr, memdesc->gpuaddr); |
| 732 | goto error_unmap; |
| 733 | } |
| 734 | return result; |
| 735 | error_unmap: |
| 736 | kgsl_mmu_unmap(pagetable, memdesc); |
| 737 | error: |
| 738 | return result; |
| 739 | } |
| 740 | EXPORT_SYMBOL(kgsl_mmu_map_global); |
| 741 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 742 | int kgsl_mmu_close(struct kgsl_device *device) |
| 743 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 744 | struct kgsl_mmu *mmu = &device->mmu; |
| 745 | |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 746 | kgsl_sharedmem_free(&mmu->setstate_memory); |
| 747 | if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 748 | return 0; |
Shubhraprakash Das | 0ff034f | 2012-05-02 15:51:07 -0600 | [diff] [blame] | 749 | else |
Shubhraprakash Das | 1c52826 | 2012-04-26 17:38:13 -0600 | [diff] [blame] | 750 | return mmu->mmu_ops->mmu_close(mmu); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 751 | } |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 752 | EXPORT_SYMBOL(kgsl_mmu_close); |
| 753 | |
| 754 | int kgsl_mmu_pt_get_flags(struct kgsl_pagetable *pt, |
| 755 | enum kgsl_deviceid id) |
| 756 | { |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 757 | unsigned int result = 0; |
| 758 | |
| 759 | if (pt == NULL) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 760 | return 0; |
Shubhraprakash Das | f764e46 | 2012-04-26 15:38:09 -0600 | [diff] [blame] | 761 | |
| 762 | spin_lock(&pt->lock); |
| 763 | if (pt->tlb_flags && (1<<id)) { |
| 764 | result = KGSL_MMUFLAGS_TLBFLUSH; |
| 765 | pt->tlb_flags &= ~(1<<id); |
| 766 | } |
| 767 | spin_unlock(&pt->lock); |
| 768 | return result; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 769 | } |
| 770 | EXPORT_SYMBOL(kgsl_mmu_pt_get_flags); |
| 771 | |
| 772 | void kgsl_mmu_ptpool_destroy(void *ptpool) |
| 773 | { |
| 774 | if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type) |
| 775 | kgsl_gpummu_ptpool_destroy(ptpool); |
| 776 | ptpool = 0; |
| 777 | } |
| 778 | EXPORT_SYMBOL(kgsl_mmu_ptpool_destroy); |
| 779 | |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 780 | void *kgsl_mmu_ptpool_init(int entries) |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 781 | { |
| 782 | if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type) |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 783 | return kgsl_gpummu_ptpool_init(entries); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 784 | else |
| 785 | return (void *)(-1); |
| 786 | } |
| 787 | EXPORT_SYMBOL(kgsl_mmu_ptpool_init); |
| 788 | |
| 789 | int kgsl_mmu_enabled(void) |
| 790 | { |
| 791 | if (KGSL_MMU_TYPE_NONE != kgsl_mmu_type) |
| 792 | return 1; |
| 793 | else |
| 794 | return 0; |
| 795 | } |
| 796 | EXPORT_SYMBOL(kgsl_mmu_enabled); |
| 797 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 798 | enum kgsl_mmutype kgsl_mmu_get_mmutype(void) |
| 799 | { |
| 800 | return kgsl_mmu_type; |
| 801 | } |
| 802 | EXPORT_SYMBOL(kgsl_mmu_get_mmutype); |
| 803 | |
| 804 | void kgsl_mmu_set_mmutype(char *mmutype) |
| 805 | { |
Jordan Crouse | 817e0b9 | 2012-02-04 10:23:53 -0700 | [diff] [blame] | 806 | /* Set the default MMU - GPU on <=8960 and nothing on >= 8064 */ |
| 807 | kgsl_mmu_type = |
| 808 | cpu_is_apq8064() ? KGSL_MMU_TYPE_NONE : KGSL_MMU_TYPE_GPU; |
| 809 | |
| 810 | /* Use the IOMMU if it is found */ |
| 811 | if (iommu_found()) |
| 812 | kgsl_mmu_type = KGSL_MMU_TYPE_IOMMU; |
| 813 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 814 | if (mmutype && !strncmp(mmutype, "gpummu", 6)) |
| 815 | kgsl_mmu_type = KGSL_MMU_TYPE_GPU; |
| 816 | if (iommu_found() && mmutype && !strncmp(mmutype, "iommu", 5)) |
| 817 | kgsl_mmu_type = KGSL_MMU_TYPE_IOMMU; |
| 818 | if (mmutype && !strncmp(mmutype, "nommu", 5)) |
| 819 | kgsl_mmu_type = KGSL_MMU_TYPE_NONE; |
| 820 | } |
| 821 | EXPORT_SYMBOL(kgsl_mmu_set_mmutype); |