chmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* chmc.c: Driver for UltraSPARC-III memory controller.
  2. *
  3. * Copyright (C) 2001, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/slab.h>
  9. #include <linux/list.h>
  10. #include <linux/string.h>
  11. #include <linux/sched.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <asm/spitfire.h>
  18. #include <asm/chmctrl.h>
  19. #include <asm/cpudata.h>
  20. #include <asm/oplib.h>
  21. #include <asm/prom.h>
  22. #include <asm/head.h>
  23. #include <asm/io.h>
  24. #include <asm/memctrl.h>
  25. #define DRV_MODULE_NAME "chmc"
  26. #define PFX DRV_MODULE_NAME ": "
  27. #define DRV_MODULE_VERSION "0.2"
  28. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  29. MODULE_DESCRIPTION("UltraSPARC-III memory controller driver");
  30. MODULE_LICENSE("GPL");
  31. MODULE_VERSION(DRV_MODULE_VERSION);
  32. #define CHMCTRL_NDGRPS 2
  33. #define CHMCTRL_NDIMMS 4
  34. #define CHMC_DIMMS_PER_MC (CHMCTRL_NDGRPS * CHMCTRL_NDIMMS)
  35. /* OBP memory-layout property format. */
  36. struct chmc_obp_map {
  37. unsigned char dimm_map[144];
  38. unsigned char pin_map[576];
  39. };
  40. #define DIMM_LABEL_SZ 8
  41. struct chmc_obp_mem_layout {
  42. /* One max 8-byte string label per DIMM. Usually
  43. * this matches the label on the motherboard where
  44. * that DIMM resides.
  45. */
  46. char dimm_labels[CHMC_DIMMS_PER_MC][DIMM_LABEL_SZ];
  47. /* If symmetric use map[0], else it is
  48. * asymmetric and map[1] should be used.
  49. */
  50. char symmetric;
  51. struct chmc_obp_map map[2];
  52. };
  53. #define CHMCTRL_NBANKS 4
  54. struct chmc_bank_info {
  55. struct chmc *p;
  56. int bank_id;
  57. u64 raw_reg;
  58. int valid;
  59. int uk;
  60. int um;
  61. int lk;
  62. int lm;
  63. int interleave;
  64. unsigned long base;
  65. unsigned long size;
  66. };
  67. struct chmc {
  68. struct list_head list;
  69. int portid;
  70. struct chmc_obp_mem_layout layout_prop;
  71. int layout_size;
  72. void __iomem *regs;
  73. u64 timing_control1;
  74. u64 timing_control2;
  75. u64 timing_control3;
  76. u64 timing_control4;
  77. u64 memaddr_control;
  78. struct chmc_bank_info logical_banks[CHMCTRL_NBANKS];
  79. };
  80. static LIST_HEAD(mctrl_list);
  81. /* Does BANK decode PHYS_ADDR? */
  82. static int chmc_bank_match(struct chmc_bank_info *bp, unsigned long phys_addr)
  83. {
  84. unsigned long upper_bits = (phys_addr & PA_UPPER_BITS) >> PA_UPPER_BITS_SHIFT;
  85. unsigned long lower_bits = (phys_addr & PA_LOWER_BITS) >> PA_LOWER_BITS_SHIFT;
  86. /* Bank must be enabled to match. */
  87. if (bp->valid == 0)
  88. return 0;
  89. /* Would BANK match upper bits? */
  90. upper_bits ^= bp->um; /* What bits are different? */
  91. upper_bits = ~upper_bits; /* Invert. */
  92. upper_bits |= bp->uk; /* What bits don't matter for matching? */
  93. upper_bits = ~upper_bits; /* Invert. */
  94. if (upper_bits)
  95. return 0;
  96. /* Would BANK match lower bits? */
  97. lower_bits ^= bp->lm; /* What bits are different? */
  98. lower_bits = ~lower_bits; /* Invert. */
  99. lower_bits |= bp->lk; /* What bits don't matter for matching? */
  100. lower_bits = ~lower_bits; /* Invert. */
  101. if (lower_bits)
  102. return 0;
  103. /* I always knew you'd be the one. */
  104. return 1;
  105. }
  106. /* Given PHYS_ADDR, search memory controller banks for a match. */
  107. static struct chmc_bank_info *chmc_find_bank(unsigned long phys_addr)
  108. {
  109. struct list_head *mctrl_head = &mctrl_list;
  110. struct list_head *mctrl_entry = mctrl_head->next;
  111. for (;;) {
  112. struct chmc *p = list_entry(mctrl_entry, struct chmc, list);
  113. int bank_no;
  114. if (mctrl_entry == mctrl_head)
  115. break;
  116. mctrl_entry = mctrl_entry->next;
  117. for (bank_no = 0; bank_no < CHMCTRL_NBANKS; bank_no++) {
  118. struct chmc_bank_info *bp;
  119. bp = &p->logical_banks[bank_no];
  120. if (chmc_bank_match(bp, phys_addr))
  121. return bp;
  122. }
  123. }
  124. return NULL;
  125. }
  126. /* This is the main purpose of this driver. */
  127. #define SYNDROME_MIN -1
  128. #define SYNDROME_MAX 144
  129. static int chmc_print_dimm(int syndrome_code,
  130. unsigned long phys_addr,
  131. char *buf, int buflen)
  132. {
  133. struct chmc_bank_info *bp;
  134. struct chmc_obp_mem_layout *prop;
  135. int bank_in_controller, first_dimm;
  136. bp = chmc_find_bank(phys_addr);
  137. if (bp == NULL ||
  138. syndrome_code < SYNDROME_MIN ||
  139. syndrome_code > SYNDROME_MAX) {
  140. buf[0] = '?';
  141. buf[1] = '?';
  142. buf[2] = '?';
  143. buf[3] = '\0';
  144. return 0;
  145. }
  146. prop = &bp->p->layout_prop;
  147. bank_in_controller = bp->bank_id & (CHMCTRL_NBANKS - 1);
  148. first_dimm = (bank_in_controller & (CHMCTRL_NDGRPS - 1));
  149. first_dimm *= CHMCTRL_NDIMMS;
  150. if (syndrome_code != SYNDROME_MIN) {
  151. struct chmc_obp_map *map;
  152. int qword, where_in_line, where, map_index, map_offset;
  153. unsigned int map_val;
  154. /* Yaay, single bit error so we can figure out
  155. * the exact dimm.
  156. */
  157. if (prop->symmetric)
  158. map = &prop->map[0];
  159. else
  160. map = &prop->map[1];
  161. /* Covert syndrome code into the way the bits are
  162. * positioned on the bus.
  163. */
  164. if (syndrome_code < 144 - 16)
  165. syndrome_code += 16;
  166. else if (syndrome_code < 144)
  167. syndrome_code -= (144 - 7);
  168. else if (syndrome_code < (144 + 3))
  169. syndrome_code -= (144 + 3 - 4);
  170. else
  171. syndrome_code -= 144 + 3;
  172. /* All this magic has to do with how a cache line
  173. * comes over the wire on Safari. A 64-bit line
  174. * comes over in 4 quadword cycles, each of which
  175. * transmit ECC/MTAG info as well as the actual
  176. * data. 144 bits per quadword, 576 total.
  177. */
  178. #define LINE_SIZE 64
  179. #define LINE_ADDR_MSK (LINE_SIZE - 1)
  180. #define QW_PER_LINE 4
  181. #define QW_BYTES (LINE_SIZE / QW_PER_LINE)
  182. #define QW_BITS 144
  183. #define LAST_BIT (576 - 1)
  184. qword = (phys_addr & LINE_ADDR_MSK) / QW_BYTES;
  185. where_in_line = ((3 - qword) * QW_BITS) + syndrome_code;
  186. where = (LAST_BIT - where_in_line);
  187. map_index = where >> 2;
  188. map_offset = where & 0x3;
  189. map_val = map->dimm_map[map_index];
  190. map_val = ((map_val >> ((3 - map_offset) << 1)) & (2 - 1));
  191. sprintf(buf, "%s, pin %3d",
  192. prop->dimm_labels[first_dimm + map_val],
  193. map->pin_map[where_in_line]);
  194. } else {
  195. int dimm;
  196. /* Multi-bit error, we just dump out all the
  197. * dimm labels associated with this bank.
  198. */
  199. for (dimm = 0; dimm < CHMCTRL_NDIMMS; dimm++) {
  200. sprintf(buf, "%s ",
  201. prop->dimm_labels[first_dimm + dimm]);
  202. buf += strlen(buf);
  203. }
  204. }
  205. return 0;
  206. }
  207. /* Accessing the registers is slightly complicated. If you want
  208. * to get at the memory controller which is on the same processor
  209. * the code is executing, you must use special ASI load/store else
  210. * you go through the global mapping.
  211. */
  212. static u64 chmc_read_mcreg(struct chmc *p, unsigned long offset)
  213. {
  214. unsigned long ret, this_cpu;
  215. preempt_disable();
  216. this_cpu = real_hard_smp_processor_id();
  217. if (p->portid == this_cpu) {
  218. __asm__ __volatile__("ldxa [%1] %2, %0"
  219. : "=r" (ret)
  220. : "r" (offset), "i" (ASI_MCU_CTRL_REG));
  221. } else {
  222. __asm__ __volatile__("ldxa [%1] %2, %0"
  223. : "=r" (ret)
  224. : "r" (p->regs + offset),
  225. "i" (ASI_PHYS_BYPASS_EC_E));
  226. }
  227. preempt_enable();
  228. return ret;
  229. }
  230. #if 0 /* currently unused */
  231. static void chmc_write_mcreg(struct chmc *p, unsigned long offset, u64 val)
  232. {
  233. if (p->portid == smp_processor_id()) {
  234. __asm__ __volatile__("stxa %0, [%1] %2"
  235. : : "r" (val),
  236. "r" (offset), "i" (ASI_MCU_CTRL_REG));
  237. } else {
  238. __asm__ __volatile__("ldxa %0, [%1] %2"
  239. : : "r" (val),
  240. "r" (p->regs + offset),
  241. "i" (ASI_PHYS_BYPASS_EC_E));
  242. }
  243. }
  244. #endif
  245. static void chmc_interpret_one_decode_reg(struct chmc *p, int which_bank, u64 val)
  246. {
  247. struct chmc_bank_info *bp = &p->logical_banks[which_bank];
  248. bp->p = p;
  249. bp->bank_id = (CHMCTRL_NBANKS * p->portid) + which_bank;
  250. bp->raw_reg = val;
  251. bp->valid = (val & MEM_DECODE_VALID) >> MEM_DECODE_VALID_SHIFT;
  252. bp->uk = (val & MEM_DECODE_UK) >> MEM_DECODE_UK_SHIFT;
  253. bp->um = (val & MEM_DECODE_UM) >> MEM_DECODE_UM_SHIFT;
  254. bp->lk = (val & MEM_DECODE_LK) >> MEM_DECODE_LK_SHIFT;
  255. bp->lm = (val & MEM_DECODE_LM) >> MEM_DECODE_LM_SHIFT;
  256. bp->base = (bp->um);
  257. bp->base &= ~(bp->uk);
  258. bp->base <<= PA_UPPER_BITS_SHIFT;
  259. switch(bp->lk) {
  260. case 0xf:
  261. default:
  262. bp->interleave = 1;
  263. break;
  264. case 0xe:
  265. bp->interleave = 2;
  266. break;
  267. case 0xc:
  268. bp->interleave = 4;
  269. break;
  270. case 0x8:
  271. bp->interleave = 8;
  272. break;
  273. case 0x0:
  274. bp->interleave = 16;
  275. break;
  276. };
  277. /* UK[10] is reserved, and UK[11] is not set for the SDRAM
  278. * bank size definition.
  279. */
  280. bp->size = (((unsigned long)bp->uk &
  281. ((1UL << 10UL) - 1UL)) + 1UL) << PA_UPPER_BITS_SHIFT;
  282. bp->size /= bp->interleave;
  283. }
  284. static void chmc_fetch_decode_regs(struct chmc *p)
  285. {
  286. if (p->layout_size == 0)
  287. return;
  288. chmc_interpret_one_decode_reg(p, 0,
  289. chmc_read_mcreg(p, CHMCTRL_DECODE1));
  290. chmc_interpret_one_decode_reg(p, 1,
  291. chmc_read_mcreg(p, CHMCTRL_DECODE2));
  292. chmc_interpret_one_decode_reg(p, 2,
  293. chmc_read_mcreg(p, CHMCTRL_DECODE3));
  294. chmc_interpret_one_decode_reg(p, 3,
  295. chmc_read_mcreg(p, CHMCTRL_DECODE4));
  296. }
  297. static int __devinit chmc_probe(struct of_device *op,
  298. const struct of_device_id *match)
  299. {
  300. struct device_node *dp = op->node;
  301. unsigned long ver;
  302. const void *pval;
  303. int len, portid;
  304. struct chmc *p;
  305. int err;
  306. err = -ENODEV;
  307. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  308. if ((ver >> 32UL) == __JALAPENO_ID ||
  309. (ver >> 32UL) == __SERRANO_ID)
  310. goto out;
  311. portid = of_getintprop_default(dp, "portid", -1);
  312. if (portid == -1)
  313. goto out;
  314. pval = of_get_property(dp, "memory-layout", &len);
  315. if (pval && len > sizeof(p->layout_prop)) {
  316. printk(KERN_ERR PFX "Unexpected memory-layout property "
  317. "size %d.\n", len);
  318. goto out;
  319. }
  320. err = -ENOMEM;
  321. p = kzalloc(sizeof(*p), GFP_KERNEL);
  322. if (!p) {
  323. printk(KERN_ERR PFX "Could not allocate struct chmc.\n");
  324. goto out;
  325. }
  326. p->portid = portid;
  327. p->layout_size = len;
  328. if (!pval)
  329. p->layout_size = 0;
  330. else
  331. memcpy(&p->layout_prop, pval, len);
  332. p->regs = of_ioremap(&op->resource[0], 0, 0x48, "chmc");
  333. if (!p->regs) {
  334. printk(KERN_ERR PFX "Could not map registers.\n");
  335. goto out_free;
  336. }
  337. if (p->layout_size != 0UL) {
  338. p->timing_control1 = chmc_read_mcreg(p, CHMCTRL_TCTRL1);
  339. p->timing_control2 = chmc_read_mcreg(p, CHMCTRL_TCTRL2);
  340. p->timing_control3 = chmc_read_mcreg(p, CHMCTRL_TCTRL3);
  341. p->timing_control4 = chmc_read_mcreg(p, CHMCTRL_TCTRL4);
  342. p->memaddr_control = chmc_read_mcreg(p, CHMCTRL_MACTRL);
  343. }
  344. chmc_fetch_decode_regs(p);
  345. list_add(&p->list, &mctrl_list);
  346. /* Report the device. */
  347. printk(KERN_INFO PFX "UltraSPARC-III memory controller at %s [%s]\n",
  348. dp->full_name,
  349. (p->layout_size ? "ACTIVE" : "INACTIVE"));
  350. dev_set_drvdata(&op->dev, p);
  351. err = 0;
  352. out:
  353. return err;
  354. out_free:
  355. kfree(p);
  356. goto out;
  357. }
  358. static int __devexit chmc_remove(struct of_device *op)
  359. {
  360. struct chmc *p = dev_get_drvdata(&op->dev);
  361. if (p) {
  362. list_del(&p->list);
  363. of_iounmap(&op->resource[0], p->regs, 0x48);
  364. kfree(p);
  365. }
  366. return 0;
  367. }
  368. static struct of_device_id chmc_match[] = {
  369. {
  370. .name = "memory-controller",
  371. },
  372. {},
  373. };
  374. MODULE_DEVICE_TABLE(of, chmc_match);
  375. static struct of_platform_driver chmc_driver = {
  376. .name = "chmc",
  377. .match_table = chmc_match,
  378. .probe = chmc_probe,
  379. .remove = __devexit_p(chmc_remove),
  380. };
  381. static inline bool chmc_platform(void)
  382. {
  383. if (tlb_type == cheetah || tlb_type == cheetah_plus)
  384. return true;
  385. return false;
  386. }
  387. static int __init chmc_init(void)
  388. {
  389. int ret;
  390. if (!chmc_platform())
  391. return -ENODEV;
  392. ret = register_dimm_printer(chmc_print_dimm);
  393. if (!ret) {
  394. ret = of_register_driver(&chmc_driver, &of_bus_type);
  395. if (ret)
  396. unregister_dimm_printer(chmc_print_dimm);
  397. }
  398. return ret;
  399. }
  400. static void __exit chmc_cleanup(void)
  401. {
  402. if (chmc_platform()) {
  403. unregister_dimm_printer(chmc_print_dimm);
  404. of_unregister_driver(&chmc_driver);
  405. }
  406. }
  407. module_init(chmc_init);
  408. module_exit(chmc_cleanup);