summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/iovmm-gart.c
blob: e11b7d8b821bb19d86c3302266117af94a1c7bf2 (plain)
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
/*
 * arch/arm/mach-tegra/iovmm-gart.c
 *
 * Tegra I/O VMM implementation for GART devices in Tegra and Tegra 2 series
 * systems-on-a-chip.
 *
 * Copyright (c) 2010, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <asm/io.h>
#include <asm/cacheflush.h>
#include "mach/iovmm.h"
#include "nvrm_drf.h"

#if defined(CONFIG_ARCH_TEGRA_2x_SOC)
#include "ap20/armc.h"
#elif defined(CONFIG_ARCH_TEGRA_1x_SOC)
#include "ap15/armc.h"
#else
#error "Unknown Tegra chip family!"
#endif

#define VMM_NAME "iovmm-gart"
#define DRIVER_NAME "tegra_gart"

#define GART_PAGE_SHIFT (12)
#define GART_PAGE_MASK (~((1<<GART_PAGE_SHIFT)-1))

struct gart_device {
	void __iomem		*regs;
	u32			*savedata;
	u32			page_count; /* total remappable size */
	tegra_iovmm_addr_t	iovmm_base; /* offset to apply to vmm_area */
	spinlock_t		pte_lock;
	struct tegra_iovmm_device iovmm;
	struct tegra_iovmm_domain domain;
	bool			enable;
	bool			needs_barrier; /* emulator WAR */
};

static int gart_map(struct tegra_iovmm_device *, struct tegra_iovmm_area *);
static void gart_unmap(struct tegra_iovmm_device *,
	struct tegra_iovmm_area *, bool);
static void gart_map_pfn(struct tegra_iovmm_device *,
	struct tegra_iovmm_area *, tegra_iovmm_addr_t, unsigned long);
static struct tegra_iovmm_domain *gart_alloc_domain(
	struct tegra_iovmm_device *, struct tegra_iovmm_client *);

static int gart_probe(struct platform_device *);
static int gart_remove(struct platform_device *);
static int gart_suspend(struct platform_device *, pm_message_t);
static int gart_resume(struct platform_device *);


static struct tegra_iovmm_device_ops tegra_iovmm_gart_ops = {
	.map		= gart_map,
	.unmap		= gart_unmap,
	.map_pfn	= gart_map_pfn,
	.alloc_domain	= gart_alloc_domain,
};

static struct platform_driver tegra_iovmm_gart_drv = {
	.probe		= gart_probe,
	.remove		= gart_remove,
	.suspend	= gart_suspend,
	.resume		= gart_resume,
	.driver		= {
		.name	= DRIVER_NAME,
	},
};

#define gpfn_to_gart(_g, _gpfn) (((_g)->iovmm_base>>GART_PAGE_SHIFT) + (_gpfn))

static int gart_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct gart_device *gart = platform_get_drvdata(pdev);
	unsigned int i;

	if (!gart)
		return -ENODEV;

	if (!gart->enable)
		return 0;

	spin_lock(&gart->pte_lock);
	for (i=0; i<gart->page_count; i++) {
		u32 reg;
		reg = NV_DRF_NUM(MC, GART_ENTRY_ADDR,
			GART_ENTRY_ADDR_TABLE_ADDR, gpfn_to_gart(gart,i));
		writel(reg, gart->regs + MC_GART_ENTRY_ADDR_0);
		gart->savedata[i] = readl(gart->regs + MC_GART_ENTRY_DATA_0);
	}
	spin_unlock(&gart->pte_lock);
	return 0;
}

static int gart_resume(struct platform_device *pdev)
{
	struct gart_device *gart = platform_get_drvdata(pdev);
	unsigned int i;
	u32 reg;

	if (!gart || (gart->enable && !gart->savedata))
		return -ENODEV;

	if (!gart->enable)
		return 0;

	spin_lock(&gart->pte_lock);
	for (i=0; i<gart->page_count; i++) {
		reg = NV_DRF_NUM(MC, GART_ENTRY_ADDR,
			GART_ENTRY_ADDR_TABLE_ADDR, gpfn_to_gart(gart, i));
		writel(reg, gart->regs + MC_GART_ENTRY_ADDR_0);
		writel(gart->savedata[i], gart->regs + MC_GART_ENTRY_DATA_0);
	}
	reg = NV_DRF_DEF(MC, GART_CONFIG, GART_ENABLE, ENABLE);
	writel(reg, gart->regs + MC_GART_CONFIG_0);
	spin_unlock(&gart->pte_lock);

	return 0;
}

