iosapic.c 28 KB

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