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