pci_gx.c 43 KB

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