irq-mbigen.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 HiSilicon Limited, All Rights Reserved.
  4. * Author: Jun Ma <majun258@huawei.com>
  5. * Author: Yun Wu <wuyun.wu@huawei.com>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irqchip.h>
  10. #include <linux/module.h>
  11. #include <linux/msi.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. /* Interrupt numbers per mbigen node supported */
  18. #define IRQS_PER_MBIGEN_NODE 128
  19. /* 64 irqs (Pin0-pin63) are reserved for each mbigen chip */
  20. #define RESERVED_IRQ_PER_MBIGEN_CHIP 64
  21. /* The maximum IRQ pin number of mbigen chip(start from 0) */
  22. #define MAXIMUM_IRQ_PIN_NUM 1407
  23. /*
  24. * In mbigen vector register
  25. * bit[21:12]: event id value
  26. * bit[11:0]: device id
  27. */
  28. #define IRQ_EVENT_ID_SHIFT 12
  29. #define IRQ_EVENT_ID_MASK 0x3ff
  30. /* register range of each mbigen node */
  31. #define MBIGEN_NODE_OFFSET 0x1000
  32. /* offset of vector register in mbigen node */
  33. #define REG_MBIGEN_VEC_OFFSET 0x200
  34. /*
  35. * offset of clear register in mbigen node
  36. * This register is used to clear the status
  37. * of interrupt
  38. */
  39. #define REG_MBIGEN_CLEAR_OFFSET 0xa000
  40. /*
  41. * offset of interrupt type register
  42. * This register is used to configure interrupt
  43. * trigger type
  44. */
  45. #define REG_MBIGEN_TYPE_OFFSET 0x0
  46. /**
  47. * struct mbigen_device - holds the information of mbigen device.
  48. *
  49. * @pdev: pointer to the platform device structure of mbigen chip.
  50. * @base: mapped address of this mbigen chip.
  51. */
  52. struct mbigen_device {
  53. struct platform_device *pdev;
  54. void __iomem *base;
  55. };
  56. static inline unsigned int get_mbigen_node_offset(unsigned int nid)
  57. {
  58. unsigned int offset = nid * MBIGEN_NODE_OFFSET;
  59. /*
  60. * To avoid touched clear register in unexpected way, we need to directly
  61. * skip clear register when access to more than 10 mbigen nodes.
  62. */
  63. if (nid >= (REG_MBIGEN_CLEAR_OFFSET / MBIGEN_NODE_OFFSET))
  64. offset += MBIGEN_NODE_OFFSET;
  65. return offset;
  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 + get_mbigen_node_offset(nid) + REG_MBIGEN_VEC_OFFSET;
  74. }
  75. static inline void get_mbigen_type_reg(irq_hw_number_t hwirq,
  76. u32 *mask, u32 *addr)
  77. {
  78. unsigned int nid, irq_ofst, ofst;
  79. hwirq -= RESERVED_IRQ_PER_MBIGEN_CHIP;
  80. nid = hwirq / IRQS_PER_MBIGEN_NODE + 1;
  81. irq_ofst = hwirq % IRQS_PER_MBIGEN_NODE;
  82. *mask = 1 << (irq_ofst % 32);
  83. ofst = irq_ofst / 32 * 4;
  84. *addr = ofst + get_mbigen_node_offset(nid) + REG_MBIGEN_TYPE_OFFSET;
  85. }
  86. static inline void get_mbigen_clear_reg(irq_hw_number_t hwirq,
  87. u32 *mask, u32 *addr)
  88. {
  89. unsigned int ofst = (hwirq / 32) * 4;
  90. *mask = 1 << (hwirq % 32);
  91. *addr = ofst + REG_MBIGEN_CLEAR_OFFSET;
  92. }
  93. static void mbigen_eoi_irq(struct irq_data *data)
  94. {
  95. void __iomem *base = data->chip_data;
  96. u32 mask, addr;
  97. get_mbigen_clear_reg(data->hwirq, &mask, &addr);
  98. writel_relaxed(mask, base + addr);
  99. irq_chip_eoi_parent(data);
  100. }
  101. static int mbigen_set_type(struct irq_data *data, unsigned int type)
  102. {
  103. void __iomem *base = data->chip_data;
  104. u32 mask, addr, val;
  105. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  106. return -EINVAL;
  107. get_mbigen_type_reg(data->hwirq, &mask, &addr);
  108. val = readl_relaxed(base + addr);
  109. if (type == IRQ_TYPE_LEVEL_HIGH)
  110. val |= mask;
  111. else
  112. val &= ~mask;
  113. writel_relaxed(val, base + addr);
  114. return 0;
  115. }
  116. static void mbigen_write_msi_msg(struct irq_data *d, struct msi_msg *msg)
  117. {
  118. void __iomem *base = d->chip_data;
  119. u32 val;
  120. if (!msg->address_lo && !msg->address_hi)
  121. return;
  122. base += get_mbigen_vec_reg(d->hwirq);
  123. val = readl_relaxed(base);
  124. val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
  125. val |= (msg->data << IRQ_EVENT_ID_SHIFT);
  126. /* The address of doorbell is encoded in mbigen register by default
  127. * So,we don't need to program the doorbell address at here
  128. */
  129. writel_relaxed(val, base);
  130. }
  131. static int mbigen_domain_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
  132. unsigned long *hwirq, unsigned int *type)
  133. {
  134. if (is_of_node(fwspec->fwnode) || is_acpi_device_node(fwspec->fwnode)) {
  135. if (fwspec->param_count != 2)
  136. return -EINVAL;
  137. if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
  138. (fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
  139. return -EINVAL;
  140. else
  141. *hwirq = fwspec->param[0];
  142. /* If there is no valid irq type, just use the default type */
  143. if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
  144. (fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
  145. *type = fwspec->param[1];
  146. else
  147. return -EINVAL;
  148. return 0;
  149. }
  150. return -EINVAL;
  151. }
  152. static void mbigen_domain_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  153. {
  154. arg->desc = desc;
  155. arg->hwirq = (u32)desc->data.icookie.value;
  156. }
  157. static const struct msi_domain_template mbigen_msi_template = {
  158. .chip = {
  159. .name = "mbigen-v2",
  160. .irq_mask = irq_chip_mask_parent,
  161. .irq_unmask = irq_chip_unmask_parent,
  162. .irq_eoi = mbigen_eoi_irq,
  163. .irq_set_type = mbigen_set_type,
  164. .irq_write_msi_msg = mbigen_write_msi_msg,
  165. },
  166. .ops = {
  167. .set_desc = mbigen_domain_set_desc,
  168. .msi_translate = mbigen_domain_translate,
  169. },
  170. .info = {
  171. .bus_token = DOMAIN_BUS_WIRED_TO_MSI,
  172. .flags = MSI_FLAG_USE_DEV_FWNODE,
  173. },
  174. };
  175. static bool mbigen_create_device_domain(struct device *dev, unsigned int size,
  176. struct mbigen_device *mgn_chip)
  177. {
  178. if (WARN_ON_ONCE(!dev->msi.domain))
  179. return false;
  180. return msi_create_device_irq_domain(dev, MSI_DEFAULT_DOMAIN,
  181. &mbigen_msi_template, size,
  182. NULL, mgn_chip->base);
  183. }
  184. static int mbigen_of_create_domain(struct platform_device *pdev,
  185. struct mbigen_device *mgn_chip)
  186. {
  187. struct platform_device *child;
  188. u32 num_pins;
  189. for_each_child_of_node_scoped(pdev->dev.of_node, np) {
  190. if (!of_property_read_bool(np, "interrupt-controller"))
  191. continue;
  192. child = of_platform_device_create(np, NULL, NULL);
  193. if (!child)
  194. return -ENOMEM;
  195. if (of_property_read_u32(child->dev.of_node, "num-pins",
  196. &num_pins) < 0) {
  197. dev_err(&pdev->dev, "No num-pins property\n");
  198. return -EINVAL;
  199. }
  200. if (!mbigen_create_device_domain(&child->dev, num_pins, mgn_chip))
  201. return -ENOMEM;
  202. }
  203. return 0;
  204. }
  205. #ifdef CONFIG_ACPI
  206. static const struct acpi_device_id mbigen_acpi_match[] = {
  207. { "HISI0152", 0 },
  208. {}
  209. };
  210. MODULE_DEVICE_TABLE(acpi, mbigen_acpi_match);
  211. static int mbigen_acpi_create_domain(struct platform_device *pdev,
  212. struct mbigen_device *mgn_chip)
  213. {
  214. u32 num_pins = 0;
  215. int ret;
  216. /*
  217. * "num-pins" is the total number of interrupt pins implemented in
  218. * this mbigen instance, and mbigen is an interrupt controller
  219. * connected to ITS converting wired interrupts into MSI, so we
  220. * use "num-pins" to alloc MSI vectors which are needed by client
  221. * devices connected to it.
  222. *
  223. * Here is the DSDT device node used for mbigen in firmware:
  224. * Device(MBI0) {
  225. * Name(_HID, "HISI0152")
  226. * Name(_UID, Zero)
  227. * Name(_CRS, ResourceTemplate() {
  228. * Memory32Fixed(ReadWrite, 0xa0080000, 0x10000)
  229. * })
  230. *
  231. * Name(_DSD, Package () {
  232. * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
  233. * Package () {
  234. * Package () {"num-pins", 378}
  235. * }
  236. * })
  237. * }
  238. */
  239. ret = device_property_read_u32(&pdev->dev, "num-pins", &num_pins);
  240. if (ret || num_pins == 0)
  241. return -EINVAL;
  242. if (!mbigen_create_device_domain(&pdev->dev, num_pins, mgn_chip))
  243. return -ENOMEM;
  244. return 0;
  245. }
  246. #else
  247. static inline int mbigen_acpi_create_domain(struct platform_device *pdev,
  248. struct mbigen_device *mgn_chip)
  249. {
  250. return -ENODEV;
  251. }
  252. #endif
  253. static int mbigen_device_probe(struct platform_device *pdev)
  254. {
  255. struct mbigen_device *mgn_chip;
  256. struct resource *res;
  257. int err;
  258. mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
  259. if (!mgn_chip)
  260. return -ENOMEM;
  261. mgn_chip->pdev = pdev;
  262. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  263. if (!res)
  264. return -EINVAL;
  265. mgn_chip->base = devm_ioremap(&pdev->dev, res->start,
  266. resource_size(res));
  267. if (!mgn_chip->base) {
  268. dev_err(&pdev->dev, "failed to ioremap %pR\n", res);
  269. return -ENOMEM;
  270. }
  271. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
  272. err = mbigen_of_create_domain(pdev, mgn_chip);
  273. else if (ACPI_COMPANION(&pdev->dev))
  274. err = mbigen_acpi_create_domain(pdev, mgn_chip);
  275. else
  276. err = -EINVAL;
  277. if (err) {
  278. dev_err(&pdev->dev, "Failed to create mbi-gen irqdomain\n");
  279. return err;
  280. }
  281. platform_set_drvdata(pdev, mgn_chip);
  282. return 0;
  283. }
  284. static const struct of_device_id mbigen_of_match[] = {
  285. { .compatible = "hisilicon,mbigen-v2" },
  286. { /* END */ }
  287. };
  288. MODULE_DEVICE_TABLE(of, mbigen_of_match);
  289. static struct platform_driver mbigen_platform_driver = {
  290. .driver = {
  291. .name = "Hisilicon MBIGEN-V2",
  292. .of_match_table = mbigen_of_match,
  293. .acpi_match_table = ACPI_PTR(mbigen_acpi_match),
  294. .suppress_bind_attrs = true,
  295. },
  296. .probe = mbigen_device_probe,
  297. };
  298. module_platform_driver(mbigen_platform_driver);
  299. MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
  300. MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
  301. MODULE_DESCRIPTION("HiSilicon MBI Generator driver");