4 * Copyright 2012-2016 Freescale Semiconductor, Inc.
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/log2.h>
12 #include <asm/fsl_pamu.h>
18 static inline int __ilog2_roundup_64(uint64_t val)
20 if ((val & (val - 1)) == 0)
21 return __ilog2_u64(val);
23 return __ilog2_u64(val) + 1;
27 static inline int count_lsb_zeroes(unsigned long val)
32 static unsigned int map_addrspace_size_to_wse(uint64_t addrspace_size)
34 /* window size is 2^(WSE+1) bytes */
35 return count_lsb_zeroes(addrspace_size >> PAMU_PAGE_SHIFT) +
39 static unsigned int map_subwindow_cnt_to_wce(uint32_t subwindow_cnt)
41 /* window count is 2^(WCE+1) bytes */
42 return count_lsb_zeroes(subwindow_cnt) - 1;
45 static void pamu_setup_default_xfer_to_host_ppaace(struct paace *ppaace)
47 set_bf(ppaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_PRIMARY);
48 set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
49 PAACE_M_COHERENCE_REQ);
52 static void pamu_setup_default_xfer_to_host_spaace(struct paace *spaace)
54 set_bf(spaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_SECONDARY);
55 set_bf(spaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
56 PAACE_M_COHERENCE_REQ);
59 /** Sets up PPAACE entry for specified liodn
61 * @param[in] liodn Logical IO device number
62 * @param[in] win_addr starting address of DSA window
63 * @param[in] win-size size of DSA window
64 * @param[in] omi Operation mapping index -- if ~omi == 0 then omi
66 * @param[in] stashid cache stash id for associated cpu -- if ~stashid == 0
67 then stashid not defined
68 * @param[in] snoopid snoop id for hardware coherency -- if ~snoopid == 0
69 then snoopid not defined
70 * @param[in] subwin_cnt number of sub-windows
72 * @return Returns 0 upon success else error code < 0 returned
74 static int pamu_config_ppaace(uint32_t liodn, uint64_t win_addr,
75 uint64_t win_size, uint32_t omi,
76 uint32_t snoopid, uint32_t stashid,
81 if ((win_size & (win_size - 1)) || win_size < PAMU_PAGE_SIZE)
84 if (win_addr & (win_size - 1))
87 if (liodn > NUM_PPAACT_ENTRIES) {
88 printf("Entries in PPACT not sufficient\n");
92 ppaace = &ppaact[liodn];
94 /* window size is 2^(WSE+1) bytes */
95 set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE,
96 map_addrspace_size_to_wse(win_size));
98 pamu_setup_default_xfer_to_host_ppaace(ppaace);
100 if (sizeof(phys_addr_t) > 4)
101 ppaace->wbah = (u64)win_addr >> (PAMU_PAGE_SHIFT + 20);
105 set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL,
106 (win_addr >> PAMU_PAGE_SHIFT));
108 /* set up operation mapping if it's configured */
109 if (omi < OME_NUMBER_ENTRIES) {
110 set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
111 ppaace->op_encode.index_ot.omi = omi;
112 } else if (~omi != 0) {
116 /* configure stash id */
118 set_bf(ppaace->impl_attr, PAACE_IA_CID, stashid);
120 /* configure snoop id */
122 ppaace->domain_attr.to_host.snpid = snoopid;
125 /* window count is 2^(WCE+1) bytes */
126 set_bf(ppaace->impl_attr, PAACE_IA_WCE,
127 map_subwindow_cnt_to_wce(subwin_cnt));
128 set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0x1);
130 fspi = fspi + DEFAULT_NUM_SUBWINDOWS - 1;
132 set_bf(ppaace->addr_bitfields, PAACE_AF_AP, PAACE_AP_PERMS_ALL);
135 asm volatile("sync" : : : "memory");
136 /* Mark the ppace entry valid */
137 ppaace->addr_bitfields |= PAACE_V_VALID;
138 asm volatile("sync" : : : "memory");
143 static int pamu_config_spaace(uint32_t liodn,
144 uint64_t subwin_size, uint64_t subwin_addr, uint64_t size,
145 uint32_t omi, uint32_t snoopid, uint32_t stashid)
148 /* Align start addr of subwin to subwindoe size */
149 uint64_t sec_addr = subwin_addr & ~(subwin_size - 1);
150 uint64_t end_addr = subwin_addr + size;
151 int size_shift = __ilog2_u64(subwin_size);
152 uint64_t win_size = 0;
153 uint32_t index, swse;
154 unsigned long fspi_idx;
156 /* Recalculate the size */
157 size = end_addr - sec_addr;
162 if (liodn > NUM_PPAACT_ENTRIES) {
163 printf("LIODN No programmed %d > no. of PPAACT entries %d\n",
164 liodn, NUM_PPAACT_ENTRIES);
168 while (sec_addr < end_addr) {
169 debug("sec_addr < end_addr is %llx < %llx\n", sec_addr,
171 paace = &ppaact[liodn];
174 fspi_idx = paace->fspi;
176 /* Calculating the win_size here as if we map in index 0,
177 paace entry woudl need to be programmed for SWSE */
178 win_size = end_addr - sec_addr;
179 win_size = 1 << __ilog2_roundup_64(win_size);
181 if (win_size > subwin_size)
182 win_size = subwin_size;
183 else if (win_size < PAMU_PAGE_SIZE)
184 win_size = PAMU_PAGE_SIZE;
186 debug("win_size is %llx\n", win_size);
188 swse = map_addrspace_size_to_wse(win_size);
189 index = sec_addr >> size_shift;
192 set_bf(paace->win_bitfields, PAACE_WIN_SWSE, swse);
193 set_bf(paace->addr_bitfields, PAACE_AF_AP,
195 sec_addr += subwin_size;
199 paace = sec + fspi_idx + index - 1;
201 debug("SPAACT:Writing at location %p, index %d\n", paace,
204 pamu_setup_default_xfer_to_host_spaace(paace);
205 set_bf(paace->addr_bitfields, SPAACE_AF_LIODN, liodn);
206 set_bf(paace->addr_bitfields, PAACE_AF_AP, PAACE_AP_PERMS_ALL);
208 /* configure snoop id */
210 paace->domain_attr.to_host.snpid = snoopid;
212 if (paace->addr_bitfields & PAACE_V_VALID) {
213 debug("Reached overlap condition\n");
214 debug("%d < %d\n", get_bf(paace->win_bitfields,
215 PAACE_WIN_SWSE), swse);
216 if (get_bf(paace->win_bitfields, PAACE_WIN_SWSE) < swse)
217 set_bf(paace->win_bitfields, PAACE_WIN_SWSE,
220 set_bf(paace->win_bitfields, PAACE_WIN_SWSE, swse);
223 paace->addr_bitfields |= PAACE_V_VALID;
224 sec_addr += subwin_size;
232 u32 base_addr = CONFIG_SYS_PAMU_ADDR;
233 struct ccsr_pamu *regs;
235 u64 ppaact_phys, ppaact_lim, ppaact_size;
236 u64 spaact_phys, spaact_lim, spaact_size;
238 ppaact_size = sizeof(struct paace) * NUM_PPAACT_ENTRIES;
239 spaact_size = sizeof(struct paace) * NUM_SPAACT_ENTRIES;
241 /* Allocate space for Primary PAACT Table */
242 #if (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_PPAACT_ADDR))
243 ppaact = (void *)CONFIG_SPL_PPAACT_ADDR;
245 ppaact = memalign(PAMU_TABLE_ALIGNMENT, ppaact_size);
249 memset(ppaact, 0, ppaact_size);
251 /* Allocate space for Secondary PAACT Table */
252 #if (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_SPAACT_ADDR))
253 sec = (void *)CONFIG_SPL_SPAACT_ADDR;
255 sec = memalign(PAMU_TABLE_ALIGNMENT, spaact_size);
259 memset(sec, 0, spaact_size);
261 ppaact_phys = virt_to_phys((void *)ppaact);
262 ppaact_lim = ppaact_phys + ppaact_size;
264 spaact_phys = (uint64_t)virt_to_phys((void *)sec);
265 spaact_lim = spaact_phys + spaact_size;
267 /* Configure all PAMU's */
268 for (i = 0; i < CONFIG_NUM_PAMU; i++) {
269 regs = (struct ccsr_pamu *)base_addr;
271 out_be32(®s->ppbah, ppaact_phys >> 32);
272 out_be32(®s->ppbal, (uint32_t)ppaact_phys);
274 out_be32(®s->pplah, (ppaact_lim) >> 32);
275 out_be32(®s->pplal, (uint32_t)ppaact_lim);
278 out_be32(®s->spbah, spaact_phys >> 32);
279 out_be32(®s->spbal, (uint32_t)spaact_phys);
280 out_be32(®s->splah, spaact_lim >> 32);
281 out_be32(®s->splal, (uint32_t)spaact_lim);
283 asm volatile("sync" : : : "memory");
285 base_addr += PAMU_OFFSET;
291 void pamu_enable(void)
294 u32 base_addr = CONFIG_SYS_PAMU_ADDR;
295 for (i = 0; i < CONFIG_NUM_PAMU; i++) {
296 setbits_be32((void *)base_addr + PAMU_PCR_OFFSET,
298 asm volatile("sync" : : : "memory");
299 base_addr += PAMU_OFFSET;
303 void pamu_reset(void)
306 u32 base_addr = CONFIG_SYS_PAMU_ADDR;
307 struct ccsr_pamu *regs;
309 for (i = 0; i < CONFIG_NUM_PAMU; i++) {
310 regs = (struct ccsr_pamu *)base_addr;
311 /* Clear PPAACT Base register */
312 out_be32(®s->ppbah, 0);
313 out_be32(®s->ppbal, 0);
314 out_be32(®s->pplah, 0);
315 out_be32(®s->pplal, 0);
316 out_be32(®s->spbah, 0);
317 out_be32(®s->spbal, 0);
318 out_be32(®s->splah, 0);
319 out_be32(®s->splal, 0);
321 clrbits_be32((void *)regs + PAMU_PCR_OFFSET, PAMU_PCR_PE);
322 asm volatile("sync" : : : "memory");
323 base_addr += PAMU_OFFSET;
327 void pamu_disable(void)
330 u32 base_addr = CONFIG_SYS_PAMU_ADDR;
333 for (i = 0; i < CONFIG_NUM_PAMU; i++) {
334 clrbits_be32((void *)base_addr + PAMU_PCR_OFFSET, PAMU_PCR_PE);
335 asm volatile("sync" : : : "memory");
336 base_addr += PAMU_OFFSET;
341 static uint64_t find_max(uint64_t arr[], int num)
345 for (i = 1 ; i < num; i++)
346 if (arr[max] < arr[i])
352 static uint64_t find_min(uint64_t arr[], int num)
356 for (i = 1 ; i < num; i++)
357 if (arr[min] > arr[i])
363 static uint32_t get_win_cnt(uint64_t size)
365 uint32_t win_cnt = DEFAULT_NUM_SUBWINDOWS;
367 while (win_cnt && (size/win_cnt) < PAMU_PAGE_SIZE)
373 int config_pamu(struct pamu_addr_tbl *tbl, int num_entries, uint32_t liodn)
377 uint32_t num_sec_windows = 0;
378 uint32_t num_windows = 0;
379 uint64_t min_addr, max_addr;
381 uint64_t subwin_size;
384 min_addr = find_min(tbl->start_addr, num_entries);
385 max_addr = find_max(tbl->end_addr, num_entries);
386 size = max_addr - min_addr + 1;
391 sizebit = __ilog2_roundup_64(size);
393 debug("min start_addr is %llx\n", min_addr);
394 debug("max end_addr is %llx\n", max_addr);
395 debug("size found is %llx\n", size);
397 if (size < PAMU_PAGE_SIZE)
398 size = PAMU_PAGE_SIZE;
401 min_addr = min_addr & ~(size - 1);
402 if (min_addr + size > max_addr)
408 debug("PAACT :Base addr is %llx\n", min_addr);
409 debug("PAACT : Size is %llx\n", size);
410 num_windows = get_win_cnt(size);
411 /* For a single window, no spaact entries are required
412 * sec_sub_window count = 0 */
414 num_sec_windows = num_windows;
418 ret = pamu_config_ppaace(liodn, min_addr,
419 size , -1, -1, -1, num_sec_windows);
424 debug("configured ppace\n");
426 if (num_sec_windows) {
427 subwin_size = size >> count_lsb_zeroes(num_sec_windows);
428 debug("subwin_size is %llx\n", subwin_size);
430 for (i = 0; i < num_entries; i++) {
431 ret = pamu_config_spaace(liodn,
432 subwin_size, tbl->start_addr[i] - min_addr,
433 tbl->size[i], -1, -1, -1);