123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884 |
- #define pr_fmt(fmt) "irq: " fmt
- #include <linux/acpi.h>
- #include <linux/debugfs.h>
- #include <linux/hardirq.h>
- #include <linux/interrupt.h>
- #include <linux/irq.h>
- #include <linux/irqdesc.h>
- #include <linux/irqdomain.h>
- #include <linux/module.h>
- #include <linux/mutex.h>
- #include <linux/of.h>
- #include <linux/of_address.h>
- #include <linux/of_irq.h>
- #include <linux/topology.h>
- #include <linux/seq_file.h>
- #include <linux/slab.h>
- #include <linux/smp.h>
- #include <linux/fs.h>
- static LIST_HEAD(irq_domain_list);
- static DEFINE_MUTEX(irq_domain_mutex);
- static DEFINE_MUTEX(revmap_trees_mutex);
- static struct irq_domain *irq_default_domain;
- static void irq_domain_check_hierarchy(struct irq_domain *domain);
- struct irqchip_fwid {
- struct fwnode_handle fwnode;
- unsigned int type;
- char *name;
- void *data;
- };
- #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
- static void debugfs_add_domain_dir(struct irq_domain *d);
- static void debugfs_remove_domain_dir(struct irq_domain *d);
- #else
- static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
- static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
- #endif
- const struct fwnode_operations irqchip_fwnode_ops;
- EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
- /**
- * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
- * identifying an irq domain
- * @type: Type of irqchip_fwnode. See linux/irqdomain.h
- * @name: Optional user provided domain name
- * @id: Optional user provided id if name != NULL
- * @data: Optional user-provided data
- *
- * Allocate a struct irqchip_fwid, and return a poiner to the embedded
- * fwnode_handle (or NULL on failure).
- *
- * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
- * solely to transport name information to irqdomain creation code. The
- * node is not stored. For other types the pointer is kept in the irq
- * domain struct.
- */
- struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
- const char *name, void *data)
- {
- struct irqchip_fwid *fwid;
- char *n;
- fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
- switch (type) {
- case IRQCHIP_FWNODE_NAMED:
- n = kasprintf(GFP_KERNEL, "%s", name);
- break;
- case IRQCHIP_FWNODE_NAMED_ID:
- n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
- break;
- default:
- n = kasprintf(GFP_KERNEL, "irqchip@%p", data);
- break;
- }
- if (!fwid || !n) {
- kfree(fwid);
- kfree(n);
- return NULL;
- }
- fwid->type = type;
- fwid->name = n;
- fwid->data = data;
- fwid->fwnode.ops = &irqchip_fwnode_ops;
- return &fwid->fwnode;
- }
- EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
- /**
- * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
- *
- * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
- */
- void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
- {
- struct irqchip_fwid *fwid;
- if (WARN_ON(!is_fwnode_irqchip(fwnode)))
- return;
- fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
- kfree(fwid->name);
- kfree(fwid);
- }
- EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
- /**
- * __irq_domain_add() - Allocate a new irq_domain data structure
- * @fwnode: firmware node for the interrupt controller
- * @size: Size of linear map; 0 for radix mapping only
- * @hwirq_max: Maximum number of interrupts supported by controller
- * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
- * direct mapping
- * @ops: domain callbacks
- * @host_data: Controller private data pointer
- *
- * Allocates and initialize and irq_domain structure.
- * Returns pointer to IRQ domain, or NULL on failure.
- */
- struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
- irq_hw_number_t hwirq_max, int direct_max,
- const struct irq_domain_ops *ops,
- void *host_data)
- {
- struct device_node *of_node = to_of_node(fwnode);
- struct irqchip_fwid *fwid;
- struct irq_domain *domain;
- static atomic_t unknown_domains;
- domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
- GFP_KERNEL, of_node_to_nid(of_node));
- if (WARN_ON(!domain))
- return NULL;
- if (fwnode && is_fwnode_irqchip(fwnode)) {
- fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
- switch (fwid->type) {
- case IRQCHIP_FWNODE_NAMED:
- case IRQCHIP_FWNODE_NAMED_ID:
- domain->fwnode = fwnode;
- domain->name = kstrdup(fwid->name, GFP_KERNEL);
- if (!domain->name) {
- kfree(domain);
- return NULL;
- }
- domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
- break;
- default:
- domain->fwnode = fwnode;
- domain->name = fwid->name;
- break;
- }
- #ifdef CONFIG_ACPI
- } else if (is_acpi_device_node(fwnode)) {
- struct acpi_buffer buf = {
- .length = ACPI_ALLOCATE_BUFFER,
- };
- acpi_handle handle;
- handle = acpi_device_handle(to_acpi_device_node(fwnode));
- if (acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf) == AE_OK) {
- domain->name = buf.pointer;
- domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
- }
- domain->fwnode = fwnode;
- #endif
- } else if (of_node) {
- char *name;
- /*
- * DT paths contain '/', which debugfs is legitimately
- * unhappy about. Replace them with ':', which does
- * the trick and is not as offensive as '\'...
- */
- name = kstrdup(of_node_full_name(of_node), GFP_KERNEL);
- if (!name) {
- kfree(domain);
- return NULL;
- }
- strreplace(name, '/', ':');
- domain->name = name;
- domain->fwnode = fwnode;
- domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
- }
- if (!domain->name) {
- if (fwnode)
- pr_err("Invalid fwnode type for irqdomain\n");
- domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
- atomic_inc_return(&unknown_domains));
- if (!domain->name) {
- kfree(domain);
- return NULL;
- }
- domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
- }
- of_node_get(of_node);
- /* Fill structure */
- INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
- domain->ops = ops;
- domain->host_data = host_data;
- domain->hwirq_max = hwirq_max;
- domain->revmap_size = size;
- domain->revmap_direct_max_irq = direct_max;
- irq_domain_check_hierarchy(domain);
- mutex_lock(&irq_domain_mutex);
- debugfs_add_domain_dir(domain);
- list_add(&domain->link, &irq_domain_list);
- mutex_unlock(&irq_domain_mutex);
- pr_debug("Added domain %s\n", domain->name);
- return domain;
- }
- EXPORT_SYMBOL_GPL(__irq_domain_add);
- /**
- * irq_domain_remove() - Remove an irq domain.
- * @domain: domain to remove
- *
- * This routine is used to remove an irq domain. The caller must ensure
- * that all mappings within the domain have been disposed of prior to
- * use, depending on the revmap type.
- */
- void irq_domain_remove(struct irq_domain *domain)
- {
- mutex_lock(&irq_domain_mutex);
- debugfs_remove_domain_dir(domain);
- WARN_ON(!radix_tree_empty(&domain->revmap_tree));
- list_del(&domain->link);
- /*
- * If the going away domain is the default one, reset it.
- */
- if (unlikely(irq_default_domain == domain))
- irq_set_default_host(NULL);
- mutex_unlock(&irq_domain_mutex);
- pr_debug("Removed domain %s\n", domain->name);
- of_node_put(irq_domain_get_of_node(domain));
- if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
- kfree(domain->name);
- kfree(domain);
- }
- EXPORT_SYMBOL_GPL(irq_domain_remove);
- void irq_domain_update_bus_token(struct irq_domain *domain,
- enum irq_domain_bus_token bus_token)
- {
- char *name;
- if (domain->bus_token == bus_token)
- return;
- mutex_lock(&irq_domain_mutex);
- domain->bus_token = bus_token;
- name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
- if (!name) {
- mutex_unlock(&irq_domain_mutex);
- return;
- }
- debugfs_remove_domain_dir(domain);
- if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
- kfree(domain->name);
- else
- domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
- domain->name = name;
- debugfs_add_domain_dir(domain);
- mutex_unlock(&irq_domain_mutex);
- }
- /**
- * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
- * @of_node: pointer to interrupt controller's device tree node.
- * @size: total number of irqs in mapping
- * @first_irq: first number of irq block assigned to the domain,
- * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
- * pre-map all of the irqs in the domain to virqs starting at first_irq.
- * @ops: domain callbacks
- * @host_data: Controller private data pointer
- *
- * Allocates an irq_domain, and optionally if first_irq is positive then also
- * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
- *
- * This is intended to implement the expected behaviour for most
- * interrupt controllers. If device tree is used, then first_irq will be 0 and
- * irqs get mapped dynamically on the fly. However, if the controller requires
- * static virq assignments (non-DT boot) then it will set that up correctly.
- */
- struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
- unsigned int size,
- unsigned int first_irq,
- const struct irq_domain_ops *ops,
- void *host_data)
- {
- struct irq_domain *domain;
- domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
- if (!domain)
- return NULL;
- if (first_irq > 0) {
- if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
- /* attempt to allocated irq_descs */
- int rc = irq_alloc_descs(first_irq, first_irq, size,
- of_node_to_nid(of_node));
- if (rc < 0)
- pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
- first_irq);
- }
- irq_domain_associate_many(domain, first_irq, 0, size);
- }
- return domain;
- }
- EXPORT_SYMBOL_GPL(irq_domain_add_simple);
- /**
- * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
- * @of_node: pointer to interrupt controller's device tree node.
- * @size: total number of irqs in legacy mapping
- * @first_irq: first number of irq block assigned to the domain
- * @first_hwirq: first hwirq number to use for the translation. Should normally
- * be '0', but a positive integer can be used if the effective
- * hwirqs numbering does not begin at zero.
- * @ops: map/unmap domain callbacks
- * @host_data: Controller private data pointer
- *
- * Note: the map() callback will be called before this function returns
- * for all legacy interrupts except 0 (which is always the invalid irq for
- * a legacy controller).
- */
- struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
- unsigned int size,
- unsigned int first_irq,
- irq_hw_number_t first_hwirq,
- const struct irq_domain_ops *ops,
- void *host_data)
- {
- struct irq_domain *domain;
- domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
- first_hwirq + size, 0, ops, host_data);
- if (domain)
- irq_domain_associate_many(domain, first_irq, first_hwirq, size);
- return domain;
- }
- EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
- /**
- * irq_find_matching_fwspec() - Locates a domain for a given fwspec
- * @fwspec: FW specifier for an interrupt
- * @bus_token: domain-specific data
- */
- struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
- enum irq_domain_bus_token bus_token)
- {
- struct irq_domain *h, *found = NULL;
- struct fwnode_handle *fwnode = fwspec->fwnode;
- int rc;
- /* We might want to match the legacy controller last since
- * it might potentially be set to match all interrupts in
- * the absence of a device node. This isn't a problem so far
- * yet though...
- *
- * bus_token == DOMAIN_BUS_ANY matches any domain, any other
- * values must generate an exact match for the domain to be
- * selected.
- */
- mutex_lock(&irq_domain_mutex);
- list_for_each_entry(h, &irq_domain_list, link) {
- if (h->ops->select && fwspec->param_count)
- rc = h->ops->select(h, fwspec, bus_token);
- else if (h->ops->match)
- rc = h->ops->match(h, to_of_node(fwnode), bus_token);
- else
- rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
- ((bus_token == DOMAIN_BUS_ANY) ||
- (h->bus_token == bus_token)));
- if (rc) {
- found = h;
- break;
- }
- }
- mutex_unlock(&irq_domain_mutex);
- return found;
- }
- EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
- /**
- * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
- * IRQ remapping
- *
- * Return: false if any MSI irq domain does not support IRQ remapping,
- * true otherwise (including if there is no MSI irq domain)
- */
- bool irq_domain_check_msi_remap(void)
- {
- struct irq_domain *h;
- bool ret = true;
- mutex_lock(&irq_domain_mutex);
- list_for_each_entry(h, &irq_domain_list, link) {
- if (irq_domain_is_msi(h) &&
- !irq_domain_hierarchical_is_msi_remap(h)) {
- ret = false;
- break;
- }
- }
- mutex_unlock(&irq_domain_mutex);
- return ret;
- }
- EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
- /**
- * irq_set_default_host() - Set a "default" irq domain
- * @domain: default domain pointer
- *
- * For convenience, it's possible to set a "default" domain that will be used
- * whenever NULL is passed to irq_create_mapping(). It makes life easier for
- * platforms that want to manipulate a few hard coded interrupt numbers that
- * aren't properly represented in the device-tree.
- */
- void irq_set_default_host(struct irq_domain *domain)
- {
- pr_debug("Default domain set to @0x%p\n", domain);
- irq_default_domain = domain;
- }
- EXPORT_SYMBOL_GPL(irq_set_default_host);
- static void irq_domain_clear_mapping(struct irq_domain *domain,
- irq_hw_number_t hwirq)
- {
- if (hwirq < domain->revmap_size) {
- domain->linear_revmap[hwirq] = 0;
- } else {
- mutex_lock(&revmap_trees_mutex);
- radix_tree_delete(&domain->revmap_tree, hwirq);
- mutex_unlock(&revmap_trees_mutex);
- }
- }
- static void irq_domain_set_mapping(struct irq_domain *domain,
- irq_hw_number_t hwirq,
- struct irq_data *irq_data)
- {
- if (hwirq < domain->revmap_size) {
- domain->linear_revmap[hwirq] = irq_data->irq;
- } else {
- mutex_lock(&revmap_trees_mutex);
- radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
- mutex_unlock(&revmap_trees_mutex);
- }
- }
- void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
- {
- struct irq_data *irq_data = irq_get_irq_data(irq);
- irq_hw_number_t hwirq;
- if (WARN(!irq_data || irq_data->domain != domain,
- "virq%i doesn't exist; cannot disassociate\n", irq))
- return;
- hwirq = irq_data->hwirq;
- irq_set_status_flags(irq, IRQ_NOREQUEST);
- /* remove chip and handler */
- irq_set_chip_and_handler(irq, NULL, NULL);
- /* Make sure it's completed */
- synchronize_irq(irq);
- /* Tell the PIC about it */
- if (domain->ops->unmap)
- domain->ops->unmap(domain, irq);
- smp_mb();
- irq_data->domain = NULL;
- irq_data->hwirq = 0;
- domain->mapcount--;
- /* Clear reverse map for this hwirq */
- irq_domain_clear_mapping(domain, hwirq);
- }
- int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
- irq_hw_number_t hwirq)
- {
- struct irq_data *irq_data = irq_get_irq_data(virq);
- int ret;
- if (WARN(hwirq >= domain->hwirq_max,
- "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
- return -EINVAL;
- if (WARN(!irq_data, "error: virq%i is not allocated", virq))
- return -EINVAL;
- if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
- return -EINVAL;
- mutex_lock(&irq_domain_mutex);
- irq_data->hwirq = hwirq;
- irq_data->domain = domain;
- if (domain->ops->map) {
- ret = domain->ops->map(domain, virq, hwirq);
- if (ret != 0) {
- /*
- * If map() returns -EPERM, this interrupt is protected
- * by the firmware or some other service and shall not
- * be mapped. Don't bother telling the user about it.
- */
- if (ret != -EPERM) {
- pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
- domain->name, hwirq, virq, ret);
- }
- irq_data->domain = NULL;
- irq_data->hwirq = 0;
- mutex_unlock(&irq_domain_mutex);
- return ret;
- }
- /* If not already assigned, give the domain the chip's name */
- if (!domain->name && irq_data->chip)
- domain->name = irq_data->chip->name;
- }
- domain->mapcount++;
- irq_domain_set_mapping(domain, hwirq, irq_data);
- mutex_unlock(&irq_domain_mutex);
- irq_clear_status_flags(virq, IRQ_NOREQUEST);
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_domain_associate);
- void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
- irq_hw_number_t hwirq_base, int count)
- {
- struct device_node *of_node;
- int i;
- of_node = irq_domain_get_of_node(domain);
- pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
- of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
- for (i = 0; i < count; i++) {
- irq_domain_associate(domain, irq_base + i, hwirq_base + i);
- }
- }
- EXPORT_SYMBOL_GPL(irq_domain_associate_many);
- /**
- * irq_create_direct_mapping() - Allocate an irq for direct mapping
- * @domain: domain to allocate the irq for or NULL for default domain
- *
- * This routine is used for irq controllers which can choose the hardware
- * interrupt numbers they generate. In such a case it's simplest to use
- * the linux irq as the hardware interrupt number. It still uses the linear
- * or radix tree to store the mapping, but the irq controller can optimize
- * the revmap path by using the hwirq directly.
- */
- unsigned int irq_create_direct_mapping(struct irq_domain *domain)
- {
- struct device_node *of_node;
- unsigned int virq;
- if (domain == NULL)
- domain = irq_default_domain;
- of_node = irq_domain_get_of_node(domain);
- virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
- if (!virq) {
- pr_debug("create_direct virq allocation failed\n");
- return 0;
- }
- if (virq >= domain->revmap_direct_max_irq) {
- pr_err("ERROR: no free irqs available below %i maximum\n",
- domain->revmap_direct_max_irq);
- irq_free_desc(virq);
- return 0;
- }
- pr_debug("create_direct obtained virq %d\n", virq);
- if (irq_domain_associate(domain, virq, virq)) {
- irq_free_desc(virq);
- return 0;
- }
- return virq;
- }
- EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
- /**
- * irq_create_mapping() - Map a hardware interrupt into linux irq space
- * @domain: domain owning this hardware interrupt or NULL for default domain
- * @hwirq: hardware irq number in that domain space
- *
- * Only one mapping per hardware interrupt is permitted. Returns a linux
- * irq number.
- * If the sense/trigger is to be specified, set_irq_type() should be called
- * on the number returned from that call.
- */
- unsigned int irq_create_mapping(struct irq_domain *domain,
- irq_hw_number_t hwirq)
- {
- struct device_node *of_node;
- int virq;
- pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
- /* Look for default domain if nececssary */
- if (domain == NULL)
- domain = irq_default_domain;
- if (domain == NULL) {
- WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
- return 0;
- }
- pr_debug("-> using domain @%p\n", domain);
- of_node = irq_domain_get_of_node(domain);
- /* Check if mapping already exists */
- virq = irq_find_mapping(domain, hwirq);
- if (virq) {
- pr_debug("-> existing mapping on virq %d\n", virq);
- return virq;
- }
- /* Allocate a virtual interrupt number */
- virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
- if (virq <= 0) {
- pr_debug("-> virq allocation failed\n");
- return 0;
- }
- if (irq_domain_associate(domain, virq, hwirq)) {
- irq_free_desc(virq);
- return 0;
- }
- pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
- hwirq, of_node_full_name(of_node), virq);
- return virq;
- }
- EXPORT_SYMBOL_GPL(irq_create_mapping);
- /**
- * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
- * @domain: domain owning the interrupt range
- * @irq_base: beginning of linux IRQ range
- * @hwirq_base: beginning of hardware IRQ range
- * @count: Number of interrupts to map
- *
- * This routine is used for allocating and mapping a range of hardware
- * irqs to linux irqs where the linux irq numbers are at pre-defined
- * locations. For use by controllers that already have static mappings
- * to insert in to the domain.
- *
- * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
- * domain insertion.
- *
- * 0 is returned upon success, while any failure to establish a static
- * mapping is treated as an error.
- */
- int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
- irq_hw_number_t hwirq_base, int count)
- {
- struct device_node *of_node;
- int ret;
- of_node = irq_domain_get_of_node(domain);
- ret = irq_alloc_descs(irq_base, irq_base, count,
- of_node_to_nid(of_node));
- if (unlikely(ret < 0))
- return ret;
- irq_domain_associate_many(domain, irq_base, hwirq_base, count);
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
- static int irq_domain_translate(struct irq_domain *d,
- struct irq_fwspec *fwspec,
- irq_hw_number_t *hwirq, unsigned int *type)
- {
- #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
- if (d->ops->translate)
- return d->ops->translate(d, fwspec, hwirq, type);
- #endif
- if (d->ops->xlate)
- return d->ops->xlate(d, to_of_node(fwspec->fwnode),
- fwspec->param, fwspec->param_count,
- hwirq, type);
- /* If domain has no translation, then we assume interrupt line */
- *hwirq = fwspec->param[0];
- return 0;
- }
- static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
- struct irq_fwspec *fwspec)
- {
- int i;
- fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
- fwspec->param_count = irq_data->args_count;
- for (i = 0; i < irq_data->args_count; i++)
- fwspec->param[i] = irq_data->args[i];
- }
- unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
- {
- struct irq_domain *domain;
- struct irq_data *irq_data;
- irq_hw_number_t hwirq;
- unsigned int type = IRQ_TYPE_NONE;
- int virq;
- if (fwspec->fwnode) {
- domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
- if (!domain)
- domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
- } else {
- domain = irq_default_domain;
- }
- if (!domain) {
- pr_warn("no irq domain found for %s !\n",
- of_node_full_name(to_of_node(fwspec->fwnode)));
- return 0;
- }
- if (irq_domain_translate(domain, fwspec, &hwirq, &type))
- return 0;
- /*
- * WARN if the irqchip returns a type with bits
- * outside the sense mask set and clear these bits.
- */
- if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
- type &= IRQ_TYPE_SENSE_MASK;
- /*
- * If we've already configured this interrupt,
- * don't do it again, or hell will break loose.
- */
- virq = irq_find_mapping(domain, hwirq);
- if (virq) {
- /*
- * If the trigger type is not specified or matches the
- * current trigger type then we are done so return the
- * interrupt number.
- */
- if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
- return virq;
- /*
- * If the trigger type has not been set yet, then set
- * it now and return the interrupt number.
- */
- if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
- irq_data = irq_get_irq_data(virq);
- if (!irq_data)
- return 0;
- irqd_set_trigger_type(irq_data, type);
- return virq;
- }
- pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
- hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
- return 0;
- }
- if (irq_domain_is_hierarchy(domain)) {
- virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
- if (virq <= 0)
- return 0;
- } else {
- /* Create mapping */
- virq = irq_create_mapping(domain, hwirq);
- if (!virq)
- return virq;
- }
- irq_data = irq_get_irq_data(virq);
- if (!irq_data) {
- if (irq_domain_is_hierarchy(domain))
- irq_domain_free_irqs(virq, 1);
- else
- irq_dispose_mapping(virq);
- return 0;
- }
- /* Store trigger type */
- irqd_set_trigger_type(irq_data, type);
- return virq;
- }
- EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
- unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
- {
- struct irq_fwspec fwspec;
- of_phandle_args_to_fwspec(irq_data, &fwspec);
- return irq_create_fwspec_mapping(&fwspec);
- }
- EXPORT_SYMBOL_GPL(irq_create_of_mapping);
- /**
- * irq_dispose_mapping() - Unmap an interrupt
- * @virq: linux irq number of the interrupt to unmap
- */
- void irq_dispose_mapping(unsigned int virq)
- {
- struct irq_data *irq_data = irq_get_irq_data(virq);
- struct irq_domain *domain;
- if (!virq || !irq_data)
- return;
- domain = irq_data->domain;
- if (WARN_ON(domain == NULL))
- return;
- if (irq_domain_is_hierarchy(domain)) {
- irq_domain_free_irqs(virq, 1);
- } else {
- irq_domain_disassociate(domain, virq);
- irq_free_desc(virq);
- }
- }
- EXPORT_SYMBOL_GPL(irq_dispose_mapping);
- /**
- * irq_find_mapping() - Find a linux irq from an hw irq number.
- * @domain: domain owning this hardware interrupt
- * @hwirq: hardware irq number in that domain space
- */
- unsigned int irq_find_mapping(struct irq_domain *domain,
- irq_hw_number_t hwirq)
- {
- struct irq_data *data;
- /* Look for default domain if nececssary */
- if (domain == NULL)
- domain = irq_default_domain;
- if (domain == NULL)
- return 0;
- if (hwirq < domain->revmap_direct_max_irq) {
- data = irq_domain_get_irq_data(domain, hwirq);
- if (data && data->hwirq == hwirq)
- return hwirq;
- }
- /* Check if the hwirq is in the linear revmap. */
- if (hwirq < domain->revmap_size)
- return domain->linear_revmap[hwirq];
- rcu_read_lock();
- data = radix_tree_lookup(&domain->revmap_tree, hwirq);
- rcu_read_unlock();
- return data ? data->irq : 0;
- }
- EXPORT_SYMBOL_GPL(irq_find_mapping);
- #ifdef CONFIG_IRQ_DOMAIN_DEBUG
- static void virq_debug_show_one(struct seq_file *m, struct irq_desc *desc)
- {
- struct irq_domain *domain;
- struct irq_data *data;
- domain = desc->irq_data.domain;
- data = &desc->irq_data;
- while (domain) {
- unsigned int irq = data->irq;
- unsigned long hwirq = data->hwirq;
- struct irq_chip *chip;
- bool direct;
- if (data == &desc->irq_data)
- seq_printf(m, "%5d ", irq);
- else
- seq_printf(m, "%5d+ ", irq);
- seq_printf(m, "0x%05lx ", hwirq);
- chip = irq_data_get_irq_chip(data);
- seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
- seq_printf(m, data ? "0x%p " : " %p ",
- irq_data_get_irq_chip_data(data));
- seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
- direct = (irq == hwirq) && (irq < domain->revmap_direct_max_irq);
- seq_printf(m, "%6s%-8s ",
- (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
- direct ? "(DIRECT)" : "");
- seq_printf(m, "%s\n", domain->name);
- #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
- domain = domain->parent;
- data = data->parent_data;
- #else
- domain = NULL;
- #endif
- }
- }
- static int virq_debug_show(struct seq_file *m, void *private)
- {
- unsigned long flags;
- struct irq_desc *desc;
- struct irq_domain *domain;
- struct radix_tree_iter iter;
- void __rcu **slot;
- int i;
- seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
- "name", "mapped", "linear-max", "direct-max", "devtree-node");
- mutex_lock(&irq_domain_mutex);
- list_for_each_entry(domain, &irq_domain_list, link) {
- struct device_node *of_node;
- const char *name;
- int count = 0;
- of_node = irq_domain_get_of_node(domain);
- if (of_node)
- name = of_node_full_name(of_node);
- else if (is_fwnode_irqchip(domain->fwnode))
- name = container_of(domain->fwnode, struct irqchip_fwid,
- fwnode)->name;
- else
- name = "";
- radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
- count++;
- seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
- domain == irq_default_domain ? '*' : ' ', domain->name,
- domain->revmap_size + count, domain->revmap_size,
- domain->revmap_direct_max_irq,
- name);
- }
- mutex_unlock(&irq_domain_mutex);
- seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
- "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
- "active", "type", "domain");
- for (i = 1; i < nr_irqs; i++) {
- desc = irq_to_desc(i);
- if (!desc)
- continue;
- raw_spin_lock_irqsave(&desc->lock, flags);
- virq_debug_show_one(m, desc);
- raw_spin_unlock_irqrestore(&desc->lock, flags);
- }
- return 0;
- }
- static int virq_debug_open(struct inode *inode, struct file *file)
- {
- return single_open(file, virq_debug_show, inode->i_private);
- }
- static const struct file_operations virq_debug_fops = {
- .open = virq_debug_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static int __init irq_debugfs_init(void)
- {
- if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
- NULL, &virq_debug_fops) == NULL)
- return -ENOMEM;
- return 0;
- }
- __initcall(irq_debugfs_init);
- #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
- /**
- * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
- *
- * Device Tree IRQ specifier translation function which works with one cell
- * bindings where the cell value maps directly to the hwirq number.
- */
- int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
- const u32 *intspec, unsigned int intsize,
- unsigned long *out_hwirq, unsigned int *out_type)
- {
- if (WARN_ON(intsize < 1))
- return -EINVAL;
- *out_hwirq = intspec[0];
- *out_type = IRQ_TYPE_NONE;
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
- /**
- * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
- *
- * Device Tree IRQ specifier translation function which works with two cell
- * bindings where the cell values map directly to the hwirq number
- * and linux irq flags.
- */
- int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
- const u32 *intspec, unsigned int intsize,
- irq_hw_number_t *out_hwirq, unsigned int *out_type)
- {
- if (WARN_ON(intsize < 2))
- return -EINVAL;
- *out_hwirq = intspec[0];
- *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
- /**
- * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
- *
- * Device Tree IRQ specifier translation function which works with either one
- * or two cell bindings where the cell values map directly to the hwirq number
- * and linux irq flags.
- *
- * Note: don't use this function unless your interrupt controller explicitly
- * supports both one and two cell bindings. For the majority of controllers
- * the _onecell() or _twocell() variants above should be used.
- */
- int irq_domain_xlate_onetwocell(struct irq_domain *d,
- struct device_node *ctrlr,
- const u32 *intspec, unsigned int intsize,
- unsigned long *out_hwirq, unsigned int *out_type)
- {
- if (WARN_ON(intsize < 1))
- return -EINVAL;
- *out_hwirq = intspec[0];
- if (intsize > 1)
- *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
- else
- *out_type = IRQ_TYPE_NONE;
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
- const struct irq_domain_ops irq_domain_simple_ops = {
- .xlate = irq_domain_xlate_onetwocell,
- };
- EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
- int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
- int node, const struct cpumask *affinity)
- {
- unsigned int hint;
- if (virq >= 0) {
- virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
- affinity);
- } else {
- hint = hwirq % nr_irqs;
- if (hint == 0)
- hint++;
- virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
- affinity);
- if (virq <= 0 && hint > 1) {
- virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
- affinity);
- }
- }
- return virq;
- }
- #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
- /**
- * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
- * @parent: Parent irq domain to associate with the new domain
- * @flags: Irq domain flags associated to the domain
- * @size: Size of the domain. See below
- * @fwnode: Optional fwnode of the interrupt controller
- * @ops: Pointer to the interrupt domain callbacks
- * @host_data: Controller private data pointer
- *
- * If @size is 0 a tree domain is created, otherwise a linear domain.
- *
- * If successful the parent is associated to the new domain and the
- * domain flags are set.
- * Returns pointer to IRQ domain, or NULL on failure.
- */
- struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
- unsigned int flags,
- unsigned int size,
- struct fwnode_handle *fwnode,
- const struct irq_domain_ops *ops,
- void *host_data)
- {
- struct irq_domain *domain;
- if (size)
- domain = irq_domain_create_linear(fwnode, size, ops, host_data);
- else
- domain = irq_domain_create_tree(fwnode, ops, host_data);
- if (domain) {
- domain->parent = parent;
- domain->flags |= flags;
- }
- return domain;
- }
- EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
- static void irq_domain_insert_irq(int virq)
- {
- struct irq_data *data;
- for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
- struct irq_domain *domain = data->domain;
- domain->mapcount++;
- irq_domain_set_mapping(domain, data->hwirq, data);
- /* If not already assigned, give the domain the chip's name */
- if (!domain->name && data->chip)
- domain->name = data->chip->name;
- }
- irq_clear_status_flags(virq, IRQ_NOREQUEST);
- }
- static void irq_domain_remove_irq(int virq)
- {
- struct irq_data *data;
- irq_set_status_flags(virq, IRQ_NOREQUEST);
- irq_set_chip_and_handler(virq, NULL, NULL);
- synchronize_irq(virq);
- smp_mb();
- for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
- struct irq_domain *domain = data->domain;
- irq_hw_number_t hwirq = data->hwirq;
- domain->mapcount--;
- irq_domain_clear_mapping(domain, hwirq);
- }
- }
- static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
- struct irq_data *child)
- {
- struct irq_data *irq_data;
- irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
- irq_data_get_node(child));
- if (irq_data) {
- child->parent_data = irq_data;
- irq_data->irq = child->irq;
- irq_data->common = child->common;
- irq_data->domain = domain;
- }
- return irq_data;
- }
- static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
- {
- struct irq_data *irq_data, *tmp;
- int i;
- for (i = 0; i < nr_irqs; i++) {
- irq_data = irq_get_irq_data(virq + i);
- tmp = irq_data->parent_data;
- irq_data->parent_data = NULL;
- irq_data->domain = NULL;
- while (tmp) {
- irq_data = tmp;
- tmp = tmp->parent_data;
- kfree(irq_data);
- }
- }
- }
- static int irq_domain_alloc_irq_data(struct irq_domain *domain,
- unsigned int virq, unsigned int nr_irqs)
- {
- struct irq_data *irq_data;
- struct irq_domain *parent;
- int i;
- /* The outermost irq_data is embedded in struct irq_desc */
- for (i = 0; i < nr_irqs; i++) {
- irq_data = irq_get_irq_data(virq + i);
- irq_data->domain = domain;
- for (parent = domain->parent; parent; parent = parent->parent) {
- irq_data = irq_domain_insert_irq_data(parent, irq_data);
- if (!irq_data) {
- irq_domain_free_irq_data(virq, i + 1);
- return -ENOMEM;
- }
- }
- }
- return 0;
- }
- /**
- * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
- * @domain: domain to match
- * @virq: IRQ number to get irq_data
- */
- struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
- unsigned int virq)
- {
- struct irq_data *irq_data;
- for (irq_data = irq_get_irq_data(virq); irq_data;
- irq_data = irq_data->parent_data)
- if (irq_data->domain == domain)
- return irq_data;
- return NULL;
- }
- EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
- /**
- * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
- * @domain: Interrupt domain to match
- * @virq: IRQ number
- * @hwirq: The hwirq number
- * @chip: The associated interrupt chip
- * @chip_data: The associated chip data
- */
- int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
- irq_hw_number_t hwirq, struct irq_chip *chip,
- void *chip_data)
- {
- struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
- if (!irq_data)
- return -ENOENT;
- irq_data->hwirq = hwirq;
- irq_data->chip = chip ? chip : &no_irq_chip;
- irq_data->chip_data = chip_data;
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
- /**
- * irq_domain_set_info - Set the complete data for a @virq in @domain
- * @domain: Interrupt domain to match
- * @virq: IRQ number
- * @hwirq: The hardware interrupt number
- * @chip: The associated interrupt chip
- * @chip_data: The associated interrupt chip data
- * @handler: The interrupt flow handler
- * @handler_data: The interrupt flow handler data
- * @handler_name: The interrupt handler name
- */
- void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
- irq_hw_number_t hwirq, struct irq_chip *chip,
- void *chip_data, irq_flow_handler_t handler,
- void *handler_data, const char *handler_name)
- {
- irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
- __irq_set_handler(virq, handler, 0, handler_name);
- irq_set_handler_data(virq, handler_data);
- }
- EXPORT_SYMBOL(irq_domain_set_info);
- /**
- * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
- * @irq_data: The pointer to irq_data
- */
- void irq_domain_reset_irq_data(struct irq_data *irq_data)
- {
- irq_data->hwirq = 0;
- irq_data->chip = &no_irq_chip;
- irq_data->chip_data = NULL;
- }
- EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
- /**
- * irq_domain_free_irqs_common - Clear irq_data and free the parent
- * @domain: Interrupt domain to match
- * @virq: IRQ number to start with
- * @nr_irqs: The number of irqs to free
- */
- void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
- unsigned int nr_irqs)
- {
- struct irq_data *irq_data;
- int i;
- for (i = 0; i < nr_irqs; i++) {
- irq_data = irq_domain_get_irq_data(domain, virq + i);
- if (irq_data)
- irq_domain_reset_irq_data(irq_data);
- }
- irq_domain_free_irqs_parent(domain, virq, nr_irqs);
- }
- EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
- /**
- * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
- * @domain: Interrupt domain to match
- * @virq: IRQ number to start with
- * @nr_irqs: The number of irqs to free
- */
- void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
- unsigned int nr_irqs)
- {
- int i;
- for (i = 0; i < nr_irqs; i++) {
- irq_set_handler_data(virq + i, NULL);
- irq_set_handler(virq + i, NULL);
- }
- irq_domain_free_irqs_common(domain, virq, nr_irqs);
- }
- static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
- unsigned int irq_base,
- unsigned int nr_irqs)
- {
- unsigned int i;
- if (!domain->ops->free)
- return;
- for (i = 0; i < nr_irqs; i++) {
- if (irq_domain_get_irq_data(domain, irq_base + i))
- domain->ops->free(domain, irq_base + i, 1);
- }
- }
- int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
- unsigned int irq_base,
- unsigned int nr_irqs, void *arg)
- {
- if (!domain->ops->alloc) {
- pr_debug("domain->ops->alloc() is NULL\n");
- return -ENOSYS;
- }
- return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
- }
- /**
- * __irq_domain_alloc_irqs - Allocate IRQs from domain
- * @domain: domain to allocate from
- * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
- * @nr_irqs: number of IRQs to allocate
- * @node: NUMA node id for memory allocation
- * @arg: domain specific argument
- * @realloc: IRQ descriptors have already been allocated if true
- * @affinity: Optional irq affinity mask for multiqueue devices
- *
- * Allocate IRQ numbers and initialized all data structures to support
- * hierarchy IRQ domains.
- * Parameter @realloc is mainly to support legacy IRQs.
- * Returns error code or allocated IRQ number
- *
- * The whole process to setup an IRQ has been split into two steps.
- * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
- * descriptor and required hardware resources. The second step,
- * irq_domain_activate_irq(), is to program hardwares with preallocated
- * resources. In this way, it's easier to rollback when failing to
- * allocate resources.
- */
- int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
- unsigned int nr_irqs, int node, void *arg,
- bool realloc, const struct cpumask *affinity)
- {
- int i, ret, virq;
- if (domain == NULL) {
- domain = irq_default_domain;
- if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
- return -EINVAL;
- }
- if (realloc && irq_base >= 0) {
- virq = irq_base;
- } else {
- virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
- affinity);
- if (virq < 0) {
- pr_debug("cannot allocate IRQ(base %d, count %d)\n",
- irq_base, nr_irqs);
- return virq;
- }
- }
- if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
- pr_debug("cannot allocate memory for IRQ%d\n", virq);
- ret = -ENOMEM;
- goto out_free_desc;
- }
- mutex_lock(&irq_domain_mutex);
- ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
- if (ret < 0) {
- mutex_unlock(&irq_domain_mutex);
- goto out_free_irq_data;
- }
- for (i = 0; i < nr_irqs; i++)
- irq_domain_insert_irq(virq + i);
- mutex_unlock(&irq_domain_mutex);
- return virq;
- out_free_irq_data:
- irq_domain_free_irq_data(virq, nr_irqs);
- out_free_desc:
- irq_free_descs(virq, nr_irqs);
- return ret;
- }
- /* The irq_data was moved, fix the revmap to refer to the new location */
- static void irq_domain_fix_revmap(struct irq_data *d)
- {
- void __rcu **slot;
- if (d->hwirq < d->domain->revmap_size)
- return; /* Not using radix tree. */
- /* Fix up the revmap. */
- mutex_lock(&revmap_trees_mutex);
- slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
- if (slot)
- radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
- mutex_unlock(&revmap_trees_mutex);
- }
- /**
- * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
- * @domain: Domain to push.
- * @virq: Irq to push the domain in to.
- * @arg: Passed to the irq_domain_ops alloc() function.
- *
- * For an already existing irqdomain hierarchy, as might be obtained
- * via a call to pci_enable_msix(), add an additional domain to the
- * head of the processing chain. Must be called before request_irq()
- * has been called.
- */
- int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
- {
- struct irq_data *child_irq_data;
- struct irq_data *root_irq_data = irq_get_irq_data(virq);
- struct irq_desc *desc;
- int rv = 0;
- /*
- * Check that no action has been set, which indicates the virq
- * is in a state where this function doesn't have to deal with
- * races between interrupt handling and maintaining the
- * hierarchy. This will catch gross misuse. Attempting to
- * make the check race free would require holding locks across
- * calls to struct irq_domain_ops->alloc(), which could lead
- * to deadlock, so we just do a simple check before starting.
- */
- desc = irq_to_desc(virq);
- if (!desc)
- return -EINVAL;
- if (WARN_ON(desc->action))
- return -EBUSY;
- if (domain == NULL)
- return -EINVAL;
- if (WARN_ON(!irq_domain_is_hierarchy(domain)))
- return -EINVAL;
- if (!root_irq_data)
- return -EINVAL;
- if (domain->parent != root_irq_data->domain)
- return -EINVAL;
- child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
- irq_data_get_node(root_irq_data));
- if (!child_irq_data)
- return -ENOMEM;
- mutex_lock(&irq_domain_mutex);
- /* Copy the original irq_data. */
- *child_irq_data = *root_irq_data;
- /*
- * Overwrite the root_irq_data, which is embedded in struct
- * irq_desc, with values for this domain.
- */
- root_irq_data->parent_data = child_irq_data;
- root_irq_data->domain = domain;
- root_irq_data->mask = 0;
- root_irq_data->hwirq = 0;
- root_irq_data->chip = NULL;
- root_irq_data->chip_data = NULL;
- /* May (probably does) set hwirq, chip, etc. */
- rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
- if (rv) {
- /* Restore the original irq_data. */
- *root_irq_data = *child_irq_data;
- kfree(child_irq_data);
- goto error;
- }
- irq_domain_fix_revmap(child_irq_data);
- irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
- error:
- mutex_unlock(&irq_domain_mutex);
- return rv;
- }
- EXPORT_SYMBOL_GPL(irq_domain_push_irq);
- /**
- * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
- * @domain: Domain to remove.
- * @virq: Irq to remove the domain from.
- *
- * Undo the effects of a call to irq_domain_push_irq(). Must be
- * called either before request_irq() or after free_irq().
- */
- int irq_domain_pop_irq(struct irq_domain *domain, int virq)
- {
- struct irq_data *root_irq_data = irq_get_irq_data(virq);
- struct irq_data *child_irq_data;
- struct irq_data *tmp_irq_data;
- struct irq_desc *desc;
- /*
- * Check that no action is set, which indicates the virq is in
- * a state where this function doesn't have to deal with races
- * between interrupt handling and maintaining the hierarchy.
- * This will catch gross misuse. Attempting to make the check
- * race free would require holding locks across calls to
- * struct irq_domain_ops->free(), which could lead to
- * deadlock, so we just do a simple check before starting.
- */
- desc = irq_to_desc(virq);
- if (!desc)
- return -EINVAL;
- if (WARN_ON(desc->action))
- return -EBUSY;
- if (domain == NULL)
- return -EINVAL;
- if (!root_irq_data)
- return -EINVAL;
- tmp_irq_data = irq_domain_get_irq_data(domain, virq);
- /* We can only "pop" if this domain is at the top of the list */
- if (WARN_ON(root_irq_data != tmp_irq_data))
- return -EINVAL;
- if (WARN_ON(root_irq_data->domain != domain))
- return -EINVAL;
- child_irq_data = root_irq_data->parent_data;
- if (WARN_ON(!child_irq_data))
- return -EINVAL;
- mutex_lock(&irq_domain_mutex);
- root_irq_data->parent_data = NULL;
- irq_domain_clear_mapping(domain, root_irq_data->hwirq);
- irq_domain_free_irqs_hierarchy(domain, virq, 1);
- /* Restore the original irq_data. */
- *root_irq_data = *child_irq_data;
- irq_domain_fix_revmap(root_irq_data);
- mutex_unlock(&irq_domain_mutex);
- kfree(child_irq_data);
- return 0;
- }
- EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
- /**
- * irq_domain_free_irqs - Free IRQ number and associated data structures
- * @virq: base IRQ number
- * @nr_irqs: number of IRQs to free
- */
- void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
- {
- struct irq_data *data = irq_get_irq_data(virq);
- int i;
- if (WARN(!data || !data->domain || !data->domain->ops->free,
- "NULL pointer, cannot free irq\n"))
- return;
- mutex_lock(&irq_domain_mutex);
- for (i = 0; i < nr_irqs; i++)
- irq_domain_remove_irq(virq + i);
- irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
- mutex_unlock(&irq_domain_mutex);
- irq_domain_free_irq_data(virq, nr_irqs);
- irq_free_descs(virq, nr_irqs);
- }
- /**
- * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
- * @irq_base: Base IRQ number
- * @nr_irqs: Number of IRQs to allocate
- * @arg: Allocation data (arch/domain specific)
- *
- * Check whether the domain has been setup recursive. If not allocate
- * through the parent domain.
- */
- int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
- unsigned int irq_base, unsigned int nr_irqs,
- void *arg)
- {
- if (!domain->parent)
- return -ENOSYS;
- return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
- nr_irqs, arg);
- }
- EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
- /**
- * irq_domain_free_irqs_parent - Free interrupts from parent domain
- * @irq_base: Base IRQ number
- * @nr_irqs: Number of IRQs to free
- *
- * Check whether the domain has been setup recursive. If not free
- * through the parent domain.
- */
- void irq_domain_free_irqs_parent(struct irq_domain *domain,
- unsigned int irq_base, unsigned int nr_irqs)
- {
- if (!domain->parent)
- return;
- irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
- }
- EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
- static void __irq_domain_activate_irq(struct irq_data *irq_data)
- {
- if (irq_data && irq_data->domain) {
- struct irq_domain *domain = irq_data->domain;
- if (irq_data->parent_data)
- __irq_domain_activate_irq(irq_data->parent_data);
- if (domain->ops->activate)
- domain->ops->activate(domain, irq_data);
- }
- }
- static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
- {
- if (irq_data && irq_data->domain) {
- struct irq_domain *domain = irq_data->domain;
- if (domain->ops->deactivate)
- domain->ops->deactivate(domain, irq_data);
- if (irq_data->parent_data)
- __irq_domain_deactivate_irq(irq_data->parent_data);
- }
- }
- /**
- * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
- * interrupt
- * @irq_data: outermost irq_data associated with interrupt
- *
- * This is the second step to call domain_ops->activate to program interrupt
- * controllers, so the interrupt could actually get delivered.
- */
- void irq_domain_activate_irq(struct irq_data *irq_data)
- {
- if (!irqd_is_activated(irq_data)) {
- __irq_domain_activate_irq(irq_data);
- irqd_set_activated(irq_data);
- }
- }
- /**
- * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
- * deactivate interrupt
- * @irq_data: outermost irq_data associated with interrupt
- *
- * It calls domain_ops->deactivate to program interrupt controllers to disable
- * interrupt delivery.
- */
- void irq_domain_deactivate_irq(struct irq_data *irq_data)
- {
- if (irqd_is_activated(irq_data)) {
- __irq_domain_deactivate_irq(irq_data);
- irqd_clr_activated(irq_data);
- }
- }
- static void irq_domain_check_hierarchy(struct irq_domain *domain)
- {
- /* Hierarchy irq_domains must implement callback alloc() */
- if (domain->ops->alloc)
- domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
- }
- /**
- * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
- * parent has MSI remapping support
- * @domain: domain pointer
- */
- bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
- {
- for (; domain; domain = domain->parent) {
- if (irq_domain_is_msi_remap(domain))
- return true;
- }
- return false;
- }
- #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
- /**
- * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
- * @domain: domain to match
- * @virq: IRQ number to get irq_data
- */
- struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
- unsigned int virq)
- {
- struct irq_data *irq_data = irq_get_irq_data(virq);
- return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
- }
- EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
- /**
- * irq_domain_set_info - Set the complete data for a @virq in @domain
- * @domain: Interrupt domain to match
- * @virq: IRQ number
- * @hwirq: The hardware interrupt number
- * @chip: The associated interrupt chip
- * @chip_data: The associated interrupt chip data
- * @handler: The interrupt flow handler
- * @handler_data: The interrupt flow handler data
- * @handler_name: The interrupt handler name
- */
- void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
- irq_hw_number_t hwirq, struct irq_chip *chip,
- void *chip_data, irq_flow_handler_t handler,
- void *handler_data, const char *handler_name)
- {
- irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
- irq_set_chip_data(virq, chip_data);
- irq_set_handler_data(virq, handler_data);
- }
- static void irq_domain_check_hierarchy(struct irq_domain *domain)
- {
- }
- #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
- #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
- static struct dentry *domain_dir;
- static void
- irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
- {
- seq_printf(m, "%*sname: %s\n", ind, "", d->name);
- seq_printf(m, "%*ssize: %u\n", ind + 1, "",
- d->revmap_size + d->revmap_direct_max_irq);
- seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
- seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
- #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
- if (!d->parent)
- return;
- seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
- irq_domain_debug_show_one(m, d->parent, ind + 4);
- #endif
- }
- static int irq_domain_debug_show(struct seq_file *m, void *p)
- {
- struct irq_domain *d = m->private;
- /* Default domain? Might be NULL */
- if (!d) {
- if (!irq_default_domain)
- return 0;
- d = irq_default_domain;
- }
- irq_domain_debug_show_one(m, d, 0);
- return 0;
- }
- static int irq_domain_debug_open(struct inode *inode, struct file *file)
- {
- return single_open(file, irq_domain_debug_show, inode->i_private);
- }
- static const struct file_operations dfs_domain_ops = {
- .open = irq_domain_debug_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static void debugfs_add_domain_dir(struct irq_domain *d)
- {
- if (!d->name || !domain_dir || d->debugfs_file)
- return;
- d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
- &dfs_domain_ops);
- }
- static void debugfs_remove_domain_dir(struct irq_domain *d)
- {
- debugfs_remove(d->debugfs_file);
- }
- void __init irq_domain_debugfs_init(struct dentry *root)
- {
- struct irq_domain *d;
- domain_dir = debugfs_create_dir("domains", root);
- if (!domain_dir)
- return;
- debugfs_create_file("default", 0444, domain_dir, NULL, &dfs_domain_ops);
- mutex_lock(&irq_domain_mutex);
- list_for_each_entry(d, &irq_domain_list, link)
- debugfs_add_domain_dir(d);
- mutex_unlock(&irq_domain_mutex);
- }
- #endif
|