Blame view

lib/logic_pio.c 8.39 KB
031e36018   Zhichang Yuan   lib: Add generic ...
1
2
3
4
5
  // SPDX-License-Identifier: GPL-2.0+
  /*
   * Copyright (C) 2017 HiSilicon Limited, All Rights Reserved.
   * Author: Gabriele Paoloni <gabriele.paoloni@huawei.com>
   * Author: Zhichang Yuan <yuanzhichang@hisilicon.com>
0376fa72a   John Garry   lib: logic_pio: E...
6
   * Author: John Garry <john.garry@huawei.com>
031e36018   Zhichang Yuan   lib: Add generic ...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   */
  
  #define pr_fmt(fmt)	"LOGIC PIO: " fmt
  
  #include <linux/of.h>
  #include <linux/io.h>
  #include <linux/logic_pio.h>
  #include <linux/mm.h>
  #include <linux/rculist.h>
  #include <linux/sizes.h>
  #include <linux/slab.h>
  
  /* The unique hardware address list */
  static LIST_HEAD(io_range_list);
  static DEFINE_MUTEX(io_range_mutex);
  
  /* Consider a kernel general helper for this */
  #define in_range(b, first, len)        ((b) >= (first) && (b) < (first) + (len))
  
  /**
   * logic_pio_register_range - register logical PIO range for a host
   * @new_range: pointer to the IO range to be registered.
   *
   * Returns 0 on success, the error code in case of failure.
   *
   * Register a new IO range node in the IO range list.
   */
  int logic_pio_register_range(struct logic_pio_hwaddr *new_range)
  {
  	struct logic_pio_hwaddr *range;
  	resource_size_t start;
  	resource_size_t end;
0a27142bd   John Garry   lib: logic_pio: A...
39
  	resource_size_t mmio_end = 0;
031e36018   Zhichang Yuan   lib: Add generic ...
40
41
  	resource_size_t iio_sz = MMIO_UPPER_LIMIT;
  	int ret = 0;
0376fa72a   John Garry   lib: logic_pio: E...
42
43
  	if (!new_range || !new_range->fwnode || !new_range->size ||
  	    (new_range->flags == LOGIC_PIO_INDIRECT && !new_range->ops))
031e36018   Zhichang Yuan   lib: Add generic ...
44
45
46
47
48
49
  		return -EINVAL;
  
  	start = new_range->hw_start;
  	end = new_range->hw_start + new_range->size;
  
  	mutex_lock(&io_range_mutex);
06709e81c   John Garry   lib: logic_pio: F...
50
  	list_for_each_entry(range, &io_range_list, list) {
031e36018   Zhichang Yuan   lib: Add generic ...
51
52
53
54
55
56
57
58
59
  		if (range->fwnode == new_range->fwnode) {
  			/* range already there */
  			goto end_register;
  		}
  		if (range->flags == LOGIC_PIO_CPU_MMIO &&
  		    new_range->flags == LOGIC_PIO_CPU_MMIO) {
  			/* for MMIO ranges we need to check for overlap */
  			if (start >= range->hw_start + range->size ||
  			    end < range->hw_start) {
0a27142bd   John Garry   lib: logic_pio: A...
60
  				mmio_end = range->io_start + range->size;
031e36018   Zhichang Yuan   lib: Add generic ...
61
62
63
64
65
66
67
68
69
70
71
72
  			} else {
  				ret = -EFAULT;
  				goto end_register;
  			}
  		} else if (range->flags == LOGIC_PIO_INDIRECT &&
  			   new_range->flags == LOGIC_PIO_INDIRECT) {
  			iio_sz += range->size;
  		}
  	}
  
  	/* range not registered yet, check for available space */
  	if (new_range->flags == LOGIC_PIO_CPU_MMIO) {
0a27142bd   John Garry   lib: logic_pio: A...
73
  		if (mmio_end + new_range->size - 1 > MMIO_UPPER_LIMIT) {
031e36018   Zhichang Yuan   lib: Add generic ...
74
  			/* if it's too big check if 64K space can be reserved */
0a27142bd   John Garry   lib: logic_pio: A...
75
  			if (mmio_end + SZ_64K - 1 > MMIO_UPPER_LIMIT) {
031e36018   Zhichang Yuan   lib: Add generic ...
76
77
78
79
80
81
82
  				ret = -E2BIG;
  				goto end_register;
  			}
  			new_range->size = SZ_64K;
  			pr_warn("Requested IO range too big, new size set to 64K
  ");
  		}
0a27142bd   John Garry   lib: logic_pio: A...
83
  		new_range->io_start = mmio_end;
031e36018   Zhichang Yuan   lib: Add generic ...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  	} else if (new_range->flags == LOGIC_PIO_INDIRECT) {
  		if (iio_sz + new_range->size - 1 > IO_SPACE_LIMIT) {
  			ret = -E2BIG;
  			goto end_register;
  		}
  		new_range->io_start = iio_sz;
  	} else {
  		/* invalid flag */
  		ret = -EINVAL;
  		goto end_register;
  	}
  
  	list_add_tail_rcu(&new_range->list, &io_range_list);
  
  end_register:
  	mutex_unlock(&io_range_mutex);
  	return ret;
  }
  
  /**
b884e2de2   John Garry   lib: logic_pio: A...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
   * logic_pio_unregister_range - unregister a logical PIO range for a host
   * @range: pointer to the IO range which has been already registered.
   *
   * Unregister a previously-registered IO range node.
   */
  void logic_pio_unregister_range(struct logic_pio_hwaddr *range)
  {
  	mutex_lock(&io_range_mutex);
  	list_del_rcu(&range->list);
  	mutex_unlock(&io_range_mutex);
  	synchronize_rcu();
  }
  
  /**
031e36018   Zhichang Yuan   lib: Add generic ...
118
119
120
121
122
123
124
125
126
   * find_io_range_by_fwnode - find logical PIO range for given FW node
   * @fwnode: FW node handle associated with logical PIO range
   *
   * Returns pointer to node on success, NULL otherwise.
   *
   * Traverse the io_range_list to find the registered node for @fwnode.
   */
  struct logic_pio_hwaddr *find_io_range_by_fwnode(struct fwnode_handle *fwnode)
  {
06709e81c   John Garry   lib: logic_pio: F...
127
  	struct logic_pio_hwaddr *range, *found_range = NULL;
031e36018   Zhichang Yuan   lib: Add generic ...
128

06709e81c   John Garry   lib: logic_pio: F...
129
  	rcu_read_lock();
031e36018   Zhichang Yuan   lib: Add generic ...
130
  	list_for_each_entry_rcu(range, &io_range_list, list) {
06709e81c   John Garry   lib: logic_pio: F...
131
132
133
134
  		if (range->fwnode == fwnode) {
  			found_range = range;
  			break;
  		}
031e36018   Zhichang Yuan   lib: Add generic ...
135
  	}
06709e81c   John Garry   lib: logic_pio: F...
136
137
138
  	rcu_read_unlock();
  
  	return found_range;
031e36018   Zhichang Yuan   lib: Add generic ...
139
140
141
142
143
  }
  
  /* Return a registered range given an input PIO token */
  static struct logic_pio_hwaddr *find_io_range(unsigned long pio)
  {
06709e81c   John Garry   lib: logic_pio: F...
144
  	struct logic_pio_hwaddr *range, *found_range = NULL;
031e36018   Zhichang Yuan   lib: Add generic ...
145

06709e81c   John Garry   lib: logic_pio: F...
146
  	rcu_read_lock();
031e36018   Zhichang Yuan   lib: Add generic ...
147
  	list_for_each_entry_rcu(range, &io_range_list, list) {
06709e81c   John Garry   lib: logic_pio: F...
148
149
150
151
  		if (in_range(pio, range->io_start, range->size)) {
  			found_range = range;
  			break;
  		}
031e36018   Zhichang Yuan   lib: Add generic ...
152
  	}
06709e81c   John Garry   lib: logic_pio: F...
153
154
155
156
157
158
159
  	rcu_read_unlock();
  
  	if (!found_range)
  		pr_err("PIO entry token 0x%lx invalid
  ", pio);
  
  	return found_range;
031e36018   Zhichang Yuan   lib: Add generic ...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  }
  
  /**
   * logic_pio_to_hwaddr - translate logical PIO to HW address
   * @pio: logical PIO value
   *
   * Returns HW address if valid, ~0 otherwise.
   *
   * Translate the input logical PIO to the corresponding hardware address.
   * The input PIO should be unique in the whole logical PIO space.
   */
  resource_size_t logic_pio_to_hwaddr(unsigned long pio)
  {
  	struct logic_pio_hwaddr *range;
  
  	range = find_io_range(pio);
  	if (range)
  		return range->hw_start + pio - range->io_start;
  
  	return (resource_size_t)~0;
  }
  
  /**
   * logic_pio_trans_hwaddr - translate HW address to logical PIO
   * @fwnode: FW node reference for the host
   * @addr: Host-relative HW address
   * @size: size to translate
   *
   * Returns Logical PIO value if successful, ~0UL otherwise
   */
  unsigned long logic_pio_trans_hwaddr(struct fwnode_handle *fwnode,
  				     resource_size_t addr, resource_size_t size)
  {
  	struct logic_pio_hwaddr *range;
  
  	range = find_io_range_by_fwnode(fwnode);
  	if (!range || range->flags == LOGIC_PIO_CPU_MMIO) {
  		pr_err("IO range not found or invalid
  ");
  		return ~0UL;
  	}
  	if (range->size < size) {
  		pr_err("resource size %pa cannot fit in IO range size %pa
  ",
  		       &size, &range->size);
  		return ~0UL;
  	}
  	return addr - range->hw_start + range->io_start;
  }
  
  unsigned long logic_pio_trans_cpuaddr(resource_size_t addr)
  {
  	struct logic_pio_hwaddr *range;
06709e81c   John Garry   lib: logic_pio: F...
213
  	rcu_read_lock();
031e36018   Zhichang Yuan   lib: Add generic ...
214
215
216
  	list_for_each_entry_rcu(range, &io_range_list, list) {
  		if (range->flags != LOGIC_PIO_CPU_MMIO)
  			continue;
06709e81c   John Garry   lib: logic_pio: F...
217
218
219
220
221
222
223
224
  		if (in_range(addr, range->hw_start, range->size)) {
  			unsigned long cpuaddr;
  
  			cpuaddr = addr - range->hw_start + range->io_start;
  
  			rcu_read_unlock();
  			return cpuaddr;
  		}
031e36018   Zhichang Yuan   lib: Add generic ...
225
  	}
06709e81c   John Garry   lib: logic_pio: F...
226
227
228
229
  	rcu_read_unlock();
  
  	pr_err("addr %pa not registered in io_range_list
  ", &addr);
031e36018   Zhichang Yuan   lib: Add generic ...
230
231
232
233
  	return ~0UL;
  }
  
  #if defined(CONFIG_INDIRECT_PIO) && defined(PCI_IOBASE)
26c4c6ce8   John Garry   logic_pio: Improv...
234
235
  #define BUILD_LOGIC_IO(bwl, type)					\
  type logic_in##bwl(unsigned long addr)					\
031e36018   Zhichang Yuan   lib: Add generic ...
236
237
238
239
  {									\
  	type ret = (type)~0;						\
  									\
  	if (addr < MMIO_UPPER_LIMIT) {					\
4acaa93ef   John Garry   logic_pio: Use _i...
240
  		ret = _in##bwl(addr);					\
031e36018   Zhichang Yuan   lib: Add generic ...
241
242
243
  	} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) { \
  		struct logic_pio_hwaddr *entry = find_io_range(addr);	\
  									\
0376fa72a   John Garry   lib: logic_pio: E...
244
  		if (entry)						\
031e36018   Zhichang Yuan   lib: Add generic ...
245
246
247
248
249
250
251
252
  			ret = entry->ops->in(entry->hostdata,		\
  					addr, sizeof(type));		\
  		else							\
  			WARN_ON_ONCE(1);				\
  	}								\
  	return ret;							\
  }									\
  									\
26c4c6ce8   John Garry   logic_pio: Improv...
253
  void logic_out##bwl(type value, unsigned long addr)			\
031e36018   Zhichang Yuan   lib: Add generic ...
254
255
  {									\
  	if (addr < MMIO_UPPER_LIMIT) {					\
4acaa93ef   John Garry   logic_pio: Use _i...
256
  		_out##bwl(value, addr);				\
031e36018   Zhichang Yuan   lib: Add generic ...
257
258
259
  	} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) {	\
  		struct logic_pio_hwaddr *entry = find_io_range(addr);	\
  									\
0376fa72a   John Garry   lib: logic_pio: E...
260
  		if (entry)						\
031e36018   Zhichang Yuan   lib: Add generic ...
261
262
263
264
265
266
267
  			entry->ops->out(entry->hostdata,		\
  					addr, value, sizeof(type));	\
  		else							\
  			WARN_ON_ONCE(1);				\
  	}								\
  }									\
  									\
26c4c6ce8   John Garry   logic_pio: Improv...
268
269
  void logic_ins##bwl(unsigned long addr, void *buffer,			\
  		    unsigned int count)					\
031e36018   Zhichang Yuan   lib: Add generic ...
270
271
  {									\
  	if (addr < MMIO_UPPER_LIMIT) {					\
26c4c6ce8   John Garry   logic_pio: Improv...
272
  		reads##bwl(PCI_IOBASE + addr, buffer, count);		\
031e36018   Zhichang Yuan   lib: Add generic ...
273
274
275
  	} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) {	\
  		struct logic_pio_hwaddr *entry = find_io_range(addr);	\
  									\
0376fa72a   John Garry   lib: logic_pio: E...
276
  		if (entry)						\
031e36018   Zhichang Yuan   lib: Add generic ...
277
278
279
280
281
282
283
284
  			entry->ops->ins(entry->hostdata,		\
  				addr, buffer, sizeof(type), count);	\
  		else							\
  			WARN_ON_ONCE(1);				\
  	}								\
  									\
  }									\
  									\
