1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __OF_RESERVED_MEM_H
3 #define __OF_RESERVED_MEM_H
4 
5 #include <linux/device.h>
6 
7 struct of_phandle_args;
8 struct reserved_mem_ops;
9 
10 struct reserved_mem {
11 	const char			*name;
12 	unsigned long			fdt_node;
13 	unsigned long			phandle;
14 	const struct reserved_mem_ops	*ops;
15 	phys_addr_t			base;
16 	phys_addr_t			size;
17 	void				*priv;
18 };
19 
20 struct reserved_mem_ops {
21 	int	(*device_init)(struct reserved_mem *rmem,
22 			       struct device *dev);
23 	void	(*device_release)(struct reserved_mem *rmem,
24 				  struct device *dev);
25 };
26 
27 typedef int (*reservedmem_of_init_fn)(struct reserved_mem *rmem);
28 
29 #define RESERVEDMEM_OF_DECLARE(name, compat, init)			\
30 	_OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn)
31 
32 #ifdef CONFIG_OF_RESERVED_MEM
33 
34 int of_reserved_mem_device_init_by_idx(struct device *dev,
35 				       struct device_node *np, int idx);
36 void of_reserved_mem_device_release(struct device *dev);
37 
38 void fdt_init_reserved_mem(void);
39 void fdt_reserved_mem_save_node(unsigned long node, const char *uname,
40 			       phys_addr_t base, phys_addr_t size);
41 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
42 #else
of_reserved_mem_device_init_by_idx(struct device * dev,struct device_node * np,int idx)43 static inline int of_reserved_mem_device_init_by_idx(struct device *dev,
44 					struct device_node *np, int idx)
45 {
46 	return -ENOSYS;
47 }
of_reserved_mem_device_release(struct device * pdev)48 static inline void of_reserved_mem_device_release(struct device *pdev) { }
49 
fdt_init_reserved_mem(void)50 static inline void fdt_init_reserved_mem(void) { }
fdt_reserved_mem_save_node(unsigned long node,const char * uname,phys_addr_t base,phys_addr_t size)51 static inline void fdt_reserved_mem_save_node(unsigned long node,
52 		const char *uname, phys_addr_t base, phys_addr_t size) { }
of_reserved_mem_lookup(struct device_node * np)53 static inline struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
54 {
55 	return NULL;
56 }
57 #endif
58 
59 /**
60  * of_reserved_mem_device_init() - assign reserved memory region to given device
61  * @dev:	Pointer to the device to configure
62  *
63  * This function assigns respective DMA-mapping operations based on the first
64  * reserved memory region specified by 'memory-region' property in device tree
65  * node of the given device.
66  *
67  * Returns error code or zero on success.
68  */
of_reserved_mem_device_init(struct device * dev)69 static inline int of_reserved_mem_device_init(struct device *dev)
70 {
71 	return of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
72 }
73 
74 #endif /* __OF_RESERVED_MEM_H */
75