1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* drivers/gpu/iommu/iommu.c *
* Copyright (C) 2011 Google, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/module.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/clk.h>
#include <linux/miscdevice.h>
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/debugfs.h>
#include <linux/platform_device.h>
#include <linux/genalloc.h>
#include <linux/sprd_iommu.h>
#include <linux/scatterlist.h>
#include <asm/io.h>
#include "sprd_iommu_sysfs.h"
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
static int sprd_iommu_probe(struct platform_device *pdev);
static int sprd_iommu_remove(struct platform_device *pdev);
static int sprd_iommu_suspend(struct platform_device *pdev, pm_message_t state);
static int sprd_iommu_resume(struct platform_device *pdev);
struct sprd_iommu_dev *iommu_devs[IOMMU_MAX] = {NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL};
static struct sprd_iommu_init_data sprd_iommu_gsp_data = {
.id = IOMMU_GSP,
.name = "sprd_iommu_gsp",
};
static struct sprd_iommu_init_data sprd_iommu_mm_data = {
.id = IOMMU_MM,
.name = "sprd_iommu_mm",
};
static struct sprd_iommu_init_data sprd_iommu_vsp_data = {
.id = IOMMU_VSP,
.name = "sprd_iommu_vsp",
};
static struct sprd_iommu_init_data sprd_iommu_dcam_data = {
.id = IOMMU_DCAM,
.name = "sprd_iommu_dcam",
};
static struct sprd_iommu_init_data sprd_iommu_dispc_data = {
.id = IOMMU_DISPC,
.name = "sprd_iommu_dispc",
};
static struct sprd_iommu_init_data sprd_iommu_gsp0_data = {
.id = IOMMU_GSP0,
.name = "sprd_iommu_gsp0",
};
static struct sprd_iommu_init_data sprd_iommu_gsp1_data = {
.id = IOMMU_GSP1,
.name = "sprd_iommu_gsp1",
};
static struct sprd_iommu_init_data sprd_iommu_vpp_data = {
.id = IOMMU_VPP,
.name = "sprd_iommu_vpp",
};
const struct of_device_id iommu_ids[] = {
{ .compatible = "sprd,sprd_iommu", },
{},
};
static struct platform_driver iommu_driver = {
.probe = sprd_iommu_probe,
.remove = sprd_iommu_remove,
#ifndef CONFIG_ARCH_SCX35L
.suspend = sprd_iommu_suspend,
.resume = sprd_iommu_resume,
#endif
.driver = {
.owner=THIS_MODULE,
.name="sprd_iommu",
.of_match_table = iommu_ids,
},
};
unsigned long sprd_iova_alloc(int iommu_id, unsigned long iova_length)
{
if (iommu_devs[iommu_id]) {
return iommu_devs[iommu_id]->ops->iova_alloc(
iommu_devs[iommu_id], iova_length);
} else {
pr_err("%s, id:%d iommu don't exist, error!",
__func__, iommu_id);
return 0;
}
}
void sprd_iova_free(int iommu_id, unsigned long iova,
unsigned long iova_length)
{
if (iommu_devs[iommu_id]) {
iommu_devs[iommu_id]->ops->iova_free(
iommu_devs[iommu_id], iova, iova_length);
} else {
pr_err("%s, id:%d iommu don't exist, error!",
__func__, iommu_id);
}
}
int sprd_iova_map(int iommu_id, unsigned long iova, unsigned long iova_length,
struct sg_table *table)
{
if (iommu_devs[iommu_id]) {
return iommu_devs[iommu_id]->ops->iova_map(
iommu_devs[iommu_id], iova, iova_length, table);
} else {
pr_err("%s, id:%d iommu don't exist, error!",
__func__, iommu_id);
return -1;
}
}
int sprd_iova_unmap(int iommu_id, unsigned long iova,
unsigned long iova_length)
{
if (iommu_devs[iommu_id]) {
return iommu_devs[iommu_id]->ops->iova_unmap(
iommu_devs[iommu_id], iova, iova_length);
} else {
pr_err("%s, id:%d iommu don't exist, error!",
__func__, iommu_id);
return -1;
}
}
void sprd_iommu_module_enable(uint32_t iommu_id)
{
if (iommu_devs[iommu_id]->light_sleep_en ||
IOMMU_GSP == iommu_id ||
IOMMU_DISPC == iommu_id ||
IOMMU_GSP0 == iommu_id ||
IOMMU_GSP1 == iommu_id) {
if (iommu_devs[iommu_id])
iommu_devs[iommu_id]->ops->enable(iommu_devs[iommu_id]);
else
pr_err("%s, id:%d iommu don't exist, error!",
__func__, iommu_id);
}
}
void sprd_iommu_module_disable(uint32_t iommu_id)
{
if (iommu_devs[iommu_id]->light_sleep_en ||
IOMMU_GSP == iommu_id ||
IOMMU_DISPC == iommu_id ||
IOMMU_GSP0 == iommu_id ||
IOMMU_GSP1 == iommu_id) {
if (iommu_devs[iommu_id])
iommu_devs[iommu_id]->ops->disable(iommu_devs[iommu_id]);
else
pr_err("%s, id:%d iommu don't exist, error!",
__func__, iommu_id);
}
}
static int sprd_iommu_suspend(struct platform_device *pdev, pm_message_t state)
{
int err = -1;
struct sprd_iommu_dev *iommu_dev = platform_get_drvdata(pdev);
pr_info("%s, id:%d, name:%s mmu_clock:0x%p mmu_mclock:0x%p suspend\n",
__func__, iommu_dev->init_data->id, iommu_dev->init_data->name,
iommu_dev->mmu_clock, iommu_dev->mmu_mclock);
err = iommu_dev->ops->backup(iommu_dev);
return err;
}
static int sprd_iommu_resume(struct platform_device *pdev)
{
int err = -1;
struct sprd_iommu_dev *iommu_dev=platform_get_drvdata(pdev);
pr_info("%s, id:%d, name:%s mmu_clock:0x%p mmu_mclock:0x%p resume\n",
__func__, iommu_dev->init_data->id, iommu_dev->init_data->name,
iommu_dev->mmu_clock, iommu_dev->mmu_mclock);
err = iommu_dev->ops->restore(iommu_dev);
return err;
}
static inline int sprd_iommu_get_description(struct device_node *np,
char* desc_name, char**desc)
{
return of_property_read_string(np, (const char*)desc_name,(const char**) desc);
}
static int sprd_iommu_get_resource(struct device_node *np,
struct sprd_iommu_init_data *pdata)
{
int err = 0;
uint32_t val;
struct resource res;
pr_info("%s,name:%s\n", __func__, pdata->name);
err = of_address_to_resource(np, 0, &res);
if (err < 0)
return err;
pdata->iova_base = res.start;
pdata->iova_size = resource_size(&res);
pr_info("%s,iova_base:0x%lx,iova_size:%zx\n", __func__,
pdata->iova_base,pdata->iova_size);
err = of_address_to_resource(np, 1, &res);
if (err < 0)
return err;
pr_info("%s,pgt_base phy:0x%x\n", __func__, res.start);
pdata->pgt_base = (unsigned long)ioremap_nocache(res.start,
resource_size(&res));
if (!pdata->pgt_base)
BUG();
pdata->pgt_size = resource_size(&res);
pr_info("%s,pgt_base:%lx,pgt_size:%zx\n", __func__, pdata->pgt_base,
pdata->pgt_size);
err = of_address_to_resource(np, 2, &res);
if (err < 0)
return err;
pr_info("%s,ctrl_reg phy:0x%x\n", __func__, res.start);
pdata->ctrl_reg = (unsigned long)ioremap_nocache(res.start,
resource_size(&res));
if (!pdata->ctrl_reg)
BUG();
pr_info("%s,ctrl_reg:0x%lx\n", __func__, pdata->ctrl_reg);
err = of_property_read_u32(np, "sprd,reserved-fault-page", &val);
if (err) {
unsigned long page = 0;
uint32_t i;
page = __get_free_page(GFP_KERNEL);
if (page) {
pdata->fault_page = virt_to_phys((void *)page);
} else {
pdata->fault_page = 0;
}
pr_info("%s, fault_page: 0x%lx\n", __func__, pdata->fault_page);
} else {
pr_info("%s, reserved fault page phy:0x%x\n", __func__, val);
pdata->fault_page = val;
}
err = of_property_read_u32(np, "sprd,reserved-rr-page", &val);
if (err) {
pr_info("%s, no reserved rr page addr\n", __func__);
pdata->re_route_page = 0;
} else {
pr_info("%s, reserved rr page phy:0x%x\n", __func__, val);
pdata->re_route_page = val;
}
err = of_property_read_u32(np, "sprd,iommu-rev", &val);
if (err) {
pr_info("%s, iommu-rev: r1\n", __func__);
pdata->iommu_rev = 1;
} else {
pr_info("%s, iommu-rev: %u\n", __func__, val);
pdata->iommu_rev = val;
}
return 0;
}
static int sprd_iommu_probe(struct platform_device *pdev)
{
char func_name[] = "func-name";
char func_desc[128];
char* pdesc=&(func_desc[0]);
struct device_node *np = pdev->dev.of_node;
int err = 0;
struct sprd_iommu_init_data *pdata;
struct sprd_iommu_dev *iommu_dev;
pr_info("%s, begin\n", __func__);
iommu_dev = kzalloc(sizeof(struct sprd_iommu_dev), GFP_KERNEL);
if (NULL == iommu_dev) {
pr_err("%s, fail to kzalloc\n", __func__);
return -ENOMEM;
}
if (-1 == sprd_iommu_get_description(np, func_name, &pdesc)) {
pr_err("%s, fail to get description\n", __func__);
err = -EINVAL;
goto errout;
}
pr_info("%s, function name is %s\n", __func__, pdesc);
if (0 == strncmp("sprd_iommu_gsp", pdesc, 14)) {
pdata = &sprd_iommu_gsp_data;
iommu_dev->ops = &iommu_gsp_ops;
} else if (0 == strncmp("sprd_iommu_mm", pdesc, 13)) {
pdata = &sprd_iommu_mm_data;
iommu_dev->ops = &iommu_mm_ops;
} else if (0 == strncmp("sprd_iommu_vsp", pdesc, 14)) {
pdata = &sprd_iommu_vsp_data;
iommu_dev->ops = &iommu_vsp_ops;
} else if (0 == strncmp("sprd_iommu_dcam", pdesc, 15)) {
pdata = &sprd_iommu_dcam_data;
iommu_dev->ops = &iommu_dcam_ops;
} else if (0 == strncmp("sprd_iommu_dispc", pdesc, 16)) {
pdata = &sprd_iommu_dispc_data;
iommu_dev->ops = &iommu_dispc_ops;
} else if (0 == strncmp("sprd_iommu_gsp0", pdesc, 15)) {
pdata = &sprd_iommu_gsp0_data;
iommu_dev->ops = &iommu_gsp0_ops;
} else if (0 == strncmp("sprd_iommu_gsp1", pdesc, 15)) {
pdata = &sprd_iommu_gsp1_data;
iommu_dev->ops = &iommu_gsp1_ops;
} else if (0 == strncmp("sprd_iommu_vpp", pdesc, 14)) {
pdata = &sprd_iommu_vpp_data;
iommu_dev->ops = &iommu_vpp_ops;
} else {
pr_err("%s, function name mismatch\n", __func__);
err = -EINVAL;
goto errout;
}
err = sprd_iommu_get_resource(np, pdata);
if (err) {
dev_err(&pdev->dev, "no reg of property specified\n");
pr_err("%s: failed to get resource!\n", pdesc);
goto errout;
}
iommu_devs[pdata->id] = iommu_dev;
iommu_dev->init_data = pdata;
mutex_init(&iommu_dev->mutex_pgt);
mutex_init(&iommu_dev->mutex_map);
/*If ddr frequency >= 500Mz, iommu need enable div2 frequency,
* because of limit of iommu iram frq.*/
iommu_dev->div2_frq = 500;
iommu_dev->light_sleep_en = false;
iommu_dev->misc_dev.minor = MISC_DYNAMIC_MINOR;
iommu_dev->misc_dev.name = pdata->name;
iommu_dev->misc_dev.fops = NULL;
iommu_dev->misc_dev.parent = NULL;
err = misc_register(&iommu_dev->misc_dev);
if (err) {
pr_err("iommu %s : failed to register misc device %d.\n",
pdata->name, err);
goto errout;
}
iommu_dev->misc_dev.this_device->of_node = pdev->dev.of_node;
err = iommu_dev->ops->init(iommu_dev, pdata);
if (err) {
pr_err("iommu %s : failed to init device %d.\n", pdata->name, err);
goto errout;
}
sprd_iommu_sysfs_register(iommu_dev, iommu_dev->init_data->name);
platform_set_drvdata(pdev, iommu_dev);
pr_info("%s, name:%s, dev->pgt:0x%lx, dev->pool:0x%p mmu_clock:0x%p\n",
__func__, pdata->name, iommu_dev->pgt,
iommu_dev->pool, iommu_dev->mmu_clock);
pr_info("%s, end\n", __func__);
return 0;
errout:
iommu_devs[pdata->id] = NULL;
kfree(iommu_dev);
return err;
}
static int sprd_iommu_remove(struct platform_device *pdev)
{
struct sprd_iommu_dev *iommu_dev=platform_get_drvdata(pdev);
misc_deregister(&iommu_dev->misc_dev);
sprd_iommu_sysfs_unregister(iommu_dev,iommu_dev->init_data->name);
iommu_dev->ops->exit(iommu_dev);
gen_pool_destroy(iommu_dev->pool);
kfree(iommu_dev);
return 0;
}
static int __init iommu_init(void)
{
int err=-1;
pr_info("%s, begin\n", __func__);
err=platform_driver_register(&iommu_driver);
if (err < 0)
pr_err("sprd_iommu register err: %d\n",err);
pr_info("%s, end\n", __func__);
return err;
}
static void __exit iommu_exit(void)
{
platform_driver_unregister(&iommu_driver);
}
module_init(iommu_init);
module_exit(iommu_exit);
MODULE_DESCRIPTION("SPRD IOMMU Driver");
MODULE_LICENSE("GPL");
|