mc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  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. #include <linux/clk.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include "mc.h"
  16. #define MC_INTSTATUS 0x000
  17. #define MC_INT_DECERR_MTS (1 << 16)
  18. #define MC_INT_SECERR_SEC (1 << 13)
  19. #define MC_INT_DECERR_VPR (1 << 12)
  20. #define MC_INT_INVALID_APB_ASID_UPDATE (1 << 11)
  21. #define MC_INT_INVALID_SMMU_PAGE (1 << 10)
  22. #define MC_INT_ARBITRATION_EMEM (1 << 9)
  23. #define MC_INT_SECURITY_VIOLATION (1 << 8)
  24. #define MC_INT_DECERR_EMEM (1 << 6)
  25. #define MC_INTMASK 0x004
  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)
  37. #define MC_ERR_STATUS_CLIENT_MASK 0x7f
  38. #define MC_ERR_ADR 0x0c
  39. #define MC_EMEM_ARB_CFG 0x90
  40. #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(x) (((x) & 0x1ff) << 0)
  41. #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK 0x1ff
  42. #define MC_EMEM_ARB_MISC0 0xd8
  43. static const struct of_device_id tegra_mc_of_match[] = {
  44. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  45. { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
  46. #endif
  47. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  48. { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
  49. #endif
  50. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  51. { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
  52. #endif
  53. { }
  54. };
  55. MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
  56. static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
  57. {
  58. unsigned long long tick;
  59. unsigned int i;
  60. u32 value;
  61. /* compute the number of MC clock cycles per tick */
  62. tick = mc->tick * clk_get_rate(mc->clk);
  63. do_div(tick, NSEC_PER_SEC);
  64. value = readl(mc->regs + MC_EMEM_ARB_CFG);
  65. value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
  66. value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
  67. writel(value, mc->regs + MC_EMEM_ARB_CFG);
  68. /* write latency allowance defaults */
  69. for (i = 0; i < mc->soc->num_clients; i++) {
  70. const struct tegra_mc_la *la = &mc->soc->clients[i].la;
  71. u32 value;
  72. value = readl(mc->regs + la->reg);
  73. value &= ~(la->mask << la->shift);
  74. value |= (la->def & la->mask) << la->shift;
  75. writel(value, mc->regs + la->reg);
  76. }
  77. return 0;
  78. }
  79. static const char *const status_names[32] = {
  80. [ 1] = "External interrupt",
  81. [ 6] = "EMEM address decode error",
  82. [ 8] = "Security violation",
  83. [ 9] = "EMEM arbitration error",
  84. [10] = "Page fault",
  85. [11] = "Invalid APB ASID update",
  86. [12] = "VPR violation",
  87. [13] = "Secure carveout violation",
  88. [16] = "MTS carveout violation",
  89. };
  90. static const char *const error_names[8] = {
  91. [2] = "EMEM decode error",
  92. [3] = "TrustZone violation",
  93. [4] = "Carveout violation",
  94. [6] = "SMMU translation error",
  95. };
  96. static irqreturn_t tegra_mc_irq(int irq, void *data)
  97. {
  98. struct tegra_mc *mc = data;
  99. unsigned long status, mask;
  100. unsigned int bit;
  101. /* mask all interrupts to avoid flooding */
  102. status = mc_readl(mc, MC_INTSTATUS);
  103. mask = mc_readl(mc, MC_INTMASK);
  104. for_each_set_bit(bit, &status, 32) {
  105. const char *error = status_names[bit] ?: "unknown";
  106. const char *client = "unknown", *desc;
  107. const char *direction, *secure;
  108. phys_addr_t addr = 0;
  109. unsigned int i;
  110. char perm[7];
  111. u8 id, type;
  112. u32 value;
  113. value = mc_readl(mc, MC_ERR_STATUS);
  114. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  115. if (mc->soc->num_address_bits > 32) {
  116. addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
  117. MC_ERR_STATUS_ADR_HI_MASK);
  118. addr <<= 32;
  119. }
  120. #endif
  121. if (value & MC_ERR_STATUS_RW)
  122. direction = "write";
  123. else
  124. direction = "read";
  125. if (value & MC_ERR_STATUS_SECURITY)
  126. secure = "secure ";
  127. else
  128. secure = "";
  129. id = value & MC_ERR_STATUS_CLIENT_MASK;
  130. for (i = 0; i < mc->soc->num_clients; i++) {
  131. if (mc->soc->clients[i].id == id) {
  132. client = mc->soc->clients[i].name;
  133. break;
  134. }
  135. }
  136. type = (value & MC_ERR_STATUS_TYPE_MASK) >>
  137. MC_ERR_STATUS_TYPE_SHIFT;
  138. desc = error_names[type];
  139. switch (value & MC_ERR_STATUS_TYPE_MASK) {
  140. case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
  141. perm[0] = ' ';
  142. perm[1] = '[';
  143. if (value & MC_ERR_STATUS_READABLE)
  144. perm[2] = 'R';
  145. else
  146. perm[2] = '-';
  147. if (value & MC_ERR_STATUS_WRITABLE)
  148. perm[3] = 'W';
  149. else
  150. perm[3] = '-';
  151. if (value & MC_ERR_STATUS_NONSECURE)
  152. perm[4] = '-';
  153. else
  154. perm[4] = 'S';
  155. perm[5] = ']';
  156. perm[6] = '\0';
  157. break;
  158. default:
  159. perm[0] = '\0';
  160. break;
  161. }
  162. value = mc_readl(mc, MC_ERR_ADR);
  163. addr |= value;
  164. dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
  165. client, secure, direction, &addr, error,
  166. desc, perm);
  167. }
  168. /* clear interrupts */
  169. mc_writel(mc, status, MC_INTSTATUS);
  170. return IRQ_HANDLED;
  171. }
  172. static int tegra_mc_probe(struct platform_device *pdev)
  173. {
  174. const struct of_device_id *match;
  175. struct resource *res;
  176. struct tegra_mc *mc;
  177. u32 value;
  178. int err;
  179. match = of_match_node(tegra_mc_of_match, pdev->dev.of_node);
  180. if (!match)
  181. return -ENODEV;
  182. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  183. if (!mc)
  184. return -ENOMEM;
  185. platform_set_drvdata(pdev, mc);
  186. mc->soc = match->data;
  187. mc->dev = &pdev->dev;
  188. /* length of MC tick in nanoseconds */
  189. mc->tick = 30;
  190. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. mc->regs = devm_ioremap_resource(&pdev->dev, res);
  192. if (IS_ERR(mc->regs))
  193. return PTR_ERR(mc->regs);
  194. mc->clk = devm_clk_get(&pdev->dev, "mc");
  195. if (IS_ERR(mc->clk)) {
  196. dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
  197. PTR_ERR(mc->clk));
  198. return PTR_ERR(mc->clk);
  199. }
  200. err = tegra_mc_setup_latency_allowance(mc);
  201. if (err < 0) {
  202. dev_err(&pdev->dev, "failed to setup latency allowance: %d\n",
  203. err);
  204. return err;
  205. }
  206. if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU)) {
  207. mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
  208. if (IS_ERR(mc->smmu)) {
  209. dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
  210. PTR_ERR(mc->smmu));
  211. return PTR_ERR(mc->smmu);
  212. }
  213. }
  214. mc->irq = platform_get_irq(pdev, 0);
  215. if (mc->irq < 0) {
  216. dev_err(&pdev->dev, "interrupt not specified\n");
  217. return mc->irq;
  218. }
  219. err = devm_request_irq(&pdev->dev, mc->irq, tegra_mc_irq, IRQF_SHARED,
  220. dev_name(&pdev->dev), mc);
  221. if (err < 0) {
  222. dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
  223. err);
  224. return err;
  225. }
  226. value = MC_INT_DECERR_MTS | MC_INT_SECERR_SEC | MC_INT_DECERR_VPR |
  227. MC_INT_INVALID_APB_ASID_UPDATE | MC_INT_INVALID_SMMU_PAGE |
  228. MC_INT_ARBITRATION_EMEM | MC_INT_SECURITY_VIOLATION |
  229. MC_INT_DECERR_EMEM;
  230. mc_writel(mc, value, MC_INTMASK);
  231. return 0;
  232. }
  233. static struct platform_driver tegra_mc_driver = {
  234. .driver = {
  235. .name = "tegra-mc",
  236. .of_match_table = tegra_mc_of_match,
  237. .suppress_bind_attrs = true,
  238. },
  239. .prevent_deferred_probe = true,
  240. .probe = tegra_mc_probe,
  241. };
  242. static int tegra_mc_init(void)
  243. {
  244. return platform_driver_register(&tegra_mc_driver);
  245. }
  246. arch_initcall(tegra_mc_init);
  247. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  248. MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
  249. MODULE_LICENSE("GPL v2");