*
  * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
  *     Moved from arch/x86/kernel/apic/io_apic.c.
+ * Jiang Liu <jiang.liu@linux.intel.com>
+ *     Enable support of hierarchical irqdomains
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 #include <asm/desc.h>
 #include <asm/irq_remapping.h>
 
+struct irq_domain *x86_vector_domain;
 static DEFINE_RAW_SPINLOCK(vector_lock);
+static struct irq_chip lapic_controller;
 
 void lock_vector_lock(void)
 {
 
 struct irq_cfg *irq_cfg(unsigned int irq)
 {
-       return irq_get_chip_data(irq);
+       return irqd_cfg(irq_get_irq_data(irq));
 }
 
 struct irq_cfg *irqd_cfg(struct irq_data *irq_data)
 {
+       if (!irq_data)
+               return NULL;
+
+       while (irq_data->parent_data)
+               irq_data = irq_data->parent_data;
+
        return irq_data->chip_data;
 }
 
-static struct irq_cfg *alloc_irq_cfg(unsigned int irq, int node)
+static struct irq_cfg *alloc_irq_cfg(int node)
 {
        struct irq_cfg *cfg;
 
                        return cfg;
        }
 
-       cfg = alloc_irq_cfg(at, node);
+       cfg = alloc_irq_cfg(node);
        if (cfg)
                irq_set_chip_data(at, cfg);
        else
        return cfg;
 }
 
-static void free_irq_cfg(unsigned int at, struct irq_cfg *cfg)
+static void free_irq_cfg(struct irq_cfg *cfg)
 {
-       if (!cfg)
-               return;
-       irq_set_chip_data(at, NULL);
-       free_cpumask_var(cfg->domain);
-       free_cpumask_var(cfg->old_domain);
-       kfree(cfg);
+       if (cfg) {
+               free_cpumask_var(cfg->domain);
+               free_cpumask_var(cfg->old_domain);
+               kfree(cfg);
+       }
 }
 
 static int
        raw_spin_unlock_irqrestore(&vector_lock, flags);
 }
 
+void init_irq_alloc_info(struct irq_alloc_info *info,
+                        const struct cpumask *mask)
+{
+       memset(info, 0, sizeof(*info));
+       info->mask = mask;
+}
+
+void copy_irq_alloc_info(struct irq_alloc_info *dst, struct irq_alloc_info *src)
+{
+       if (src)
+               *dst = *src;
+       else
+               memset(dst, 0, sizeof(*dst));
+}
+
+static inline const struct cpumask *
+irq_alloc_info_get_mask(struct irq_alloc_info *info)
+{
+       return (!info || !info->mask) ? apic->target_cpus() : info->mask;
+}
+
+static void x86_vector_free_irqs(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(x86_vector_domain, virq + i);
+               if (irq_data && irq_data->chip_data) {
+                       free_remapped_irq(virq);
+                       clear_irq_vector(virq + i, irq_data->chip_data);
+                       free_irq_cfg(irq_data->chip_data);
+                       irq_domain_reset_irq_data(irq_data);
+               }
+       }
+}
+
+static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
+                                unsigned int nr_irqs, void *arg)
+{
+       struct irq_alloc_info *info = arg;
+       const struct cpumask *mask;
+       struct irq_data *irq_data;
+       struct irq_cfg *cfg;
+       int i, err;
+
+       if (disable_apic)
+               return -ENXIO;
+
+       /* Currently vector allocator can't guarantee contiguous allocations */
+       if ((info->flags & X86_IRQ_ALLOC_CONTIGUOUS_VECTORS) && nr_irqs > 1)
+               return -ENOSYS;
+
+       mask = irq_alloc_info_get_mask(info);
+       for (i = 0; i < nr_irqs; i++) {
+               irq_data = irq_domain_get_irq_data(domain, virq + i);
+               BUG_ON(!irq_data);
+               cfg = alloc_irq_cfg(irq_data->node);
+               if (!cfg) {
+                       err = -ENOMEM;
+                       goto error;
+               }
+
+               irq_data->chip = &lapic_controller;
+               irq_data->chip_data = cfg;
+               irq_data->hwirq = virq + i;
+               err = assign_irq_vector(virq, cfg, mask);
+               if (err)
+                       goto error;
+       }
+
+       return 0;
+
+error:
+       x86_vector_free_irqs(domain, virq, i + 1);
+       return err;
+}
+
+static struct irq_domain_ops x86_vector_domain_ops = {
+       .alloc = x86_vector_alloc_irqs,
+       .free = x86_vector_free_irqs,
+};
+
 int __init arch_probe_nr_irqs(void)
 {
        int nr;
 
 int __init arch_early_irq_init(void)
 {
+       x86_vector_domain = irq_domain_add_tree(NULL, &x86_vector_domain_ops,
+                                               NULL);
+       BUG_ON(x86_vector_domain == NULL);
+       irq_set_default_host(x86_vector_domain);
+
        return arch_early_ioapic_init();
 }
 
        return 0;
 }
 
+static int vector_set_affinity(struct irq_data *irq_data,
+                              const struct cpumask *dest, bool force)
+{
+       struct irq_cfg *cfg = irq_data->chip_data;
+       int err, irq = irq_data->irq;
+
+       if (!config_enabled(CONFIG_SMP))
+               return -EPERM;
+
+       if (!cpumask_intersects(dest, cpu_online_mask))
+               return -EINVAL;
+
+       err = assign_irq_vector(irq, cfg, dest);
+       if (err) {
+               struct irq_data *top = irq_get_irq_data(irq);
+
+               if (assign_irq_vector(irq, cfg, top->affinity))
+                       pr_err("Failed to recover vector for irq %d\n", irq);
+               return err;
+       }
+
+       return IRQ_SET_MASK_OK;
+}
+
+static struct irq_chip lapic_controller = {
+       .irq_ack                = apic_ack_edge,
+       .irq_set_affinity       = vector_set_affinity,
+       .irq_retrigger          = apic_retrigger_irq,
+};
+
 #ifdef CONFIG_SMP
 void send_cleanup_vector(struct irq_cfg *cfg)
 {
        unsigned long flags;
        int ret;
 
-       cfg = alloc_irq_cfg(irq, node);
+       cfg = alloc_irq_cfg(node);
        if (!cfg)
                return -ENOMEM;
 
        if (!ret)
                irq_set_chip_data(irq, cfg);
        else
-               free_irq_cfg(irq, cfg);
+               free_irq_cfg(cfg);
        return ret;
 }
 
 
        free_remapped_irq(irq);
        clear_irq_vector(irq, cfg);
-       free_irq_cfg(irq, cfg);
+       irq_set_chip_data(irq, NULL);
+       free_irq_cfg(cfg);
 }
 
 static void __init print_APIC_field(int base)