cache-l2x0.c 41 KB

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