HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / bus / brcmstb_gisb.c
blobf5918301828073dd24e9c4291b356d24ba3101a4
1 /*
2 * Copyright (C) 2014-2017 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/interrupt.h>
19 #include <linux/sysfs.h>
20 #include <linux/io.h>
21 #include <linux/string.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
24 #include <linux/of.h>
25 #include <linux/bitops.h>
26 #include <linux/pm.h>
28 #ifdef CONFIG_ARM
29 #include <asm/bug.h>
30 #include <asm/signal.h>
31 #endif
33 #define ARB_ERR_CAP_CLEAR (1 << 0)
34 #define ARB_ERR_CAP_STATUS_TIMEOUT (1 << 12)
35 #define ARB_ERR_CAP_STATUS_TEA (1 << 11)
36 #define ARB_ERR_CAP_STATUS_WRITE (1 << 1)
37 #define ARB_ERR_CAP_STATUS_VALID (1 << 0)
39 enum {
40 ARB_TIMER,
41 ARB_ERR_CAP_CLR,
42 ARB_ERR_CAP_HI_ADDR,
43 ARB_ERR_CAP_ADDR,
44 ARB_ERR_CAP_STATUS,
45 ARB_ERR_CAP_MASTER,
48 static const int gisb_offsets_bcm7038[] = {
49 [ARB_TIMER] = 0x00c,
50 [ARB_ERR_CAP_CLR] = 0x0c4,
51 [ARB_ERR_CAP_HI_ADDR] = -1,
52 [ARB_ERR_CAP_ADDR] = 0x0c8,
53 [ARB_ERR_CAP_STATUS] = 0x0d0,
54 [ARB_ERR_CAP_MASTER] = -1,
57 static const int gisb_offsets_bcm7400[] = {
58 [ARB_TIMER] = 0x00c,
59 [ARB_ERR_CAP_CLR] = 0x0c8,
60 [ARB_ERR_CAP_HI_ADDR] = -1,
61 [ARB_ERR_CAP_ADDR] = 0x0cc,
62 [ARB_ERR_CAP_STATUS] = 0x0d4,
63 [ARB_ERR_CAP_MASTER] = 0x0d8,
66 static const int gisb_offsets_bcm7435[] = {
67 [ARB_TIMER] = 0x00c,
68 [ARB_ERR_CAP_CLR] = 0x168,
69 [ARB_ERR_CAP_HI_ADDR] = -1,
70 [ARB_ERR_CAP_ADDR] = 0x16c,
71 [ARB_ERR_CAP_STATUS] = 0x174,
72 [ARB_ERR_CAP_MASTER] = 0x178,
75 static const int gisb_offsets_bcm7445[] = {
76 [ARB_TIMER] = 0x008,
77 [ARB_ERR_CAP_CLR] = 0x7e4,
78 [ARB_ERR_CAP_HI_ADDR] = 0x7e8,
79 [ARB_ERR_CAP_ADDR] = 0x7ec,
80 [ARB_ERR_CAP_STATUS] = 0x7f4,
81 [ARB_ERR_CAP_MASTER] = 0x7f8,
84 struct brcmstb_gisb_arb_device {
85 void __iomem *base;
86 const int *gisb_offsets;
87 bool big_endian;
88 struct mutex lock;
89 struct list_head next;
90 u32 valid_mask;
91 const char *master_names[sizeof(u32) * BITS_PER_BYTE];
92 u32 saved_timeout;
95 static LIST_HEAD(brcmstb_gisb_arb_device_list);
97 static u32 gisb_read(struct brcmstb_gisb_arb_device *gdev, int reg)
99 int offset = gdev->gisb_offsets[reg];
101 if (offset < 0) {
102 /* return 1 if the hardware doesn't have ARB_ERR_CAP_MASTER */
103 if (reg == ARB_ERR_CAP_MASTER)
104 return 1;
105 else
106 return 0;
109 if (gdev->big_endian)
110 return ioread32be(gdev->base + offset);
111 else
112 return ioread32(gdev->base + offset);
115 static u64 gisb_read_address(struct brcmstb_gisb_arb_device *gdev)
117 u64 value;
119 value = gisb_read(gdev, ARB_ERR_CAP_ADDR);
120 value |= (u64)gisb_read(gdev, ARB_ERR_CAP_HI_ADDR) << 32;
122 return value;
125 static void gisb_write(struct brcmstb_gisb_arb_device *gdev, u32 val, int reg)
127 int offset = gdev->gisb_offsets[reg];
129 if (offset == -1)
130 return;
132 if (gdev->big_endian)
133 iowrite32be(val, gdev->base + offset);
134 else
135 iowrite32(val, gdev->base + offset);
138 static ssize_t gisb_arb_get_timeout(struct device *dev,
139 struct device_attribute *attr,
140 char *buf)
142 struct platform_device *pdev = to_platform_device(dev);
143 struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
144 u32 timeout;
146 mutex_lock(&gdev->lock);
147 timeout = gisb_read(gdev, ARB_TIMER);
148 mutex_unlock(&gdev->lock);
150 return sprintf(buf, "%d", timeout);
153 static ssize_t gisb_arb_set_timeout(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t count)
157 struct platform_device *pdev = to_platform_device(dev);
158 struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
159 int val, ret;
161 ret = kstrtoint(buf, 10, &val);
162 if (ret < 0)
163 return ret;
165 if (val == 0 || val >= 0xffffffff)
166 return -EINVAL;
168 mutex_lock(&gdev->lock);
169 gisb_write(gdev, val, ARB_TIMER);
170 mutex_unlock(&gdev->lock);
172 return count;
175 static const char *
176 brcmstb_gisb_master_to_str(struct brcmstb_gisb_arb_device *gdev,
177 u32 masters)
179 u32 mask = gdev->valid_mask & masters;
181 if (hweight_long(mask) != 1)
182 return NULL;
184 return gdev->master_names[ffs(mask) - 1];
187 static int brcmstb_gisb_arb_decode_addr(struct brcmstb_gisb_arb_device *gdev,
188 const char *reason)
190 u32 cap_status;
191 u64 arb_addr;
192 u32 master;
193 const char *m_name;
194 char m_fmt[11];
196 cap_status = gisb_read(gdev, ARB_ERR_CAP_STATUS);
198 /* Invalid captured address, bail out */
199 if (!(cap_status & ARB_ERR_CAP_STATUS_VALID))
200 return 1;
202 /* Read the address and master */
203 arb_addr = gisb_read_address(gdev);
204 master = gisb_read(gdev, ARB_ERR_CAP_MASTER);
206 m_name = brcmstb_gisb_master_to_str(gdev, master);
207 if (!m_name) {
208 snprintf(m_fmt, sizeof(m_fmt), "0x%08x", master);
209 m_name = m_fmt;
212 pr_crit("%s: %s at 0x%llx [%c %s], core: %s\n",
213 __func__, reason, arb_addr,
214 cap_status & ARB_ERR_CAP_STATUS_WRITE ? 'W' : 'R',
215 cap_status & ARB_ERR_CAP_STATUS_TIMEOUT ? "timeout" : "",
216 m_name);
218 /* clear the GISB error */
219 gisb_write(gdev, ARB_ERR_CAP_CLEAR, ARB_ERR_CAP_CLR);
221 return 0;
224 #ifdef CONFIG_ARM
225 static int brcmstb_bus_error_handler(unsigned long addr, unsigned int fsr,
226 struct pt_regs *regs)
228 int ret = 0;
229 struct brcmstb_gisb_arb_device *gdev;
231 /* iterate over each GISB arb registered handlers */
232 list_for_each_entry(gdev, &brcmstb_gisb_arb_device_list, next)
233 ret |= brcmstb_gisb_arb_decode_addr(gdev, "bus error");
235 * If it was an imprecise abort, then we need to correct the
236 * return address to be _after_ the instruction.
238 if (fsr & (1 << 10))
239 regs->ARM_pc += 4;
241 return ret;
243 #endif
245 static irqreturn_t brcmstb_gisb_timeout_handler(int irq, void *dev_id)
247 brcmstb_gisb_arb_decode_addr(dev_id, "timeout");
249 return IRQ_HANDLED;
252 static irqreturn_t brcmstb_gisb_tea_handler(int irq, void *dev_id)
254 brcmstb_gisb_arb_decode_addr(dev_id, "target abort");
256 return IRQ_HANDLED;
259 static DEVICE_ATTR(gisb_arb_timeout, S_IWUSR | S_IRUGO,
260 gisb_arb_get_timeout, gisb_arb_set_timeout);
262 static struct attribute *gisb_arb_sysfs_attrs[] = {
263 &dev_attr_gisb_arb_timeout.attr,
264 NULL,
267 static struct attribute_group gisb_arb_sysfs_attr_group = {
268 .attrs = gisb_arb_sysfs_attrs,
271 static const struct of_device_id brcmstb_gisb_arb_of_match[] = {
272 { .compatible = "brcm,gisb-arb", .data = gisb_offsets_bcm7445 },
273 { .compatible = "brcm,bcm7445-gisb-arb", .data = gisb_offsets_bcm7445 },
274 { .compatible = "brcm,bcm7435-gisb-arb", .data = gisb_offsets_bcm7435 },
275 { .compatible = "brcm,bcm7400-gisb-arb", .data = gisb_offsets_bcm7400 },
276 { .compatible = "brcm,bcm7038-gisb-arb", .data = gisb_offsets_bcm7038 },
277 { },
280 static int __init brcmstb_gisb_arb_probe(struct platform_device *pdev)
282 struct device_node *dn = pdev->dev.of_node;
283 struct brcmstb_gisb_arb_device *gdev;
284 const struct of_device_id *of_id;
285 struct resource *r;
286 int err, timeout_irq, tea_irq;
287 unsigned int num_masters, j = 0;
288 int i, first, last;
290 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
291 timeout_irq = platform_get_irq(pdev, 0);
292 tea_irq = platform_get_irq(pdev, 1);
294 gdev = devm_kzalloc(&pdev->dev, sizeof(*gdev), GFP_KERNEL);
295 if (!gdev)
296 return -ENOMEM;
298 mutex_init(&gdev->lock);
299 INIT_LIST_HEAD(&gdev->next);
301 gdev->base = devm_ioremap_resource(&pdev->dev, r);
302 if (IS_ERR(gdev->base))
303 return PTR_ERR(gdev->base);
305 of_id = of_match_node(brcmstb_gisb_arb_of_match, dn);
306 if (!of_id) {
307 pr_err("failed to look up compatible string\n");
308 return -EINVAL;
310 gdev->gisb_offsets = of_id->data;
311 gdev->big_endian = of_device_is_big_endian(dn);
313 err = devm_request_irq(&pdev->dev, timeout_irq,
314 brcmstb_gisb_timeout_handler, 0, pdev->name,
315 gdev);
316 if (err < 0)
317 return err;
319 err = devm_request_irq(&pdev->dev, tea_irq,
320 brcmstb_gisb_tea_handler, 0, pdev->name,
321 gdev);
322 if (err < 0)
323 return err;
325 /* If we do not have a valid mask, assume all masters are enabled */
326 if (of_property_read_u32(dn, "brcm,gisb-arb-master-mask",
327 &gdev->valid_mask))
328 gdev->valid_mask = 0xffffffff;
330 /* Proceed with reading the litteral names if we agree on the
331 * number of masters
333 num_masters = of_property_count_strings(dn,
334 "brcm,gisb-arb-master-names");
335 if (hweight_long(gdev->valid_mask) == num_masters) {
336 first = ffs(gdev->valid_mask) - 1;
337 last = fls(gdev->valid_mask) - 1;
339 for (i = first; i < last; i++) {
340 if (!(gdev->valid_mask & BIT(i)))
341 continue;
343 of_property_read_string_index(dn,
344 "brcm,gisb-arb-master-names", j,
345 &gdev->master_names[i]);
346 j++;
350 err = sysfs_create_group(&pdev->dev.kobj, &gisb_arb_sysfs_attr_group);
351 if (err)
352 return err;
354 platform_set_drvdata(pdev, gdev);
356 list_add_tail(&gdev->next, &brcmstb_gisb_arb_device_list);
358 #ifdef CONFIG_ARM
359 hook_fault_code(22, brcmstb_bus_error_handler, SIGBUS, 0,
360 "imprecise external abort");
361 #endif
363 dev_info(&pdev->dev, "registered mem: %p, irqs: %d, %d\n",
364 gdev->base, timeout_irq, tea_irq);
366 return 0;
369 #ifdef CONFIG_PM_SLEEP
370 static int brcmstb_gisb_arb_suspend(struct device *dev)
372 struct platform_device *pdev = to_platform_device(dev);
373 struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
375 gdev->saved_timeout = gisb_read(gdev, ARB_TIMER);
377 return 0;
380 /* Make sure we provide the same timeout value that was configured before, and
381 * do this before the GISB timeout interrupt handler has any chance to run.
383 static int brcmstb_gisb_arb_resume_noirq(struct device *dev)
385 struct platform_device *pdev = to_platform_device(dev);
386 struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
388 gisb_write(gdev, gdev->saved_timeout, ARB_TIMER);
390 return 0;
392 #else
393 #define brcmstb_gisb_arb_suspend NULL
394 #define brcmstb_gisb_arb_resume_noirq NULL
395 #endif
397 static const struct dev_pm_ops brcmstb_gisb_arb_pm_ops = {
398 .suspend = brcmstb_gisb_arb_suspend,
399 .resume_noirq = brcmstb_gisb_arb_resume_noirq,
402 static struct platform_driver brcmstb_gisb_arb_driver = {
403 .driver = {
404 .name = "brcm-gisb-arb",
405 .of_match_table = brcmstb_gisb_arb_of_match,
406 .pm = &brcmstb_gisb_arb_pm_ops,
410 static int __init brcm_gisb_driver_init(void)
412 return platform_driver_probe(&brcmstb_gisb_arb_driver,
413 brcmstb_gisb_arb_probe);
416 module_init(brcm_gisb_driver_init);