irq-mbigen.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (C) 2015 Hisilicon Limited, All Rights Reserved.
  3. * Author: Jun Ma <majun258@huawei.com>
  4. * Author: Yun Wu <wuyun.wu@huawei.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/acpi.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irqchip.h>
  21. #include <linux/module.h>
  22. #include <linux/msi.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. /* Interrupt numbers per mbigen node supported */
  29. #define IRQS_PER_MBIGEN_NODE 128
  30. /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */
  31. #define RESERVED_IRQ_PER_MBIGEN_CHIP 64
  32. /* The maximum IRQ pin number of mbigen chip(start from 0) */
  33. #define MAXIMUM_IRQ_PIN_NUM 1407
  34. /**
  35. * In mbigen vector register
  36. * bit[21:12]: event id value
  37. * bit[11:0]: device id
  38. */
  39. #define IRQ_EVENT_ID_SHIFT 12
  40. #define IRQ_EVENT_ID_MASK 0x3ff
  41. /* register range of each mbigen node */
  42. #define MBIGEN_NODE_OFFSET 0x1000
  43. /* offset of vector register in mbigen node */
  44. #define REG_MBIGEN_VEC_OFFSET 0x200
  45. /**
  46. * offset of clear register in mbigen node
  47. * This register is used to clear the status
  48. * of interrupt
  49. */
  50. #define REG_MBIGEN_CLEAR_OFFSET 0xa000
  51. /**
  52. * offset of interrupt type register
  53. * This register is used to configure interrupt
  54. * trigger type
  55. */
  56. #define REG_MBIGEN_TYPE_OFFSET 0x0
  57. /**
  58. * struct mbigen_device - holds the information of mbigen device.
  59. *
  60. * @pdev: pointer to the platform device structure of mbigen chip.
  61. * @base: mapped address of this mbigen chip.
  62. */
  63. struct mbigen_device {
  64. struct platform_device *pdev;
  65. void __iomem *base;
  66. };
  67. static inline unsigned int get_mbigen_vec_reg(irq_hw_number_t hwirq)
  68. {
  69. unsigned int nid, pin;
  70. hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
  71. nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
  72. pin = hwirq % IRQS_PER_MBIGEN_NODE;
  73. return pin * 4 + nid * MBIGEN_NODE_OFFSET
  74. + REG_MBIGEN_VEC_OFFSET;
  75. }
  76. static inline void get_mbigen_type_reg(irq_hw_number_t hwirq,
  77. u32 *mask, u32 *addr)
  78. {
  79. unsigned int nid, irq_ofst, ofst;
  80. hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
  81. nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
  82. irq_ofst = hwirq % IRQS_PER_MBIGEN_NODE;
  83. *mask = 1 << (irq_ofst % 32);
  84. ofst = irq_ofst / 32 * 4;
  85. *addr = ofst + nid * MBIGEN_NODE_OFFSET
  86. + REG_MBIGEN_TYPE_OFFSET;
  87. }
  88. static inline void get_mbigen_clear_reg(irq_hw_number_t hwirq,
  89. u32 *mask, u32 *addr)
  90. {
  91. unsigned int ofst = (hwirq / 32) * 4;
  92. *mask = 1 << (hwirq % 32);
  93. *addr = ofst + REG_MBIGEN_CLEAR_OFFSET;
  94. }
  95. static void mbigen_eoi_irq(struct irq_data *data)
  96. {
  97. void __iomem *base = data->chip_data;
  98. u32 mask, addr;
  99. get_mbigen_clear_reg(data->hwirq, &mask, &addr);
  100. writel_relaxed(mask, base + addr);
  101. irq_chip_eoi_parent(data);
  102. }
  103. static int mbigen_set_type(struct irq_data *data, unsigned int type)
  104. {
  105. void __iomem *base = data->chip_data;
  106. u32 mask, addr, val;
  107. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  108. return -EINVAL;
  109. get_mbigen_type_reg(data->hwirq, &mask, &addr);
  110. val = readl_relaxed(base + addr);
  111. if (type == IRQ_TYPE_LEVEL_HIGH)
  112. val |= mask;
  113. else
  114. val &= ~mask;
  115. writel_relaxed(val, base + addr);
  116. return 0;
  117. }
  118. static struct irq_chip mbigen_irq_chip = {
  119. .name = "mbigen-v2",
  120. .irq_mask = irq_chip_mask_parent,
  121. .irq_unmask = irq_chip_unmask_parent,
  122. .irq_eoi = mbigen_eoi_irq,
  123. .irq_set_type = mbigen_set_type,
  124. .irq_set_affinity = irq_chip_set_affinity_parent,
  125. };
  126. static void mbigen_write_msg(struct msi_desc *desc, struct msi_msg *msg)
  127. {
  128. struct irq_data *d = irq_get_irq_data(desc->irq);
  129. void __iomem *base = d->chip_data;
  130. u32 val;
  131. base += get_mbigen_vec_reg(d->hwirq);
  132. val = readl_relaxed(base);
  133. val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
  134. val |= (msg->data << IRQ_EVENT_ID_SHIFT);
  135. /* The address of doorbell is encoded in mbigen register by default
  136. * So,we don't need to program the doorbell address at here
  137. */
  138. writel_relaxed(val, base);
  139. }
  140. static int mbigen_domain_translate(struct irq_domain *d,
  141. struct irq_fwspec *fwspec,
  142. unsigned long *hwirq,
  143. unsigned int *type)
  144. {
  145. if (is_of_node(fwspec->fwnode) || is_acpi_device_node(fwspec->fwnode)) {
  146. if (fwspec->param_count != 2)
  147. return -EINVAL;
  148. if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
  149. (fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
  150. return -EINVAL;
  151. else
  152. *hwirq = fwspec->param[0];
  153. /* If there is no valid irq type, just use the default type */
  154. if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
  155. (fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
  156. *type = fwspec->param[1];
  157. else
  158. return -EINVAL;
  159. return 0;
  160. }
  161. return -EINVAL;
  162. }
  163. static int mbigen_irq_domain_alloc(struct irq_domain *domain,
  164. unsigned int virq,
  165. unsigned int nr_irqs,
  166. void *args)
  167. {
  168. struct irq_fwspec *fwspec = args;
  169. irq_hw_number_t hwirq;
  170. unsigned int type;
  171. struct mbigen_device *mgn_chip;
  172. int i, err;
  173. err = mbigen_domain_translate(domain, fwspec, &hwirq, &type);
  174. if (err)
  175. return err;
  176. err = platform_msi_domain_alloc(domain, virq, nr_irqs);
  177. if (err)
  178. return err;
  179. mgn_chip = platform_msi_get_host_data(domain);
  180. for (i = 0; i < nr_irqs; i++)
  181. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  182. &mbigen_irq_chip, mgn_chip->base);
  183. return 0;
  184. }
  185. static const struct irq_domain_ops mbigen_domain_ops = {
  186. .translate = mbigen_domain_translate,
  187. .alloc = mbigen_irq_domain_alloc,
  188. .free = irq_domain_free_irqs_common,
  189. };
  190. static int mbigen_of_create_domain(struct platform_device *pdev,
  191. struct mbigen_device *mgn_chip)
  192. {
  193. struct device *parent;
  194. struct platform_device *child;
  195. struct irq_domain *domain;
  196. struct device_node *np;
  197. u32 num_pins;
  198. for_each_child_of_node(pdev->dev.of_node, np) {
  199. if (!of_property_read_bool(np, "interrupt-controller"))
  200. continue;
  201. parent = platform_bus_type.dev_root;
  202. child = of_platform_device_create(np, NULL, parent);
  203. if (!child)
  204. return -ENOMEM;
  205. if (of_property_read_u32(child->dev.of_node, "num-pins",
  206. &num_pins) < 0) {
  207. dev_err(&pdev->dev, "No num-pins property\n");
  208. return -EINVAL;
  209. }
  210. domain = platform_msi_create_device_domain(&child->dev, num_pins,
  211. mbigen_write_msg,
  212. &mbigen_domain_ops,
  213. mgn_chip);
  214. if (!domain)
  215. return -ENOMEM;
  216. }
  217. return 0;
  218. }
  219. #ifdef CONFIG_ACPI
  220. static int mbigen_acpi_create_domain(struct platform_device *pdev,
  221. struct mbigen_device *mgn_chip)
  222. {
  223. struct irq_domain *domain;
  224. u32 num_pins = 0;
  225. int ret;
  226. /*
  227. * "num-pins" is the total number of interrupt pins implemented in
  228. * this mbigen instance, and mbigen is an interrupt controller
  229. * connected to ITS converting wired interrupts into MSI, so we
  230. * use "num-pins" to alloc MSI vectors which are needed by client
  231. * devices connected to it.
  232. *
  233. * Here is the DSDT device node used for mbigen in firmware:
  234. * Device(MBI0) {
  235. * Name(_HID, "HISI0152")
  236. * Name(_UID, Zero)
  237. * Name(_CRS, ResourceTemplate() {
  238. * Memory32Fixed(ReadWrite, 0xa0080000, 0x10000)
  239. * })
  240. *
  241. * Name(_DSD, Package () {
  242. * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
  243. * Package () {
  244. * Package () {"num-pins", 378}
  245. * }
  246. * })
  247. * }
  248. */
  249. ret = device_property_read_u32(&pdev->dev, "num-pins", &num_pins);
  250. if (ret || num_pins == 0)
  251. return -EINVAL;
  252. domain = platform_msi_create_device_domain(&pdev->dev, num_pins,
  253. mbigen_write_msg,
  254. &mbigen_domain_ops,
  255. mgn_chip);
  256. if (!domain)
  257. return -ENOMEM;
  258. return 0;
  259. }
  260. #else
  261. static inline int mbigen_acpi_create_domain(struct platform_device *pdev,
  262. struct mbigen_device *mgn_chip)
  263. {
  264. return -ENODEV;
  265. }
  266. #endif
  267. static int mbigen_device_probe(struct platform_device *pdev)
  268. {
  269. struct mbigen_device *mgn_chip;
  270. struct resource *res;
  271. int err;
  272. mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
  273. if (!mgn_chip)
  274. return -ENOMEM;
  275. mgn_chip->pdev = pdev;
  276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. if (!res)
  278. return -EINVAL;
  279. mgn_chip->base = devm_ioremap(&pdev->dev, res->start,
  280. resource_size(res));
  281. if (!mgn_chip->base) {
  282. dev_err(&pdev->dev, "failed to ioremap %pR\n", res);
  283. return -ENOMEM;
  284. }
  285. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
  286. err = mbigen_of_create_domain(pdev, mgn_chip);
  287. else if (ACPI_COMPANION(&pdev->dev))
  288. err = mbigen_acpi_create_domain(pdev, mgn_chip);
  289. else
  290. err = -EINVAL;
  291. if (err) {
  292. dev_err(&pdev->dev, "Failed to create mbi-gen@%p irqdomain",
  293. mgn_chip->base);
  294. return err;
  295. }
  296. platform_set_drvdata(pdev, mgn_chip);
  297. return 0;
  298. }
  299. static const struct of_device_id mbigen_of_match[] = {
  300. { .compatible = "hisilicon,mbigen-v2" },
  301. { /* END */ }
  302. };
  303. MODULE_DEVICE_TABLE(of, mbigen_of_match);
  304. static const struct acpi_device_id mbigen_acpi_match[] = {
  305. { "HISI0152", 0 },
  306. {}
  307. };
  308. MODULE_DEVICE_TABLE(acpi, mbigen_acpi_match);
  309. static struct platform_driver mbigen_platform_driver = {
  310. .driver = {
  311. .name = "Hisilicon MBIGEN-V2",
  312. .of_match_table = mbigen_of_match,
  313. .acpi_match_table = ACPI_PTR(mbigen_acpi_match),
  314. },
  315. .probe = mbigen_device_probe,
  316. };
  317. module_platform_driver(mbigen_platform_driver);
  318. MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
  319. MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
  320. MODULE_LICENSE("GPL");
  321. MODULE_DESCRIPTION("Hisilicon MBI Generator driver");