pci_gx.c 38 KB

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