1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Hisilicon Limited, All Rights Reserved.
4 * Author: Gabriele Paoloni <gabriele.paoloni@huawei.com>
5 * Author: Zhichang Yuan <yuanzhichang@hisilicon.com>
9 #define pr_fmt(fmt) "LOGIC PIO: " fmt
13 #include <linux/logic_pio.h>
15 #include <linux/rculist.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
19 /* The unique hardware address list. */
20 static LIST_HEAD(io_range_list
);
21 static DEFINE_MUTEX(io_range_mutex
);
24 * logic_pio_register_range - register logical PIO range for a host
25 * @new_range: pointer to the io range to be registered.
27 * returns 0 on success, the error code in case of failure
29 * Register a new io range node in the io range list.
31 int logic_pio_register_range(struct logic_pio_hwaddr
*new_range
)
33 struct logic_pio_hwaddr
*range
;
34 resource_size_t start
= new_range
->hw_start
;
35 resource_size_t end
= new_range
->hw_start
+ new_range
->size
;
36 resource_size_t allocated_mmio_size
= 0;
37 resource_size_t allocated_iio_size
= MMIO_UPPER_LIMIT
;
40 if (!new_range
|| !new_range
->fwnode
|| !new_range
->size
)
43 mutex_lock(&io_range_mutex
);
44 list_for_each_entry_rcu(range
, &io_range_list
, list
) {
45 if (range
->fwnode
== new_range
->fwnode
) {
46 /* range already there */
50 if (range
->flags
== PIO_CPU_MMIO
&&
51 new_range
->flags
== PIO_CPU_MMIO
) {
52 /* for MMIO ranges we need to check for overlap */
53 if (start
>= range
->hw_start
+ range
->size
||
54 end
< range
->hw_start
)
55 allocated_mmio_size
+= range
->size
;
60 } else if (range
->flags
== PIO_INDIRECT
&&
61 new_range
->flags
== PIO_INDIRECT
) {
62 allocated_iio_size
+= range
->size
;
66 /* range not registered yet, check for available space */
67 if (new_range
->flags
== PIO_CPU_MMIO
) {
68 if (allocated_mmio_size
+ new_range
->size
- 1 >
70 /* if it's too big check if 64K space can be reserved */
71 if (allocated_mmio_size
+ SZ_64K
- 1 >
76 new_range
->size
= SZ_64K
;
77 pr_warn("Requested IO range too big, new size set to 64K\n");
79 new_range
->io_start
= allocated_mmio_size
;
80 } else if (new_range
->flags
== PIO_INDIRECT
) {
81 if (allocated_iio_size
+ new_range
->size
- 1 >
86 new_range
->io_start
= allocated_iio_size
;
93 list_add_tail_rcu(&new_range
->list
, &io_range_list
);
96 mutex_unlock(&io_range_mutex
);
101 * find_io_range_by_fwnode - find logical PIO range for given FW node
102 * @fwnode: FW node handle associated with logical PIO range
104 * Returns pointer to node on success, NULL otherwise
106 * Traverse the io_range_list to find the registered node whose device node
107 * and/or physical IO address match to.
109 struct logic_pio_hwaddr
*find_io_range_by_fwnode(struct fwnode_handle
*fwnode
)
111 struct logic_pio_hwaddr
*range
;
113 list_for_each_entry_rcu(range
, &io_range_list
, list
) {
114 if (range
->fwnode
== fwnode
)
120 /* Return a registered range given an input PIO token */
121 static struct logic_pio_hwaddr
*find_io_range(unsigned long pio
)
123 struct logic_pio_hwaddr
*range
;
125 list_for_each_entry_rcu(range
, &io_range_list
, list
) {
126 if (pio
>= range
->io_start
&&
127 pio
< range
->io_start
+ range
->size
)
130 pr_err("PIO entry token invalid\n");
135 * logic_pio_to_hwaddr - translate logical PIO to HW address
136 * @pio: logical PIO value
138 * Returns HW address if valid, -1 otherwise
140 * Translate the input logical pio to the corresponding hardware address.
141 * The input pio should be unique in the whole logical PIO space.
143 resource_size_t
logic_pio_to_hwaddr(unsigned long pio
)
145 struct logic_pio_hwaddr
*range
;
146 resource_size_t hwaddr
= -1;
148 range
= find_io_range(pio
);
150 hwaddr
= range
->hw_start
+ pio
- range
->io_start
;
156 * logic_pio_trans_hwaddr - translate HW address to logical PIO
157 * @fwnode: FW node reference for the host
158 * @addr: Host-relative HW address
159 * @size: size to translate
161 * Returns Logical PIO value if successful, -1 otherwise
164 logic_pio_trans_hwaddr(struct fwnode_handle
*fwnode
, resource_size_t addr
,
165 resource_size_t size
)
167 struct logic_pio_hwaddr
*range
;
169 range
= find_io_range_by_fwnode(fwnode
);
170 if (!range
|| range
->flags
== PIO_CPU_MMIO
) {
171 pr_err("range not found or invalid\n");
174 if (range
->size
< size
) {
175 pr_err("resource size %pa cannot fit in IO range size %pa\n",
176 &size
, &range
->size
);
179 return addr
- range
->hw_start
+ range
->io_start
;
183 logic_pio_trans_cpuaddr(resource_size_t addr
)
185 struct logic_pio_hwaddr
*range
;
187 list_for_each_entry_rcu(range
, &io_range_list
, list
) {
188 if (range
->flags
!= PIO_CPU_MMIO
)
190 if (addr
>= range
->hw_start
&&
191 addr
< range
->hw_start
+ range
->size
)
192 return addr
- range
->hw_start
+
195 pr_err("addr not registered in io_range_list\n");
199 #if defined(CONFIG_INDIRECT_PIO) && defined(PCI_IOBASE)
200 #define BUILD_LOGIC_IO(bw, type) \
201 type logic_in##bw(unsigned long addr) \
205 if (addr < MMIO_UPPER_LIMIT) { \
206 ret = read##bw(PCI_IOBASE + addr); \
207 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
208 struct logic_pio_hwaddr *entry = find_io_range(addr); \
210 if (entry && entry->ops) \
211 ret = entry->ops->in(entry->hostdata, \
212 addr, sizeof(type)); \
219 void logic_out##bw(type value, unsigned long addr) \
221 if (addr < MMIO_UPPER_LIMIT) { \
222 write##bw(value, PCI_IOBASE + addr); \
223 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
224 struct logic_pio_hwaddr *entry = find_io_range(addr); \
226 if (entry && entry->ops) \
227 entry->ops->out(entry->hostdata, \
228 addr, value, sizeof(type)); \
234 void logic_ins##bw(unsigned long addr, void *buffer, \
235 unsigned int count) \
237 if (addr < MMIO_UPPER_LIMIT) { \
238 reads##bw(PCI_IOBASE + addr, buffer, count); \
239 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
240 struct logic_pio_hwaddr *entry = find_io_range(addr); \
242 if (entry && entry->ops) \
243 entry->ops->ins(entry->hostdata, \
244 addr, buffer, sizeof(type), count); \
251 void logic_outs##bw(unsigned long addr, const void *buffer, \
252 unsigned int count) \
254 if (addr < MMIO_UPPER_LIMIT) { \
255 writes##bw(PCI_IOBASE + addr, buffer, count); \
256 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
257 struct logic_pio_hwaddr *entry = find_io_range(addr); \
259 if (entry && entry->ops) \
260 entry->ops->outs(entry->hostdata, \
261 addr, buffer, sizeof(type), count); \
267 BUILD_LOGIC_IO(b
, u8
)
268 EXPORT_SYMBOL(logic_inb
);
269 EXPORT_SYMBOL(logic_insb
);
270 EXPORT_SYMBOL(logic_outb
);
271 EXPORT_SYMBOL(logic_outsb
);
273 BUILD_LOGIC_IO(w
, u16
)
274 EXPORT_SYMBOL(logic_inw
);
275 EXPORT_SYMBOL(logic_insw
);
276 EXPORT_SYMBOL(logic_outw
);
277 EXPORT_SYMBOL(logic_outsw
);
279 BUILD_LOGIC_IO(l
, u32
)
280 EXPORT_SYMBOL(logic_inl
);
281 EXPORT_SYMBOL(logic_insl
);
282 EXPORT_SYMBOL(logic_outl
);
283 EXPORT_SYMBOL(logic_outsl
);
285 #endif /* CONFIG_INDIRECT_PIO && PCI_IOBASE */