linux/arch/s390/include/asm/pci_io.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2#ifndef _ASM_S390_PCI_IO_H
   3#define _ASM_S390_PCI_IO_H
   4
   5#ifdef CONFIG_PCI
   6
   7#include <linux/kernel.h>
   8#include <linux/slab.h>
   9#include <asm/pci_insn.h>
  10
  11/* I/O Map */
  12#define ZPCI_IOMAP_SHIFT                48
  13#define ZPCI_IOMAP_ADDR_BASE            0x8000000000000000UL
  14#define ZPCI_IOMAP_ADDR_OFF_MASK        ((1UL << ZPCI_IOMAP_SHIFT) - 1)
  15#define ZPCI_IOMAP_MAX_ENTRIES                                                  \
  16        ((ULONG_MAX - ZPCI_IOMAP_ADDR_BASE + 1) / (1UL << ZPCI_IOMAP_SHIFT))
  17#define ZPCI_IOMAP_ADDR_IDX_MASK                                                \
  18        (~ZPCI_IOMAP_ADDR_OFF_MASK - ZPCI_IOMAP_ADDR_BASE)
  19
  20struct zpci_iomap_entry {
  21        u32 fh;
  22        u8 bar;
  23        u16 count;
  24};
  25
  26extern struct zpci_iomap_entry *zpci_iomap_start;
  27
  28#define ZPCI_ADDR(idx) (ZPCI_IOMAP_ADDR_BASE | ((u64) idx << ZPCI_IOMAP_SHIFT))
  29#define ZPCI_IDX(addr)                                                          \
  30        (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> ZPCI_IOMAP_SHIFT)
  31#define ZPCI_OFFSET(addr)                                                       \
  32        ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
  33
  34#define ZPCI_CREATE_REQ(handle, space, len)                                     \
  35        ((u64) handle << 32 | space << 16 | len)
  36
  37#define zpci_read(LENGTH, RETTYPE)                                              \
  38static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr)    \
  39{                                                                               \
  40        struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)];     \
  41        u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH);               \
  42        u64 data;                                                               \
  43        int rc;                                                                 \
  44                                                                                \
  45        rc = zpci_load(&data, req, ZPCI_OFFSET(addr));                          \
  46        if (rc)                                                                 \
  47                data = -1ULL;                                                   \
  48        return (RETTYPE) data;                                                  \
  49}
  50
  51#define zpci_write(LENGTH, VALTYPE)                                             \
  52static inline void zpci_write_##VALTYPE(VALTYPE val,                            \
  53                                        const volatile void __iomem *addr)      \
  54{                                                                               \
  55        struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)];     \
  56        u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH);               \
  57        u64 data = (VALTYPE) val;                                               \
  58                                                                                \
  59        zpci_store(data, req, ZPCI_OFFSET(addr));                               \
  60}
  61
  62zpci_read(8, u64)
  63zpci_read(4, u32)
  64zpci_read(2, u16)
  65zpci_read(1, u8)
  66zpci_write(8, u64)
  67zpci_write(4, u32)
  68zpci_write(2, u16)
  69zpci_write(1, u8)
  70
  71static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
  72{
  73        u64 val;
  74
  75        switch (len) {
  76        case 1:
  77                val = (u64) *((u8 *) data);
  78                break;
  79        case 2:
  80                val = (u64) *((u16 *) data);
  81                break;
  82        case 4:
  83                val = (u64) *((u32 *) data);
  84                break;
  85        case 8:
  86                val = (u64) *((u64 *) data);
  87                break;
  88        default:
  89                val = 0;                /* let FW report error */
  90                break;
  91        }
  92        return zpci_store(val, req, offset);
  93}
  94
  95static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
  96{
  97        u64 data;
  98        int cc;
  99
 100        cc = zpci_load(&data, req, offset);
 101        if (cc)
 102                goto out;
 103
 104        switch (len) {
 105        case 1:
 106                *((u8 *) dst) = (u8) data;
 107                break;
 108        case 2:
 109                *((u16 *) dst) = (u16) data;
 110                break;
 111        case 4:
 112                *((u32 *) dst) = (u32) data;
 113                break;
 114        case 8:
 115                *((u64 *) dst) = (u64) data;
 116                break;
 117        }
 118out:
 119        return cc;
 120}
 121
 122static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
 123{
 124        return zpci_store_block(data, req, offset);
 125}
 126
 127static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
 128{
 129        int count = len > max ? max : len, size = 1;
 130
 131        while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
 132                dst = dst >> 1;
 133                src = src >> 1;
 134                size = size << 1;
 135        }
 136        return size;
 137}
 138
 139static inline int zpci_memcpy_fromio(void *dst,
 140                                     const volatile void __iomem *src,
 141                                     unsigned long n)
 142{
 143        struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
 144        u64 req, offset = ZPCI_OFFSET(src);
 145        int size, rc = 0;
 146
 147        while (n > 0) {
 148                size = zpci_get_max_write_size((u64 __force) src,
 149                                               (u64) dst, n, 8);
 150                req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
 151                rc = zpci_read_single(req, dst, offset, size);
 152                if (rc)
 153                        break;
 154                offset += size;
 155                dst += size;
 156                n -= size;
 157        }
 158        return rc;
 159}
 160
 161static inline int zpci_memcpy_toio(volatile void __iomem *dst,
 162                                   const void *src, unsigned long n)
 163{
 164        struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
 165        u64 req, offset = ZPCI_OFFSET(dst);
 166        int size, rc = 0;
 167
 168        if (!src)
 169                return -EINVAL;
 170
 171        while (n > 0) {
 172                size = zpci_get_max_write_size((u64 __force) dst,
 173                                               (u64) src, n, 128);
 174                req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
 175
 176                if (size > 8) /* main path */
 177                        rc = zpci_write_block(req, src, offset);
 178                else
 179                        rc = zpci_write_single(req, src, offset, size);
 180                if (rc)
 181                        break;
 182                offset += size;
 183                src += size;
 184                n -= size;
 185        }
 186        return rc;
 187}
 188
 189static inline int zpci_memset_io(volatile void __iomem *dst,
 190                                 unsigned char val, size_t count)
 191{
 192        u8 *src = kmalloc(count, GFP_KERNEL);
 193        int rc;
 194
 195        if (src == NULL)
 196                return -ENOMEM;
 197        memset(src, val, count);
 198
 199        rc = zpci_memcpy_toio(dst, src, count);
 200        kfree(src);
 201        return rc;
 202}
 203
 204#endif /* CONFIG_PCI */
 205
 206#endif /* _ASM_S390_PCI_IO_H */
 207