tegra30-mc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Tegra30 Memory Controller
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/ratelimit.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #define DRV_NAME "tegra30-mc"
  26. #define MC_INTSTATUS 0x0
  27. #define MC_INTMASK 0x4
  28. #define MC_INT_ERR_SHIFT 6
  29. #define MC_INT_ERR_MASK (0x1f << MC_INT_ERR_SHIFT)
  30. #define MC_INT_DECERR_EMEM BIT(MC_INT_ERR_SHIFT)
  31. #define MC_INT_SECURITY_VIOLATION BIT(MC_INT_ERR_SHIFT + 2)
  32. #define MC_INT_ARBITRATION_EMEM BIT(MC_INT_ERR_SHIFT + 3)
  33. #define MC_INT_INVALID_SMMU_PAGE BIT(MC_INT_ERR_SHIFT + 4)
  34. #define MC_ERR_STATUS 0x8
  35. #define MC_ERR_ADR 0xc
  36. #define MC_ERR_TYPE_SHIFT 28
  37. #define MC_ERR_TYPE_MASK (7 << MC_ERR_TYPE_SHIFT)
  38. #define MC_ERR_TYPE_DECERR_EMEM 2
  39. #define MC_ERR_TYPE_SECURITY_TRUSTZONE 3
  40. #define MC_ERR_TYPE_SECURITY_CARVEOUT 4
  41. #define MC_ERR_TYPE_INVALID_SMMU_PAGE 6
  42. #define MC_ERR_INVALID_SMMU_PAGE_SHIFT 25
  43. #define MC_ERR_INVALID_SMMU_PAGE_MASK (7 << MC_ERR_INVALID_SMMU_PAGE_SHIFT)
  44. #define MC_ERR_RW_SHIFT 16
  45. #define MC_ERR_RW BIT(MC_ERR_RW_SHIFT)
  46. #define MC_ERR_SECURITY BIT(MC_ERR_RW_SHIFT + 1)
  47. #define SECURITY_VIOLATION_TYPE BIT(30) /* 0=TRUSTZONE, 1=CARVEOUT */
  48. #define MC_EMEM_ARB_CFG 0x90
  49. #define MC_EMEM_ARB_OUTSTANDING_REQ 0x94
  50. #define MC_EMEM_ARB_TIMING_RCD 0x98
  51. #define MC_EMEM_ARB_TIMING_RP 0x9c
  52. #define MC_EMEM_ARB_TIMING_RC 0xa0
  53. #define MC_EMEM_ARB_TIMING_RAS 0xa4
  54. #define MC_EMEM_ARB_TIMING_FAW 0xa8
  55. #define MC_EMEM_ARB_TIMING_RRD 0xac
  56. #define MC_EMEM_ARB_TIMING_RAP2PRE 0xb0
  57. #define MC_EMEM_ARB_TIMING_WAP2PRE 0xb4
  58. #define MC_EMEM_ARB_TIMING_R2R 0xb8
  59. #define MC_EMEM_ARB_TIMING_W2W 0xbc
  60. #define MC_EMEM_ARB_TIMING_R2W 0xc0
  61. #define MC_EMEM_ARB_TIMING_W2R 0xc4
  62. #define MC_EMEM_ARB_DA_TURNS 0xd0
  63. #define MC_EMEM_ARB_DA_COVERS 0xd4
  64. #define MC_EMEM_ARB_MISC0 0xd8
  65. #define MC_EMEM_ARB_MISC1 0xdc
  66. #define MC_EMEM_ARB_RING3_THROTTLE 0xe4
  67. #define MC_EMEM_ARB_OVERRIDE 0xe8
  68. #define MC_TIMING_CONTROL 0xfc
  69. #define MC_CLIENT_ID_MASK 0x7f
  70. #define NUM_MC_REG_BANKS 4
  71. struct tegra30_mc {
  72. void __iomem *regs[NUM_MC_REG_BANKS];
  73. struct device *dev;
  74. u32 ctx[0];
  75. };
  76. static inline u32 mc_readl(struct tegra30_mc *mc, u32 offs)
  77. {
  78. if (offs < 0x10)
  79. return readl(mc->regs[0] + offs);
  80. BUG_ON(offs < 0x3c);
  81. if (offs < 0x1f0)
  82. return readl(mc->regs[1] + offs - 0x3c);
  83. BUG_ON(offs < 0x200);
  84. if (offs < 0x228)
  85. return readl(mc->regs[2] + offs - 0x200);
  86. BUG_ON(offs < 0x284);
  87. if (offs < 0x400)
  88. return readl(mc->regs[3] + offs - 0x284);
  89. BUG();
  90. }
  91. static inline void mc_writel(struct tegra30_mc *mc, u32 val, u32 offs)
  92. {
  93. if (offs < 0x10) {
  94. writel(val, mc->regs[0] + offs);
  95. return;
  96. }
  97. BUG_ON(offs < 0x3c);
  98. if (offs < 0x1f0) {
  99. writel(val, mc->regs[1] + offs - 0x3c);
  100. return;
  101. }
  102. BUG_ON(offs < 0x200);
  103. if (offs < 0x228) {
  104. writel(val, mc->regs[2] + offs - 0x200);
  105. return;
  106. }
  107. BUG_ON(offs < 0x284);
  108. if (offs < 0x400) {
  109. writel(val, mc->regs[3] + offs - 0x284);
  110. return;
  111. }
  112. BUG();
  113. }
  114. static const char * const tegra30_mc_client[] = {
  115. "csr_ptcr",
  116. "cbr_display0a",
  117. "cbr_display0ab",
  118. "cbr_display0b",
  119. "cbr_display0bb",
  120. "cbr_display0c",
  121. "cbr_display0cb",
  122. "cbr_display1b",
  123. "cbr_display1bb",
  124. "cbr_eppup",
  125. "cbr_g2pr",
  126. "cbr_g2sr",
  127. "cbr_mpeunifbr",
  128. "cbr_viruv",
  129. "csr_afir",
  130. "csr_avpcarm7r",
  131. "csr_displayhc",
  132. "csr_displayhcb",
  133. "csr_fdcdrd",
  134. "csr_fdcdrd2",
  135. "csr_g2dr",
  136. "csr_hdar",
  137. "csr_host1xdmar",
  138. "csr_host1xr",
  139. "csr_idxsrd",
  140. "csr_idxsrd2",
  141. "csr_mpe_ipred",
  142. "csr_mpeamemrd",
  143. "csr_mpecsrd",
  144. "csr_ppcsahbdmar",
  145. "csr_ppcsahbslvr",
  146. "csr_satar",
  147. "csr_texsrd",
  148. "csr_texsrd2",
  149. "csr_vdebsevr",
  150. "csr_vdember",
  151. "csr_vdemcer",
  152. "csr_vdetper",
  153. "csr_mpcorelpr",
  154. "csr_mpcorer",
  155. "cbw_eppu",
  156. "cbw_eppv",
  157. "cbw_eppy",
  158. "cbw_mpeunifbw",
  159. "cbw_viwsb",
  160. "cbw_viwu",
  161. "cbw_viwv",
  162. "cbw_viwy",
  163. "ccw_g2dw",
  164. "csw_afiw",
  165. "csw_avpcarm7w",
  166. "csw_fdcdwr",
  167. "csw_fdcdwr2",
  168. "csw_hdaw",
  169. "csw_host1xw",
  170. "csw_ispw",
  171. "csw_mpcorelpw",
  172. "csw_mpcorew",
  173. "csw_mpecswr",
  174. "csw_ppcsahbdmaw",
  175. "csw_ppcsahbslvw",
  176. "csw_sataw",
  177. "csw_vdebsevw",
  178. "csw_vdedbgw",
  179. "csw_vdembew",
  180. "csw_vdetpmw",
  181. };
  182. static void tegra30_mc_decode(struct tegra30_mc *mc, int n)
  183. {
  184. u32 err, addr;
  185. const char * const mc_int_err[] = {
  186. "MC_DECERR",
  187. "Unknown",
  188. "MC_SECURITY_ERR",
  189. "MC_ARBITRATION_EMEM",
  190. "MC_SMMU_ERR",
  191. };
  192. const char * const err_type[] = {
  193. "Unknown",
  194. "Unknown",
  195. "DECERR_EMEM",
  196. "SECURITY_TRUSTZONE",
  197. "SECURITY_CARVEOUT",
  198. "Unknown",
  199. "INVALID_SMMU_PAGE",
  200. "Unknown",
  201. };
  202. char attr[6];
  203. int cid, perm, type, idx;
  204. const char *client = "Unknown";
  205. idx = n - MC_INT_ERR_SHIFT;
  206. if ((idx < 0) || (idx >= ARRAY_SIZE(mc_int_err)) || (idx == 1)) {
  207. pr_err_ratelimited("Unknown interrupt status %08lx\n", BIT(n));
  208. return;
  209. }
  210. err = readl(mc + MC_ERR_STATUS);
  211. type = (err & MC_ERR_TYPE_MASK) >> MC_ERR_TYPE_SHIFT;
  212. perm = (err & MC_ERR_INVALID_SMMU_PAGE_MASK) >>
  213. MC_ERR_INVALID_SMMU_PAGE_SHIFT;
  214. if (type == MC_ERR_TYPE_INVALID_SMMU_PAGE)
  215. sprintf(attr, "%c-%c-%c",
  216. (perm & BIT(2)) ? 'R' : '-',
  217. (perm & BIT(1)) ? 'W' : '-',
  218. (perm & BIT(0)) ? 'S' : '-');
  219. else
  220. attr[0] = '\0';
  221. cid = err & MC_CLIENT_ID_MASK;
  222. if (cid < ARRAY_SIZE(tegra30_mc_client))
  223. client = tegra30_mc_client[cid];
  224. addr = readl(mc + MC_ERR_ADR);
  225. pr_err_ratelimited("%s (0x%08x): 0x%08x %s (%s %s %s %s)\n",
  226. mc_int_err[idx], err, addr, client,
  227. (err & MC_ERR_SECURITY) ? "secure" : "non-secure",
  228. (err & MC_ERR_RW) ? "write" : "read",
  229. err_type[type], attr);
  230. }
  231. static const u32 tegra30_mc_ctx[] = {
  232. MC_EMEM_ARB_CFG,
  233. MC_EMEM_ARB_OUTSTANDING_REQ,
  234. MC_EMEM_ARB_TIMING_RCD,
  235. MC_EMEM_ARB_TIMING_RP,
  236. MC_EMEM_ARB_TIMING_RC,
  237. MC_EMEM_ARB_TIMING_RAS,
  238. MC_EMEM_ARB_TIMING_FAW,
  239. MC_EMEM_ARB_TIMING_RRD,
  240. MC_EMEM_ARB_TIMING_RAP2PRE,
  241. MC_EMEM_ARB_TIMING_WAP2PRE,
  242. MC_EMEM_ARB_TIMING_R2R,
  243. MC_EMEM_ARB_TIMING_W2W,
  244. MC_EMEM_ARB_TIMING_R2W,
  245. MC_EMEM_ARB_TIMING_W2R,
  246. MC_EMEM_ARB_DA_TURNS,
  247. MC_EMEM_ARB_DA_COVERS,
  248. MC_EMEM_ARB_MISC0,
  249. MC_EMEM_ARB_MISC1,
  250. MC_EMEM_ARB_RING3_THROTTLE,
  251. MC_EMEM_ARB_OVERRIDE,
  252. MC_INTMASK,
  253. };
  254. static int tegra30_mc_suspend(struct device *dev)
  255. {
  256. int i;
  257. struct tegra30_mc *mc = dev_get_drvdata(dev);
  258. for (i = 0; i < ARRAY_SIZE(tegra30_mc_ctx); i++)
  259. mc->ctx[i] = mc_readl(mc, tegra30_mc_ctx[i]);
  260. return 0;
  261. }
  262. static int tegra30_mc_resume(struct device *dev)
  263. {
  264. int i;
  265. struct tegra30_mc *mc = dev_get_drvdata(dev);
  266. for (i = 0; i < ARRAY_SIZE(tegra30_mc_ctx); i++)
  267. mc_writel(mc, mc->ctx[i], tegra30_mc_ctx[i]);
  268. mc_writel(mc, 1, MC_TIMING_CONTROL);
  269. /* Read-back to ensure that write reached */
  270. mc_readl(mc, MC_TIMING_CONTROL);
  271. return 0;
  272. }
  273. static UNIVERSAL_DEV_PM_OPS(tegra30_mc_pm,
  274. tegra30_mc_suspend,
  275. tegra30_mc_resume, NULL);
  276. static const struct of_device_id tegra30_mc_of_match[] __devinitconst = {
  277. { .compatible = "nvidia,tegra30-mc", },
  278. {},
  279. };
  280. static irqreturn_t tegra30_mc_isr(int irq, void *data)
  281. {
  282. u32 stat, mask, bit;
  283. struct tegra30_mc *mc = data;
  284. stat = mc_readl(mc, MC_INTSTATUS);
  285. mask = mc_readl(mc, MC_INTMASK);
  286. mask &= stat;
  287. if (!mask)
  288. return IRQ_NONE;
  289. while ((bit = ffs(mask)) != 0)
  290. tegra30_mc_decode(mc, bit - 1);
  291. mc_writel(mc, stat, MC_INTSTATUS);
  292. return IRQ_HANDLED;
  293. }
  294. static int __devinit tegra30_mc_probe(struct platform_device *pdev)
  295. {
  296. struct resource *irq;
  297. struct tegra30_mc *mc;
  298. size_t bytes;
  299. int err, i;
  300. u32 intmask;
  301. bytes = sizeof(*mc) + sizeof(u32) * ARRAY_SIZE(tegra30_mc_ctx);
  302. mc = devm_kzalloc(&pdev->dev, bytes, GFP_KERNEL);
  303. if (!mc)
  304. return -ENOMEM;
  305. mc->dev = &pdev->dev;
  306. for (i = 0; i < ARRAY_SIZE(mc->regs); i++) {
  307. struct resource *res;
  308. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  309. if (!res)
  310. return -ENODEV;
  311. mc->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
  312. if (!mc->regs[i])
  313. return -EBUSY;
  314. }
  315. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  316. if (!irq)
  317. return -ENODEV;
  318. err = devm_request_irq(&pdev->dev, irq->start, tegra30_mc_isr,
  319. IRQF_SHARED, dev_name(&pdev->dev), mc);
  320. if (err)
  321. return -ENODEV;
  322. platform_set_drvdata(pdev, mc);
  323. intmask = MC_INT_INVALID_SMMU_PAGE |
  324. MC_INT_DECERR_EMEM | MC_INT_SECURITY_VIOLATION;
  325. mc_writel(mc, intmask, MC_INTMASK);
  326. return 0;
  327. }
  328. static int __devexit tegra30_mc_remove(struct platform_device *pdev)
  329. {
  330. return 0;
  331. }
  332. static struct platform_driver tegra30_mc_driver = {
  333. .probe = tegra30_mc_probe,
  334. .remove = __devexit_p(tegra30_mc_remove),
  335. .driver = {
  336. .name = DRV_NAME,
  337. .owner = THIS_MODULE,
  338. .of_match_table = tegra30_mc_of_match,
  339. .pm = &tegra30_mc_pm,
  340. },
  341. };
  342. module_platform_driver(tegra30_mc_driver);
  343. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  344. MODULE_DESCRIPTION("Tegra30 MC driver");
  345. MODULE_LICENSE("GPL v2");
  346. MODULE_ALIAS("platform:" DRV_NAME);