blob: 742fa0e8c45b88d4e84dde43a420a5847a2aa95e [file] [log] [blame]
Thomas Gleixnerd94d71c2019-05-29 07:12:40 -07001// SPDX-License-Identifier: GPL-2.0-only
Varun Sethi695093e2013-07-15 10:20:57 +05302/*
Varun Sethi695093e2013-07-15 10:20:57 +05303 *
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
Varun Sethi695093e2013-07-15 10:20:57 +05305 */
6
7#define pr_fmt(fmt) "fsl-pamu: %s: " fmt, __func__
8
Varun Sethi695093e2013-07-15 10:20:57 +05309#include "fsl_pamu.h"
10
Scott Wood94848652015-09-19 23:29:53 -050011#include <linux/fsl/guts.h>
Emil Medvecd70d462015-01-28 08:34:33 -060012#include <linux/interrupt.h>
13#include <linux/genalloc.h>
14
15#include <asm/mpc85xx.h>
Emil Medvecd70d462015-01-28 08:34:33 -060016
Varun Sethi695093e2013-07-15 10:20:57 +053017/* define indexes for each operation mapping scenario */
18#define OMI_QMAN 0x00
19#define OMI_FMAN 0x01
20#define OMI_QMAN_PRIV 0x02
21#define OMI_CAAM 0x03
22
23#define make64(high, low) (((u64)(high) << 32) | (low))
24
25struct pamu_isr_data {
Emil Medvecd70d462015-01-28 08:34:33 -060026 void __iomem *pamu_reg_base; /* Base address of PAMU regs */
Varun Sethi695093e2013-07-15 10:20:57 +053027 unsigned int count; /* The number of PAMUs */
28};
29
30static struct paace *ppaact;
31static struct paace *spaact;
Varun Sethi695093e2013-07-15 10:20:57 +053032
Joerg Roedel07eb6fd2017-08-09 16:15:43 +020033static bool probed; /* Has PAMU been probed? */
34
Varun Sethi695093e2013-07-15 10:20:57 +053035/*
36 * Table for matching compatible strings, for device tree
37 * guts node, for QorIQ SOCs.
38 * "fsl,qoriq-device-config-2.0" corresponds to T4 & B4
39 * SOCs. For the older SOCs "fsl,qoriq-device-config-1.0"
40 * string would be used.
Emil Medvecd70d462015-01-28 08:34:33 -060041 */
Emil Medve57fb9072015-03-25 00:28:48 -050042static const struct of_device_id guts_device_ids[] = {
Varun Sethi695093e2013-07-15 10:20:57 +053043 { .compatible = "fsl,qoriq-device-config-1.0", },
44 { .compatible = "fsl,qoriq-device-config-2.0", },
45 {}
46};
47
Varun Sethi695093e2013-07-15 10:20:57 +053048/*
49 * Table for matching compatible strings, for device tree
50 * L3 cache controller node.
51 * "fsl,t4240-l3-cache-controller" corresponds to T4,
52 * "fsl,b4860-l3-cache-controller" corresponds to B4 &
53 * "fsl,p4080-l3-cache-controller" corresponds to other,
54 * SOCs.
Emil Medvecd70d462015-01-28 08:34:33 -060055 */
Varun Sethi695093e2013-07-15 10:20:57 +053056static const struct of_device_id l3_device_ids[] = {
57 { .compatible = "fsl,t4240-l3-cache-controller", },
58 { .compatible = "fsl,b4860-l3-cache-controller", },
59 { .compatible = "fsl,p4080-l3-cache-controller", },
60 {}
61};
62
63/* maximum subwindows permitted per liodn */
64static u32 max_subwindow_count;
65
Varun Sethi695093e2013-07-15 10:20:57 +053066/**
67 * pamu_get_ppaace() - Return the primary PACCE
68 * @liodn: liodn PAACT index for desired PAACE
69 *
70 * Returns the ppace pointer upon success else return
71 * null.
72 */
73static struct paace *pamu_get_ppaace(int liodn)
74{
75 if (!ppaact || liodn >= PAACE_NUMBER_ENTRIES) {
76 pr_debug("PPAACT doesn't exist\n");
77 return NULL;
78 }
79
80 return &ppaact[liodn];
81}
82
83/**
84 * pamu_enable_liodn() - Set valid bit of PACCE
85 * @liodn: liodn PAACT index for desired PAACE
86 *
87 * Returns 0 upon success else error code < 0 returned
88 */
89int pamu_enable_liodn(int liodn)
90{
91 struct paace *ppaace;
92
93 ppaace = pamu_get_ppaace(liodn);
94 if (!ppaace) {
95 pr_debug("Invalid primary paace entry\n");
96 return -ENOENT;
97 }
98
99 if (!get_bf(ppaace->addr_bitfields, PPAACE_AF_WSE)) {
100 pr_debug("liodn %d not configured\n", liodn);
101 return -EINVAL;
102 }
103
104 /* Ensure that all other stores to the ppaace complete first */
105 mb();
106
107 set_bf(ppaace->addr_bitfields, PAACE_AF_V, PAACE_V_VALID);
108 mb();
109
110 return 0;
111}
112
113/**
114 * pamu_disable_liodn() - Clears valid bit of PACCE
115 * @liodn: liodn PAACT index for desired PAACE
116 *
117 * Returns 0 upon success else error code < 0 returned
118 */
119int pamu_disable_liodn(int liodn)
120{
121 struct paace *ppaace;
122
123 ppaace = pamu_get_ppaace(liodn);
124 if (!ppaace) {
125 pr_debug("Invalid primary paace entry\n");
126 return -ENOENT;
127 }
128
129 set_bf(ppaace->addr_bitfields, PAACE_AF_V, PAACE_V_INVALID);
130 mb();
131
132 return 0;
133}
134
135/* Derive the window size encoding for a particular PAACE entry */
136static unsigned int map_addrspace_size_to_wse(phys_addr_t addrspace_size)
137{
138 /* Bug if not a power of 2 */
Emil Medvecd70d462015-01-28 08:34:33 -0600139 BUG_ON(addrspace_size & (addrspace_size - 1));
Varun Sethi695093e2013-07-15 10:20:57 +0530140
141 /* window size is 2^(WSE+1) bytes */
Varun Sethid033f482014-06-24 19:27:15 +0530142 return fls64(addrspace_size) - 2;
Varun Sethi695093e2013-07-15 10:20:57 +0530143}
144
Varun Sethi695093e2013-07-15 10:20:57 +0530145/*
146 * Set the PAACE type as primary and set the coherency required domain
147 * attribute
148 */
149static void pamu_init_ppaace(struct paace *ppaace)
150{
151 set_bf(ppaace->addr_bitfields, PAACE_AF_PT, PAACE_PT_PRIMARY);
152
153 set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
154 PAACE_M_COHERENCE_REQ);
155}
156
157/*
Varun Sethi695093e2013-07-15 10:20:57 +0530158 * Function used for updating stash destination for the coressponding
159 * LIODN.
160 */
Christoph Hellwigba58d122021-04-01 17:52:41 +0200161int pamu_update_paace_stash(int liodn, u32 value)
Varun Sethi695093e2013-07-15 10:20:57 +0530162{
163 struct paace *paace;
164
165 paace = pamu_get_ppaace(liodn);
166 if (!paace) {
167 pr_debug("Invalid liodn entry\n");
168 return -ENOENT;
169 }
Varun Sethi695093e2013-07-15 10:20:57 +0530170 set_bf(paace->impl_attr, PAACE_IA_CID, value);
171
172 mb();
173
174 return 0;
175}
176
Varun Sethi695093e2013-07-15 10:20:57 +0530177/**
178 * pamu_config_paace() - Sets up PPAACE entry for specified liodn
179 *
180 * @liodn: Logical IO device number
181 * @win_addr: starting address of DSA window
182 * @win-size: size of DSA window
183 * @omi: Operation mapping index -- if ~omi == 0 then omi not defined
Varun Sethi695093e2013-07-15 10:20:57 +0530184 * @stashid: cache stash id for associated cpu -- if ~stashid == 0 then
185 * stashid not defined
Varun Sethi695093e2013-07-15 10:20:57 +0530186 * @prot: window permissions
187 *
188 * Returns 0 upon success else error code < 0 returned
189 */
190int pamu_config_ppaace(int liodn, phys_addr_t win_addr, phys_addr_t win_size,
Christoph Hellwig151f9412021-04-01 17:52:48 +0200191 u32 omi, u32 stashid, int prot)
Varun Sethi695093e2013-07-15 10:20:57 +0530192{
193 struct paace *ppaace;
Varun Sethi695093e2013-07-15 10:20:57 +0530194
Varun Sethid033f482014-06-24 19:27:15 +0530195 if ((win_size & (win_size - 1)) || win_size < PAMU_PAGE_SIZE) {
Emil Medvecd70d462015-01-28 08:34:33 -0600196 pr_debug("window size too small or not a power of two %pa\n",
197 &win_size);
Varun Sethi695093e2013-07-15 10:20:57 +0530198 return -EINVAL;
199 }
200
201 if (win_addr & (win_size - 1)) {
202 pr_debug("window address is not aligned with window size\n");
203 return -EINVAL;
204 }
205
206 ppaace = pamu_get_ppaace(liodn);
Emil Medvecd70d462015-01-28 08:34:33 -0600207 if (!ppaace)
Varun Sethi695093e2013-07-15 10:20:57 +0530208 return -ENOENT;
Varun Sethi695093e2013-07-15 10:20:57 +0530209
210 /* window size is 2^(WSE+1) bytes */
211 set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE,
Emil Medvecd70d462015-01-28 08:34:33 -0600212 map_addrspace_size_to_wse(win_size));
Varun Sethi695093e2013-07-15 10:20:57 +0530213
214 pamu_init_ppaace(ppaace);
215
216 ppaace->wbah = win_addr >> (PAMU_PAGE_SHIFT + 20);
217 set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL,
218 (win_addr >> PAMU_PAGE_SHIFT));
219
220 /* set up operation mapping if it's configured */
221 if (omi < OME_NUMBER_ENTRIES) {
222 set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
223 ppaace->op_encode.index_ot.omi = omi;
224 } else if (~omi != 0) {
225 pr_debug("bad operation mapping index: %d\n", omi);
226 return -EINVAL;
227 }
228
229 /* configure stash id */
230 if (~stashid != 0)
231 set_bf(ppaace->impl_attr, PAACE_IA_CID, stashid);
232
Christoph Hellwigba58d122021-04-01 17:52:41 +0200233 set_bf(ppaace->impl_attr, PAACE_IA_ATM, PAACE_ATM_WINDOW_XLATE);
Christoph Hellwig151f9412021-04-01 17:52:48 +0200234 ppaace->twbah = 0;
235 set_bf(ppaace->win_bitfields, PAACE_WIN_TWBAL, 0);
Christoph Hellwigba58d122021-04-01 17:52:41 +0200236 set_bf(ppaace->addr_bitfields, PAACE_AF_AP, prot);
237 set_bf(ppaace->impl_attr, PAACE_IA_WCE, 0);
238 set_bf(ppaace->addr_bitfields, PPAACE_AF_MW, 0);
Varun Sethi695093e2013-07-15 10:20:57 +0530239 mb();
240
241 return 0;
242}
243
244/**
Emil Medvecd70d462015-01-28 08:34:33 -0600245 * get_ome_index() - Returns the index in the operation mapping table
246 * for device.
247 * @*omi_index: pointer for storing the index value
248 *
249 */
Varun Sethi695093e2013-07-15 10:20:57 +0530250void get_ome_index(u32 *omi_index, struct device *dev)
251{
252 if (of_device_is_compatible(dev->of_node, "fsl,qman-portal"))
253 *omi_index = OMI_QMAN;
254 if (of_device_is_compatible(dev->of_node, "fsl,qman"))
255 *omi_index = OMI_QMAN_PRIV;
256}
257
258/**
259 * get_stash_id - Returns stash destination id corresponding to a
260 * cache type and vcpu.
261 * @stash_dest_hint: L1, L2 or L3
262 * @vcpu: vpcu target for a particular cache type.
263 *
264 * Returs stash on success or ~(u32)0 on failure.
265 *
266 */
267u32 get_stash_id(u32 stash_dest_hint, u32 vcpu)
268{
269 const u32 *prop;
270 struct device_node *node;
271 u32 cache_level;
272 int len, found = 0;
273 int i;
274
275 /* Fastpath, exit early if L3/CPC cache is target for stashing */
276 if (stash_dest_hint == PAMU_ATTR_CACHE_L3) {
277 node = of_find_matching_node(NULL, l3_device_ids);
278 if (node) {
Emil Medvecd70d462015-01-28 08:34:33 -0600279 prop = of_get_property(node, "cache-stash-id", NULL);
Varun Sethi695093e2013-07-15 10:20:57 +0530280 if (!prop) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -0500281 pr_debug("missing cache-stash-id at %pOF\n",
282 node);
Varun Sethi695093e2013-07-15 10:20:57 +0530283 of_node_put(node);
284 return ~(u32)0;
285 }
286 of_node_put(node);
287 return be32_to_cpup(prop);
288 }
289 return ~(u32)0;
290 }
291
Rob Herringa9a455e2018-08-27 09:46:27 -0500292 for_each_of_cpu_node(node) {
Varun Sethi695093e2013-07-15 10:20:57 +0530293 prop = of_get_property(node, "reg", &len);
294 for (i = 0; i < len / sizeof(u32); i++) {
295 if (be32_to_cpup(&prop[i]) == vcpu) {
296 found = 1;
297 goto found_cpu_node;
298 }
299 }
300 }
301found_cpu_node:
302
303 /* find the hwnode that represents the cache */
304 for (cache_level = PAMU_ATTR_CACHE_L1; (cache_level < PAMU_ATTR_CACHE_L3) && found; cache_level++) {
305 if (stash_dest_hint == cache_level) {
Emil Medvecd70d462015-01-28 08:34:33 -0600306 prop = of_get_property(node, "cache-stash-id", NULL);
Varun Sethi695093e2013-07-15 10:20:57 +0530307 if (!prop) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -0500308 pr_debug("missing cache-stash-id at %pOF\n",
309 node);
Varun Sethi695093e2013-07-15 10:20:57 +0530310 of_node_put(node);
311 return ~(u32)0;
312 }
313 of_node_put(node);
314 return be32_to_cpup(prop);
315 }
316
Emil Medvecd70d462015-01-28 08:34:33 -0600317 prop = of_get_property(node, "next-level-cache", NULL);
Varun Sethi695093e2013-07-15 10:20:57 +0530318 if (!prop) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -0500319 pr_debug("can't find next-level-cache at %pOF\n", node);
Varun Sethi695093e2013-07-15 10:20:57 +0530320 of_node_put(node);
321 return ~(u32)0; /* can't traverse any further */
322 }
323 of_node_put(node);
324
325 /* advance to next node in cache hierarchy */
326 node = of_find_node_by_phandle(*prop);
327 if (!node) {
Rickard Strandqvistd6a71bf2014-05-17 19:16:44 +0200328 pr_debug("Invalid node for cache hierarchy\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530329 return ~(u32)0;
330 }
331 }
332
333 pr_debug("stash dest not found for %d on vcpu %d\n",
Emil Medvecd70d462015-01-28 08:34:33 -0600334 stash_dest_hint, vcpu);
Varun Sethi695093e2013-07-15 10:20:57 +0530335 return ~(u32)0;
336}
337
338/* Identify if the PAACT table entry belongs to QMAN, BMAN or QMAN Portal */
339#define QMAN_PAACE 1
340#define QMAN_PORTAL_PAACE 2
341#define BMAN_PAACE 3
342
343/**
344 * Setup operation mapping and stash destinations for QMAN and QMAN portal.
345 * Memory accesses to QMAN and BMAN private memory need not be coherent, so
346 * clear the PAACE entry coherency attribute for them.
347 */
Emil Medve57fb9072015-03-25 00:28:48 -0500348static void setup_qbman_paace(struct paace *ppaace, int paace_type)
Varun Sethi695093e2013-07-15 10:20:57 +0530349{
350 switch (paace_type) {
351 case QMAN_PAACE:
352 set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
353 ppaace->op_encode.index_ot.omi = OMI_QMAN_PRIV;
354 /* setup QMAN Private data stashing for the L3 cache */
355 set_bf(ppaace->impl_attr, PAACE_IA_CID, get_stash_id(PAMU_ATTR_CACHE_L3, 0));
356 set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
357 0);
358 break;
359 case QMAN_PORTAL_PAACE:
360 set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED);
361 ppaace->op_encode.index_ot.omi = OMI_QMAN;
Emil Medvecd70d462015-01-28 08:34:33 -0600362 /* Set DQRR and Frame stashing for the L3 cache */
Varun Sethi695093e2013-07-15 10:20:57 +0530363 set_bf(ppaace->impl_attr, PAACE_IA_CID, get_stash_id(PAMU_ATTR_CACHE_L3, 0));
364 break;
365 case BMAN_PAACE:
366 set_bf(ppaace->domain_attr.to_host.coherency_required, PAACE_DA_HOST_CR,
367 0);
368 break;
369 }
370}
371
372/**
373 * Setup the operation mapping table for various devices. This is a static
374 * table where each table index corresponds to a particular device. PAMU uses
375 * this table to translate device transaction to appropriate corenet
376 * transaction.
377 */
Emil Medve57fb9072015-03-25 00:28:48 -0500378static void setup_omt(struct ome *omt)
Varun Sethi695093e2013-07-15 10:20:57 +0530379{
380 struct ome *ome;
381
382 /* Configure OMI_QMAN */
383 ome = &omt[OMI_QMAN];
384
385 ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READ;
386 ome->moe[IOE_EREAD0_IDX] = EOE_VALID | EOE_RSA;
387 ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
388 ome->moe[IOE_EWRITE0_IDX] = EOE_VALID | EOE_WWSAO;
389
390 ome->moe[IOE_DIRECT0_IDX] = EOE_VALID | EOE_LDEC;
391 ome->moe[IOE_DIRECT1_IDX] = EOE_VALID | EOE_LDECPE;
392
393 /* Configure OMI_FMAN */
394 ome = &omt[OMI_FMAN];
395 ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READI;
396 ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
397
398 /* Configure OMI_QMAN private */
399 ome = &omt[OMI_QMAN_PRIV];
400 ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READ;
401 ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
402 ome->moe[IOE_EREAD0_IDX] = EOE_VALID | EOE_RSA;
403 ome->moe[IOE_EWRITE0_IDX] = EOE_VALID | EOE_WWSA;
404
405 /* Configure OMI_CAAM */
406 ome = &omt[OMI_CAAM];
407 ome->moe[IOE_READ_IDX] = EOE_VALID | EOE_READI;
408 ome->moe[IOE_WRITE_IDX] = EOE_VALID | EOE_WRITE;
409}
410
411/*
412 * Get the maximum number of PAACT table entries
413 * and subwindows supported by PAMU
414 */
Emil Medve57fb9072015-03-25 00:28:48 -0500415static void get_pamu_cap_values(unsigned long pamu_reg_base)
Varun Sethi695093e2013-07-15 10:20:57 +0530416{
417 u32 pc_val;
418
419 pc_val = in_be32((u32 *)(pamu_reg_base + PAMU_PC3));
420 /* Maximum number of subwindows per liodn */
421 max_subwindow_count = 1 << (1 + PAMU_PC3_MWCE(pc_val));
422}
423
424/* Setup PAMU registers pointing to PAACT, SPAACT and OMT */
Emil Medve57fb9072015-03-25 00:28:48 -0500425static int setup_one_pamu(unsigned long pamu_reg_base, unsigned long pamu_reg_size,
426 phys_addr_t ppaact_phys, phys_addr_t spaact_phys,
427 phys_addr_t omt_phys)
Varun Sethi695093e2013-07-15 10:20:57 +0530428{
429 u32 *pc;
430 struct pamu_mmap_regs *pamu_regs;
431
432 pc = (u32 *) (pamu_reg_base + PAMU_PC);
433 pamu_regs = (struct pamu_mmap_regs *)
434 (pamu_reg_base + PAMU_MMAP_REGS_BASE);
435
436 /* set up pointers to corenet control blocks */
437
438 out_be32(&pamu_regs->ppbah, upper_32_bits(ppaact_phys));
439 out_be32(&pamu_regs->ppbal, lower_32_bits(ppaact_phys));
440 ppaact_phys = ppaact_phys + PAACT_SIZE;
441 out_be32(&pamu_regs->pplah, upper_32_bits(ppaact_phys));
442 out_be32(&pamu_regs->pplal, lower_32_bits(ppaact_phys));
443
444 out_be32(&pamu_regs->spbah, upper_32_bits(spaact_phys));
445 out_be32(&pamu_regs->spbal, lower_32_bits(spaact_phys));
446 spaact_phys = spaact_phys + SPAACT_SIZE;
447 out_be32(&pamu_regs->splah, upper_32_bits(spaact_phys));
448 out_be32(&pamu_regs->splal, lower_32_bits(spaact_phys));
449
450 out_be32(&pamu_regs->obah, upper_32_bits(omt_phys));
451 out_be32(&pamu_regs->obal, lower_32_bits(omt_phys));
452 omt_phys = omt_phys + OMT_SIZE;
453 out_be32(&pamu_regs->olah, upper_32_bits(omt_phys));
454 out_be32(&pamu_regs->olal, lower_32_bits(omt_phys));
455
456 /*
457 * set PAMU enable bit,
458 * allow ppaact & omt to be cached
459 * & enable PAMU access violation interrupts.
460 */
461
462 out_be32((u32 *)(pamu_reg_base + PAMU_PICS),
Emil Medvecd70d462015-01-28 08:34:33 -0600463 PAMU_ACCESS_VIOLATION_ENABLE);
Varun Sethi695093e2013-07-15 10:20:57 +0530464 out_be32(pc, PAMU_PC_PE | PAMU_PC_OCE | PAMU_PC_SPCC | PAMU_PC_PPCC);
465 return 0;
466}
467
468/* Enable all device LIODNS */
Emil Medve57fb9072015-03-25 00:28:48 -0500469static void setup_liodns(void)
Varun Sethi695093e2013-07-15 10:20:57 +0530470{
471 int i, len;
472 struct paace *ppaace;
473 struct device_node *node = NULL;
474 const u32 *prop;
475
476 for_each_node_with_property(node, "fsl,liodn") {
477 prop = of_get_property(node, "fsl,liodn", &len);
478 for (i = 0; i < len / sizeof(u32); i++) {
479 int liodn;
480
481 liodn = be32_to_cpup(&prop[i]);
482 if (liodn >= PAACE_NUMBER_ENTRIES) {
483 pr_debug("Invalid LIODN value %d\n", liodn);
484 continue;
485 }
486 ppaace = pamu_get_ppaace(liodn);
487 pamu_init_ppaace(ppaace);
488 /* window size is 2^(WSE+1) bytes */
489 set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE, 35);
490 ppaace->wbah = 0;
491 set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL, 0);
492 set_bf(ppaace->impl_attr, PAACE_IA_ATM,
Emil Medvecd70d462015-01-28 08:34:33 -0600493 PAACE_ATM_NO_XLATE);
Varun Sethi695093e2013-07-15 10:20:57 +0530494 set_bf(ppaace->addr_bitfields, PAACE_AF_AP,
Emil Medvecd70d462015-01-28 08:34:33 -0600495 PAACE_AP_PERMS_ALL);
Varun Sethi695093e2013-07-15 10:20:57 +0530496 if (of_device_is_compatible(node, "fsl,qman-portal"))
497 setup_qbman_paace(ppaace, QMAN_PORTAL_PAACE);
498 if (of_device_is_compatible(node, "fsl,qman"))
499 setup_qbman_paace(ppaace, QMAN_PAACE);
500 if (of_device_is_compatible(node, "fsl,bman"))
501 setup_qbman_paace(ppaace, BMAN_PAACE);
502 mb();
503 pamu_enable_liodn(liodn);
504 }
505 }
506}
507
Emil Medvecd70d462015-01-28 08:34:33 -0600508static irqreturn_t pamu_av_isr(int irq, void *arg)
Varun Sethi695093e2013-07-15 10:20:57 +0530509{
510 struct pamu_isr_data *data = arg;
511 phys_addr_t phys;
512 unsigned int i, j, ret;
513
Joerg Roedel634544b2013-08-14 11:44:30 +0200514 pr_emerg("access violation interrupt\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530515
516 for (i = 0; i < data->count; i++) {
517 void __iomem *p = data->pamu_reg_base + i * PAMU_OFFSET;
518 u32 pics = in_be32(p + PAMU_PICS);
519
520 if (pics & PAMU_ACCESS_VIOLATION_STAT) {
521 u32 avs1 = in_be32(p + PAMU_AVS1);
522 struct paace *paace;
523
524 pr_emerg("POES1=%08x\n", in_be32(p + PAMU_POES1));
525 pr_emerg("POES2=%08x\n", in_be32(p + PAMU_POES2));
526 pr_emerg("AVS1=%08x\n", avs1);
527 pr_emerg("AVS2=%08x\n", in_be32(p + PAMU_AVS2));
Emil Medvecd70d462015-01-28 08:34:33 -0600528 pr_emerg("AVA=%016llx\n",
529 make64(in_be32(p + PAMU_AVAH),
530 in_be32(p + PAMU_AVAL)));
Varun Sethi695093e2013-07-15 10:20:57 +0530531 pr_emerg("UDAD=%08x\n", in_be32(p + PAMU_UDAD));
Emil Medvecd70d462015-01-28 08:34:33 -0600532 pr_emerg("POEA=%016llx\n",
533 make64(in_be32(p + PAMU_POEAH),
534 in_be32(p + PAMU_POEAL)));
Varun Sethi695093e2013-07-15 10:20:57 +0530535
536 phys = make64(in_be32(p + PAMU_POEAH),
Emil Medvecd70d462015-01-28 08:34:33 -0600537 in_be32(p + PAMU_POEAL));
Varun Sethi695093e2013-07-15 10:20:57 +0530538
539 /* Assume that POEA points to a PAACE */
540 if (phys) {
541 u32 *paace = phys_to_virt(phys);
542
543 /* Only the first four words are relevant */
544 for (j = 0; j < 4; j++)
Emil Medvecd70d462015-01-28 08:34:33 -0600545 pr_emerg("PAACE[%u]=%08x\n",
546 j, in_be32(paace + j));
Varun Sethi695093e2013-07-15 10:20:57 +0530547 }
548
549 /* clear access violation condition */
Emil Medvecd70d462015-01-28 08:34:33 -0600550 out_be32(p + PAMU_AVS1, avs1 & PAMU_AV_MASK);
Varun Sethi695093e2013-07-15 10:20:57 +0530551 paace = pamu_get_ppaace(avs1 >> PAMU_AVS1_LIODN_SHIFT);
552 BUG_ON(!paace);
553 /* check if we got a violation for a disabled LIODN */
554 if (!get_bf(paace->addr_bitfields, PAACE_AF_V)) {
555 /*
556 * As per hardware erratum A-003638, access
557 * violation can be reported for a disabled
558 * LIODN. If we hit that condition, disable
559 * access violation reporting.
560 */
561 pics &= ~PAMU_ACCESS_VIOLATION_ENABLE;
562 } else {
563 /* Disable the LIODN */
564 ret = pamu_disable_liodn(avs1 >> PAMU_AVS1_LIODN_SHIFT);
565 BUG_ON(ret);
Emil Medvecd70d462015-01-28 08:34:33 -0600566 pr_emerg("Disabling liodn %x\n",
567 avs1 >> PAMU_AVS1_LIODN_SHIFT);
Varun Sethi695093e2013-07-15 10:20:57 +0530568 }
569 out_be32((p + PAMU_PICS), pics);
570 }
571 }
572
Varun Sethi695093e2013-07-15 10:20:57 +0530573 return IRQ_HANDLED;
574}
575
576#define LAWAR_EN 0x80000000
577#define LAWAR_TARGET_MASK 0x0FF00000
578#define LAWAR_TARGET_SHIFT 20
579#define LAWAR_SIZE_MASK 0x0000003F
580#define LAWAR_CSDID_MASK 0x000FF000
581#define LAWAR_CSDID_SHIFT 12
582
583#define LAW_SIZE_4K 0xb
584
585struct ccsr_law {
586 u32 lawbarh; /* LAWn base address high */
587 u32 lawbarl; /* LAWn base address low */
588 u32 lawar; /* LAWn attributes */
589 u32 reserved;
590};
591
592/*
593 * Create a coherence subdomain for a given memory block.
594 */
Emil Medve57fb9072015-03-25 00:28:48 -0500595static int create_csd(phys_addr_t phys, size_t size, u32 csd_port_id)
Varun Sethi695093e2013-07-15 10:20:57 +0530596{
597 struct device_node *np;
598 const __be32 *iprop;
599 void __iomem *lac = NULL; /* Local Access Control registers */
600 struct ccsr_law __iomem *law;
601 void __iomem *ccm = NULL;
602 u32 __iomem *csdids;
603 unsigned int i, num_laws, num_csds;
604 u32 law_target = 0;
605 u32 csd_id = 0;
606 int ret = 0;
607
608 np = of_find_compatible_node(NULL, NULL, "fsl,corenet-law");
609 if (!np)
610 return -ENODEV;
611
612 iprop = of_get_property(np, "fsl,num-laws", NULL);
613 if (!iprop) {
614 ret = -ENODEV;
615 goto error;
616 }
617
618 num_laws = be32_to_cpup(iprop);
619 if (!num_laws) {
620 ret = -ENODEV;
621 goto error;
622 }
623
624 lac = of_iomap(np, 0);
625 if (!lac) {
626 ret = -ENODEV;
627 goto error;
628 }
629
630 /* LAW registers are at offset 0xC00 */
631 law = lac + 0xC00;
632
633 of_node_put(np);
634
635 np = of_find_compatible_node(NULL, NULL, "fsl,corenet-cf");
636 if (!np) {
637 ret = -ENODEV;
638 goto error;
639 }
640
641 iprop = of_get_property(np, "fsl,ccf-num-csdids", NULL);
642 if (!iprop) {
643 ret = -ENODEV;
644 goto error;
645 }
646
647 num_csds = be32_to_cpup(iprop);
648 if (!num_csds) {
649 ret = -ENODEV;
650 goto error;
651 }
652
653 ccm = of_iomap(np, 0);
654 if (!ccm) {
655 ret = -ENOMEM;
656 goto error;
657 }
658
659 /* The undocumented CSDID registers are at offset 0x600 */
660 csdids = ccm + 0x600;
661
662 of_node_put(np);
663 np = NULL;
664
665 /* Find an unused coherence subdomain ID */
666 for (csd_id = 0; csd_id < num_csds; csd_id++) {
667 if (!csdids[csd_id])
668 break;
669 }
670
671 /* Store the Port ID in the (undocumented) proper CIDMRxx register */
672 csdids[csd_id] = csd_port_id;
673
674 /* Find the DDR LAW that maps to our buffer. */
675 for (i = 0; i < num_laws; i++) {
676 if (law[i].lawar & LAWAR_EN) {
677 phys_addr_t law_start, law_end;
678
679 law_start = make64(law[i].lawbarh, law[i].lawbarl);
680 law_end = law_start +
681 (2ULL << (law[i].lawar & LAWAR_SIZE_MASK));
682
683 if (law_start <= phys && phys < law_end) {
684 law_target = law[i].lawar & LAWAR_TARGET_MASK;
685 break;
686 }
687 }
688 }
689
690 if (i == 0 || i == num_laws) {
Emil Medvecd70d462015-01-28 08:34:33 -0600691 /* This should never happen */
Varun Sethi695093e2013-07-15 10:20:57 +0530692 ret = -ENOENT;
693 goto error;
694 }
695
696 /* Find a free LAW entry */
697 while (law[--i].lawar & LAWAR_EN) {
698 if (i == 0) {
699 /* No higher priority LAW slots available */
700 ret = -ENOENT;
701 goto error;
702 }
703 }
704
705 law[i].lawbarh = upper_32_bits(phys);
706 law[i].lawbarl = lower_32_bits(phys);
707 wmb();
708 law[i].lawar = LAWAR_EN | law_target | (csd_id << LAWAR_CSDID_SHIFT) |
709 (LAW_SIZE_4K + get_order(size));
710 wmb();
711
712error:
713 if (ccm)
714 iounmap(ccm);
715
716 if (lac)
717 iounmap(lac);
718
719 if (np)
720 of_node_put(np);
721
722 return ret;
723}
724
725/*
726 * Table of SVRs and the corresponding PORT_ID values. Port ID corresponds to a
727 * bit map of snoopers for a given range of memory mapped by a LAW.
728 *
729 * All future CoreNet-enabled SOCs will have this erratum(A-004510) fixed, so this
730 * table should never need to be updated. SVRs are guaranteed to be unique, so
731 * there is no worry that a future SOC will inadvertently have one of these
732 * values.
733 */
734static const struct {
735 u32 svr;
736 u32 port_id;
Emil Medve57fb9072015-03-25 00:28:48 -0500737} port_id_map[] = {
Emil Medvecd70d462015-01-28 08:34:33 -0600738 {(SVR_P2040 << 8) | 0x10, 0xFF000000}, /* P2040 1.0 */
739 {(SVR_P2040 << 8) | 0x11, 0xFF000000}, /* P2040 1.1 */
740 {(SVR_P2041 << 8) | 0x10, 0xFF000000}, /* P2041 1.0 */
741 {(SVR_P2041 << 8) | 0x11, 0xFF000000}, /* P2041 1.1 */
742 {(SVR_P3041 << 8) | 0x10, 0xFF000000}, /* P3041 1.0 */
743 {(SVR_P3041 << 8) | 0x11, 0xFF000000}, /* P3041 1.1 */
744 {(SVR_P4040 << 8) | 0x20, 0xFFF80000}, /* P4040 2.0 */
745 {(SVR_P4080 << 8) | 0x20, 0xFFF80000}, /* P4080 2.0 */
746 {(SVR_P5010 << 8) | 0x10, 0xFC000000}, /* P5010 1.0 */
747 {(SVR_P5010 << 8) | 0x20, 0xFC000000}, /* P5010 2.0 */
748 {(SVR_P5020 << 8) | 0x10, 0xFC000000}, /* P5020 1.0 */
749 {(SVR_P5021 << 8) | 0x10, 0xFF800000}, /* P5021 1.0 */
750 {(SVR_P5040 << 8) | 0x10, 0xFF800000}, /* P5040 1.0 */
Varun Sethi695093e2013-07-15 10:20:57 +0530751};
752
753#define SVR_SECURITY 0x80000 /* The Security (E) bit */
754
Emil Medve57fb9072015-03-25 00:28:48 -0500755static int fsl_pamu_probe(struct platform_device *pdev)
Varun Sethi695093e2013-07-15 10:20:57 +0530756{
Emil Medvecd70d462015-01-28 08:34:33 -0600757 struct device *dev = &pdev->dev;
Varun Sethi695093e2013-07-15 10:20:57 +0530758 void __iomem *pamu_regs = NULL;
759 struct ccsr_guts __iomem *guts_regs = NULL;
760 u32 pamubypenr, pamu_counter;
761 unsigned long pamu_reg_off;
762 unsigned long pamu_reg_base;
763 struct pamu_isr_data *data = NULL;
764 struct device_node *guts_node;
765 u64 size;
766 struct page *p;
767 int ret = 0;
768 int irq;
769 phys_addr_t ppaact_phys;
770 phys_addr_t spaact_phys;
Emil Medve57fb9072015-03-25 00:28:48 -0500771 struct ome *omt;
Varun Sethi695093e2013-07-15 10:20:57 +0530772 phys_addr_t omt_phys;
773 size_t mem_size = 0;
774 unsigned int order = 0;
775 u32 csd_port_id = 0;
776 unsigned i;
777 /*
778 * enumerate all PAMUs and allocate and setup PAMU tables
779 * for each of them,
780 * NOTE : All PAMUs share the same LIODN tables.
781 */
782
Joerg Roedel07eb6fd2017-08-09 16:15:43 +0200783 if (WARN_ON(probed))
784 return -EBUSY;
785
Emil Medvecd70d462015-01-28 08:34:33 -0600786 pamu_regs = of_iomap(dev->of_node, 0);
Varun Sethi695093e2013-07-15 10:20:57 +0530787 if (!pamu_regs) {
Emil Medvecd70d462015-01-28 08:34:33 -0600788 dev_err(dev, "ioremap of PAMU node failed\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530789 return -ENOMEM;
790 }
Emil Medvecd70d462015-01-28 08:34:33 -0600791 of_get_address(dev->of_node, 0, &size, NULL);
Varun Sethi695093e2013-07-15 10:20:57 +0530792
Emil Medvecd70d462015-01-28 08:34:33 -0600793 irq = irq_of_parse_and_map(dev->of_node, 0);
Varun Sethi695093e2013-07-15 10:20:57 +0530794 if (irq == NO_IRQ) {
Emil Medvecd70d462015-01-28 08:34:33 -0600795 dev_warn(dev, "no interrupts listed in PAMU node\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530796 goto error;
797 }
798
Emil Medvecd70d462015-01-28 08:34:33 -0600799 data = kzalloc(sizeof(*data), GFP_KERNEL);
Varun Sethi695093e2013-07-15 10:20:57 +0530800 if (!data) {
Varun Sethi695093e2013-07-15 10:20:57 +0530801 ret = -ENOMEM;
802 goto error;
803 }
804 data->pamu_reg_base = pamu_regs;
805 data->count = size / PAMU_OFFSET;
806
807 /* The ISR needs access to the regs, so we won't iounmap them */
808 ret = request_irq(irq, pamu_av_isr, 0, "pamu", data);
809 if (ret < 0) {
Emil Medvecd70d462015-01-28 08:34:33 -0600810 dev_err(dev, "error %i installing ISR for irq %i\n", ret, irq);
Varun Sethi695093e2013-07-15 10:20:57 +0530811 goto error;
812 }
813
814 guts_node = of_find_matching_node(NULL, guts_device_ids);
815 if (!guts_node) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -0500816 dev_err(dev, "could not find GUTS node %pOF\n", dev->of_node);
Varun Sethi695093e2013-07-15 10:20:57 +0530817 ret = -ENODEV;
818 goto error;
819 }
820
821 guts_regs = of_iomap(guts_node, 0);
822 of_node_put(guts_node);
823 if (!guts_regs) {
Emil Medvecd70d462015-01-28 08:34:33 -0600824 dev_err(dev, "ioremap of GUTS node failed\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530825 ret = -ENODEV;
826 goto error;
827 }
828
829 /* read in the PAMU capability registers */
830 get_pamu_cap_values((unsigned long)pamu_regs);
831 /*
832 * To simplify the allocation of a coherency domain, we allocate the
833 * PAACT and the OMT in the same memory buffer. Unfortunately, this
834 * wastes more memory compared to allocating the buffers separately.
835 */
836 /* Determine how much memory we need */
837 mem_size = (PAGE_SIZE << get_order(PAACT_SIZE)) +
838 (PAGE_SIZE << get_order(SPAACT_SIZE)) +
839 (PAGE_SIZE << get_order(OMT_SIZE));
840 order = get_order(mem_size);
841
842 p = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
843 if (!p) {
Emil Medvecd70d462015-01-28 08:34:33 -0600844 dev_err(dev, "unable to allocate PAACT/SPAACT/OMT block\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530845 ret = -ENOMEM;
846 goto error;
847 }
848
849 ppaact = page_address(p);
850 ppaact_phys = page_to_phys(p);
851
852 /* Make sure the memory is naturally aligned */
853 if (ppaact_phys & ((PAGE_SIZE << order) - 1)) {
Emil Medvecd70d462015-01-28 08:34:33 -0600854 dev_err(dev, "PAACT/OMT block is unaligned\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530855 ret = -ENOMEM;
856 goto error;
857 }
858
859 spaact = (void *)ppaact + (PAGE_SIZE << get_order(PAACT_SIZE));
860 omt = (void *)spaact + (PAGE_SIZE << get_order(SPAACT_SIZE));
861
Emil Medvecd70d462015-01-28 08:34:33 -0600862 dev_dbg(dev, "ppaact virt=%p phys=%pa\n", ppaact, &ppaact_phys);
Varun Sethi695093e2013-07-15 10:20:57 +0530863
864 /* Check to see if we need to implement the work-around on this SOC */
865
866 /* Determine the Port ID for our coherence subdomain */
867 for (i = 0; i < ARRAY_SIZE(port_id_map); i++) {
868 if (port_id_map[i].svr == (mfspr(SPRN_SVR) & ~SVR_SECURITY)) {
869 csd_port_id = port_id_map[i].port_id;
Emil Medvecd70d462015-01-28 08:34:33 -0600870 dev_dbg(dev, "found matching SVR %08x\n",
Varun Sethi695093e2013-07-15 10:20:57 +0530871 port_id_map[i].svr);
872 break;
873 }
874 }
875
876 if (csd_port_id) {
Emil Medvecd70d462015-01-28 08:34:33 -0600877 dev_dbg(dev, "creating coherency subdomain at address %pa, size %zu, port id 0x%08x",
878 &ppaact_phys, mem_size, csd_port_id);
Varun Sethi695093e2013-07-15 10:20:57 +0530879
880 ret = create_csd(ppaact_phys, mem_size, csd_port_id);
881 if (ret) {
Emil Medvecd70d462015-01-28 08:34:33 -0600882 dev_err(dev, "could not create coherence subdomain\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530883 return ret;
884 }
885 }
886
887 spaact_phys = virt_to_phys(spaact);
888 omt_phys = virt_to_phys(omt);
889
Varun Sethi695093e2013-07-15 10:20:57 +0530890 pamubypenr = in_be32(&guts_regs->pamubypenr);
891
892 for (pamu_reg_off = 0, pamu_counter = 0x80000000; pamu_reg_off < size;
893 pamu_reg_off += PAMU_OFFSET, pamu_counter >>= 1) {
894
Emil Medvecd70d462015-01-28 08:34:33 -0600895 pamu_reg_base = (unsigned long)pamu_regs + pamu_reg_off;
Varun Sethi695093e2013-07-15 10:20:57 +0530896 setup_one_pamu(pamu_reg_base, pamu_reg_off, ppaact_phys,
Emil Medvecd70d462015-01-28 08:34:33 -0600897 spaact_phys, omt_phys);
Varun Sethi695093e2013-07-15 10:20:57 +0530898 /* Disable PAMU bypass for this PAMU */
899 pamubypenr &= ~pamu_counter;
900 }
901
902 setup_omt(omt);
903
904 /* Enable all relevant PAMU(s) */
905 out_be32(&guts_regs->pamubypenr, pamubypenr);
906
907 iounmap(guts_regs);
908
Emil Medvecd70d462015-01-28 08:34:33 -0600909 /* Enable DMA for the LIODNs in the device tree */
Varun Sethi695093e2013-07-15 10:20:57 +0530910
911 setup_liodns();
912
Joerg Roedel07eb6fd2017-08-09 16:15:43 +0200913 probed = true;
914
Varun Sethi695093e2013-07-15 10:20:57 +0530915 return 0;
916
Varun Sethi695093e2013-07-15 10:20:57 +0530917error:
918 if (irq != NO_IRQ)
919 free_irq(irq, data);
920
Alex Dewarce433d02020-09-11 14:53:25 +0100921 kfree_sensitive(data);
Varun Sethi695093e2013-07-15 10:20:57 +0530922
923 if (pamu_regs)
924 iounmap(pamu_regs);
925
926 if (guts_regs)
927 iounmap(guts_regs);
928
929 if (ppaact)
930 free_pages((unsigned long)ppaact, order);
931
932 ppaact = NULL;
933
934 return ret;
935}
936
Emil Medve57fb9072015-03-25 00:28:48 -0500937static struct platform_driver fsl_of_pamu_driver = {
Varun Sethi695093e2013-07-15 10:20:57 +0530938 .driver = {
939 .name = "fsl-of-pamu",
Varun Sethi695093e2013-07-15 10:20:57 +0530940 },
941 .probe = fsl_pamu_probe,
942};
943
944static __init int fsl_pamu_init(void)
945{
946 struct platform_device *pdev = NULL;
947 struct device_node *np;
948 int ret;
949
950 /*
951 * The normal OF process calls the probe function at some
952 * indeterminate later time, after most drivers have loaded. This is
953 * too late for us, because PAMU clients (like the Qman driver)
954 * depend on PAMU being initialized early.
955 *
956 * So instead, we "manually" call our probe function by creating the
957 * platform devices ourselves.
958 */
959
960 /*
961 * We assume that there is only one PAMU node in the device tree. A
962 * single PAMU node represents all of the PAMU devices in the SOC
963 * already. Everything else already makes that assumption, and the
964 * binding for the PAMU nodes doesn't allow for any parent-child
965 * relationships anyway. In other words, support for more than one
966 * PAMU node would require significant changes to a lot of code.
967 */
968
969 np = of_find_compatible_node(NULL, NULL, "fsl,pamu");
970 if (!np) {
Joerg Roedel634544b2013-08-14 11:44:30 +0200971 pr_err("could not find a PAMU node\n");
Varun Sethi695093e2013-07-15 10:20:57 +0530972 return -ENODEV;
973 }
974
975 ret = platform_driver_register(&fsl_of_pamu_driver);
976 if (ret) {
Joerg Roedel634544b2013-08-14 11:44:30 +0200977 pr_err("could not register driver (err=%i)\n", ret);
Varun Sethi695093e2013-07-15 10:20:57 +0530978 goto error_driver_register;
979 }
980
981 pdev = platform_device_alloc("fsl-of-pamu", 0);
982 if (!pdev) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -0500983 pr_err("could not allocate device %pOF\n", np);
Varun Sethi695093e2013-07-15 10:20:57 +0530984 ret = -ENOMEM;
985 goto error_device_alloc;
986 }
987 pdev->dev.of_node = of_node_get(np);
988
989 ret = pamu_domain_init();
990 if (ret)
991 goto error_device_add;
992
993 ret = platform_device_add(pdev);
994 if (ret) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -0500995 pr_err("could not add device %pOF (err=%i)\n", np, ret);
Varun Sethi695093e2013-07-15 10:20:57 +0530996 goto error_device_add;
997 }
998
999 return 0;
1000
1001error_device_add:
1002 of_node_put(pdev->dev.of_node);
1003 pdev->dev.of_node = NULL;
1004
1005 platform_device_put(pdev);
1006
1007error_device_alloc:
1008 platform_driver_unregister(&fsl_of_pamu_driver);
1009
1010error_driver_register:
1011 of_node_put(np);
1012
1013 return ret;
1014}
1015arch_initcall(fsl_pamu_init);