cache-uniphier.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015-2016 Socionext Inc.
  4. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5. */
  6. #define pr_fmt(fmt) "uniphier: " fmt
  7. #include <linux/bitops.h>
  8. #include <linux/init.h>
  9. #include <linux/io.h>
  10. #include <linux/log2.h>
  11. #include <linux/of_address.h>
  12. #include <linux/slab.h>
  13. #include <asm/hardware/cache-uniphier.h>
  14. #include <asm/outercache.h>
  15. /* control registers */
  16. #define UNIPHIER_SSCC 0x0 /* Control Register */
  17. #define UNIPHIER_SSCC_BST BIT(20) /* UCWG burst read */
  18. #define UNIPHIER_SSCC_ACT BIT(19) /* Inst-Data separate */
  19. #define UNIPHIER_SSCC_WTG BIT(18) /* WT gathering on */
  20. #define UNIPHIER_SSCC_PRD BIT(17) /* enable pre-fetch */
  21. #define UNIPHIER_SSCC_ON BIT(0) /* enable cache */
  22. #define UNIPHIER_SSCLPDAWCR 0x30 /* Unified/Data Active Way Control */
  23. #define UNIPHIER_SSCLPIAWCR 0x34 /* Instruction Active Way Control */
  24. /* revision registers */
  25. #define UNIPHIER_SSCID 0x0 /* ID Register */
  26. /* operation registers */
  27. #define UNIPHIER_SSCOPE 0x244 /* Cache Operation Primitive Entry */
  28. #define UNIPHIER_SSCOPE_CM_INV 0x0 /* invalidate */
  29. #define UNIPHIER_SSCOPE_CM_CLEAN 0x1 /* clean */
  30. #define UNIPHIER_SSCOPE_CM_FLUSH 0x2 /* flush */
  31. #define UNIPHIER_SSCOPE_CM_SYNC 0x8 /* sync (drain bufs) */
  32. #define UNIPHIER_SSCOPE_CM_FLUSH_PREFETCH 0x9 /* flush p-fetch buf */
  33. #define UNIPHIER_SSCOQM 0x248 /* Cache Operation Queue Mode */
  34. #define UNIPHIER_SSCOQM_S_MASK (0x3 << 17)
  35. #define UNIPHIER_SSCOQM_S_RANGE (0x0 << 17)
  36. #define UNIPHIER_SSCOQM_S_ALL (0x1 << 17)
  37. #define UNIPHIER_SSCOQM_CE BIT(15) /* notify completion */
  38. #define UNIPHIER_SSCOQM_CM_INV 0x0 /* invalidate */
  39. #define UNIPHIER_SSCOQM_CM_CLEAN 0x1 /* clean */
  40. #define UNIPHIER_SSCOQM_CM_FLUSH 0x2 /* flush */
  41. #define UNIPHIER_SSCOQAD 0x24c /* Cache Operation Queue Address */
  42. #define UNIPHIER_SSCOQSZ 0x250 /* Cache Operation Queue Size */
  43. #define UNIPHIER_SSCOPPQSEF 0x25c /* Cache Operation Queue Set Complete*/
  44. #define UNIPHIER_SSCOPPQSEF_FE BIT(1)
  45. #define UNIPHIER_SSCOPPQSEF_OE BIT(0)
  46. #define UNIPHIER_SSCOLPQS 0x260 /* Cache Operation Queue Status */
  47. #define UNIPHIER_SSCOLPQS_EF BIT(2)
  48. #define UNIPHIER_SSCOLPQS_EST BIT(1)
  49. #define UNIPHIER_SSCOLPQS_QST BIT(0)
  50. /* Is the operation region specified by address range? */
  51. #define UNIPHIER_SSCOQM_S_IS_RANGE(op) \
  52. ((op & UNIPHIER_SSCOQM_S_MASK) == UNIPHIER_SSCOQM_S_RANGE)
  53. /**
  54. * struct uniphier_cache_data - UniPhier outer cache specific data
  55. *
  56. * @ctrl_base: virtual base address of control registers
  57. * @rev_base: virtual base address of revision registers
  58. * @op_base: virtual base address of operation registers
  59. * @way_ctrl_base: virtual address of the way control registers for this
  60. * SoC revision
  61. * @way_mask: each bit specifies if the way is present
  62. * @nsets: number of associativity sets
  63. * @line_size: line size in bytes
  64. * @range_op_max_size: max size that can be handled by a single range operation
  65. * @list: list node to include this level in the whole cache hierarchy
  66. */
  67. struct uniphier_cache_data {
  68. void __iomem *ctrl_base;
  69. void __iomem *rev_base;
  70. void __iomem *op_base;
  71. void __iomem *way_ctrl_base;
  72. u32 way_mask;
  73. u32 nsets;
  74. u32 line_size;
  75. u32 range_op_max_size;
  76. struct list_head list;
  77. };
  78. /*
  79. * List of the whole outer cache hierarchy. This list is only modified during
  80. * the early boot stage, so no mutex is taken for the access to the list.
  81. */
  82. static LIST_HEAD(uniphier_cache_list);
  83. /**
  84. * __uniphier_cache_sync - perform a sync point for a particular cache level
  85. *
  86. * @data: cache controller specific data
  87. */
  88. static void __uniphier_cache_sync(struct uniphier_cache_data *data)
  89. {
  90. /* This sequence need not be atomic. Do not disable IRQ. */
  91. writel_relaxed(UNIPHIER_SSCOPE_CM_SYNC,
  92. data->op_base + UNIPHIER_SSCOPE);
  93. /* need a read back to confirm */
  94. readl_relaxed(data->op_base + UNIPHIER_SSCOPE);
  95. }
  96. /**
  97. * __uniphier_cache_maint_common - run a queue operation for a particular level
  98. *
  99. * @data: cache controller specific data
  100. * @start: start address of range operation (don't care for "all" operation)
  101. * @size: data size of range operation (don't care for "all" operation)
  102. * @operation: flags to specify the desired cache operation
  103. */
  104. static void __uniphier_cache_maint_common(struct uniphier_cache_data *data,
  105. unsigned long start,
  106. unsigned long size,
  107. u32 operation)
  108. {
  109. unsigned long flags;
  110. /*
  111. * No spin lock is necessary here because:
  112. *
  113. * [1] This outer cache controller is able to accept maintenance
  114. * operations from multiple CPUs at a time in an SMP system; if a
  115. * maintenance operation is under way and another operation is issued,
  116. * the new one is stored in the queue. The controller performs one
  117. * operation after another. If the queue is full, the status register,
  118. * UNIPHIER_SSCOPPQSEF, indicates that the queue registration has
  119. * failed. The status registers, UNIPHIER_{SSCOPPQSEF, SSCOLPQS}, have
  120. * different instances for each CPU, i.e. each CPU can track the status
  121. * of the maintenance operations triggered by itself.
  122. *
  123. * [2] The cache command registers, UNIPHIER_{SSCOQM, SSCOQAD, SSCOQSZ,
  124. * SSCOQWN}, are shared between multiple CPUs, but the hardware still
  125. * guarantees the registration sequence is atomic; the write access to
  126. * them are arbitrated by the hardware. The first accessor to the
  127. * register, UNIPHIER_SSCOQM, holds the access right and it is released
  128. * by reading the status register, UNIPHIER_SSCOPPQSEF. While one CPU
  129. * is holding the access right, other CPUs fail to register operations.
  130. * One CPU should not hold the access right for a long time, so local
  131. * IRQs should be disabled while the following sequence.
  132. */
  133. local_irq_save(flags);
  134. /* clear the complete notification flag */
  135. writel_relaxed(UNIPHIER_SSCOLPQS_EF, data->op_base + UNIPHIER_SSCOLPQS);
  136. do {
  137. /* set cache operation */
  138. writel_relaxed(UNIPHIER_SSCOQM_CE | operation,
  139. data->op_base + UNIPHIER_SSCOQM);
  140. /* set address range if needed */
  141. if (likely(UNIPHIER_SSCOQM_S_IS_RANGE(operation))) {
  142. writel_relaxed(start, data->op_base + UNIPHIER_SSCOQAD);
  143. writel_relaxed(size, data->op_base + UNIPHIER_SSCOQSZ);
  144. }
  145. } while (unlikely(readl_relaxed(data->op_base + UNIPHIER_SSCOPPQSEF) &
  146. (UNIPHIER_SSCOPPQSEF_FE | UNIPHIER_SSCOPPQSEF_OE)));
  147. /* wait until the operation is completed */
  148. while (likely(readl_relaxed(data->op_base + UNIPHIER_SSCOLPQS) !=
  149. UNIPHIER_SSCOLPQS_EF))
  150. cpu_relax();
  151. local_irq_restore(flags);
  152. }
  153. static void __uniphier_cache_maint_all(struct uniphier_cache_data *data,
  154. u32 operation)
  155. {
  156. __uniphier_cache_maint_common(data, 0, 0,
  157. UNIPHIER_SSCOQM_S_ALL | operation);
  158. __uniphier_cache_sync(data);
  159. }
  160. static void __uniphier_cache_maint_range(struct uniphier_cache_data *data,
  161. unsigned long start, unsigned long end,
  162. u32 operation)
  163. {
  164. unsigned long size;
  165. /*
  166. * If the start address is not aligned,
  167. * perform a cache operation for the first cache-line
  168. */
  169. start = start & ~(data->line_size - 1);
  170. size = end - start;
  171. if (unlikely(size >= (unsigned long)(-data->line_size))) {
  172. /* this means cache operation for all range */
  173. __uniphier_cache_maint_all(data, operation);
  174. return;
  175. }
  176. /*
  177. * If the end address is not aligned,
  178. * perform a cache operation for the last cache-line
  179. */
  180. size = ALIGN(size, data->line_size);
  181. while (size) {
  182. unsigned long chunk_size = min_t(unsigned long, size,
  183. data->range_op_max_size);
  184. __uniphier_cache_maint_common(data, start, chunk_size,
  185. UNIPHIER_SSCOQM_S_RANGE | operation);
  186. start += chunk_size;
  187. size -= chunk_size;
  188. }
  189. __uniphier_cache_sync(data);
  190. }
  191. static void __uniphier_cache_enable(struct uniphier_cache_data *data, bool on)
  192. {
  193. u32 val = 0;
  194. if (on)
  195. val = UNIPHIER_SSCC_WTG | UNIPHIER_SSCC_PRD | UNIPHIER_SSCC_ON;
  196. writel_relaxed(val, data->ctrl_base + UNIPHIER_SSCC);
  197. }
  198. static void __init __uniphier_cache_set_active_ways(
  199. struct uniphier_cache_data *data)
  200. {
  201. unsigned int cpu;
  202. for_each_possible_cpu(cpu)
  203. writel_relaxed(data->way_mask, data->way_ctrl_base + 4 * cpu);
  204. }
  205. static void uniphier_cache_maint_range(unsigned long start, unsigned long end,
  206. u32 operation)
  207. {
  208. struct uniphier_cache_data *data;
  209. list_for_each_entry(data, &uniphier_cache_list, list)
  210. __uniphier_cache_maint_range(data, start, end, operation);
  211. }
  212. static void uniphier_cache_maint_all(u32 operation)
  213. {
  214. struct uniphier_cache_data *data;
  215. list_for_each_entry(data, &uniphier_cache_list, list)
  216. __uniphier_cache_maint_all(data, operation);
  217. }
  218. static void uniphier_cache_inv_range(unsigned long start, unsigned long end)
  219. {
  220. uniphier_cache_maint_range(start, end, UNIPHIER_SSCOQM_CM_INV);
  221. }
  222. static void uniphier_cache_clean_range(unsigned long start, unsigned long end)
  223. {
  224. uniphier_cache_maint_range(start, end, UNIPHIER_SSCOQM_CM_CLEAN);
  225. }
  226. static void uniphier_cache_flush_range(unsigned long start, unsigned long end)
  227. {
  228. uniphier_cache_maint_range(start, end, UNIPHIER_SSCOQM_CM_FLUSH);
  229. }
  230. static void __init uniphier_cache_inv_all(void)
  231. {
  232. uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_INV);
  233. }
  234. static void uniphier_cache_flush_all(void)
  235. {
  236. uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_FLUSH);
  237. }
  238. static void uniphier_cache_disable(void)
  239. {
  240. struct uniphier_cache_data *data;
  241. list_for_each_entry_reverse(data, &uniphier_cache_list, list)
  242. __uniphier_cache_enable(data, false);
  243. uniphier_cache_flush_all();
  244. }
  245. static void __init uniphier_cache_enable(void)
  246. {
  247. struct uniphier_cache_data *data;
  248. uniphier_cache_inv_all();
  249. list_for_each_entry(data, &uniphier_cache_list, list) {
  250. __uniphier_cache_enable(data, true);
  251. __uniphier_cache_set_active_ways(data);
  252. }
  253. }
  254. static void uniphier_cache_sync(void)
  255. {
  256. struct uniphier_cache_data *data;
  257. list_for_each_entry(data, &uniphier_cache_list, list)
  258. __uniphier_cache_sync(data);
  259. }
  260. static const struct of_device_id uniphier_cache_match[] __initconst = {
  261. { .compatible = "socionext,uniphier-system-cache" },
  262. { /* sentinel */ }
  263. };
  264. static int __init __uniphier_cache_init(struct device_node *np,
  265. unsigned int *cache_level)
  266. {
  267. struct uniphier_cache_data *data;
  268. u32 level, cache_size;
  269. struct device_node *next_np;
  270. int ret = 0;
  271. if (!of_match_node(uniphier_cache_match, np)) {
  272. pr_err("L%d: not compatible with uniphier cache\n",
  273. *cache_level);
  274. return -EINVAL;
  275. }
  276. if (of_property_read_u32(np, "cache-level", &level)) {
  277. pr_err("L%d: cache-level is not specified\n", *cache_level);
  278. return -EINVAL;
  279. }
  280. if (level != *cache_level) {
  281. pr_err("L%d: cache-level is unexpected value %d\n",
  282. *cache_level, level);
  283. return -EINVAL;
  284. }
  285. if (!of_property_read_bool(np, "cache-unified")) {
  286. pr_err("L%d: cache-unified is not specified\n", *cache_level);
  287. return -EINVAL;
  288. }
  289. data = kzalloc(sizeof(*data), GFP_KERNEL);
  290. if (!data)
  291. return -ENOMEM;
  292. if (of_property_read_u32(np, "cache-line-size", &data->line_size) ||
  293. !is_power_of_2(data->line_size)) {
  294. pr_err("L%d: cache-line-size is unspecified or invalid\n",
  295. *cache_level);
  296. ret = -EINVAL;
  297. goto err;
  298. }
  299. if (of_property_read_u32(np, "cache-sets", &data->nsets) ||
  300. !is_power_of_2(data->nsets)) {
  301. pr_err("L%d: cache-sets is unspecified or invalid\n",
  302. *cache_level);
  303. ret = -EINVAL;
  304. goto err;
  305. }
  306. if (of_property_read_u32(np, "cache-size", &cache_size) ||
  307. cache_size == 0 || cache_size % (data->nsets * data->line_size)) {
  308. pr_err("L%d: cache-size is unspecified or invalid\n",
  309. *cache_level);
  310. ret = -EINVAL;
  311. goto err;
  312. }
  313. data->way_mask = GENMASK(cache_size / data->nsets / data->line_size - 1,
  314. 0);
  315. data->ctrl_base = of_iomap(np, 0);
  316. if (!data->ctrl_base) {
  317. pr_err("L%d: failed to map control register\n", *cache_level);
  318. ret = -ENOMEM;
  319. goto err;
  320. }
  321. data->rev_base = of_iomap(np, 1);
  322. if (!data->rev_base) {
  323. pr_err("L%d: failed to map revision register\n", *cache_level);
  324. ret = -ENOMEM;
  325. goto err;
  326. }
  327. data->op_base = of_iomap(np, 2);
  328. if (!data->op_base) {
  329. pr_err("L%d: failed to map operation register\n", *cache_level);
  330. ret = -ENOMEM;
  331. goto err;
  332. }
  333. data->way_ctrl_base = data->ctrl_base + 0xc00;
  334. if (*cache_level == 2) {
  335. u32 revision = readl(data->rev_base + UNIPHIER_SSCID);
  336. /*
  337. * The size of range operation is limited to (1 << 22) or less
  338. * for PH-sLD8 or older SoCs.
  339. */
  340. if (revision <= 0x16)
  341. data->range_op_max_size = (u32)1 << 22;
  342. /*
  343. * Unfortunatly, the offset address of active way control base
  344. * varies from SoC to SoC.
  345. */
  346. switch (revision) {
  347. case 0x11: /* sLD3 */
  348. data->way_ctrl_base = data->ctrl_base + 0x870;
  349. break;
  350. case 0x12: /* LD4 */
  351. case 0x16: /* sld8 */
  352. data->way_ctrl_base = data->ctrl_base + 0x840;
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. data->range_op_max_size -= data->line_size;
  359. INIT_LIST_HEAD(&data->list);
  360. list_add_tail(&data->list, &uniphier_cache_list); /* no mutex */
  361. /*
  362. * OK, this level has been successfully initialized. Look for the next
  363. * level cache. Do not roll back even if the initialization of the
  364. * next level cache fails because we want to continue with available
  365. * cache levels.
  366. */
  367. next_np = of_find_next_cache_node(np);
  368. if (next_np) {
  369. (*cache_level)++;
  370. ret = __uniphier_cache_init(next_np, cache_level);
  371. }
  372. of_node_put(next_np);
  373. return ret;
  374. err:
  375. iounmap(data->op_base);
  376. iounmap(data->rev_base);
  377. iounmap(data->ctrl_base);
  378. kfree(data);
  379. return ret;
  380. }
  381. int __init uniphier_cache_init(void)
  382. {
  383. struct device_node *np = NULL;
  384. unsigned int cache_level;
  385. int ret = 0;
  386. /* look for level 2 cache */
  387. while ((np = of_find_matching_node(np, uniphier_cache_match)))
  388. if (!of_property_read_u32(np, "cache-level", &cache_level) &&
  389. cache_level == 2)
  390. break;
  391. if (!np)
  392. return -ENODEV;
  393. ret = __uniphier_cache_init(np, &cache_level);
  394. of_node_put(np);
  395. if (ret) {
  396. /*
  397. * Error out iif L2 initialization fails. Continue with any
  398. * error on L3 or outer because they are optional.
  399. */
  400. if (cache_level == 2) {
  401. pr_err("failed to initialize L2 cache\n");
  402. return ret;
  403. }
  404. cache_level--;
  405. ret = 0;
  406. }
  407. outer_cache.inv_range = uniphier_cache_inv_range;
  408. outer_cache.clean_range = uniphier_cache_clean_range;
  409. outer_cache.flush_range = uniphier_cache_flush_range;
  410. outer_cache.flush_all = uniphier_cache_flush_all;
  411. outer_cache.disable = uniphier_cache_disable;
  412. outer_cache.sync = uniphier_cache_sync;
  413. uniphier_cache_enable();
  414. pr_info("enabled outer cache (cache level: %d)\n", cache_level);
  415. return ret;
  416. }