pci_gx.c 37 KB

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