blob: 6c89228560493df8d17d6e0f7d1b65d28ee543ad [file] [log] [blame]
Mark Maulefd58e552006-04-10 21:17:48 -05001/*
2 * MSI hooks for standard x86 apic
3 */
4
5#include <linux/pci.h>
6#include <linux/irq.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07007#include <linux/msi.h>
Fenghua Yu62fdd762008-10-17 12:14:13 -07008#include <linux/dmar.h>
Christian Kujaua4cffb62006-06-26 14:00:02 +02009#include <asm/smp.h>
Xiantao Zhang2fa89372009-02-16 15:14:48 +080010#include <asm/msidef.h>
Mark Maulefd58e552006-04-10 21:17:48 -050011
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070012static struct irq_chip ia64_msi_chip;
Mark Maulefd58e552006-04-10 21:17:48 -050013
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070014#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -070015static int ia64_set_msi_irq_affinity(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +103016 const cpumask_t *cpu_mask)
Mark Maulefd58e552006-04-10 21:17:48 -050017{
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070018 struct msi_msg msg;
Yasuaki Ishimatsucd378f12007-07-17 21:22:48 +090019 u32 addr, data;
Rusty Russell0de26522008-12-13 21:20:26 +103020 int cpu = first_cpu(*cpu_mask);
Mark Maulefd58e552006-04-10 21:17:48 -050021
Yasuaki Ishimatsucd378f12007-07-17 21:22:48 +090022 if (!cpu_online(cpu))
Yinghai Lud5dedd42009-04-27 17:59:21 -070023 return -1;
Yasuaki Ishimatsucd378f12007-07-17 21:22:48 +090024
Kenji Kaneshigea6cd63222008-02-25 14:32:22 +090025 if (irq_prepare_move(irq, cpu))
Yinghai Lud5dedd42009-04-27 17:59:21 -070026 return -1;
Yasuaki Ishimatsu4994be12007-07-17 21:22:33 +090027
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070028 read_msi_msg(irq, &msg);
29
30 addr = msg.address_lo;
Xiantao Zhang2fa89372009-02-16 15:14:48 +080031 addr &= MSI_ADDR_DEST_ID_MASK;
32 addr |= MSI_ADDR_DEST_ID_CPU(cpu_physical_id(cpu));
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070033 msg.address_lo = addr;
Mark Maulefd58e552006-04-10 21:17:48 -050034
Yasuaki Ishimatsucd378f12007-07-17 21:22:48 +090035 data = msg.data;
36 data &= MSI_DATA_VECTOR_MASK;
37 data |= MSI_DATA_VECTOR(irq_to_vector(irq));
38 msg.data = data;
39
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070040 write_msi_msg(irq, &msg);
Mike Travise65e49d2009-01-12 15:27:13 -080041 cpumask_copy(irq_desc[irq].affinity, cpumask_of(cpu));
Yinghai Lud5dedd42009-04-27 17:59:21 -070042
43 return 0;
Mark Maulefd58e552006-04-10 21:17:48 -050044}
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070045#endif /* CONFIG_SMP */
Mark Maulefd58e552006-04-10 21:17:48 -050046
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -070047int ia64_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
Mark Maulefd58e552006-04-10 21:17:48 -050048{
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070049 struct msi_msg msg;
Mark Maulefd58e552006-04-10 21:17:48 -050050 unsigned long dest_phys_id;
Kenji Kaneshige8a3a0ee2007-03-26 09:38:42 +090051 int irq, vector;
Yasuaki Ishimatsu4994be12007-07-17 21:22:33 +090052 cpumask_t mask;
Mark Maulefd58e552006-04-10 21:17:48 -050053
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -070054 irq = create_irq();
55 if (irq < 0)
56 return irq;
57
58 set_irq_msi(irq, desc);
Yasuaki Ishimatsu4994be12007-07-17 21:22:33 +090059 cpus_and(mask, irq_to_domain(irq), cpu_online_map);
60 dest_phys_id = cpu_physical_id(first_cpu(mask));
Ishimatsu Yasuaki9438a122007-04-06 16:51:12 +090061 vector = irq_to_vector(irq);
Mark Maulefd58e552006-04-10 21:17:48 -050062
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070063 msg.address_hi = 0;
64 msg.address_lo =
Eric W. Biederman38bc0362006-10-04 02:16:34 -070065 MSI_ADDR_HEADER |
Xiantao Zhang2fa89372009-02-16 15:14:48 +080066 MSI_ADDR_DEST_MODE_PHYS |
Eric W. Biederman38bc0362006-10-04 02:16:34 -070067 MSI_ADDR_REDIRECTION_CPU |
Xiantao Zhang2fa89372009-02-16 15:14:48 +080068 MSI_ADDR_DEST_ID_CPU(dest_phys_id);
Mark Maulefd58e552006-04-10 21:17:48 -050069
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070070 msg.data =
Eric W. Biederman38bc0362006-10-04 02:16:34 -070071 MSI_DATA_TRIGGER_EDGE |
Mark Maulefd58e552006-04-10 21:17:48 -050072 MSI_DATA_LEVEL_ASSERT |
73 MSI_DATA_DELIVERY_FIXED |
74 MSI_DATA_VECTOR(vector);
75
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070076 write_msi_msg(irq, &msg);
77 set_irq_chip_and_handler(irq, &ia64_msi_chip, handle_edge_irq);
78
Kenji Kaneshige3aff0372007-10-30 16:01:49 +090079 return 0;
Mark Maulefd58e552006-04-10 21:17:48 -050080}
81
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070082void ia64_teardown_msi_irq(unsigned int irq)
Mark Maulefd58e552006-04-10 21:17:48 -050083{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -070084 destroy_irq(irq);
Mark Maulefd58e552006-04-10 21:17:48 -050085}
86
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070087static void ia64_ack_msi_irq(unsigned int irq)
88{
Kenji Kaneshigea6cd63222008-02-25 14:32:22 +090089 irq_complete_move(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070090 move_native_irq(irq);
91 ia64_eoi();
92}
Mark Maulefd58e552006-04-10 21:17:48 -050093
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070094static int ia64_msi_retrigger_irq(unsigned int irq)
95{
Ishimatsu Yasuaki9438a122007-04-06 16:51:12 +090096 unsigned int vector = irq_to_vector(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070097 ia64_resend_irq(vector);
98
99 return 1;
100}
101
102/*
103 * Generic ops used on most IA64 platforms.
104 */
105static struct irq_chip ia64_msi_chip = {
106 .name = "PCI-MSI",
107 .mask = mask_msi_irq,
108 .unmask = unmask_msi_irq,
109 .ack = ia64_ack_msi_irq,
110#ifdef CONFIG_SMP
111 .set_affinity = ia64_set_msi_irq_affinity,
112#endif
113 .retrigger = ia64_msi_retrigger_irq,
Mark Maulefd58e552006-04-10 21:17:48 -0500114};
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700115
116
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700117int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700118{
119 if (platform_setup_msi_irq)
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700120 return platform_setup_msi_irq(pdev, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700121
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -0700122 return ia64_setup_msi_irq(pdev, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -0700123}
124
125void arch_teardown_msi_irq(unsigned int irq)
126{
127 if (platform_teardown_msi_irq)
128 return platform_teardown_msi_irq(irq);
129
130 return ia64_teardown_msi_irq(irq);
131}
Fenghua Yu62fdd762008-10-17 12:14:13 -0700132
133#ifdef CONFIG_DMAR
134#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -0700135static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Fenghua Yu62fdd762008-10-17 12:14:13 -0700136{
137 struct irq_cfg *cfg = irq_cfg + irq;
138 struct msi_msg msg;
Rusty Russell0de26522008-12-13 21:20:26 +1030139 int cpu = cpumask_first(mask);
Fenghua Yu62fdd762008-10-17 12:14:13 -0700140
141 if (!cpu_online(cpu))
Yinghai Lud5dedd42009-04-27 17:59:21 -0700142 return -1;
Fenghua Yu62fdd762008-10-17 12:14:13 -0700143
144 if (irq_prepare_move(irq, cpu))
Yinghai Lud5dedd42009-04-27 17:59:21 -0700145 return -1;
Fenghua Yu62fdd762008-10-17 12:14:13 -0700146
147 dmar_msi_read(irq, &msg);
148
149 msg.data &= ~MSI_DATA_VECTOR_MASK;
150 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Xiantao Zhang2fa89372009-02-16 15:14:48 +0800151 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
152 msg.address_lo |= MSI_ADDR_DEST_ID_CPU(cpu_physical_id(cpu));
Fenghua Yu62fdd762008-10-17 12:14:13 -0700153
154 dmar_msi_write(irq, &msg);
Mike Travise65e49d2009-01-12 15:27:13 -0800155 cpumask_copy(irq_desc[irq].affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700156
157 return 0;
Fenghua Yu62fdd762008-10-17 12:14:13 -0700158}
159#endif /* CONFIG_SMP */
160
Jaswinder Singh Rajput9542b212009-06-10 12:45:01 -0700161static struct irq_chip dmar_msi_type = {
Fenghua Yu62fdd762008-10-17 12:14:13 -0700162 .name = "DMAR_MSI",
163 .unmask = dmar_msi_unmask,
164 .mask = dmar_msi_mask,
165 .ack = ia64_ack_msi_irq,
166#ifdef CONFIG_SMP
167 .set_affinity = dmar_msi_set_affinity,
168#endif
169 .retrigger = ia64_msi_retrigger_irq,
170};
171
172static int
173msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
174{
175 struct irq_cfg *cfg = irq_cfg + irq;
176 unsigned dest;
177 cpumask_t mask;
178
179 cpus_and(mask, irq_to_domain(irq), cpu_online_map);
180 dest = cpu_physical_id(first_cpu(mask));
181
182 msg->address_hi = 0;
183 msg->address_lo =
184 MSI_ADDR_HEADER |
Xiantao Zhang2fa89372009-02-16 15:14:48 +0800185 MSI_ADDR_DEST_MODE_PHYS |
Fenghua Yu62fdd762008-10-17 12:14:13 -0700186 MSI_ADDR_REDIRECTION_CPU |
Xiantao Zhang2fa89372009-02-16 15:14:48 +0800187 MSI_ADDR_DEST_ID_CPU(dest);
Fenghua Yu62fdd762008-10-17 12:14:13 -0700188
189 msg->data =
190 MSI_DATA_TRIGGER_EDGE |
191 MSI_DATA_LEVEL_ASSERT |
192 MSI_DATA_DELIVERY_FIXED |
193 MSI_DATA_VECTOR(cfg->vector);
194 return 0;
195}
196
197int arch_setup_dmar_msi(unsigned int irq)
198{
199 int ret;
200 struct msi_msg msg;
201
202 ret = msi_compose_msg(NULL, irq, &msg);
203 if (ret < 0)
204 return ret;
205 dmar_msi_write(irq, &msg);
206 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
207 "edge");
208 return 0;
209}
210#endif /* CONFIG_DMAR */
211