040-0004-PCI-iproc-Add-iProc-PCIe-MSI-support.patch 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. From 3bc2b2348835f6edd33c383a2fbcf15fe3dac3b2 Mon Sep 17 00:00:00 2001
  2. From: Ray Jui <rjui@broadcom.com>
  3. Date: Wed, 6 Jan 2016 18:04:35 -0600
  4. Subject: [PATCH 4/5] PCI: iproc: Add iProc PCIe MSI support
  5. Add PCIe MSI support for both PAXB and PAXC interfaces on all iProc-based
  6. platforms.
  7. The iProc PCIe MSI support deploys an event queue-based implementation.
  8. Each event queue is serviced by a GIC interrupt and can support up to 64
  9. MSI vectors. Host memory is allocated for the event queues, and each event
  10. queue consists of 64 word-sized entries. MSI data is written to the lower
  11. 16-bit of each entry, whereas the upper 16-bit of the entry is reserved for
  12. the controller for internal processing.
  13. Each event queue is tracked by a head pointer and tail pointer. Head
  14. pointer indicates the next entry in the event queue to be processed by
  15. the driver and is updated by the driver after processing is done.
  16. The controller uses the tail pointer as the next MSI data insertion
  17. point. The controller ensures MSI data is flushed to host memory before
  18. updating the tail pointer and then triggering the interrupt.
  19. MSI IRQ affinity is supported by evenly distributing the interrupts to each
  20. CPU core. MSI vector is moved from one GIC interrupt to another in order
  21. to steer to the target CPU.
  22. Therefore, the actual number of supported MSI vectors is:
  23. M * 64 / N
  24. where M denotes the number of GIC interrupts (event queues), and N denotes
  25. the number of CPU cores.
  26. This iProc event queue-based MSI support should not be used with newer
  27. platforms with integrated MSI support in the GIC (e.g., giv2m or
  28. gicv3-its).
  29. [bhelgaas: fold in Kconfig fixes from Arnd Bergmann <arnd@arndb.de>]
  30. Signed-off-by: Ray Jui <rjui@broadcom.com>
  31. Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
  32. Reviewed-by: Anup Patel <anup.patel@broadcom.com>
  33. Reviewed-by: Vikram Prakash <vikramp@broadcom.com>
  34. Reviewed-by: Scott Branden <sbranden@broadcom.com>
  35. Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
  36. ---
  37. drivers/pci/host/Kconfig | 10 +
  38. drivers/pci/host/Makefile | 1 +
  39. drivers/pci/host/pcie-iproc-bcma.c | 1 +
  40. drivers/pci/host/pcie-iproc-msi.c | 675 +++++++++++++++++++++++++++++++++
  41. drivers/pci/host/pcie-iproc-platform.c | 1 +
  42. drivers/pci/host/pcie-iproc.c | 26 ++
  43. drivers/pci/host/pcie-iproc.h | 23 +-
  44. 7 files changed, 735 insertions(+), 2 deletions(-)
  45. create mode 100644 drivers/pci/host/pcie-iproc-msi.c
  46. --- a/drivers/pci/host/Kconfig
  47. +++ b/drivers/pci/host/Kconfig
  48. @@ -133,5 +133,15 @@ config PCIE_IPROC_BCMA
  49. help
  50. Say Y here if you want to use the Broadcom iProc PCIe controller
  51. through the BCMA bus interface
  52. +
  53. +config PCIE_IPROC_MSI
  54. + bool "Broadcom iProc PCIe MSI support"
  55. + depends on PCIE_IPROC_PLATFORM || PCIE_IPROC_BCMA
  56. + depends on PCI_MSI
  57. + select PCI_MSI_IRQ_DOMAIN
  58. + default ARCH_BCM_IPROC
  59. + help
  60. + Say Y here if you want to enable MSI support for Broadcom's iProc
  61. + PCIe controller
  62. endmenu
  63. --- a/drivers/pci/host/Makefile
  64. +++ b/drivers/pci/host/Makefile
  65. @@ -14,5 +14,6 @@ obj-$(CONFIG_PCI_XGENE) += pci-xgene.o
  66. obj-$(CONFIG_PCI_LAYERSCAPE) += pci-layerscape.o
  67. obj-$(CONFIG_PCI_VERSATILE) += pci-versatile.o
  68. obj-$(CONFIG_PCIE_IPROC) += pcie-iproc.o
  69. +obj-$(CONFIG_PCIE_IPROC_MSI) += pcie-iproc-msi.o
  70. obj-$(CONFIG_PCIE_IPROC_PLATFORM) += pcie-iproc-platform.o
  71. obj-$(CONFIG_PCIE_IPROC_BCMA) += pcie-iproc-bcma.o
  72. --- a/drivers/pci/host/pcie-iproc-bcma.c
  73. +++ b/drivers/pci/host/pcie-iproc-bcma.c
  74. @@ -55,6 +55,7 @@ static int iproc_pcie_bcma_probe(struct
  75. bcma_set_drvdata(bdev, pcie);
  76. pcie->base = bdev->io_addr;
  77. + pcie->base_addr = bdev->addr;
  78. res_mem.start = bdev->addr_s[0];
  79. res_mem.end = bdev->addr_s[0] + SZ_128M - 1;
  80. --- /dev/null
  81. +++ b/drivers/pci/host/pcie-iproc-msi.c
  82. @@ -0,0 +1,675 @@
  83. +/*
  84. + * Copyright (C) 2015 Broadcom Corporation
  85. + *
  86. + * This program is free software; you can redistribute it and/or
  87. + * modify it under the terms of the GNU General Public License as
  88. + * published by the Free Software Foundation version 2.
  89. + *
  90. + * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  91. + * kind, whether express or implied; without even the implied warranty
  92. + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  93. + * GNU General Public License for more details.
  94. + */
  95. +
  96. +#include <linux/interrupt.h>
  97. +#include <linux/irqchip/chained_irq.h>
  98. +#include <linux/irqdomain.h>
  99. +#include <linux/msi.h>
  100. +#include <linux/of_irq.h>
  101. +#include <linux/of_pci.h>
  102. +#include <linux/pci.h>
  103. +
  104. +#include "pcie-iproc.h"
  105. +
  106. +#define IPROC_MSI_INTR_EN_SHIFT 11
  107. +#define IPROC_MSI_INTR_EN BIT(IPROC_MSI_INTR_EN_SHIFT)
  108. +#define IPROC_MSI_INT_N_EVENT_SHIFT 1
  109. +#define IPROC_MSI_INT_N_EVENT BIT(IPROC_MSI_INT_N_EVENT_SHIFT)
  110. +#define IPROC_MSI_EQ_EN_SHIFT 0
  111. +#define IPROC_MSI_EQ_EN BIT(IPROC_MSI_EQ_EN_SHIFT)
  112. +
  113. +#define IPROC_MSI_EQ_MASK 0x3f
  114. +
  115. +/* Max number of GIC interrupts */
  116. +#define NR_HW_IRQS 6
  117. +
  118. +/* Number of entries in each event queue */
  119. +#define EQ_LEN 64
  120. +
  121. +/* Size of each event queue memory region */
  122. +#define EQ_MEM_REGION_SIZE SZ_4K
  123. +
  124. +/* Size of each MSI address region */
  125. +#define MSI_MEM_REGION_SIZE SZ_4K
  126. +
  127. +enum iproc_msi_reg {
  128. + IPROC_MSI_EQ_PAGE = 0,
  129. + IPROC_MSI_EQ_PAGE_UPPER,
  130. + IPROC_MSI_PAGE,
  131. + IPROC_MSI_PAGE_UPPER,
  132. + IPROC_MSI_CTRL,
  133. + IPROC_MSI_EQ_HEAD,
  134. + IPROC_MSI_EQ_TAIL,
  135. + IPROC_MSI_INTS_EN,
  136. + IPROC_MSI_REG_SIZE,
  137. +};
  138. +
  139. +struct iproc_msi;
  140. +
  141. +/**
  142. + * iProc MSI group
  143. + *
  144. + * One MSI group is allocated per GIC interrupt, serviced by one iProc MSI
  145. + * event queue.
  146. + *
  147. + * @msi: pointer to iProc MSI data
  148. + * @gic_irq: GIC interrupt
  149. + * @eq: Event queue number
  150. + */
  151. +struct iproc_msi_grp {
  152. + struct iproc_msi *msi;
  153. + int gic_irq;
  154. + unsigned int eq;
  155. +};
  156. +
  157. +/**
  158. + * iProc event queue based MSI
  159. + *
  160. + * Only meant to be used on platforms without MSI support integrated into the
  161. + * GIC.
  162. + *
  163. + * @pcie: pointer to iProc PCIe data
  164. + * @reg_offsets: MSI register offsets
  165. + * @grps: MSI groups
  166. + * @nr_irqs: number of total interrupts connected to GIC
  167. + * @nr_cpus: number of toal CPUs
  168. + * @has_inten_reg: indicates the MSI interrupt enable register needs to be
  169. + * set explicitly (required for some legacy platforms)
  170. + * @bitmap: MSI vector bitmap
  171. + * @bitmap_lock: lock to protect access to the MSI bitmap
  172. + * @nr_msi_vecs: total number of MSI vectors
  173. + * @inner_domain: inner IRQ domain
  174. + * @msi_domain: MSI IRQ domain
  175. + * @nr_eq_region: required number of 4K aligned memory region for MSI event
  176. + * queues
  177. + * @nr_msi_region: required number of 4K aligned address region for MSI posted
  178. + * writes
  179. + * @eq_cpu: pointer to allocated memory region for MSI event queues
  180. + * @eq_dma: DMA address of MSI event queues
  181. + * @msi_addr: MSI address
  182. + */
  183. +struct iproc_msi {
  184. + struct iproc_pcie *pcie;
  185. + const u16 (*reg_offsets)[IPROC_MSI_REG_SIZE];
  186. + struct iproc_msi_grp *grps;
  187. + int nr_irqs;
  188. + int nr_cpus;
  189. + bool has_inten_reg;
  190. + unsigned long *bitmap;
  191. + struct mutex bitmap_lock;
  192. + unsigned int nr_msi_vecs;
  193. + struct irq_domain *inner_domain;
  194. + struct irq_domain *msi_domain;
  195. + unsigned int nr_eq_region;
  196. + unsigned int nr_msi_region;
  197. + void *eq_cpu;
  198. + dma_addr_t eq_dma;
  199. + phys_addr_t msi_addr;
  200. +};
  201. +
  202. +static const u16 iproc_msi_reg_paxb[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = {
  203. + { 0x200, 0x2c0, 0x204, 0x2c4, 0x210, 0x250, 0x254, 0x208 },
  204. + { 0x200, 0x2c0, 0x204, 0x2c4, 0x214, 0x258, 0x25c, 0x208 },
  205. + { 0x200, 0x2c0, 0x204, 0x2c4, 0x218, 0x260, 0x264, 0x208 },
  206. + { 0x200, 0x2c0, 0x204, 0x2c4, 0x21c, 0x268, 0x26c, 0x208 },
  207. + { 0x200, 0x2c0, 0x204, 0x2c4, 0x220, 0x270, 0x274, 0x208 },
  208. + { 0x200, 0x2c0, 0x204, 0x2c4, 0x224, 0x278, 0x27c, 0x208 },
  209. +};
  210. +
  211. +static const u16 iproc_msi_reg_paxc[NR_HW_IRQS][IPROC_MSI_REG_SIZE] = {
  212. + { 0xc00, 0xc04, 0xc08, 0xc0c, 0xc40, 0xc50, 0xc60 },
  213. + { 0xc10, 0xc14, 0xc18, 0xc1c, 0xc44, 0xc54, 0xc64 },
  214. + { 0xc20, 0xc24, 0xc28, 0xc2c, 0xc48, 0xc58, 0xc68 },
  215. + { 0xc30, 0xc34, 0xc38, 0xc3c, 0xc4c, 0xc5c, 0xc6c },
  216. +};
  217. +
  218. +static inline u32 iproc_msi_read_reg(struct iproc_msi *msi,
  219. + enum iproc_msi_reg reg,
  220. + unsigned int eq)
  221. +{
  222. + struct iproc_pcie *pcie = msi->pcie;
  223. +
  224. + return readl_relaxed(pcie->base + msi->reg_offsets[eq][reg]);
  225. +}
  226. +
  227. +static inline void iproc_msi_write_reg(struct iproc_msi *msi,
  228. + enum iproc_msi_reg reg,
  229. + int eq, u32 val)
  230. +{
  231. + struct iproc_pcie *pcie = msi->pcie;
  232. +
  233. + writel_relaxed(val, pcie->base + msi->reg_offsets[eq][reg]);
  234. +}
  235. +
  236. +static inline u32 hwirq_to_group(struct iproc_msi *msi, unsigned long hwirq)
  237. +{
  238. + return (hwirq % msi->nr_irqs);
  239. +}
  240. +
  241. +static inline unsigned int iproc_msi_addr_offset(struct iproc_msi *msi,
  242. + unsigned long hwirq)
  243. +{
  244. + if (msi->nr_msi_region > 1)
  245. + return hwirq_to_group(msi, hwirq) * MSI_MEM_REGION_SIZE;
  246. + else
  247. + return hwirq_to_group(msi, hwirq) * sizeof(u32);
  248. +}
  249. +
  250. +static inline unsigned int iproc_msi_eq_offset(struct iproc_msi *msi, u32 eq)
  251. +{
  252. + if (msi->nr_eq_region > 1)
  253. + return eq * EQ_MEM_REGION_SIZE;
  254. + else
  255. + return eq * EQ_LEN * sizeof(u32);
  256. +}
  257. +
  258. +static struct irq_chip iproc_msi_irq_chip = {
  259. + .name = "iProc-MSI",
  260. +};
  261. +
  262. +static struct msi_domain_info iproc_msi_domain_info = {
  263. + .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  264. + MSI_FLAG_PCI_MSIX,
  265. + .chip = &iproc_msi_irq_chip,
  266. +};
  267. +
  268. +/*
  269. + * In iProc PCIe core, each MSI group is serviced by a GIC interrupt and a
  270. + * dedicated event queue. Each MSI group can support up to 64 MSI vectors.
  271. + *
  272. + * The number of MSI groups varies between different iProc SoCs. The total
  273. + * number of CPU cores also varies. To support MSI IRQ affinity, we
  274. + * distribute GIC interrupts across all available CPUs. MSI vector is moved
  275. + * from one GIC interrupt to another to steer to the target CPU.
  276. + *
  277. + * Assuming:
  278. + * - the number of MSI groups is M
  279. + * - the number of CPU cores is N
  280. + * - M is always a multiple of N
  281. + *
  282. + * Total number of raw MSI vectors = M * 64
  283. + * Total number of supported MSI vectors = (M * 64) / N
  284. + */
  285. +static inline int hwirq_to_cpu(struct iproc_msi *msi, unsigned long hwirq)
  286. +{
  287. + return (hwirq % msi->nr_cpus);
  288. +}
  289. +
  290. +static inline unsigned long hwirq_to_canonical_hwirq(struct iproc_msi *msi,
  291. + unsigned long hwirq)
  292. +{
  293. + return (hwirq - hwirq_to_cpu(msi, hwirq));
  294. +}
  295. +
  296. +static int iproc_msi_irq_set_affinity(struct irq_data *data,
  297. + const struct cpumask *mask, bool force)
  298. +{
  299. + struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
  300. + int target_cpu = cpumask_first(mask);
  301. + int curr_cpu;
  302. +
  303. + curr_cpu = hwirq_to_cpu(msi, data->hwirq);
  304. + if (curr_cpu == target_cpu)
  305. + return IRQ_SET_MASK_OK_DONE;
  306. +
  307. + /* steer MSI to the target CPU */
  308. + data->hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq) + target_cpu;
  309. +
  310. + return IRQ_SET_MASK_OK;
  311. +}
  312. +
  313. +static void iproc_msi_irq_compose_msi_msg(struct irq_data *data,
  314. + struct msi_msg *msg)
  315. +{
  316. + struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
  317. + dma_addr_t addr;
  318. +
  319. + addr = msi->msi_addr + iproc_msi_addr_offset(msi, data->hwirq);
  320. + msg->address_lo = lower_32_bits(addr);
  321. + msg->address_hi = upper_32_bits(addr);
  322. + msg->data = data->hwirq;
  323. +}
  324. +
  325. +static struct irq_chip iproc_msi_bottom_irq_chip = {
  326. + .name = "MSI",
  327. + .irq_set_affinity = iproc_msi_irq_set_affinity,
  328. + .irq_compose_msi_msg = iproc_msi_irq_compose_msi_msg,
  329. +};
  330. +
  331. +static int iproc_msi_irq_domain_alloc(struct irq_domain *domain,
  332. + unsigned int virq, unsigned int nr_irqs,
  333. + void *args)
  334. +{
  335. + struct iproc_msi *msi = domain->host_data;
  336. + int hwirq;
  337. +
  338. + mutex_lock(&msi->bitmap_lock);
  339. +
  340. + /* Allocate 'nr_cpus' number of MSI vectors each time */
  341. + hwirq = bitmap_find_next_zero_area(msi->bitmap, msi->nr_msi_vecs, 0,
  342. + msi->nr_cpus, 0);
  343. + if (hwirq < msi->nr_msi_vecs) {
  344. + bitmap_set(msi->bitmap, hwirq, msi->nr_cpus);
  345. + } else {
  346. + mutex_unlock(&msi->bitmap_lock);
  347. + return -ENOSPC;
  348. + }
  349. +
  350. + mutex_unlock(&msi->bitmap_lock);
  351. +
  352. + irq_domain_set_info(domain, virq, hwirq, &iproc_msi_bottom_irq_chip,
  353. + domain->host_data, handle_simple_irq, NULL, NULL);
  354. +
  355. + return 0;
  356. +}
  357. +
  358. +static void iproc_msi_irq_domain_free(struct irq_domain *domain,
  359. + unsigned int virq, unsigned int nr_irqs)
  360. +{
  361. + struct irq_data *data = irq_domain_get_irq_data(domain, virq);
  362. + struct iproc_msi *msi = irq_data_get_irq_chip_data(data);
  363. + unsigned int hwirq;
  364. +
  365. + mutex_lock(&msi->bitmap_lock);
  366. +
  367. + hwirq = hwirq_to_canonical_hwirq(msi, data->hwirq);
  368. + bitmap_clear(msi->bitmap, hwirq, msi->nr_cpus);
  369. +
  370. + mutex_unlock(&msi->bitmap_lock);
  371. +
  372. + irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  373. +}
  374. +
  375. +static const struct irq_domain_ops msi_domain_ops = {
  376. + .alloc = iproc_msi_irq_domain_alloc,
  377. + .free = iproc_msi_irq_domain_free,
  378. +};
  379. +
  380. +static inline u32 decode_msi_hwirq(struct iproc_msi *msi, u32 eq, u32 head)
  381. +{
  382. + u32 *msg, hwirq;
  383. + unsigned int offs;
  384. +
  385. + offs = iproc_msi_eq_offset(msi, eq) + head * sizeof(u32);
  386. + msg = (u32 *)(msi->eq_cpu + offs);
  387. + hwirq = *msg & IPROC_MSI_EQ_MASK;
  388. +
  389. + /*
  390. + * Since we have multiple hwirq mapped to a single MSI vector,
  391. + * now we need to derive the hwirq at CPU0. It can then be used to
  392. + * mapped back to virq.
  393. + */
  394. + return hwirq_to_canonical_hwirq(msi, hwirq);
  395. +}
  396. +
  397. +static void iproc_msi_handler(struct irq_desc *desc)
  398. +{
  399. + struct irq_chip *chip = irq_desc_get_chip(desc);
  400. + struct iproc_msi_grp *grp;
  401. + struct iproc_msi *msi;
  402. + struct iproc_pcie *pcie;
  403. + u32 eq, head, tail, nr_events;
  404. + unsigned long hwirq;
  405. + int virq;
  406. +
  407. + chained_irq_enter(chip, desc);
  408. +
  409. + grp = irq_desc_get_handler_data(desc);
  410. + msi = grp->msi;
  411. + pcie = msi->pcie;
  412. + eq = grp->eq;
  413. +
  414. + /*
  415. + * iProc MSI event queue is tracked by head and tail pointers. Head
  416. + * pointer indicates the next entry (MSI data) to be consumed by SW in
  417. + * the queue and needs to be updated by SW. iProc MSI core uses the
  418. + * tail pointer as the next data insertion point.
  419. + *
  420. + * Entries between head and tail pointers contain valid MSI data. MSI
  421. + * data is guaranteed to be in the event queue memory before the tail
  422. + * pointer is updated by the iProc MSI core.
  423. + */
  424. + head = iproc_msi_read_reg(msi, IPROC_MSI_EQ_HEAD,
  425. + eq) & IPROC_MSI_EQ_MASK;
  426. + do {
  427. + tail = iproc_msi_read_reg(msi, IPROC_MSI_EQ_TAIL,
  428. + eq) & IPROC_MSI_EQ_MASK;
  429. +
  430. + /*
  431. + * Figure out total number of events (MSI data) to be
  432. + * processed.
  433. + */
  434. + nr_events = (tail < head) ?
  435. + (EQ_LEN - (head - tail)) : (tail - head);
  436. + if (!nr_events)
  437. + break;
  438. +
  439. + /* process all outstanding events */
  440. + while (nr_events--) {
  441. + hwirq = decode_msi_hwirq(msi, eq, head);
  442. + virq = irq_find_mapping(msi->inner_domain, hwirq);
  443. + generic_handle_irq(virq);
  444. +
  445. + head++;
  446. + head %= EQ_LEN;
  447. + }
  448. +
  449. + /*
  450. + * Now all outstanding events have been processed. Update the
  451. + * head pointer.
  452. + */
  453. + iproc_msi_write_reg(msi, IPROC_MSI_EQ_HEAD, eq, head);
  454. +
  455. + /*
  456. + * Now go read the tail pointer again to see if there are new
  457. + * oustanding events that came in during the above window.
  458. + */
  459. + } while (true);
  460. +
  461. + chained_irq_exit(chip, desc);
  462. +}
  463. +
  464. +static void iproc_msi_enable(struct iproc_msi *msi)
  465. +{
  466. + int i, eq;
  467. + u32 val;
  468. +
  469. + /* Program memory region for each event queue */
  470. + for (i = 0; i < msi->nr_eq_region; i++) {
  471. + dma_addr_t addr = msi->eq_dma + (i * EQ_MEM_REGION_SIZE);
  472. +
  473. + iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE, i,
  474. + lower_32_bits(addr));
  475. + iproc_msi_write_reg(msi, IPROC_MSI_EQ_PAGE_UPPER, i,
  476. + upper_32_bits(addr));
  477. + }
  478. +
  479. + /* Program address region for MSI posted writes */
  480. + for (i = 0; i < msi->nr_msi_region; i++) {
  481. + phys_addr_t addr = msi->msi_addr + (i * MSI_MEM_REGION_SIZE);
  482. +
  483. + iproc_msi_write_reg(msi, IPROC_MSI_PAGE, i,
  484. + lower_32_bits(addr));
  485. + iproc_msi_write_reg(msi, IPROC_MSI_PAGE_UPPER, i,
  486. + upper_32_bits(addr));
  487. + }
  488. +
  489. + for (eq = 0; eq < msi->nr_irqs; eq++) {
  490. + /* Enable MSI event queue */
  491. + val = IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
  492. + IPROC_MSI_EQ_EN;
  493. + iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
  494. +
  495. + /*
  496. + * Some legacy platforms require the MSI interrupt enable
  497. + * register to be set explicitly.
  498. + */
  499. + if (msi->has_inten_reg) {
  500. + val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
  501. + val |= BIT(eq);
  502. + iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
  503. + }
  504. + }
  505. +}
  506. +
  507. +static void iproc_msi_disable(struct iproc_msi *msi)
  508. +{
  509. + u32 eq, val;
  510. +
  511. + for (eq = 0; eq < msi->nr_irqs; eq++) {
  512. + if (msi->has_inten_reg) {
  513. + val = iproc_msi_read_reg(msi, IPROC_MSI_INTS_EN, eq);
  514. + val &= ~BIT(eq);
  515. + iproc_msi_write_reg(msi, IPROC_MSI_INTS_EN, eq, val);
  516. + }
  517. +
  518. + val = iproc_msi_read_reg(msi, IPROC_MSI_CTRL, eq);
  519. + val &= ~(IPROC_MSI_INTR_EN | IPROC_MSI_INT_N_EVENT |
  520. + IPROC_MSI_EQ_EN);
  521. + iproc_msi_write_reg(msi, IPROC_MSI_CTRL, eq, val);
  522. + }
  523. +}
  524. +
  525. +static int iproc_msi_alloc_domains(struct device_node *node,
  526. + struct iproc_msi *msi)
  527. +{
  528. + msi->inner_domain = irq_domain_add_linear(NULL, msi->nr_msi_vecs,
  529. + &msi_domain_ops, msi);
  530. + if (!msi->inner_domain)
  531. + return -ENOMEM;
  532. +
  533. + msi->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(node),
  534. + &iproc_msi_domain_info,
  535. + msi->inner_domain);
  536. + if (!msi->msi_domain) {
  537. + irq_domain_remove(msi->inner_domain);
  538. + return -ENOMEM;
  539. + }
  540. +
  541. + return 0;
  542. +}
  543. +
  544. +static void iproc_msi_free_domains(struct iproc_msi *msi)
  545. +{
  546. + if (msi->msi_domain)
  547. + irq_domain_remove(msi->msi_domain);
  548. +
  549. + if (msi->inner_domain)
  550. + irq_domain_remove(msi->inner_domain);
  551. +}
  552. +
  553. +static void iproc_msi_irq_free(struct iproc_msi *msi, unsigned int cpu)
  554. +{
  555. + int i;
  556. +
  557. + for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
  558. + irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
  559. + NULL, NULL);
  560. + }
  561. +}
  562. +
  563. +static int iproc_msi_irq_setup(struct iproc_msi *msi, unsigned int cpu)
  564. +{
  565. + int i, ret;
  566. + cpumask_var_t mask;
  567. + struct iproc_pcie *pcie = msi->pcie;
  568. +
  569. + for (i = cpu; i < msi->nr_irqs; i += msi->nr_cpus) {
  570. + irq_set_chained_handler_and_data(msi->grps[i].gic_irq,
  571. + iproc_msi_handler,
  572. + &msi->grps[i]);
  573. + /* Dedicate GIC interrupt to each CPU core */
  574. + if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  575. + cpumask_clear(mask);
  576. + cpumask_set_cpu(cpu, mask);
  577. + ret = irq_set_affinity(msi->grps[i].gic_irq, mask);
  578. + if (ret)
  579. + dev_err(pcie->dev,
  580. + "failed to set affinity for IRQ%d\n",
  581. + msi->grps[i].gic_irq);
  582. + free_cpumask_var(mask);
  583. + } else {
  584. + dev_err(pcie->dev, "failed to alloc CPU mask\n");
  585. + ret = -EINVAL;
  586. + }
  587. +
  588. + if (ret) {
  589. + /* Free all configured/unconfigured IRQs */
  590. + iproc_msi_irq_free(msi, cpu);
  591. + return ret;
  592. + }
  593. + }
  594. +
  595. + return 0;
  596. +}
  597. +
  598. +int iproc_msi_init(struct iproc_pcie *pcie, struct device_node *node)
  599. +{
  600. + struct iproc_msi *msi;
  601. + int i, ret;
  602. + unsigned int cpu;
  603. +
  604. + if (!of_device_is_compatible(node, "brcm,iproc-msi"))
  605. + return -ENODEV;
  606. +
  607. + if (!of_find_property(node, "msi-controller", NULL))
  608. + return -ENODEV;
  609. +
  610. + if (pcie->msi)
  611. + return -EBUSY;
  612. +
  613. + msi = devm_kzalloc(pcie->dev, sizeof(*msi), GFP_KERNEL);
  614. + if (!msi)
  615. + return -ENOMEM;
  616. +
  617. + msi->pcie = pcie;
  618. + pcie->msi = msi;
  619. + msi->msi_addr = pcie->base_addr;
  620. + mutex_init(&msi->bitmap_lock);
  621. + msi->nr_cpus = num_possible_cpus();
  622. +
  623. + msi->nr_irqs = of_irq_count(node);
  624. + if (!msi->nr_irqs) {
  625. + dev_err(pcie->dev, "found no MSI GIC interrupt\n");
  626. + return -ENODEV;
  627. + }
  628. +
  629. + if (msi->nr_irqs > NR_HW_IRQS) {
  630. + dev_warn(pcie->dev, "too many MSI GIC interrupts defined %d\n",
  631. + msi->nr_irqs);
  632. + msi->nr_irqs = NR_HW_IRQS;
  633. + }
  634. +
  635. + if (msi->nr_irqs < msi->nr_cpus) {
  636. + dev_err(pcie->dev,
  637. + "not enough GIC interrupts for MSI affinity\n");
  638. + return -EINVAL;
  639. + }
  640. +
  641. + if (msi->nr_irqs % msi->nr_cpus != 0) {
  642. + msi->nr_irqs -= msi->nr_irqs % msi->nr_cpus;
  643. + dev_warn(pcie->dev, "Reducing number of interrupts to %d\n",
  644. + msi->nr_irqs);
  645. + }
  646. +
  647. + switch (pcie->type) {
  648. + case IPROC_PCIE_PAXB:
  649. + msi->reg_offsets = iproc_msi_reg_paxb;
  650. + msi->nr_eq_region = 1;
  651. + msi->nr_msi_region = 1;
  652. + break;
  653. + case IPROC_PCIE_PAXC:
  654. + msi->reg_offsets = iproc_msi_reg_paxc;
  655. + msi->nr_eq_region = msi->nr_irqs;
  656. + msi->nr_msi_region = msi->nr_irqs;
  657. + break;
  658. + default:
  659. + dev_err(pcie->dev, "incompatible iProc PCIe interface\n");
  660. + return -EINVAL;
  661. + }
  662. +
  663. + if (of_find_property(node, "brcm,pcie-msi-inten", NULL))
  664. + msi->has_inten_reg = true;
  665. +
  666. + msi->nr_msi_vecs = msi->nr_irqs * EQ_LEN;
  667. + msi->bitmap = devm_kcalloc(pcie->dev, BITS_TO_LONGS(msi->nr_msi_vecs),
  668. + sizeof(*msi->bitmap), GFP_KERNEL);
  669. + if (!msi->bitmap)
  670. + return -ENOMEM;
  671. +
  672. + msi->grps = devm_kcalloc(pcie->dev, msi->nr_irqs, sizeof(*msi->grps),
  673. + GFP_KERNEL);
  674. + if (!msi->grps)
  675. + return -ENOMEM;
  676. +
  677. + for (i = 0; i < msi->nr_irqs; i++) {
  678. + unsigned int irq = irq_of_parse_and_map(node, i);
  679. +
  680. + if (!irq) {
  681. + dev_err(pcie->dev, "unable to parse/map interrupt\n");
  682. + ret = -ENODEV;
  683. + goto free_irqs;
  684. + }
  685. + msi->grps[i].gic_irq = irq;
  686. + msi->grps[i].msi = msi;
  687. + msi->grps[i].eq = i;
  688. + }
  689. +
  690. + /* Reserve memory for event queue and make sure memories are zeroed */
  691. + msi->eq_cpu = dma_zalloc_coherent(pcie->dev,
  692. + msi->nr_eq_region * EQ_MEM_REGION_SIZE,
  693. + &msi->eq_dma, GFP_KERNEL);
  694. + if (!msi->eq_cpu) {
  695. + ret = -ENOMEM;
  696. + goto free_irqs;
  697. + }
  698. +
  699. + ret = iproc_msi_alloc_domains(node, msi);
  700. + if (ret) {
  701. + dev_err(pcie->dev, "failed to create MSI domains\n");
  702. + goto free_eq_dma;
  703. + }
  704. +
  705. + for_each_online_cpu(cpu) {
  706. + ret = iproc_msi_irq_setup(msi, cpu);
  707. + if (ret)
  708. + goto free_msi_irq;
  709. + }
  710. +
  711. + iproc_msi_enable(msi);
  712. +
  713. + return 0;
  714. +
  715. +free_msi_irq:
  716. + for_each_online_cpu(cpu)
  717. + iproc_msi_irq_free(msi, cpu);
  718. + iproc_msi_free_domains(msi);
  719. +
  720. +free_eq_dma:
  721. + dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
  722. + msi->eq_cpu, msi->eq_dma);
  723. +
  724. +free_irqs:
  725. + for (i = 0; i < msi->nr_irqs; i++) {
  726. + if (msi->grps[i].gic_irq)
  727. + irq_dispose_mapping(msi->grps[i].gic_irq);
  728. + }
  729. + pcie->msi = NULL;
  730. + return ret;
  731. +}
  732. +EXPORT_SYMBOL(iproc_msi_init);
  733. +
  734. +void iproc_msi_exit(struct iproc_pcie *pcie)
  735. +{
  736. + struct iproc_msi *msi = pcie->msi;
  737. + unsigned int i, cpu;
  738. +
  739. + if (!msi)
  740. + return;
  741. +
  742. + iproc_msi_disable(msi);
  743. +
  744. + for_each_online_cpu(cpu)
  745. + iproc_msi_irq_free(msi, cpu);
  746. +
  747. + iproc_msi_free_domains(msi);
  748. +
  749. + dma_free_coherent(pcie->dev, msi->nr_eq_region * EQ_MEM_REGION_SIZE,
  750. + msi->eq_cpu, msi->eq_dma);
  751. +
  752. + for (i = 0; i < msi->nr_irqs; i++) {
  753. + if (msi->grps[i].gic_irq)
  754. + irq_dispose_mapping(msi->grps[i].gic_irq);
  755. + }
  756. +}
  757. +EXPORT_SYMBOL(iproc_msi_exit);
  758. --- a/drivers/pci/host/pcie-iproc-platform.c
  759. +++ b/drivers/pci/host/pcie-iproc-platform.c
  760. @@ -71,6 +71,7 @@ static int iproc_pcie_pltfm_probe(struct
  761. dev_err(pcie->dev, "unable to map controller registers\n");
  762. return -ENOMEM;
  763. }
  764. + pcie->base_addr = reg.start;
  765. if (of_property_read_bool(np, "brcm,pcie-ob")) {
  766. u32 val;
  767. --- a/drivers/pci/host/pcie-iproc.c
  768. +++ b/drivers/pci/host/pcie-iproc.c
  769. @@ -440,6 +440,26 @@ static int iproc_pcie_map_ranges(struct
  770. return 0;
  771. }
  772. +static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
  773. +{
  774. + struct device_node *msi_node;
  775. +
  776. + msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
  777. + if (!msi_node)
  778. + return -ENODEV;
  779. +
  780. + /*
  781. + * If another MSI controller is being used, the call below should fail
  782. + * but that is okay
  783. + */
  784. + return iproc_msi_init(pcie, msi_node);
  785. +}
  786. +
  787. +static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
  788. +{
  789. + iproc_msi_exit(pcie);
  790. +}
  791. +
  792. int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
  793. {
  794. int ret;
  795. @@ -506,6 +526,10 @@ int iproc_pcie_setup(struct iproc_pcie *
  796. iproc_pcie_enable(pcie);
  797. + if (IS_ENABLED(CONFIG_PCI_MSI))
  798. + if (iproc_pcie_msi_enable(pcie))
  799. + dev_info(pcie->dev, "not using iProc MSI\n");
  800. +
  801. pci_scan_child_bus(bus);
  802. pci_assign_unassigned_bus_resources(bus);
  803. pci_fixup_irqs(pci_common_swizzle, pcie->map_irq);
  804. @@ -530,6 +554,8 @@ int iproc_pcie_remove(struct iproc_pcie
  805. pci_stop_root_bus(pcie->root_bus);
  806. pci_remove_root_bus(pcie->root_bus);
  807. + iproc_pcie_msi_disable(pcie);
  808. +
  809. phy_power_off(pcie->phy);
  810. phy_exit(pcie->phy);
  811. --- a/drivers/pci/host/pcie-iproc.h
  812. +++ b/drivers/pci/host/pcie-iproc.h
  813. @@ -41,6 +41,8 @@ struct iproc_pcie_ob {
  814. resource_size_t window_size;
  815. };
  816. +struct iproc_msi;
  817. +
  818. /**
  819. * iProc PCIe device
  820. *
  821. @@ -48,19 +50,21 @@ struct iproc_pcie_ob {
  822. * @type: iProc PCIe interface type
  823. * @reg_offsets: register offsets
  824. * @base: PCIe host controller I/O register base
  825. + * @base_addr: PCIe host controller register base physical address
  826. * @sysdata: Per PCI controller data (ARM-specific)
  827. * @root_bus: pointer to root bus
  828. * @phy: optional PHY device that controls the Serdes
  829. - * @irqs: interrupt IDs
  830. * @map_irq: function callback to map interrupts
  831. - * @need_ob_cfg: indidates SW needs to configure the outbound mapping window
  832. + * @need_ob_cfg: indicates SW needs to configure the outbound mapping window
  833. * @ob: outbound mapping parameters
  834. + * @msi: MSI data
  835. */
  836. struct iproc_pcie {
  837. struct device *dev;
  838. enum iproc_pcie_type type;
  839. const u16 *reg_offsets;
  840. void __iomem *base;
  841. + phys_addr_t base_addr;
  842. #ifdef CONFIG_ARM
  843. struct pci_sys_data sysdata;
  844. #endif
  845. @@ -69,9 +73,24 @@ struct iproc_pcie {
  846. int (*map_irq)(const struct pci_dev *, u8, u8);
  847. bool need_ob_cfg;
  848. struct iproc_pcie_ob ob;
  849. + struct iproc_msi *msi;
  850. };
  851. int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res);
  852. int iproc_pcie_remove(struct iproc_pcie *pcie);
  853. +#ifdef CONFIG_PCIE_IPROC_MSI
  854. +int iproc_msi_init(struct iproc_pcie *pcie, struct device_node *node);
  855. +void iproc_msi_exit(struct iproc_pcie *pcie);
  856. +#else
  857. +static inline int iproc_msi_init(struct iproc_pcie *pcie,
  858. + struct device_node *node)
  859. +{
  860. + return -ENODEV;
  861. +}
  862. +static inline void iproc_msi_exit(struct iproc_pcie *pcie)
  863. +{
  864. +}
  865. +#endif
  866. +
  867. #endif /* _PCIE_IPROC_H */