26c4c6ce8   John Garry   logic_pio: Improv...
285
286
  void logic_outs##bwl(unsigned long addr, const void *buffer,		\
  		     unsigned int count)				\
031e36018   Zhichang Yuan   lib: Add generic ...
287
288
  {									\
  	if (addr < MMIO_UPPER_LIMIT) {					\
26c4c6ce8   John Garry   logic_pio: Improv...
289
  		writes##bwl(PCI_IOBASE + addr, buffer, count);		\
031e36018   Zhichang Yuan   lib: Add generic ...
290
291
292
  	} else if (addr >= MMIO_UPPER_LIMIT && addr < IO_SPACE_LIMIT) {	\
  		struct logic_pio_hwaddr *entry = find_io_range(addr);	\
  									\
0376fa72a   John Garry   lib: logic_pio: E...
293
  		if (entry)						\
031e36018   Zhichang Yuan   lib: Add generic ...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  			entry->ops->outs(entry->hostdata,		\
  				addr, buffer, sizeof(type), count);	\
  		else							\
  			WARN_ON_ONCE(1);				\
  	}								\
  }
  
  BUILD_LOGIC_IO(b, u8)
  EXPORT_SYMBOL(logic_inb);
  EXPORT_SYMBOL(logic_insb);
  EXPORT_SYMBOL(logic_outb);
  EXPORT_SYMBOL(logic_outsb);
  
  BUILD_LOGIC_IO(w, u16)
  EXPORT_SYMBOL(logic_inw);
  EXPORT_SYMBOL(logic_insw);
  EXPORT_SYMBOL(logic_outw);
  EXPORT_SYMBOL(logic_outsw);
  
  BUILD_LOGIC_IO(l, u32)
  EXPORT_SYMBOL(logic_inl);
  EXPORT_SYMBOL(logic_insl);
  EXPORT_SYMBOL(logic_outl);
  EXPORT_SYMBOL(logic_outsl);
  
  #endif /* CONFIG_INDIRECT_PIO && PCI_IOBASE */