pci_gx.c 37 KB

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