msi-octeon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2005-2009, 2010 Cavium Networks
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/msi.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/interrupt.h>
  13. #include <asm/octeon/octeon.h>
  14. #include <asm/octeon/cvmx-npi-defs.h>
  15. #include <asm/octeon/cvmx-pci-defs.h>
  16. #include <asm/octeon/cvmx-npei-defs.h>
  17. #include <asm/octeon/cvmx-sli-defs.h>
  18. #include <asm/octeon/cvmx-pexp-defs.h>
  19. #include <asm/octeon/pci-octeon.h>
  20. /*
  21. * Each bit in msi_free_irq_bitmask represents a MSI interrupt that is
  22. * in use.
  23. */
  24. static u64 msi_free_irq_bitmask[4];
  25. /*
  26. * Each bit in msi_multiple_irq_bitmask tells that the device using
  27. * this bit in msi_free_irq_bitmask is also using the next bit. This
  28. * is used so we can disable all of the MSI interrupts when a device
  29. * uses multiple.
  30. */
  31. static u64 msi_multiple_irq_bitmask[4];
  32. /*
  33. * This lock controls updates to msi_free_irq_bitmask and
  34. * msi_multiple_irq_bitmask.
  35. */
  36. static DEFINE_SPINLOCK(msi_free_irq_bitmask_lock);
  37. /*
  38. * Number of MSI IRQs used. This variable is set up in
  39. * the module init time.
  40. */
  41. static int msi_irq_size;
  42. /**
  43. * Called when a driver request MSI interrupts instead of the
  44. * legacy INT A-D. This routine will allocate multiple interrupts
  45. * for MSI devices that support them. A device can override this by
  46. * programming the MSI control bits [6:4] before calling
  47. * pci_enable_msi().
  48. *
  49. * @dev: Device requesting MSI interrupts
  50. * @desc: MSI descriptor
  51. *
  52. * Returns 0 on success.
  53. */
  54. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  55. {
  56. struct msi_msg msg;
  57. u16 control;
  58. int configured_private_bits;
  59. int request_private_bits;
  60. int irq = 0;
  61. int irq_step;
  62. u64 search_mask;
  63. int index;
  64. /*
  65. * Read the MSI config to figure out how many IRQs this device
  66. * wants. Most devices only want 1, which will give
  67. * configured_private_bits and request_private_bits equal 0.
  68. */
  69. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  70. /*
  71. * If the number of private bits has been configured then use
  72. * that value instead of the requested number. This gives the
  73. * driver the chance to override the number of interrupts
  74. * before calling pci_enable_msi().
  75. */
  76. configured_private_bits = (control & PCI_MSI_FLAGS_QSIZE) >> 4;
  77. if (configured_private_bits == 0) {
  78. /* Nothing is configured, so use the hardware requested size */
  79. request_private_bits = (control & PCI_MSI_FLAGS_QMASK) >> 1;
  80. } else {
  81. /*
  82. * Use the number of configured bits, assuming the
  83. * driver wanted to override the hardware request
  84. * value.
  85. */
  86. request_private_bits = configured_private_bits;
  87. }
  88. /*
  89. * The PCI 2.3 spec mandates that there are at most 32
  90. * interrupts. If this device asks for more, only give it one.
  91. */
  92. if (request_private_bits > 5)
  93. request_private_bits = 0;
  94. try_only_one:
  95. /*
  96. * The IRQs have to be aligned on a power of two based on the
  97. * number being requested.
  98. */
  99. irq_step = 1 << request_private_bits;
  100. /* Mask with one bit for each IRQ */
  101. search_mask = (1 << irq_step) - 1;
  102. /*
  103. * We're going to search msi_free_irq_bitmask_lock for zero
  104. * bits. This represents an MSI interrupt number that isn't in
  105. * use.
  106. */
  107. spin_lock(&msi_free_irq_bitmask_lock);
  108. for (index = 0; index < msi_irq_size/64; index++) {
  109. for (irq = 0; irq < 64; irq += irq_step) {
  110. if ((msi_free_irq_bitmask[index] & (search_mask << irq)) == 0) {
  111. msi_free_irq_bitmask[index] |= search_mask << irq;
  112. msi_multiple_irq_bitmask[index] |= (search_mask >> 1) << irq;
  113. goto msi_irq_allocated;
  114. }
  115. }
  116. }
  117. msi_irq_allocated:
  118. spin_unlock(&msi_free_irq_bitmask_lock);
  119. /* Make sure the search for available interrupts didn't fail */
  120. if (irq >= 64) {
  121. if (request_private_bits) {
  122. pr_err("arch_setup_msi_irq: Unable to find %d free interrupts, trying just one",
  123. 1 << request_private_bits);
  124. request_private_bits = 0;
  125. goto try_only_one;
  126. } else
  127. panic("arch_setup_msi_irq: Unable to find a free MSI interrupt");
  128. }
  129. /* MSI interrupts start at logical IRQ OCTEON_IRQ_MSI_BIT0 */
  130. irq += index*64;
  131. irq += OCTEON_IRQ_MSI_BIT0;
  132. switch (octeon_dma_bar_type) {
  133. case OCTEON_DMA_BAR_TYPE_SMALL:
  134. /* When not using big bar, Bar 0 is based at 128MB */
  135. msg.address_lo =
  136. ((128ul << 20) + CVMX_PCI_MSI_RCV) & 0xffffffff;
  137. msg.address_hi = ((128ul << 20) + CVMX_PCI_MSI_RCV) >> 32;
  138. break;
  139. case OCTEON_DMA_BAR_TYPE_BIG:
  140. /* When using big bar, Bar 0 is based at 0 */
  141. msg.address_lo = (0 + CVMX_PCI_MSI_RCV) & 0xffffffff;
  142. msg.address_hi = (0 + CVMX_PCI_MSI_RCV) >> 32;
  143. break;
  144. case OCTEON_DMA_BAR_TYPE_PCIE:
  145. /* When using PCIe, Bar 0 is based at 0 */
  146. /* FIXME CVMX_NPEI_MSI_RCV* other than 0? */
  147. msg.address_lo = (0 + CVMX_NPEI_PCIE_MSI_RCV) & 0xffffffff;
  148. msg.address_hi = (0 + CVMX_NPEI_PCIE_MSI_RCV) >> 32;
  149. break;
  150. case OCTEON_DMA_BAR_TYPE_PCIE2:
  151. /* When using PCIe2, Bar 0 is based at 0 */
  152. msg.address_lo = (0 + CVMX_SLI_PCIE_MSI_RCV) & 0xffffffff;
  153. msg.address_hi = (0 + CVMX_SLI_PCIE_MSI_RCV) >> 32;
  154. break;
  155. default:
  156. panic("arch_setup_msi_irq: Invalid octeon_dma_bar_type");
  157. }
  158. msg.data = irq - OCTEON_IRQ_MSI_BIT0;
  159. /* Update the number of IRQs the device has available to it */
  160. control &= ~PCI_MSI_FLAGS_QSIZE;
  161. control |= request_private_bits << 4;
  162. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  163. irq_set_msi_desc(irq, desc);
  164. pci_write_msi_msg(irq, &msg);
  165. return 0;
  166. }
  167. int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  168. {
  169. struct msi_desc *entry;
  170. int ret;
  171. /*
  172. * MSI-X is not supported.
  173. */
  174. if (type == PCI_CAP_ID_MSIX)
  175. return -EINVAL;
  176. /*
  177. * If an architecture wants to support multiple MSI, it needs to
  178. * override arch_setup_msi_irqs()
  179. */
  180. if (type == PCI_CAP_ID_MSI && nvec > 1)
  181. return 1;
  182. list_for_each_entry(entry, &dev->msi_list, list) {
  183. ret = arch_setup_msi_irq(dev, entry);
  184. if (ret < 0)
  185. return ret;
  186. if (ret > 0)
  187. return -ENOSPC;
  188. }
  189. return 0;
  190. }
  191. /**
  192. * Called when a device no longer needs its MSI interrupts. All
  193. * MSI interrupts for the device are freed.
  194. *
  195. * @irq: The devices first irq number. There may be multple in sequence.
  196. */
  197. void arch_teardown_msi_irq(unsigned int irq)
  198. {
  199. int number_irqs;
  200. u64 bitmask;
  201. int index = 0;
  202. int irq0;
  203. if ((irq < OCTEON_IRQ_MSI_BIT0)
  204. || (irq > msi_irq_size + OCTEON_IRQ_MSI_BIT0))
  205. panic("arch_teardown_msi_irq: Attempted to teardown illegal "
  206. "MSI interrupt (%d)", irq);
  207. irq -= OCTEON_IRQ_MSI_BIT0;
  208. index = irq / 64;
  209. irq0 = irq % 64;
  210. /*
  211. * Count the number of IRQs we need to free by looking at the
  212. * msi_multiple_irq_bitmask. Each bit set means that the next
  213. * IRQ is also owned by this device.
  214. */
  215. number_irqs = 0;
  216. while ((irq0 + number_irqs < 64) &&
  217. (msi_multiple_irq_bitmask[index]
  218. & (1ull << (irq0 + number_irqs))))
  219. number_irqs++;
  220. number_irqs++;
  221. /* Mask with one bit for each IRQ */
  222. bitmask = (1 << number_irqs) - 1;
  223. /* Shift the mask to the correct bit location */
  224. bitmask <<= irq0;
  225. if ((msi_free_irq_bitmask[index] & bitmask) != bitmask)
  226. panic("arch_teardown_msi_irq: Attempted to teardown MSI "
  227. "interrupt (%d) not in use", irq);
  228. /* Checks are done, update the in use bitmask */
  229. spin_lock(&msi_free_irq_bitmask_lock);
  230. msi_free_irq_bitmask[index] &= ~bitmask;
  231. msi_multiple_irq_bitmask[index] &= ~bitmask;
  232. spin_unlock(&msi_free_irq_bitmask_lock);
  233. }
  234. static DEFINE_RAW_SPINLOCK(octeon_irq_msi_lock);
  235. static u64 msi_rcv_reg[4];
  236. static u64 mis_ena_reg[4];
  237. static void octeon_irq_msi_enable_pcie(struct irq_data *data)
  238. {
  239. u64 en;
  240. unsigned long flags;
  241. int msi_number = data->irq - OCTEON_IRQ_MSI_BIT0;
  242. int irq_index = msi_number >> 6;
  243. int irq_bit = msi_number & 0x3f;
  244. raw_spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  245. en = cvmx_read_csr(mis_ena_reg[irq_index]);
  246. en |= 1ull << irq_bit;
  247. cvmx_write_csr(mis_ena_reg[irq_index], en);
  248. cvmx_read_csr(mis_ena_reg[irq_index]);
  249. raw_spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  250. }
  251. static void octeon_irq_msi_disable_pcie(struct irq_data *data)
  252. {
  253. u64 en;
  254. unsigned long flags;
  255. int msi_number = data->irq - OCTEON_IRQ_MSI_BIT0;
  256. int irq_index = msi_number >> 6;
  257. int irq_bit = msi_number & 0x3f;
  258. raw_spin_lock_irqsave(&octeon_irq_msi_lock, flags);
  259. en = cvmx_read_csr(mis_ena_reg[irq_index]);
  260. en &= ~(1ull << irq_bit);
  261. cvmx_write_csr(mis_ena_reg[irq_index], en);
  262. cvmx_read_csr(mis_ena_reg[irq_index]);
  263. raw_spin_unlock_irqrestore(&octeon_irq_msi_lock, flags);
  264. }
  265. static struct irq_chip octeon_irq_chip_msi_pcie = {
  266. .name = "MSI",
  267. .irq_enable = octeon_irq_msi_enable_pcie,
  268. .irq_disable = octeon_irq_msi_disable_pcie,
  269. };
  270. static void octeon_irq_msi_enable_pci(struct irq_data *data)
  271. {
  272. /*
  273. * Octeon PCI doesn't have the ability to mask/unmask MSI
  274. * interrupts individually. Instead of masking/unmasking them
  275. * in groups of 16, we simple assume MSI devices are well
  276. * behaved. MSI interrupts are always enable and the ACK is
  277. * assumed to be enough
  278. */
  279. }
  280. static void octeon_irq_msi_disable_pci(struct irq_data *data)
  281. {
  282. /* See comment in enable */
  283. }
  284. static struct irq_chip octeon_irq_chip_msi_pci = {
  285. .name = "MSI",
  286. .irq_enable = octeon_irq_msi_enable_pci,
  287. .irq_disable = octeon_irq_msi_disable_pci,
  288. };
  289. /*
  290. * Called by the interrupt handling code when an MSI interrupt
  291. * occurs.
  292. */
  293. static irqreturn_t __octeon_msi_do_interrupt(int index, u64 msi_bits)
  294. {
  295. int irq;
  296. int bit;
  297. bit = fls64(msi_bits);
  298. if (bit) {
  299. bit--;
  300. /* Acknowledge it first. */
  301. cvmx_write_csr(msi_rcv_reg[index], 1ull << bit);
  302. irq = bit + OCTEON_IRQ_MSI_BIT0 + 64 * index;
  303. do_IRQ(irq);
  304. return IRQ_HANDLED;
  305. }
  306. return IRQ_NONE;
  307. }
  308. #define OCTEON_MSI_INT_HANDLER_X(x) \
  309. static irqreturn_t octeon_msi_interrupt##x(int cpl, void *dev_id) \
  310. { \
  311. u64 msi_bits = cvmx_read_csr(msi_rcv_reg[(x)]); \
  312. return __octeon_msi_do_interrupt((x), msi_bits); \
  313. }
  314. /*
  315. * Create octeon_msi_interrupt{0-3} function body
  316. */
  317. OCTEON_MSI_INT_HANDLER_X(0);
  318. OCTEON_MSI_INT_HANDLER_X(1);
  319. OCTEON_MSI_INT_HANDLER_X(2);
  320. OCTEON_MSI_INT_HANDLER_X(3);
  321. /*
  322. * Initializes the MSI interrupt handling code
  323. */
  324. int __init octeon_msi_initialize(void)
  325. {
  326. int irq;
  327. struct irq_chip *msi;
  328. if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_PCIE) {
  329. msi_rcv_reg[0] = CVMX_PEXP_NPEI_MSI_RCV0;
  330. msi_rcv_reg[1] = CVMX_PEXP_NPEI_MSI_RCV1;
  331. msi_rcv_reg[2] = CVMX_PEXP_NPEI_MSI_RCV2;
  332. msi_rcv_reg[3] = CVMX_PEXP_NPEI_MSI_RCV3;
  333. mis_ena_reg[0] = CVMX_PEXP_NPEI_MSI_ENB0;
  334. mis_ena_reg[1] = CVMX_PEXP_NPEI_MSI_ENB1;
  335. mis_ena_reg[2] = CVMX_PEXP_NPEI_MSI_ENB2;
  336. mis_ena_reg[3] = CVMX_PEXP_NPEI_MSI_ENB3;
  337. msi = &octeon_irq_chip_msi_pcie;
  338. } else {
  339. msi_rcv_reg[0] = CVMX_NPI_NPI_MSI_RCV;
  340. #define INVALID_GENERATE_ADE 0x8700000000000000ULL;
  341. msi_rcv_reg[1] = INVALID_GENERATE_ADE;
  342. msi_rcv_reg[2] = INVALID_GENERATE_ADE;
  343. msi_rcv_reg[3] = INVALID_GENERATE_ADE;
  344. mis_ena_reg[0] = INVALID_GENERATE_ADE;
  345. mis_ena_reg[1] = INVALID_GENERATE_ADE;
  346. mis_ena_reg[2] = INVALID_GENERATE_ADE;
  347. mis_ena_reg[3] = INVALID_GENERATE_ADE;
  348. msi = &octeon_irq_chip_msi_pci;
  349. }
  350. for (irq = OCTEON_IRQ_MSI_BIT0; irq <= OCTEON_IRQ_MSI_LAST; irq++)
  351. irq_set_chip_and_handler(irq, msi, handle_simple_irq);
  352. if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
  353. if (request_irq(OCTEON_IRQ_PCI_MSI0, octeon_msi_interrupt0,
  354. 0, "MSI[0:63]", octeon_msi_interrupt0))
  355. panic("request_irq(OCTEON_IRQ_PCI_MSI0) failed");
  356. if (request_irq(OCTEON_IRQ_PCI_MSI1, octeon_msi_interrupt1,
  357. 0, "MSI[64:127]", octeon_msi_interrupt1))
  358. panic("request_irq(OCTEON_IRQ_PCI_MSI1) failed");
  359. if (request_irq(OCTEON_IRQ_PCI_MSI2, octeon_msi_interrupt2,
  360. 0, "MSI[127:191]", octeon_msi_interrupt2))
  361. panic("request_irq(OCTEON_IRQ_PCI_MSI2) failed");
  362. if (request_irq(OCTEON_IRQ_PCI_MSI3, octeon_msi_interrupt3,
  363. 0, "MSI[192:255]", octeon_msi_interrupt3))
  364. panic("request_irq(OCTEON_IRQ_PCI_MSI3) failed");
  365. msi_irq_size = 256;
  366. } else if (octeon_is_pci_host()) {
  367. if (request_irq(OCTEON_IRQ_PCI_MSI0, octeon_msi_interrupt0,
  368. 0, "MSI[0:15]", octeon_msi_interrupt0))
  369. panic("request_irq(OCTEON_IRQ_PCI_MSI0) failed");
  370. if (request_irq(OCTEON_IRQ_PCI_MSI1, octeon_msi_interrupt0,
  371. 0, "MSI[16:31]", octeon_msi_interrupt0))
  372. panic("request_irq(OCTEON_IRQ_PCI_MSI1) failed");
  373. if (request_irq(OCTEON_IRQ_PCI_MSI2, octeon_msi_interrupt0,
  374. 0, "MSI[32:47]", octeon_msi_interrupt0))
  375. panic("request_irq(OCTEON_IRQ_PCI_MSI2) failed");
  376. if (request_irq(OCTEON_IRQ_PCI_MSI3, octeon_msi_interrupt0,
  377. 0, "MSI[48:63]", octeon_msi_interrupt0))
  378. panic("request_irq(OCTEON_IRQ_PCI_MSI3) failed");
  379. msi_irq_size = 64;
  380. }
  381. return 0;
  382. }
  383. subsys_initcall(octeon_msi_initialize);