proc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/module.h>
  4. #include <linux/sched.h>
  5. #include <linux/mm.h>
  6. #include <asm/nds32.h>
  7. #include <asm/pgtable.h>
  8. #include <asm/tlbflush.h>
  9. #include <asm/cacheflush.h>
  10. #include <asm/l2_cache.h>
  11. #include <nds32_intrinsic.h>
  12. #include <asm/cache_info.h>
  13. extern struct cache_info L1_cache_info[2];
  14. int va_kernel_present(unsigned long addr)
  15. {
  16. pmd_t *pmd;
  17. pte_t *ptep, pte;
  18. pmd = pmd_offset(pgd_offset_k(addr), addr);
  19. if (!pmd_none(*pmd)) {
  20. ptep = pte_offset_map(pmd, addr);
  21. pte = *ptep;
  22. if (pte_present(pte))
  23. return pte;
  24. }
  25. return 0;
  26. }
  27. pte_t va_present(struct mm_struct * mm, unsigned long addr)
  28. {
  29. pgd_t *pgd;
  30. pud_t *pud;
  31. pmd_t *pmd;
  32. pte_t *ptep, pte;
  33. pgd = pgd_offset(mm, addr);
  34. if (!pgd_none(*pgd)) {
  35. pud = pud_offset(pgd, addr);
  36. if (!pud_none(*pud)) {
  37. pmd = pmd_offset(pud, addr);
  38. if (!pmd_none(*pmd)) {
  39. ptep = pte_offset_map(pmd, addr);
  40. pte = *ptep;
  41. if (pte_present(pte))
  42. return pte;
  43. }
  44. }
  45. }
  46. return 0;
  47. }
  48. int va_readable(struct pt_regs *regs, unsigned long addr)
  49. {
  50. struct mm_struct *mm = current->mm;
  51. pte_t pte;
  52. int ret = 0;
  53. if (user_mode(regs)) {
  54. /* user mode */
  55. pte = va_present(mm, addr);
  56. if (!pte && pte_read(pte))
  57. ret = 1;
  58. } else {
  59. /* superuser mode is always readable, so we can only
  60. * check it is present or not*/
  61. return (! !va_kernel_present(addr));
  62. }
  63. return ret;
  64. }
  65. int va_writable(struct pt_regs *regs, unsigned long addr)
  66. {
  67. struct mm_struct *mm = current->mm;
  68. pte_t pte;
  69. int ret = 0;
  70. if (user_mode(regs)) {
  71. /* user mode */
  72. pte = va_present(mm, addr);
  73. if (!pte && pte_write(pte))
  74. ret = 1;
  75. } else {
  76. /* superuser mode */
  77. pte = va_kernel_present(addr);
  78. if (!pte && pte_kernel_write(pte))
  79. ret = 1;
  80. }
  81. return ret;
  82. }
  83. /*
  84. * All
  85. */
  86. void cpu_icache_inval_all(void)
  87. {
  88. unsigned long end, line_size;
  89. line_size = L1_cache_info[ICACHE].line_size;
  90. end =
  91. line_size * L1_cache_info[ICACHE].ways * L1_cache_info[ICACHE].sets;
  92. do {
  93. end -= line_size;
  94. __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
  95. end -= line_size;
  96. __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
  97. end -= line_size;
  98. __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
  99. end -= line_size;
  100. __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL"::"r" (end));
  101. } while (end > 0);
  102. __nds32__isb();
  103. }
  104. void cpu_dcache_inval_all(void)
  105. {
  106. __nds32__cctl_l1d_invalall();
  107. }
  108. #ifdef CONFIG_CACHE_L2
  109. void dcache_wb_all_level(void)
  110. {
  111. unsigned long flags, cmd;
  112. local_irq_save(flags);
  113. __nds32__cctl_l1d_wball_alvl();
  114. /* Section 1: Ensure the section 2 & 3 program code execution after */
  115. __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
  116. /* Section 2: Confirm the writeback all level is done in CPU and L2C */
  117. cmd = CCTL_CMD_L2_SYNC;
  118. L2_CMD_RDY();
  119. L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
  120. L2_CMD_RDY();
  121. /* Section 3: Writeback whole L2 cache */
  122. cmd = CCTL_ALL_CMD | CCTL_CMD_L2_IX_WB;
  123. L2_CMD_RDY();
  124. L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
  125. L2_CMD_RDY();
  126. __nds32__msync_all();
  127. local_irq_restore(flags);
  128. }
  129. EXPORT_SYMBOL(dcache_wb_all_level);
  130. #endif
  131. void cpu_dcache_wb_all(void)
  132. {
  133. __nds32__cctl_l1d_wball_one_lvl();
  134. __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
  135. }
  136. void cpu_dcache_wbinval_all(void)
  137. {
  138. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  139. unsigned long flags;
  140. local_irq_save(flags);
  141. #endif
  142. cpu_dcache_wb_all();
  143. cpu_dcache_inval_all();
  144. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  145. local_irq_restore(flags);
  146. #endif
  147. }
  148. /*
  149. * Page
  150. */
  151. void cpu_icache_inval_page(unsigned long start)
  152. {
  153. unsigned long line_size, end;
  154. line_size = L1_cache_info[ICACHE].line_size;
  155. end = start + PAGE_SIZE;
  156. do {
  157. end -= line_size;
  158. __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
  159. end -= line_size;
  160. __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
  161. end -= line_size;
  162. __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
  163. end -= line_size;
  164. __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (end));
  165. } while (end != start);
  166. __nds32__isb();
  167. }
  168. void cpu_dcache_inval_page(unsigned long start)
  169. {
  170. unsigned long line_size, end;
  171. line_size = L1_cache_info[DCACHE].line_size;
  172. end = start + PAGE_SIZE;
  173. do {
  174. end -= line_size;
  175. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  176. end -= line_size;
  177. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  178. end -= line_size;
  179. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  180. end -= line_size;
  181. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  182. } while (end != start);
  183. }
  184. void cpu_dcache_wb_page(unsigned long start)
  185. {
  186. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  187. unsigned long line_size, end;
  188. line_size = L1_cache_info[DCACHE].line_size;
  189. end = start + PAGE_SIZE;
  190. do {
  191. end -= line_size;
  192. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  193. end -= line_size;
  194. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  195. end -= line_size;
  196. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  197. end -= line_size;
  198. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  199. } while (end != start);
  200. __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
  201. #endif
  202. }
  203. void cpu_dcache_wbinval_page(unsigned long start)
  204. {
  205. unsigned long line_size, end;
  206. line_size = L1_cache_info[DCACHE].line_size;
  207. end = start + PAGE_SIZE;
  208. do {
  209. end -= line_size;
  210. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  211. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  212. #endif
  213. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  214. end -= line_size;
  215. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  216. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  217. #endif
  218. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  219. end -= line_size;
  220. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  221. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  222. #endif
  223. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  224. end -= line_size;
  225. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  226. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (end));
  227. #endif
  228. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (end));
  229. } while (end != start);
  230. __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
  231. }
  232. void cpu_cache_wbinval_page(unsigned long page, int flushi)
  233. {
  234. cpu_dcache_wbinval_page(page);
  235. if (flushi)
  236. cpu_icache_inval_page(page);
  237. }
  238. /*
  239. * Range
  240. */
  241. void cpu_icache_inval_range(unsigned long start, unsigned long end)
  242. {
  243. unsigned long line_size;
  244. line_size = L1_cache_info[ICACHE].line_size;
  245. while (end > start) {
  246. __asm__ volatile ("\n\tcctl %0, L1I_VA_INVAL"::"r" (start));
  247. start += line_size;
  248. }
  249. __nds32__isb();
  250. }
  251. void cpu_dcache_inval_range(unsigned long start, unsigned long end)
  252. {
  253. unsigned long line_size;
  254. line_size = L1_cache_info[DCACHE].line_size;
  255. while (end > start) {
  256. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (start));
  257. start += line_size;
  258. }
  259. }
  260. void cpu_dcache_wb_range(unsigned long start, unsigned long end)
  261. {
  262. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  263. unsigned long line_size;
  264. line_size = L1_cache_info[DCACHE].line_size;
  265. while (end > start) {
  266. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (start));
  267. start += line_size;
  268. }
  269. __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
  270. #endif
  271. }
  272. void cpu_dcache_wbinval_range(unsigned long start, unsigned long end)
  273. {
  274. unsigned long line_size;
  275. line_size = L1_cache_info[DCACHE].line_size;
  276. while (end > start) {
  277. #ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
  278. __asm__ volatile ("\n\tcctl %0, L1D_VA_WB"::"r" (start));
  279. #endif
  280. __asm__ volatile ("\n\tcctl %0, L1D_VA_INVAL"::"r" (start));
  281. start += line_size;
  282. }
  283. __nds32__cctlidx_read(NDS32_CCTL_L1D_IX_RWD,0);
  284. }
  285. void cpu_cache_wbinval_range(unsigned long start, unsigned long end, int flushi)
  286. {
  287. unsigned long line_size, align_start, align_end;
  288. line_size = L1_cache_info[DCACHE].line_size;
  289. align_start = start & ~(line_size - 1);
  290. align_end = (end + line_size - 1) & ~(line_size - 1);
  291. cpu_dcache_wbinval_range(align_start, align_end);
  292. if (flushi) {
  293. line_size = L1_cache_info[ICACHE].line_size;
  294. align_start = start & ~(line_size - 1);
  295. align_end = (end + line_size - 1) & ~(line_size - 1);
  296. cpu_icache_inval_range(align_start, align_end);
  297. }
  298. }
  299. void cpu_cache_wbinval_range_check(struct vm_area_struct *vma,
  300. unsigned long start, unsigned long end,
  301. bool flushi, bool wbd)
  302. {
  303. unsigned long line_size, t_start, t_end;
  304. if (!flushi && !wbd)
  305. return;
  306. line_size = L1_cache_info[DCACHE].line_size;
  307. start = start & ~(line_size - 1);
  308. end = (end + line_size - 1) & ~(line_size - 1);
  309. if ((end - start) > (8 * PAGE_SIZE)) {
  310. if (wbd)
  311. cpu_dcache_wbinval_all();
  312. if (flushi)
  313. cpu_icache_inval_all();
  314. return;
  315. }
  316. t_start = (start + PAGE_SIZE) & PAGE_MASK;
  317. t_end = ((end - 1) & PAGE_MASK);
  318. if ((start & PAGE_MASK) == t_end) {
  319. if (va_present(vma->vm_mm, start)) {
  320. if (wbd)
  321. cpu_dcache_wbinval_range(start, end);
  322. if (flushi)
  323. cpu_icache_inval_range(start, end);
  324. }
  325. return;
  326. }
  327. if (va_present(vma->vm_mm, start)) {
  328. if (wbd)
  329. cpu_dcache_wbinval_range(start, t_start);
  330. if (flushi)
  331. cpu_icache_inval_range(start, t_start);
  332. }
  333. if (va_present(vma->vm_mm, end - 1)) {
  334. if (wbd)
  335. cpu_dcache_wbinval_range(t_end, end);
  336. if (flushi)
  337. cpu_icache_inval_range(t_end, end);
  338. }
  339. while (t_start < t_end) {
  340. if (va_present(vma->vm_mm, t_start)) {
  341. if (wbd)
  342. cpu_dcache_wbinval_page(t_start);
  343. if (flushi)
  344. cpu_icache_inval_page(t_start);
  345. }
  346. t_start += PAGE_SIZE;
  347. }
  348. }
  349. #ifdef CONFIG_CACHE_L2
  350. static inline void cpu_l2cache_op(unsigned long start, unsigned long end, unsigned long op)
  351. {
  352. if (atl2c_base) {
  353. unsigned long p_start = __pa(start);
  354. unsigned long p_end = __pa(end);
  355. unsigned long cmd;
  356. unsigned long line_size;
  357. /* TODO Can Use PAGE Mode to optimize if range large than PAGE_SIZE */
  358. line_size = L2_CACHE_LINE_SIZE();
  359. p_start = p_start & (~(line_size - 1));
  360. p_end = (p_end + line_size - 1) & (~(line_size - 1));
  361. cmd =
  362. (p_start & ~(line_size - 1)) | op |
  363. CCTL_SINGLE_CMD;
  364. do {
  365. L2_CMD_RDY();
  366. L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
  367. cmd += line_size;
  368. p_start += line_size;
  369. } while (p_end > p_start);
  370. cmd = CCTL_CMD_L2_SYNC;
  371. L2_CMD_RDY();
  372. L2C_W_REG(L2_CCTL_CMD_OFF, cmd);
  373. L2_CMD_RDY();
  374. }
  375. }
  376. #else
  377. #define cpu_l2cache_op(start,end,op) do { } while (0)
  378. #endif
  379. /*
  380. * DMA
  381. */
  382. void cpu_dma_wb_range(unsigned long start, unsigned long end)
  383. {
  384. unsigned long line_size;
  385. unsigned long flags;
  386. line_size = L1_cache_info[DCACHE].line_size;
  387. start = start & (~(line_size - 1));
  388. end = (end + line_size - 1) & (~(line_size - 1));
  389. if (unlikely(start == end))
  390. return;
  391. local_irq_save(flags);
  392. cpu_dcache_wb_range(start, end);
  393. cpu_l2cache_op(start, end, CCTL_CMD_L2_PA_WB);
  394. __nds32__msync_all();
  395. local_irq_restore(flags);
  396. }
  397. void cpu_dma_inval_range(unsigned long start, unsigned long end)
  398. {
  399. unsigned long line_size;
  400. unsigned long old_start = start;
  401. unsigned long old_end = end;
  402. unsigned long flags;
  403. line_size = L1_cache_info[DCACHE].line_size;
  404. start = start & (~(line_size - 1));
  405. end = (end + line_size - 1) & (~(line_size - 1));
  406. if (unlikely(start == end))
  407. return;
  408. local_irq_save(flags);
  409. if (start != old_start) {
  410. cpu_dcache_wbinval_range(start, start + line_size);
  411. cpu_l2cache_op(start, start + line_size, CCTL_CMD_L2_PA_WBINVAL);
  412. }
  413. if (end != old_end) {
  414. cpu_dcache_wbinval_range(end - line_size, end);
  415. cpu_l2cache_op(end - line_size, end, CCTL_CMD_L2_PA_WBINVAL);
  416. }
  417. cpu_dcache_inval_range(start, end);
  418. cpu_l2cache_op(start, end, CCTL_CMD_L2_PA_INVAL);
  419. __nds32__msync_all();
  420. local_irq_restore(flags);
  421. }
  422. void cpu_dma_wbinval_range(unsigned long start, unsigned long end)
  423. {
  424. unsigned long line_size;
  425. unsigned long flags;
  426. line_size = L1_cache_info[DCACHE].line_size;
  427. start = start & (~(line_size - 1));
  428. end = (end + line_size - 1) & (~(line_size - 1));
  429. if (unlikely(start == end))
  430. return;
  431. local_irq_save(flags);
  432. cpu_dcache_wbinval_range(start, end);
  433. cpu_l2cache_op(start, end, CCTL_CMD_L2_PA_WBINVAL);
  434. __nds32__msync_all();
  435. local_irq_restore(flags);
  436. }
  437. void cpu_proc_init(void)
  438. {
  439. }
  440. void cpu_proc_fin(void)
  441. {
  442. }
  443. void cpu_do_idle(void)
  444. {
  445. __nds32__standby_no_wake_grant();
  446. }
  447. void cpu_reset(unsigned long reset)
  448. {
  449. u32 tmp;
  450. GIE_DISABLE();
  451. tmp = __nds32__mfsr(NDS32_SR_CACHE_CTL);
  452. tmp &= ~(CACHE_CTL_mskIC_EN | CACHE_CTL_mskDC_EN);
  453. __nds32__mtsr_isb(tmp, NDS32_SR_CACHE_CTL);
  454. cpu_dcache_wbinval_all();
  455. cpu_icache_inval_all();
  456. __asm__ __volatile__("jr.toff %0\n\t"::"r"(reset));
  457. }
  458. void cpu_switch_mm(struct mm_struct *mm)
  459. {
  460. unsigned long cid;
  461. cid = __nds32__mfsr(NDS32_SR_TLB_MISC);
  462. cid = (cid & ~TLB_MISC_mskCID) | mm->context.id;
  463. __nds32__mtsr_dsb(cid, NDS32_SR_TLB_MISC);
  464. __nds32__mtsr_isb(__pa(mm->pgd), NDS32_SR_L1_PPTB);
  465. }