cache-l2x0.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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 int __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 -ENODEV;
  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 -EINVAL;
  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 -EINVAL;
  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. return 0;
  881. }
  882. static void __init l2x0_of_parse(const struct device_node *np,
  883. u32 *aux_val, u32 *aux_mask)
  884. {
  885. u32 data[2] = { 0, 0 };
  886. u32 tag = 0;
  887. u32 dirty = 0;
  888. u32 val = 0, mask = 0;
  889. u32 assoc;
  890. int ret;
  891. of_property_read_u32(np, "arm,tag-latency", &tag);
  892. if (tag) {
  893. mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
  894. val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
  895. }
  896. of_property_read_u32_array(np, "arm,data-latency",
  897. data, ARRAY_SIZE(data));
  898. if (data[0] && data[1]) {
  899. mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
  900. L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
  901. val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
  902. ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
  903. }
  904. of_property_read_u32(np, "arm,dirty-latency", &dirty);
  905. if (dirty) {
  906. mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
  907. val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
  908. }
  909. ret = l2x0_cache_size_of_parse(np, aux_val, aux_mask, &assoc, SZ_256K);
  910. if (ret)
  911. return;
  912. if (assoc > 8) {
  913. pr_err("l2x0 of: cache setting yield too high associativity\n");
  914. pr_err("l2x0 of: %d calculated, max 8\n", assoc);
  915. } else {
  916. mask |= L2X0_AUX_CTRL_ASSOC_MASK;
  917. val |= (assoc << L2X0_AUX_CTRL_ASSOC_SHIFT);
  918. }
  919. *aux_val &= ~mask;
  920. *aux_val |= val;
  921. *aux_mask &= ~mask;
  922. }
  923. static const struct l2c_init_data of_l2c210_data __initconst = {
  924. .type = "L2C-210",
  925. .way_size_0 = SZ_8K,
  926. .num_lock = 1,
  927. .of_parse = l2x0_of_parse,
  928. .enable = l2c_enable,
  929. .save = l2c_save,
  930. .outer_cache = {
  931. .inv_range = l2c210_inv_range,
  932. .clean_range = l2c210_clean_range,
  933. .flush_range = l2c210_flush_range,
  934. .flush_all = l2c210_flush_all,
  935. .disable = l2c_disable,
  936. .sync = l2c210_sync,
  937. .resume = l2c210_resume,
  938. },
  939. };
  940. static const struct l2c_init_data of_l2c220_data __initconst = {
  941. .type = "L2C-220",
  942. .way_size_0 = SZ_8K,
  943. .num_lock = 1,
  944. .of_parse = l2x0_of_parse,
  945. .enable = l2c220_enable,
  946. .save = l2c_save,
  947. .outer_cache = {
  948. .inv_range = l2c220_inv_range,
  949. .clean_range = l2c220_clean_range,
  950. .flush_range = l2c220_flush_range,
  951. .flush_all = l2c220_flush_all,
  952. .disable = l2c_disable,
  953. .sync = l2c220_sync,
  954. .resume = l2c210_resume,
  955. },
  956. };
  957. static void __init l2c310_of_parse(const struct device_node *np,
  958. u32 *aux_val, u32 *aux_mask)
  959. {
  960. u32 data[3] = { 0, 0, 0 };
  961. u32 tag[3] = { 0, 0, 0 };
  962. u32 filter[2] = { 0, 0 };
  963. u32 assoc;
  964. int ret;
  965. of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
  966. if (tag[0] && tag[1] && tag[2])
  967. writel_relaxed(
  968. L310_LATENCY_CTRL_RD(tag[0] - 1) |
  969. L310_LATENCY_CTRL_WR(tag[1] - 1) |
  970. L310_LATENCY_CTRL_SETUP(tag[2] - 1),
  971. l2x0_base + L310_TAG_LATENCY_CTRL);
  972. of_property_read_u32_array(np, "arm,data-latency",
  973. data, ARRAY_SIZE(data));
  974. if (data[0] && data[1] && data[2])
  975. writel_relaxed(
  976. L310_LATENCY_CTRL_RD(data[0] - 1) |
  977. L310_LATENCY_CTRL_WR(data[1] - 1) |
  978. L310_LATENCY_CTRL_SETUP(data[2] - 1),
  979. l2x0_base + L310_DATA_LATENCY_CTRL);
  980. of_property_read_u32_array(np, "arm,filter-ranges",
  981. filter, ARRAY_SIZE(filter));
  982. if (filter[1]) {
  983. writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
  984. l2x0_base + L310_ADDR_FILTER_END);
  985. writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L310_ADDR_FILTER_EN,
  986. l2x0_base + L310_ADDR_FILTER_START);
  987. }
  988. ret = l2x0_cache_size_of_parse(np, aux_val, aux_mask, &assoc, SZ_512K);
  989. if (ret)
  990. return;
  991. switch (assoc) {
  992. case 16:
  993. *aux_val &= ~L2X0_AUX_CTRL_ASSOC_MASK;
  994. *aux_val |= L310_AUX_CTRL_ASSOCIATIVITY_16;
  995. *aux_mask &= ~L2X0_AUX_CTRL_ASSOC_MASK;
  996. break;
  997. case 8:
  998. *aux_val &= ~L2X0_AUX_CTRL_ASSOC_MASK;
  999. *aux_mask &= ~L2X0_AUX_CTRL_ASSOC_MASK;
  1000. break;
  1001. default:
  1002. pr_err("L2C-310 OF cache associativity %d invalid, only 8 or 16 permitted\n",
  1003. assoc);
  1004. break;
  1005. }
  1006. }
  1007. static const struct l2c_init_data of_l2c310_data __initconst = {
  1008. .type = "L2C-310",
  1009. .way_size_0 = SZ_8K,
  1010. .num_lock = 8,
  1011. .of_parse = l2c310_of_parse,
  1012. .enable = l2c310_enable,
  1013. .fixup = l2c310_fixup,
  1014. .save = l2c310_save,
  1015. .outer_cache = {
  1016. .inv_range = l2c210_inv_range,
  1017. .clean_range = l2c210_clean_range,
  1018. .flush_range = l2c210_flush_range,
  1019. .flush_all = l2c210_flush_all,
  1020. .disable = l2c310_disable,
  1021. .sync = l2c210_sync,
  1022. .resume = l2c310_resume,
  1023. },
  1024. };
  1025. /*
  1026. * This is a variant of the of_l2c310_data with .sync set to
  1027. * NULL. Outer sync operations are not needed when the system is I/O
  1028. * coherent, and potentially harmful in certain situations (PCIe/PL310
  1029. * deadlock on Armada 375/38x due to hardware I/O coherency). The
  1030. * other operations are kept because they are infrequent (therefore do
  1031. * not cause the deadlock in practice) and needed for secondary CPU
  1032. * boot and other power management activities.
  1033. */
  1034. static const struct l2c_init_data of_l2c310_coherent_data __initconst = {
  1035. .type = "L2C-310 Coherent",
  1036. .way_size_0 = SZ_8K,
  1037. .num_lock = 8,
  1038. .of_parse = l2c310_of_parse,
  1039. .enable = l2c310_enable,
  1040. .fixup = l2c310_fixup,
  1041. .save = l2c310_save,
  1042. .outer_cache = {
  1043. .inv_range = l2c210_inv_range,
  1044. .clean_range = l2c210_clean_range,
  1045. .flush_range = l2c210_flush_range,
  1046. .flush_all = l2c210_flush_all,
  1047. .disable = l2c310_disable,
  1048. .resume = l2c310_resume,
  1049. },
  1050. };
  1051. /*
  1052. * Note that the end addresses passed to Linux primitives are
  1053. * noninclusive, while the hardware cache range operations use
  1054. * inclusive start and end addresses.
  1055. */
  1056. static unsigned long calc_range_end(unsigned long start, unsigned long end)
  1057. {
  1058. /*
  1059. * Limit the number of cache lines processed at once,
  1060. * since cache range operations stall the CPU pipeline
  1061. * until completion.
  1062. */
  1063. if (end > start + MAX_RANGE_SIZE)
  1064. end = start + MAX_RANGE_SIZE;
  1065. /*
  1066. * Cache range operations can't straddle a page boundary.
  1067. */
  1068. if (end > PAGE_ALIGN(start+1))
  1069. end = PAGE_ALIGN(start+1);
  1070. return end;
  1071. }
  1072. /*
  1073. * Make sure 'start' and 'end' reference the same page, as L2 is PIPT
  1074. * and range operations only do a TLB lookup on the start address.
  1075. */
  1076. static void aurora_pa_range(unsigned long start, unsigned long end,
  1077. unsigned long offset)
  1078. {
  1079. unsigned long flags;
  1080. raw_spin_lock_irqsave(&l2x0_lock, flags);
  1081. writel_relaxed(start, l2x0_base + AURORA_RANGE_BASE_ADDR_REG);
  1082. writel_relaxed(end, l2x0_base + offset);
  1083. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  1084. cache_sync();
  1085. }
  1086. static void aurora_inv_range(unsigned long start, unsigned long end)
  1087. {
  1088. /*
  1089. * round start and end adresses up to cache line size
  1090. */
  1091. start &= ~(CACHE_LINE_SIZE - 1);
  1092. end = ALIGN(end, CACHE_LINE_SIZE);
  1093. /*
  1094. * Invalidate all full cache lines between 'start' and 'end'.
  1095. */
  1096. while (start < end) {
  1097. unsigned long range_end = calc_range_end(start, end);
  1098. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  1099. AURORA_INVAL_RANGE_REG);
  1100. start = range_end;
  1101. }
  1102. }
  1103. static void aurora_clean_range(unsigned long start, unsigned long end)
  1104. {
  1105. /*
  1106. * If L2 is forced to WT, the L2 will always be clean and we
  1107. * don't need to do anything here.
  1108. */
  1109. if (!l2_wt_override) {
  1110. start &= ~(CACHE_LINE_SIZE - 1);
  1111. end = ALIGN(end, CACHE_LINE_SIZE);
  1112. while (start != end) {
  1113. unsigned long range_end = calc_range_end(start, end);
  1114. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  1115. AURORA_CLEAN_RANGE_REG);
  1116. start = range_end;
  1117. }
  1118. }
  1119. }
  1120. static void aurora_flush_range(unsigned long start, unsigned long end)
  1121. {
  1122. start &= ~(CACHE_LINE_SIZE - 1);
  1123. end = ALIGN(end, CACHE_LINE_SIZE);
  1124. while (start != end) {
  1125. unsigned long range_end = calc_range_end(start, end);
  1126. /*
  1127. * If L2 is forced to WT, the L2 will always be clean and we
  1128. * just need to invalidate.
  1129. */
  1130. if (l2_wt_override)
  1131. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  1132. AURORA_INVAL_RANGE_REG);
  1133. else
  1134. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  1135. AURORA_FLUSH_RANGE_REG);
  1136. start = range_end;
  1137. }
  1138. }
  1139. static void aurora_save(void __iomem *base)
  1140. {
  1141. l2x0_saved_regs.ctrl = readl_relaxed(base + L2X0_CTRL);
  1142. l2x0_saved_regs.aux_ctrl = readl_relaxed(base + L2X0_AUX_CTRL);
  1143. }
  1144. static void aurora_resume(void)
  1145. {
  1146. void __iomem *base = l2x0_base;
  1147. if (!(readl(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  1148. writel_relaxed(l2x0_saved_regs.aux_ctrl, base + L2X0_AUX_CTRL);
  1149. writel_relaxed(l2x0_saved_regs.ctrl, base + L2X0_CTRL);
  1150. }
  1151. }
  1152. /*
  1153. * For Aurora cache in no outer mode, enable via the CP15 coprocessor
  1154. * broadcasting of cache commands to L2.
  1155. */
  1156. static void __init aurora_enable_no_outer(void __iomem *base, u32 aux,
  1157. unsigned num_lock)
  1158. {
  1159. u32 u;
  1160. asm volatile("mrc p15, 1, %0, c15, c2, 0" : "=r" (u));
  1161. u |= AURORA_CTRL_FW; /* Set the FW bit */
  1162. asm volatile("mcr p15, 1, %0, c15, c2, 0" : : "r" (u));
  1163. isb();
  1164. l2c_enable(base, aux, num_lock);
  1165. }
  1166. static void __init aurora_fixup(void __iomem *base, u32 cache_id,
  1167. struct outer_cache_fns *fns)
  1168. {
  1169. sync_reg_offset = AURORA_SYNC_REG;
  1170. }
  1171. static void __init aurora_of_parse(const struct device_node *np,
  1172. u32 *aux_val, u32 *aux_mask)
  1173. {
  1174. u32 val = AURORA_ACR_REPLACEMENT_TYPE_SEMIPLRU;
  1175. u32 mask = AURORA_ACR_REPLACEMENT_MASK;
  1176. of_property_read_u32(np, "cache-id-part",
  1177. &cache_id_part_number_from_dt);
  1178. /* Determine and save the write policy */
  1179. l2_wt_override = of_property_read_bool(np, "wt-override");
  1180. if (l2_wt_override) {
  1181. val |= AURORA_ACR_FORCE_WRITE_THRO_POLICY;
  1182. mask |= AURORA_ACR_FORCE_WRITE_POLICY_MASK;
  1183. }
  1184. *aux_val &= ~mask;
  1185. *aux_val |= val;
  1186. *aux_mask &= ~mask;
  1187. }
  1188. static const struct l2c_init_data of_aurora_with_outer_data __initconst = {
  1189. .type = "Aurora",
  1190. .way_size_0 = SZ_4K,
  1191. .num_lock = 4,
  1192. .of_parse = aurora_of_parse,
  1193. .enable = l2c_enable,
  1194. .fixup = aurora_fixup,
  1195. .save = aurora_save,
  1196. .outer_cache = {
  1197. .inv_range = aurora_inv_range,
  1198. .clean_range = aurora_clean_range,
  1199. .flush_range = aurora_flush_range,
  1200. .flush_all = l2x0_flush_all,
  1201. .disable = l2x0_disable,
  1202. .sync = l2x0_cache_sync,
  1203. .resume = aurora_resume,
  1204. },
  1205. };
  1206. static const struct l2c_init_data of_aurora_no_outer_data __initconst = {
  1207. .type = "Aurora",
  1208. .way_size_0 = SZ_4K,
  1209. .num_lock = 4,
  1210. .of_parse = aurora_of_parse,
  1211. .enable = aurora_enable_no_outer,
  1212. .fixup = aurora_fixup,
  1213. .save = aurora_save,
  1214. .outer_cache = {
  1215. .resume = aurora_resume,
  1216. },
  1217. };
  1218. /*
  1219. * For certain Broadcom SoCs, depending on the address range, different offsets
  1220. * need to be added to the address before passing it to L2 for
  1221. * invalidation/clean/flush
  1222. *
  1223. * Section Address Range Offset EMI
  1224. * 1 0x00000000 - 0x3FFFFFFF 0x80000000 VC
  1225. * 2 0x40000000 - 0xBFFFFFFF 0x40000000 SYS
  1226. * 3 0xC0000000 - 0xFFFFFFFF 0x80000000 VC
  1227. *
  1228. * When the start and end addresses have crossed two different sections, we
  1229. * need to break the L2 operation into two, each within its own section.
  1230. * For example, if we need to invalidate addresses starts at 0xBFFF0000 and
  1231. * ends at 0xC0001000, we need do invalidate 1) 0xBFFF0000 - 0xBFFFFFFF and 2)
  1232. * 0xC0000000 - 0xC0001000
  1233. *
  1234. * Note 1:
  1235. * By breaking a single L2 operation into two, we may potentially suffer some
  1236. * performance hit, but keep in mind the cross section case is very rare
  1237. *
  1238. * Note 2:
  1239. * We do not need to handle the case when the start address is in
  1240. * Section 1 and the end address is in Section 3, since it is not a valid use
  1241. * case
  1242. *
  1243. * Note 3:
  1244. * Section 1 in practical terms can no longer be used on rev A2. Because of
  1245. * that the code does not need to handle section 1 at all.
  1246. *
  1247. */
  1248. #define BCM_SYS_EMI_START_ADDR 0x40000000UL
  1249. #define BCM_VC_EMI_SEC3_START_ADDR 0xC0000000UL
  1250. #define BCM_SYS_EMI_OFFSET 0x40000000UL
  1251. #define BCM_VC_EMI_OFFSET 0x80000000UL
  1252. static inline int bcm_addr_is_sys_emi(unsigned long addr)
  1253. {
  1254. return (addr >= BCM_SYS_EMI_START_ADDR) &&
  1255. (addr < BCM_VC_EMI_SEC3_START_ADDR);
  1256. }
  1257. static inline unsigned long bcm_l2_phys_addr(unsigned long addr)
  1258. {
  1259. if (bcm_addr_is_sys_emi(addr))
  1260. return addr + BCM_SYS_EMI_OFFSET;
  1261. else
  1262. return addr + BCM_VC_EMI_OFFSET;
  1263. }
  1264. static void bcm_inv_range(unsigned long start, unsigned long end)
  1265. {
  1266. unsigned long new_start, new_end;
  1267. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  1268. if (unlikely(end <= start))
  1269. return;
  1270. new_start = bcm_l2_phys_addr(start);
  1271. new_end = bcm_l2_phys_addr(end);
  1272. /* normal case, no cross section between start and end */
  1273. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  1274. l2c210_inv_range(new_start, new_end);
  1275. return;
  1276. }
  1277. /* They cross sections, so it can only be a cross from section
  1278. * 2 to section 3
  1279. */
  1280. l2c210_inv_range(new_start,
  1281. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  1282. l2c210_inv_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  1283. new_end);
  1284. }
  1285. static void bcm_clean_range(unsigned long start, unsigned long end)
  1286. {
  1287. unsigned long new_start, new_end;
  1288. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  1289. if (unlikely(end <= start))
  1290. return;
  1291. new_start = bcm_l2_phys_addr(start);
  1292. new_end = bcm_l2_phys_addr(end);
  1293. /* normal case, no cross section between start and end */
  1294. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  1295. l2c210_clean_range(new_start, new_end);
  1296. return;
  1297. }
  1298. /* They cross sections, so it can only be a cross from section
  1299. * 2 to section 3
  1300. */
  1301. l2c210_clean_range(new_start,
  1302. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  1303. l2c210_clean_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  1304. new_end);
  1305. }
  1306. static void bcm_flush_range(unsigned long start, unsigned long end)
  1307. {
  1308. unsigned long new_start, new_end;
  1309. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  1310. if (unlikely(end <= start))
  1311. return;
  1312. if ((end - start) >= l2x0_size) {
  1313. outer_cache.flush_all();
  1314. return;
  1315. }
  1316. new_start = bcm_l2_phys_addr(start);
  1317. new_end = bcm_l2_phys_addr(end);
  1318. /* normal case, no cross section between start and end */
  1319. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  1320. l2c210_flush_range(new_start, new_end);
  1321. return;
  1322. }
  1323. /* They cross sections, so it can only be a cross from section
  1324. * 2 to section 3
  1325. */
  1326. l2c210_flush_range(new_start,
  1327. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  1328. l2c210_flush_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  1329. new_end);
  1330. }
  1331. /* Broadcom L2C-310 start from ARMs R3P2 or later, and require no fixups */
  1332. static const struct l2c_init_data of_bcm_l2x0_data __initconst = {
  1333. .type = "BCM-L2C-310",
  1334. .way_size_0 = SZ_8K,
  1335. .num_lock = 8,
  1336. .of_parse = l2c310_of_parse,
  1337. .enable = l2c310_enable,
  1338. .save = l2c310_save,
  1339. .outer_cache = {
  1340. .inv_range = bcm_inv_range,
  1341. .clean_range = bcm_clean_range,
  1342. .flush_range = bcm_flush_range,
  1343. .flush_all = l2c210_flush_all,
  1344. .disable = l2c310_disable,
  1345. .sync = l2c210_sync,
  1346. .resume = l2c310_resume,
  1347. },
  1348. };
  1349. static void __init tauros3_save(void __iomem *base)
  1350. {
  1351. l2c_save(base);
  1352. l2x0_saved_regs.aux2_ctrl =
  1353. readl_relaxed(base + TAUROS3_AUX2_CTRL);
  1354. l2x0_saved_regs.prefetch_ctrl =
  1355. readl_relaxed(base + L310_PREFETCH_CTRL);
  1356. }
  1357. static void tauros3_resume(void)
  1358. {
  1359. void __iomem *base = l2x0_base;
  1360. if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  1361. writel_relaxed(l2x0_saved_regs.aux2_ctrl,
  1362. base + TAUROS3_AUX2_CTRL);
  1363. writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
  1364. base + L310_PREFETCH_CTRL);
  1365. l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
  1366. }
  1367. }
  1368. static const struct l2c_init_data of_tauros3_data __initconst = {
  1369. .type = "Tauros3",
  1370. .way_size_0 = SZ_8K,
  1371. .num_lock = 8,
  1372. .enable = l2c_enable,
  1373. .save = tauros3_save,
  1374. /* Tauros3 broadcasts L1 cache operations to L2 */
  1375. .outer_cache = {
  1376. .resume = tauros3_resume,
  1377. },
  1378. };
  1379. #define L2C_ID(name, fns) { .compatible = name, .data = (void *)&fns }
  1380. static const struct of_device_id l2x0_ids[] __initconst = {
  1381. L2C_ID("arm,l210-cache", of_l2c210_data),
  1382. L2C_ID("arm,l220-cache", of_l2c220_data),
  1383. L2C_ID("arm,pl310-cache", of_l2c310_data),
  1384. L2C_ID("brcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
  1385. L2C_ID("marvell,aurora-outer-cache", of_aurora_with_outer_data),
  1386. L2C_ID("marvell,aurora-system-cache", of_aurora_no_outer_data),
  1387. L2C_ID("marvell,tauros3-cache", of_tauros3_data),
  1388. /* Deprecated IDs */
  1389. L2C_ID("bcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
  1390. {}
  1391. };
  1392. int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
  1393. {
  1394. const struct l2c_init_data *data;
  1395. struct device_node *np;
  1396. struct resource res;
  1397. u32 cache_id, old_aux;
  1398. np = of_find_matching_node(NULL, l2x0_ids);
  1399. if (!np)
  1400. return -ENODEV;
  1401. if (of_address_to_resource(np, 0, &res))
  1402. return -ENODEV;
  1403. l2x0_base = ioremap(res.start, resource_size(&res));
  1404. if (!l2x0_base)
  1405. return -ENOMEM;
  1406. l2x0_saved_regs.phy_base = res.start;
  1407. data = of_match_node(l2x0_ids, np)->data;
  1408. if (of_device_is_compatible(np, "arm,pl310-cache") &&
  1409. of_property_read_bool(np, "arm,io-coherent"))
  1410. data = &of_l2c310_coherent_data;
  1411. old_aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  1412. if (old_aux != ((old_aux & aux_mask) | aux_val)) {
  1413. pr_warn("L2C: platform modifies aux control register: 0x%08x -> 0x%08x\n",
  1414. old_aux, (old_aux & aux_mask) | aux_val);
  1415. } else if (aux_mask != ~0U && aux_val != 0) {
  1416. pr_alert("L2C: platform provided aux values match the hardware, so have no effect. Please remove them.\n");
  1417. }
  1418. /* All L2 caches are unified, so this property should be specified */
  1419. if (!of_property_read_bool(np, "cache-unified"))
  1420. pr_err("L2C: device tree omits to specify unified cache\n");
  1421. /* L2 configuration can only be changed if the cache is disabled */
  1422. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
  1423. if (data->of_parse)
  1424. data->of_parse(np, &aux_val, &aux_mask);
  1425. if (cache_id_part_number_from_dt)
  1426. cache_id = cache_id_part_number_from_dt;
  1427. else
  1428. cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
  1429. __l2c_init(data, aux_val, aux_mask, cache_id);
  1430. return 0;
  1431. }
  1432. #endif