mc.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014-2025 NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/export.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/sort.h>
  17. #include <linux/tegra-icc.h>
  18. #include <soc/tegra/fuse.h>
  19. #include "mc.h"
  20. static const struct of_device_id tegra_mc_of_match[] = {
  21. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  22. { .compatible = "nvidia,tegra20-mc-gart", .data = &tegra20_mc_soc },
  23. #endif
  24. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  25. { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
  26. #endif
  27. #ifdef CONFIG_ARCH_TEGRA_114_SOC
  28. { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
  29. #endif
  30. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  31. { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
  32. #endif
  33. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  34. { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
  35. #endif
  36. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  37. { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
  38. #endif
  39. #ifdef CONFIG_ARCH_TEGRA_186_SOC
  40. { .compatible = "nvidia,tegra186-mc", .data = &tegra186_mc_soc },
  41. #endif
  42. #ifdef CONFIG_ARCH_TEGRA_194_SOC
  43. { .compatible = "nvidia,tegra194-mc", .data = &tegra194_mc_soc },
  44. #endif
  45. #ifdef CONFIG_ARCH_TEGRA_234_SOC
  46. { .compatible = "nvidia,tegra234-mc", .data = &tegra234_mc_soc },
  47. #endif
  48. #ifdef CONFIG_ARCH_TEGRA_264_SOC
  49. { .compatible = "nvidia,tegra264-mc", .data = &tegra264_mc_soc },
  50. #endif
  51. { /* sentinel */ }
  52. };
  53. MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
  54. static void tegra_mc_devm_action_put_device(void *data)
  55. {
  56. struct tegra_mc *mc = data;
  57. put_device(mc->dev);
  58. }
  59. /**
  60. * devm_tegra_memory_controller_get() - get Tegra Memory Controller handle
  61. * @dev: device pointer for the consumer device
  62. *
  63. * This function will search for the Memory Controller node in a device-tree
  64. * and retrieve the Memory Controller handle.
  65. *
  66. * Return: ERR_PTR() on error or a valid pointer to a struct tegra_mc.
  67. */
  68. struct tegra_mc *devm_tegra_memory_controller_get(struct device *dev)
  69. {
  70. struct platform_device *pdev;
  71. struct device_node *np;
  72. struct tegra_mc *mc;
  73. int err;
  74. np = of_parse_phandle(dev->of_node, "nvidia,memory-controller", 0);
  75. if (!np)
  76. return ERR_PTR(-ENOENT);
  77. pdev = of_find_device_by_node(np);
  78. of_node_put(np);
  79. if (!pdev)
  80. return ERR_PTR(-ENODEV);
  81. mc = platform_get_drvdata(pdev);
  82. if (!mc) {
  83. put_device(&pdev->dev);
  84. return ERR_PTR(-EPROBE_DEFER);
  85. }
  86. err = devm_add_action_or_reset(dev, tegra_mc_devm_action_put_device, mc);
  87. if (err)
  88. return ERR_PTR(err);
  89. return mc;
  90. }
  91. EXPORT_SYMBOL_GPL(devm_tegra_memory_controller_get);
  92. int tegra_mc_probe_device(struct tegra_mc *mc, struct device *dev)
  93. {
  94. if (mc->soc->ops && mc->soc->ops->probe_device)
  95. return mc->soc->ops->probe_device(mc, dev);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL_GPL(tegra_mc_probe_device);
  99. int tegra_mc_get_carveout_info(struct tegra_mc *mc, unsigned int id,
  100. phys_addr_t *base, u64 *size)
  101. {
  102. u32 offset;
  103. if (id < 1 || id >= mc->soc->num_carveouts)
  104. return -EINVAL;
  105. if (id < 6)
  106. offset = 0xc0c + 0x50 * (id - 1);
  107. else
  108. offset = 0x2004 + 0x50 * (id - 6);
  109. *base = mc_ch_readl(mc, MC_BROADCAST_CHANNEL, offset + 0x0);
  110. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  111. *base |= (phys_addr_t)mc_ch_readl(mc, MC_BROADCAST_CHANNEL, offset + 0x4) << 32;
  112. #endif
  113. if (size)
  114. *size = mc_ch_readl(mc, MC_BROADCAST_CHANNEL, offset + 0x8) << 17;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(tegra_mc_get_carveout_info);
  118. static int tegra_mc_block_dma_common(struct tegra_mc *mc,
  119. const struct tegra_mc_reset *rst)
  120. {
  121. unsigned long flags;
  122. u32 value;
  123. spin_lock_irqsave(&mc->lock, flags);
  124. value = mc_readl(mc, rst->control) | BIT(rst->bit);
  125. mc_writel(mc, value, rst->control);
  126. spin_unlock_irqrestore(&mc->lock, flags);
  127. return 0;
  128. }
  129. static bool tegra_mc_dma_idling_common(struct tegra_mc *mc,
  130. const struct tegra_mc_reset *rst)
  131. {
  132. return (mc_readl(mc, rst->status) & BIT(rst->bit)) != 0;
  133. }
  134. static int tegra_mc_unblock_dma_common(struct tegra_mc *mc,
  135. const struct tegra_mc_reset *rst)
  136. {
  137. unsigned long flags;
  138. u32 value;
  139. spin_lock_irqsave(&mc->lock, flags);
  140. value = mc_readl(mc, rst->control) & ~BIT(rst->bit);
  141. mc_writel(mc, value, rst->control);
  142. spin_unlock_irqrestore(&mc->lock, flags);
  143. return 0;
  144. }
  145. static int tegra_mc_reset_status_common(struct tegra_mc *mc,
  146. const struct tegra_mc_reset *rst)
  147. {
  148. return (mc_readl(mc, rst->control) & BIT(rst->bit)) != 0;
  149. }
  150. const struct tegra_mc_reset_ops tegra_mc_reset_ops_common = {
  151. .block_dma = tegra_mc_block_dma_common,
  152. .dma_idling = tegra_mc_dma_idling_common,
  153. .unblock_dma = tegra_mc_unblock_dma_common,
  154. .reset_status = tegra_mc_reset_status_common,
  155. };
  156. static inline struct tegra_mc *reset_to_mc(struct reset_controller_dev *rcdev)
  157. {
  158. return container_of(rcdev, struct tegra_mc, reset);
  159. }
  160. static const struct tegra_mc_reset *tegra_mc_reset_find(struct tegra_mc *mc,
  161. unsigned long id)
  162. {
  163. unsigned int i;
  164. for (i = 0; i < mc->soc->num_resets; i++)
  165. if (mc->soc->resets[i].id == id)
  166. return &mc->soc->resets[i];
  167. return NULL;
  168. }
  169. static int tegra_mc_hotreset_assert(struct reset_controller_dev *rcdev,
  170. unsigned long id)
  171. {
  172. struct tegra_mc *mc = reset_to_mc(rcdev);
  173. const struct tegra_mc_reset_ops *rst_ops;
  174. const struct tegra_mc_reset *rst;
  175. int retries = 500;
  176. int err;
  177. rst = tegra_mc_reset_find(mc, id);
  178. if (!rst)
  179. return -ENODEV;
  180. rst_ops = mc->soc->reset_ops;
  181. if (!rst_ops)
  182. return -ENODEV;
  183. /* DMA flushing will fail if reset is already asserted */
  184. if (rst_ops->reset_status) {
  185. /* check whether reset is asserted */
  186. if (rst_ops->reset_status(mc, rst))
  187. return 0;
  188. }
  189. if (rst_ops->block_dma) {
  190. /* block clients DMA requests */
  191. err = rst_ops->block_dma(mc, rst);
  192. if (err) {
  193. dev_err(mc->dev, "failed to block %s DMA: %d\n",
  194. rst->name, err);
  195. return err;
  196. }
  197. }
  198. if (rst_ops->dma_idling) {
  199. /* wait for completion of the outstanding DMA requests */
  200. while (!rst_ops->dma_idling(mc, rst)) {
  201. if (!retries--) {
  202. dev_err(mc->dev, "failed to flush %s DMA\n",
  203. rst->name);
  204. return -EBUSY;
  205. }
  206. usleep_range(10, 100);
  207. }
  208. }
  209. if (rst_ops->hotreset_assert) {
  210. /* clear clients DMA requests sitting before arbitration */
  211. err = rst_ops->hotreset_assert(mc, rst);
  212. if (err) {
  213. dev_err(mc->dev, "failed to hot reset %s: %d\n",
  214. rst->name, err);
  215. return err;
  216. }
  217. }
  218. return 0;
  219. }
  220. static int tegra_mc_hotreset_deassert(struct reset_controller_dev *rcdev,
  221. unsigned long id)
  222. {
  223. struct tegra_mc *mc = reset_to_mc(rcdev);
  224. const struct tegra_mc_reset_ops *rst_ops;
  225. const struct tegra_mc_reset *rst;
  226. int err;
  227. rst = tegra_mc_reset_find(mc, id);
  228. if (!rst)
  229. return -ENODEV;
  230. rst_ops = mc->soc->reset_ops;
  231. if (!rst_ops)
  232. return -ENODEV;
  233. if (rst_ops->hotreset_deassert) {
  234. /* take out client from hot reset */
  235. err = rst_ops->hotreset_deassert(mc, rst);
  236. if (err) {
  237. dev_err(mc->dev, "failed to deassert hot reset %s: %d\n",
  238. rst->name, err);
  239. return err;
  240. }
  241. }
  242. if (rst_ops->unblock_dma) {
  243. /* allow new DMA requests to proceed to arbitration */
  244. err = rst_ops->unblock_dma(mc, rst);
  245. if (err) {
  246. dev_err(mc->dev, "failed to unblock %s DMA : %d\n",
  247. rst->name, err);
  248. return err;
  249. }
  250. }
  251. return 0;
  252. }
  253. static int tegra_mc_hotreset_status(struct reset_controller_dev *rcdev,
  254. unsigned long id)
  255. {
  256. struct tegra_mc *mc = reset_to_mc(rcdev);
  257. const struct tegra_mc_reset_ops *rst_ops;
  258. const struct tegra_mc_reset *rst;
  259. rst = tegra_mc_reset_find(mc, id);
  260. if (!rst)
  261. return -ENODEV;
  262. rst_ops = mc->soc->reset_ops;
  263. if (!rst_ops)
  264. return -ENODEV;
  265. return rst_ops->reset_status(mc, rst);
  266. }
  267. static const struct reset_control_ops tegra_mc_reset_ops = {
  268. .assert = tegra_mc_hotreset_assert,
  269. .deassert = tegra_mc_hotreset_deassert,
  270. .status = tegra_mc_hotreset_status,
  271. };
  272. static int tegra_mc_reset_setup(struct tegra_mc *mc)
  273. {
  274. int err;
  275. mc->reset.ops = &tegra_mc_reset_ops;
  276. mc->reset.owner = THIS_MODULE;
  277. mc->reset.of_node = mc->dev->of_node;
  278. mc->reset.of_reset_n_cells = 1;
  279. mc->reset.nr_resets = mc->soc->num_resets;
  280. err = reset_controller_register(&mc->reset);
  281. if (err < 0)
  282. return err;
  283. return 0;
  284. }
  285. int tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
  286. {
  287. unsigned int i;
  288. struct tegra_mc_timing *timing = NULL;
  289. for (i = 0; i < mc->num_timings; i++) {
  290. if (mc->timings[i].rate == rate) {
  291. timing = &mc->timings[i];
  292. break;
  293. }
  294. }
  295. if (!timing) {
  296. dev_err(mc->dev, "no memory timing registered for rate %lu\n",
  297. rate);
  298. return -EINVAL;
  299. }
  300. for (i = 0; i < mc->soc->num_emem_regs; ++i)
  301. mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
  302. return 0;
  303. }
  304. EXPORT_SYMBOL_GPL(tegra_mc_write_emem_configuration);
  305. unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
  306. {
  307. u8 dram_count;
  308. dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
  309. dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
  310. dram_count++;
  311. return dram_count;
  312. }
  313. EXPORT_SYMBOL_GPL(tegra_mc_get_emem_device_count);
  314. #if defined(CONFIG_ARCH_TEGRA_3x_SOC) || \
  315. defined(CONFIG_ARCH_TEGRA_114_SOC) || \
  316. defined(CONFIG_ARCH_TEGRA_124_SOC) || \
  317. defined(CONFIG_ARCH_TEGRA_132_SOC) || \
  318. defined(CONFIG_ARCH_TEGRA_210_SOC)
  319. static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
  320. {
  321. unsigned long long tick;
  322. unsigned int i;
  323. u32 value;
  324. /* compute the number of MC clock cycles per tick */
  325. tick = (unsigned long long)mc->tick * clk_get_rate(mc->clk);
  326. do_div(tick, NSEC_PER_SEC);
  327. value = mc_readl(mc, MC_EMEM_ARB_CFG);
  328. value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
  329. value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
  330. mc_writel(mc, value, MC_EMEM_ARB_CFG);
  331. /* write latency allowance defaults */
  332. for (i = 0; i < mc->soc->num_clients; i++) {
  333. const struct tegra_mc_client *client = &mc->soc->clients[i];
  334. u32 value;
  335. value = mc_readl(mc, client->regs.la.reg);
  336. value &= ~(client->regs.la.mask << client->regs.la.shift);
  337. value |= (client->regs.la.def & client->regs.la.mask) << client->regs.la.shift;
  338. mc_writel(mc, value, client->regs.la.reg);
  339. }
  340. /* latch new values */
  341. mc_writel(mc, MC_TIMING_UPDATE, MC_TIMING_CONTROL);
  342. return 0;
  343. }
  344. static int load_one_timing(struct tegra_mc *mc,
  345. struct tegra_mc_timing *timing,
  346. struct device_node *node)
  347. {
  348. int err;
  349. u32 tmp;
  350. err = of_property_read_u32(node, "clock-frequency", &tmp);
  351. if (err) {
  352. dev_err(mc->dev,
  353. "timing %pOFn: failed to read rate\n", node);
  354. return err;
  355. }
  356. timing->rate = tmp;
  357. timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
  358. sizeof(u32), GFP_KERNEL);
  359. if (!timing->emem_data)
  360. return -ENOMEM;
  361. err = of_property_read_u32_array(node, "nvidia,emem-configuration",
  362. timing->emem_data,
  363. mc->soc->num_emem_regs);
  364. if (err) {
  365. dev_err(mc->dev,
  366. "timing %pOFn: failed to read EMEM configuration\n",
  367. node);
  368. return err;
  369. }
  370. return 0;
  371. }
  372. static int load_timings(struct tegra_mc *mc, struct device_node *node)
  373. {
  374. struct tegra_mc_timing *timing;
  375. int child_count = of_get_child_count(node);
  376. int i = 0, err;
  377. mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
  378. GFP_KERNEL);
  379. if (!mc->timings)
  380. return -ENOMEM;
  381. mc->num_timings = child_count;
  382. for_each_child_of_node_scoped(node, child) {
  383. timing = &mc->timings[i++];
  384. err = load_one_timing(mc, timing, child);
  385. if (err)
  386. return err;
  387. }
  388. return 0;
  389. }
  390. static int tegra_mc_setup_timings(struct tegra_mc *mc)
  391. {
  392. u32 ram_code, node_ram_code;
  393. int err;
  394. ram_code = tegra_read_ram_code();
  395. mc->num_timings = 0;
  396. for_each_child_of_node_scoped(mc->dev->of_node, node) {
  397. err = of_property_read_u32(node, "nvidia,ram-code",
  398. &node_ram_code);
  399. if (err || (node_ram_code != ram_code))
  400. continue;
  401. err = load_timings(mc, node);
  402. if (err)
  403. return err;
  404. break;
  405. }
  406. if (mc->num_timings == 0)
  407. dev_warn(mc->dev,
  408. "no memory timings for RAM code %u registered\n",
  409. ram_code);
  410. return 0;
  411. }
  412. int tegra30_mc_probe(struct tegra_mc *mc)
  413. {
  414. int err;
  415. mc->clk = devm_clk_get_optional(mc->dev, "mc");
  416. if (IS_ERR(mc->clk)) {
  417. dev_err(mc->dev, "failed to get MC clock: %ld\n", PTR_ERR(mc->clk));
  418. return PTR_ERR(mc->clk);
  419. }
  420. /* ensure that debug features are disabled */
  421. mc_writel(mc, 0x00000000, MC_TIMING_CONTROL_DBG);
  422. err = tegra_mc_setup_latency_allowance(mc);
  423. if (err < 0) {
  424. dev_err(mc->dev, "failed to setup latency allowance: %d\n", err);
  425. return err;
  426. }
  427. err = tegra_mc_setup_timings(mc);
  428. if (err < 0) {
  429. dev_err(mc->dev, "failed to setup timings: %d\n", err);
  430. return err;
  431. }
  432. return 0;
  433. }
  434. const struct tegra_mc_ops tegra30_mc_ops = {
  435. .probe = tegra30_mc_probe,
  436. .handle_irq = tegra30_mc_handle_irq,
  437. };
  438. #endif
  439. static int mc_global_intstatus_to_channel(const struct tegra_mc *mc, u32 status,
  440. unsigned int *mc_channel)
  441. {
  442. if ((status & mc->soc->ch_intmask) == 0)
  443. return -EINVAL;
  444. *mc_channel = __ffs((status & mc->soc->ch_intmask) >>
  445. mc->soc->global_intstatus_channel_shift);
  446. return 0;
  447. }
  448. static u32 mc_channel_to_global_intstatus(const struct tegra_mc *mc,
  449. unsigned int channel)
  450. {
  451. return BIT(channel) << mc->soc->global_intstatus_channel_shift;
  452. }
  453. irqreturn_t tegra30_mc_handle_irq(int irq, void *data)
  454. {
  455. struct tegra_mc *mc = data;
  456. unsigned int bit, channel;
  457. unsigned long status;
  458. if (mc->soc->num_channels) {
  459. u32 global_status;
  460. int err;
  461. global_status = mc_ch_readl(mc, MC_BROADCAST_CHANNEL, MC_GLOBAL_INTSTATUS);
  462. err = mc_global_intstatus_to_channel(mc, global_status, &channel);
  463. if (err < 0) {
  464. dev_err_ratelimited(mc->dev, "unknown interrupt channel 0x%08x\n",
  465. global_status);
  466. return IRQ_NONE;
  467. }
  468. /* mask all interrupts to avoid flooding */
  469. status = mc_ch_readl(mc, channel, MC_INTSTATUS) & mc->soc->intmask;
  470. } else {
  471. status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
  472. }
  473. if (!status)
  474. return IRQ_NONE;
  475. for_each_set_bit(bit, &status, 32) {
  476. const char *error = tegra_mc_status_names[bit] ?: "unknown";
  477. const char *client = "unknown", *desc;
  478. const char *direction, *secure;
  479. u32 status_reg, addr_reg;
  480. u32 intmask = BIT(bit);
  481. phys_addr_t addr = 0;
  482. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  483. u32 addr_hi_reg = 0;
  484. #endif
  485. unsigned int i;
  486. char perm[7];
  487. u8 id, type;
  488. u32 value;
  489. switch (intmask) {
  490. case MC_INT_DECERR_VPR:
  491. status_reg = MC_ERR_VPR_STATUS;
  492. addr_reg = MC_ERR_VPR_ADR;
  493. break;
  494. case MC_INT_SECERR_SEC:
  495. status_reg = MC_ERR_SEC_STATUS;
  496. addr_reg = MC_ERR_SEC_ADR;
  497. break;
  498. case MC_INT_DECERR_MTS:
  499. status_reg = MC_ERR_MTS_STATUS;
  500. addr_reg = MC_ERR_MTS_ADR;
  501. break;
  502. case MC_INT_DECERR_GENERALIZED_CARVEOUT:
  503. status_reg = MC_ERR_GENERALIZED_CARVEOUT_STATUS;
  504. addr_reg = MC_ERR_GENERALIZED_CARVEOUT_ADR;
  505. break;
  506. case MC_INT_DECERR_ROUTE_SANITY:
  507. status_reg = MC_ERR_ROUTE_SANITY_STATUS;
  508. addr_reg = MC_ERR_ROUTE_SANITY_ADR;
  509. break;
  510. default:
  511. status_reg = MC_ERR_STATUS;
  512. addr_reg = MC_ERR_ADR;
  513. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  514. if (mc->soc->has_addr_hi_reg)
  515. addr_hi_reg = MC_ERR_ADR_HI;
  516. #endif
  517. break;
  518. }
  519. if (mc->soc->num_channels)
  520. value = mc_ch_readl(mc, channel, status_reg);
  521. else
  522. value = mc_readl(mc, status_reg);
  523. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  524. if (mc->soc->num_address_bits > 32) {
  525. if (addr_hi_reg) {
  526. if (mc->soc->num_channels)
  527. addr = mc_ch_readl(mc, channel, addr_hi_reg);
  528. else
  529. addr = mc_readl(mc, addr_hi_reg);
  530. } else {
  531. addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
  532. MC_ERR_STATUS_ADR_HI_MASK);
  533. }
  534. addr <<= 32;
  535. }
  536. #endif
  537. if (value & MC_ERR_STATUS_RW)
  538. direction = "write";
  539. else
  540. direction = "read";
  541. if (value & MC_ERR_STATUS_SECURITY)
  542. secure = "secure ";
  543. else
  544. secure = "";
  545. id = value & mc->soc->client_id_mask;
  546. for (i = 0; i < mc->soc->num_clients; i++) {
  547. if (mc->soc->clients[i].id == id) {
  548. client = mc->soc->clients[i].name;
  549. break;
  550. }
  551. }
  552. type = (value & MC_ERR_STATUS_TYPE_MASK) >>
  553. MC_ERR_STATUS_TYPE_SHIFT;
  554. desc = tegra_mc_error_names[type];
  555. switch (value & MC_ERR_STATUS_TYPE_MASK) {
  556. case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
  557. perm[0] = ' ';
  558. perm[1] = '[';
  559. if (value & MC_ERR_STATUS_READABLE)
  560. perm[2] = 'R';
  561. else
  562. perm[2] = '-';
  563. if (value & MC_ERR_STATUS_WRITABLE)
  564. perm[3] = 'W';
  565. else
  566. perm[3] = '-';
  567. if (value & MC_ERR_STATUS_NONSECURE)
  568. perm[4] = '-';
  569. else
  570. perm[4] = 'S';
  571. perm[5] = ']';
  572. perm[6] = '\0';
  573. break;
  574. default:
  575. perm[0] = '\0';
  576. break;
  577. }
  578. if (mc->soc->num_channels)
  579. value = mc_ch_readl(mc, channel, addr_reg);
  580. else
  581. value = mc_readl(mc, addr_reg);
  582. addr |= value;
  583. dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
  584. client, secure, direction, &addr, error,
  585. desc, perm);
  586. }
  587. /* clear interrupts */
  588. if (mc->soc->num_channels) {
  589. mc_ch_writel(mc, channel, status, MC_INTSTATUS);
  590. mc_ch_writel(mc, MC_BROADCAST_CHANNEL,
  591. mc_channel_to_global_intstatus(mc, channel),
  592. MC_GLOBAL_INTSTATUS);
  593. } else {
  594. mc_writel(mc, status, MC_INTSTATUS);
  595. }
  596. return IRQ_HANDLED;
  597. }
  598. const char *const tegra_mc_status_names[32] = {
  599. [ 1] = "External interrupt",
  600. [ 6] = "EMEM address decode error",
  601. [ 7] = "GART page fault",
  602. [ 8] = "Security violation",
  603. [ 9] = "EMEM arbitration error",
  604. [10] = "Page fault",
  605. [11] = "Invalid APB ASID update",
  606. [12] = "VPR violation",
  607. [13] = "Secure carveout violation",
  608. [16] = "MTS carveout violation",
  609. [17] = "Generalized carveout violation",
  610. [20] = "Route Sanity error",
  611. };
  612. const char *const tegra_mc_error_names[8] = {
  613. [2] = "EMEM decode error",
  614. [3] = "TrustZone violation",
  615. [4] = "Carveout violation",
  616. [6] = "SMMU translation error",
  617. };
  618. struct icc_node *tegra_mc_icc_xlate(const struct of_phandle_args *spec, void *data)
  619. {
  620. struct tegra_mc *mc = icc_provider_to_tegra_mc(data);
  621. struct icc_node *node;
  622. list_for_each_entry(node, &mc->provider.nodes, node_list) {
  623. if (node->id == spec->args[0])
  624. return node;
  625. }
  626. /*
  627. * If a client driver calls devm_of_icc_get() before the MC driver
  628. * is probed, then return EPROBE_DEFER to the client driver.
  629. */
  630. return ERR_PTR(-EPROBE_DEFER);
  631. }
  632. static int tegra_mc_icc_get(struct icc_node *node, u32 *average, u32 *peak)
  633. {
  634. *average = 0;
  635. *peak = 0;
  636. return 0;
  637. }
  638. static int tegra_mc_icc_set(struct icc_node *src, struct icc_node *dst)
  639. {
  640. return 0;
  641. }
  642. const struct tegra_mc_icc_ops tegra_mc_icc_ops = {
  643. .xlate = tegra_mc_icc_xlate,
  644. .aggregate = icc_std_aggregate,
  645. .get_bw = tegra_mc_icc_get,
  646. .set = tegra_mc_icc_set,
  647. };
  648. /*
  649. * Memory Controller (MC) has few Memory Clients that are issuing memory
  650. * bandwidth allocation requests to the MC interconnect provider. The MC
  651. * provider aggregates the requests and then sends the aggregated request
  652. * up to the External Memory Controller (EMC) interconnect provider which
  653. * re-configures hardware interface to External Memory (EMEM) in accordance
  654. * to the required bandwidth. Each MC interconnect node represents an
  655. * individual Memory Client.
  656. *
  657. * Memory interconnect topology:
  658. *
  659. * +----+
  660. * +--------+ | |
  661. * | TEXSRD +--->+ |
  662. * +--------+ | |
  663. * | | +-----+ +------+
  664. * ... | MC +--->+ EMC +--->+ EMEM |
  665. * | | +-----+ +------+
  666. * +--------+ | |
  667. * | DISP.. +--->+ |
  668. * +--------+ | |
  669. * +----+
  670. */
  671. static int tegra_mc_interconnect_setup(struct tegra_mc *mc)
  672. {
  673. struct icc_node *node;
  674. unsigned int i;
  675. int err;
  676. /* older device-trees don't have interconnect properties */
  677. if (!device_property_present(mc->dev, "#interconnect-cells") ||
  678. !mc->soc->icc_ops)
  679. return 0;
  680. mc->provider.dev = mc->dev;
  681. mc->provider.data = &mc->provider;
  682. mc->provider.set = mc->soc->icc_ops->set;
  683. mc->provider.aggregate = mc->soc->icc_ops->aggregate;
  684. mc->provider.get_bw = mc->soc->icc_ops->get_bw;
  685. mc->provider.xlate = mc->soc->icc_ops->xlate;
  686. mc->provider.xlate_extended = mc->soc->icc_ops->xlate_extended;
  687. icc_provider_init(&mc->provider);
  688. /* create Memory Controller node */
  689. node = icc_node_create(TEGRA_ICC_MC);
  690. if (IS_ERR(node))
  691. return PTR_ERR(node);
  692. node->name = "Memory Controller";
  693. icc_node_add(node, &mc->provider);
  694. /* link Memory Controller to External Memory Controller */
  695. err = icc_link_create(node, TEGRA_ICC_EMC);
  696. if (err)
  697. goto remove_nodes;
  698. for (i = 0; i < mc->soc->num_clients; i++) {
  699. /* create MC client node */
  700. node = icc_node_create(mc->soc->clients[i].id);
  701. if (IS_ERR(node)) {
  702. err = PTR_ERR(node);
  703. goto remove_nodes;
  704. }
  705. node->name = mc->soc->clients[i].name;
  706. icc_node_add(node, &mc->provider);
  707. /* link Memory Client to Memory Controller */
  708. err = icc_link_create(node, TEGRA_ICC_MC);
  709. if (err)
  710. goto remove_nodes;
  711. node->data = (struct tegra_mc_client *)&(mc->soc->clients[i]);
  712. }
  713. err = icc_provider_register(&mc->provider);
  714. if (err)
  715. goto remove_nodes;
  716. return 0;
  717. remove_nodes:
  718. icc_nodes_remove(&mc->provider);
  719. return err;
  720. }
  721. static void tegra_mc_num_channel_enabled(struct tegra_mc *mc)
  722. {
  723. unsigned int i;
  724. u32 value;
  725. value = mc_ch_readl(mc, 0, MC_EMEM_ADR_CFG_CHANNEL_ENABLE);
  726. if (value <= 0) {
  727. mc->num_channels = mc->soc->num_channels;
  728. return;
  729. }
  730. for (i = 0; i < 32; i++) {
  731. if (value & BIT(i))
  732. mc->num_channels++;
  733. }
  734. }
  735. static int tegra_mc_probe(struct platform_device *pdev)
  736. {
  737. struct tegra_mc *mc;
  738. u64 mask;
  739. int err;
  740. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  741. if (!mc)
  742. return -ENOMEM;
  743. platform_set_drvdata(pdev, mc);
  744. spin_lock_init(&mc->lock);
  745. mc->soc = of_device_get_match_data(&pdev->dev);
  746. mc->dev = &pdev->dev;
  747. mask = DMA_BIT_MASK(mc->soc->num_address_bits);
  748. err = dma_coerce_mask_and_coherent(&pdev->dev, mask);
  749. if (err < 0) {
  750. dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
  751. return err;
  752. }
  753. /* length of MC tick in nanoseconds */
  754. mc->tick = 30;
  755. mc->regs = devm_platform_ioremap_resource(pdev, 0);
  756. if (IS_ERR(mc->regs))
  757. return PTR_ERR(mc->regs);
  758. mc->debugfs.root = debugfs_create_dir("mc", NULL);
  759. if (mc->soc->ops && mc->soc->ops->probe) {
  760. err = mc->soc->ops->probe(mc);
  761. if (err < 0)
  762. return err;
  763. }
  764. tegra_mc_num_channel_enabled(mc);
  765. if (mc->soc->ops && mc->soc->ops->handle_irq) {
  766. mc->irq = platform_get_irq(pdev, 0);
  767. if (mc->irq < 0)
  768. return mc->irq;
  769. WARN(!mc->soc->client_id_mask, "missing client ID mask for this SoC\n");
  770. if (mc->soc->num_channels)
  771. mc_ch_writel(mc, MC_BROADCAST_CHANNEL, mc->soc->intmask,
  772. MC_INTMASK);
  773. else
  774. mc_writel(mc, mc->soc->intmask, MC_INTMASK);
  775. err = devm_request_irq(&pdev->dev, mc->irq, mc->soc->ops->handle_irq, 0,
  776. dev_name(&pdev->dev), mc);
  777. if (err < 0) {
  778. dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
  779. err);
  780. return err;
  781. }
  782. }
  783. if (mc->soc->reset_ops) {
  784. err = tegra_mc_reset_setup(mc);
  785. if (err < 0)
  786. dev_err(&pdev->dev, "failed to register reset controller: %d\n", err);
  787. }
  788. err = tegra_mc_interconnect_setup(mc);
  789. if (err < 0)
  790. dev_err(&pdev->dev, "failed to initialize interconnect: %d\n",
  791. err);
  792. if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU) && mc->soc->smmu) {
  793. mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
  794. if (IS_ERR(mc->smmu)) {
  795. dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
  796. PTR_ERR(mc->smmu));
  797. mc->smmu = NULL;
  798. }
  799. }
  800. return 0;
  801. }
  802. static void tegra_mc_sync_state(struct device *dev)
  803. {
  804. struct tegra_mc *mc = dev_get_drvdata(dev);
  805. /* check whether ICC provider is registered */
  806. if (mc->provider.dev == dev)
  807. icc_sync_state(dev);
  808. }
  809. static struct platform_driver tegra_mc_driver = {
  810. .driver = {
  811. .name = "tegra-mc",
  812. .of_match_table = tegra_mc_of_match,
  813. .suppress_bind_attrs = true,
  814. .sync_state = tegra_mc_sync_state,
  815. },
  816. .prevent_deferred_probe = true,
  817. .probe = tegra_mc_probe,
  818. };
  819. static int tegra_mc_init(void)
  820. {
  821. return platform_driver_register(&tegra_mc_driver);
  822. }
  823. arch_initcall(tegra_mc_init);
  824. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  825. MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");