irq-mbigen.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (!msg->address_lo && !msg->address_hi)
  132. return;
  133. base += get_mbigen_vec_reg(d->hwirq);
  134. val = readl_relaxed(base);
  135. val &= ~(IRQ_EVENT_ID_MASK << IRQ_EVENT_ID_SHIFT);
  136. val |= (msg->data << IRQ_EVENT_ID_SHIFT);
  137. /* The address of doorbell is encoded in mbigen register by default
  138. * So,we don't need to program the doorbell address at here
  139. */
  140. writel_relaxed(val, base);
  141. }
  142. static int mbigen_domain_translate(struct irq_domain *d,
  143. struct irq_fwspec *fwspec,
  144. unsigned long *hwirq,
  145. unsigned int *type)
  146. {
  147. if (is_of_node(fwspec->fwnode) || is_acpi_device_node(fwspec->fwnode)) {
  148. if (fwspec->param_count != 2)
  149. return -EINVAL;
  150. if ((fwspec->param[0] > MAXIMUM_IRQ_PIN_NUM) ||
  151. (fwspec->param[0] < RESERVED_IRQ_PER_MBIGEN_CHIP))
  152. return -EINVAL;
  153. else
  154. *hwirq = fwspec->param[0];
  155. /* If there is no valid irq type, just use the default type */
  156. if ((fwspec->param[1] == IRQ_TYPE_EDGE_RISING) ||
  157. (fwspec->param[1] == IRQ_TYPE_LEVEL_HIGH))
  158. *type = fwspec->param[1];
  159. else
  160. return -EINVAL;
  161. return 0;
  162. }
  163. return -EINVAL;
  164. }
  165. static int mbigen_irq_domain_alloc(struct irq_domain *domain,
  166. unsigned int virq,
  167. unsigned int nr_irqs,
  168. void *args)
  169. {
  170. struct irq_fwspec *fwspec = args;
  171. irq_hw_number_t hwirq;
  172. unsigned int type;
  173. struct mbigen_device *mgn_chip;
  174. int i, err;
  175. err = mbigen_domain_translate(domain, fwspec, &hwirq, &type);
  176. if (err)
  177. return err;
  178. err = platform_msi_domain_alloc(domain, virq, nr_irqs);
  179. if (err)
  180. return err;
  181. mgn_chip = platform_msi_get_host_data(domain);
  182. for (i = 0; i < nr_irqs; i++)
  183. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  184. &mbigen_irq_chip, mgn_chip->base);
  185. return 0;
  186. }
  187. static void mbigen_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  188. unsigned int nr_irqs)
  189. {
  190. platform_msi_domain_free(domain, virq, nr_irqs);
  191. }
  192. static const struct irq_domain_ops mbigen_domain_ops = {
  193. .translate = mbigen_domain_translate,
  194. .alloc = mbigen_irq_domain_alloc,
  195. .free = mbigen_irq_domain_free,
  196. };
  197. static int mbigen_of_create_domain(struct platform_device *pdev,
  198. struct mbigen_device *mgn_chip)
  199. {
  200. struct device *parent;
  201. struct platform_device *child;
  202. struct irq_domain *domain;
  203. struct device_node *np;
  204. u32 num_pins;
  205. for_each_child_of_node(pdev->dev.of_node, np) {
  206. if (!of_property_read_bool(np, "interrupt-controller"))
  207. continue;
  208. parent = platform_bus_type.dev_root;
  209. child = of_platform_device_create(np, NULL, parent);
  210. if (!child)
  211. return -ENOMEM;
  212. if (of_property_read_u32(child->dev.of_node, "num-pins",
  213. &num_pins) < 0) {
  214. dev_err(&pdev->dev, "No num-pins property\n");
  215. return -EINVAL;
  216. }
  217. domain = platform_msi_create_device_domain(&child->dev, num_pins,
  218. mbigen_write_msg,
  219. &mbigen_domain_ops,
  220. mgn_chip);
  221. if (!domain)
  222. return -ENOMEM;
  223. }
  224. return 0;
  225. }
  226. #ifdef CONFIG_ACPI
  227. static int mbigen_acpi_create_domain(struct platform_device *pdev,
  228. struct mbigen_device *mgn_chip)
  229. {
  230. struct irq_domain *domain;
  231. u32 num_pins = 0;
  232. int ret;
  233. /*
  234. * "num-pins" is the total number of interrupt pins implemented in
  235. * this mbigen instance, and mbigen is an interrupt controller
  236. * connected to ITS converting wired interrupts into MSI, so we
  237. * use "num-pins" to alloc MSI vectors which are needed by client
  238. * devices connected to it.
  239. *
  240. * Here is the DSDT device node used for mbigen in firmware:
  241. * Device(MBI0) {
  242. * Name(_HID, "HISI0152")
  243. * Name(_UID, Zero)
  244. * Name(_CRS, ResourceTemplate() {
  245. * Memory32Fixed(ReadWrite, 0xa0080000, 0x10000)
  246. * })
  247. *
  248. * Name(_DSD, Package () {
  249. * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
  250. * Package () {
  251. * Package () {"num-pins", 378}
  252. * }
  253. * })
  254. * }
  255. */
  256. ret = device_property_read_u32(&pdev->dev, "num-pins", &num_pins);
  257. if (ret || num_pins == 0)
  258. return -EINVAL;
  259. domain = platform_msi_create_device_domain(&pdev->dev, num_pins,
  260. mbigen_write_msg,
  261. &mbigen_domain_ops,
  262. mgn_chip);
  263. if (!domain)
  264. return -ENOMEM;
  265. return 0;
  266. }
  267. #else
  268. static inline int mbigen_acpi_create_domain(struct platform_device *pdev,
  269. struct mbigen_device *mgn_chip)
  270. {
  271. return -ENODEV;
  272. }
  273. #endif
  274. static int mbigen_device_probe(struct platform_device *pdev)
  275. {
  276. struct mbigen_device *mgn_chip;
  277. struct resource *res;
  278. int err;
  279. mgn_chip = devm_kzalloc(&pdev->dev, sizeof(*mgn_chip), GFP_KERNEL);
  280. if (!mgn_chip)
  281. return -ENOMEM;
  282. mgn_chip->pdev = pdev;
  283. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. if (!res)
  285. return -EINVAL;
  286. mgn_chip->base = devm_ioremap(&pdev->dev, res->start,
  287. resource_size(res));
  288. if (!mgn_chip->base) {
  289. dev_err(&pdev->dev, "failed to ioremap %pR\n", res);
  290. return -ENOMEM;
  291. }
  292. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
  293. err = mbigen_of_create_domain(pdev, mgn_chip);
  294. else if (ACPI_COMPANION(&pdev->dev))
  295. err = mbigen_acpi_create_domain(pdev, mgn_chip);
  296. else
  297. err = -EINVAL;
  298. if (err) {
  299. dev_err(&pdev->dev, "Failed to create mbi-gen@%p irqdomain",
  300. mgn_chip->base);
  301. return err;
  302. }
  303. platform_set_drvdata(pdev, mgn_chip);
  304. return 0;
  305. }
  306. static const struct of_device_id mbigen_of_match[] = {
  307. { .compatible = "hisilicon,mbigen-v2" },
  308. { /* END */ }
  309. };
  310. MODULE_DEVICE_TABLE(of, mbigen_of_match);
  311. static const struct acpi_device_id mbigen_acpi_match[] = {
  312. { "HISI0152", 0 },
  313. {}
  314. };
  315. MODULE_DEVICE_TABLE(acpi, mbigen_acpi_match);
  316. static struct platform_driver mbigen_platform_driver = {
  317. .driver = {
  318. .name = "Hisilicon MBIGEN-V2",
  319. .of_match_table = mbigen_of_match,
  320. .acpi_match_table = ACPI_PTR(mbigen_acpi_match),
  321. .suppress_bind_attrs = true,
  322. },
  323. .probe = mbigen_device_probe,
  324. };
  325. module_platform_driver(mbigen_platform_driver);
  326. MODULE_AUTHOR("Jun Ma <majun258@huawei.com>");
  327. MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>");
  328. MODULE_LICENSE("GPL");
  329. MODULE_DESCRIPTION("Hisilicon MBI Generator driver");