blob: dfaadbace9976089d051172cc7ac3c1ed7ceadfa [file] [log] [blame]
Jordan Crouse00714012012-03-16 14:53:40 -06001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/export.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070014#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 Das767fdda2011-08-15 15:49:45 -060020#include <linux/iommu.h>
Jordan Crouse817e0b92012-02-04 10:23:53 -070021#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022
23#include "kgsl.h"
24#include "kgsl_mmu.h"
25#include "kgsl_device.h"
26#include "kgsl_sharedmem.h"
Jeremy Gebbena3d07a42011-10-17 12:08:16 -060027#include "adreno_postmortem.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#define KGSL_MMU_ALIGN_SHIFT 13
30#define KGSL_MMU_ALIGN_MASK (~((1 << KGSL_MMU_ALIGN_SHIFT) - 1))
31
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060032static enum kgsl_mmutype kgsl_mmu_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34static void pagetable_remove_sysfs_objects(struct kgsl_pagetable *pagetable);
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036static int kgsl_cleanup_pt(struct kgsl_pagetable *pt)
37{
38 int i;
Shubhraprakash Das84fdb112012-04-04 12:49:31 -060039 /* For IOMMU only unmap the global structures to global pt */
Shubhraprakash Das6b30c9f2012-04-20 01:15:55 -060040 if ((KGSL_MMU_TYPE_NONE != kgsl_mmu_type) &&
41 (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type) &&
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -060042 (KGSL_MMU_GLOBAL_PT != pt->name) &&
43 (KGSL_MMU_PRIV_BANK_TABLE_NAME != pt->name))
Shubhraprakash Das84fdb112012-04-04 12:49:31 -060044 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045 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 Das6b30c9f2012-04-20 01:15:55 -060053
54static 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 Das19ca4a62012-05-18 12:11:20 -060062 (KGSL_MMU_GLOBAL_PT != pt->name) &&
63 (KGSL_MMU_PRIV_BANK_TABLE_NAME != pt->name))
Shubhraprakash Das6b30c9f2012-04-20 01:15:55 -060064 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;
74error_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 Huntsman3f2bc4d2011-08-16 17:27:22 -070084static 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 Das84fdb112012-04-04 12:49:31 -060098 if (pagetable->kgsl_pool)
99 gen_pool_destroy(pagetable->kgsl_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100 if (pagetable->pool)
101 gen_pool_destroy(pagetable->pool);
102
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600103 pagetable->pt_ops->mmu_destroy_pagetable(pagetable->priv);
104
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 kfree(pagetable);
106}
107
108static inline void kgsl_put_pagetable(struct kgsl_pagetable *pagetable)
109{
110 if (pagetable)
111 kref_put(&pagetable->refcount, kgsl_destroy_pagetable);
112}
113
114static struct kgsl_pagetable *
115kgsl_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
133static 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
147static ssize_t
148sysfs_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 Gebbena87bb862011-08-08 16:09:38 -0600158 ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.entries);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159
160 kgsl_put_pagetable(pt);
161 return ret;
162}
163
164static ssize_t
165sysfs_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 Gebbena87bb862011-08-08 16:09:38 -0600175 ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.mapped);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176
177 kgsl_put_pagetable(pt);
178 return ret;
179}
180
181static ssize_t
182sysfs_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 Crouse6d76c4d2012-03-26 09:50:43 -0600191 if (pt) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600192 ret += snprintf(buf, PAGE_SIZE, "0x%x\n",
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600193 kgsl_mmu_get_ptsize());
194 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195
196 kgsl_put_pagetable(pt);
197 return ret;
198}
199
200static ssize_t
201sysfs_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 Gebbena87bb862011-08-08 16:09:38 -0600211 ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.max_mapped);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213 kgsl_put_pagetable(pt);
214 return ret;
215}
216
217static ssize_t
218sysfs_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 Gebbena87bb862011-08-08 16:09:38 -0600228 ret += snprintf(buf, PAGE_SIZE, "%d\n", pt->stats.max_entries);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229
230 kgsl_put_pagetable(pt);
231 return ret;
232}
233
234static struct kobj_attribute attr_entries = {
235 .attr = { .name = "entries", .mode = 0444 },
236 .show = sysfs_show_entries,
237 .store = NULL,
238};
239
240static struct kobj_attribute attr_mapped = {
241 .attr = { .name = "mapped", .mode = 0444 },
242 .show = sysfs_show_mapped,
243 .store = NULL,
244};
245
246static struct kobj_attribute attr_va_range = {
247 .attr = { .name = "va_range", .mode = 0444 },
248 .show = sysfs_show_va_range,
249 .store = NULL,
250};
251
252static struct kobj_attribute attr_max_mapped = {
253 .attr = { .name = "max_mapped", .mode = 0444 },
254 .show = sysfs_show_max_mapped,
255 .store = NULL,
256};
257
258static struct kobj_attribute attr_max_entries = {
259 .attr = { .name = "max_entries", .mode = 0444 },
260 .show = sysfs_show_max_entries,
261 .store = NULL,
262};
263
264static 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
273static struct attribute_group pagetable_attr_group = {
274 .attrs = pagetable_attrs,
275};
276
277static void
278pagetable_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
287static int
288pagetable_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
301err:
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 Crouse6d76c4d2012-03-26 09:50:43 -0600312unsigned 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)
323 return SZ_2G;
324 else
325 return 0;
326}
327
Sushmita Susheelendra354d9712011-07-28 17:16:49 -0600328int
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600329kgsl_mmu_get_ptname_from_ptbase(unsigned int pt_base)
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600330{
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600331 struct kgsl_pagetable *pt;
332 int ptid = -1;
333
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600334 spin_lock(&kgsl_driver.ptlock);
335 list_for_each_entry(pt, &kgsl_driver.pagetable_list, list) {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600336 if (pt->pt_ops->mmu_pt_equal(pt, pt_base)) {
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600337 ptid = (int) pt->name;
338 break;
339 }
340 }
341 spin_unlock(&kgsl_driver.ptlock);
342
Sushmita Susheelendra354d9712011-07-28 17:16:49 -0600343 return ptid;
344}
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600345EXPORT_SYMBOL(kgsl_mmu_get_ptname_from_ptbase);
Sushmita Susheelendra354d9712011-07-28 17:16:49 -0600346
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600347int kgsl_mmu_init(struct kgsl_device *device)
348{
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600349 int status = 0;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600350 struct kgsl_mmu *mmu = &device->mmu;
351
352 mmu->device = device;
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600353 status = kgsl_allocate_contiguous(&mmu->setstate_memory, PAGE_SIZE);
354 if (status)
355 return status;
356 kgsl_sharedmem_set(&mmu->setstate_memory, 0, 0,
357 mmu->setstate_memory.size);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600358
359 if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type) {
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600360 dev_info(device->dev, "|%s| MMU type set for device is "
Shubhraprakash Dasf5526a12012-04-20 00:48:33 -0600361 "NOMMU\n", __func__);
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600362 goto done;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600363 } else if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type)
364 mmu->mmu_ops = &gpummu_ops;
365 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type)
366 mmu->mmu_ops = &iommu_ops;
367
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600368 status = mmu->mmu_ops->mmu_init(mmu);
369done:
370 if (status)
371 kgsl_sharedmem_free(&mmu->setstate_memory);
372 return status;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600373}
374EXPORT_SYMBOL(kgsl_mmu_init);
375
376int kgsl_mmu_start(struct kgsl_device *device)
377{
378 struct kgsl_mmu *mmu = &device->mmu;
379
380 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) {
381 kgsl_regwrite(device, MH_MMU_CONFIG, 0);
Shubhraprakash Das6b30c9f2012-04-20 01:15:55 -0600382 /* Setup gpuaddr of global mappings */
383 if (!mmu->setstate_memory.gpuaddr)
384 kgsl_setup_pt(NULL);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600385 return 0;
386 } else {
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600387 return mmu->mmu_ops->mmu_start(mmu);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600388 }
389}
390EXPORT_SYMBOL(kgsl_mmu_start);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600391
Jeremy Gebbenf4ea0822012-04-05 16:27:08 -0600392static void mh_axi_error(struct kgsl_device *device, const char* type)
393{
394 unsigned int reg, gpu_err, phys_err, pt_base;
395
396 kgsl_regread(device, MH_AXI_ERROR, &reg);
397 pt_base = kgsl_mmu_get_current_ptbase(&device->mmu);
398 /*
399 * Read gpu virtual and physical addresses that
400 * caused the error from the debug data.
401 */
402 kgsl_regwrite(device, MH_DEBUG_CTRL, 44);
403 kgsl_regread(device, MH_DEBUG_DATA, &gpu_err);
404 kgsl_regwrite(device, MH_DEBUG_CTRL, 45);
405 kgsl_regread(device, MH_DEBUG_DATA, &phys_err);
406 KGSL_MEM_CRIT(device,
407 "axi %s error: %08x pt %08x gpu %08x phys %08x\n",
408 type, reg, pt_base, gpu_err, phys_err);
409}
410
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411void kgsl_mh_intrcallback(struct kgsl_device *device)
412{
413 unsigned int status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414
415 kgsl_regread(device, MH_INTERRUPT_STATUS, &status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417 if (status & MH_INTERRUPT_MASK__AXI_READ_ERROR)
Jeremy Gebbenf4ea0822012-04-05 16:27:08 -0600418 mh_axi_error(device, "read");
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600419 if (status & MH_INTERRUPT_MASK__AXI_WRITE_ERROR)
Jeremy Gebbenf4ea0822012-04-05 16:27:08 -0600420 mh_axi_error(device, "write");
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600421 if (status & MH_INTERRUPT_MASK__MMU_PAGE_FAULT)
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600422 device->mmu.mmu_ops->mmu_pagefault(&device->mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423
Jordan Crousec8c9fcd2011-07-28 08:37:58 -0600424 status &= KGSL_MMU_INT_MASK;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 kgsl_regwrite(device, MH_INTERRUPT_CLEAR, status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426}
427EXPORT_SYMBOL(kgsl_mh_intrcallback);
428
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429static struct kgsl_pagetable *kgsl_mmu_createpagetableobject(
430 unsigned int name)
431{
432 int status = 0;
433 struct kgsl_pagetable *pagetable = NULL;
434 unsigned long flags;
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600435 unsigned int ptsize;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436
437 pagetable = kzalloc(sizeof(struct kgsl_pagetable), GFP_KERNEL);
438 if (pagetable == NULL) {
439 KGSL_CORE_ERR("kzalloc(%d) failed\n",
440 sizeof(struct kgsl_pagetable));
441 return NULL;
442 }
443
444 kref_init(&pagetable->refcount);
445
446 spin_lock_init(&pagetable->lock);
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600447
448 ptsize = kgsl_mmu_get_ptsize();
449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 pagetable->name = name;
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600451 pagetable->max_entries = KGSL_PAGETABLE_ENTRIES(ptsize);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600453 /*
454 * create a separate kgsl pool for IOMMU, global mappings can be mapped
455 * just once from this pool of the defaultpagetable
456 */
457 if ((KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype()) &&
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600458 ((KGSL_MMU_GLOBAL_PT == name) ||
459 (KGSL_MMU_PRIV_BANK_TABLE_NAME == name))) {
460 pagetable->kgsl_pool = gen_pool_create(PAGE_SHIFT, -1);
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600461 if (pagetable->kgsl_pool == NULL) {
462 KGSL_CORE_ERR("gen_pool_create(%d) failed\n",
Jeremy Gebbenc589ccb2012-05-16 10:26:20 -0600463 KGSL_MMU_ALIGN_SHIFT);
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600464 goto err_alloc;
465 }
466 if (gen_pool_add(pagetable->kgsl_pool,
467 KGSL_IOMMU_GLOBAL_MEM_BASE,
468 KGSL_IOMMU_GLOBAL_MEM_SIZE, -1)) {
469 KGSL_CORE_ERR("gen_pool_add failed\n");
470 goto err_kgsl_pool;
471 }
472 }
473
Jeremy Gebbenc589ccb2012-05-16 10:26:20 -0600474 pagetable->pool = gen_pool_create(KGSL_MMU_ALIGN_SHIFT, -1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 if (pagetable->pool == NULL) {
Jeremy Gebbenc589ccb2012-05-16 10:26:20 -0600476 KGSL_CORE_ERR("gen_pool_create(%d) failed\n",
477 KGSL_MMU_ALIGN_SHIFT);
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600478 goto err_kgsl_pool;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 }
480
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600481 if (gen_pool_add(pagetable->pool, KGSL_PAGETABLE_BASE,
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600482 ptsize, -1)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 KGSL_CORE_ERR("gen_pool_add failed\n");
484 goto err_pool;
485 }
486
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600487 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type)
488 pagetable->pt_ops = &gpummu_pt_ops;
489 else if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_type)
490 pagetable->pt_ops = &iommu_pt_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600492 pagetable->priv = pagetable->pt_ops->mmu_create_pagetable();
493 if (!pagetable->priv)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 goto err_pool;
495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 status = kgsl_setup_pt(pagetable);
497 if (status)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600498 goto err_mmu_create;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
500 spin_lock_irqsave(&kgsl_driver.ptlock, flags);
501 list_add(&pagetable->list, &kgsl_driver.pagetable_list);
502 spin_unlock_irqrestore(&kgsl_driver.ptlock, flags);
503
504 /* Create the sysfs entries */
505 pagetable_add_sysfs_objects(pagetable);
506
507 return pagetable;
508
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600509err_mmu_create:
510 pagetable->pt_ops->mmu_destroy_pagetable(pagetable->priv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511err_pool:
512 gen_pool_destroy(pagetable->pool);
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600513err_kgsl_pool:
514 if (pagetable->kgsl_pool)
515 gen_pool_destroy(pagetable->kgsl_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516err_alloc:
517 kfree(pagetable);
518
519 return NULL;
520}
521
522struct kgsl_pagetable *kgsl_mmu_getpagetable(unsigned long name)
523{
524 struct kgsl_pagetable *pt;
525
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600526 if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type)
527 return (void *)(-1);
528
Shubhraprakash Dasd8cbcd12012-05-07 16:11:32 -0600529#ifndef CONFIG_KGSL_PER_PROCESS_PAGE_TABLE
530 name = KGSL_MMU_GLOBAL_PT;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600531#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 pt = kgsl_get_pagetable(name);
533
534 if (pt == NULL)
535 pt = kgsl_mmu_createpagetableobject(name);
536
537 return pt;
538}
539
540void kgsl_mmu_putpagetable(struct kgsl_pagetable *pagetable)
541{
542 kgsl_put_pagetable(pagetable);
543}
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600544EXPORT_SYMBOL(kgsl_mmu_putpagetable);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600546void kgsl_setstate(struct kgsl_mmu *mmu, uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547{
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600548 struct kgsl_device *device = mmu->device;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600549 if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type)
550 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600551 else if (device->ftbl->setstate)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 device->ftbl->setstate(device, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600553 else if (mmu->mmu_ops->mmu_device_setstate)
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600554 mmu->mmu_ops->mmu_device_setstate(mmu, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555}
556EXPORT_SYMBOL(kgsl_setstate);
557
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600558void kgsl_mh_start(struct kgsl_device *device)
559{
560 struct kgsl_mh *mh = &device->mh;
561 /* force mmu off to for now*/
562 kgsl_regwrite(device, MH_MMU_CONFIG, 0);
563 kgsl_idle(device, KGSL_TIMEOUT_DEFAULT);
564
565 /* define physical memory range accessible by the core */
566 kgsl_regwrite(device, MH_MMU_MPU_BASE, mh->mpu_base);
567 kgsl_regwrite(device, MH_MMU_MPU_END,
568 mh->mpu_base + mh->mpu_range);
569 kgsl_regwrite(device, MH_ARBITER_CONFIG, mh->mharb);
570
571 if (mh->mh_intf_cfg1 != 0)
572 kgsl_regwrite(device, MH_CLNT_INTF_CTRL_CONFIG1,
573 mh->mh_intf_cfg1);
574
575 if (mh->mh_intf_cfg2 != 0)
576 kgsl_regwrite(device, MH_CLNT_INTF_CTRL_CONFIG2,
577 mh->mh_intf_cfg2);
578
579 /*
580 * Interrupts are enabled on a per-device level when
581 * kgsl_pwrctrl_irq() is called
582 */
583}
584
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600585static inline struct gen_pool *
586_get_pool(struct kgsl_pagetable *pagetable, unsigned int flags)
587{
588 if (pagetable->kgsl_pool &&
589 (KGSL_MEMFLAGS_GLOBAL & flags))
590 return pagetable->kgsl_pool;
591 return pagetable->pool;
592}
593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594int
595kgsl_mmu_map(struct kgsl_pagetable *pagetable,
596 struct kgsl_memdesc *memdesc,
597 unsigned int protflags)
598{
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600599 int ret;
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600600 struct gen_pool *pool;
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600601 int size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600603 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) {
Jordan Crouse40861a42012-02-06 10:18:23 -0700604 if (memdesc->sglen == 1) {
Shubhraprakash Das4d6af2b2012-04-20 00:35:03 -0600605 memdesc->gpuaddr = sg_dma_address(memdesc->sg);
606 if (!memdesc->gpuaddr)
607 memdesc->gpuaddr = sg_phys(memdesc->sg);
608 if (!memdesc->gpuaddr) {
609 KGSL_CORE_ERR("Unable to get a valid physical "
610 "address for memdesc\n");
611 return -EINVAL;
612 }
Jordan Crouse40861a42012-02-06 10:18:23 -0700613 return 0;
614 } else {
615 KGSL_CORE_ERR("Memory is not contigious "
616 "(sglen = %d)\n", memdesc->sglen);
617 return -EINVAL;
618 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600619 }
Jordan Crouse40861a42012-02-06 10:18:23 -0700620
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600621 size = kgsl_sg_size(memdesc->sg, memdesc->sglen);
622
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600623 /* Allocate from kgsl pool if it exists for global mappings */
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600624 pool = _get_pool(pagetable, memdesc->priv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600626 memdesc->gpuaddr = gen_pool_alloc(pool, size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 if (memdesc->gpuaddr == 0) {
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600628 KGSL_CORE_ERR("gen_pool_alloc(%d) failed from pool: %s\n",
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600629 size,
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600630 (pool == pagetable->kgsl_pool) ?
631 "kgsl_pool" : "general_pool");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 KGSL_CORE_ERR(" [%d] allocated=%d, entries=%d\n",
633 pagetable->name, pagetable->stats.mapped,
634 pagetable->stats.entries);
635 return -ENOMEM;
636 }
637
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600638 if (KGSL_MMU_TYPE_IOMMU != kgsl_mmu_get_mmutype())
639 spin_lock(&pagetable->lock);
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600640 ret = pagetable->pt_ops->mmu_map(pagetable->priv, memdesc, protflags,
641 &pagetable->tlb_flags);
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600642 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
643 spin_lock(&pagetable->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600645 if (ret)
646 goto err_free_gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647
648 /* Keep track of the statistics for the sysfs files */
649
650 KGSL_STATS_ADD(1, pagetable->stats.entries,
651 pagetable->stats.max_entries);
652
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600653 KGSL_STATS_ADD(size, pagetable->stats.mapped,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 pagetable->stats.max_mapped);
655
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 spin_unlock(&pagetable->lock);
657
658 return 0;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600659
660err_free_gpuaddr:
661 spin_unlock(&pagetable->lock);
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600662 gen_pool_free(pool, memdesc->gpuaddr, size);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600663 memdesc->gpuaddr = 0;
664 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665}
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600666EXPORT_SYMBOL(kgsl_mmu_map);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667
668int
669kgsl_mmu_unmap(struct kgsl_pagetable *pagetable,
670 struct kgsl_memdesc *memdesc)
671{
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600672 struct gen_pool *pool;
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600673 int size;
674
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600675 if (memdesc->size == 0 || memdesc->gpuaddr == 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 return 0;
677
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600678 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) {
679 memdesc->gpuaddr = 0;
680 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 }
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600682
683 size = kgsl_sg_size(memdesc->sg, memdesc->sglen);
684
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600685 if (KGSL_MMU_TYPE_IOMMU != kgsl_mmu_get_mmutype())
686 spin_lock(&pagetable->lock);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600687 pagetable->pt_ops->mmu_unmap(pagetable->priv, memdesc);
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600688 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
689 spin_lock(&pagetable->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 /* Remove the statistics */
691 pagetable->stats.entries--;
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600692 pagetable->stats.mapped -= size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693
694 spin_unlock(&pagetable->lock);
695
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600696 pool = _get_pool(pagetable, memdesc->priv);
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600697 gen_pool_free(pool, memdesc->gpuaddr, size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698
Jeremy Gebben7faf9ec2012-03-21 14:09:55 -0600699 /*
700 * Don't clear the gpuaddr on global mappings because they
701 * may be in use by other pagetables
702 */
703 if (!(memdesc->priv & KGSL_MEMFLAGS_GLOBAL))
704 memdesc->gpuaddr = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 return 0;
706}
707EXPORT_SYMBOL(kgsl_mmu_unmap);
708
709int kgsl_mmu_map_global(struct kgsl_pagetable *pagetable,
710 struct kgsl_memdesc *memdesc, unsigned int protflags)
711{
712 int result = -EINVAL;
713 unsigned int gpuaddr = 0;
714
715 if (memdesc == NULL) {
716 KGSL_CORE_ERR("invalid memdesc\n");
717 goto error;
718 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600719 /* Not all global mappings are needed for all MMU types */
720 if (!memdesc->size)
721 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722
723 gpuaddr = memdesc->gpuaddr;
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600724 memdesc->priv |= KGSL_MEMFLAGS_GLOBAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725
726 result = kgsl_mmu_map(pagetable, memdesc, protflags);
727 if (result)
728 goto error;
729
730 /*global mappings must have the same gpu address in all pagetables*/
731 if (gpuaddr && gpuaddr != memdesc->gpuaddr) {
732 KGSL_CORE_ERR("pt %p addr mismatch phys 0x%08x"
733 "gpu 0x%0x 0x%08x", pagetable, memdesc->physaddr,
734 gpuaddr, memdesc->gpuaddr);
735 goto error_unmap;
736 }
737 return result;
738error_unmap:
739 kgsl_mmu_unmap(pagetable, memdesc);
740error:
741 return result;
742}
743EXPORT_SYMBOL(kgsl_mmu_map_global);
744
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745int kgsl_mmu_close(struct kgsl_device *device)
746{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 struct kgsl_mmu *mmu = &device->mmu;
748
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600749 kgsl_sharedmem_free(&mmu->setstate_memory);
750 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600751 return 0;
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600752 else
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600753 return mmu->mmu_ops->mmu_close(mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754}
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600755EXPORT_SYMBOL(kgsl_mmu_close);
756
757int kgsl_mmu_pt_get_flags(struct kgsl_pagetable *pt,
758 enum kgsl_deviceid id)
759{
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600760 unsigned int result = 0;
761
762 if (pt == NULL)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600763 return 0;
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600764
765 spin_lock(&pt->lock);
Shubhraprakash Das97828ae2012-06-06 22:46:37 -0600766 if (pt->tlb_flags & (1<<id)) {
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600767 result = KGSL_MMUFLAGS_TLBFLUSH;
768 pt->tlb_flags &= ~(1<<id);
769 }
770 spin_unlock(&pt->lock);
771 return result;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600772}
773EXPORT_SYMBOL(kgsl_mmu_pt_get_flags);
774
775void kgsl_mmu_ptpool_destroy(void *ptpool)
776{
777 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type)
778 kgsl_gpummu_ptpool_destroy(ptpool);
779 ptpool = 0;
780}
781EXPORT_SYMBOL(kgsl_mmu_ptpool_destroy);
782
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600783void *kgsl_mmu_ptpool_init(int entries)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600784{
785 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type)
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600786 return kgsl_gpummu_ptpool_init(entries);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600787 else
788 return (void *)(-1);
789}
790EXPORT_SYMBOL(kgsl_mmu_ptpool_init);
791
792int kgsl_mmu_enabled(void)
793{
794 if (KGSL_MMU_TYPE_NONE != kgsl_mmu_type)
795 return 1;
796 else
797 return 0;
798}
799EXPORT_SYMBOL(kgsl_mmu_enabled);
800
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600801enum kgsl_mmutype kgsl_mmu_get_mmutype(void)
802{
803 return kgsl_mmu_type;
804}
805EXPORT_SYMBOL(kgsl_mmu_get_mmutype);
806
807void kgsl_mmu_set_mmutype(char *mmutype)
808{
Jordan Crouse817e0b92012-02-04 10:23:53 -0700809 /* Set the default MMU - GPU on <=8960 and nothing on >= 8064 */
810 kgsl_mmu_type =
811 cpu_is_apq8064() ? KGSL_MMU_TYPE_NONE : KGSL_MMU_TYPE_GPU;
812
813 /* Use the IOMMU if it is found */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700814 if (iommu_present(&platform_bus_type))
Jordan Crouse817e0b92012-02-04 10:23:53 -0700815 kgsl_mmu_type = KGSL_MMU_TYPE_IOMMU;
816
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600817 if (mmutype && !strncmp(mmutype, "gpummu", 6))
818 kgsl_mmu_type = KGSL_MMU_TYPE_GPU;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700819 if (iommu_present(&platform_bus_type) && mmutype &&
820 !strncmp(mmutype, "iommu", 5))
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600821 kgsl_mmu_type = KGSL_MMU_TYPE_IOMMU;
822 if (mmutype && !strncmp(mmutype, "nommu", 5))
823 kgsl_mmu_type = KGSL_MMU_TYPE_NONE;
824}
825EXPORT_SYMBOL(kgsl_mmu_set_mmutype);