iosapic.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. ** I/O Sapic Driver - PCI interrupt line support
  3. **
  4. ** (c) Copyright 1999 Grant Grundler
  5. ** (c) Copyright 1999 Hewlett-Packard Company
  6. **
  7. ** This program is free software; you can redistribute it and/or modify
  8. ** it under the terms of the GNU General Public License as published by
  9. ** the Free Software Foundation; either version 2 of the License, or
  10. ** (at your option) any later version.
  11. **
  12. ** The I/O sapic driver manages the Interrupt Redirection Table which is
  13. ** the control logic to convert PCI line based interrupts into a Message
  14. ** Signaled Interrupt (aka Transaction Based Interrupt, TBI).
  15. **
  16. ** Acronyms
  17. ** --------
  18. ** HPA Hard Physical Address (aka MMIO address)
  19. ** IRQ Interrupt ReQuest. Implies Line based interrupt.
  20. ** IRT Interrupt Routing Table (provided by PAT firmware)
  21. ** IRdT Interrupt Redirection Table. IRQ line to TXN ADDR/DATA
  22. ** table which is implemented in I/O SAPIC.
  23. ** ISR Interrupt Service Routine. aka Interrupt handler.
  24. ** MSI Message Signaled Interrupt. PCI 2.2 functionality.
  25. ** aka Transaction Based Interrupt (or TBI).
  26. ** PA Precision Architecture. HP's RISC architecture.
  27. ** RISC Reduced Instruction Set Computer.
  28. **
  29. **
  30. ** What's a Message Signalled Interrupt?
  31. ** -------------------------------------
  32. ** MSI is a write transaction which targets a processor and is similar
  33. ** to a processor write to memory or MMIO. MSIs can be generated by I/O
  34. ** devices as well as processors and require *architecture* to work.
  35. **
  36. ** PA only supports MSI. So I/O subsystems must either natively generate
  37. ** MSIs (e.g. GSC or HP-PB) or convert line based interrupts into MSIs
  38. ** (e.g. PCI and EISA). IA64 supports MSIs via a "local SAPIC" which
  39. ** acts on behalf of a processor.
  40. **
  41. ** MSI allows any I/O device to interrupt any processor. This makes
  42. ** load balancing of the interrupt processing possible on an SMP platform.
  43. ** Interrupts are also ordered WRT to DMA data. It's possible on I/O
  44. ** coherent systems to completely eliminate PIO reads from the interrupt
  45. ** path. The device and driver must be designed and implemented to
  46. ** guarantee all DMA has been issued (issues about atomicity here)
  47. ** before the MSI is issued. I/O status can then safely be read from
  48. ** DMA'd data by the ISR.
  49. **
  50. **
  51. ** PA Firmware
  52. ** -----------
  53. ** PA-RISC platforms have two fundamentally different types of firmware.
  54. ** For PCI devices, "Legacy" PDC initializes the "INTERRUPT_LINE" register
  55. ** and BARs similar to a traditional PC BIOS.
  56. ** The newer "PAT" firmware supports PDC calls which return tables.
  57. ** PAT firmware only initializes the PCI Console and Boot interface.
  58. ** With these tables, the OS can program all other PCI devices.
  59. **
  60. ** One such PAT PDC call returns the "Interrupt Routing Table" (IRT).
  61. ** The IRT maps each PCI slot's INTA-D "output" line to an I/O SAPIC
  62. ** input line. If the IRT is not available, this driver assumes
  63. ** INTERRUPT_LINE register has been programmed by firmware. The latter
  64. ** case also means online addition of PCI cards can NOT be supported
  65. ** even if HW support is present.
  66. **
  67. ** All platforms with PAT firmware to date (Oct 1999) use one Interrupt
  68. ** Routing Table for the entire platform.
  69. **
  70. ** Where's the iosapic?
  71. ** --------------------
  72. ** I/O sapic is part of the "Core Electronics Complex". And on HP platforms
  73. ** it's integrated as part of the PCI bus adapter, "lba". So no bus walk
  74. ** will discover I/O Sapic. I/O Sapic driver learns about each device
  75. ** when lba driver advertises the presence of the I/O sapic by calling
  76. ** iosapic_register().
  77. **
  78. **
  79. ** IRQ handling notes
  80. ** ------------------
  81. ** The IO-SAPIC can indicate to the CPU which interrupt was asserted.
  82. ** So, unlike the GSC-ASIC and Dino, we allocate one CPU interrupt per
  83. ** IO-SAPIC interrupt and call the device driver's handler directly.
  84. ** The IO-SAPIC driver hijacks the CPU interrupt handler so it can
  85. ** issue the End Of Interrupt command to the IO-SAPIC.
  86. **
  87. ** Overview of exported iosapic functions
  88. ** --------------------------------------
  89. ** (caveat: code isn't finished yet - this is just the plan)
  90. **
  91. ** iosapic_init:
  92. ** o initialize globals (lock, etc)
  93. ** o try to read IRT. Presence of IRT determines if this is
  94. ** a PAT platform or not.
  95. **
  96. ** iosapic_register():
  97. ** o create iosapic_info instance data structure
  98. ** o allocate vector_info array for this iosapic
  99. ** o initialize vector_info - read corresponding IRdT?
  100. **
  101. ** iosapic_xlate_pin: (only called by fixup_irq for PAT platform)
  102. ** o intr_pin = read cfg (INTERRUPT_PIN);
  103. ** o if (device under PCI-PCI bridge)
  104. ** translate slot/pin
  105. **
  106. ** iosapic_fixup_irq:
  107. ** o if PAT platform (IRT present)
  108. ** intr_pin = iosapic_xlate_pin(isi,pcidev):
  109. ** intr_line = find IRT entry(isi, PCI_SLOT(pcidev), intr_pin)
  110. ** save IRT entry into vector_info later
  111. ** write cfg INTERRUPT_LINE (with intr_line)?
  112. ** else
  113. ** intr_line = pcidev->irq
  114. ** IRT pointer = NULL
  115. ** endif
  116. ** o locate vector_info (needs: isi, intr_line)
  117. ** o allocate processor "irq" and get txn_addr/data
  118. ** o request_irq(processor_irq, iosapic_interrupt, vector_info,...)
  119. **
  120. ** iosapic_enable_irq:
  121. ** o clear any pending IRQ on that line
  122. ** o enable IRdT - call enable_irq(vector[line]->processor_irq)
  123. ** o write EOI in case line is already asserted.
  124. **
  125. ** iosapic_disable_irq:
  126. ** o disable IRdT - call disable_irq(vector[line]->processor_irq)
  127. */
  128. /* FIXME: determine which include files are really needed */
  129. #include <linux/types.h>
  130. #include <linux/kernel.h>
  131. #include <linux/spinlock.h>
  132. #include <linux/pci.h>
  133. #include <linux/init.h>
  134. #include <linux/slab.h>
  135. #include <linux/interrupt.h>
  136. #include <asm/byteorder.h> /* get in-line asm for swab */
  137. #include <asm/pdc.h>
  138. #include <asm/pdcpat.h>
  139. #include <asm/page.h>
  140. #include <asm/io.h> /* read/write functions */
  141. #ifdef CONFIG_SUPERIO
  142. #include <asm/superio.h>
  143. #endif
  144. #include <asm/ropes.h>
  145. #include "iosapic_private.h"
  146. #define MODULE_NAME "iosapic"
  147. /* "local" compile flags */
  148. #undef PCI_BRIDGE_FUNCS
  149. #undef DEBUG_IOSAPIC
  150. #undef DEBUG_IOSAPIC_IRT
  151. #ifdef DEBUG_IOSAPIC
  152. #define DBG(x...) printk(x)
  153. #else /* DEBUG_IOSAPIC */
  154. #define DBG(x...)
  155. #endif /* DEBUG_IOSAPIC */
  156. #ifdef DEBUG_IOSAPIC_IRT
  157. #define DBG_IRT(x...) printk(x)
  158. #else
  159. #define DBG_IRT(x...)
  160. #endif
  161. #ifdef CONFIG_64BIT
  162. #define COMPARE_IRTE_ADDR(irte, hpa) ((irte)->dest_iosapic_addr == (hpa))
  163. #else
  164. #define COMPARE_IRTE_ADDR(irte, hpa) \
  165. ((irte)->dest_iosapic_addr == ((hpa) | 0xffffffff00000000ULL))
  166. #endif
  167. #define IOSAPIC_REG_SELECT 0x00
  168. #define IOSAPIC_REG_WINDOW 0x10
  169. #define IOSAPIC_REG_EOI 0x40
  170. #define IOSAPIC_REG_VERSION 0x1
  171. #define IOSAPIC_IRDT_ENTRY(idx) (0x10+(idx)*2)
  172. #define IOSAPIC_IRDT_ENTRY_HI(idx) (0x11+(idx)*2)
  173. static inline unsigned int iosapic_read(void __iomem *iosapic, unsigned int reg)
  174. {
  175. writel(reg, iosapic + IOSAPIC_REG_SELECT);
  176. return readl(iosapic + IOSAPIC_REG_WINDOW);
  177. }
  178. static inline void iosapic_write(void __iomem *iosapic, unsigned int reg, u32 val)
  179. {
  180. writel(reg, iosapic + IOSAPIC_REG_SELECT);
  181. writel(val, iosapic + IOSAPIC_REG_WINDOW);
  182. }
  183. #define IOSAPIC_VERSION_MASK 0x000000ff
  184. #define IOSAPIC_VERSION(ver) ((int) (ver & IOSAPIC_VERSION_MASK))
  185. #define IOSAPIC_MAX_ENTRY_MASK 0x00ff0000
  186. #define IOSAPIC_MAX_ENTRY_SHIFT 0x10
  187. #define IOSAPIC_IRDT_MAX_ENTRY(ver) \
  188. (int) (((ver) & IOSAPIC_MAX_ENTRY_MASK) >> IOSAPIC_MAX_ENTRY_SHIFT)
  189. /* bits in the "low" I/O Sapic IRdT entry */
  190. #define IOSAPIC_IRDT_ENABLE 0x10000
  191. #define IOSAPIC_IRDT_PO_LOW 0x02000
  192. #define IOSAPIC_IRDT_LEVEL_TRIG 0x08000
  193. #define IOSAPIC_IRDT_MODE_LPRI 0x00100
  194. /* bits in the "high" I/O Sapic IRdT entry */
  195. #define IOSAPIC_IRDT_ID_EID_SHIFT 0x10
  196. static DEFINE_SPINLOCK(iosapic_lock);
  197. static inline void iosapic_eoi(void __iomem *addr, unsigned int data)
  198. {
  199. __raw_writel(data, addr);
  200. }
  201. /*
  202. ** REVISIT: future platforms may have more than one IRT.
  203. ** If so, the following three fields form a structure which
  204. ** then be linked into a list. Names are chosen to make searching
  205. ** for them easy - not necessarily accurate (eg "cell").
  206. **
  207. ** Alternative: iosapic_info could point to the IRT it's in.
  208. ** iosapic_register() could search a list of IRT's.
  209. */
  210. static struct irt_entry *irt_cell;
  211. static size_t irt_num_entry;
  212. static struct irt_entry *iosapic_alloc_irt(int num_entries)
  213. {
  214. unsigned long a;
  215. /* The IRT needs to be 8-byte aligned for the PDC call.
  216. * Normally kmalloc would guarantee larger alignment, but
  217. * if CONFIG_DEBUG_SLAB is enabled, then we can get only
  218. * 4-byte alignment on 32-bit kernels
  219. */
  220. a = (unsigned long)kmalloc(sizeof(struct irt_entry) * num_entries + 8, GFP_KERNEL);
  221. a = (a + 7UL) & ~7UL;
  222. return (struct irt_entry *)a;
  223. }
  224. /**
  225. * iosapic_load_irt - Fill in the interrupt routing table
  226. * @cell_num: The cell number of the CPU we're currently executing on
  227. * @irt: The address to place the new IRT at
  228. * @return The number of entries found
  229. *
  230. * The "Get PCI INT Routing Table Size" option returns the number of
  231. * entries in the PCI interrupt routing table for the cell specified
  232. * in the cell_number argument. The cell number must be for a cell
  233. * within the caller's protection domain.
  234. *
  235. * The "Get PCI INT Routing Table" option returns, for the cell
  236. * specified in the cell_number argument, the PCI interrupt routing
  237. * table in the caller allocated memory pointed to by mem_addr.
  238. * We assume the IRT only contains entries for I/O SAPIC and
  239. * calculate the size based on the size of I/O sapic entries.
  240. *
  241. * The PCI interrupt routing table entry format is derived from the
  242. * IA64 SAL Specification 2.4. The PCI interrupt routing table defines
  243. * the routing of PCI interrupt signals between the PCI device output
  244. * "pins" and the IO SAPICs' input "lines" (including core I/O PCI
  245. * devices). This table does NOT include information for devices/slots
  246. * behind PCI to PCI bridges. See PCI to PCI Bridge Architecture Spec.
  247. * for the architected method of routing of IRQ's behind PPB's.
  248. */
  249. static int __init
  250. iosapic_load_irt(unsigned long cell_num, struct irt_entry **irt)
  251. {
  252. long status; /* PDC return value status */
  253. struct irt_entry *table; /* start of interrupt routing tbl */
  254. unsigned long num_entries = 0UL;
  255. BUG_ON(!irt);
  256. if (is_pdc_pat()) {
  257. /* Use pat pdc routine to get interrupt routing table size */
  258. DBG("calling get_irt_size (cell %ld)\n", cell_num);
  259. status = pdc_pat_get_irt_size(&num_entries, cell_num);
  260. DBG("get_irt_size: %ld\n", status);
  261. BUG_ON(status != PDC_OK);
  262. BUG_ON(num_entries == 0);
  263. /*
  264. ** allocate memory for interrupt routing table
  265. ** This interface isn't really right. We are assuming
  266. ** the contents of the table are exclusively
  267. ** for I/O sapic devices.
  268. */
  269. table = iosapic_alloc_irt(num_entries);
  270. if (table == NULL) {
  271. printk(KERN_WARNING MODULE_NAME ": read_irt : can "
  272. "not alloc mem for IRT\n");
  273. return 0;
  274. }
  275. /* get PCI INT routing table */
  276. status = pdc_pat_get_irt(table, cell_num);
  277. DBG("pdc_pat_get_irt: %ld\n", status);
  278. WARN_ON(status != PDC_OK);
  279. } else {
  280. /*
  281. ** C3000/J5000 (and similar) platforms with Sprockets PDC
  282. ** will return exactly one IRT for all iosapics.
  283. ** So if we have one, don't need to get it again.
  284. */
  285. if (irt_cell)
  286. return 0;
  287. /* Should be using the Elroy's HPA, but it's ignored anyway */
  288. status = pdc_pci_irt_size(&num_entries, 0);
  289. DBG("pdc_pci_irt_size: %ld\n", status);
  290. if (status != PDC_OK) {
  291. /* Not a "legacy" system with I/O SAPIC either */
  292. return 0;
  293. }
  294. BUG_ON(num_entries == 0);
  295. table = iosapic_alloc_irt(num_entries);
  296. if (!table) {
  297. printk(KERN_WARNING MODULE_NAME ": read_irt : can "
  298. "not alloc mem for IRT\n");
  299. return 0;
  300. }
  301. /* HPA ignored by this call too. */
  302. status = pdc_pci_irt(num_entries, 0, table);
  303. BUG_ON(status != PDC_OK);
  304. }
  305. /* return interrupt table address */
  306. *irt = table;
  307. #ifdef DEBUG_IOSAPIC_IRT
  308. {
  309. struct irt_entry *p = table;
  310. int i;
  311. printk(MODULE_NAME " Interrupt Routing Table (cell %ld)\n", cell_num);
  312. printk(MODULE_NAME " start = 0x%p num_entries %ld entry_size %d\n",
  313. table,
  314. num_entries,
  315. (int) sizeof(struct irt_entry));
  316. for (i = 0 ; i < num_entries ; i++, p++) {
  317. printk(MODULE_NAME " %02x %02x %02x %02x %02x %02x %02x %02x %08x%08x\n",
  318. p->entry_type, p->entry_length, p->interrupt_type,
  319. p->polarity_trigger, p->src_bus_irq_devno, p->src_bus_id,
  320. p->src_seg_id, p->dest_iosapic_intin,
  321. ((u32 *) p)[2],
  322. ((u32 *) p)[3]
  323. );
  324. }
  325. }
  326. #endif /* DEBUG_IOSAPIC_IRT */
  327. return num_entries;
  328. }
  329. void __init iosapic_init(void)
  330. {
  331. unsigned long cell = 0;
  332. DBG("iosapic_init()\n");
  333. #ifdef __LP64__
  334. if (is_pdc_pat()) {
  335. int status;
  336. struct pdc_pat_cell_num cell_info;
  337. status = pdc_pat_cell_get_number(&cell_info);
  338. if (status == PDC_OK) {
  339. cell = cell_info.cell_num;
  340. }
  341. }
  342. #endif
  343. /* get interrupt routing table for this cell */
  344. irt_num_entry = iosapic_load_irt(cell, &irt_cell);
  345. if (irt_num_entry == 0)
  346. irt_cell = NULL; /* old PDC w/o iosapic */
  347. }
  348. /*
  349. ** Return the IRT entry in case we need to look something else up.
  350. */
  351. static struct irt_entry *
  352. irt_find_irqline(struct iosapic_info *isi, u8 slot, u8 intr_pin)
  353. {
  354. struct irt_entry *i = irt_cell;
  355. int cnt; /* track how many entries we've looked at */
  356. u8 irq_devno = (slot << IRT_DEV_SHIFT) | (intr_pin-1);
  357. DBG_IRT("irt_find_irqline() SLOT %d pin %d\n", slot, intr_pin);
  358. for (cnt=0; cnt < irt_num_entry; cnt++, i++) {
  359. /*
  360. ** Validate: entry_type, entry_length, interrupt_type
  361. **
  362. ** Difference between validate vs compare is the former
  363. ** should print debug info and is not expected to "fail"
  364. ** on current platforms.
  365. */
  366. if (i->entry_type != IRT_IOSAPIC_TYPE) {
  367. DBG_IRT(KERN_WARNING MODULE_NAME ":find_irqline(0x%p): skipping entry %d type %d\n", i, cnt, i->entry_type);
  368. continue;
  369. }
  370. if (i->entry_length != IRT_IOSAPIC_LENGTH) {
  371. DBG_IRT(KERN_WARNING MODULE_NAME ":find_irqline(0x%p): skipping entry %d length %d\n", i, cnt, i->entry_length);
  372. continue;
  373. }
  374. if (i->interrupt_type != IRT_VECTORED_INTR) {
  375. DBG_IRT(KERN_WARNING MODULE_NAME ":find_irqline(0x%p): skipping entry %d interrupt_type %d\n", i, cnt, i->interrupt_type);
  376. continue;
  377. }
  378. if (!COMPARE_IRTE_ADDR(i, isi->isi_hpa))
  379. continue;
  380. if ((i->src_bus_irq_devno & IRT_IRQ_DEVNO_MASK) != irq_devno)
  381. continue;
  382. /*
  383. ** Ignore: src_bus_id and rc_seg_id correlate with
  384. ** iosapic_info->isi_hpa on HP platforms.
  385. ** If needed, pass in "PFA" (aka config space addr)
  386. ** instead of slot.
  387. */
  388. /* Found it! */
  389. return i;
  390. }
  391. printk(KERN_WARNING MODULE_NAME ": 0x%lx : no IRT entry for slot %d, pin %d\n",
  392. isi->isi_hpa, slot, intr_pin);
  393. return NULL;
  394. }
  395. /*
  396. ** xlate_pin() supports the skewing of IRQ lines done by subsidiary bridges.
  397. ** Legacy PDC already does this translation for us and stores it in INTR_LINE.
  398. **
  399. ** PAT PDC needs to basically do what legacy PDC does:
  400. ** o read PIN
  401. ** o adjust PIN in case device is "behind" a PPB
  402. ** (eg 4-port 100BT and SCSI/LAN "Combo Card")
  403. ** o convert slot/pin to I/O SAPIC input line.
  404. **
  405. ** HP platforms only support:
  406. ** o one level of skewing for any number of PPBs
  407. ** o only support PCI-PCI Bridges.
  408. */
  409. static struct irt_entry *
  410. iosapic_xlate_pin(struct iosapic_info *isi, struct pci_dev *pcidev)
  411. {
  412. u8 intr_pin, intr_slot;
  413. pci_read_config_byte(pcidev, PCI_INTERRUPT_PIN, &intr_pin);
  414. DBG_IRT("iosapic_xlate_pin(%s) SLOT %d pin %d\n",
  415. pcidev->slot_name, PCI_SLOT(pcidev->devfn), intr_pin);
  416. if (intr_pin == 0) {
  417. /* The device does NOT support/use IRQ lines. */
  418. return NULL;
  419. }
  420. /* Check if pcidev behind a PPB */
  421. if (pcidev->bus->parent) {
  422. /* Convert pcidev INTR_PIN into something we
  423. ** can lookup in the IRT.
  424. */
  425. #ifdef PCI_BRIDGE_FUNCS
  426. /*
  427. ** Proposal #1:
  428. **
  429. ** call implementation specific translation function
  430. ** This is architecturally "cleaner". HP-UX doesn't
  431. ** support other secondary bus types (eg. E/ISA) directly.
  432. ** May be needed for other processor (eg IA64) architectures
  433. ** or by some ambitous soul who wants to watch TV.
  434. */
  435. if (pci_bridge_funcs->xlate_intr_line) {
  436. intr_pin = pci_bridge_funcs->xlate_intr_line(pcidev);
  437. }
  438. #else /* PCI_BRIDGE_FUNCS */
  439. struct pci_bus *p = pcidev->bus;
  440. /*
  441. ** Proposal #2:
  442. ** The "pin" is skewed ((pin + dev - 1) % 4).
  443. **
  444. ** This isn't very clean since I/O SAPIC must assume:
  445. ** - all platforms only have PCI busses.
  446. ** - only PCI-PCI bridge (eg not PCI-EISA, PCI-PCMCIA)
  447. ** - IRQ routing is only skewed once regardless of
  448. ** the number of PPB's between iosapic and device.
  449. ** (Bit3 expansion chassis follows this rule)
  450. **
  451. ** Advantage is it's really easy to implement.
  452. */
  453. intr_pin = pci_swizzle_interrupt_pin(pcidev, intr_pin);
  454. #endif /* PCI_BRIDGE_FUNCS */
  455. /*
  456. * Locate the host slot of the PPB.
  457. */
  458. while (p->parent->parent)
  459. p = p->parent;
  460. intr_slot = PCI_SLOT(p->self->devfn);
  461. } else {
  462. intr_slot = PCI_SLOT(pcidev->devfn);
  463. }
  464. DBG_IRT("iosapic_xlate_pin: bus %d slot %d pin %d\n",
  465. pcidev->bus->busn_res.start, intr_slot, intr_pin);
  466. return irt_find_irqline(isi, intr_slot, intr_pin);
  467. }
  468. static void iosapic_rd_irt_entry(struct vector_info *vi , u32 *dp0, u32 *dp1)
  469. {
  470. struct iosapic_info *isp = vi->iosapic;
  471. u8 idx = vi->irqline;
  472. *dp0 = iosapic_read(isp->addr, IOSAPIC_IRDT_ENTRY(idx));
  473. *dp1 = iosapic_read(isp->addr, IOSAPIC_IRDT_ENTRY_HI(idx));
  474. }
  475. static void iosapic_wr_irt_entry(struct vector_info *vi, u32 dp0, u32 dp1)
  476. {
  477. struct iosapic_info *isp = vi->iosapic;
  478. DBG_IRT("iosapic_wr_irt_entry(): irq %d hpa %lx 0x%x 0x%x\n",
  479. vi->irqline, isp->isi_hpa, dp0, dp1);
  480. iosapic_write(isp->addr, IOSAPIC_IRDT_ENTRY(vi->irqline), dp0);
  481. /* Read the window register to flush the writes down to HW */
  482. dp0 = readl(isp->addr+IOSAPIC_REG_WINDOW);
  483. iosapic_write(isp->addr, IOSAPIC_IRDT_ENTRY_HI(vi->irqline), dp1);
  484. /* Read the window register to flush the writes down to HW */
  485. dp1 = readl(isp->addr+IOSAPIC_REG_WINDOW);
  486. }
  487. /*
  488. ** set_irt prepares the data (dp0, dp1) according to the vector_info
  489. ** and target cpu (id_eid). dp0/dp1 are then used to program I/O SAPIC
  490. ** IRdT for the given "vector" (aka IRQ line).
  491. */
  492. static void
  493. iosapic_set_irt_data( struct vector_info *vi, u32 *dp0, u32 *dp1)
  494. {
  495. u32 mode = 0;
  496. struct irt_entry *p = vi->irte;
  497. if ((p->polarity_trigger & IRT_PO_MASK) == IRT_ACTIVE_LO)
  498. mode |= IOSAPIC_IRDT_PO_LOW;
  499. if (((p->polarity_trigger >> IRT_EL_SHIFT) & IRT_EL_MASK) == IRT_LEVEL_TRIG)
  500. mode |= IOSAPIC_IRDT_LEVEL_TRIG;
  501. /*
  502. ** IA64 REVISIT
  503. ** PA doesn't support EXTINT or LPRIO bits.
  504. */
  505. *dp0 = mode | (u32) vi->txn_data;
  506. /*
  507. ** Extracting id_eid isn't a real clean way of getting it.
  508. ** But the encoding is the same for both PA and IA64 platforms.
  509. */
  510. if (is_pdc_pat()) {
  511. /*
  512. ** PAT PDC just hands it to us "right".
  513. ** txn_addr comes from cpu_data[x].txn_addr.
  514. */
  515. *dp1 = (u32) (vi->txn_addr);
  516. } else {
  517. /*
  518. ** eg if base_addr == 0xfffa0000),
  519. ** we want to get 0xa0ff0000.
  520. **
  521. ** eid 0x0ff00000 -> 0x00ff0000
  522. ** id 0x000ff000 -> 0xff000000
  523. */
  524. *dp1 = (((u32)vi->txn_addr & 0x0ff00000) >> 4) |
  525. (((u32)vi->txn_addr & 0x000ff000) << 12);
  526. }
  527. DBG_IRT("iosapic_set_irt_data(): 0x%x 0x%x\n", *dp0, *dp1);
  528. }
  529. static void iosapic_mask_irq(struct irq_data *d)
  530. {
  531. unsigned long flags;
  532. struct vector_info *vi = irq_data_get_irq_chip_data(d);
  533. u32 d0, d1;
  534. spin_lock_irqsave(&iosapic_lock, flags);
  535. iosapic_rd_irt_entry(vi, &d0, &d1);
  536. d0 |= IOSAPIC_IRDT_ENABLE;
  537. iosapic_wr_irt_entry(vi, d0, d1);
  538. spin_unlock_irqrestore(&iosapic_lock, flags);
  539. }
  540. static void iosapic_unmask_irq(struct irq_data *d)
  541. {
  542. struct vector_info *vi = irq_data_get_irq_chip_data(d);
  543. u32 d0, d1;
  544. /* data is initialized by fixup_irq */
  545. WARN_ON(vi->txn_irq == 0);
  546. iosapic_set_irt_data(vi, &d0, &d1);
  547. iosapic_wr_irt_entry(vi, d0, d1);
  548. #ifdef DEBUG_IOSAPIC_IRT
  549. {
  550. u32 *t = (u32 *) ((ulong) vi->eoi_addr & ~0xffUL);
  551. printk("iosapic_enable_irq(): regs %p", vi->eoi_addr);
  552. for ( ; t < vi->eoi_addr; t++)
  553. printk(" %x", readl(t));
  554. printk("\n");
  555. }
  556. printk("iosapic_enable_irq(): sel ");
  557. {
  558. struct iosapic_info *isp = vi->iosapic;
  559. for (d0=0x10; d0<0x1e; d0++) {
  560. d1 = iosapic_read(isp->addr, d0);
  561. printk(" %x", d1);
  562. }
  563. }
  564. printk("\n");
  565. #endif
  566. /*
  567. * Issuing I/O SAPIC an EOI causes an interrupt IFF IRQ line is
  568. * asserted. IRQ generally should not be asserted when a driver
  569. * enables their IRQ. It can lead to "interesting" race conditions
  570. * in the driver initialization sequence.
  571. */
  572. DBG(KERN_DEBUG "enable_irq(%d): eoi(%p, 0x%x)\n", d->irq,
  573. vi->eoi_addr, vi->eoi_data);
  574. iosapic_eoi(vi->eoi_addr, vi->eoi_data);
  575. }
  576. static void iosapic_eoi_irq(struct irq_data *d)
  577. {
  578. struct vector_info *vi = irq_data_get_irq_chip_data(d);
  579. iosapic_eoi(vi->eoi_addr, vi->eoi_data);
  580. cpu_eoi_irq(d);
  581. }
  582. #ifdef CONFIG_SMP
  583. static int iosapic_set_affinity_irq(struct irq_data *d,
  584. const struct cpumask *dest, bool force)
  585. {
  586. struct vector_info *vi = irq_data_get_irq_chip_data(d);
  587. u32 d0, d1, dummy_d0;
  588. unsigned long flags;
  589. int dest_cpu;
  590. dest_cpu = cpu_check_affinity(d, dest);
  591. if (dest_cpu < 0)
  592. return -1;
  593. cpumask_copy(irq_data_get_affinity_mask(d), cpumask_of(dest_cpu));
  594. vi->txn_addr = txn_affinity_addr(d->irq, dest_cpu);
  595. spin_lock_irqsave(&iosapic_lock, flags);
  596. /* d1 contains the destination CPU, so only want to set that
  597. * entry */
  598. iosapic_rd_irt_entry(vi, &d0, &d1);
  599. iosapic_set_irt_data(vi, &dummy_d0, &d1);
  600. iosapic_wr_irt_entry(vi, d0, d1);
  601. spin_unlock_irqrestore(&iosapic_lock, flags);
  602. return 0;
  603. }
  604. #endif
  605. static struct irq_chip iosapic_interrupt_type = {
  606. .name = "IO-SAPIC-level",
  607. .irq_unmask = iosapic_unmask_irq,
  608. .irq_mask = iosapic_mask_irq,
  609. .irq_ack = cpu_ack_irq,
  610. .irq_eoi = iosapic_eoi_irq,
  611. #ifdef CONFIG_SMP
  612. .irq_set_affinity = iosapic_set_affinity_irq,
  613. #endif
  614. };
  615. int iosapic_fixup_irq(void *isi_obj, struct pci_dev *pcidev)
  616. {
  617. struct iosapic_info *isi = isi_obj;
  618. struct irt_entry *irte = NULL; /* only used if PAT PDC */
  619. struct vector_info *vi;
  620. int isi_line; /* line used by device */
  621. if (!isi) {
  622. printk(KERN_WARNING MODULE_NAME ": hpa not registered for %s\n",
  623. pci_name(pcidev));
  624. return -1;
  625. }
  626. #ifdef CONFIG_SUPERIO
  627. /*
  628. * HACK ALERT! (non-compliant PCI device support)
  629. *
  630. * All SuckyIO interrupts are routed through the PIC's on function 1.
  631. * But SuckyIO OHCI USB controller gets an IRT entry anyway because
  632. * it advertises INT D for INT_PIN. Use that IRT entry to get the
  633. * SuckyIO interrupt routing for PICs on function 1 (*BLEECCHH*).
  634. */
  635. if (is_superio_device(pcidev)) {
  636. /* We must call superio_fixup_irq() to register the pdev */
  637. pcidev->irq = superio_fixup_irq(pcidev);
  638. /* Don't return if need to program the IOSAPIC's IRT... */
  639. if (PCI_FUNC(pcidev->devfn) != SUPERIO_USB_FN)
  640. return pcidev->irq;
  641. }
  642. #endif /* CONFIG_SUPERIO */
  643. /* lookup IRT entry for isi/slot/pin set */
  644. irte = iosapic_xlate_pin(isi, pcidev);
  645. if (!irte) {
  646. printk("iosapic: no IRTE for %s (IRQ not connected?)\n",
  647. pci_name(pcidev));
  648. return -1;
  649. }
  650. DBG_IRT("iosapic_fixup_irq(): irte %p %x %x %x %x %x %x %x %x\n",
  651. irte,
  652. irte->entry_type,
  653. irte->entry_length,
  654. irte->polarity_trigger,
  655. irte->src_bus_irq_devno,
  656. irte->src_bus_id,
  657. irte->src_seg_id,
  658. irte->dest_iosapic_intin,
  659. (u32) irte->dest_iosapic_addr);
  660. isi_line = irte->dest_iosapic_intin;
  661. /* get vector info for this input line */
  662. vi = isi->isi_vector + isi_line;
  663. DBG_IRT("iosapic_fixup_irq: line %d vi 0x%p\n", isi_line, vi);
  664. /* If this IRQ line has already been setup, skip it */
  665. if (vi->irte)
  666. goto out;
  667. vi->irte = irte;
  668. /*
  669. * Allocate processor IRQ
  670. *
  671. * XXX/FIXME The txn_alloc_irq() code and related code should be
  672. * moved to enable_irq(). That way we only allocate processor IRQ
  673. * bits for devices that actually have drivers claiming them.
  674. * Right now we assign an IRQ to every PCI device present,
  675. * regardless of whether it's used or not.
  676. */
  677. vi->txn_irq = txn_alloc_irq(8);
  678. if (vi->txn_irq < 0)
  679. panic("I/O sapic: couldn't get TXN IRQ\n");
  680. /* enable_irq() will use txn_* to program IRdT */
  681. vi->txn_addr = txn_alloc_addr(vi->txn_irq);
  682. vi->txn_data = txn_alloc_data(vi->txn_irq);
  683. vi->eoi_addr = isi->addr + IOSAPIC_REG_EOI;
  684. vi->eoi_data = cpu_to_le32(vi->txn_data);
  685. cpu_claim_irq(vi->txn_irq, &iosapic_interrupt_type, vi);
  686. out:
  687. pcidev->irq = vi->txn_irq;
  688. DBG_IRT("iosapic_fixup_irq() %d:%d %x %x line %d irq %d\n",
  689. PCI_SLOT(pcidev->devfn), PCI_FUNC(pcidev->devfn),
  690. pcidev->vendor, pcidev->device, isi_line, pcidev->irq);
  691. return pcidev->irq;
  692. }
  693. static struct iosapic_info *iosapic_list;
  694. #ifdef CONFIG_64BIT
  695. int iosapic_serial_irq(struct parisc_device *dev)
  696. {
  697. struct iosapic_info *isi;
  698. struct irt_entry *irte;
  699. struct vector_info *vi;
  700. int cnt;
  701. int intin;
  702. intin = (dev->mod_info >> 24) & 15;
  703. /* lookup IRT entry for isi/slot/pin set */
  704. for (cnt = 0; cnt < irt_num_entry; cnt++) {
  705. irte = &irt_cell[cnt];
  706. if (COMPARE_IRTE_ADDR(irte, dev->mod0) &&
  707. irte->dest_iosapic_intin == intin)
  708. break;
  709. }
  710. if (cnt >= irt_num_entry)
  711. return 0; /* no irq found, force polling */
  712. DBG_IRT("iosapic_serial_irq(): irte %p %x %x %x %x %x %x %x %x\n",
  713. irte,
  714. irte->entry_type,
  715. irte->entry_length,
  716. irte->polarity_trigger,
  717. irte->src_bus_irq_devno,
  718. irte->src_bus_id,
  719. irte->src_seg_id,
  720. irte->dest_iosapic_intin,
  721. (u32) irte->dest_iosapic_addr);
  722. /* search for iosapic */
  723. for (isi = iosapic_list; isi; isi = isi->isi_next)
  724. if (isi->isi_hpa == dev->mod0)
  725. break;
  726. if (!isi)
  727. return 0; /* no iosapic found, force polling */
  728. /* get vector info for this input line */
  729. vi = isi->isi_vector + intin;
  730. DBG_IRT("iosapic_serial_irq: line %d vi 0x%p\n", iosapic_intin, vi);
  731. /* If this IRQ line has already been setup, skip it */
  732. if (vi->irte)
  733. goto out;
  734. vi->irte = irte;
  735. /*
  736. * Allocate processor IRQ
  737. *
  738. * XXX/FIXME The txn_alloc_irq() code and related code should be
  739. * moved to enable_irq(). That way we only allocate processor IRQ
  740. * bits for devices that actually have drivers claiming them.
  741. * Right now we assign an IRQ to every PCI device present,
  742. * regardless of whether it's used or not.
  743. */
  744. vi->txn_irq = txn_alloc_irq(8);
  745. if (vi->txn_irq < 0)
  746. panic("I/O sapic: couldn't get TXN IRQ\n");
  747. /* enable_irq() will use txn_* to program IRdT */
  748. vi->txn_addr = txn_alloc_addr(vi->txn_irq);
  749. vi->txn_data = txn_alloc_data(vi->txn_irq);
  750. vi->eoi_addr = isi->addr + IOSAPIC_REG_EOI;
  751. vi->eoi_data = cpu_to_le32(vi->txn_data);
  752. cpu_claim_irq(vi->txn_irq, &iosapic_interrupt_type, vi);
  753. out:
  754. return vi->txn_irq;
  755. }
  756. #endif
  757. /*
  758. ** squirrel away the I/O Sapic Version
  759. */
  760. static unsigned int
  761. iosapic_rd_version(struct iosapic_info *isi)
  762. {
  763. return iosapic_read(isi->addr, IOSAPIC_REG_VERSION);
  764. }
  765. /*
  766. ** iosapic_register() is called by "drivers" with an integrated I/O SAPIC.
  767. ** Caller must be certain they have an I/O SAPIC and know its MMIO address.
  768. **
  769. ** o allocate iosapic_info and add it to the list
  770. ** o read iosapic version and squirrel that away
  771. ** o read size of IRdT.
  772. ** o allocate and initialize isi_vector[]
  773. ** o allocate irq region
  774. */
  775. void *iosapic_register(unsigned long hpa)
  776. {
  777. struct iosapic_info *isi = NULL;
  778. struct irt_entry *irte = irt_cell;
  779. struct vector_info *vip;
  780. int cnt; /* track how many entries we've looked at */
  781. /*
  782. * Astro based platforms can only support PCI OLARD if they implement
  783. * PAT PDC. Legacy PDC omits LBAs with no PCI devices from the IRT.
  784. * Search the IRT and ignore iosapic's which aren't in the IRT.
  785. */
  786. for (cnt=0; cnt < irt_num_entry; cnt++, irte++) {
  787. WARN_ON(IRT_IOSAPIC_TYPE != irte->entry_type);
  788. if (COMPARE_IRTE_ADDR(irte, hpa))
  789. break;
  790. }
  791. if (cnt >= irt_num_entry) {
  792. DBG("iosapic_register() ignoring 0x%lx (NOT FOUND)\n", hpa);
  793. return NULL;
  794. }
  795. isi = kzalloc(sizeof(struct iosapic_info), GFP_KERNEL);
  796. if (!isi) {
  797. BUG();
  798. return NULL;
  799. }
  800. isi->addr = ioremap_nocache(hpa, 4096);
  801. isi->isi_hpa = hpa;
  802. isi->isi_version = iosapic_rd_version(isi);
  803. isi->isi_num_vectors = IOSAPIC_IRDT_MAX_ENTRY(isi->isi_version) + 1;
  804. vip = isi->isi_vector = kcalloc(isi->isi_num_vectors,
  805. sizeof(struct vector_info), GFP_KERNEL);
  806. if (vip == NULL) {
  807. kfree(isi);
  808. return NULL;
  809. }
  810. for (cnt=0; cnt < isi->isi_num_vectors; cnt++, vip++) {
  811. vip->irqline = (unsigned char) cnt;
  812. vip->iosapic = isi;
  813. }
  814. isi->isi_next = iosapic_list;
  815. iosapic_list = isi;
  816. return isi;
  817. }
  818. #ifdef DEBUG_IOSAPIC
  819. static void
  820. iosapic_prt_irt(void *irt, long num_entry)
  821. {
  822. unsigned int i, *irp = (unsigned int *) irt;
  823. printk(KERN_DEBUG MODULE_NAME ": Interrupt Routing Table (%lx entries)\n", num_entry);
  824. for (i=0; i<num_entry; i++, irp += 4) {
  825. printk(KERN_DEBUG "%p : %2d %.8x %.8x %.8x %.8x\n",
  826. irp, i, irp[0], irp[1], irp[2], irp[3]);
  827. }
  828. }
  829. static void
  830. iosapic_prt_vi(struct vector_info *vi)
  831. {
  832. printk(KERN_DEBUG MODULE_NAME ": vector_info[%d] is at %p\n", vi->irqline, vi);
  833. printk(KERN_DEBUG "\t\tstatus: %.4x\n", vi->status);
  834. printk(KERN_DEBUG "\t\ttxn_irq: %d\n", vi->txn_irq);
  835. printk(KERN_DEBUG "\t\ttxn_addr: %lx\n", vi->txn_addr);
  836. printk(KERN_DEBUG "\t\ttxn_data: %lx\n", vi->txn_data);
  837. printk(KERN_DEBUG "\t\teoi_addr: %p\n", vi->eoi_addr);
  838. printk(KERN_DEBUG "\t\teoi_data: %x\n", vi->eoi_data);
  839. }
  840. static void
  841. iosapic_prt_isi(struct iosapic_info *isi)
  842. {
  843. printk(KERN_DEBUG MODULE_NAME ": io_sapic_info at %p\n", isi);
  844. printk(KERN_DEBUG "\t\tisi_hpa: %lx\n", isi->isi_hpa);
  845. printk(KERN_DEBUG "\t\tisi_status: %x\n", isi->isi_status);
  846. printk(KERN_DEBUG "\t\tisi_version: %x\n", isi->isi_version);
  847. printk(KERN_DEBUG "\t\tisi_vector: %p\n", isi->isi_vector);
  848. }
  849. #endif /* DEBUG_IOSAPIC */