xref: /linux/include/linux/ioport.h (revision c34e9ab9a612ee8b18273398ef75c207b01f516d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ioport.h	Definitions of routines for detecting, reserving and
4  *		allocating system resources.
5  *
6  * Authors:	Linus Torvalds
7  */
8 
9 #ifndef _LINUX_IOPORT_H
10 #define _LINUX_IOPORT_H
11 
12 #ifndef __ASSEMBLY__
13 #include <linux/bits.h>
14 #include <linux/compiler.h>
15 #include <linux/minmax.h>
16 #include <linux/types.h>
17 /*
18  * Resources are tree-like, allowing
19  * nesting etc..
20  */
21 struct resource {
22 	resource_size_t start;
23 	resource_size_t end;
24 	const char *name;
25 	unsigned long flags;
26 	unsigned long desc;
27 	struct resource *parent, *sibling, *child;
28 };
29 
30 /*
31  * IO resources have these defined flags.
32  *
33  * PCI devices expose these flags to userspace in the "resource" sysfs file,
34  * so don't move them.
35  */
36 #define IORESOURCE_BITS		0x000000ff	/* Bus-specific bits */
37 
38 #define IORESOURCE_TYPE_BITS	0x00001f00	/* Resource type */
39 #define IORESOURCE_IO		0x00000100	/* PCI/ISA I/O ports */
40 #define IORESOURCE_MEM		0x00000200
41 #define IORESOURCE_REG		0x00000300	/* Register offsets */
42 #define IORESOURCE_IRQ		0x00000400
43 #define IORESOURCE_DMA		0x00000800
44 #define IORESOURCE_BUS		0x00001000
45 
46 #define IORESOURCE_PREFETCH	0x00002000	/* No side effects */
47 #define IORESOURCE_READONLY	0x00004000
48 #define IORESOURCE_CACHEABLE	0x00008000
49 #define IORESOURCE_RANGELENGTH	0x00010000
50 #define IORESOURCE_SHADOWABLE	0x00020000
51 
52 #define IORESOURCE_SIZEALIGN	0x00040000	/* size indicates alignment */
53 #define IORESOURCE_STARTALIGN	0x00080000	/* start field is alignment */
54 
55 #define IORESOURCE_MEM_64	0x00100000
56 #define IORESOURCE_WINDOW	0x00200000	/* forwarded by bridge */
57 #define IORESOURCE_MUXED	0x00400000	/* Resource is software muxed */
58 
59 #define IORESOURCE_EXT_TYPE_BITS 0x01000000	/* Resource extended types */
60 #define IORESOURCE_SYSRAM	0x01000000	/* System RAM (modifier) */
61 
62 /* IORESOURCE_SYSRAM specific bits. */
63 #define IORESOURCE_SYSRAM_DRIVER_MANAGED	0x02000000 /* Always detected via a driver. */
64 #define IORESOURCE_SYSRAM_MERGEABLE		0x04000000 /* Resource can be merged. */
65 
66 #define IORESOURCE_EXCLUSIVE	0x08000000	/* Userland may not map this resource */
67 
68 #define IORESOURCE_DISABLED	0x10000000
69 #define IORESOURCE_UNSET	0x20000000	/* No address assigned yet */
70 #define IORESOURCE_AUTO		0x40000000
71 #define IORESOURCE_BUSY		0x80000000	/* Driver has marked this resource busy */
72 
73 /* I/O resource extended types */
74 #define IORESOURCE_SYSTEM_RAM		(IORESOURCE_MEM|IORESOURCE_SYSRAM)
75 
76 /* PnP IRQ specific bits (IORESOURCE_BITS) */
77 #define IORESOURCE_IRQ_HIGHEDGE		(1<<0)
78 #define IORESOURCE_IRQ_LOWEDGE		(1<<1)
79 #define IORESOURCE_IRQ_HIGHLEVEL	(1<<2)
80 #define IORESOURCE_IRQ_LOWLEVEL		(1<<3)
81 #define IORESOURCE_IRQ_SHAREABLE	(1<<4)
82 #define IORESOURCE_IRQ_OPTIONAL		(1<<5)
83 #define IORESOURCE_IRQ_WAKECAPABLE	(1<<6)
84 
85 /* PnP DMA specific bits (IORESOURCE_BITS) */
86 #define IORESOURCE_DMA_TYPE_MASK	(3<<0)
87 #define IORESOURCE_DMA_8BIT		(0<<0)
88 #define IORESOURCE_DMA_8AND16BIT	(1<<0)
89 #define IORESOURCE_DMA_16BIT		(2<<0)
90 
91 #define IORESOURCE_DMA_MASTER		(1<<2)
92 #define IORESOURCE_DMA_BYTE		(1<<3)
93 #define IORESOURCE_DMA_WORD		(1<<4)
94 
95 #define IORESOURCE_DMA_SPEED_MASK	(3<<6)
96 #define IORESOURCE_DMA_COMPATIBLE	(0<<6)
97 #define IORESOURCE_DMA_TYPEA		(1<<6)
98 #define IORESOURCE_DMA_TYPEB		(2<<6)
99 #define IORESOURCE_DMA_TYPEF		(3<<6)
100 
101 /* PnP memory I/O specific bits (IORESOURCE_BITS) */
102 #define IORESOURCE_MEM_WRITEABLE	(1<<0)	/* dup: IORESOURCE_READONLY */
103 #define IORESOURCE_MEM_CACHEABLE	(1<<1)	/* dup: IORESOURCE_CACHEABLE */
104 #define IORESOURCE_MEM_RANGELENGTH	(1<<2)	/* dup: IORESOURCE_RANGELENGTH */
105 #define IORESOURCE_MEM_TYPE_MASK	(3<<3)
106 #define IORESOURCE_MEM_8BIT		(0<<3)
107 #define IORESOURCE_MEM_16BIT		(1<<3)
108 #define IORESOURCE_MEM_8AND16BIT	(2<<3)
109 #define IORESOURCE_MEM_32BIT		(3<<3)
110 #define IORESOURCE_MEM_SHADOWABLE	(1<<5)	/* dup: IORESOURCE_SHADOWABLE */
111 #define IORESOURCE_MEM_EXPANSIONROM	(1<<6)
112 #define IORESOURCE_MEM_NONPOSTED	(1<<7)
113 
114 /* PnP I/O specific bits (IORESOURCE_BITS) */
115 #define IORESOURCE_IO_16BIT_ADDR	(1<<0)
116 #define IORESOURCE_IO_FIXED		(1<<1)
117 #define IORESOURCE_IO_SPARSE		(1<<2)
118 
119 /* PCI ROM control bits (IORESOURCE_BITS) */
120 #define IORESOURCE_ROM_ENABLE		(1<<0)	/* ROM is enabled, same as PCI_ROM_ADDRESS_ENABLE */
121 #define IORESOURCE_ROM_SHADOW		(1<<1)	/* Use RAM image, not ROM BAR */
122 
123 /* PCI control bits.  Shares IORESOURCE_BITS with above PCI ROM.  */
124 #define IORESOURCE_PCI_FIXED		(1<<4)	/* Do not move resource */
125 #define IORESOURCE_PCI_EA_BEI		(1<<5)	/* BAR Equivalent Indicator */
126 
127 /*
128  * I/O Resource Descriptors
129  *
130  * Descriptors are used by walk_iomem_res_desc() and region_intersects()
131  * for searching a specific resource range in the iomem table.  Assign
132  * a new descriptor when a resource range supports the search interfaces.
133  * Otherwise, resource.desc must be set to IORES_DESC_NONE (0).
134  */
135 enum {
136 	IORES_DESC_NONE				= 0,
137 	IORES_DESC_CRASH_KERNEL			= 1,
138 	IORES_DESC_ACPI_TABLES			= 2,
139 	IORES_DESC_ACPI_NV_STORAGE		= 3,
140 	IORES_DESC_PERSISTENT_MEMORY		= 4,
141 	IORES_DESC_PERSISTENT_MEMORY_LEGACY	= 5,
142 	IORES_DESC_DEVICE_PRIVATE_MEMORY	= 6,
143 	IORES_DESC_RESERVED			= 7,
144 	IORES_DESC_SOFT_RESERVED		= 8,
145 	IORES_DESC_CXL				= 9,
146 };
147 
148 /*
149  * Flags controlling ioremap() behavior.
150  */
151 enum {
152 	IORES_MAP_SYSTEM_RAM		= BIT(0),
153 	IORES_MAP_ENCRYPTED		= BIT(1),
154 };
155 
156 /* helpers to define resources */
157 #define DEFINE_RES_NAMED(_start, _size, _name, _flags)			\
158 (struct resource) {							\
159 		.start = (_start),					\
160 		.end = (_start) + (_size) - 1,				\
161 		.name = (_name),					\
162 		.flags = (_flags),					\
163 		.desc = IORES_DESC_NONE,				\
164 	}
165 
166 #define DEFINE_RES_IO_NAMED(_start, _size, _name)			\
167 	DEFINE_RES_NAMED((_start), (_size), (_name), IORESOURCE_IO)
168 #define DEFINE_RES_IO(_start, _size)					\
169 	DEFINE_RES_IO_NAMED((_start), (_size), NULL)
170 
171 #define DEFINE_RES_MEM_NAMED(_start, _size, _name)			\
172 	DEFINE_RES_NAMED((_start), (_size), (_name), IORESOURCE_MEM)
173 #define DEFINE_RES_MEM(_start, _size)					\
174 	DEFINE_RES_MEM_NAMED((_start), (_size), NULL)
175 
176 #define DEFINE_RES_REG_NAMED(_start, _size, _name)			\
177 	DEFINE_RES_NAMED((_start), (_size), (_name), IORESOURCE_REG)
178 #define DEFINE_RES_REG(_start, _size)					\
179 	DEFINE_RES_REG_NAMED((_start), (_size), NULL)
180 
181 #define DEFINE_RES_IRQ_NAMED(_irq, _name)				\
182 	DEFINE_RES_NAMED((_irq), 1, (_name), IORESOURCE_IRQ)
183 #define DEFINE_RES_IRQ(_irq)						\
184 	DEFINE_RES_IRQ_NAMED((_irq), NULL)
185 
186 #define DEFINE_RES_DMA_NAMED(_dma, _name)				\
187 	DEFINE_RES_NAMED((_dma), 1, (_name), IORESOURCE_DMA)
188 #define DEFINE_RES_DMA(_dma)						\
189 	DEFINE_RES_DMA_NAMED((_dma), NULL)
190 
191 /**
192  * typedef resource_alignf - Resource alignment callback
193  * @data:	Private data used by the callback
194  * @res:	Resource candidate range (an empty resource space)
195  * @size:	The minimum size of the empty space
196  * @align:	Alignment from the constraints
197  *
198  * Callback allows calculating resource placement and alignment beyond min,
199  * max, and align fields in the struct resource_constraint.
200  *
201  * Return: Start address for the resource.
202  */
203 typedef resource_size_t (*resource_alignf)(void *data,
204 					   const struct resource *res,
205 					   resource_size_t size,
206 					   resource_size_t align);
207 
208 /**
209  * struct resource_constraint - constraints to be met while searching empty
210  *				resource space
211  * @min:		The minimum address for the memory range
212  * @max:		The maximum address for the memory range
213  * @align:		Alignment for the start address of the empty space
214  * @alignf:		Additional alignment constraints callback
215  * @alignf_data:	Data provided for @alignf callback
216  *
217  * Contains the range and alignment constraints that have to be met during
218  * find_resource_space(). @alignf can be NULL indicating no alignment beyond
219  * @align is necessary.
220  */
221 struct resource_constraint {
222 	resource_size_t min, max, align;
223 	resource_alignf alignf;
224 	void *alignf_data;
225 };
226 
227 /* PC/ISA/whatever - the normal PC address spaces: IO and memory */
228 extern struct resource ioport_resource;
229 extern struct resource iomem_resource;
230 
231 extern struct resource *request_resource_conflict(struct resource *root, struct resource *new);
232 extern int request_resource(struct resource *root, struct resource *new);
233 extern int release_resource(struct resource *new);
234 void release_child_resources(struct resource *new);
235 extern void reserve_region_with_split(struct resource *root,
236 			     resource_size_t start, resource_size_t end,
237 			     const char *name);
238 extern struct resource *insert_resource_conflict(struct resource *parent, struct resource *new);
239 extern int insert_resource(struct resource *parent, struct resource *new);
240 extern void insert_resource_expand_to_fit(struct resource *root, struct resource *new);
241 extern int remove_resource(struct resource *old);
242 extern void arch_remove_reservations(struct resource *avail);
243 extern int allocate_resource(struct resource *root, struct resource *new,
244 			     resource_size_t size, resource_size_t min,
245 			     resource_size_t max, resource_size_t align,
246 			     resource_alignf alignf,
247 			     void *alignf_data);
248 struct resource *lookup_resource(struct resource *root, resource_size_t start);
249 int adjust_resource(struct resource *res, resource_size_t start,
250 		    resource_size_t size);
251 resource_size_t resource_alignment(struct resource *res);
252 
253 /**
254  * resource_set_size - Calculate resource end address from size and start
255  * @res: Resource descriptor
256  * @size: Size of the resource
257  *
258  * Calculate the end address for @res based on @size.
259  *
260  * Note: The start address of @res must be set when calling this function.
261  * Prefer resource_set_range() if setting both the start address and @size.
262  */
263 static inline void resource_set_size(struct resource *res, resource_size_t size)
264 {
265 	res->end = res->start + size - 1;
266 }
267 
268 /**
269  * resource_set_range - Set resource start and end addresses
270  * @res: Resource descriptor
271  * @start: Start address for the resource
272  * @size: Size of the resource
273  *
274  * Set @res start address and calculate the end address based on @size.
275  */
276 static inline void resource_set_range(struct resource *res,
277 				      resource_size_t start,
278 				      resource_size_t size)
279 {
280 	res->start = start;
281 	resource_set_size(res, size);
282 }
283 
284 static inline resource_size_t resource_size(const struct resource *res)
285 {
286 	return res->end - res->start + 1;
287 }
288 static inline unsigned long resource_type(const struct resource *res)
289 {
290 	return res->flags & IORESOURCE_TYPE_BITS;
291 }
292 static inline unsigned long resource_ext_type(const struct resource *res)
293 {
294 	return res->flags & IORESOURCE_EXT_TYPE_BITS;
295 }
296 /* True iff r1 completely contains r2 */
297 static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
298 {
299 	if (resource_type(r1) != resource_type(r2))
300 		return false;
301 	if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
302 		return false;
303 	return r1->start <= r2->start && r1->end >= r2->end;
304 }
305 
306 /* True if any part of r1 overlaps r2 */
307 static inline bool resource_overlaps(const struct resource *r1, const struct resource *r2)
308 {
309        return r1->start <= r2->end && r1->end >= r2->start;
310 }
311 
312 static inline bool resource_intersection(const struct resource *r1, const struct resource *r2,
313 					 struct resource *r)
314 {
315 	if (!resource_overlaps(r1, r2))
316 		return false;
317 	r->start = max(r1->start, r2->start);
318 	r->end = min(r1->end, r2->end);
319 	return true;
320 }
321 
322 static inline bool resource_union(const struct resource *r1, const struct resource *r2,
323 				  struct resource *r)
324 {
325 	if (!resource_overlaps(r1, r2))
326 		return false;
327 	r->start = min(r1->start, r2->start);
328 	r->end = max(r1->end, r2->end);
329 	return true;
330 }
331 
332 int find_resource_space(struct resource *root, struct resource *new,
333 			resource_size_t size, struct resource_constraint *constraint);
334 
335 /* Convenience shorthand with allocation */
336 #define request_region(start,n,name)		__request_region(&ioport_resource, (start), (n), (name), 0)
337 #define request_muxed_region(start,n,name)	__request_region(&ioport_resource, (start), (n), (name), IORESOURCE_MUXED)
338 #define __request_mem_region(start,n,name, excl) __request_region(&iomem_resource, (start), (n), (name), excl)
339 #define request_mem_region(start,n,name) __request_region(&iomem_resource, (start), (n), (name), 0)
340 #define request_mem_region_muxed(start, n, name) \
341 	__request_region(&iomem_resource, (start), (n), (name), IORESOURCE_MUXED)
342 #define request_mem_region_exclusive(start,n,name) \
343 	__request_region(&iomem_resource, (start), (n), (name), IORESOURCE_EXCLUSIVE)
344 #define rename_region(region, newname) do { (region)->name = (newname); } while (0)
345 
346 extern struct resource * __request_region(struct resource *,
347 					resource_size_t start,
348 					resource_size_t n,
349 					const char *name, int flags);
350 
351 /* Compatibility cruft */
352 #define release_region(start,n)	__release_region(&ioport_resource, (start), (n))
353 #define release_mem_region(start,n)	__release_region(&iomem_resource, (start), (n))
354 
355 extern void __release_region(struct resource *, resource_size_t,
356 				resource_size_t);
357 #ifdef CONFIG_MEMORY_HOTREMOVE
358 extern void release_mem_region_adjustable(resource_size_t, resource_size_t);
359 #endif
360 #ifdef CONFIG_MEMORY_HOTPLUG
361 extern void merge_system_ram_resource(struct resource *res);
362 #endif
363 
364 /* Wrappers for managed devices */
365 struct device;
366 
367 extern int devm_request_resource(struct device *dev, struct resource *root,
368 				 struct resource *new);
369 extern void devm_release_resource(struct device *dev, struct resource *new);
370 
371 #define devm_request_region(dev,start,n,name) \
372 	__devm_request_region(dev, &ioport_resource, (start), (n), (name))
373 #define devm_request_mem_region(dev,start,n,name) \
374 	__devm_request_region(dev, &iomem_resource, (start), (n), (name))
375 
376 extern struct resource * __devm_request_region(struct device *dev,
377 				struct resource *parent, resource_size_t start,
378 				resource_size_t n, const char *name);
379 
380 #define devm_release_region(dev, start, n) \
381 	__devm_release_region(dev, &ioport_resource, (start), (n))
382 #define devm_release_mem_region(dev, start, n) \
383 	__devm_release_region(dev, &iomem_resource, (start), (n))
384 
385 extern void __devm_release_region(struct device *dev, struct resource *parent,
386 				  resource_size_t start, resource_size_t n);
387 extern int iomem_map_sanity_check(resource_size_t addr, unsigned long size);
388 extern bool iomem_is_exclusive(u64 addr);
389 extern bool resource_is_exclusive(struct resource *resource, u64 addr,
390 				  resource_size_t size);
391 
392 extern int
393 walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
394 		void *arg, int (*func)(unsigned long, unsigned long, void *));
395 extern int
396 walk_mem_res(u64 start, u64 end, void *arg,
397 	     int (*func)(struct resource *, void *));
398 extern int
399 walk_system_ram_res(u64 start, u64 end, void *arg,
400 		    int (*func)(struct resource *, void *));
401 extern int
402 walk_system_ram_res_rev(u64 start, u64 end, void *arg,
403 			int (*func)(struct resource *, void *));
404 extern int
405 walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end,
406 		    void *arg, int (*func)(struct resource *, void *));
407 
408 struct resource *devm_request_free_mem_region(struct device *dev,
409 		struct resource *base, unsigned long size);
410 struct resource *request_free_mem_region(struct resource *base,
411 		unsigned long size, const char *name);
412 struct resource *alloc_free_mem_region(struct resource *base,
413 		unsigned long size, unsigned long align, const char *name);
414 
415 static inline void irqresource_disabled(struct resource *res, u32 irq)
416 {
417 	res->start = irq;
418 	res->end = irq;
419 	res->flags |= IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
420 }
421 
422 extern struct address_space *iomem_get_mapping(void);
423 
424 #endif /* __ASSEMBLY__ */
425 #endif	/* _LINUX_IOPORT_H */
426