pci_gx.c 41 KB

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