cvmx-l2c.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (c) 2003-2010 Cavium Networks
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this file; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. * or visit http://www.gnu.org/licenses/.
  23. *
  24. * This file may also be available under a different license from Cavium.
  25. * Contact Cavium Networks for more information
  26. ***********************license end**************************************/
  27. /*
  28. * Implementation of the Level 2 Cache (L2C) control,
  29. * measurement, and debugging facilities.
  30. */
  31. #include <asm/octeon/cvmx.h>
  32. #include <asm/octeon/cvmx-l2c.h>
  33. #include <asm/octeon/cvmx-spinlock.h>
  34. /*
  35. * This spinlock is used internally to ensure that only one core is
  36. * performing certain L2 operations at a time.
  37. *
  38. * NOTE: This only protects calls from within a single application -
  39. * if multiple applications or operating systems are running, then it
  40. * is up to the user program to coordinate between them.
  41. */
  42. cvmx_spinlock_t cvmx_l2c_spinlock;
  43. int cvmx_l2c_get_core_way_partition(uint32_t core)
  44. {
  45. uint32_t field;
  46. /* Validate the core number */
  47. if (core >= cvmx_octeon_num_cores())
  48. return -1;
  49. if (OCTEON_IS_MODEL(OCTEON_CN63XX))
  50. return cvmx_read_csr(CVMX_L2C_WPAR_PPX(core)) & 0xffff;
  51. /*
  52. * Use the lower two bits of the coreNumber to determine the
  53. * bit offset of the UMSK[] field in the L2C_SPAR register.
  54. */
  55. field = (core & 0x3) * 8;
  56. /*
  57. * Return the UMSK[] field from the appropriate L2C_SPAR
  58. * register based on the coreNumber.
  59. */
  60. switch (core & 0xC) {
  61. case 0x0:
  62. return (cvmx_read_csr(CVMX_L2C_SPAR0) & (0xFF << field)) >> field;
  63. case 0x4:
  64. return (cvmx_read_csr(CVMX_L2C_SPAR1) & (0xFF << field)) >> field;
  65. case 0x8:
  66. return (cvmx_read_csr(CVMX_L2C_SPAR2) & (0xFF << field)) >> field;
  67. case 0xC:
  68. return (cvmx_read_csr(CVMX_L2C_SPAR3) & (0xFF << field)) >> field;
  69. }
  70. return 0;
  71. }
  72. int cvmx_l2c_set_core_way_partition(uint32_t core, uint32_t mask)
  73. {
  74. uint32_t field;
  75. uint32_t valid_mask;
  76. valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;
  77. mask &= valid_mask;
  78. /* A UMSK setting which blocks all L2C Ways is an error on some chips */
  79. if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))
  80. return -1;
  81. /* Validate the core number */
  82. if (core >= cvmx_octeon_num_cores())
  83. return -1;
  84. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  85. cvmx_write_csr(CVMX_L2C_WPAR_PPX(core), mask);
  86. return 0;
  87. }
  88. /*
  89. * Use the lower two bits of core to determine the bit offset of the
  90. * UMSK[] field in the L2C_SPAR register.
  91. */
  92. field = (core & 0x3) * 8;
  93. /*
  94. * Assign the new mask setting to the UMSK[] field in the appropriate
  95. * L2C_SPAR register based on the core_num.
  96. *
  97. */
  98. switch (core & 0xC) {
  99. case 0x0:
  100. cvmx_write_csr(CVMX_L2C_SPAR0,
  101. (cvmx_read_csr(CVMX_L2C_SPAR0) & ~(0xFF << field)) |
  102. mask << field);
  103. break;
  104. case 0x4:
  105. cvmx_write_csr(CVMX_L2C_SPAR1,
  106. (cvmx_read_csr(CVMX_L2C_SPAR1) & ~(0xFF << field)) |
  107. mask << field);
  108. break;
  109. case 0x8:
  110. cvmx_write_csr(CVMX_L2C_SPAR2,
  111. (cvmx_read_csr(CVMX_L2C_SPAR2) & ~(0xFF << field)) |
  112. mask << field);
  113. break;
  114. case 0xC:
  115. cvmx_write_csr(CVMX_L2C_SPAR3,
  116. (cvmx_read_csr(CVMX_L2C_SPAR3) & ~(0xFF << field)) |
  117. mask << field);
  118. break;
  119. }
  120. return 0;
  121. }
  122. int cvmx_l2c_set_hw_way_partition(uint32_t mask)
  123. {
  124. uint32_t valid_mask;
  125. valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;
  126. mask &= valid_mask;
  127. /* A UMSK setting which blocks all L2C Ways is an error on some chips */
  128. if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))
  129. return -1;
  130. if (OCTEON_IS_MODEL(OCTEON_CN63XX))
  131. cvmx_write_csr(CVMX_L2C_WPAR_IOBX(0), mask);
  132. else
  133. cvmx_write_csr(CVMX_L2C_SPAR4,
  134. (cvmx_read_csr(CVMX_L2C_SPAR4) & ~0xFF) | mask);
  135. return 0;
  136. }
  137. int cvmx_l2c_get_hw_way_partition(void)
  138. {
  139. if (OCTEON_IS_MODEL(OCTEON_CN63XX))
  140. return cvmx_read_csr(CVMX_L2C_WPAR_IOBX(0)) & 0xffff;
  141. else
  142. return cvmx_read_csr(CVMX_L2C_SPAR4) & (0xFF);
  143. }
  144. void cvmx_l2c_config_perf(uint32_t counter, enum cvmx_l2c_event event,
  145. uint32_t clear_on_read)
  146. {
  147. if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
  148. union cvmx_l2c_pfctl pfctl;
  149. pfctl.u64 = cvmx_read_csr(CVMX_L2C_PFCTL);
  150. switch (counter) {
  151. case 0:
  152. pfctl.s.cnt0sel = event;
  153. pfctl.s.cnt0ena = 1;
  154. pfctl.s.cnt0rdclr = clear_on_read;
  155. break;
  156. case 1:
  157. pfctl.s.cnt1sel = event;
  158. pfctl.s.cnt1ena = 1;
  159. pfctl.s.cnt1rdclr = clear_on_read;
  160. break;
  161. case 2:
  162. pfctl.s.cnt2sel = event;
  163. pfctl.s.cnt2ena = 1;
  164. pfctl.s.cnt2rdclr = clear_on_read;
  165. break;
  166. case 3:
  167. default:
  168. pfctl.s.cnt3sel = event;
  169. pfctl.s.cnt3ena = 1;
  170. pfctl.s.cnt3rdclr = clear_on_read;
  171. break;
  172. }
  173. cvmx_write_csr(CVMX_L2C_PFCTL, pfctl.u64);
  174. } else {
  175. union cvmx_l2c_tadx_prf l2c_tadx_prf;
  176. int tad;
  177. cvmx_dprintf("L2C performance counter events are different for this chip, mapping 'event' to cvmx_l2c_tad_event_t\n");
  178. if (clear_on_read)
  179. cvmx_dprintf("L2C counters don't support clear on read for this chip\n");
  180. l2c_tadx_prf.u64 = cvmx_read_csr(CVMX_L2C_TADX_PRF(0));
  181. switch (counter) {
  182. case 0:
  183. l2c_tadx_prf.s.cnt0sel = event;
  184. break;
  185. case 1:
  186. l2c_tadx_prf.s.cnt1sel = event;
  187. break;
  188. case 2:
  189. l2c_tadx_prf.s.cnt2sel = event;
  190. break;
  191. default:
  192. case 3:
  193. l2c_tadx_prf.s.cnt3sel = event;
  194. break;
  195. }
  196. for (tad = 0; tad < CVMX_L2C_TADS; tad++)
  197. cvmx_write_csr(CVMX_L2C_TADX_PRF(tad),
  198. l2c_tadx_prf.u64);
  199. }
  200. }
  201. uint64_t cvmx_l2c_read_perf(uint32_t counter)
  202. {
  203. switch (counter) {
  204. case 0:
  205. if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
  206. return cvmx_read_csr(CVMX_L2C_PFC0);
  207. else {
  208. uint64_t counter = 0;
  209. int tad;
  210. for (tad = 0; tad < CVMX_L2C_TADS; tad++)
  211. counter += cvmx_read_csr(CVMX_L2C_TADX_PFC0(tad));
  212. return counter;
  213. }
  214. case 1:
  215. if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
  216. return cvmx_read_csr(CVMX_L2C_PFC1);
  217. else {
  218. uint64_t counter = 0;
  219. int tad;
  220. for (tad = 0; tad < CVMX_L2C_TADS; tad++)
  221. counter += cvmx_read_csr(CVMX_L2C_TADX_PFC1(tad));
  222. return counter;
  223. }
  224. case 2:
  225. if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
  226. return cvmx_read_csr(CVMX_L2C_PFC2);
  227. else {
  228. uint64_t counter = 0;
  229. int tad;
  230. for (tad = 0; tad < CVMX_L2C_TADS; tad++)
  231. counter += cvmx_read_csr(CVMX_L2C_TADX_PFC2(tad));
  232. return counter;
  233. }
  234. case 3:
  235. default:
  236. if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
  237. return cvmx_read_csr(CVMX_L2C_PFC3);
  238. else {
  239. uint64_t counter = 0;
  240. int tad;
  241. for (tad = 0; tad < CVMX_L2C_TADS; tad++)
  242. counter += cvmx_read_csr(CVMX_L2C_TADX_PFC3(tad));
  243. return counter;
  244. }
  245. }
  246. }
  247. /**
  248. * @INTERNAL
  249. * Helper function use to fault in cache lines for L2 cache locking
  250. *
  251. * @addr: Address of base of memory region to read into L2 cache
  252. * @len: Length (in bytes) of region to fault in
  253. */
  254. static void fault_in(uint64_t addr, int len)
  255. {
  256. volatile char *ptr;
  257. volatile char dummy;
  258. /*
  259. * Adjust addr and length so we get all cache lines even for
  260. * small ranges spanning two cache lines.
  261. */
  262. len += addr & CVMX_CACHE_LINE_MASK;
  263. addr &= ~CVMX_CACHE_LINE_MASK;
  264. ptr = (volatile char *)cvmx_phys_to_ptr(addr);
  265. /*
  266. * Invalidate L1 cache to make sure all loads result in data
  267. * being in L2.
  268. */
  269. CVMX_DCACHE_INVALIDATE;
  270. while (len > 0) {
  271. dummy += *ptr;
  272. len -= CVMX_CACHE_LINE_SIZE;
  273. ptr += CVMX_CACHE_LINE_SIZE;
  274. }
  275. }
  276. int cvmx_l2c_lock_line(uint64_t addr)
  277. {
  278. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  279. int shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;
  280. uint64_t assoc = cvmx_l2c_get_num_assoc();
  281. uint64_t tag = addr >> shift;
  282. uint64_t index = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, cvmx_l2c_address_to_index(addr) << CVMX_L2C_IDX_ADDR_SHIFT);
  283. uint64_t way;
  284. union cvmx_l2c_tadx_tag l2c_tadx_tag;
  285. CVMX_CACHE_LCKL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, addr), 0);
  286. /* Make sure we were able to lock the line */
  287. for (way = 0; way < assoc; way++) {
  288. CVMX_CACHE_LTGL2I(index | (way << shift), 0);
  289. /* make sure CVMX_L2C_TADX_TAG is updated */
  290. CVMX_SYNC;
  291. l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));
  292. if (l2c_tadx_tag.s.valid && l2c_tadx_tag.s.tag == tag)
  293. break;
  294. }
  295. /* Check if a valid line is found */
  296. if (way >= assoc) {
  297. /* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: line not found for locking at 0x%llx address\n", (unsigned long long)addr); */
  298. return -1;
  299. }
  300. /* Check if lock bit is not set */
  301. if (!l2c_tadx_tag.s.lock) {
  302. /* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: Not able to lock at 0x%llx address\n", (unsigned long long)addr); */
  303. return -1;
  304. }
  305. return way;
  306. } else {
  307. int retval = 0;
  308. union cvmx_l2c_dbg l2cdbg;
  309. union cvmx_l2c_lckbase lckbase;
  310. union cvmx_l2c_lckoff lckoff;
  311. union cvmx_l2t_err l2t_err;
  312. cvmx_spinlock_lock(&cvmx_l2c_spinlock);
  313. l2cdbg.u64 = 0;
  314. lckbase.u64 = 0;
  315. lckoff.u64 = 0;
  316. /* Clear l2t error bits if set */
  317. l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);
  318. l2t_err.s.lckerr = 1;
  319. l2t_err.s.lckerr2 = 1;
  320. cvmx_write_csr(CVMX_L2T_ERR, l2t_err.u64);
  321. addr &= ~CVMX_CACHE_LINE_MASK;
  322. /* Set this core as debug core */
  323. l2cdbg.s.ppnum = cvmx_get_core_num();
  324. CVMX_SYNC;
  325. cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);
  326. cvmx_read_csr(CVMX_L2C_DBG);
  327. lckoff.s.lck_offset = 0; /* Only lock 1 line at a time */
  328. cvmx_write_csr(CVMX_L2C_LCKOFF, lckoff.u64);
  329. cvmx_read_csr(CVMX_L2C_LCKOFF);
  330. if (((union cvmx_l2c_cfg)(cvmx_read_csr(CVMX_L2C_CFG))).s.idxalias) {
  331. int alias_shift = CVMX_L2C_IDX_ADDR_SHIFT + 2 * CVMX_L2_SET_BITS - 1;
  332. uint64_t addr_tmp = addr ^ (addr & ((1 << alias_shift) - 1)) >> CVMX_L2_SET_BITS;
  333. lckbase.s.lck_base = addr_tmp >> 7;
  334. } else {
  335. lckbase.s.lck_base = addr >> 7;
  336. }
  337. lckbase.s.lck_ena = 1;
  338. cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);
  339. /* Make sure it gets there */
  340. cvmx_read_csr(CVMX_L2C_LCKBASE);
  341. fault_in(addr, CVMX_CACHE_LINE_SIZE);
  342. lckbase.s.lck_ena = 0;
  343. cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);
  344. /* Make sure it gets there */
  345. cvmx_read_csr(CVMX_L2C_LCKBASE);
  346. /* Stop being debug core */
  347. cvmx_write_csr(CVMX_L2C_DBG, 0);
  348. cvmx_read_csr(CVMX_L2C_DBG);
  349. l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);
  350. if (l2t_err.s.lckerr || l2t_err.s.lckerr2)
  351. retval = 1; /* We were unable to lock the line */
  352. cvmx_spinlock_unlock(&cvmx_l2c_spinlock);
  353. return retval;
  354. }
  355. }
  356. int cvmx_l2c_lock_mem_region(uint64_t start, uint64_t len)
  357. {
  358. int retval = 0;
  359. /* Round start/end to cache line boundaries */
  360. len += start & CVMX_CACHE_LINE_MASK;
  361. start &= ~CVMX_CACHE_LINE_MASK;
  362. len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;
  363. while (len) {
  364. retval += cvmx_l2c_lock_line(start);
  365. start += CVMX_CACHE_LINE_SIZE;
  366. len -= CVMX_CACHE_LINE_SIZE;
  367. }
  368. return retval;
  369. }
  370. void cvmx_l2c_flush(void)
  371. {
  372. uint64_t assoc, set;
  373. uint64_t n_assoc, n_set;
  374. n_set = cvmx_l2c_get_num_sets();
  375. n_assoc = cvmx_l2c_get_num_assoc();
  376. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  377. uint64_t address;
  378. /* These may look like constants, but they aren't... */
  379. int assoc_shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;
  380. int set_shift = CVMX_L2C_IDX_ADDR_SHIFT;
  381. for (set = 0; set < n_set; set++) {
  382. for (assoc = 0; assoc < n_assoc; assoc++) {
  383. address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
  384. (assoc << assoc_shift) | (set << set_shift));
  385. CVMX_CACHE_WBIL2I(address, 0);
  386. }
  387. }
  388. } else {
  389. for (set = 0; set < n_set; set++)
  390. for (assoc = 0; assoc < n_assoc; assoc++)
  391. cvmx_l2c_flush_line(assoc, set);
  392. }
  393. }
  394. int cvmx_l2c_unlock_line(uint64_t address)
  395. {
  396. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  397. int assoc;
  398. union cvmx_l2c_tag tag;
  399. uint32_t tag_addr;
  400. uint32_t index = cvmx_l2c_address_to_index(address);
  401. tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));
  402. /*
  403. * For 63XX, we can flush a line by using the physical
  404. * address directly, so finding the cache line used by
  405. * the address is only required to provide the proper
  406. * return value for the function.
  407. */
  408. for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {
  409. tag = cvmx_l2c_get_tag(assoc, index);
  410. if (tag.s.V && (tag.s.addr == tag_addr)) {
  411. CVMX_CACHE_WBIL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, address), 0);
  412. return tag.s.L;
  413. }
  414. }
  415. } else {
  416. int assoc;
  417. union cvmx_l2c_tag tag;
  418. uint32_t tag_addr;
  419. uint32_t index = cvmx_l2c_address_to_index(address);
  420. /* Compute portion of address that is stored in tag */
  421. tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));
  422. for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {
  423. tag = cvmx_l2c_get_tag(assoc, index);
  424. if (tag.s.V && (tag.s.addr == tag_addr)) {
  425. cvmx_l2c_flush_line(assoc, index);
  426. return tag.s.L;
  427. }
  428. }
  429. }
  430. return 0;
  431. }
  432. int cvmx_l2c_unlock_mem_region(uint64_t start, uint64_t len)
  433. {
  434. int num_unlocked = 0;
  435. /* Round start/end to cache line boundaries */
  436. len += start & CVMX_CACHE_LINE_MASK;
  437. start &= ~CVMX_CACHE_LINE_MASK;
  438. len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;
  439. while (len > 0) {
  440. num_unlocked += cvmx_l2c_unlock_line(start);
  441. start += CVMX_CACHE_LINE_SIZE;
  442. len -= CVMX_CACHE_LINE_SIZE;
  443. }
  444. return num_unlocked;
  445. }
  446. /*
  447. * Internal l2c tag types. These are converted to a generic structure
  448. * that can be used on all chips.
  449. */
  450. union __cvmx_l2c_tag {
  451. uint64_t u64;
  452. struct cvmx_l2c_tag_cn50xx {
  453. uint64_t reserved:40;
  454. uint64_t V:1; /* Line valid */
  455. uint64_t D:1; /* Line dirty */
  456. uint64_t L:1; /* Line locked */
  457. uint64_t U:1; /* Use, LRU eviction */
  458. uint64_t addr:20; /* Phys mem addr (33..14) */
  459. } cn50xx;
  460. struct cvmx_l2c_tag_cn30xx {
  461. uint64_t reserved:41;
  462. uint64_t V:1; /* Line valid */
  463. uint64_t D:1; /* Line dirty */
  464. uint64_t L:1; /* Line locked */
  465. uint64_t U:1; /* Use, LRU eviction */
  466. uint64_t addr:19; /* Phys mem addr (33..15) */
  467. } cn30xx;
  468. struct cvmx_l2c_tag_cn31xx {
  469. uint64_t reserved:42;
  470. uint64_t V:1; /* Line valid */
  471. uint64_t D:1; /* Line dirty */
  472. uint64_t L:1; /* Line locked */
  473. uint64_t U:1; /* Use, LRU eviction */
  474. uint64_t addr:18; /* Phys mem addr (33..16) */
  475. } cn31xx;
  476. struct cvmx_l2c_tag_cn38xx {
  477. uint64_t reserved:43;
  478. uint64_t V:1; /* Line valid */
  479. uint64_t D:1; /* Line dirty */
  480. uint64_t L:1; /* Line locked */
  481. uint64_t U:1; /* Use, LRU eviction */
  482. uint64_t addr:17; /* Phys mem addr (33..17) */
  483. } cn38xx;
  484. struct cvmx_l2c_tag_cn58xx {
  485. uint64_t reserved:44;
  486. uint64_t V:1; /* Line valid */
  487. uint64_t D:1; /* Line dirty */
  488. uint64_t L:1; /* Line locked */
  489. uint64_t U:1; /* Use, LRU eviction */
  490. uint64_t addr:16; /* Phys mem addr (33..18) */
  491. } cn58xx;
  492. struct cvmx_l2c_tag_cn58xx cn56xx; /* 2048 sets */
  493. struct cvmx_l2c_tag_cn31xx cn52xx; /* 512 sets */
  494. };
  495. /**
  496. * @INTERNAL
  497. * Function to read a L2C tag. This code make the current core
  498. * the 'debug core' for the L2. This code must only be executed by
  499. * 1 core at a time.
  500. *
  501. * @assoc: Association (way) of the tag to dump
  502. * @index: Index of the cacheline
  503. *
  504. * Returns The Octeon model specific tag structure. This is
  505. * translated by a wrapper function to a generic form that is
  506. * easier for applications to use.
  507. */
  508. static union __cvmx_l2c_tag __read_l2_tag(uint64_t assoc, uint64_t index)
  509. {
  510. uint64_t debug_tag_addr = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, (index << 7) + 96);
  511. uint64_t core = cvmx_get_core_num();
  512. union __cvmx_l2c_tag tag_val;
  513. uint64_t dbg_addr = CVMX_L2C_DBG;
  514. unsigned long flags;
  515. union cvmx_l2c_dbg debug_val;
  516. debug_val.u64 = 0;
  517. /*
  518. * For low core count parts, the core number is always small
  519. * enough to stay in the correct field and not set any
  520. * reserved bits.
  521. */
  522. debug_val.s.ppnum = core;
  523. debug_val.s.l2t = 1;
  524. debug_val.s.set = assoc;
  525. local_irq_save(flags);
  526. /*
  527. * Make sure core is quiet (no prefetches, etc.) before
  528. * entering debug mode.
  529. */
  530. CVMX_SYNC;
  531. /* Flush L1 to make sure debug load misses L1 */
  532. CVMX_DCACHE_INVALIDATE;
  533. /*
  534. * The following must be done in assembly as when in debug
  535. * mode all data loads from L2 return special debug data, not
  536. * normal memory contents. Also, interrupts must be disabled,
  537. * since if an interrupt occurs while in debug mode the ISR
  538. * will get debug data from all its memory * reads instead of
  539. * the contents of memory.
  540. */
  541. asm volatile (
  542. ".set push\n\t"
  543. ".set mips64\n\t"
  544. ".set noreorder\n\t"
  545. "sd %[dbg_val], 0(%[dbg_addr])\n\t" /* Enter debug mode, wait for store */
  546. "ld $0, 0(%[dbg_addr])\n\t"
  547. "ld %[tag_val], 0(%[tag_addr])\n\t" /* Read L2C tag data */
  548. "sd $0, 0(%[dbg_addr])\n\t" /* Exit debug mode, wait for store */
  549. "ld $0, 0(%[dbg_addr])\n\t"
  550. "cache 9, 0($0)\n\t" /* Invalidate dcache to discard debug data */
  551. ".set pop"
  552. : [tag_val] "=r" (tag_val)
  553. : [dbg_addr] "r" (dbg_addr), [dbg_val] "r" (debug_val), [tag_addr] "r" (debug_tag_addr)
  554. : "memory");
  555. local_irq_restore(flags);
  556. return tag_val;
  557. }
  558. union cvmx_l2c_tag cvmx_l2c_get_tag(uint32_t association, uint32_t index)
  559. {
  560. union cvmx_l2c_tag tag;
  561. tag.u64 = 0;
  562. if ((int)association >= cvmx_l2c_get_num_assoc()) {
  563. cvmx_dprintf("ERROR: cvmx_l2c_get_tag association out of range\n");
  564. return tag;
  565. }
  566. if ((int)index >= cvmx_l2c_get_num_sets()) {
  567. cvmx_dprintf("ERROR: cvmx_l2c_get_tag index out of range (arg: %d, max: %d)\n",
  568. (int)index, cvmx_l2c_get_num_sets());
  569. return tag;
  570. }
  571. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  572. union cvmx_l2c_tadx_tag l2c_tadx_tag;
  573. uint64_t address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
  574. (association << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |
  575. (index << CVMX_L2C_IDX_ADDR_SHIFT));
  576. /*
  577. * Use L2 cache Index load tag cache instruction, as
  578. * hardware loads the virtual tag for the L2 cache
  579. * block with the contents of L2C_TAD0_TAG
  580. * register.
  581. */
  582. CVMX_CACHE_LTGL2I(address, 0);
  583. CVMX_SYNC; /* make sure CVMX_L2C_TADX_TAG is updated */
  584. l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));
  585. tag.s.V = l2c_tadx_tag.s.valid;
  586. tag.s.D = l2c_tadx_tag.s.dirty;
  587. tag.s.L = l2c_tadx_tag.s.lock;
  588. tag.s.U = l2c_tadx_tag.s.use;
  589. tag.s.addr = l2c_tadx_tag.s.tag;
  590. } else {
  591. union __cvmx_l2c_tag tmp_tag;
  592. /* __read_l2_tag is intended for internal use only */
  593. tmp_tag = __read_l2_tag(association, index);
  594. /*
  595. * Convert all tag structure types to generic version,
  596. * as it can represent all models.
  597. */
  598. if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
  599. tag.s.V = tmp_tag.cn58xx.V;
  600. tag.s.D = tmp_tag.cn58xx.D;
  601. tag.s.L = tmp_tag.cn58xx.L;
  602. tag.s.U = tmp_tag.cn58xx.U;
  603. tag.s.addr = tmp_tag.cn58xx.addr;
  604. } else if (OCTEON_IS_MODEL(OCTEON_CN38XX)) {
  605. tag.s.V = tmp_tag.cn38xx.V;
  606. tag.s.D = tmp_tag.cn38xx.D;
  607. tag.s.L = tmp_tag.cn38xx.L;
  608. tag.s.U = tmp_tag.cn38xx.U;
  609. tag.s.addr = tmp_tag.cn38xx.addr;
  610. } else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  611. tag.s.V = tmp_tag.cn31xx.V;
  612. tag.s.D = tmp_tag.cn31xx.D;
  613. tag.s.L = tmp_tag.cn31xx.L;
  614. tag.s.U = tmp_tag.cn31xx.U;
  615. tag.s.addr = tmp_tag.cn31xx.addr;
  616. } else if (OCTEON_IS_MODEL(OCTEON_CN30XX)) {
  617. tag.s.V = tmp_tag.cn30xx.V;
  618. tag.s.D = tmp_tag.cn30xx.D;
  619. tag.s.L = tmp_tag.cn30xx.L;
  620. tag.s.U = tmp_tag.cn30xx.U;
  621. tag.s.addr = tmp_tag.cn30xx.addr;
  622. } else if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  623. tag.s.V = tmp_tag.cn50xx.V;
  624. tag.s.D = tmp_tag.cn50xx.D;
  625. tag.s.L = tmp_tag.cn50xx.L;
  626. tag.s.U = tmp_tag.cn50xx.U;
  627. tag.s.addr = tmp_tag.cn50xx.addr;
  628. } else {
  629. cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);
  630. }
  631. }
  632. return tag;
  633. }
  634. uint32_t cvmx_l2c_address_to_index(uint64_t addr)
  635. {
  636. uint64_t idx = addr >> CVMX_L2C_IDX_ADDR_SHIFT;
  637. int indxalias = 0;
  638. if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
  639. union cvmx_l2c_ctl l2c_ctl;
  640. l2c_ctl.u64 = cvmx_read_csr(CVMX_L2C_CTL);
  641. indxalias = !l2c_ctl.s.disidxalias;
  642. } else {
  643. union cvmx_l2c_cfg l2c_cfg;
  644. l2c_cfg.u64 = cvmx_read_csr(CVMX_L2C_CFG);
  645. indxalias = l2c_cfg.s.idxalias;
  646. }
  647. if (indxalias) {
  648. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  649. uint32_t a_14_12 = (idx / (CVMX_L2C_MEMBANK_SELECT_SIZE/(1<<CVMX_L2C_IDX_ADDR_SHIFT))) & 0x7;
  650. idx ^= idx / cvmx_l2c_get_num_sets();
  651. idx ^= a_14_12;
  652. } else {
  653. idx ^= ((addr & CVMX_L2C_ALIAS_MASK) >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT);
  654. }
  655. }
  656. idx &= CVMX_L2C_IDX_MASK;
  657. return idx;
  658. }
  659. int cvmx_l2c_get_cache_size_bytes(void)
  660. {
  661. return cvmx_l2c_get_num_sets() * cvmx_l2c_get_num_assoc() *
  662. CVMX_CACHE_LINE_SIZE;
  663. }
  664. /**
  665. * Return log base 2 of the number of sets in the L2 cache
  666. * Returns
  667. */
  668. int cvmx_l2c_get_set_bits(void)
  669. {
  670. int l2_set_bits;
  671. if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN58XX))
  672. l2_set_bits = 11; /* 2048 sets */
  673. else if (OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))
  674. l2_set_bits = 10; /* 1024 sets */
  675. else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX))
  676. l2_set_bits = 9; /* 512 sets */
  677. else if (OCTEON_IS_MODEL(OCTEON_CN30XX))
  678. l2_set_bits = 8; /* 256 sets */
  679. else if (OCTEON_IS_MODEL(OCTEON_CN50XX))
  680. l2_set_bits = 7; /* 128 sets */
  681. else {
  682. cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);
  683. l2_set_bits = 11; /* 2048 sets */
  684. }
  685. return l2_set_bits;
  686. }
  687. /* Return the number of sets in the L2 Cache */
  688. int cvmx_l2c_get_num_sets(void)
  689. {
  690. return 1 << cvmx_l2c_get_set_bits();
  691. }
  692. /* Return the number of associations in the L2 Cache */
  693. int cvmx_l2c_get_num_assoc(void)
  694. {
  695. int l2_assoc;
  696. if (OCTEON_IS_MODEL(OCTEON_CN56XX) ||
  697. OCTEON_IS_MODEL(OCTEON_CN52XX) ||
  698. OCTEON_IS_MODEL(OCTEON_CN58XX) ||
  699. OCTEON_IS_MODEL(OCTEON_CN50XX) ||
  700. OCTEON_IS_MODEL(OCTEON_CN38XX))
  701. l2_assoc = 8;
  702. else if (OCTEON_IS_MODEL(OCTEON_CN63XX))
  703. l2_assoc = 16;
  704. else if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||
  705. OCTEON_IS_MODEL(OCTEON_CN30XX))
  706. l2_assoc = 4;
  707. else {
  708. cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);
  709. l2_assoc = 8;
  710. }
  711. /* Check to see if part of the cache is disabled */
  712. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  713. union cvmx_mio_fus_dat3 mio_fus_dat3;
  714. mio_fus_dat3.u64 = cvmx_read_csr(CVMX_MIO_FUS_DAT3);
  715. /*
  716. * cvmx_mio_fus_dat3.s.l2c_crip fuses map as follows
  717. * <2> will be not used for 63xx
  718. * <1> disables 1/2 ways
  719. * <0> disables 1/4 ways
  720. * They are cumulative, so for 63xx:
  721. * <1> <0>
  722. * 0 0 16-way 2MB cache
  723. * 0 1 12-way 1.5MB cache
  724. * 1 0 8-way 1MB cache
  725. * 1 1 4-way 512KB cache
  726. */
  727. if (mio_fus_dat3.s.l2c_crip == 3)
  728. l2_assoc = 4;
  729. else if (mio_fus_dat3.s.l2c_crip == 2)
  730. l2_assoc = 8;
  731. else if (mio_fus_dat3.s.l2c_crip == 1)
  732. l2_assoc = 12;
  733. } else {
  734. union cvmx_l2d_fus3 val;
  735. val.u64 = cvmx_read_csr(CVMX_L2D_FUS3);
  736. /*
  737. * Using shifts here, as bit position names are
  738. * different for each model but they all mean the
  739. * same.
  740. */
  741. if ((val.u64 >> 35) & 0x1)
  742. l2_assoc = l2_assoc >> 2;
  743. else if ((val.u64 >> 34) & 0x1)
  744. l2_assoc = l2_assoc >> 1;
  745. }
  746. return l2_assoc;
  747. }
  748. /**
  749. * Flush a line from the L2 cache
  750. * This should only be called from one core at a time, as this routine
  751. * sets the core to the 'debug' core in order to flush the line.
  752. *
  753. * @assoc: Association (or way) to flush
  754. * @index: Index to flush
  755. */
  756. void cvmx_l2c_flush_line(uint32_t assoc, uint32_t index)
  757. {
  758. /* Check the range of the index. */
  759. if (index > (uint32_t)cvmx_l2c_get_num_sets()) {
  760. cvmx_dprintf("ERROR: cvmx_l2c_flush_line index out of range.\n");
  761. return;
  762. }
  763. /* Check the range of association. */
  764. if (assoc > (uint32_t)cvmx_l2c_get_num_assoc()) {
  765. cvmx_dprintf("ERROR: cvmx_l2c_flush_line association out of range.\n");
  766. return;
  767. }
  768. if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
  769. uint64_t address;
  770. /* Create the address based on index and association.
  771. * Bits<20:17> select the way of the cache block involved in
  772. * the operation
  773. * Bits<16:7> of the effect address select the index
  774. */
  775. address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
  776. (assoc << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |
  777. (index << CVMX_L2C_IDX_ADDR_SHIFT));
  778. CVMX_CACHE_WBIL2I(address, 0);
  779. } else {
  780. union cvmx_l2c_dbg l2cdbg;
  781. l2cdbg.u64 = 0;
  782. if (!OCTEON_IS_MODEL(OCTEON_CN30XX))
  783. l2cdbg.s.ppnum = cvmx_get_core_num();
  784. l2cdbg.s.finv = 1;
  785. l2cdbg.s.set = assoc;
  786. cvmx_spinlock_lock(&cvmx_l2c_spinlock);
  787. /*
  788. * Enter debug mode, and make sure all other writes
  789. * complete before we enter debug mode
  790. */
  791. CVMX_SYNC;
  792. cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);
  793. cvmx_read_csr(CVMX_L2C_DBG);
  794. CVMX_PREPARE_FOR_STORE(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
  795. index * CVMX_CACHE_LINE_SIZE),
  796. 0);
  797. /* Exit debug mode */
  798. CVMX_SYNC;
  799. cvmx_write_csr(CVMX_L2C_DBG, 0);
  800. cvmx_read_csr(CVMX_L2C_DBG);
  801. cvmx_spinlock_unlock(&cvmx_l2c_spinlock);
  802. }
  803. }