blob: b1a060ce8116687fa7b292fc2d04d8566618d74a [file] [log] [blame]
Thierry Reding89184652014-04-16 09:24:44 +02001/*
2 * Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
10#include <linux/interrupt.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/slab.h>
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +010016#include <linux/sort.h>
17
18#include <soc/tegra/fuse.h>
Thierry Reding89184652014-04-16 09:24:44 +020019
20#include "mc.h"
21
22#define MC_INTSTATUS 0x000
Thierry Reding89184652014-04-16 09:24:44 +020023
24#define MC_INTMASK 0x004
25
26#define MC_ERR_STATUS 0x08
27#define MC_ERR_STATUS_TYPE_SHIFT 28
28#define MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE (6 << MC_ERR_STATUS_TYPE_SHIFT)
29#define MC_ERR_STATUS_TYPE_MASK (0x7 << MC_ERR_STATUS_TYPE_SHIFT)
30#define MC_ERR_STATUS_READABLE (1 << 27)
31#define MC_ERR_STATUS_WRITABLE (1 << 26)
32#define MC_ERR_STATUS_NONSECURE (1 << 25)
33#define MC_ERR_STATUS_ADR_HI_SHIFT 20
34#define MC_ERR_STATUS_ADR_HI_MASK 0x3
35#define MC_ERR_STATUS_SECURITY (1 << 17)
36#define MC_ERR_STATUS_RW (1 << 16)
Thierry Reding89184652014-04-16 09:24:44 +020037
38#define MC_ERR_ADR 0x0c
39
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +030040#define MC_DECERR_EMEM_OTHERS_STATUS 0x58
41#define MC_SECURITY_VIOLATION_STATUS 0x74
42
Thierry Reding89184652014-04-16 09:24:44 +020043#define MC_EMEM_ARB_CFG 0x90
44#define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(x) (((x) & 0x1ff) << 0)
45#define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK 0x1ff
46#define MC_EMEM_ARB_MISC0 0xd8
47
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +010048#define MC_EMEM_ADR_CFG 0x54
49#define MC_EMEM_ADR_CFG_EMEM_NUMDEV BIT(0)
50
Thierry Reding89184652014-04-16 09:24:44 +020051static const struct of_device_id tegra_mc_of_match[] = {
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +030052#ifdef CONFIG_ARCH_TEGRA_2x_SOC
53 { .compatible = "nvidia,tegra20-mc", .data = &tegra20_mc_soc },
54#endif
Thierry Reding89184652014-04-16 09:24:44 +020055#ifdef CONFIG_ARCH_TEGRA_3x_SOC
56 { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
57#endif
58#ifdef CONFIG_ARCH_TEGRA_114_SOC
59 { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
60#endif
61#ifdef CONFIG_ARCH_TEGRA_124_SOC
62 { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
63#endif
Thierry Reding242b1d72014-11-07 16:10:41 +010064#ifdef CONFIG_ARCH_TEGRA_132_SOC
65 { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
66#endif
Thierry Reding588c43a2015-03-23 10:45:12 +010067#ifdef CONFIG_ARCH_TEGRA_210_SOC
68 { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
69#endif
Thierry Reding89184652014-04-16 09:24:44 +020070 { }
71};
72MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
73
74static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
75{
76 unsigned long long tick;
77 unsigned int i;
78 u32 value;
79
80 /* compute the number of MC clock cycles per tick */
81 tick = mc->tick * clk_get_rate(mc->clk);
82 do_div(tick, NSEC_PER_SEC);
83
84 value = readl(mc->regs + MC_EMEM_ARB_CFG);
85 value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
86 value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
87 writel(value, mc->regs + MC_EMEM_ARB_CFG);
88
89 /* write latency allowance defaults */
90 for (i = 0; i < mc->soc->num_clients; i++) {
91 const struct tegra_mc_la *la = &mc->soc->clients[i].la;
92 u32 value;
93
94 value = readl(mc->regs + la->reg);
95 value &= ~(la->mask << la->shift);
96 value |= (la->def & la->mask) << la->shift;
97 writel(value, mc->regs + la->reg);
98 }
99
100 return 0;
101}
102
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100103void tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
104{
105 unsigned int i;
106 struct tegra_mc_timing *timing = NULL;
107
108 for (i = 0; i < mc->num_timings; i++) {
109 if (mc->timings[i].rate == rate) {
110 timing = &mc->timings[i];
111 break;
112 }
113 }
114
115 if (!timing) {
116 dev_err(mc->dev, "no memory timing registered for rate %lu\n",
117 rate);
118 return;
119 }
120
121 for (i = 0; i < mc->soc->num_emem_regs; ++i)
122 mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
123}
124
125unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
126{
127 u8 dram_count;
128
129 dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
130 dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
131 dram_count++;
132
133 return dram_count;
134}
135
136static int load_one_timing(struct tegra_mc *mc,
137 struct tegra_mc_timing *timing,
138 struct device_node *node)
139{
140 int err;
141 u32 tmp;
142
143 err = of_property_read_u32(node, "clock-frequency", &tmp);
144 if (err) {
145 dev_err(mc->dev,
146 "timing %s: failed to read rate\n", node->name);
147 return err;
148 }
149
150 timing->rate = tmp;
151 timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
152 sizeof(u32), GFP_KERNEL);
153 if (!timing->emem_data)
154 return -ENOMEM;
155
156 err = of_property_read_u32_array(node, "nvidia,emem-configuration",
157 timing->emem_data,
158 mc->soc->num_emem_regs);
159 if (err) {
160 dev_err(mc->dev,
161 "timing %s: failed to read EMEM configuration\n",
162 node->name);
163 return err;
164 }
165
166 return 0;
167}
168
169static int load_timings(struct tegra_mc *mc, struct device_node *node)
170{
171 struct device_node *child;
172 struct tegra_mc_timing *timing;
173 int child_count = of_get_child_count(node);
174 int i = 0, err;
175
176 mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
177 GFP_KERNEL);
178 if (!mc->timings)
179 return -ENOMEM;
180
181 mc->num_timings = child_count;
182
183 for_each_child_of_node(node, child) {
184 timing = &mc->timings[i++];
185
186 err = load_one_timing(mc, timing, child);
Amitoj Kaur Chawla55bb1d82016-01-25 22:53:07 +0530187 if (err) {
188 of_node_put(child);
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100189 return err;
Amitoj Kaur Chawla55bb1d82016-01-25 22:53:07 +0530190 }
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100191 }
192
193 return 0;
194}
195
196static int tegra_mc_setup_timings(struct tegra_mc *mc)
197{
198 struct device_node *node;
199 u32 ram_code, node_ram_code;
200 int err;
201
202 ram_code = tegra_read_ram_code();
203
204 mc->num_timings = 0;
205
206 for_each_child_of_node(mc->dev->of_node, node) {
207 err = of_property_read_u32(node, "nvidia,ram-code",
208 &node_ram_code);
Julia Lawalld1122e42015-10-09 19:47:40 +0200209 if (err || (node_ram_code != ram_code))
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100210 continue;
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100211
212 err = load_timings(mc, node);
Amitoj Kaur Chawla55bb1d82016-01-25 22:53:07 +0530213 of_node_put(node);
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100214 if (err)
215 return err;
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100216 break;
217 }
218
219 if (mc->num_timings == 0)
220 dev_warn(mc->dev,
221 "no memory timings for RAM code %u registered\n",
222 ram_code);
223
224 return 0;
225}
226
Thierry Reding89184652014-04-16 09:24:44 +0200227static const char *const status_names[32] = {
228 [ 1] = "External interrupt",
229 [ 6] = "EMEM address decode error",
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +0300230 [ 7] = "GART page fault",
Thierry Reding89184652014-04-16 09:24:44 +0200231 [ 8] = "Security violation",
232 [ 9] = "EMEM arbitration error",
233 [10] = "Page fault",
234 [11] = "Invalid APB ASID update",
235 [12] = "VPR violation",
236 [13] = "Secure carveout violation",
237 [16] = "MTS carveout violation",
238};
239
240static const char *const error_names[8] = {
241 [2] = "EMEM decode error",
242 [3] = "TrustZone violation",
243 [4] = "Carveout violation",
244 [6] = "SMMU translation error",
245};
246
247static irqreturn_t tegra_mc_irq(int irq, void *data)
248{
249 struct tegra_mc *mc = data;
Dmitry Osipenko1c74d5c2018-04-09 22:28:29 +0300250 unsigned long status;
Thierry Reding89184652014-04-16 09:24:44 +0200251 unsigned int bit;
252
253 /* mask all interrupts to avoid flooding */
Dmitry Osipenko1c74d5c2018-04-09 22:28:29 +0300254 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
Dmitry Osipenkobf3fbdf2018-04-09 22:28:27 +0300255 if (!status)
256 return IRQ_NONE;
Thierry Reding89184652014-04-16 09:24:44 +0200257
258 for_each_set_bit(bit, &status, 32) {
259 const char *error = status_names[bit] ?: "unknown";
260 const char *client = "unknown", *desc;
261 const char *direction, *secure;
262 phys_addr_t addr = 0;
263 unsigned int i;
264 char perm[7];
265 u8 id, type;
266 u32 value;
267
268 value = mc_readl(mc, MC_ERR_STATUS);
269
270#ifdef CONFIG_PHYS_ADDR_T_64BIT
271 if (mc->soc->num_address_bits > 32) {
272 addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
273 MC_ERR_STATUS_ADR_HI_MASK);
274 addr <<= 32;
275 }
276#endif
277
278 if (value & MC_ERR_STATUS_RW)
279 direction = "write";
280 else
281 direction = "read";
282
283 if (value & MC_ERR_STATUS_SECURITY)
284 secure = "secure ";
285 else
286 secure = "";
287
Paul Walmsley3c01cf32015-06-04 19:33:48 +0000288 id = value & mc->soc->client_id_mask;
Thierry Reding89184652014-04-16 09:24:44 +0200289
290 for (i = 0; i < mc->soc->num_clients; i++) {
291 if (mc->soc->clients[i].id == id) {
292 client = mc->soc->clients[i].name;
293 break;
294 }
295 }
296
297 type = (value & MC_ERR_STATUS_TYPE_MASK) >>
298 MC_ERR_STATUS_TYPE_SHIFT;
299 desc = error_names[type];
300
301 switch (value & MC_ERR_STATUS_TYPE_MASK) {
302 case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
303 perm[0] = ' ';
304 perm[1] = '[';
305
306 if (value & MC_ERR_STATUS_READABLE)
307 perm[2] = 'R';
308 else
309 perm[2] = '-';
310
311 if (value & MC_ERR_STATUS_WRITABLE)
312 perm[3] = 'W';
313 else
314 perm[3] = '-';
315
316 if (value & MC_ERR_STATUS_NONSECURE)
317 perm[4] = '-';
318 else
319 perm[4] = 'S';
320
321 perm[5] = ']';
322 perm[6] = '\0';
323 break;
324
325 default:
326 perm[0] = '\0';
327 break;
328 }
329
330 value = mc_readl(mc, MC_ERR_ADR);
331 addr |= value;
332
333 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
334 client, secure, direction, &addr, error,
335 desc, perm);
336 }
337
338 /* clear interrupts */
339 mc_writel(mc, status, MC_INTSTATUS);
340
341 return IRQ_HANDLED;
342}
343
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +0300344static __maybe_unused irqreturn_t tegra20_mc_irq(int irq, void *data)
345{
346 struct tegra_mc *mc = data;
347 unsigned long status;
348 unsigned int bit;
349
350 /* mask all interrupts to avoid flooding */
351 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
352 if (!status)
353 return IRQ_NONE;
354
355 for_each_set_bit(bit, &status, 32) {
356 const char *direction = "read", *secure = "";
357 const char *error = status_names[bit];
358 const char *client, *desc;
359 phys_addr_t addr;
360 u32 value, reg;
361 u8 id, type;
362
363 switch (BIT(bit)) {
364 case MC_INT_DECERR_EMEM:
365 reg = MC_DECERR_EMEM_OTHERS_STATUS;
366 value = mc_readl(mc, reg);
367
368 id = value & mc->soc->client_id_mask;
369 desc = error_names[2];
370
371 if (value & BIT(31))
372 direction = "write";
373 break;
374
375 case MC_INT_INVALID_GART_PAGE:
376 dev_err_ratelimited(mc->dev, "%s\n", error);
377 continue;
378
379 case MC_INT_SECURITY_VIOLATION:
380 reg = MC_SECURITY_VIOLATION_STATUS;
381 value = mc_readl(mc, reg);
382
383 id = value & mc->soc->client_id_mask;
384 type = (value & BIT(30)) ? 4 : 3;
385 desc = error_names[type];
386 secure = "secure ";
387
388 if (value & BIT(31))
389 direction = "write";
390 break;
391
392 default:
393 continue;
394 }
395
396 client = mc->soc->clients[id].name;
397 addr = mc_readl(mc, reg + sizeof(u32));
398
399 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s)\n",
400 client, secure, direction, &addr, error,
401 desc);
402 }
403
404 /* clear interrupts */
405 mc_writel(mc, status, MC_INTSTATUS);
406
407 return IRQ_HANDLED;
408}
409
Thierry Reding89184652014-04-16 09:24:44 +0200410static int tegra_mc_probe(struct platform_device *pdev)
411{
412 const struct of_device_id *match;
413 struct resource *res;
414 struct tegra_mc *mc;
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +0300415 void *isr;
Thierry Reding89184652014-04-16 09:24:44 +0200416 int err;
417
418 match = of_match_node(tegra_mc_of_match, pdev->dev.of_node);
419 if (!match)
420 return -ENODEV;
421
422 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
423 if (!mc)
424 return -ENOMEM;
425
426 platform_set_drvdata(pdev, mc);
427 mc->soc = match->data;
428 mc->dev = &pdev->dev;
429
430 /* length of MC tick in nanoseconds */
431 mc->tick = 30;
432
433 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434 mc->regs = devm_ioremap_resource(&pdev->dev, res);
435 if (IS_ERR(mc->regs))
436 return PTR_ERR(mc->regs);
437
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +0300438#ifdef CONFIG_ARCH_TEGRA_2x_SOC
439 if (mc->soc == &tegra20_mc_soc) {
440 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
441 mc->regs2 = devm_ioremap_resource(&pdev->dev, res);
442 if (IS_ERR(mc->regs2))
443 return PTR_ERR(mc->regs2);
Thierry Reding89184652014-04-16 09:24:44 +0200444
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +0300445 isr = tegra20_mc_irq;
446 } else
447#endif
448 {
449 mc->clk = devm_clk_get(&pdev->dev, "mc");
450 if (IS_ERR(mc->clk)) {
451 dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
452 PTR_ERR(mc->clk));
453 return PTR_ERR(mc->clk);
454 }
455
456 err = tegra_mc_setup_latency_allowance(mc);
457 if (err < 0) {
458 dev_err(&pdev->dev, "failed to setup latency allowance: %d\n",
459 err);
460 return err;
461 }
462
463 isr = tegra_mc_irq;
Thierry Reding89184652014-04-16 09:24:44 +0200464 }
465
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100466 err = tegra_mc_setup_timings(mc);
467 if (err < 0) {
468 dev_err(&pdev->dev, "failed to setup timings: %d\n", err);
469 return err;
470 }
471
Thierry Reding89184652014-04-16 09:24:44 +0200472 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU)) {
473 mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
474 if (IS_ERR(mc->smmu)) {
475 dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
476 PTR_ERR(mc->smmu));
477 return PTR_ERR(mc->smmu);
478 }
479 }
480
481 mc->irq = platform_get_irq(pdev, 0);
482 if (mc->irq < 0) {
483 dev_err(&pdev->dev, "interrupt not specified\n");
484 return mc->irq;
485 }
486
Paul Walmsley3c01cf32015-06-04 19:33:48 +0000487 WARN(!mc->soc->client_id_mask, "Missing client ID mask for this SoC\n");
488
Dmitry Osipenko1c74d5c2018-04-09 22:28:29 +0300489 mc_writel(mc, mc->soc->intmask, MC_INTMASK);
Thierry Reding89184652014-04-16 09:24:44 +0200490
Dmitry Osipenkoa8d502f2018-04-09 22:28:31 +0300491 err = devm_request_irq(&pdev->dev, mc->irq, isr, IRQF_SHARED,
Dmitry Osipenkodb4a9c12018-04-09 22:28:28 +0300492 dev_name(&pdev->dev), mc);
493 if (err < 0) {
494 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
495 err);
496 return err;
497 }
498
Thierry Reding89184652014-04-16 09:24:44 +0200499 return 0;
500}
501
502static struct platform_driver tegra_mc_driver = {
503 .driver = {
504 .name = "tegra-mc",
505 .of_match_table = tegra_mc_of_match,
506 .suppress_bind_attrs = true,
507 },
508 .prevent_deferred_probe = true,
509 .probe = tegra_mc_probe,
510};
511
512static int tegra_mc_init(void)
513{
514 return platform_driver_register(&tegra_mc_driver);
515}
516arch_initcall(tegra_mc_init);
517
518MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
519MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
520MODULE_LICENSE("GPL v2");