genx2apic_cluster.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <linux/threads.h>
  2. #include <linux/cpumask.h>
  3. #include <linux/string.h>
  4. #include <linux/kernel.h>
  5. #include <linux/ctype.h>
  6. #include <linux/init.h>
  7. #include <asm/smp.h>
  8. #include <asm/ipi.h>
  9. #include <asm/genapic.h>
  10. DEFINE_PER_CPU(u32, x86_cpu_to_logical_apicid);
  11. /* Start with all IRQs pointing to boot CPU. IRQ balancing will shift them. */
  12. static cpumask_t x2apic_target_cpus(void)
  13. {
  14. return cpumask_of_cpu(0);
  15. }
  16. /*
  17. * for now each logical cpu is in its own vector allocation domain.
  18. */
  19. static cpumask_t x2apic_vector_allocation_domain(int cpu)
  20. {
  21. cpumask_t domain = CPU_MASK_NONE;
  22. cpu_set(cpu, domain);
  23. return domain;
  24. }
  25. static void __x2apic_send_IPI_dest(unsigned int apicid, int vector,
  26. unsigned int dest)
  27. {
  28. unsigned long cfg;
  29. cfg = __prepare_ICR(0, vector, dest);
  30. /*
  31. * send the IPI.
  32. */
  33. x2apic_icr_write(cfg, apicid);
  34. }
  35. /*
  36. * for now, we send the IPI's one by one in the cpumask.
  37. * TBD: Based on the cpu mask, we can send the IPI's to the cluster group
  38. * at once. We have 16 cpu's in a cluster. This will minimize IPI register
  39. * writes.
  40. */
  41. static void x2apic_send_IPI_mask(cpumask_t mask, int vector)
  42. {
  43. unsigned long flags;
  44. unsigned long query_cpu;
  45. local_irq_save(flags);
  46. for_each_cpu_mask(query_cpu, mask) {
  47. __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_logical_apicid, query_cpu),
  48. vector, APIC_DEST_LOGICAL);
  49. }
  50. local_irq_restore(flags);
  51. }
  52. static void x2apic_send_IPI_allbutself(int vector)
  53. {
  54. cpumask_t mask = cpu_online_map;
  55. cpu_clear(smp_processor_id(), mask);
  56. if (!cpus_empty(mask))
  57. x2apic_send_IPI_mask(mask, vector);
  58. }
  59. static void x2apic_send_IPI_all(int vector)
  60. {
  61. x2apic_send_IPI_mask(cpu_online_map, vector);
  62. }
  63. static int x2apic_apic_id_registered(void)
  64. {
  65. return 1;
  66. }
  67. static unsigned int x2apic_cpu_mask_to_apicid(cpumask_t cpumask)
  68. {
  69. int cpu;
  70. /*
  71. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  72. * May as well be the first.
  73. */
  74. cpu = first_cpu(cpumask);
  75. if ((unsigned)cpu < NR_CPUS)
  76. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  77. else
  78. return BAD_APICID;
  79. }
  80. static unsigned int get_apic_id(unsigned long x)
  81. {
  82. unsigned int id;
  83. id = x;
  84. return id;
  85. }
  86. static unsigned long set_apic_id(unsigned int id)
  87. {
  88. unsigned long x;
  89. x = id;
  90. return x;
  91. }
  92. static unsigned int x2apic_read_id(void)
  93. {
  94. return apic_read(APIC_ID);
  95. }
  96. static unsigned int phys_pkg_id(int index_msb)
  97. {
  98. return x2apic_read_id() >> index_msb;
  99. }
  100. static void x2apic_send_IPI_self(int vector)
  101. {
  102. apic_write(APIC_SELF_IPI, vector);
  103. }
  104. static void init_x2apic_ldr(void)
  105. {
  106. int cpu = smp_processor_id();
  107. per_cpu(x86_cpu_to_logical_apicid, cpu) = apic_read(APIC_LDR);
  108. return;
  109. }
  110. struct genapic apic_x2apic_cluster = {
  111. .name = "cluster x2apic",
  112. .int_delivery_mode = dest_LowestPrio,
  113. .int_dest_mode = (APIC_DEST_LOGICAL != 0),
  114. .target_cpus = x2apic_target_cpus,
  115. .vector_allocation_domain = x2apic_vector_allocation_domain,
  116. .apic_id_registered = x2apic_apic_id_registered,
  117. .init_apic_ldr = init_x2apic_ldr,
  118. .send_IPI_all = x2apic_send_IPI_all,
  119. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  120. .send_IPI_mask = x2apic_send_IPI_mask,
  121. .send_IPI_self = x2apic_send_IPI_self,
  122. .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid,
  123. .phys_pkg_id = phys_pkg_id,
  124. .get_apic_id = get_apic_id,
  125. .set_apic_id = set_apic_id,
  126. .apic_id_mask = (0xFFFFFFFFu),
  127. };