pci_gx.c 38 KB

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