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>
6 * Author: John Garry <john.garry@huawei.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);
23 /* Consider a kernel general helper for this */
24 #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
27 * logic_pio_register_range - register logical PIO range for a host
28 * @new_range: pointer to the IO range to be registered.
30 * Returns 0 on success, the error code in case of failure.
32 * Register a new IO range node in the IO range list.
34 int logic_pio_register_range(struct logic_pio_hwaddr *new_range)
36 struct logic_pio_hwaddr *range;
37 resource_size_t start;
39 resource_size_t mmio_end = 0;
40 resource_size_t iio_sz = MMIO_UPPER_LIMIT;
43 if (!new_range || !new_range->fwnode || !new_range->size ||
44 (new_range->flags == LOGIC_PIO_INDIRECT && !new_range->ops))
47 start = new_range->hw_start;
48 end = new_range->hw_start + new_range->size;
50 mutex_lock(&io_range_mutex);
51 list_for_each_entry(range, &io_range_list, list) {
52 if (range->fwnode == new_range->fwnode) {
53 /* range already there */
56 if (range->flags == LOGIC_PIO_CPU_MMIO &&
57 new_range->flags == LOGIC_PIO_CPU_MMIO) {
58 /* for MMIO ranges we need to check for overlap */
59 if (start >= range->hw_start + range->size ||
60 end < range->hw_start) {
61 mmio_end = range->io_start + range->size;
66 } else if (range->flags == LOGIC_PIO_INDIRECT &&
67 new_range->flags == LOGIC_PIO_INDIRECT) {
68 iio_sz += range->size;
72 /* range not registered yet, check for available space */
73 if (new_range->flags == LOGIC_PIO_CPU_MMIO) {
74 if (mmio_end + new_range->size - 1 > MMIO_UPPER_LIMIT) {
75 /* if it's too big check if 64K space can be reserved */
76 if (mmio_end + SZ_64K - 1 > MMIO_UPPER_LIMIT) {
80 new_range->size = SZ_64K;
81 pr_warn("Requested IO range too big, new size set to 64K\n");
83 new_range->io_start = mmio_end;
84 } else if (new_range->flags == LOGIC_PIO_INDIRECT) {
85 if (iio_sz + new_range->size - 1 > IO_SPACE_LIMIT) {
89 new_range->io_start = iio_sz;
96 list_add_tail_rcu(&new_range->list, &io_range_list);
99 mutex_unlock(&io_range_mutex);
104 * logic_pio_unregister_range - unregister a logical PIO range for a host
105 * @range: pointer to the IO range which has been already registered.
107 * Unregister a previously-registered IO range node.
109 void logic_pio_unregister_range(struct logic_pio_hwaddr *range)
111 mutex_lock(&io_range_mutex);
112 list_del_rcu(&range->list);
113 mutex_unlock(&io_range_mutex);
118 * find_io_range_by_fwnode - find logical PIO range for given FW node
119 * @fwnode: FW node handle associated with logical PIO range
121 * Returns pointer to node on success, NULL otherwise.
123 * Traverse the io_range_list to find the registered node for @fwnode.
125 struct logic_pio_hwaddr *find_io_range_by_fwnode(struct fwnode_handle *fwnode)
127 struct logic_pio_hwaddr *range, *found_range = NULL;
130 list_for_each_entry_rcu(range, &io_range_list, list) {
131 if (range->fwnode == fwnode) {
141 /* Return a registered range given an input PIO token */
142 static struct logic_pio_hwaddr *find_io_range(unsigned long pio)
144 struct logic_pio_hwaddr *range, *found_range = NULL;
147 list_for_each_entry_rcu(range, &io_range_list, list) {
148 if (in_range(pio, range->io_start, range->size)) {
156 pr_err("PIO entry token 0x%lx invalid\n", pio);
162 * logic_pio_to_hwaddr - translate logical PIO to HW address
163 * @pio: logical PIO value
165 * Returns HW address if valid, ~0 otherwise.
167 * Translate the input logical PIO to the corresponding hardware address.
168 * The input PIO should be unique in the whole logical PIO space.
170 resource_size_t logic_pio_to_hwaddr(unsigned long pio)
172 struct logic_pio_hwaddr *range;
174 range = find_io_range(pio);
176 return range->hw_start + pio - range->io_start;
178 return (resource_size_t)~0;
182 * logic_pio_trans_hwaddr - translate HW address to logical PIO
183 * @fwnode: FW node reference for the host
184 * @addr: Host-relative HW address
185 * @size: size to translate
187 * Returns Logical PIO value if successful, ~0UL otherwise
189 unsigned long logic_pio_trans_hwaddr(struct fwnode_handle *fwnode,
190 resource_size_t addr, resource_size_t size)
192 struct logic_pio_hwaddr *range;
194 range = find_io_range_by_fwnode(fwnode);
195 if (!range || range->flags == LOGIC_PIO_CPU_MMIO) {
196 pr_err("IO range not found or invalid\n");
199 if (range->size < size) {
200 pr_err("resource size %pa cannot fit in IO range size %pa\n",
201 &size, &range->size);
204 return addr - range->hw_start + range->io_start;
207 unsigned long logic_pio_trans_cpuaddr(resource_size_t addr)
209 struct logic_pio_hwaddr *range;
212 list_for_each_entry_rcu(range, &io_range_list, list) {
213 if (range->flags != LOGIC_PIO_CPU_MMIO)
215 if (in_range(addr, range->hw_start, range->size)) {
216 unsigned long cpuaddr;
218 cpuaddr = addr - range->hw_start + range->io_start;
226 pr_err("addr %pa not registered in io_range_list\n", &addr);
231 #if defined(CONFIG_INDIRECT_PIO) && defined(PCI_IOBASE)
232 #define BUILD_LOGIC_IO(bwl, type) \
233 type logic_in##bwl(unsigned long addr) \
235 type ret = (type)~0; \
237 if (addr < MMIO_UPPER_LIMIT) { \
238 ret = _in##bwl(addr); \
239 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
240 struct logic_pio_hwaddr *entry = find_io_range(addr); \
243 ret = entry->ops->in(entry->hostdata, \
244 addr, sizeof(type)); \
251 void logic_out##bwl(type value, unsigned long addr) \
253 if (addr < MMIO_UPPER_LIMIT) { \
254 _out##bwl(value, addr); \
255 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
256 struct logic_pio_hwaddr *entry = find_io_range(addr); \
259 entry->ops->out(entry->hostdata, \
260 addr, value, sizeof(type)); \
266 void logic_ins##bwl(unsigned long addr, void *buffer, \
267 unsigned int count) \
269 if (addr < MMIO_UPPER_LIMIT) { \
270 reads##bwl(PCI_IOBASE + addr, buffer, count); \
271 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
272 struct logic_pio_hwaddr *entry = find_io_range(addr); \
275 entry->ops->ins(entry->hostdata, \
276 addr, buffer, sizeof(type), count); \
283 void logic_outs##bwl(unsigned long addr, const void *buffer, \
284 unsigned int count) \
286 if (addr < MMIO_UPPER_LIMIT) { \
287 writes##bwl(PCI_IOBASE + addr, buffer, count); \
288 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
289 struct logic_pio_hwaddr *entry = find_io_range(addr); \
292 entry->ops->outs(entry->hostdata, \
293 addr, buffer, sizeof(type), count); \
299 BUILD_LOGIC_IO(b, u8)
300 EXPORT_SYMBOL(logic_inb);
301 EXPORT_SYMBOL(logic_insb);
302 EXPORT_SYMBOL(logic_outb);
303 EXPORT_SYMBOL(logic_outsb);
305 BUILD_LOGIC_IO(w, u16)
306 EXPORT_SYMBOL(logic_inw);
307 EXPORT_SYMBOL(logic_insw);
308 EXPORT_SYMBOL(logic_outw);
309 EXPORT_SYMBOL(logic_outsw);
311 BUILD_LOGIC_IO(l, u32)
312 EXPORT_SYMBOL(logic_inl);
313 EXPORT_SYMBOL(logic_insl);
314 EXPORT_SYMBOL(logic_outl);
315 EXPORT_SYMBOL(logic_outsl);
317 #endif /* CONFIG_INDIRECT_PIO && PCI_IOBASE */