Path: blob/master/arch/powerpc/sysdev/mpic_u3msi.c
10817 views
/*1* Copyright 2006, Segher Boessenkool, IBM Corporation.2* Copyright 2006-2007, Michael Ellerman, IBM Corporation.3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License6* as published by the Free Software Foundation; version 2 of the7* License.8*9*/1011#include <linux/irq.h>12#include <linux/bootmem.h>13#include <linux/msi.h>14#include <asm/mpic.h>15#include <asm/prom.h>16#include <asm/hw_irq.h>17#include <asm/ppc-pci.h>18#include <asm/msi_bitmap.h>1920#include "mpic.h"2122/* A bit ugly, can we get this from the pci_dev somehow? */23static struct mpic *msi_mpic;2425static void mpic_u3msi_mask_irq(struct irq_data *data)26{27mask_msi_irq(data);28mpic_mask_irq(data);29}3031static void mpic_u3msi_unmask_irq(struct irq_data *data)32{33mpic_unmask_irq(data);34unmask_msi_irq(data);35}3637static struct irq_chip mpic_u3msi_chip = {38.irq_shutdown = mpic_u3msi_mask_irq,39.irq_mask = mpic_u3msi_mask_irq,40.irq_unmask = mpic_u3msi_unmask_irq,41.irq_eoi = mpic_end_irq,42.irq_set_type = mpic_set_irq_type,43.irq_set_affinity = mpic_set_affinity,44.name = "MPIC-U3MSI",45};4647static u64 read_ht_magic_addr(struct pci_dev *pdev, unsigned int pos)48{49u8 flags;50u32 tmp;51u64 addr;5253pci_read_config_byte(pdev, pos + HT_MSI_FLAGS, &flags);5455if (flags & HT_MSI_FLAGS_FIXED)56return HT_MSI_FIXED_ADDR;5758pci_read_config_dword(pdev, pos + HT_MSI_ADDR_LO, &tmp);59addr = tmp & HT_MSI_ADDR_LO_MASK;60pci_read_config_dword(pdev, pos + HT_MSI_ADDR_HI, &tmp);61addr = addr | ((u64)tmp << 32);6263return addr;64}6566static u64 find_ht_magic_addr(struct pci_dev *pdev, unsigned int hwirq)67{68struct pci_bus *bus;69unsigned int pos;7071for (bus = pdev->bus; bus && bus->self; bus = bus->parent) {72pos = pci_find_ht_capability(bus->self, HT_CAPTYPE_MSI_MAPPING);73if (pos)74return read_ht_magic_addr(bus->self, pos);75}7677return 0;78}7980static u64 find_u4_magic_addr(struct pci_dev *pdev, unsigned int hwirq)81{82struct pci_controller *hose = pci_bus_to_host(pdev->bus);8384/* U4 PCIe MSIs need to write to the special register in85* the bridge that generates interrupts. There should be86* theorically a register at 0xf8005000 where you just write87* the MSI number and that triggers the right interrupt, but88* unfortunately, this is busted in HW, the bridge endian swaps89* the value and hits the wrong nibble in the register.90*91* So instead we use another register set which is used normally92* for converting HT interrupts to MPIC interrupts, which decodes93* the interrupt number as part of the low address bits94*95* This will not work if we ever use more than one legacy MSI in96* a block but we never do. For one MSI or multiple MSI-X where97* each interrupt address can be specified separately, it works98* just fine.99*/100if (of_device_is_compatible(hose->dn, "u4-pcie") ||101of_device_is_compatible(hose->dn, "U4-pcie"))102return 0xf8004000 | (hwirq << 4);103104return 0;105}106107static int u3msi_msi_check_device(struct pci_dev *pdev, int nvec, int type)108{109if (type == PCI_CAP_ID_MSIX)110pr_debug("u3msi: MSI-X untested, trying anyway.\n");111112/* If we can't find a magic address then MSI ain't gonna work */113if (find_ht_magic_addr(pdev, 0) == 0 &&114find_u4_magic_addr(pdev, 0) == 0) {115pr_debug("u3msi: no magic address found for %s\n",116pci_name(pdev));117return -ENXIO;118}119120return 0;121}122123static void u3msi_teardown_msi_irqs(struct pci_dev *pdev)124{125struct msi_desc *entry;126127list_for_each_entry(entry, &pdev->msi_list, list) {128if (entry->irq == NO_IRQ)129continue;130131irq_set_msi_desc(entry->irq, NULL);132msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap,133virq_to_hw(entry->irq), 1);134irq_dispose_mapping(entry->irq);135}136137return;138}139140static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)141{142unsigned int virq;143struct msi_desc *entry;144struct msi_msg msg;145u64 addr;146int hwirq;147148list_for_each_entry(entry, &pdev->msi_list, list) {149hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1);150if (hwirq < 0) {151pr_debug("u3msi: failed allocating hwirq\n");152return hwirq;153}154155addr = find_ht_magic_addr(pdev, hwirq);156if (addr == 0)157addr = find_u4_magic_addr(pdev, hwirq);158msg.address_lo = addr & 0xFFFFFFFF;159msg.address_hi = addr >> 32;160161virq = irq_create_mapping(msi_mpic->irqhost, hwirq);162if (virq == NO_IRQ) {163pr_debug("u3msi: failed mapping hwirq 0x%x\n", hwirq);164msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);165return -ENOSPC;166}167168irq_set_msi_desc(virq, entry);169irq_set_chip(virq, &mpic_u3msi_chip);170irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);171172pr_debug("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",173virq, hwirq, (unsigned long)addr);174175printk("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",176virq, hwirq, (unsigned long)addr);177msg.data = hwirq;178write_msi_msg(virq, &msg);179180hwirq++;181}182183return 0;184}185186int mpic_u3msi_init(struct mpic *mpic)187{188int rc;189190rc = mpic_msi_init_allocator(mpic);191if (rc) {192pr_debug("u3msi: Error allocating bitmap!\n");193return rc;194}195196pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n");197198BUG_ON(msi_mpic);199msi_mpic = mpic;200201WARN_ON(ppc_md.setup_msi_irqs);202ppc_md.setup_msi_irqs = u3msi_setup_msi_irqs;203ppc_md.teardown_msi_irqs = u3msi_teardown_msi_irqs;204ppc_md.msi_check_device = u3msi_msi_check_device;205206return 0;207}208209210