1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014-2021 Broadcom
6 #include <linux/init.h>
7 #include <linux/types.h>
8 #include <linux/module.h>
9 #include <linux/panic_notifier.h>
10 #include <linux/platform_device.h>
11 #include <linux/interrupt.h>
12 #include <linux/sysfs.h>
14 #include <linux/string.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
18 #include <linux/bitops.h>
20 #include <linux/kernel.h>
21 #include <linux/kdebug.h>
22 #include <linux/notifier.h>
25 #include <asm/traps.h>
28 #define ARB_ERR_CAP_CLEAR (1 << 0)
29 #define ARB_ERR_CAP_STATUS_TIMEOUT (1 << 12)
30 #define ARB_ERR_CAP_STATUS_TEA (1 << 11)
31 #define ARB_ERR_CAP_STATUS_WRITE (1 << 1)
32 #define ARB_ERR_CAP_STATUS_VALID (1 << 0)
34 #define ARB_BP_CAP_CLEAR (1 << 0)
35 #define ARB_BP_CAP_STATUS_PROT_SHIFT 14
36 #define ARB_BP_CAP_STATUS_TYPE (1 << 13)
37 #define ARB_BP_CAP_STATUS_RSP_SHIFT 10
38 #define ARB_BP_CAP_STATUS_MASK GENMASK(1, 0)
39 #define ARB_BP_CAP_STATUS_BS_SHIFT 2
40 #define ARB_BP_CAP_STATUS_WRITE (1 << 1)
41 #define ARB_BP_CAP_STATUS_VALID (1 << 0)
57 static const int gisb_offsets_bcm7038[] = {
59 [ARB_BP_CAP_CLR] = 0x014,
60 [ARB_BP_CAP_HI_ADDR] = -1,
61 [ARB_BP_CAP_ADDR] = 0x0b8,
62 [ARB_BP_CAP_STATUS] = 0x0c0,
63 [ARB_BP_CAP_MASTER] = -1,
64 [ARB_ERR_CAP_CLR] = 0x0c4,
65 [ARB_ERR_CAP_HI_ADDR] = -1,
66 [ARB_ERR_CAP_ADDR] = 0x0c8,
67 [ARB_ERR_CAP_STATUS] = 0x0d0,
68 [ARB_ERR_CAP_MASTER] = -1,
71 static const int gisb_offsets_bcm7278[] = {
73 [ARB_BP_CAP_CLR] = 0x01c,
74 [ARB_BP_CAP_HI_ADDR] = -1,
75 [ARB_BP_CAP_ADDR] = 0x220,
76 [ARB_BP_CAP_STATUS] = 0x230,
77 [ARB_BP_CAP_MASTER] = 0x234,
78 [ARB_ERR_CAP_CLR] = 0x7f8,
79 [ARB_ERR_CAP_HI_ADDR] = -1,
80 [ARB_ERR_CAP_ADDR] = 0x7e0,
81 [ARB_ERR_CAP_STATUS] = 0x7f0,
82 [ARB_ERR_CAP_MASTER] = 0x7f4,
85 static const int gisb_offsets_bcm7400[] = {
87 [ARB_BP_CAP_CLR] = 0x014,
88 [ARB_BP_CAP_HI_ADDR] = -1,
89 [ARB_BP_CAP_ADDR] = 0x0b8,
90 [ARB_BP_CAP_STATUS] = 0x0c0,
91 [ARB_BP_CAP_MASTER] = 0x0c4,
92 [ARB_ERR_CAP_CLR] = 0x0c8,
93 [ARB_ERR_CAP_HI_ADDR] = -1,
94 [ARB_ERR_CAP_ADDR] = 0x0cc,
95 [ARB_ERR_CAP_STATUS] = 0x0d4,
96 [ARB_ERR_CAP_MASTER] = 0x0d8,
99 static const int gisb_offsets_bcm7435[] = {
101 [ARB_BP_CAP_CLR] = 0x014,
102 [ARB_BP_CAP_HI_ADDR] = -1,
103 [ARB_BP_CAP_ADDR] = 0x158,
104 [ARB_BP_CAP_STATUS] = 0x160,
105 [ARB_BP_CAP_MASTER] = 0x164,
106 [ARB_ERR_CAP_CLR] = 0x168,
107 [ARB_ERR_CAP_HI_ADDR] = -1,
108 [ARB_ERR_CAP_ADDR] = 0x16c,
109 [ARB_ERR_CAP_STATUS] = 0x174,
110 [ARB_ERR_CAP_MASTER] = 0x178,
113 static const int gisb_offsets_bcm7445[] = {
115 [ARB_BP_CAP_CLR] = 0x010,
116 [ARB_BP_CAP_HI_ADDR] = -1,
117 [ARB_BP_CAP_ADDR] = 0x1d8,
118 [ARB_BP_CAP_STATUS] = 0x1e0,
119 [ARB_BP_CAP_MASTER] = 0x1e4,
120 [ARB_ERR_CAP_CLR] = 0x7e4,
121 [ARB_ERR_CAP_HI_ADDR] = 0x7e8,
122 [ARB_ERR_CAP_ADDR] = 0x7ec,
123 [ARB_ERR_CAP_STATUS] = 0x7f4,
124 [ARB_ERR_CAP_MASTER] = 0x7f8,
127 struct brcmstb_gisb_arb_device {
129 const int *gisb_offsets;
132 struct list_head next;
134 const char *master_names[sizeof(u32) * BITS_PER_BYTE];
138 static LIST_HEAD(brcmstb_gisb_arb_device_list);
140 static u32 gisb_read(struct brcmstb_gisb_arb_device *gdev, int reg)
142 int offset = gdev->gisb_offsets[reg];
145 /* return 1 if the hardware doesn't have ARB_ERR_CAP_MASTER */
146 if (reg == ARB_ERR_CAP_MASTER)
152 if (gdev->big_endian)
153 return ioread32be(gdev->base + offset);
155 return ioread32(gdev->base + offset);
158 static u64 gisb_read_address(struct brcmstb_gisb_arb_device *gdev)
162 value = gisb_read(gdev, ARB_ERR_CAP_ADDR);
163 value |= (u64)gisb_read(gdev, ARB_ERR_CAP_HI_ADDR) << 32;
168 static u64 gisb_read_bp_address(struct brcmstb_gisb_arb_device *gdev)
172 value = gisb_read(gdev, ARB_BP_CAP_ADDR);
173 value |= (u64)gisb_read(gdev, ARB_BP_CAP_HI_ADDR) << 32;
178 static void gisb_write(struct brcmstb_gisb_arb_device *gdev, u32 val, int reg)
180 int offset = gdev->gisb_offsets[reg];
185 if (gdev->big_endian)
186 iowrite32be(val, gdev->base + offset);
188 iowrite32(val, gdev->base + offset);
191 static ssize_t gisb_arb_get_timeout(struct device *dev,
192 struct device_attribute *attr,
195 struct brcmstb_gisb_arb_device *gdev = dev_get_drvdata(dev);
198 mutex_lock(&gdev->lock);
199 timeout = gisb_read(gdev, ARB_TIMER);
200 mutex_unlock(&gdev->lock);
202 return sprintf(buf, "%d", timeout);
205 static ssize_t gisb_arb_set_timeout(struct device *dev,
206 struct device_attribute *attr,
207 const char *buf, size_t count)
209 struct brcmstb_gisb_arb_device *gdev = dev_get_drvdata(dev);
212 ret = kstrtoint(buf, 10, &val);
216 if (val == 0 || val >= 0xffffffff)
219 mutex_lock(&gdev->lock);
220 gisb_write(gdev, val, ARB_TIMER);
221 mutex_unlock(&gdev->lock);
227 brcmstb_gisb_master_to_str(struct brcmstb_gisb_arb_device *gdev,
230 u32 mask = gdev->valid_mask & masters;
232 if (hweight_long(mask) != 1)
235 return gdev->master_names[ffs(mask) - 1];
238 static int brcmstb_gisb_arb_decode_addr(struct brcmstb_gisb_arb_device *gdev,
247 cap_status = gisb_read(gdev, ARB_ERR_CAP_STATUS);
249 /* Invalid captured address, bail out */
250 if (!(cap_status & ARB_ERR_CAP_STATUS_VALID))
253 /* Read the address and master */
254 arb_addr = gisb_read_address(gdev);
255 master = gisb_read(gdev, ARB_ERR_CAP_MASTER);
257 m_name = brcmstb_gisb_master_to_str(gdev, master);
259 snprintf(m_fmt, sizeof(m_fmt), "0x%08x", master);
263 pr_crit("GISB: %s at 0x%llx [%c %s], core: %s\n",
265 cap_status & ARB_ERR_CAP_STATUS_WRITE ? 'W' : 'R',
266 cap_status & ARB_ERR_CAP_STATUS_TIMEOUT ? "timeout" : "",
269 /* clear the GISB error */
270 gisb_write(gdev, ARB_ERR_CAP_CLEAR, ARB_ERR_CAP_CLR);
276 static int brcmstb_bus_error_handler(struct pt_regs *regs, int is_fixup)
279 struct brcmstb_gisb_arb_device *gdev;
282 list_for_each_entry(gdev, &brcmstb_gisb_arb_device_list, next) {
283 cap_status = gisb_read(gdev, ARB_ERR_CAP_STATUS);
285 /* Invalid captured address, bail out */
286 if (!(cap_status & ARB_ERR_CAP_STATUS_VALID)) {
291 ret |= brcmstb_gisb_arb_decode_addr(gdev, "bus error");
294 return is_fixup ? MIPS_BE_FIXUP : MIPS_BE_FATAL;
298 static irqreturn_t brcmstb_gisb_timeout_handler(int irq, void *dev_id)
300 brcmstb_gisb_arb_decode_addr(dev_id, "timeout");
305 static irqreturn_t brcmstb_gisb_tea_handler(int irq, void *dev_id)
307 brcmstb_gisb_arb_decode_addr(dev_id, "target abort");
312 static irqreturn_t brcmstb_gisb_bp_handler(int irq, void *dev_id)
314 struct brcmstb_gisb_arb_device *gdev = dev_id;
321 bp_status = gisb_read(gdev, ARB_BP_CAP_STATUS);
323 /* Invalid captured address, bail out */
324 if (!(bp_status & ARB_BP_CAP_STATUS_VALID))
327 /* Read the address and master */
328 arb_addr = gisb_read_bp_address(gdev);
329 master = gisb_read(gdev, ARB_BP_CAP_MASTER);
331 m_name = brcmstb_gisb_master_to_str(gdev, master);
333 snprintf(m_fmt, sizeof(m_fmt), "0x%08x", master);
337 pr_crit("GISB: breakpoint at 0x%llx [%c], core: %s\n",
338 arb_addr, bp_status & ARB_BP_CAP_STATUS_WRITE ? 'W' : 'R',
341 /* clear the GISB error */
342 gisb_write(gdev, ARB_ERR_CAP_CLEAR, ARB_ERR_CAP_CLR);
348 * Dump out gisb errors on die or panic.
350 static int dump_gisb_error(struct notifier_block *self, unsigned long v,
353 static struct notifier_block gisb_die_notifier = {
354 .notifier_call = dump_gisb_error,
357 static struct notifier_block gisb_panic_notifier = {
358 .notifier_call = dump_gisb_error,
361 static int dump_gisb_error(struct notifier_block *self, unsigned long v,
364 struct brcmstb_gisb_arb_device *gdev;
365 const char *reason = "panic";
367 if (self == &gisb_die_notifier)
370 /* iterate over each GISB arb registered handlers */
371 list_for_each_entry(gdev, &brcmstb_gisb_arb_device_list, next)
372 brcmstb_gisb_arb_decode_addr(gdev, reason);
377 static DEVICE_ATTR(gisb_arb_timeout, S_IWUSR | S_IRUGO,
378 gisb_arb_get_timeout, gisb_arb_set_timeout);
380 static struct attribute *gisb_arb_sysfs_attrs[] = {
381 &dev_attr_gisb_arb_timeout.attr,
385 static struct attribute_group gisb_arb_sysfs_attr_group = {
386 .attrs = gisb_arb_sysfs_attrs,
389 static const struct of_device_id brcmstb_gisb_arb_of_match[] = {
390 { .compatible = "brcm,gisb-arb", .data = gisb_offsets_bcm7445 },
391 { .compatible = "brcm,bcm7445-gisb-arb", .data = gisb_offsets_bcm7445 },
392 { .compatible = "brcm,bcm7435-gisb-arb", .data = gisb_offsets_bcm7435 },
393 { .compatible = "brcm,bcm7400-gisb-arb", .data = gisb_offsets_bcm7400 },
394 { .compatible = "brcm,bcm7278-gisb-arb", .data = gisb_offsets_bcm7278 },
395 { .compatible = "brcm,bcm7038-gisb-arb", .data = gisb_offsets_bcm7038 },
399 static int __init brcmstb_gisb_arb_probe(struct platform_device *pdev)
401 struct device_node *dn = pdev->dev.of_node;
402 struct brcmstb_gisb_arb_device *gdev;
403 const struct of_device_id *of_id;
404 int err, timeout_irq, tea_irq, bp_irq;
405 unsigned int num_masters, j = 0;
408 timeout_irq = platform_get_irq(pdev, 0);
409 tea_irq = platform_get_irq(pdev, 1);
410 bp_irq = platform_get_irq(pdev, 2);
412 gdev = devm_kzalloc(&pdev->dev, sizeof(*gdev), GFP_KERNEL);
416 mutex_init(&gdev->lock);
417 INIT_LIST_HEAD(&gdev->next);
419 gdev->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
420 if (IS_ERR(gdev->base))
421 return PTR_ERR(gdev->base);
423 of_id = of_match_node(brcmstb_gisb_arb_of_match, dn);
425 pr_err("failed to look up compatible string\n");
428 gdev->gisb_offsets = of_id->data;
429 gdev->big_endian = of_device_is_big_endian(dn);
431 err = devm_request_irq(&pdev->dev, timeout_irq,
432 brcmstb_gisb_timeout_handler, 0, pdev->name,
437 err = devm_request_irq(&pdev->dev, tea_irq,
438 brcmstb_gisb_tea_handler, 0, pdev->name,
443 /* Interrupt is optional */
445 err = devm_request_irq(&pdev->dev, bp_irq,
446 brcmstb_gisb_bp_handler, 0, pdev->name,
452 /* If we do not have a valid mask, assume all masters are enabled */
453 if (of_property_read_u32(dn, "brcm,gisb-arb-master-mask",
455 gdev->valid_mask = 0xffffffff;
457 /* Proceed with reading the litteral names if we agree on the
460 num_masters = of_property_count_strings(dn,
461 "brcm,gisb-arb-master-names");
462 if (hweight_long(gdev->valid_mask) == num_masters) {
463 first = ffs(gdev->valid_mask) - 1;
464 last = fls(gdev->valid_mask) - 1;
466 for (i = first; i < last; i++) {
467 if (!(gdev->valid_mask & BIT(i)))
470 of_property_read_string_index(dn,
471 "brcm,gisb-arb-master-names", j,
472 &gdev->master_names[i]);
477 err = sysfs_create_group(&pdev->dev.kobj, &gisb_arb_sysfs_attr_group);
481 platform_set_drvdata(pdev, gdev);
483 list_add_tail(&gdev->next, &brcmstb_gisb_arb_device_list);
486 mips_set_be_handler(brcmstb_bus_error_handler);
489 if (list_is_singular(&brcmstb_gisb_arb_device_list)) {
490 register_die_notifier(&gisb_die_notifier);
491 atomic_notifier_chain_register(&panic_notifier_list,
492 &gisb_panic_notifier);
495 dev_info(&pdev->dev, "registered irqs: %d, %d\n",
496 timeout_irq, tea_irq);
501 #ifdef CONFIG_PM_SLEEP
502 static int brcmstb_gisb_arb_suspend(struct device *dev)
504 struct brcmstb_gisb_arb_device *gdev = dev_get_drvdata(dev);
506 gdev->saved_timeout = gisb_read(gdev, ARB_TIMER);
511 /* Make sure we provide the same timeout value that was configured before, and
512 * do this before the GISB timeout interrupt handler has any chance to run.
514 static int brcmstb_gisb_arb_resume_noirq(struct device *dev)
516 struct brcmstb_gisb_arb_device *gdev = dev_get_drvdata(dev);
518 gisb_write(gdev, gdev->saved_timeout, ARB_TIMER);
523 #define brcmstb_gisb_arb_suspend NULL
524 #define brcmstb_gisb_arb_resume_noirq NULL
527 static const struct dev_pm_ops brcmstb_gisb_arb_pm_ops = {
528 .suspend = brcmstb_gisb_arb_suspend,
529 .resume_noirq = brcmstb_gisb_arb_resume_noirq,
532 static struct platform_driver brcmstb_gisb_arb_driver = {
534 .name = "brcm-gisb-arb",
535 .of_match_table = brcmstb_gisb_arb_of_match,
536 .pm = &brcmstb_gisb_arb_pm_ops,
540 static int __init brcm_gisb_driver_init(void)
542 return platform_driver_probe(&brcmstb_gisb_arb_driver,
543 brcmstb_gisb_arb_probe);
546 module_init(brcm_gisb_driver_init);
548 MODULE_AUTHOR("Broadcom");
549 MODULE_DESCRIPTION("Broadcom STB GISB arbiter driver");
550 MODULE_LICENSE("GPL v2");