blob: 3e729196ba0f2956c82595ef7ca2feec2ffd95ba [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 Dasb2abc452012-06-08 16:33:03 -0600546void kgsl_setstate(struct kgsl_mmu *mmu, unsigned int context_id,
547 uint32_t flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548{
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600549 struct kgsl_device *device = mmu->device;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600550 if (KGSL_MMU_TYPE_NONE == kgsl_mmu_type)
551 return;
Shubhraprakash Dasc6e21012012-05-11 17:24:51 -0600552 else if (device->ftbl->setstate)
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600553 device->ftbl->setstate(device, context_id, flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600554 else if (mmu->mmu_ops->mmu_device_setstate)
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600555 mmu->mmu_ops->mmu_device_setstate(mmu, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556}
557EXPORT_SYMBOL(kgsl_setstate);
558
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600559void kgsl_mh_start(struct kgsl_device *device)
560{
561 struct kgsl_mh *mh = &device->mh;
562 /* force mmu off to for now*/
563 kgsl_regwrite(device, MH_MMU_CONFIG, 0);
564 kgsl_idle(device, KGSL_TIMEOUT_DEFAULT);
565
566 /* define physical memory range accessible by the core */
567 kgsl_regwrite(device, MH_MMU_MPU_BASE, mh->mpu_base);
568 kgsl_regwrite(device, MH_MMU_MPU_END,
569 mh->mpu_base + mh->mpu_range);
570 kgsl_regwrite(device, MH_ARBITER_CONFIG, mh->mharb);
571
572 if (mh->mh_intf_cfg1 != 0)
573 kgsl_regwrite(device, MH_CLNT_INTF_CTRL_CONFIG1,
574 mh->mh_intf_cfg1);
575
576 if (mh->mh_intf_cfg2 != 0)
577 kgsl_regwrite(device, MH_CLNT_INTF_CTRL_CONFIG2,
578 mh->mh_intf_cfg2);
579
580 /*
581 * Interrupts are enabled on a per-device level when
582 * kgsl_pwrctrl_irq() is called
583 */
584}
585
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600586static inline struct gen_pool *
587_get_pool(struct kgsl_pagetable *pagetable, unsigned int flags)
588{
589 if (pagetable->kgsl_pool &&
590 (KGSL_MEMFLAGS_GLOBAL & flags))
591 return pagetable->kgsl_pool;
592 return pagetable->pool;
593}
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595int
596kgsl_mmu_map(struct kgsl_pagetable *pagetable,
597 struct kgsl_memdesc *memdesc,
598 unsigned int protflags)
599{
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600600 int ret;
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600601 struct gen_pool *pool;
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600602 int size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600604 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) {
Jordan Crouse40861a42012-02-06 10:18:23 -0700605 if (memdesc->sglen == 1) {
Shubhraprakash Das4d6af2b2012-04-20 00:35:03 -0600606 memdesc->gpuaddr = sg_dma_address(memdesc->sg);
607 if (!memdesc->gpuaddr)
608 memdesc->gpuaddr = sg_phys(memdesc->sg);
609 if (!memdesc->gpuaddr) {
610 KGSL_CORE_ERR("Unable to get a valid physical "
611 "address for memdesc\n");
612 return -EINVAL;
613 }
Jordan Crouse40861a42012-02-06 10:18:23 -0700614 return 0;
615 } else {
616 KGSL_CORE_ERR("Memory is not contigious "
617 "(sglen = %d)\n", memdesc->sglen);
618 return -EINVAL;
619 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600620 }
Jordan Crouse40861a42012-02-06 10:18:23 -0700621
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600622 size = kgsl_sg_size(memdesc->sg, memdesc->sglen);
623
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600624 /* Allocate from kgsl pool if it exists for global mappings */
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600625 pool = _get_pool(pagetable, memdesc->priv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600627 memdesc->gpuaddr = gen_pool_alloc(pool, size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 if (memdesc->gpuaddr == 0) {
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600629 KGSL_CORE_ERR("gen_pool_alloc(%d) failed from pool: %s\n",
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600630 size,
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600631 (pool == pagetable->kgsl_pool) ?
632 "kgsl_pool" : "general_pool");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 KGSL_CORE_ERR(" [%d] allocated=%d, entries=%d\n",
634 pagetable->name, pagetable->stats.mapped,
635 pagetable->stats.entries);
636 return -ENOMEM;
637 }
638
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600639 if (KGSL_MMU_TYPE_IOMMU != kgsl_mmu_get_mmutype())
640 spin_lock(&pagetable->lock);
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600641 ret = pagetable->pt_ops->mmu_map(pagetable->priv, memdesc, protflags,
642 &pagetable->tlb_flags);
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600643 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
644 spin_lock(&pagetable->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600646 if (ret)
647 goto err_free_gpuaddr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648
649 /* Keep track of the statistics for the sysfs files */
650
651 KGSL_STATS_ADD(1, pagetable->stats.entries,
652 pagetable->stats.max_entries);
653
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600654 KGSL_STATS_ADD(size, pagetable->stats.mapped,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 pagetable->stats.max_mapped);
656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 spin_unlock(&pagetable->lock);
658
659 return 0;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600660
661err_free_gpuaddr:
662 spin_unlock(&pagetable->lock);
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600663 gen_pool_free(pool, memdesc->gpuaddr, size);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600664 memdesc->gpuaddr = 0;
665 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666}
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600667EXPORT_SYMBOL(kgsl_mmu_map);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668
669int
670kgsl_mmu_unmap(struct kgsl_pagetable *pagetable,
671 struct kgsl_memdesc *memdesc)
672{
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600673 struct gen_pool *pool;
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600674 int size;
675
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600676 if (memdesc->size == 0 || memdesc->gpuaddr == 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 return 0;
678
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600679 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE) {
680 memdesc->gpuaddr = 0;
681 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 }
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600683
684 size = kgsl_sg_size(memdesc->sg, memdesc->sglen);
685
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600686 if (KGSL_MMU_TYPE_IOMMU != kgsl_mmu_get_mmutype())
687 spin_lock(&pagetable->lock);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600688 pagetable->pt_ops->mmu_unmap(pagetable->priv, memdesc);
Shubhraprakash Dasbadaeda2012-03-21 00:31:39 -0600689 if (KGSL_MMU_TYPE_IOMMU == kgsl_mmu_get_mmutype())
690 spin_lock(&pagetable->lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691 /* Remove the statistics */
692 pagetable->stats.entries--;
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600693 pagetable->stats.mapped -= size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694
695 spin_unlock(&pagetable->lock);
696
Jeremy Gebben1b9b1f142012-05-16 10:43:28 -0600697 pool = _get_pool(pagetable, memdesc->priv);
Jordan Crouse3c86ca82012-05-21 08:41:52 -0600698 gen_pool_free(pool, memdesc->gpuaddr, size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699
Jeremy Gebben7faf9ec2012-03-21 14:09:55 -0600700 /*
701 * Don't clear the gpuaddr on global mappings because they
702 * may be in use by other pagetables
703 */
704 if (!(memdesc->priv & KGSL_MEMFLAGS_GLOBAL))
705 memdesc->gpuaddr = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 return 0;
707}
708EXPORT_SYMBOL(kgsl_mmu_unmap);
709
710int kgsl_mmu_map_global(struct kgsl_pagetable *pagetable,
711 struct kgsl_memdesc *memdesc, unsigned int protflags)
712{
713 int result = -EINVAL;
714 unsigned int gpuaddr = 0;
715
716 if (memdesc == NULL) {
717 KGSL_CORE_ERR("invalid memdesc\n");
718 goto error;
719 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600720 /* Not all global mappings are needed for all MMU types */
721 if (!memdesc->size)
722 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723
724 gpuaddr = memdesc->gpuaddr;
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600725 memdesc->priv |= KGSL_MEMFLAGS_GLOBAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726
727 result = kgsl_mmu_map(pagetable, memdesc, protflags);
728 if (result)
729 goto error;
730
731 /*global mappings must have the same gpu address in all pagetables*/
732 if (gpuaddr && gpuaddr != memdesc->gpuaddr) {
733 KGSL_CORE_ERR("pt %p addr mismatch phys 0x%08x"
734 "gpu 0x%0x 0x%08x", pagetable, memdesc->physaddr,
735 gpuaddr, memdesc->gpuaddr);
736 goto error_unmap;
737 }
738 return result;
739error_unmap:
740 kgsl_mmu_unmap(pagetable, memdesc);
741error:
742 return result;
743}
744EXPORT_SYMBOL(kgsl_mmu_map_global);
745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746int kgsl_mmu_close(struct kgsl_device *device)
747{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 struct kgsl_mmu *mmu = &device->mmu;
749
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600750 kgsl_sharedmem_free(&mmu->setstate_memory);
751 if (kgsl_mmu_type == KGSL_MMU_TYPE_NONE)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600752 return 0;
Shubhraprakash Das0ff034f2012-05-02 15:51:07 -0600753 else
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600754 return mmu->mmu_ops->mmu_close(mmu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755}
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600756EXPORT_SYMBOL(kgsl_mmu_close);
757
758int kgsl_mmu_pt_get_flags(struct kgsl_pagetable *pt,
759 enum kgsl_deviceid id)
760{
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600761 unsigned int result = 0;
762
763 if (pt == NULL)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600764 return 0;
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600765
766 spin_lock(&pt->lock);
Shubhraprakash Das97828ae2012-06-06 22:46:37 -0600767 if (pt->tlb_flags & (1<<id)) {
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600768 result = KGSL_MMUFLAGS_TLBFLUSH;
769 pt->tlb_flags &= ~(1<<id);
770 }
771 spin_unlock(&pt->lock);
772 return result;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600773}
774EXPORT_SYMBOL(kgsl_mmu_pt_get_flags);
775
776void kgsl_mmu_ptpool_destroy(void *ptpool)
777{
778 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type)
779 kgsl_gpummu_ptpool_destroy(ptpool);
780 ptpool = 0;
781}
782EXPORT_SYMBOL(kgsl_mmu_ptpool_destroy);
783
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600784void *kgsl_mmu_ptpool_init(int entries)
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600785{
786 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_type)
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600787 return kgsl_gpummu_ptpool_init(entries);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600788 else
789 return (void *)(-1);
790}
791EXPORT_SYMBOL(kgsl_mmu_ptpool_init);
792
793int kgsl_mmu_enabled(void)
794{
795 if (KGSL_MMU_TYPE_NONE != kgsl_mmu_type)
796 return 1;
797 else
798 return 0;
799}
800EXPORT_SYMBOL(kgsl_mmu_enabled);
801
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600802enum kgsl_mmutype kgsl_mmu_get_mmutype(void)
803{
804 return kgsl_mmu_type;
805}
806EXPORT_SYMBOL(kgsl_mmu_get_mmutype);
807
808void kgsl_mmu_set_mmutype(char *mmutype)
809{
Jordan Crouse817e0b92012-02-04 10:23:53 -0700810 /* Set the default MMU - GPU on <=8960 and nothing on >= 8064 */
811 kgsl_mmu_type =
812 cpu_is_apq8064() ? KGSL_MMU_TYPE_NONE : KGSL_MMU_TYPE_GPU;
813
814 /* Use the IOMMU if it is found */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700815 if (iommu_present(&platform_bus_type))
Jordan Crouse817e0b92012-02-04 10:23:53 -0700816 kgsl_mmu_type = KGSL_MMU_TYPE_IOMMU;
817
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600818 if (mmutype && !strncmp(mmutype, "gpummu", 6))
819 kgsl_mmu_type = KGSL_MMU_TYPE_GPU;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700820 if (iommu_present(&platform_bus_type) && mmutype &&
821 !strncmp(mmutype, "iommu", 5))
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600822 kgsl_mmu_type = KGSL_MMU_TYPE_IOMMU;
823 if (mmutype && !strncmp(mmutype, "nommu", 5))
824 kgsl_mmu_type = KGSL_MMU_TYPE_NONE;
825}
826EXPORT_SYMBOL(kgsl_mmu_set_mmutype);