cache-l2x0.c 36 KB

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