This patch adds a check to ensure that the CPU MMIO region is registered
prior to accessing the PCI IO ports.
[1] https://lore.kernel.org/linux-pci/56F209A9.4040304@xxxxxxxxxx
[2] https://lore.kernel.org/linux-arm-kernel/e6995b4a-184a-d8d4-f4d4-9ce75d8f47c0@xxxxxxxxxx/
This patch includes some other tidy-up.
Signed-off-by: John Garry <john.garry@xxxxxxxxxx>
---
lib/logic_pio.c | 103 +++++++++++++++++++++++++++++++++++-------------
1 file changed, 75 insertions(+), 28 deletions(-)
diff --git a/lib/logic_pio.c b/lib/logic_pio.c
index 431cd8d99236..3d8d986e9dcb 100644
--- a/lib/logic_pio.c
+++ b/lib/logic_pio.c
@@ -193,95 +193,135 @@ unsigned long logic_pio_trans_cpuaddr(resource_size_t addr)
#if defined(PCI_IOBASE)
#if defined(CONFIG_INDIRECT_PIO)
+#define INVALID_RANGE(range) \
+ (!(range) || ((range)->flags == LOGIC_PIO_INDIRECT && !(range)->ops))
+
#define BUILD_LOGIC_IO(bw, type) \
type logic_in##bw(unsigned long addr) \
{ \
type ret = (type)~0; \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return ret; \
+ } \
\
if (addr < MMIO_UPPER_LIMIT) { \
ret = read##bw(PCI_IOBASE + addr); \
} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
- struct logic_pio_hwaddr *entry = find_io_range(addr); \
size_t sz = sizeof(type); \
+ void *hostdata = range->hostdata; \
\
- if (entry && entry->ops) \
- ret = entry->ops->in(entry->hostdata, addr, sz);\
- else \
- WARN_ON_ONCE(1); \
+ if (range->ops->in) \
+ ret = range->ops->in(hostdata, addr, sz); \
} \
return ret; \
} \
\
-void logic_out##bw(type value, unsigned long addr) \
+void logic_out##bw(type val, unsigned long addr) \
{ \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return; \
+ } \
+ \
if (addr < MMIO_UPPER_LIMIT) { \
- write##bw(value, PCI_IOBASE + addr); \
+ write##bw(val, PCI_IOBASE + addr); \
} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
- struct logic_pio_hwaddr *entry = find_io_range(addr); \
size_t sz = sizeof(type); \
+ void *hostdata = range->hostdata; \
\
- if (entry && entry->ops) \
- entry->ops->out(entry->hostdata, \
- addr, value, sz); \
- else \
- WARN_ON_ONCE(1); \
+ if (range->ops->out) \
+ range->ops->out(hostdata, addr, val, sz); \
} \
} \
\
void logic_ins##bw(unsigned long addr, void *buf, unsigned int cnt) \
{ \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return; \
+ } \
+ \
if (addr < MMIO_UPPER_LIMIT) { \
reads##bw(PCI_IOBASE + addr, buf, cnt); \
} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
- struct logic_pio_hwaddr *entry = find_io_range(addr); \
size_t sz = sizeof(type); \
+ void *hostdata = range->hostdata; \
\
- if (entry && entry->ops) \
- entry->ops->ins(entry->hostdata, \
- addr, buf, sz, cnt); \
- else \
- WARN_ON_ONCE(1); \
+ if (range->ops->ins) \
+ range->ops->ins(hostdata, addr, buf, sz, cnt); \
} \
- \
} \
\
void logic_outs##bw(unsigned long addr, const void *buf, \
unsigned int cnt) \
{ \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return; \
+ } \
+ \
if (addr < MMIO_UPPER_LIMIT) { \
writes##bw(PCI_IOBASE + addr, buf, cnt); \
} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
- struct logic_pio_hwaddr *entry = find_io_range(addr); \
size_t sz = sizeof(type); \
+ void *hostdata = range->hostdata; \
\
- if (entry && entry->ops) \
- entry->ops->outs(entry->hostdata, \
- addr, buf, sz, cnt); \
- else \
- WARN_ON_ONCE(1); \
+ if (range->ops->outs) \
+ range->ops->outs(hostdata, addr, buf, sz, cnt); \
} \
}
#else /* CONFIG_INDIRECT_PIO */
+#define INVALID_RANGE(range) (!(range))
+
#define BUILD_LOGIC_IO(bw, type) \
type logic_in##bw(unsigned long addr) \
{ \
type ret = (type)~0; \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return ret; \
+ } \
\
if (addr < MMIO_UPPER_LIMIT) \
ret = read##bw(PCI_IOBASE + addr); \
return ret; \
} \
\
-void logic_out##bw(type value, unsigned long addr) \
+void logic_out##bw(type val, unsigned long addr) \
{ \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return; \
+ } \
+ \
if (addr < MMIO_UPPER_LIMIT) \
- write##bw(value, PCI_IOBASE + addr); \
+ write##bw(val, PCI_IOBASE + addr); \
} \
\
void logic_ins##bw(unsigned long addr, void *buf, unsigned int cnt) \
{ \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return; \
+ } \
+ \
if (addr < MMIO_UPPER_LIMIT) \
reads##bw(PCI_IOBASE + addr, buf, cnt); \
} \
@@ -289,6 +329,13 @@ void logic_ins##bw(unsigned long addr, void *buf, unsigned int cnt) \
void logic_outs##bw(unsigned long addr, const void *buf, \
unsigned int cnt) \
{ \
+ struct logic_pio_hwaddr *range = find_io_range(addr); \
+ \
+ if (INVALID_RANGE(range)) { \
+ WARN_ON_ONCE(1); \
+ return; \
+ } \
+ \
if (addr < MMIO_UPPER_LIMIT) \
writes##bw(PCI_IOBASE + addr, buf, cnt); \
}
--
2.17.1