pcie-apple.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host bridge driver for Apple system-on-chips.
  4. *
  5. * The HW is ECAM compliant, so once the controller is initialized,
  6. * the driver mostly deals MSI mapping and handling of per-port
  7. * interrupts (INTx, management and error signals).
  8. *
  9. * Initialization requires enabling power and clocks, along with a
  10. * number of register pokes.
  11. *
  12. * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io>
  13. * Copyright (C) 2021 Google LLC
  14. * Copyright (C) 2021 Corellium LLC
  15. * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
  16. *
  17. * Author: Alyssa Rosenzweig <alyssa@rosenzweig.io>
  18. * Author: Marc Zyngier <maz@kernel.org>
  19. */
  20. #include <linux/bitfield.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/kernel.h>
  23. #include <linux/iopoll.h>
  24. #include <linux/irqchip/chained_irq.h>
  25. #include <linux/irqchip/irq-msi-lib.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/list.h>
  28. #include <linux/module.h>
  29. #include <linux/msi.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/pci-ecam.h>
  32. #include "pci-host-common.h"
  33. /* T8103 (original M1) and related SoCs */
  34. #define CORE_RC_PHYIF_CTL 0x00024
  35. #define CORE_RC_PHYIF_CTL_RUN BIT(0)
  36. #define CORE_RC_PHYIF_STAT 0x00028
  37. #define CORE_RC_PHYIF_STAT_REFCLK BIT(4)
  38. #define CORE_RC_CTL 0x00050
  39. #define CORE_RC_CTL_RUN BIT(0)
  40. #define CORE_RC_STAT 0x00058
  41. #define CORE_RC_STAT_READY BIT(0)
  42. #define CORE_FABRIC_STAT 0x04000
  43. #define CORE_FABRIC_STAT_MASK 0x001F001F
  44. #define CORE_PHY_DEFAULT_BASE(port) (0x84000 + 0x4000 * (port))
  45. #define PHY_LANE_CFG 0x00000
  46. #define PHY_LANE_CFG_REFCLK0REQ BIT(0)
  47. #define PHY_LANE_CFG_REFCLK1REQ BIT(1)
  48. #define PHY_LANE_CFG_REFCLK0ACK BIT(2)
  49. #define PHY_LANE_CFG_REFCLK1ACK BIT(3)
  50. #define PHY_LANE_CFG_REFCLKEN (BIT(9) | BIT(10))
  51. #define PHY_LANE_CFG_REFCLKCGEN (BIT(30) | BIT(31))
  52. #define PHY_LANE_CTL 0x00004
  53. #define PHY_LANE_CTL_CFGACC BIT(15)
  54. #define PORT_LTSSMCTL 0x00080
  55. #define PORT_LTSSMCTL_START BIT(0)
  56. #define PORT_INTSTAT 0x00100
  57. #define PORT_INT_TUNNEL_ERR 31
  58. #define PORT_INT_CPL_TIMEOUT 23
  59. #define PORT_INT_RID2SID_MAPERR 22
  60. #define PORT_INT_CPL_ABORT 21
  61. #define PORT_INT_MSI_BAD_DATA 19
  62. #define PORT_INT_MSI_ERR 18
  63. #define PORT_INT_REQADDR_GT32 17
  64. #define PORT_INT_AF_TIMEOUT 15
  65. #define PORT_INT_LINK_DOWN 14
  66. #define PORT_INT_LINK_UP 12
  67. #define PORT_INT_LINK_BWMGMT 11
  68. #define PORT_INT_AER_MASK (15 << 4)
  69. #define PORT_INT_PORT_ERR 4
  70. #define PORT_INT_INTx(i) i
  71. #define PORT_INT_INTx_MASK 15
  72. #define PORT_INTMSK 0x00104
  73. #define PORT_INTMSKSET 0x00108
  74. #define PORT_INTMSKCLR 0x0010c
  75. #define PORT_MSICFG 0x00124
  76. #define PORT_MSICFG_EN BIT(0)
  77. #define PORT_MSICFG_L2MSINUM_SHIFT 4
  78. #define PORT_MSIBASE 0x00128
  79. #define PORT_MSIBASE_1_SHIFT 16
  80. #define PORT_MSIADDR 0x00168
  81. #define PORT_LINKSTS 0x00208
  82. #define PORT_LINKSTS_UP BIT(0)
  83. #define PORT_LINKSTS_BUSY BIT(2)
  84. #define PORT_LINKCMDSTS 0x00210
  85. #define PORT_OUTS_NPREQS 0x00284
  86. #define PORT_OUTS_NPREQS_REQ BIT(24)
  87. #define PORT_OUTS_NPREQS_CPL BIT(16)
  88. #define PORT_RXWR_FIFO 0x00288
  89. #define PORT_RXWR_FIFO_HDR GENMASK(15, 10)
  90. #define PORT_RXWR_FIFO_DATA GENMASK(9, 0)
  91. #define PORT_RXRD_FIFO 0x0028C
  92. #define PORT_RXRD_FIFO_REQ GENMASK(6, 0)
  93. #define PORT_OUTS_CPLS 0x00290
  94. #define PORT_OUTS_CPLS_SHRD GENMASK(14, 8)
  95. #define PORT_OUTS_CPLS_WAIT GENMASK(6, 0)
  96. #define PORT_APPCLK 0x00800
  97. #define PORT_APPCLK_EN BIT(0)
  98. #define PORT_APPCLK_CGDIS BIT(8)
  99. #define PORT_STATUS 0x00804
  100. #define PORT_STATUS_READY BIT(0)
  101. #define PORT_REFCLK 0x00810
  102. #define PORT_REFCLK_EN BIT(0)
  103. #define PORT_REFCLK_CGDIS BIT(8)
  104. #define PORT_PERST 0x00814
  105. #define PORT_PERST_OFF BIT(0)
  106. #define PORT_RID2SID 0x00828
  107. #define PORT_RID2SID_VALID BIT(31)
  108. #define PORT_RID2SID_SID_SHIFT 16
  109. #define PORT_RID2SID_BUS_SHIFT 8
  110. #define PORT_RID2SID_DEV_SHIFT 3
  111. #define PORT_RID2SID_FUNC_SHIFT 0
  112. #define PORT_OUTS_PREQS_HDR 0x00980
  113. #define PORT_OUTS_PREQS_HDR_MASK GENMASK(9, 0)
  114. #define PORT_OUTS_PREQS_DATA 0x00984
  115. #define PORT_OUTS_PREQS_DATA_MASK GENMASK(15, 0)
  116. #define PORT_TUNCTRL 0x00988
  117. #define PORT_TUNCTRL_PERST_ON BIT(0)
  118. #define PORT_TUNCTRL_PERST_ACK_REQ BIT(1)
  119. #define PORT_TUNSTAT 0x0098c
  120. #define PORT_TUNSTAT_PERST_ON BIT(0)
  121. #define PORT_TUNSTAT_PERST_ACK_PEND BIT(1)
  122. #define PORT_PREFMEM_ENABLE 0x00994
  123. /* T602x (M2-pro and co) */
  124. #define PORT_T602X_MSIADDR 0x016c
  125. #define PORT_T602X_MSIADDR_HI 0x0170
  126. #define PORT_T602X_PERST 0x082c
  127. #define PORT_T602X_RID2SID 0x3000
  128. #define PORT_T602X_MSIMAP 0x3800
  129. #define PORT_MSIMAP_ENABLE BIT(31)
  130. #define PORT_MSIMAP_TARGET GENMASK(7, 0)
  131. /*
  132. * The doorbell address is set to 0xfffff000, which by convention
  133. * matches what MacOS does, and it is possible to use any other
  134. * address (in the bottom 4GB, as the base register is only 32bit).
  135. * However, it has to be excluded from the IOVA range, and the DART
  136. * driver has to know about it.
  137. */
  138. #define DOORBELL_ADDR CONFIG_PCIE_APPLE_MSI_DOORBELL_ADDR
  139. struct hw_info {
  140. u32 phy_lane_ctl;
  141. u32 port_msiaddr;
  142. u32 port_msiaddr_hi;
  143. u32 port_refclk;
  144. u32 port_perst;
  145. u32 port_rid2sid;
  146. u32 port_msimap;
  147. u32 max_rid2sid;
  148. };
  149. static const struct hw_info t8103_hw = {
  150. .phy_lane_ctl = PHY_LANE_CTL,
  151. .port_msiaddr = PORT_MSIADDR,
  152. .port_msiaddr_hi = 0,
  153. .port_refclk = PORT_REFCLK,
  154. .port_perst = PORT_PERST,
  155. .port_rid2sid = PORT_RID2SID,
  156. .port_msimap = 0,
  157. .max_rid2sid = 64,
  158. };
  159. static const struct hw_info t602x_hw = {
  160. .phy_lane_ctl = 0,
  161. .port_msiaddr = PORT_T602X_MSIADDR,
  162. .port_msiaddr_hi = PORT_T602X_MSIADDR_HI,
  163. .port_refclk = 0,
  164. .port_perst = PORT_T602X_PERST,
  165. .port_rid2sid = PORT_T602X_RID2SID,
  166. .port_msimap = PORT_T602X_MSIMAP,
  167. /* 16 on t602x, guess for autodetect on future HW */
  168. .max_rid2sid = 512,
  169. };
  170. struct apple_pcie {
  171. struct mutex lock;
  172. struct device *dev;
  173. void __iomem *base;
  174. const struct hw_info *hw;
  175. unsigned long *bitmap;
  176. struct list_head ports;
  177. struct completion event;
  178. struct irq_fwspec fwspec;
  179. u32 nvecs;
  180. };
  181. struct apple_pcie_port {
  182. raw_spinlock_t lock;
  183. struct apple_pcie *pcie;
  184. struct device_node *np;
  185. void __iomem *base;
  186. void __iomem *phy;
  187. struct irq_domain *domain;
  188. struct list_head entry;
  189. unsigned long *sid_map;
  190. int sid_map_sz;
  191. int idx;
  192. };
  193. static void rmw_set(u32 set, void __iomem *addr)
  194. {
  195. writel_relaxed(readl_relaxed(addr) | set, addr);
  196. }
  197. static void rmw_clear(u32 clr, void __iomem *addr)
  198. {
  199. writel_relaxed(readl_relaxed(addr) & ~clr, addr);
  200. }
  201. static void apple_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
  202. {
  203. msg->address_hi = upper_32_bits(DOORBELL_ADDR);
  204. msg->address_lo = lower_32_bits(DOORBELL_ADDR);
  205. msg->data = data->hwirq;
  206. }
  207. static struct irq_chip apple_msi_bottom_chip = {
  208. .name = "MSI",
  209. .irq_mask = irq_chip_mask_parent,
  210. .irq_unmask = irq_chip_unmask_parent,
  211. .irq_eoi = irq_chip_eoi_parent,
  212. .irq_set_affinity = irq_chip_set_affinity_parent,
  213. .irq_set_type = irq_chip_set_type_parent,
  214. .irq_compose_msi_msg = apple_msi_compose_msg,
  215. };
  216. static int apple_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
  217. unsigned int nr_irqs, void *args)
  218. {
  219. struct apple_pcie *pcie = domain->host_data;
  220. struct irq_fwspec fwspec = pcie->fwspec;
  221. unsigned int i;
  222. int ret, hwirq;
  223. mutex_lock(&pcie->lock);
  224. hwirq = bitmap_find_free_region(pcie->bitmap, pcie->nvecs,
  225. order_base_2(nr_irqs));
  226. mutex_unlock(&pcie->lock);
  227. if (hwirq < 0)
  228. return -ENOSPC;
  229. fwspec.param[fwspec.param_count - 2] += hwirq;
  230. ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &fwspec);
  231. if (ret)
  232. return ret;
  233. for (i = 0; i < nr_irqs; i++) {
  234. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  235. &apple_msi_bottom_chip, pcie);
  236. }
  237. return 0;
  238. }
  239. static void apple_msi_domain_free(struct irq_domain *domain, unsigned int virq,
  240. unsigned int nr_irqs)
  241. {
  242. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  243. struct apple_pcie *pcie = domain->host_data;
  244. mutex_lock(&pcie->lock);
  245. bitmap_release_region(pcie->bitmap, d->hwirq, order_base_2(nr_irqs));
  246. mutex_unlock(&pcie->lock);
  247. }
  248. static const struct irq_domain_ops apple_msi_domain_ops = {
  249. .alloc = apple_msi_domain_alloc,
  250. .free = apple_msi_domain_free,
  251. };
  252. static void apple_port_irq_mask(struct irq_data *data)
  253. {
  254. struct apple_pcie_port *port = irq_data_get_irq_chip_data(data);
  255. guard(raw_spinlock_irqsave)(&port->lock);
  256. rmw_set(BIT(data->hwirq), port->base + PORT_INTMSK);
  257. }
  258. static void apple_port_irq_unmask(struct irq_data *data)
  259. {
  260. struct apple_pcie_port *port = irq_data_get_irq_chip_data(data);
  261. guard(raw_spinlock_irqsave)(&port->lock);
  262. rmw_clear(BIT(data->hwirq), port->base + PORT_INTMSK);
  263. }
  264. static bool hwirq_is_intx(unsigned int hwirq)
  265. {
  266. return BIT(hwirq) & PORT_INT_INTx_MASK;
  267. }
  268. static void apple_port_irq_ack(struct irq_data *data)
  269. {
  270. struct apple_pcie_port *port = irq_data_get_irq_chip_data(data);
  271. if (!hwirq_is_intx(data->hwirq))
  272. writel_relaxed(BIT(data->hwirq), port->base + PORT_INTSTAT);
  273. }
  274. static int apple_port_irq_set_type(struct irq_data *data, unsigned int type)
  275. {
  276. /*
  277. * It doesn't seem that there is any way to configure the
  278. * trigger, so assume INTx have to be level (as per the spec),
  279. * and the rest is edge (which looks likely).
  280. */
  281. if (hwirq_is_intx(data->hwirq) ^ !!(type & IRQ_TYPE_LEVEL_MASK))
  282. return -EINVAL;
  283. irqd_set_trigger_type(data, type);
  284. return 0;
  285. }
  286. static struct irq_chip apple_port_irqchip = {
  287. .name = "PCIe",
  288. .irq_ack = apple_port_irq_ack,
  289. .irq_mask = apple_port_irq_mask,
  290. .irq_unmask = apple_port_irq_unmask,
  291. .irq_set_type = apple_port_irq_set_type,
  292. };
  293. static int apple_port_irq_domain_alloc(struct irq_domain *domain,
  294. unsigned int virq, unsigned int nr_irqs,
  295. void *args)
  296. {
  297. struct apple_pcie_port *port = domain->host_data;
  298. struct irq_fwspec *fwspec = args;
  299. int i;
  300. for (i = 0; i < nr_irqs; i++) {
  301. irq_flow_handler_t flow = handle_edge_irq;
  302. unsigned int type = IRQ_TYPE_EDGE_RISING;
  303. if (hwirq_is_intx(fwspec->param[0] + i)) {
  304. flow = handle_level_irq;
  305. type = IRQ_TYPE_LEVEL_HIGH;
  306. }
  307. irq_domain_set_info(domain, virq + i, fwspec->param[0] + i,
  308. &apple_port_irqchip, port, flow,
  309. NULL, NULL);
  310. irq_set_irq_type(virq + i, type);
  311. }
  312. return 0;
  313. }
  314. static void apple_port_irq_domain_free(struct irq_domain *domain,
  315. unsigned int virq, unsigned int nr_irqs)
  316. {
  317. int i;
  318. for (i = 0; i < nr_irqs; i++) {
  319. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  320. irq_set_handler(virq + i, NULL);
  321. irq_domain_reset_irq_data(d);
  322. }
  323. }
  324. static const struct irq_domain_ops apple_port_irq_domain_ops = {
  325. .translate = irq_domain_translate_onecell,
  326. .alloc = apple_port_irq_domain_alloc,
  327. .free = apple_port_irq_domain_free,
  328. };
  329. static void apple_port_irq_handler(struct irq_desc *desc)
  330. {
  331. struct apple_pcie_port *port = irq_desc_get_handler_data(desc);
  332. struct irq_chip *chip = irq_desc_get_chip(desc);
  333. unsigned long stat;
  334. int i;
  335. chained_irq_enter(chip, desc);
  336. stat = readl_relaxed(port->base + PORT_INTSTAT);
  337. for_each_set_bit(i, &stat, 32)
  338. generic_handle_domain_irq(port->domain, i);
  339. chained_irq_exit(chip, desc);
  340. }
  341. static int apple_pcie_port_setup_irq(struct apple_pcie_port *port)
  342. {
  343. struct fwnode_handle *fwnode = &port->np->fwnode;
  344. struct apple_pcie *pcie = port->pcie;
  345. unsigned int irq;
  346. u32 val = 0;
  347. /* FIXME: consider moving each interrupt under each port */
  348. irq = irq_of_parse_and_map(to_of_node(dev_fwnode(port->pcie->dev)),
  349. port->idx);
  350. if (!irq)
  351. return -ENXIO;
  352. port->domain = irq_domain_create_linear(fwnode, 32,
  353. &apple_port_irq_domain_ops,
  354. port);
  355. if (!port->domain)
  356. return -ENOMEM;
  357. /* Disable all interrupts */
  358. writel_relaxed(~0, port->base + PORT_INTMSK);
  359. writel_relaxed(~0, port->base + PORT_INTSTAT);
  360. writel_relaxed(~0, port->base + PORT_LINKCMDSTS);
  361. irq_set_chained_handler_and_data(irq, apple_port_irq_handler, port);
  362. /* Configure MSI base address */
  363. BUILD_BUG_ON(upper_32_bits(DOORBELL_ADDR));
  364. writel_relaxed(lower_32_bits(DOORBELL_ADDR),
  365. port->base + pcie->hw->port_msiaddr);
  366. if (pcie->hw->port_msiaddr_hi)
  367. writel_relaxed(0, port->base + pcie->hw->port_msiaddr_hi);
  368. /* Enable MSIs, shared between all ports */
  369. if (pcie->hw->port_msimap) {
  370. for (int i = 0; i < pcie->nvecs; i++)
  371. writel_relaxed(FIELD_PREP(PORT_MSIMAP_TARGET, i) |
  372. PORT_MSIMAP_ENABLE,
  373. port->base + pcie->hw->port_msimap + 4 * i);
  374. } else {
  375. writel_relaxed(0, port->base + PORT_MSIBASE);
  376. val = ilog2(pcie->nvecs) << PORT_MSICFG_L2MSINUM_SHIFT;
  377. }
  378. writel_relaxed(val | PORT_MSICFG_EN, port->base + PORT_MSICFG);
  379. return 0;
  380. }
  381. static irqreturn_t apple_pcie_port_irq(int irq, void *data)
  382. {
  383. struct apple_pcie_port *port = data;
  384. unsigned int hwirq = irq_domain_get_irq_data(port->domain, irq)->hwirq;
  385. switch (hwirq) {
  386. case PORT_INT_LINK_UP:
  387. dev_info_ratelimited(port->pcie->dev, "Link up on %pOF\n",
  388. port->np);
  389. complete_all(&port->pcie->event);
  390. break;
  391. case PORT_INT_LINK_DOWN:
  392. dev_info_ratelimited(port->pcie->dev, "Link down on %pOF\n",
  393. port->np);
  394. break;
  395. default:
  396. return IRQ_NONE;
  397. }
  398. return IRQ_HANDLED;
  399. }
  400. static int apple_pcie_port_register_irqs(struct apple_pcie_port *port)
  401. {
  402. static struct {
  403. unsigned int hwirq;
  404. const char *name;
  405. } port_irqs[] = {
  406. { PORT_INT_LINK_UP, "Link up", },
  407. { PORT_INT_LINK_DOWN, "Link down", },
  408. };
  409. int i;
  410. for (i = 0; i < ARRAY_SIZE(port_irqs); i++) {
  411. struct irq_fwspec fwspec = {
  412. .fwnode = &port->np->fwnode,
  413. .param_count = 1,
  414. .param = {
  415. [0] = port_irqs[i].hwirq,
  416. },
  417. };
  418. unsigned int irq;
  419. int ret;
  420. irq = irq_domain_alloc_irqs(port->domain, 1, NUMA_NO_NODE,
  421. &fwspec);
  422. if (WARN_ON(!irq))
  423. continue;
  424. ret = request_irq(irq, apple_pcie_port_irq, 0,
  425. port_irqs[i].name, port);
  426. WARN_ON(ret);
  427. }
  428. return 0;
  429. }
  430. static int apple_pcie_setup_refclk(struct apple_pcie *pcie,
  431. struct apple_pcie_port *port)
  432. {
  433. u32 stat;
  434. int res;
  435. if (pcie->hw->phy_lane_ctl)
  436. rmw_set(PHY_LANE_CTL_CFGACC, port->phy + pcie->hw->phy_lane_ctl);
  437. rmw_set(PHY_LANE_CFG_REFCLK0REQ, port->phy + PHY_LANE_CFG);
  438. res = readl_relaxed_poll_timeout(port->phy + PHY_LANE_CFG,
  439. stat, stat & PHY_LANE_CFG_REFCLK0ACK,
  440. 100, 50000);
  441. if (res < 0)
  442. return res;
  443. rmw_set(PHY_LANE_CFG_REFCLK1REQ, port->phy + PHY_LANE_CFG);
  444. res = readl_relaxed_poll_timeout(port->phy + PHY_LANE_CFG,
  445. stat, stat & PHY_LANE_CFG_REFCLK1ACK,
  446. 100, 50000);
  447. if (res < 0)
  448. return res;
  449. if (pcie->hw->phy_lane_ctl)
  450. rmw_clear(PHY_LANE_CTL_CFGACC, port->phy + pcie->hw->phy_lane_ctl);
  451. rmw_set(PHY_LANE_CFG_REFCLKEN, port->phy + PHY_LANE_CFG);
  452. if (pcie->hw->port_refclk)
  453. rmw_set(PORT_REFCLK_EN, port->base + pcie->hw->port_refclk);
  454. return 0;
  455. }
  456. static void __iomem *port_rid2sid_addr(struct apple_pcie_port *port, int idx)
  457. {
  458. return port->base + port->pcie->hw->port_rid2sid + 4 * idx;
  459. }
  460. static u32 apple_pcie_rid2sid_write(struct apple_pcie_port *port,
  461. int idx, u32 val)
  462. {
  463. writel_relaxed(val, port_rid2sid_addr(port, idx));
  464. /* Read back to ensure completion of the write */
  465. return readl_relaxed(port_rid2sid_addr(port, idx));
  466. }
  467. static int apple_pcie_setup_port(struct apple_pcie *pcie,
  468. struct device_node *np)
  469. {
  470. struct platform_device *platform = to_platform_device(pcie->dev);
  471. struct apple_pcie_port *port;
  472. struct gpio_desc *reset;
  473. struct resource *res;
  474. char name[16];
  475. u32 stat, idx;
  476. int ret, i;
  477. reset = devm_fwnode_gpiod_get(pcie->dev, of_fwnode_handle(np), "reset",
  478. GPIOD_OUT_LOW, "PERST#");
  479. if (IS_ERR(reset))
  480. return PTR_ERR(reset);
  481. port = devm_kzalloc(pcie->dev, sizeof(*port), GFP_KERNEL);
  482. if (!port)
  483. return -ENOMEM;
  484. port->sid_map = devm_bitmap_zalloc(pcie->dev, pcie->hw->max_rid2sid, GFP_KERNEL);
  485. if (!port->sid_map)
  486. return -ENOMEM;
  487. ret = of_property_read_u32_index(np, "reg", 0, &idx);
  488. if (ret)
  489. return ret;
  490. /* Use the first reg entry to work out the port index */
  491. port->idx = idx >> 11;
  492. port->pcie = pcie;
  493. port->np = np;
  494. raw_spin_lock_init(&port->lock);
  495. snprintf(name, sizeof(name), "port%d", port->idx);
  496. res = platform_get_resource_byname(platform, IORESOURCE_MEM, name);
  497. if (!res)
  498. res = platform_get_resource(platform, IORESOURCE_MEM, port->idx + 2);
  499. port->base = devm_ioremap_resource(&platform->dev, res);
  500. if (IS_ERR(port->base))
  501. return PTR_ERR(port->base);
  502. snprintf(name, sizeof(name), "phy%d", port->idx);
  503. res = platform_get_resource_byname(platform, IORESOURCE_MEM, name);
  504. if (res)
  505. port->phy = devm_ioremap_resource(&platform->dev, res);
  506. else
  507. port->phy = pcie->base + CORE_PHY_DEFAULT_BASE(port->idx);
  508. rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK);
  509. /* Assert PERST# before setting up the clock */
  510. gpiod_set_value_cansleep(reset, 1);
  511. ret = apple_pcie_setup_refclk(pcie, port);
  512. if (ret < 0)
  513. return ret;
  514. /* The minimal Tperst-clk value is 100us (PCIe CEM r5.0, 2.9.2) */
  515. usleep_range(100, 200);
  516. /* Deassert PERST# */
  517. rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst);
  518. gpiod_set_value_cansleep(reset, 0);
  519. /* Wait for 100ms after PERST# deassertion (PCIe r5.0, 6.6.1) */
  520. msleep(100);
  521. ret = readl_relaxed_poll_timeout(port->base + PORT_STATUS, stat,
  522. stat & PORT_STATUS_READY, 100, 250000);
  523. if (ret < 0) {
  524. dev_err(pcie->dev, "port %pOF ready wait timeout\n", np);
  525. return ret;
  526. }
  527. if (pcie->hw->port_refclk)
  528. rmw_clear(PORT_REFCLK_CGDIS, port->base + pcie->hw->port_refclk);
  529. else
  530. rmw_set(PHY_LANE_CFG_REFCLKCGEN, port->phy + PHY_LANE_CFG);
  531. rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK);
  532. ret = apple_pcie_port_setup_irq(port);
  533. if (ret)
  534. return ret;
  535. /* Reset all RID/SID mappings, and check for RAZ/WI registers */
  536. for (i = 0; i < pcie->hw->max_rid2sid; i++) {
  537. if (apple_pcie_rid2sid_write(port, i, 0xbad1d) != 0xbad1d)
  538. break;
  539. apple_pcie_rid2sid_write(port, i, 0);
  540. }
  541. dev_dbg(pcie->dev, "%pOF: %d RID/SID mapping entries\n", np, i);
  542. port->sid_map_sz = i;
  543. list_add_tail(&port->entry, &pcie->ports);
  544. init_completion(&pcie->event);
  545. /* In the success path, we keep a reference to np around */
  546. of_node_get(np);
  547. ret = apple_pcie_port_register_irqs(port);
  548. WARN_ON(ret);
  549. writel_relaxed(PORT_LTSSMCTL_START, port->base + PORT_LTSSMCTL);
  550. if (!wait_for_completion_timeout(&pcie->event, HZ / 10))
  551. dev_warn(pcie->dev, "%pOF link didn't come up\n", np);
  552. return 0;
  553. }
  554. static const struct msi_parent_ops apple_msi_parent_ops = {
  555. .supported_flags = (MSI_GENERIC_FLAGS_MASK |
  556. MSI_FLAG_PCI_MSIX |
  557. MSI_FLAG_MULTI_PCI_MSI),
  558. .required_flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  559. MSI_FLAG_USE_DEF_CHIP_OPS |
  560. MSI_FLAG_PCI_MSI_MASK_PARENT),
  561. .chip_flags = MSI_CHIP_FLAG_SET_EOI,
  562. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  563. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  564. };
  565. static int apple_msi_init(struct apple_pcie *pcie)
  566. {
  567. struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
  568. struct irq_domain_info info = {
  569. .fwnode = fwnode,
  570. .ops = &apple_msi_domain_ops,
  571. .size = pcie->nvecs,
  572. .host_data = pcie,
  573. };
  574. struct of_phandle_args args = {};
  575. int ret;
  576. ret = of_parse_phandle_with_args(to_of_node(fwnode), "msi-ranges",
  577. "#interrupt-cells", 0, &args);
  578. if (ret)
  579. return ret;
  580. ret = of_property_read_u32_index(to_of_node(fwnode), "msi-ranges",
  581. args.args_count + 1, &pcie->nvecs);
  582. if (ret)
  583. return ret;
  584. of_phandle_args_to_fwspec(args.np, args.args, args.args_count,
  585. &pcie->fwspec);
  586. pcie->bitmap = devm_bitmap_zalloc(pcie->dev, pcie->nvecs, GFP_KERNEL);
  587. if (!pcie->bitmap)
  588. return -ENOMEM;
  589. info.parent = irq_find_matching_fwspec(&pcie->fwspec, DOMAIN_BUS_WIRED);
  590. if (!info.parent) {
  591. dev_err(pcie->dev, "failed to find parent domain\n");
  592. return -ENXIO;
  593. }
  594. if (!msi_create_parent_irq_domain(&info, &apple_msi_parent_ops)) {
  595. dev_err(pcie->dev, "failed to create IRQ domain\n");
  596. return -ENOMEM;
  597. }
  598. return 0;
  599. }
  600. static struct apple_pcie *apple_pcie_lookup(struct device *dev)
  601. {
  602. return pci_host_bridge_priv(dev_get_drvdata(dev));
  603. }
  604. static struct apple_pcie_port *apple_pcie_get_port(struct pci_dev *pdev)
  605. {
  606. struct pci_config_window *cfg = pdev->sysdata;
  607. struct apple_pcie *pcie;
  608. struct pci_dev *port_pdev;
  609. struct apple_pcie_port *port;
  610. pcie = apple_pcie_lookup(cfg->parent);
  611. if (WARN_ON(!pcie))
  612. return NULL;
  613. /* Find the root port this device is on */
  614. port_pdev = pcie_find_root_port(pdev);
  615. /* If finding the port itself, nothing to do */
  616. if (WARN_ON(!port_pdev) || pdev == port_pdev)
  617. return NULL;
  618. list_for_each_entry(port, &pcie->ports, entry) {
  619. if (port->idx == PCI_SLOT(port_pdev->devfn))
  620. return port;
  621. }
  622. return NULL;
  623. }
  624. static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_dev *pdev)
  625. {
  626. u32 sid, rid = pci_dev_id(pdev);
  627. struct apple_pcie_port *port;
  628. int idx, err;
  629. port = apple_pcie_get_port(pdev);
  630. if (!port)
  631. return 0;
  632. dev_dbg(&pdev->dev, "added to bus %s, index %d\n",
  633. pci_name(pdev->bus->self), port->idx);
  634. err = of_map_id(port->pcie->dev->of_node, rid, "iommu-map",
  635. "iommu-map-mask", NULL, &sid);
  636. if (err)
  637. return err;
  638. mutex_lock(&port->pcie->lock);
  639. idx = bitmap_find_free_region(port->sid_map, port->sid_map_sz, 0);
  640. if (idx >= 0) {
  641. apple_pcie_rid2sid_write(port, idx,
  642. PORT_RID2SID_VALID |
  643. (sid << PORT_RID2SID_SID_SHIFT) | rid);
  644. dev_dbg(&pdev->dev, "mapping RID%x to SID%x (index %d)\n",
  645. rid, sid, idx);
  646. }
  647. mutex_unlock(&port->pcie->lock);
  648. return idx >= 0 ? 0 : -ENOSPC;
  649. }
  650. static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci_dev *pdev)
  651. {
  652. struct apple_pcie_port *port;
  653. u32 rid = pci_dev_id(pdev);
  654. int idx;
  655. port = apple_pcie_get_port(pdev);
  656. if (!port)
  657. return;
  658. mutex_lock(&port->pcie->lock);
  659. for_each_set_bit(idx, port->sid_map, port->sid_map_sz) {
  660. u32 val;
  661. val = readl_relaxed(port_rid2sid_addr(port, idx));
  662. if ((val & 0xffff) == rid) {
  663. apple_pcie_rid2sid_write(port, idx, 0);
  664. bitmap_release_region(port->sid_map, idx, 0);
  665. dev_dbg(&pdev->dev, "Released %x (%d)\n", val, idx);
  666. break;
  667. }
  668. }
  669. mutex_unlock(&port->pcie->lock);
  670. }
  671. static int apple_pcie_init(struct pci_config_window *cfg)
  672. {
  673. struct device *dev = cfg->parent;
  674. struct apple_pcie *pcie;
  675. int ret;
  676. pcie = apple_pcie_lookup(dev);
  677. if (WARN_ON(!pcie))
  678. return -ENOENT;
  679. for_each_available_child_of_node_scoped(dev->of_node, of_port) {
  680. ret = apple_pcie_setup_port(pcie, of_port);
  681. if (ret) {
  682. dev_err(dev, "Port %pOF setup fail: %d\n", of_port, ret);
  683. return ret;
  684. }
  685. }
  686. return 0;
  687. }
  688. static const struct pci_ecam_ops apple_pcie_cfg_ecam_ops = {
  689. .init = apple_pcie_init,
  690. .enable_device = apple_pcie_enable_device,
  691. .disable_device = apple_pcie_disable_device,
  692. .pci_ops = {
  693. .map_bus = pci_ecam_map_bus,
  694. .read = pci_generic_config_read,
  695. .write = pci_generic_config_write,
  696. }
  697. };
  698. static int apple_pcie_probe(struct platform_device *pdev)
  699. {
  700. struct device *dev = &pdev->dev;
  701. struct pci_host_bridge *bridge;
  702. struct apple_pcie *pcie;
  703. int ret;
  704. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  705. if (!bridge)
  706. return -ENOMEM;
  707. pcie = pci_host_bridge_priv(bridge);
  708. pcie->dev = dev;
  709. pcie->hw = of_device_get_match_data(dev);
  710. if (!pcie->hw)
  711. return -ENODEV;
  712. pcie->base = devm_platform_ioremap_resource(pdev, 1);
  713. if (IS_ERR(pcie->base))
  714. return PTR_ERR(pcie->base);
  715. mutex_init(&pcie->lock);
  716. INIT_LIST_HEAD(&pcie->ports);
  717. ret = apple_msi_init(pcie);
  718. if (ret)
  719. return ret;
  720. return pci_host_common_init(pdev, bridge, &apple_pcie_cfg_ecam_ops);
  721. }
  722. static const struct of_device_id apple_pcie_of_match[] = {
  723. { .compatible = "apple,t6020-pcie", .data = &t602x_hw },
  724. { .compatible = "apple,pcie", .data = &t8103_hw },
  725. { }
  726. };
  727. MODULE_DEVICE_TABLE(of, apple_pcie_of_match);
  728. static struct platform_driver apple_pcie_driver = {
  729. .probe = apple_pcie_probe,
  730. .driver = {
  731. .name = "pcie-apple",
  732. .of_match_table = apple_pcie_of_match,
  733. .suppress_bind_attrs = true,
  734. },
  735. };
  736. module_platform_driver(apple_pcie_driver);
  737. MODULE_DESCRIPTION("Apple PCIe host bridge driver");
  738. MODULE_LICENSE("GPL v2");