static int gart_remove(struct platform_device *pdev)
{
	struct gart_device *gart = platform_get_drvdata(pdev);

	if (!gart)
		return 0;

	if (gart->enable) {
		u32 reg;
		reg = NV_DRF_DEF(MC, GART_CONFIG, GART_ENABLE, DISABLE);
		writel(reg, gart->regs + MC_GART_CONFIG_0);
		gart->enable = 0;
	}
	platform_set_drvdata(pdev, NULL);
	tegra_iovmm_unregister(&gart->iovmm);
	if (gart->savedata)
		vfree(gart->savedata);
	if (gart->regs)
		iounmap(gart->regs);
	kfree(gart);
	return 0;
}

static int gart_probe(struct platform_device *pdev)
{
	struct gart_device *gart = NULL;
	struct resource *res, *res_remap;
	void __iomem *gart_regs = NULL;
	u32 reg;
	unsigned int i;
	int e;

	if (!pdev) {
		pr_err(DRIVER_NAME ": platform_device required\n");
		return -ENODEV;
	}

	if (PAGE_SHIFT != GART_PAGE_SHIFT) {
		pr_err(DRIVER_NAME ": GART and CPU page size must match\n");
		return -ENXIO;
	}

	/* the GART memory aperture is required */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	res_remap = platform_get_resource(pdev, IORESOURCE_MEM, 1);

	if (!res || !res_remap) {
		pr_err(DRIVER_NAME ": GART memory aperture expected\n");
		return -ENXIO;
	}
	gart = kzalloc(sizeof(*gart), GFP_KERNEL);
	if (!gart) {
		pr_err(DRIVER_NAME ": failed to allocate tegra_iovmm_device\n");
		e = -ENOMEM;
		goto fail;
	}

	gart_regs = ioremap_wc(res->start, res->end - res->start + 1);
	if (!gart_regs) {
		pr_err(DRIVER_NAME ": failed to remap GART registers\n");
		e = -ENXIO;
		goto fail;
	}

	gart->iovmm.name = VMM_NAME;
	gart->iovmm.ops = &tegra_iovmm_gart_ops;
	gart->iovmm.pgsize_bits = GART_PAGE_SHIFT;
	spin_lock_init(&gart->pte_lock);

	platform_set_drvdata(pdev, gart);

	e = tegra_iovmm_register(&gart->iovmm);
	if (e) goto fail;

	e = tegra_iovmm_domain_init(&gart->domain, &gart->iovmm,
		(tegra_iovmm_addr_t)res_remap->start,
		(tegra_iovmm_addr_t)res_remap->end+1);
	if (e) goto fail;

	gart->regs = gart_regs;
	gart->iovmm_base = (tegra_iovmm_addr_t)res_remap->start;
	gart->page_count = res_remap->end - res_remap->start + 1;
	gart->page_count >>= GART_PAGE_SHIFT;

	gart->savedata = vmalloc(sizeof(u32)*gart->page_count);
	if (!gart->savedata) {
		pr_err(DRIVER_NAME ": failed to allocate context save area\n");
		e = -ENOMEM;
		goto fail;
	}

	spin_lock(&gart->pte_lock);
	for (i=0; i<gart->page_count; i++) {
		reg = NV_DRF_NUM(MC, GART_ENTRY_ADDR,
			GART_ENTRY_ADDR_TABLE_ADDR, gpfn_to_gart(gart, i));
		writel(reg, gart->regs + MC_GART_ENTRY_ADDR_0);
		writel(0, gart->regs + MC_GART_ENTRY_DATA_0);
	}
	reg = NV_DRF_DEF(MC, GART_CONFIG, GART_ENABLE, ENABLE);
	writel(reg, gart->regs + MC_GART_CONFIG_0);
	spin_unlock(&gart->pte_lock);
	gart->enable = 1;
	return 0;

fail:
	if (gart_regs)
		iounmap(gart_regs);
	if (gart && gart->savedata)
		vfree(gart->savedata);
	if (gart)
		kfree(gart);
	return e;
}

static int __devinit gart_init(void)
{
	return platform_driver_register(&tegra_iovmm_gart_drv);
}

static void __exit gart_exit(void)
{
	return platform_driver_unregister(&tegra_iovmm_gart_drv);
}

