tegra20-mc.c 5.8 KB

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