]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - lib/logic_pio.c
Fix misannotated out-of-line _copy_to_user()
[mirror_ubuntu-bionic-kernel.git] / lib / logic_pio.c
CommitLineData
342c2a0e
ZY
1// 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
9#define pr_fmt(fmt) "LOGIC PIO: " fmt
10
11#include <linux/of.h>
12#include <linux/io.h>
13#include <linux/logic_pio.h>
14#include <linux/mm.h>
15#include <linux/rculist.h>
16#include <linux/sizes.h>
17#include <linux/slab.h>
18
19/* The unique hardware address list. */
20static LIST_HEAD(io_range_list);
21static DEFINE_MUTEX(io_range_mutex);
22
23/**
24 * logic_pio_register_range - register logical PIO range for a host
25 * @new_range: pointer to the io range to be registered.
26 *
27 * returns 0 on success, the error code in case of failure
28 *
29 * Register a new io range node in the io range list.
30 */
31int logic_pio_register_range(struct logic_pio_hwaddr *new_range)
32{
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;
38 int ret = 0;
39
40 if (!new_range || !new_range->fwnode || !new_range->size)
41 return -EINVAL;
42
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 */
47 ret = -EFAULT;
48 goto end_register;
49 }
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;
56 else {
57 ret = -EFAULT;
58 goto end_register;
59 }
60 } else if (range->flags == PIO_INDIRECT &&
61 new_range->flags == PIO_INDIRECT) {
62 allocated_iio_size += range->size;
63 }
64 }
65
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 >
69 MMIO_UPPER_LIMIT) {
70 /* if it's too big check if 64K space can be reserved */
71 if (allocated_mmio_size + SZ_64K - 1 >
72 MMIO_UPPER_LIMIT) {
73 ret = -E2BIG;
74 goto end_register;
75 }
76 new_range->size = SZ_64K;
77 pr_warn("Requested IO range too big, new size set to 64K\n");
78 }
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 >
82 IO_SPACE_LIMIT) {
83 ret = -E2BIG;
84 goto end_register;
85 }
86 new_range->io_start = allocated_iio_size;
87 } else {
88 /* invalid flag */
89 ret = -EINVAL;
90 goto end_register;
91 }
92
93 list_add_tail_rcu(&new_range->list, &io_range_list);
94
95end_register:
96 mutex_unlock(&io_range_mutex);
97 return ret;
98}
99
100/**
101 * find_io_range_by_fwnode - find logical PIO range for given FW node
102 * @fwnode: FW node handle associated with logical PIO range
103 *
104 * Returns pointer to node on success, NULL otherwise
105 *
106 * Traverse the io_range_list to find the registered node whose device node
107 * and/or physical IO address match to.
108 */
109struct logic_pio_hwaddr *find_io_range_by_fwnode(struct fwnode_handle *fwnode)
110{
111 struct logic_pio_hwaddr *range;
112
113 list_for_each_entry_rcu(range, &io_range_list, list) {
114 if (range->fwnode == fwnode)
115 return range;
116 }
117 return NULL;
118}
119
120/* Return a registered range given an input PIO token */
121static struct logic_pio_hwaddr *find_io_range(unsigned long pio)
122{
123 struct logic_pio_hwaddr *range;
124
125 list_for_each_entry_rcu(range, &io_range_list, list) {
126 if (pio >= range->io_start &&
127 pio < range->io_start + range->size)
128 return range;
129 }
130 pr_err("PIO entry token invalid\n");
131 return NULL;
132}
133
134/**
135 * logic_pio_to_hwaddr - translate logical PIO to HW address
136 * @pio: logical PIO value
137 *
138 * Returns HW address if valid, -1 otherwise
139 *
140 * Translate the input logical pio to the corresponding hardware address.
141 * The input pio should be unique in the whole logical PIO space.
142 */
143resource_size_t logic_pio_to_hwaddr(unsigned long pio)
144{
145 struct logic_pio_hwaddr *range;
146 resource_size_t hwaddr = -1;
147
148 range = find_io_range(pio);
149 if (range)
150 hwaddr = range->hw_start + pio - range->io_start;
151
152 return hwaddr;
153}
154
155/**
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
160 *
161 * Returns Logical PIO value if successful, -1 otherwise
162 */
163unsigned long
164logic_pio_trans_hwaddr(struct fwnode_handle *fwnode, resource_size_t addr,
165 resource_size_t size)
166{
167 struct logic_pio_hwaddr *range;
168
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");
172 return -1;
173 }
174 if (range->size < size) {
175 pr_err("resource size %pa cannot fit in IO range size %pa\n",
176 &size, &range->size);
177 return -1;
178 }
179 return addr - range->hw_start + range->io_start;
180}
181
182unsigned long
183logic_pio_trans_cpuaddr(resource_size_t addr)
184{
185 struct logic_pio_hwaddr *range;
186
187 list_for_each_entry_rcu(range, &io_range_list, list) {
188 if (range->flags != PIO_CPU_MMIO)
189 continue;
190 if (addr >= range->hw_start &&
191 addr < range->hw_start + range->size)
192 return addr - range->hw_start +
193 range->io_start;
194 }
195 pr_err("addr not registered in io_range_list\n");
196 return -1;
197}
198
199#if defined(CONFIG_INDIRECT_PIO) && defined(PCI_IOBASE)
200#define BUILD_LOGIC_IO(bw, type) \
201type logic_in##bw(unsigned long addr) \
202{ \
203 type ret = -1; \
204 \
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); \
209 \
210 if (entry && entry->ops) \
211 ret = entry->ops->in(entry->hostdata, \
212 addr, sizeof(type)); \
213 else \
214 WARN_ON_ONCE(1); \
215 } \
216 return ret; \
217} \
218 \
219void logic_out##bw(type value, unsigned long addr) \
220{ \
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); \
225 \
226 if (entry && entry->ops) \
227 entry->ops->out(entry->hostdata, \
228 addr, value, sizeof(type)); \
229 else \
230 WARN_ON_ONCE(1); \
231 } \
232} \
233 \
234void logic_ins##bw(unsigned long addr, void *buffer, \
235 unsigned int count) \
236{ \
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); \
241 \
242 if (entry && entry->ops) \
243 entry->ops->ins(entry->hostdata, \
244 addr, buffer, sizeof(type), count); \
245 else \
246 WARN_ON_ONCE(1); \
247 } \
248 \
249} \
250 \
251void logic_outs##bw(unsigned long addr, const void *buffer, \
252 unsigned int count) \
253{ \
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); \
258 \
259 if (entry && entry->ops) \
260 entry->ops->outs(entry->hostdata, \
261 addr, buffer, sizeof(type), count); \
262 else \
263 WARN_ON_ONCE(1); \
264 } \
265}
266
267BUILD_LOGIC_IO(b, u8)
268EXPORT_SYMBOL(logic_inb);
269EXPORT_SYMBOL(logic_insb);
270EXPORT_SYMBOL(logic_outb);
271EXPORT_SYMBOL(logic_outsb);
272
273BUILD_LOGIC_IO(w, u16)
274EXPORT_SYMBOL(logic_inw);
275EXPORT_SYMBOL(logic_insw);
276EXPORT_SYMBOL(logic_outw);
277EXPORT_SYMBOL(logic_outsw);
278
279BUILD_LOGIC_IO(l, u32)
280EXPORT_SYMBOL(logic_inl);
281EXPORT_SYMBOL(logic_insl);
282EXPORT_SYMBOL(logic_outl);
283EXPORT_SYMBOL(logic_outsl);
284
285#endif /* CONFIG_INDIRECT_PIO && PCI_IOBASE */