1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/sort.h>
18 #include <soc/tegra/fuse.h>
22 static const struct of_device_id tegra_mc_of_match[] = {
23 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
24 { .compatible = "nvidia,tegra20-mc-gart", .data = &tegra20_mc_soc },
26 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
27 { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
29 #ifdef CONFIG_ARCH_TEGRA_114_SOC
30 { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
32 #ifdef CONFIG_ARCH_TEGRA_124_SOC
33 { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
35 #ifdef CONFIG_ARCH_TEGRA_132_SOC
36 { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
38 #ifdef CONFIG_ARCH_TEGRA_210_SOC
39 { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
43 MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
45 static void tegra_mc_devm_action_put_device(void *data)
47 struct tegra_mc *mc = data;
53 * devm_tegra_memory_controller_get() - get Tegra Memory Controller handle
54 * @dev: device pointer for the consumer device
56 * This function will search for the Memory Controller node in a device-tree
57 * and retrieve the Memory Controller handle.
59 * Return: ERR_PTR() on error or a valid pointer to a struct tegra_mc.
61 struct tegra_mc *devm_tegra_memory_controller_get(struct device *dev)
63 struct platform_device *pdev;
64 struct device_node *np;
68 np = of_parse_phandle(dev->of_node, "nvidia,memory-controller", 0);
70 return ERR_PTR(-ENOENT);
72 pdev = of_find_device_by_node(np);
75 return ERR_PTR(-ENODEV);
77 mc = platform_get_drvdata(pdev);
79 put_device(&pdev->dev);
80 return ERR_PTR(-EPROBE_DEFER);
83 err = devm_add_action(dev, tegra_mc_devm_action_put_device, mc);
91 EXPORT_SYMBOL_GPL(devm_tegra_memory_controller_get);
93 static int tegra_mc_block_dma_common(struct tegra_mc *mc,
94 const struct tegra_mc_reset *rst)
99 spin_lock_irqsave(&mc->lock, flags);
101 value = mc_readl(mc, rst->control) | BIT(rst->bit);
102 mc_writel(mc, value, rst->control);
104 spin_unlock_irqrestore(&mc->lock, flags);
109 static bool tegra_mc_dma_idling_common(struct tegra_mc *mc,
110 const struct tegra_mc_reset *rst)
112 return (mc_readl(mc, rst->status) & BIT(rst->bit)) != 0;
115 static int tegra_mc_unblock_dma_common(struct tegra_mc *mc,
116 const struct tegra_mc_reset *rst)
121 spin_lock_irqsave(&mc->lock, flags);
123 value = mc_readl(mc, rst->control) & ~BIT(rst->bit);
124 mc_writel(mc, value, rst->control);
126 spin_unlock_irqrestore(&mc->lock, flags);
131 static int tegra_mc_reset_status_common(struct tegra_mc *mc,
132 const struct tegra_mc_reset *rst)
134 return (mc_readl(mc, rst->control) & BIT(rst->bit)) != 0;
137 const struct tegra_mc_reset_ops tegra_mc_reset_ops_common = {
138 .block_dma = tegra_mc_block_dma_common,
139 .dma_idling = tegra_mc_dma_idling_common,
140 .unblock_dma = tegra_mc_unblock_dma_common,
141 .reset_status = tegra_mc_reset_status_common,
144 static inline struct tegra_mc *reset_to_mc(struct reset_controller_dev *rcdev)
146 return container_of(rcdev, struct tegra_mc, reset);
149 static const struct tegra_mc_reset *tegra_mc_reset_find(struct tegra_mc *mc,
154 for (i = 0; i < mc->soc->num_resets; i++)
155 if (mc->soc->resets[i].id == id)
156 return &mc->soc->resets[i];
161 static int tegra_mc_hotreset_assert(struct reset_controller_dev *rcdev,
164 struct tegra_mc *mc = reset_to_mc(rcdev);
165 const struct tegra_mc_reset_ops *rst_ops;
166 const struct tegra_mc_reset *rst;
170 rst = tegra_mc_reset_find(mc, id);
174 rst_ops = mc->soc->reset_ops;
178 if (rst_ops->block_dma) {
179 /* block clients DMA requests */
180 err = rst_ops->block_dma(mc, rst);
182 dev_err(mc->dev, "failed to block %s DMA: %d\n",
188 if (rst_ops->dma_idling) {
189 /* wait for completion of the outstanding DMA requests */
190 while (!rst_ops->dma_idling(mc, rst)) {
192 dev_err(mc->dev, "failed to flush %s DMA\n",
197 usleep_range(10, 100);
201 if (rst_ops->hotreset_assert) {
202 /* clear clients DMA requests sitting before arbitration */
203 err = rst_ops->hotreset_assert(mc, rst);
205 dev_err(mc->dev, "failed to hot reset %s: %d\n",
214 static int tegra_mc_hotreset_deassert(struct reset_controller_dev *rcdev,
217 struct tegra_mc *mc = reset_to_mc(rcdev);
218 const struct tegra_mc_reset_ops *rst_ops;
219 const struct tegra_mc_reset *rst;
222 rst = tegra_mc_reset_find(mc, id);
226 rst_ops = mc->soc->reset_ops;
230 if (rst_ops->hotreset_deassert) {
231 /* take out client from hot reset */
232 err = rst_ops->hotreset_deassert(mc, rst);
234 dev_err(mc->dev, "failed to deassert hot reset %s: %d\n",
240 if (rst_ops->unblock_dma) {
241 /* allow new DMA requests to proceed to arbitration */
242 err = rst_ops->unblock_dma(mc, rst);
244 dev_err(mc->dev, "failed to unblock %s DMA : %d\n",
253 static int tegra_mc_hotreset_status(struct reset_controller_dev *rcdev,
256 struct tegra_mc *mc = reset_to_mc(rcdev);
257 const struct tegra_mc_reset_ops *rst_ops;
258 const struct tegra_mc_reset *rst;
260 rst = tegra_mc_reset_find(mc, id);
264 rst_ops = mc->soc->reset_ops;
268 return rst_ops->reset_status(mc, rst);
271 static const struct reset_control_ops tegra_mc_reset_ops = {
272 .assert = tegra_mc_hotreset_assert,
273 .deassert = tegra_mc_hotreset_deassert,
274 .status = tegra_mc_hotreset_status,
277 static int tegra_mc_reset_setup(struct tegra_mc *mc)
281 mc->reset.ops = &tegra_mc_reset_ops;
282 mc->reset.owner = THIS_MODULE;
283 mc->reset.of_node = mc->dev->of_node;
284 mc->reset.of_reset_n_cells = 1;
285 mc->reset.nr_resets = mc->soc->num_resets;
287 err = reset_controller_register(&mc->reset);
294 static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
296 unsigned long long tick;
300 /* compute the number of MC clock cycles per tick */
301 tick = (unsigned long long)mc->tick * clk_get_rate(mc->clk);
302 do_div(tick, NSEC_PER_SEC);
304 value = mc_readl(mc, MC_EMEM_ARB_CFG);
305 value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
306 value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
307 mc_writel(mc, value, MC_EMEM_ARB_CFG);
309 /* write latency allowance defaults */
310 for (i = 0; i < mc->soc->num_clients; i++) {
311 const struct tegra_mc_la *la = &mc->soc->clients[i].la;
314 value = mc_readl(mc, la->reg);
315 value &= ~(la->mask << la->shift);
316 value |= (la->def & la->mask) << la->shift;
317 mc_writel(mc, value, la->reg);
320 /* latch new values */
321 mc_writel(mc, MC_TIMING_UPDATE, MC_TIMING_CONTROL);
326 int tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
329 struct tegra_mc_timing *timing = NULL;
331 for (i = 0; i < mc->num_timings; i++) {
332 if (mc->timings[i].rate == rate) {
333 timing = &mc->timings[i];
339 dev_err(mc->dev, "no memory timing registered for rate %lu\n",
344 for (i = 0; i < mc->soc->num_emem_regs; ++i)
345 mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
350 unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
354 dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
355 dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
361 static int load_one_timing(struct tegra_mc *mc,
362 struct tegra_mc_timing *timing,
363 struct device_node *node)
368 err = of_property_read_u32(node, "clock-frequency", &tmp);
371 "timing %pOFn: failed to read rate\n", node);
376 timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
377 sizeof(u32), GFP_KERNEL);
378 if (!timing->emem_data)
381 err = of_property_read_u32_array(node, "nvidia,emem-configuration",
383 mc->soc->num_emem_regs);
386 "timing %pOFn: failed to read EMEM configuration\n",
394 static int load_timings(struct tegra_mc *mc, struct device_node *node)
396 struct device_node *child;
397 struct tegra_mc_timing *timing;
398 int child_count = of_get_child_count(node);
401 mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
406 mc->num_timings = child_count;
408 for_each_child_of_node(node, child) {
409 timing = &mc->timings[i++];
411 err = load_one_timing(mc, timing, child);
421 static int tegra_mc_setup_timings(struct tegra_mc *mc)
423 struct device_node *node;
424 u32 ram_code, node_ram_code;
427 ram_code = tegra_read_ram_code();
431 for_each_child_of_node(mc->dev->of_node, node) {
432 err = of_property_read_u32(node, "nvidia,ram-code",
434 if (err || (node_ram_code != ram_code))
437 err = load_timings(mc, node);
444 if (mc->num_timings == 0)
446 "no memory timings for RAM code %u registered\n",
452 static const char *const status_names[32] = {
453 [ 1] = "External interrupt",
454 [ 6] = "EMEM address decode error",
455 [ 7] = "GART page fault",
456 [ 8] = "Security violation",
457 [ 9] = "EMEM arbitration error",
459 [11] = "Invalid APB ASID update",
460 [12] = "VPR violation",
461 [13] = "Secure carveout violation",
462 [16] = "MTS carveout violation",
465 static const char *const error_names[8] = {
466 [2] = "EMEM decode error",
467 [3] = "TrustZone violation",
468 [4] = "Carveout violation",
469 [6] = "SMMU translation error",
472 static irqreturn_t tegra_mc_irq(int irq, void *data)
474 struct tegra_mc *mc = data;
475 unsigned long status;
478 /* mask all interrupts to avoid flooding */
479 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
483 for_each_set_bit(bit, &status, 32) {
484 const char *error = status_names[bit] ?: "unknown";
485 const char *client = "unknown", *desc;
486 const char *direction, *secure;
487 phys_addr_t addr = 0;
493 value = mc_readl(mc, MC_ERR_STATUS);
495 #ifdef CONFIG_PHYS_ADDR_T_64BIT
496 if (mc->soc->num_address_bits > 32) {
497 addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
498 MC_ERR_STATUS_ADR_HI_MASK);
503 if (value & MC_ERR_STATUS_RW)
508 if (value & MC_ERR_STATUS_SECURITY)
513 id = value & mc->soc->client_id_mask;
515 for (i = 0; i < mc->soc->num_clients; i++) {
516 if (mc->soc->clients[i].id == id) {
517 client = mc->soc->clients[i].name;
522 type = (value & MC_ERR_STATUS_TYPE_MASK) >>
523 MC_ERR_STATUS_TYPE_SHIFT;
524 desc = error_names[type];
526 switch (value & MC_ERR_STATUS_TYPE_MASK) {
527 case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
531 if (value & MC_ERR_STATUS_READABLE)
536 if (value & MC_ERR_STATUS_WRITABLE)
541 if (value & MC_ERR_STATUS_NONSECURE)
555 value = mc_readl(mc, MC_ERR_ADR);
558 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
559 client, secure, direction, &addr, error,
563 /* clear interrupts */
564 mc_writel(mc, status, MC_INTSTATUS);
569 static __maybe_unused irqreturn_t tegra20_mc_irq(int irq, void *data)
571 struct tegra_mc *mc = data;
572 unsigned long status;
575 /* mask all interrupts to avoid flooding */
576 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
580 for_each_set_bit(bit, &status, 32) {
581 const char *direction = "read", *secure = "";
582 const char *error = status_names[bit];
583 const char *client, *desc;
589 case MC_INT_DECERR_EMEM:
590 reg = MC_DECERR_EMEM_OTHERS_STATUS;
591 value = mc_readl(mc, reg);
593 id = value & mc->soc->client_id_mask;
594 desc = error_names[2];
600 case MC_INT_INVALID_GART_PAGE:
601 reg = MC_GART_ERROR_REQ;
602 value = mc_readl(mc, reg);
604 id = (value >> 1) & mc->soc->client_id_mask;
605 desc = error_names[2];
611 case MC_INT_SECURITY_VIOLATION:
612 reg = MC_SECURITY_VIOLATION_STATUS;
613 value = mc_readl(mc, reg);
615 id = value & mc->soc->client_id_mask;
616 type = (value & BIT(30)) ? 4 : 3;
617 desc = error_names[type];
628 client = mc->soc->clients[id].name;
629 addr = mc_readl(mc, reg + sizeof(u32));
631 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s)\n",
632 client, secure, direction, &addr, error,
636 /* clear interrupts */
637 mc_writel(mc, status, MC_INTSTATUS);
642 static int tegra_mc_probe(struct platform_device *pdev)
644 struct resource *res;
650 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
654 platform_set_drvdata(pdev, mc);
655 spin_lock_init(&mc->lock);
656 mc->soc = of_device_get_match_data(&pdev->dev);
657 mc->dev = &pdev->dev;
659 mask = DMA_BIT_MASK(mc->soc->num_address_bits);
661 err = dma_coerce_mask_and_coherent(&pdev->dev, mask);
663 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
667 /* length of MC tick in nanoseconds */
670 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
671 mc->regs = devm_ioremap_resource(&pdev->dev, res);
672 if (IS_ERR(mc->regs))
673 return PTR_ERR(mc->regs);
675 mc->clk = devm_clk_get(&pdev->dev, "mc");
676 if (IS_ERR(mc->clk)) {
677 dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
679 return PTR_ERR(mc->clk);
682 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
683 if (mc->soc == &tegra20_mc_soc) {
684 isr = tegra20_mc_irq;
688 /* ensure that debug features are disabled */
689 mc_writel(mc, 0x00000000, MC_TIMING_CONTROL_DBG);
691 err = tegra_mc_setup_latency_allowance(mc);
694 "failed to setup latency allowance: %d\n",
701 err = tegra_mc_setup_timings(mc);
703 dev_err(&pdev->dev, "failed to setup timings: %d\n",
709 mc->irq = platform_get_irq(pdev, 0);
713 WARN(!mc->soc->client_id_mask, "missing client ID mask for this SoC\n");
715 mc_writel(mc, mc->soc->intmask, MC_INTMASK);
717 err = devm_request_irq(&pdev->dev, mc->irq, isr, 0,
718 dev_name(&pdev->dev), mc);
720 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
725 err = tegra_mc_reset_setup(mc);
727 dev_err(&pdev->dev, "failed to register reset controller: %d\n",
730 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU) && mc->soc->smmu) {
731 mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
732 if (IS_ERR(mc->smmu)) {
733 dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
739 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && !mc->soc->smmu) {
740 mc->gart = tegra_gart_probe(&pdev->dev, mc);
741 if (IS_ERR(mc->gart)) {
742 dev_err(&pdev->dev, "failed to probe GART: %ld\n",
751 static int tegra_mc_suspend(struct device *dev)
753 struct tegra_mc *mc = dev_get_drvdata(dev);
756 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) {
757 err = tegra_gart_suspend(mc->gart);
765 static int tegra_mc_resume(struct device *dev)
767 struct tegra_mc *mc = dev_get_drvdata(dev);
770 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) {
771 err = tegra_gart_resume(mc->gart);
779 static const struct dev_pm_ops tegra_mc_pm_ops = {
780 .suspend = tegra_mc_suspend,
781 .resume = tegra_mc_resume,
784 static struct platform_driver tegra_mc_driver = {
787 .of_match_table = tegra_mc_of_match,
788 .pm = &tegra_mc_pm_ops,
789 .suppress_bind_attrs = true,
791 .prevent_deferred_probe = true,
792 .probe = tegra_mc_probe,
795 static int tegra_mc_init(void)
797 return platform_driver_register(&tegra_mc_driver);
799 arch_initcall(tegra_mc_init);
801 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
802 MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
803 MODULE_LICENSE("GPL v2");