msi-octeon.c 12 KB

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