mc.c 12 KB

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