tegra20-mc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Tegra20 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/err.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/ratelimit.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #define DRV_NAME "tegra20-mc"
  27. #define MC_INTSTATUS 0x0
  28. #define MC_INTMASK 0x4
  29. #define MC_INT_ERR_SHIFT 6
  30. #define MC_INT_ERR_MASK (0x1f << MC_INT_ERR_SHIFT)
  31. #define MC_INT_DECERR_EMEM BIT(MC_INT_ERR_SHIFT)
  32. #define MC_INT_INVALID_GART_PAGE BIT(MC_INT_ERR_SHIFT + 1)
  33. #define MC_INT_SECURITY_VIOLATION BIT(MC_INT_ERR_SHIFT + 2)
  34. #define MC_INT_ARBITRATION_EMEM BIT(MC_INT_ERR_SHIFT + 3)
  35. #define MC_GART_ERROR_REQ 0x30
  36. #define MC_DECERR_EMEM_OTHERS_STATUS 0x58
  37. #define MC_SECURITY_VIOLATION_STATUS 0x74
  38. #define SECURITY_VIOLATION_TYPE BIT(30) /* 0=TRUSTZONE, 1=CARVEOUT */
  39. #define MC_CLIENT_ID_MASK 0x3f
  40. #define NUM_MC_REG_BANKS 2
  41. struct tegra20_mc {
  42. void __iomem *regs[NUM_MC_REG_BANKS];
  43. struct device *dev;
  44. };
  45. static inline u32 mc_readl(struct tegra20_mc *mc, u32 offs)
  46. {
  47. u32 val = 0;
  48. if (offs < 0x24)
  49. val = readl(mc->regs[0] + offs);
  50. else if (offs < 0x400)
  51. val = readl(mc->regs[1] + offs - 0x3c);
  52. return val;
  53. }
  54. static inline void mc_writel(struct tegra20_mc *mc, u32 val, u32 offs)
  55. {
  56. if (offs < 0x24)
  57. writel(val, mc->regs[0] + offs);
  58. else if (offs < 0x400)
  59. writel(val, mc->regs[1] + offs - 0x3c);
  60. }
  61. static const char * const tegra20_mc_client[] = {
  62. "cbr_display0a",
  63. "cbr_display0ab",
  64. "cbr_display0b",
  65. "cbr_display0bb",
  66. "cbr_display0c",
  67. "cbr_display0cb",
  68. "cbr_display1b",
  69. "cbr_display1bb",
  70. "cbr_eppup",
  71. "cbr_g2pr",
  72. "cbr_g2sr",
  73. "cbr_mpeunifbr",
  74. "cbr_viruv",
  75. "csr_avpcarm7r",
  76. "csr_displayhc",
  77. "csr_displayhcb",
  78. "csr_fdcdrd",
  79. "csr_g2dr",
  80. "csr_host1xdmar",
  81. "csr_host1xr",
  82. "csr_idxsrd",
  83. "csr_mpcorer",
  84. "csr_mpe_ipred",
  85. "csr_mpeamemrd",
  86. "csr_mpecsrd",
  87. "csr_ppcsahbdmar",
  88. "csr_ppcsahbslvr",
  89. "csr_texsrd",
  90. "csr_vdebsevr",
  91. "csr_vdember",
  92. "csr_vdemcer",
  93. "csr_vdetper",
  94. "cbw_eppu",
  95. "cbw_eppv",
  96. "cbw_eppy",
  97. "cbw_mpeunifbw",
  98. "cbw_viwsb",
  99. "cbw_viwu",
  100. "cbw_viwv",
  101. "cbw_viwy",
  102. "ccw_g2dw",
  103. "csw_avpcarm7w",
  104. "csw_fdcdwr",
  105. "csw_host1xw",
  106. "csw_ispw",
  107. "csw_mpcorew",
  108. "csw_mpecswr",
  109. "csw_ppcsahbdmaw",
  110. "csw_ppcsahbslvw",
  111. "csw_vdebsevw",
  112. "csw_vdembew",
  113. "csw_vdetpmw",
  114. };
  115. static void tegra20_mc_decode(struct tegra20_mc *mc, int n)
  116. {
  117. u32 addr, req;
  118. const char *client = "Unknown";
  119. int idx, cid;
  120. const struct reg_info {
  121. u32 offset;
  122. u32 write_bit; /* 0=READ, 1=WRITE */
  123. int cid_shift;
  124. char *message;
  125. } reg[] = {
  126. {
  127. .offset = MC_DECERR_EMEM_OTHERS_STATUS,
  128. .write_bit = 31,
  129. .message = "MC_DECERR",
  130. },
  131. {
  132. .offset = MC_GART_ERROR_REQ,
  133. .cid_shift = 1,
  134. .message = "MC_GART_ERR",
  135. },
  136. {
  137. .offset = MC_SECURITY_VIOLATION_STATUS,
  138. .write_bit = 31,
  139. .message = "MC_SECURITY_ERR",
  140. },
  141. };
  142. idx = n - MC_INT_ERR_SHIFT;
  143. if ((idx < 0) || (idx >= ARRAY_SIZE(reg))) {
  144. dev_err_ratelimited(mc->dev, "Unknown interrupt status %08lx\n",
  145. BIT(n));
  146. return;
  147. }
  148. req = mc_readl(mc, reg[idx].offset);
  149. cid = (req >> reg[idx].cid_shift) & MC_CLIENT_ID_MASK;
  150. if (cid < ARRAY_SIZE(tegra20_mc_client))
  151. client = tegra20_mc_client[cid];
  152. addr = mc_readl(mc, reg[idx].offset + sizeof(u32));
  153. dev_err_ratelimited(mc->dev, "%s (0x%08x): 0x%08x %s (%s %s)\n",
  154. reg[idx].message, req, addr, client,
  155. (req & BIT(reg[idx].write_bit)) ? "write" : "read",
  156. (reg[idx].offset == MC_SECURITY_VIOLATION_STATUS) ?
  157. ((req & SECURITY_VIOLATION_TYPE) ?
  158. "carveout" : "trustzone") : "");
  159. }
  160. static const struct of_device_id tegra20_mc_of_match[] = {
  161. { .compatible = "nvidia,tegra20-mc", },
  162. {},
  163. };
  164. static irqreturn_t tegra20_mc_isr(int irq, void *data)
  165. {
  166. u32 stat, mask, bit;
  167. struct tegra20_mc *mc = data;
  168. stat = mc_readl(mc, MC_INTSTATUS);
  169. mask = mc_readl(mc, MC_INTMASK);
  170. mask &= stat;
  171. if (!mask)
  172. return IRQ_NONE;
  173. while ((bit = ffs(mask)) != 0) {
  174. tegra20_mc_decode(mc, bit - 1);
  175. mask &= ~BIT(bit - 1);
  176. }
  177. mc_writel(mc, stat, MC_INTSTATUS);
  178. return IRQ_HANDLED;
  179. }
  180. static int tegra20_mc_probe(struct platform_device *pdev)
  181. {
  182. struct resource *irq;
  183. struct tegra20_mc *mc;
  184. int i, err;
  185. u32 intmask;
  186. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  187. if (!mc)
  188. return -ENOMEM;
  189. mc->dev = &pdev->dev;
  190. for (i = 0; i < ARRAY_SIZE(mc->regs); i++) {
  191. struct resource *res;
  192. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  193. mc->regs[i] = devm_ioremap_resource(&pdev->dev, res);
  194. if (IS_ERR(mc->regs[i]))
  195. return PTR_ERR(mc->regs[i]);
  196. }
  197. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  198. if (!irq)
  199. return -ENODEV;
  200. err = devm_request_irq(&pdev->dev, irq->start, tegra20_mc_isr,
  201. IRQF_SHARED, dev_name(&pdev->dev), mc);
  202. if (err)
  203. return -ENODEV;
  204. platform_set_drvdata(pdev, mc);
  205. intmask = MC_INT_INVALID_GART_PAGE |
  206. MC_INT_DECERR_EMEM | MC_INT_SECURITY_VIOLATION;
  207. mc_writel(mc, intmask, MC_INTMASK);
  208. return 0;
  209. }
  210. static struct platform_driver tegra20_mc_driver = {
  211. .probe = tegra20_mc_probe,
  212. .driver = {
  213. .name = DRV_NAME,
  214. .of_match_table = tegra20_mc_of_match,
  215. },
  216. };
  217. module_platform_driver(tegra20_mc_driver);
  218. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  219. MODULE_DESCRIPTION("Tegra20 MC driver");
  220. MODULE_LICENSE("GPL v2");
  221. MODULE_ALIAS("platform:" DRV_NAME);