2019-05-27 13:55:05 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-03-30 06:10:24 +07:00
|
|
|
/*
|
|
|
|
* Adding PCI-E MSI support for PPC4XX SoCs.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010, Applied Micro Circuits Corporation
|
|
|
|
* Authors: Tirumala R Marri <tmarri@apm.com>
|
|
|
|
* Feng Kan <fkan@apm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/msi.h>
|
|
|
|
#include <linux/of_platform.h>
|
|
|
|
#include <linux/interrupt.h>
|
2011-07-29 13:19:31 +07:00
|
|
|
#include <linux/export.h>
|
2012-03-19 00:59:08 +07:00
|
|
|
#include <linux/kernel.h>
|
2011-03-30 06:10:24 +07:00
|
|
|
#include <asm/prom.h>
|
|
|
|
#include <asm/hw_irq.h>
|
|
|
|
#include <asm/ppc-pci.h>
|
2012-03-19 00:59:08 +07:00
|
|
|
#include <asm/dcr.h>
|
2011-03-30 06:10:24 +07:00
|
|
|
#include <asm/dcr-regs.h>
|
|
|
|
#include <asm/msi_bitmap.h>
|
|
|
|
|
|
|
|
#define PEIH_TERMADH 0x00
|
|
|
|
#define PEIH_TERMADL 0x08
|
|
|
|
#define PEIH_MSIED 0x10
|
|
|
|
#define PEIH_MSIMK 0x18
|
|
|
|
#define PEIH_MSIASS 0x20
|
|
|
|
#define PEIH_FLUSH0 0x30
|
|
|
|
#define PEIH_FLUSH1 0x38
|
|
|
|
#define PEIH_CNTRST 0x48
|
2012-03-19 00:59:08 +07:00
|
|
|
|
|
|
|
static int msi_irqs;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
struct ppc4xx_msi {
|
|
|
|
u32 msi_addr_lo;
|
|
|
|
u32 msi_addr_hi;
|
|
|
|
void __iomem *msi_regs;
|
2012-03-19 00:59:08 +07:00
|
|
|
int *msi_virqs;
|
2011-03-30 06:10:24 +07:00
|
|
|
struct msi_bitmap bitmap;
|
|
|
|
struct device_node *msi_dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ppc4xx_msi ppc4xx_msi;
|
|
|
|
|
|
|
|
static int ppc4xx_msi_init_allocator(struct platform_device *dev,
|
|
|
|
struct ppc4xx_msi *msi_data)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2012-03-19 00:59:08 +07:00
|
|
|
err = msi_bitmap_alloc(&msi_data->bitmap, msi_irqs,
|
2011-03-30 06:10:24 +07:00
|
|
|
dev->dev.of_node);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
|
|
|
|
if (err < 0) {
|
|
|
|
msi_bitmap_free(&msi_data->bitmap);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
|
|
|
|
{
|
|
|
|
int int_no = -ENOMEM;
|
|
|
|
unsigned int virq;
|
|
|
|
struct msi_msg msg;
|
|
|
|
struct msi_desc *entry;
|
|
|
|
struct ppc4xx_msi *msi_data = &ppc4xx_msi;
|
|
|
|
|
2014-09-08 01:57:53 +07:00
|
|
|
dev_dbg(&dev->dev, "PCIE-MSI:%s called. vec %x type %d\n",
|
|
|
|
__func__, nvec, type);
|
|
|
|
if (type == PCI_CAP_ID_MSIX)
|
|
|
|
pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
|
|
|
|
|
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:
kmalloc(a * b, gfp)
with:
kmalloc_array(a * b, gfp)
as well as handling cases of:
kmalloc(a * b * c, gfp)
with:
kmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kmalloc
+ kmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kmalloc(sizeof(THING) * C2, ...)
|
kmalloc(sizeof(TYPE) * C2, ...)
|
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 03:55:00 +07:00
|
|
|
msi_data->msi_virqs = kmalloc_array(msi_irqs, sizeof(int), GFP_KERNEL);
|
2012-03-19 00:59:08 +07:00
|
|
|
if (!msi_data->msi_virqs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-07-09 15:00:38 +07:00
|
|
|
for_each_pci_msi_entry(entry, dev) {
|
2011-03-30 06:10:24 +07:00
|
|
|
int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
|
|
|
|
if (int_no >= 0)
|
|
|
|
break;
|
|
|
|
if (int_no < 0) {
|
|
|
|
pr_debug("%s: fail allocating msi interrupt\n",
|
|
|
|
__func__);
|
|
|
|
}
|
|
|
|
virq = irq_of_parse_and_map(msi_data->msi_dev, int_no);
|
2016-09-06 18:53:24 +07:00
|
|
|
if (!virq) {
|
2011-03-30 06:10:24 +07:00
|
|
|
dev_err(&dev->dev, "%s: fail mapping irq\n", __func__);
|
|
|
|
msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1);
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq);
|
|
|
|
|
|
|
|
/* Setup msi address space */
|
|
|
|
msg.address_hi = msi_data->msi_addr_hi;
|
|
|
|
msg.address_lo = msi_data->msi_addr_lo;
|
|
|
|
|
|
|
|
irq_set_msi_desc(virq, entry);
|
|
|
|
msg.data = int_no;
|
2014-11-09 22:10:34 +07:00
|
|
|
pci_write_msi_msg(virq, &msg);
|
2011-03-30 06:10:24 +07:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ppc4xx_teardown_msi_irqs(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
struct msi_desc *entry;
|
|
|
|
struct ppc4xx_msi *msi_data = &ppc4xx_msi;
|
powerpc/MSI: Fix race condition in tearing down MSI interrupts
This fixes a race which can result in the same virtual IRQ number
being assigned to two different MSI interrupts. The most visible
consequence of that is usually a warning and stack trace from the
sysfs code about an attempt to create a duplicate entry in sysfs.
The race happens when one CPU (say CPU 0) is disposing of an MSI
while another CPU (say CPU 1) is setting up an MSI. CPU 0 calls
(for example) pnv_teardown_msi_irqs(), which calls
msi_bitmap_free_hwirqs() to indicate that the MSI (i.e. its
hardware IRQ number) is no longer in use. Then, before CPU 0 gets
to calling irq_dispose_mapping() to free up the virtal IRQ number,
CPU 1 comes in and calls msi_bitmap_alloc_hwirqs() to allocate an
MSI, and gets the same hardware IRQ number that CPU 0 just freed.
CPU 1 then calls irq_create_mapping() to get a virtual IRQ number,
which sees that there is currently a mapping for that hardware IRQ
number and returns the corresponding virtual IRQ number (which is
the same virtual IRQ number that CPU 0 was using). CPU 0 then
calls irq_dispose_mapping() and frees that virtual IRQ number.
Now, if another CPU comes along and calls irq_create_mapping(), it
is likely to get the virtual IRQ number that was just freed,
resulting in the same virtual IRQ number apparently being used for
two different hardware interrupts.
To fix this race, we just move the call to msi_bitmap_free_hwirqs()
to after the call to irq_dispose_mapping(). Since virq_to_hw()
doesn't work for the virtual IRQ number after irq_dispose_mapping()
has been called, we need to call it before irq_dispose_mapping() and
remember the result for the msi_bitmap_free_hwirqs() call.
The pattern of calling msi_bitmap_free_hwirqs() before
irq_dispose_mapping() appears in 5 places under arch/powerpc, and
appears to have originated in commit 05af7bd2d75e ("[POWERPC] MPIC
U3/U4 MSI backend") from 2007.
Fixes: 05af7bd2d75e ("[POWERPC] MPIC U3/U4 MSI backend")
Cc: stable@vger.kernel.org # v2.6.22+
Reported-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2015-09-10 11:36:21 +07:00
|
|
|
irq_hw_number_t hwirq;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n");
|
|
|
|
|
2015-07-09 15:00:38 +07:00
|
|
|
for_each_pci_msi_entry(entry, dev) {
|
2016-09-06 18:53:24 +07:00
|
|
|
if (!entry->irq)
|
2011-03-30 06:10:24 +07:00
|
|
|
continue;
|
powerpc/MSI: Fix race condition in tearing down MSI interrupts
This fixes a race which can result in the same virtual IRQ number
being assigned to two different MSI interrupts. The most visible
consequence of that is usually a warning and stack trace from the
sysfs code about an attempt to create a duplicate entry in sysfs.
The race happens when one CPU (say CPU 0) is disposing of an MSI
while another CPU (say CPU 1) is setting up an MSI. CPU 0 calls
(for example) pnv_teardown_msi_irqs(), which calls
msi_bitmap_free_hwirqs() to indicate that the MSI (i.e. its
hardware IRQ number) is no longer in use. Then, before CPU 0 gets
to calling irq_dispose_mapping() to free up the virtal IRQ number,
CPU 1 comes in and calls msi_bitmap_alloc_hwirqs() to allocate an
MSI, and gets the same hardware IRQ number that CPU 0 just freed.
CPU 1 then calls irq_create_mapping() to get a virtual IRQ number,
which sees that there is currently a mapping for that hardware IRQ
number and returns the corresponding virtual IRQ number (which is
the same virtual IRQ number that CPU 0 was using). CPU 0 then
calls irq_dispose_mapping() and frees that virtual IRQ number.
Now, if another CPU comes along and calls irq_create_mapping(), it
is likely to get the virtual IRQ number that was just freed,
resulting in the same virtual IRQ number apparently being used for
two different hardware interrupts.
To fix this race, we just move the call to msi_bitmap_free_hwirqs()
to after the call to irq_dispose_mapping(). Since virq_to_hw()
doesn't work for the virtual IRQ number after irq_dispose_mapping()
has been called, we need to call it before irq_dispose_mapping() and
remember the result for the msi_bitmap_free_hwirqs() call.
The pattern of calling msi_bitmap_free_hwirqs() before
irq_dispose_mapping() appears in 5 places under arch/powerpc, and
appears to have originated in commit 05af7bd2d75e ("[POWERPC] MPIC
U3/U4 MSI backend") from 2007.
Fixes: 05af7bd2d75e ("[POWERPC] MPIC U3/U4 MSI backend")
Cc: stable@vger.kernel.org # v2.6.22+
Reported-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2015-09-10 11:36:21 +07:00
|
|
|
hwirq = virq_to_hw(entry->irq);
|
2011-03-30 06:10:24 +07:00
|
|
|
irq_set_msi_desc(entry->irq, NULL);
|
|
|
|
irq_dispose_mapping(entry->irq);
|
powerpc/MSI: Fix race condition in tearing down MSI interrupts
This fixes a race which can result in the same virtual IRQ number
being assigned to two different MSI interrupts. The most visible
consequence of that is usually a warning and stack trace from the
sysfs code about an attempt to create a duplicate entry in sysfs.
The race happens when one CPU (say CPU 0) is disposing of an MSI
while another CPU (say CPU 1) is setting up an MSI. CPU 0 calls
(for example) pnv_teardown_msi_irqs(), which calls
msi_bitmap_free_hwirqs() to indicate that the MSI (i.e. its
hardware IRQ number) is no longer in use. Then, before CPU 0 gets
to calling irq_dispose_mapping() to free up the virtal IRQ number,
CPU 1 comes in and calls msi_bitmap_alloc_hwirqs() to allocate an
MSI, and gets the same hardware IRQ number that CPU 0 just freed.
CPU 1 then calls irq_create_mapping() to get a virtual IRQ number,
which sees that there is currently a mapping for that hardware IRQ
number and returns the corresponding virtual IRQ number (which is
the same virtual IRQ number that CPU 0 was using). CPU 0 then
calls irq_dispose_mapping() and frees that virtual IRQ number.
Now, if another CPU comes along and calls irq_create_mapping(), it
is likely to get the virtual IRQ number that was just freed,
resulting in the same virtual IRQ number apparently being used for
two different hardware interrupts.
To fix this race, we just move the call to msi_bitmap_free_hwirqs()
to after the call to irq_dispose_mapping(). Since virq_to_hw()
doesn't work for the virtual IRQ number after irq_dispose_mapping()
has been called, we need to call it before irq_dispose_mapping() and
remember the result for the msi_bitmap_free_hwirqs() call.
The pattern of calling msi_bitmap_free_hwirqs() before
irq_dispose_mapping() appears in 5 places under arch/powerpc, and
appears to have originated in commit 05af7bd2d75e ("[POWERPC] MPIC
U3/U4 MSI backend") from 2007.
Fixes: 05af7bd2d75e ("[POWERPC] MPIC U3/U4 MSI backend")
Cc: stable@vger.kernel.org # v2.6.22+
Reported-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2015-09-10 11:36:21 +07:00
|
|
|
msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
|
2011-03-30 06:10:24 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ppc4xx_setup_pcieh_hw(struct platform_device *dev,
|
|
|
|
struct resource res, struct ppc4xx_msi *msi)
|
|
|
|
{
|
|
|
|
const u32 *msi_data;
|
|
|
|
const u32 *msi_mask;
|
|
|
|
const u32 *sdr_addr;
|
|
|
|
dma_addr_t msi_phys;
|
|
|
|
void *msi_virt;
|
2018-07-31 08:44:14 +07:00
|
|
|
int err;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL);
|
|
|
|
if (!sdr_addr)
|
2018-07-31 08:44:14 +07:00
|
|
|
return -EINVAL;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
2018-07-31 08:44:14 +07:00
|
|
|
msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL);
|
|
|
|
if (!msi_data)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL);
|
|
|
|
if (!msi_mask)
|
|
|
|
return -EINVAL;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi");
|
2012-03-19 00:59:08 +07:00
|
|
|
if (!msi->msi_dev)
|
2011-03-30 06:10:24 +07:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
msi->msi_regs = of_iomap(msi->msi_dev, 0);
|
|
|
|
if (!msi->msi_regs) {
|
2018-07-31 08:44:14 +07:00
|
|
|
dev_err(&dev->dev, "of_iomap failed\n");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto node_put;
|
2011-03-30 06:10:24 +07:00
|
|
|
}
|
|
|
|
dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n",
|
|
|
|
(u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs));
|
|
|
|
|
|
|
|
msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL);
|
2018-07-31 08:44:14 +07:00
|
|
|
if (!msi_virt) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto iounmap;
|
|
|
|
}
|
2012-05-04 07:13:13 +07:00
|
|
|
msi->msi_addr_hi = upper_32_bits(msi_phys);
|
|
|
|
msi->msi_addr_lo = lower_32_bits(msi_phys & 0xffffffff);
|
2012-03-19 00:59:08 +07:00
|
|
|
dev_dbg(&dev->dev, "PCIE-MSI: msi address high 0x%x, low 0x%x\n",
|
|
|
|
msi->msi_addr_hi, msi->msi_addr_lo);
|
2011-03-30 06:10:24 +07:00
|
|
|
|
2018-07-31 08:44:14 +07:00
|
|
|
mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */
|
|
|
|
mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */
|
|
|
|
|
2011-03-30 06:10:24 +07:00
|
|
|
/* Progam the Interrupt handler Termination addr registers */
|
|
|
|
out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi);
|
|
|
|
out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo);
|
|
|
|
|
|
|
|
/* Program MSI Expected data and Mask bits */
|
|
|
|
out_be32(msi->msi_regs + PEIH_MSIED, *msi_data);
|
|
|
|
out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask);
|
|
|
|
|
2012-03-19 00:59:08 +07:00
|
|
|
dma_free_coherent(&dev->dev, 64, msi_virt, msi_phys);
|
|
|
|
|
2011-03-30 06:10:24 +07:00
|
|
|
return 0;
|
2018-07-31 08:44:14 +07:00
|
|
|
|
|
|
|
iounmap:
|
|
|
|
iounmap(msi->msi_regs);
|
|
|
|
node_put:
|
|
|
|
of_node_put(msi->msi_dev);
|
|
|
|
return err;
|
2011-03-30 06:10:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ppc4xx_of_msi_remove(struct platform_device *dev)
|
|
|
|
{
|
|
|
|
struct ppc4xx_msi *msi = dev->dev.platform_data;
|
|
|
|
int i;
|
|
|
|
int virq;
|
|
|
|
|
2012-03-19 00:59:08 +07:00
|
|
|
for (i = 0; i < msi_irqs; i++) {
|
2011-03-30 06:10:24 +07:00
|
|
|
virq = msi->msi_virqs[i];
|
2016-09-06 18:53:24 +07:00
|
|
|
if (virq)
|
2011-03-30 06:10:24 +07:00
|
|
|
irq_dispose_mapping(virq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msi->bitmap.bitmap)
|
|
|
|
msi_bitmap_free(&msi->bitmap);
|
|
|
|
iounmap(msi->msi_regs);
|
|
|
|
of_node_put(msi->msi_dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-22 05:04:10 +07:00
|
|
|
static int ppc4xx_msi_probe(struct platform_device *dev)
|
2011-03-30 06:10:24 +07:00
|
|
|
{
|
|
|
|
struct ppc4xx_msi *msi;
|
|
|
|
struct resource res;
|
|
|
|
int err = 0;
|
2015-04-14 11:27:59 +07:00
|
|
|
struct pci_controller *phb;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n");
|
|
|
|
|
2018-07-31 08:44:14 +07:00
|
|
|
msi = devm_kzalloc(&dev->dev, sizeof(*msi), GFP_KERNEL);
|
|
|
|
if (!msi)
|
2011-03-30 06:10:24 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
dev->dev.platform_data = msi;
|
|
|
|
|
|
|
|
/* Get MSI ranges */
|
|
|
|
err = of_address_to_resource(dev->dev.of_node, 0, &res);
|
|
|
|
if (err) {
|
2017-08-21 22:16:47 +07:00
|
|
|
dev_err(&dev->dev, "%pOF resource error!\n", dev->dev.of_node);
|
2018-07-31 08:44:14 +07:00
|
|
|
return err;
|
2011-03-30 06:10:24 +07:00
|
|
|
}
|
|
|
|
|
2012-03-19 00:59:08 +07:00
|
|
|
msi_irqs = of_irq_count(dev->dev.of_node);
|
|
|
|
if (!msi_irqs)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2018-03-26 21:43:09 +07:00
|
|
|
err = ppc4xx_setup_pcieh_hw(dev, res, msi);
|
|
|
|
if (err)
|
2018-07-31 08:44:14 +07:00
|
|
|
return err;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
err = ppc4xx_msi_init_allocator(dev, msi);
|
|
|
|
if (err) {
|
|
|
|
dev_err(&dev->dev, "Error allocating MSI bitmap\n");
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-03-19 00:59:08 +07:00
|
|
|
ppc4xx_msi = *msi;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
2015-04-14 11:27:59 +07:00
|
|
|
list_for_each_entry(phb, &hose_list, list_node) {
|
|
|
|
phb->controller_ops.setup_msi_irqs = ppc4xx_setup_msi_irqs;
|
|
|
|
phb->controller_ops.teardown_msi_irqs = ppc4xx_teardown_msi_irqs;
|
|
|
|
}
|
2018-07-31 08:44:14 +07:00
|
|
|
return 0;
|
2011-03-30 06:10:24 +07:00
|
|
|
|
|
|
|
error_out:
|
|
|
|
ppc4xx_of_msi_remove(dev);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
static const struct of_device_id ppc4xx_msi_ids[] = {
|
|
|
|
{
|
|
|
|
.compatible = "amcc,ppc4xx-msi",
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
static struct platform_driver ppc4xx_msi_driver = {
|
|
|
|
.probe = ppc4xx_msi_probe,
|
|
|
|
.remove = ppc4xx_of_msi_remove,
|
|
|
|
.driver = {
|
|
|
|
.name = "ppc4xx-msi",
|
|
|
|
.of_match_table = ppc4xx_msi_ids,
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static __init int ppc4xx_msi_init(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&ppc4xx_msi_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(ppc4xx_msi_init);
|