Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3) #define pr_fmt(fmt)  "irq: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/irqdesc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/irqdomain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) static LIST_HEAD(irq_domain_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) static DEFINE_MUTEX(irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) static struct irq_domain *irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) static void irq_domain_check_hierarchy(struct irq_domain *domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) struct irqchip_fwid {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	struct fwnode_handle	fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	unsigned int		type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	char			*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	phys_addr_t		*pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static void debugfs_add_domain_dir(struct irq_domain *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static void debugfs_remove_domain_dir(struct irq_domain *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) const struct fwnode_operations irqchip_fwnode_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  *                           identifying an irq domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * @type:	Type of irqchip_fwnode. See linux/irqdomain.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * @id:		Optional user provided id if name != NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * @name:	Optional user provided domain name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * @pa:		Optional user-provided physical address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * Allocate a struct irqchip_fwid, and return a poiner to the embedded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * fwnode_handle (or NULL on failure).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * solely to transport name information to irqdomain creation code. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * node is not stored. For other types the pointer is kept in the irq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * domain struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 						const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 						phys_addr_t *pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct irqchip_fwid *fwid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	char *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	case IRQCHIP_FWNODE_NAMED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		n = kasprintf(GFP_KERNEL, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	case IRQCHIP_FWNODE_NAMED_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	if (!fwid || !n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		kfree(fwid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 		kfree(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	fwid->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	fwid->name = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	fwid->pa = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	return &fwid->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct irqchip_fwid *fwid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	if (WARN_ON(!is_fwnode_irqchip(fwnode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	kfree(fwid->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	kfree(fwid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118)  * __irq_domain_add() - Allocate a new irq_domain data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119)  * @fwnode: firmware node for the interrupt controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120)  * @size: Size of linear map; 0 for radix mapping only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * @hwirq_max: Maximum number of interrupts supported by controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  *              direct mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  * @ops: domain callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * @host_data: Controller private data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  * Allocates and initializes an irq_domain structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * Returns pointer to IRQ domain, or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 				    irq_hw_number_t hwirq_max, int direct_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 				    const struct irq_domain_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 				    void *host_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	struct irqchip_fwid *fwid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct irq_domain *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	static atomic_t unknown_domains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			      GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (!domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (is_fwnode_irqchip(fwnode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		switch (fwid->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 		case IRQCHIP_FWNODE_NAMED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		case IRQCHIP_FWNODE_NAMED_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 			domain->fwnode = fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 			domain->name = kstrdup(fwid->name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			if (!domain->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 				kfree(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 			domain->fwnode = fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			domain->name = fwid->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	} else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		   is_software_node(fwnode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		 * fwnode paths contain '/', which debugfs is legitimately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		 * unhappy about. Replace them with ':', which does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		 * the trick and is not as offensive as '\'...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			kfree(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		strreplace(name, '/', ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		domain->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		domain->fwnode = fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (!domain->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		if (fwnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			pr_err("Invalid fwnode type for irqdomain\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 					 atomic_inc_return(&unknown_domains));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		if (!domain->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			kfree(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	fwnode_handle_get(fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	fwnode_dev_initialized(fwnode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	/* Fill structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	mutex_init(&domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	domain->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	domain->host_data = host_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	domain->hwirq_max = hwirq_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	domain->revmap_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	domain->revmap_direct_max_irq = direct_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	irq_domain_check_hierarchy(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	debugfs_add_domain_dir(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	list_add(&domain->link, &irq_domain_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	pr_debug("Added domain %s\n", domain->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	return domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) EXPORT_SYMBOL_GPL(__irq_domain_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  * irq_domain_remove() - Remove an irq domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223)  * @domain: domain to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  * This routine is used to remove an irq domain. The caller must ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  * that all mappings within the domain have been disposed of prior to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * use, depending on the revmap type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) void irq_domain_remove(struct irq_domain *domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	debugfs_remove_domain_dir(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	WARN_ON(!radix_tree_empty(&domain->revmap_tree));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	list_del(&domain->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * If the going away domain is the default one, reset it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (unlikely(irq_default_domain == domain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		irq_set_default_host(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	pr_debug("Removed domain %s\n", domain->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	fwnode_dev_initialized(domain->fwnode, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	fwnode_handle_put(domain->fwnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		kfree(domain->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	kfree(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) EXPORT_SYMBOL_GPL(irq_domain_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) void irq_domain_update_bus_token(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 				 enum irq_domain_bus_token bus_token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	if (domain->bus_token == bus_token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	domain->bus_token = bus_token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	debugfs_remove_domain_dir(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		kfree(domain->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	domain->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	debugfs_add_domain_dir(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290)  * @of_node: pointer to interrupt controller's device tree node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291)  * @size: total number of irqs in mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * @first_irq: first number of irq block assigned to the domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  *	pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  *	pre-map all of the irqs in the domain to virqs starting at first_irq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * @ops: domain callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * @host_data: Controller private data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  * Allocates an irq_domain, and optionally if first_irq is positive then also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299)  * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301)  * This is intended to implement the expected behaviour for most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302)  * interrupt controllers. If device tree is used, then first_irq will be 0 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303)  * irqs get mapped dynamically on the fly. However, if the controller requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  * static virq assignments (non-DT boot) then it will set that up correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 					 unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 					 unsigned int first_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 					 const struct irq_domain_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 					 void *host_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	struct irq_domain *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	if (!domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	if (first_irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			/* attempt to allocated irq_descs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			int rc = irq_alloc_descs(first_irq, first_irq, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 						 of_node_to_nid(of_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 					first_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		irq_domain_associate_many(domain, first_irq, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	return domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) EXPORT_SYMBOL_GPL(irq_domain_add_simple);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * @of_node: pointer to interrupt controller's device tree node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  * @size: total number of irqs in legacy mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * @first_irq: first number of irq block assigned to the domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  * @first_hwirq: first hwirq number to use for the translation. Should normally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  *               be '0', but a positive integer can be used if the effective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  *               hwirqs numbering does not begin at zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  * @ops: map/unmap domain callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  * @host_data: Controller private data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  * Note: the map() callback will be called before this function returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  * for all legacy interrupts except 0 (which is always the invalid irq for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  * a legacy controller).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 					 unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 					 unsigned int first_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 					 irq_hw_number_t first_hwirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 					 const struct irq_domain_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 					 void *host_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	struct irq_domain *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				  first_hwirq + size, 0, ops, host_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		irq_domain_associate_many(domain, first_irq, first_hwirq, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	return domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368)  * irq_find_matching_fwspec() - Locates a domain for a given fwspec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * @fwspec: FW specifier for an interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  * @bus_token: domain-specific data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 					    enum irq_domain_bus_token bus_token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	struct irq_domain *h, *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct fwnode_handle *fwnode = fwspec->fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	/* We might want to match the legacy controller last since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	 * it might potentially be set to match all interrupts in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	 * the absence of a device node. This isn't a problem so far
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	 * yet though...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	 * values must generate an exact match for the domain to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	 * selected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	list_for_each_entry(h, &irq_domain_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		if (h->ops->select && fwspec->param_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 			rc = h->ops->select(h, fwspec, bus_token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		else if (h->ops->match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			rc = h->ops->match(h, to_of_node(fwnode), bus_token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			      ((bus_token == DOMAIN_BUS_ANY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			       (h->bus_token == bus_token)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			found = h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  * IRQ remapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  * Return: false if any MSI irq domain does not support IRQ remapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  * true otherwise (including if there is no MSI irq domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) bool irq_domain_check_msi_remap(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	struct irq_domain *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	list_for_each_entry(h, &irq_domain_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		if (irq_domain_is_msi(h) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		    !irq_domain_hierarchical_is_msi_remap(h)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * irq_set_default_host() - Set a "default" irq domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * @domain: default domain pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  * For convenience, it's possible to set a "default" domain that will be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  * whenever NULL is passed to irq_create_mapping(). It makes life easier for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  * platforms that want to manipulate a few hard coded interrupt numbers that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  * aren't properly represented in the device-tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) void irq_set_default_host(struct irq_domain *domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	pr_debug("Default domain set to @0x%p\n", domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	irq_default_domain = domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) EXPORT_SYMBOL_GPL(irq_set_default_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  * irq_get_default_host() - Retrieve the "default" irq domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * Returns: the default domain, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * Modern code should never use this. This should only be used on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * systems that cannot implement a firmware->fwnode mapping (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  * both DT and ACPI provide).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) struct irq_domain *irq_get_default_host(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	return irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static void irq_domain_clear_mapping(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 				     irq_hw_number_t hwirq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	if (hwirq < domain->revmap_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		domain->linear_revmap[hwirq] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		mutex_lock(&domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		radix_tree_delete(&domain->revmap_tree, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		mutex_unlock(&domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) static void irq_domain_set_mapping(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 				   irq_hw_number_t hwirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 				   struct irq_data *irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (hwirq < domain->revmap_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		domain->linear_revmap[hwirq] = irq_data->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		mutex_lock(&domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		mutex_unlock(&domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct irq_data *irq_data = irq_get_irq_data(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	irq_hw_number_t hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	if (WARN(!irq_data || irq_data->domain != domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		 "virq%i doesn't exist; cannot disassociate\n", irq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	hwirq = irq_data->hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	irq_set_status_flags(irq, IRQ_NOREQUEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	/* remove chip and handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	irq_set_chip_and_handler(irq, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	/* Make sure it's completed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	synchronize_irq(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	/* Tell the PIC about it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (domain->ops->unmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		domain->ops->unmap(domain, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	irq_data->domain = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	irq_data->hwirq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	domain->mapcount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	/* Clear reverse map for this hwirq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	irq_domain_clear_mapping(domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			 irq_hw_number_t hwirq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	struct irq_data *irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	if (WARN(hwirq >= domain->hwirq_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (WARN(!irq_data, "error: virq%i is not allocated", virq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	irq_data->hwirq = hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	irq_data->domain = domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	if (domain->ops->map) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		ret = domain->ops->map(domain, virq, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			 * If map() returns -EPERM, this interrupt is protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			 * by the firmware or some other service and shall not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 			 * be mapped. Don't bother telling the user about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			if (ret != -EPERM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 				pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 				       domain->name, hwirq, virq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 			irq_data->domain = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 			irq_data->hwirq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 			mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		/* If not already assigned, give the domain the chip's name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		if (!domain->name && irq_data->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			domain->name = irq_data->chip->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	domain->mapcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	irq_domain_set_mapping(domain, hwirq, irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	irq_clear_status_flags(virq, IRQ_NOREQUEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) EXPORT_SYMBOL_GPL(irq_domain_associate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			       irq_hw_number_t hwirq_base, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	struct device_node *of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	of_node = irq_domain_get_of_node(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		irq_domain_associate(domain, irq_base + i, hwirq_base + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) EXPORT_SYMBOL_GPL(irq_domain_associate_many);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588)  * irq_create_direct_mapping() - Allocate an irq for direct mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589)  * @domain: domain to allocate the irq for or NULL for default domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591)  * This routine is used for irq controllers which can choose the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592)  * interrupt numbers they generate. In such a case it's simplest to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * the linux irq as the hardware interrupt number. It still uses the linear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * or radix tree to store the mapping, but the irq controller can optimize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  * the revmap path by using the hwirq directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) unsigned int irq_create_direct_mapping(struct irq_domain *domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	struct device_node *of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	unsigned int virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	if (domain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		domain = irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	of_node = irq_domain_get_of_node(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	if (!virq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		pr_debug("create_direct virq allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (virq >= domain->revmap_direct_max_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		pr_err("ERROR: no free irqs available below %i maximum\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			domain->revmap_direct_max_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		irq_free_desc(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	pr_debug("create_direct obtained virq %d\n", virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	if (irq_domain_associate(domain, virq, virq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		irq_free_desc(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  * @domain: domain owning this hardware interrupt or NULL for default domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * @hwirq: hardware irq number in that domain space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * @affinity: irq affinity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  * Only one mapping per hardware interrupt is permitted. Returns a linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635)  * irq number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  * If the sense/trigger is to be specified, set_irq_type() should be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  * on the number returned from that call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 				       irq_hw_number_t hwirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 				       const struct irq_affinity_desc *affinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	struct device_node *of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	int virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	/* Look for default domain if nececssary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	if (domain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		domain = irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	if (domain == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	pr_debug("-> using domain @%p\n", domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	of_node = irq_domain_get_of_node(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	/* Check if mapping already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	virq = irq_find_mapping(domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (virq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		pr_debug("-> existing mapping on virq %d\n", virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	/* Allocate a virtual interrupt number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				      affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (virq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		pr_debug("-> virq allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (irq_domain_associate(domain, virq, hwirq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		irq_free_desc(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		hwirq, of_node_full_name(of_node), virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687)  * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688)  * @domain: domain owning the interrupt range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  * @irq_base: beginning of linux IRQ range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  * @hwirq_base: beginning of hardware IRQ range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691)  * @count: Number of interrupts to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693)  * This routine is used for allocating and mapping a range of hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694)  * irqs to linux irqs where the linux irq numbers are at pre-defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695)  * locations. For use by controllers that already have static mappings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696)  * to insert in to the domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698)  * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699)  * domain insertion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701)  * 0 is returned upon success, while any failure to establish a static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702)  * mapping is treated as an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 			       irq_hw_number_t hwirq_base, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	struct device_node *of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	of_node = irq_domain_get_of_node(domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	ret = irq_alloc_descs(irq_base, irq_base, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			      of_node_to_nid(of_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (unlikely(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	irq_domain_associate_many(domain, irq_base, hwirq_base, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) static int irq_domain_translate(struct irq_domain *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 				struct irq_fwspec *fwspec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 				irq_hw_number_t *hwirq, unsigned int *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (d->ops->translate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		return d->ops->translate(d, fwspec, hwirq, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	if (d->ops->xlate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return d->ops->xlate(d, to_of_node(fwspec->fwnode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 				     fwspec->param, fwspec->param_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 				     hwirq, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	/* If domain has no translation, then we assume interrupt line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	*hwirq = fwspec->param[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 				      unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 				      struct irq_fwspec *fwspec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	fwspec->fwnode = np ? &np->fwnode : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	fwspec->param_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		fwspec->param[i] = args[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	struct irq_domain *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	struct irq_data *irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	irq_hw_number_t hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	unsigned int type = IRQ_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	int virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (fwspec->fwnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		if (!domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		domain = irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (!domain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		pr_warn("no irq domain found for %s !\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			of_node_full_name(to_of_node(fwspec->fwnode)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	if (irq_domain_translate(domain, fwspec, &hwirq, &type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 * WARN if the irqchip returns a type with bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	 * outside the sense mask set and clear these bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		type &= IRQ_TYPE_SENSE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	 * If we've already configured this interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	 * don't do it again, or hell will break loose.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	virq = irq_find_mapping(domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (virq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		 * If the trigger type is not specified or matches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		 * current trigger type then we are done so return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		 * interrupt number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		 * If the trigger type has not been set yet, then set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		 * it now and return the interrupt number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			if (!irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			irqd_set_trigger_type(irq_data, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 			hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (irq_domain_is_hierarchy(domain)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		if (virq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		/* Create mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		virq = irq_create_mapping(domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		if (!virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (!irq_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		if (irq_domain_is_hierarchy(domain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			irq_domain_free_irqs(virq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			irq_dispose_mapping(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	/* Store trigger type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	irqd_set_trigger_type(irq_data, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	struct irq_fwspec fwspec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 				  irq_data->args_count, &fwspec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	return irq_create_fwspec_mapping(&fwspec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) EXPORT_SYMBOL_GPL(irq_create_of_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855)  * irq_dispose_mapping() - Unmap an interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856)  * @virq: linux irq number of the interrupt to unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) void irq_dispose_mapping(unsigned int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	struct irq_data *irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	struct irq_domain *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (!virq || !irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	domain = irq_data->domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (WARN_ON(domain == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	if (irq_domain_is_hierarchy(domain)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		irq_domain_free_irqs(virq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		irq_domain_disassociate(domain, virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		irq_free_desc(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) EXPORT_SYMBOL_GPL(irq_dispose_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  * irq_find_mapping() - Find a linux irq from a hw irq number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  * @domain: domain owning this hardware interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  * @hwirq: hardware irq number in that domain space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) unsigned int irq_find_mapping(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			      irq_hw_number_t hwirq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct irq_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	/* Look for default domain if nececssary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (domain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		domain = irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (domain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (hwirq < domain->revmap_direct_max_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		data = irq_domain_get_irq_data(domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		if (data && data->hwirq == hwirq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			return hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	/* Check if the hwirq is in the linear revmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (hwirq < domain->revmap_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		return domain->linear_revmap[hwirq];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	data = radix_tree_lookup(&domain->revmap_tree, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	return data ? data->irq : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) EXPORT_SYMBOL_GPL(irq_find_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913)  * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * Device Tree IRQ specifier translation function which works with one cell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  * bindings where the cell value maps directly to the hwirq number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			     const u32 *intspec, unsigned int intsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			     unsigned long *out_hwirq, unsigned int *out_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (WARN_ON(intsize < 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	*out_hwirq = intspec[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	*out_type = IRQ_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * Device Tree IRQ specifier translation function which works with two cell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * bindings where the cell values map directly to the hwirq number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  * and linux irq flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			const u32 *intspec, unsigned int intsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			irq_hw_number_t *out_hwirq, unsigned int *out_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	struct irq_fwspec fwspec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949)  * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951)  * Device Tree IRQ specifier translation function which works with either one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952)  * or two cell bindings where the cell values map directly to the hwirq number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953)  * and linux irq flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955)  * Note: don't use this function unless your interrupt controller explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956)  * supports both one and two cell bindings.  For the majority of controllers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957)  * the _onecell() or _twocell() variants above should be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) int irq_domain_xlate_onetwocell(struct irq_domain *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 				struct device_node *ctrlr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				const u32 *intspec, unsigned int intsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 				unsigned long *out_hwirq, unsigned int *out_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	if (WARN_ON(intsize < 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	*out_hwirq = intspec[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	if (intsize > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		*out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		*out_type = IRQ_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) const struct irq_domain_ops irq_domain_simple_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	.xlate = irq_domain_xlate_onetwocell,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981)  * irq_domain_translate_onecell() - Generic translate for direct one cell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982)  * bindings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) int irq_domain_translate_onecell(struct irq_domain *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 				 struct irq_fwspec *fwspec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 				 unsigned long *out_hwirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 				 unsigned int *out_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	if (WARN_ON(fwspec->param_count < 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	*out_hwirq = fwspec->param[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	*out_type = IRQ_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * irq_domain_translate_twocell() - Generic translate for direct two cell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * bindings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * Device Tree IRQ specifier translation function which works with two cell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  * bindings where the cell values map directly to the hwirq number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * and linux irq flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) int irq_domain_translate_twocell(struct irq_domain *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 				 struct irq_fwspec *fwspec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 				 unsigned long *out_hwirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 				 unsigned int *out_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (WARN_ON(fwspec->param_count < 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	*out_hwirq = fwspec->param[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	*out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			   int node, const struct irq_affinity_desc *affinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	unsigned int hint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	if (virq >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 					 affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		hint = hwirq % nr_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		if (hint == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			hint++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 					 affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		if (virq <= 0 && hint > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 						 affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)  * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)  * @irq_data:	The pointer to irq_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) void irq_domain_reset_irq_data(struct irq_data *irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	irq_data->hwirq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	irq_data->chip = &no_irq_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	irq_data->chip_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)  * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)  * @parent:	Parent irq domain to associate with the new domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)  * @flags:	Irq domain flags associated to the domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)  * @size:	Size of the domain. See below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)  * @fwnode:	Optional fwnode of the interrupt controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)  * @ops:	Pointer to the interrupt domain callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)  * @host_data:	Controller private data pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)  * If @size is 0 a tree domain is created, otherwise a linear domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)  * If successful the parent is associated to the new domain and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)  * domain flags are set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  * Returns pointer to IRQ domain, or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 					    unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 					    unsigned int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 					    struct fwnode_handle *fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 					    const struct irq_domain_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 					    void *host_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	struct irq_domain *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		domain = irq_domain_create_linear(fwnode, size, ops, host_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		domain = irq_domain_create_tree(fwnode, ops, host_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	if (domain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		domain->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		domain->flags |= flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	return domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) static void irq_domain_insert_irq(int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	struct irq_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		struct irq_domain *domain = data->domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		domain->mapcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		irq_domain_set_mapping(domain, data->hwirq, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		/* If not already assigned, give the domain the chip's name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		if (!domain->name && data->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			domain->name = data->chip->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	irq_clear_status_flags(virq, IRQ_NOREQUEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) static void irq_domain_remove_irq(int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	struct irq_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	irq_set_status_flags(virq, IRQ_NOREQUEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	irq_set_chip_and_handler(virq, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	synchronize_irq(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		struct irq_domain *domain = data->domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		irq_hw_number_t hwirq = data->hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		domain->mapcount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		irq_domain_clear_mapping(domain, hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 						   struct irq_data *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	struct irq_data *irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 				irq_data_get_node(child));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (irq_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		child->parent_data = irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		irq_data->irq = child->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		irq_data->common = child->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		irq_data->domain = domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	return irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	struct irq_data *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	while (irq_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		tmp = irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		irq_data = irq_data->parent_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct irq_data *irq_data, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	for (i = 0; i < nr_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		irq_data = irq_get_irq_data(virq + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		tmp = irq_data->parent_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		irq_data->parent_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		irq_data->domain = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		__irq_domain_free_hierarchy(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)  * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)  * @domain:	IRQ domain from which the hierarchy is to be disconnected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)  * @virq:	IRQ number where the hierarchy is to be trimmed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)  * Marks the @virq level belonging to @domain as disconnected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)  * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)  * to @domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * Its only use is to be able to trim levels of hierarchy that do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * have any real meaning for this interrupt, and that the driver marks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  * as such from its .alloc() callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 				    unsigned int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	struct irq_data *irqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	irqd = irq_domain_get_irq_data(domain, virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	if (!irqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	irqd->chip = ERR_PTR(-ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) static int irq_domain_trim_hierarchy(unsigned int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	struct irq_data *tail, *irqd, *irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	tail = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	/* The first entry must have a valid irqchip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	if (!irq_data->chip || IS_ERR(irq_data->chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	 * Validate that the irq_data chain is sane in the presence of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	 * a hierarchy trimming marker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		/* Can't have a valid irqchip after a trim marker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		if (irqd->chip && tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		/* Can't have an empty irqchip before a trim marker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		if (!irqd->chip && !tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		if (IS_ERR(irqd->chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			/* Only -ENOTCONN is a valid trim marker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			if (PTR_ERR(irqd->chip) != -ENOTCONN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			tail = irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	/* No trim marker, nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (!tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	pr_info("IRQ%d: trimming hierarchy from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		virq, tail->parent_data->domain->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	/* Sever the inner part of the hierarchy...  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	irqd = tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	tail = tail->parent_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	irqd->parent_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	__irq_domain_free_hierarchy(tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static int irq_domain_alloc_irq_data(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 				     unsigned int virq, unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	struct irq_data *irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	struct irq_domain *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	/* The outermost irq_data is embedded in struct irq_desc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	for (i = 0; i < nr_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		irq_data = irq_get_irq_data(virq + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		irq_data->domain = domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		for (parent = domain->parent; parent; parent = parent->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			irq_data = irq_domain_insert_irq_data(parent, irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			if (!irq_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 				irq_domain_free_irq_data(virq, i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * @domain:	domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * @virq:	IRQ number to get irq_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 					 unsigned int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	struct irq_data *irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	for (irq_data = irq_get_irq_data(virq); irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	     irq_data = irq_data->parent_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		if (irq_data->domain == domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 			return irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)  * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291)  * @domain:	Interrupt domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)  * @virq:	IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)  * @hwirq:	The hwirq number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)  * @chip:	The associated interrupt chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)  * @chip_data:	The associated chip data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 				  irq_hw_number_t hwirq, struct irq_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 				  void *chip_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	if (!irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	irq_data->hwirq = hwirq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	irq_data->chip = chip ? chip : &no_irq_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	irq_data->chip_data = chip_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)  * irq_domain_set_info - Set the complete data for a @virq in @domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)  * @domain:		Interrupt domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)  * @virq:		IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)  * @hwirq:		The hardware interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)  * @chip:		The associated interrupt chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)  * @chip_data:		The associated interrupt chip data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)  * @handler:		The interrupt flow handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)  * @handler_data:	The interrupt flow handler data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)  * @handler_name:	The interrupt handler name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			 irq_hw_number_t hwirq, struct irq_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			 void *chip_data, irq_flow_handler_t handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			 void *handler_data, const char *handler_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	__irq_set_handler(virq, handler, 0, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	irq_set_handler_data(virq, handler_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) EXPORT_SYMBOL(irq_domain_set_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)  * irq_domain_free_irqs_common - Clear irq_data and free the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)  * @domain:	Interrupt domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)  * @virq:	IRQ number to start with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)  * @nr_irqs:	The number of irqs to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 				 unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	struct irq_data *irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	for (i = 0; i < nr_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		irq_data = irq_domain_get_irq_data(domain, virq + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		if (irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			irq_domain_reset_irq_data(irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)  * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)  * @domain:	Interrupt domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)  * @virq:	IRQ number to start with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)  * @nr_irqs:	The number of irqs to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			      unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	for (i = 0; i < nr_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		irq_set_handler_data(virq + i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		irq_set_handler(virq + i, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 					   unsigned int irq_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 					   unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	if (!domain->ops->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	for (i = 0; i < nr_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		if (irq_domain_get_irq_data(domain, irq_base + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			domain->ops->free(domain, irq_base + i, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 				    unsigned int irq_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 				    unsigned int nr_irqs, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	if (!domain->ops->alloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		pr_debug("domain->ops->alloc() is NULL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)  * __irq_domain_alloc_irqs - Allocate IRQs from domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)  * @domain:	domain to allocate from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)  * @irq_base:	allocate specified IRQ number if irq_base >= 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  * @nr_irqs:	number of IRQs to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * @node:	NUMA node id for memory allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  * @arg:	domain specific argument
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * @realloc:	IRQ descriptors have already been allocated if true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  * @affinity:	Optional irq affinity mask for multiqueue devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)  * Allocate IRQ numbers and initialized all data structures to support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)  * hierarchy IRQ domains.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)  * Parameter @realloc is mainly to support legacy IRQs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)  * Returns error code or allocated IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)  * The whole process to setup an IRQ has been split into two steps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)  * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)  * descriptor and required hardware resources. The second step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)  * irq_domain_activate_irq(), is to program hardwares with preallocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)  * resources. In this way, it's easier to rollback when failing to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)  * allocate resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			    unsigned int nr_irqs, int node, void *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			    bool realloc, const struct irq_affinity_desc *affinity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	int i, ret, virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	if (domain == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		domain = irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	if (realloc && irq_base >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		virq = irq_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 					      affinity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		if (virq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			pr_debug("cannot allocate IRQ(base %d, count %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 				 irq_base, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 			return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		pr_debug("cannot allocate memory for IRQ%d\n", virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 		goto out_free_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		goto out_free_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	for (i = 0; i < nr_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		ret = irq_domain_trim_hierarchy(virq + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			goto out_free_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	for (i = 0; i < nr_irqs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		irq_domain_insert_irq(virq + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	return virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) out_free_irq_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	irq_domain_free_irq_data(virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) out_free_desc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	irq_free_descs(virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) /* The irq_data was moved, fix the revmap to refer to the new location */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) static void irq_domain_fix_revmap(struct irq_data *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	void __rcu **slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	if (d->hwirq < d->domain->revmap_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		return; /* Not using radix tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	/* Fix up the revmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	mutex_lock(&d->domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	if (slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	mutex_unlock(&d->domain->revmap_tree_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)  * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)  * @domain:	Domain to push.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)  * @virq:	Irq to push the domain in to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)  * @arg:	Passed to the irq_domain_ops alloc() function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  * For an already existing irqdomain hierarchy, as might be obtained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * via a call to pci_enable_msix(), add an additional domain to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * head of the processing chain.  Must be called before request_irq()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  * has been called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	struct irq_data *child_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct irq_data *root_irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	int rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	 * Check that no action has been set, which indicates the virq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	 * is in a state where this function doesn't have to deal with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	 * races between interrupt handling and maintaining the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	 * hierarchy.  This will catch gross misuse.  Attempting to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	 * make the check race free would require holding locks across
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	 * calls to struct irq_domain_ops->alloc(), which could lead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	 * to deadlock, so we just do a simple check before starting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	desc = irq_to_desc(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	if (WARN_ON(desc->action))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	if (domain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	if (WARN_ON(!irq_domain_is_hierarchy(domain)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (!root_irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (domain->parent != root_irq_data->domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 				      irq_data_get_node(root_irq_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (!child_irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	/* Copy the original irq_data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	*child_irq_data = *root_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	 * Overwrite the root_irq_data, which is embedded in struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	 * irq_desc, with values for this domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	root_irq_data->parent_data = child_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	root_irq_data->domain = domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	root_irq_data->mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	root_irq_data->hwirq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	root_irq_data->chip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	root_irq_data->chip_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	/* May (probably does) set hwirq, chip, etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	if (rv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		/* Restore the original irq_data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		*root_irq_data = *child_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		kfree(child_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	irq_domain_fix_revmap(child_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) EXPORT_SYMBOL_GPL(irq_domain_push_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)  * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)  * @domain:	Domain to remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)  * @virq:	Irq to remove the domain from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)  * Undo the effects of a call to irq_domain_push_irq().  Must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)  * called either before request_irq() or after free_irq().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) int irq_domain_pop_irq(struct irq_domain *domain, int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	struct irq_data *root_irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	struct irq_data *child_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	struct irq_data *tmp_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	 * Check that no action is set, which indicates the virq is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	 * a state where this function doesn't have to deal with races
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	 * between interrupt handling and maintaining the hierarchy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	 * This will catch gross misuse.  Attempting to make the check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	 * race free would require holding locks across calls to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	 * struct irq_domain_ops->free(), which could lead to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	 * deadlock, so we just do a simple check before starting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	desc = irq_to_desc(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	if (!desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	if (WARN_ON(desc->action))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	if (domain == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	if (!root_irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 	tmp_irq_data = irq_domain_get_irq_data(domain, virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	/* We can only "pop" if this domain is at the top of the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	if (WARN_ON(root_irq_data != tmp_irq_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (WARN_ON(root_irq_data->domain != domain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	child_irq_data = root_irq_data->parent_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	if (WARN_ON(!child_irq_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	root_irq_data->parent_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	irq_domain_clear_mapping(domain, root_irq_data->hwirq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	irq_domain_free_irqs_hierarchy(domain, virq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	/* Restore the original irq_data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	*root_irq_data = *child_irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	irq_domain_fix_revmap(root_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	kfree(child_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)  * irq_domain_free_irqs - Free IRQ number and associated data structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  * @virq:	base IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  * @nr_irqs:	number of IRQs to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	struct irq_data *data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	if (WARN(!data || !data->domain || !data->domain->ops->free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		 "NULL pointer, cannot free irq\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	for (i = 0; i < nr_irqs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		irq_domain_remove_irq(virq + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	irq_domain_free_irq_data(virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	irq_free_descs(virq, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)  * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)  * @irq_base:	Base IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)  * @nr_irqs:	Number of IRQs to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)  * @arg:	Allocation data (arch/domain specific)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)  * Check whether the domain has been setup recursive. If not allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)  * through the parent domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 				 unsigned int irq_base, unsigned int nr_irqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 				 void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	if (!domain->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 					       nr_irqs, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)  * irq_domain_free_irqs_parent - Free interrupts from parent domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)  * @irq_base:	Base IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700)  * @nr_irqs:	Number of IRQs to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)  * Check whether the domain has been setup recursive. If not free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703)  * through the parent domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) void irq_domain_free_irqs_parent(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 				 unsigned int irq_base, unsigned int nr_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (!domain->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	if (irq_data && irq_data->domain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		struct irq_domain *domain = irq_data->domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		if (domain->ops->deactivate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			domain->ops->deactivate(domain, irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		if (irq_data->parent_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			__irq_domain_deactivate_irq(irq_data->parent_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (irqd && irqd->domain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		struct irq_domain *domain = irqd->domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		if (irqd->parent_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 			ret = __irq_domain_activate_irq(irqd->parent_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 							reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		if (!ret && domain->ops->activate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			ret = domain->ops->activate(domain, irqd, reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			/* Rollback in case of error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			if (ret && irqd->parent_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 				__irq_domain_deactivate_irq(irqd->parent_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748)  * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)  *			     interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)  * @irq_data:	Outermost irq_data associated with interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)  * @reserve:	If set only reserve an interrupt vector instead of assigning one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)  * This is the second step to call domain_ops->activate to program interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)  * controllers, so the interrupt could actually get delivered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	if (!irqd_is_activated(irq_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		ret = __irq_domain_activate_irq(irq_data, reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		irqd_set_activated(irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768)  * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769)  *			       deactivate interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)  * @irq_data: outermost irq_data associated with interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772)  * It calls domain_ops->deactivate to program interrupt controllers to disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773)  * interrupt delivery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) void irq_domain_deactivate_irq(struct irq_data *irq_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	if (irqd_is_activated(irq_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		__irq_domain_deactivate_irq(irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		irqd_clr_activated(irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) static void irq_domain_check_hierarchy(struct irq_domain *domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	/* Hierarchy irq_domains must implement callback alloc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	if (domain->ops->alloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791)  * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792)  * parent has MSI remapping support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793)  * @domain: domain pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	for (; domain; domain = domain->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		if (irq_domain_is_msi_remap(domain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) #else	/* CONFIG_IRQ_DOMAIN_HIERARCHY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806)  * @domain:	domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807)  * @virq:	IRQ number to get irq_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 					 unsigned int virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	struct irq_data *irq_data = irq_get_irq_data(virq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819)  * irq_domain_set_info - Set the complete data for a @virq in @domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)  * @domain:		Interrupt domain to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)  * @virq:		IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822)  * @hwirq:		The hardware interrupt number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)  * @chip:		The associated interrupt chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824)  * @chip_data:		The associated interrupt chip data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825)  * @handler:		The interrupt flow handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)  * @handler_data:	The interrupt flow handler data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827)  * @handler_name:	The interrupt handler name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			 irq_hw_number_t hwirq, struct irq_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 			 void *chip_data, irq_flow_handler_t handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 			 void *handler_data, const char *handler_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	irq_set_chip_data(virq, chip_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	irq_set_handler_data(virq, handler_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) static void irq_domain_check_hierarchy(struct irq_domain *domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) #endif	/* CONFIG_IRQ_DOMAIN_HIERARCHY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) static struct dentry *domain_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	seq_printf(m, "%*sname:   %s\n", ind, "", d->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	seq_printf(m, "%*ssize:   %u\n", ind + 1, "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 		   d->revmap_size + d->revmap_direct_max_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	seq_printf(m, "%*sflags:  0x%08x\n", ind +1 , "", d->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	if (d->ops && d->ops->debug_show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		d->ops->debug_show(m, d, NULL, ind + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	if (!d->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	irq_domain_debug_show_one(m, d->parent, ind + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) static int irq_domain_debug_show(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	struct irq_domain *d = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	/* Default domain? Might be NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	if (!d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		if (!irq_default_domain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		d = irq_default_domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	irq_domain_debug_show_one(m, d, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) static void debugfs_add_domain_dir(struct irq_domain *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	if (!d->name || !domain_dir || d->debugfs_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 					      &irq_domain_debug_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) static void debugfs_remove_domain_dir(struct irq_domain *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	debugfs_remove(d->debugfs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	d->debugfs_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) void __init irq_domain_debugfs_init(struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	struct irq_domain *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	domain_dir = debugfs_create_dir("domains", root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	debugfs_create_file("default", 0444, domain_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			    &irq_domain_debug_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	mutex_lock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	list_for_each_entry(d, &irq_domain_list, link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		debugfs_add_domain_dir(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	mutex_unlock(&irq_domain_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) #endif