mc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 <linux/sort.h>
  16. #include <soc/tegra/fuse.h>
  17. #include "mc.h"
  18. #define MC_INTSTATUS 0x000
  19. #define MC_INT_DECERR_MTS (1 << 16)
  20. #define MC_INT_SECERR_SEC (1 << 13)
  21. #define MC_INT_DECERR_VPR (1 << 12)
  22. #define MC_INT_INVALID_APB_ASID_UPDATE (1 << 11)
  23. #define MC_INT_INVALID_SMMU_PAGE (1 << 10)
  24. #define MC_INT_ARBITRATION_EMEM (1 << 9)
  25. #define MC_INT_SECURITY_VIOLATION (1 << 8)
  26. #define MC_INT_DECERR_EMEM (1 << 6)
  27. #define MC_INTMASK 0x004
  28. #define MC_ERR_STATUS 0x08
  29. #define MC_ERR_STATUS_TYPE_SHIFT 28
  30. #define MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE (6 << MC_ERR_STATUS_TYPE_SHIFT)
  31. #define MC_ERR_STATUS_TYPE_MASK (0x7 << MC_ERR_STATUS_TYPE_SHIFT)
  32. #define MC_ERR_STATUS_READABLE (1 << 27)
  33. #define MC_ERR_STATUS_WRITABLE (1 << 26)
  34. #define MC_ERR_STATUS_NONSECURE (1 << 25)
  35. #define MC_ERR_STATUS_ADR_HI_SHIFT 20
  36. #define MC_ERR_STATUS_ADR_HI_MASK 0x3
  37. #define MC_ERR_STATUS_SECURITY (1 << 17)
  38. #define MC_ERR_STATUS_RW (1 << 16)
  39. #define MC_ERR_ADR 0x0c
  40. #define MC_EMEM_ARB_CFG 0x90
  41. #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(x) (((x) & 0x1ff) << 0)
  42. #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK 0x1ff
  43. #define MC_EMEM_ARB_MISC0 0xd8
  44. #define MC_EMEM_ADR_CFG 0x54
  45. #define MC_EMEM_ADR_CFG_EMEM_NUMDEV BIT(0)
  46. static const struct of_device_id tegra_mc_of_match[] = {
  47. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  48. { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
  49. #endif
  50. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  51. { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
  52. #endif
  53. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  54. { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
  55. #endif
  56. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  57. { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
  58. #endif
  59. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  60. { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
  61. #endif
  62. { }
  63. };
  64. MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
  65. static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
  66. {
  67. unsigned long long tick;
  68. unsigned int i;
  69. u32 value;
  70. /* compute the number of MC clock cycles per tick */
  71. tick = mc->tick * clk_get_rate(mc->clk);
  72. do_div(tick, NSEC_PER_SEC);
  73. value = readl(mc->regs + MC_EMEM_ARB_CFG);
  74. value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
  75. value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
  76. writel(value, mc->regs + MC_EMEM_ARB_CFG);
  77. /* write latency allowance defaults */
  78. for (i = 0; i < mc->soc->num_clients; i++) {
  79. const struct tegra_mc_la *la = &mc->soc->clients[i].la;
  80. u32 value;
  81. value = readl(mc->regs + la->reg);
  82. value &= ~(la->mask << la->shift);
  83. value |= (la->def & la->mask) << la->shift;
  84. writel(value, mc->regs + la->reg);
  85. }
  86. return 0;
  87. }
  88. void tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
  89. {
  90. unsigned int i;
  91. struct tegra_mc_timing *timing = NULL;
  92. for (i = 0; i < mc->num_timings; i++) {
  93. if (mc->timings[i].rate == rate) {
  94. timing = &mc->timings[i];
  95. break;
  96. }
  97. }
  98. if (!timing) {
  99. dev_err(mc->dev, "no memory timing registered for rate %lu\n",
  100. rate);
  101. return;
  102. }
  103. for (i = 0; i < mc->soc->num_emem_regs; ++i)
  104. mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
  105. }
  106. unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
  107. {
  108. u8 dram_count;
  109. dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
  110. dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
  111. dram_count++;
  112. return dram_count;
  113. }
  114. static int load_one_timing(struct tegra_mc *mc,
  115. struct tegra_mc_timing *timing,
  116. struct device_node *node)
  117. {
  118. int err;
  119. u32 tmp;
  120. err = of_property_read_u32(node, "clock-frequency", &tmp);
  121. if (err) {
  122. dev_err(mc->dev,
  123. "timing %s: failed to read rate\n", node->name);
  124. return err;
  125. }
  126. timing->rate = tmp;
  127. timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
  128. sizeof(u32), GFP_KERNEL);
  129. if (!timing->emem_data)
  130. return -ENOMEM;
  131. err = of_property_read_u32_array(node, "nvidia,emem-configuration",
  132. timing->emem_data,
  133. mc->soc->num_emem_regs);
  134. if (err) {
  135. dev_err(mc->dev,
  136. "timing %s: failed to read EMEM configuration\n",
  137. node->name);
  138. return err;
  139. }
  140. return 0;
  141. }
  142. static int load_timings(struct tegra_mc *mc, struct device_node *node)
  143. {
  144. struct device_node *child;
  145. struct tegra_mc_timing *timing;
  146. int child_count = of_get_child_count(node);
  147. int i = 0, err;
  148. mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
  149. GFP_KERNEL);
  150. if (!mc->timings)
  151. return -ENOMEM;
  152. mc->num_timings = child_count;
  153. for_each_child_of_node(node, child) {
  154. timing = &mc->timings[i++];
  155. err = load_one_timing(mc, timing, child);
  156. if (err) {
  157. of_node_put(child);
  158. return err;
  159. }
  160. }
  161. return 0;
  162. }
  163. static int tegra_mc_setup_timings(struct tegra_mc *mc)
  164. {
  165. struct device_node *node;
  166. u32 ram_code, node_ram_code;
  167. int err;
  168. ram_code = tegra_read_ram_code();
  169. mc->num_timings = 0;
  170. for_each_child_of_node(mc->dev->of_node, node) {
  171. err = of_property_read_u32(node, "nvidia,ram-code",
  172. &node_ram_code);
  173. if (err || (node_ram_code != ram_code))
  174. continue;
  175. err = load_timings(mc, node);
  176. of_node_put(node);
  177. if (err)
  178. return err;
  179. break;
  180. }
  181. if (mc->num_timings == 0)
  182. dev_warn(mc->dev,
  183. "no memory timings for RAM code %u registered\n",
  184. ram_code);
  185. return 0;
  186. }
  187. static const char *const status_names[32] = {
  188. [ 1] = "External interrupt",
  189. [ 6] = "EMEM address decode error",
  190. [ 8] = "Security violation",
  191. [ 9] = "EMEM arbitration error",
  192. [10] = "Page fault",
  193. [11] = "Invalid APB ASID update",
  194. [12] = "VPR violation",
  195. [13] = "Secure carveout violation",
  196. [16] = "MTS carveout violation",
  197. };
  198. static const char *const error_names[8] = {
  199. [2] = "EMEM decode error",
  200. [3] = "TrustZone violation",
  201. [4] = "Carveout violation",
  202. [6] = "SMMU translation error",
  203. };
  204. static irqreturn_t tegra_mc_irq(int irq, void *data)
  205. {
  206. struct tegra_mc *mc = data;
  207. unsigned long status, mask;
  208. unsigned int bit;
  209. /* mask all interrupts to avoid flooding */
  210. status = mc_readl(mc, MC_INTSTATUS);
  211. mask = mc_readl(mc, MC_INTMASK);
  212. for_each_set_bit(bit, &status, 32) {
  213. const char *error = status_names[bit] ?: "unknown";
  214. const char *client = "unknown", *desc;
  215. const char *direction, *secure;
  216. phys_addr_t addr = 0;
  217. unsigned int i;
  218. char perm[7];
  219. u8 id, type;
  220. u32 value;
  221. value = mc_readl(mc, MC_ERR_STATUS);
  222. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  223. if (mc->soc->num_address_bits > 32) {
  224. addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
  225. MC_ERR_STATUS_ADR_HI_MASK);
  226. addr <<= 32;
  227. }
  228. #endif
  229. if (value & MC_ERR_STATUS_RW)
  230. direction = "write";
  231. else
  232. direction = "read";
  233. if (value & MC_ERR_STATUS_SECURITY)
  234. secure = "secure ";
  235. else
  236. secure = "";
  237. id = value & mc->soc->client_id_mask;
  238. for (i = 0; i < mc->soc->num_clients; i++) {
  239. if (mc->soc->clients[i].id == id) {
  240. client = mc->soc->clients[i].name;
  241. break;
  242. }
  243. }
  244. type = (value & MC_ERR_STATUS_TYPE_MASK) >>
  245. MC_ERR_STATUS_TYPE_SHIFT;
  246. desc = error_names[type];
  247. switch (value & MC_ERR_STATUS_TYPE_MASK) {
  248. case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
  249. perm[0] = ' ';
  250. perm[1] = '[';
  251. if (value & MC_ERR_STATUS_READABLE)
  252. perm[2] = 'R';
  253. else
  254. perm[2] = '-';
  255. if (value & MC_ERR_STATUS_WRITABLE)
  256. perm[3] = 'W';
  257. else
  258. perm[3] = '-';
  259. if (value & MC_ERR_STATUS_NONSECURE)
  260. perm[4] = '-';
  261. else
  262. perm[4] = 'S';
  263. perm[5] = ']';
  264. perm[6] = '\0';
  265. break;
  266. default:
  267. perm[0] = '\0';
  268. break;
  269. }
  270. value = mc_readl(mc, MC_ERR_ADR);
  271. addr |= value;
  272. dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
  273. client, secure, direction, &addr, error,
  274. desc, perm);
  275. }
  276. /* clear interrupts */
  277. mc_writel(mc, status, MC_INTSTATUS);
  278. return IRQ_HANDLED;
  279. }
  280. static int tegra_mc_probe(struct platform_device *pdev)
  281. {
  282. const struct of_device_id *match;
  283. struct resource *res;
  284. struct tegra_mc *mc;
  285. u32 value;
  286. int err;
  287. match = of_match_node(tegra_mc_of_match, pdev->dev.of_node);
  288. if (!match)
  289. return -ENODEV;
  290. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  291. if (!mc)
  292. return -ENOMEM;
  293. platform_set_drvdata(pdev, mc);
  294. mc->soc = match->data;
  295. mc->dev = &pdev->dev;
  296. /* length of MC tick in nanoseconds */
  297. mc->tick = 30;
  298. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  299. mc->regs = devm_ioremap_resource(&pdev->dev, res);
  300. if (IS_ERR(mc->regs))
  301. return PTR_ERR(mc->regs);
  302. mc->clk = devm_clk_get(&pdev->dev, "mc");
  303. if (IS_ERR(mc->clk)) {
  304. dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
  305. PTR_ERR(mc->clk));
  306. return PTR_ERR(mc->clk);
  307. }
  308. err = tegra_mc_setup_latency_allowance(mc);
  309. if (err < 0) {
  310. dev_err(&pdev->dev, "failed to setup latency allowance: %d\n",
  311. err);
  312. return err;
  313. }
  314. err = tegra_mc_setup_timings(mc);
  315. if (err < 0) {
  316. dev_err(&pdev->dev, "failed to setup timings: %d\n", err);
  317. return err;
  318. }
  319. if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU)) {
  320. mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
  321. if (IS_ERR(mc->smmu)) {
  322. dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
  323. PTR_ERR(mc->smmu));
  324. return PTR_ERR(mc->smmu);
  325. }
  326. }
  327. mc->irq = platform_get_irq(pdev, 0);
  328. if (mc->irq < 0) {
  329. dev_err(&pdev->dev, "interrupt not specified\n");
  330. return mc->irq;
  331. }
  332. err = devm_request_irq(&pdev->dev, mc->irq, tegra_mc_irq, IRQF_SHARED,
  333. dev_name(&pdev->dev), mc);
  334. if (err < 0) {
  335. dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
  336. err);
  337. return err;
  338. }
  339. WARN(!mc->soc->client_id_mask, "Missing client ID mask for this SoC\n");
  340. value = MC_INT_DECERR_MTS | MC_INT_SECERR_SEC | MC_INT_DECERR_VPR |
  341. MC_INT_INVALID_APB_ASID_UPDATE | MC_INT_INVALID_SMMU_PAGE |
  342. MC_INT_SECURITY_VIOLATION | MC_INT_DECERR_EMEM;
  343. mc_writel(mc, value, MC_INTMASK);
  344. return 0;
  345. }
  346. static struct platform_driver tegra_mc_driver = {
  347. .driver = {
  348. .name = "tegra-mc",
  349. .of_match_table = tegra_mc_of_match,
  350. .suppress_bind_attrs = true,
  351. },
  352. .prevent_deferred_probe = true,
  353. .probe = tegra_mc_probe,
  354. };
  355. static int tegra_mc_init(void)
  356. {
  357. return platform_driver_register(&tegra_mc_driver);
  358. }
  359. arch_initcall(tegra_mc_init);
  360. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  361. MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
  362. MODULE_LICENSE("GPL v2");