cache-l2x0.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. /*
  2. * arch/arm/mm/cache-l2x0.c - L210/L220 cache controller support
  3. *
  4. * Copyright (C) 2007 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/hardware/cache-l2x0.h>
  27. #include "cache-tauros3.h"
  28. #include "cache-aurora-l2.h"
  29. struct l2c_init_data {
  30. const char *type;
  31. unsigned way_size_0;
  32. unsigned num_lock;
  33. void (*of_parse)(const struct device_node *, u32 *, u32 *);
  34. void (*enable)(void __iomem *, u32, unsigned);
  35. void (*fixup)(void __iomem *, u32, struct outer_cache_fns *);
  36. void (*save)(void __iomem *);
  37. struct outer_cache_fns outer_cache;
  38. };
  39. #define CACHE_LINE_SIZE 32
  40. static void __iomem *l2x0_base;
  41. static DEFINE_RAW_SPINLOCK(l2x0_lock);
  42. static u32 l2x0_way_mask; /* Bitmask of active ways */
  43. static u32 l2x0_size;
  44. static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
  45. struct l2x0_regs l2x0_saved_regs;
  46. /*
  47. * Common code for all cache controllers.
  48. */
  49. static inline void l2c_wait_mask(void __iomem *reg, unsigned long mask)
  50. {
  51. /* wait for cache operation by line or way to complete */
  52. while (readl_relaxed(reg) & mask)
  53. cpu_relax();
  54. }
  55. /*
  56. * By default, we write directly to secure registers. Platforms must
  57. * override this if they are running non-secure.
  58. */
  59. static void l2c_write_sec(unsigned long val, void __iomem *base, unsigned reg)
  60. {
  61. if (val == readl_relaxed(base + reg))
  62. return;
  63. if (outer_cache.write_sec)
  64. outer_cache.write_sec(val, reg);
  65. else
  66. writel_relaxed(val, base + reg);
  67. }
  68. /*
  69. * This should only be called when we have a requirement that the
  70. * register be written due to a work-around, as platforms running
  71. * in non-secure mode may not be able to access this register.
  72. */
  73. static inline void l2c_set_debug(void __iomem *base, unsigned long val)
  74. {
  75. if (outer_cache.set_debug)
  76. outer_cache.set_debug(val);
  77. else
  78. l2c_write_sec(val, base, L2X0_DEBUG_CTRL);
  79. }
  80. static void __l2c_op_way(void __iomem *reg)
  81. {
  82. writel_relaxed(l2x0_way_mask, reg);
  83. l2c_wait_mask(reg, l2x0_way_mask);
  84. }
  85. static inline void l2c_unlock(void __iomem *base, unsigned num)
  86. {
  87. unsigned i;
  88. for (i = 0; i < num; i++) {
  89. writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_D_BASE +
  90. i * L2X0_LOCKDOWN_STRIDE);
  91. writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_I_BASE +
  92. i * L2X0_LOCKDOWN_STRIDE);
  93. }
  94. }
  95. /*
  96. * Enable the L2 cache controller. This function must only be
  97. * called when the cache controller is known to be disabled.
  98. */
  99. static void l2c_enable(void __iomem *base, u32 aux, unsigned num_lock)
  100. {
  101. unsigned long flags;
  102. l2c_write_sec(aux, base, L2X0_AUX_CTRL);
  103. l2c_unlock(base, num_lock);
  104. local_irq_save(flags);
  105. __l2c_op_way(base + L2X0_INV_WAY);
  106. writel_relaxed(0, base + sync_reg_offset);
  107. l2c_wait_mask(base + sync_reg_offset, 1);
  108. local_irq_restore(flags);
  109. l2c_write_sec(L2X0_CTRL_EN, base, L2X0_CTRL);
  110. }
  111. static void l2c_disable(void)
  112. {
  113. void __iomem *base = l2x0_base;
  114. outer_cache.flush_all();
  115. l2c_write_sec(0, base, L2X0_CTRL);
  116. dsb(st);
  117. }
  118. #ifdef CONFIG_CACHE_PL310
  119. static inline void cache_wait(void __iomem *reg, unsigned long mask)
  120. {
  121. /* cache operations by line are atomic on PL310 */
  122. }
  123. #else
  124. #define cache_wait l2c_wait_mask
  125. #endif
  126. static inline void cache_sync(void)
  127. {
  128. void __iomem *base = l2x0_base;
  129. writel_relaxed(0, base + sync_reg_offset);
  130. cache_wait(base + L2X0_CACHE_SYNC, 1);
  131. }
  132. #if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
  133. static inline void debug_writel(unsigned long val)
  134. {
  135. if (outer_cache.set_debug || outer_cache.write_sec)
  136. l2c_set_debug(l2x0_base, val);
  137. }
  138. #else
  139. /* Optimised out for non-errata case */
  140. static inline void debug_writel(unsigned long val)
  141. {
  142. }
  143. #endif
  144. static void l2x0_cache_sync(void)
  145. {
  146. unsigned long flags;
  147. raw_spin_lock_irqsave(&l2x0_lock, flags);
  148. cache_sync();
  149. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  150. }
  151. static void __l2x0_flush_all(void)
  152. {
  153. debug_writel(0x03);
  154. __l2c_op_way(l2x0_base + L2X0_CLEAN_INV_WAY);
  155. cache_sync();
  156. debug_writel(0x00);
  157. }
  158. static void l2x0_flush_all(void)
  159. {
  160. unsigned long flags;
  161. /* clean all ways */
  162. raw_spin_lock_irqsave(&l2x0_lock, flags);
  163. __l2x0_flush_all();
  164. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  165. }
  166. static void l2x0_disable(void)
  167. {
  168. unsigned long flags;
  169. raw_spin_lock_irqsave(&l2x0_lock, flags);
  170. __l2x0_flush_all();
  171. l2c_write_sec(0, l2x0_base, L2X0_CTRL);
  172. dsb(st);
  173. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  174. }
  175. static void l2c_save(void __iomem *base)
  176. {
  177. l2x0_saved_regs.aux_ctrl = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  178. }
  179. /*
  180. * L2C-210 specific code.
  181. *
  182. * The L2C-2x0 PA, set/way and sync operations are atomic, but we must
  183. * ensure that no background operation is running. The way operations
  184. * are all background tasks.
  185. *
  186. * While a background operation is in progress, any new operation is
  187. * ignored (unspecified whether this causes an error.) Thankfully, not
  188. * used on SMP.
  189. *
  190. * Never has a different sync register other than L2X0_CACHE_SYNC, but
  191. * we use sync_reg_offset here so we can share some of this with L2C-310.
  192. */
  193. static void __l2c210_cache_sync(void __iomem *base)
  194. {
  195. writel_relaxed(0, base + sync_reg_offset);
  196. }
  197. static void __l2c210_op_pa_range(void __iomem *reg, unsigned long start,
  198. unsigned long end)
  199. {
  200. while (start < end) {
  201. writel_relaxed(start, reg);
  202. start += CACHE_LINE_SIZE;
  203. }
  204. }
  205. static void l2c210_inv_range(unsigned long start, unsigned long end)
  206. {
  207. void __iomem *base = l2x0_base;
  208. if (start & (CACHE_LINE_SIZE - 1)) {
  209. start &= ~(CACHE_LINE_SIZE - 1);
  210. writel_relaxed(start, base + L2X0_CLEAN_INV_LINE_PA);
  211. start += CACHE_LINE_SIZE;
  212. }
  213. if (end & (CACHE_LINE_SIZE - 1)) {
  214. end &= ~(CACHE_LINE_SIZE - 1);
  215. writel_relaxed(end, base + L2X0_CLEAN_INV_LINE_PA);
  216. }
  217. __l2c210_op_pa_range(base + L2X0_INV_LINE_PA, start, end);
  218. __l2c210_cache_sync(base);
  219. }
  220. static void l2c210_clean_range(unsigned long start, unsigned long end)
  221. {
  222. void __iomem *base = l2x0_base;
  223. start &= ~(CACHE_LINE_SIZE - 1);
  224. __l2c210_op_pa_range(base + L2X0_CLEAN_LINE_PA, start, end);
  225. __l2c210_cache_sync(base);
  226. }
  227. static void l2c210_flush_range(unsigned long start, unsigned long end)
  228. {
  229. void __iomem *base = l2x0_base;
  230. start &= ~(CACHE_LINE_SIZE - 1);
  231. __l2c210_op_pa_range(base + L2X0_CLEAN_INV_LINE_PA, start, end);
  232. __l2c210_cache_sync(base);
  233. }
  234. static void l2c210_flush_all(void)
  235. {
  236. void __iomem *base = l2x0_base;
  237. BUG_ON(!irqs_disabled());
  238. __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
  239. __l2c210_cache_sync(base);
  240. }
  241. static void l2c210_sync(void)
  242. {
  243. __l2c210_cache_sync(l2x0_base);
  244. }
  245. static void l2c210_resume(void)
  246. {
  247. void __iomem *base = l2x0_base;
  248. if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN))
  249. l2c_enable(base, l2x0_saved_regs.aux_ctrl, 1);
  250. }
  251. static const struct l2c_init_data l2c210_data __initconst = {
  252. .type = "L2C-210",
  253. .way_size_0 = SZ_8K,
  254. .num_lock = 1,
  255. .enable = l2c_enable,
  256. .save = l2c_save,
  257. .outer_cache = {
  258. .inv_range = l2c210_inv_range,
  259. .clean_range = l2c210_clean_range,
  260. .flush_range = l2c210_flush_range,
  261. .flush_all = l2c210_flush_all,
  262. .disable = l2c_disable,
  263. .sync = l2c210_sync,
  264. .resume = l2c210_resume,
  265. },
  266. };
  267. /*
  268. * L2C-220 specific code.
  269. *
  270. * All operations are background operations: they have to be waited for.
  271. * Conflicting requests generate a slave error (which will cause an
  272. * imprecise abort.) Never uses sync_reg_offset, so we hard-code the
  273. * sync register here.
  274. *
  275. * However, we can re-use the l2c210_resume call.
  276. */
  277. static inline void __l2c220_cache_sync(void __iomem *base)
  278. {
  279. writel_relaxed(0, base + L2X0_CACHE_SYNC);
  280. l2c_wait_mask(base + L2X0_CACHE_SYNC, 1);
  281. }
  282. static void l2c220_op_way(void __iomem *base, unsigned reg)
  283. {
  284. unsigned long flags;
  285. raw_spin_lock_irqsave(&l2x0_lock, flags);
  286. __l2c_op_way(base + reg);
  287. __l2c220_cache_sync(base);
  288. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  289. }
  290. static unsigned long l2c220_op_pa_range(void __iomem *reg, unsigned long start,
  291. unsigned long end, unsigned long flags)
  292. {
  293. raw_spinlock_t *lock = &l2x0_lock;
  294. while (start < end) {
  295. unsigned long blk_end = start + min(end - start, 4096UL);
  296. while (start < blk_end) {
  297. l2c_wait_mask(reg, 1);
  298. writel_relaxed(start, reg);
  299. start += CACHE_LINE_SIZE;
  300. }
  301. if (blk_end < end) {
  302. raw_spin_unlock_irqrestore(lock, flags);
  303. raw_spin_lock_irqsave(lock, flags);
  304. }
  305. }
  306. return flags;
  307. }
  308. static void l2c220_inv_range(unsigned long start, unsigned long end)
  309. {
  310. void __iomem *base = l2x0_base;
  311. unsigned long flags;
  312. raw_spin_lock_irqsave(&l2x0_lock, flags);
  313. if ((start | end) & (CACHE_LINE_SIZE - 1)) {
  314. if (start & (CACHE_LINE_SIZE - 1)) {
  315. start &= ~(CACHE_LINE_SIZE - 1);
  316. writel_relaxed(start, base + L2X0_CLEAN_INV_LINE_PA);
  317. start += CACHE_LINE_SIZE;
  318. }
  319. if (end & (CACHE_LINE_SIZE - 1)) {
  320. end &= ~(CACHE_LINE_SIZE - 1);
  321. l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
  322. writel_relaxed(end, base + L2X0_CLEAN_INV_LINE_PA);
  323. }
  324. }
  325. flags = l2c220_op_pa_range(base + L2X0_INV_LINE_PA,
  326. start, end, flags);
  327. l2c_wait_mask(base + L2X0_INV_LINE_PA, 1);
  328. __l2c220_cache_sync(base);
  329. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  330. }
  331. static void l2c220_clean_range(unsigned long start, unsigned long end)
  332. {
  333. void __iomem *base = l2x0_base;
  334. unsigned long flags;
  335. start &= ~(CACHE_LINE_SIZE - 1);
  336. if ((end - start) >= l2x0_size) {
  337. l2c220_op_way(base, L2X0_CLEAN_WAY);
  338. return;
  339. }
  340. raw_spin_lock_irqsave(&l2x0_lock, flags);
  341. flags = l2c220_op_pa_range(base + L2X0_CLEAN_LINE_PA,
  342. start, end, flags);
  343. l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
  344. __l2c220_cache_sync(base);
  345. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  346. }
  347. static void l2c220_flush_range(unsigned long start, unsigned long end)
  348. {
  349. void __iomem *base = l2x0_base;
  350. unsigned long flags;
  351. start &= ~(CACHE_LINE_SIZE - 1);
  352. if ((end - start) >= l2x0_size) {
  353. l2c220_op_way(base, L2X0_CLEAN_INV_WAY);
  354. return;
  355. }
  356. raw_spin_lock_irqsave(&l2x0_lock, flags);
  357. flags = l2c220_op_pa_range(base + L2X0_CLEAN_INV_LINE_PA,
  358. start, end, flags);
  359. l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
  360. __l2c220_cache_sync(base);
  361. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  362. }
  363. static void l2c220_flush_all(void)
  364. {
  365. l2c220_op_way(l2x0_base, L2X0_CLEAN_INV_WAY);
  366. }
  367. static void l2c220_sync(void)
  368. {
  369. unsigned long flags;
  370. raw_spin_lock_irqsave(&l2x0_lock, flags);
  371. __l2c220_cache_sync(l2x0_base);
  372. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  373. }
  374. static const struct l2c_init_data l2c220_data = {
  375. .type = "L2C-220",
  376. .way_size_0 = SZ_8K,
  377. .num_lock = 1,
  378. .enable = l2c_enable,
  379. .save = l2c_save,
  380. .outer_cache = {
  381. .inv_range = l2c220_inv_range,
  382. .clean_range = l2c220_clean_range,
  383. .flush_range = l2c220_flush_range,
  384. .flush_all = l2c220_flush_all,
  385. .disable = l2c_disable,
  386. .sync = l2c220_sync,
  387. .resume = l2c210_resume,
  388. },
  389. };
  390. /*
  391. * L2C-310 specific code.
  392. *
  393. * Very similar to L2C-210, the PA, set/way and sync operations are atomic,
  394. * and the way operations are all background tasks. However, issuing an
  395. * operation while a background operation is in progress results in a
  396. * SLVERR response. We can reuse:
  397. *
  398. * __l2c210_cache_sync (using sync_reg_offset)
  399. * l2c210_sync
  400. * l2c210_inv_range (if 588369 is not applicable)
  401. * l2c210_clean_range
  402. * l2c210_flush_range (if 588369 is not applicable)
  403. * l2c210_flush_all (if 727915 is not applicable)
  404. *
  405. * Errata:
  406. * 588369: PL310 R0P0->R1P0, fixed R2P0.
  407. * Affects: all clean+invalidate operations
  408. * clean and invalidate skips the invalidate step, so we need to issue
  409. * separate operations. We also require the above debug workaround
  410. * enclosing this code fragment on affected parts. On unaffected parts,
  411. * we must not use this workaround without the debug register writes
  412. * to avoid exposing a problem similar to 727915.
  413. *
  414. * 727915: PL310 R2P0->R3P0, fixed R3P1.
  415. * Affects: clean+invalidate by way
  416. * clean and invalidate by way runs in the background, and a store can
  417. * hit the line between the clean operation and invalidate operation,
  418. * resulting in the store being lost.
  419. *
  420. * 752271: PL310 R3P0->R3P1-50REL0, fixed R3P2.
  421. * Affects: 8x64-bit (double fill) line fetches
  422. * double fill line fetches can fail to cause dirty data to be evicted
  423. * from the cache before the new data overwrites the second line.
  424. *
  425. * 753970: PL310 R3P0, fixed R3P1.
  426. * Affects: sync
  427. * prevents merging writes after the sync operation, until another L2C
  428. * operation is performed (or a number of other conditions.)
  429. *
  430. * 769419: PL310 R0P0->R3P1, fixed R3P2.
  431. * Affects: store buffer
  432. * store buffer is not automatically drained.
  433. */
  434. static void l2c310_set_debug(unsigned long val)
  435. {
  436. writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
  437. }
  438. static void l2c310_inv_range_erratum(unsigned long start, unsigned long end)
  439. {
  440. void __iomem *base = l2x0_base;
  441. if ((start | end) & (CACHE_LINE_SIZE - 1)) {
  442. unsigned long flags;
  443. /* Erratum 588369 for both clean+invalidate operations */
  444. raw_spin_lock_irqsave(&l2x0_lock, flags);
  445. l2c_set_debug(base, 0x03);
  446. if (start & (CACHE_LINE_SIZE - 1)) {
  447. start &= ~(CACHE_LINE_SIZE - 1);
  448. writel_relaxed(start, base + L2X0_CLEAN_LINE_PA);
  449. writel_relaxed(start, base + L2X0_INV_LINE_PA);
  450. start += CACHE_LINE_SIZE;
  451. }
  452. if (end & (CACHE_LINE_SIZE - 1)) {
  453. end &= ~(CACHE_LINE_SIZE - 1);
  454. writel_relaxed(end, base + L2X0_CLEAN_LINE_PA);
  455. writel_relaxed(end, base + L2X0_INV_LINE_PA);
  456. }
  457. l2c_set_debug(base, 0x00);
  458. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  459. }
  460. __l2c210_op_pa_range(base + L2X0_INV_LINE_PA, start, end);
  461. __l2c210_cache_sync(base);
  462. }
  463. static void l2c310_flush_range_erratum(unsigned long start, unsigned long end)
  464. {
  465. raw_spinlock_t *lock = &l2x0_lock;
  466. unsigned long flags;
  467. void __iomem *base = l2x0_base;
  468. raw_spin_lock_irqsave(lock, flags);
  469. while (start < end) {
  470. unsigned long blk_end = start + min(end - start, 4096UL);
  471. l2c_set_debug(base, 0x03);
  472. while (start < blk_end) {
  473. writel_relaxed(start, base + L2X0_CLEAN_LINE_PA);
  474. writel_relaxed(start, base + L2X0_INV_LINE_PA);
  475. start += CACHE_LINE_SIZE;
  476. }
  477. l2c_set_debug(base, 0x00);
  478. if (blk_end < end) {
  479. raw_spin_unlock_irqrestore(lock, flags);
  480. raw_spin_lock_irqsave(lock, flags);
  481. }
  482. }
  483. raw_spin_unlock_irqrestore(lock, flags);
  484. __l2c210_cache_sync(base);
  485. }
  486. static void l2c310_flush_all_erratum(void)
  487. {
  488. void __iomem *base = l2x0_base;
  489. unsigned long flags;
  490. raw_spin_lock_irqsave(&l2x0_lock, flags);
  491. l2c_set_debug(base, 0x03);
  492. __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
  493. l2c_set_debug(base, 0x00);
  494. __l2c210_cache_sync(base);
  495. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  496. }
  497. static void __init l2c310_save(void __iomem *base)
  498. {
  499. unsigned revision;
  500. l2c_save(base);
  501. l2x0_saved_regs.tag_latency = readl_relaxed(base +
  502. L310_TAG_LATENCY_CTRL);
  503. l2x0_saved_regs.data_latency = readl_relaxed(base +
  504. L310_DATA_LATENCY_CTRL);
  505. l2x0_saved_regs.filter_end = readl_relaxed(base +
  506. L310_ADDR_FILTER_END);
  507. l2x0_saved_regs.filter_start = readl_relaxed(base +
  508. L310_ADDR_FILTER_START);
  509. revision = readl_relaxed(base + L2X0_CACHE_ID) &
  510. L2X0_CACHE_ID_RTL_MASK;
  511. /* From r2p0, there is Prefetch offset/control register */
  512. if (revision >= L310_CACHE_ID_RTL_R2P0)
  513. l2x0_saved_regs.prefetch_ctrl = readl_relaxed(base +
  514. L310_PREFETCH_CTRL);
  515. /* From r3p0, there is Power control register */
  516. if (revision >= L310_CACHE_ID_RTL_R3P0)
  517. l2x0_saved_regs.pwr_ctrl = readl_relaxed(base +
  518. L310_POWER_CTRL);
  519. }
  520. static void l2c310_resume(void)
  521. {
  522. void __iomem *base = l2x0_base;
  523. if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  524. unsigned revision;
  525. /* restore pl310 setup */
  526. writel_relaxed(l2x0_saved_regs.tag_latency,
  527. base + L310_TAG_LATENCY_CTRL);
  528. writel_relaxed(l2x0_saved_regs.data_latency,
  529. base + L310_DATA_LATENCY_CTRL);
  530. writel_relaxed(l2x0_saved_regs.filter_end,
  531. base + L310_ADDR_FILTER_END);
  532. writel_relaxed(l2x0_saved_regs.filter_start,
  533. base + L310_ADDR_FILTER_START);
  534. revision = readl_relaxed(base + L2X0_CACHE_ID) &
  535. L2X0_CACHE_ID_RTL_MASK;
  536. if (revision >= L310_CACHE_ID_RTL_R2P0)
  537. l2c_write_sec(l2x0_saved_regs.prefetch_ctrl, base,
  538. L310_PREFETCH_CTRL);
  539. if (revision >= L310_CACHE_ID_RTL_R3P0)
  540. l2c_write_sec(l2x0_saved_regs.pwr_ctrl, base,
  541. L310_POWER_CTRL);
  542. l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
  543. }
  544. }
  545. static void __init l2c310_fixup(void __iomem *base, u32 cache_id,
  546. struct outer_cache_fns *fns)
  547. {
  548. unsigned revision = cache_id & L2X0_CACHE_ID_RTL_MASK;
  549. const char *errata[8];
  550. unsigned n = 0;
  551. /* For compatibility */
  552. if (revision <= L310_CACHE_ID_RTL_R3P0)
  553. fns->set_debug = l2c310_set_debug;
  554. if (IS_ENABLED(CONFIG_PL310_ERRATA_588369) &&
  555. revision < L310_CACHE_ID_RTL_R2P0 &&
  556. /* For bcm compatibility */
  557. fns->inv_range == l2c210_inv_range) {
  558. fns->inv_range = l2c310_inv_range_erratum;
  559. fns->flush_range = l2c310_flush_range_erratum;
  560. errata[n++] = "588369";
  561. }
  562. if (IS_ENABLED(CONFIG_PL310_ERRATA_727915) &&
  563. revision >= L310_CACHE_ID_RTL_R2P0 &&
  564. revision < L310_CACHE_ID_RTL_R3P1) {
  565. fns->flush_all = l2c310_flush_all_erratum;
  566. errata[n++] = "727915";
  567. }
  568. if (revision >= L310_CACHE_ID_RTL_R3P0 &&
  569. revision < L310_CACHE_ID_RTL_R3P2) {
  570. u32 val = readl_relaxed(base + L310_PREFETCH_CTRL);
  571. /* I don't think bit23 is required here... but iMX6 does so */
  572. if (val & (BIT(30) | BIT(23))) {
  573. val &= ~(BIT(30) | BIT(23));
  574. l2c_write_sec(val, base, L310_PREFETCH_CTRL);
  575. errata[n++] = "752271";
  576. }
  577. }
  578. if (IS_ENABLED(CONFIG_PL310_ERRATA_753970) &&
  579. revision == L310_CACHE_ID_RTL_R3P0) {
  580. sync_reg_offset = L2X0_DUMMY_REG;
  581. errata[n++] = "753970";
  582. }
  583. if (IS_ENABLED(CONFIG_PL310_ERRATA_769419))
  584. errata[n++] = "769419";
  585. if (n) {
  586. unsigned i;
  587. pr_info("L2C-310 errat%s", n > 1 ? "a" : "um");
  588. for (i = 0; i < n; i++)
  589. pr_cont(" %s", errata[i]);
  590. pr_cont(" enabled\n");
  591. }
  592. }
  593. static const struct l2c_init_data l2c310_init_fns __initconst = {
  594. .type = "L2C-310",
  595. .way_size_0 = SZ_8K,
  596. .num_lock = 8,
  597. .enable = l2c_enable,
  598. .fixup = l2c310_fixup,
  599. .save = l2c310_save,
  600. .outer_cache = {
  601. .inv_range = l2c210_inv_range,
  602. .clean_range = l2c210_clean_range,
  603. .flush_range = l2c210_flush_range,
  604. .flush_all = l2c210_flush_all,
  605. .disable = l2c_disable,
  606. .sync = l2c210_sync,
  607. .set_debug = l2c310_set_debug,
  608. .resume = l2c310_resume,
  609. },
  610. };
  611. static void __init __l2c_init(const struct l2c_init_data *data,
  612. u32 aux_val, u32 aux_mask, u32 cache_id)
  613. {
  614. struct outer_cache_fns fns;
  615. unsigned way_size_bits, ways;
  616. u32 aux;
  617. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  618. aux &= aux_mask;
  619. aux |= aux_val;
  620. /* Determine the number of ways */
  621. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  622. case L2X0_CACHE_ID_PART_L310:
  623. if (aux & (1 << 16))
  624. ways = 16;
  625. else
  626. ways = 8;
  627. break;
  628. case L2X0_CACHE_ID_PART_L210:
  629. case L2X0_CACHE_ID_PART_L220:
  630. ways = (aux >> 13) & 0xf;
  631. break;
  632. case AURORA_CACHE_ID:
  633. ways = (aux >> 13) & 0xf;
  634. ways = 2 << ((ways + 1) >> 2);
  635. break;
  636. default:
  637. /* Assume unknown chips have 8 ways */
  638. ways = 8;
  639. break;
  640. }
  641. l2x0_way_mask = (1 << ways) - 1;
  642. /*
  643. * way_size_0 is the size that a way_size value of zero would be
  644. * given the calculation: way_size = way_size_0 << way_size_bits.
  645. * So, if way_size_bits=0 is reserved, but way_size_bits=1 is 16k,
  646. * then way_size_0 would be 8k.
  647. *
  648. * L2 cache size = number of ways * way size.
  649. */
  650. way_size_bits = (aux & L2C_AUX_CTRL_WAY_SIZE_MASK) >>
  651. L2C_AUX_CTRL_WAY_SIZE_SHIFT;
  652. l2x0_size = ways * (data->way_size_0 << way_size_bits);
  653. fns = data->outer_cache;
  654. fns.write_sec = outer_cache.write_sec;
  655. if (data->fixup)
  656. data->fixup(l2x0_base, cache_id, &fns);
  657. if (fns.write_sec)
  658. fns.set_debug = NULL;
  659. /*
  660. * Check if l2x0 controller is already enabled. If we are booting
  661. * in non-secure mode accessing the below registers will fault.
  662. */
  663. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
  664. data->enable(l2x0_base, aux, data->num_lock);
  665. outer_cache = fns;
  666. /*
  667. * It is strange to save the register state before initialisation,
  668. * but hey, this is what the DT implementations decided to do.
  669. */
  670. if (data->save)
  671. data->save(l2x0_base);
  672. /* Re-read it in case some bits are reserved. */
  673. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  674. pr_info("%s cache controller enabled, %d ways, %d kB\n",
  675. data->type, ways, l2x0_size >> 10);
  676. pr_info("%s: CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
  677. data->type, cache_id, aux);
  678. }
  679. void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
  680. {
  681. const struct l2c_init_data *data;
  682. u32 cache_id;
  683. l2x0_base = base;
  684. cache_id = readl_relaxed(base + L2X0_CACHE_ID);
  685. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  686. default:
  687. case L2X0_CACHE_ID_PART_L210:
  688. data = &l2c210_data;
  689. break;
  690. case L2X0_CACHE_ID_PART_L220:
  691. data = &l2c220_data;
  692. break;
  693. case L2X0_CACHE_ID_PART_L310:
  694. data = &l2c310_init_fns;
  695. break;
  696. }
  697. __l2c_init(data, aux_val, aux_mask, cache_id);
  698. }
  699. #ifdef CONFIG_OF
  700. static int l2_wt_override;
  701. /* Aurora don't have the cache ID register available, so we have to
  702. * pass it though the device tree */
  703. static u32 cache_id_part_number_from_dt;
  704. static void __init l2x0_of_parse(const struct device_node *np,
  705. u32 *aux_val, u32 *aux_mask)
  706. {
  707. u32 data[2] = { 0, 0 };
  708. u32 tag = 0;
  709. u32 dirty = 0;
  710. u32 val = 0, mask = 0;
  711. of_property_read_u32(np, "arm,tag-latency", &tag);
  712. if (tag) {
  713. mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
  714. val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
  715. }
  716. of_property_read_u32_array(np, "arm,data-latency",
  717. data, ARRAY_SIZE(data));
  718. if (data[0] && data[1]) {
  719. mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
  720. L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
  721. val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
  722. ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
  723. }
  724. of_property_read_u32(np, "arm,dirty-latency", &dirty);
  725. if (dirty) {
  726. mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
  727. val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
  728. }
  729. *aux_val &= ~mask;
  730. *aux_val |= val;
  731. *aux_mask &= ~mask;
  732. }
  733. static const struct l2c_init_data of_l2c210_data __initconst = {
  734. .type = "L2C-210",
  735. .way_size_0 = SZ_8K,
  736. .num_lock = 1,
  737. .of_parse = l2x0_of_parse,
  738. .enable = l2c_enable,
  739. .save = l2c_save,
  740. .outer_cache = {
  741. .inv_range = l2c210_inv_range,
  742. .clean_range = l2c210_clean_range,
  743. .flush_range = l2c210_flush_range,
  744. .flush_all = l2c210_flush_all,
  745. .disable = l2c_disable,
  746. .sync = l2c210_sync,
  747. .resume = l2c210_resume,
  748. },
  749. };
  750. static const struct l2c_init_data of_l2c220_data __initconst = {
  751. .type = "L2C-220",
  752. .way_size_0 = SZ_8K,
  753. .num_lock = 1,
  754. .of_parse = l2x0_of_parse,
  755. .enable = l2c_enable,
  756. .save = l2c_save,
  757. .outer_cache = {
  758. .inv_range = l2c220_inv_range,
  759. .clean_range = l2c220_clean_range,
  760. .flush_range = l2c220_flush_range,
  761. .flush_all = l2c220_flush_all,
  762. .disable = l2c_disable,
  763. .sync = l2c220_sync,
  764. .resume = l2c210_resume,
  765. },
  766. };
  767. static void __init l2c310_of_parse(const struct device_node *np,
  768. u32 *aux_val, u32 *aux_mask)
  769. {
  770. u32 data[3] = { 0, 0, 0 };
  771. u32 tag[3] = { 0, 0, 0 };
  772. u32 filter[2] = { 0, 0 };
  773. of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
  774. if (tag[0] && tag[1] && tag[2])
  775. writel_relaxed(
  776. L310_LATENCY_CTRL_RD(tag[0] - 1) |
  777. L310_LATENCY_CTRL_WR(tag[1] - 1) |
  778. L310_LATENCY_CTRL_SETUP(tag[2] - 1),
  779. l2x0_base + L310_TAG_LATENCY_CTRL);
  780. of_property_read_u32_array(np, "arm,data-latency",
  781. data, ARRAY_SIZE(data));
  782. if (data[0] && data[1] && data[2])
  783. writel_relaxed(
  784. L310_LATENCY_CTRL_RD(data[0] - 1) |
  785. L310_LATENCY_CTRL_WR(data[1] - 1) |
  786. L310_LATENCY_CTRL_SETUP(data[2] - 1),
  787. l2x0_base + L310_DATA_LATENCY_CTRL);
  788. of_property_read_u32_array(np, "arm,filter-ranges",
  789. filter, ARRAY_SIZE(filter));
  790. if (filter[1]) {
  791. writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
  792. l2x0_base + L310_ADDR_FILTER_END);
  793. writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L310_ADDR_FILTER_EN,
  794. l2x0_base + L310_ADDR_FILTER_START);
  795. }
  796. }
  797. static const struct l2c_init_data of_l2c310_data __initconst = {
  798. .type = "L2C-310",
  799. .way_size_0 = SZ_8K,
  800. .num_lock = 8,
  801. .of_parse = l2c310_of_parse,
  802. .enable = l2c_enable,
  803. .fixup = l2c310_fixup,
  804. .save = l2c310_save,
  805. .outer_cache = {
  806. .inv_range = l2c210_inv_range,
  807. .clean_range = l2c210_clean_range,
  808. .flush_range = l2c210_flush_range,
  809. .flush_all = l2c210_flush_all,
  810. .disable = l2c_disable,
  811. .sync = l2c210_sync,
  812. .set_debug = l2c310_set_debug,
  813. .resume = l2c310_resume,
  814. },
  815. };
  816. /*
  817. * Note that the end addresses passed to Linux primitives are
  818. * noninclusive, while the hardware cache range operations use
  819. * inclusive start and end addresses.
  820. */
  821. static unsigned long calc_range_end(unsigned long start, unsigned long end)
  822. {
  823. /*
  824. * Limit the number of cache lines processed at once,
  825. * since cache range operations stall the CPU pipeline
  826. * until completion.
  827. */
  828. if (end > start + MAX_RANGE_SIZE)
  829. end = start + MAX_RANGE_SIZE;
  830. /*
  831. * Cache range operations can't straddle a page boundary.
  832. */
  833. if (end > PAGE_ALIGN(start+1))
  834. end = PAGE_ALIGN(start+1);
  835. return end;
  836. }
  837. /*
  838. * Make sure 'start' and 'end' reference the same page, as L2 is PIPT
  839. * and range operations only do a TLB lookup on the start address.
  840. */
  841. static void aurora_pa_range(unsigned long start, unsigned long end,
  842. unsigned long offset)
  843. {
  844. unsigned long flags;
  845. raw_spin_lock_irqsave(&l2x0_lock, flags);
  846. writel_relaxed(start, l2x0_base + AURORA_RANGE_BASE_ADDR_REG);
  847. writel_relaxed(end, l2x0_base + offset);
  848. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  849. cache_sync();
  850. }
  851. static void aurora_inv_range(unsigned long start, unsigned long end)
  852. {
  853. /*
  854. * round start and end adresses up to cache line size
  855. */
  856. start &= ~(CACHE_LINE_SIZE - 1);
  857. end = ALIGN(end, CACHE_LINE_SIZE);
  858. /*
  859. * Invalidate all full cache lines between 'start' and 'end'.
  860. */
  861. while (start < end) {
  862. unsigned long range_end = calc_range_end(start, end);
  863. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  864. AURORA_INVAL_RANGE_REG);
  865. start = range_end;
  866. }
  867. }
  868. static void aurora_clean_range(unsigned long start, unsigned long end)
  869. {
  870. /*
  871. * If L2 is forced to WT, the L2 will always be clean and we
  872. * don't need to do anything here.
  873. */
  874. if (!l2_wt_override) {
  875. start &= ~(CACHE_LINE_SIZE - 1);
  876. end = ALIGN(end, CACHE_LINE_SIZE);
  877. while (start != end) {
  878. unsigned long range_end = calc_range_end(start, end);
  879. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  880. AURORA_CLEAN_RANGE_REG);
  881. start = range_end;
  882. }
  883. }
  884. }
  885. static void aurora_flush_range(unsigned long start, unsigned long end)
  886. {
  887. start &= ~(CACHE_LINE_SIZE - 1);
  888. end = ALIGN(end, CACHE_LINE_SIZE);
  889. while (start != end) {
  890. unsigned long range_end = calc_range_end(start, end);
  891. /*
  892. * If L2 is forced to WT, the L2 will always be clean and we
  893. * just need to invalidate.
  894. */
  895. if (l2_wt_override)
  896. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  897. AURORA_INVAL_RANGE_REG);
  898. else
  899. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  900. AURORA_FLUSH_RANGE_REG);
  901. start = range_end;
  902. }
  903. }
  904. static void aurora_save(void __iomem *base)
  905. {
  906. l2x0_saved_regs.ctrl = readl_relaxed(base + L2X0_CTRL);
  907. l2x0_saved_regs.aux_ctrl = readl_relaxed(base + L2X0_AUX_CTRL);
  908. }
  909. static void aurora_resume(void)
  910. {
  911. void __iomem *base = l2x0_base;
  912. if (!(readl(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  913. writel_relaxed(l2x0_saved_regs.aux_ctrl, base + L2X0_AUX_CTRL);
  914. writel_relaxed(l2x0_saved_regs.ctrl, base + L2X0_CTRL);
  915. }
  916. }
  917. /*
  918. * For Aurora cache in no outer mode, enable via the CP15 coprocessor
  919. * broadcasting of cache commands to L2.
  920. */
  921. static void __init aurora_enable_no_outer(void __iomem *base, u32 aux,
  922. unsigned num_lock)
  923. {
  924. u32 u;
  925. asm volatile("mrc p15, 1, %0, c15, c2, 0" : "=r" (u));
  926. u |= AURORA_CTRL_FW; /* Set the FW bit */
  927. asm volatile("mcr p15, 1, %0, c15, c2, 0" : : "r" (u));
  928. isb();
  929. l2c_enable(base, aux, num_lock);
  930. }
  931. static void __init aurora_fixup(void __iomem *base, u32 cache_id,
  932. struct outer_cache_fns *fns)
  933. {
  934. sync_reg_offset = AURORA_SYNC_REG;
  935. }
  936. static void __init aurora_of_parse(const struct device_node *np,
  937. u32 *aux_val, u32 *aux_mask)
  938. {
  939. u32 val = AURORA_ACR_REPLACEMENT_TYPE_SEMIPLRU;
  940. u32 mask = AURORA_ACR_REPLACEMENT_MASK;
  941. of_property_read_u32(np, "cache-id-part",
  942. &cache_id_part_number_from_dt);
  943. /* Determine and save the write policy */
  944. l2_wt_override = of_property_read_bool(np, "wt-override");
  945. if (l2_wt_override) {
  946. val |= AURORA_ACR_FORCE_WRITE_THRO_POLICY;
  947. mask |= AURORA_ACR_FORCE_WRITE_POLICY_MASK;
  948. }
  949. *aux_val &= ~mask;
  950. *aux_val |= val;
  951. *aux_mask &= ~mask;
  952. }
  953. static const struct l2c_init_data of_aurora_with_outer_data __initconst = {
  954. .type = "Aurora",
  955. .way_size_0 = SZ_4K,
  956. .num_lock = 4,
  957. .of_parse = aurora_of_parse,
  958. .enable = l2c_enable,
  959. .fixup = aurora_fixup,
  960. .save = aurora_save,
  961. .outer_cache = {
  962. .inv_range = aurora_inv_range,
  963. .clean_range = aurora_clean_range,
  964. .flush_range = aurora_flush_range,
  965. .flush_all = l2x0_flush_all,
  966. .disable = l2x0_disable,
  967. .sync = l2x0_cache_sync,
  968. .resume = aurora_resume,
  969. },
  970. };
  971. static const struct l2c_init_data of_aurora_no_outer_data __initconst = {
  972. .type = "Aurora",
  973. .way_size_0 = SZ_4K,
  974. .num_lock = 4,
  975. .of_parse = aurora_of_parse,
  976. .enable = aurora_enable_no_outer,
  977. .fixup = aurora_fixup,
  978. .save = aurora_save,
  979. .outer_cache = {
  980. .resume = aurora_resume,
  981. },
  982. };
  983. /*
  984. * For certain Broadcom SoCs, depending on the address range, different offsets
  985. * need to be added to the address before passing it to L2 for
  986. * invalidation/clean/flush
  987. *
  988. * Section Address Range Offset EMI
  989. * 1 0x00000000 - 0x3FFFFFFF 0x80000000 VC
  990. * 2 0x40000000 - 0xBFFFFFFF 0x40000000 SYS
  991. * 3 0xC0000000 - 0xFFFFFFFF 0x80000000 VC
  992. *
  993. * When the start and end addresses have crossed two different sections, we
  994. * need to break the L2 operation into two, each within its own section.
  995. * For example, if we need to invalidate addresses starts at 0xBFFF0000 and
  996. * ends at 0xC0001000, we need do invalidate 1) 0xBFFF0000 - 0xBFFFFFFF and 2)
  997. * 0xC0000000 - 0xC0001000
  998. *
  999. * Note 1:
  1000. * By breaking a single L2 operation into two, we may potentially suffer some
  1001. * performance hit, but keep in mind the cross section case is very rare
  1002. *
  1003. * Note 2:
  1004. * We do not need to handle the case when the start address is in
  1005. * Section 1 and the end address is in Section 3, since it is not a valid use
  1006. * case
  1007. *
  1008. * Note 3:
  1009. * Section 1 in practical terms can no longer be used on rev A2. Because of
  1010. * that the code does not need to handle section 1 at all.
  1011. *
  1012. */
  1013. #define BCM_SYS_EMI_START_ADDR 0x40000000UL
  1014. #define BCM_VC_EMI_SEC3_START_ADDR 0xC0000000UL
  1015. #define BCM_SYS_EMI_OFFSET 0x40000000UL
  1016. #define BCM_VC_EMI_OFFSET 0x80000000UL
  1017. static inline int bcm_addr_is_sys_emi(unsigned long addr)
  1018. {
  1019. return (addr >= BCM_SYS_EMI_START_ADDR) &&
  1020. (addr < BCM_VC_EMI_SEC3_START_ADDR);
  1021. }
  1022. static inline unsigned long bcm_l2_phys_addr(unsigned long addr)
  1023. {
  1024. if (bcm_addr_is_sys_emi(addr))
  1025. return addr + BCM_SYS_EMI_OFFSET;
  1026. else
  1027. return addr + BCM_VC_EMI_OFFSET;
  1028. }
  1029. static void bcm_inv_range(unsigned long start, unsigned long end)
  1030. {
  1031. unsigned long new_start, new_end;
  1032. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  1033. if (unlikely(end <= start))
  1034. return;
  1035. new_start = bcm_l2_phys_addr(start);
  1036. new_end = bcm_l2_phys_addr(end);
  1037. /* normal case, no cross section between start and end */
  1038. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  1039. l2c210_inv_range(new_start, new_end);
  1040. return;
  1041. }
  1042. /* They cross sections, so it can only be a cross from section
  1043. * 2 to section 3
  1044. */
  1045. l2c210_inv_range(new_start,
  1046. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  1047. l2c210_inv_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  1048. new_end);
  1049. }
  1050. static void bcm_clean_range(unsigned long start, unsigned long end)
  1051. {
  1052. unsigned long new_start, new_end;
  1053. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  1054. if (unlikely(end <= start))
  1055. return;
  1056. new_start = bcm_l2_phys_addr(start);
  1057. new_end = bcm_l2_phys_addr(end);
  1058. /* normal case, no cross section between start and end */
  1059. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  1060. l2c210_clean_range(new_start, new_end);
  1061. return;
  1062. }
  1063. /* They cross sections, so it can only be a cross from section
  1064. * 2 to section 3
  1065. */
  1066. l2c210_clean_range(new_start,
  1067. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  1068. l2c210_clean_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  1069. new_end);
  1070. }
  1071. static void bcm_flush_range(unsigned long start, unsigned long end)
  1072. {
  1073. unsigned long new_start, new_end;
  1074. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  1075. if (unlikely(end <= start))
  1076. return;
  1077. if ((end - start) >= l2x0_size) {
  1078. outer_cache.flush_all();
  1079. return;
  1080. }
  1081. new_start = bcm_l2_phys_addr(start);
  1082. new_end = bcm_l2_phys_addr(end);
  1083. /* normal case, no cross section between start and end */
  1084. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  1085. l2c210_flush_range(new_start, new_end);
  1086. return;
  1087. }
  1088. /* They cross sections, so it can only be a cross from section
  1089. * 2 to section 3
  1090. */
  1091. l2c210_flush_range(new_start,
  1092. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  1093. l2c210_flush_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  1094. new_end);
  1095. }
  1096. /* Broadcom L2C-310 start from ARMs R3P2 or later, and require no fixups */
  1097. static const struct l2c_init_data of_bcm_l2x0_data __initconst = {
  1098. .type = "BCM-L2C-310",
  1099. .way_size_0 = SZ_8K,
  1100. .num_lock = 8,
  1101. .of_parse = l2c310_of_parse,
  1102. .enable = l2c_enable,
  1103. .save = l2c310_save,
  1104. .outer_cache = {
  1105. .inv_range = bcm_inv_range,
  1106. .clean_range = bcm_clean_range,
  1107. .flush_range = bcm_flush_range,
  1108. .flush_all = l2c210_flush_all,
  1109. .disable = l2c_disable,
  1110. .sync = l2c210_sync,
  1111. .resume = l2c310_resume,
  1112. },
  1113. };
  1114. static void __init tauros3_save(void __iomem *base)
  1115. {
  1116. l2c_save(base);
  1117. l2x0_saved_regs.aux2_ctrl =
  1118. readl_relaxed(base + TAUROS3_AUX2_CTRL);
  1119. l2x0_saved_regs.prefetch_ctrl =
  1120. readl_relaxed(base + L310_PREFETCH_CTRL);
  1121. }
  1122. static void tauros3_resume(void)
  1123. {
  1124. void __iomem *base = l2x0_base;
  1125. if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  1126. writel_relaxed(l2x0_saved_regs.aux2_ctrl,
  1127. base + TAUROS3_AUX2_CTRL);
  1128. writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
  1129. base + L310_PREFETCH_CTRL);
  1130. l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
  1131. }
  1132. }
  1133. static const struct l2c_init_data of_tauros3_data __initconst = {
  1134. .type = "Tauros3",
  1135. .way_size_0 = SZ_8K,
  1136. .num_lock = 8,
  1137. .enable = l2c_enable,
  1138. .save = tauros3_save,
  1139. /* Tauros3 broadcasts L1 cache operations to L2 */
  1140. .outer_cache = {
  1141. .resume = tauros3_resume,
  1142. },
  1143. };
  1144. #define L2C_ID(name, fns) { .compatible = name, .data = (void *)&fns }
  1145. static const struct of_device_id l2x0_ids[] __initconst = {
  1146. L2C_ID("arm,l210-cache", of_l2c210_data),
  1147. L2C_ID("arm,l220-cache", of_l2c220_data),
  1148. L2C_ID("arm,pl310-cache", of_l2c310_data),
  1149. L2C_ID("brcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
  1150. L2C_ID("marvell,aurora-outer-cache", of_aurora_with_outer_data),
  1151. L2C_ID("marvell,aurora-system-cache", of_aurora_no_outer_data),
  1152. L2C_ID("marvell,tauros3-cache", of_tauros3_data),
  1153. /* Deprecated IDs */
  1154. L2C_ID("bcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
  1155. {}
  1156. };
  1157. int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
  1158. {
  1159. const struct l2c_init_data *data;
  1160. struct device_node *np;
  1161. struct resource res;
  1162. u32 cache_id;
  1163. np = of_find_matching_node(NULL, l2x0_ids);
  1164. if (!np)
  1165. return -ENODEV;
  1166. if (of_address_to_resource(np, 0, &res))
  1167. return -ENODEV;
  1168. l2x0_base = ioremap(res.start, resource_size(&res));
  1169. if (!l2x0_base)
  1170. return -ENOMEM;
  1171. l2x0_saved_regs.phy_base = res.start;
  1172. data = of_match_node(l2x0_ids, np)->data;
  1173. /* All L2 caches are unified, so this property should be specified */
  1174. if (!of_property_read_bool(np, "cache-unified"))
  1175. pr_err("L2C: device tree omits to specify unified cache\n");
  1176. /* L2 configuration can only be changed if the cache is disabled */
  1177. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
  1178. if (data->of_parse)
  1179. data->of_parse(np, &aux_val, &aux_mask);
  1180. if (cache_id_part_number_from_dt)
  1181. cache_id = cache_id_part_number_from_dt;
  1182. else
  1183. cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
  1184. __l2c_init(data, aux_val, aux_mask, cache_id);
  1185. return 0;
  1186. }
  1187. #endif