pci_gx.c 42 KB

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