#define GART_PTE(_valid, _pfn) \
	(NV_DRF_NUM(MC,GART_ENTRY_DATA,GART_ENTRY_DATA_PHYS_ADDR_VALID,(_valid))|\
	 NV_DRF_NUM(MC,GART_ENTRY_DATA,GART_ENTRY_DATA_PHYS_ADDR,\
		((_pfn)<<PAGE_SHIFT)>>GART_PAGE_SHIFT))


static int gart_map(struct tegra_iovmm_device *dev,
	struct tegra_iovmm_area *iovma)
{
	struct gart_device *gart = container_of(dev, struct gart_device, iovmm);
	u32 gart_page, count;
	unsigned int i;

	gart_page = iovma->iovm_start >> GART_PAGE_SHIFT;
	count = iovma->iovm_length >> GART_PAGE_SHIFT;

	for (i=0; i<count; i++) {
		unsigned long pfn;
		u32 reg;

		pfn = iovma->ops->lock_makeresident(iovma, i<<PAGE_SHIFT);
		if (!pfn_valid(pfn))
			goto fail;

		spin_lock(&gart->pte_lock);
		/* gpfn translation not needed, since iovm_start already
		 * includes the offset */
		reg = NV_DRF_NUM(MC, GART_ENTRY_ADDR,
			GART_ENTRY_ADDR_TABLE_ADDR, gart_page + i);
		writel(reg, gart->regs + MC_GART_ENTRY_ADDR_0);
		reg = GART_PTE(1,pfn);
		writel(reg, gart->regs + MC_GART_ENTRY_DATA_0);
		if (unlikely(gart->needs_barrier))
			reg = readl(gart->regs + MC_GART_ENTRY_DATA_0);
		spin_unlock(&gart->pte_lock);
	}

	dmb();
	outer_sync();
	return 0;

fail:
	while (i--) {
		u32 reg;
		iovma->ops->release(iovma, i<<PAGE_SHIFT);
		spin_lock(&gart->pte_lock);
		reg = NV_DRF_NUM(MC, GART_ENTRY_ADDR,
			GART_ENTRY_ADDR_TABLE_ADDR, gart_page + i);
		writel(reg, gart->regs + MC_GART_ENTRY_ADDR_0);
		writel(0, gart->regs + MC_GART_ENTRY_DATA_0);
		spin_unlock(&gart->pte_lock);
	}

	return -ENOMEM;
}

static void gart_unmap(struct tegra_iovmm_device *dev,
	struct tegra_iovmm_area *iovma, bool decommit)
{
	struct gart_device *gart = container_of(dev, struct gart_device, iovmm);
	u32 gart_page;
	unsigned int i;

	spin_lock(&gart->pte_lock);
	for (i=0, gart_page=iovma->iovm_start;
	    gart_page<iovma->iovm_start + iovma->iovm_length;
	    gart_page+=(1<<GART_PAGE_SHIFT), i++) {
		if (iovma->ops && iovma->ops->release)
			iovma->ops->release(iovma, i<<PAGE_SHIFT);

		writel(gart_page, gart->regs + MC_GART_ENTRY_ADDR_0);
		writel(0, gart->regs + MC_GART_ENTRY_DATA_0);
	}
	spin_unlock(&gart->pte_lock);
	dmb();
	outer_sync();
}

static void gart_map_pfn(struct tegra_iovmm_device *dev,
	struct tegra_iovmm_area *iovma, tegra_iovmm_addr_t offs,
	unsigned long pfn)
{
	struct gart_device *gart = container_of(dev, struct gart_device, iovmm);

	BUG_ON((pfn<<PAGE_SHIFT)>=0x40000000ul);
	spin_lock(&gart->pte_lock);
	writel(offs, gart->regs + MC_GART_ENTRY_ADDR_0);
	writel(GART_PTE(1,pfn), gart->regs + MC_GART_ENTRY_DATA_0);
	(void)readl(gart->regs + MC_GART_ENTRY_DATA_0);
	spin_unlock(&gart->pte_lock);
	dmb();
	outer_sync();
}

static struct tegra_iovmm_domain *gart_alloc_domain(
	struct tegra_iovmm_device *dev, struct tegra_iovmm_client *client)
{
	struct gart_device *gart = container_of(dev, struct gart_device, iovmm);
	return &gart->domain;
}

module_init(gart_init);
module_exit(gart_exit);