cache-l2x0.c 45 KB

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