fsl_pamu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * FSL PAMU driver
  4. *
  5. * Copyright 2012-2016 Freescale Semiconductor, Inc.
  6. */
  7. #include <common.h>
  8. #include <linux/log2.h>
  9. #include <malloc.h>
  10. #include <asm/fsl_pamu.h>
  11. struct paace *ppaact;
  12. struct paace *sec;
  13. unsigned long fspi;
  14. static inline int __ilog2_roundup_64(uint64_t val)
  15. {
  16. if ((val & (val - 1)) == 0)
  17. return __ilog2_u64(val);
  18. else
  19. return __ilog2_u64(val) + 1;
  20. }
  21. static inline int count_lsb_zeroes(unsigned long val)
  22. {
  23. return ffs(val) - 1;
  24. }
  25. static unsigned int map_addrspace_size_to_wse(uint64_t addrspace_size)
  26. {
  27. /* window size is 2^(WSE+1) bytes */
  28. return count_lsb_zeroes(addrspace_size >> PAMU_PAGE_SHIFT) +
  29. PAMU_PAGE_SHIFT - 1;
  30. }
  31. static unsigned int map_subwindow_cnt_to_wce(uint32_t subwindow_cnt)
  32. {
  33. /* window count is 2^(WCE+1) bytes */
  34. return count_lsb_zeroes(subwindow_cnt) - 1;
  35. }
  36. static void pamu_setup_default_xfer_to_host_ppaace(struct paace *ppaace)
  37. {
  38. set_bf(ppaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_PRIMARY);
  39. set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
  40. PAACE_M_COHERENCE_REQ);
  41. }
  42. static void pamu_setup_default_xfer_to_host_spaace(struct paace *spaace)
  43. {
  44. set_bf(spaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_SECONDARY);
  45. set_bf(spaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
  46. PAACE_M_COHERENCE_REQ);
  47. }
  48. /** Sets up PPAACE entry for specified liodn
  49. *
  50. * @param[in] liodn Logical IO device number
  51. * @param[in] win_addr starting address of DSA window
  52. * @param[in] win-size size of DSA window
  53. * @param[in] omi Operation mapping index -- if ~omi == 0 then omi
  54. not defined
  55. * @param[in] stashid cache stash id for associated cpu -- if ~stashid == 0
  56. then stashid not defined
  57. * @param[in] snoopid snoop id for hardware coherency -- if ~snoopid == 0
  58. then snoopid not defined
  59. * @param[in] subwin_cnt number of sub-windows
  60. *
  61. * @return Returns 0 upon success else error code < 0 returned
  62. */
  63. static int pamu_config_ppaace(uint32_t liodn, uint64_t win_addr,
  64. uint64_t win_size, uint32_t omi,
  65. uint32_t snoopid, uint32_t stashid,
  66. uint32_t subwin_cnt)
  67. {
  68. struct paace *ppaace;
  69. if ((win_size & (win_size - 1)) || win_size < PAMU_PAGE_SIZE)
  70. return -1;
  71. if (win_addr & (win_size - 1))
  72. return -2;
  73. if (liodn > NUM_PPAACT_ENTRIES) {
  74. printf("Entries in PPACT not sufficient\n");
  75. return -3;
  76. }
  77. ppaace = &ppaact[liodn];
  78. /* window size is 2^(WSE+1) bytes */
  79. set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE,
  80. map_addrspace_size_to_wse(win_size));
  81. pamu_setup_default_xfer_to_host_ppaace(ppaace);
  82. if (sizeof(phys_addr_t) > 4)
  83. ppaace->wbah = (u64)win_addr >> (PAMU_PAGE_SHIFT + 20);
  84. else
  85. ppaace->wbah = 0;
  86. set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL,
  87. (win_addr >> PAMU_PAGE_SHIFT));
  88. /* set up operation mapping if it's configured */
  89. if (omi < OME_NUMBER_ENTRIES) {
  90. set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
  91. ppaace->op_encode.index_ot.omi = omi;
  92. } else if (~omi != 0) {
  93. return -3;
  94. }
  95. /* configure stash id */
  96. if (~stashid != 0)
  97. set_bf(ppaace->impl_attr, PAACE_IA_CID, stashid);
  98. /* configure snoop id */
  99. if (~snoopid != 0)
  100. ppaace->domain_attr.to_host.snpid = snoopid;
  101. if (subwin_cnt) {
  102. /* window count is 2^(WCE+1) bytes */
  103. set_bf(ppaace->impl_attr, PAACE_IA_WCE,
  104. map_subwindow_cnt_to_wce(subwin_cnt));
  105. set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0x1);
  106. ppaace->fspi = fspi;
  107. fspi = fspi + DEFAULT_NUM_SUBWINDOWS - 1;
  108. } else {
  109. set_bf(ppaace->addr_bitfields, PAACE_AF_AP, PAACE_AP_PERMS_ALL);
  110. }
  111. asm volatile("sync" : : : "memory");
  112. /* Mark the ppace entry valid */
  113. ppaace->addr_bitfields |= PAACE_V_VALID;
  114. asm volatile("sync" : : : "memory");
  115. return 0;
  116. }
  117. static int pamu_config_spaace(uint32_t liodn,
  118. uint64_t subwin_size, uint64_t subwin_addr, uint64_t size,
  119. uint32_t omi, uint32_t snoopid, uint32_t stashid)
  120. {
  121. struct paace *paace;
  122. /* Align start addr of subwin to subwindoe size */
  123. uint64_t sec_addr = subwin_addr & ~(subwin_size - 1);
  124. uint64_t end_addr = subwin_addr + size;
  125. int size_shift = __ilog2_u64(subwin_size);
  126. uint64_t win_size = 0;
  127. uint32_t index, swse;
  128. unsigned long fspi_idx;
  129. /* Recalculate the size */
  130. size = end_addr - sec_addr;
  131. if (!subwin_size)
  132. return -1;
  133. if (liodn > NUM_PPAACT_ENTRIES) {
  134. printf("LIODN No programmed %d > no. of PPAACT entries %d\n",
  135. liodn, NUM_PPAACT_ENTRIES);
  136. return -1;
  137. }
  138. while (sec_addr < end_addr) {
  139. debug("sec_addr < end_addr is %llx < %llx\n", sec_addr,
  140. end_addr);
  141. paace = &ppaact[liodn];
  142. if (!paace)
  143. return -1;
  144. fspi_idx = paace->fspi;
  145. /* Calculating the win_size here as if we map in index 0,
  146. paace entry woudl need to be programmed for SWSE */
  147. win_size = end_addr - sec_addr;
  148. win_size = 1 << __ilog2_roundup_64(win_size);
  149. if (win_size > subwin_size)
  150. win_size = subwin_size;
  151. else if (win_size < PAMU_PAGE_SIZE)
  152. win_size = PAMU_PAGE_SIZE;
  153. debug("win_size is %llx\n", win_size);
  154. swse = map_addrspace_size_to_wse(win_size);
  155. index = sec_addr >> size_shift;
  156. if (index == 0) {
  157. set_bf(paace->win_bitfields, PAACE_WIN_SWSE, swse);
  158. set_bf(paace->addr_bitfields, PAACE_AF_AP,
  159. PAACE_AP_PERMS_ALL);
  160. sec_addr += subwin_size;
  161. continue;
  162. }
  163. paace = sec + fspi_idx + index - 1;
  164. debug("SPAACT:Writing at location %p, index %d\n", paace,
  165. index);
  166. pamu_setup_default_xfer_to_host_spaace(paace);
  167. set_bf(paace->addr_bitfields, SPAACE_AF_LIODN, liodn);
  168. set_bf(paace->addr_bitfields, PAACE_AF_AP, PAACE_AP_PERMS_ALL);
  169. /* configure snoop id */
  170. if (~snoopid != 0)
  171. paace->domain_attr.to_host.snpid = snoopid;
  172. if (paace->addr_bitfields & PAACE_V_VALID) {
  173. debug("Reached overlap condition\n");
  174. debug("%d < %d\n", get_bf(paace->win_bitfields,
  175. PAACE_WIN_SWSE), swse);
  176. if (get_bf(paace->win_bitfields, PAACE_WIN_SWSE) < swse)
  177. set_bf(paace->win_bitfields, PAACE_WIN_SWSE,
  178. swse);
  179. } else {
  180. set_bf(paace->win_bitfields, PAACE_WIN_SWSE, swse);
  181. }
  182. paace->addr_bitfields |= PAACE_V_VALID;
  183. sec_addr += subwin_size;
  184. }
  185. return 0;
  186. }
  187. int pamu_init(void)
  188. {
  189. u32 base_addr = CONFIG_SYS_PAMU_ADDR;
  190. struct ccsr_pamu *regs;
  191. u32 i = 0;
  192. u64 ppaact_phys, ppaact_lim, ppaact_size;
  193. u64 spaact_phys, spaact_lim, spaact_size;
  194. ppaact_size = sizeof(struct paace) * NUM_PPAACT_ENTRIES;
  195. spaact_size = sizeof(struct paace) * NUM_SPAACT_ENTRIES;
  196. /* Allocate space for Primary PAACT Table */
  197. #if (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_PPAACT_ADDR))
  198. ppaact = (void *)CONFIG_SPL_PPAACT_ADDR;
  199. #else
  200. ppaact = memalign(PAMU_TABLE_ALIGNMENT, ppaact_size);
  201. if (!ppaact)
  202. return -1;
  203. #endif
  204. memset(ppaact, 0, ppaact_size);
  205. /* Allocate space for Secondary PAACT Table */
  206. #if (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_SPAACT_ADDR))
  207. sec = (void *)CONFIG_SPL_SPAACT_ADDR;
  208. #else
  209. sec = memalign(PAMU_TABLE_ALIGNMENT, spaact_size);
  210. if (!sec)
  211. return -1;
  212. #endif
  213. memset(sec, 0, spaact_size);
  214. ppaact_phys = virt_to_phys((void *)ppaact);
  215. ppaact_lim = ppaact_phys + ppaact_size;
  216. spaact_phys = (uint64_t)virt_to_phys((void *)sec);
  217. spaact_lim = spaact_phys + spaact_size;
  218. /* Configure all PAMU's */
  219. for (i = 0; i < CONFIG_NUM_PAMU; i++) {
  220. regs = (struct ccsr_pamu *)base_addr;
  221. out_be32(&regs->ppbah, ppaact_phys >> 32);
  222. out_be32(&regs->ppbal, (uint32_t)ppaact_phys);
  223. out_be32(&regs->pplah, (ppaact_lim) >> 32);
  224. out_be32(&regs->pplal, (uint32_t)ppaact_lim);
  225. if (sec != NULL) {
  226. out_be32(&regs->spbah, spaact_phys >> 32);
  227. out_be32(&regs->spbal, (uint32_t)spaact_phys);
  228. out_be32(&regs->splah, spaact_lim >> 32);
  229. out_be32(&regs->splal, (uint32_t)spaact_lim);
  230. }
  231. asm volatile("sync" : : : "memory");
  232. base_addr += PAMU_OFFSET;
  233. }
  234. return 0;
  235. }
  236. void pamu_enable(void)
  237. {
  238. u32 i = 0;
  239. u32 base_addr = CONFIG_SYS_PAMU_ADDR;
  240. for (i = 0; i < CONFIG_NUM_PAMU; i++) {
  241. setbits_be32((void *)base_addr + PAMU_PCR_OFFSET,
  242. PAMU_PCR_PE);
  243. asm volatile("sync" : : : "memory");
  244. base_addr += PAMU_OFFSET;
  245. }
  246. }
  247. void pamu_reset(void)
  248. {
  249. u32 i = 0;
  250. u32 base_addr = CONFIG_SYS_PAMU_ADDR;
  251. struct ccsr_pamu *regs;
  252. for (i = 0; i < CONFIG_NUM_PAMU; i++) {
  253. regs = (struct ccsr_pamu *)base_addr;
  254. /* Clear PPAACT Base register */
  255. out_be32(&regs->ppbah, 0);
  256. out_be32(&regs->ppbal, 0);
  257. out_be32(&regs->pplah, 0);
  258. out_be32(&regs->pplal, 0);
  259. out_be32(&regs->spbah, 0);
  260. out_be32(&regs->spbal, 0);
  261. out_be32(&regs->splah, 0);
  262. out_be32(&regs->splal, 0);
  263. clrbits_be32((void *)regs + PAMU_PCR_OFFSET, PAMU_PCR_PE);
  264. asm volatile("sync" : : : "memory");
  265. base_addr += PAMU_OFFSET;
  266. }
  267. }
  268. void pamu_disable(void)
  269. {
  270. u32 i = 0;
  271. u32 base_addr = CONFIG_SYS_PAMU_ADDR;
  272. for (i = 0; i < CONFIG_NUM_PAMU; i++) {
  273. clrbits_be32((void *)base_addr + PAMU_PCR_OFFSET, PAMU_PCR_PE);
  274. asm volatile("sync" : : : "memory");
  275. base_addr += PAMU_OFFSET;
  276. }
  277. }
  278. static uint64_t find_max(uint64_t arr[], int num)
  279. {
  280. int i = 0;
  281. int max = 0;
  282. for (i = 1 ; i < num; i++)
  283. if (arr[max] < arr[i])
  284. max = i;
  285. return arr[max];
  286. }
  287. static uint64_t find_min(uint64_t arr[], int num)
  288. {
  289. int i = 0;
  290. int min = 0;
  291. for (i = 1 ; i < num; i++)
  292. if (arr[min] > arr[i])
  293. min = i;
  294. return arr[min];
  295. }
  296. static uint32_t get_win_cnt(uint64_t size)
  297. {
  298. uint32_t win_cnt = DEFAULT_NUM_SUBWINDOWS;
  299. while (win_cnt && (size/win_cnt) < PAMU_PAGE_SIZE)
  300. win_cnt >>= 1;
  301. return win_cnt;
  302. }
  303. int config_pamu(struct pamu_addr_tbl *tbl, int num_entries, uint32_t liodn)
  304. {
  305. int i = 0;
  306. int ret = 0;
  307. uint32_t num_sec_windows = 0;
  308. uint32_t num_windows = 0;
  309. uint64_t min_addr, max_addr;
  310. uint64_t size;
  311. uint64_t subwin_size;
  312. int sizebit;
  313. min_addr = find_min(tbl->start_addr, num_entries);
  314. max_addr = find_max(tbl->end_addr, num_entries);
  315. size = max_addr - min_addr + 1;
  316. if (!size)
  317. return -1;
  318. sizebit = __ilog2_roundup_64(size);
  319. size = 1 << sizebit;
  320. debug("min start_addr is %llx\n", min_addr);
  321. debug("max end_addr is %llx\n", max_addr);
  322. debug("size found is %llx\n", size);
  323. if (size < PAMU_PAGE_SIZE)
  324. size = PAMU_PAGE_SIZE;
  325. while (1) {
  326. min_addr = min_addr & ~(size - 1);
  327. if (min_addr + size > max_addr)
  328. break;
  329. size <<= 1;
  330. if (!size)
  331. return -1;
  332. }
  333. debug("PAACT :Base addr is %llx\n", min_addr);
  334. debug("PAACT : Size is %llx\n", size);
  335. num_windows = get_win_cnt(size);
  336. /* For a single window, no spaact entries are required
  337. * sec_sub_window count = 0 */
  338. if (num_windows > 1)
  339. num_sec_windows = num_windows;
  340. else
  341. num_sec_windows = 0;
  342. ret = pamu_config_ppaace(liodn, min_addr,
  343. size , -1, -1, -1, num_sec_windows);
  344. if (ret < 0)
  345. return ret;
  346. debug("configured ppace\n");
  347. if (num_sec_windows) {
  348. subwin_size = size >> count_lsb_zeroes(num_sec_windows);
  349. debug("subwin_size is %llx\n", subwin_size);
  350. for (i = 0; i < num_entries; i++) {
  351. ret = pamu_config_spaace(liodn,
  352. subwin_size, tbl->start_addr[i] - min_addr,
  353. tbl->size[i], -1, -1, -1);
  354. if (ret < 0)
  355. return ret;
  356. }
  357. }
  358. return ret;
  359. }