pci_gx.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. To use a port, it must be allowed to be in RC mode by the
  365. * Board Information Block, and the hardware strapping pins must be
  366. * set to RC mode.
  367. */
  368. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  369. gxio_trio_context_t *context = &trio_contexts[i];
  370. if (context->fd < 0)
  371. continue;
  372. for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
  373. if (pcie_ports[i].ports[j].allow_rc &&
  374. strapped_for_rc(context, j)) {
  375. pcie_rc[i][j] = 1;
  376. num_rc_controllers++;
  377. }
  378. }
  379. }
  380. /* Return if no PCIe ports are configured to operate in RC mode. */
  381. if (num_rc_controllers == 0)
  382. return 0;
  383. /* Set the TRIO pointer and MAC index for each PCIe RC port. */
  384. for (i = 0; i < TILEGX_NUM_TRIO; i++) {
  385. for (j = 0; j < TILEGX_TRIO_PCIES; j++) {
  386. if (pcie_rc[i][j]) {
  387. pci_controllers[ctl_index].trio =
  388. &trio_contexts[i];
  389. pci_controllers[ctl_index].mac = j;
  390. pci_controllers[ctl_index].trio_index = i;
  391. ctl_index++;
  392. if (ctl_index == num_rc_controllers)
  393. goto out;
  394. }
  395. }
  396. }
  397. out:
  398. /* Configure each PCIe RC port. */
  399. for (i = 0; i < num_rc_controllers; i++) {
  400. /* Configure the PCIe MAC to run in RC mode. */
  401. struct pci_controller *controller = &pci_controllers[i];
  402. controller->index = i;
  403. controller->ops = &tile_cfg_ops;
  404. controller->io_space.start = PCIBIOS_MIN_IO +
  405. (i * IO_SPACE_SIZE);
  406. controller->io_space.end = controller->io_space.start +
  407. IO_SPACE_SIZE - 1;
  408. BUG_ON(controller->io_space.end > IO_SPACE_LIMIT);
  409. controller->io_space.flags = IORESOURCE_IO;
  410. snprintf(controller->io_space_name,
  411. sizeof(controller->io_space_name),
  412. "PCI I/O domain %d", i);
  413. controller->io_space.name = controller->io_space_name;
  414. /*
  415. * The PCI memory resource is located above the PA space.
  416. * For every host bridge, the BAR window or the MMIO aperture
  417. * is in range [3GB, 4GB - 1] of a 4GB space beyond the
  418. * PA space.
  419. */
  420. controller->mem_offset = TILE_PCI_MEM_START +
  421. (i * TILE_PCI_BAR_WINDOW_TOP);
  422. controller->mem_space.start = controller->mem_offset +
  423. TILE_PCI_BAR_WINDOW_TOP - TILE_PCI_BAR_WINDOW_SIZE;
  424. controller->mem_space.end = controller->mem_offset +
  425. TILE_PCI_BAR_WINDOW_TOP - 1;
  426. controller->mem_space.flags = IORESOURCE_MEM;
  427. snprintf(controller->mem_space_name,
  428. sizeof(controller->mem_space_name),
  429. "PCI mem domain %d", i);
  430. controller->mem_space.name = controller->mem_space_name;
  431. }
  432. return num_rc_controllers;
  433. }
  434. /*
  435. * (pin - 1) converts from the PCI standard's [1:4] convention to
  436. * a normal [0:3] range.
  437. */
  438. static int tile_map_irq(const struct pci_dev *dev, u8 device, u8 pin)
  439. {
  440. struct pci_controller *controller =
  441. (struct pci_controller *)dev->sysdata;
  442. return controller->irq_intx_table[pin - 1];
  443. }
  444. static void fixup_read_and_payload_sizes(struct pci_controller *controller)
  445. {
  446. gxio_trio_context_t *trio_context = controller->trio;
  447. struct pci_bus *root_bus = controller->root_bus;
  448. TRIO_PCIE_RC_DEVICE_CONTROL_t dev_control;
  449. TRIO_PCIE_RC_DEVICE_CAP_t rc_dev_cap;
  450. unsigned int reg_offset;
  451. struct pci_bus *child;
  452. int mac;
  453. int err;
  454. mac = controller->mac;
  455. /* Set our max read request size to be 4KB. */
  456. reg_offset =
  457. (TRIO_PCIE_RC_DEVICE_CONTROL <<
  458. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  459. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  460. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  461. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  462. dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  463. reg_offset);
  464. dev_control.max_read_req_sz = 5;
  465. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  466. dev_control.word);
  467. /*
  468. * Set the max payload size supported by this Gx PCIe MAC.
  469. * Though Gx PCIe supports Max Payload Size of up to 1024 bytes,
  470. * experiments have shown that setting MPS to 256 yields the
  471. * best performance.
  472. */
  473. reg_offset =
  474. (TRIO_PCIE_RC_DEVICE_CAP <<
  475. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  476. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  477. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  478. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  479. rc_dev_cap.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  480. reg_offset);
  481. rc_dev_cap.mps_sup = 1;
  482. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  483. rc_dev_cap.word);
  484. /* Configure PCI Express MPS setting. */
  485. list_for_each_entry(child, &root_bus->children, node) {
  486. struct pci_dev *self = child->self;
  487. if (!self)
  488. continue;
  489. pcie_bus_configure_settings(child, self->pcie_mpss);
  490. }
  491. /*
  492. * Set the mac_config register in trio based on the MPS/MRS of the link.
  493. */
  494. reg_offset =
  495. (TRIO_PCIE_RC_DEVICE_CONTROL <<
  496. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  497. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  498. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  499. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  500. dev_control.word = __gxio_mmio_read32(trio_context->mmio_base_mac +
  501. reg_offset);
  502. err = gxio_trio_set_mps_mrs(trio_context,
  503. dev_control.max_payload_size,
  504. dev_control.max_read_req_sz,
  505. mac);
  506. if (err < 0) {
  507. pr_err("PCI: PCIE_CONFIGURE_MAC_MPS_MRS failure, "
  508. "MAC %d on TRIO %d\n",
  509. mac, controller->trio_index);
  510. }
  511. }
  512. static int setup_pcie_rc_delay(char *str)
  513. {
  514. unsigned long delay = 0;
  515. unsigned long trio_index;
  516. unsigned long mac;
  517. if (str == NULL || !isdigit(*str))
  518. return -EINVAL;
  519. trio_index = simple_strtoul(str, (char **)&str, 10);
  520. if (trio_index >= TILEGX_NUM_TRIO)
  521. return -EINVAL;
  522. if (*str != ',')
  523. return -EINVAL;
  524. str++;
  525. if (!isdigit(*str))
  526. return -EINVAL;
  527. mac = simple_strtoul(str, (char **)&str, 10);
  528. if (mac >= TILEGX_TRIO_PCIES)
  529. return -EINVAL;
  530. if (*str != '\0') {
  531. if (*str != ',')
  532. return -EINVAL;
  533. str++;
  534. if (!isdigit(*str))
  535. return -EINVAL;
  536. delay = simple_strtoul(str, (char **)&str, 10);
  537. }
  538. rc_delay[trio_index][mac] = delay ? : DEFAULT_RC_DELAY;
  539. return 0;
  540. }
  541. early_param("pcie_rc_delay", setup_pcie_rc_delay);
  542. /* PCI initialization entry point, called by subsys_initcall. */
  543. int __init pcibios_init(void)
  544. {
  545. resource_size_t offset;
  546. LIST_HEAD(resources);
  547. int next_busno;
  548. int i;
  549. tile_pci_init();
  550. if (num_rc_controllers == 0)
  551. return 0;
  552. /*
  553. * Delay a bit in case devices aren't ready. Some devices are
  554. * known to require at least 20ms here, but we use a more
  555. * conservative value.
  556. */
  557. msleep(250);
  558. /* Scan all of the recorded PCI controllers. */
  559. for (next_busno = 0, i = 0; i < num_rc_controllers; i++) {
  560. struct pci_controller *controller = &pci_controllers[i];
  561. gxio_trio_context_t *trio_context = controller->trio;
  562. TRIO_PCIE_INTFC_PORT_STATUS_t port_status;
  563. TRIO_PCIE_INTFC_TX_FIFO_CTL_t tx_fifo_ctl;
  564. struct pci_bus *bus;
  565. unsigned int reg_offset;
  566. unsigned int class_code_revision;
  567. int trio_index;
  568. int mac;
  569. int ret;
  570. if (trio_context->fd < 0)
  571. continue;
  572. trio_index = controller->trio_index;
  573. mac = controller->mac;
  574. /*
  575. * Check for PCIe link-up status to decide if we need
  576. * to force the link to come up.
  577. */
  578. reg_offset =
  579. (TRIO_PCIE_INTFC_PORT_STATUS <<
  580. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  581. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  582. TRIO_CFG_REGION_ADDR__INTFC_SHIFT) |
  583. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  584. port_status.word =
  585. __gxio_mmio_read(trio_context->mmio_base_mac +
  586. reg_offset);
  587. if (!port_status.dl_up) {
  588. if (rc_delay[trio_index][mac]) {
  589. pr_info("Delaying PCIe RC TRIO init %d sec"
  590. " on MAC %d on TRIO %d\n",
  591. rc_delay[trio_index][mac], mac,
  592. trio_index);
  593. msleep(rc_delay[trio_index][mac] * 1000);
  594. }
  595. ret = gxio_trio_force_rc_link_up(trio_context, mac);
  596. if (ret < 0)
  597. pr_err("PCI: PCIE_FORCE_LINK_UP failure, "
  598. "MAC %d on TRIO %d\n", mac, trio_index);
  599. }
  600. pr_info("PCI: Found PCI controller #%d on TRIO %d MAC %d\n", i,
  601. trio_index, controller->mac);
  602. /* Delay the bus probe if needed. */
  603. if (rc_delay[trio_index][mac]) {
  604. pr_info("Delaying PCIe RC bus enumerating %d sec"
  605. " on MAC %d on TRIO %d\n",
  606. rc_delay[trio_index][mac], mac,
  607. trio_index);
  608. msleep(rc_delay[trio_index][mac] * 1000);
  609. } else {
  610. /*
  611. * Wait a bit here because some EP devices
  612. * take longer to come up.
  613. */
  614. msleep(1000);
  615. }
  616. /* Check for PCIe link-up status again. */
  617. port_status.word =
  618. __gxio_mmio_read(trio_context->mmio_base_mac +
  619. reg_offset);
  620. if (!port_status.dl_up) {
  621. if (pcie_ports[trio_index].ports[mac].removable) {
  622. pr_info("PCI: link is down, MAC %d on TRIO %d\n",
  623. mac, trio_index);
  624. pr_info("This is expected if no PCIe card"
  625. " is connected to this link\n");
  626. } else
  627. pr_err("PCI: link is down, MAC %d on TRIO %d\n",
  628. mac, trio_index);
  629. continue;
  630. }
  631. /*
  632. * Ensure that the link can come out of L1 power down state.
  633. * Strictly speaking, this is needed only in the case of
  634. * heavy RC-initiated DMAs.
  635. */
  636. reg_offset =
  637. (TRIO_PCIE_INTFC_TX_FIFO_CTL <<
  638. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  639. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_INTERFACE <<
  640. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  641. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  642. tx_fifo_ctl.word =
  643. __gxio_mmio_read(trio_context->mmio_base_mac +
  644. reg_offset);
  645. tx_fifo_ctl.min_p_credits = 0;
  646. __gxio_mmio_write(trio_context->mmio_base_mac + reg_offset,
  647. tx_fifo_ctl.word);
  648. /*
  649. * Change the device ID so that Linux bus crawl doesn't confuse
  650. * the internal bridge with any Tilera endpoints.
  651. */
  652. reg_offset =
  653. (TRIO_PCIE_RC_DEVICE_ID_VEN_ID <<
  654. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  655. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  656. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  657. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  658. __gxio_mmio_write32(trio_context->mmio_base_mac + reg_offset,
  659. (TILERA_GX36_RC_DEV_ID <<
  660. TRIO_PCIE_RC_DEVICE_ID_VEN_ID__DEV_ID_SHIFT) |
  661. TILERA_VENDOR_ID);
  662. /* Set the internal P2P bridge class code. */
  663. reg_offset =
  664. (TRIO_PCIE_RC_REVISION_ID <<
  665. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  666. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_STANDARD <<
  667. TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  668. (mac << TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  669. class_code_revision =
  670. __gxio_mmio_read32(trio_context->mmio_base_mac +
  671. reg_offset);
  672. class_code_revision = (class_code_revision & 0xff) |
  673. (PCI_CLASS_BRIDGE_PCI << 16);
  674. __gxio_mmio_write32(trio_context->mmio_base_mac +
  675. reg_offset, class_code_revision);
  676. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  677. /* Map in the MMIO space for the PIO region. */
  678. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index) |
  679. (((unsigned long long)mac) <<
  680. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  681. #else
  682. /* Alloc a PIO region for PCI config access per MAC. */
  683. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  684. if (ret < 0) {
  685. pr_err("PCI: PCI CFG PIO alloc failure for mac %d "
  686. "on TRIO %d, give up\n", mac, trio_index);
  687. continue;
  688. }
  689. trio_context->pio_cfg_index[mac] = ret;
  690. /* For PIO CFG, the bus_address_hi parameter is 0. */
  691. ret = gxio_trio_init_pio_region_aux(trio_context,
  692. trio_context->pio_cfg_index[mac],
  693. mac, 0, HV_TRIO_PIO_FLAG_CONFIG_SPACE);
  694. if (ret < 0) {
  695. pr_err("PCI: PCI CFG PIO init failure for mac %d "
  696. "on TRIO %d, give up\n", mac, trio_index);
  697. continue;
  698. }
  699. offset = HV_TRIO_PIO_OFFSET(trio_context->pio_cfg_index[mac]) |
  700. (((unsigned long long)mac) <<
  701. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT);
  702. #endif
  703. trio_context->mmio_base_pio_cfg[mac] =
  704. iorpc_ioremap(trio_context->fd, offset,
  705. (1 << TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR__MAC_SHIFT));
  706. if (trio_context->mmio_base_pio_cfg[mac] == NULL) {
  707. pr_err("PCI: PIO map failure for mac %d on TRIO %d\n",
  708. mac, trio_index);
  709. continue;
  710. }
  711. /* Initialize the PCIe interrupts. */
  712. if (tile_init_irqs(controller)) {
  713. pr_err("PCI: IRQs init failure for mac %d on TRIO %d\n",
  714. mac, trio_index);
  715. continue;
  716. }
  717. /*
  718. * The PCI memory resource is located above the PA space.
  719. * The memory range for the PCI root bus should not overlap
  720. * with the physical RAM.
  721. */
  722. pci_add_resource_offset(&resources, &controller->mem_space,
  723. controller->mem_offset);
  724. pci_add_resource(&resources, &controller->io_space);
  725. controller->first_busno = next_busno;
  726. bus = pci_scan_root_bus(NULL, next_busno, controller->ops,
  727. controller, &resources);
  728. controller->root_bus = bus;
  729. next_busno = bus->busn_res.end + 1;
  730. }
  731. /* Do machine dependent PCI interrupt routing */
  732. pci_fixup_irqs(pci_common_swizzle, tile_map_irq);
  733. /*
  734. * This comes from the generic Linux PCI driver.
  735. *
  736. * It allocates all of the resources (I/O memory, etc)
  737. * associated with the devices read in above.
  738. */
  739. pci_assign_unassigned_resources();
  740. /* Record the I/O resources in the PCI controller structure. */
  741. for (i = 0; i < num_rc_controllers; i++) {
  742. struct pci_controller *controller = &pci_controllers[i];
  743. gxio_trio_context_t *trio_context = controller->trio;
  744. struct pci_bus *root_bus = pci_controllers[i].root_bus;
  745. int ret;
  746. int j;
  747. /*
  748. * Skip controllers that are not properly initialized or
  749. * have down links.
  750. */
  751. if (root_bus == NULL)
  752. continue;
  753. /* Configure the max_payload_size values for this domain. */
  754. fixup_read_and_payload_sizes(controller);
  755. /* Alloc a PIO region for PCI memory access for each RC port. */
  756. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  757. if (ret < 0) {
  758. pr_err("PCI: MEM PIO alloc failure on TRIO %d mac %d, "
  759. "give up\n", controller->trio_index,
  760. controller->mac);
  761. continue;
  762. }
  763. controller->pio_mem_index = ret;
  764. /*
  765. * For PIO MEM, the bus_address_hi parameter is hard-coded 0
  766. * because we always assign 32-bit PCI bus BAR ranges.
  767. */
  768. ret = gxio_trio_init_pio_region_aux(trio_context,
  769. controller->pio_mem_index,
  770. controller->mac,
  771. 0,
  772. 0);
  773. if (ret < 0) {
  774. pr_err("PCI: MEM PIO init failure on TRIO %d mac %d, "
  775. "give up\n", controller->trio_index,
  776. controller->mac);
  777. continue;
  778. }
  779. #ifdef CONFIG_TILE_PCI_IO
  780. /*
  781. * Alloc a PIO region for PCI I/O space access for each RC port.
  782. */
  783. ret = gxio_trio_alloc_pio_regions(trio_context, 1, 0, 0);
  784. if (ret < 0) {
  785. pr_err("PCI: I/O PIO alloc failure on TRIO %d mac %d, "
  786. "give up\n", controller->trio_index,
  787. controller->mac);
  788. continue;
  789. }
  790. controller->pio_io_index = ret;
  791. /*
  792. * For PIO IO, the bus_address_hi parameter is hard-coded 0
  793. * because PCI I/O address space is 32-bit.
  794. */
  795. ret = gxio_trio_init_pio_region_aux(trio_context,
  796. controller->pio_io_index,
  797. controller->mac,
  798. 0,
  799. HV_TRIO_PIO_FLAG_IO_SPACE);
  800. if (ret < 0) {
  801. pr_err("PCI: I/O PIO init failure on TRIO %d mac %d, "
  802. "give up\n", controller->trio_index,
  803. controller->mac);
  804. continue;
  805. }
  806. #endif
  807. /*
  808. * Configure a Mem-Map region for each memory controller so
  809. * that Linux can map all of its PA space to the PCI bus.
  810. * Use the IOMMU to handle hash-for-home memory.
  811. */
  812. for_each_online_node(j) {
  813. unsigned long start_pfn = node_start_pfn[j];
  814. unsigned long end_pfn = node_end_pfn[j];
  815. unsigned long nr_pages = end_pfn - start_pfn;
  816. ret = gxio_trio_alloc_memory_maps(trio_context, 1, 0,
  817. 0);
  818. if (ret < 0) {
  819. pr_err("PCI: Mem-Map alloc failure on TRIO %d "
  820. "mac %d for MC %d, give up\n",
  821. controller->trio_index,
  822. controller->mac, j);
  823. goto alloc_mem_map_failed;
  824. }
  825. controller->mem_maps[j] = ret;
  826. /*
  827. * Initialize the Mem-Map and the I/O MMU so that all
  828. * the physical memory can be accessed by the endpoint
  829. * devices. The base bus address is set to the base CPA
  830. * of this memory controller plus an offset (see pci.h).
  831. * The region's base VA is set to the base CPA. The
  832. * I/O MMU table essentially translates the CPA to
  833. * the real PA. Implicitly, for node 0, we create
  834. * a separate Mem-Map region that serves as the inbound
  835. * window for legacy 32-bit devices. This is a direct
  836. * map of the low 4GB CPA space.
  837. */
  838. ret = gxio_trio_init_memory_map_mmu_aux(trio_context,
  839. controller->mem_maps[j],
  840. start_pfn << PAGE_SHIFT,
  841. nr_pages << PAGE_SHIFT,
  842. trio_context->asid,
  843. controller->mac,
  844. (start_pfn << PAGE_SHIFT) +
  845. TILE_PCI_MEM_MAP_BASE_OFFSET,
  846. j,
  847. GXIO_TRIO_ORDER_MODE_UNORDERED);
  848. if (ret < 0) {
  849. pr_err("PCI: Mem-Map init failure on TRIO %d "
  850. "mac %d for MC %d, give up\n",
  851. controller->trio_index,
  852. controller->mac, j);
  853. goto alloc_mem_map_failed;
  854. }
  855. continue;
  856. alloc_mem_map_failed:
  857. break;
  858. }
  859. }
  860. return 0;
  861. }
  862. subsys_initcall(pcibios_init);
  863. /* No bus fixups needed. */
  864. void pcibios_fixup_bus(struct pci_bus *bus)
  865. {
  866. }
  867. /* Process any "pci=" kernel boot arguments. */
  868. char *pcibios_setup(char *str)
  869. {
  870. if (!strcmp(str, "off")) {
  871. pci_probe = 0;
  872. return NULL;
  873. }
  874. return str;
  875. }
  876. /*
  877. * Enable memory address decoding, as appropriate, for the
  878. * device described by the 'dev' struct.
  879. *
  880. * This is called from the generic PCI layer, and can be called
  881. * for bridges or endpoints.
  882. */
  883. int pcibios_enable_device(struct pci_dev *dev, int mask)
  884. {
  885. return pci_enable_resources(dev, mask);
  886. }
  887. /* Called for each device after PCI setup is done. */
  888. static void pcibios_fixup_final(struct pci_dev *pdev)
  889. {
  890. set_dma_ops(&pdev->dev, gx_pci_dma_map_ops);
  891. set_dma_offset(&pdev->dev, TILE_PCI_MEM_MAP_BASE_OFFSET);
  892. pdev->dev.archdata.max_direct_dma_addr =
  893. TILE_PCI_MAX_DIRECT_DMA_ADDRESS;
  894. }
  895. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_final);
  896. /* Map a PCI MMIO bus address into VA space. */
  897. void __iomem *ioremap(resource_size_t phys_addr, unsigned long size)
  898. {
  899. struct pci_controller *controller = NULL;
  900. resource_size_t bar_start;
  901. resource_size_t bar_end;
  902. resource_size_t offset;
  903. resource_size_t start;
  904. resource_size_t end;
  905. int trio_fd;
  906. int i;
  907. start = phys_addr;
  908. end = phys_addr + size - 1;
  909. /*
  910. * By searching phys_addr in each controller's mem_space, we can
  911. * determine the controller that should accept the PCI memory access.
  912. */
  913. for (i = 0; i < num_rc_controllers; i++) {
  914. /*
  915. * Skip controllers that are not properly initialized or
  916. * have down links.
  917. */
  918. if (pci_controllers[i].root_bus == NULL)
  919. continue;
  920. bar_start = pci_controllers[i].mem_space.start;
  921. bar_end = pci_controllers[i].mem_space.end;
  922. if ((start >= bar_start) && (end <= bar_end)) {
  923. controller = &pci_controllers[i];
  924. break;
  925. }
  926. }
  927. if (controller == NULL)
  928. return NULL;
  929. trio_fd = controller->trio->fd;
  930. /* Convert the resource start to the bus address offset. */
  931. start = phys_addr - controller->mem_offset;
  932. offset = HV_TRIO_PIO_OFFSET(controller->pio_mem_index) + start;
  933. /* We need to keep the PCI bus address's in-page offset in the VA. */
  934. return iorpc_ioremap(trio_fd, offset, size) +
  935. (start & (PAGE_SIZE - 1));
  936. }
  937. EXPORT_SYMBOL(ioremap);
  938. #ifdef CONFIG_TILE_PCI_IO
  939. /* Map a PCI I/O address into VA space. */
  940. void __iomem *ioport_map(unsigned long port, unsigned int size)
  941. {
  942. struct pci_controller *controller = NULL;
  943. resource_size_t bar_start;
  944. resource_size_t bar_end;
  945. resource_size_t offset;
  946. resource_size_t start;
  947. resource_size_t end;
  948. int trio_fd;
  949. int i;
  950. start = port;
  951. end = port + size - 1;
  952. /*
  953. * By searching the port in each controller's io_space, we can
  954. * determine the controller that should accept the PCI I/O access.
  955. */
  956. for (i = 0; i < num_rc_controllers; i++) {
  957. /*
  958. * Skip controllers that are not properly initialized or
  959. * have down links.
  960. */
  961. if (pci_controllers[i].root_bus == NULL)
  962. continue;
  963. bar_start = pci_controllers[i].io_space.start;
  964. bar_end = pci_controllers[i].io_space.end;
  965. if ((start >= bar_start) && (end <= bar_end)) {
  966. controller = &pci_controllers[i];
  967. break;
  968. }
  969. }
  970. if (controller == NULL)
  971. return NULL;
  972. trio_fd = controller->trio->fd;
  973. /* Convert the resource start to the bus address offset. */
  974. port -= controller->io_space.start;
  975. offset = HV_TRIO_PIO_OFFSET(controller->pio_io_index) + port;
  976. /* We need to keep the PCI bus address's in-page offset in the VA. */
  977. return iorpc_ioremap(trio_fd, offset, size) + (port & (PAGE_SIZE - 1));
  978. }
  979. EXPORT_SYMBOL(ioport_map);
  980. void ioport_unmap(void __iomem *addr)
  981. {
  982. iounmap(addr);
  983. }
  984. EXPORT_SYMBOL(ioport_unmap);
  985. #endif
  986. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  987. {
  988. iounmap(addr);
  989. }
  990. EXPORT_SYMBOL(pci_iounmap);
  991. /****************************************************************
  992. *
  993. * Tile PCI config space read/write routines
  994. *
  995. ****************************************************************/
  996. /*
  997. * These are the normal read and write ops
  998. * These are expanded with macros from pci_bus_read_config_byte() etc.
  999. *
  1000. * devfn is the combined PCI device & function.
  1001. *
  1002. * offset is in bytes, from the start of config space for the
  1003. * specified bus & device.
  1004. */
  1005. static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
  1006. int size, u32 *val)
  1007. {
  1008. struct pci_controller *controller = bus->sysdata;
  1009. gxio_trio_context_t *trio_context = controller->trio;
  1010. int busnum = bus->number & 0xff;
  1011. int device = PCI_SLOT(devfn);
  1012. int function = PCI_FUNC(devfn);
  1013. int config_type = 1;
  1014. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1015. void *mmio_addr;
  1016. /*
  1017. * Map all accesses to the local device on root bus into the
  1018. * MMIO space of the MAC. Accesses to the downstream devices
  1019. * go to the PIO space.
  1020. */
  1021. if (pci_is_root_bus(bus)) {
  1022. if (device == 0) {
  1023. /*
  1024. * This is the internal downstream P2P bridge,
  1025. * access directly.
  1026. */
  1027. unsigned int reg_offset;
  1028. reg_offset = ((offset & 0xFFF) <<
  1029. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1030. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1031. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1032. (controller->mac <<
  1033. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1034. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1035. goto valid_device;
  1036. } else {
  1037. /*
  1038. * We fake an empty device for (device > 0),
  1039. * since there is only one device on bus 0.
  1040. */
  1041. goto invalid_device;
  1042. }
  1043. }
  1044. /*
  1045. * Accesses to the directly attached device have to be
  1046. * sent as type-0 configs.
  1047. */
  1048. if (busnum == (controller->first_busno + 1)) {
  1049. /*
  1050. * There is only one device off of our built-in P2P bridge.
  1051. */
  1052. if (device != 0)
  1053. goto invalid_device;
  1054. config_type = 0;
  1055. }
  1056. cfg_addr.word = 0;
  1057. cfg_addr.reg_addr = (offset & 0xFFF);
  1058. cfg_addr.fn = function;
  1059. cfg_addr.dev = device;
  1060. cfg_addr.bus = busnum;
  1061. cfg_addr.type = config_type;
  1062. /*
  1063. * Note that we don't set the mac field in cfg_addr because the
  1064. * mapping is per port.
  1065. */
  1066. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1067. cfg_addr.word;
  1068. valid_device:
  1069. switch (size) {
  1070. case 4:
  1071. *val = __gxio_mmio_read32(mmio_addr);
  1072. break;
  1073. case 2:
  1074. *val = __gxio_mmio_read16(mmio_addr);
  1075. break;
  1076. case 1:
  1077. *val = __gxio_mmio_read8(mmio_addr);
  1078. break;
  1079. default:
  1080. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1081. }
  1082. TRACE_CFG_RD(size, *val, busnum, device, function, offset);
  1083. return 0;
  1084. invalid_device:
  1085. switch (size) {
  1086. case 4:
  1087. *val = 0xFFFFFFFF;
  1088. break;
  1089. case 2:
  1090. *val = 0xFFFF;
  1091. break;
  1092. case 1:
  1093. *val = 0xFF;
  1094. break;
  1095. default:
  1096. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1097. }
  1098. return 0;
  1099. }
  1100. /*
  1101. * See tile_cfg_read() for relevent comments.
  1102. * Note that "val" is the value to write, not a pointer to that value.
  1103. */
  1104. static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
  1105. int size, u32 val)
  1106. {
  1107. struct pci_controller *controller = bus->sysdata;
  1108. gxio_trio_context_t *trio_context = controller->trio;
  1109. int busnum = bus->number & 0xff;
  1110. int device = PCI_SLOT(devfn);
  1111. int function = PCI_FUNC(devfn);
  1112. int config_type = 1;
  1113. TRIO_TILE_PIO_REGION_SETUP_CFG_ADDR_t cfg_addr;
  1114. void *mmio_addr;
  1115. u32 val_32 = (u32)val;
  1116. u16 val_16 = (u16)val;
  1117. u8 val_8 = (u8)val;
  1118. /*
  1119. * Map all accesses to the local device on root bus into the
  1120. * MMIO space of the MAC. Accesses to the downstream devices
  1121. * go to the PIO space.
  1122. */
  1123. if (pci_is_root_bus(bus)) {
  1124. if (device == 0) {
  1125. /*
  1126. * This is the internal downstream P2P bridge,
  1127. * access directly.
  1128. */
  1129. unsigned int reg_offset;
  1130. reg_offset = ((offset & 0xFFF) <<
  1131. TRIO_CFG_REGION_ADDR__REG_SHIFT) |
  1132. (TRIO_CFG_REGION_ADDR__INTFC_VAL_MAC_PROTECTED
  1133. << TRIO_CFG_REGION_ADDR__INTFC_SHIFT ) |
  1134. (controller->mac <<
  1135. TRIO_CFG_REGION_ADDR__MAC_SEL_SHIFT);
  1136. mmio_addr = trio_context->mmio_base_mac + reg_offset;
  1137. goto valid_device;
  1138. } else {
  1139. /*
  1140. * We fake an empty device for (device > 0),
  1141. * since there is only one device on bus 0.
  1142. */
  1143. goto invalid_device;
  1144. }
  1145. }
  1146. /*
  1147. * Accesses to the directly attached device have to be
  1148. * sent as type-0 configs.
  1149. */
  1150. if (busnum == (controller->first_busno + 1)) {
  1151. /*
  1152. * There is only one device off of our built-in P2P bridge.
  1153. */
  1154. if (device != 0)
  1155. goto invalid_device;
  1156. config_type = 0;
  1157. }
  1158. cfg_addr.word = 0;
  1159. cfg_addr.reg_addr = (offset & 0xFFF);
  1160. cfg_addr.fn = function;
  1161. cfg_addr.dev = device;
  1162. cfg_addr.bus = busnum;
  1163. cfg_addr.type = config_type;
  1164. /*
  1165. * Note that we don't set the mac field in cfg_addr because the
  1166. * mapping is per port.
  1167. */
  1168. mmio_addr = trio_context->mmio_base_pio_cfg[controller->mac] +
  1169. cfg_addr.word;
  1170. valid_device:
  1171. switch (size) {
  1172. case 4:
  1173. __gxio_mmio_write32(mmio_addr, val_32);
  1174. TRACE_CFG_WR(size, val_32, busnum, device, function, offset);
  1175. break;
  1176. case 2:
  1177. __gxio_mmio_write16(mmio_addr, val_16);
  1178. TRACE_CFG_WR(size, val_16, busnum, device, function, offset);
  1179. break;
  1180. case 1:
  1181. __gxio_mmio_write8(mmio_addr, val_8);
  1182. TRACE_CFG_WR(size, val_8, busnum, device, function, offset);
  1183. break;
  1184. default:
  1185. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1186. }
  1187. invalid_device:
  1188. return 0;
  1189. }
  1190. static struct pci_ops tile_cfg_ops = {
  1191. .read = tile_cfg_read,
  1192. .write = tile_cfg_write,
  1193. };
  1194. /* MSI support starts here. */
  1195. static unsigned int tilegx_msi_startup(struct irq_data *d)
  1196. {
  1197. if (d->msi_desc)
  1198. unmask_msi_irq(d);
  1199. return 0;
  1200. }
  1201. static void tilegx_msi_ack(struct irq_data *d)
  1202. {
  1203. __insn_mtspr(SPR_IPI_EVENT_RESET_K, 1UL << d->irq);
  1204. }
  1205. static void tilegx_msi_mask(struct irq_data *d)
  1206. {
  1207. mask_msi_irq(d);
  1208. __insn_mtspr(SPR_IPI_MASK_SET_K, 1UL << d->irq);
  1209. }
  1210. static void tilegx_msi_unmask(struct irq_data *d)
  1211. {
  1212. __insn_mtspr(SPR_IPI_MASK_RESET_K, 1UL << d->irq);
  1213. unmask_msi_irq(d);
  1214. }
  1215. static struct irq_chip tilegx_msi_chip = {
  1216. .name = "tilegx_msi",
  1217. .irq_startup = tilegx_msi_startup,
  1218. .irq_ack = tilegx_msi_ack,
  1219. .irq_mask = tilegx_msi_mask,
  1220. .irq_unmask = tilegx_msi_unmask,
  1221. /* TBD: support set_affinity. */
  1222. };
  1223. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  1224. {
  1225. struct pci_controller *controller;
  1226. gxio_trio_context_t *trio_context;
  1227. struct msi_msg msg;
  1228. int default_irq;
  1229. uint64_t mem_map_base;
  1230. uint64_t mem_map_limit;
  1231. u64 msi_addr;
  1232. int mem_map;
  1233. int cpu;
  1234. int irq;
  1235. int ret;
  1236. irq = create_irq();
  1237. if (irq < 0)
  1238. return irq;
  1239. /*
  1240. * Since we use a 64-bit Mem-Map to accept the MSI write, we fail
  1241. * devices that are not capable of generating a 64-bit message address.
  1242. * These devices will fall back to using the legacy interrupts.
  1243. * Most PCIe endpoint devices do support 64-bit message addressing.
  1244. */
  1245. if (desc->msi_attrib.is_64 == 0) {
  1246. dev_printk(KERN_INFO, &pdev->dev,
  1247. "64-bit MSI message address not supported, "
  1248. "falling back to legacy interrupts.\n");
  1249. ret = -ENOMEM;
  1250. goto is_64_failure;
  1251. }
  1252. default_irq = desc->msi_attrib.default_irq;
  1253. controller = irq_get_handler_data(default_irq);
  1254. BUG_ON(!controller);
  1255. trio_context = controller->trio;
  1256. /*
  1257. * Allocate a scatter-queue that will accept the MSI write and
  1258. * trigger the TILE-side interrupts. We use the scatter-queue regions
  1259. * before the mem map regions, because the latter are needed by more
  1260. * applications.
  1261. */
  1262. mem_map = gxio_trio_alloc_scatter_queues(trio_context, 1, 0, 0);
  1263. if (mem_map >= 0) {
  1264. TRIO_MAP_SQ_DOORBELL_FMT_t doorbell_template = {{
  1265. .pop = 0,
  1266. .doorbell = 1,
  1267. }};
  1268. mem_map += TRIO_NUM_MAP_MEM_REGIONS;
  1269. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1270. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1271. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1272. msi_addr = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 8;
  1273. msg.data = (unsigned int)doorbell_template.word;
  1274. } else {
  1275. /* SQ regions are out, allocate from map mem regions. */
  1276. mem_map = gxio_trio_alloc_memory_maps(trio_context, 1, 0, 0);
  1277. if (mem_map < 0) {
  1278. dev_printk(KERN_INFO, &pdev->dev,
  1279. "%s Mem-Map alloc failure. "
  1280. "Failed to initialize MSI interrupts. "
  1281. "Falling back to legacy interrupts.\n",
  1282. desc->msi_attrib.is_msix ? "MSI-X" : "MSI");
  1283. ret = -ENOMEM;
  1284. goto msi_mem_map_alloc_failure;
  1285. }
  1286. mem_map_base = MEM_MAP_INTR_REGIONS_BASE +
  1287. mem_map * MEM_MAP_INTR_REGION_SIZE;
  1288. mem_map_limit = mem_map_base + MEM_MAP_INTR_REGION_SIZE - 1;
  1289. msi_addr = mem_map_base + TRIO_MAP_MEM_REG_INT3 -
  1290. TRIO_MAP_MEM_REG_INT0;
  1291. msg.data = mem_map;
  1292. }
  1293. /* We try to distribute different IRQs to different tiles. */
  1294. cpu = tile_irq_cpu(irq);
  1295. /*
  1296. * Now call up to the HV to configure the MSI interrupt and
  1297. * set up the IPI binding.
  1298. */
  1299. ret = gxio_trio_config_msi_intr(trio_context, cpu_x(cpu), cpu_y(cpu),
  1300. KERNEL_PL, irq, controller->mac,
  1301. mem_map, mem_map_base, mem_map_limit,
  1302. trio_context->asid);
  1303. if (ret < 0) {
  1304. dev_printk(KERN_INFO, &pdev->dev, "HV MSI config failed.\n");
  1305. goto hv_msi_config_failure;
  1306. }
  1307. irq_set_msi_desc(irq, desc);
  1308. msg.address_hi = msi_addr >> 32;
  1309. msg.address_lo = msi_addr & 0xffffffff;
  1310. write_msi_msg(irq, &msg);
  1311. irq_set_chip_and_handler(irq, &tilegx_msi_chip, handle_level_irq);
  1312. irq_set_handler_data(irq, controller);
  1313. return 0;
  1314. hv_msi_config_failure:
  1315. /* Free mem-map */
  1316. msi_mem_map_alloc_failure:
  1317. is_64_failure:
  1318. destroy_irq(irq);
  1319. return ret;
  1320. }
  1321. void arch_teardown_msi_irq(unsigned int irq)
  1322. {
  1323. destroy_irq(irq);
  1324. }