pci_gx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mmzone.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/capability.h>
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/irq.h>
  24. #include <linux/msi.h>
  25. #include <linux/io.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/ctype.h>
  28. #include <asm/processor.h>
  29. #include <asm/sections.h>
  30. #include <asm/byteorder.h>
  31. #include <gxio/iorpc_globals.h>
  32. #include <gxio/kiorpc.h>
  33. #include <gxio/trio.h>
  34. #include <gxio/iorpc_trio.h>
  35. #include <hv/drv_trio_intf.h>
  36. #include <arch/sim.h>
  37. /*
  38. * This file containes the routines to search for PCI buses,
  39. * enumerate the buses, and configure any attached devices.
  40. */
  41. #define DEBUG_PCI_CFG 0
  42. #if DEBUG_PCI_CFG
  43. #define TRACE_CFG_WR(size, val, bus, dev, func, offset) \
  44. pr_info("CFG WR %d-byte VAL %#x to bus %d dev %d func %d addr %u\n", \
  45. size, val, bus, dev, func, offset & 0xFFF);
  46. #define TRACE_CFG_RD(size, val, bus, dev, func, offset) \
  47. pr_info("CFG RD %d-byte VAL %#x from bus %d dev %d func %d addr %u\n", \
  48. size, val, bus, dev, func, offset & 0xFFF);
  49. #else
  50. #define TRACE_CFG_WR(...)
  51. #define TRACE_CFG_RD(...)
  52. #endif
  53. static int pci_probe = 1;
  54. /* Information on the PCIe RC ports configuration. */
  55. static int pcie_rc[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
  56. /*
  57. * On some platforms with one or more Gx endpoint ports, we need to
  58. * delay the PCIe RC port probe for a few seconds to work around
  59. * a HW PCIe link-training bug. The exact delay is specified with
  60. * a kernel boot argument in the form of "pcie_rc_delay=T,P,S",
  61. * where T is the TRIO instance number, P is the port number and S is
  62. * the delay in seconds. If the argument is specified, but the delay is
  63. * not provided, the value will be DEFAULT_RC_DELAY.
  64. */
  65. static int rc_delay[TILEGX_NUM_TRIO][TILEGX_TRIO_PCIES];
  66. /* Default number of seconds that the PCIe RC port probe can be delayed. */
  67. #define DEFAULT_RC_DELAY 10
  68. /* The PCI I/O space size in each PCI domain. */
  69. #define IO_SPACE_SIZE 0x10000
  70. /* Provide shorter versions of some very long constant names. */
  71. #define AUTO_CONFIG_RC \
  72. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC
  73. #define AUTO_CONFIG_RC_G1 \
  74. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_RC_G1
  75. #define AUTO_CONFIG_EP \
  76. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT
  77. #define AUTO_CONFIG_EP_G1 \
  78. TRIO_PCIE_INTFC_PORT_CONFIG__STRAP_STATE_VAL_AUTO_CONFIG_ENDPOINT_G1
  79. /* Array of the PCIe ports configuration info obtained from the BIB. */
  80. struct pcie_trio_ports_property pcie_ports[TILEGX_NUM_TRIO];
  81. /* Number of configured TRIO instances. */
  82. int num_trio_shims;
  83. /* All drivers share the TRIO contexts defined here. */
  84. gxio_trio_context_t trio_contexts[TILEGX_NUM_TRIO];
  85. /* Pointer to an array of PCIe RC controllers. */
  86. struct pci_controller pci_controllers[TILEGX_NUM_TRIO * TILEGX_TRIO_PCIES];
  87. int num_rc_controllers;
  88. static struct pci_ops tile_cfg_ops;
  89. /* Mask of CPUs that should receive PCIe interrupts. */
  90. static struct cpumask intr_cpus_map;
  91. /* We don't need to worry about the alignment of resources. */
  92. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  93. resource_size_t size,
  94. resource_size_t align)
  95. {
  96. return res->start;
  97. }
  98. EXPORT_SYMBOL(pcibios_align_resource);
  99. /*
  100. * Pick a CPU to receive and handle the PCIe interrupts, based on the IRQ #.
  101. * For now, we simply send interrupts to non-dataplane CPUs.
  102. * We may implement methods to allow user to specify the target CPUs,
  103. * e.g. via boot arguments.
  104. */
  105. static int tile_irq_cpu(int irq)
  106. {
  107. unsigned int count;
  108. int i = 0;
  109. int cpu;
  110. count = cpumask_weight(&intr_cpus_map);
  111. if (unlikely(count == 0)) {
  112. pr_warn("intr_cpus_map empty, interrupts will be delievered to dataplane tiles\n");
  113. return irq % (smp_height * smp_width);
  114. }
  115. count = irq % count;
  116. for_each_cpu(cpu, &intr_cpus_map) {
  117. if (i++ == count)
  118. break;
  119. }
  120. return cpu;
  121. }
  122. /* Open a file descriptor to the TRIO shim. */
  123. static int tile_pcie_open(int trio_index)
  124. {
  125. gxio_trio_context_t *context = &trio_contexts[trio_index];
  126. int ret;
  127. int mac;
  128. /* This opens a file descriptor to the TRIO shim. */
  129. ret = gxio_trio_init(context, trio_index);
  130. if (ret < 0)
  131. goto gxio_trio_init_failure;
  132. /* Allocate an ASID for the kernel. */
  133. ret = gxio_trio_alloc_asids(context, 1, 0, 0);
  134. if (ret < 0) {
  135. pr_err("PCI: ASID alloc failure on TRIO %d, give up\n",
  136. trio_index);
  137. goto asid_alloc_failure;
  138. }
  139. context->asid = ret;
  140. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  141. /*
  142. * Alloc a PIO region for config access, shared by all MACs per TRIO.
  143. * This shouldn't fail since the kernel is supposed to the first
  144. * client of the TRIO's PIO regions.
  145. */
  146. ret = gxio_trio_alloc_pio_regions(context, 1, 0, 0);
  147. if (ret < 0) {
  148. pr_err("PCI: CFG PIO alloc failure on TRIO %d, give up\n",
  149. trio_index);
  150. goto pio_alloc_failure;
  151. }
  152. context->pio_cfg_index = ret;
  153. /*
  154. * For PIO CFG, the bus_address_hi parameter is 0. The mac parameter
  155. * is also 0 because it is specified in PIO_REGION_SETUP_CFG_ADDR.
  156. */
  157. ret = gxio_trio_init_pio_region_aux(context, context->pio_cfg_index,
  158. 0, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  159. if (ret < 0) {
  160. pr_err("PCI: CFG PIO init failure on TRIO %d, give up\n",
  161. trio_index);
  162. goto pio_alloc_failure;
  163. }
  164. #endif
  165. /* Get the properties of the PCIe ports on this TRIO instance. */
  166. ret = gxio_trio_get_port_property(context, &pcie_ports[trio_index]);
  167. if (ret < 0) {
  168. pr_err("PCI: PCIE_GET_PORT_PROPERTY failure, error %d, on TRIO %d\n",
  169. ret, trio_index);
  170. goto get_port_property_failure;
  171. }
  172. context->mmio_base_mac =
  173. iorpc_ioremap(context->fd, 0, HV_TRIO_CONFIG_IOREMAP_SIZE);
  174. if (context->mmio_base_mac == NULL) {
  175. pr_err("PCI: TRIO config space mapping failure, error %d, on TRIO %d\n",
  176. ret, trio_index);
  177. ret = -ENOMEM;
  178. goto trio_mmio_mapping_failure;
  179. }
  180. /* Check the port strap state which will override the BIB setting. */
  181. for (mac = 0; mac < TILEGX_TRIO_PCIES; mac++) {
  182. TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
  183. unsigned int reg_offset;
  184. /* Ignore ports that are not specified in the BIB. */
  185. if (!pcie_ports[trio_index].ports[mac].allow_rc &&
  186. !pcie_ports[trio_index].ports[mac].allow_ep)
  187. continue;
  188. reg_offset =
  189. (TRIO_PCIE_INTFC_PORT_CONFIG <<
  190. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  191. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  192. TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
  193. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  194. port_config.word =
  195. __gxio_mmio_read(context->mmio_base_mac + reg_offset);
  196. if (port_config.strap_state != AUTO_CONFIG_RC &&
  197. port_config.strap_state != AUTO_CONFIG_RC_G1) {
  198. /*
  199. * If this is really intended to be an EP port, record
  200. * it so that the endpoint driver will know about it.
  201. */
  202. if (port_config.strap_state == AUTO_CONFIG_EP ||
  203. port_config.strap_state == AUTO_CONFIG_EP_G1)
  204. pcie_ports[trio_index].ports[mac].allow_ep = 1;
  205. }
  206. }
  207. return ret;
  208. trio_mmio_mapping_failure:
  209. get_port_property_failure:
  210. asid_alloc_failure:
  211. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  212. pio_alloc_failure:
  213. #endif
  214. hv_dev_close(context->fd);
  215. gxio_trio_init_failure:
  216. context->fd = -1;
  217. return ret;
  218. }
  219. static int __init tile_trio_init(void)
  220. {
  221. int i;
  222. /* We loop over all the TRIO shims. */
  223. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  224. if (tile_pcie_open(i) < 0)
  225. continue;
  226. num_trio_shims++;
  227. }
  228. return 0;
  229. }
  230. postcore_initcall(tile_trio_init);
  231. static void tilegx_legacy_irq_ack(struct irq_data *d)
  232. {
  233. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  234. }
  235. static void tilegx_legacy_irq_mask(struct irq_data *d)
  236. {
  237. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  238. }
  239. static void tilegx_legacy_irq_unmask(struct irq_data *d)
  240. {
  241. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  242. }
  243. static struct irq_chip tilegx_legacy_irq_chip = {
  244. .name = "tilegx_legacy_irq",
  245. .irq_ack = tilegx_legacy_irq_ack,
  246. .irq_mask = tilegx_legacy_irq_mask,
  247. .irq_unmask = tilegx_legacy_irq_unmask,
  248. /* TBD: support set_affinity. */
  249. };
  250. /*
  251. * This is a wrapper function of the kernel level-trigger interrupt
  252. * handler handle_level_irq() for PCI legacy interrupts. The TRIO
  253. * is configured such that only INTx Assert interrupts are proxied
  254. * to Linux which just calls handle_level_irq() after clearing the
  255. * MAC INTx Assert status bit associated with this interrupt.
  256. */
  257. static void trio_handle_level_irq(unsigned int __irq, struct irq_desc *desc)
  258. {
  259. struct pci_controller *controller = irq_desc_get_handler_data(desc);
  260. gxio_trio_context_t *trio_context = controller->trio;
  261. uint64_t intx = (uint64_t)irq_desc_get_chip_data(desc);
  262. unsigned int irq = irq_desc_get_irq(desc);
  263. int mac = controller->mac;
  264. unsigned int reg_offset;
  265. uint64_t level_mask;
  266. handle_level_irq(irq, desc);
  267. /*
  268. * Clear the INTx Level status, otherwise future interrupts are
  269. * not sent.
  270. */
  271. reg_offset = (TRIO_PCIE_INTFC_MAC_INT_STS <<
  272. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  273. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  274. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  275. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  276. level_mask = TRIO_PCIE_INTFC_MAC_INT_STS__INT_LEVEL_MASK << intx;
  277. __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset, level_mask);
  278. }
  279. /*
  280. * Create kernel irqs and set up the handlers for the legacy interrupts.
  281. * Also some minimum initialization for the MSI support.
  282. */
  283. static int tile_init_irqs(struct pci_controller *controller)
  284. {
  285. int i;
  286. int j;
  287. int irq;
  288. int result;
  289. cpumask_copy(&intr_cpus_map, cpu_online_mask);
  290. for (i = 0; i < 4; i++) {
  291. gxio_trio_context_t *context = controller->trio;
  292. int cpu;
  293. /* Ask the kernel to allocate an IRQ. */
  294. irq = irq_alloc_hwirq(-1);
  295. if (!irq) {
  296. pr_err("PCI: no free irq vectors, failed for %d\n", i);
  297. goto free_irqs;
  298. }
  299. controller->irq_intx_table[i] = irq;
  300. /* Distribute the 4 IRQs to different tiles. */
  301. cpu = tile_irq_cpu(irq);
  302. /* Configure the TRIO intr binding for this IRQ. */
  303. result = gxio_trio_config_legacy_intr(context, cpu_x(cpu),
  304. cpu_y(cpu), KERNEL_PL,
  305. irq, controller->mac, i);
  306. if (result < 0) {
  307. pr_err("PCI: MAC intx config failed for %d\n", i);
  308. goto free_irqs;
  309. }
  310. /* Register the IRQ handler with the kernel. */
  311. irq_set_chip_and_handler(irq, &tilegx_legacy_irq_chip,
  312. trio_handle_level_irq);
  313. irq_set_chip_data(irq, (void *)(uint64_t)i);
  314. irq_set_handler_data(irq, controller);
  315. }
  316. return 0;
  317. free_irqs:
  318. for (j = 0; j < i; j++)
  319. irq_free_hwirq(controller->irq_intx_table[j]);
  320. return -1;
  321. }
  322. /*
  323. * Return 1 if the port is strapped to operate in RC mode.
  324. */
  325. static int
  326. strapped_for_rc(gxio_trio_context_t *trio_context, int mac)
  327. {
  328. TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
  329. unsigned int reg_offset;
  330. /* Check the port configuration. */
  331. reg_offset =
  332. (TRIO_PCIE_INTFC_PORT_CONFIG <<
  333. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  334. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  335. TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
  336. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  337. port_config.word =
  338. __gxio_mmio_read(trio_context->mmio_base_mac + reg_offset);
  339. if (port_config.strap_state == AUTO_CONFIG_RC ||
  340. port_config.strap_state == AUTO_CONFIG_RC_G1)
  341. return 1;
  342. else
  343. return 0;
  344. }
  345. /*
  346. * Find valid controllers and fill in pci_controller structs for each
  347. * of them.
  348. *
  349. * Return the number of controllers discovered.
  350. */
  351. int __init tile_pci_init(void)
  352. {
  353. int ctl_index = 0;
  354. int i, j;
  355. if (!pci_probe) {
  356. pr_info("PCI: disabled by boot argument\n");
  357. return 0;
  358. }
  359. pr_info("PCI: Searching for controllers...\n");
  360. if (num_trio_shims == 0 || sim_is_simulator())
  361. return 0;
  362. /*
  363. * Now determine which PCIe ports are configured to operate in RC
  364. * mode. There is a differece in the port configuration capability
  365. * between the Gx36 and Gx72 devices.
  366. *
  367. * The Gx36 has configuration capability for each of the 3 PCIe
  368. * interfaces (disable, auto endpoint, auto RC, etc.).
  369. * On the Gx72, you can only select one of the 3 PCIe interfaces per
  370. * TRIO to train automatically. Further, the allowable training modes
  371. * are reduced to four options (auto endpoint, auto RC, stream x1,
  372. * stream x4).
  373. *
  374. * For Gx36 ports, it must be allowed to be in RC mode by the
  375. * Board Information Block, and the hardware strapping pins must be
  376. * set to RC mode.
  377. *
  378. * For Gx72 ports, the port will operate in RC mode if either of the
  379. * following is true:
  380. * 1. It is allowed to be in RC mode by the Board Information Block,
  381. * and the BIB doesn't allow the EP mode.
  382. * 2. It is allowed to be in either the RC or the EP mode by the BIB,
  383. * and the hardware strapping pin is set to RC mode.
  384. */
  385. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  386. gxio_trio_context_t *context = &trio_contexts[i];
  387. if (context->fd < 0)
  388. continue;
  389. for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
  390. int is_rc = 0;
  391. if (pcie_ports[i].is_gx72 &&
  392. pcie_ports[i].ports[j].allow_rc) {
  393. if (!pcie_ports[i].ports[j].allow_ep ||
  394. strapped_for_rc(context, j))
  395. is_rc = 1;
  396. } else if (pcie_ports[i].ports[j].allow_rc &&
  397. strapped_for_rc(context, j)) {
  398. is_rc = 1;
  399. }
  400. if (is_rc) {
  401. pcie_rc[i][j] = 1;
  402. num_rc_controllers++;
  403. }
  404. }
  405. }
  406. /* Return if no PCIe ports are configured to operate in RC mode. */
  407. if (num_rc_controllers == 0)
  408. return 0;
  409. /* Set the TRIO pointer and MAC index for each PCIe RC port. */
  410. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  411. for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
  412. if (pcie_rc[i][j]) {
  413. pci_controllers[ctl_index].trio =
  414. &trio_contexts[i];
  415. pci_controllers[ctl_index].mac = j;
  416. pci_controllers[ctl_index].trio_index = i;
  417. ctl_index++;
  418. if (ctl_index == num_rc_controllers)
  419. goto out;
  420. }
  421. }
  422. }
  423. out:
  424. /* Configure each PCIe RC port. */
  425. for (i = 0; i < num_rc_controllers; i++) {
  426. /* Configure the PCIe MAC to run in RC mode. */
  427. struct pci_controller *controller = &pci_controllers[i];
  428. controller->index = i;
  429. controller->ops = &tile_cfg_ops;
  430. controller->io_space.start = PCIBIOS_MIN_IO +
  431. (i * IO_SPACE_SIZE);
  432. controller->io_space.end = controller->io_space.start +
  433. IO_SPACE_SIZE - 1;
  434. BUG_ON(controller->io_space.end > IO_SPACE_LIMIT);
  435. controller->io_space.flags = IORESOURCE_IO;
  436. snprintf(controller->io_space_name,
  437. sizeof(controller->io_space_name),
  438. "PCI I/O domain %d", i);
  439. controller->io_space.name = controller->io_space_name;
  440. /*
  441. * The PCI memory resource is located above the PA space.
  442. * For every host bridge, the BAR window or the MMIO aperture
  443. * is in range [3GB, 4GB - 1] of a 4GB space beyond the
  444. * PA space.
  445. */
  446. controller->mem_offset = TILE_PCI_MEM_START +
  447. (i * TILE_PCI_BAR_WINDOW_TOP);
  448. controller->mem_space.start = controller->mem_offset +
  449. TILE_PCI_BAR_WINDOW_TOP - TILE_PCI_BAR_WINDOW_SIZE;
  450. controller->mem_space.end = controller->mem_offset +
  451. TILE_PCI_BAR_WINDOW_TOP - 1;
  452. controller->mem_space.flags = IORESOURCE_MEM;
  453. snprintf(controller->mem_space_name,
  454. sizeof(controller->mem_space_name),
  455. "PCI mem domain %d", i);
  456. controller->mem_space.name = controller->mem_space_name;
  457. }
  458. return num_rc_controllers;
  459. }
  460. /*
  461. * (pin - 1) converts from the PCI standard's [1:4] convention to
  462. * a normal [0:3] range.
  463. */
  464. static int tile_map_irq(const struct pci_dev *dev, u8 device, u8 pin)
  465. {
  466. struct pci_controller *controller =
  467. (struct pci_controller *)dev->sysdata;
  468. return controller->irq_intx_table[pin - 1];
  469. }
  470. static void fixup_read_and_payload_sizes(struct pci_controller *controller)
  471. {
  472. gxio_trio_context_t *trio_context = controller->trio;
  473. struct pci_bus *root_bus = controller->root_bus;
  474. TRIO_PCIE_RC_DEVICE_CONTROL_t dev_control;
  475. TRIO_PCIE_RC_DEVICE_CAP_t rc_dev_cap;
  476. unsigned int reg_offset;
  477. struct pci_bus *child;
  478. int mac;
  479. int err;
  480. mac = controller->mac;
  481. /* Set our max read request size to be 4KB. */
  482. reg_offset =
  483. (TRIO_PCIE_RC_DEVICE_CONTROL <<
  484. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  485. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  486. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  487. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  488. dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  489. reg_offset);
  490. dev_control.max_read_req_sz = 5;
  491. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  492. dev_control.word);
  493. /*
  494. * Set the max payload size supported by this Gx PCIe MAC.
  495. * Though Gx PCIe supports Max Payload Size of up to 1024 bytes,
  496. * experiments have shown that setting MPS to 256 yields the
  497. * best performance.
  498. */
  499. reg_offset =
  500. (TRIO_PCIE_RC_DEVICE_CAP <<
  501. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  502. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  503. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  504. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  505. rc_dev_cap.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  506. reg_offset);
  507. rc_dev_cap.mps_sup = 1;
  508. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  509. rc_dev_cap.word);
  510. /* Configure PCI Express MPS setting. */
  511. list_for_each_entry(child, &root_bus->children, node)
  512. pcie_bus_configure_settings(child);
  513. /*
  514. * Set the mac_config register in trio based on the MPS/MRS of the link.
  515. */
  516. reg_offset =
  517. (TRIO_PCIE_RC_DEVICE_CONTROL <<
  518. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  519. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  520. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  521. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  522. dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  523. reg_offset);
  524. err = gxio_trio_set_mps_mrs(trio_context,
  525. dev_control.max_payload_size,
  526. dev_control.max_read_req_sz,
  527. mac);
  528. if (err < 0) {
  529. pr_err("PCI: PCIE_CONFIGURE_MAC_MPS_MRS failure, MAC %d on TRIO %d\n",
  530. mac, controller->trio_index);
  531. }
  532. }
  533. static int setup_pcie_rc_delay(char *str)
  534. {
  535. unsigned long delay = 0;
  536. unsigned long trio_index;
  537. unsigned long mac;
  538. if (str == NULL || !isdigit(*str))
  539. return -EINVAL;
  540. trio_index = simple_strtoul(str, (char **)&str, 10);
  541. if (trio_index >= TILEGX_NUM_TRIO)
  542. return -EINVAL;
  543. if (*str != ',')
  544. return -EINVAL;
  545. str++;
  546. if (!isdigit(*str))
  547. return -EINVAL;
  548. mac = simple_strtoul(str, (char **)&str, 10);
  549. if (mac >= TILEGX_TRIO_PCIES)
  550. return -EINVAL;
  551. if (*str != '\0') {
  552. if (*str != ',')
  553. return -EINVAL;
  554. str++;
  555. if (!isdigit(*str))
  556. return -EINVAL;
  557. delay = simple_strtoul(str, (char **)&str, 10);
  558. }
  559. rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
  560. return 0;
  561. }
  562. early_param("pcie_rc_delay", setup_pcie_rc_delay);
  563. /* PCI initialization entry point, called by subsys_initcall. */
  564. int __init pcibios_init(void)
  565. {
  566. resource_size_t offset;
  567. LIST_HEAD(resources);
  568. int next_busno;
  569. int i;
  570. tile_pci_init();
  571. if (num_rc_controllers == 0)
  572. return 0;
  573. /*
  574. * Delay a bit in case devices aren't ready. Some devices are
  575. * known to require at least 20ms here, but we use a more
  576. * conservative value.
  577. */
  578. msleep(250);
  579. /* Scan all of the recorded PCI controllers. */
  580. for (next_busno = 0, i = 0; i < num_rc_controllers; i++) {
  581. struct pci_controller *controller = &pci_controllers[i];
  582. gxio_trio_context_t *trio_context = controller->trio;
  583. TRIO_PCIE_INTFC_PORT_STATUS_t port_status;
  584. TRIO_PCIE_INTFC_TX_FIFO_CTL_t tx_fifo_ctl;
  585. struct pci_bus *bus;
  586. unsigned int reg_offset;
  587. unsigned int class_code_revision;
  588. int trio_index;
  589. int mac;
  590. int ret;
  591. if (trio_context->fd < 0)
  592. continue;
  593. trio_index = controller->trio_index;
  594. mac = controller->mac;
  595. /*
  596. * Check for PCIe link-up status to decide if we need
  597. * to force the link to come up.
  598. */
  599. reg_offset =
  600. (TRIO_PCIE_INTFC_PORT_STATUS <<
  601. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  602. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  603. TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
  604. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  605. port_status.word =
  606. __gxio_mmio_read(trio_context->mmio_base_mac +
  607. reg_offset);
  608. if (!port_status.dl_up) {
  609. if (rc_delay[trio_index][mac]) {
  610. pr_info("Delaying PCIe RC TRIO init %d sec on MAC %d on TRIO %d\n",
  611. rc_delay[trio_index][mac], mac,
  612. trio_index);
  613. msleep(rc_delay[trio_index][mac] * 1000);
  614. }
  615. ret = gxio_trio_force_rc_link_up(trio_context, mac);
  616. if (ret < 0)
  617. pr_err("PCI: PCIE_FORCE_LINK_UP failure, MAC %d on TRIO %d\n",
  618. mac, trio_index);
  619. }
  620. pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n",
  621. i, trio_index, controller->mac);
  622. /* Delay the bus probe if needed. */
  623. if (rc_delay[trio_index][mac]) {
  624. pr_info("Delaying PCIe RC bus enumerating %d sec on MAC %d on TRIO %d\n",
  625. rc_delay[trio_index][mac], mac, trio_index);
  626. msleep(rc_delay[trio_index][mac] * 1000);
  627. } else {
  628. /*
  629. * Wait a bit here because some EP devices
  630. * take longer to come up.
  631. */
  632. msleep(1000);
  633. }
  634. /* Check for PCIe link-up status again. */
  635. port_status.word =
  636. __gxio_mmio_read(trio_context->mmio_base_mac +
  637. reg_offset);
  638. if (!port_status.dl_up) {
  639. if (pcie_ports[trio_index].ports[mac].removable) {
  640. pr_info("PCI: link is down, MAC %d on TRIO %d\n",
  641. mac, trio_index);
  642. pr_info("This is expected if no PCIe card is connected to this link\n");
  643. } else
  644. pr_err("PCI: link is down, MAC %d on TRIO %d\n",
  645. mac, trio_index);
  646. continue;
  647. }
  648. /*
  649. * Ensure that the link can come out of L1 power down state.
  650. * Strictly speaking, this is needed only in the case of
  651. * heavy RC-initiated DMAs.
  652. */
  653. reg_offset =
  654. (TRIO_PCIE_INTFC_TX_FIFO_CTL <<
  655. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  656. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  657. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  658. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  659. tx_fifo_ctl.word =
  660. __gxio_mmio_read(trio_context->mmio_base_mac +
  661. reg_offset);
  662. tx_fifo_ctl.min_p_credits = 0;
  663. __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
  664. tx_fifo_ctl.word);
  665. /*
  666. * Change the device ID so that Linux bus crawl doesn't confuse
  667. * the internal bridge with any Tilera endpoints.
  668. */
  669. reg_offset =
  670. (TRIO_PCIE_RC_DEVICE_ID_VEN_ID <<
  671. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  672. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  673. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  674. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  675. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  676. (TILERA_GX36_RC_DEV_ID <<
  677. TRIO_PCIE_RC_DEVICE_ID_VEN_ID__DEV_ID_SHIFT) |
  678. TILERA_VENDOR_ID);
  679. /* Set the internal P2P bridge class code. */
  680. reg_offset =
  681. (TRIO_PCIE_RC_REVISION_ID <<
  682. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  683. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  684. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  685. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  686. class_code_revision =
  687. __gxio_mmio_read32(trio_context->mmio_base_mac +
  688. reg_offset);
  689. class_code_revision = (class_code_revision & 0xff) |
  690. (PCI_CLASS_BRIDGE_PCI << 16);
  691. __gxio_mmio_write32(trio_context->mmio_base_mac +
  692. reg_offset, class_code_revision);
  693. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  694. /* Map in the MMIO space for the PIO region. */
  695. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index) |
  696. (((unsigned long long)mac) <<
  697. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  698. #else
  699. /* Alloc a PIO region for PCI config access per MAC. */
  700. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  701. if (ret < 0) {
  702. pr_err("PCI: PCI CFG PIO alloc failure for mac %d on TRIO %d, give up\n",
  703. mac, trio_index);
  704. continue;
  705. }
  706. trio_context->pio_cfg_index[mac] = ret;
  707. /* For PIO CFG, the bus_address_hi parameter is 0. */
  708. ret = gxio_trio_init_pio_region_aux(trio_context,
  709. trio_context->pio_cfg_index[mac],
  710. mac, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  711. if (ret < 0) {
  712. pr_err("PCI: PCI CFG PIO init failure for mac %d on TRIO %d, give up\n",
  713. mac, trio_index);
  714. continue;
  715. }
  716. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index[mac]) |
  717. (((unsigned long long)mac) <<
  718. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  719. #endif
  720. /*
  721. * To save VMALLOC space, we take advantage of the fact that
  722. * bit 29 in the PIO CFG address format is reserved 0. With
  723. * TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT being 30,
  724. * this cuts VMALLOC space usage from 1GB to 512MB per mac.
  725. */
  726. trio_context->mmio_base_pio_cfg[mac] =
  727. iorpc_ioremap(trio_context->fd, offset, (1UL <<
  728. (TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT - 1)));
  729. if (trio_context->mmio_base_pio_cfg[mac] == NULL) {
  730. pr_err("PCI: PIO map failure for mac %d on TRIO %d\n",
  731. mac, trio_index);
  732. continue;
  733. }
  734. /* Initialize the PCIe interrupts. */
  735. if (tile_init_irqs(controller)) {
  736. pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
  737. mac, trio_index);
  738. continue;
  739. }
  740. /*
  741. * The PCI memory resource is located above the PA space.
  742. * The memory range for the PCI root bus should not overlap
  743. * with the physical RAM.
  744. */
  745. pci_add_resource_offset(&resources, &controller->mem_space,
  746. controller->mem_offset);
  747. pci_add_resource(&resources, &controller->io_space);
  748. controller->first_busno = next_busno;
  749. bus = pci_scan_root_bus(NULL, next_busno, controller->ops,
  750. controller, &resources);
  751. controller->root_bus = bus;
  752. next_busno = bus->busn_res.end + 1;
  753. }
  754. /* Do machine dependent PCI interrupt routing */
  755. pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
  756. /*
  757. * This comes from the generic Linux PCI driver.
  758. *
  759. * It allocates all of the resources (I/O memory, etc)
  760. * associated with the devices read in above.
  761. */
  762. pci_assign_unassigned_resources();
  763. /* Record the I/O resources in the PCI controller structure. */
  764. for (i = 0; i < num_rc_controllers; i++) {
  765. struct pci_controller *controller = &pci_controllers[i];
  766. gxio_trio_context_t *trio_context = controller->trio;
  767. struct pci_bus *root_bus = pci_controllers[i].root_bus;
  768. int ret;
  769. int j;
  770. /*
  771. * Skip controllers that are not properly initialized or
  772. * have down links.
  773. */
  774. if (root_bus == NULL)
  775. continue;
  776. /* Configure the max_payload_size values for this domain. */
  777. fixup_read_and_payload_sizes(controller);
  778. /* Alloc a PIO region for PCI memory access for each RC port. */
  779. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  780. if (ret < 0) {
  781. pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, give up\n",
  782. controller->trio_index, controller->mac);
  783. continue;
  784. }
  785. controller->pio_mem_index = ret;
  786. /*
  787. * For PIO MEM, the bus_address_hi parameter is hard-coded 0
  788. * because we always assign 32-bit PCI bus BAR ranges.
  789. */
  790. ret = gxio_trio_init_pio_region_aux(trio_context,
  791. controller->pio_mem_index,
  792. controller->mac,
  793. 0,
  794. 0);
  795. if (ret < 0) {
  796. pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, give up\n",
  797. controller->trio_index, controller->mac);
  798. continue;
  799. }
  800. #ifdef CONFIG_TILE_PCI_IO
  801. /*
  802. * Alloc a PIO region for PCI I/O space access for each RC port.
  803. */
  804. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  805. if (ret < 0) {
  806. pr_err("PCI: I/O PIO alloc failure on TRIO %d mac %d, give up\n",
  807. controller->trio_index, controller->mac);
  808. continue;
  809. }
  810. controller->pio_io_index = ret;
  811. /*
  812. * For PIO IO, the bus_address_hi parameter is hard-coded 0
  813. * because PCI I/O address space is 32-bit.
  814. */
  815. ret = gxio_trio_init_pio_region_aux(trio_context,
  816. controller->pio_io_index,
  817. controller->mac,
  818. 0,
  819. HV_TRIO_PIO_FLAG_IO_SPACE);
  820. if (ret < 0) {
  821. pr_err("PCI: I/O PIO init failure on TRIO %d mac %d, give up\n",
  822. controller->trio_index, controller->mac);
  823. continue;
  824. }
  825. #endif
  826. /*
  827. * Configure a Mem-Map region for each memory controller so
  828. * that Linux can map all of its PA space to the PCI bus.
  829. * Use the IOMMU to handle hash-for-home memory.
  830. */
  831. for_each_online_node(j) {
  832. unsigned long start_pfn = node_start_pfn[j];
  833. unsigned long end_pfn = node_end_pfn[j];
  834. unsigned long nr_pages = end_pfn - start_pfn;
  835. ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
  836. 0);
  837. if (ret < 0) {
  838. pr_err("PCI: Mem-Map alloc failure on TRIO %d mac %d for MC %d, give up\n",
  839. controller->trio_index, controller->mac,
  840. j);
  841. goto alloc_mem_map_failed;
  842. }
  843. controller->mem_maps[j] = ret;
  844. /*
  845. * Initialize the Mem-Map and the I/O MMU so that all
  846. * the physical memory can be accessed by the endpoint
  847. * devices. The base bus address is set to the base CPA
  848. * of this memory controller plus an offset (see pci.h).
  849. * The region's base VA is set to the base CPA. The
  850. * I/O MMU table essentially translates the CPA to
  851. * the real PA. Implicitly, for node 0, we create
  852. * a separate Mem-Map region that serves as the inbound
  853. * window for legacy 32-bit devices. This is a direct
  854. * map of the low 4GB CPA space.
  855. */
  856. ret = gxio_trio_init_memory_map_mmu_aux(trio_context,
  857. controller->mem_maps[j],
  858. start_pfn << PAGE_SHIFT,
  859. nr_pages << PAGE_SHIFT,
  860. trio_context->asid,
  861. controller->mac,
  862. (start_pfn << PAGE_SHIFT) +
  863. TILE_PCI_MEM_MAP_BASE_OFFSET,
  864. j,
  865. GXIO_TRIO_ORDER_MODE_UNORDERED);
  866. if (ret < 0) {
  867. pr_err("PCI: Mem-Map init failure on TRIO %d mac %d for MC %d, give up\n",
  868. controller->trio_index, controller->mac,
  869. j);
  870. goto alloc_mem_map_failed;
  871. }
  872. continue;
  873. alloc_mem_map_failed:
  874. break;
  875. }
  876. pci_bus_add_devices(root_bus);
  877. }
  878. return 0;
  879. }
  880. subsys_initcall(pcibios_init);
  881. /* No bus fixups needed. */
  882. void pcibios_fixup_bus(struct pci_bus *bus)
  883. {
  884. }
  885. /* Process any "pci=" kernel boot arguments. */
  886. char *__init pcibios_setup(char *str)
  887. {
  888. if (!strcmp(str, "off")) {
  889. pci_probe = 0;
  890. return NULL;
  891. }
  892. return str;
  893. }
  894. /*
  895. * Called for each device after PCI setup is done.
  896. * We initialize the PCI device capabilities conservatively, assuming that
  897. * all devices can only address the 32-bit DMA space. The exception here is
  898. * that the device dma_offset is set to the value that matches the 64-bit
  899. * capable devices. This is OK because dma_offset is not used by legacy
  900. * dma_ops, nor by the hybrid dma_ops's streaming DMAs, which are 64-bit ops.
  901. * This implementation matches the kernel design of setting PCI devices'
  902. * coherent_dma_mask to 0xffffffffull by default, allowing the device drivers
  903. * to skip calling pci_set_consistent_dma_mask(DMA_BIT_MASK(32)).
  904. */
  905. static void pcibios_fixup_final(struct pci_dev *pdev)
  906. {
  907. set_dma_ops(&pdev->dev, gx_legacy_pci_dma_map_ops);
  908. set_dma_offset(&pdev->dev, TILE_PCI_MEM_MAP_BASE_OFFSET);
  909. pdev->dev.archdata.max_direct_dma_addr =
  910. TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
  911. pdev->dev.coherent_dma_mask = TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
  912. }
  913. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  914. /* Map a PCI MMIO bus address into VA space. */
  915. void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
  916. {
  917. struct pci_controller *controller = NULL;
  918. resource_size_t bar_start;
  919. resource_size_t bar_end;
  920. resource_size_t offset;
  921. resource_size_t start;
  922. resource_size_t end;
  923. int trio_fd;
  924. int i;
  925. start = phys_addr;
  926. end = phys_addr + size - 1;
  927. /*
  928. * By searching phys_addr in each controller's mem_space, we can
  929. * determine the controller that should accept the PCI memory access.
  930. */
  931. for (i = 0; i < num_rc_controllers; i++) {
  932. /*
  933. * Skip controllers that are not properly initialized or
  934. * have down links.
  935. */
  936. if (pci_controllers[i].root_bus == NULL)
  937. continue;
  938. bar_start = pci_controllers[i].mem_space.start;
  939. bar_end = pci_controllers[i].mem_space.end;
  940. if ((start >= bar_start) && (end <= bar_end)) {
  941. controller = &pci_controllers[i];
  942. break;
  943. }
  944. }
  945. if (controller == NULL)
  946. return NULL;
  947. trio_fd = controller->trio->fd;
  948. /* Convert the resource start to the bus address offset. */
  949. start = phys_addr - controller->mem_offset;
  950. offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + start;
  951. /* We need to keep the PCI bus address's in-page offset in the VA. */
  952. return iorpc_ioremap(trio_fd, offset, size) +
  953. (start & (PAGE_SIZE - 1));
  954. }
  955. EXPORT_SYMBOL(ioremap);
  956. #ifdef CONFIG_TILE_PCI_IO
  957. /* Map a PCI I/O address into VA space. */
  958. void __iomem *ioport_map(unsigned long port, unsigned int size)
  959. {
  960. struct pci_controller *controller = NULL;
  961. resource_size_t bar_start;
  962. resource_size_t bar_end;
  963. resource_size_t offset;
  964. resource_size_t start;
  965. resource_size_t end;
  966. int trio_fd;
  967. int i;
  968. start = port;
  969. end = port + size - 1;
  970. /*
  971. * By searching the port in each controller's io_space, we can
  972. * determine the controller that should accept the PCI I/O access.
  973. */
  974. for (i = 0; i < num_rc_controllers; i++) {
  975. /*
  976. * Skip controllers that are not properly initialized or
  977. * have down links.
  978. */
  979. if (pci_controllers[i].root_bus == NULL)
  980. continue;
  981. bar_start = pci_controllers[i].io_space.start;
  982. bar_end = pci_controllers[i].io_space.end;
  983. if ((start >= bar_start) && (end <= bar_end)) {
  984. controller = &pci_controllers[i];
  985. break;
  986. }
  987. }
  988. if (controller == NULL)
  989. return NULL;
  990. trio_fd = controller->trio->fd;
  991. /* Convert the resource start to the bus address offset. */
  992. port -= controller->io_space.start;
  993. offset = HV_TRIO_PIO_OFFSET(controller->pio_io_index) + port;
  994. /* We need to keep the PCI bus address's in-page offset in the VA. */
  995. return iorpc_ioremap(trio_fd, offset, size) + (port & (PAGE_SIZE - 1));
  996. }
  997. EXPORT_SYMBOL(ioport_map);
  998. void ioport_unmap(void __iomem *addr)
  999. {
  1000. iounmap(addr);
  1001. }
  1002. EXPORT_SYMBOL(ioport_unmap);
  1003. #endif
  1004. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1005. {
  1006. iounmap(addr);
  1007. }
  1008. EXPORT_SYMBOL(pci_iounmap);
  1009. /****************************************************************
  1010. *
  1011. * Tile PCI config space read/write routines
  1012. *
  1013. ****************************************************************/
  1014. /*
  1015. * These are the normal read and write ops
  1016. * These are expanded with macros from pci_bus_read_config_byte() etc.
  1017. *
  1018. * devfn is the combined PCI device & function.
  1019. *
  1020. * offset is in bytes, from the start of config space for the
  1021. * specified bus & device.
  1022. */
  1023. static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
  1024. int size, u32 *val)
  1025. {
  1026. struct pci_controller *controller = bus->sysdata;
  1027. gxio_trio_context_t *trio_context = controller->trio;
  1028. int busnum = bus->number & 0xff;
  1029. int device = PCI_SLOT(devfn);
  1030. int function = PCI_FUNC(devfn);
  1031. int config_type = 1;
  1032. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1033. void *mmio_addr;
  1034. /*
  1035. * Map all accesses to the local device on root bus into the
  1036. * MMIO space of the MAC. Accesses to the downstream devices
  1037. * go to the PIO space.
  1038. */
  1039. if (pci_is_root_bus(bus)) {
  1040. if (device == 0) {
  1041. /*
  1042. * This is the internal downstream P2P bridge,
  1043. * access directly.
  1044. */
  1045. unsigned int reg_offset;
  1046. reg_offset = ((offset & 0xFFF) <<
  1047. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1048. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1049. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1050. (controller->mac <<
  1051. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1052. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1053. goto valid_device;
  1054. } else {
  1055. /*
  1056. * We fake an empty device for (device > 0),
  1057. * since there is only one device on bus 0.
  1058. */
  1059. goto invalid_device;
  1060. }
  1061. }
  1062. /*
  1063. * Accesses to the directly attached device have to be
  1064. * sent as type-0 configs.
  1065. */
  1066. if (busnum == (controller->first_busno + 1)) {
  1067. /*
  1068. * There is only one device off of our built-in P2P bridge.
  1069. */
  1070. if (device != 0)
  1071. goto invalid_device;
  1072. config_type = 0;
  1073. }
  1074. cfg_addr.word = 0;
  1075. cfg_addr.reg_addr = (offset & 0xFFF);
  1076. cfg_addr.fn = function;
  1077. cfg_addr.dev = device;
  1078. cfg_addr.bus = busnum;
  1079. cfg_addr.type = config_type;
  1080. /*
  1081. * Note that we don't set the mac field in cfg_addr because the
  1082. * mapping is per port.
  1083. */
  1084. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1085. cfg_addr.word;
  1086. valid_device:
  1087. switch (size) {
  1088. case 4:
  1089. *val = __gxio_mmio_read32(mmio_addr);
  1090. break;
  1091. case 2:
  1092. *val = __gxio_mmio_read16(mmio_addr);
  1093. break;
  1094. case 1:
  1095. *val = __gxio_mmio_read8(mmio_addr);
  1096. break;
  1097. default:
  1098. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1099. }
  1100. TRACE_CFG_RD(size, *val, busnum, device, function, offset);
  1101. return 0;
  1102. invalid_device:
  1103. switch (size) {
  1104. case 4:
  1105. *val = 0xFFFFFFFF;
  1106. break;
  1107. case 2:
  1108. *val = 0xFFFF;
  1109. break;
  1110. case 1:
  1111. *val = 0xFF;
  1112. break;
  1113. default:
  1114. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1115. }
  1116. return 0;
  1117. }
  1118. /*
  1119. * See tile_cfg_read() for relevent comments.
  1120. * Note that "val" is the value to write, not a pointer to that value.
  1121. */
  1122. static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
  1123. int size, u32 val)
  1124. {
  1125. struct pci_controller *controller = bus->sysdata;
  1126. gxio_trio_context_t *trio_context = controller->trio;
  1127. int busnum = bus->number & 0xff;
  1128. int device = PCI_SLOT(devfn);
  1129. int function = PCI_FUNC(devfn);
  1130. int config_type = 1;
  1131. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1132. void *mmio_addr;
  1133. u32 val_32 = (u32)val;
  1134. u16 val_16 = (u16)val;
  1135. u8 val_8 = (u8)val;
  1136. /*
  1137. * Map all accesses to the local device on root bus into the
  1138. * MMIO space of the MAC. Accesses to the downstream devices
  1139. * go to the PIO space.
  1140. */
  1141. if (pci_is_root_bus(bus)) {
  1142. if (device == 0) {
  1143. /*
  1144. * This is the internal downstream P2P bridge,
  1145. * access directly.
  1146. */
  1147. unsigned int reg_offset;
  1148. reg_offset = ((offset & 0xFFF) <<
  1149. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1150. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1151. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1152. (controller->mac <<
  1153. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1154. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1155. goto valid_device;
  1156. } else {
  1157. /*
  1158. * We fake an empty device for (device > 0),
  1159. * since there is only one device on bus 0.
  1160. */
  1161. goto invalid_device;
  1162. }
  1163. }
  1164. /*
  1165. * Accesses to the directly attached device have to be
  1166. * sent as type-0 configs.
  1167. */
  1168. if (busnum == (controller->first_busno + 1)) {
  1169. /*
  1170. * There is only one device off of our built-in P2P bridge.
  1171. */
  1172. if (device != 0)
  1173. goto invalid_device;
  1174. config_type = 0;
  1175. }
  1176. cfg_addr.word = 0;
  1177. cfg_addr.reg_addr = (offset & 0xFFF);
  1178. cfg_addr.fn = function;
  1179. cfg_addr.dev = device;
  1180. cfg_addr.bus = busnum;
  1181. cfg_addr.type = config_type;
  1182. /*
  1183. * Note that we don't set the mac field in cfg_addr because the
  1184. * mapping is per port.
  1185. */
  1186. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1187. cfg_addr.word;
  1188. valid_device:
  1189. switch (size) {
  1190. case 4:
  1191. __gxio_mmio_write32(mmio_addr, val_32);
  1192. TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
  1193. break;
  1194. case 2:
  1195. __gxio_mmio_write16(mmio_addr, val_16);
  1196. TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
  1197. break;
  1198. case 1:
  1199. __gxio_mmio_write8(mmio_addr, val_8);
  1200. TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
  1201. break;
  1202. default:
  1203. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1204. }
  1205. invalid_device:
  1206. return 0;
  1207. }
  1208. static struct pci_ops tile_cfg_ops = {
  1209. .read = tile_cfg_read,
  1210. .write = tile_cfg_write,
  1211. };
  1212. /* MSI support starts here. */
  1213. static unsigned int tilegx_msi_startup(struct irq_data *d)
  1214. {
  1215. if (irq_data_get_msi_desc(d))
  1216. pci_msi_unmask_irq(d);
  1217. return 0;
  1218. }
  1219. static void tilegx_msi_ack(struct irq_data *d)
  1220. {
  1221. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  1222. }
  1223. static void tilegx_msi_mask(struct irq_data *d)
  1224. {
  1225. pci_msi_mask_irq(d);
  1226. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  1227. }
  1228. static void tilegx_msi_unmask(struct irq_data *d)
  1229. {
  1230. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  1231. pci_msi_unmask_irq(d);
  1232. }
  1233. static struct irq_chip tilegx_msi_chip = {
  1234. .name = "tilegx_msi",
  1235. .irq_startup = tilegx_msi_startup,
  1236. .irq_ack = tilegx_msi_ack,
  1237. .irq_mask = tilegx_msi_mask,
  1238. .irq_unmask = tilegx_msi_unmask,
  1239. /* TBD: support set_affinity. */
  1240. };
  1241. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  1242. {
  1243. struct pci_controller *controller;
  1244. gxio_trio_context_t *trio_context;
  1245. struct msi_msg msg;
  1246. int default_irq;
  1247. uint64_t mem_map_base;
  1248. uint64_t mem_map_limit;
  1249. u64 msi_addr;
  1250. int mem_map;
  1251. int cpu;
  1252. int irq;
  1253. int ret;
  1254. irq = irq_alloc_hwirq(-1);
  1255. if (!irq)
  1256. return -ENOSPC;
  1257. /*
  1258. * Since we use a 64-bit Mem-Map to accept the MSI write, we fail
  1259. * devices that are not capable of generating a 64-bit message address.
  1260. * These devices will fall back to using the legacy interrupts.
  1261. * Most PCIe endpoint devices do support 64-bit message addressing.
  1262. */
  1263. if (desc->msi_attrib.is_64 == 0) {
  1264. dev_info(&pdev->dev, "64-bit MSI message address not supported, falling back to legacy interrupts\n");
  1265. ret = -ENOMEM;
  1266. goto is_64_failure;
  1267. }
  1268. default_irq = desc->msi_attrib.default_irq;
  1269. controller = irq_get_handler_data(default_irq);
  1270. BUG_ON(!controller);
  1271. trio_context = controller->trio;
  1272. /*
  1273. * Allocate a scatter-queue that will accept the MSI write and
  1274. * trigger the TILE-side interrupts. We use the scatter-queue regions
  1275. * before the mem map regions, because the latter are needed by more
  1276. * applications.
  1277. */
  1278. mem_map = gxio_trio_alloc_scatter_queues(trio_context, 1, 0, 0);
  1279. if (mem_map >= 0) {
  1280. TRIO_MAP_SQ_DOORBELL_FMT_t doorbell_template = {{
  1281. .pop = 0,
  1282. .doorbell = 1,
  1283. }};
  1284. mem_map += TRIO_NUM_MAP_MEM_REGIONS;
  1285. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1286. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1287. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1288. msi_addr = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 8;
  1289. msg.data = (unsigned int)doorbell_template.word;
  1290. } else {
  1291. /* SQ regions are out, allocate from map mem regions. */
  1292. mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
  1293. if (mem_map < 0) {
  1294. dev_info(&pdev->dev, "%s Mem-Map alloc failure - failed to initialize MSI interrupts - falling back to legacy interrupts\n",
  1295. desc->msi_attrib.is_msix ? "MSI-X" : "MSI");
  1296. ret = -ENOMEM;
  1297. goto msi_mem_map_alloc_failure;
  1298. }
  1299. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1300. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1301. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1302. msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 -
  1303. TRIO_MAP_MEM_REG_INT0;
  1304. msg.data = mem_map;
  1305. }
  1306. /* We try to distribute different IRQs to different tiles. */
  1307. cpu = tile_irq_cpu(irq);
  1308. /*
  1309. * Now call up to the HV to configure the MSI interrupt and
  1310. * set up the IPI binding.
  1311. */
  1312. ret = gxio_trio_config_msi_intr(trio_context, cpu_x(cpu), cpu_y(cpu),
  1313. KERNEL_PL, irq, controller->mac,
  1314. mem_map, mem_map_base, mem_map_limit,
  1315. trio_context->asid);
  1316. if (ret < 0) {
  1317. dev_info(&pdev->dev, "HV MSI config failed\n");
  1318. goto hv_msi_config_failure;
  1319. }
  1320. irq_set_msi_desc(irq, desc);
  1321. msg.address_hi = msi_addr >> 32;
  1322. msg.address_lo = msi_addr & 0xffffffff;
  1323. pci_write_msi_msg(irq, &msg);
  1324. irq_set_chip_and_handler(irq, &tilegx_msi_chip, handle_level_irq);
  1325. irq_set_handler_data(irq, controller);
  1326. return 0;
  1327. hv_msi_config_failure:
  1328. /* Free mem-map */
  1329. msi_mem_map_alloc_failure:
  1330. is_64_failure:
  1331. irq_free_hwirq(irq);
  1332. return ret;
  1333. }
  1334. void arch_teardown_msi_irq(unsigned int irq)
  1335. {
  1336. irq_free_hwirq(irq);
  1337. }