blob: 774bb02fff10aa8bfa9fb98c3c8d59813e629818 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
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 */
7
8#define pr_fmt(fmt) "LOGIC PIO: " fmt
9
10#include <linux/of.h>
11#include <linux/io.h>
12#include <linux/logic_pio.h>
13#include <linux/mm.h>
14#include <linux/rculist.h>
15#include <linux/sizes.h>
16#include <linux/slab.h>
17
18/* The unique hardware address list */
19static LIST_HEAD(io_range_list);
20static DEFINE_MUTEX(io_range_mutex);
21
22/* Consider a kernel general helper for this */
23#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
24
25/**
26 * logic_pio_register_range - register logical PIO range for a host
27 * @new_range: pointer to the IO range to be registered.
28 *
29 * Returns 0 on success, the error code in case of failure.
Olivier Deprez0e641232021-09-23 10:07:05 +020030 * If the range already exists, -EEXIST will be returned, which should be
31 * considered a success.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032 *
33 * Register a new IO range node in the IO range list.
34 */
35int logic_pio_register_range(struct logic_pio_hwaddr *new_range)
36{
37 struct logic_pio_hwaddr *range;
38 resource_size_t start;
39 resource_size_t end;
David Brazdil0f672f62019-12-10 10:32:29 +000040 resource_size_t mmio_end = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041 resource_size_t iio_sz = MMIO_UPPER_LIMIT;
42 int ret = 0;
43
44 if (!new_range || !new_range->fwnode || !new_range->size)
45 return -EINVAL;
46
47 start = new_range->hw_start;
48 end = new_range->hw_start + new_range->size;
49
50 mutex_lock(&io_range_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +000051 list_for_each_entry(range, &io_range_list, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052 if (range->fwnode == new_range->fwnode) {
53 /* range already there */
Olivier Deprez0e641232021-09-23 10:07:05 +020054 ret = -EEXIST;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 goto end_register;
56 }
57 if (range->flags == LOGIC_PIO_CPU_MMIO &&
58 new_range->flags == LOGIC_PIO_CPU_MMIO) {
59 /* for MMIO ranges we need to check for overlap */
60 if (start >= range->hw_start + range->size ||
61 end < range->hw_start) {
David Brazdil0f672f62019-12-10 10:32:29 +000062 mmio_end = range->io_start + range->size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 } else {
64 ret = -EFAULT;
65 goto end_register;
66 }
67 } else if (range->flags == LOGIC_PIO_INDIRECT &&
68 new_range->flags == LOGIC_PIO_INDIRECT) {
69 iio_sz += range->size;
70 }
71 }
72
73 /* range not registered yet, check for available space */
74 if (new_range->flags == LOGIC_PIO_CPU_MMIO) {
David Brazdil0f672f62019-12-10 10:32:29 +000075 if (mmio_end + new_range->size - 1 > MMIO_UPPER_LIMIT) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 /* if it's too big check if 64K space can be reserved */
David Brazdil0f672f62019-12-10 10:32:29 +000077 if (mmio_end + SZ_64K - 1 > MMIO_UPPER_LIMIT) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 ret = -E2BIG;
79 goto end_register;
80 }
81 new_range->size = SZ_64K;
82 pr_warn("Requested IO range too big, new size set to 64K\n");
83 }
David Brazdil0f672f62019-12-10 10:32:29 +000084 new_range->io_start = mmio_end;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085 } else if (new_range->flags == LOGIC_PIO_INDIRECT) {
86 if (iio_sz + new_range->size - 1 > IO_SPACE_LIMIT) {
87 ret = -E2BIG;
88 goto end_register;
89 }
90 new_range->io_start = iio_sz;
91 } else {
92 /* invalid flag */
93 ret = -EINVAL;
94 goto end_register;
95 }
96
97 list_add_tail_rcu(&new_range->list, &io_range_list);
98
99end_register:
100 mutex_unlock(&io_range_mutex);
101 return ret;
102}
103
104/**
David Brazdil0f672f62019-12-10 10:32:29 +0000105 * logic_pio_unregister_range - unregister a logical PIO range for a host
106 * @range: pointer to the IO range which has been already registered.
107 *
108 * Unregister a previously-registered IO range node.
109 */
110void logic_pio_unregister_range(struct logic_pio_hwaddr *range)
111{
112 mutex_lock(&io_range_mutex);
113 list_del_rcu(&range->list);
114 mutex_unlock(&io_range_mutex);
115 synchronize_rcu();
116}
117
118/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119 * find_io_range_by_fwnode - find logical PIO range for given FW node
120 * @fwnode: FW node handle associated with logical PIO range
121 *
122 * Returns pointer to node on success, NULL otherwise.
123 *
124 * Traverse the io_range_list to find the registered node for @fwnode.
125 */
126struct logic_pio_hwaddr *find_io_range_by_fwnode(struct fwnode_handle *fwnode)
127{
David Brazdil0f672f62019-12-10 10:32:29 +0000128 struct logic_pio_hwaddr *range, *found_range = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129
David Brazdil0f672f62019-12-10 10:32:29 +0000130 rcu_read_lock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131 list_for_each_entry_rcu(range, &io_range_list, list) {
David Brazdil0f672f62019-12-10 10:32:29 +0000132 if (range->fwnode == fwnode) {
133 found_range = range;
134 break;
135 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 }
David Brazdil0f672f62019-12-10 10:32:29 +0000137 rcu_read_unlock();
138
139 return found_range;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140}
141
142/* Return a registered range given an input PIO token */
143static struct logic_pio_hwaddr *find_io_range(unsigned long pio)
144{
David Brazdil0f672f62019-12-10 10:32:29 +0000145 struct logic_pio_hwaddr *range, *found_range = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146
David Brazdil0f672f62019-12-10 10:32:29 +0000147 rcu_read_lock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 list_for_each_entry_rcu(range, &io_range_list, list) {
David Brazdil0f672f62019-12-10 10:32:29 +0000149 if (in_range(pio, range->io_start, range->size)) {
150 found_range = range;
151 break;
152 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 }
David Brazdil0f672f62019-12-10 10:32:29 +0000154 rcu_read_unlock();
155
156 if (!found_range)
157 pr_err("PIO entry token 0x%lx invalid\n", pio);
158
159 return found_range;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160}
161
162/**
163 * logic_pio_to_hwaddr - translate logical PIO to HW address
164 * @pio: logical PIO value
165 *
166 * Returns HW address if valid, ~0 otherwise.
167 *
168 * Translate the input logical PIO to the corresponding hardware address.
169 * The input PIO should be unique in the whole logical PIO space.
170 */
171resource_size_t logic_pio_to_hwaddr(unsigned long pio)
172{
173 struct logic_pio_hwaddr *range;
174
175 range = find_io_range(pio);
176 if (range)
177 return range->hw_start + pio - range->io_start;
178
179 return (resource_size_t)~0;
180}
181
182/**
183 * logic_pio_trans_hwaddr - translate HW address to logical PIO
184 * @fwnode: FW node reference for the host
185 * @addr: Host-relative HW address
186 * @size: size to translate
187 *
188 * Returns Logical PIO value if successful, ~0UL otherwise
189 */
190unsigned long logic_pio_trans_hwaddr(struct fwnode_handle *fwnode,
191 resource_size_t addr, resource_size_t size)
192{
193 struct logic_pio_hwaddr *range;
194
195 range = find_io_range_by_fwnode(fwnode);
196 if (!range || range->flags == LOGIC_PIO_CPU_MMIO) {
197 pr_err("IO range not found or invalid\n");
198 return ~0UL;
199 }
200 if (range->size < size) {
201 pr_err("resource size %pa cannot fit in IO range size %pa\n",
202 &size, &range->size);
203 return ~0UL;
204 }
205 return addr - range->hw_start + range->io_start;
206}
207
208unsigned long logic_pio_trans_cpuaddr(resource_size_t addr)
209{
210 struct logic_pio_hwaddr *range;
211
David Brazdil0f672f62019-12-10 10:32:29 +0000212 rcu_read_lock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213 list_for_each_entry_rcu(range, &io_range_list, list) {
214 if (range->flags != LOGIC_PIO_CPU_MMIO)
215 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000216 if (in_range(addr, range->hw_start, range->size)) {
217 unsigned long cpuaddr;
218
219 cpuaddr = addr - range->hw_start + range->io_start;
220
221 rcu_read_unlock();
222 return cpuaddr;
223 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224 }
David Brazdil0f672f62019-12-10 10:32:29 +0000225 rcu_read_unlock();
226
227 pr_err("addr %pa not registered in io_range_list\n", &addr);
228
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 return ~0UL;
230}
231
232#if defined(CONFIG_INDIRECT_PIO) && defined(PCI_IOBASE)
233#define BUILD_LOGIC_IO(bw, type) \
234type logic_in##bw(unsigned long addr) \
235{ \
236 type ret = (type)~0; \
237 \
238 if (addr < MMIO_UPPER_LIMIT) { \
239 ret = read##bw(PCI_IOBASE + addr); \
240 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
241 struct logic_pio_hwaddr *entry = find_io_range(addr); \
242 \
243 if (entry && entry->ops) \
244 ret = entry->ops->in(entry->hostdata, \
245 addr, sizeof(type)); \
246 else \
247 WARN_ON_ONCE(1); \
248 } \
249 return ret; \
250} \
251 \
252void logic_out##bw(type value, unsigned long addr) \
253{ \
254 if (addr < MMIO_UPPER_LIMIT) { \
255 write##bw(value, PCI_IOBASE + addr); \
256 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
257 struct logic_pio_hwaddr *entry = find_io_range(addr); \
258 \
259 if (entry && entry->ops) \
260 entry->ops->out(entry->hostdata, \
261 addr, value, sizeof(type)); \
262 else \
263 WARN_ON_ONCE(1); \
264 } \
265} \
266 \
267void logic_ins##bw(unsigned long addr, void *buffer, \
268 unsigned int count) \
269{ \
270 if (addr < MMIO_UPPER_LIMIT) { \
271 reads##bw(PCI_IOBASE + addr, buffer, count); \
272 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
273 struct logic_pio_hwaddr *entry = find_io_range(addr); \
274 \
275 if (entry && entry->ops) \
276 entry->ops->ins(entry->hostdata, \
277 addr, buffer, sizeof(type), count); \
278 else \
279 WARN_ON_ONCE(1); \
280 } \
281 \
282} \
283 \
284void logic_outs##bw(unsigned long addr, const void *buffer, \
285 unsigned int count) \
286{ \
287 if (addr < MMIO_UPPER_LIMIT) { \
288 writes##bw(PCI_IOBASE + addr, buffer, count); \
289 } else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
290 struct logic_pio_hwaddr *entry = find_io_range(addr); \
291 \
292 if (entry && entry->ops) \
293 entry->ops->outs(entry->hostdata, \
294 addr, buffer, sizeof(type), count); \
295 else \
296 WARN_ON_ONCE(1); \
297 } \
298}
299
300BUILD_LOGIC_IO(b, u8)
301EXPORT_SYMBOL(logic_inb);
302EXPORT_SYMBOL(logic_insb);
303EXPORT_SYMBOL(logic_outb);
304EXPORT_SYMBOL(logic_outsb);
305
306BUILD_LOGIC_IO(w, u16)
307EXPORT_SYMBOL(logic_inw);
308EXPORT_SYMBOL(logic_insw);
309EXPORT_SYMBOL(logic_outw);
310EXPORT_SYMBOL(logic_outsw);
311
312BUILD_LOGIC_IO(l, u32)
313EXPORT_SYMBOL(logic_inl);
314EXPORT_SYMBOL(logic_insl);
315EXPORT_SYMBOL(logic_outl);
316EXPORT_SYMBOL(logic_outsl);
317
318#endif /* CONFIG_INDIRECT_PIO && PCI_IOBASE */