cache-l2x0.c 46 KB

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