pci_gx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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_warning("intr_cpus_map empty, interrupts will be"
  113. " delievered to dataplane tiles\n");
  114. return irq % (smp_height * smp_width);
  115. }
  116. count = irq % count;
  117. for_each_cpu(cpu, &intr_cpus_map) {
  118. if (i++ == count)
  119. break;
  120. }
  121. return cpu;
  122. }
  123. /* Open a file descriptor to the TRIO shim. */
  124. static int tile_pcie_open(int trio_index)
  125. {
  126. gxio_trio_context_t *context = &trio_contexts[trio_index];
  127. int ret;
  128. int mac;
  129. /* This opens a file descriptor to the TRIO shim. */
  130. ret = gxio_trio_init(context, trio_index);
  131. if (ret < 0)
  132. goto gxio_trio_init_failure;
  133. /* Allocate an ASID for the kernel. */
  134. ret = gxio_trio_alloc_asids(context, 1, 0, 0);
  135. if (ret < 0) {
  136. pr_err("PCI: ASID alloc failure on TRIO %d, give up\n",
  137. trio_index);
  138. goto asid_alloc_failure;
  139. }
  140. context->asid = ret;
  141. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  142. /*
  143. * Alloc a PIO region for config access, shared by all MACs per TRIO.
  144. * This shouldn't fail since the kernel is supposed to the first
  145. * client of the TRIO's PIO regions.
  146. */
  147. ret = gxio_trio_alloc_pio_regions(context, 1, 0, 0);
  148. if (ret < 0) {
  149. pr_err("PCI: CFG PIO alloc failure on TRIO %d, give up\n",
  150. trio_index);
  151. goto pio_alloc_failure;
  152. }
  153. context->pio_cfg_index = ret;
  154. /*
  155. * For PIO CFG, the bus_address_hi parameter is 0. The mac parameter
  156. * is also 0 because it is specified in PIO_REGION_SETUP_CFG_ADDR.
  157. */
  158. ret = gxio_trio_init_pio_region_aux(context, context->pio_cfg_index,
  159. 0, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  160. if (ret < 0) {
  161. pr_err("PCI: CFG PIO init failure on TRIO %d, give up\n",
  162. trio_index);
  163. goto pio_alloc_failure;
  164. }
  165. #endif
  166. /* Get the properties of the PCIe ports on this TRIO instance. */
  167. ret = gxio_trio_get_port_property(context, &pcie_ports[trio_index]);
  168. if (ret < 0) {
  169. pr_err("PCI: PCIE_GET_PORT_PROPERTY failure, error %d,"
  170. " on TRIO %d\n", ret, trio_index);
  171. goto get_port_property_failure;
  172. }
  173. context->mmio_base_mac =
  174. iorpc_ioremap(context->fd, 0, HV_TRIO_CONFIG_IOREMAP_SIZE);
  175. if (context->mmio_base_mac == NULL) {
  176. pr_err("PCI: TRIO config space mapping failure, error %d,"
  177. " on TRIO %d\n", ret, trio_index);
  178. ret = -ENOMEM;
  179. goto trio_mmio_mapping_failure;
  180. }
  181. /* Check the port strap state which will override the BIB setting. */
  182. for (mac = 0; mac < TILEGX_TRIO_PCIES; mac++) {
  183. TRIO_PCIE_INTFC_PORT_CONFIG_t port_config;
  184. unsigned int reg_offset;
  185. /* Ignore ports that are not specified in the BIB. */
  186. if (!pcie_ports[trio_index].ports[mac].allow_rc &&
  187. !pcie_ports[trio_index].ports[mac].allow_ep)
  188. continue;
  189. reg_offset =
  190. (TRIO_PCIE_INTFC_PORT_CONFIG <<
  191. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  192. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  193. TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
  194. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  195. port_config.word =
  196. __gxio_mmio_read(context->mmio_base_mac + reg_offset);
  197. if (port_config.strap_state != AUTO_CONFIG_RC &&
  198. port_config.strap_state != AUTO_CONFIG_RC_G1) {
  199. /*
  200. * If this is really intended to be an EP port, record
  201. * it so that the endpoint driver will know about it.
  202. */
  203. if (port_config.strap_state == AUTO_CONFIG_EP ||
  204. port_config.strap_state == AUTO_CONFIG_EP_G1)
  205. pcie_ports[trio_index].ports[mac].allow_ep = 1;
  206. }
  207. }
  208. return ret;
  209. trio_mmio_mapping_failure:
  210. get_port_property_failure:
  211. asid_alloc_failure:
  212. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  213. pio_alloc_failure:
  214. #endif
  215. hv_dev_close(context->fd);
  216. gxio_trio_init_failure:
  217. context->fd = -1;
  218. return ret;
  219. }
  220. static int __init tile_trio_init(void)
  221. {
  222. int i;
  223. /* We loop over all the TRIO shims. */
  224. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  225. if (tile_pcie_open(i) < 0)
  226. continue;
  227. num_trio_shims++;
  228. }
  229. return 0;
  230. }
  231. postcore_initcall(tile_trio_init);
  232. static void tilegx_legacy_irq_ack(struct irq_data *d)
  233. {
  234. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  235. }
  236. static void tilegx_legacy_irq_mask(struct irq_data *d)
  237. {
  238. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  239. }
  240. static void tilegx_legacy_irq_unmask(struct irq_data *d)
  241. {
  242. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  243. }
  244. static struct irq_chip tilegx_legacy_irq_chip = {
  245. .name = "tilegx_legacy_irq",
  246. .irq_ack = tilegx_legacy_irq_ack,
  247. .irq_mask = tilegx_legacy_irq_mask,
  248. .irq_unmask = tilegx_legacy_irq_unmask,
  249. /* TBD: support set_affinity. */
  250. };
  251. /*
  252. * This is a wrapper function of the kernel level-trigger interrupt
  253. * handler handle_level_irq() for PCI legacy interrupts. The TRIO
  254. * is configured such that only INTx Assert interrupts are proxied
  255. * to Linux which just calls handle_level_irq() after clearing the
  256. * MAC INTx Assert status bit associated with this interrupt.
  257. */
  258. static void trio_handle_level_irq(unsigned int irq, struct irq_desc *desc)
  259. {
  260. struct pci_controller *controller = irq_desc_get_handler_data(desc);
  261. gxio_trio_context_t *trio_context = controller->trio;
  262. uint64_t intx = (uint64_t)irq_desc_get_chip_data(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 = create_irq();
  295. if (irq < 0) {
  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. destroy_irq(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, "
  530. "MAC %d on TRIO %d\n",
  531. mac, controller->trio_index);
  532. }
  533. }
  534. static int setup_pcie_rc_delay(char *str)
  535. {
  536. unsigned long delay = 0;
  537. unsigned long trio_index;
  538. unsigned long mac;
  539. if (str == NULL || !isdigit(*str))
  540. return -EINVAL;
  541. trio_index = simple_strtoul(str, (char **)&str, 10);
  542. if (trio_index >= TILEGX_NUM_TRIO)
  543. return -EINVAL;
  544. if (*str != ',')
  545. return -EINVAL;
  546. str++;
  547. if (!isdigit(*str))
  548. return -EINVAL;
  549. mac = simple_strtoul(str, (char **)&str, 10);
  550. if (mac >= TILEGX_TRIO_PCIES)
  551. return -EINVAL;
  552. if (*str != '\0') {
  553. if (*str != ',')
  554. return -EINVAL;
  555. str++;
  556. if (!isdigit(*str))
  557. return -EINVAL;
  558. delay = simple_strtoul(str, (char **)&str, 10);
  559. }
  560. rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
  561. return 0;
  562. }
  563. early_param("pcie_rc_delay", setup_pcie_rc_delay);
  564. /* PCI initialization entry point, called by subsys_initcall. */
  565. int __init pcibios_init(void)
  566. {
  567. resource_size_t offset;
  568. LIST_HEAD(resources);
  569. int next_busno;
  570. int i;
  571. tile_pci_init();
  572. if (num_rc_controllers == 0)
  573. return 0;
  574. /*
  575. * Delay a bit in case devices aren't ready. Some devices are
  576. * known to require at least 20ms here, but we use a more
  577. * conservative value.
  578. */
  579. msleep(250);
  580. /* Scan all of the recorded PCI controllers. */
  581. for (next_busno = 0, i = 0; i < num_rc_controllers; i++) {
  582. struct pci_controller *controller = &pci_controllers[i];
  583. gxio_trio_context_t *trio_context = controller->trio;
  584. TRIO_PCIE_INTFC_PORT_STATUS_t port_status;
  585. TRIO_PCIE_INTFC_TX_FIFO_CTL_t tx_fifo_ctl;
  586. struct pci_bus *bus;
  587. unsigned int reg_offset;
  588. unsigned int class_code_revision;
  589. int trio_index;
  590. int mac;
  591. int ret;
  592. if (trio_context->fd < 0)
  593. continue;
  594. trio_index = controller->trio_index;
  595. mac = controller->mac;
  596. /*
  597. * Check for PCIe link-up status to decide if we need
  598. * to force the link to come up.
  599. */
  600. reg_offset =
  601. (TRIO_PCIE_INTFC_PORT_STATUS <<
  602. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  603. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  604. TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
  605. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  606. port_status.word =
  607. __gxio_mmio_read(trio_context->mmio_base_mac +
  608. reg_offset);
  609. if (!port_status.dl_up) {
  610. if (rc_delay[trio_index][mac]) {
  611. pr_info("Delaying PCIe RC TRIO init %d sec"
  612. " on MAC %d on TRIO %d\n",
  613. rc_delay[trio_index][mac], mac,
  614. trio_index);
  615. msleep(rc_delay[trio_index][mac] * 1000);
  616. }
  617. ret = gxio_trio_force_rc_link_up(trio_context, mac);
  618. if (ret < 0)
  619. pr_err("PCI: PCIE_FORCE_LINK_UP failure, "
  620. "MAC %d on TRIO %d\n", mac, trio_index);
  621. }
  622. pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n", i,
  623. trio_index, controller->mac);
  624. /* Delay the bus probe if needed. */
  625. if (rc_delay[trio_index][mac]) {
  626. pr_info("Delaying PCIe RC bus enumerating %d sec"
  627. " on MAC %d on TRIO %d\n",
  628. rc_delay[trio_index][mac], mac,
  629. trio_index);
  630. msleep(rc_delay[trio_index][mac] * 1000);
  631. } else {
  632. /*
  633. * Wait a bit here because some EP devices
  634. * take longer to come up.
  635. */
  636. msleep(1000);
  637. }
  638. /* Check for PCIe link-up status again. */
  639. port_status.word =
  640. __gxio_mmio_read(trio_context->mmio_base_mac +
  641. reg_offset);
  642. if (!port_status.dl_up) {
  643. if (pcie_ports[trio_index].ports[mac].removable) {
  644. pr_info("PCI: link is down, MAC %d on TRIO %d\n",
  645. mac, trio_index);
  646. pr_info("This is expected if no PCIe card"
  647. " is connected to this link\n");
  648. } else
  649. pr_err("PCI: link is down, MAC %d on TRIO %d\n",
  650. mac, trio_index);
  651. continue;
  652. }
  653. /*
  654. * Ensure that the link can come out of L1 power down state.
  655. * Strictly speaking, this is needed only in the case of
  656. * heavy RC-initiated DMAs.
  657. */
  658. reg_offset =
  659. (TRIO_PCIE_INTFC_TX_FIFO_CTL <<
  660. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  661. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  662. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  663. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  664. tx_fifo_ctl.word =
  665. __gxio_mmio_read(trio_context->mmio_base_mac +
  666. reg_offset);
  667. tx_fifo_ctl.min_p_credits = 0;
  668. __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
  669. tx_fifo_ctl.word);
  670. /*
  671. * Change the device ID so that Linux bus crawl doesn't confuse
  672. * the internal bridge with any Tilera endpoints.
  673. */
  674. reg_offset =
  675. (TRIO_PCIE_RC_DEVICE_ID_VEN_ID <<
  676. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  677. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  678. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  679. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  680. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  681. (TILERA_GX36_RC_DEV_ID <<
  682. TRIO_PCIE_RC_DEVICE_ID_VEN_ID__DEV_ID_SHIFT) |
  683. TILERA_VENDOR_ID);
  684. /* Set the internal P2P bridge class code. */
  685. reg_offset =
  686. (TRIO_PCIE_RC_REVISION_ID <<
  687. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  688. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  689. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  690. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  691. class_code_revision =
  692. __gxio_mmio_read32(trio_context->mmio_base_mac +
  693. reg_offset);
  694. class_code_revision = (class_code_revision & 0xff) |
  695. (PCI_CLASS_BRIDGE_PCI << 16);
  696. __gxio_mmio_write32(trio_context->mmio_base_mac +
  697. reg_offset, class_code_revision);
  698. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  699. /* Map in the MMIO space for the PIO region. */
  700. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index) |
  701. (((unsigned long long)mac) <<
  702. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  703. #else
  704. /* Alloc a PIO region for PCI config access per MAC. */
  705. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  706. if (ret < 0) {
  707. pr_err("PCI: PCI CFG PIO alloc failure for mac %d "
  708. "on TRIO %d, give up\n", mac, trio_index);
  709. continue;
  710. }
  711. trio_context->pio_cfg_index[mac] = ret;
  712. /* For PIO CFG, the bus_address_hi parameter is 0. */
  713. ret = gxio_trio_init_pio_region_aux(trio_context,
  714. trio_context->pio_cfg_index[mac],
  715. mac, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  716. if (ret < 0) {
  717. pr_err("PCI: PCI CFG PIO init failure for mac %d "
  718. "on TRIO %d, give up\n", mac, trio_index);
  719. continue;
  720. }
  721. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index[mac]) |
  722. (((unsigned long long)mac) <<
  723. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  724. #endif
  725. /*
  726. * To save VMALLOC space, we take advantage of the fact that
  727. * bit 29 in the PIO CFG address format is reserved 0. With
  728. * TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT being 30,
  729. * this cuts VMALLOC space usage from 1GB to 512MB per mac.
  730. */
  731. trio_context->mmio_base_pio_cfg[mac] =
  732. iorpc_ioremap(trio_context->fd, offset, (1UL <<
  733. (TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT - 1)));
  734. if (trio_context->mmio_base_pio_cfg[mac] == NULL) {
  735. pr_err("PCI: PIO map failure for mac %d on TRIO %d\n",
  736. mac, trio_index);
  737. continue;
  738. }
  739. /* Initialize the PCIe interrupts. */
  740. if (tile_init_irqs(controller)) {
  741. pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
  742. mac, trio_index);
  743. continue;
  744. }
  745. /*
  746. * The PCI memory resource is located above the PA space.
  747. * The memory range for the PCI root bus should not overlap
  748. * with the physical RAM.
  749. */
  750. pci_add_resource_offset(&resources, &controller->mem_space,
  751. controller->mem_offset);
  752. pci_add_resource(&resources, &controller->io_space);
  753. controller->first_busno = next_busno;
  754. bus = pci_scan_root_bus(NULL, next_busno, controller->ops,
  755. controller, &resources);
  756. controller->root_bus = bus;
  757. next_busno = bus->busn_res.end + 1;
  758. }
  759. /* Do machine dependent PCI interrupt routing */
  760. pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
  761. /*
  762. * This comes from the generic Linux PCI driver.
  763. *
  764. * It allocates all of the resources (I/O memory, etc)
  765. * associated with the devices read in above.
  766. */
  767. pci_assign_unassigned_resources();
  768. /* Record the I/O resources in the PCI controller structure. */
  769. for (i = 0; i < num_rc_controllers; i++) {
  770. struct pci_controller *controller = &pci_controllers[i];
  771. gxio_trio_context_t *trio_context = controller->trio;
  772. struct pci_bus *root_bus = pci_controllers[i].root_bus;
  773. int ret;
  774. int j;
  775. /*
  776. * Skip controllers that are not properly initialized or
  777. * have down links.
  778. */
  779. if (root_bus == NULL)
  780. continue;
  781. /* Configure the max_payload_size values for this domain. */
  782. fixup_read_and_payload_sizes(controller);
  783. /* Alloc a PIO region for PCI memory access for each RC port. */
  784. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  785. if (ret < 0) {
  786. pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, "
  787. "give up\n", controller->trio_index,
  788. controller->mac);
  789. continue;
  790. }
  791. controller->pio_mem_index = ret;
  792. /*
  793. * For PIO MEM, the bus_address_hi parameter is hard-coded 0
  794. * because we always assign 32-bit PCI bus BAR ranges.
  795. */
  796. ret = gxio_trio_init_pio_region_aux(trio_context,
  797. controller->pio_mem_index,
  798. controller->mac,
  799. 0,
  800. 0);
  801. if (ret < 0) {
  802. pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, "
  803. "give up\n", controller->trio_index,
  804. controller->mac);
  805. continue;
  806. }
  807. #ifdef CONFIG_TILE_PCI_IO
  808. /*
  809. * Alloc a PIO region for PCI I/O space access for each RC port.
  810. */
  811. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  812. if (ret < 0) {
  813. pr_err("PCI: I/O PIO alloc failure on TRIO %d mac %d, "
  814. "give up\n", controller->trio_index,
  815. controller->mac);
  816. continue;
  817. }
  818. controller->pio_io_index = ret;
  819. /*
  820. * For PIO IO, the bus_address_hi parameter is hard-coded 0
  821. * because PCI I/O address space is 32-bit.
  822. */
  823. ret = gxio_trio_init_pio_region_aux(trio_context,
  824. controller->pio_io_index,
  825. controller->mac,
  826. 0,
  827. HV_TRIO_PIO_FLAG_IO_SPACE);
  828. if (ret < 0) {
  829. pr_err("PCI: I/O PIO init failure on TRIO %d mac %d, "
  830. "give up\n", controller->trio_index,
  831. controller->mac);
  832. continue;
  833. }
  834. #endif
  835. /*
  836. * Configure a Mem-Map region for each memory controller so
  837. * that Linux can map all of its PA space to the PCI bus.
  838. * Use the IOMMU to handle hash-for-home memory.
  839. */
  840. for_each_online_node(j) {
  841. unsigned long start_pfn = node_start_pfn[j];
  842. unsigned long end_pfn = node_end_pfn[j];
  843. unsigned long nr_pages = end_pfn - start_pfn;
  844. ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
  845. 0);
  846. if (ret < 0) {
  847. pr_err("PCI: Mem-Map alloc failure on TRIO %d "
  848. "mac %d for MC %d, give up\n",
  849. controller->trio_index,
  850. controller->mac, j);
  851. goto alloc_mem_map_failed;
  852. }
  853. controller->mem_maps[j] = ret;
  854. /*
  855. * Initialize the Mem-Map and the I/O MMU so that all
  856. * the physical memory can be accessed by the endpoint
  857. * devices. The base bus address is set to the base CPA
  858. * of this memory controller plus an offset (see pci.h).
  859. * The region's base VA is set to the base CPA. The
  860. * I/O MMU table essentially translates the CPA to
  861. * the real PA. Implicitly, for node 0, we create
  862. * a separate Mem-Map region that serves as the inbound
  863. * window for legacy 32-bit devices. This is a direct
  864. * map of the low 4GB CPA space.
  865. */
  866. ret = gxio_trio_init_memory_map_mmu_aux(trio_context,
  867. controller->mem_maps[j],
  868. start_pfn << PAGE_SHIFT,
  869. nr_pages << PAGE_SHIFT,
  870. trio_context->asid,
  871. controller->mac,
  872. (start_pfn << PAGE_SHIFT) +
  873. TILE_PCI_MEM_MAP_BASE_OFFSET,
  874. j,
  875. GXIO_TRIO_ORDER_MODE_UNORDERED);
  876. if (ret < 0) {
  877. pr_err("PCI: Mem-Map init failure on TRIO %d "
  878. "mac %d for MC %d, give up\n",
  879. controller->trio_index,
  880. controller->mac, j);
  881. goto alloc_mem_map_failed;
  882. }
  883. continue;
  884. alloc_mem_map_failed:
  885. break;
  886. }
  887. }
  888. return 0;
  889. }
  890. subsys_initcall(pcibios_init);
  891. /* No bus fixups needed. */
  892. void pcibios_fixup_bus(struct pci_bus *bus)
  893. {
  894. }
  895. /* Process any "pci=" kernel boot arguments. */
  896. char *__init pcibios_setup(char *str)
  897. {
  898. if (!strcmp(str, "off")) {
  899. pci_probe = 0;
  900. return NULL;
  901. }
  902. return str;
  903. }
  904. /*
  905. * Enable memory address decoding, as appropriate, for the
  906. * device described by the 'dev' struct.
  907. *
  908. * This is called from the generic PCI layer, and can be called
  909. * for bridges or endpoints.
  910. */
  911. int pcibios_enable_device(struct pci_dev *dev, int mask)
  912. {
  913. return pci_enable_resources(dev, mask);
  914. }
  915. /*
  916. * Called for each device after PCI setup is done.
  917. * We initialize the PCI device capabilities conservatively, assuming that
  918. * all devices can only address the 32-bit DMA space. The exception here is
  919. * that the device dma_offset is set to the value that matches the 64-bit
  920. * capable devices. This is OK because dma_offset is not used by legacy
  921. * dma_ops, nor by the hybrid dma_ops's streaming DMAs, which are 64-bit ops.
  922. * This implementation matches the kernel design of setting PCI devices'
  923. * coherent_dma_mask to 0xffffffffull by default, allowing the device drivers
  924. * to skip calling pci_set_consistent_dma_mask(DMA_BIT_MASK(32)).
  925. */
  926. static void pcibios_fixup_final(struct pci_dev *pdev)
  927. {
  928. set_dma_ops(&pdev->dev, gx_legacy_pci_dma_map_ops);
  929. set_dma_offset(&pdev->dev, TILE_PCI_MEM_MAP_BASE_OFFSET);
  930. pdev->dev.archdata.max_direct_dma_addr =
  931. TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
  932. pdev->dev.coherent_dma_mask = TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
  933. }
  934. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  935. /* Map a PCI MMIO bus address into VA space. */
  936. void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
  937. {
  938. struct pci_controller *controller = NULL;
  939. resource_size_t bar_start;
  940. resource_size_t bar_end;
  941. resource_size_t offset;
  942. resource_size_t start;
  943. resource_size_t end;
  944. int trio_fd;
  945. int i;
  946. start = phys_addr;
  947. end = phys_addr + size - 1;
  948. /*
  949. * By searching phys_addr in each controller's mem_space, we can
  950. * determine the controller that should accept the PCI memory access.
  951. */
  952. for (i = 0; i < num_rc_controllers; i++) {
  953. /*
  954. * Skip controllers that are not properly initialized or
  955. * have down links.
  956. */
  957. if (pci_controllers[i].root_bus == NULL)
  958. continue;
  959. bar_start = pci_controllers[i].mem_space.start;
  960. bar_end = pci_controllers[i].mem_space.end;
  961. if ((start >= bar_start) && (end <= bar_end)) {
  962. controller = &pci_controllers[i];
  963. break;
  964. }
  965. }
  966. if (controller == NULL)
  967. return NULL;
  968. trio_fd = controller->trio->fd;
  969. /* Convert the resource start to the bus address offset. */
  970. start = phys_addr - controller->mem_offset;
  971. offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + start;
  972. /* We need to keep the PCI bus address's in-page offset in the VA. */
  973. return iorpc_ioremap(trio_fd, offset, size) +
  974. (start & (PAGE_SIZE - 1));
  975. }
  976. EXPORT_SYMBOL(ioremap);
  977. #ifdef CONFIG_TILE_PCI_IO
  978. /* Map a PCI I/O address into VA space. */
  979. void __iomem *ioport_map(unsigned long port, unsigned int size)
  980. {
  981. struct pci_controller *controller = NULL;
  982. resource_size_t bar_start;
  983. resource_size_t bar_end;
  984. resource_size_t offset;
  985. resource_size_t start;
  986. resource_size_t end;
  987. int trio_fd;
  988. int i;
  989. start = port;
  990. end = port + size - 1;
  991. /*
  992. * By searching the port in each controller's io_space, we can
  993. * determine the controller that should accept the PCI I/O access.
  994. */
  995. for (i = 0; i < num_rc_controllers; i++) {
  996. /*
  997. * Skip controllers that are not properly initialized or
  998. * have down links.
  999. */
  1000. if (pci_controllers[i].root_bus == NULL)
  1001. continue;
  1002. bar_start = pci_controllers[i].io_space.start;
  1003. bar_end = pci_controllers[i].io_space.end;
  1004. if ((start >= bar_start) && (end <= bar_end)) {
  1005. controller = &pci_controllers[i];
  1006. break;
  1007. }
  1008. }
  1009. if (controller == NULL)
  1010. return NULL;
  1011. trio_fd = controller->trio->fd;
  1012. /* Convert the resource start to the bus address offset. */
  1013. port -= controller->io_space.start;
  1014. offset = HV_TRIO_PIO_OFFSET(controller->pio_io_index) + port;
  1015. /* We need to keep the PCI bus address's in-page offset in the VA. */
  1016. return iorpc_ioremap(trio_fd, offset, size) + (port & (PAGE_SIZE - 1));
  1017. }
  1018. EXPORT_SYMBOL(ioport_map);
  1019. void ioport_unmap(void __iomem *addr)
  1020. {
  1021. iounmap(addr);
  1022. }
  1023. EXPORT_SYMBOL(ioport_unmap);
  1024. #endif
  1025. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  1026. {
  1027. iounmap(addr);
  1028. }
  1029. EXPORT_SYMBOL(pci_iounmap);
  1030. /****************************************************************
  1031. *
  1032. * Tile PCI config space read/write routines
  1033. *
  1034. ****************************************************************/
  1035. /*
  1036. * These are the normal read and write ops
  1037. * These are expanded with macros from pci_bus_read_config_byte() etc.
  1038. *
  1039. * devfn is the combined PCI device & function.
  1040. *
  1041. * offset is in bytes, from the start of config space for the
  1042. * specified bus & device.
  1043. */
  1044. static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
  1045. int size, u32 *val)
  1046. {
  1047. struct pci_controller *controller = bus->sysdata;
  1048. gxio_trio_context_t *trio_context = controller->trio;
  1049. int busnum = bus->number & 0xff;
  1050. int device = PCI_SLOT(devfn);
  1051. int function = PCI_FUNC(devfn);
  1052. int config_type = 1;
  1053. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1054. void *mmio_addr;
  1055. /*
  1056. * Map all accesses to the local device on root bus into the
  1057. * MMIO space of the MAC. Accesses to the downstream devices
  1058. * go to the PIO space.
  1059. */
  1060. if (pci_is_root_bus(bus)) {
  1061. if (device == 0) {
  1062. /*
  1063. * This is the internal downstream P2P bridge,
  1064. * access directly.
  1065. */
  1066. unsigned int reg_offset;
  1067. reg_offset = ((offset & 0xFFF) <<
  1068. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1069. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1070. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1071. (controller->mac <<
  1072. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1073. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1074. goto valid_device;
  1075. } else {
  1076. /*
  1077. * We fake an empty device for (device > 0),
  1078. * since there is only one device on bus 0.
  1079. */
  1080. goto invalid_device;
  1081. }
  1082. }
  1083. /*
  1084. * Accesses to the directly attached device have to be
  1085. * sent as type-0 configs.
  1086. */
  1087. if (busnum == (controller->first_busno + 1)) {
  1088. /*
  1089. * There is only one device off of our built-in P2P bridge.
  1090. */
  1091. if (device != 0)
  1092. goto invalid_device;
  1093. config_type = 0;
  1094. }
  1095. cfg_addr.word = 0;
  1096. cfg_addr.reg_addr = (offset & 0xFFF);
  1097. cfg_addr.fn = function;
  1098. cfg_addr.dev = device;
  1099. cfg_addr.bus = busnum;
  1100. cfg_addr.type = config_type;
  1101. /*
  1102. * Note that we don't set the mac field in cfg_addr because the
  1103. * mapping is per port.
  1104. */
  1105. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1106. cfg_addr.word;
  1107. valid_device:
  1108. switch (size) {
  1109. case 4:
  1110. *val = __gxio_mmio_read32(mmio_addr);
  1111. break;
  1112. case 2:
  1113. *val = __gxio_mmio_read16(mmio_addr);
  1114. break;
  1115. case 1:
  1116. *val = __gxio_mmio_read8(mmio_addr);
  1117. break;
  1118. default:
  1119. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1120. }
  1121. TRACE_CFG_RD(size, *val, busnum, device, function, offset);
  1122. return 0;
  1123. invalid_device:
  1124. switch (size) {
  1125. case 4:
  1126. *val = 0xFFFFFFFF;
  1127. break;
  1128. case 2:
  1129. *val = 0xFFFF;
  1130. break;
  1131. case 1:
  1132. *val = 0xFF;
  1133. break;
  1134. default:
  1135. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1136. }
  1137. return 0;
  1138. }
  1139. /*
  1140. * See tile_cfg_read() for relevent comments.
  1141. * Note that "val" is the value to write, not a pointer to that value.
  1142. */
  1143. static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
  1144. int size, u32 val)
  1145. {
  1146. struct pci_controller *controller = bus->sysdata;
  1147. gxio_trio_context_t *trio_context = controller->trio;
  1148. int busnum = bus->number & 0xff;
  1149. int device = PCI_SLOT(devfn);
  1150. int function = PCI_FUNC(devfn);
  1151. int config_type = 1;
  1152. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1153. void *mmio_addr;
  1154. u32 val_32 = (u32)val;
  1155. u16 val_16 = (u16)val;
  1156. u8 val_8 = (u8)val;
  1157. /*
  1158. * Map all accesses to the local device on root bus into the
  1159. * MMIO space of the MAC. Accesses to the downstream devices
  1160. * go to the PIO space.
  1161. */
  1162. if (pci_is_root_bus(bus)) {
  1163. if (device == 0) {
  1164. /*
  1165. * This is the internal downstream P2P bridge,
  1166. * access directly.
  1167. */
  1168. unsigned int reg_offset;
  1169. reg_offset = ((offset & 0xFFF) <<
  1170. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1171. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1172. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1173. (controller->mac <<
  1174. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1175. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1176. goto valid_device;
  1177. } else {
  1178. /*
  1179. * We fake an empty device for (device > 0),
  1180. * since there is only one device on bus 0.
  1181. */
  1182. goto invalid_device;
  1183. }
  1184. }
  1185. /*
  1186. * Accesses to the directly attached device have to be
  1187. * sent as type-0 configs.
  1188. */
  1189. if (busnum == (controller->first_busno + 1)) {
  1190. /*
  1191. * There is only one device off of our built-in P2P bridge.
  1192. */
  1193. if (device != 0)
  1194. goto invalid_device;
  1195. config_type = 0;
  1196. }
  1197. cfg_addr.word = 0;
  1198. cfg_addr.reg_addr = (offset & 0xFFF);
  1199. cfg_addr.fn = function;
  1200. cfg_addr.dev = device;
  1201. cfg_addr.bus = busnum;
  1202. cfg_addr.type = config_type;
  1203. /*
  1204. * Note that we don't set the mac field in cfg_addr because the
  1205. * mapping is per port.
  1206. */
  1207. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1208. cfg_addr.word;
  1209. valid_device:
  1210. switch (size) {
  1211. case 4:
  1212. __gxio_mmio_write32(mmio_addr, val_32);
  1213. TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
  1214. break;
  1215. case 2:
  1216. __gxio_mmio_write16(mmio_addr, val_16);
  1217. TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
  1218. break;
  1219. case 1:
  1220. __gxio_mmio_write8(mmio_addr, val_8);
  1221. TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
  1222. break;
  1223. default:
  1224. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1225. }
  1226. invalid_device:
  1227. return 0;
  1228. }
  1229. static struct pci_ops tile_cfg_ops = {
  1230. .read = tile_cfg_read,
  1231. .write = tile_cfg_write,
  1232. };
  1233. /* MSI support starts here. */
  1234. static unsigned int tilegx_msi_startup(struct irq_data *d)
  1235. {
  1236. if (d->msi_desc)
  1237. unmask_msi_irq(d);
  1238. return 0;
  1239. }
  1240. static void tilegx_msi_ack(struct irq_data *d)
  1241. {
  1242. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  1243. }
  1244. static void tilegx_msi_mask(struct irq_data *d)
  1245. {
  1246. mask_msi_irq(d);
  1247. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  1248. }
  1249. static void tilegx_msi_unmask(struct irq_data *d)
  1250. {
  1251. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  1252. unmask_msi_irq(d);
  1253. }
  1254. static struct irq_chip tilegx_msi_chip = {
  1255. .name = "tilegx_msi",
  1256. .irq_startup = tilegx_msi_startup,
  1257. .irq_ack = tilegx_msi_ack,
  1258. .irq_mask = tilegx_msi_mask,
  1259. .irq_unmask = tilegx_msi_unmask,
  1260. /* TBD: support set_affinity. */
  1261. };
  1262. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  1263. {
  1264. struct pci_controller *controller;
  1265. gxio_trio_context_t *trio_context;
  1266. struct msi_msg msg;
  1267. int default_irq;
  1268. uint64_t mem_map_base;
  1269. uint64_t mem_map_limit;
  1270. u64 msi_addr;
  1271. int mem_map;
  1272. int cpu;
  1273. int irq;
  1274. int ret;
  1275. irq = create_irq();
  1276. if (irq < 0)
  1277. return irq;
  1278. /*
  1279. * Since we use a 64-bit Mem-Map to accept the MSI write, we fail
  1280. * devices that are not capable of generating a 64-bit message address.
  1281. * These devices will fall back to using the legacy interrupts.
  1282. * Most PCIe endpoint devices do support 64-bit message addressing.
  1283. */
  1284. if (desc->msi_attrib.is_64 == 0) {
  1285. dev_printk(KERN_INFO, &pdev->dev,
  1286. "64-bit MSI message address not supported, "
  1287. "falling back to legacy interrupts.\n");
  1288. ret = -ENOMEM;
  1289. goto is_64_failure;
  1290. }
  1291. default_irq = desc->msi_attrib.default_irq;
  1292. controller = irq_get_handler_data(default_irq);
  1293. BUG_ON(!controller);
  1294. trio_context = controller->trio;
  1295. /*
  1296. * Allocate a scatter-queue that will accept the MSI write and
  1297. * trigger the TILE-side interrupts. We use the scatter-queue regions
  1298. * before the mem map regions, because the latter are needed by more
  1299. * applications.
  1300. */
  1301. mem_map = gxio_trio_alloc_scatter_queues(trio_context, 1, 0, 0);
  1302. if (mem_map >= 0) {
  1303. TRIO_MAP_SQ_DOORBELL_FMT_t doorbell_template = {{
  1304. .pop = 0,
  1305. .doorbell = 1,
  1306. }};
  1307. mem_map += TRIO_NUM_MAP_MEM_REGIONS;
  1308. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1309. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1310. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1311. msi_addr = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 8;
  1312. msg.data = (unsigned int)doorbell_template.word;
  1313. } else {
  1314. /* SQ regions are out, allocate from map mem regions. */
  1315. mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
  1316. if (mem_map < 0) {
  1317. dev_printk(KERN_INFO, &pdev->dev,
  1318. "%s Mem-Map alloc failure. "
  1319. "Failed to initialize MSI interrupts. "
  1320. "Falling back to legacy interrupts.\n",
  1321. desc->msi_attrib.is_msix ? "MSI-X" : "MSI");
  1322. ret = -ENOMEM;
  1323. goto msi_mem_map_alloc_failure;
  1324. }
  1325. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1326. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1327. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1328. msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 -
  1329. TRIO_MAP_MEM_REG_INT0;
  1330. msg.data = mem_map;
  1331. }
  1332. /* We try to distribute different IRQs to different tiles. */
  1333. cpu = tile_irq_cpu(irq);
  1334. /*
  1335. * Now call up to the HV to configure the MSI interrupt and
  1336. * set up the IPI binding.
  1337. */
  1338. ret = gxio_trio_config_msi_intr(trio_context, cpu_x(cpu), cpu_y(cpu),
  1339. KERNEL_PL, irq, controller->mac,
  1340. mem_map, mem_map_base, mem_map_limit,
  1341. trio_context->asid);
  1342. if (ret < 0) {
  1343. dev_printk(KERN_INFO, &pdev->dev, "HV MSI config failed.\n");
  1344. goto hv_msi_config_failure;
  1345. }
  1346. irq_set_msi_desc(irq, desc);
  1347. msg.address_hi = msi_addr >> 32;
  1348. msg.address_lo = msi_addr & 0xffffffff;
  1349. write_msi_msg(irq, &msg);
  1350. irq_set_chip_and_handler(irq, &tilegx_msi_chip, handle_level_irq);
  1351. irq_set_handler_data(irq, controller);
  1352. return 0;
  1353. hv_msi_config_failure:
  1354. /* Free mem-map */
  1355. msi_mem_map_alloc_failure:
  1356. is_64_failure:
  1357. destroy_irq(irq);
  1358. return ret;
  1359. }
  1360. void arch_teardown_msi_irq(unsigned int irq)
  1361. {
  1362. destroy_irq(irq);
  1363. }