irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * This module supports the iSeries PCI bus interrupt handling
  3. * Copyright (C) 20yy <Robert L Holtorf> <IBM Corp>
  4. * Copyright (C) 2004-2005 IBM Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the:
  18. * Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330,
  20. * Boston, MA 02111-1307 USA
  21. *
  22. * Change Activity:
  23. * Created, December 13, 2000 by Wayne Holm
  24. * End Change Activity
  25. */
  26. #include <linux/pci.h>
  27. #include <linux/init.h>
  28. #include <linux/threads.h>
  29. #include <linux/smp.h>
  30. #include <linux/param.h>
  31. #include <linux/string.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/irq.h>
  34. #include <linux/spinlock.h>
  35. #include <asm/paca.h>
  36. #include <asm/iseries/hv_types.h>
  37. #include <asm/iseries/hv_lp_event.h>
  38. #include <asm/iseries/hv_call_xm.h>
  39. #include <asm/iseries/it_lp_queue.h>
  40. #include "irq.h"
  41. #include "pci.h"
  42. #include "call_pci.h"
  43. #ifdef CONFIG_PCI
  44. enum pci_event_type {
  45. pe_bus_created = 0, /* PHB has been created */
  46. pe_bus_error = 1, /* PHB has failed */
  47. pe_bus_failed = 2, /* Msg to Secondary, Primary failed bus */
  48. pe_node_failed = 4, /* Multi-adapter bridge has failed */
  49. pe_node_recovered = 5, /* Multi-adapter bridge has recovered */
  50. pe_bus_recovered = 12, /* PHB has been recovered */
  51. pe_unquiese_bus = 18, /* Secondary bus unqiescing */
  52. pe_bridge_error = 21, /* Bridge Error */
  53. pe_slot_interrupt = 22 /* Slot interrupt */
  54. };
  55. struct pci_event {
  56. struct HvLpEvent event;
  57. union {
  58. u64 __align; /* Align on an 8-byte boundary */
  59. struct {
  60. u32 fisr;
  61. HvBusNumber bus_number;
  62. HvSubBusNumber sub_bus_number;
  63. HvAgentId dev_id;
  64. } slot;
  65. struct {
  66. HvBusNumber bus_number;
  67. HvSubBusNumber sub_bus_number;
  68. } bus;
  69. struct {
  70. HvBusNumber bus_number;
  71. HvSubBusNumber sub_bus_number;
  72. HvAgentId dev_id;
  73. } node;
  74. } data;
  75. };
  76. static DEFINE_SPINLOCK(pending_irqs_lock);
  77. static int num_pending_irqs;
  78. static int pending_irqs[NR_IRQS];
  79. static void int_received(struct pci_event *event)
  80. {
  81. int irq;
  82. switch (event->event.xSubtype) {
  83. case pe_slot_interrupt:
  84. irq = event->event.xCorrelationToken;
  85. if (irq < NR_IRQS) {
  86. spin_lock(&pending_irqs_lock);
  87. pending_irqs[irq]++;
  88. num_pending_irqs++;
  89. spin_unlock(&pending_irqs_lock);
  90. } else {
  91. printk(KERN_WARNING "int_received: bad irq number %d\n",
  92. irq);
  93. HvCallPci_eoi(event->data.slot.bus_number,
  94. event->data.slot.sub_bus_number,
  95. event->data.slot.dev_id);
  96. }
  97. break;
  98. /* Ignore error recovery events for now */
  99. case pe_bus_created:
  100. printk(KERN_INFO "int_received: system bus %d created\n",
  101. event->data.bus.bus_number);
  102. break;
  103. case pe_bus_error:
  104. case pe_bus_failed:
  105. printk(KERN_INFO "int_received: system bus %d failed\n",
  106. event->data.bus.bus_number);
  107. break;
  108. case pe_bus_recovered:
  109. case pe_unquiese_bus:
  110. printk(KERN_INFO "int_received: system bus %d recovered\n",
  111. event->data.bus.bus_number);
  112. break;
  113. case pe_node_failed:
  114. case pe_bridge_error:
  115. printk(KERN_INFO
  116. "int_received: multi-adapter bridge %d/%d/%d failed\n",
  117. event->data.node.bus_number,
  118. event->data.node.sub_bus_number,
  119. event->data.node.dev_id);
  120. break;
  121. case pe_node_recovered:
  122. printk(KERN_INFO
  123. "int_received: multi-adapter bridge %d/%d/%d recovered\n",
  124. event->data.node.bus_number,
  125. event->data.node.sub_bus_number,
  126. event->data.node.dev_id);
  127. break;
  128. default:
  129. printk(KERN_ERR
  130. "int_received: unrecognized event subtype 0x%x\n",
  131. event->event.xSubtype);
  132. break;
  133. }
  134. }
  135. static void pci_event_handler(struct HvLpEvent *event)
  136. {
  137. if (event && (event->xType == HvLpEvent_Type_PciIo)) {
  138. if (hvlpevent_is_int(event))
  139. int_received((struct pci_event *)event);
  140. else
  141. printk(KERN_ERR
  142. "pci_event_handler: unexpected ack received\n");
  143. } else if (event)
  144. printk(KERN_ERR
  145. "pci_event_handler: Unrecognized PCI event type 0x%x\n",
  146. (int)event->xType);
  147. else
  148. printk(KERN_ERR "pci_event_handler: NULL event received\n");
  149. }
  150. #define REAL_IRQ_TO_SUBBUS(irq) (((irq) >> 14) & 0xff)
  151. #define REAL_IRQ_TO_BUS(irq) ((((irq) >> 6) & 0xff) + 1)
  152. #define REAL_IRQ_TO_IDSEL(irq) ((((irq) >> 3) & 7) + 1)
  153. #define REAL_IRQ_TO_FUNC(irq) ((irq) & 7)
  154. /*
  155. * This will be called by device drivers (via enable_IRQ)
  156. * to enable INTA in the bridge interrupt status register.
  157. */
  158. static void iseries_enable_IRQ(struct irq_data *d)
  159. {
  160. u32 bus, dev_id, function, mask;
  161. const u32 sub_bus = 0;
  162. unsigned int rirq = (unsigned int)irqd_to_hwirq(d);
  163. /* The IRQ has already been locked by the caller */
  164. bus = REAL_IRQ_TO_BUS(rirq);
  165. function = REAL_IRQ_TO_FUNC(rirq);
  166. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  167. /* Unmask secondary INTA */
  168. mask = 0x80000000;
  169. HvCallPci_unmaskInterrupts(bus, sub_bus, dev_id, mask);
  170. }
  171. /* This is called by iseries_activate_IRQs */
  172. static unsigned int iseries_startup_IRQ(struct irq_data *d)
  173. {
  174. u32 bus, dev_id, function, mask;
  175. const u32 sub_bus = 0;
  176. unsigned int rirq = (unsigned int)irqd_to_hwirq(d);
  177. bus = REAL_IRQ_TO_BUS(rirq);
  178. function = REAL_IRQ_TO_FUNC(rirq);
  179. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  180. /* Link the IRQ number to the bridge */
  181. HvCallXm_connectBusUnit(bus, sub_bus, dev_id, d->irq);
  182. /* Unmask bridge interrupts in the FISR */
  183. mask = 0x01010000 << function;
  184. HvCallPci_unmaskFisr(bus, sub_bus, dev_id, mask);
  185. iseries_enable_IRQ(d);
  186. return 0;
  187. }
  188. /*
  189. * This is called out of iSeries_fixup to activate interrupt
  190. * generation for usable slots
  191. */
  192. void __init iSeries_activate_IRQs()
  193. {
  194. int irq;
  195. unsigned long flags;
  196. for_each_irq (irq) {
  197. struct irq_desc *desc = irq_to_desc(irq);
  198. struct irq_chip *chip;
  199. if (!desc)
  200. continue;
  201. chip = irq_desc_get_chip(desc);
  202. if (chip && chip->irq_startup) {
  203. raw_spin_lock_irqsave(&desc->lock, flags);
  204. chip->irq_startup(&desc->irq_data);
  205. raw_spin_unlock_irqrestore(&desc->lock, flags);
  206. }
  207. }
  208. }
  209. /* this is not called anywhere currently */
  210. static void iseries_shutdown_IRQ(struct irq_data *d)
  211. {
  212. u32 bus, dev_id, function, mask;
  213. const u32 sub_bus = 0;
  214. unsigned int rirq = (unsigned int)irqd_to_hwirq(d);
  215. /* irq should be locked by the caller */
  216. bus = REAL_IRQ_TO_BUS(rirq);
  217. function = REAL_IRQ_TO_FUNC(rirq);
  218. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  219. /* Invalidate the IRQ number in the bridge */
  220. HvCallXm_connectBusUnit(bus, sub_bus, dev_id, 0);
  221. /* Mask bridge interrupts in the FISR */
  222. mask = 0x01010000 << function;
  223. HvCallPci_maskFisr(bus, sub_bus, dev_id, mask);
  224. }
  225. /*
  226. * This will be called by device drivers (via disable_IRQ)
  227. * to disable INTA in the bridge interrupt status register.
  228. */
  229. static void iseries_disable_IRQ(struct irq_data *d)
  230. {
  231. u32 bus, dev_id, function, mask;
  232. const u32 sub_bus = 0;
  233. unsigned int rirq = (unsigned int)irqd_to_hwirq(d);
  234. /* The IRQ has already been locked by the caller */
  235. bus = REAL_IRQ_TO_BUS(rirq);
  236. function = REAL_IRQ_TO_FUNC(rirq);
  237. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  238. /* Mask secondary INTA */
  239. mask = 0x80000000;
  240. HvCallPci_maskInterrupts(bus, sub_bus, dev_id, mask);
  241. }
  242. static void iseries_end_IRQ(struct irq_data *d)
  243. {
  244. unsigned int rirq = (unsigned int)irqd_to_hwirq(d);
  245. HvCallPci_eoi(REAL_IRQ_TO_BUS(rirq), REAL_IRQ_TO_SUBBUS(rirq),
  246. (REAL_IRQ_TO_IDSEL(rirq) << 4) + REAL_IRQ_TO_FUNC(rirq));
  247. }
  248. static struct irq_chip iseries_pic = {
  249. .name = "iSeries",
  250. .irq_startup = iseries_startup_IRQ,
  251. .irq_shutdown = iseries_shutdown_IRQ,
  252. .irq_unmask = iseries_enable_IRQ,
  253. .irq_mask = iseries_disable_IRQ,
  254. .irq_eoi = iseries_end_IRQ
  255. };
  256. /*
  257. * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
  258. * It calculates the irq value for the slot.
  259. * Note that sub_bus is always 0 (at the moment at least).
  260. */
  261. int __init iSeries_allocate_IRQ(HvBusNumber bus,
  262. HvSubBusNumber sub_bus, u32 bsubbus)
  263. {
  264. unsigned int realirq;
  265. u8 idsel = ISERIES_GET_DEVICE_FROM_SUBBUS(bsubbus);
  266. u8 function = ISERIES_GET_FUNCTION_FROM_SUBBUS(bsubbus);
  267. realirq = (((((sub_bus << 8) + (bus - 1)) << 3) + (idsel - 1)) << 3)
  268. + function;
  269. return irq_create_mapping(NULL, realirq);
  270. }
  271. #endif /* CONFIG_PCI */
  272. /*
  273. * Get the next pending IRQ.
  274. */
  275. unsigned int iSeries_get_irq(void)
  276. {
  277. int irq = NO_IRQ_IGNORE;
  278. #ifdef CONFIG_SMP
  279. if (get_lppaca()->int_dword.fields.ipi_cnt) {
  280. get_lppaca()->int_dword.fields.ipi_cnt = 0;
  281. smp_ipi_demux();
  282. }
  283. #endif /* CONFIG_SMP */
  284. if (hvlpevent_is_pending())
  285. process_hvlpevents();
  286. #ifdef CONFIG_PCI
  287. if (num_pending_irqs) {
  288. spin_lock(&pending_irqs_lock);
  289. for (irq = 0; irq < NR_IRQS; irq++) {
  290. if (pending_irqs[irq]) {
  291. pending_irqs[irq]--;
  292. num_pending_irqs--;
  293. break;
  294. }
  295. }
  296. spin_unlock(&pending_irqs_lock);
  297. if (irq >= NR_IRQS)
  298. irq = NO_IRQ_IGNORE;
  299. }
  300. #endif
  301. return irq;
  302. }
  303. #ifdef CONFIG_PCI
  304. static int iseries_irq_host_map(struct irq_host *h, unsigned int virq,
  305. irq_hw_number_t hw)
  306. {
  307. irq_set_chip_and_handler(virq, &iseries_pic, handle_fasteoi_irq);
  308. return 0;
  309. }
  310. static int iseries_irq_host_match(struct irq_host *h, struct device_node *np)
  311. {
  312. /* Match all */
  313. return 1;
  314. }
  315. static struct irq_host_ops iseries_irq_host_ops = {
  316. .map = iseries_irq_host_map,
  317. .match = iseries_irq_host_match,
  318. };
  319. /*
  320. * This is called by init_IRQ. set in ppc_md.init_IRQ by iSeries_setup.c
  321. * It must be called before the bus walk.
  322. */
  323. void __init iSeries_init_IRQ(void)
  324. {
  325. /* Register PCI event handler and open an event path */
  326. struct irq_host *host;
  327. int ret;
  328. /*
  329. * The Hypervisor only allows us up to 256 interrupt
  330. * sources (the irq number is passed in a u8).
  331. */
  332. irq_set_virq_count(256);
  333. /* Create irq host. No need for a revmap since HV will give us
  334. * back our virtual irq number
  335. */
  336. host = irq_alloc_host(NULL, IRQ_HOST_MAP_NOMAP, 0,
  337. &iseries_irq_host_ops, 0);
  338. BUG_ON(host == NULL);
  339. irq_set_default_host(host);
  340. ret = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
  341. &pci_event_handler);
  342. if (ret == 0) {
  343. ret = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
  344. if (ret != 0)
  345. printk(KERN_ERR "iseries_init_IRQ: open event path "
  346. "failed with rc 0x%x\n", ret);
  347. } else
  348. printk(KERN_ERR "iseries_init_IRQ: register handler "
  349. "failed with rc 0x%x\n", ret);
  350. }
  351. #endif /* CONFIG_PCI */