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

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