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/export.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
19 #include <soc/tegra/fuse.h>
23 static const struct of_device_id tegra_mc_of_match[] = {
24 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
25 { .compatible = "nvidia,tegra20-mc-gart", .data = &tegra20_mc_soc },
27 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
28 { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
30 #ifdef CONFIG_ARCH_TEGRA_114_SOC
31 { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
33 #ifdef CONFIG_ARCH_TEGRA_124_SOC
34 { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
36 #ifdef CONFIG_ARCH_TEGRA_132_SOC
37 { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
39 #ifdef CONFIG_ARCH_TEGRA_210_SOC
40 { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
44 MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
46 static void tegra_mc_devm_action_put_device(void *data)
48 struct tegra_mc *mc = data;
54 * devm_tegra_memory_controller_get() - get Tegra Memory Controller handle
55 * @dev: device pointer for the consumer device
57 * This function will search for the Memory Controller node in a device-tree
58 * and retrieve the Memory Controller handle.
60 * Return: ERR_PTR() on error or a valid pointer to a struct tegra_mc.
62 struct tegra_mc *devm_tegra_memory_controller_get(struct device *dev)
64 struct platform_device *pdev;
65 struct device_node *np;
69 np = of_parse_phandle(dev->of_node, "nvidia,memory-controller", 0);
71 return ERR_PTR(-ENOENT);
73 pdev = of_find_device_by_node(np);
76 return ERR_PTR(-ENODEV);
78 mc = platform_get_drvdata(pdev);
80 put_device(&pdev->dev);
81 return ERR_PTR(-EPROBE_DEFER);
84 err = devm_add_action(dev, tegra_mc_devm_action_put_device, mc);
92 EXPORT_SYMBOL_GPL(devm_tegra_memory_controller_get);
94 static int tegra_mc_block_dma_common(struct tegra_mc *mc,
95 const struct tegra_mc_reset *rst)
100 spin_lock_irqsave(&mc->lock, flags);
102 value = mc_readl(mc, rst->control) | BIT(rst->bit);
103 mc_writel(mc, value, rst->control);
105 spin_unlock_irqrestore(&mc->lock, flags);
110 static bool tegra_mc_dma_idling_common(struct tegra_mc *mc,
111 const struct tegra_mc_reset *rst)
113 return (mc_readl(mc, rst->status) & BIT(rst->bit)) != 0;
116 static int tegra_mc_unblock_dma_common(struct tegra_mc *mc,
117 const struct tegra_mc_reset *rst)
122 spin_lock_irqsave(&mc->lock, flags);
124 value = mc_readl(mc, rst->control) & ~BIT(rst->bit);
125 mc_writel(mc, value, rst->control);
127 spin_unlock_irqrestore(&mc->lock, flags);
132 static int tegra_mc_reset_status_common(struct tegra_mc *mc,
133 const struct tegra_mc_reset *rst)
135 return (mc_readl(mc, rst->control) & BIT(rst->bit)) != 0;
138 const struct tegra_mc_reset_ops tegra_mc_reset_ops_common = {
139 .block_dma = tegra_mc_block_dma_common,
140 .dma_idling = tegra_mc_dma_idling_common,
141 .unblock_dma = tegra_mc_unblock_dma_common,
142 .reset_status = tegra_mc_reset_status_common,
145 static inline struct tegra_mc *reset_to_mc(struct reset_controller_dev *rcdev)
147 return container_of(rcdev, struct tegra_mc, reset);
150 static const struct tegra_mc_reset *tegra_mc_reset_find(struct tegra_mc *mc,
155 for (i = 0; i < mc->soc->num_resets; i++)
156 if (mc->soc->resets[i].id == id)
157 return &mc->soc->resets[i];
162 static int tegra_mc_hotreset_assert(struct reset_controller_dev *rcdev,
165 struct tegra_mc *mc = reset_to_mc(rcdev);
166 const struct tegra_mc_reset_ops *rst_ops;
167 const struct tegra_mc_reset *rst;
171 rst = tegra_mc_reset_find(mc, id);
175 rst_ops = mc->soc->reset_ops;
179 /* DMA flushing will fail if reset is already asserted */
180 if (rst_ops->reset_status) {
181 /* check whether reset is asserted */
182 if (rst_ops->reset_status(mc, rst))
186 if (rst_ops->block_dma) {
187 /* block clients DMA requests */
188 err = rst_ops->block_dma(mc, rst);
190 dev_err(mc->dev, "failed to block %s DMA: %d\n",
196 if (rst_ops->dma_idling) {
197 /* wait for completion of the outstanding DMA requests */
198 while (!rst_ops->dma_idling(mc, rst)) {
200 dev_err(mc->dev, "failed to flush %s DMA\n",
205 usleep_range(10, 100);
209 if (rst_ops->hotreset_assert) {
210 /* clear clients DMA requests sitting before arbitration */
211 err = rst_ops->hotreset_assert(mc, rst);
213 dev_err(mc->dev, "failed to hot reset %s: %d\n",
222 static int tegra_mc_hotreset_deassert(struct reset_controller_dev *rcdev,
225 struct tegra_mc *mc = reset_to_mc(rcdev);
226 const struct tegra_mc_reset_ops *rst_ops;
227 const struct tegra_mc_reset *rst;
230 rst = tegra_mc_reset_find(mc, id);
234 rst_ops = mc->soc->reset_ops;
238 if (rst_ops->hotreset_deassert) {
239 /* take out client from hot reset */
240 err = rst_ops->hotreset_deassert(mc, rst);
242 dev_err(mc->dev, "failed to deassert hot reset %s: %d\n",
248 if (rst_ops->unblock_dma) {
249 /* allow new DMA requests to proceed to arbitration */
250 err = rst_ops->unblock_dma(mc, rst);
252 dev_err(mc->dev, "failed to unblock %s DMA : %d\n",
261 static int tegra_mc_hotreset_status(struct reset_controller_dev *rcdev,
264 struct tegra_mc *mc = reset_to_mc(rcdev);
265 const struct tegra_mc_reset_ops *rst_ops;
266 const struct tegra_mc_reset *rst;
268 rst = tegra_mc_reset_find(mc, id);
272 rst_ops = mc->soc->reset_ops;
276 return rst_ops->reset_status(mc, rst);
279 static const struct reset_control_ops tegra_mc_reset_ops = {
280 .assert = tegra_mc_hotreset_assert,
281 .deassert = tegra_mc_hotreset_deassert,
282 .status = tegra_mc_hotreset_status,
285 static int tegra_mc_reset_setup(struct tegra_mc *mc)
289 mc->reset.ops = &tegra_mc_reset_ops;
290 mc->reset.owner = THIS_MODULE;
291 mc->reset.of_node = mc->dev->of_node;
292 mc->reset.of_reset_n_cells = 1;
293 mc->reset.nr_resets = mc->soc->num_resets;
295 err = reset_controller_register(&mc->reset);
302 static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
304 unsigned long long tick;
308 /* compute the number of MC clock cycles per tick */
309 tick = (unsigned long long)mc->tick * clk_get_rate(mc->clk);
310 do_div(tick, NSEC_PER_SEC);
312 value = mc_readl(mc, MC_EMEM_ARB_CFG);
313 value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
314 value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
315 mc_writel(mc, value, MC_EMEM_ARB_CFG);
317 /* write latency allowance defaults */
318 for (i = 0; i < mc->soc->num_clients; i++) {
319 const struct tegra_mc_la *la = &mc->soc->clients[i].la;
322 value = mc_readl(mc, la->reg);
323 value &= ~(la->mask << la->shift);
324 value |= (la->def & la->mask) << la->shift;
325 mc_writel(mc, value, la->reg);
328 /* latch new values */
329 mc_writel(mc, MC_TIMING_UPDATE, MC_TIMING_CONTROL);
334 int tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
337 struct tegra_mc_timing *timing = NULL;
339 for (i = 0; i < mc->num_timings; i++) {
340 if (mc->timings[i].rate == rate) {
341 timing = &mc->timings[i];
347 dev_err(mc->dev, "no memory timing registered for rate %lu\n",
352 for (i = 0; i < mc->soc->num_emem_regs; ++i)
353 mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
357 EXPORT_SYMBOL_GPL(tegra_mc_write_emem_configuration);
359 unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
363 dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
364 dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
369 EXPORT_SYMBOL_GPL(tegra_mc_get_emem_device_count);
371 static int load_one_timing(struct tegra_mc *mc,
372 struct tegra_mc_timing *timing,
373 struct device_node *node)
378 err = of_property_read_u32(node, "clock-frequency", &tmp);
381 "timing %pOFn: failed to read rate\n", node);
386 timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
387 sizeof(u32), GFP_KERNEL);
388 if (!timing->emem_data)
391 err = of_property_read_u32_array(node, "nvidia,emem-configuration",
393 mc->soc->num_emem_regs);
396 "timing %pOFn: failed to read EMEM configuration\n",
404 static int load_timings(struct tegra_mc *mc, struct device_node *node)
406 struct device_node *child;
407 struct tegra_mc_timing *timing;
408 int child_count = of_get_child_count(node);
411 mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
416 mc->num_timings = child_count;
418 for_each_child_of_node(node, child) {
419 timing = &mc->timings[i++];
421 err = load_one_timing(mc, timing, child);
431 static int tegra_mc_setup_timings(struct tegra_mc *mc)
433 struct device_node *node;
434 u32 ram_code, node_ram_code;
437 ram_code = tegra_read_ram_code();
441 for_each_child_of_node(mc->dev->of_node, node) {
442 err = of_property_read_u32(node, "nvidia,ram-code",
444 if (err || (node_ram_code != ram_code))
447 err = load_timings(mc, node);
454 if (mc->num_timings == 0)
456 "no memory timings for RAM code %u registered\n",
462 static const char *const status_names[32] = {
463 [ 1] = "External interrupt",
464 [ 6] = "EMEM address decode error",
465 [ 7] = "GART page fault",
466 [ 8] = "Security violation",
467 [ 9] = "EMEM arbitration error",
469 [11] = "Invalid APB ASID update",
470 [12] = "VPR violation",
471 [13] = "Secure carveout violation",
472 [16] = "MTS carveout violation",
475 static const char *const error_names[8] = {
476 [2] = "EMEM decode error",
477 [3] = "TrustZone violation",
478 [4] = "Carveout violation",
479 [6] = "SMMU translation error",
482 static irqreturn_t tegra_mc_irq(int irq, void *data)
484 struct tegra_mc *mc = data;
485 unsigned long status;
488 /* mask all interrupts to avoid flooding */
489 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
493 for_each_set_bit(bit, &status, 32) {
494 const char *error = status_names[bit] ?: "unknown";
495 const char *client = "unknown", *desc;
496 const char *direction, *secure;
497 phys_addr_t addr = 0;
503 value = mc_readl(mc, MC_ERR_STATUS);
505 #ifdef CONFIG_PHYS_ADDR_T_64BIT
506 if (mc->soc->num_address_bits > 32) {
507 addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
508 MC_ERR_STATUS_ADR_HI_MASK);
513 if (value & MC_ERR_STATUS_RW)
518 if (value & MC_ERR_STATUS_SECURITY)
523 id = value & mc->soc->client_id_mask;
525 for (i = 0; i < mc->soc->num_clients; i++) {
526 if (mc->soc->clients[i].id == id) {
527 client = mc->soc->clients[i].name;
532 type = (value & MC_ERR_STATUS_TYPE_MASK) >>
533 MC_ERR_STATUS_TYPE_SHIFT;
534 desc = error_names[type];
536 switch (value & MC_ERR_STATUS_TYPE_MASK) {
537 case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
541 if (value & MC_ERR_STATUS_READABLE)
546 if (value & MC_ERR_STATUS_WRITABLE)
551 if (value & MC_ERR_STATUS_NONSECURE)
565 value = mc_readl(mc, MC_ERR_ADR);
568 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
569 client, secure, direction, &addr, error,
573 /* clear interrupts */
574 mc_writel(mc, status, MC_INTSTATUS);
579 static __maybe_unused irqreturn_t tegra20_mc_irq(int irq, void *data)
581 struct tegra_mc *mc = data;
582 unsigned long status;
585 /* mask all interrupts to avoid flooding */
586 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
590 for_each_set_bit(bit, &status, 32) {
591 const char *direction = "read", *secure = "";
592 const char *error = status_names[bit];
593 const char *client, *desc;
599 case MC_INT_DECERR_EMEM:
600 reg = MC_DECERR_EMEM_OTHERS_STATUS;
601 value = mc_readl(mc, reg);
603 id = value & mc->soc->client_id_mask;
604 desc = error_names[2];
610 case MC_INT_INVALID_GART_PAGE:
611 reg = MC_GART_ERROR_REQ;
612 value = mc_readl(mc, reg);
614 id = (value >> 1) & mc->soc->client_id_mask;
615 desc = error_names[2];
621 case MC_INT_SECURITY_VIOLATION:
622 reg = MC_SECURITY_VIOLATION_STATUS;
623 value = mc_readl(mc, reg);
625 id = value & mc->soc->client_id_mask;
626 type = (value & BIT(30)) ? 4 : 3;
627 desc = error_names[type];
638 client = mc->soc->clients[id].name;
639 addr = mc_readl(mc, reg + sizeof(u32));
641 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s)\n",
642 client, secure, direction, &addr, error,
646 /* clear interrupts */
647 mc_writel(mc, status, MC_INTSTATUS);
653 * Memory Controller (MC) has few Memory Clients that are issuing memory
654 * bandwidth allocation requests to the MC interconnect provider. The MC
655 * provider aggregates the requests and then sends the aggregated request
656 * up to the External Memory Controller (EMC) interconnect provider which
657 * re-configures hardware interface to External Memory (EMEM) in accordance
658 * to the required bandwidth. Each MC interconnect node represents an
659 * individual Memory Client.
661 * Memory interconnect topology:
667 * | | +-----+ +------+
668 * ... | MC +--->+ EMC +--->+ EMEM |
669 * | | +-----+ +------+
675 static int tegra_mc_interconnect_setup(struct tegra_mc *mc)
677 struct icc_node *node;
681 /* older device-trees don't have interconnect properties */
682 if (!device_property_present(mc->dev, "#interconnect-cells") ||
686 mc->provider.dev = mc->dev;
687 mc->provider.data = &mc->provider;
688 mc->provider.set = mc->soc->icc_ops->set;
689 mc->provider.aggregate = mc->soc->icc_ops->aggregate;
690 mc->provider.xlate_extended = mc->soc->icc_ops->xlate_extended;
692 err = icc_provider_add(&mc->provider);
696 /* create Memory Controller node */
697 node = icc_node_create(TEGRA_ICC_MC);
703 node->name = "Memory Controller";
704 icc_node_add(node, &mc->provider);
706 /* link Memory Controller to External Memory Controller */
707 err = icc_link_create(node, TEGRA_ICC_EMC);
711 for (i = 0; i < mc->soc->num_clients; i++) {
712 /* create MC client node */
713 node = icc_node_create(mc->soc->clients[i].id);
719 node->name = mc->soc->clients[i].name;
720 icc_node_add(node, &mc->provider);
722 /* link Memory Client to Memory Controller */
723 err = icc_link_create(node, TEGRA_ICC_MC);
729 * MC driver is registered too early, so early that generic driver
730 * syncing doesn't work for the MC. But it doesn't really matter
731 * since syncing works for the EMC drivers, hence we can sync the
732 * MC driver by ourselves and then EMC will complete syncing of
733 * the whole ICC state.
735 icc_sync_state(mc->dev);
740 icc_nodes_remove(&mc->provider);
742 icc_provider_del(&mc->provider);
747 static int tegra_mc_probe(struct platform_device *pdev)
749 struct resource *res;
755 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
759 platform_set_drvdata(pdev, mc);
760 spin_lock_init(&mc->lock);
761 mc->soc = of_device_get_match_data(&pdev->dev);
762 mc->dev = &pdev->dev;
764 mask = DMA_BIT_MASK(mc->soc->num_address_bits);
766 err = dma_coerce_mask_and_coherent(&pdev->dev, mask);
768 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
772 /* length of MC tick in nanoseconds */
775 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
776 mc->regs = devm_ioremap_resource(&pdev->dev, res);
777 if (IS_ERR(mc->regs))
778 return PTR_ERR(mc->regs);
780 mc->clk = devm_clk_get(&pdev->dev, "mc");
781 if (IS_ERR(mc->clk)) {
782 dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
784 return PTR_ERR(mc->clk);
787 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
788 if (mc->soc == &tegra20_mc_soc) {
789 isr = tegra20_mc_irq;
793 /* ensure that debug features are disabled */
794 mc_writel(mc, 0x00000000, MC_TIMING_CONTROL_DBG);
796 err = tegra_mc_setup_latency_allowance(mc);
799 "failed to setup latency allowance: %d\n",
806 err = tegra_mc_setup_timings(mc);
808 dev_err(&pdev->dev, "failed to setup timings: %d\n",
814 mc->irq = platform_get_irq(pdev, 0);
818 WARN(!mc->soc->client_id_mask, "missing client ID mask for this SoC\n");
820 mc_writel(mc, mc->soc->intmask, MC_INTMASK);
822 err = devm_request_irq(&pdev->dev, mc->irq, isr, 0,
823 dev_name(&pdev->dev), mc);
825 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
830 mc->debugfs.root = debugfs_create_dir("mc", NULL);
833 err = mc->soc->init(mc);
835 dev_err(&pdev->dev, "failed to initialize SoC driver: %d\n",
839 err = tegra_mc_reset_setup(mc);
841 dev_err(&pdev->dev, "failed to register reset controller: %d\n",
844 err = tegra_mc_interconnect_setup(mc);
846 dev_err(&pdev->dev, "failed to initialize interconnect: %d\n",
849 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU) && mc->soc->smmu) {
850 mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
851 if (IS_ERR(mc->smmu)) {
852 dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
858 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && !mc->soc->smmu) {
859 mc->gart = tegra_gart_probe(&pdev->dev, mc);
860 if (IS_ERR(mc->gart)) {
861 dev_err(&pdev->dev, "failed to probe GART: %ld\n",
870 static int tegra_mc_suspend(struct device *dev)
872 struct tegra_mc *mc = dev_get_drvdata(dev);
875 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) {
876 err = tegra_gart_suspend(mc->gart);
884 static int tegra_mc_resume(struct device *dev)
886 struct tegra_mc *mc = dev_get_drvdata(dev);
889 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) {
890 err = tegra_gart_resume(mc->gart);
898 static const struct dev_pm_ops tegra_mc_pm_ops = {
899 .suspend = tegra_mc_suspend,
900 .resume = tegra_mc_resume,
903 static struct platform_driver tegra_mc_driver = {
906 .of_match_table = tegra_mc_of_match,
907 .pm = &tegra_mc_pm_ops,
908 .suppress_bind_attrs = true,
910 .prevent_deferred_probe = true,
911 .probe = tegra_mc_probe,
914 static int tegra_mc_init(void)
916 return platform_driver_register(&tegra_mc_driver);
918 arch_initcall(tegra_mc_init);
920 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
921 MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
922 MODULE_LICENSE("GPL v2");