irq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #include "smp.h"
  44. #ifdef CONFIG_PCI
  45. enum pci_event_type {
  46. pe_bus_created = 0, /* PHB has been created */
  47. pe_bus_error = 1, /* PHB has failed */
  48. pe_bus_failed = 2, /* Msg to Secondary, Primary failed bus */
  49. pe_node_failed = 4, /* Multi-adapter bridge has failed */
  50. pe_node_recovered = 5, /* Multi-adapter bridge has recovered */
  51. pe_bus_recovered = 12, /* PHB has been recovered */
  52. pe_unquiese_bus = 18, /* Secondary bus unqiescing */
  53. pe_bridge_error = 21, /* Bridge Error */
  54. pe_slot_interrupt = 22 /* Slot interrupt */
  55. };
  56. struct pci_event {
  57. struct HvLpEvent event;
  58. union {
  59. u64 __align; /* Align on an 8-byte boundary */
  60. struct {
  61. u32 fisr;
  62. HvBusNumber bus_number;
  63. HvSubBusNumber sub_bus_number;
  64. HvAgentId dev_id;
  65. } slot;
  66. struct {
  67. HvBusNumber bus_number;
  68. HvSubBusNumber sub_bus_number;
  69. } bus;
  70. struct {
  71. HvBusNumber bus_number;
  72. HvSubBusNumber sub_bus_number;
  73. HvAgentId dev_id;
  74. } node;
  75. } data;
  76. };
  77. static DEFINE_SPINLOCK(pending_irqs_lock);
  78. static int num_pending_irqs;
  79. static int pending_irqs[NR_IRQS];
  80. static void int_received(struct pci_event *event)
  81. {
  82. int irq;
  83. switch (event->event.xSubtype) {
  84. case pe_slot_interrupt:
  85. irq = event->event.xCorrelationToken;
  86. if (irq < NR_IRQS) {
  87. spin_lock(&pending_irqs_lock);
  88. pending_irqs[irq]++;
  89. num_pending_irqs++;
  90. spin_unlock(&pending_irqs_lock);
  91. } else {
  92. printk(KERN_WARNING "int_received: bad irq number %d\n",
  93. irq);
  94. HvCallPci_eoi(event->data.slot.bus_number,
  95. event->data.slot.sub_bus_number,
  96. event->data.slot.dev_id);
  97. }
  98. break;
  99. /* Ignore error recovery events for now */
  100. case pe_bus_created:
  101. printk(KERN_INFO "int_received: system bus %d created\n",
  102. event->data.bus.bus_number);
  103. break;
  104. case pe_bus_error:
  105. case pe_bus_failed:
  106. printk(KERN_INFO "int_received: system bus %d failed\n",
  107. event->data.bus.bus_number);
  108. break;
  109. case pe_bus_recovered:
  110. case pe_unquiese_bus:
  111. printk(KERN_INFO "int_received: system bus %d recovered\n",
  112. event->data.bus.bus_number);
  113. break;
  114. case pe_node_failed:
  115. case pe_bridge_error:
  116. printk(KERN_INFO
  117. "int_received: multi-adapter bridge %d/%d/%d failed\n",
  118. event->data.node.bus_number,
  119. event->data.node.sub_bus_number,
  120. event->data.node.dev_id);
  121. break;
  122. case pe_node_recovered:
  123. printk(KERN_INFO
  124. "int_received: multi-adapter bridge %d/%d/%d recovered\n",
  125. event->data.node.bus_number,
  126. event->data.node.sub_bus_number,
  127. event->data.node.dev_id);
  128. break;
  129. default:
  130. printk(KERN_ERR
  131. "int_received: unrecognized event subtype 0x%x\n",
  132. event->event.xSubtype);
  133. break;
  134. }
  135. }
  136. static void pci_event_handler(struct HvLpEvent *event)
  137. {
  138. if (event && (event->xType == HvLpEvent_Type_PciIo)) {
  139. if (hvlpevent_is_int(event))
  140. int_received((struct pci_event *)event);
  141. else
  142. printk(KERN_ERR
  143. "pci_event_handler: unexpected ack received\n");
  144. } else if (event)
  145. printk(KERN_ERR
  146. "pci_event_handler: Unrecognized PCI event type 0x%x\n",
  147. (int)event->xType);
  148. else
  149. printk(KERN_ERR "pci_event_handler: NULL event received\n");
  150. }
  151. #define REAL_IRQ_TO_SUBBUS(irq) (((irq) >> 14) & 0xff)
  152. #define REAL_IRQ_TO_BUS(irq) ((((irq) >> 6) & 0xff) + 1)
  153. #define REAL_IRQ_TO_IDSEL(irq) ((((irq) >> 3) & 7) + 1)
  154. #define REAL_IRQ_TO_FUNC(irq) ((irq) & 7)
  155. /*
  156. * This will be called by device drivers (via enable_IRQ)
  157. * to enable INTA in the bridge interrupt status register.
  158. */
  159. static void iseries_enable_IRQ(unsigned int irq)
  160. {
  161. u32 bus, dev_id, function, mask;
  162. const u32 sub_bus = 0;
  163. unsigned int rirq = (unsigned int)irq_map[irq].hwirq;
  164. /* The IRQ has already been locked by the caller */
  165. bus = REAL_IRQ_TO_BUS(rirq);
  166. function = REAL_IRQ_TO_FUNC(rirq);
  167. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  168. /* Unmask secondary INTA */
  169. mask = 0x80000000;
  170. HvCallPci_unmaskInterrupts(bus, sub_bus, dev_id, mask);
  171. }
  172. /* This is called by iseries_activate_IRQs */
  173. static unsigned int iseries_startup_IRQ(unsigned int irq)
  174. {
  175. u32 bus, dev_id, function, mask;
  176. const u32 sub_bus = 0;
  177. unsigned int rirq = (unsigned int)irq_map[irq].hwirq;
  178. bus = REAL_IRQ_TO_BUS(rirq);
  179. function = REAL_IRQ_TO_FUNC(rirq);
  180. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  181. /* Link the IRQ number to the bridge */
  182. HvCallXm_connectBusUnit(bus, sub_bus, dev_id, irq);
  183. /* Unmask bridge interrupts in the FISR */
  184. mask = 0x01010000 << function;
  185. HvCallPci_unmaskFisr(bus, sub_bus, dev_id, mask);
  186. iseries_enable_IRQ(irq);
  187. return 0;
  188. }
  189. /*
  190. * This is called out of iSeries_fixup to activate interrupt
  191. * generation for usable slots
  192. */
  193. void __init iSeries_activate_IRQs()
  194. {
  195. int irq;
  196. unsigned long flags;
  197. for_each_irq (irq) {
  198. irq_desc_t *desc = get_irq_desc(irq);
  199. if (desc && desc->chip && desc->chip->startup) {
  200. spin_lock_irqsave(&desc->lock, flags);
  201. desc->chip->startup(irq);
  202. spin_unlock_irqrestore(&desc->lock, flags);
  203. }
  204. }
  205. }
  206. /* this is not called anywhere currently */
  207. static void iseries_shutdown_IRQ(unsigned int irq)
  208. {
  209. u32 bus, dev_id, function, mask;
  210. const u32 sub_bus = 0;
  211. unsigned int rirq = (unsigned int)irq_map[irq].hwirq;
  212. /* irq should be locked by the caller */
  213. bus = REAL_IRQ_TO_BUS(rirq);
  214. function = REAL_IRQ_TO_FUNC(rirq);
  215. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  216. /* Invalidate the IRQ number in the bridge */
  217. HvCallXm_connectBusUnit(bus, sub_bus, dev_id, 0);
  218. /* Mask bridge interrupts in the FISR */
  219. mask = 0x01010000 << function;
  220. HvCallPci_maskFisr(bus, sub_bus, dev_id, mask);
  221. }
  222. /*
  223. * This will be called by device drivers (via disable_IRQ)
  224. * to disable INTA in the bridge interrupt status register.
  225. */
  226. static void iseries_disable_IRQ(unsigned int irq)
  227. {
  228. u32 bus, dev_id, function, mask;
  229. const u32 sub_bus = 0;
  230. unsigned int rirq = (unsigned int)irq_map[irq].hwirq;
  231. /* The IRQ has already been locked by the caller */
  232. bus = REAL_IRQ_TO_BUS(rirq);
  233. function = REAL_IRQ_TO_FUNC(rirq);
  234. dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
  235. /* Mask secondary INTA */
  236. mask = 0x80000000;
  237. HvCallPci_maskInterrupts(bus, sub_bus, dev_id, mask);
  238. }
  239. static void iseries_end_IRQ(unsigned int irq)
  240. {
  241. unsigned int rirq = (unsigned int)irq_map[irq].hwirq;
  242. HvCallPci_eoi(REAL_IRQ_TO_BUS(rirq), REAL_IRQ_TO_SUBBUS(rirq),
  243. (REAL_IRQ_TO_IDSEL(rirq) << 4) + REAL_IRQ_TO_FUNC(rirq));
  244. }
  245. static struct irq_chip iseries_pic = {
  246. .typename = "iSeries irq controller",
  247. .startup = iseries_startup_IRQ,
  248. .shutdown = iseries_shutdown_IRQ,
  249. .unmask = iseries_enable_IRQ,
  250. .mask = iseries_disable_IRQ,
  251. .eoi = iseries_end_IRQ
  252. };
  253. /*
  254. * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
  255. * It calculates the irq value for the slot.
  256. * Note that sub_bus is always 0 (at the moment at least).
  257. */
  258. int __init iSeries_allocate_IRQ(HvBusNumber bus,
  259. HvSubBusNumber sub_bus, u32 bsubbus)
  260. {
  261. unsigned int realirq;
  262. u8 idsel = ISERIES_GET_DEVICE_FROM_SUBBUS(bsubbus);
  263. u8 function = ISERIES_GET_FUNCTION_FROM_SUBBUS(bsubbus);
  264. realirq = (((((sub_bus << 8) + (bus - 1)) << 3) + (idsel - 1)) << 3)
  265. + function;
  266. return irq_create_mapping(NULL, realirq);
  267. }
  268. #endif /* CONFIG_PCI */
  269. /*
  270. * Get the next pending IRQ.
  271. */
  272. unsigned int iSeries_get_irq(void)
  273. {
  274. int irq = NO_IRQ_IGNORE;
  275. #ifdef CONFIG_SMP
  276. if (get_lppaca()->int_dword.fields.ipi_cnt) {
  277. get_lppaca()->int_dword.fields.ipi_cnt = 0;
  278. iSeries_smp_message_recv();
  279. }
  280. #endif /* CONFIG_SMP */
  281. if (hvlpevent_is_pending())
  282. process_hvlpevents();
  283. #ifdef CONFIG_PCI
  284. if (num_pending_irqs) {
  285. spin_lock(&pending_irqs_lock);
  286. for (irq = 0; irq < NR_IRQS; irq++) {
  287. if (pending_irqs[irq]) {
  288. pending_irqs[irq]--;
  289. num_pending_irqs--;
  290. break;
  291. }
  292. }
  293. spin_unlock(&pending_irqs_lock);
  294. if (irq >= NR_IRQS)
  295. irq = NO_IRQ_IGNORE;
  296. }
  297. #endif
  298. return irq;
  299. }
  300. #ifdef CONFIG_PCI
  301. static int iseries_irq_host_map(struct irq_host *h, unsigned int virq,
  302. irq_hw_number_t hw)
  303. {
  304. set_irq_chip_and_handler(virq, &iseries_pic, handle_fasteoi_irq);
  305. return 0;
  306. }
  307. static int iseries_irq_host_match(struct irq_host *h, struct device_node *np)
  308. {
  309. /* Match all */
  310. return 1;
  311. }
  312. static struct irq_host_ops iseries_irq_host_ops = {
  313. .map = iseries_irq_host_map,
  314. .match = iseries_irq_host_match,
  315. };
  316. /*
  317. * This is called by init_IRQ. set in ppc_md.init_IRQ by iSeries_setup.c
  318. * It must be called before the bus walk.
  319. */
  320. void __init iSeries_init_IRQ(void)
  321. {
  322. /* Register PCI event handler and open an event path */
  323. struct irq_host *host;
  324. int ret;
  325. /*
  326. * The Hypervisor only allows us up to 256 interrupt
  327. * sources (the irq number is passed in a u8).
  328. */
  329. irq_set_virq_count(256);
  330. /* Create irq host. No need for a revmap since HV will give us
  331. * back our virtual irq number
  332. */
  333. host = irq_alloc_host(NULL, IRQ_HOST_MAP_NOMAP, 0,
  334. &iseries_irq_host_ops, 0);
  335. BUG_ON(host == NULL);
  336. irq_set_default_host(host);
  337. ret = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
  338. &pci_event_handler);
  339. if (ret == 0) {
  340. ret = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
  341. if (ret != 0)
  342. printk(KERN_ERR "iseries_init_IRQ: open event path "
  343. "failed with rc 0x%x\n", ret);
  344. } else
  345. printk(KERN_ERR "iseries_init_IRQ: register handler "
  346. "failed with rc 0x%x\n", ret);
  347. }
  348. #endif /* CONFIG_PCI */