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