pci_gx.c 37 KB

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